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