Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(241)

Side by Side Diff: chrome/browser/android/webapk/webapk_installer.cc

Issue 2364133002: Download WebAPK into a WebAPK-specifc directory, and delete all cached ones. (Closed)
Patch Set: pkotwicz@'s comments. Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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 |
pkotwicz 2016/09/26 21:04:25 Nit: rename |KBasicPosixPermissions| to |kWorldRea
Xi Han 2016/09/26 22:22:17 Done.
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_READ_BY_OTHERS;
60
55 // Returns the scope from |info| if it is specified. Otherwise, returns the 61 // Returns the scope from |info| if it is specified. Otherwise, returns the
56 // default scope. 62 // default scope.
57 GURL GetScope(const ShortcutInfo& info) { 63 GURL GetScope(const ShortcutInfo& info) {
58 return (info.scope.is_valid()) 64 return (info.scope.is_valid())
59 ? info.scope 65 ? info.scope
60 : ShortcutHelper::GetScopeFromURL(info.url); 66 : ShortcutHelper::GetScopeFromURL(info.url);
61 } 67 }
62 68
63 // Converts a color from the format specified in content::Manifest to a CSS 69 // Converts a color from the format specified in content::Manifest to a CSS
64 // string. 70 // string.
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
121 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN); 127 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN);
122 } 128 }
123 129
124 GURL GetServerUrlForUpdate(const GURL& server_url, 130 GURL GetServerUrlForUpdate(const GURL& server_url,
125 const std::string& webapk_package) { 131 const std::string& webapk_package) {
126 // crbug.com/636552. Simplify the server URL. 132 // crbug.com/636552. Simplify the server URL.
127 return GURL(server_url.spec() + webapk_package + "/" + 133 return GURL(server_url.spec() + webapk_package + "/" +
128 kDefaultWebApkServerUrlResponseType); 134 kDefaultWebApkServerUrlResponseType);
129 } 135 }
130 136
137 // Creates a directory depending on the type of the task, and set permissions.
138 // It also creates any parent directory along the path if doesn't exist,
139 // and sets permissions as well.
140 // The previously downloaded APKs are deleted before downloading a new one,
141 // which helps to clean up cached data. Creating different downloaded
pkotwicz 2016/09/26 21:04:25 How about: "Previously downloaded APKs are deleted
Xi Han 2016/09/26 22:22:17 Acknowledged.
142 // directory for install/update cases is to prevent deleting the APK which is
143 // still in use when an install and an update happen at the same time.
144 // However, it doesn't help cases of when mutiple installs (or multiple
145 // updates) happen at the same time.
146 base::FilePath CreateSubDirAndSetPermissionsInBackground(
147 const base::FilePath& output_root_dir,
148 WebApkInstaller::TaskType task_type,
149 const std::string& package_name) {
150 base::FilePath webapk_dir = output_root_dir.AppendASCII("webapks");
pkotwicz 2016/09/26 21:04:24 Move the part of the comment which starts with "Cr
Xi Han 2016/09/26 22:22:17 Done.
151 base::FilePath output_dir = webapk_dir.AppendASCII(
152 task_type == WebApkInstaller::INSTALL ? "install" : "update");
153 int posix_permissions = KBasicPosixPermissions |
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();
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
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; 387 base::FilePath output_dir;
pkotwicz 2016/09/26 21:04:24 Nit: Move GetCacheDirectory() to CreateSubDirAndSe
Xi Han 2016/09/26 22:22:17 Done.
352 base::android::GetCacheDirectory(&output_dir); 388 base::android::GetCacheDirectory(&output_dir);
353 webapk_package_ = package_name; 389 webapk_package_ = package_name;
354 // TODO(pkotwicz): Download WebAPKs into WebAPK-specific subdirectory 390
355 // directory. 391 base::PostTaskAndReplyWithResult(
356 // TODO(pkotwicz): Figure out when downloaded WebAPK should be deleted. 392 GetBackgroundTaskRunner().get(), FROM_HERE,
393 base::Bind(&CreateSubDirAndSetPermissionsInBackground,
394 output_dir, task_type_, package_name),
395 base::Bind(&WebApkInstaller::OnCreateSubDirAndSetPermissions,
396 weak_ptr_factory_.GetWeakPtr(), download_url));
397 }
398
399 void WebApkInstaller::OnCreateSubDirAndSetPermissions(
400 const GURL& download_url, const base::FilePath& output_dir) {
pkotwicz 2016/09/26 21:04:25 Nit: OnCreateSubDirAndSetPermissions() -> OnCreate
Xi Han 2016/09/26 22:22:17 Done.
401 if (output_dir.empty()) {
402 OnFailure();
403 return;
404 }
357 405
358 timer_.Start( 406 timer_.Start(
359 FROM_HERE, base::TimeDelta::FromMilliseconds(download_timeout_ms_), 407 FROM_HERE, base::TimeDelta::FromMilliseconds(download_timeout_ms_),
360 base::Bind(&WebApkInstaller::OnTimeout, weak_ptr_factory_.GetWeakPtr())); 408 base::Bind(&WebApkInstaller::OnTimeout, weak_ptr_factory_.GetWeakPtr()));
361 409
362 base::FilePath output_path = output_dir.AppendASCII(webapk_package_); 410 base::FilePath output_path = output_dir.AppendASCII(webapk_package_);
363 downloader_.reset(new FileDownloader( 411 downloader_.reset(new FileDownloader(
364 download_url, output_path, true, request_context_getter_, 412 download_url, output_path, true, request_context_getter_,
365 base::Bind(&WebApkInstaller::OnWebApkDownloaded, 413 base::Bind(&WebApkInstaller::OnWebApkDownloaded,
366 weak_ptr_factory_.GetWeakPtr(), output_path))); 414 weak_ptr_factory_.GetWeakPtr(), output_path)));
367 } 415 }
368 416
369 void WebApkInstaller::OnWebApkDownloaded(const base::FilePath& file_path, 417 void WebApkInstaller::OnWebApkDownloaded(const base::FilePath& file_path,
370 FileDownloader::Result result) { 418 FileDownloader::Result result) {
371 timer_.Stop(); 419 timer_.Stop();
372 420
373 if (result != FileDownloader::DOWNLOADED) { 421 if (result != FileDownloader::DOWNLOADED) {
374 OnFailure(); 422 OnFailure();
375 return; 423 return;
376 } 424 }
377 425
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( 426 base::PostTaskAndReplyWithResult(
383 GetBackgroundTaskRunner().get(), FROM_HERE, 427 GetBackgroundTaskRunner().get(), FROM_HERE,
384 base::Bind(&base::SetPosixFilePermissions, file_path, posix_permissions), 428 base::Bind(&base::SetPosixFilePermissions, file_path,
429 KBasicPosixPermissions),
385 base::Bind(&WebApkInstaller::OnWebApkMadeWorldReadable, 430 base::Bind(&WebApkInstaller::OnWebApkMadeWorldReadable,
386 weak_ptr_factory_.GetWeakPtr(), file_path)); 431 weak_ptr_factory_.GetWeakPtr(), file_path));
387 } 432 }
388 433
389 void WebApkInstaller::OnWebApkMadeWorldReadable( 434 void WebApkInstaller::OnWebApkMadeWorldReadable(
390 const base::FilePath& file_path, 435 const base::FilePath& file_path,
391 bool change_permission_success) { 436 bool change_permission_success) {
392 if (!change_permission_success) { 437 if (!change_permission_success) {
393 OnFailure(); 438 OnFailure();
394 return; 439 return;
(...skipping 30 matching lines...) Expand all
425 std::string webapk_package = webapk_package_; 470 std::string webapk_package = webapk_package_;
426 delete this; 471 delete this;
427 callback.Run(true, webapk_package); 472 callback.Run(true, webapk_package);
428 } 473 }
429 474
430 void WebApkInstaller::OnFailure() { 475 void WebApkInstaller::OnFailure() {
431 FinishCallback callback = finish_callback_; 476 FinishCallback callback = finish_callback_;
432 delete this; 477 delete this;
433 callback.Run(false, ""); 478 callback.Run(false, "");
434 } 479 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698