| 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 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 154 finish_callback); | 154 finish_callback); |
| 155 } | 155 } |
| 156 | 156 |
| 157 void WebApkInstaller::InstallAsyncWithURLRequestContextGetter( | 157 void WebApkInstaller::InstallAsyncWithURLRequestContextGetter( |
| 158 net::URLRequestContextGetter* request_context_getter, | 158 net::URLRequestContextGetter* request_context_getter, |
| 159 const FinishCallback& finish_callback) { | 159 const FinishCallback& finish_callback) { |
| 160 request_context_getter_ = request_context_getter; | 160 request_context_getter_ = request_context_getter; |
| 161 finish_callback_ = finish_callback; | 161 finish_callback_ = finish_callback; |
| 162 task_type_ = INSTALL; | 162 task_type_ = INSTALL; |
| 163 | 163 |
| 164 if (!shortcut_info_.icon_url.is_valid()) { | |
| 165 OnFailure(); | |
| 166 return; | |
| 167 } | |
| 168 | |
| 169 // We need to take the hash of the bitmap at the icon URL prior to any | 164 // We need to take the hash of the bitmap at the icon URL prior to any |
| 170 // transformations being applied to the bitmap (such as encoding/decoding | 165 // transformations being applied to the bitmap (such as encoding/decoding |
| 171 // the bitmap). The icon hash is used to determine whether the icon that | 166 // the bitmap). The icon hash is used to determine whether the icon that |
| 172 // the user sees matches the icon of a WebAPK that the WebAPK server | 167 // the user sees matches the icon of a WebAPK that the WebAPK server |
| 173 // generated for another user. (The icon can be dynamically generated.) | 168 // generated for another user. (The icon can be dynamically generated.) |
| 174 // | 169 // |
| 175 // We redownload the icon in order to take the Murmur2 hash. The redownload | 170 // We redownload the icon in order to take the Murmur2 hash. The redownload |
| 176 // should be fast because the icon should be in the HTTP cache. | 171 // should be fast because the icon should be in the HTTP cache. |
| 177 DownloadAppIconAndComputeMurmur2Hash(); | 172 DownloadAppIconAndComputeMurmur2Hash(); |
| 178 } | 173 } |
| 179 | 174 |
| 180 void WebApkInstaller::SetTimeoutMs(int timeout_ms) { | 175 void WebApkInstaller::SetTimeoutMs(int timeout_ms) { |
| 181 webapk_download_url_timeout_ms_ = timeout_ms; | 176 webapk_download_url_timeout_ms_ = timeout_ms; |
| 182 download_timeout_ms_ = timeout_ms; | 177 download_timeout_ms_ = timeout_ms; |
| 183 } | 178 } |
| 184 | 179 |
| 185 void WebApkInstaller::UpdateAsync(content::BrowserContext* browser_context, | 180 void WebApkInstaller::UpdateAsync(content::BrowserContext* browser_context, |
| 186 const FinishCallback& finish_callback, | 181 const FinishCallback& finish_callback, |
| 182 const std::string& icon_murmur2_hash, |
| 187 const std::string& webapk_package, | 183 const std::string& webapk_package, |
| 188 int webapk_version) { | 184 int webapk_version) { |
| 189 UpdateAsyncWithURLRequestContextGetter( | 185 UpdateAsyncWithURLRequestContextGetter( |
| 190 Profile::FromBrowserContext(browser_context)->GetRequestContext(), | 186 Profile::FromBrowserContext(browser_context)->GetRequestContext(), |
| 191 finish_callback, webapk_package, webapk_version); | 187 finish_callback, icon_murmur2_hash, webapk_package, webapk_version); |
| 192 } | 188 } |
| 193 | 189 |
| 194 void WebApkInstaller::UpdateAsyncWithURLRequestContextGetter( | 190 void WebApkInstaller::UpdateAsyncWithURLRequestContextGetter( |
| 195 net::URLRequestContextGetter* request_context_getter, | 191 net::URLRequestContextGetter* request_context_getter, |
| 196 const FinishCallback& finish_callback, | 192 const FinishCallback& finish_callback, |
| 193 const std::string& icon_murmur2_hash, |
| 197 const std::string& webapk_package, | 194 const std::string& webapk_package, |
| 198 int webapk_version) { | 195 int webapk_version) { |
| 199 request_context_getter_ = request_context_getter; | 196 request_context_getter_ = request_context_getter; |
| 200 finish_callback_ = finish_callback; | 197 finish_callback_ = finish_callback; |
| 198 shortcut_icon_murmur2_hash_ = icon_murmur2_hash; |
| 201 webapk_package_ = webapk_package; | 199 webapk_package_ = webapk_package; |
| 202 webapk_version_ = webapk_version; | 200 webapk_version_ = webapk_version; |
| 203 task_type_ = UPDATE; | 201 task_type_ = UPDATE; |
| 204 | 202 |
| 205 if (!shortcut_info_.icon_url.is_valid()) { | 203 base::PostTaskAndReplyWithResult( |
| 206 OnFailure(); | 204 GetBackgroundTaskRunner().get(), FROM_HERE, |
| 207 return; | 205 base::Bind(&BuildWebApkProtoInBackground, shortcut_info_, |
| 208 } | 206 shortcut_icon_, shortcut_icon_murmur2_hash_), |
| 209 | 207 base::Bind(&WebApkInstaller::SendUpdateWebApkRequest, |
| 210 DownloadAppIconAndComputeMurmur2Hash(); | 208 weak_ptr_factory_.GetWeakPtr())); |
| 211 } | 209 } |
| 212 | 210 |
| 213 bool WebApkInstaller::StartInstallingDownloadedWebApk( | 211 bool WebApkInstaller::StartInstallingDownloadedWebApk( |
| 214 JNIEnv* env, | 212 JNIEnv* env, |
| 215 const base::android::ScopedJavaLocalRef<jstring>& java_file_path, | 213 const base::android::ScopedJavaLocalRef<jstring>& java_file_path, |
| 216 const base::android::ScopedJavaLocalRef<jstring>& java_package_name) { | 214 const base::android::ScopedJavaLocalRef<jstring>& java_package_name) { |
| 217 return Java_WebApkInstaller_installAsyncFromNative(env, java_file_path, | 215 return Java_WebApkInstaller_installAsyncFromNative(env, java_file_path, |
| 218 java_package_name); | 216 java_package_name); |
| 219 } | 217 } |
| 220 | 218 |
| (...skipping 26 matching lines...) Expand all Loading... |
| 247 | 245 |
| 248 GURL signed_download_url(response->signed_download_url()); | 246 GURL signed_download_url(response->signed_download_url()); |
| 249 if (!signed_download_url.is_valid() || response->package_name().empty()) { | 247 if (!signed_download_url.is_valid() || response->package_name().empty()) { |
| 250 OnFailure(); | 248 OnFailure(); |
| 251 return; | 249 return; |
| 252 } | 250 } |
| 253 OnGotWebApkDownloadUrl(signed_download_url, response->package_name()); | 251 OnGotWebApkDownloadUrl(signed_download_url, response->package_name()); |
| 254 } | 252 } |
| 255 | 253 |
| 256 void WebApkInstaller::DownloadAppIconAndComputeMurmur2Hash() { | 254 void WebApkInstaller::DownloadAppIconAndComputeMurmur2Hash() { |
| 255 // Safeguard. WebApkIconHasher crashes if asked to fetch an invalid URL. |
| 256 if (!shortcut_info_.icon_url.is_valid()) { |
| 257 OnFailure(); |
| 258 return; |
| 259 } |
| 260 |
| 257 timer_.Start( | 261 timer_.Start( |
| 258 FROM_HERE, base::TimeDelta::FromMilliseconds(download_timeout_ms_), | 262 FROM_HERE, base::TimeDelta::FromMilliseconds(download_timeout_ms_), |
| 259 base::Bind(&WebApkInstaller::OnTimeout, weak_ptr_factory_.GetWeakPtr())); | 263 base::Bind(&WebApkInstaller::OnTimeout, weak_ptr_factory_.GetWeakPtr())); |
| 260 | 264 |
| 261 icon_hasher_.reset(new WebApkIconHasher()); | 265 icon_hasher_.reset(new WebApkIconHasher()); |
| 262 icon_hasher_->DownloadAndComputeMurmur2Hash( | 266 icon_hasher_->DownloadAndComputeMurmur2Hash( |
| 263 request_context_getter_, shortcut_info_.icon_url, | 267 request_context_getter_, shortcut_info_.icon_url, |
| 264 base::Bind(&WebApkInstaller::OnGotIconMurmur2Hash, | 268 base::Bind(&WebApkInstaller::OnGotIconMurmur2Hash, |
| 265 weak_ptr_factory_.GetWeakPtr())); | 269 weak_ptr_factory_.GetWeakPtr())); |
| 266 } | 270 } |
| 267 | 271 |
| 268 void WebApkInstaller::OnGotIconMurmur2Hash( | 272 void WebApkInstaller::OnGotIconMurmur2Hash( |
| 269 const std::string& icon_murmur2_hash) { | 273 const std::string& icon_murmur2_hash) { |
| 270 timer_.Stop(); | 274 timer_.Stop(); |
| 271 icon_hasher_.reset(); | 275 icon_hasher_.reset(); |
| 272 | 276 |
| 273 shortcut_icon_murmur2_hash_ = icon_murmur2_hash; | 277 shortcut_icon_murmur2_hash_ = icon_murmur2_hash; |
| 274 | 278 |
| 275 // An empty hash indicates that |icon_hasher_| encountered an error. | 279 // An empty hash indicates that |icon_hasher_| encountered an error. |
| 276 if (icon_murmur2_hash.empty()) { | 280 if (icon_murmur2_hash.empty()) { |
| 277 OnFailure(); | 281 OnFailure(); |
| 278 return; | 282 return; |
| 279 } | 283 } |
| 280 | 284 |
| 281 if (task_type_ == INSTALL) { | 285 base::PostTaskAndReplyWithResult( |
| 282 base::PostTaskAndReplyWithResult( | 286 GetBackgroundTaskRunner().get(), FROM_HERE, |
| 283 GetBackgroundTaskRunner().get(), FROM_HERE, | 287 base::Bind(&BuildWebApkProtoInBackground, shortcut_info_, |
| 284 base::Bind(&BuildWebApkProtoInBackground, shortcut_info_, | 288 shortcut_icon_, shortcut_icon_murmur2_hash_), |
| 285 shortcut_icon_, shortcut_icon_murmur2_hash_), | 289 base::Bind(&WebApkInstaller::SendCreateWebApkRequest, |
| 286 base::Bind(&WebApkInstaller::SendCreateWebApkRequest, | 290 weak_ptr_factory_.GetWeakPtr())); |
| 287 weak_ptr_factory_.GetWeakPtr())); | |
| 288 } else if (task_type_ == UPDATE) { | |
| 289 base::PostTaskAndReplyWithResult( | |
| 290 GetBackgroundTaskRunner().get(), FROM_HERE, | |
| 291 base::Bind(&BuildWebApkProtoInBackground, shortcut_info_, | |
| 292 shortcut_icon_, shortcut_icon_murmur2_hash_), | |
| 293 base::Bind(&WebApkInstaller::SendUpdateWebApkRequest, | |
| 294 weak_ptr_factory_.GetWeakPtr())); | |
| 295 } | |
| 296 } | 291 } |
| 297 | 292 |
| 298 void WebApkInstaller::SendCreateWebApkRequest( | 293 void WebApkInstaller::SendCreateWebApkRequest( |
| 299 std::unique_ptr<webapk::WebApk> webapk) { | 294 std::unique_ptr<webapk::WebApk> webapk) { |
| 300 GURL server_url(server_url_.spec() + kDefaultWebApkServerUrlResponseType); | 295 GURL server_url(server_url_.spec() + kDefaultWebApkServerUrlResponseType); |
| 301 SendRequest(std::move(webapk), net::URLFetcher::POST, server_url); | 296 SendRequest(std::move(webapk), net::URLFetcher::POST, server_url); |
| 302 } | 297 } |
| 303 | 298 |
| 304 void WebApkInstaller::SendUpdateWebApkRequest( | 299 void WebApkInstaller::SendUpdateWebApkRequest( |
| 305 std::unique_ptr<webapk::WebApk> webapk) { | 300 std::unique_ptr<webapk::WebApk> webapk) { |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 402 FinishCallback callback = finish_callback_; | 397 FinishCallback callback = finish_callback_; |
| 403 delete this; | 398 delete this; |
| 404 callback.Run(true); | 399 callback.Run(true); |
| 405 } | 400 } |
| 406 | 401 |
| 407 void WebApkInstaller::OnFailure() { | 402 void WebApkInstaller::OnFailure() { |
| 408 FinishCallback callback = finish_callback_; | 403 FinishCallback callback = finish_callback_; |
| 409 delete this; | 404 delete this; |
| 410 callback.Run(false); | 405 callback.Run(false); |
| 411 } | 406 } |
| OLD | NEW |