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 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
104 #else | 104 #else |
105 #error "Unsupported target abi" | 105 #error "Unsupported target abi" |
106 #endif | 106 #endif |
107 } | 107 } |
108 | 108 |
109 // Populates webapk::WebApk and returns it. | 109 // Populates webapk::WebApk and returns it. |
110 // Must be called on a worker thread because it encodes an SkBitmap. | 110 // Must be called on a worker thread because it encodes an SkBitmap. |
111 std::unique_ptr<webapk::WebApk> BuildWebApkProtoInBackground( | 111 std::unique_ptr<webapk::WebApk> BuildWebApkProtoInBackground( |
112 const ShortcutInfo& shortcut_info, | 112 const ShortcutInfo& shortcut_info, |
113 const SkBitmap& shortcut_icon, | 113 const SkBitmap& shortcut_icon, |
114 const std::string& shortcut_icon_murmur2_hash) { | 114 bool stale_manifest, |
dominickn
2016/12/20 05:05:49
is_manifest_stale
Xi Han
2016/12/20 20:25:09
Done.
| |
115 std::map<std::string, std::string> icon_url_to_murmur2_hash_map) { | |
dominickn
2016/12/20 05:05:48
const std::map<std::string, std::string>&
Also ca
Xi Han
2016/12/20 20:25:09
Done.
dominickn
2016/12/21 02:51:49
We should try and eliminate this confusion. An alt
Xi Han
2016/12/21 22:45:44
Done.
| |
115 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); | 116 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); |
116 | 117 |
117 std::unique_ptr<webapk::WebApk> webapk(new webapk::WebApk); | 118 std::unique_ptr<webapk::WebApk> webapk(new webapk::WebApk); |
118 webapk->set_manifest_url(shortcut_info.manifest_url.spec()); | 119 webapk->set_manifest_url(shortcut_info.manifest_url.spec()); |
119 webapk->set_requester_application_package( | 120 webapk->set_requester_application_package( |
120 base::android::BuildInfo::GetInstance()->package_name()); | 121 base::android::BuildInfo::GetInstance()->package_name()); |
121 webapk->set_requester_application_version(version_info::GetVersionNumber()); | 122 webapk->set_requester_application_version(version_info::GetVersionNumber()); |
122 webapk->set_android_abi(getCurrentAbi()); | 123 webapk->set_android_abi(getCurrentAbi()); |
123 | 124 webapk->set_stale_manifest(stale_manifest); |
124 // TODO(hanxi): crbug.com/665078. Add a flag in WebAPK's proto to indicate | |
125 // that the Web Manifest data in the proto might be stale. | |
126 if (shortcut_icon_murmur2_hash.empty()) | |
127 return webapk; | |
128 | 125 |
129 webapk::WebAppManifest* web_app_manifest = webapk->mutable_manifest(); | 126 webapk::WebAppManifest* web_app_manifest = webapk->mutable_manifest(); |
130 web_app_manifest->set_name(base::UTF16ToUTF8(shortcut_info.name)); | 127 web_app_manifest->set_name(base::UTF16ToUTF8(shortcut_info.name)); |
131 web_app_manifest->set_short_name( | 128 web_app_manifest->set_short_name( |
132 base::UTF16ToUTF8(shortcut_info.short_name)); | 129 base::UTF16ToUTF8(shortcut_info.short_name)); |
133 web_app_manifest->set_start_url(shortcut_info.url.spec()); | 130 web_app_manifest->set_start_url(shortcut_info.url.spec()); |
134 web_app_manifest->set_orientation( | 131 web_app_manifest->set_orientation( |
135 content::WebScreenOrientationLockTypeToString( | 132 content::WebScreenOrientationLockTypeToString( |
136 shortcut_info.orientation)); | 133 shortcut_info.orientation)); |
137 web_app_manifest->set_display_mode( | 134 web_app_manifest->set_display_mode( |
138 content::WebDisplayModeToString(shortcut_info.display)); | 135 content::WebDisplayModeToString(shortcut_info.display)); |
139 web_app_manifest->set_background_color( | 136 web_app_manifest->set_background_color( |
140 ColorToString(shortcut_info.background_color)); | 137 ColorToString(shortcut_info.background_color)); |
141 web_app_manifest->set_theme_color(ColorToString(shortcut_info.theme_color)); | 138 web_app_manifest->set_theme_color(ColorToString(shortcut_info.theme_color)); |
142 | 139 |
143 std::string* scope = web_app_manifest->add_scopes(); | 140 std::string* scope = web_app_manifest->add_scopes(); |
144 scope->assign(GetScope(shortcut_info).spec()); | 141 scope->assign(GetScope(shortcut_info).spec()); |
145 | 142 |
146 webapk::Image* best_image = web_app_manifest->add_icons(); | 143 webapk::Image* best_image = web_app_manifest->add_icons(); |
147 best_image->set_src(shortcut_info.best_icon_url.spec()); | 144 std::string best_icon_url = shortcut_info.best_icon_url.spec(); |
148 best_image->set_hash(shortcut_icon_murmur2_hash); | 145 best_image->set_src(best_icon_url); |
146 std::map<std::string, std::string>::iterator it = | |
dominickn
2016/12/20 05:05:48
Does auto it work here?
Xi Han
2016/12/20 20:25:08
Done.
| |
147 icon_url_to_murmur2_hash_map.find(best_icon_url); | |
148 if (it != icon_url_to_murmur2_hash_map.end()) | |
149 best_image->set_hash(it->second); | |
149 std::vector<unsigned char> png_bytes; | 150 std::vector<unsigned char> png_bytes; |
150 gfx::PNGCodec::EncodeBGRASkBitmap(shortcut_icon, false, &png_bytes); | 151 gfx::PNGCodec::EncodeBGRASkBitmap(shortcut_icon, false, &png_bytes); |
151 best_image->set_image_data(&png_bytes.front(), png_bytes.size()); | 152 best_image->set_image_data(&png_bytes.front(), png_bytes.size()); |
152 | 153 |
153 for (const std::string& icon_url : shortcut_info.icon_urls) { | 154 for (const std::pair<std::string, std::string>& entry : |
dominickn
2016/12/20 05:05:48
const auto& entry?
Xi Han
2016/12/20 20:25:09
Done.
| |
154 if (icon_url == shortcut_info.best_icon_url.spec()) | 155 icon_url_to_murmur2_hash_map) { |
156 if (entry.first == shortcut_info.best_icon_url.spec()) | |
155 continue; | 157 continue; |
156 webapk::Image* image = web_app_manifest->add_icons(); | 158 webapk::Image* image = web_app_manifest->add_icons(); |
157 image->set_src(icon_url); | 159 image->set_src(entry.first); |
160 image->set_hash(entry.second); | |
158 } | 161 } |
159 | 162 |
160 return webapk; | 163 return webapk; |
161 } | 164 } |
162 | 165 |
166 // Calls the callback when the |webapk| request is created. | |
167 void OnWebApkProtoBuilt( | |
168 const base::Callback<void(std::unique_ptr<webapk::WebApk>)>& callback, | |
169 std::unique_ptr<webapk::WebApk> webapk) { | |
170 callback.Run(std::move(webapk)); | |
171 } | |
172 | |
163 // Returns task runner for running background tasks. | 173 // Returns task runner for running background tasks. |
164 scoped_refptr<base::TaskRunner> GetBackgroundTaskRunner() { | 174 scoped_refptr<base::TaskRunner> GetBackgroundTaskRunner() { |
165 return content::BrowserThread::GetBlockingPool() | 175 return content::BrowserThread::GetBlockingPool() |
166 ->GetTaskRunnerWithShutdownBehavior( | 176 ->GetTaskRunnerWithShutdownBehavior( |
167 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN); | 177 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN); |
168 } | 178 } |
169 | 179 |
170 // Creates a directory depending on the type of the task, and set permissions. | 180 // Creates a directory depending on the type of the task, and set permissions. |
171 // It also creates any parent directory along the path if doesn't exist, | 181 // It also creates any parent directory along the path if doesn't exist, |
172 // and sets permissions as well. | 182 // and sets permissions as well. |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
243 // transformations being applied to the bitmap (such as encoding/decoding | 253 // transformations being applied to the bitmap (such as encoding/decoding |
244 // the bitmap). The icon hash is used to determine whether the icon that | 254 // the bitmap). The icon hash is used to determine whether the icon that |
245 // the user sees matches the icon of a WebAPK that the WebAPK server | 255 // the user sees matches the icon of a WebAPK that the WebAPK server |
246 // generated for another user. (The icon can be dynamically generated.) | 256 // generated for another user. (The icon can be dynamically generated.) |
247 // | 257 // |
248 // We redownload the icon in order to take the Murmur2 hash. The redownload | 258 // We redownload the icon in order to take the Murmur2 hash. The redownload |
249 // should be fast because the icon should be in the HTTP cache. | 259 // should be fast because the icon should be in the HTTP cache. |
250 DownloadAppIconAndComputeMurmur2Hash(); | 260 DownloadAppIconAndComputeMurmur2Hash(); |
251 } | 261 } |
252 | 262 |
253 void WebApkInstaller::UpdateAsync(content::BrowserContext* browser_context, | 263 void WebApkInstaller::UpdateAsync( |
254 const FinishCallback& finish_callback, | 264 content::BrowserContext* browser_context, |
255 const std::string& icon_murmur2_hash, | 265 const FinishCallback& finish_callback, |
256 const std::string& webapk_package, | 266 const std::string& webapk_package, |
257 int webapk_version) { | 267 int webapk_version, |
268 bool stale_manifest, | |
269 const std::map<std::string, std::string>& icon_url_to_murmur2_hash_map) { | |
dominickn
2016/12/20 05:05:48
icon_url_to_murmur2_map (std::map is not a hash ma
Xi Han
2016/12/20 20:25:09
same.
| |
258 UpdateAsyncWithURLRequestContextGetter( | 270 UpdateAsyncWithURLRequestContextGetter( |
259 Profile::FromBrowserContext(browser_context)->GetRequestContext(), | 271 Profile::FromBrowserContext(browser_context)->GetRequestContext(), |
260 finish_callback, icon_murmur2_hash, webapk_package, webapk_version); | 272 finish_callback, webapk_package, webapk_version, stale_manifest, |
273 icon_url_to_murmur2_hash_map); | |
261 } | 274 } |
262 | 275 |
263 void WebApkInstaller::UpdateAsyncWithURLRequestContextGetter( | 276 void WebApkInstaller::UpdateAsyncWithURLRequestContextGetter( |
264 net::URLRequestContextGetter* request_context_getter, | 277 net::URLRequestContextGetter* request_context_getter, |
265 const FinishCallback& finish_callback, | 278 const FinishCallback& finish_callback, |
266 const std::string& icon_murmur2_hash, | |
267 const std::string& webapk_package, | 279 const std::string& webapk_package, |
268 int webapk_version) { | 280 int webapk_version, |
281 bool stale_manifest, | |
282 const std::map<std::string, std::string>& icon_url_to_murmur2_hash_map) { | |
dominickn
2016/12/20 05:05:49
icon_url_to_murmur2_map (std::map is not a hash ma
Xi Han
2016/12/20 20:25:09
same.
| |
269 request_context_getter_ = request_context_getter; | 283 request_context_getter_ = request_context_getter; |
270 finish_callback_ = finish_callback; | 284 finish_callback_ = finish_callback; |
271 shortcut_icon_murmur2_hash_ = icon_murmur2_hash; | |
272 webapk_package_ = webapk_package; | 285 webapk_package_ = webapk_package; |
273 webapk_version_ = webapk_version; | 286 webapk_version_ = webapk_version; |
274 task_type_ = UPDATE; | 287 task_type_ = UPDATE; |
275 | 288 |
276 base::PostTaskAndReplyWithResult( | 289 base::PostTaskAndReplyWithResult( |
277 GetBackgroundTaskRunner().get(), FROM_HERE, | 290 GetBackgroundTaskRunner().get(), FROM_HERE, |
278 base::Bind(&BuildWebApkProtoInBackground, shortcut_info_, | 291 base::Bind(&BuildWebApkProtoInBackground, shortcut_info_, shortcut_icon_, |
279 shortcut_icon_, shortcut_icon_murmur2_hash_), | 292 stale_manifest, icon_url_to_murmur2_hash_map), |
280 base::Bind(&WebApkInstaller::SendUpdateWebApkRequest, | 293 base::Bind(&WebApkInstaller::SendUpdateWebApkRequest, |
281 weak_ptr_factory_.GetWeakPtr())); | 294 weak_ptr_factory_.GetWeakPtr())); |
282 } | 295 } |
283 | 296 |
284 void WebApkInstaller::SetTimeoutMs(int timeout_ms) { | 297 void WebApkInstaller::SetTimeoutMs(int timeout_ms) { |
285 webapk_download_url_timeout_ms_ = timeout_ms; | 298 webapk_download_url_timeout_ms_ = timeout_ms; |
286 download_timeout_ms_ = timeout_ms; | 299 download_timeout_ms_ = timeout_ms; |
287 } | 300 } |
288 | 301 |
289 void WebApkInstaller::OnInstallFinished( | 302 void WebApkInstaller::OnInstallFinished( |
290 JNIEnv* env, | 303 JNIEnv* env, |
291 const base::android::JavaParamRef<jobject>& obj, | 304 const base::android::JavaParamRef<jobject>& obj, |
292 jboolean success) { | 305 jboolean success) { |
293 if (success) | 306 if (success) |
294 OnSuccess(); | 307 OnSuccess(); |
295 else | 308 else |
296 OnFailure(); | 309 OnFailure(); |
297 } | 310 } |
298 | 311 |
312 void WebApkInstaller::BuildWebApkProtoInBackgroundForTesting( | |
313 const base::Callback<void(std::unique_ptr<webapk::WebApk>)>& callback, | |
314 bool stale_manifest, | |
dominickn
2016/12/20 05:05:48
is_manifest_stale
Xi Han
2016/12/20 20:25:09
Done.
| |
315 const std::map<std::string, std::string>& icon_url_to_murmur2_hash_map) { | |
dominickn
2016/12/20 05:05:48
icon_url_to_murmur2_map (std::map is not a hash ma
Xi Han
2016/12/20 20:25:09
same.
| |
316 base::PostTaskAndReplyWithResult( | |
317 GetBackgroundTaskRunner().get(), FROM_HERE, | |
318 base::Bind(&BuildWebApkProtoInBackground, shortcut_info_, shortcut_icon_, | |
319 stale_manifest, icon_url_to_murmur2_hash_map), | |
320 base::Bind(&OnWebApkProtoBuilt, callback)); | |
321 } | |
322 | |
299 // static | 323 // static |
300 bool WebApkInstaller::Register(JNIEnv* env) { | 324 bool WebApkInstaller::Register(JNIEnv* env) { |
301 return RegisterNativesImpl(env); | 325 return RegisterNativesImpl(env); |
302 } | 326 } |
303 | 327 |
304 bool WebApkInstaller::StartInstallingDownloadedWebApk( | 328 bool WebApkInstaller::StartInstallingDownloadedWebApk( |
305 JNIEnv* env, | 329 JNIEnv* env, |
306 const base::android::ScopedJavaLocalRef<jstring>& java_file_path, | 330 const base::android::ScopedJavaLocalRef<jstring>& java_file_path, |
307 const base::android::ScopedJavaLocalRef<jstring>& java_package_name) { | 331 const base::android::ScopedJavaLocalRef<jstring>& java_package_name) { |
308 return Java_WebApkInstaller_installAsyncAndMonitorInstallationFromNative( | 332 return Java_WebApkInstaller_installAsyncAndMonitorInstallationFromNative( |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
403 request_context_getter_, shortcut_info_.best_icon_url, | 427 request_context_getter_, shortcut_info_.best_icon_url, |
404 base::Bind(&WebApkInstaller::OnGotIconMurmur2Hash, | 428 base::Bind(&WebApkInstaller::OnGotIconMurmur2Hash, |
405 weak_ptr_factory_.GetWeakPtr())); | 429 weak_ptr_factory_.GetWeakPtr())); |
406 } | 430 } |
407 | 431 |
408 void WebApkInstaller::OnGotIconMurmur2Hash( | 432 void WebApkInstaller::OnGotIconMurmur2Hash( |
409 const std::string& icon_murmur2_hash) { | 433 const std::string& icon_murmur2_hash) { |
410 timer_.Stop(); | 434 timer_.Stop(); |
411 icon_hasher_.reset(); | 435 icon_hasher_.reset(); |
412 | 436 |
413 shortcut_icon_murmur2_hash_ = icon_murmur2_hash; | |
414 | |
415 // An empty hash indicates that |icon_hasher_| encountered an error. | 437 // An empty hash indicates that |icon_hasher_| encountered an error. |
416 if (icon_murmur2_hash.empty()) { | 438 if (icon_murmur2_hash.empty()) { |
417 OnFailure(); | 439 OnFailure(); |
418 return; | 440 return; |
419 } | 441 } |
420 | 442 |
443 std::map<std::string, std::string> icon_url_to_murmur2_hash_map; | |
dominickn
2016/12/20 05:05:49
Minor nit: call this icon_url_to_murmur2_map. std:
Xi Han
2016/12/20 20:25:09
same.
| |
444 for (const std::string& icon_url : shortcut_info_.icon_urls) { | |
445 if (icon_url != shortcut_info_.best_icon_url.spec()) | |
446 icon_url_to_murmur2_hash_map[icon_url] = ""; | |
447 else | |
448 icon_url_to_murmur2_hash_map[icon_url] = icon_murmur2_hash; | |
449 } | |
450 | |
421 base::PostTaskAndReplyWithResult( | 451 base::PostTaskAndReplyWithResult( |
422 GetBackgroundTaskRunner().get(), FROM_HERE, | 452 GetBackgroundTaskRunner().get(), FROM_HERE, |
423 base::Bind(&BuildWebApkProtoInBackground, shortcut_info_, | 453 base::Bind(&BuildWebApkProtoInBackground, shortcut_info_, shortcut_icon_, |
424 shortcut_icon_, shortcut_icon_murmur2_hash_), | 454 false, icon_url_to_murmur2_hash_map), |
425 base::Bind(&WebApkInstaller::SendCreateWebApkRequest, | 455 base::Bind(&WebApkInstaller::SendCreateWebApkRequest, |
426 weak_ptr_factory_.GetWeakPtr())); | 456 weak_ptr_factory_.GetWeakPtr())); |
427 } | 457 } |
428 | 458 |
429 void WebApkInstaller::SendCreateWebApkRequest( | 459 void WebApkInstaller::SendCreateWebApkRequest( |
430 std::unique_ptr<webapk::WebApk> webapk) { | 460 std::unique_ptr<webapk::WebApk> webapk) { |
431 SendRequest(std::move(webapk), server_url_); | 461 SendRequest(std::move(webapk), server_url_); |
432 } | 462 } |
433 | 463 |
434 void WebApkInstaller::SendUpdateWebApkRequest( | 464 void WebApkInstaller::SendUpdateWebApkRequest( |
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
560 | 590 |
561 void WebApkInstaller::OnSuccess() { | 591 void WebApkInstaller::OnSuccess() { |
562 finish_callback_.Run(true, webapk_package_); | 592 finish_callback_.Run(true, webapk_package_); |
563 delete this; | 593 delete this; |
564 } | 594 } |
565 | 595 |
566 void WebApkInstaller::OnFailure() { | 596 void WebApkInstaller::OnFailure() { |
567 finish_callback_.Run(false, webapk_package_); | 597 finish_callback_.Run(false, webapk_package_); |
568 delete this; | 598 delete this; |
569 } | 599 } |
OLD | NEW |