Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(263)

Side by Side Diff: chrome/browser/android/webapk/webapk_installer.cc

Issue 2515293004: Chrome talks to Play to install WebAPKs. (Closed)
Patch Set: Add a test for play install webapk. Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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"
11 #include "base/bind.h" 11 #include "base/bind.h"
12 #include "base/command_line.h" 12 #include "base/command_line.h"
13 #include "base/files/file_path.h" 13 #include "base/files/file_path.h"
14 #include "base/files/file_util.h" 14 #include "base/files/file_util.h"
15 #include "base/memory/ref_counted.h" 15 #include "base/memory/ref_counted.h"
16 #include "base/strings/string_number_conversions.h"
16 #include "base/strings/string_util.h" 17 #include "base/strings/string_util.h"
17 #include "base/strings/stringprintf.h" 18 #include "base/strings/stringprintf.h"
18 #include "base/strings/utf_string_conversions.h" 19 #include "base/strings/utf_string_conversions.h"
19 #include "base/task_runner_util.h" 20 #include "base/task_runner_util.h"
20 #include "base/threading/sequenced_worker_pool.h" 21 #include "base/threading/sequenced_worker_pool.h"
21 #include "chrome/browser/android/shortcut_helper.h" 22 #include "chrome/browser/android/shortcut_helper.h"
22 #include "chrome/browser/android/webapk/webapk.pb.h" 23 #include "chrome/browser/android/webapk/webapk.pb.h"
23 #include "chrome/browser/android/webapk/webapk_icon_hasher.h" 24 #include "chrome/browser/android/webapk/webapk_icon_hasher.h"
24 #include "chrome/browser/profiles/profile.h" 25 #include "chrome/browser/profiles/profile.h"
25 #include "chrome/common/chrome_switches.h" 26 #include "chrome/common/chrome_switches.h"
(...skipping 282 matching lines...) Expand 10 before | Expand all | Expand 10 after
308 env, java_ref_, java_file_path, java_package_name); 309 env, java_ref_, java_file_path, java_package_name);
309 } 310 }
310 311
311 bool WebApkInstaller::StartUpdateUsingDownloadedWebApk( 312 bool WebApkInstaller::StartUpdateUsingDownloadedWebApk(
312 JNIEnv* env, 313 JNIEnv* env,
313 const base::android::ScopedJavaLocalRef<jstring>& java_file_path) { 314 const base::android::ScopedJavaLocalRef<jstring>& java_file_path) {
314 return Java_WebApkInstaller_updateAsyncFromNative( 315 return Java_WebApkInstaller_updateAsyncFromNative(
315 env, java_ref_, java_file_path); 316 env, java_ref_, java_file_path);
316 } 317 }
317 318
319 bool WebApkInstaller::HasGooglePlayWebApkInstallDelegate() {
320 JNIEnv* env = base::android::AttachCurrentThread();
321 return Java_WebApkInstaller_hasGooglePlayWebApkInstallDelegate(
322 env, java_ref_);
323 }
324
318 void WebApkInstaller::OnURLFetchComplete(const net::URLFetcher* source) { 325 void WebApkInstaller::OnURLFetchComplete(const net::URLFetcher* source) {
319 timer_.Stop(); 326 timer_.Stop();
320 327
321 if (!source->GetStatus().is_success() || 328 if (!source->GetStatus().is_success() ||
322 source->GetResponseCode() != net::HTTP_OK) { 329 source->GetResponseCode() != net::HTTP_OK) {
323 OnFailure(); 330 OnFailure();
324 return; 331 return;
325 } 332 }
326 333
327 std::string response_string; 334 std::string response_string;
328 source->GetResponseAsString(&response_string); 335 source->GetResponseAsString(&response_string);
329 336
330 std::unique_ptr<webapk::WebApkResponse> response( 337 std::unique_ptr<webapk::WebApkResponse> response(
331 new webapk::WebApkResponse); 338 new webapk::WebApkResponse);
332 if (!response->ParseFromString(response_string)) { 339 if (!response->ParseFromString(response_string)) {
333 OnFailure(); 340 OnFailure();
334 return; 341 return;
335 } 342 }
336 343
337 GURL signed_download_url(response->signed_download_url()); 344 GURL signed_download_url(response->signed_download_url());
338 if (!signed_download_url.is_valid() || response->package_name().empty()) { 345 if (!signed_download_url.is_valid() || response->package_name().empty()) {
339 OnFailure(); 346 OnFailure();
340 return; 347 return;
341 } 348 }
349
350 if (HasGooglePlayWebApkInstallDelegate()) {
351 int version = 1;
352 base::StringToInt(response->version(), &version);
353 if (!InstallOrUpdateWebApkFromGooglePlay(
354 response->package_name(), version, response->token())) {
355 OnFailure();
356 }
357 return;
358 }
359
342 OnGotWebApkDownloadUrl(signed_download_url, response->package_name()); 360 OnGotWebApkDownloadUrl(signed_download_url, response->package_name());
343 } 361 }
344 362
345 void WebApkInstaller::DownloadAppIconAndComputeMurmur2Hash() { 363 void WebApkInstaller::DownloadAppIconAndComputeMurmur2Hash() {
346 // Safeguard. WebApkIconHasher crashes if asked to fetch an invalid URL. 364 // Safeguard. WebApkIconHasher crashes if asked to fetch an invalid URL.
347 if (!shortcut_info_.best_icon_url.is_valid()) { 365 if (!shortcut_info_.best_icon_url.is_valid()) {
348 OnFailure(); 366 OnFailure();
349 return; 367 return;
350 } 368 }
351 369
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
403 421
404 url_fetcher_ = 422 url_fetcher_ =
405 net::URLFetcher::Create(server_url, net::URLFetcher::POST, this); 423 net::URLFetcher::Create(server_url, net::URLFetcher::POST, this);
406 url_fetcher_->SetRequestContext(request_context_getter_); 424 url_fetcher_->SetRequestContext(request_context_getter_);
407 std::string serialized_request; 425 std::string serialized_request;
408 request_proto->SerializeToString(&serialized_request); 426 request_proto->SerializeToString(&serialized_request);
409 url_fetcher_->SetUploadData(kProtoMimeType, serialized_request); 427 url_fetcher_->SetUploadData(kProtoMimeType, serialized_request);
410 url_fetcher_->Start(); 428 url_fetcher_->Start();
411 } 429 }
412 430
431 bool WebApkInstaller::InstallOrUpdateWebApkFromGooglePlay(
pkotwicz 2016/12/06 16:46:32 Nit: Reorder this function to match order in .h fi
Xi Han 2016/12/06 20:30:38 Done.
432 const std::string& package_name,
433 int version,
434 const std::string& token) {
435 webapk_package_ = package_name;
436
437 JNIEnv* env = base::android::AttachCurrentThread();
438 base::android::ScopedJavaLocalRef<jstring> java_webapk_package =
439 base::android::ConvertUTF8ToJavaString(env, webapk_package_);
440 base::android::ScopedJavaLocalRef<jstring> java_title =
441 base::android::ConvertUTF16ToJavaString(env, shortcut_info_.user_title);
442 base::android::ScopedJavaLocalRef<jstring> java_token =
443 base::android::ConvertUTF8ToJavaString(env, token);
444
445 if (task_type_ == WebApkInstaller::INSTALL) {
446 return Java_WebApkInstaller_installWebApkFromGooglePlayAsync(
447 env, java_ref_, java_webapk_package, version, java_title, java_token);
448 } else {
449 return Java_WebApkInstaller_updateAsyncFromGooglePlay(
450 env, java_ref_, java_webapk_package, version, java_title, java_token);
451 }
452 }
453
413 void WebApkInstaller::OnGotWebApkDownloadUrl(const GURL& download_url, 454 void WebApkInstaller::OnGotWebApkDownloadUrl(const GURL& download_url,
414 const std::string& package_name) { 455 const std::string& package_name) {
415 webapk_package_ = package_name; 456 webapk_package_ = package_name;
416 457
417 base::PostTaskAndReplyWithResult( 458 base::PostTaskAndReplyWithResult(
418 GetBackgroundTaskRunner().get(), FROM_HERE, 459 GetBackgroundTaskRunner().get(), FROM_HERE,
419 base::Bind(&CreateSubDirAndSetPermissionsInBackground, 460 base::Bind(&CreateSubDirAndSetPermissionsInBackground,
420 task_type_ == WebApkInstaller::INSTALL ? "install" : "update", 461 task_type_ == WebApkInstaller::INSTALL ? "install" : "update",
421 package_name), 462 package_name),
422 base::Bind(&WebApkInstaller::OnCreatedSubDirAndSetPermissions, 463 base::Bind(&WebApkInstaller::OnCreatedSubDirAndSetPermissions,
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after
515 556
516 void WebApkInstaller::OnSuccess() { 557 void WebApkInstaller::OnSuccess() {
517 finish_callback_.Run(true, webapk_package_); 558 finish_callback_.Run(true, webapk_package_);
518 delete this; 559 delete this;
519 } 560 }
520 561
521 void WebApkInstaller::OnFailure() { 562 void WebApkInstaller::OnFailure() {
522 finish_callback_.Run(false, webapk_package_); 563 finish_callback_.Run(false, webapk_package_);
523 delete this; 564 delete this;
524 } 565 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698