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 5acb396200b16497b01c67f7013a7d2722cc105c..e33cc1e87addaffd30aa58265092ec0b59c7856f 100644 |
--- a/chrome/browser/android/webapk/webapk_installer.cc |
+++ b/chrome/browser/android/webapk/webapk_installer.cc |
@@ -89,6 +89,14 @@ WebApkInstaller::WebApkInstaller(content::BrowserContext* browser_context, |
: kDefaultWebApkServerUrl); |
} |
+WebApkInstaller::WebApkInstaller(content::BrowserContext* browser_context, |
+ const ShortcutInfo& shortcut_info, |
+ const SkBitmap& shortcut_icon, |
+ const std::string& webapk_package) |
+ : WebApkInstaller(browser_context, shortcut_info, shortcut_icon) { |
+ webapk_package_ = webapk_package; |
+} |
+ |
WebApkInstaller::~WebApkInstaller() {} |
// static |
@@ -101,11 +109,25 @@ void WebApkInstaller::InstallAsync(const FinishCallback& finish_callback) { |
finish_callback_ = finish_callback; |
// base::Unretained() is safe because WebApkInstaller owns itself and does not |
// start the timeout timer till after |
- // InitializeRequestContextGetterOnUIThread() is called. |
+ // InitializeCreateRequestContextGetterOnUIThread() is called. |
pkotwicz
2016/08/03 01:07:06
I think you can use BrowserThread::PostTaskAndRepl
Xi Han
2016/08/03 17:30:05
Good idea, thanks!
|
content::BrowserThread::PostTask( |
content::BrowserThread::UI, FROM_HERE, |
- base::Bind(&WebApkInstaller::InitializeRequestContextGetterOnUIThread, |
- base::Unretained(this))); |
+ base::Bind( |
+ &WebApkInstaller::InitializeCreateRequestContextGetterOnUIThread, |
+ base::Unretained(this))); |
+} |
+ |
+void WebApkInstaller::UpdateAsync(const FinishCallback& finish_callback) { |
+ DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
+ finish_callback_ = finish_callback; |
+ // base::Unretained() is safe because WebApkInstaller owns itself and does not |
+ // start the timeout timer till after |
+ // InitializeUpdateRequestContextGetterOnUIThread() is called. |
+ content::BrowserThread::PostTask( |
+ content::BrowserThread::UI, FROM_HERE, |
+ base::Bind( |
+ &WebApkInstaller::InitializeUpdateRequestContextGetterOnUIThread, |
+ base::Unretained(this))); |
} |
void WebApkInstaller::OnURLFetchComplete(const net::URLFetcher* source) { |
@@ -121,23 +143,23 @@ void WebApkInstaller::OnURLFetchComplete(const net::URLFetcher* source) { |
std::string response_string; |
source->GetResponseAsString(&response_string); |
- std::unique_ptr<webapk::CreateWebApkResponse> response( |
- new webapk::CreateWebApkResponse); |
+ std::unique_ptr<webapk::WebApkResponse> response( |
+ new webapk::WebApkResponse); |
if (!response->ParseFromString(response_string)) { |
OnFailure(); |
return; |
} |
- if (response->signed_download_url().empty() || |
+ if (response->signed_market_url().empty() || |
response->webapk_package_name().empty()) { |
OnFailure(); |
return; |
} |
- OnGotWebApkDownloadUrl(response->signed_download_url(), |
+ OnGotWebApkDownloadUrl(response->signed_market_url(), |
response->webapk_package_name()); |
} |
-void WebApkInstaller::InitializeRequestContextGetterOnUIThread() { |
+void WebApkInstaller::InitializeCreateRequestContextGetterOnUIThread() { |
DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
// Must be called on UI thread. |
request_context_getter_ = |
@@ -149,11 +171,37 @@ void WebApkInstaller::InitializeRequestContextGetterOnUIThread() { |
io_weak_ptr_factory_.GetWeakPtr())); |
} |
+void WebApkInstaller::InitializeUpdateRequestContextGetterOnUIThread() { |
+ DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
+ // Must be called on UI thread. |
+ request_context_getter_ = |
+ Profile::FromBrowserContext(browser_context_)->GetRequestContext(); |
+ |
+ content::BrowserThread::PostTask( |
+ content::BrowserThread::IO, FROM_HERE, |
+ base::Bind(&WebApkInstaller::SendUpdateWebApkRequest, |
+ base::Unretained(this))); |
+} |
+ |
void WebApkInstaller::SendCreateWebApkRequest() { |
DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
- std::unique_ptr<webapk::CreateWebApkRequest> request = |
- BuildCreateWebApkRequest(); |
+ SendRequest(BuildRequest(std::unique_ptr<webapk::CreateWebApkRequest>( |
+ new webapk::CreateWebApkRequest))); |
+} |
+ |
+void WebApkInstaller::SendUpdateWebApkRequest() { |
+ DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
+ std::unique_ptr<webapk::UpdateWebApkRequest> request |
+ = BuildRequest(std::unique_ptr<webapk::UpdateWebApkRequest>( |
+ new webapk::UpdateWebApkRequest)); |
+ webapk::WebApk* webapk = request->mutable_webapk(); |
+ webapk->set_package_name(webapk_package_); |
+ SendRequest(std::move(request)); |
+} |
+ |
+template <class RequestProto> |
+void WebApkInstaller::SendRequest(std::unique_ptr<RequestProto> request_proto) { |
pkotwicz
2016/08/03 01:07:06
Nit: You can change the type to std::unique_ptr<::
Xi Han
2016/08/03 17:30:05
It is great to have the super class of the proto,
|
timer_.Start(FROM_HERE, |
base::TimeDelta::FromMilliseconds(kWebApkDownloadUrlTimeoutMs), |
base::Bind(&WebApkInstaller::OnTimeout, |
@@ -163,7 +211,7 @@ void WebApkInstaller::SendCreateWebApkRequest() { |
net::URLFetcher::Create(server_url_, net::URLFetcher::POST, this); |
url_fetcher_->SetRequestContext(request_context_getter_); |
std::string serialized_request; |
- request->SerializeToString(&serialized_request); |
+ request_proto->SerializeToString(&serialized_request); |
url_fetcher_->SetUploadData(kProtoMimeType, serialized_request); |
url_fetcher_->Start(); |
} |
@@ -215,11 +263,9 @@ void WebApkInstaller::OnWebApkDownloaded(const base::FilePath& file_path, |
OnFailure(); |
} |
pkotwicz
2016/08/03 01:07:06
I think that templates are generally discouraged i
Xi Han
2016/08/03 17:30:05
Ok, I refereed some examples of the template in Ch
|
-std::unique_ptr<webapk::CreateWebApkRequest> |
-WebApkInstaller::BuildCreateWebApkRequest() { |
- std::unique_ptr<webapk::CreateWebApkRequest> request( |
- new webapk::CreateWebApkRequest); |
- |
+template<class RequestProto> |
+std::unique_ptr<RequestProto> WebApkInstaller::BuildRequest( |
+ std::unique_ptr<RequestProto> request) { |
webapk::WebApk* webapk = request->mutable_webapk(); |
webapk->set_manifest_url(shortcut_info_.manifest_url.spec()); |
webapk->set_requester_application_package( |
@@ -260,11 +306,11 @@ void WebApkInstaller::OnTimeout() { |
} |
void WebApkInstaller::OnSuccess() { |
- finish_callback_.Run(true); |
+ finish_callback_.Run(true, webapk_package_); |
delete this; |
} |
void WebApkInstaller::OnFailure() { |
- finish_callback_.Run(false); |
+ finish_callback_.Run(false, webapk_package_); |
delete this; |
} |