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