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" |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 45 const char kProtoMimeType[] = "application/x-protobuf"; | 45 const char kProtoMimeType[] = "application/x-protobuf"; |
| 46 | 46 |
| 47 // The default number of milliseconds to wait for the WebAPK download URL from | 47 // The default number of milliseconds to wait for the WebAPK download URL from |
| 48 // the WebAPK server. | 48 // the WebAPK server. |
| 49 const int kWebApkDownloadUrlTimeoutMs = 60000; | 49 const int kWebApkDownloadUrlTimeoutMs = 60000; |
| 50 | 50 |
| 51 // The default number of milliseconds to wait for the WebAPK download to | 51 // The default number of milliseconds to wait for the WebAPK download to |
| 52 // complete. | 52 // complete. |
| 53 const int kDownloadTimeoutMs = 60000; | 53 const int kDownloadTimeoutMs = 60000; |
| 54 | 54 |
| 55 const int kWorldReadableFilePermission = base::FILE_PERMISSION_READ_BY_USER | | |
| 56 base::FILE_PERMISSION_READ_BY_GROUP | | |
| 57 base::FILE_PERMISSION_READ_BY_OTHERS; | |
| 58 | |
| 55 // Returns the scope from |info| if it is specified. Otherwise, returns the | 59 // Returns the scope from |info| if it is specified. Otherwise, returns the |
| 56 // default scope. | 60 // default scope. |
| 57 GURL GetScope(const ShortcutInfo& info) { | 61 GURL GetScope(const ShortcutInfo& info) { |
| 58 return (info.scope.is_valid()) | 62 return (info.scope.is_valid()) |
| 59 ? info.scope | 63 ? info.scope |
| 60 : ShortcutHelper::GetScopeFromURL(info.url); | 64 : ShortcutHelper::GetScopeFromURL(info.url); |
| 61 } | 65 } |
| 62 | 66 |
| 63 // Converts a color from the format specified in content::Manifest to a CSS | 67 // Converts a color from the format specified in content::Manifest to a CSS |
| 64 // string. | 68 // string. |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 121 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN); | 125 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN); |
| 122 } | 126 } |
| 123 | 127 |
| 124 GURL GetServerUrlForUpdate(const GURL& server_url, | 128 GURL GetServerUrlForUpdate(const GURL& server_url, |
| 125 const std::string& webapk_package) { | 129 const std::string& webapk_package) { |
| 126 // crbug.com/636552. Simplify the server URL. | 130 // crbug.com/636552. Simplify the server URL. |
| 127 return GURL(server_url.spec() + webapk_package + "/" + | 131 return GURL(server_url.spec() + webapk_package + "/" + |
| 128 kDefaultWebApkServerUrlResponseType); | 132 kDefaultWebApkServerUrlResponseType); |
| 129 } | 133 } |
| 130 | 134 |
| 135 // Creates a directory depending on the type of the task, and set permissions. | |
| 136 // It also creates any parent directory along the path if doesn't exist, | |
| 137 // and sets permissions as well. | |
| 138 // The previously downloaded APKs are deleted in order to clean up unused cached | |
| 139 // data. | |
| 140 base::FilePath CreateSubDirAndSetPermissionsInBackground( | |
| 141 const base::StringPiece& output_dir_name, | |
| 142 const std::string& package_name) { | |
| 143 base::FilePath output_root_dir; | |
| 144 base::android::GetCacheDirectory(&output_root_dir); | |
| 145 base::FilePath webapk_dir = output_root_dir.AppendASCII("webapks"); | |
| 146 // Creating different downloaded directory for install/update cases is | |
| 147 // to prevent deleting the APK which is still in use when an install and an | |
| 148 // update happen at the same time. However, it doesn't help the cases of when | |
| 149 // mutiple installs (or multiple updates) happen at the same time. | |
| 150 base::FilePath output_dir = webapk_dir.AppendASCII(output_dir_name); | |
| 151 int posix_permissions = kWorldReadableFilePermission | | |
| 152 base::FILE_PERMISSION_WRITE_BY_USER | | |
| 153 base::FILE_PERMISSION_EXECUTE_BY_USER | | |
| 154 base::FILE_PERMISSION_EXECUTE_BY_OTHERS; | |
| 155 if (base::PathExists(output_dir)) | |
| 156 base::DeleteFile(output_dir, true); | |
| 157 | |
| 158 // Creates the directory to download and sets permissions. | |
| 159 if (!base::CreateDirectory(output_dir) || | |
| 160 !base::SetPosixFilePermissions(webapk_dir, posix_permissions) || | |
| 161 !base::SetPosixFilePermissions(output_dir, posix_permissions)) | |
| 162 return base::FilePath(); | |
|
dominickn
2016/09/28 02:15:03
Minor nit/question: is it appropriate to clean up
Xi Han
2016/09/28 02:47:54
Good point. I am not sure in which cases the setti
| |
| 163 | |
| 164 return output_dir; | |
| 165 } | |
| 166 | |
| 131 } // anonymous namespace | 167 } // anonymous namespace |
| 132 | 168 |
| 133 WebApkInstaller::WebApkInstaller(const ShortcutInfo& shortcut_info, | 169 WebApkInstaller::WebApkInstaller(const ShortcutInfo& shortcut_info, |
| 134 const SkBitmap& shortcut_icon) | 170 const SkBitmap& shortcut_icon) |
| 135 : shortcut_info_(shortcut_info), | 171 : shortcut_info_(shortcut_info), |
| 136 shortcut_icon_(shortcut_icon), | 172 shortcut_icon_(shortcut_icon), |
| 137 webapk_download_url_timeout_ms_(kWebApkDownloadUrlTimeoutMs), | 173 webapk_download_url_timeout_ms_(kWebApkDownloadUrlTimeoutMs), |
| 138 download_timeout_ms_(kDownloadTimeoutMs), | 174 download_timeout_ms_(kDownloadTimeoutMs), |
| 139 task_type_(UNDEFINED), | 175 task_type_(UNDEFINED), |
| 140 weak_ptr_factory_(this) { | 176 weak_ptr_factory_(this) { |
| (...skipping 200 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 341 url_fetcher_ = net::URLFetcher::Create(server_url, request_type, this); | 377 url_fetcher_ = net::URLFetcher::Create(server_url, request_type, this); |
| 342 url_fetcher_->SetRequestContext(request_context_getter_); | 378 url_fetcher_->SetRequestContext(request_context_getter_); |
| 343 std::string serialized_request; | 379 std::string serialized_request; |
| 344 request_proto->SerializeToString(&serialized_request); | 380 request_proto->SerializeToString(&serialized_request); |
| 345 url_fetcher_->SetUploadData(kProtoMimeType, serialized_request); | 381 url_fetcher_->SetUploadData(kProtoMimeType, serialized_request); |
| 346 url_fetcher_->Start(); | 382 url_fetcher_->Start(); |
| 347 } | 383 } |
| 348 | 384 |
| 349 void WebApkInstaller::OnGotWebApkDownloadUrl(const GURL& download_url, | 385 void WebApkInstaller::OnGotWebApkDownloadUrl(const GURL& download_url, |
| 350 const std::string& package_name) { | 386 const std::string& package_name) { |
| 351 base::FilePath output_dir; | |
| 352 base::android::GetCacheDirectory(&output_dir); | |
| 353 webapk_package_ = package_name; | 387 webapk_package_ = package_name; |
| 354 // TODO(pkotwicz): Download WebAPKs into WebAPK-specific subdirectory | 388 |
| 355 // directory. | 389 base::PostTaskAndReplyWithResult( |
| 356 // TODO(pkotwicz): Figure out when downloaded WebAPK should be deleted. | 390 GetBackgroundTaskRunner().get(), FROM_HERE, |
| 391 base::Bind(&CreateSubDirAndSetPermissionsInBackground, | |
| 392 task_type_ == WebApkInstaller::INSTALL ? "install" : "update", | |
| 393 package_name), | |
| 394 base::Bind(&WebApkInstaller::OnCreatedSubDirAndSetPermissions, | |
| 395 weak_ptr_factory_.GetWeakPtr(), download_url)); | |
| 396 } | |
| 397 | |
| 398 void WebApkInstaller::OnCreatedSubDirAndSetPermissions( | |
| 399 const GURL& download_url, const base::FilePath& output_dir) { | |
| 400 if (output_dir.empty()) { | |
| 401 OnFailure(); | |
| 402 return; | |
| 403 } | |
| 357 | 404 |
| 358 timer_.Start( | 405 timer_.Start( |
| 359 FROM_HERE, base::TimeDelta::FromMilliseconds(download_timeout_ms_), | 406 FROM_HERE, base::TimeDelta::FromMilliseconds(download_timeout_ms_), |
| 360 base::Bind(&WebApkInstaller::OnTimeout, weak_ptr_factory_.GetWeakPtr())); | 407 base::Bind(&WebApkInstaller::OnTimeout, weak_ptr_factory_.GetWeakPtr())); |
| 361 | 408 |
| 362 base::FilePath output_path = output_dir.AppendASCII(webapk_package_); | 409 base::FilePath output_path = output_dir.AppendASCII(webapk_package_); |
| 363 downloader_.reset(new FileDownloader( | 410 downloader_.reset(new FileDownloader( |
| 364 download_url, output_path, true, request_context_getter_, | 411 download_url, output_path, true, request_context_getter_, |
| 365 base::Bind(&WebApkInstaller::OnWebApkDownloaded, | 412 base::Bind(&WebApkInstaller::OnWebApkDownloaded, |
| 366 weak_ptr_factory_.GetWeakPtr(), output_path))); | 413 weak_ptr_factory_.GetWeakPtr(), output_path))); |
| 367 } | 414 } |
| 368 | 415 |
| 369 void WebApkInstaller::OnWebApkDownloaded(const base::FilePath& file_path, | 416 void WebApkInstaller::OnWebApkDownloaded(const base::FilePath& file_path, |
| 370 FileDownloader::Result result) { | 417 FileDownloader::Result result) { |
| 371 timer_.Stop(); | 418 timer_.Stop(); |
| 372 | 419 |
| 373 if (result != FileDownloader::DOWNLOADED) { | 420 if (result != FileDownloader::DOWNLOADED) { |
| 374 OnFailure(); | 421 OnFailure(); |
| 375 return; | 422 return; |
| 376 } | 423 } |
| 377 | 424 |
| 378 int posix_permissions = base::FILE_PERMISSION_READ_BY_USER | | 425 int posix_permissions = kWorldReadableFilePermission | |
| 379 base::FILE_PERMISSION_WRITE_BY_USER | | 426 base::FILE_PERMISSION_WRITE_BY_USER | |
| 380 base::FILE_PERMISSION_READ_BY_GROUP | | 427 base::FILE_PERMISSION_EXECUTE_BY_USER; |
| 381 base::FILE_PERMISSION_READ_BY_OTHERS; | |
| 382 base::PostTaskAndReplyWithResult( | 428 base::PostTaskAndReplyWithResult( |
| 383 GetBackgroundTaskRunner().get(), FROM_HERE, | 429 GetBackgroundTaskRunner().get(), FROM_HERE, |
| 384 base::Bind(&base::SetPosixFilePermissions, file_path, posix_permissions), | 430 base::Bind(&base::SetPosixFilePermissions, file_path, posix_permissions), |
| 385 base::Bind(&WebApkInstaller::OnWebApkMadeWorldReadable, | 431 base::Bind(&WebApkInstaller::OnWebApkMadeWorldReadable, |
| 386 weak_ptr_factory_.GetWeakPtr(), file_path)); | 432 weak_ptr_factory_.GetWeakPtr(), file_path)); |
| 387 } | 433 } |
| 388 | 434 |
| 389 void WebApkInstaller::OnWebApkMadeWorldReadable( | 435 void WebApkInstaller::OnWebApkMadeWorldReadable( |
| 390 const base::FilePath& file_path, | 436 const base::FilePath& file_path, |
| 391 bool change_permission_success) { | 437 bool change_permission_success) { |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 425 std::string webapk_package = webapk_package_; | 471 std::string webapk_package = webapk_package_; |
| 426 delete this; | 472 delete this; |
| 427 callback.Run(true, webapk_package); | 473 callback.Run(true, webapk_package); |
| 428 } | 474 } |
| 429 | 475 |
| 430 void WebApkInstaller::OnFailure() { | 476 void WebApkInstaller::OnFailure() { |
| 431 FinishCallback callback = finish_callback_; | 477 FinishCallback callback = finish_callback_; |
| 432 delete this; | 478 delete this; |
| 433 callback.Run(false, ""); | 479 callback.Run(false, ""); |
| 434 } | 480 } |
| OLD | NEW |