FIRIAMMessageContentDataWithImageURL.m 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*
  2. * Copyright 2017 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import <TargetConditionals.h>
  17. #if TARGET_OS_IOS || TARGET_OS_TV
  18. #import "FirebaseCore/Sources/Private/FirebaseCoreInternal.h"
  19. #import "FirebaseInAppMessaging/Sources/FIRCore+InAppMessaging.h"
  20. #import "FirebaseInAppMessaging/Sources/Private/Data/FIRIAMMessageContentData.h"
  21. #import "FirebaseInAppMessaging/Sources/Private/Data/FIRIAMMessageContentDataWithImageURL.h"
  22. #import "FirebaseInAppMessaging/Sources/Runtime/FIRIAMSDKRuntimeErrorCodes.h"
  23. static NSInteger const SuccessHTTPStatusCode = 200;
  24. @interface FIRIAMMessageContentDataWithImageURL ()
  25. @property(nonatomic, readwrite, nonnull, copy) NSString *titleText;
  26. @property(nonatomic, readwrite, nonnull, copy) NSString *bodyText;
  27. @property(nonatomic, copy, nullable) NSString *actionButtonText;
  28. @property(nonatomic, copy, nullable) NSString *secondaryActionButtonText;
  29. @property(nonatomic, copy, nullable) NSURL *actionURL;
  30. @property(nonatomic, copy, nullable) NSURL *secondaryActionURL;
  31. @property(nonatomic, nullable, copy) NSURL *imageURL;
  32. @property(nonatomic, nullable, copy) NSURL *landscapeImageURL;
  33. @property(readonly) NSURLSession *URLSession;
  34. @end
  35. @implementation FIRIAMMessageContentDataWithImageURL
  36. - (instancetype)initWithMessageTitle:(NSString *)title
  37. messageBody:(NSString *)body
  38. actionButtonText:(nullable NSString *)actionButtonText
  39. secondaryActionButtonText:(nullable NSString *)secondaryActionButtonText
  40. actionURL:(nullable NSURL *)actionURL
  41. secondaryActionURL:(nullable NSURL *)secondaryActionURL
  42. imageURL:(nullable NSURL *)imageURL
  43. landscapeImageURL:(nullable NSURL *)landscapeImageURL
  44. usingURLSession:(nullable NSURLSession *)URLSession {
  45. if (self = [super init]) {
  46. _titleText = title;
  47. _bodyText = body;
  48. _imageURL = imageURL;
  49. _landscapeImageURL = landscapeImageURL;
  50. _actionButtonText = actionButtonText;
  51. _secondaryActionButtonText = secondaryActionButtonText;
  52. _actionURL = actionURL;
  53. _secondaryActionURL = secondaryActionURL;
  54. if (imageURL) {
  55. _URLSession = URLSession ? URLSession : [NSURLSession sharedSession];
  56. }
  57. }
  58. return self;
  59. }
  60. #pragma protocol FIRIAMMessageContentData
  61. - (NSString *)description {
  62. return [NSString stringWithFormat:@"Message content: title '%@',"
  63. "body '%@', imageURL '%@', action URL '%@'",
  64. self.titleText, self.bodyText, self.imageURL, self.actionURL];
  65. }
  66. - (NSString *)getTitleText {
  67. return _titleText;
  68. }
  69. - (NSString *)getBodyText {
  70. return _bodyText;
  71. }
  72. - (nullable NSString *)getActionButtonText {
  73. return _actionButtonText;
  74. }
  75. - (void)loadImageDataWithBlock:(void (^)(NSData *_Nullable standardImageData,
  76. NSData *_Nullable landscapeImageData,
  77. NSError *_Nullable error))block {
  78. if (!block) {
  79. // no need for any further action if block is nil
  80. return;
  81. }
  82. if (!_imageURL && !_landscapeImageURL) {
  83. // no image data since image url is nil
  84. block(nil, nil, nil);
  85. } else if (!_landscapeImageURL) {
  86. // Only fetch standard image.
  87. [self fetchImageFromURL:_imageURL
  88. withBlock:^(NSData *_Nullable imageData, NSError *_Nullable error) {
  89. block(imageData, nil, error);
  90. }];
  91. } else if (!_imageURL) {
  92. // Only fetch portrait image.
  93. [self fetchImageFromURL:_landscapeImageURL
  94. withBlock:^(NSData *_Nullable imageData, NSError *_Nullable error) {
  95. block(nil, imageData, error);
  96. }];
  97. } else {
  98. // Fetch both images separately, call completion when they're both fetched.
  99. __block NSData *portrait = nil;
  100. __block NSData *landscape = nil;
  101. __block NSError *landscapeImageLoadError = nil;
  102. [self fetchImageFromURL:_imageURL
  103. withBlock:^(NSData *_Nullable imageData, NSError *_Nullable error) {
  104. __weak FIRIAMMessageContentDataWithImageURL *weakSelf = self;
  105. // If the portrait image fails to load, we treat this as a failure.
  106. if (error) {
  107. // Cancel landscape image fetch.
  108. [weakSelf.URLSession invalidateAndCancel];
  109. block(nil, nil, error);
  110. return;
  111. }
  112. portrait = imageData;
  113. if (landscape || landscapeImageLoadError) {
  114. block(portrait, landscape, nil);
  115. }
  116. }];
  117. [self fetchImageFromURL:_landscapeImageURL
  118. withBlock:^(NSData *_Nullable imageData, NSError *_Nullable error) {
  119. if (error) {
  120. landscapeImageLoadError = error;
  121. } else {
  122. landscape = imageData;
  123. }
  124. if (portrait) {
  125. block(portrait, landscape, nil);
  126. }
  127. }];
  128. }
  129. }
  130. - (void)fetchImageFromURL:(NSURL *)imageURL
  131. withBlock:(void (^)(NSData *_Nullable imageData, NSError *_Nullable error))block {
  132. NSURLRequest *imageDataRequest = [NSURLRequest requestWithURL:imageURL];
  133. NSURLSessionDataTask *task = [_URLSession
  134. dataTaskWithRequest:imageDataRequest
  135. completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
  136. if (error) {
  137. FIRLogWarning(kFIRLoggerInAppMessaging, @"I-IAM000003", @"Error in fetching image: %@",
  138. error);
  139. block(nil, error);
  140. } else {
  141. if ([response isKindOfClass:[NSHTTPURLResponse class]]) {
  142. NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
  143. if (httpResponse.statusCode == SuccessHTTPStatusCode) {
  144. if (httpResponse.MIMEType == nil || ![httpResponse.MIMEType hasPrefix:@"image"]) {
  145. NSString *errorDesc =
  146. [NSString stringWithFormat:@"No image MIME type %@"
  147. " detected for URL %@",
  148. httpResponse.MIMEType, self.imageURL];
  149. FIRLogWarning(kFIRLoggerInAppMessaging, @"I-IAM000004", @"%@", errorDesc);
  150. NSError *error =
  151. [NSError errorWithDomain:kFirebaseInAppMessagingErrorDomain
  152. code:FIRIAMSDKRuntimeErrorNonImageMimetypeFromImageURL
  153. userInfo:@{NSLocalizedDescriptionKey : errorDesc}];
  154. block(nil, error);
  155. } else {
  156. block(data, nil);
  157. }
  158. } else {
  159. NSString *errorDesc =
  160. [NSString stringWithFormat:@"Failed HTTP request to crawl image %@: "
  161. "HTTP status code as %ld",
  162. self->_imageURL, (long)httpResponse.statusCode];
  163. FIRLogWarning(kFIRLoggerInAppMessaging, @"I-IAM000001", @"%@", errorDesc);
  164. NSError *error = [NSError errorWithDomain:NSURLErrorDomain
  165. code:httpResponse.statusCode
  166. userInfo:@{NSLocalizedDescriptionKey : errorDesc}];
  167. block(nil, error);
  168. }
  169. } else {
  170. NSString *errorDesc =
  171. [NSString stringWithFormat:@"Internal error: got a non HTTP response from "
  172. @"fetching image for image URL as %@",
  173. imageURL];
  174. FIRLogWarning(kFIRLoggerInAppMessaging, @"I-IAM000002", @"%@", errorDesc);
  175. NSError *error = [NSError errorWithDomain:NSURLErrorDomain
  176. code:FIRIAMSDKRuntimeErrorNonHTTPResponseForImage
  177. userInfo:@{NSLocalizedDescriptionKey : errorDesc}];
  178. block(nil, error);
  179. }
  180. }
  181. }];
  182. [task resume];
  183. }
  184. @end
  185. #endif // TARGET_OS_IOS || TARGET_OS_TV