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> |
11 | 12 |
12 #include "base/android/scoped_java_ref.h" | 13 #include "base/android/scoped_java_ref.h" |
13 #include "base/callback.h" | 14 #include "base/callback.h" |
14 #include "base/macros.h" | 15 #include "base/macros.h" |
15 #include "base/memory/weak_ptr.h" | 16 #include "base/memory/weak_ptr.h" |
| 17 #include "base/strings/string16.h" |
16 #include "base/timer/timer.h" | 18 #include "base/timer/timer.h" |
17 #include "chrome/browser/android/shortcut_info.h" | 19 #include "chrome/browser/android/shortcut_info.h" |
18 #include "chrome/browser/android/webapk/webapk_install_service.h" | 20 #include "chrome/browser/android/webapk/webapk_install_service.h" |
19 #include "net/url_request/url_fetcher.h" | 21 #include "net/url_request/url_fetcher.h" |
20 #include "net/url_request/url_fetcher_delegate.h" | 22 #include "net/url_request/url_fetcher_delegate.h" |
21 #include "third_party/skia/include/core/SkBitmap.h" | 23 #include "third_party/skia/include/core/SkBitmap.h" |
22 | 24 |
23 namespace base { | 25 namespace base { |
24 class ElapsedTimer; | 26 class ElapsedTimer; |
25 } | 27 } |
26 | 28 |
27 namespace content { | 29 namespace content { |
28 class BrowserContext; | 30 class BrowserContext; |
29 } | 31 } |
30 | 32 |
31 namespace webapk { | 33 // Talks to Chrome WebAPK server to download metadata about a WebAPK and issue |
32 class WebApk; | |
33 } | |
34 | |
35 // Talks to Chrome WebAPK server to download metadata about a WebApk and issue | |
36 // 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 |
37 // WebApkInstaller counterpart. | 35 // WebApkInstaller counterpart. |
38 class WebApkInstaller : public net::URLFetcherDelegate { | 36 class WebApkInstaller : public net::URLFetcherDelegate { |
39 public: | 37 public: |
40 using FinishCallback = WebApkInstallService::FinishCallback; | 38 using FinishCallback = WebApkInstallService::FinishCallback; |
41 | 39 |
42 ~WebApkInstaller() override; | 40 ~WebApkInstaller() override; |
43 | 41 |
44 // Creates a self-owned WebApkInstaller instance and talks to the Chrome | 42 // Creates a self-owned WebApkInstaller instance and talks to the Chrome |
45 // 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 |
46 // 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. |
47 static void InstallAsync(content::BrowserContext* context, | 45 static void InstallAsync(content::BrowserContext* context, |
48 const ShortcutInfo& shortcut_info, | 46 const ShortcutInfo& shortcut_info, |
49 const SkBitmap& primary_icon, | 47 const SkBitmap& primary_icon, |
50 const SkBitmap& badge_icon, | 48 const SkBitmap& badge_icon, |
51 const FinishCallback& finish_callback); | 49 const FinishCallback& finish_callback); |
52 | 50 |
53 // Creates a self-owned WebApkInstaller instance and talks to the Chrome | 51 // Creates a self-owned WebApkInstaller instance and talks to the Chrome |
54 // 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 |
55 // 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. |
56 static void UpdateAsync( | 54 static void UpdateAsync( |
57 content::BrowserContext* context, | 55 content::BrowserContext* context, |
58 const ShortcutInfo& shortcut_info, | |
59 const SkBitmap& primary_icon, | |
60 const SkBitmap& badge_icon, | |
61 const std::string& webapk_package, | 56 const std::string& webapk_package, |
62 int webapk_version, | 57 const GURL& start_url, |
63 const std::map<std::string, std::string>& icon_url_to_murmur2_hash, | 58 const base::string16& short_name, |
64 bool is_manifest_stale, | 59 std::unique_ptr<std::vector<uint8_t>> serialized_proto, |
65 const FinishCallback& callback); | 60 const FinishCallback& callback); |
66 | 61 |
67 // Calls the private function |InstallAsync| for testing. | 62 // Calls the private function |InstallAsync| for testing. |
68 // Should be used only for testing. | 63 // Should be used only for testing. |
69 static void InstallAsyncForTesting(WebApkInstaller* installer, | 64 static void InstallAsyncForTesting(WebApkInstaller* installer, |
| 65 const ShortcutInfo& shortcut_info, |
| 66 const SkBitmap& primary_icon, |
| 67 const SkBitmap& badge_icon, |
70 const FinishCallback& finish_callback); | 68 const FinishCallback& finish_callback); |
71 | 69 |
72 // Calls the private function |UpdateAsync| for testing. | 70 // Calls the private function |UpdateAsync| for testing. |
73 // Should be used only for testing. | 71 // Should be used only for testing. |
74 static void UpdateAsyncForTesting( | 72 static void UpdateAsyncForTesting( |
75 WebApkInstaller* installer, | 73 WebApkInstaller* installer, |
76 const std::string& webapk_package, | 74 const std::string& webapk_package, |
77 int webapk_version, | 75 const GURL& start_url, |
78 const std::map<std::string, std::string>& icon_url_to_murmur2_hash, | 76 const base::string16& short_name, |
79 bool is_manifest_stale, | 77 std::unique_ptr<std::vector<uint8_t>> serialized_proto, |
80 const FinishCallback& callback); | 78 const FinishCallback& callback); |
81 | 79 |
82 // Sets the timeout for the server requests. | 80 // Sets the timeout for the server requests. |
83 void SetTimeoutMs(int timeout_ms); | 81 void SetTimeoutMs(int timeout_ms); |
84 | 82 |
85 // Called once the installation is complete or failed. | 83 // Called once the installation is complete or failed. |
86 void OnInstallFinished(JNIEnv* env, | 84 void OnInstallFinished(JNIEnv* env, |
87 const base::android::JavaParamRef<jobject>& obj, | 85 const base::android::JavaParamRef<jobject>& obj, |
88 jint result); | 86 jint result); |
89 | 87 |
90 // Asynchronously builds the WebAPK proto on a background thread for an update | 88 // Asynchronously builds the WebAPK proto on a background thread for an update |
91 // or install request. Runs |callback| on the calling thread when complete. | 89 // or install request. Runs |callback| on the calling thread when complete. |
92 static void BuildProto( | 90 static void BuildProto( |
93 const ShortcutInfo& shortcut_info, | 91 const ShortcutInfo& shortcut_info, |
94 const SkBitmap& primary_icon, | 92 const SkBitmap& primary_icon, |
95 const SkBitmap& badge_icon, | 93 const SkBitmap& badge_icon, |
96 const std::string& package_name, | 94 const std::string& package_name, |
97 const std::string& version, | 95 const std::string& version, |
98 const std::map<std::string, std::string>& icon_url_to_murmur2_hash, | 96 const std::map<std::string, std::string>& icon_url_to_murmur2_hash, |
99 bool is_manifest_stale, | 97 bool is_manifest_stale, |
100 const base::Callback<void(std::unique_ptr<webapk::WebApk>)>& callback); | 98 const base::Callback<void(std::unique_ptr<std::vector<uint8_t>>)>& |
| 99 callback); |
101 | 100 |
102 // Registers JNI hooks. | 101 // Registers JNI hooks. |
103 static bool Register(JNIEnv* env); | 102 static bool Register(JNIEnv* env); |
104 | 103 |
105 protected: | 104 protected: |
106 WebApkInstaller(content::BrowserContext* browser_context, | 105 explicit WebApkInstaller(content::BrowserContext* browser_context); |
107 const ShortcutInfo& shortcut_info, | |
108 const SkBitmap& primary_icon, | |
109 const SkBitmap& badge_icon); | |
110 | 106 |
111 // 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 |
112 // or update request should be issued. | 108 // or update request should be issued. |
113 virtual void InstallOrUpdateWebApk(const std::string& package_name, | 109 virtual void InstallOrUpdateWebApk(const std::string& package_name, |
114 int version, | 110 int version, |
115 const std::string& token); | 111 const std::string& token); |
116 | 112 |
117 // Called when the install or update process has completed or failed. | 113 // Called when the install or update process has completed or failed. |
118 void OnResult(WebApkInstallResult result); | 114 void OnResult(WebApkInstallResult result); |
119 | 115 |
120 private: | 116 private: |
121 enum TaskType { | 117 enum TaskType { |
122 UNDEFINED, | 118 UNDEFINED, |
123 INSTALL, | 119 INSTALL, |
124 UPDATE, | 120 UPDATE, |
125 }; | 121 }; |
126 | 122 |
127 // Create the Java object. | 123 // Create the Java object. |
128 void CreateJavaRef(); | 124 void CreateJavaRef(); |
129 | 125 |
130 // Talks to the Chrome WebAPK server to generate a WebAPK on the server and to | 126 // Talks to the Chrome WebAPK server to generate a WebAPK on the server and to |
131 // Google Play to install the downloaded WebAPK. Calls |callback| once the | 127 // Google Play to install the downloaded WebAPK. Calls |callback| once the |
132 // install completed or failed. | 128 // install completed or failed. |
133 void InstallAsync(const FinishCallback& finish_callback); | 129 void InstallAsync(const ShortcutInfo& shortcut_info, |
| 130 const SkBitmap& primary_icon, |
| 131 const SkBitmap& badge_icon, |
| 132 const FinishCallback& finish_callback); |
134 | 133 |
135 // Talks to the Chrome WebAPK server to update a WebAPK on the server and to | 134 // Talks to the Chrome WebAPK server to update a WebAPK on the server and to |
136 // the Google Play server to install the downloaded WebAPK. Calls |callback| | 135 // the Google Play server to install the downloaded WebAPK. Calls |
137 // after the request to install the WebAPK is sent to the Google Play server. | 136 // |finish_callback| once the update completed or failed. |
138 void UpdateAsync( | 137 void UpdateAsync(const std::string& webapk_package, |
139 const std::string& webapk_package, | 138 const GURL& start_url, |
140 int webapk_version, | 139 const base::string16& short_name, |
141 const std::map<std::string, std::string>& icon_url_to_murmur2_hash, | 140 const std::unique_ptr<std::vector<uint8_t>> serialized_proto, |
142 bool is_manifest_stale, | 141 const FinishCallback& callback); |
143 const FinishCallback& callback); | |
144 | 142 |
145 // net::URLFetcherDelegate: | 143 // net::URLFetcherDelegate: |
146 void OnURLFetchComplete(const net::URLFetcher* source) override; | 144 void OnURLFetchComplete(const net::URLFetcher* source) override; |
147 | 145 |
148 // Called with the computed Murmur2 hash for the primary icon. | 146 // Called with the computed Murmur2 hash for the primary icon. |
149 void OnGotPrimaryIconMurmur2Hash(const std::string& primary_icon_hash); | 147 void OnGotPrimaryIconMurmur2Hash(const std::string& primary_icon_hash); |
150 | 148 |
151 // Called with the computed Murmur2 hash for the badge icon, and | 149 // Called with the computed Murmur2 hash for the badge icon, and |
152 // |did_fetch_badge_icon| to indicate whether there was an attempt to fetch | 150 // |did_fetch_badge_icon| to indicate whether there was an attempt to fetch |
153 // badge icon. | 151 // badge icon. |
154 void OnGotBadgeIconMurmur2Hash(bool did_fetch_badge_icon, | 152 void OnGotBadgeIconMurmur2Hash(bool did_fetch_badge_icon, |
155 const std::string& primary_icon_hash, | 153 const std::string& primary_icon_hash, |
156 const std::string& badge_icon_hash); | 154 const std::string& badge_icon_hash); |
157 | 155 |
158 // Sends a request to WebAPK server to create/update WebAPK. During a | 156 // Sends a request to WebAPK server to create/update WebAPK. During a |
159 // successful request the WebAPK server responds with the URL of the generated | 157 // successful request the WebAPK server responds with a token to send to |
160 // WebAPK. | 158 // Google Play. |
161 void SendRequest(std::unique_ptr<webapk::WebApk> request_proto); | 159 void SendRequest(std::unique_ptr<std::vector<uint8_t>> serialized_proto); |
162 | 160 |
163 net::URLRequestContextGetter* request_context_getter_; | 161 net::URLRequestContextGetter* request_context_getter_; |
164 | 162 |
165 // Sends HTTP request to WebAPK server. | 163 // Sends HTTP request to WebAPK server. |
166 std::unique_ptr<net::URLFetcher> url_fetcher_; | 164 std::unique_ptr<net::URLFetcher> url_fetcher_; |
167 | 165 |
168 // Fails WebApkInstaller if WebAPK server takes too long to respond or if the | 166 // Fails WebApkInstaller if WebAPK server takes too long to respond or if the |
169 // download takes too long. | 167 // download takes too long. |
170 base::OneShotTimer timer_; | 168 base::OneShotTimer timer_; |
171 | 169 |
172 // Tracks how long it takes to install a WebAPK. | 170 // Tracks how long it takes to install a WebAPK. |
173 std::unique_ptr<base::ElapsedTimer> install_duration_timer_; | 171 std::unique_ptr<base::ElapsedTimer> install_duration_timer_; |
174 | 172 |
175 // Callback to call once WebApkInstaller succeeds or fails. | 173 // Callback to call once WebApkInstaller succeeds or fails. |
176 FinishCallback finish_callback_; | 174 FinishCallback finish_callback_; |
177 | 175 |
178 // Web Manifest info. | 176 // Data for installs. |
179 const ShortcutInfo shortcut_info_; | 177 std::unique_ptr<ShortcutInfo> install_shortcut_info_; |
| 178 SkBitmap install_primary_icon_; |
| 179 SkBitmap install_badge_icon_; |
180 | 180 |
181 // WebAPK primary icon. | 181 GURL start_url_; |
182 const SkBitmap primary_icon_; | 182 base::string16 short_name_; |
183 | |
184 // WebAPK badge icon. | |
185 const SkBitmap badge_icon_; | |
186 | 183 |
187 // WebAPK server URL. | 184 // WebAPK server URL. |
188 GURL server_url_; | 185 GURL server_url_; |
189 | 186 |
190 // The number of milliseconds to wait for the WebAPK server to respond. | 187 // The number of milliseconds to wait for the WebAPK server to respond. |
191 int webapk_server_timeout_ms_; | 188 int webapk_server_timeout_ms_; |
192 | 189 |
193 // WebAPK package name. | 190 // WebAPK package name. |
194 std::string webapk_package_; | 191 std::string webapk_package_; |
195 | 192 |
196 // Whether the server wants the WebAPK to request updates less frequently. | 193 // Whether the server wants the WebAPK to request updates less frequently. |
197 bool relax_updates_; | 194 bool relax_updates_; |
198 | 195 |
199 // Indicates whether the installer is for installing or updating a WebAPK. | 196 // Indicates whether the installer is for installing or updating a WebAPK. |
200 TaskType task_type_; | 197 TaskType task_type_; |
201 | 198 |
202 // Points to the Java Object. | 199 // Points to the Java Object. |
203 base::android::ScopedJavaGlobalRef<jobject> java_ref_; | 200 base::android::ScopedJavaGlobalRef<jobject> java_ref_; |
204 | 201 |
205 // Used to get |weak_ptr_|. | 202 // Used to get |weak_ptr_|. |
206 base::WeakPtrFactory<WebApkInstaller> weak_ptr_factory_; | 203 base::WeakPtrFactory<WebApkInstaller> weak_ptr_factory_; |
207 | 204 |
208 DISALLOW_COPY_AND_ASSIGN(WebApkInstaller); | 205 DISALLOW_COPY_AND_ASSIGN(WebApkInstaller); |
209 }; | 206 }; |
210 | 207 |
211 #endif // CHROME_BROWSER_ANDROID_WEBAPK_WEBAPK_INSTALLER_H_ | 208 #endif // CHROME_BROWSER_ANDROID_WEBAPK_WEBAPK_INSTALLER_H_ |
OLD | NEW |