Chromium Code Reviews| Index: chrome/browser/android/webapk/webapk_installer.cc |
| diff --git a/chrome/browser/android/webapk/webapk_installer.cc b/chrome/browser/android/webapk/webapk_installer.cc |
| index 37df154bd8a3115e4a5972973c4ac9b6b9766e7b..4bc1b60616884bff5c06b50f8d3025856d50c348 100644 |
| --- a/chrome/browser/android/webapk/webapk_installer.cc |
| +++ b/chrome/browser/android/webapk/webapk_installer.cc |
| @@ -52,6 +52,11 @@ const int kWebApkDownloadUrlTimeoutMs = 60000; |
| // complete. |
| const int kDownloadTimeoutMs = 60000; |
| +const int kWorldReadableFilePermission = base::FILE_PERMISSION_READ_BY_USER | |
| + 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.
|
| + base::FILE_PERMISSION_READ_BY_GROUP | |
| + base::FILE_PERMISSION_READ_BY_OTHERS; |
| + |
| // Returns the scope from |info| if it is specified. Otherwise, returns the |
| // default scope. |
| GURL GetScope(const ShortcutInfo& info) { |
| @@ -128,6 +133,38 @@ GURL GetServerUrlForUpdate(const GURL& server_url, |
| kDefaultWebApkServerUrlResponseType); |
| } |
| +// Creates a directory depending on the type of the task, and set permissions. |
| +// It also creates any parent directory along the path if doesn't exist, |
| +// and sets permissions as well. |
| +// 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.
|
| +// cached data. |
| +base::FilePath CreateSubDirAndSetPermissionsInBackground( |
| + WebApkInstaller::TaskType task_type, |
| + const std::string& package_name) { |
| + base::FilePath output_root_dir; |
| + base::android::GetCacheDirectory(&output_root_dir); |
| + base::FilePath webapk_dir = output_root_dir.AppendASCII("webapks"); |
| + // Creating different downloaded directory for install/update cases is |
| + // to prevent deleting the APK which is still in use when an install and an |
| + // update happen at the same time. However, it doesn't help the cases of when |
| + // mutiple installs (or multiple updates) happen at the same time. |
| + base::FilePath output_dir = webapk_dir.AppendASCII( |
| + task_type == WebApkInstaller::INSTALL ? "install" : "update"); |
| + int posix_permissions = kWorldReadableFilePermission | |
| + base::FILE_PERMISSION_EXECUTE_BY_USER | |
| + base::FILE_PERMISSION_EXECUTE_BY_OTHERS; |
| + if (base::PathExists(output_dir)) |
| + base::DeleteFile(output_dir, true); |
| + |
| + // Creates the directory to download and sets permissions. |
| + if (!base::CreateDirectory(output_dir) || |
| + !base::SetPosixFilePermissions(webapk_dir, posix_permissions) || |
| + !base::SetPosixFilePermissions(output_dir, posix_permissions)) |
| + return base::FilePath(); |
| + |
| + return output_dir; |
| +} |
| + |
| } // anonymous namespace |
| WebApkInstaller::WebApkInstaller(const ShortcutInfo& shortcut_info, |
| @@ -348,12 +385,22 @@ void WebApkInstaller::SendRequest(std::unique_ptr<webapk::WebApk> request_proto, |
| void WebApkInstaller::OnGotWebApkDownloadUrl(const GURL& download_url, |
| const std::string& package_name) { |
| - base::FilePath output_dir; |
| - base::android::GetCacheDirectory(&output_dir); |
| webapk_package_ = package_name; |
| - // TODO(pkotwicz): Download WebAPKs into WebAPK-specific subdirectory |
| - // directory. |
| - // TODO(pkotwicz): Figure out when downloaded WebAPK should be deleted. |
| + |
| + base::PostTaskAndReplyWithResult( |
| + GetBackgroundTaskRunner().get(), FROM_HERE, |
| + base::Bind(&CreateSubDirAndSetPermissionsInBackground, |
| + task_type_, package_name), |
| + base::Bind(&WebApkInstaller::OnCreatedSubDirAndSetPermissions, |
| + weak_ptr_factory_.GetWeakPtr(), download_url)); |
| +} |
| + |
| +void WebApkInstaller::OnCreatedSubDirAndSetPermissions( |
| + const GURL& download_url, const base::FilePath& output_dir) { |
| + if (output_dir.empty()) { |
| + OnFailure(); |
| + return; |
| + } |
| timer_.Start( |
| FROM_HERE, base::TimeDelta::FromMilliseconds(download_timeout_ms_), |
| @@ -375,13 +422,12 @@ void WebApkInstaller::OnWebApkDownloaded(const base::FilePath& file_path, |
| return; |
| } |
| - int posix_permissions = base::FILE_PERMISSION_READ_BY_USER | |
| - base::FILE_PERMISSION_WRITE_BY_USER | |
| - base::FILE_PERMISSION_READ_BY_GROUP | |
| - base::FILE_PERMISSION_READ_BY_OTHERS; |
| + int posix_permissions = kWorldReadableFilePermission | |
| + base::FILE_PERMISSION_EXECUTE_BY_USER; |
| base::PostTaskAndReplyWithResult( |
| GetBackgroundTaskRunner().get(), FROM_HERE, |
| - base::Bind(&base::SetPosixFilePermissions, file_path, posix_permissions), |
| + base::Bind(&base::SetPosixFilePermissions, file_path, |
| + posix_permissions), |
| base::Bind(&WebApkInstaller::OnWebApkMadeWorldReadable, |
| weak_ptr_factory_.GetWeakPtr(), file_path)); |
| } |