RCTNetworking.h 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. * Copyright (c) Facebook, Inc. and its affiliates.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. */
  7. #import <React/RCTEventEmitter.h>
  8. #import <React/RCTNetworkTask.h>
  9. #import <React/RCTURLRequestHandler.h>
  10. @protocol RCTNetworkingRequestHandler <NSObject>
  11. // @lint-ignore FBOBJCUNTYPEDCOLLECTION1
  12. - (BOOL)canHandleNetworkingRequest:(NSDictionary *)data;
  13. // @lint-ignore FBOBJCUNTYPEDCOLLECTION1
  14. - (NSDictionary *)handleNetworkingRequest:(NSDictionary *)data;
  15. @end
  16. @protocol RCTNetworkingResponseHandler <NSObject>
  17. - (BOOL)canHandleNetworkingResponse:(NSString *)responseType;
  18. - (id)handleNetworkingResponse:(NSURLResponse *)response data:(NSData *)data;
  19. @end
  20. @interface RCTNetworking : RCTEventEmitter
  21. /**
  22. * Allows RCTNetworking instances to be initialized with handlers.
  23. * The handlers will be requested via the bridge's moduleForName method when required.
  24. */
  25. - (instancetype)initWithHandlersProvider:(NSArray<id<RCTURLRequestHandler>> * (^)(void))getHandlers;
  26. /**
  27. * Does a handler exist for the specified request?
  28. */
  29. - (BOOL)canHandleRequest:(NSURLRequest *)request;
  30. /**
  31. * Return an RCTNetworkTask for the specified request. This is useful for
  32. * invoking the React Native networking stack from within native code.
  33. */
  34. - (RCTNetworkTask *)networkTaskWithRequest:(NSURLRequest *)request
  35. completionBlock:(RCTURLRequestCompletionBlock)completionBlock;
  36. - (void)addRequestHandler:(id<RCTNetworkingRequestHandler>)handler;
  37. - (void)addResponseHandler:(id<RCTNetworkingResponseHandler>)handler;
  38. - (void)removeRequestHandler:(id<RCTNetworkingRequestHandler>)handler;
  39. - (void)removeResponseHandler:(id<RCTNetworkingResponseHandler>)handler;
  40. @end
  41. @interface RCTBridge (RCTNetworking)
  42. @property (nonatomic, readonly) RCTNetworking *networking;
  43. @end
  44. // HACK: When uploading images/video from PHAssetLibrary, we change the URL scheme to be
  45. // ph-upload://. This is to ensure that we upload a full video when given a ph-upload:// URL,
  46. // instead of just the thumbnail. Consider the following problem:
  47. // The user has a video in their camera roll with URL ph://1B3E2DDB-0AD3-4E33-A7A1-9F4AA9A762AA/L0/001
  48. // 1. We want to display that video in an <Image> and show the thumbnail
  49. // 2. We later want to upload that video.
  50. // At this point, if we use the same URL for both uses, there is no way to distinguish the intent
  51. // and we will either upload the thumbnail (bad!) or try to show the video in an <Image> (bad!).
  52. // Our solution is to change the URL scheme in the uploader.
  53. extern NSString *const RCTNetworkingPHUploadHackScheme;