| 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 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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. | 109 // Populates webapk::WebApk and returns it. |
| 110 // Must be called on a worker thread because it encodes an SkBitmap. | 110 // Must be called on a worker thread because it encodes an SkBitmap. |
| 111 std::unique_ptr<webapk::WebApk> BuildWebApkProtoInBackground( | 111 std::unique_ptr<webapk::WebApk> BuildWebApkProtoInBackground( |
| 112 const ShortcutInfo& shortcut_info, | 112 const ShortcutInfo& shortcut_info, |
| 113 const SkBitmap& shortcut_icon, | 113 const SkBitmap& shortcut_icon, |
| 114 const std::string& shortcut_icon_murmur2_hash) { | 114 const std::map<std::string, std::string>& icon_url_to_murmur2_hash, |
| 115 bool is_manifest_stale) { |
| 115 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); | 116 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); |
| 116 | 117 |
| 117 std::unique_ptr<webapk::WebApk> webapk(new webapk::WebApk); | 118 std::unique_ptr<webapk::WebApk> webapk(new webapk::WebApk); |
| 118 webapk->set_manifest_url(shortcut_info.manifest_url.spec()); | 119 webapk->set_manifest_url(shortcut_info.manifest_url.spec()); |
| 119 webapk->set_requester_application_package( | 120 webapk->set_requester_application_package( |
| 120 base::android::BuildInfo::GetInstance()->package_name()); | 121 base::android::BuildInfo::GetInstance()->package_name()); |
| 121 webapk->set_requester_application_version(version_info::GetVersionNumber()); | 122 webapk->set_requester_application_version(version_info::GetVersionNumber()); |
| 122 webapk->set_android_abi(getCurrentAbi()); | 123 webapk->set_android_abi(getCurrentAbi()); |
| 123 | 124 webapk->set_stale_manifest(is_manifest_stale); |
| 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 | 125 |
| 129 webapk::WebAppManifest* web_app_manifest = webapk->mutable_manifest(); | 126 webapk::WebAppManifest* web_app_manifest = webapk->mutable_manifest(); |
| 130 web_app_manifest->set_name(base::UTF16ToUTF8(shortcut_info.name)); | 127 web_app_manifest->set_name(base::UTF16ToUTF8(shortcut_info.name)); |
| 131 web_app_manifest->set_short_name( | 128 web_app_manifest->set_short_name( |
| 132 base::UTF16ToUTF8(shortcut_info.short_name)); | 129 base::UTF16ToUTF8(shortcut_info.short_name)); |
| 133 web_app_manifest->set_start_url(shortcut_info.url.spec()); | 130 web_app_manifest->set_start_url(shortcut_info.url.spec()); |
| 134 web_app_manifest->set_orientation( | 131 web_app_manifest->set_orientation( |
| 135 content::WebScreenOrientationLockTypeToString( | 132 content::WebScreenOrientationLockTypeToString( |
| 136 shortcut_info.orientation)); | 133 shortcut_info.orientation)); |
| 137 web_app_manifest->set_display_mode( | 134 web_app_manifest->set_display_mode( |
| 138 content::WebDisplayModeToString(shortcut_info.display)); | 135 content::WebDisplayModeToString(shortcut_info.display)); |
| 139 web_app_manifest->set_background_color( | 136 web_app_manifest->set_background_color( |
| 140 ColorToString(shortcut_info.background_color)); | 137 ColorToString(shortcut_info.background_color)); |
| 141 web_app_manifest->set_theme_color(ColorToString(shortcut_info.theme_color)); | 138 web_app_manifest->set_theme_color(ColorToString(shortcut_info.theme_color)); |
| 142 | 139 |
| 143 std::string* scope = web_app_manifest->add_scopes(); | 140 std::string* scope = web_app_manifest->add_scopes(); |
| 144 scope->assign(GetScope(shortcut_info).spec()); | 141 scope->assign(GetScope(shortcut_info).spec()); |
| 145 | 142 |
| 146 webapk::Image* best_image = web_app_manifest->add_icons(); | 143 webapk::Image* best_image = web_app_manifest->add_icons(); |
| 147 best_image->set_src(shortcut_info.best_icon_url.spec()); | 144 std::string best_icon_url = shortcut_info.best_icon_url.spec(); |
| 148 best_image->set_hash(shortcut_icon_murmur2_hash); | 145 best_image->set_src(best_icon_url); |
| 146 auto it = icon_url_to_murmur2_hash.find(best_icon_url); |
| 147 if (it != icon_url_to_murmur2_hash.end()) |
| 148 best_image->set_hash(it->second); |
| 149 std::vector<unsigned char> png_bytes; | 149 std::vector<unsigned char> png_bytes; |
| 150 gfx::PNGCodec::EncodeBGRASkBitmap(shortcut_icon, false, &png_bytes); | 150 gfx::PNGCodec::EncodeBGRASkBitmap(shortcut_icon, false, &png_bytes); |
| 151 best_image->set_image_data(&png_bytes.front(), png_bytes.size()); | 151 best_image->set_image_data(&png_bytes.front(), png_bytes.size()); |
| 152 | 152 |
| 153 for (const std::string& icon_url : shortcut_info.icon_urls) { | 153 for (const auto& entry : icon_url_to_murmur2_hash) { |
| 154 if (icon_url == shortcut_info.best_icon_url.spec()) | 154 if (entry.first == shortcut_info.best_icon_url.spec()) |
| 155 continue; | 155 continue; |
| 156 webapk::Image* image = web_app_manifest->add_icons(); | 156 webapk::Image* image = web_app_manifest->add_icons(); |
| 157 image->set_src(icon_url); | 157 image->set_src(entry.first); |
| 158 image->set_hash(entry.second); |
| 158 } | 159 } |
| 159 | 160 |
| 160 return webapk; | 161 return webapk; |
| 161 } | 162 } |
| 162 | 163 |
| 164 // Calls the callback when the |webapk| request is created. |
| 165 void OnWebApkProtoBuilt( |
| 166 const base::Callback<void(std::unique_ptr<webapk::WebApk>)>& callback, |
| 167 std::unique_ptr<webapk::WebApk> webapk) { |
| 168 callback.Run(std::move(webapk)); |
| 169 } |
| 170 |
| 163 // Returns task runner for running background tasks. | 171 // Returns task runner for running background tasks. |
| 164 scoped_refptr<base::TaskRunner> GetBackgroundTaskRunner() { | 172 scoped_refptr<base::TaskRunner> GetBackgroundTaskRunner() { |
| 165 return content::BrowserThread::GetBlockingPool() | 173 return content::BrowserThread::GetBlockingPool() |
| 166 ->GetTaskRunnerWithShutdownBehavior( | 174 ->GetTaskRunnerWithShutdownBehavior( |
| 167 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN); | 175 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN); |
| 168 } | 176 } |
| 169 | 177 |
| 170 // Creates a directory depending on the type of the task, and set permissions. | 178 // 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, | 179 // It also creates any parent directory along the path if doesn't exist, |
| 172 // and sets permissions as well. | 180 // 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 | 251 // transformations being applied to the bitmap (such as encoding/decoding |
| 244 // the bitmap). The icon hash is used to determine whether the icon that | 252 // 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 | 253 // the user sees matches the icon of a WebAPK that the WebAPK server |
| 246 // generated for another user. (The icon can be dynamically generated.) | 254 // generated for another user. (The icon can be dynamically generated.) |
| 247 // | 255 // |
| 248 // We redownload the icon in order to take the Murmur2 hash. The redownload | 256 // 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. | 257 // should be fast because the icon should be in the HTTP cache. |
| 250 DownloadAppIconAndComputeMurmur2Hash(); | 258 DownloadAppIconAndComputeMurmur2Hash(); |
| 251 } | 259 } |
| 252 | 260 |
| 253 void WebApkInstaller::UpdateAsync(content::BrowserContext* browser_context, | 261 void WebApkInstaller::UpdateAsync( |
| 254 const FinishCallback& finish_callback, | 262 content::BrowserContext* browser_context, |
| 255 const std::string& icon_murmur2_hash, | 263 const FinishCallback& finish_callback, |
| 256 const std::string& webapk_package, | 264 const std::string& webapk_package, |
| 257 int webapk_version) { | 265 int webapk_version, |
| 266 const std::map<std::string, std::string>& icon_url_to_murmur2_hash, |
| 267 bool is_manifest_stale) { |
| 258 UpdateAsyncWithURLRequestContextGetter( | 268 UpdateAsyncWithURLRequestContextGetter( |
| 259 Profile::FromBrowserContext(browser_context)->GetRequestContext(), | 269 Profile::FromBrowserContext(browser_context)->GetRequestContext(), |
| 260 finish_callback, icon_murmur2_hash, webapk_package, webapk_version); | 270 finish_callback, webapk_package, webapk_version, |
| 271 icon_url_to_murmur2_hash, is_manifest_stale); |
| 261 } | 272 } |
| 262 | 273 |
| 263 void WebApkInstaller::UpdateAsyncWithURLRequestContextGetter( | 274 void WebApkInstaller::UpdateAsyncWithURLRequestContextGetter( |
| 264 net::URLRequestContextGetter* request_context_getter, | 275 net::URLRequestContextGetter* request_context_getter, |
| 265 const FinishCallback& finish_callback, | 276 const FinishCallback& finish_callback, |
| 266 const std::string& icon_murmur2_hash, | |
| 267 const std::string& webapk_package, | 277 const std::string& webapk_package, |
| 268 int webapk_version) { | 278 int webapk_version, |
| 279 const std::map<std::string, std::string>& icon_url_to_murmur2_hash, |
| 280 bool is_manifest_stale) { |
| 269 request_context_getter_ = request_context_getter; | 281 request_context_getter_ = request_context_getter; |
| 270 finish_callback_ = finish_callback; | 282 finish_callback_ = finish_callback; |
| 271 shortcut_icon_murmur2_hash_ = icon_murmur2_hash; | |
| 272 webapk_package_ = webapk_package; | 283 webapk_package_ = webapk_package; |
| 273 webapk_version_ = webapk_version; | 284 webapk_version_ = webapk_version; |
| 274 task_type_ = UPDATE; | 285 task_type_ = UPDATE; |
| 275 | 286 |
| 276 base::PostTaskAndReplyWithResult( | 287 base::PostTaskAndReplyWithResult( |
| 277 GetBackgroundTaskRunner().get(), FROM_HERE, | 288 GetBackgroundTaskRunner().get(), FROM_HERE, |
| 278 base::Bind(&BuildWebApkProtoInBackground, shortcut_info_, | 289 base::Bind(&BuildWebApkProtoInBackground, shortcut_info_, shortcut_icon_, |
| 279 shortcut_icon_, shortcut_icon_murmur2_hash_), | 290 icon_url_to_murmur2_hash, is_manifest_stale), |
| 280 base::Bind(&WebApkInstaller::SendUpdateWebApkRequest, | 291 base::Bind(&WebApkInstaller::SendUpdateWebApkRequest, |
| 281 weak_ptr_factory_.GetWeakPtr())); | 292 weak_ptr_factory_.GetWeakPtr())); |
| 282 } | 293 } |
| 283 | 294 |
| 284 void WebApkInstaller::SetTimeoutMs(int timeout_ms) { | 295 void WebApkInstaller::SetTimeoutMs(int timeout_ms) { |
| 285 webapk_download_url_timeout_ms_ = timeout_ms; | 296 webapk_download_url_timeout_ms_ = timeout_ms; |
| 286 download_timeout_ms_ = timeout_ms; | 297 download_timeout_ms_ = timeout_ms; |
| 287 } | 298 } |
| 288 | 299 |
| 289 void WebApkInstaller::OnInstallFinished( | 300 void WebApkInstaller::OnInstallFinished( |
| 290 JNIEnv* env, | 301 JNIEnv* env, |
| 291 const base::android::JavaParamRef<jobject>& obj, | 302 const base::android::JavaParamRef<jobject>& obj, |
| 292 jboolean success) { | 303 jboolean success) { |
| 293 if (success) | 304 if (success) |
| 294 OnSuccess(); | 305 OnSuccess(); |
| 295 else | 306 else |
| 296 OnFailure(); | 307 OnFailure(); |
| 297 } | 308 } |
| 298 | 309 |
| 310 void WebApkInstaller::BuildWebApkProtoInBackgroundForTesting( |
| 311 const base::Callback<void(std::unique_ptr<webapk::WebApk>)>& callback, |
| 312 const std::map<std::string, std::string>& icon_url_to_murmur2_hash, |
| 313 bool is_manifest_stale) { |
| 314 base::PostTaskAndReplyWithResult( |
| 315 GetBackgroundTaskRunner().get(), FROM_HERE, |
| 316 base::Bind(&BuildWebApkProtoInBackground, shortcut_info_, shortcut_icon_, |
| 317 icon_url_to_murmur2_hash, is_manifest_stale), |
| 318 base::Bind(&OnWebApkProtoBuilt, callback)); |
| 319 } |
| 320 |
| 299 // static | 321 // static |
| 300 bool WebApkInstaller::Register(JNIEnv* env) { | 322 bool WebApkInstaller::Register(JNIEnv* env) { |
| 301 return RegisterNativesImpl(env); | 323 return RegisterNativesImpl(env); |
| 302 } | 324 } |
| 303 | 325 |
| 304 bool WebApkInstaller::StartInstallingDownloadedWebApk( | 326 bool WebApkInstaller::StartInstallingDownloadedWebApk( |
| 305 JNIEnv* env, | 327 JNIEnv* env, |
| 306 const base::android::ScopedJavaLocalRef<jstring>& java_file_path, | 328 const base::android::ScopedJavaLocalRef<jstring>& java_file_path, |
| 307 const base::android::ScopedJavaLocalRef<jstring>& java_package_name) { | 329 const base::android::ScopedJavaLocalRef<jstring>& java_package_name) { |
| 308 return Java_WebApkInstaller_installAsyncAndMonitorInstallationFromNative( | 330 return Java_WebApkInstaller_installAsyncAndMonitorInstallationFromNative( |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 403 request_context_getter_, shortcut_info_.best_icon_url, | 425 request_context_getter_, shortcut_info_.best_icon_url, |
| 404 base::Bind(&WebApkInstaller::OnGotIconMurmur2Hash, | 426 base::Bind(&WebApkInstaller::OnGotIconMurmur2Hash, |
| 405 weak_ptr_factory_.GetWeakPtr())); | 427 weak_ptr_factory_.GetWeakPtr())); |
| 406 } | 428 } |
| 407 | 429 |
| 408 void WebApkInstaller::OnGotIconMurmur2Hash( | 430 void WebApkInstaller::OnGotIconMurmur2Hash( |
| 409 const std::string& icon_murmur2_hash) { | 431 const std::string& icon_murmur2_hash) { |
| 410 timer_.Stop(); | 432 timer_.Stop(); |
| 411 icon_hasher_.reset(); | 433 icon_hasher_.reset(); |
| 412 | 434 |
| 413 shortcut_icon_murmur2_hash_ = icon_murmur2_hash; | |
| 414 | |
| 415 // An empty hash indicates that |icon_hasher_| encountered an error. | 435 // An empty hash indicates that |icon_hasher_| encountered an error. |
| 416 if (icon_murmur2_hash.empty()) { | 436 if (icon_murmur2_hash.empty()) { |
| 417 OnFailure(); | 437 OnFailure(); |
| 418 return; | 438 return; |
| 419 } | 439 } |
| 420 | 440 |
| 441 std::map<std::string, std::string> icon_url_to_murmur2_hash; |
| 442 for (const std::string& icon_url : shortcut_info_.icon_urls) { |
| 443 if (icon_url != shortcut_info_.best_icon_url.spec()) |
| 444 icon_url_to_murmur2_hash[icon_url] = ""; |
| 445 else |
| 446 icon_url_to_murmur2_hash[icon_url] = icon_murmur2_hash; |
| 447 } |
| 448 |
| 421 base::PostTaskAndReplyWithResult( | 449 base::PostTaskAndReplyWithResult( |
| 422 GetBackgroundTaskRunner().get(), FROM_HERE, | 450 GetBackgroundTaskRunner().get(), FROM_HERE, |
| 423 base::Bind(&BuildWebApkProtoInBackground, shortcut_info_, | 451 base::Bind(&BuildWebApkProtoInBackground, shortcut_info_, shortcut_icon_, |
| 424 shortcut_icon_, shortcut_icon_murmur2_hash_), | 452 icon_url_to_murmur2_hash, false /* is_manifest_stale */), |
| 425 base::Bind(&WebApkInstaller::SendCreateWebApkRequest, | 453 base::Bind(&WebApkInstaller::SendCreateWebApkRequest, |
| 426 weak_ptr_factory_.GetWeakPtr())); | 454 weak_ptr_factory_.GetWeakPtr())); |
| 427 } | 455 } |
| 428 | 456 |
| 429 void WebApkInstaller::SendCreateWebApkRequest( | 457 void WebApkInstaller::SendCreateWebApkRequest( |
| 430 std::unique_ptr<webapk::WebApk> webapk) { | 458 std::unique_ptr<webapk::WebApk> webapk) { |
| 431 SendRequest(std::move(webapk), server_url_); | 459 SendRequest(std::move(webapk), server_url_); |
| 432 } | 460 } |
| 433 | 461 |
| 434 void WebApkInstaller::SendUpdateWebApkRequest( | 462 void WebApkInstaller::SendUpdateWebApkRequest( |
| (...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 560 | 588 |
| 561 void WebApkInstaller::OnSuccess() { | 589 void WebApkInstaller::OnSuccess() { |
| 562 finish_callback_.Run(true, webapk_package_); | 590 finish_callback_.Run(true, webapk_package_); |
| 563 delete this; | 591 delete this; |
| 564 } | 592 } |
| 565 | 593 |
| 566 void WebApkInstaller::OnFailure() { | 594 void WebApkInstaller::OnFailure() { |
| 567 finish_callback_.Run(false, webapk_package_); | 595 finish_callback_.Run(false, webapk_package_); |
| 568 delete this; | 596 delete this; |
| 569 } | 597 } |
| OLD | NEW |