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 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
100 return "x86_64"; | 100 return "x86_64"; |
101 #elif defined(__aarch64__) | 101 #elif defined(__aarch64__) |
102 return "arm64-v8a"; | 102 return "arm64-v8a"; |
103 #else | 103 #else |
104 #error "Unsupported target abi" | 104 #error "Unsupported target abi" |
105 #endif | 105 #endif |
106 } | 106 } |
107 | 107 |
108 // Populates webapk::WebApk and returns it. | 108 // Populates webapk::WebApk and returns it. |
109 // Must be called on a worker thread because it encodes an SkBitmap. | 109 // Must be called on a worker thread because it encodes an SkBitmap. |
110 std::unique_ptr<webapk::WebApk> BuildWebApkProtoInBackground( | 110 std::unique_ptr<webapk::WebApk> BuildWebApkProtoInBackground( |
pkotwicz
2016/12/07 20:41:30
We should test the behavior of this method when Sh
Xi Han
2016/12/09 18:40:11
It is easier to parse the created proto directly.
| |
111 const ShortcutInfo& shortcut_info, | 111 const ShortcutInfo& shortcut_info, |
112 const SkBitmap& shortcut_icon, | 112 const SkBitmap& shortcut_icon, |
113 const std::string& shortcut_icon_murmur2_hash) { | 113 bool stale_manifest, |
114 std::map<std::string, std::string> icon_url_hash_map) { | |
114 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); | 115 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); |
115 | 116 |
116 std::unique_ptr<webapk::WebApk> webapk(new webapk::WebApk); | 117 std::unique_ptr<webapk::WebApk> webapk(new webapk::WebApk); |
117 webapk->set_manifest_url(shortcut_info.manifest_url.spec()); | 118 webapk->set_manifest_url(shortcut_info.manifest_url.spec()); |
118 webapk->set_requester_application_package( | 119 webapk->set_requester_application_package( |
119 base::android::BuildInfo::GetInstance()->package_name()); | 120 base::android::BuildInfo::GetInstance()->package_name()); |
120 webapk->set_requester_application_version(version_info::GetVersionNumber()); | 121 webapk->set_requester_application_version(version_info::GetVersionNumber()); |
121 webapk->set_android_abi(getCurrentAbi()); | 122 webapk->set_android_abi(getCurrentAbi()); |
122 | 123 webapk->set_stale_manifest(stale_manifest); |
123 // TODO(hanxi): crbug.com/665078. Add a flag in WebAPK's proto to indicate | |
124 // that the Web Manifest data in the proto might be stale. | |
125 if (shortcut_icon_murmur2_hash.empty()) | |
126 return webapk; | |
127 | 124 |
128 webapk::WebAppManifest* web_app_manifest = webapk->mutable_manifest(); | 125 webapk::WebAppManifest* web_app_manifest = webapk->mutable_manifest(); |
129 web_app_manifest->set_name(base::UTF16ToUTF8(shortcut_info.name)); | 126 web_app_manifest->set_name(base::UTF16ToUTF8(shortcut_info.name)); |
130 web_app_manifest->set_short_name( | 127 web_app_manifest->set_short_name( |
131 base::UTF16ToUTF8(shortcut_info.short_name)); | 128 base::UTF16ToUTF8(shortcut_info.short_name)); |
132 web_app_manifest->set_start_url(shortcut_info.url.spec()); | 129 web_app_manifest->set_start_url(shortcut_info.url.spec()); |
133 web_app_manifest->set_orientation( | 130 web_app_manifest->set_orientation( |
134 content::WebScreenOrientationLockTypeToString( | 131 content::WebScreenOrientationLockTypeToString( |
135 shortcut_info.orientation)); | 132 shortcut_info.orientation)); |
136 web_app_manifest->set_display_mode( | 133 web_app_manifest->set_display_mode( |
137 content::WebDisplayModeToString(shortcut_info.display)); | 134 content::WebDisplayModeToString(shortcut_info.display)); |
138 web_app_manifest->set_background_color( | 135 web_app_manifest->set_background_color( |
139 ColorToString(shortcut_info.background_color)); | 136 ColorToString(shortcut_info.background_color)); |
140 web_app_manifest->set_theme_color(ColorToString(shortcut_info.theme_color)); | 137 web_app_manifest->set_theme_color(ColorToString(shortcut_info.theme_color)); |
141 | 138 |
142 std::string* scope = web_app_manifest->add_scopes(); | 139 std::string* scope = web_app_manifest->add_scopes(); |
143 scope->assign(GetScope(shortcut_info).spec()); | 140 scope->assign(GetScope(shortcut_info).spec()); |
144 | 141 |
145 webapk::Image* best_image = web_app_manifest->add_icons(); | 142 webapk::Image* best_image = web_app_manifest->add_icons(); |
146 best_image->set_src(shortcut_info.best_icon_url.spec()); | 143 std::string best_icon_url = shortcut_info.best_icon_url.spec(); |
147 best_image->set_hash(shortcut_icon_murmur2_hash); | 144 best_image->set_src(best_icon_url); |
145 std::map<std::string, std::string>::iterator it = | |
146 icon_url_hash_map.find(best_icon_url); | |
147 if (it != icon_url_hash_map.end()) | |
148 best_image->set_hash(it->second); | |
149 else | |
150 best_image->set_hash(""); | |
pkotwicz
2016/12/07 20:41:30
Is line 150 necessary? What would go wrong if we j
Xi Han
2016/12/09 18:40:11
Looks like should be fine. Removed.
| |
148 std::vector<unsigned char> png_bytes; | 151 std::vector<unsigned char> png_bytes; |
149 gfx::PNGCodec::EncodeBGRASkBitmap(shortcut_icon, false, &png_bytes); | 152 gfx::PNGCodec::EncodeBGRASkBitmap(shortcut_icon, false, &png_bytes); |
150 best_image->set_image_data(&png_bytes.front(), png_bytes.size()); | 153 best_image->set_image_data(&png_bytes.front(), png_bytes.size()); |
151 | 154 |
152 for (const std::string& icon_url : shortcut_info.icon_urls) { | 155 for (const std::pair<std::string, std::string>& entry : icon_url_hash_map) { |
153 if (icon_url == shortcut_info.best_icon_url.spec()) | 156 if (entry.first == shortcut_info.best_icon_url.spec()) |
154 continue; | 157 continue; |
155 webapk::Image* image = web_app_manifest->add_icons(); | 158 webapk::Image* image = web_app_manifest->add_icons(); |
156 image->set_src(icon_url); | 159 image->set_src(entry.first); |
160 // crbug.com/669060. Sends all the icon hashes when the Web Manifest has | |
161 // been removed and can't be fetched by the WebAPK server. The WebAPK server | |
162 // will be able to create the same AndroidManifest.xml as before, therefore | |
163 // prevents unnecessary request for update if the Web Manifest becomes | |
164 // available again. | |
165 if (stale_manifest) | |
166 image->set_hash(entry.second); | |
pkotwicz
2016/12/07 20:41:30
Can't we always set the hash and remove line 165?
Xi Han
2016/12/09 18:40:11
Personally I don't prefer to do that, since more d
pkotwicz
2016/12/09 20:09:44
Always sending the hashes makes things clearer. Th
| |
157 } | 167 } |
158 | 168 |
159 return webapk; | 169 return webapk; |
160 } | 170 } |
161 | 171 |
162 // Returns task runner for running background tasks. | 172 // Returns task runner for running background tasks. |
163 scoped_refptr<base::TaskRunner> GetBackgroundTaskRunner() { | 173 scoped_refptr<base::TaskRunner> GetBackgroundTaskRunner() { |
164 return content::BrowserThread::GetBlockingPool() | 174 return content::BrowserThread::GetBlockingPool() |
165 ->GetTaskRunnerWithShutdownBehavior( | 175 ->GetTaskRunnerWithShutdownBehavior( |
166 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN); | 176 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN); |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
242 // transformations being applied to the bitmap (such as encoding/decoding | 252 // transformations being applied to the bitmap (such as encoding/decoding |
243 // the bitmap). The icon hash is used to determine whether the icon that | 253 // the bitmap). The icon hash is used to determine whether the icon that |
244 // the user sees matches the icon of a WebAPK that the WebAPK server | 254 // the user sees matches the icon of a WebAPK that the WebAPK server |
245 // generated for another user. (The icon can be dynamically generated.) | 255 // generated for another user. (The icon can be dynamically generated.) |
246 // | 256 // |
247 // We redownload the icon in order to take the Murmur2 hash. The redownload | 257 // We redownload the icon in order to take the Murmur2 hash. The redownload |
248 // should be fast because the icon should be in the HTTP cache. | 258 // should be fast because the icon should be in the HTTP cache. |
249 DownloadAppIconAndComputeMurmur2Hash(); | 259 DownloadAppIconAndComputeMurmur2Hash(); |
250 } | 260 } |
251 | 261 |
252 void WebApkInstaller::UpdateAsync(content::BrowserContext* browser_context, | 262 void WebApkInstaller::UpdateAsync( |
253 const FinishCallback& finish_callback, | 263 content::BrowserContext* browser_context, |
254 const std::string& icon_murmur2_hash, | 264 const FinishCallback& finish_callback, |
255 const std::string& webapk_package, | 265 const std::string& webapk_package, |
256 int webapk_version) { | 266 int webapk_version, |
267 bool stale_manifest, | |
268 const std::map<std::string, std::string>& icon_url_hash_map) { | |
257 UpdateAsyncWithURLRequestContextGetter( | 269 UpdateAsyncWithURLRequestContextGetter( |
258 Profile::FromBrowserContext(browser_context)->GetRequestContext(), | 270 Profile::FromBrowserContext(browser_context)->GetRequestContext(), |
259 finish_callback, icon_murmur2_hash, webapk_package, webapk_version); | 271 finish_callback, webapk_package, webapk_version, stale_manifest, |
272 icon_url_hash_map); | |
260 } | 273 } |
261 | 274 |
262 void WebApkInstaller::UpdateAsyncWithURLRequestContextGetter( | 275 void WebApkInstaller::UpdateAsyncWithURLRequestContextGetter( |
263 net::URLRequestContextGetter* request_context_getter, | 276 net::URLRequestContextGetter* request_context_getter, |
264 const FinishCallback& finish_callback, | 277 const FinishCallback& finish_callback, |
265 const std::string& icon_murmur2_hash, | |
266 const std::string& webapk_package, | 278 const std::string& webapk_package, |
267 int webapk_version) { | 279 int webapk_version, |
280 bool stale_manifest, | |
281 const std::map<std::string, std::string>& icon_url_hash_map) { | |
268 request_context_getter_ = request_context_getter; | 282 request_context_getter_ = request_context_getter; |
269 finish_callback_ = finish_callback; | 283 finish_callback_ = finish_callback; |
270 shortcut_icon_murmur2_hash_ = icon_murmur2_hash; | |
271 webapk_package_ = webapk_package; | 284 webapk_package_ = webapk_package; |
272 webapk_version_ = webapk_version; | 285 webapk_version_ = webapk_version; |
273 task_type_ = UPDATE; | 286 task_type_ = UPDATE; |
274 | 287 |
275 base::PostTaskAndReplyWithResult( | 288 base::PostTaskAndReplyWithResult( |
276 GetBackgroundTaskRunner().get(), FROM_HERE, | 289 GetBackgroundTaskRunner().get(), FROM_HERE, |
277 base::Bind(&BuildWebApkProtoInBackground, shortcut_info_, | 290 base::Bind(&BuildWebApkProtoInBackground, shortcut_info_, shortcut_icon_, |
278 shortcut_icon_, shortcut_icon_murmur2_hash_), | 291 stale_manifest, icon_url_hash_map), |
279 base::Bind(&WebApkInstaller::SendUpdateWebApkRequest, | 292 base::Bind(&WebApkInstaller::SendUpdateWebApkRequest, |
280 weak_ptr_factory_.GetWeakPtr())); | 293 weak_ptr_factory_.GetWeakPtr())); |
281 } | 294 } |
282 | 295 |
283 void WebApkInstaller::SetTimeoutMs(int timeout_ms) { | 296 void WebApkInstaller::SetTimeoutMs(int timeout_ms) { |
284 webapk_download_url_timeout_ms_ = timeout_ms; | 297 webapk_download_url_timeout_ms_ = timeout_ms; |
285 download_timeout_ms_ = timeout_ms; | 298 download_timeout_ms_ = timeout_ms; |
286 } | 299 } |
287 | 300 |
288 void WebApkInstaller::OnInstallFinished( | 301 void WebApkInstaller::OnInstallFinished( |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
358 request_context_getter_, shortcut_info_.best_icon_url, | 371 request_context_getter_, shortcut_info_.best_icon_url, |
359 base::Bind(&WebApkInstaller::OnGotIconMurmur2Hash, | 372 base::Bind(&WebApkInstaller::OnGotIconMurmur2Hash, |
360 weak_ptr_factory_.GetWeakPtr())); | 373 weak_ptr_factory_.GetWeakPtr())); |
361 } | 374 } |
362 | 375 |
363 void WebApkInstaller::OnGotIconMurmur2Hash( | 376 void WebApkInstaller::OnGotIconMurmur2Hash( |
364 const std::string& icon_murmur2_hash) { | 377 const std::string& icon_murmur2_hash) { |
365 timer_.Stop(); | 378 timer_.Stop(); |
366 icon_hasher_.reset(); | 379 icon_hasher_.reset(); |
367 | 380 |
368 shortcut_icon_murmur2_hash_ = icon_murmur2_hash; | |
369 | |
370 // An empty hash indicates that |icon_hasher_| encountered an error. | 381 // An empty hash indicates that |icon_hasher_| encountered an error. |
371 if (icon_murmur2_hash.empty()) { | 382 if (icon_murmur2_hash.empty()) { |
372 OnFailure(); | 383 OnFailure(); |
373 return; | 384 return; |
374 } | 385 } |
375 | 386 |
387 std::map<std::string, std::string> icon_url_hash_map; | |
388 for (const std::string& icon_url: shortcut_info_.icon_urls) { | |
pkotwicz
2016/12/07 20:41:30
Nit: Space before ':'
Xi Han
2016/12/09 18:40:11
Done.
| |
389 if (icon_url != shortcut_info_.best_icon_url.spec()) | |
390 icon_url_hash_map[icon_url] = ""; | |
391 else | |
392 icon_url_hash_map[icon_url] = icon_murmur2_hash; | |
393 } | |
394 | |
376 base::PostTaskAndReplyWithResult( | 395 base::PostTaskAndReplyWithResult( |
377 GetBackgroundTaskRunner().get(), FROM_HERE, | 396 GetBackgroundTaskRunner().get(), FROM_HERE, |
378 base::Bind(&BuildWebApkProtoInBackground, shortcut_info_, | 397 base::Bind(&BuildWebApkProtoInBackground, shortcut_info_, shortcut_icon_, |
379 shortcut_icon_, shortcut_icon_murmur2_hash_), | 398 false, icon_url_hash_map), |
380 base::Bind(&WebApkInstaller::SendCreateWebApkRequest, | 399 base::Bind(&WebApkInstaller::SendCreateWebApkRequest, |
381 weak_ptr_factory_.GetWeakPtr())); | 400 weak_ptr_factory_.GetWeakPtr())); |
382 } | 401 } |
383 | 402 |
384 void WebApkInstaller::SendCreateWebApkRequest( | 403 void WebApkInstaller::SendCreateWebApkRequest( |
385 std::unique_ptr<webapk::WebApk> webapk) { | 404 std::unique_ptr<webapk::WebApk> webapk) { |
386 SendRequest(std::move(webapk), server_url_); | 405 SendRequest(std::move(webapk), server_url_); |
387 } | 406 } |
388 | 407 |
389 void WebApkInstaller::SendUpdateWebApkRequest( | 408 void WebApkInstaller::SendUpdateWebApkRequest( |
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
515 | 534 |
516 void WebApkInstaller::OnSuccess() { | 535 void WebApkInstaller::OnSuccess() { |
517 finish_callback_.Run(true, webapk_package_); | 536 finish_callback_.Run(true, webapk_package_); |
518 delete this; | 537 delete this; |
519 } | 538 } |
520 | 539 |
521 void WebApkInstaller::OnFailure() { | 540 void WebApkInstaller::OnFailure() { |
522 finish_callback_.Run(false, webapk_package_); | 541 finish_callback_.Run(false, webapk_package_); |
523 delete this; | 542 delete this; |
524 } | 543 } |
OLD | NEW |