| 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 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 115 const std::string& shortcut_icon_murmur2_hash) { | 115 const std::string& shortcut_icon_murmur2_hash) { |
| 116 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); | 116 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); |
| 117 | 117 |
| 118 std::unique_ptr<webapk::WebApk> webapk(new webapk::WebApk); | 118 std::unique_ptr<webapk::WebApk> webapk(new webapk::WebApk); |
| 119 webapk->set_manifest_url(shortcut_info.manifest_url.spec()); | 119 webapk->set_manifest_url(shortcut_info.manifest_url.spec()); |
| 120 webapk->set_requester_application_package( | 120 webapk->set_requester_application_package( |
| 121 base::android::BuildInfo::GetInstance()->package_name()); | 121 base::android::BuildInfo::GetInstance()->package_name()); |
| 122 webapk->set_requester_application_version(version_info::GetVersionNumber()); | 122 webapk->set_requester_application_version(version_info::GetVersionNumber()); |
| 123 webapk->set_android_abi(getCurrentAbi()); | 123 webapk->set_android_abi(getCurrentAbi()); |
| 124 | 124 |
| 125 // TODO(hanxi): crbug.com/665078. Add a flag in WebAPK's proto to indicate |
| 126 // that the Web Manifest data in the proto might be stale. |
| 127 if (shortcut_icon_murmur2_hash.empty()) |
| 128 return webapk; |
| 129 |
| 125 webapk::WebAppManifest* web_app_manifest = webapk->mutable_manifest(); | 130 webapk::WebAppManifest* web_app_manifest = webapk->mutable_manifest(); |
| 126 web_app_manifest->set_name(base::UTF16ToUTF8(shortcut_info.name)); | 131 web_app_manifest->set_name(base::UTF16ToUTF8(shortcut_info.name)); |
| 127 web_app_manifest->set_short_name( | 132 web_app_manifest->set_short_name( |
| 128 base::UTF16ToUTF8(shortcut_info.short_name)); | 133 base::UTF16ToUTF8(shortcut_info.short_name)); |
| 129 web_app_manifest->set_start_url(shortcut_info.url.spec()); | 134 web_app_manifest->set_start_url(shortcut_info.url.spec()); |
| 130 web_app_manifest->set_orientation( | 135 web_app_manifest->set_orientation( |
| 131 content::WebScreenOrientationLockTypeToString( | 136 content::WebScreenOrientationLockTypeToString( |
| 132 shortcut_info.orientation)); | 137 shortcut_info.orientation)); |
| 133 web_app_manifest->set_display_mode( | 138 web_app_manifest->set_display_mode( |
| 134 content::WebDisplayModeToString(shortcut_info.display)); | 139 content::WebDisplayModeToString(shortcut_info.display)); |
| 135 web_app_manifest->set_background_color( | 140 web_app_manifest->set_background_color( |
| 136 ColorToString(shortcut_info.background_color)); | 141 ColorToString(shortcut_info.background_color)); |
| 137 web_app_manifest->set_theme_color(ColorToString(shortcut_info.theme_color)); | 142 web_app_manifest->set_theme_color(ColorToString(shortcut_info.theme_color)); |
| 138 | 143 |
| 139 std::string* scope = web_app_manifest->add_scopes(); | 144 std::string* scope = web_app_manifest->add_scopes(); |
| 140 scope->assign(GetScope(shortcut_info).spec()); | 145 scope->assign(GetScope(shortcut_info).spec()); |
| 141 webapk::Image* image = web_app_manifest->add_icons(); | 146 |
| 142 image->set_src(shortcut_info.icon_url.spec()); | 147 webapk::Image* best_image = web_app_manifest->add_icons(); |
| 143 image->set_hash(shortcut_icon_murmur2_hash); | 148 best_image->set_src(shortcut_info.best_icon_url.spec()); |
| 149 best_image->set_hash(shortcut_icon_murmur2_hash); |
| 144 std::vector<unsigned char> png_bytes; | 150 std::vector<unsigned char> png_bytes; |
| 145 gfx::PNGCodec::EncodeBGRASkBitmap(shortcut_icon, false, &png_bytes); | 151 gfx::PNGCodec::EncodeBGRASkBitmap(shortcut_icon, false, &png_bytes); |
| 146 image->set_image_data(&png_bytes.front(), png_bytes.size()); | 152 best_image->set_image_data(&png_bytes.front(), png_bytes.size()); |
| 153 |
| 154 for (const std::string& icon_url : shortcut_info.icon_urls) { |
| 155 if (icon_url == shortcut_info.best_icon_url.spec()) |
| 156 continue; |
| 157 webapk::Image* image = web_app_manifest->add_icons(); |
| 158 image->set_src(icon_url); |
| 159 } |
| 147 | 160 |
| 148 return webapk; | 161 return webapk; |
| 149 } | 162 } |
| 150 | 163 |
| 151 // Returns task runner for running background tasks. | 164 // Returns task runner for running background tasks. |
| 152 scoped_refptr<base::TaskRunner> GetBackgroundTaskRunner() { | 165 scoped_refptr<base::TaskRunner> GetBackgroundTaskRunner() { |
| 153 return content::BrowserThread::GetBlockingPool() | 166 return content::BrowserThread::GetBlockingPool() |
| 154 ->GetTaskRunnerWithShutdownBehavior( | 167 ->GetTaskRunnerWithShutdownBehavior( |
| 155 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN); | 168 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN); |
| 156 } | 169 } |
| (...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 326 GURL signed_download_url(response->signed_download_url()); | 339 GURL signed_download_url(response->signed_download_url()); |
| 327 if (!signed_download_url.is_valid() || response->package_name().empty()) { | 340 if (!signed_download_url.is_valid() || response->package_name().empty()) { |
| 328 OnFailure(); | 341 OnFailure(); |
| 329 return; | 342 return; |
| 330 } | 343 } |
| 331 OnGotWebApkDownloadUrl(signed_download_url, response->package_name()); | 344 OnGotWebApkDownloadUrl(signed_download_url, response->package_name()); |
| 332 } | 345 } |
| 333 | 346 |
| 334 void WebApkInstaller::DownloadAppIconAndComputeMurmur2Hash() { | 347 void WebApkInstaller::DownloadAppIconAndComputeMurmur2Hash() { |
| 335 // Safeguard. WebApkIconHasher crashes if asked to fetch an invalid URL. | 348 // Safeguard. WebApkIconHasher crashes if asked to fetch an invalid URL. |
| 336 if (!shortcut_info_.icon_url.is_valid()) { | 349 if (!shortcut_info_.best_icon_url.is_valid()) { |
| 337 OnFailure(); | 350 OnFailure(); |
| 338 return; | 351 return; |
| 339 } | 352 } |
| 340 | 353 |
| 341 timer_.Start( | 354 timer_.Start( |
| 342 FROM_HERE, base::TimeDelta::FromMilliseconds(download_timeout_ms_), | 355 FROM_HERE, base::TimeDelta::FromMilliseconds(download_timeout_ms_), |
| 343 base::Bind(&WebApkInstaller::OnTimeout, weak_ptr_factory_.GetWeakPtr())); | 356 base::Bind(&WebApkInstaller::OnTimeout, weak_ptr_factory_.GetWeakPtr())); |
| 344 | 357 |
| 345 icon_hasher_.reset(new WebApkIconHasher()); | 358 icon_hasher_.reset(new WebApkIconHasher()); |
| 346 icon_hasher_->DownloadAndComputeMurmur2Hash( | 359 icon_hasher_->DownloadAndComputeMurmur2Hash( |
| 347 request_context_getter_, shortcut_info_.icon_url, | 360 request_context_getter_, shortcut_info_.best_icon_url, |
| 348 base::Bind(&WebApkInstaller::OnGotIconMurmur2Hash, | 361 base::Bind(&WebApkInstaller::OnGotIconMurmur2Hash, |
| 349 weak_ptr_factory_.GetWeakPtr())); | 362 weak_ptr_factory_.GetWeakPtr())); |
| 350 } | 363 } |
| 351 | 364 |
| 352 void WebApkInstaller::OnGotIconMurmur2Hash( | 365 void WebApkInstaller::OnGotIconMurmur2Hash( |
| 353 const std::string& icon_murmur2_hash) { | 366 const std::string& icon_murmur2_hash) { |
| 354 timer_.Stop(); | 367 timer_.Stop(); |
| 355 icon_hasher_.reset(); | 368 icon_hasher_.reset(); |
| 356 | 369 |
| 357 shortcut_icon_murmur2_hash_ = icon_murmur2_hash; | 370 shortcut_icon_murmur2_hash_ = icon_murmur2_hash; |
| (...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 504 | 517 |
| 505 void WebApkInstaller::OnSuccess() { | 518 void WebApkInstaller::OnSuccess() { |
| 506 finish_callback_.Run(true, webapk_package_); | 519 finish_callback_.Run(true, webapk_package_); |
| 507 delete this; | 520 delete this; |
| 508 } | 521 } |
| 509 | 522 |
| 510 void WebApkInstaller::OnFailure() { | 523 void WebApkInstaller::OnFailure() { |
| 511 finish_callback_.Run(false, webapk_package_); | 524 finish_callback_.Run(false, webapk_package_); |
| 512 delete this; | 525 delete this; |
| 513 } | 526 } |
| OLD | NEW |