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 KBasicPosixPermissions = base::FILE_PERMISSION_READ_BY_USER | | |
| 56 base::FILE_PERMISSION_WRITE_BY_USER | | |
| 57 base::FILE_PERMISSION_EXECUTE_BY_USER | | |
| 58 base::FILE_PERMISSION_READ_BY_GROUP | | |
| 59 base::FILE_PERMISSION_WRITE_BY_GROUP | | |
| 60 base::FILE_PERMISSION_READ_BY_OTHERS | | |
| 61 base::FILE_PERMISSION_WRITE_BY_OTHERS; | |
|
pkotwicz
2016/09/24 01:28:35
I did some local testing. According to my testing,
Xi Han
2016/09/26 17:17:11
Good catch, reverted it back.
| |
| 62 | |
| 55 // Returns the scope from |info| if it is specified. Otherwise, returns the | 63 // Returns the scope from |info| if it is specified. Otherwise, returns the |
| 56 // default scope. | 64 // default scope. |
| 57 GURL GetScope(const ShortcutInfo& info) { | 65 GURL GetScope(const ShortcutInfo& info) { |
| 58 return (info.scope.is_valid()) | 66 return (info.scope.is_valid()) |
| 59 ? info.scope | 67 ? info.scope |
| 60 : ShortcutHelper::GetScopeFromURL(info.url); | 68 : ShortcutHelper::GetScopeFromURL(info.url); |
| 61 } | 69 } |
| 62 | 70 |
| 63 // Converts a color from the format specified in content::Manifest to a CSS | 71 // Converts a color from the format specified in content::Manifest to a CSS |
| 64 // string. | 72 // string. |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 121 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN); | 129 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN); |
| 122 } | 130 } |
| 123 | 131 |
| 124 GURL GetServerUrlForUpdate(const GURL& server_url, | 132 GURL GetServerUrlForUpdate(const GURL& server_url, |
| 125 const std::string& webapk_package) { | 133 const std::string& webapk_package) { |
| 126 // crbug.com/636552. Simplify the server URL. | 134 // crbug.com/636552. Simplify the server URL. |
| 127 return GURL(server_url.spec() + webapk_package + "/" + | 135 return GURL(server_url.spec() + webapk_package + "/" + |
| 128 kDefaultWebApkServerUrlResponseType); | 136 kDefaultWebApkServerUrlResponseType); |
| 129 } | 137 } |
| 130 | 138 |
| 139 // Creates a directory depending on the type of the task, and set permissions. | |
| 140 // It also creates any parent directory along the path if doesn't exist, | |
| 141 // and sets permissions as well. | |
| 142 base::FilePath CreateSubDirAndSetPermissionsInBackground( | |
| 143 const base::FilePath& output_root_dir, | |
| 144 WebApkInstaller::TaskType task_type, | |
| 145 const std::string& package_name) { | |
| 146 base::FilePath webapk_dir = output_root_dir.AppendASCII("webapks"); | |
| 147 base::FilePath output_dir = webapk_dir.AppendASCII( | |
| 148 task_type == WebApkInstaller::INSTALL ? "install" : "update"); | |
|
pkotwicz
2016/09/24 01:28:34
Can you explain why you use a different directory
Xi Han
2016/09/26 17:17:10
As discussed offline, add a comment here.
| |
| 149 int posix_permissions = KBasicPosixPermissions | | |
| 150 base::FILE_PERMISSION_EXECUTE_BY_OTHERS; | |
| 151 if (base::PathExists(output_dir)) | |
| 152 base::DeleteFile(output_dir, true); | |
| 153 | |
| 154 // Creates the directory to download and sets permissions. | |
| 155 if (!base::CreateDirectory(output_dir) || | |
| 156 !base::SetPosixFilePermissions(webapk_dir, posix_permissions) || | |
| 157 !base::SetPosixFilePermissions(output_dir, posix_permissions)) | |
| 158 return base::FilePath(); | |
| 159 | |
| 160 return output_dir; | |
| 161 } | |
| 162 | |
| 131 } // anonymous namespace | 163 } // anonymous namespace |
| 132 | 164 |
| 133 WebApkInstaller::WebApkInstaller(const ShortcutInfo& shortcut_info, | 165 WebApkInstaller::WebApkInstaller(const ShortcutInfo& shortcut_info, |
| 134 const SkBitmap& shortcut_icon) | 166 const SkBitmap& shortcut_icon) |
| 135 : shortcut_info_(shortcut_info), | 167 : shortcut_info_(shortcut_info), |
| 136 shortcut_icon_(shortcut_icon), | 168 shortcut_icon_(shortcut_icon), |
| 137 webapk_download_url_timeout_ms_(kWebApkDownloadUrlTimeoutMs), | 169 webapk_download_url_timeout_ms_(kWebApkDownloadUrlTimeoutMs), |
| 138 download_timeout_ms_(kDownloadTimeoutMs), | 170 download_timeout_ms_(kDownloadTimeoutMs), |
| 139 task_type_(UNDEFINED), | 171 task_type_(UNDEFINED), |
| 140 weak_ptr_factory_(this) { | 172 weak_ptr_factory_(this) { |
| (...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 344 request_proto->SerializeToString(&serialized_request); | 376 request_proto->SerializeToString(&serialized_request); |
| 345 url_fetcher_->SetUploadData(kProtoMimeType, serialized_request); | 377 url_fetcher_->SetUploadData(kProtoMimeType, serialized_request); |
| 346 url_fetcher_->Start(); | 378 url_fetcher_->Start(); |
| 347 } | 379 } |
| 348 | 380 |
| 349 void WebApkInstaller::OnGotWebApkDownloadUrl(const GURL& download_url, | 381 void WebApkInstaller::OnGotWebApkDownloadUrl(const GURL& download_url, |
| 350 const std::string& package_name) { | 382 const std::string& package_name) { |
| 351 base::FilePath output_dir; | 383 base::FilePath output_dir; |
| 352 base::android::GetCacheDirectory(&output_dir); | 384 base::android::GetCacheDirectory(&output_dir); |
| 353 webapk_package_ = package_name; | 385 webapk_package_ = package_name; |
| 354 // TODO(pkotwicz): Download WebAPKs into WebAPK-specific subdirectory | 386 |
| 355 // directory. | 387 base::PostTaskAndReplyWithResult( |
| 356 // TODO(pkotwicz): Figure out when downloaded WebAPK should be deleted. | 388 GetBackgroundTaskRunner().get(), FROM_HERE, |
| 389 base::Bind(&CreateSubDirAndSetPermissionsInBackground, | |
| 390 output_dir, task_type_, package_name), | |
| 391 base::Bind(&WebApkInstaller::OnCreateSubDirAndSetPermissions, | |
| 392 weak_ptr_factory_.GetWeakPtr(), download_url)); | |
| 393 } | |
| 394 | |
| 395 void WebApkInstaller::OnCreateSubDirAndSetPermissions( | |
| 396 const GURL& download_url, const base::FilePath& output_dir) { | |
| 397 if (output_dir.empty()) { | |
| 398 OnFailure(); | |
| 399 return; | |
| 400 } | |
| 357 | 401 |
| 358 timer_.Start( | 402 timer_.Start( |
| 359 FROM_HERE, base::TimeDelta::FromMilliseconds(download_timeout_ms_), | 403 FROM_HERE, base::TimeDelta::FromMilliseconds(download_timeout_ms_), |
| 360 base::Bind(&WebApkInstaller::OnTimeout, weak_ptr_factory_.GetWeakPtr())); | 404 base::Bind(&WebApkInstaller::OnTimeout, weak_ptr_factory_.GetWeakPtr())); |
| 361 | 405 |
| 362 base::FilePath output_path = output_dir.AppendASCII(webapk_package_); | 406 base::FilePath output_path = output_dir.AppendASCII(webapk_package_); |
| 363 downloader_.reset(new FileDownloader( | 407 downloader_.reset(new FileDownloader( |
| 364 download_url, output_path, true, request_context_getter_, | 408 download_url, output_path, true, request_context_getter_, |
| 365 base::Bind(&WebApkInstaller::OnWebApkDownloaded, | 409 base::Bind(&WebApkInstaller::OnWebApkDownloaded, |
| 366 weak_ptr_factory_.GetWeakPtr(), output_path))); | 410 weak_ptr_factory_.GetWeakPtr(), output_path))); |
| 367 } | 411 } |
| 368 | 412 |
| 369 void WebApkInstaller::OnWebApkDownloaded(const base::FilePath& file_path, | 413 void WebApkInstaller::OnWebApkDownloaded(const base::FilePath& file_path, |
| 370 FileDownloader::Result result) { | 414 FileDownloader::Result result) { |
| 371 timer_.Stop(); | 415 timer_.Stop(); |
| 372 | 416 |
| 373 if (result != FileDownloader::DOWNLOADED) { | 417 if (result != FileDownloader::DOWNLOADED) { |
| 374 OnFailure(); | 418 OnFailure(); |
| 375 return; | 419 return; |
| 376 } | 420 } |
| 377 | 421 |
| 378 int posix_permissions = base::FILE_PERMISSION_READ_BY_USER | | |
| 379 base::FILE_PERMISSION_WRITE_BY_USER | | |
| 380 base::FILE_PERMISSION_READ_BY_GROUP | | |
| 381 base::FILE_PERMISSION_READ_BY_OTHERS; | |
| 382 base::PostTaskAndReplyWithResult( | 422 base::PostTaskAndReplyWithResult( |
| 383 GetBackgroundTaskRunner().get(), FROM_HERE, | 423 GetBackgroundTaskRunner().get(), FROM_HERE, |
| 384 base::Bind(&base::SetPosixFilePermissions, file_path, posix_permissions), | 424 base::Bind(&base::SetPosixFilePermissions, file_path, |
| 425 KBasicPosixPermissions), | |
| 385 base::Bind(&WebApkInstaller::OnWebApkMadeWorldReadable, | 426 base::Bind(&WebApkInstaller::OnWebApkMadeWorldReadable, |
| 386 weak_ptr_factory_.GetWeakPtr(), file_path)); | 427 weak_ptr_factory_.GetWeakPtr(), file_path)); |
| 387 } | 428 } |
| 388 | 429 |
| 389 void WebApkInstaller::OnWebApkMadeWorldReadable( | 430 void WebApkInstaller::OnWebApkMadeWorldReadable( |
| 390 const base::FilePath& file_path, | 431 const base::FilePath& file_path, |
| 391 bool change_permission_success) { | 432 bool change_permission_success) { |
| 392 if (!change_permission_success) { | 433 if (!change_permission_success) { |
| 393 OnFailure(); | 434 OnFailure(); |
| 394 return; | 435 return; |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 425 std::string webapk_package = webapk_package_; | 466 std::string webapk_package = webapk_package_; |
| 426 delete this; | 467 delete this; |
| 427 callback.Run(true, webapk_package); | 468 callback.Run(true, webapk_package); |
| 428 } | 469 } |
| 429 | 470 |
| 430 void WebApkInstaller::OnFailure() { | 471 void WebApkInstaller::OnFailure() { |
| 431 FinishCallback callback = finish_callback_; | 472 FinishCallback callback = finish_callback_; |
| 432 delete this; | 473 delete this; |
| 433 callback.Run(false, ""); | 474 callback.Run(false, ""); |
| 434 } | 475 } |
| OLD | NEW |