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 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
99 return "mips"; | 99 return "mips"; |
100 #elif defined(__x86_64__) | 100 #elif defined(__x86_64__) |
101 return "x86_64"; | 101 return "x86_64"; |
102 #elif defined(__aarch64__) | 102 #elif defined(__aarch64__) |
103 return "arm64-v8a"; | 103 return "arm64-v8a"; |
104 #else | 104 #else |
105 #error "Unsupported target abi" | 105 #error "Unsupported target abi" |
106 #endif | 106 #endif |
107 } | 107 } |
108 | 108 |
109 // Populates webapk::WebApk and returns it. | |
110 // Must be called on a worker thread because it encodes an SkBitmap. | |
111 std::unique_ptr<webapk::WebApk> BuildWebApkProtoInBackground( | |
112 const ShortcutInfo& shortcut_info, | |
113 const SkBitmap& shortcut_icon, | |
114 const std::string& shortcut_icon_murmur2_hash) { | |
115 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); | |
116 | |
117 std::unique_ptr<webapk::WebApk> webapk(new webapk::WebApk); | |
118 webapk->set_manifest_url(shortcut_info.manifest_url.spec()); | |
119 webapk->set_requester_application_package( | |
120 base::android::BuildInfo::GetInstance()->package_name()); | |
121 webapk->set_requester_application_version(version_info::GetVersionNumber()); | |
122 webapk->set_android_abi(getCurrentAbi()); | |
123 | |
124 // TODO(hanxi): crbug.com/665078. Add a flag in WebAPK's proto to indicate | |
125 // that the Web Manifest data in the proto might be stale. | |
126 if (shortcut_icon_murmur2_hash.empty()) | |
127 return webapk; | |
128 | |
129 webapk::WebAppManifest* web_app_manifest = webapk->mutable_manifest(); | |
130 web_app_manifest->set_name(base::UTF16ToUTF8(shortcut_info.name)); | |
131 web_app_manifest->set_short_name( | |
132 base::UTF16ToUTF8(shortcut_info.short_name)); | |
133 web_app_manifest->set_start_url(shortcut_info.url.spec()); | |
134 web_app_manifest->set_orientation( | |
135 content::WebScreenOrientationLockTypeToString( | |
136 shortcut_info.orientation)); | |
137 web_app_manifest->set_display_mode( | |
138 content::WebDisplayModeToString(shortcut_info.display)); | |
139 web_app_manifest->set_background_color( | |
140 ColorToString(shortcut_info.background_color)); | |
141 web_app_manifest->set_theme_color(ColorToString(shortcut_info.theme_color)); | |
142 | |
143 std::string* scope = web_app_manifest->add_scopes(); | |
144 scope->assign(GetScope(shortcut_info).spec()); | |
145 | |
146 webapk::Image* best_image = web_app_manifest->add_icons(); | |
147 best_image->set_src(shortcut_info.best_icon_url.spec()); | |
148 best_image->set_hash(shortcut_icon_murmur2_hash); | |
149 std::vector<unsigned char> png_bytes; | |
150 gfx::PNGCodec::EncodeBGRASkBitmap(shortcut_icon, false, &png_bytes); | |
151 best_image->set_image_data(&png_bytes.front(), png_bytes.size()); | |
152 | |
153 for (const std::string& icon_url : shortcut_info.icon_urls) { | |
154 if (icon_url == shortcut_info.best_icon_url.spec()) | |
155 continue; | |
156 webapk::Image* image = web_app_manifest->add_icons(); | |
157 image->set_src(icon_url); | |
158 } | |
159 | |
160 return webapk; | |
161 } | |
162 | |
163 // Returns task runner for running background tasks. | 109 // Returns task runner for running background tasks. |
164 scoped_refptr<base::TaskRunner> GetBackgroundTaskRunner() { | 110 scoped_refptr<base::TaskRunner> GetBackgroundTaskRunner() { |
165 return content::BrowserThread::GetBlockingPool() | 111 return content::BrowserThread::GetBlockingPool() |
166 ->GetTaskRunnerWithShutdownBehavior( | 112 ->GetTaskRunnerWithShutdownBehavior( |
167 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN); | 113 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN); |
168 } | 114 } |
169 | 115 |
170 // Creates a directory depending on the type of the task, and set permissions. | 116 // Creates a directory depending on the type of the task, and set permissions. |
171 // It also creates any parent directory along the path if doesn't exist, | 117 // It also creates any parent directory along the path if doesn't exist, |
172 // and sets permissions as well. | 118 // and sets permissions as well. |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
243 // transformations being applied to the bitmap (such as encoding/decoding | 189 // transformations being applied to the bitmap (such as encoding/decoding |
244 // the bitmap). The icon hash is used to determine whether the icon that | 190 // the bitmap). The icon hash is used to determine whether the icon that |
245 // the user sees matches the icon of a WebAPK that the WebAPK server | 191 // the user sees matches the icon of a WebAPK that the WebAPK server |
246 // generated for another user. (The icon can be dynamically generated.) | 192 // generated for another user. (The icon can be dynamically generated.) |
247 // | 193 // |
248 // We redownload the icon in order to take the Murmur2 hash. The redownload | 194 // We redownload the icon in order to take the Murmur2 hash. The redownload |
249 // should be fast because the icon should be in the HTTP cache. | 195 // should be fast because the icon should be in the HTTP cache. |
250 DownloadAppIconAndComputeMurmur2Hash(); | 196 DownloadAppIconAndComputeMurmur2Hash(); |
251 } | 197 } |
252 | 198 |
253 void WebApkInstaller::UpdateAsync(content::BrowserContext* browser_context, | 199 void WebApkInstaller::UpdateAsync( |
254 const FinishCallback& finish_callback, | 200 content::BrowserContext* browser_context, |
255 const std::string& icon_murmur2_hash, | 201 const FinishCallback& finish_callback, |
256 const std::string& webapk_package, | 202 const std::string& webapk_package, |
257 int webapk_version) { | 203 int webapk_version, |
| 204 bool stale_manifest, |
| 205 const std::map<std::string, std::string>& icon_url_to_murmur2_hash_map) { |
258 UpdateAsyncWithURLRequestContextGetter( | 206 UpdateAsyncWithURLRequestContextGetter( |
259 Profile::FromBrowserContext(browser_context)->GetRequestContext(), | 207 Profile::FromBrowserContext(browser_context)->GetRequestContext(), |
260 finish_callback, icon_murmur2_hash, webapk_package, webapk_version); | 208 finish_callback, webapk_package, webapk_version, stale_manifest, |
| 209 icon_url_to_murmur2_hash_map); |
261 } | 210 } |
262 | 211 |
263 void WebApkInstaller::UpdateAsyncWithURLRequestContextGetter( | 212 void WebApkInstaller::UpdateAsyncWithURLRequestContextGetter( |
264 net::URLRequestContextGetter* request_context_getter, | 213 net::URLRequestContextGetter* request_context_getter, |
265 const FinishCallback& finish_callback, | 214 const FinishCallback& finish_callback, |
266 const std::string& icon_murmur2_hash, | |
267 const std::string& webapk_package, | 215 const std::string& webapk_package, |
268 int webapk_version) { | 216 int webapk_version, |
| 217 bool stale_manifest, |
| 218 const std::map<std::string, std::string>& icon_url_to_murmur2_hash_map) { |
269 request_context_getter_ = request_context_getter; | 219 request_context_getter_ = request_context_getter; |
270 finish_callback_ = finish_callback; | 220 finish_callback_ = finish_callback; |
271 shortcut_icon_murmur2_hash_ = icon_murmur2_hash; | |
272 webapk_package_ = webapk_package; | 221 webapk_package_ = webapk_package; |
273 webapk_version_ = webapk_version; | 222 webapk_version_ = webapk_version; |
274 task_type_ = UPDATE; | 223 task_type_ = UPDATE; |
275 | 224 |
276 base::PostTaskAndReplyWithResult( | 225 base::PostTaskAndReplyWithResult( |
277 GetBackgroundTaskRunner().get(), FROM_HERE, | 226 GetBackgroundTaskRunner().get(), FROM_HERE, |
278 base::Bind(&BuildWebApkProtoInBackground, shortcut_info_, | 227 base::Bind(&BuildWebApkProtoInBackground, shortcut_info_, shortcut_icon_, |
279 shortcut_icon_, shortcut_icon_murmur2_hash_), | 228 stale_manifest, icon_url_to_murmur2_hash_map), |
280 base::Bind(&WebApkInstaller::SendUpdateWebApkRequest, | 229 base::Bind(&WebApkInstaller::SendUpdateWebApkRequest, |
281 weak_ptr_factory_.GetWeakPtr())); | 230 weak_ptr_factory_.GetWeakPtr())); |
282 } | 231 } |
283 | 232 |
284 void WebApkInstaller::SetTimeoutMs(int timeout_ms) { | 233 void WebApkInstaller::SetTimeoutMs(int timeout_ms) { |
285 webapk_download_url_timeout_ms_ = timeout_ms; | 234 webapk_download_url_timeout_ms_ = timeout_ms; |
286 download_timeout_ms_ = timeout_ms; | 235 download_timeout_ms_ = timeout_ms; |
287 } | 236 } |
288 | 237 |
289 void WebApkInstaller::OnInstallFinished( | 238 void WebApkInstaller::OnInstallFinished( |
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
403 request_context_getter_, shortcut_info_.best_icon_url, | 352 request_context_getter_, shortcut_info_.best_icon_url, |
404 base::Bind(&WebApkInstaller::OnGotIconMurmur2Hash, | 353 base::Bind(&WebApkInstaller::OnGotIconMurmur2Hash, |
405 weak_ptr_factory_.GetWeakPtr())); | 354 weak_ptr_factory_.GetWeakPtr())); |
406 } | 355 } |
407 | 356 |
408 void WebApkInstaller::OnGotIconMurmur2Hash( | 357 void WebApkInstaller::OnGotIconMurmur2Hash( |
409 const std::string& icon_murmur2_hash) { | 358 const std::string& icon_murmur2_hash) { |
410 timer_.Stop(); | 359 timer_.Stop(); |
411 icon_hasher_.reset(); | 360 icon_hasher_.reset(); |
412 | 361 |
413 shortcut_icon_murmur2_hash_ = icon_murmur2_hash; | |
414 | |
415 // An empty hash indicates that |icon_hasher_| encountered an error. | 362 // An empty hash indicates that |icon_hasher_| encountered an error. |
416 if (icon_murmur2_hash.empty()) { | 363 if (icon_murmur2_hash.empty()) { |
417 OnFailure(); | 364 OnFailure(); |
418 return; | 365 return; |
419 } | 366 } |
420 | 367 |
| 368 std::map<std::string, std::string> icon_url_to_murmur2_hash_map; |
| 369 for (const std::string& icon_url : shortcut_info_.icon_urls) { |
| 370 if (icon_url != shortcut_info_.best_icon_url.spec()) |
| 371 icon_url_to_murmur2_hash_map[icon_url] = ""; |
| 372 else |
| 373 icon_url_to_murmur2_hash_map[icon_url] = icon_murmur2_hash; |
| 374 } |
| 375 |
421 base::PostTaskAndReplyWithResult( | 376 base::PostTaskAndReplyWithResult( |
422 GetBackgroundTaskRunner().get(), FROM_HERE, | 377 GetBackgroundTaskRunner().get(), FROM_HERE, |
423 base::Bind(&BuildWebApkProtoInBackground, shortcut_info_, | 378 base::Bind(&BuildWebApkProtoInBackground, shortcut_info_, shortcut_icon_, |
424 shortcut_icon_, shortcut_icon_murmur2_hash_), | 379 false, icon_url_to_murmur2_hash_map), |
425 base::Bind(&WebApkInstaller::SendCreateWebApkRequest, | 380 base::Bind(&WebApkInstaller::SendCreateWebApkRequest, |
426 weak_ptr_factory_.GetWeakPtr())); | 381 weak_ptr_factory_.GetWeakPtr())); |
427 } | 382 } |
428 | 383 |
429 void WebApkInstaller::SendCreateWebApkRequest( | 384 void WebApkInstaller::SendCreateWebApkRequest( |
430 std::unique_ptr<webapk::WebApk> webapk) { | 385 std::unique_ptr<webapk::WebApk> webapk) { |
431 SendRequest(std::move(webapk), server_url_); | 386 SendRequest(std::move(webapk), server_url_); |
432 } | 387 } |
433 | 388 |
434 void WebApkInstaller::SendUpdateWebApkRequest( | 389 void WebApkInstaller::SendUpdateWebApkRequest( |
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
560 | 515 |
561 void WebApkInstaller::OnSuccess() { | 516 void WebApkInstaller::OnSuccess() { |
562 finish_callback_.Run(true, webapk_package_); | 517 finish_callback_.Run(true, webapk_package_); |
563 delete this; | 518 delete this; |
564 } | 519 } |
565 | 520 |
566 void WebApkInstaller::OnFailure() { | 521 void WebApkInstaller::OnFailure() { |
567 finish_callback_.Run(false, webapk_package_); | 522 finish_callback_.Run(false, webapk_package_); |
568 delete this; | 523 delete this; |
569 } | 524 } |
| 525 |
| 526 // static |
| 527 std::unique_ptr<webapk::WebApk> WebApkInstaller::BuildWebApkProtoInBackground( |
| 528 const ShortcutInfo& shortcut_info, |
| 529 const SkBitmap& shortcut_icon, |
| 530 bool stale_manifest, |
| 531 std::map<std::string, std::string> icon_url_to_murmur2_hash_map) { |
| 532 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); |
| 533 |
| 534 std::unique_ptr<webapk::WebApk> webapk(new webapk::WebApk); |
| 535 webapk->set_manifest_url(shortcut_info.manifest_url.spec()); |
| 536 webapk->set_requester_application_package( |
| 537 base::android::BuildInfo::GetInstance()->package_name()); |
| 538 webapk->set_requester_application_version(version_info::GetVersionNumber()); |
| 539 webapk->set_android_abi(getCurrentAbi()); |
| 540 webapk->set_stale_manifest(stale_manifest); |
| 541 |
| 542 webapk::WebAppManifest* web_app_manifest = webapk->mutable_manifest(); |
| 543 web_app_manifest->set_name(base::UTF16ToUTF8(shortcut_info.name)); |
| 544 web_app_manifest->set_short_name( |
| 545 base::UTF16ToUTF8(shortcut_info.short_name)); |
| 546 web_app_manifest->set_start_url(shortcut_info.url.spec()); |
| 547 web_app_manifest->set_orientation( |
| 548 content::WebScreenOrientationLockTypeToString( |
| 549 shortcut_info.orientation)); |
| 550 web_app_manifest->set_display_mode( |
| 551 content::WebDisplayModeToString(shortcut_info.display)); |
| 552 web_app_manifest->set_background_color( |
| 553 ColorToString(shortcut_info.background_color)); |
| 554 web_app_manifest->set_theme_color(ColorToString(shortcut_info.theme_color)); |
| 555 |
| 556 std::string* scope = web_app_manifest->add_scopes(); |
| 557 scope->assign(GetScope(shortcut_info).spec()); |
| 558 |
| 559 webapk::Image* best_image = web_app_manifest->add_icons(); |
| 560 std::string best_icon_url = shortcut_info.best_icon_url.spec(); |
| 561 best_image->set_src(best_icon_url); |
| 562 std::map<std::string, std::string>::iterator it = |
| 563 icon_url_to_murmur2_hash_map.find(best_icon_url); |
| 564 if (it != icon_url_to_murmur2_hash_map.end()) |
| 565 best_image->set_hash(it->second); |
| 566 std::vector<unsigned char> png_bytes; |
| 567 gfx::PNGCodec::EncodeBGRASkBitmap(shortcut_icon, false, &png_bytes); |
| 568 best_image->set_image_data(&png_bytes.front(), png_bytes.size()); |
| 569 |
| 570 for (const std::pair<std::string, std::string>& entry : |
| 571 icon_url_to_murmur2_hash_map) { |
| 572 if (entry.first == shortcut_info.best_icon_url.spec()) |
| 573 continue; |
| 574 webapk::Image* image = web_app_manifest->add_icons(); |
| 575 image->set_src(entry.first); |
| 576 image->set_hash(entry.second); |
| 577 } |
| 578 |
| 579 return webapk; |
| 580 } |
OLD | NEW |