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" |
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
136 shortcut_icon_(shortcut_icon), | 136 shortcut_icon_(shortcut_icon), |
137 webapk_download_url_timeout_ms_(kWebApkDownloadUrlTimeoutMs), | 137 webapk_download_url_timeout_ms_(kWebApkDownloadUrlTimeoutMs), |
138 download_timeout_ms_(kDownloadTimeoutMs), | 138 download_timeout_ms_(kDownloadTimeoutMs), |
139 task_type_(UNDEFINED), | 139 task_type_(UNDEFINED), |
140 weak_ptr_factory_(this) { | 140 weak_ptr_factory_(this) { |
141 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); | 141 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); |
142 server_url_ = | 142 server_url_ = |
143 GURL(command_line->HasSwitch(switches::kWebApkServerUrl) | 143 GURL(command_line->HasSwitch(switches::kWebApkServerUrl) |
144 ? command_line->GetSwitchValueASCII(switches::kWebApkServerUrl) | 144 ? command_line->GetSwitchValueASCII(switches::kWebApkServerUrl) |
145 : kDefaultWebApkServerUrl); | 145 : kDefaultWebApkServerUrl); |
146 CreateJavaRef(); | |
146 } | 147 } |
147 | 148 |
148 WebApkInstaller::~WebApkInstaller() {} | 149 void WebApkInstaller::CreateJavaRef() { |
150 JNIEnv* env = base::android::AttachCurrentThread(); | |
151 java_ref_.Reset(Java_WebApkInstaller_create( | |
152 env, reinterpret_cast<intptr_t>(this))); | |
153 } | |
154 | |
155 WebApkInstaller::~WebApkInstaller() { | |
pkotwicz
2016/08/25 22:23:15
Noob question: Is calling WebApkInstaller#destroy(
Xi Han
2016/08/26 17:04:18
That is a good point. The ScopedJavaGlobalRef dest
pkotwicz
2016/08/26 22:31:24
Thank you for looking into this. I learned somethi
| |
156 JNIEnv* env = base::android::AttachCurrentThread(); | |
157 Java_WebApkInstaller_destroy(env, java_ref_); | |
158 java_ref_.Reset(); | |
159 } | |
149 | 160 |
150 void WebApkInstaller::InstallAsync(content::BrowserContext* browser_context, | 161 void WebApkInstaller::InstallAsync(content::BrowserContext* browser_context, |
151 const FinishCallback& finish_callback) { | 162 const FinishCallback& finish_callback) { |
152 InstallAsyncWithURLRequestContextGetter( | 163 InstallAsyncWithURLRequestContextGetter( |
153 Profile::FromBrowserContext(browser_context)->GetRequestContext(), | 164 Profile::FromBrowserContext(browser_context)->GetRequestContext(), |
154 finish_callback); | 165 finish_callback); |
155 } | 166 } |
156 | 167 |
157 void WebApkInstaller::InstallAsyncWithURLRequestContextGetter( | 168 void WebApkInstaller::InstallAsyncWithURLRequestContextGetter( |
158 net::URLRequestContextGetter* request_context_getter, | 169 net::URLRequestContextGetter* request_context_getter, |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
205 base::Bind(&BuildWebApkProtoInBackground, shortcut_info_, | 216 base::Bind(&BuildWebApkProtoInBackground, shortcut_info_, |
206 shortcut_icon_, shortcut_icon_murmur2_hash_), | 217 shortcut_icon_, shortcut_icon_murmur2_hash_), |
207 base::Bind(&WebApkInstaller::SendUpdateWebApkRequest, | 218 base::Bind(&WebApkInstaller::SendUpdateWebApkRequest, |
208 weak_ptr_factory_.GetWeakPtr())); | 219 weak_ptr_factory_.GetWeakPtr())); |
209 } | 220 } |
210 | 221 |
211 bool WebApkInstaller::StartInstallingDownloadedWebApk( | 222 bool WebApkInstaller::StartInstallingDownloadedWebApk( |
212 JNIEnv* env, | 223 JNIEnv* env, |
213 const base::android::ScopedJavaLocalRef<jstring>& java_file_path, | 224 const base::android::ScopedJavaLocalRef<jstring>& java_file_path, |
214 const base::android::ScopedJavaLocalRef<jstring>& java_package_name) { | 225 const base::android::ScopedJavaLocalRef<jstring>& java_package_name) { |
215 return Java_WebApkInstaller_installAsyncFromNative(env, java_file_path, | 226 return Java_WebApkInstaller_installAsyncFromNative( |
216 java_package_name); | 227 env, java_ref_, java_file_path, java_package_name); |
217 } | 228 } |
218 | 229 |
219 bool WebApkInstaller::StartUpdateUsingDownloadedWebApk( | 230 bool WebApkInstaller::StartUpdateUsingDownloadedWebApk( |
220 JNIEnv* env, | 231 JNIEnv* env, |
221 const base::android::ScopedJavaLocalRef<jstring>& java_file_path, | 232 const base::android::ScopedJavaLocalRef<jstring>& java_file_path, |
222 const base::android::ScopedJavaLocalRef<jstring>& java_package_name) { | 233 const base::android::ScopedJavaLocalRef<jstring>& java_package_name) { |
223 return Java_WebApkInstaller_updateAsyncFromNative(env, java_file_path, | 234 return Java_WebApkInstaller_updateAsyncFromNative( |
224 java_package_name); | 235 env, java_ref_, java_file_path, java_package_name); |
225 } | 236 } |
226 | 237 |
227 void WebApkInstaller::OnURLFetchComplete(const net::URLFetcher* source) { | 238 void WebApkInstaller::OnURLFetchComplete(const net::URLFetcher* source) { |
228 timer_.Stop(); | 239 timer_.Stop(); |
229 | 240 |
230 if (!source->GetStatus().is_success() || | 241 if (!source->GetStatus().is_success() || |
231 source->GetResponseCode() != net::HTTP_OK) { | 242 source->GetResponseCode() != net::HTTP_OK) { |
232 OnFailure(); | 243 OnFailure(); |
233 return; | 244 return; |
234 } | 245 } |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
280 if (icon_murmur2_hash.empty()) { | 291 if (icon_murmur2_hash.empty()) { |
281 OnFailure(); | 292 OnFailure(); |
282 return; | 293 return; |
283 } | 294 } |
284 | 295 |
285 base::PostTaskAndReplyWithResult( | 296 base::PostTaskAndReplyWithResult( |
286 GetBackgroundTaskRunner().get(), FROM_HERE, | 297 GetBackgroundTaskRunner().get(), FROM_HERE, |
287 base::Bind(&BuildWebApkProtoInBackground, shortcut_info_, | 298 base::Bind(&BuildWebApkProtoInBackground, shortcut_info_, |
288 shortcut_icon_, shortcut_icon_murmur2_hash_), | 299 shortcut_icon_, shortcut_icon_murmur2_hash_), |
289 base::Bind(&WebApkInstaller::SendCreateWebApkRequest, | 300 base::Bind(&WebApkInstaller::SendCreateWebApkRequest, |
290 weak_ptr_factory_.GetWeakPtr())); | 301 weak_ptr_factory_.GetWeakPtr())); |
291 } | 302 } |
292 | 303 |
293 void WebApkInstaller::SendCreateWebApkRequest( | 304 void WebApkInstaller::SendCreateWebApkRequest( |
294 std::unique_ptr<webapk::WebApk> webapk) { | 305 std::unique_ptr<webapk::WebApk> webapk) { |
295 GURL server_url(server_url_.spec() + kDefaultWebApkServerUrlResponseType); | 306 GURL server_url(server_url_.spec() + kDefaultWebApkServerUrlResponseType); |
296 SendRequest(std::move(webapk), net::URLFetcher::POST, server_url); | 307 SendRequest(std::move(webapk), net::URLFetcher::POST, server_url); |
297 } | 308 } |
298 | 309 |
299 void WebApkInstaller::SendUpdateWebApkRequest( | 310 void WebApkInstaller::SendUpdateWebApkRequest( |
300 std::unique_ptr<webapk::WebApk> webapk) { | 311 std::unique_ptr<webapk::WebApk> webapk) { |
(...skipping 14 matching lines...) Expand all Loading... | |
315 | 326 |
316 url_fetcher_ = net::URLFetcher::Create(server_url, request_type, this); | 327 url_fetcher_ = net::URLFetcher::Create(server_url, request_type, this); |
317 url_fetcher_->SetRequestContext(request_context_getter_); | 328 url_fetcher_->SetRequestContext(request_context_getter_); |
318 std::string serialized_request; | 329 std::string serialized_request; |
319 request_proto->SerializeToString(&serialized_request); | 330 request_proto->SerializeToString(&serialized_request); |
320 url_fetcher_->SetUploadData(kProtoMimeType, serialized_request); | 331 url_fetcher_->SetUploadData(kProtoMimeType, serialized_request); |
321 url_fetcher_->Start(); | 332 url_fetcher_->Start(); |
322 } | 333 } |
323 | 334 |
324 void WebApkInstaller::OnGotWebApkDownloadUrl(const GURL& download_url, | 335 void WebApkInstaller::OnGotWebApkDownloadUrl(const GURL& download_url, |
325 const std::string& package_name) { | 336 const std::string& package_name) { |
pkotwicz
2016/08/25 22:23:15
Can you set |webapk_package_| here? This avoids pa
Xi Han
2016/08/26 17:04:17
Done.
| |
326 base::FilePath output_dir; | 337 base::FilePath output_dir; |
327 base::android::GetCacheDirectory(&output_dir); | 338 base::android::GetCacheDirectory(&output_dir); |
328 // TODO(pkotwicz): Download WebAPKs into WebAPK-specific subdirectory | 339 // TODO(pkotwicz): Download WebAPKs into WebAPK-specific subdirectory |
329 // directory. | 340 // directory. |
330 // TODO(pkotwicz): Figure out when downloaded WebAPK should be deleted. | 341 // TODO(pkotwicz): Figure out when downloaded WebAPK should be deleted. |
331 | 342 |
332 timer_.Start( | 343 timer_.Start( |
333 FROM_HERE, base::TimeDelta::FromMilliseconds(download_timeout_ms_), | 344 FROM_HERE, base::TimeDelta::FromMilliseconds(download_timeout_ms_), |
334 base::Bind(&WebApkInstaller::OnTimeout, weak_ptr_factory_.GetWeakPtr())); | 345 base::Bind(&WebApkInstaller::OnTimeout, weak_ptr_factory_.GetWeakPtr())); |
335 | 346 |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
370 return; | 381 return; |
371 } | 382 } |
372 | 383 |
373 JNIEnv* env = base::android::AttachCurrentThread(); | 384 JNIEnv* env = base::android::AttachCurrentThread(); |
374 base::android::ScopedJavaLocalRef<jstring> java_file_path = | 385 base::android::ScopedJavaLocalRef<jstring> java_file_path = |
375 base::android::ConvertUTF8ToJavaString(env, file_path.value()); | 386 base::android::ConvertUTF8ToJavaString(env, file_path.value()); |
376 base::android::ScopedJavaLocalRef<jstring> java_package_name = | 387 base::android::ScopedJavaLocalRef<jstring> java_package_name = |
377 base::android::ConvertUTF8ToJavaString(env, package_name); | 388 base::android::ConvertUTF8ToJavaString(env, package_name); |
378 bool success = false; | 389 bool success = false; |
379 if (task_type_ == INSTALL) { | 390 if (task_type_ == INSTALL) { |
391 webapk_package_ = package_name; | |
380 success = StartInstallingDownloadedWebApk(env, java_file_path, | 392 success = StartInstallingDownloadedWebApk(env, java_file_path, |
381 java_package_name); | 393 java_package_name); |
382 } else if (task_type_ == UPDATE) { | 394 if (success) |
395 return; | |
396 } | |
397 if (task_type_ == UPDATE) { | |
383 success = StartUpdateUsingDownloadedWebApk(env, java_file_path, | 398 success = StartUpdateUsingDownloadedWebApk(env, java_file_path, |
384 java_package_name); | 399 java_package_name); |
pkotwicz
2016/08/25 22:23:16
For updates you should call onSuccess() only once
Xi Han
2016/08/26 17:04:17
Yes, the update will also get a callback for the r
| |
385 } | 400 } |
401 OnInstallFinished(success); | |
402 } | |
403 | |
404 void WebApkInstaller::OnInstallFinished( | |
405 JNIEnv* env, | |
406 const base::android::JavaParamRef<jobject>& obj, | |
407 jboolean success) { | |
408 OnInstallFinished(success); | |
409 } | |
410 | |
411 void WebApkInstaller::OnInstallFinished(bool success) { | |
386 if (success) | 412 if (success) |
387 OnSuccess(); | 413 OnSuccess(); |
388 else | 414 else |
389 OnFailure(); | 415 OnFailure(); |
390 } | 416 } |
391 | 417 |
392 void WebApkInstaller::OnTimeout() { | 418 void WebApkInstaller::OnTimeout() { |
393 OnFailure(); | 419 OnFailure(); |
394 } | 420 } |
395 | 421 |
396 void WebApkInstaller::OnSuccess() { | 422 void WebApkInstaller::OnSuccess() { |
397 FinishCallback callback = finish_callback_; | 423 FinishCallback callback = finish_callback_; |
398 delete this; | 424 delete this; |
399 callback.Run(true); | 425 callback.Run(true, webapk_package_); |
400 } | 426 } |
401 | 427 |
402 void WebApkInstaller::OnFailure() { | 428 void WebApkInstaller::OnFailure() { |
403 FinishCallback callback = finish_callback_; | 429 FinishCallback callback = finish_callback_; |
404 delete this; | 430 delete this; |
405 callback.Run(false); | 431 callback.Run(false, ""); |
406 } | 432 } |
433 | |
434 // static | |
435 bool WebApkInstaller::Register(JNIEnv* env) { | |
436 return RegisterNativesImpl(env); | |
437 } | |
OLD | NEW |