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_WRITE_BY_USER | | |
|
dominickn
2016/09/27 03:16:24
Why is WRITE_BY_USER in the world-readable list he
Xi Han
2016/09/27 12:55:13
Good point, move it out.
| |
| 57 base::FILE_PERMISSION_READ_BY_GROUP | | |
| 58 base::FILE_PERMISSION_READ_BY_OTHERS; | |
| 59 | |
| 55 // Returns the scope from |info| if it is specified. Otherwise, returns the | 60 // Returns the scope from |info| if it is specified. Otherwise, returns the |
| 56 // default scope. | 61 // default scope. |
| 57 GURL GetScope(const ShortcutInfo& info) { | 62 GURL GetScope(const ShortcutInfo& info) { |
| 58 return (info.scope.is_valid()) | 63 return (info.scope.is_valid()) |
| 59 ? info.scope | 64 ? info.scope |
| 60 : ShortcutHelper::GetScopeFromURL(info.url); | 65 : ShortcutHelper::GetScopeFromURL(info.url); |
| 61 } | 66 } |
| 62 | 67 |
| 63 // Converts a color from the format specified in content::Manifest to a CSS | 68 // Converts a color from the format specified in content::Manifest to a CSS |
| 64 // string. | 69 // string. |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 121 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN); | 126 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN); |
| 122 } | 127 } |
| 123 | 128 |
| 124 GURL GetServerUrlForUpdate(const GURL& server_url, | 129 GURL GetServerUrlForUpdate(const GURL& server_url, |
| 125 const std::string& webapk_package) { | 130 const std::string& webapk_package) { |
| 126 // crbug.com/636552. Simplify the server URL. | 131 // crbug.com/636552. Simplify the server URL. |
| 127 return GURL(server_url.spec() + webapk_package + "/" + | 132 return GURL(server_url.spec() + webapk_package + "/" + |
| 128 kDefaultWebApkServerUrlResponseType); | 133 kDefaultWebApkServerUrlResponseType); |
| 129 } | 134 } |
| 130 | 135 |
| 136 // Creates a directory depending on the type of the task, and set permissions. | |
| 137 // It also creates any parent directory along the path if doesn't exist, | |
| 138 // and sets permissions as well. | |
| 139 // The previously downloaded APKs are deleted in order to clean up unused | |
|
dominickn
2016/09/27 03:16:24
Nit: fill comment to 80 chaarcters
Xi Han
2016/09/27 12:55:13
Done.
| |
| 140 // cached data. | |
| 141 base::FilePath CreateSubDirAndSetPermissionsInBackground( | |
| 142 WebApkInstaller::TaskType task_type, | |
| 143 const std::string& package_name) { | |
| 144 base::FilePath output_root_dir; | |
| 145 base::android::GetCacheDirectory(&output_root_dir); | |
| 146 base::FilePath webapk_dir = output_root_dir.AppendASCII("webapks"); | |
| 147 // Creating different downloaded directory for install/update cases is | |
| 148 // to prevent deleting the APK which is still in use when an install and an | |
| 149 // update happen at the same time. However, it doesn't help the cases of when | |
| 150 // mutiple installs (or multiple updates) happen at the same time. | |
| 151 base::FilePath output_dir = webapk_dir.AppendASCII( | |
| 152 task_type == WebApkInstaller::INSTALL ? "install" : "update"); | |
| 153 int posix_permissions = kWorldReadableFilePermission | | |
| 154 base::FILE_PERMISSION_EXECUTE_BY_USER | | |
| 155 base::FILE_PERMISSION_EXECUTE_BY_OTHERS; | |
| 156 if (base::PathExists(output_dir)) | |
| 157 base::DeleteFile(output_dir, true); | |
| 158 | |
| 159 // Creates the directory to download and sets permissions. | |
| 160 if (!base::CreateDirectory(output_dir) || | |
| 161 !base::SetPosixFilePermissions(webapk_dir, posix_permissions) || | |
| 162 !base::SetPosixFilePermissions(output_dir, posix_permissions)) | |
| 163 return base::FilePath(); | |
| 164 | |
| 165 return output_dir; | |
| 166 } | |
| 167 | |
| 131 } // anonymous namespace | 168 } // anonymous namespace |
| 132 | 169 |
| 133 WebApkInstaller::WebApkInstaller(const ShortcutInfo& shortcut_info, | 170 WebApkInstaller::WebApkInstaller(const ShortcutInfo& shortcut_info, |
| 134 const SkBitmap& shortcut_icon) | 171 const SkBitmap& shortcut_icon) |
| 135 : shortcut_info_(shortcut_info), | 172 : shortcut_info_(shortcut_info), |
| 136 shortcut_icon_(shortcut_icon), | 173 shortcut_icon_(shortcut_icon), |
| 137 webapk_download_url_timeout_ms_(kWebApkDownloadUrlTimeoutMs), | 174 webapk_download_url_timeout_ms_(kWebApkDownloadUrlTimeoutMs), |
| 138 download_timeout_ms_(kDownloadTimeoutMs), | 175 download_timeout_ms_(kDownloadTimeoutMs), |
| 139 task_type_(UNDEFINED), | 176 task_type_(UNDEFINED), |
| 140 weak_ptr_factory_(this) { | 177 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); | 378 url_fetcher_ = net::URLFetcher::Create(server_url, request_type, this); |
| 342 url_fetcher_->SetRequestContext(request_context_getter_); | 379 url_fetcher_->SetRequestContext(request_context_getter_); |
| 343 std::string serialized_request; | 380 std::string serialized_request; |
| 344 request_proto->SerializeToString(&serialized_request); | 381 request_proto->SerializeToString(&serialized_request); |
| 345 url_fetcher_->SetUploadData(kProtoMimeType, serialized_request); | 382 url_fetcher_->SetUploadData(kProtoMimeType, serialized_request); |
| 346 url_fetcher_->Start(); | 383 url_fetcher_->Start(); |
| 347 } | 384 } |
| 348 | 385 |
| 349 void WebApkInstaller::OnGotWebApkDownloadUrl(const GURL& download_url, | 386 void WebApkInstaller::OnGotWebApkDownloadUrl(const GURL& download_url, |
| 350 const std::string& package_name) { | 387 const std::string& package_name) { |
| 351 base::FilePath output_dir; | |
| 352 base::android::GetCacheDirectory(&output_dir); | |
| 353 webapk_package_ = package_name; | 388 webapk_package_ = package_name; |
| 354 // TODO(pkotwicz): Download WebAPKs into WebAPK-specific subdirectory | 389 |
| 355 // directory. | 390 base::PostTaskAndReplyWithResult( |
| 356 // TODO(pkotwicz): Figure out when downloaded WebAPK should be deleted. | 391 GetBackgroundTaskRunner().get(), FROM_HERE, |
| 392 base::Bind(&CreateSubDirAndSetPermissionsInBackground, | |
| 393 task_type_, 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_EXECUTE_BY_USER; |
| 380 base::FILE_PERMISSION_READ_BY_GROUP | | |
| 381 base::FILE_PERMISSION_READ_BY_OTHERS; | |
| 382 base::PostTaskAndReplyWithResult( | 427 base::PostTaskAndReplyWithResult( |
| 383 GetBackgroundTaskRunner().get(), FROM_HERE, | 428 GetBackgroundTaskRunner().get(), FROM_HERE, |
| 384 base::Bind(&base::SetPosixFilePermissions, file_path, posix_permissions), | 429 base::Bind(&base::SetPosixFilePermissions, file_path, |
| 430 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) { |
| 392 if (!change_permission_success) { | 438 if (!change_permission_success) { |
| 393 OnFailure(); | 439 OnFailure(); |
| 394 return; | 440 return; |
| (...skipping 30 matching lines...) Expand all 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 |