| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CHROME_BROWSER_ANDROID_WEBAPK_WEBAPK_INSTALLER_H_ | 5 #ifndef CHROME_BROWSER_ANDROID_WEBAPK_WEBAPK_INSTALLER_H_ |
| 6 #define CHROME_BROWSER_ANDROID_WEBAPK_WEBAPK_INSTALLER_H_ | 6 #define CHROME_BROWSER_ANDROID_WEBAPK_WEBAPK_INSTALLER_H_ |
| 7 | 7 |
| 8 #include <jni.h> | 8 #include <jni.h> |
| 9 #include <map> | 9 #include <map> |
| 10 #include <memory> | 10 #include <memory> |
| 11 | 11 |
| 12 #include "base/android/scoped_java_ref.h" | 12 #include "base/android/scoped_java_ref.h" |
| 13 #include "base/callback.h" | 13 #include "base/callback.h" |
| 14 #include "base/macros.h" | 14 #include "base/macros.h" |
| 15 #include "base/memory/weak_ptr.h" | 15 #include "base/memory/weak_ptr.h" |
| 16 #include "base/timer/timer.h" | 16 #include "base/timer/timer.h" |
| 17 #include "chrome/browser/android/shortcut_info.h" | 17 #include "chrome/browser/android/shortcut_info.h" |
| 18 #include "chrome/browser/android/webapk/webapk_install_service.h" | 18 #include "chrome/browser/android/webapk/webapk_install_service.h" |
| 19 #include "chrome/browser/net/file_downloader.h" | 19 #include "chrome/browser/net/file_downloader.h" |
| 20 #include "net/url_request/url_fetcher.h" | 20 #include "net/url_request/url_fetcher.h" |
| 21 #include "net/url_request/url_fetcher_delegate.h" | 21 #include "net/url_request/url_fetcher_delegate.h" |
| 22 #include "third_party/skia/include/core/SkBitmap.h" | 22 #include "third_party/skia/include/core/SkBitmap.h" |
| 23 | 23 |
| 24 namespace base { | |
| 25 class FilePath; | |
| 26 } | |
| 27 | |
| 28 namespace content { | 24 namespace content { |
| 29 class BrowserContext; | 25 class BrowserContext; |
| 30 } | 26 } |
| 31 | 27 |
| 32 namespace webapk { | 28 namespace webapk { |
| 33 class WebApk; | 29 class WebApk; |
| 34 } | 30 } |
| 35 | 31 |
| 36 class WebApkIconHasher; | 32 class WebApkIconHasher; |
| 37 | 33 |
| 38 // Talks to Chrome WebAPK server and Google Play to generate a WebAPK on the | 34 // Talks to Chrome WebAPK server to download metadata about WebApk and issue |
| 39 // server, download it, and install it. The native WebApkInstaller owns the | 35 // a request for it to be installed. The native WebApkInstaller owns the Java |
| 40 // Java WebApkInstaller counterpart. | 36 // WebApkInstaller counterpart. |
| 41 class WebApkInstaller : public net::URLFetcherDelegate { | 37 class WebApkInstaller : public net::URLFetcherDelegate { |
| 42 public: | 38 public: |
| 43 using FinishCallback = WebApkInstallService::FinishCallback; | 39 using FinishCallback = WebApkInstallService::FinishCallback; |
| 44 | 40 |
| 45 ~WebApkInstaller() override; | 41 ~WebApkInstaller() override; |
| 46 | 42 |
| 47 // Creates a self-owned WebApkInstaller instance and talks to the Chrome | 43 // Creates a self-owned WebApkInstaller instance and talks to the Chrome |
| 48 // WebAPK server to generate a WebAPK on the server and to Google Play to | 44 // WebAPK server to generate a WebAPK on the server and locally requests the |
| 49 // install the downloaded WebAPK. Calls |callback| once the install completed | 45 // APK to be installed. Calls |callback| once the install completed or failed. |
| 50 // or failed. | |
| 51 static void InstallAsync(content::BrowserContext* context, | 46 static void InstallAsync(content::BrowserContext* context, |
| 52 const ShortcutInfo& shortcut_info, | 47 const ShortcutInfo& shortcut_info, |
| 53 const SkBitmap& shortcut_icon, | 48 const SkBitmap& shortcut_icon, |
| 54 const FinishCallback& finish_callback); | 49 const FinishCallback& finish_callback); |
| 55 | 50 |
| 56 // Creates a self-owned WebApkInstaller instance and talks to the Chrome | 51 // Creates a self-owned WebApkInstaller instance and talks to the Chrome |
| 57 // WebAPK server to update a WebAPK on the server and to the Google Play | 52 // WebAPK server to update a WebAPK on the server and locally requests the |
| 58 // server to install the downloaded WebAPK. Calls |callback| after the request | 53 // APK to be installed. Calls |callback| once the install completed or failed. |
| 59 // to install the WebAPK is sent to the Google Play server. | |
| 60 static void UpdateAsync( | 54 static void UpdateAsync( |
| 61 content::BrowserContext* context, | 55 content::BrowserContext* context, |
| 62 const ShortcutInfo& shortcut_info, | 56 const ShortcutInfo& shortcut_info, |
| 63 const SkBitmap& shortcut_icon, | 57 const SkBitmap& shortcut_icon, |
| 64 const std::string& webapk_package, | 58 const std::string& webapk_package, |
| 65 int webapk_version, | 59 int webapk_version, |
| 66 const std::map<std::string, std::string>& icon_url_to_murmur2_hash, | 60 const std::map<std::string, std::string>& icon_url_to_murmur2_hash, |
| 67 bool is_manifest_stale, | 61 bool is_manifest_stale, |
| 68 const FinishCallback& callback); | 62 const FinishCallback& callback); |
| 69 | 63 |
| (...skipping 28 matching lines...) Expand all Loading... |
| 98 bool is_manifest_stale); | 92 bool is_manifest_stale); |
| 99 | 93 |
| 100 // Registers JNI hooks. | 94 // Registers JNI hooks. |
| 101 static bool Register(JNIEnv* env); | 95 static bool Register(JNIEnv* env); |
| 102 | 96 |
| 103 protected: | 97 protected: |
| 104 WebApkInstaller(content::BrowserContext* browser_context, | 98 WebApkInstaller(content::BrowserContext* browser_context, |
| 105 const ShortcutInfo& shortcut_info, | 99 const ShortcutInfo& shortcut_info, |
| 106 const SkBitmap& shortcut_icon); | 100 const SkBitmap& shortcut_icon); |
| 107 | 101 |
| 108 // Starts installion of the downloaded WebAPK. | 102 // Returns whether the device supports installing WebAPKs. |
| 109 // |file_path| is the file path that the WebAPK was downloaded to. | 103 virtual bool CanInstallWebApks(); |
| 110 // |package_name| is the package name of the WebAPK. | |
| 111 virtual void InstallDownloadedWebApk( | |
| 112 JNIEnv* env, | |
| 113 const base::android::ScopedJavaLocalRef<jstring>& java_file_path, | |
| 114 const base::android::ScopedJavaLocalRef<jstring>& java_package_name); | |
| 115 | |
| 116 // Starts update using the downloaded WebAPK. | |
| 117 // |file_path| is the file path that the WebAPK was downloaded to. | |
| 118 virtual void UpdateUsingDownloadedWebApk( | |
| 119 JNIEnv* env, | |
| 120 const base::android::ScopedJavaLocalRef<jstring>& java_file_path); | |
| 121 | |
| 122 // Returns whether Google Play supports installing WebAPKs. | |
| 123 virtual bool CanUseGooglePlayInstallService(); | |
| 124 | 104 |
| 125 // Called when the package name of the WebAPK is available and the install | 105 // Called when the package name of the WebAPK is available and the install |
| 126 // or update request is handled by Google Play. | 106 // or update request should be issued. |
| 127 virtual void InstallOrUpdateWebApkFromGooglePlay( | 107 virtual void InstallOrUpdateWebApk(const std::string& package_name, |
| 128 const std::string& package_name, | 108 int version, |
| 129 int version, | 109 const std::string& token); |
| 130 const std::string& token); | |
| 131 | 110 |
| 132 // Called when the install or update process has completed or failed. | 111 // Called when the install or update process has completed or failed. |
| 133 void OnResult(WebApkInstallResult result); | 112 void OnResult(WebApkInstallResult result); |
| 134 | 113 |
| 135 private: | 114 private: |
| 136 enum TaskType { | 115 enum TaskType { |
| 137 UNDEFINED, | 116 UNDEFINED, |
| 138 INSTALL, | 117 INSTALL, |
| 139 UPDATE, | 118 UPDATE, |
| 140 }; | 119 }; |
| (...skipping 26 matching lines...) Expand all Loading... |
| 167 void OnGotIconMurmur2Hash(const std::string& icon_murmur2_hash); | 146 void OnGotIconMurmur2Hash(const std::string& icon_murmur2_hash); |
| 168 | 147 |
| 169 // Sends request to WebAPK server to create WebAPK. During a successful | 148 // Sends request to WebAPK server to create WebAPK. During a successful |
| 170 // request the WebAPK server responds with the URL of the generated WebAPK. | 149 // request the WebAPK server responds with the URL of the generated WebAPK. |
| 171 // |webapk| is the proto to send to the WebAPK server. | 150 // |webapk| is the proto to send to the WebAPK server. |
| 172 void SendCreateWebApkRequest(std::unique_ptr<webapk::WebApk> webapk_proto); | 151 void SendCreateWebApkRequest(std::unique_ptr<webapk::WebApk> webapk_proto); |
| 173 | 152 |
| 174 // Sends request to WebAPK server to update a WebAPK. During a successful | 153 // Sends request to WebAPK server to update a WebAPK. During a successful |
| 175 // request the WebAPK server responds with the URL of the generated WebAPK. | 154 // request the WebAPK server responds with the URL of the generated WebAPK. |
| 176 // |webapk| is the proto to send to the WebAPK server. | 155 // |webapk| is the proto to send to the WebAPK server. |
| 177 void SendUpdateWebApkRequest(std::unique_ptr<webapk::WebApk> webapk_proto); | 156 void SendUpdateWebApkRequest(int webapk_version, |
| 157 std::unique_ptr<webapk::WebApk> webapk_proto); |
| 178 | 158 |
| 179 // Sends a request to WebAPK server to create/update WebAPK. During a | 159 // Sends a request to WebAPK server to create/update WebAPK. During a |
| 180 // successful request the WebAPK server responds with the URL of the generated | 160 // successful request the WebAPK server responds with the URL of the generated |
| 181 // WebAPK. | 161 // WebAPK. |
| 182 void SendRequest(std::unique_ptr<webapk::WebApk> request_proto, | 162 void SendRequest(std::unique_ptr<webapk::WebApk> request_proto, |
| 183 const GURL& server_url); | 163 const GURL& server_url); |
| 184 | 164 |
| 185 // Called with the URL of generated WebAPK and the package name that the | |
| 186 // WebAPK should be installed at. | |
| 187 void OnGotWebApkDownloadUrl(const GURL& download_url, | |
| 188 const std::string& package_name); | |
| 189 | |
| 190 // Downloads the WebAPK from the given |download_url|. | |
| 191 void DownloadWebApk(const base::FilePath& output_path, | |
| 192 const GURL& download_url, | |
| 193 bool retry_if_fails); | |
| 194 | |
| 195 // Called once the sub directory to store the downloaded WebAPK was | |
| 196 // created with permissions set properly or if creation failed. | |
| 197 void OnCreatedSubDirAndSetPermissions(const GURL& download_url, | |
| 198 const base::FilePath& file_path); | |
| 199 | |
| 200 // Called once the WebAPK has been downloaded. Makes the downloaded WebAPK | |
| 201 // world readable and installs the WebAPK if the download was successful. | |
| 202 // |file_path| is the file path that the WebAPK was downloaded to. | |
| 203 // If |retry_if_fails| is true, will post a delayed task and retry the | |
| 204 // download after 2 seconds. | |
| 205 void OnWebApkDownloaded(const base::FilePath& file_path, | |
| 206 const GURL& download_url, | |
| 207 bool retry_if_fails, | |
| 208 FileDownloader::Result result); | |
| 209 | |
| 210 // Called once the downloaded WebAPK has been made world readable. Installs | |
| 211 // the WebAPK. | |
| 212 // |file_path| is the file path that the WebAPK was downloaded to. | |
| 213 // |change_permission_success| is whether the WebAPK could be made world | |
| 214 // readable. | |
| 215 void OnWebApkMadeWorldReadable(const base::FilePath& file_path, | |
| 216 bool change_permission_success); | |
| 217 | |
| 218 net::URLRequestContextGetter* request_context_getter_; | 165 net::URLRequestContextGetter* request_context_getter_; |
| 219 | 166 |
| 220 // Sends HTTP request to WebAPK server. | 167 // Sends HTTP request to WebAPK server. |
| 221 std::unique_ptr<net::URLFetcher> url_fetcher_; | 168 std::unique_ptr<net::URLFetcher> url_fetcher_; |
| 222 | 169 |
| 223 // Downloads app icon and computes Murmur2 hash. | 170 // Downloads app icon and computes Murmur2 hash. |
| 224 std::unique_ptr<WebApkIconHasher> icon_hasher_; | 171 std::unique_ptr<WebApkIconHasher> icon_hasher_; |
| 225 | 172 |
| 226 // Downloads WebAPK. | 173 // Downloads WebAPK. |
| 227 std::unique_ptr<FileDownloader> downloader_; | 174 std::unique_ptr<FileDownloader> downloader_; |
| 228 | 175 |
| 229 // Fails WebApkInstaller if WebAPK server takes too long to respond or if the | 176 // Fails WebApkInstaller if WebAPK server takes too long to respond or if the |
| 230 // download takes too long. | 177 // download takes too long. |
| 231 base::OneShotTimer timer_; | 178 base::OneShotTimer timer_; |
| 232 | 179 |
| 233 // Callback to call once WebApkInstaller succeeds or fails. | 180 // Callback to call once WebApkInstaller succeeds or fails. |
| 234 FinishCallback finish_callback_; | 181 FinishCallback finish_callback_; |
| 235 | 182 |
| 236 // Web Manifest info. | 183 // Web Manifest info. |
| 237 const ShortcutInfo shortcut_info_; | 184 const ShortcutInfo shortcut_info_; |
| 238 | 185 |
| 239 // WebAPK app icon. | 186 // WebAPK app icon. |
| 240 const SkBitmap shortcut_icon_; | 187 const SkBitmap shortcut_icon_; |
| 241 | 188 |
| 242 // WebAPK server URL. | 189 // WebAPK server URL. |
| 243 GURL server_url_; | 190 GURL server_url_; |
| 244 | 191 |
| 245 // The number of milliseconds to wait for the WebAPK download URL from the | 192 // The number of milliseconds to wait for the WebAPK server to respond. |
| 246 // WebAPK server. | 193 int webapk_server_timeout_ms_; |
| 247 int webapk_download_url_timeout_ms_; | |
| 248 | |
| 249 // The number of milliseconds to wait for the WebAPK download to complete. | |
| 250 int download_timeout_ms_; | |
| 251 | 194 |
| 252 // WebAPK package name. | 195 // WebAPK package name. |
| 253 std::string webapk_package_; | 196 std::string webapk_package_; |
| 254 | 197 |
| 255 // Whether the server wants the WebAPK to request updates less frequently. | 198 // Whether the server wants the WebAPK to request updates less frequently. |
| 256 bool relax_updates_; | 199 bool relax_updates_; |
| 257 | 200 |
| 258 // WebAPK version code. | |
| 259 int webapk_version_; | |
| 260 | 201 |
| 261 // Indicates whether the installer is for installing or updating a WebAPK. | 202 // Indicates whether the installer is for installing or updating a WebAPK. |
| 262 TaskType task_type_; | 203 TaskType task_type_; |
| 263 | 204 |
| 264 // Points to the Java Object. | 205 // Points to the Java Object. |
| 265 base::android::ScopedJavaGlobalRef<jobject> java_ref_; | 206 base::android::ScopedJavaGlobalRef<jobject> java_ref_; |
| 266 | 207 |
| 267 // Used to get |weak_ptr_|. | 208 // Used to get |weak_ptr_|. |
| 268 base::WeakPtrFactory<WebApkInstaller> weak_ptr_factory_; | 209 base::WeakPtrFactory<WebApkInstaller> weak_ptr_factory_; |
| 269 | 210 |
| 270 DISALLOW_COPY_AND_ASSIGN(WebApkInstaller); | 211 DISALLOW_COPY_AND_ASSIGN(WebApkInstaller); |
| 271 }; | 212 }; |
| 272 | 213 |
| 273 #endif // CHROME_BROWSER_ANDROID_WEBAPK_WEBAPK_INSTALLER_H_ | 214 #endif // CHROME_BROWSER_ANDROID_WEBAPK_WEBAPK_INSTALLER_H_ |
| OLD | NEW |