| 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 #include <vector> | |
| 12 | 11 |
| 13 #include "base/android/scoped_java_ref.h" | 12 #include "base/android/scoped_java_ref.h" |
| 14 #include "base/callback.h" | 13 #include "base/callback.h" |
| 15 #include "base/macros.h" | 14 #include "base/macros.h" |
| 16 #include "base/memory/weak_ptr.h" | 15 #include "base/memory/weak_ptr.h" |
| 17 #include "base/strings/string16.h" | 16 #include "base/strings/string16.h" |
| 18 #include "base/timer/timer.h" | 17 #include "base/timer/timer.h" |
| 19 #include "chrome/browser/android/shortcut_info.h" | 18 #include "chrome/browser/android/shortcut_info.h" |
| 20 #include "chrome/browser/android/webapk/webapk_install_service.h" | 19 #include "chrome/browser/android/webapk/webapk_install_service.h" |
| 21 #include "net/url_request/url_fetcher.h" | 20 #include "net/url_request/url_fetcher.h" |
| 22 #include "net/url_request/url_fetcher_delegate.h" | 21 #include "net/url_request/url_fetcher_delegate.h" |
| 23 #include "third_party/skia/include/core/SkBitmap.h" | 22 #include "third_party/skia/include/core/SkBitmap.h" |
| 24 | 23 |
| 25 namespace base { | 24 namespace base { |
| 26 class ElapsedTimer; | 25 class ElapsedTimer; |
| 26 class FilePath; |
| 27 } | 27 } |
| 28 | 28 |
| 29 namespace content { | 29 namespace content { |
| 30 class BrowserContext; | 30 class BrowserContext; |
| 31 } | 31 } |
| 32 | 32 |
| 33 // Talks to Chrome WebAPK server to download metadata about a WebAPK and issue | 33 // Talks to Chrome WebAPK server to download metadata about a WebAPK and issue |
| 34 // a request for it to be installed. The native WebApkInstaller owns the Java | 34 // a request for it to be installed. The native WebApkInstaller owns the Java |
| 35 // WebApkInstaller counterpart. | 35 // WebApkInstaller counterpart. |
| 36 class WebApkInstaller : public net::URLFetcherDelegate { | 36 class WebApkInstaller : public net::URLFetcherDelegate { |
| 37 public: | 37 public: |
| 38 using FinishCallback = WebApkInstallService::FinishCallback; | 38 using FinishCallback = WebApkInstallService::FinishCallback; |
| 39 | 39 |
| 40 ~WebApkInstaller() override; | 40 ~WebApkInstaller() override; |
| 41 | 41 |
| 42 // Creates a self-owned WebApkInstaller instance and talks to the Chrome | 42 // Creates a self-owned WebApkInstaller instance and talks to the Chrome |
| 43 // WebAPK server to generate a WebAPK on the server and locally requests the | 43 // WebAPK server to generate a WebAPK on the server and locally requests the |
| 44 // APK to be installed. Calls |callback| once the install completed or failed. | 44 // APK to be installed. Calls |callback| once the install completed or failed. |
| 45 static void InstallAsync(content::BrowserContext* context, | 45 static void InstallAsync(content::BrowserContext* context, |
| 46 const ShortcutInfo& shortcut_info, | 46 const ShortcutInfo& shortcut_info, |
| 47 const SkBitmap& primary_icon, | 47 const SkBitmap& primary_icon, |
| 48 const SkBitmap& badge_icon, | 48 const SkBitmap& badge_icon, |
| 49 const FinishCallback& finish_callback); | 49 const FinishCallback& finish_callback); |
| 50 | 50 |
| 51 // Creates a self-owned WebApkInstaller instance and talks to the Chrome | 51 // Creates a self-owned WebApkInstaller instance and talks to the Chrome |
| 52 // WebAPK server to update a WebAPK on the server and locally requests the | 52 // WebAPK server to update a WebAPK on the server and locally requests the |
| 53 // APK to be installed. Calls |callback| once the install completed or failed. | 53 // APK to be installed. Calls |callback| once the install completed or failed. |
| 54 static void UpdateAsync( | 54 // |update_request_file_path| is the path of the file with the update request. |
| 55 content::BrowserContext* context, | 55 static void UpdateAsync(content::BrowserContext* context, |
| 56 const std::string& webapk_package, | 56 const base::FilePath& update_request_file, |
| 57 const GURL& start_url, | 57 const FinishCallback& callback); |
| 58 const base::string16& short_name, | |
| 59 std::unique_ptr<std::vector<uint8_t>> serialized_proto, | |
| 60 const FinishCallback& callback); | |
| 61 | 58 |
| 62 // Calls the private function |InstallAsync| for testing. | 59 // Calls the private function |InstallAsync| for testing. |
| 63 // Should be used only for testing. | 60 // Should be used only for testing. |
| 64 static void InstallAsyncForTesting(WebApkInstaller* installer, | 61 static void InstallAsyncForTesting(WebApkInstaller* installer, |
| 65 const ShortcutInfo& shortcut_info, | 62 const ShortcutInfo& shortcut_info, |
| 66 const SkBitmap& primary_icon, | 63 const SkBitmap& primary_icon, |
| 67 const SkBitmap& badge_icon, | 64 const SkBitmap& badge_icon, |
| 68 const FinishCallback& finish_callback); | 65 const FinishCallback& callback); |
| 69 | 66 |
| 70 // Calls the private function |UpdateAsync| for testing. | 67 // Calls the private function |UpdateAsync| for testing. |
| 71 // Should be used only for testing. | 68 // Should be used only for testing. |
| 72 static void UpdateAsyncForTesting( | 69 static void UpdateAsyncForTesting(WebApkInstaller* installer, |
| 73 WebApkInstaller* installer, | 70 const base::FilePath& update_request_file, |
| 74 const std::string& webapk_package, | 71 const FinishCallback& callback); |
| 75 const GURL& start_url, | |
| 76 const base::string16& short_name, | |
| 77 std::unique_ptr<std::vector<uint8_t>> serialized_proto, | |
| 78 const FinishCallback& callback); | |
| 79 | 72 |
| 80 // Sets the timeout for the server requests. | 73 // Sets the timeout for the server requests. |
| 81 void SetTimeoutMs(int timeout_ms); | 74 void SetTimeoutMs(int timeout_ms); |
| 82 | 75 |
| 83 // Called once the installation is complete or failed. | 76 // Called once the installation is complete or failed. |
| 84 void OnInstallFinished(JNIEnv* env, | 77 void OnInstallFinished(JNIEnv* env, |
| 85 const base::android::JavaParamRef<jobject>& obj, | 78 const base::android::JavaParamRef<jobject>& obj, |
| 86 jint result); | 79 jint result); |
| 87 | 80 |
| 88 // Asynchronously builds the WebAPK proto on a background thread for an update | 81 // Asynchronously builds the WebAPK proto on a background thread for an update |
| 89 // or install request. Runs |callback| on the calling thread when complete. | 82 // or install request. Runs |callback| on the calling thread when complete. |
| 90 static void BuildProto( | 83 static void BuildProto( |
| 91 const ShortcutInfo& shortcut_info, | 84 const ShortcutInfo& shortcut_info, |
| 92 const SkBitmap& primary_icon, | 85 const SkBitmap& primary_icon, |
| 93 const SkBitmap& badge_icon, | 86 const SkBitmap& badge_icon, |
| 94 const std::string& package_name, | 87 const std::string& package_name, |
| 95 const std::string& version, | 88 const std::string& version, |
| 96 const std::map<std::string, std::string>& icon_url_to_murmur2_hash, | 89 const std::map<std::string, std::string>& icon_url_to_murmur2_hash, |
| 97 bool is_manifest_stale, | 90 bool is_manifest_stale, |
| 98 const base::Callback<void(std::unique_ptr<std::vector<uint8_t>>)>& | 91 const base::Callback<void(std::unique_ptr<std::string>)>& callback); |
| 99 callback); | 92 |
| 93 // Builds the WebAPK proto for an update or an install request and stores it |
| 94 // to |file|. Runs |callback| with a boolean indicating whether the proto was |
| 95 // successfully written to disk. |
| 96 static void StoreUpdateRequestToDisk( |
| 97 const base::FilePath& file, |
| 98 const ShortcutInfo& shortcut_info, |
| 99 const SkBitmap& primary_icon, |
| 100 const SkBitmap& badge_icon, |
| 101 const std::string& package_name, |
| 102 const std::string& version, |
| 103 const std::map<std::string, std::string>& icon_url_to_murmur2_hash, |
| 104 bool is_manifest_stale, |
| 105 const base::Callback<void(bool)> callback); |
| 100 | 106 |
| 101 protected: | 107 protected: |
| 102 explicit WebApkInstaller(content::BrowserContext* browser_context); | 108 explicit WebApkInstaller(content::BrowserContext* browser_context); |
| 103 | 109 |
| 104 // Called when the package name of the WebAPK is available and the install | 110 // Called when the package name of the WebAPK is available and the install |
| 105 // or update request should be issued. | 111 // or update request should be issued. |
| 106 virtual void InstallOrUpdateWebApk(const std::string& package_name, | 112 virtual void InstallOrUpdateWebApk(const std::string& package_name, |
| 107 int version, | 113 int version, |
| 108 const std::string& token); | 114 const std::string& token); |
| 109 | 115 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 122 | 128 |
| 123 // Talks to the Chrome WebAPK server to generate a WebAPK on the server and to | 129 // Talks to the Chrome WebAPK server to generate a WebAPK on the server and to |
| 124 // Google Play to install the downloaded WebAPK. Calls |callback| once the | 130 // Google Play to install the downloaded WebAPK. Calls |callback| once the |
| 125 // install completed or failed. | 131 // install completed or failed. |
| 126 void InstallAsync(const ShortcutInfo& shortcut_info, | 132 void InstallAsync(const ShortcutInfo& shortcut_info, |
| 127 const SkBitmap& primary_icon, | 133 const SkBitmap& primary_icon, |
| 128 const SkBitmap& badge_icon, | 134 const SkBitmap& badge_icon, |
| 129 const FinishCallback& finish_callback); | 135 const FinishCallback& finish_callback); |
| 130 | 136 |
| 131 // Talks to the Chrome WebAPK server to update a WebAPK on the server and to | 137 // Talks to the Chrome WebAPK server to update a WebAPK on the server and to |
| 132 // the Google Play server to install the downloaded WebAPK. Calls | 138 // the Google Play server to install the downloaded WebAPK. |
| 133 // |finish_callback| once the update completed or failed. | 139 // |update_request_file_path| is the path of the file with the update request. |
| 134 void UpdateAsync(const std::string& webapk_package, | 140 // Calls |finish_callback| once the update completed or failed. |
| 135 const GURL& start_url, | 141 void UpdateAsync(const base::FilePath& update_request_file, |
| 136 const base::string16& short_name, | 142 const FinishCallback& finish_callback); |
| 137 const std::unique_ptr<std::vector<uint8_t>> serialized_proto, | 143 |
| 138 const FinishCallback& callback); | 144 // Called with the contents of the update request file. |
| 145 void OnReadUpdateRequest(std::unique_ptr<std::string> update_request); |
| 139 | 146 |
| 140 // net::URLFetcherDelegate: | 147 // net::URLFetcherDelegate: |
| 141 void OnURLFetchComplete(const net::URLFetcher* source) override; | 148 void OnURLFetchComplete(const net::URLFetcher* source) override; |
| 142 | 149 |
| 143 // Called with the computed Murmur2 hash for the primary icon. | 150 // Called with the computed Murmur2 hash for the primary icon. |
| 144 void OnGotPrimaryIconMurmur2Hash(const std::string& primary_icon_hash); | 151 void OnGotPrimaryIconMurmur2Hash(const std::string& primary_icon_hash); |
| 145 | 152 |
| 146 // Called with the computed Murmur2 hash for the badge icon, and | 153 // Called with the computed Murmur2 hash for the badge icon, and |
| 147 // |did_fetch_badge_icon| to indicate whether there was an attempt to fetch | 154 // |did_fetch_badge_icon| to indicate whether there was an attempt to fetch |
| 148 // badge icon. | 155 // badge icon. |
| 149 void OnGotBadgeIconMurmur2Hash(bool did_fetch_badge_icon, | 156 void OnGotBadgeIconMurmur2Hash(bool did_fetch_badge_icon, |
| 150 const std::string& primary_icon_hash, | 157 const std::string& primary_icon_hash, |
| 151 const std::string& badge_icon_hash); | 158 const std::string& badge_icon_hash); |
| 152 | 159 |
| 153 // Sends a request to WebAPK server to create/update WebAPK. During a | 160 // Sends a request to WebAPK server to create/update WebAPK. During a |
| 154 // successful request the WebAPK server responds with a token to send to | 161 // successful request the WebAPK server responds with a token to send to |
| 155 // Google Play. | 162 // Google Play. |
| 156 void SendRequest(std::unique_ptr<std::vector<uint8_t>> serialized_proto); | 163 void SendRequest(std::unique_ptr<std::string> serialized_proto); |
| 157 | 164 |
| 158 net::URLRequestContextGetter* request_context_getter_; | 165 net::URLRequestContextGetter* request_context_getter_; |
| 159 | 166 |
| 160 // Sends HTTP request to WebAPK server. | 167 // Sends HTTP request to WebAPK server. |
| 161 std::unique_ptr<net::URLFetcher> url_fetcher_; | 168 std::unique_ptr<net::URLFetcher> url_fetcher_; |
| 162 | 169 |
| 163 // Fails WebApkInstaller if WebAPK server takes too long to respond or if the | 170 // Fails WebApkInstaller if WebAPK server takes too long to respond or if the |
| 164 // download takes too long. | 171 // download takes too long. |
| 165 base::OneShotTimer timer_; | 172 base::OneShotTimer timer_; |
| 166 | 173 |
| (...skipping 29 matching lines...) Expand all Loading... |
| 196 // Points to the Java Object. | 203 // Points to the Java Object. |
| 197 base::android::ScopedJavaGlobalRef<jobject> java_ref_; | 204 base::android::ScopedJavaGlobalRef<jobject> java_ref_; |
| 198 | 205 |
| 199 // Used to get |weak_ptr_|. | 206 // Used to get |weak_ptr_|. |
| 200 base::WeakPtrFactory<WebApkInstaller> weak_ptr_factory_; | 207 base::WeakPtrFactory<WebApkInstaller> weak_ptr_factory_; |
| 201 | 208 |
| 202 DISALLOW_COPY_AND_ASSIGN(WebApkInstaller); | 209 DISALLOW_COPY_AND_ASSIGN(WebApkInstaller); |
| 203 }; | 210 }; |
| 204 | 211 |
| 205 #endif // CHROME_BROWSER_ANDROID_WEBAPK_WEBAPK_INSTALLER_H_ | 212 #endif // CHROME_BROWSER_ANDROID_WEBAPK_WEBAPK_INSTALLER_H_ |
| OLD | NEW |