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" |
11 #include "base/bind.h" | 11 #include "base/bind.h" |
12 #include "base/command_line.h" | 12 #include "base/command_line.h" |
13 #include "base/files/file_path.h" | 13 #include "base/files/file_path.h" |
14 #include "base/files/file_util.h" | 14 #include "base/files/file_util.h" |
15 #include "base/strings/string_number_conversions.h" | 15 #include "base/strings/string_number_conversions.h" |
16 #include "base/strings/string_util.h" | 16 #include "base/strings/string_util.h" |
17 #include "base/strings/stringprintf.h" | 17 #include "base/strings/stringprintf.h" |
18 #include "base/strings/utf_string_conversions.h" | 18 #include "base/strings/utf_string_conversions.h" |
19 #include "chrome/browser/android/shortcut_helper.h" | 19 #include "chrome/browser/android/shortcut_helper.h" |
20 #include "chrome/browser/android/webapk/webapk.pb.h" | 20 #include "chrome/browser/android/webapk/webapk.pb.h" |
21 #include "chrome/browser/profiles/profile.h" | 21 #include "chrome/browser/profiles/profile.h" |
22 #include "chrome/common/chrome_switches.h" | 22 #include "chrome/common/chrome_switches.h" |
23 #include "components/version_info/version_info.h" | 23 #include "components/version_info/version_info.h" |
24 #include "content/public/browser/browser_thread.h" | 24 #include "content/public/browser/browser_thread.h" |
25 #include "jni/WebApkInstaller_jni.h" | 25 #include "jni/WebApkInstaller_jni.h" |
26 #include "net/http/http_status_code.h" | 26 #include "net/http/http_status_code.h" |
27 #include "net/url_request/url_fetcher.h" | |
28 #include "third_party/smhasher/src/MurmurHash2.h" | 27 #include "third_party/smhasher/src/MurmurHash2.h" |
29 #include "ui/gfx/codec/png_codec.h" | 28 #include "ui/gfx/codec/png_codec.h" |
30 #include "url/gurl.h" | 29 #include "url/gurl.h" |
31 | 30 |
32 namespace { | 31 namespace { |
33 | 32 |
34 // The default WebAPK server URL. | 33 // The default WebAPK server URL. |
35 const char kDefaultWebApkServerUrl[] = | 34 const char kDefaultWebApkServerUrl[] = |
36 "https://webapk.googleapis.com/v1alpha/webApks?alt=proto"; | 35 "https://webapk.googleapis.com/v1alpha/webApks/"; |
36 | |
37 // The response format type expected from WebAPK server. | |
pkotwicz
2016/08/09 14:35:42
Nit: "from WebAPK server" -> "from the WebAPK serv
Xi Han
2016/08/11 19:01:15
Done.
| |
38 const char kDefaultWebApkServerUrlResponseType[] = "?alt=proto"; | |
37 | 39 |
38 // The MIME type of the POST data sent to the server. | 40 // The MIME type of the POST data sent to the server. |
39 const char kProtoMimeType[] = "application/x-protobuf"; | 41 const char kProtoMimeType[] = "application/x-protobuf"; |
40 | 42 |
41 // The seed to use the murmur2 hash of the app icon. | 43 // The seed to use the murmur2 hash of the app icon. |
42 const uint32_t kMurmur2HashSeed = 0; | 44 const uint32_t kMurmur2HashSeed = 0; |
43 | 45 |
44 // The default number of milliseconds to wait for the WebAPK download URL from | 46 // The default number of milliseconds to wait for the WebAPK download URL from |
45 // the WebAPK server. | 47 // the WebAPK server. |
46 const int kWebApkDownloadUrlTimeoutMs = 60000; | 48 const int kWebApkDownloadUrlTimeoutMs = 60000; |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
81 } | 83 } |
82 | 84 |
83 } // anonymous namespace | 85 } // anonymous namespace |
84 | 86 |
85 WebApkInstaller::WebApkInstaller(const ShortcutInfo& shortcut_info, | 87 WebApkInstaller::WebApkInstaller(const ShortcutInfo& shortcut_info, |
86 const SkBitmap& shortcut_icon) | 88 const SkBitmap& shortcut_icon) |
87 : shortcut_info_(shortcut_info), | 89 : shortcut_info_(shortcut_info), |
88 shortcut_icon_(shortcut_icon), | 90 shortcut_icon_(shortcut_icon), |
89 webapk_download_url_timeout_ms_(kWebApkDownloadUrlTimeoutMs), | 91 webapk_download_url_timeout_ms_(kWebApkDownloadUrlTimeoutMs), |
90 download_timeout_ms_(kDownloadTimeoutMs), | 92 download_timeout_ms_(kDownloadTimeoutMs), |
93 task_type_(UNDEFINED), | |
91 io_weak_ptr_factory_(this) { | 94 io_weak_ptr_factory_(this) { |
92 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); | 95 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); |
93 server_url_ = | 96 server_url_ = |
94 GURL(command_line->HasSwitch(switches::kWebApkServerUrl) | 97 GURL(command_line->HasSwitch(switches::kWebApkServerUrl) |
95 ? command_line->GetSwitchValueASCII(switches::kWebApkServerUrl) | 98 ? command_line->GetSwitchValueASCII(switches::kWebApkServerUrl) |
96 : kDefaultWebApkServerUrl); | 99 : kDefaultWebApkServerUrl); |
97 } | 100 } |
98 | 101 |
99 WebApkInstaller::~WebApkInstaller() {} | 102 WebApkInstaller::~WebApkInstaller() {} |
100 | 103 |
101 void WebApkInstaller::InstallAsync(content::BrowserContext* browser_context, | 104 void WebApkInstaller::InstallAsync(content::BrowserContext* browser_context, |
102 const FinishCallback& finish_callback) { | 105 const FinishCallback& finish_callback) { |
103 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 106 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
104 finish_callback_ = finish_callback; | 107 finish_callback_ = finish_callback; |
108 task_type_ = INSTALL; | |
105 // base::Unretained() is safe because WebApkInstaller owns itself and does not | 109 // base::Unretained() is safe because WebApkInstaller owns itself and does not |
106 // start the timeout timer till after | 110 // start the timeout timer till after |
107 // InitializeRequestContextGetterOnUIThread() is called. | 111 // InitializeRequestContextGetterOnUIThread() is called. |
108 content::BrowserThread::PostTask( | 112 content::BrowserThread::PostTaskAndReply( |
109 content::BrowserThread::UI, FROM_HERE, | 113 content::BrowserThread::UI, FROM_HERE, |
110 base::Bind(&WebApkInstaller::InitializeRequestContextGetterOnUIThread, | 114 base::Bind(&WebApkInstaller::InitializeRequestContextGetterOnUIThread, |
111 base::Unretained(this), browser_context)); | 115 base::Unretained(this), browser_context), |
116 base::Bind(&WebApkInstaller::SendCreateWebApkRequest, | |
117 io_weak_ptr_factory_.GetWeakPtr())); | |
112 } | 118 } |
113 | 119 |
114 void WebApkInstaller::InstallAsyncWithURLRequestContextGetter( | 120 void WebApkInstaller::InstallAsyncWithURLRequestContextGetter( |
115 net::URLRequestContextGetter* request_context_getter, | 121 net::URLRequestContextGetter* request_context_getter, |
116 const FinishCallback& finish_callback) { | 122 const FinishCallback& finish_callback) { |
117 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 123 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
118 request_context_getter_ = request_context_getter; | 124 request_context_getter_ = request_context_getter; |
119 finish_callback_ = finish_callback; | 125 finish_callback_ = finish_callback; |
126 task_type_ = INSTALL; | |
120 | 127 |
121 SendCreateWebApkRequest(); | 128 SendCreateWebApkRequest(); |
122 } | 129 } |
123 | 130 |
124 void WebApkInstaller::SetTimeoutMs(int timeout_ms) { | 131 void WebApkInstaller::SetTimeoutMs(int timeout_ms) { |
125 webapk_download_url_timeout_ms_ = timeout_ms; | 132 webapk_download_url_timeout_ms_ = timeout_ms; |
126 download_timeout_ms_ = timeout_ms; | 133 download_timeout_ms_ = timeout_ms; |
127 } | 134 } |
128 | 135 |
136 void WebApkInstaller::UpdateAsync(content::BrowserContext* browser_context, | |
137 const FinishCallback& finish_callback, | |
138 const std::string& webapk_package, | |
139 int version) { | |
pkotwicz
2016/08/09 14:35:42
Nit: |version| -> |webapk_version|
Xi Han
2016/08/11 19:01:15
Done.
| |
140 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | |
141 finish_callback_ = finish_callback; | |
142 webapk_package_ = webapk_package; | |
143 webapk_version_ = version; | |
144 task_type_ = UPDATE; | |
145 // base::Unretained() is safe because WebApkInstaller owns itself and does not | |
146 // start the timeout timer till after | |
147 // InitializeRequestContextGetterOnUIThread() is called. | |
148 content::BrowserThread::PostTaskAndReply( | |
149 content::BrowserThread::UI, FROM_HERE, | |
150 base::Bind(&WebApkInstaller::InitializeRequestContextGetterOnUIThread, | |
151 base::Unretained(this), browser_context), | |
152 base::Bind(&WebApkInstaller::SendUpdateWebApkRequest, | |
153 io_weak_ptr_factory_.GetWeakPtr())); | |
154 } | |
155 | |
129 bool WebApkInstaller::StartDownloadedWebApkInstall( | 156 bool WebApkInstaller::StartDownloadedWebApkInstall( |
130 JNIEnv* env, | 157 JNIEnv* env, |
131 const base::android::ScopedJavaLocalRef<jstring>& java_file_path, | 158 const base::android::ScopedJavaLocalRef<jstring>& java_file_path, |
132 const base::android::ScopedJavaLocalRef<jstring>& java_package_name) { | 159 const base::android::ScopedJavaLocalRef<jstring>& java_package_name) { |
pkotwicz
2016/08/09 14:35:42
Can you move this logic to OnWebApkDownloaded()? T
Xi Han
2016/08/11 19:01:14
Done.
| |
133 return Java_WebApkInstaller_installAsyncFromNative(env, java_file_path.obj(), | 160 if (task_type_ == INSTALL) { |
134 java_package_name.obj()); | 161 return Java_WebApkInstaller_installAsyncFromNative( |
162 env, java_file_path.obj(), java_package_name.obj()); | |
163 } else if (task_type_ == UPDATE) { | |
164 return Java_WebApkInstaller_updateAsyncFromNative( | |
165 env, java_file_path.obj(), java_package_name.obj()); | |
166 } else { | |
167 return false; | |
168 } | |
135 } | 169 } |
136 | 170 |
137 void WebApkInstaller::OnURLFetchComplete(const net::URLFetcher* source) { | 171 void WebApkInstaller::OnURLFetchComplete(const net::URLFetcher* source) { |
138 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 172 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
139 timer_.Stop(); | 173 timer_.Stop(); |
140 | 174 |
141 if (!source->GetStatus().is_success() || | 175 if (!source->GetStatus().is_success() || |
142 source->GetResponseCode() != net::HTTP_OK) { | 176 source->GetResponseCode() != net::HTTP_OK) { |
143 OnFailure(); | 177 OnFailure(); |
144 return; | 178 return; |
(...skipping 16 matching lines...) Expand all Loading... | |
161 } | 195 } |
162 OnGotWebApkDownloadUrl(signed_download_url, response->package_name()); | 196 OnGotWebApkDownloadUrl(signed_download_url, response->package_name()); |
163 } | 197 } |
164 | 198 |
165 void WebApkInstaller::InitializeRequestContextGetterOnUIThread( | 199 void WebApkInstaller::InitializeRequestContextGetterOnUIThread( |
166 content::BrowserContext* browser_context) { | 200 content::BrowserContext* browser_context) { |
167 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 201 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
168 // Profile::GetRequestContext() must be called on UI thread. | 202 // Profile::GetRequestContext() must be called on UI thread. |
169 request_context_getter_ = | 203 request_context_getter_ = |
170 Profile::FromBrowserContext(browser_context)->GetRequestContext(); | 204 Profile::FromBrowserContext(browser_context)->GetRequestContext(); |
171 | |
172 content::BrowserThread::PostTask( | |
173 content::BrowserThread::IO, FROM_HERE, | |
174 base::Bind(&WebApkInstaller::SendCreateWebApkRequest, | |
175 io_weak_ptr_factory_.GetWeakPtr())); | |
176 } | 205 } |
177 | 206 |
178 void WebApkInstaller::SendCreateWebApkRequest() { | 207 void WebApkInstaller::SendCreateWebApkRequest() { |
179 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 208 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
180 std::unique_ptr<webapk::WebApk> webapk_proto = BuildWebApkProto(); | |
181 | 209 |
210 std::unique_ptr<webapk::WebApk> webapk = BuildWebApkProto(); | |
pkotwicz
2016/08/09 14:35:42
Nit: Remove the comment. It just reiterates what i
Xi Han
2016/08/11 19:01:15
Done.
| |
211 // The WebAPK server URL to download and install a WebAPK is: | |
212 // |server_url_| + |kDeaultWebApkServerUrlResponseType|. | |
213 GURL server_url(server_url_.spec() + kDefaultWebApkServerUrlResponseType); | |
214 SendRequest(std::move(webapk), net::URLFetcher::POST, server_url); | |
215 } | |
216 | |
217 void WebApkInstaller::SendUpdateWebApkRequest() { | |
218 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | |
219 | |
220 std::unique_ptr<webapk::WebApk> webapk = BuildWebApkProto(); | |
221 webapk->set_package_name(webapk_package_); | |
222 webapk->set_version(std::to_string(webapk_version_)); | |
223 // The WebAPK server URL to download and update a WebAPK is: | |
224 // |server_url_| + |WebAPK package name| + "/" | |
225 // + |kDeaultWebApkServerUrlResponseType| | |
pkotwicz
2016/08/09 14:35:42
Nit: The comment is not useful. It just reiterates
Xi Han
2016/08/11 19:01:15
I think it is part of the package name in the comm
| |
226 GURL server_url(server_url_.spec() + webapk_package_ + "/" | |
227 + kDefaultWebApkServerUrlResponseType); | |
228 SendRequest(std::move(webapk), net::URLFetcher::PUT, server_url); | |
229 } | |
230 | |
231 void WebApkInstaller::SendRequest( | |
232 std::unique_ptr<webapk::WebApk> request_proto, | |
233 net::URLFetcher::RequestType request_type, | |
234 const GURL& server_url) { | |
182 timer_.Start(FROM_HERE, base::TimeDelta::FromMilliseconds( | 235 timer_.Start(FROM_HERE, base::TimeDelta::FromMilliseconds( |
183 webapk_download_url_timeout_ms_), | 236 webapk_download_url_timeout_ms_), |
184 base::Bind(&WebApkInstaller::OnTimeout, | 237 base::Bind(&WebApkInstaller::OnTimeout, |
185 io_weak_ptr_factory_.GetWeakPtr())); | 238 io_weak_ptr_factory_.GetWeakPtr())); |
186 | 239 |
187 url_fetcher_ = | 240 url_fetcher_ = |
188 net::URLFetcher::Create(server_url_, net::URLFetcher::POST, this); | 241 net::URLFetcher::Create(server_url, request_type, this); |
189 url_fetcher_->SetRequestContext(request_context_getter_); | 242 url_fetcher_->SetRequestContext(request_context_getter_); |
190 std::string serialized_request; | 243 std::string serialized_request; |
191 webapk_proto->SerializeToString(&serialized_request); | 244 request_proto->SerializeToString(&serialized_request); |
192 url_fetcher_->SetUploadData(kProtoMimeType, serialized_request); | 245 url_fetcher_->SetUploadData(kProtoMimeType, serialized_request); |
193 url_fetcher_->Start(); | 246 url_fetcher_->Start(); |
194 } | 247 } |
195 | 248 |
196 void WebApkInstaller::OnGotWebApkDownloadUrl(const GURL& download_url, | 249 void WebApkInstaller::OnGotWebApkDownloadUrl(const GURL& download_url, |
197 const std::string& package_name) { | 250 const std::string& package_name) { |
198 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 251 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
199 | 252 |
200 base::FilePath output_dir; | 253 base::FilePath output_dir; |
201 base::android::GetCacheDirectory(&output_dir); | 254 base::android::GetCacheDirectory(&output_dir); |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
279 | 332 |
280 void WebApkInstaller::OnSuccess() { | 333 void WebApkInstaller::OnSuccess() { |
281 finish_callback_.Run(true); | 334 finish_callback_.Run(true); |
282 delete this; | 335 delete this; |
283 } | 336 } |
284 | 337 |
285 void WebApkInstaller::OnFailure() { | 338 void WebApkInstaller::OnFailure() { |
286 finish_callback_.Run(false); | 339 finish_callback_.Run(false); |
287 delete this; | 340 delete this; |
288 } | 341 } |
OLD | NEW |