FIRIAMMessageContentDataWithImageURL.m 8.5 KB

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