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 #include "chrome/browser/android/webapk/webapk_installer.h" | 5 #include "chrome/browser/android/webapk/webapk_installer.h" |
6 | 6 |
7 #include "base/android/build_info.h" | 7 #include "base/android/build_info.h" |
8 #include "base/android/jni_android.h" | 8 #include "base/android/jni_android.h" |
9 #include "base/android/jni_string.h" | 9 #include "base/android/jni_string.h" |
10 #include "base/android/path_utils.h" | 10 #include "base/android/path_utils.h" |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
103 #else | 103 #else |
104 #error "Unsupported target abi" | 104 #error "Unsupported target abi" |
105 #endif | 105 #endif |
106 } | 106 } |
107 | 107 |
108 // Populates webapk::WebApk and returns it. | 108 // Populates webapk::WebApk and returns it. |
109 // Must be called on a worker thread because it encodes an SkBitmap. | 109 // Must be called on a worker thread because it encodes an SkBitmap. |
110 std::unique_ptr<webapk::WebApk> BuildWebApkProtoInBackground( | 110 std::unique_ptr<webapk::WebApk> BuildWebApkProtoInBackground( |
111 const ShortcutInfo& shortcut_info, | 111 const ShortcutInfo& shortcut_info, |
112 const SkBitmap& shortcut_icon, | 112 const SkBitmap& shortcut_icon, |
113 const std::string& shortcut_icon_murmur2_hash) { | 113 bool stale_manifest, |
| 114 std::map<std::string, std::string> icon_url_to_murmur2_hash_map) { |
114 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); | 115 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); |
115 | 116 |
116 std::unique_ptr<webapk::WebApk> webapk(new webapk::WebApk); | 117 std::unique_ptr<webapk::WebApk> webapk(new webapk::WebApk); |
117 webapk->set_manifest_url(shortcut_info.manifest_url.spec()); | 118 webapk->set_manifest_url(shortcut_info.manifest_url.spec()); |
118 webapk->set_requester_application_package( | 119 webapk->set_requester_application_package( |
119 base::android::BuildInfo::GetInstance()->package_name()); | 120 base::android::BuildInfo::GetInstance()->package_name()); |
120 webapk->set_requester_application_version(version_info::GetVersionNumber()); | 121 webapk->set_requester_application_version(version_info::GetVersionNumber()); |
121 webapk->set_android_abi(getCurrentAbi()); | 122 webapk->set_android_abi(getCurrentAbi()); |
122 | 123 webapk->set_stale_manifest(stale_manifest); |
123 // TODO(hanxi): crbug.com/665078. Add a flag in WebAPK's proto to indicate | |
124 // that the Web Manifest data in the proto might be stale. | |
125 if (shortcut_icon_murmur2_hash.empty()) | |
126 return webapk; | |
127 | 124 |
128 webapk::WebAppManifest* web_app_manifest = webapk->mutable_manifest(); | 125 webapk::WebAppManifest* web_app_manifest = webapk->mutable_manifest(); |
129 web_app_manifest->set_name(base::UTF16ToUTF8(shortcut_info.name)); | 126 web_app_manifest->set_name(base::UTF16ToUTF8(shortcut_info.name)); |
130 web_app_manifest->set_short_name( | 127 web_app_manifest->set_short_name( |
131 base::UTF16ToUTF8(shortcut_info.short_name)); | 128 base::UTF16ToUTF8(shortcut_info.short_name)); |
132 web_app_manifest->set_start_url(shortcut_info.url.spec()); | 129 web_app_manifest->set_start_url(shortcut_info.url.spec()); |
133 web_app_manifest->set_orientation( | 130 web_app_manifest->set_orientation( |
134 content::WebScreenOrientationLockTypeToString( | 131 content::WebScreenOrientationLockTypeToString( |
135 shortcut_info.orientation)); | 132 shortcut_info.orientation)); |
136 web_app_manifest->set_display_mode( | 133 web_app_manifest->set_display_mode( |
137 content::WebDisplayModeToString(shortcut_info.display)); | 134 content::WebDisplayModeToString(shortcut_info.display)); |
138 web_app_manifest->set_background_color( | 135 web_app_manifest->set_background_color( |
139 ColorToString(shortcut_info.background_color)); | 136 ColorToString(shortcut_info.background_color)); |
140 web_app_manifest->set_theme_color(ColorToString(shortcut_info.theme_color)); | 137 web_app_manifest->set_theme_color(ColorToString(shortcut_info.theme_color)); |
141 | 138 |
142 std::string* scope = web_app_manifest->add_scopes(); | 139 std::string* scope = web_app_manifest->add_scopes(); |
143 scope->assign(GetScope(shortcut_info).spec()); | 140 scope->assign(GetScope(shortcut_info).spec()); |
144 | 141 |
145 webapk::Image* best_image = web_app_manifest->add_icons(); | 142 webapk::Image* best_image = web_app_manifest->add_icons(); |
146 best_image->set_src(shortcut_info.best_icon_url.spec()); | 143 std::string best_icon_url = shortcut_info.best_icon_url.spec(); |
147 best_image->set_hash(shortcut_icon_murmur2_hash); | 144 best_image->set_src(best_icon_url); |
| 145 std::map<std::string, std::string>::iterator it = |
| 146 icon_url_to_murmur2_hash_map.find(best_icon_url); |
| 147 if (it != icon_url_to_murmur2_hash_map.end()) |
| 148 best_image->set_hash(it->second); |
148 std::vector<unsigned char> png_bytes; | 149 std::vector<unsigned char> png_bytes; |
149 gfx::PNGCodec::EncodeBGRASkBitmap(shortcut_icon, false, &png_bytes); | 150 gfx::PNGCodec::EncodeBGRASkBitmap(shortcut_icon, false, &png_bytes); |
150 best_image->set_image_data(&png_bytes.front(), png_bytes.size()); | 151 best_image->set_image_data(&png_bytes.front(), png_bytes.size()); |
151 | 152 |
152 for (const std::string& icon_url : shortcut_info.icon_urls) { | 153 for (const std::pair<std::string, std::string>& entry : |
153 if (icon_url == shortcut_info.best_icon_url.spec()) | 154 icon_url_to_murmur2_hash_map) { |
| 155 if (entry.first == shortcut_info.best_icon_url.spec()) |
154 continue; | 156 continue; |
155 webapk::Image* image = web_app_manifest->add_icons(); | 157 webapk::Image* image = web_app_manifest->add_icons(); |
156 image->set_src(icon_url); | 158 image->set_src(entry.first); |
| 159 image->set_hash(entry.second); |
157 } | 160 } |
158 | 161 |
159 return webapk; | 162 return webapk; |
160 } | 163 } |
161 | 164 |
162 // Returns task runner for running background tasks. | 165 // Returns task runner for running background tasks. |
163 scoped_refptr<base::TaskRunner> GetBackgroundTaskRunner() { | 166 scoped_refptr<base::TaskRunner> GetBackgroundTaskRunner() { |
164 return content::BrowserThread::GetBlockingPool() | 167 return content::BrowserThread::GetBlockingPool() |
165 ->GetTaskRunnerWithShutdownBehavior( | 168 ->GetTaskRunnerWithShutdownBehavior( |
166 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN); | 169 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN); |
(...skipping 24 matching lines...) Expand all Loading... |
191 | 194 |
192 // Creates the directory to download and sets permissions. | 195 // Creates the directory to download and sets permissions. |
193 if (!base::CreateDirectory(output_dir) || | 196 if (!base::CreateDirectory(output_dir) || |
194 !base::SetPosixFilePermissions(webapk_dir, posix_permissions) || | 197 !base::SetPosixFilePermissions(webapk_dir, posix_permissions) || |
195 !base::SetPosixFilePermissions(output_dir, posix_permissions)) | 198 !base::SetPosixFilePermissions(output_dir, posix_permissions)) |
196 return base::FilePath(); | 199 return base::FilePath(); |
197 | 200 |
198 return output_dir; | 201 return output_dir; |
199 } | 202 } |
200 | 203 |
| 204 // Calls the callback when the |webapk| request is created. |
| 205 void OnWebApkProtoBuilt( |
| 206 const base::Callback<void(std::unique_ptr<webapk::WebApk>)>& callback, |
| 207 std::unique_ptr<webapk::WebApk> webapk) { |
| 208 callback.Run(std::move(webapk)); |
| 209 } |
| 210 |
201 } // anonymous namespace | 211 } // anonymous namespace |
202 | 212 |
203 WebApkInstaller::WebApkInstaller(const ShortcutInfo& shortcut_info, | 213 WebApkInstaller::WebApkInstaller(const ShortcutInfo& shortcut_info, |
204 const SkBitmap& shortcut_icon) | 214 const SkBitmap& shortcut_icon) |
205 : shortcut_info_(shortcut_info), | 215 : shortcut_info_(shortcut_info), |
206 shortcut_icon_(shortcut_icon), | 216 shortcut_icon_(shortcut_icon), |
207 server_url_(GetServerUrl()), | 217 server_url_(GetServerUrl()), |
208 webapk_download_url_timeout_ms_(kWebApkDownloadUrlTimeoutMs), | 218 webapk_download_url_timeout_ms_(kWebApkDownloadUrlTimeoutMs), |
209 download_timeout_ms_(kDownloadTimeoutMs), | 219 download_timeout_ms_(kDownloadTimeoutMs), |
210 task_type_(UNDEFINED), | 220 task_type_(UNDEFINED), |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
242 // transformations being applied to the bitmap (such as encoding/decoding | 252 // transformations being applied to the bitmap (such as encoding/decoding |
243 // the bitmap). The icon hash is used to determine whether the icon that | 253 // the bitmap). The icon hash is used to determine whether the icon that |
244 // the user sees matches the icon of a WebAPK that the WebAPK server | 254 // the user sees matches the icon of a WebAPK that the WebAPK server |
245 // generated for another user. (The icon can be dynamically generated.) | 255 // generated for another user. (The icon can be dynamically generated.) |
246 // | 256 // |
247 // We redownload the icon in order to take the Murmur2 hash. The redownload | 257 // We redownload the icon in order to take the Murmur2 hash. The redownload |
248 // should be fast because the icon should be in the HTTP cache. | 258 // should be fast because the icon should be in the HTTP cache. |
249 DownloadAppIconAndComputeMurmur2Hash(); | 259 DownloadAppIconAndComputeMurmur2Hash(); |
250 } | 260 } |
251 | 261 |
252 void WebApkInstaller::UpdateAsync(content::BrowserContext* browser_context, | 262 void WebApkInstaller::UpdateAsync( |
253 const FinishCallback& finish_callback, | 263 content::BrowserContext* browser_context, |
254 const std::string& icon_murmur2_hash, | 264 const FinishCallback& finish_callback, |
255 const std::string& webapk_package, | 265 const std::string& webapk_package, |
256 int webapk_version) { | 266 int webapk_version, |
| 267 bool stale_manifest, |
| 268 const std::map<std::string, std::string>& icon_url_to_murmur2_hash_map) { |
257 UpdateAsyncWithURLRequestContextGetter( | 269 UpdateAsyncWithURLRequestContextGetter( |
258 Profile::FromBrowserContext(browser_context)->GetRequestContext(), | 270 Profile::FromBrowserContext(browser_context)->GetRequestContext(), |
259 finish_callback, icon_murmur2_hash, webapk_package, webapk_version); | 271 finish_callback, webapk_package, webapk_version, stale_manifest, |
| 272 icon_url_to_murmur2_hash_map); |
260 } | 273 } |
261 | 274 |
262 void WebApkInstaller::UpdateAsyncWithURLRequestContextGetter( | 275 void WebApkInstaller::UpdateAsyncWithURLRequestContextGetter( |
263 net::URLRequestContextGetter* request_context_getter, | 276 net::URLRequestContextGetter* request_context_getter, |
264 const FinishCallback& finish_callback, | 277 const FinishCallback& finish_callback, |
265 const std::string& icon_murmur2_hash, | |
266 const std::string& webapk_package, | 278 const std::string& webapk_package, |
267 int webapk_version) { | 279 int webapk_version, |
| 280 bool stale_manifest, |
| 281 const std::map<std::string, std::string>& icon_url_to_murmur2_hash_map) { |
268 request_context_getter_ = request_context_getter; | 282 request_context_getter_ = request_context_getter; |
269 finish_callback_ = finish_callback; | 283 finish_callback_ = finish_callback; |
270 shortcut_icon_murmur2_hash_ = icon_murmur2_hash; | |
271 webapk_package_ = webapk_package; | 284 webapk_package_ = webapk_package; |
272 webapk_version_ = webapk_version; | 285 webapk_version_ = webapk_version; |
273 task_type_ = UPDATE; | 286 task_type_ = UPDATE; |
274 | 287 |
275 base::PostTaskAndReplyWithResult( | 288 base::PostTaskAndReplyWithResult( |
276 GetBackgroundTaskRunner().get(), FROM_HERE, | 289 GetBackgroundTaskRunner().get(), FROM_HERE, |
277 base::Bind(&BuildWebApkProtoInBackground, shortcut_info_, | 290 base::Bind(&BuildWebApkProtoInBackground, shortcut_info_, shortcut_icon_, |
278 shortcut_icon_, shortcut_icon_murmur2_hash_), | 291 stale_manifest, icon_url_to_murmur2_hash_map), |
279 base::Bind(&WebApkInstaller::SendUpdateWebApkRequest, | 292 base::Bind(&WebApkInstaller::SendUpdateWebApkRequest, |
280 weak_ptr_factory_.GetWeakPtr())); | 293 weak_ptr_factory_.GetWeakPtr())); |
281 } | 294 } |
282 | 295 |
283 void WebApkInstaller::SetTimeoutMs(int timeout_ms) { | 296 void WebApkInstaller::SetTimeoutMs(int timeout_ms) { |
284 webapk_download_url_timeout_ms_ = timeout_ms; | 297 webapk_download_url_timeout_ms_ = timeout_ms; |
285 download_timeout_ms_ = timeout_ms; | 298 download_timeout_ms_ = timeout_ms; |
286 } | 299 } |
287 | 300 |
288 void WebApkInstaller::OnInstallFinished( | 301 void WebApkInstaller::OnInstallFinished( |
289 JNIEnv* env, | 302 JNIEnv* env, |
290 const base::android::JavaParamRef<jobject>& obj, | 303 const base::android::JavaParamRef<jobject>& obj, |
291 jboolean success) { | 304 jboolean success) { |
292 if (success) | 305 if (success) |
293 OnSuccess(); | 306 OnSuccess(); |
294 else | 307 else |
295 OnFailure(); | 308 OnFailure(); |
296 } | 309 } |
297 | 310 |
| 311 void WebApkInstaller::BuildWebApkProtoInBackgroundForTesting( |
| 312 const base::Callback<void(std::unique_ptr<webapk::WebApk>)>& callback, |
| 313 bool stale_manifest, |
| 314 const std::map<std::string, std::string>& icon_url_to_murmur2_hash_map) { |
| 315 base::PostTaskAndReplyWithResult( |
| 316 GetBackgroundTaskRunner().get(), FROM_HERE, |
| 317 base::Bind(&BuildWebApkProtoInBackground, shortcut_info_, shortcut_icon_, |
| 318 stale_manifest, icon_url_to_murmur2_hash_map), |
| 319 base::Bind(&OnWebApkProtoBuilt, callback)); |
| 320 } |
| 321 |
298 // static | 322 // static |
299 bool WebApkInstaller::Register(JNIEnv* env) { | 323 bool WebApkInstaller::Register(JNIEnv* env) { |
300 return RegisterNativesImpl(env); | 324 return RegisterNativesImpl(env); |
301 } | 325 } |
302 | 326 |
303 bool WebApkInstaller::StartInstallingDownloadedWebApk( | 327 bool WebApkInstaller::StartInstallingDownloadedWebApk( |
304 JNIEnv* env, | 328 JNIEnv* env, |
305 const base::android::ScopedJavaLocalRef<jstring>& java_file_path, | 329 const base::android::ScopedJavaLocalRef<jstring>& java_file_path, |
306 const base::android::ScopedJavaLocalRef<jstring>& java_package_name) { | 330 const base::android::ScopedJavaLocalRef<jstring>& java_package_name) { |
307 return Java_WebApkInstaller_installAsyncAndMonitorInstallationFromNative( | 331 return Java_WebApkInstaller_installAsyncAndMonitorInstallationFromNative( |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
358 request_context_getter_, shortcut_info_.best_icon_url, | 382 request_context_getter_, shortcut_info_.best_icon_url, |
359 base::Bind(&WebApkInstaller::OnGotIconMurmur2Hash, | 383 base::Bind(&WebApkInstaller::OnGotIconMurmur2Hash, |
360 weak_ptr_factory_.GetWeakPtr())); | 384 weak_ptr_factory_.GetWeakPtr())); |
361 } | 385 } |
362 | 386 |
363 void WebApkInstaller::OnGotIconMurmur2Hash( | 387 void WebApkInstaller::OnGotIconMurmur2Hash( |
364 const std::string& icon_murmur2_hash) { | 388 const std::string& icon_murmur2_hash) { |
365 timer_.Stop(); | 389 timer_.Stop(); |
366 icon_hasher_.reset(); | 390 icon_hasher_.reset(); |
367 | 391 |
368 shortcut_icon_murmur2_hash_ = icon_murmur2_hash; | |
369 | |
370 // An empty hash indicates that |icon_hasher_| encountered an error. | 392 // An empty hash indicates that |icon_hasher_| encountered an error. |
371 if (icon_murmur2_hash.empty()) { | 393 if (icon_murmur2_hash.empty()) { |
372 OnFailure(); | 394 OnFailure(); |
373 return; | 395 return; |
374 } | 396 } |
375 | 397 |
| 398 std::map<std::string, std::string> icon_url_to_murmur2_hash_map; |
| 399 for (const std::string& icon_url : shortcut_info_.icon_urls) { |
| 400 if (icon_url != shortcut_info_.best_icon_url.spec()) |
| 401 icon_url_to_murmur2_hash_map[icon_url] = ""; |
| 402 else |
| 403 icon_url_to_murmur2_hash_map[icon_url] = icon_murmur2_hash; |
| 404 } |
| 405 |
376 base::PostTaskAndReplyWithResult( | 406 base::PostTaskAndReplyWithResult( |
377 GetBackgroundTaskRunner().get(), FROM_HERE, | 407 GetBackgroundTaskRunner().get(), FROM_HERE, |
378 base::Bind(&BuildWebApkProtoInBackground, shortcut_info_, | 408 base::Bind(&BuildWebApkProtoInBackground, shortcut_info_, shortcut_icon_, |
379 shortcut_icon_, shortcut_icon_murmur2_hash_), | 409 false, icon_url_to_murmur2_hash_map), |
380 base::Bind(&WebApkInstaller::SendCreateWebApkRequest, | 410 base::Bind(&WebApkInstaller::SendCreateWebApkRequest, |
381 weak_ptr_factory_.GetWeakPtr())); | 411 weak_ptr_factory_.GetWeakPtr())); |
382 } | 412 } |
383 | 413 |
384 void WebApkInstaller::SendCreateWebApkRequest( | 414 void WebApkInstaller::SendCreateWebApkRequest( |
385 std::unique_ptr<webapk::WebApk> webapk) { | 415 std::unique_ptr<webapk::WebApk> webapk) { |
386 SendRequest(std::move(webapk), server_url_); | 416 SendRequest(std::move(webapk), server_url_); |
387 } | 417 } |
388 | 418 |
389 void WebApkInstaller::SendUpdateWebApkRequest( | 419 void WebApkInstaller::SendUpdateWebApkRequest( |
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
515 | 545 |
516 void WebApkInstaller::OnSuccess() { | 546 void WebApkInstaller::OnSuccess() { |
517 finish_callback_.Run(true, webapk_package_); | 547 finish_callback_.Run(true, webapk_package_); |
518 delete this; | 548 delete this; |
519 } | 549 } |
520 | 550 |
521 void WebApkInstaller::OnFailure() { | 551 void WebApkInstaller::OnFailure() { |
522 finish_callback_.Run(false, webapk_package_); | 552 finish_callback_.Run(false, webapk_package_); |
523 delete this; | 553 delete this; |
524 } | 554 } |
OLD | NEW |