FIRIAMMessageContentDataWithImageURL.m 8.5 KB

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