Chromium Code Reviews| 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 <stdlib.h> | |
| 8 | |
| 7 #include "base/android/build_info.h" | 9 #include "base/android/build_info.h" |
| 8 #include "base/android/jni_android.h" | 10 #include "base/android/jni_android.h" |
| 9 #include "base/android/jni_string.h" | 11 #include "base/android/jni_string.h" |
| 10 #include "base/android/path_utils.h" | 12 #include "base/android/path_utils.h" |
| 11 #include "base/bind.h" | 13 #include "base/bind.h" |
| 12 #include "base/command_line.h" | 14 #include "base/command_line.h" |
| 13 #include "base/files/file_path.h" | 15 #include "base/files/file_path.h" |
| 14 #include "base/files/file_util.h" | 16 #include "base/files/file_util.h" |
| 15 #include "base/memory/ref_counted.h" | 17 #include "base/memory/ref_counted.h" |
| 16 #include "base/strings/string_util.h" | 18 #include "base/strings/string_util.h" |
| 17 #include "base/strings/stringprintf.h" | 19 #include "base/strings/stringprintf.h" |
| 18 #include "base/strings/utf_string_conversions.h" | 20 #include "base/strings/utf_string_conversions.h" |
| 19 #include "base/task_runner_util.h" | 21 #include "base/task_runner_util.h" |
| 20 #include "base/threading/sequenced_worker_pool.h" | 22 #include "base/threading/sequenced_worker_pool.h" |
| 23 #include "chrome/browser/android/chrome_feature_list.h" | |
| 21 #include "chrome/browser/android/shortcut_helper.h" | 24 #include "chrome/browser/android/shortcut_helper.h" |
| 22 #include "chrome/browser/android/webapk/webapk.pb.h" | 25 #include "chrome/browser/android/webapk/webapk.pb.h" |
| 23 #include "chrome/browser/android/webapk/webapk_icon_hasher.h" | 26 #include "chrome/browser/android/webapk/webapk_icon_hasher.h" |
| 24 #include "chrome/browser/profiles/profile.h" | 27 #include "chrome/browser/profiles/profile.h" |
| 25 #include "chrome/common/chrome_switches.h" | 28 #include "chrome/common/chrome_switches.h" |
| 29 #include "components/variations/variations_associated_data.h" | |
| 26 #include "components/version_info/version_info.h" | 30 #include "components/version_info/version_info.h" |
| 27 #include "content/public/browser/browser_thread.h" | 31 #include "content/public/browser/browser_thread.h" |
| 28 #include "content/public/common/manifest_util.h" | 32 #include "content/public/common/manifest_util.h" |
| 29 #include "jni/WebApkInstaller_jni.h" | 33 #include "jni/WebApkInstaller_jni.h" |
| 30 #include "net/http/http_status_code.h" | 34 #include "net/http/http_status_code.h" |
| 31 #include "net/url_request/url_fetcher.h" | 35 #include "net/url_request/url_fetcher.h" |
| 32 #include "ui/gfx/codec/png_codec.h" | 36 #include "ui/gfx/codec/png_codec.h" |
| 33 #include "url/gurl.h" | 37 #include "url/gurl.h" |
| 34 | 38 |
| 35 namespace { | 39 namespace { |
| 36 | 40 |
| 37 // The default WebAPK server URL. | 41 // The default WebAPK server URL. |
| 38 const char kDefaultServerUrl[] = | 42 const char kDefaultServerUrl[] = |
| 39 "https://test-webapk.sandbox.googleapis.com/v1/webApks/?alt=proto&key=AIzaSy AoI6v-F31-3t9NunLYEiKcPIqgTJIUZBw"; | 43 "https://test-webapk.sandbox.googleapis.com/v1/webApks/?alt=proto&key=AIzaSy AoI6v-F31-3t9NunLYEiKcPIqgTJIUZBw"; |
| 40 | 44 |
| 45 // Flag of whether installing WebAPKs from Play Store is enabled. | |
| 46 const char kPlayInstallWebApks[] = "play_install_webapks"; | |
| 47 | |
| 41 // The MIME type of the POST data sent to the server. | 48 // The MIME type of the POST data sent to the server. |
| 42 const char kProtoMimeType[] = "application/x-protobuf"; | 49 const char kProtoMimeType[] = "application/x-protobuf"; |
| 43 | 50 |
| 44 // The default number of milliseconds to wait for the WebAPK download URL from | 51 // The default number of milliseconds to wait for the WebAPK download URL from |
| 45 // the WebAPK server. | 52 // the WebAPK server. |
| 46 const int kWebApkDownloadUrlTimeoutMs = 60000; | 53 const int kWebApkDownloadUrlTimeoutMs = 60000; |
| 47 | 54 |
| 48 // The default number of milliseconds to wait for the WebAPK download to | 55 // The default number of milliseconds to wait for the WebAPK download to |
| 49 // complete. | 56 // complete. |
| 50 const int kDownloadTimeoutMs = 60000; | 57 const int kDownloadTimeoutMs = 60000; |
| 51 | 58 |
| 52 const int kWorldReadableFilePermission = base::FILE_PERMISSION_READ_BY_USER | | 59 const int kWorldReadableFilePermission = base::FILE_PERMISSION_READ_BY_USER | |
| 53 base::FILE_PERMISSION_READ_BY_GROUP | | 60 base::FILE_PERMISSION_READ_BY_GROUP | |
| 54 base::FILE_PERMISSION_READ_BY_OTHERS; | 61 base::FILE_PERMISSION_READ_BY_OTHERS; |
| 55 | 62 |
| 63 // Returns whether the |play_install_webapks| param of the currently active | |
| 64 // field trials is true. | |
| 65 bool IsPlayInstallWebApksEnabled() { | |
| 66 return variations::GetVariationParamValueByFeature(chrome::android::kWebApks, | |
| 67 kPlayInstallWebApks) == "true"; | |
| 68 } | |
| 69 | |
| 56 // Returns the WebAPK server URL based on the command line. | 70 // Returns the WebAPK server URL based on the command line. |
| 57 GURL GetServerUrl() { | 71 GURL GetServerUrl() { |
| 58 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); | 72 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); |
| 59 GURL command_line_url( | 73 GURL command_line_url( |
| 60 command_line->GetSwitchValueASCII(switches::kWebApkServerUrl)); | 74 command_line->GetSwitchValueASCII(switches::kWebApkServerUrl)); |
| 61 return command_line_url.is_valid() ? command_line_url | 75 return command_line_url.is_valid() ? command_line_url |
| 62 : GURL(kDefaultServerUrl); | 76 : GURL(kDefaultServerUrl); |
| 63 } | 77 } |
| 64 | 78 |
| 65 // Returns the scope from |info| if it is specified. Otherwise, returns the | 79 // Returns the scope from |info| if it is specified. Otherwise, returns the |
| (...skipping 266 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 332 if (!response->ParseFromString(response_string)) { | 346 if (!response->ParseFromString(response_string)) { |
| 333 OnFailure(); | 347 OnFailure(); |
| 334 return; | 348 return; |
| 335 } | 349 } |
| 336 | 350 |
| 337 GURL signed_download_url(response->signed_download_url()); | 351 GURL signed_download_url(response->signed_download_url()); |
| 338 if (!signed_download_url.is_valid() || response->package_name().empty()) { | 352 if (!signed_download_url.is_valid() || response->package_name().empty()) { |
| 339 OnFailure(); | 353 OnFailure(); |
| 340 return; | 354 return; |
| 341 } | 355 } |
| 356 | |
| 357 JNIEnv* env = base::android::AttachCurrentThread(); | |
| 358 if (IsPlayInstallWebApksEnabled() && | |
| 359 Java_WebApkInstaller_hasWebApkInstallClientDelegate(env, java_ref_)) { | |
| 360 InstallWebApkFromPlayStore(response->package_name(), | |
| 361 std::atoi(response->version().c_str()), | |
| 362 response->wam_token()); | |
| 363 return; | |
| 364 } | |
| 365 | |
| 342 OnGotWebApkDownloadUrl(signed_download_url, response->package_name()); | 366 OnGotWebApkDownloadUrl(signed_download_url, response->package_name()); |
| 343 } | 367 } |
| 344 | 368 |
| 345 void WebApkInstaller::DownloadAppIconAndComputeMurmur2Hash() { | 369 void WebApkInstaller::DownloadAppIconAndComputeMurmur2Hash() { |
| 346 // Safeguard. WebApkIconHasher crashes if asked to fetch an invalid URL. | 370 // Safeguard. WebApkIconHasher crashes if asked to fetch an invalid URL. |
| 347 if (!shortcut_info_.best_icon_url.is_valid()) { | 371 if (!shortcut_info_.best_icon_url.is_valid()) { |
| 348 OnFailure(); | 372 OnFailure(); |
| 349 return; | 373 return; |
| 350 } | 374 } |
| 351 | 375 |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 403 | 427 |
| 404 url_fetcher_ = | 428 url_fetcher_ = |
| 405 net::URLFetcher::Create(server_url, net::URLFetcher::POST, this); | 429 net::URLFetcher::Create(server_url, net::URLFetcher::POST, this); |
| 406 url_fetcher_->SetRequestContext(request_context_getter_); | 430 url_fetcher_->SetRequestContext(request_context_getter_); |
| 407 std::string serialized_request; | 431 std::string serialized_request; |
| 408 request_proto->SerializeToString(&serialized_request); | 432 request_proto->SerializeToString(&serialized_request); |
| 409 url_fetcher_->SetUploadData(kProtoMimeType, serialized_request); | 433 url_fetcher_->SetUploadData(kProtoMimeType, serialized_request); |
| 410 url_fetcher_->Start(); | 434 url_fetcher_->Start(); |
| 411 } | 435 } |
| 412 | 436 |
| 437 void WebApkInstaller::InstallWebApkFromPlayStore( | |
|
pkotwicz
2016/12/01 04:18:04
We should probably rename this method to InstallOr
Xi Han
2016/12/01 22:22:28
Good catch! The update part is added.
| |
| 438 const std::string& package_name, | |
| 439 int version, | |
| 440 const std::string& wam_token) { | |
| 441 webapk_package_ = package_name; | |
| 442 | |
| 443 JNIEnv* env = base::android::AttachCurrentThread(); | |
| 444 base::android::ScopedJavaLocalRef<jstring> java_webapk_package = | |
| 445 base::android::ConvertUTF8ToJavaString(env, webapk_package_); | |
| 446 base::android::ScopedJavaLocalRef<jstring> java_title = | |
| 447 base::android::ConvertUTF16ToJavaString(env, shortcut_info_.user_title); | |
| 448 base::android::ScopedJavaLocalRef<jstring> java_wam_token = | |
| 449 base::android::ConvertUTF8ToJavaString(env, wam_token); | |
| 450 | |
| 451 Java_WebApkInstaller_installWebApkFromPlayStoreAsyncAndMonitorInstallation( | |
| 452 env, java_ref_, java_webapk_package, version, java_title, java_wam_token); | |
| 453 } | |
| 454 | |
| 413 void WebApkInstaller::OnGotWebApkDownloadUrl(const GURL& download_url, | 455 void WebApkInstaller::OnGotWebApkDownloadUrl(const GURL& download_url, |
| 414 const std::string& package_name) { | 456 const std::string& package_name) { |
| 415 webapk_package_ = package_name; | 457 webapk_package_ = package_name; |
| 416 | 458 |
| 417 base::PostTaskAndReplyWithResult( | 459 base::PostTaskAndReplyWithResult( |
| 418 GetBackgroundTaskRunner().get(), FROM_HERE, | 460 GetBackgroundTaskRunner().get(), FROM_HERE, |
| 419 base::Bind(&CreateSubDirAndSetPermissionsInBackground, | 461 base::Bind(&CreateSubDirAndSetPermissionsInBackground, |
| 420 task_type_ == WebApkInstaller::INSTALL ? "install" : "update", | 462 task_type_ == WebApkInstaller::INSTALL ? "install" : "update", |
| 421 package_name), | 463 package_name), |
| 422 base::Bind(&WebApkInstaller::OnCreatedSubDirAndSetPermissions, | 464 base::Bind(&WebApkInstaller::OnCreatedSubDirAndSetPermissions, |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 515 | 557 |
| 516 void WebApkInstaller::OnSuccess() { | 558 void WebApkInstaller::OnSuccess() { |
| 517 finish_callback_.Run(true, webapk_package_); | 559 finish_callback_.Run(true, webapk_package_); |
| 518 delete this; | 560 delete this; |
| 519 } | 561 } |
| 520 | 562 |
| 521 void WebApkInstaller::OnFailure() { | 563 void WebApkInstaller::OnFailure() { |
| 522 finish_callback_.Run(false, webapk_package_); | 564 finish_callback_.Run(false, webapk_package_); |
| 523 delete this; | 565 delete this; |
| 524 } | 566 } |
| OLD | NEW |