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() { |
| 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 17 matching lines...) Expand all Loading... |
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) { |
326 base::FilePath output_dir; | 337 base::FilePath output_dir; |
327 base::android::GetCacheDirectory(&output_dir); | 338 base::android::GetCacheDirectory(&output_dir); |
| 339 webapk_package_ = package_name; |
328 // TODO(pkotwicz): Download WebAPKs into WebAPK-specific subdirectory | 340 // TODO(pkotwicz): Download WebAPKs into WebAPK-specific subdirectory |
329 // directory. | 341 // directory. |
330 // TODO(pkotwicz): Figure out when downloaded WebAPK should be deleted. | 342 // TODO(pkotwicz): Figure out when downloaded WebAPK should be deleted. |
331 | 343 |
332 timer_.Start( | 344 timer_.Start( |
333 FROM_HERE, base::TimeDelta::FromMilliseconds(download_timeout_ms_), | 345 FROM_HERE, base::TimeDelta::FromMilliseconds(download_timeout_ms_), |
334 base::Bind(&WebApkInstaller::OnTimeout, weak_ptr_factory_.GetWeakPtr())); | 346 base::Bind(&WebApkInstaller::OnTimeout, weak_ptr_factory_.GetWeakPtr())); |
335 | 347 |
336 base::FilePath output_path = output_dir.AppendASCII(package_name); | 348 base::FilePath output_path = output_dir.AppendASCII(webapk_package_); |
337 downloader_.reset(new FileDownloader( | 349 downloader_.reset(new FileDownloader( |
338 download_url, output_path, true, request_context_getter_, | 350 download_url, output_path, true, request_context_getter_, |
339 base::Bind(&WebApkInstaller::OnWebApkDownloaded, | 351 base::Bind(&WebApkInstaller::OnWebApkDownloaded, |
340 weak_ptr_factory_.GetWeakPtr(), output_path, package_name))); | 352 weak_ptr_factory_.GetWeakPtr(), output_path))); |
341 } | 353 } |
342 | 354 |
343 void WebApkInstaller::OnWebApkDownloaded(const base::FilePath& file_path, | 355 void WebApkInstaller::OnWebApkDownloaded(const base::FilePath& file_path, |
344 const std::string& package_name, | |
345 FileDownloader::Result result) { | 356 FileDownloader::Result result) { |
346 timer_.Stop(); | 357 timer_.Stop(); |
347 | 358 |
348 if (result != FileDownloader::DOWNLOADED) { | 359 if (result != FileDownloader::DOWNLOADED) { |
349 OnFailure(); | 360 OnFailure(); |
350 return; | 361 return; |
351 } | 362 } |
352 | 363 |
353 int posix_permissions = base::FILE_PERMISSION_READ_BY_USER | | 364 int posix_permissions = base::FILE_PERMISSION_READ_BY_USER | |
354 base::FILE_PERMISSION_WRITE_BY_USER | | 365 base::FILE_PERMISSION_WRITE_BY_USER | |
355 base::FILE_PERMISSION_READ_BY_GROUP | | 366 base::FILE_PERMISSION_READ_BY_GROUP | |
356 base::FILE_PERMISSION_READ_BY_OTHERS; | 367 base::FILE_PERMISSION_READ_BY_OTHERS; |
357 base::PostTaskAndReplyWithResult( | 368 base::PostTaskAndReplyWithResult( |
358 GetBackgroundTaskRunner().get(), FROM_HERE, | 369 GetBackgroundTaskRunner().get(), FROM_HERE, |
359 base::Bind(&base::SetPosixFilePermissions, file_path, posix_permissions), | 370 base::Bind(&base::SetPosixFilePermissions, file_path, posix_permissions), |
360 base::Bind(&WebApkInstaller::OnWebApkMadeWorldReadable, | 371 base::Bind(&WebApkInstaller::OnWebApkMadeWorldReadable, |
361 weak_ptr_factory_.GetWeakPtr(), file_path, package_name)); | 372 weak_ptr_factory_.GetWeakPtr(), file_path)); |
362 } | 373 } |
363 | 374 |
364 void WebApkInstaller::OnWebApkMadeWorldReadable( | 375 void WebApkInstaller::OnWebApkMadeWorldReadable( |
365 const base::FilePath& file_path, | 376 const base::FilePath& file_path, |
366 const std::string& package_name, | |
367 bool change_permission_success) { | 377 bool change_permission_success) { |
368 if (!change_permission_success) { | 378 if (!change_permission_success) { |
369 OnFailure(); | 379 OnFailure(); |
370 return; | 380 return; |
371 } | 381 } |
372 | 382 |
373 JNIEnv* env = base::android::AttachCurrentThread(); | 383 JNIEnv* env = base::android::AttachCurrentThread(); |
374 base::android::ScopedJavaLocalRef<jstring> java_file_path = | 384 base::android::ScopedJavaLocalRef<jstring> java_file_path = |
375 base::android::ConvertUTF8ToJavaString(env, file_path.value()); | 385 base::android::ConvertUTF8ToJavaString(env, file_path.value()); |
376 base::android::ScopedJavaLocalRef<jstring> java_package_name = | 386 base::android::ScopedJavaLocalRef<jstring> java_package_name = |
377 base::android::ConvertUTF8ToJavaString(env, package_name); | 387 base::android::ConvertUTF8ToJavaString(env, webapk_package_); |
378 bool success = false; | 388 bool success = false; |
379 if (task_type_ == INSTALL) { | 389 if (task_type_ == INSTALL) { |
380 success = StartInstallingDownloadedWebApk(env, java_file_path, | 390 success = StartInstallingDownloadedWebApk(env, java_file_path, |
381 java_package_name); | 391 java_package_name); |
382 } else if (task_type_ == UPDATE) { | 392 } else if (task_type_ == UPDATE) { |
383 success = StartUpdateUsingDownloadedWebApk(env, java_file_path, | 393 success = StartUpdateUsingDownloadedWebApk(env, java_file_path, |
384 java_package_name); | 394 java_package_name); |
385 } | 395 } |
| 396 if (!success) |
| 397 OnFailure(); |
| 398 } |
| 399 |
| 400 void WebApkInstaller::OnInstallFinished( |
| 401 JNIEnv* env, |
| 402 const base::android::JavaParamRef<jobject>& obj, |
| 403 jboolean success) { |
386 if (success) | 404 if (success) |
387 OnSuccess(); | 405 OnSuccess(); |
388 else | 406 else |
389 OnFailure(); | 407 OnFailure(); |
390 } | 408 } |
391 | 409 |
392 void WebApkInstaller::OnTimeout() { | 410 void WebApkInstaller::OnTimeout() { |
393 OnFailure(); | 411 OnFailure(); |
394 } | 412 } |
395 | 413 |
396 void WebApkInstaller::OnSuccess() { | 414 void WebApkInstaller::OnSuccess() { |
397 FinishCallback callback = finish_callback_; | 415 FinishCallback callback = finish_callback_; |
398 delete this; | 416 delete this; |
399 callback.Run(true); | 417 callback.Run(true, webapk_package_); |
400 } | 418 } |
401 | 419 |
402 void WebApkInstaller::OnFailure() { | 420 void WebApkInstaller::OnFailure() { |
403 FinishCallback callback = finish_callback_; | 421 FinishCallback callback = finish_callback_; |
404 delete this; | 422 delete this; |
405 callback.Run(false); | 423 callback.Run(false, ""); |
406 } | 424 } |
| 425 |
| 426 // static |
| 427 bool WebApkInstaller::Register(JNIEnv* env) { |
| 428 return RegisterNativesImpl(env); |
| 429 } |
OLD | NEW |