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 6e30ff6d51ed30b29ff9ce7232817333fb2da8a5..439c7e0b922c797942725a26da10b959a3b964fe 100644 |
--- a/chrome/browser/android/webapk/webapk_installer.cc |
+++ b/chrome/browser/android/webapk/webapk_installer.cc |
@@ -28,7 +28,6 @@ |
#include "content/public/common/manifest_util.h" |
#include "jni/WebApkInstaller_jni.h" |
#include "net/http/http_status_code.h" |
-#include "net/url_request/url_fetcher.h" |
#include "third_party/smhasher/src/MurmurHash2.h" |
#include "ui/gfx/codec/png_codec.h" |
#include "url/gurl.h" |
@@ -37,7 +36,10 @@ namespace { |
// The default WebAPK server URL. |
const char kDefaultWebApkServerUrl[] = |
- "https://webapk.googleapis.com/v1alpha/webApks?alt=proto"; |
+ "https://webapk.googleapis.com/v1alpha/webApks/"; |
+ |
+// The response format type expected from the WebAPK server. |
+const char kDefaultWebApkServerUrlResponseType[] = "?alt=proto"; |
// The MIME type of the POST data sent to the server. |
const char kProtoMimeType[] = "application/x-protobuf"; |
@@ -84,6 +86,12 @@ std::string ColorToString(int64_t color) { |
return base::StringPrintf("rgba(%d,%d,%d,%.2f)", r, g, b, a); |
} |
+GURL GetServerUrlForUpdate(const GURL& server_url, |
+ const std::string& webapk_package) { |
+ return GURL(server_url.spec() + webapk_package + "/" + |
Yaron
2016/08/12 21:16:17
i see what you're saying now about the url. I thin
Xi Han
2016/08/15 21:38:45
Yes, I added the bug number back.
|
+ kDefaultWebApkServerUrlResponseType); |
+} |
+ |
} // anonymous namespace |
WebApkInstaller::WebApkInstaller(const ShortcutInfo& shortcut_info, |
@@ -92,6 +100,7 @@ WebApkInstaller::WebApkInstaller(const ShortcutInfo& shortcut_info, |
shortcut_icon_(shortcut_icon), |
webapk_download_url_timeout_ms_(kWebApkDownloadUrlTimeoutMs), |
download_timeout_ms_(kDownloadTimeoutMs), |
+ task_type_(UNDEFINED), |
weak_ptr_factory_(this) { |
base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); |
server_url_ = |
@@ -114,6 +123,7 @@ void WebApkInstaller::InstallAsyncWithURLRequestContextGetter( |
const FinishCallback& finish_callback) { |
request_context_getter_ = request_context_getter; |
finish_callback_ = finish_callback; |
+ task_type_ = INSTALL; |
// base::Unretained() is safe because WebApkInstaller owns itself and does not |
// start the timeout timer till after SendCreateWebApkRequest() is called. |
@@ -134,7 +144,41 @@ void WebApkInstaller::SetTimeoutMs(int timeout_ms) { |
download_timeout_ms_ = timeout_ms; |
} |
-bool WebApkInstaller::StartDownloadedWebApkInstall( |
+void WebApkInstaller::UpdateAsync(content::BrowserContext* browser_context, |
+ const FinishCallback& finish_callback, |
+ const std::string& webapk_package, |
+ int webapk_version) { |
+ UpdateAsyncWithURLRequestContextGetter( |
+ Profile::FromBrowserContext(browser_context)->GetRequestContext(), |
+ finish_callback, webapk_package, webapk_version); |
+} |
+ |
+void WebApkInstaller::UpdateAsyncWithURLRequestContextGetter( |
+ net::URLRequestContextGetter* request_context_getter, |
+ const FinishCallback& finish_callback, |
+ const std::string& webapk_package, |
+ int webapk_version) { |
+ request_context_getter_ = request_context_getter; |
+ finish_callback_ = finish_callback; |
+ webapk_package_ = webapk_package; |
+ webapk_version_ = webapk_version; |
+ task_type_ = UPDATE; |
+ |
pkotwicz
2016/08/11 22:02:44
Nit: You can steal GetBackgroundTaskRunner() from
Xi Han
2016/08/15 21:38:45
Done.
|
+ // base::Unretained() is safe because WebApkInstaller owns itself and does not |
+ // start the timeout timer till after SendUpdateWebApkRequest() is called. |
+ scoped_refptr<base::TaskRunner> background_task_runner = |
+ content::BrowserThread::GetBlockingPool() |
+ ->GetTaskRunnerWithShutdownBehavior( |
+ base::SequencedWorkerPool::SKIP_ON_SHUTDOWN); |
+ base::PostTaskAndReplyWithResult( |
+ background_task_runner.get(), FROM_HERE, |
+ base::Bind(&WebApkInstaller::BuildWebApkProtoInBackground, |
+ base::Unretained(this)), |
+ base::Bind(&WebApkInstaller::SendUpdateWebApkRequest, |
+ base::Unretained(this))); |
+} |
+ |
+bool WebApkInstaller::StartInstallingDownloadedWebApk( |
JNIEnv* env, |
const base::android::ScopedJavaLocalRef<jstring>& java_file_path, |
const base::android::ScopedJavaLocalRef<jstring>& java_package_name) { |
@@ -142,6 +186,14 @@ bool WebApkInstaller::StartDownloadedWebApkInstall( |
java_package_name.obj()); |
} |
+bool WebApkInstaller::StartUpdateUsingDownloadedWebApk( |
+ JNIEnv* env, |
+ const base::android::ScopedJavaLocalRef<jstring>& java_file_path, |
+ const base::android::ScopedJavaLocalRef<jstring>& java_package_name) { |
+ return Java_WebApkInstaller_updateAsyncFromNative(env, java_file_path.obj(), |
+ java_package_name.obj()); |
+} |
+ |
void WebApkInstaller::OnURLFetchComplete(const net::URLFetcher* source) { |
timer_.Stop(); |
@@ -170,17 +222,32 @@ void WebApkInstaller::OnURLFetchComplete(const net::URLFetcher* source) { |
} |
void WebApkInstaller::SendCreateWebApkRequest( |
- std::unique_ptr<webapk::WebApk> webapk_proto) { |
+ std::unique_ptr<webapk::WebApk> webapk) { |
+ GURL server_url(server_url_.spec() + kDefaultWebApkServerUrlResponseType); |
+ SendRequest(std::move(webapk), net::URLFetcher::POST, server_url); |
+} |
+ |
+void WebApkInstaller::SendUpdateWebApkRequest( |
+ std::unique_ptr<webapk::WebApk> webapk) { |
+ webapk->set_package_name(webapk_package_); |
+ webapk->set_version(std::to_string(webapk_version_)); |
+ |
+ SendRequest(std::move(webapk), net::URLFetcher::PUT, |
+ GetServerUrlForUpdate(server_url_, webapk_package_)); |
+} |
+ |
+void WebApkInstaller::SendRequest(std::unique_ptr<webapk::WebApk> request_proto, |
+ net::URLFetcher::RequestType request_type, |
+ const GURL& server_url) { |
timer_.Start( |
FROM_HERE, |
base::TimeDelta::FromMilliseconds(webapk_download_url_timeout_ms_), |
base::Bind(&WebApkInstaller::OnTimeout, weak_ptr_factory_.GetWeakPtr())); |
- url_fetcher_ = |
- net::URLFetcher::Create(server_url_, net::URLFetcher::POST, this); |
+ url_fetcher_ = net::URLFetcher::Create(server_url, request_type, this); |
url_fetcher_->SetRequestContext(request_context_getter_); |
std::string serialized_request; |
- webapk_proto->SerializeToString(&serialized_request); |
+ request_proto->SerializeToString(&serialized_request); |
url_fetcher_->SetUploadData(kProtoMimeType, serialized_request); |
url_fetcher_->Start(); |
} |
@@ -219,8 +286,14 @@ void WebApkInstaller::OnWebApkDownloaded(const base::FilePath& file_path, |
base::android::ConvertUTF8ToJavaString(env, file_path.value()); |
base::android::ScopedJavaLocalRef<jstring> java_package_name = |
base::android::ConvertUTF8ToJavaString(env, package_name); |
- bool success = |
- StartDownloadedWebApkInstall(env, java_file_path, java_package_name); |
+ bool success = false; |
+ if (task_type_ == INSTALL) { |
+ success = |
+ StartInstallingDownloadedWebApk(env, java_file_path, java_package_name); |
+ } else if (task_type_ == UPDATE) { |
+ success = StartUpdateUsingDownloadedWebApk(env, java_file_path, |
+ java_package_name); |
+ } |
if (success) |
OnSuccess(); |
else |