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> | |
pkotwicz
2016/12/02 22:27:04
Do you need this include?
Xi Han
2016/12/05 17:27:54
Done.
| |
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" |
18 #include "base/strings/string_number_conversions.h" | |
16 #include "base/strings/string_util.h" | 19 #include "base/strings/string_util.h" |
17 #include "base/strings/stringprintf.h" | 20 #include "base/strings/stringprintf.h" |
18 #include "base/strings/utf_string_conversions.h" | 21 #include "base/strings/utf_string_conversions.h" |
19 #include "base/task_runner_util.h" | 22 #include "base/task_runner_util.h" |
20 #include "base/threading/sequenced_worker_pool.h" | 23 #include "base/threading/sequenced_worker_pool.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" |
(...skipping 282 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
308 env, java_ref_, java_file_path, java_package_name); | 311 env, java_ref_, java_file_path, java_package_name); |
309 } | 312 } |
310 | 313 |
311 bool WebApkInstaller::StartUpdateUsingDownloadedWebApk( | 314 bool WebApkInstaller::StartUpdateUsingDownloadedWebApk( |
312 JNIEnv* env, | 315 JNIEnv* env, |
313 const base::android::ScopedJavaLocalRef<jstring>& java_file_path) { | 316 const base::android::ScopedJavaLocalRef<jstring>& java_file_path) { |
314 return Java_WebApkInstaller_updateAsyncFromNative( | 317 return Java_WebApkInstaller_updateAsyncFromNative( |
315 env, java_ref_, java_file_path); | 318 env, java_ref_, java_file_path); |
316 } | 319 } |
317 | 320 |
321 bool WebApkInstaller::HasGooglePlayWebApkInstallDelegate() { | |
322 JNIEnv* env = base::android::AttachCurrentThread(); | |
323 return Java_WebApkInstaller_hasGooglePlayWebApkInstallDelegate(env, java_ref_) ; | |
324 } | |
325 | |
318 void WebApkInstaller::OnURLFetchComplete(const net::URLFetcher* source) { | 326 void WebApkInstaller::OnURLFetchComplete(const net::URLFetcher* source) { |
319 timer_.Stop(); | 327 timer_.Stop(); |
320 | 328 |
321 if (!source->GetStatus().is_success() || | 329 if (!source->GetStatus().is_success() || |
322 source->GetResponseCode() != net::HTTP_OK) { | 330 source->GetResponseCode() != net::HTTP_OK) { |
323 OnFailure(); | 331 OnFailure(); |
324 return; | 332 return; |
325 } | 333 } |
326 | 334 |
327 std::string response_string; | 335 std::string response_string; |
328 source->GetResponseAsString(&response_string); | 336 source->GetResponseAsString(&response_string); |
329 | 337 |
330 std::unique_ptr<webapk::WebApkResponse> response( | 338 std::unique_ptr<webapk::WebApkResponse> response( |
331 new webapk::WebApkResponse); | 339 new webapk::WebApkResponse); |
332 if (!response->ParseFromString(response_string)) { | 340 if (!response->ParseFromString(response_string)) { |
333 OnFailure(); | 341 OnFailure(); |
334 return; | 342 return; |
335 } | 343 } |
336 | 344 |
337 GURL signed_download_url(response->signed_download_url()); | 345 GURL signed_download_url(response->signed_download_url()); |
338 if (!signed_download_url.is_valid() || response->package_name().empty()) { | 346 if (!signed_download_url.is_valid() || response->package_name().empty()) { |
339 OnFailure(); | 347 OnFailure(); |
340 return; | 348 return; |
341 } | 349 } |
350 | |
351 if (HasGooglePlayWebApkInstallDelegate()) { | |
352 int version = 1; | |
353 base::StringToInt(response->version(), &version); | |
354 InstallOrUpdateWebApkFromGooglePlay( | |
355 response->package_name(), version, response->token()); | |
pkotwicz
2016/12/02 22:27:04
Once InstallOrUpdateWebApkFromGooglePlay() returns
Xi Han
2016/12/05 17:27:54
Done.
| |
356 return; | |
357 } | |
358 | |
342 OnGotWebApkDownloadUrl(signed_download_url, response->package_name()); | 359 OnGotWebApkDownloadUrl(signed_download_url, response->package_name()); |
343 } | 360 } |
344 | 361 |
345 void WebApkInstaller::DownloadAppIconAndComputeMurmur2Hash() { | 362 void WebApkInstaller::DownloadAppIconAndComputeMurmur2Hash() { |
346 // Safeguard. WebApkIconHasher crashes if asked to fetch an invalid URL. | 363 // Safeguard. WebApkIconHasher crashes if asked to fetch an invalid URL. |
347 if (!shortcut_info_.best_icon_url.is_valid()) { | 364 if (!shortcut_info_.best_icon_url.is_valid()) { |
348 OnFailure(); | 365 OnFailure(); |
349 return; | 366 return; |
350 } | 367 } |
351 | 368 |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
403 | 420 |
404 url_fetcher_ = | 421 url_fetcher_ = |
405 net::URLFetcher::Create(server_url, net::URLFetcher::POST, this); | 422 net::URLFetcher::Create(server_url, net::URLFetcher::POST, this); |
406 url_fetcher_->SetRequestContext(request_context_getter_); | 423 url_fetcher_->SetRequestContext(request_context_getter_); |
407 std::string serialized_request; | 424 std::string serialized_request; |
408 request_proto->SerializeToString(&serialized_request); | 425 request_proto->SerializeToString(&serialized_request); |
409 url_fetcher_->SetUploadData(kProtoMimeType, serialized_request); | 426 url_fetcher_->SetUploadData(kProtoMimeType, serialized_request); |
410 url_fetcher_->Start(); | 427 url_fetcher_->Start(); |
411 } | 428 } |
412 | 429 |
430 void WebApkInstaller::InstallOrUpdateWebApkFromGooglePlay( | |
431 const std::string& package_name, | |
432 int version, | |
433 const std::string& token) { | |
434 webapk_package_ = package_name; | |
435 | |
436 JNIEnv* env = base::android::AttachCurrentThread(); | |
437 base::android::ScopedJavaLocalRef<jstring> java_webapk_package = | |
438 base::android::ConvertUTF8ToJavaString(env, webapk_package_); | |
439 base::android::ScopedJavaLocalRef<jstring> java_title = | |
440 base::android::ConvertUTF16ToJavaString(env, shortcut_info_.user_title); | |
441 base::android::ScopedJavaLocalRef<jstring> java_token = | |
442 base::android::ConvertUTF8ToJavaString(env, token); | |
443 | |
444 if (task_type_ == WebApkInstaller::INSTALL) { | |
445 Java_WebApkInstaller_installWebApkFromPlayStoreAsyncAndMonitorInstallation( | |
446 env, java_ref_, java_webapk_package, version, java_title, java_token); | |
447 } else { | |
448 Java_WebApkInstaller_updateAsyncFromPlayStore( | |
449 env, java_ref_, java_webapk_package, version, java_title, java_token); | |
450 } | |
451 } | |
452 | |
413 void WebApkInstaller::OnGotWebApkDownloadUrl(const GURL& download_url, | 453 void WebApkInstaller::OnGotWebApkDownloadUrl(const GURL& download_url, |
414 const std::string& package_name) { | 454 const std::string& package_name) { |
415 webapk_package_ = package_name; | 455 webapk_package_ = package_name; |
416 | 456 |
417 base::PostTaskAndReplyWithResult( | 457 base::PostTaskAndReplyWithResult( |
418 GetBackgroundTaskRunner().get(), FROM_HERE, | 458 GetBackgroundTaskRunner().get(), FROM_HERE, |
419 base::Bind(&CreateSubDirAndSetPermissionsInBackground, | 459 base::Bind(&CreateSubDirAndSetPermissionsInBackground, |
420 task_type_ == WebApkInstaller::INSTALL ? "install" : "update", | 460 task_type_ == WebApkInstaller::INSTALL ? "install" : "update", |
421 package_name), | 461 package_name), |
422 base::Bind(&WebApkInstaller::OnCreatedSubDirAndSetPermissions, | 462 base::Bind(&WebApkInstaller::OnCreatedSubDirAndSetPermissions, |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
515 | 555 |
516 void WebApkInstaller::OnSuccess() { | 556 void WebApkInstaller::OnSuccess() { |
517 finish_callback_.Run(true, webapk_package_); | 557 finish_callback_.Run(true, webapk_package_); |
518 delete this; | 558 delete this; |
519 } | 559 } |
520 | 560 |
521 void WebApkInstaller::OnFailure() { | 561 void WebApkInstaller::OnFailure() { |
522 finish_callback_.Run(false, webapk_package_); | 562 finish_callback_.Run(false, webapk_package_); |
523 delete this; | 563 delete this; |
524 } | 564 } |
OLD | NEW |