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() && | |
pkotwicz
2016/12/02 02:24:00
Is the IsPlayInstallWebApksEnabled() check necessa
Xi Han
2016/12/02 20:45:37
It isn't any more, removed.
| |
359 Java_WebApkInstaller_hasWebApkInstallClientDelegate(env, java_ref_)) { | |
360 InstallOrUpdateWebApkFromPlayStore(response->package_name(), | |
pkotwicz
2016/12/02 02:24:00
Nit: InstallOrUpdateWebApkFromPlayStore() -> Insta
Xi Han
2016/12/02 20:45:37
Done.
| |
361 std::atoi(response->version().c_str()), | |
pkotwicz
2016/12/02 02:24:00
Nit: Use base::StringToInt()
Xi Han
2016/12/02 20:45:37
Done.
| |
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::InstallOrUpdateWebApkFromPlayStore( | |
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 if (task_type_ == WebApkInstaller::INSTALL) { | |
452 Java_WebApkInstaller_installWebApkFromPlayStoreAsyncAndMonitorInstallation( | |
453 env, java_ref_, java_webapk_package, version, java_title, | |
454 java_wam_token); | |
455 } else { | |
456 Java_WebApkInstaller_updateAsyncFromPlayStore( | |
457 env, java_ref_, java_webapk_package, version, java_title, | |
458 java_wam_token); | |
459 } | |
460 } | |
461 | |
413 void WebApkInstaller::OnGotWebApkDownloadUrl(const GURL& download_url, | 462 void WebApkInstaller::OnGotWebApkDownloadUrl(const GURL& download_url, |
414 const std::string& package_name) { | 463 const std::string& package_name) { |
415 webapk_package_ = package_name; | 464 webapk_package_ = package_name; |
416 | 465 |
417 base::PostTaskAndReplyWithResult( | 466 base::PostTaskAndReplyWithResult( |
418 GetBackgroundTaskRunner().get(), FROM_HERE, | 467 GetBackgroundTaskRunner().get(), FROM_HERE, |
419 base::Bind(&CreateSubDirAndSetPermissionsInBackground, | 468 base::Bind(&CreateSubDirAndSetPermissionsInBackground, |
420 task_type_ == WebApkInstaller::INSTALL ? "install" : "update", | 469 task_type_ == WebApkInstaller::INSTALL ? "install" : "update", |
421 package_name), | 470 package_name), |
422 base::Bind(&WebApkInstaller::OnCreatedSubDirAndSetPermissions, | 471 base::Bind(&WebApkInstaller::OnCreatedSubDirAndSetPermissions, |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
515 | 564 |
516 void WebApkInstaller::OnSuccess() { | 565 void WebApkInstaller::OnSuccess() { |
517 finish_callback_.Run(true, webapk_package_); | 566 finish_callback_.Run(true, webapk_package_); |
518 delete this; | 567 delete this; |
519 } | 568 } |
520 | 569 |
521 void WebApkInstaller::OnFailure() { | 570 void WebApkInstaller::OnFailure() { |
522 finish_callback_.Run(false, webapk_package_); | 571 finish_callback_.Run(false, webapk_package_); |
523 delete this; | 572 delete this; |
524 } | 573 } |
OLD | NEW |