@revopush/react-native-code-push 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (174) hide show
  1. package/.azurepipelines/build-rn-code-push-1es.yml +104 -0
  2. package/.azurepipelines/test-rn-code-push.yml +94 -0
  3. package/.config/CredScanSuppressions.json +14 -0
  4. package/.node-version +1 -0
  5. package/AlertAdapter.js +24 -0
  6. package/CONTRIBUTING.md +134 -0
  7. package/CodePush.js +671 -0
  8. package/CodePush.podspec +28 -0
  9. package/LICENSE.md +13 -0
  10. package/README.md +413 -0
  11. package/SECURITY.md +41 -0
  12. package/android/app/build.gradle +48 -0
  13. package/android/app/proguard-rules.pro +25 -0
  14. package/android/app/src/main/AndroidManifest.xml +5 -0
  15. package/android/app/src/main/java/com/microsoft/codepush/react/CodePush.java +444 -0
  16. package/android/app/src/main/java/com/microsoft/codepush/react/CodePushConstants.java +35 -0
  17. package/android/app/src/main/java/com/microsoft/codepush/react/CodePushDialog.java +102 -0
  18. package/android/app/src/main/java/com/microsoft/codepush/react/CodePushInstallMode.java +16 -0
  19. package/android/app/src/main/java/com/microsoft/codepush/react/CodePushInvalidPublicKeyException.java +12 -0
  20. package/android/app/src/main/java/com/microsoft/codepush/react/CodePushInvalidUpdateException.java +7 -0
  21. package/android/app/src/main/java/com/microsoft/codepush/react/CodePushMalformedDataException.java +12 -0
  22. package/android/app/src/main/java/com/microsoft/codepush/react/CodePushNativeModule.java +848 -0
  23. package/android/app/src/main/java/com/microsoft/codepush/react/CodePushNotInitializedException.java +12 -0
  24. package/android/app/src/main/java/com/microsoft/codepush/react/CodePushTelemetryManager.java +175 -0
  25. package/android/app/src/main/java/com/microsoft/codepush/react/CodePushUnknownException.java +12 -0
  26. package/android/app/src/main/java/com/microsoft/codepush/react/CodePushUpdateManager.java +383 -0
  27. package/android/app/src/main/java/com/microsoft/codepush/react/CodePushUpdateState.java +15 -0
  28. package/android/app/src/main/java/com/microsoft/codepush/react/CodePushUpdateUtils.java +275 -0
  29. package/android/app/src/main/java/com/microsoft/codepush/react/CodePushUtils.java +238 -0
  30. package/android/app/src/main/java/com/microsoft/codepush/react/DownloadProgress.java +30 -0
  31. package/android/app/src/main/java/com/microsoft/codepush/react/DownloadProgressCallback.java +5 -0
  32. package/android/app/src/main/java/com/microsoft/codepush/react/FileUtils.java +203 -0
  33. package/android/app/src/main/java/com/microsoft/codepush/react/ReactHostHolder.java +11 -0
  34. package/android/app/src/main/java/com/microsoft/codepush/react/ReactInstanceHolder.java +17 -0
  35. package/android/app/src/main/java/com/microsoft/codepush/react/SettingsManager.java +173 -0
  36. package/android/app/src/main/java/com/microsoft/codepush/react/TLSSocketFactory.java +72 -0
  37. package/android/build.gradle +24 -0
  38. package/android/codepush.gradle +162 -0
  39. package/android/gradle/wrapper/gradle-wrapper.jar +0 -0
  40. package/android/gradle/wrapper/gradle-wrapper.properties +5 -0
  41. package/android/gradle.properties +20 -0
  42. package/android/gradlew +164 -0
  43. package/android/gradlew.bat +90 -0
  44. package/android/settings.gradle +1 -0
  45. package/docs/api-android.md +52 -0
  46. package/docs/api-ios.md +31 -0
  47. package/docs/api-js.md +592 -0
  48. package/docs/multi-deployment-testing-android.md +55 -0
  49. package/docs/multi-deployment-testing-ios.md +59 -0
  50. package/docs/setup-android.md +92 -0
  51. package/docs/setup-ios.md +137 -0
  52. package/ios/CodePush/Base64/Base64/MF_Base64Additions.h +34 -0
  53. package/ios/CodePush/Base64/Base64/MF_Base64Additions.m +252 -0
  54. package/ios/CodePush/Base64/README.md +47 -0
  55. package/ios/CodePush/CodePush.h +235 -0
  56. package/ios/CodePush/CodePush.m +1122 -0
  57. package/ios/CodePush/CodePushConfig.m +116 -0
  58. package/ios/CodePush/CodePushDownloadHandler.m +130 -0
  59. package/ios/CodePush/CodePushErrorUtils.m +20 -0
  60. package/ios/CodePush/CodePushPackage.m +602 -0
  61. package/ios/CodePush/CodePushTelemetryManager.m +175 -0
  62. package/ios/CodePush/CodePushUpdateUtils.m +376 -0
  63. package/ios/CodePush/CodePushUtils.m +9 -0
  64. package/ios/CodePush/JWT/Core/Algorithms/Base/JWTAlgorithm.h +69 -0
  65. package/ios/CodePush/JWT/Core/Algorithms/Base/JWTAlgorithmFactory.h +16 -0
  66. package/ios/CodePush/JWT/Core/Algorithms/Base/JWTAlgorithmFactory.m +51 -0
  67. package/ios/CodePush/JWT/Core/Algorithms/Base/JWTAlgorithmNone.h +15 -0
  68. package/ios/CodePush/JWT/Core/Algorithms/Base/JWTAlgorithmNone.m +55 -0
  69. package/ios/CodePush/JWT/Core/Algorithms/ESFamily/JWTAlgorithmESBase.h +24 -0
  70. package/ios/CodePush/JWT/Core/Algorithms/ESFamily/JWTAlgorithmESBase.m +41 -0
  71. package/ios/CodePush/JWT/Core/Algorithms/HSFamily/JWTAlgorithmHSBase.h +28 -0
  72. package/ios/CodePush/JWT/Core/Algorithms/HSFamily/JWTAlgorithmHSBase.m +205 -0
  73. package/ios/CodePush/JWT/Core/Algorithms/Holders/JWTAlgorithmDataHolder.h +103 -0
  74. package/ios/CodePush/JWT/Core/Algorithms/Holders/JWTAlgorithmDataHolder.m +322 -0
  75. package/ios/CodePush/JWT/Core/Algorithms/Holders/JWTAlgorithmDataHolderChain.h +37 -0
  76. package/ios/CodePush/JWT/Core/Algorithms/Holders/JWTAlgorithmDataHolderChain.m +145 -0
  77. package/ios/CodePush/JWT/Core/Algorithms/RSFamily/JWTAlgorithmRSBase.h +35 -0
  78. package/ios/CodePush/JWT/Core/Algorithms/RSFamily/JWTAlgorithmRSBase.m +551 -0
  79. package/ios/CodePush/JWT/Core/Algorithms/RSFamily/JWTRSAlgorithm.h +23 -0
  80. package/ios/CodePush/JWT/Core/Algorithms/RSFamily/RSKeys/JWTCryptoKey.h +43 -0
  81. package/ios/CodePush/JWT/Core/Algorithms/RSFamily/RSKeys/JWTCryptoKey.m +230 -0
  82. package/ios/CodePush/JWT/Core/Algorithms/RSFamily/RSKeys/JWTCryptoKeyExtractor.h +31 -0
  83. package/ios/CodePush/JWT/Core/Algorithms/RSFamily/RSKeys/JWTCryptoKeyExtractor.m +113 -0
  84. package/ios/CodePush/JWT/Core/Algorithms/RSFamily/RSKeys/JWTCryptoSecurity.h +38 -0
  85. package/ios/CodePush/JWT/Core/Algorithms/RSFamily/RSKeys/JWTCryptoSecurity.m +500 -0
  86. package/ios/CodePush/JWT/Core/ClaimSet/JWTClaim.h +18 -0
  87. package/ios/CodePush/JWT/Core/ClaimSet/JWTClaim.m +214 -0
  88. package/ios/CodePush/JWT/Core/ClaimSet/JWTClaimsSet.h +23 -0
  89. package/ios/CodePush/JWT/Core/ClaimSet/JWTClaimsSet.m +29 -0
  90. package/ios/CodePush/JWT/Core/ClaimSet/JWTClaimsSetSerializer.h +19 -0
  91. package/ios/CodePush/JWT/Core/ClaimSet/JWTClaimsSetSerializer.m +68 -0
  92. package/ios/CodePush/JWT/Core/ClaimSet/JWTClaimsSetVerifier.h +18 -0
  93. package/ios/CodePush/JWT/Core/ClaimSet/JWTClaimsSetVerifier.m +72 -0
  94. package/ios/CodePush/JWT/Core/Coding/JWTCoding+ResultTypes.h +67 -0
  95. package/ios/CodePush/JWT/Core/Coding/JWTCoding+ResultTypes.m +111 -0
  96. package/ios/CodePush/JWT/Core/Coding/JWTCoding+VersionOne.h +119 -0
  97. package/ios/CodePush/JWT/Core/Coding/JWTCoding+VersionOne.m +307 -0
  98. package/ios/CodePush/JWT/Core/Coding/JWTCoding+VersionThree.h +94 -0
  99. package/ios/CodePush/JWT/Core/Coding/JWTCoding+VersionThree.m +619 -0
  100. package/ios/CodePush/JWT/Core/Coding/JWTCoding+VersionTwo.h +164 -0
  101. package/ios/CodePush/JWT/Core/Coding/JWTCoding+VersionTwo.m +514 -0
  102. package/ios/CodePush/JWT/Core/Coding/JWTCoding.h +24 -0
  103. package/ios/CodePush/JWT/Core/Coding/JWTCoding.m +11 -0
  104. package/ios/CodePush/JWT/Core/FrameworkSupplement/JWT.h +52 -0
  105. package/ios/CodePush/JWT/Core/FrameworkSupplement/Map.modulemap +5 -0
  106. package/ios/CodePush/JWT/Core/Supplement/JWTBase64Coder.h +28 -0
  107. package/ios/CodePush/JWT/Core/Supplement/JWTBase64Coder.m +70 -0
  108. package/ios/CodePush/JWT/Core/Supplement/JWTDeprecations.h +22 -0
  109. package/ios/CodePush/JWT/Core/Supplement/JWTErrorDescription.h +34 -0
  110. package/ios/CodePush/JWT/Core/Supplement/JWTErrorDescription.m +73 -0
  111. package/ios/CodePush/JWT/LICENSE +19 -0
  112. package/ios/CodePush/JWT/README.md +489 -0
  113. package/ios/CodePush/RCTConvert+CodePushInstallMode.m +20 -0
  114. package/ios/CodePush/RCTConvert+CodePushUpdateState.m +20 -0
  115. package/ios/CodePush/SSZipArchive/Info.plist +26 -0
  116. package/ios/CodePush/SSZipArchive/README.md +1 -0
  117. package/ios/CodePush/SSZipArchive/SSZipArchive.h +178 -0
  118. package/ios/CodePush/SSZipArchive/SSZipArchive.m +1496 -0
  119. package/ios/CodePush/SSZipArchive/SSZipCommon.h +71 -0
  120. package/ios/CodePush/SSZipArchive/Supporting Files/PrivacyInfo.xcprivacy +23 -0
  121. package/ios/CodePush/SSZipArchive/include/ZipArchive.h +25 -0
  122. package/ios/CodePush/SSZipArchive/minizip/LICENSE +17 -0
  123. package/ios/CodePush/SSZipArchive/minizip/mz.h +273 -0
  124. package/ios/CodePush/SSZipArchive/minizip/mz_compat.c +1306 -0
  125. package/ios/CodePush/SSZipArchive/minizip/mz_compat.h +346 -0
  126. package/ios/CodePush/SSZipArchive/minizip/mz_crypt.c +187 -0
  127. package/ios/CodePush/SSZipArchive/minizip/mz_crypt.h +65 -0
  128. package/ios/CodePush/SSZipArchive/minizip/mz_crypt_apple.c +526 -0
  129. package/ios/CodePush/SSZipArchive/minizip/mz_os.c +348 -0
  130. package/ios/CodePush/SSZipArchive/minizip/mz_os.h +176 -0
  131. package/ios/CodePush/SSZipArchive/minizip/mz_os_posix.c +350 -0
  132. package/ios/CodePush/SSZipArchive/minizip/mz_strm.c +556 -0
  133. package/ios/CodePush/SSZipArchive/minizip/mz_strm.h +132 -0
  134. package/ios/CodePush/SSZipArchive/minizip/mz_strm_buf.c +383 -0
  135. package/ios/CodePush/SSZipArchive/minizip/mz_strm_buf.h +42 -0
  136. package/ios/CodePush/SSZipArchive/minizip/mz_strm_mem.c +269 -0
  137. package/ios/CodePush/SSZipArchive/minizip/mz_strm_mem.h +48 -0
  138. package/ios/CodePush/SSZipArchive/minizip/mz_strm_os.h +40 -0
  139. package/ios/CodePush/SSZipArchive/minizip/mz_strm_os_posix.c +203 -0
  140. package/ios/CodePush/SSZipArchive/minizip/mz_strm_pkcrypt.c +334 -0
  141. package/ios/CodePush/SSZipArchive/minizip/mz_strm_pkcrypt.h +46 -0
  142. package/ios/CodePush/SSZipArchive/minizip/mz_strm_split.c +429 -0
  143. package/ios/CodePush/SSZipArchive/minizip/mz_strm_split.h +43 -0
  144. package/ios/CodePush/SSZipArchive/minizip/mz_strm_wzaes.c +360 -0
  145. package/ios/CodePush/SSZipArchive/minizip/mz_strm_wzaes.h +46 -0
  146. package/ios/CodePush/SSZipArchive/minizip/mz_strm_zlib.c +389 -0
  147. package/ios/CodePush/SSZipArchive/minizip/mz_strm_zlib.h +43 -0
  148. package/ios/CodePush/SSZipArchive/minizip/mz_zip.c +2782 -0
  149. package/ios/CodePush/SSZipArchive/minizip/mz_zip.h +262 -0
  150. package/ios/CodePush/SSZipArchive/minizip/mz_zip_rw.c +1942 -0
  151. package/ios/CodePush/SSZipArchive/minizip/mz_zip_rw.h +285 -0
  152. package/ios/CodePush.xcodeproj/project.pbxproj +1052 -0
  153. package/ios/CodePush.xcodeproj/project.xcworkspace/contents.xcworkspacedata +7 -0
  154. package/ios/CodePush.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist +8 -0
  155. package/ios/PrivacyInfo.xcprivacy +31 -0
  156. package/logging.js +6 -0
  157. package/package-mixins.js +68 -0
  158. package/package.json +82 -0
  159. package/react-native.config.js +11 -0
  160. package/request-fetch-adapter.js +52 -0
  161. package/scripts/generateBundledResourcesHash.js +125 -0
  162. package/scripts/getFilesInFolder.js +19 -0
  163. package/scripts/postlink/android/postlink.js +87 -0
  164. package/scripts/postlink/ios/postlink.js +116 -0
  165. package/scripts/postlink/run.js +11 -0
  166. package/scripts/postunlink/android/postunlink.js +74 -0
  167. package/scripts/postunlink/ios/postunlink.js +87 -0
  168. package/scripts/postunlink/run.js +11 -0
  169. package/scripts/recordFilesBeforeBundleCommand.js +41 -0
  170. package/scripts/tools/linkToolsAndroid.js +57 -0
  171. package/scripts/tools/linkToolsIos.js +130 -0
  172. package/tsconfig.json +17 -0
  173. package/tslint.json +32 -0
  174. package/typings/react-native-code-push.d.ts +455 -0
@@ -0,0 +1,1122 @@
1
+ #if __has_include(<React/RCTAssert.h>)
2
+ #import <React/RCTAssert.h>
3
+ #import <React/RCTBridgeModule.h>
4
+ #import <React/RCTConvert.h>
5
+ #import <React/RCTEventDispatcher.h>
6
+ #import <React/RCTRootView.h>
7
+ #import <React/RCTUtils.h>
8
+ #import <React/RCTReloadCommand.h>
9
+ #else // back compatibility for RN version < 0.40
10
+ #import "RCTAssert.h"
11
+ #import "RCTBridgeModule.h"
12
+ #import "RCTConvert.h"
13
+ #import "RCTEventDispatcher.h"
14
+ #import "RCTRootView.h"
15
+ #import "RCTUtils.h"
16
+ #endif
17
+
18
+ #import "CodePush.h"
19
+
20
+ @interface CodePush () <RCTBridgeModule, RCTFrameUpdateObserver>
21
+ @end
22
+
23
+ @implementation CodePush {
24
+ BOOL _hasResumeListener;
25
+ BOOL _isFirstRunAfterUpdate;
26
+ int _minimumBackgroundDuration;
27
+ NSDate *_lastResignedDate;
28
+ CodePushInstallMode _installMode;
29
+ NSTimer *_appSuspendTimer;
30
+
31
+ // Used to coordinate the dispatching of download progress events to JS.
32
+ long long _latestExpectedContentLength;
33
+ long long _latestReceivedConentLength;
34
+ BOOL _didUpdateProgress;
35
+
36
+ BOOL _allowed;
37
+ BOOL _restartInProgress;
38
+ NSMutableArray *_restartQueue;
39
+ }
40
+
41
+ RCT_EXPORT_MODULE()
42
+
43
+ #pragma mark - Private constants
44
+
45
+ // These constants represent emitted events
46
+ static NSString *const DownloadProgressEvent = @"CodePushDownloadProgress";
47
+
48
+ // These constants represent valid deployment statuses
49
+ static NSString *const DeploymentFailed = @"DeploymentFailed";
50
+ static NSString *const DeploymentSucceeded = @"DeploymentSucceeded";
51
+
52
+ // These keys represent the names we use to store data in NSUserDefaults
53
+ static NSString *const FailedUpdatesKey = @"CODE_PUSH_FAILED_UPDATES";
54
+ static NSString *const PendingUpdateKey = @"CODE_PUSH_PENDING_UPDATE";
55
+
56
+ // These keys are already "namespaced" by the PendingUpdateKey, so
57
+ // their values don't need to be obfuscated to prevent collision with app data
58
+ static NSString *const PendingUpdateHashKey = @"hash";
59
+ static NSString *const PendingUpdateIsLoadingKey = @"isLoading";
60
+
61
+ // These keys are used to inspect/augment the metadata
62
+ // that is associated with an update's package.
63
+ static NSString *const AppVersionKey = @"appVersion";
64
+ static NSString *const BinaryBundleDateKey = @"binaryDate";
65
+ static NSString *const PackageHashKey = @"packageHash";
66
+ static NSString *const PackageIsPendingKey = @"isPending";
67
+
68
+ #pragma mark - Static variables
69
+
70
+ static BOOL isRunningBinaryVersion = NO;
71
+ static BOOL needToReportRollback = NO;
72
+ static BOOL testConfigurationFlag = NO;
73
+
74
+ // These values are used to save the NS bundle, name, extension and subdirectory
75
+ // for the JS bundle in the binary.
76
+ static NSBundle *bundleResourceBundle = nil;
77
+ static NSString *bundleResourceExtension = @"jsbundle";
78
+ static NSString *bundleResourceName = @"main";
79
+ static NSString *bundleResourceSubdirectory = nil;
80
+
81
+ // These keys represent the names we use to store information about the latest rollback
82
+ static NSString *const LatestRollbackInfoKey = @"LATEST_ROLLBACK_INFO";
83
+ static NSString *const LatestRollbackPackageHashKey = @"packageHash";
84
+ static NSString *const LatestRollbackTimeKey = @"time";
85
+ static NSString *const LatestRollbackCountKey = @"count";
86
+
87
+ + (void)initialize
88
+ {
89
+ [super initialize];
90
+ if (self == [CodePush class]) {
91
+ // Use the mainBundle by default.
92
+ bundleResourceBundle = [NSBundle mainBundle];
93
+ }
94
+ }
95
+
96
+ #pragma mark - Public Obj-C API
97
+
98
+ + (NSURL *)binaryBundleURL
99
+ {
100
+ return [bundleResourceBundle URLForResource:bundleResourceName
101
+ withExtension:bundleResourceExtension
102
+ subdirectory:bundleResourceSubdirectory];
103
+ }
104
+
105
+ + (NSString *)bundleAssetsPath
106
+ {
107
+ NSString *resourcePath = [bundleResourceBundle resourcePath];
108
+ if (bundleResourceSubdirectory) {
109
+ resourcePath = [resourcePath stringByAppendingPathComponent:bundleResourceSubdirectory];
110
+ }
111
+
112
+ return [resourcePath stringByAppendingPathComponent:[CodePushUpdateUtils assetsFolderName]];
113
+ }
114
+
115
+ + (NSURL *)bundleURL
116
+ {
117
+ return [self bundleURLForResource:bundleResourceName
118
+ withExtension:bundleResourceExtension
119
+ subdirectory:bundleResourceSubdirectory
120
+ bundle:bundleResourceBundle];
121
+ }
122
+
123
+ + (NSURL *)bundleURLForResource:(NSString *)resourceName
124
+ {
125
+ return [self bundleURLForResource:resourceName
126
+ withExtension:bundleResourceExtension
127
+ subdirectory:bundleResourceSubdirectory
128
+ bundle:bundleResourceBundle];
129
+ }
130
+
131
+ + (NSURL *)bundleURLForResource:(NSString *)resourceName
132
+ withExtension:(NSString *)resourceExtension
133
+ {
134
+ return [self bundleURLForResource:resourceName
135
+ withExtension:resourceExtension
136
+ subdirectory:bundleResourceSubdirectory
137
+ bundle:bundleResourceBundle];
138
+ }
139
+
140
+ + (NSURL *)bundleURLForResource:(NSString *)resourceName
141
+ withExtension:(NSString *)resourceExtension
142
+ subdirectory:(NSString *)resourceSubdirectory
143
+ {
144
+ return [self bundleURLForResource:resourceName
145
+ withExtension:resourceExtension
146
+ subdirectory:resourceSubdirectory
147
+ bundle:bundleResourceBundle];
148
+ }
149
+
150
+ + (NSURL *)bundleURLForResource:(NSString *)resourceName
151
+ withExtension:(NSString *)resourceExtension
152
+ subdirectory:(NSString *)resourceSubdirectory
153
+ bundle:(NSBundle *)resourceBundle
154
+ {
155
+ bundleResourceName = resourceName;
156
+ bundleResourceExtension = resourceExtension;
157
+ bundleResourceSubdirectory = resourceSubdirectory;
158
+ bundleResourceBundle = resourceBundle;
159
+
160
+ [self ensureBinaryBundleExists];
161
+
162
+ NSString *logMessageFormat = @"Loading JS bundle from %@";
163
+
164
+ NSError *error;
165
+ NSString *packageFile = [CodePushPackage getCurrentPackageBundlePath:&error];
166
+ NSURL *binaryBundleURL = [self binaryBundleURL];
167
+
168
+ if (error || !packageFile) {
169
+ CPLog(logMessageFormat, binaryBundleURL);
170
+ isRunningBinaryVersion = YES;
171
+ return binaryBundleURL;
172
+ }
173
+
174
+ NSString *binaryAppVersion = [[CodePushConfig current] appVersion];
175
+ NSDictionary *currentPackageMetadata = [CodePushPackage getCurrentPackage:&error];
176
+ if (error || !currentPackageMetadata) {
177
+ CPLog(logMessageFormat, binaryBundleURL);
178
+ isRunningBinaryVersion = YES;
179
+ return binaryBundleURL;
180
+ }
181
+
182
+ NSString *packageDate = [currentPackageMetadata objectForKey:BinaryBundleDateKey];
183
+ NSString *packageAppVersion = [currentPackageMetadata objectForKey:AppVersionKey];
184
+
185
+ if ([[CodePushUpdateUtils modifiedDateStringOfFileAtURL:binaryBundleURL] isEqualToString:packageDate] && ([CodePush isUsingTestConfiguration] ||[binaryAppVersion isEqualToString:packageAppVersion])) {
186
+ // Return package file because it is newer than the app store binary's JS bundle
187
+ NSURL *packageUrl = [[NSURL alloc] initFileURLWithPath:packageFile];
188
+ CPLog(logMessageFormat, packageUrl);
189
+ isRunningBinaryVersion = NO;
190
+ return packageUrl;
191
+ } else {
192
+ BOOL isRelease = NO;
193
+ #ifndef DEBUG
194
+ isRelease = YES;
195
+ #endif
196
+
197
+ if (isRelease || ![binaryAppVersion isEqualToString:packageAppVersion]) {
198
+ [CodePush clearUpdates];
199
+ }
200
+
201
+ CPLog(logMessageFormat, binaryBundleURL);
202
+ isRunningBinaryVersion = YES;
203
+ return binaryBundleURL;
204
+ }
205
+ }
206
+
207
+ + (NSString *)getApplicationSupportDirectory
208
+ {
209
+ NSString *applicationSupportDirectory = [NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES) objectAtIndex:0];
210
+ return applicationSupportDirectory;
211
+ }
212
+
213
+ + (void)overrideAppVersion:(NSString *)appVersion
214
+ {
215
+ [CodePushConfig current].appVersion = appVersion;
216
+ }
217
+
218
+ + (void)setDeploymentKey:(NSString *)deploymentKey
219
+ {
220
+ [CodePushConfig current].deploymentKey = deploymentKey;
221
+ }
222
+
223
+ /*
224
+ * WARNING: This cleans up all downloaded and pending updates.
225
+ */
226
+ + (void)clearUpdates
227
+ {
228
+ [CodePushPackage clearUpdates];
229
+ [self removePendingUpdate];
230
+ [self removeFailedUpdates];
231
+ }
232
+
233
+ #pragma mark - Test-only methods
234
+
235
+ /*
236
+ * This returns a boolean value indicating whether CodePush has
237
+ * been set to run under a test configuration.
238
+ */
239
+ + (BOOL)isUsingTestConfiguration
240
+ {
241
+ return testConfigurationFlag;
242
+ }
243
+
244
+ /*
245
+ * This is used to enable an environment in which tests can be run.
246
+ * Specifically, it flips a boolean flag that causes bundles to be
247
+ * saved to a test folder and enables the ability to modify
248
+ * installed bundles on the fly from JavaScript.
249
+ */
250
+ + (void)setUsingTestConfiguration:(BOOL)shouldUseTestConfiguration
251
+ {
252
+ testConfigurationFlag = shouldUseTestConfiguration;
253
+ }
254
+
255
+ #pragma mark - Private API methods
256
+
257
+ @synthesize methodQueue = _methodQueue;
258
+ @synthesize pauseCallback = _pauseCallback;
259
+ @synthesize paused = _paused;
260
+
261
+ - (void)setPaused:(BOOL)paused
262
+ {
263
+ if (_paused != paused) {
264
+ _paused = paused;
265
+ if (_pauseCallback) {
266
+ _pauseCallback();
267
+ }
268
+ }
269
+ }
270
+
271
+ /*
272
+ * This method is used to clear updates that are installed
273
+ * under a different app version and hence don't apply anymore,
274
+ * during a debug run configuration and when the bridge is
275
+ * running the JS bundle from the dev server.
276
+ */
277
+ - (void)clearDebugUpdates
278
+ {
279
+ dispatch_async(dispatch_get_main_queue(), ^{
280
+ if ([super.bridge.bundleURL.scheme hasPrefix:@"http"]) {
281
+ NSError *error;
282
+ NSString *binaryAppVersion = [[CodePushConfig current] appVersion];
283
+ NSDictionary *currentPackageMetadata = [CodePushPackage getCurrentPackage:&error];
284
+ if (currentPackageMetadata) {
285
+ NSString *packageAppVersion = [currentPackageMetadata objectForKey:AppVersionKey];
286
+ if (![binaryAppVersion isEqualToString:packageAppVersion]) {
287
+ [CodePush clearUpdates];
288
+ }
289
+ }
290
+ }
291
+ });
292
+ }
293
+
294
+ /*
295
+ * This method is used by the React Native bridge to allow
296
+ * our plugin to expose constants to the JS-side. In our case
297
+ * we're simply exporting enum values so that the JS and Native
298
+ * sides of the plugin can be in sync.
299
+ */
300
+ - (NSDictionary *)constantsToExport
301
+ {
302
+ // Export the values of the CodePushInstallMode and CodePushUpdateState
303
+ // enums so that the script-side can easily stay in sync
304
+ return @{
305
+ @"codePushInstallModeOnNextRestart":@(CodePushInstallModeOnNextRestart),
306
+ @"codePushInstallModeImmediate": @(CodePushInstallModeImmediate),
307
+ @"codePushInstallModeOnNextResume": @(CodePushInstallModeOnNextResume),
308
+ @"codePushInstallModeOnNextSuspend": @(CodePushInstallModeOnNextSuspend),
309
+
310
+ @"codePushUpdateStateRunning": @(CodePushUpdateStateRunning),
311
+ @"codePushUpdateStatePending": @(CodePushUpdateStatePending),
312
+ @"codePushUpdateStateLatest": @(CodePushUpdateStateLatest)
313
+ };
314
+ };
315
+
316
+ + (BOOL)requiresMainQueueSetup
317
+ {
318
+ return YES;
319
+ }
320
+
321
+ - (void)dealloc
322
+ {
323
+ // Ensure the global resume handler is cleared, so that
324
+ // this object isn't kept alive unnecessarily
325
+ [[NSNotificationCenter defaultCenter] removeObserver:self];
326
+ }
327
+
328
+ - (void)dispatchDownloadProgressEvent {
329
+ // Notify the script-side about the progress
330
+ [self sendEventWithName:DownloadProgressEvent
331
+ body:@{
332
+ @"totalBytes" : [NSNumber
333
+ numberWithLongLong:_latestExpectedContentLength],
334
+ @"receivedBytes" : [NSNumber
335
+ numberWithLongLong:_latestReceivedConentLength]
336
+ }];
337
+ }
338
+
339
+ /*
340
+ * This method ensures that the app was packaged with a JS bundle
341
+ * file, and if not, it throws the appropriate exception.
342
+ */
343
+ + (void)ensureBinaryBundleExists
344
+ {
345
+ if (![self binaryBundleURL]) {
346
+ NSString *errorMessage;
347
+
348
+ #ifdef DEBUG
349
+ #if TARGET_IPHONE_SIMULATOR
350
+ errorMessage = @"React Native doesn't generate your app's JS bundle by default when deploying to the simulator. "
351
+ "If you'd like to test CodePush using the simulator, you can do one of the following depending on your "
352
+ "React Native version and/or preferred workflow:\n\n"
353
+
354
+ "1. Update your AppDelegate.m file to load the JS bundle from the packager instead of from CodePush. "
355
+ "You can still test your CodePush update experience using this workflow (Debug builds only).\n\n"
356
+
357
+ "2. Force the JS bundle to be generated in simulator builds by adding 'export FORCE_BUNDLING=true' to the script under "
358
+ "\"Build Phases\" > \"Bundle React Native code and images\" (React Native >=0.48 only).\n\n"
359
+
360
+ "3. Force the JS bundle to be generated in simulator builds by removing the if block that echoes "
361
+ "\"Skipping bundling for Simulator platform\" in the \"node_modules/react-native/packager/react-native-xcode.sh\" file (React Native <=0.47 only)\n\n"
362
+
363
+ "4. Deploy a Release build to the simulator, which unlike Debug builds, will generate the JS bundle (React Native >=0.22.0 only).";
364
+ #else
365
+ errorMessage = [NSString stringWithFormat:@"The specified JS bundle file wasn't found within the app's binary. Is \"%@\" the correct file name?", [bundleResourceName stringByAppendingPathExtension:bundleResourceExtension]];
366
+ #endif
367
+ #else
368
+ errorMessage = @"Something went wrong. Please verify if generated JS bundle is correct. ";
369
+ #endif
370
+
371
+ RCTFatal([CodePushErrorUtils errorWithMessage:errorMessage]);
372
+ }
373
+ }
374
+
375
+ - (instancetype)init
376
+ {
377
+ _allowed = YES;
378
+ _restartInProgress = NO;
379
+ _restartQueue = [NSMutableArray arrayWithCapacity:1];
380
+
381
+ self = [super init];
382
+ if (self) {
383
+ [self initializeUpdateAfterRestart];
384
+ }
385
+
386
+ return self;
387
+ }
388
+
389
+ /*
390
+ * This method is used when the app is started to either
391
+ * initialize a pending update or rollback a faulty update
392
+ * to the previous version.
393
+ */
394
+ - (void)initializeUpdateAfterRestart
395
+ {
396
+ #ifdef DEBUG
397
+ [self clearDebugUpdates];
398
+ #endif
399
+ self.paused = YES;
400
+ NSUserDefaults *preferences = [NSUserDefaults standardUserDefaults];
401
+ NSDictionary *pendingUpdate = [preferences objectForKey:PendingUpdateKey];
402
+ if (pendingUpdate) {
403
+ _isFirstRunAfterUpdate = YES;
404
+ BOOL updateIsLoading = [pendingUpdate[PendingUpdateIsLoadingKey] boolValue];
405
+ if (updateIsLoading) {
406
+ // Pending update was initialized, but notifyApplicationReady was not called.
407
+ // Therefore, deduce that it is a broken update and rollback.
408
+ CPLog(@"Update did not finish loading the last time, rolling back to a previous version.");
409
+ needToReportRollback = YES;
410
+ [self rollbackPackage];
411
+ } else {
412
+ // Mark that we tried to initialize the new update, so that if it crashes,
413
+ // we will know that we need to rollback when the app next starts.
414
+ [self savePendingUpdate:pendingUpdate[PendingUpdateHashKey]
415
+ isLoading:YES];
416
+ }
417
+ }
418
+ }
419
+
420
+ /*
421
+ * This method is used to get information about the latest rollback.
422
+ * This information will be used to decide whether the application
423
+ * should ignore the update or not.
424
+ */
425
+ + (NSDictionary *)getLatestRollbackInfo
426
+ {
427
+ NSUserDefaults *preferences = [NSUserDefaults standardUserDefaults];
428
+ NSDictionary *latestRollbackInfo = [preferences objectForKey:LatestRollbackInfoKey];
429
+ return latestRollbackInfo;
430
+ }
431
+
432
+ /*
433
+ * This method is used to save information about the latest rollback.
434
+ * This information will be used to decide whether the application
435
+ * should ignore the update or not.
436
+ */
437
+ + (void)setLatestRollbackInfo:(NSString*)packageHash
438
+ {
439
+ if (packageHash == nil) {
440
+ return;
441
+ }
442
+
443
+ NSUserDefaults *preferences = [NSUserDefaults standardUserDefaults];
444
+ NSMutableDictionary *latestRollbackInfo = [preferences objectForKey:LatestRollbackInfoKey];
445
+ if (latestRollbackInfo == nil) {
446
+ latestRollbackInfo = [[NSMutableDictionary alloc] init];
447
+ } else {
448
+ latestRollbackInfo = [latestRollbackInfo mutableCopy];
449
+ }
450
+
451
+ int initialRollbackCount = [self getRollbackCountForPackage: packageHash fromLatestRollbackInfo: latestRollbackInfo];
452
+ NSNumber *count = [NSNumber numberWithInt: initialRollbackCount + 1];
453
+ NSNumber *currentTimeMillis = [NSNumber numberWithDouble: [[NSDate date] timeIntervalSince1970] * 1000];
454
+
455
+ [latestRollbackInfo setValue:count forKey:LatestRollbackCountKey];
456
+ [latestRollbackInfo setValue:currentTimeMillis forKey:LatestRollbackTimeKey];
457
+ [latestRollbackInfo setValue:packageHash forKey:LatestRollbackPackageHashKey];
458
+
459
+ [preferences setObject:latestRollbackInfo forKey:LatestRollbackInfoKey];
460
+ [preferences synchronize];
461
+ }
462
+
463
+ /*
464
+ * This method is used to get the count of rollback for the package
465
+ * using the latest rollback information.
466
+ */
467
+ + (int)getRollbackCountForPackage:(NSString*) packageHash fromLatestRollbackInfo:(NSMutableDictionary*) latestRollbackInfo
468
+ {
469
+ NSString *oldPackageHash = [latestRollbackInfo objectForKey:LatestRollbackPackageHashKey];
470
+ if ([packageHash isEqualToString: oldPackageHash]) {
471
+ NSNumber *oldCount = [latestRollbackInfo objectForKey:LatestRollbackCountKey];
472
+ return [oldCount intValue];
473
+ } else {
474
+ return 0;
475
+ }
476
+ }
477
+
478
+ /*
479
+ * This method checks to see whether a specific package hash
480
+ * has previously failed installation.
481
+ */
482
+ + (BOOL)isFailedHash:(NSString*)packageHash
483
+ {
484
+ NSUserDefaults *preferences = [NSUserDefaults standardUserDefaults];
485
+ NSMutableArray *failedUpdates = [preferences objectForKey:FailedUpdatesKey];
486
+ if (failedUpdates == nil || packageHash == nil) {
487
+ return NO;
488
+ } else {
489
+ for (NSDictionary *failedPackage in failedUpdates)
490
+ {
491
+ // Type check is needed for backwards compatibility, where we used to just store
492
+ // the failed package hash instead of the metadata. This only impacts "dev"
493
+ // scenarios, since in production we clear out old information whenever a new
494
+ // binary is applied.
495
+ if ([failedPackage isKindOfClass:[NSDictionary class]]) {
496
+ NSString *failedPackageHash = [failedPackage objectForKey:PackageHashKey];
497
+ if ([packageHash isEqualToString:failedPackageHash]) {
498
+ return YES;
499
+ }
500
+ }
501
+ }
502
+
503
+ return NO;
504
+ }
505
+ }
506
+
507
+ /*
508
+ * This method checks to see whether a specific package hash
509
+ * represents a downloaded and installed update, that hasn't
510
+ * been applied yet via an app restart.
511
+ */
512
+ + (BOOL)isPendingUpdate:(NSString*)packageHash
513
+ {
514
+ NSUserDefaults *preferences = [NSUserDefaults standardUserDefaults];
515
+ NSDictionary *pendingUpdate = [preferences objectForKey:PendingUpdateKey];
516
+
517
+ // If there is a pending update whose "state" isn't loading, then we consider it "pending".
518
+ // Additionally, if a specific hash was provided, we ensure it matches that of the pending update.
519
+ BOOL updateIsPending = pendingUpdate &&
520
+ [pendingUpdate[PendingUpdateIsLoadingKey] boolValue] == NO &&
521
+ (!packageHash || [pendingUpdate[PendingUpdateHashKey] isEqualToString:packageHash]);
522
+
523
+ return updateIsPending;
524
+ }
525
+
526
+ /*
527
+ * This method updates the React Native bridge's bundle URL
528
+ * to point at the latest CodePush update, and then restarts
529
+ * the bridge. This isn't meant to be called directly.
530
+ */
531
+ - (void)loadBundle
532
+ {
533
+ // This needs to be async dispatched because the bridge is not set on init
534
+ // when the app first starts, therefore rollbacks will not take effect.
535
+ dispatch_async(dispatch_get_main_queue(), ^{
536
+ // If the current bundle URL is using http(s), then assume the dev
537
+ // is debugging and therefore, shouldn't be redirected to a local
538
+ // file (since Chrome wouldn't support it). Otherwise, update
539
+ // the current bundle URL to point at the latest update
540
+ if ([CodePush isUsingTestConfiguration] || ![super.bridge.bundleURL.scheme hasPrefix:@"http"]) {
541
+ [super.bridge setValue:[CodePush bundleURL] forKey:@"bundleURL"];
542
+ }
543
+
544
+ RCTTriggerReloadCommandListeners(@"react-native-code-push: Restart");
545
+ });
546
+ }
547
+
548
+ /*
549
+ * This method is used when an update has failed installation
550
+ * and the app needs to be rolled back to the previous bundle.
551
+ * This method is automatically called when the rollback timer
552
+ * expires without the app indicating whether the update succeeded,
553
+ * and therefore, it shouldn't be called directly.
554
+ */
555
+ - (void)rollbackPackage
556
+ {
557
+ NSError *error;
558
+ NSDictionary *failedPackage = [CodePushPackage getCurrentPackage:&error];
559
+ if (!failedPackage) {
560
+ if (error) {
561
+ CPLog(@"Error getting current update metadata during rollback: %@", error);
562
+ } else {
563
+ CPLog(@"Attempted to perform a rollback when there is no current update");
564
+ }
565
+ } else {
566
+ // Write the current package's metadata to the "failed list"
567
+ [self saveFailedUpdate:failedPackage];
568
+ }
569
+
570
+ // Rollback to the previous version and de-register the new update
571
+ [CodePushPackage rollbackPackage];
572
+ [CodePush removePendingUpdate];
573
+ [self loadBundle];
574
+ }
575
+
576
+ /*
577
+ * When an update failed to apply, this method can be called
578
+ * to store its hash so that it can be ignored on future
579
+ * attempts to check the server for an update.
580
+ */
581
+ - (void)saveFailedUpdate:(NSDictionary *)failedPackage
582
+ {
583
+ if ([[self class] isFailedHash:[failedPackage objectForKey:PackageHashKey]]) {
584
+ return;
585
+ }
586
+
587
+ NSUserDefaults *preferences = [NSUserDefaults standardUserDefaults];
588
+ NSMutableArray *failedUpdates = [preferences objectForKey:FailedUpdatesKey];
589
+ if (failedUpdates == nil) {
590
+ failedUpdates = [[NSMutableArray alloc] init];
591
+ } else {
592
+ // The NSUserDefaults sytem always returns immutable
593
+ // objects, regardless if you stored something mutable.
594
+ failedUpdates = [failedUpdates mutableCopy];
595
+ }
596
+
597
+ [failedUpdates addObject:failedPackage];
598
+ [preferences setObject:failedUpdates forKey:FailedUpdatesKey];
599
+ [preferences synchronize];
600
+ }
601
+
602
+ /*
603
+ * This method is used to clear away failed updates in the event that
604
+ * a new app store binary is installed.
605
+ */
606
+ + (void)removeFailedUpdates
607
+ {
608
+ NSUserDefaults *preferences = [NSUserDefaults standardUserDefaults];
609
+ [preferences removeObjectForKey:FailedUpdatesKey];
610
+ [preferences synchronize];
611
+ }
612
+
613
+ /*
614
+ * This method is used to register the fact that a pending
615
+ * update succeeded and therefore can be removed.
616
+ */
617
+ + (void)removePendingUpdate
618
+ {
619
+ NSUserDefaults *preferences = [NSUserDefaults standardUserDefaults];
620
+ [preferences removeObjectForKey:PendingUpdateKey];
621
+ [preferences synchronize];
622
+ }
623
+
624
+ /*
625
+ * When an update is installed whose mode isn't IMMEDIATE, this method
626
+ * can be called to store the pending update's metadata (e.g. packageHash)
627
+ * so that it can be used when the actual update application occurs at a later point.
628
+ */
629
+ - (void)savePendingUpdate:(NSString *)packageHash
630
+ isLoading:(BOOL)isLoading
631
+ {
632
+ // Since we're not restarting, we need to store the fact that the update
633
+ // was installed, but hasn't yet become "active".
634
+ NSUserDefaults *preferences = [NSUserDefaults standardUserDefaults];
635
+ NSDictionary *pendingUpdate = [[NSDictionary alloc] initWithObjectsAndKeys:
636
+ packageHash,PendingUpdateHashKey,
637
+ [NSNumber numberWithBool:isLoading],PendingUpdateIsLoadingKey, nil];
638
+
639
+ [preferences setObject:pendingUpdate forKey:PendingUpdateKey];
640
+ [preferences synchronize];
641
+ }
642
+
643
+ - (NSArray<NSString *> *)supportedEvents {
644
+ return @[DownloadProgressEvent];
645
+ }
646
+
647
+ // Determine how long the app was in the background
648
+ - (int)getDurationInBackground
649
+ {
650
+ int duration = 0;
651
+ if (_lastResignedDate) {
652
+ duration = [[NSDate date] timeIntervalSinceDate:_lastResignedDate];
653
+ }
654
+
655
+ return duration;
656
+ }
657
+
658
+ #pragma mark - Application lifecycle event handlers
659
+
660
+ // These three handlers will only be registered when there is
661
+ // a resume-based update still pending installation.
662
+ - (void)applicationDidBecomeActive
663
+ {
664
+ if (_installMode == CodePushInstallModeOnNextSuspend) {
665
+ int durationInBackground = [self getDurationInBackground];
666
+ // We shouldn't use loadBundle in this case, because _appSuspendTimer will call loadBundleOnTick.
667
+ // We should cancel timer for _appSuspendTimer because otherwise, we would call loadBundle two times.
668
+ if (durationInBackground < _minimumBackgroundDuration) {
669
+ [_appSuspendTimer invalidate];
670
+ _appSuspendTimer = nil;
671
+ }
672
+ }
673
+ }
674
+
675
+ - (void)applicationWillEnterForeground
676
+ {
677
+ if (_installMode == CodePushInstallModeOnNextResume) {
678
+ int durationInBackground = [self getDurationInBackground];
679
+ if (durationInBackground >= _minimumBackgroundDuration) {
680
+ [self restartAppInternal:NO];
681
+ }
682
+ }
683
+ }
684
+
685
+ - (void)applicationWillResignActive
686
+ {
687
+ // Save the current time so that when the app is later
688
+ // resumed, we can detect how long it was in the background.
689
+ _lastResignedDate = [NSDate date];
690
+
691
+ if (_installMode == CodePushInstallModeOnNextSuspend && [[self class] isPendingUpdate:nil]) {
692
+ _appSuspendTimer = [NSTimer scheduledTimerWithTimeInterval:_minimumBackgroundDuration
693
+ target:self
694
+ selector:@selector(loadBundleOnTick:)
695
+ userInfo:nil
696
+ repeats:NO];
697
+ }
698
+ }
699
+
700
+ -(void)loadBundleOnTick:(NSTimer *)timer {
701
+ [self restartAppInternal:NO];
702
+ }
703
+
704
+ #pragma mark - JavaScript-exported module methods (Public)
705
+
706
+ /*
707
+ * This is native-side of the RemotePackage.download method
708
+ */
709
+ RCT_EXPORT_METHOD(downloadUpdate:(NSDictionary*)updatePackage
710
+ notifyProgress:(BOOL)notifyProgress
711
+ resolver:(RCTPromiseResolveBlock)resolve
712
+ rejecter:(RCTPromiseRejectBlock)reject)
713
+ {
714
+ NSDictionary *mutableUpdatePackage = [updatePackage mutableCopy];
715
+ NSURL *binaryBundleURL = [CodePush binaryBundleURL];
716
+ if (binaryBundleURL != nil) {
717
+ [mutableUpdatePackage setValue:[CodePushUpdateUtils modifiedDateStringOfFileAtURL:binaryBundleURL]
718
+ forKey:BinaryBundleDateKey];
719
+ }
720
+
721
+ if (notifyProgress) {
722
+ // Set up and unpause the frame observer so that it can emit
723
+ // progress events every frame if the progress is updated.
724
+ _didUpdateProgress = NO;
725
+ self.paused = NO;
726
+ }
727
+
728
+ NSString * publicKey = [[CodePushConfig current] publicKey];
729
+
730
+ [CodePushPackage
731
+ downloadPackage:mutableUpdatePackage
732
+ expectedBundleFileName:[bundleResourceName stringByAppendingPathExtension:bundleResourceExtension]
733
+ publicKey:publicKey
734
+ operationQueue:_methodQueue
735
+ // The download is progressing forward
736
+ progressCallback:^(long long expectedContentLength, long long receivedContentLength) {
737
+ // Update the download progress so that the frame observer can notify the JS side
738
+ _latestExpectedContentLength = expectedContentLength;
739
+ _latestReceivedConentLength = receivedContentLength;
740
+ _didUpdateProgress = YES;
741
+
742
+ // If the download is completed, stop observing frame
743
+ // updates and synchronously send the last event.
744
+ if (expectedContentLength == receivedContentLength) {
745
+ _didUpdateProgress = NO;
746
+ self.paused = YES;
747
+ [self dispatchDownloadProgressEvent];
748
+ }
749
+ }
750
+ // The download completed
751
+ doneCallback:^{
752
+ NSError *err;
753
+ NSDictionary *newPackage = [CodePushPackage getPackage:mutableUpdatePackage[PackageHashKey] error:&err];
754
+
755
+ if (err) {
756
+ return reject([NSString stringWithFormat: @"%lu", (long)err.code], err.localizedDescription, err);
757
+ }
758
+ resolve(newPackage);
759
+ }
760
+ // The download failed
761
+ failCallback:^(NSError *err) {
762
+ if ([CodePushErrorUtils isCodePushError:err]) {
763
+ [self saveFailedUpdate:mutableUpdatePackage];
764
+ }
765
+
766
+ // Stop observing frame updates if the download fails.
767
+ _didUpdateProgress = NO;
768
+ self.paused = YES;
769
+ reject([NSString stringWithFormat: @"%lu", (long)err.code], err.localizedDescription, err);
770
+ }];
771
+ }
772
+
773
+ - (void)restartAppInternal:(BOOL)onlyIfUpdateIsPending
774
+ {
775
+ if (_restartInProgress) {
776
+ CPLog(@"Restart request queued until the current restart is completed.");
777
+ [_restartQueue addObject:@(onlyIfUpdateIsPending)];
778
+ return;
779
+ } else if (!_allowed) {
780
+ CPLog(@"Restart request queued until restarts are re-allowed.");
781
+ [_restartQueue addObject:@(onlyIfUpdateIsPending)];
782
+ return;
783
+ }
784
+
785
+ _restartInProgress = YES;
786
+ if (!onlyIfUpdateIsPending || [[self class] isPendingUpdate:nil]) {
787
+ [self loadBundle];
788
+ CPLog(@"Restarting app.");
789
+ return;
790
+ }
791
+
792
+ _restartInProgress = NO;
793
+ if ([_restartQueue count] > 0) {
794
+ BOOL buf = [_restartQueue valueForKey: @"@firstObject"];
795
+ [_restartQueue removeObjectAtIndex:0];
796
+ [self restartAppInternal:buf];
797
+ }
798
+ }
799
+
800
+ /*
801
+ * This is the native side of the CodePush.getConfiguration method. It isn't
802
+ * currently exposed via the "react-native-code-push" module, and is used
803
+ * internally only by the CodePush.checkForUpdate method in order to get the
804
+ * app version, as well as the deployment key that was configured in the Info.plist file.
805
+ */
806
+ RCT_EXPORT_METHOD(getConfiguration:(RCTPromiseResolveBlock)resolve
807
+ rejecter:(RCTPromiseRejectBlock)reject)
808
+ {
809
+ NSDictionary *configuration = [[CodePushConfig current] configuration];
810
+ NSError *error;
811
+ if (isRunningBinaryVersion) {
812
+ // isRunningBinaryVersion will not get set to "YES" if running against the packager.
813
+ NSString *binaryHash = [CodePushUpdateUtils getHashForBinaryContents:[CodePush binaryBundleURL] error:&error];
814
+ if (error) {
815
+ CPLog(@"Error obtaining hash for binary contents: %@", error);
816
+ resolve(configuration);
817
+ return;
818
+ }
819
+
820
+ if (binaryHash == nil) {
821
+ // The hash was not generated either due to a previous unknown error or the fact that
822
+ // the React Native assets were not bundled in the binary (e.g. during dev/simulator)
823
+ // builds.
824
+ resolve(configuration);
825
+ return;
826
+ }
827
+
828
+ NSMutableDictionary *mutableConfiguration = [configuration mutableCopy];
829
+ [mutableConfiguration setObject:binaryHash forKey:PackageHashKey];
830
+ resolve(mutableConfiguration);
831
+ return;
832
+ }
833
+
834
+ resolve(configuration);
835
+ }
836
+
837
+ /*
838
+ * This method is the native side of the CodePush.getUpdateMetadata method.
839
+ */
840
+ RCT_EXPORT_METHOD(getUpdateMetadata:(CodePushUpdateState)updateState
841
+ resolver:(RCTPromiseResolveBlock)resolve
842
+ rejecter:(RCTPromiseRejectBlock)reject)
843
+ {
844
+ NSError *error;
845
+ NSMutableDictionary *package = [[CodePushPackage getCurrentPackage:&error] mutableCopy];
846
+
847
+ if (error) {
848
+ return reject([NSString stringWithFormat: @"%lu", (long)error.code], error.localizedDescription, error);
849
+ } else if (package == nil) {
850
+ // The app hasn't downloaded any CodePush updates yet,
851
+ // so we simply return nil regardless if the user
852
+ // wanted to retrieve the pending or running update.
853
+ return resolve(nil);
854
+ }
855
+
856
+ // We have a CodePush update, so let's see if it's currently in a pending state.
857
+ BOOL currentUpdateIsPending = [[self class] isPendingUpdate:[package objectForKey:PackageHashKey]];
858
+
859
+ if (updateState == CodePushUpdateStatePending && !currentUpdateIsPending) {
860
+ // The caller wanted a pending update
861
+ // but there isn't currently one.
862
+ resolve(nil);
863
+ } else if (updateState == CodePushUpdateStateRunning && currentUpdateIsPending) {
864
+ // The caller wants the running update, but the current
865
+ // one is pending, so we need to grab the previous.
866
+ resolve([CodePushPackage getPreviousPackage:&error]);
867
+ } else {
868
+ // The current package satisfies the request:
869
+ // 1) Caller wanted a pending, and there is a pending update
870
+ // 2) Caller wanted the running update, and there isn't a pending
871
+ // 3) Caller wants the latest update, regardless if it's pending or not
872
+ if (isRunningBinaryVersion) {
873
+ // This only matters in Debug builds. Since we do not clear "outdated" updates,
874
+ // we need to indicate to the JS side that somehow we have a current update on
875
+ // disk that is not actually running.
876
+ [package setObject:@(YES) forKey:@"_isDebugOnly"];
877
+ }
878
+
879
+ // Enable differentiating pending vs. non-pending updates
880
+ [package setObject:@(currentUpdateIsPending) forKey:PackageIsPendingKey];
881
+ resolve(package);
882
+ }
883
+ }
884
+
885
+ /*
886
+ * This method is the native side of the LocalPackage.install method.
887
+ */
888
+ RCT_EXPORT_METHOD(installUpdate:(NSDictionary*)updatePackage
889
+ installMode:(CodePushInstallMode)installMode
890
+ minimumBackgroundDuration:(int)minimumBackgroundDuration
891
+ resolver:(RCTPromiseResolveBlock)resolve
892
+ rejecter:(RCTPromiseRejectBlock)reject)
893
+ {
894
+ NSError *error;
895
+ [CodePushPackage installPackage:updatePackage
896
+ removePendingUpdate:[[self class] isPendingUpdate:nil]
897
+ error:&error];
898
+
899
+ if (error) {
900
+ reject([NSString stringWithFormat: @"%lu", (long)error.code], error.localizedDescription, error);
901
+ } else {
902
+ [self savePendingUpdate:updatePackage[PackageHashKey]
903
+ isLoading:NO];
904
+
905
+ _installMode = installMode;
906
+ if (_installMode == CodePushInstallModeOnNextResume || _installMode == CodePushInstallModeOnNextSuspend) {
907
+ _minimumBackgroundDuration = minimumBackgroundDuration;
908
+
909
+ if (!_hasResumeListener) {
910
+ // Ensure we do not add the listener twice.
911
+ // Register for app resume notifications so that we
912
+ // can check for pending updates which support "restart on resume"
913
+ [[NSNotificationCenter defaultCenter] addObserver:self
914
+ selector:@selector(applicationDidBecomeActive)
915
+ name:UIApplicationDidBecomeActiveNotification
916
+ object:RCTSharedApplication()];
917
+
918
+ [[NSNotificationCenter defaultCenter] addObserver:self
919
+ selector:@selector(applicationWillEnterForeground)
920
+ name:UIApplicationWillEnterForegroundNotification
921
+ object:RCTSharedApplication()];
922
+
923
+ [[NSNotificationCenter defaultCenter] addObserver:self
924
+ selector:@selector(applicationWillResignActive)
925
+ name:UIApplicationWillResignActiveNotification
926
+ object:RCTSharedApplication()];
927
+
928
+ _hasResumeListener = YES;
929
+ }
930
+ }
931
+
932
+ // Signal to JS that the update has been applied.
933
+ resolve(nil);
934
+ }
935
+ }
936
+
937
+ /*
938
+ * This method isn't publicly exposed via the "react-native-code-push"
939
+ * module, and is only used internally to populate the RemotePackage.failedInstall property.
940
+ */
941
+ RCT_EXPORT_METHOD(isFailedUpdate:(NSString *)packageHash
942
+ resolve:(RCTPromiseResolveBlock)resolve
943
+ reject:(RCTPromiseRejectBlock)reject)
944
+ {
945
+ BOOL isFailedHash = [[self class] isFailedHash:packageHash];
946
+ resolve(@(isFailedHash));
947
+ }
948
+
949
+ RCT_EXPORT_METHOD(setLatestRollbackInfo:(NSString *)packageHash
950
+ resolve:(RCTPromiseResolveBlock)resolve
951
+ reject:(RCTPromiseRejectBlock)reject)
952
+ {
953
+ [[self class] setLatestRollbackInfo:packageHash];
954
+ resolve(nil);
955
+ }
956
+
957
+
958
+ RCT_EXPORT_METHOD(getLatestRollbackInfo:(RCTPromiseResolveBlock)resolve
959
+ rejecter:(RCTPromiseRejectBlock)reject)
960
+ {
961
+ NSDictionary *latestRollbackInfo = [[self class] getLatestRollbackInfo];
962
+ resolve(latestRollbackInfo);
963
+ }
964
+
965
+ /*
966
+ * This method isn't publicly exposed via the "react-native-code-push"
967
+ * module, and is only used internally to populate the LocalPackage.isFirstRun property.
968
+ */
969
+ RCT_EXPORT_METHOD(isFirstRun:(NSString *)packageHash
970
+ resolve:(RCTPromiseResolveBlock)resolve
971
+ rejecter:(RCTPromiseRejectBlock)reject)
972
+ {
973
+ NSError *error;
974
+ BOOL isFirstRun = _isFirstRunAfterUpdate
975
+ && nil != packageHash
976
+ && [packageHash length] > 0
977
+ && [packageHash isEqualToString:[CodePushPackage getCurrentPackageHash:&error]];
978
+
979
+ resolve(@(isFirstRun));
980
+ }
981
+
982
+ /*
983
+ * This method is the native side of the CodePush.notifyApplicationReady() method.
984
+ */
985
+ RCT_EXPORT_METHOD(notifyApplicationReady:(RCTPromiseResolveBlock)resolve
986
+ rejecter:(RCTPromiseRejectBlock)reject)
987
+ {
988
+ [CodePush removePendingUpdate];
989
+ resolve(nil);
990
+ }
991
+
992
+ RCT_EXPORT_METHOD(allow:(RCTPromiseResolveBlock)resolve
993
+ rejecter:(RCTPromiseRejectBlock)reject)
994
+ {
995
+ CPLog(@"Re-allowing restarts.");
996
+ _allowed = YES;
997
+
998
+ if ([_restartQueue count] > 0) {
999
+ CPLog(@"Executing pending restart.");
1000
+ BOOL buf = [_restartQueue valueForKey: @"@firstObject"];
1001
+ [_restartQueue removeObjectAtIndex:0];
1002
+ [self restartAppInternal:buf];
1003
+ }
1004
+
1005
+ resolve(nil);
1006
+ }
1007
+
1008
+ RCT_EXPORT_METHOD(clearPendingRestart:(RCTPromiseResolveBlock)resolve
1009
+ rejecter:(RCTPromiseRejectBlock)reject)
1010
+ {
1011
+ [_restartQueue removeAllObjects];
1012
+ resolve(nil);
1013
+ }
1014
+
1015
+ RCT_EXPORT_METHOD(disallow:(RCTPromiseResolveBlock)resolve
1016
+ rejecter:(RCTPromiseRejectBlock)reject)
1017
+ {
1018
+ CPLog(@"Disallowing restarts.");
1019
+ _allowed = NO;
1020
+ resolve(nil);
1021
+ }
1022
+
1023
+ /*
1024
+ * This method is the native side of the CodePush.restartApp() method.
1025
+ */
1026
+ RCT_EXPORT_METHOD(restartApp:(BOOL)onlyIfUpdateIsPending
1027
+ resolve:(RCTPromiseResolveBlock)resolve
1028
+ rejecter:(RCTPromiseRejectBlock)reject)
1029
+ {
1030
+ [self restartAppInternal:onlyIfUpdateIsPending];
1031
+ resolve(nil);
1032
+ }
1033
+
1034
+ /*
1035
+ * This method clears CodePush's downloaded updates.
1036
+ * It is needed to switch to a different deployment if the current deployment is more recent.
1037
+ * Note: we don’t recommend to use this method in scenarios other than that (CodePush will call this method
1038
+ * automatically when needed in other cases) as it could lead to unpredictable behavior.
1039
+ */
1040
+ RCT_EXPORT_METHOD(clearUpdates) {
1041
+ CPLog(@"Clearing updates.");
1042
+ [CodePush clearUpdates];
1043
+ }
1044
+
1045
+ #pragma mark - JavaScript-exported module methods (Private)
1046
+
1047
+ /*
1048
+ * This method is the native side of the CodePush.downloadAndReplaceCurrentBundle()
1049
+ * method, which replaces the current bundle with the one downloaded from
1050
+ * removeBundleUrl. It is only to be used during tests and no-ops if the test
1051
+ * configuration flag is not set.
1052
+ */
1053
+ RCT_EXPORT_METHOD(downloadAndReplaceCurrentBundle:(NSString *)remoteBundleUrl)
1054
+ {
1055
+ if ([CodePush isUsingTestConfiguration]) {
1056
+ [CodePushPackage downloadAndReplaceCurrentBundle:remoteBundleUrl];
1057
+ }
1058
+ }
1059
+
1060
+ /*
1061
+ * This method is checks if a new status update exists (new version was installed,
1062
+ * or an update failed) and return its details (version label, status).
1063
+ */
1064
+ RCT_EXPORT_METHOD(getNewStatusReport:(RCTPromiseResolveBlock)resolve
1065
+ rejecter:(RCTPromiseRejectBlock)reject)
1066
+ {
1067
+ if (needToReportRollback) {
1068
+ needToReportRollback = NO;
1069
+ NSUserDefaults *preferences = [NSUserDefaults standardUserDefaults];
1070
+ NSMutableArray *failedUpdates = [preferences objectForKey:FailedUpdatesKey];
1071
+ if (failedUpdates) {
1072
+ NSDictionary *lastFailedPackage = [failedUpdates lastObject];
1073
+ if (lastFailedPackage) {
1074
+ resolve([CodePushTelemetryManager getRollbackReport:lastFailedPackage]);
1075
+ return;
1076
+ }
1077
+ }
1078
+ } else if (_isFirstRunAfterUpdate) {
1079
+ NSError *error;
1080
+ NSDictionary *currentPackage = [CodePushPackage getCurrentPackage:&error];
1081
+ if (!error && currentPackage) {
1082
+ resolve([CodePushTelemetryManager getUpdateReport:currentPackage]);
1083
+ return;
1084
+ }
1085
+ } else if (isRunningBinaryVersion) {
1086
+ NSString *appVersion = [[CodePushConfig current] appVersion];
1087
+ resolve([CodePushTelemetryManager getBinaryUpdateReport:appVersion]);
1088
+ return;
1089
+ } else {
1090
+ NSDictionary *retryStatusReport = [CodePushTelemetryManager getRetryStatusReport];
1091
+ if (retryStatusReport) {
1092
+ resolve(retryStatusReport);
1093
+ return;
1094
+ }
1095
+ }
1096
+
1097
+ resolve(nil);
1098
+ }
1099
+
1100
+ RCT_EXPORT_METHOD(recordStatusReported:(NSDictionary *)statusReport)
1101
+ {
1102
+ [CodePushTelemetryManager recordStatusReported:statusReport];
1103
+ }
1104
+
1105
+ RCT_EXPORT_METHOD(saveStatusReportForRetry:(NSDictionary *)statusReport)
1106
+ {
1107
+ [CodePushTelemetryManager saveStatusReportForRetry:statusReport];
1108
+ }
1109
+
1110
+ #pragma mark - RCTFrameUpdateObserver Methods
1111
+
1112
+ - (void)didUpdateFrame:(RCTFrameUpdate *)update
1113
+ {
1114
+ if (!_didUpdateProgress) {
1115
+ return;
1116
+ }
1117
+
1118
+ [self dispatchDownloadProgressEvent];
1119
+ _didUpdateProgress = NO;
1120
+ }
1121
+
1122
+ @end