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 "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_util.h" | 16 #include "base/strings/string_util.h" |
| 17 #include "base/strings/stringprintf.h" | 17 #include "base/strings/stringprintf.h" |
| 18 #include "base/strings/utf_string_conversions.h" | 18 #include "base/strings/utf_string_conversions.h" |
| 19 #include "base/task_runner_util.h" | 19 #include "base/task_runner_util.h" |
| 20 #include "base/threading/platform_thread.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" |
| 26 #include "components/version_info/version_info.h" | 27 #include "components/version_info/version_info.h" |
| 27 #include "content/public/browser/browser_thread.h" | 28 #include "content/public/browser/browser_thread.h" |
| 28 #include "content/public/common/manifest_util.h" | 29 #include "content/public/common/manifest_util.h" |
| 29 #include "jni/WebApkInstaller_jni.h" | 30 #include "jni/WebApkInstaller_jni.h" |
| (...skipping 318 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 348 | 349 |
| 349 void WebApkInstaller::OnGotWebApkDownloadUrl(const GURL& download_url, | 350 void WebApkInstaller::OnGotWebApkDownloadUrl(const GURL& download_url, |
| 350 const std::string& package_name) { | 351 const std::string& package_name) { |
| 351 base::FilePath output_dir; | 352 base::FilePath output_dir; |
| 352 base::android::GetCacheDirectory(&output_dir); | 353 base::android::GetCacheDirectory(&output_dir); |
| 353 webapk_package_ = package_name; | 354 webapk_package_ = package_name; |
| 354 // TODO(pkotwicz): Download WebAPKs into WebAPK-specific subdirectory | 355 // TODO(pkotwicz): Download WebAPKs into WebAPK-specific subdirectory |
| 355 // directory. | 356 // directory. |
| 356 // TODO(pkotwicz): Figure out when downloaded WebAPK should be deleted. | 357 // TODO(pkotwicz): Figure out when downloaded WebAPK should be deleted. |
| 357 | 358 |
| 359 StartDownloadingWebApk( | |
| 360 output_dir.AppendASCII(webapk_package_), download_url, true); | |
| 361 } | |
| 362 | |
| 363 void WebApkInstaller::StartDownloadingWebApk(const base::FilePath& output_path, | |
|
pkotwicz
2016/09/27 19:47:28
Nit: Rename this function to DownloadWebApk()
I t
Xi Han
2016/09/27 20:53:31
Done.
| |
| 364 const GURL& download_url, | |
| 365 bool retry_if_fails) { | |
| 358 timer_.Start( | 366 timer_.Start( |
| 359 FROM_HERE, base::TimeDelta::FromMilliseconds(download_timeout_ms_), | 367 FROM_HERE, base::TimeDelta::FromMilliseconds(download_timeout_ms_), |
| 360 base::Bind(&WebApkInstaller::OnTimeout, weak_ptr_factory_.GetWeakPtr())); | 368 base::Bind(&WebApkInstaller::OnTimeout, weak_ptr_factory_.GetWeakPtr())); |
| 361 | 369 |
| 362 base::FilePath output_path = output_dir.AppendASCII(webapk_package_); | |
| 363 downloader_.reset(new FileDownloader( | 370 downloader_.reset(new FileDownloader( |
| 364 download_url, output_path, true, request_context_getter_, | 371 download_url, output_path, true, request_context_getter_, |
| 365 base::Bind(&WebApkInstaller::OnWebApkDownloaded, | 372 base::Bind(&WebApkInstaller::OnWebApkDownloaded, |
| 366 weak_ptr_factory_.GetWeakPtr(), output_path))); | 373 weak_ptr_factory_.GetWeakPtr(), |
| 374 output_path, download_url, retry_if_fails))); | |
| 367 } | 375 } |
| 368 | 376 |
| 369 void WebApkInstaller::OnWebApkDownloaded(const base::FilePath& file_path, | 377 void WebApkInstaller::OnWebApkDownloaded(const base::FilePath& file_path, |
| 378 const GURL& download_url, | |
| 379 bool retry_if_fails, | |
| 370 FileDownloader::Result result) { | 380 FileDownloader::Result result) { |
| 371 timer_.Stop(); | 381 timer_.Stop(); |
|
pkotwicz
2016/09/27 19:47:28
Nit: Maybe reset |downloader_| for clarity
Xi Han
2016/09/27 20:53:31
Done.
| |
| 372 | 382 |
| 373 if (result != FileDownloader::DOWNLOADED) { | 383 if (result != FileDownloader::DOWNLOADED) { |
| 374 OnFailure(); | 384 if (!retry_if_fails) { |
| 385 OnFailure(); | |
| 386 return; | |
| 387 } | |
| 388 | |
| 389 base::PlatformThread::Sleep(base::TimeDelta::FromSeconds(2)); | |
|
pkotwicz
2016/09/27 19:47:28
Use BrowserThread::PostDelayedTask() instead
Xi Han
2016/09/27 20:53:31
Done.
| |
| 390 StartDownloadingWebApk(file_path, download_url, false); | |
| 375 return; | 391 return; |
| 376 } | 392 } |
| 377 | 393 |
| 378 int posix_permissions = base::FILE_PERMISSION_READ_BY_USER | | 394 int posix_permissions = base::FILE_PERMISSION_READ_BY_USER | |
| 379 base::FILE_PERMISSION_WRITE_BY_USER | | 395 base::FILE_PERMISSION_WRITE_BY_USER | |
| 380 base::FILE_PERMISSION_READ_BY_GROUP | | 396 base::FILE_PERMISSION_READ_BY_GROUP | |
| 381 base::FILE_PERMISSION_READ_BY_OTHERS; | 397 base::FILE_PERMISSION_READ_BY_OTHERS; |
| 382 base::PostTaskAndReplyWithResult( | 398 base::PostTaskAndReplyWithResult( |
| 383 GetBackgroundTaskRunner().get(), FROM_HERE, | 399 GetBackgroundTaskRunner().get(), FROM_HERE, |
| 384 base::Bind(&base::SetPosixFilePermissions, file_path, posix_permissions), | 400 base::Bind(&base::SetPosixFilePermissions, file_path, posix_permissions), |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 425 std::string webapk_package = webapk_package_; | 441 std::string webapk_package = webapk_package_; |
| 426 delete this; | 442 delete this; |
| 427 callback.Run(true, webapk_package); | 443 callback.Run(true, webapk_package); |
| 428 } | 444 } |
| 429 | 445 |
| 430 void WebApkInstaller::OnFailure() { | 446 void WebApkInstaller::OnFailure() { |
| 431 FinishCallback callback = finish_callback_; | 447 FinishCallback callback = finish_callback_; |
| 432 delete this; | 448 delete this; |
| 433 callback.Run(false, ""); | 449 callback.Run(false, ""); |
| 434 } | 450 } |
| OLD | NEW |