Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(492)

Side by Side Diff: chrome/browser/android/webapk/webapk_installer.cc

Issue 2676863002: Update WebApkInstaller to support badge icon in installation. (Closed)
Patch Set: Revert git cl format Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
104 return "arm64-v8a"; 104 return "arm64-v8a";
105 #else 105 #else
106 #error "Unsupported target abi" 106 #error "Unsupported target abi"
107 #endif 107 #endif
108 } 108 }
109 109
110 // Populates webapk::WebApk and returns it. 110 // Populates webapk::WebApk and returns it.
111 // Must be called on a worker thread because it encodes an SkBitmap. 111 // Must be called on a worker thread because it encodes an SkBitmap.
112 std::unique_ptr<webapk::WebApk> BuildWebApkProtoInBackground( 112 std::unique_ptr<webapk::WebApk> BuildWebApkProtoInBackground(
113 const ShortcutInfo& shortcut_info, 113 const ShortcutInfo& shortcut_info,
114 const SkBitmap& shortcut_icon, 114 const SkBitmap& primary_icon,
115 const SkBitmap& badge_icon,
115 const std::map<std::string, std::string>& icon_url_to_murmur2_hash, 116 const std::map<std::string, std::string>& icon_url_to_murmur2_hash,
116 bool is_manifest_stale) { 117 bool is_manifest_stale) {
117 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); 118 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread());
118 119
119 std::unique_ptr<webapk::WebApk> webapk(new webapk::WebApk); 120 std::unique_ptr<webapk::WebApk> webapk(new webapk::WebApk);
120 webapk->set_manifest_url(shortcut_info.manifest_url.spec()); 121 webapk->set_manifest_url(shortcut_info.manifest_url.spec());
121 webapk->set_requester_application_package( 122 webapk->set_requester_application_package(
122 base::android::BuildInfo::GetInstance()->package_name()); 123 base::android::BuildInfo::GetInstance()->package_name());
123 webapk->set_requester_application_version(version_info::GetVersionNumber()); 124 webapk->set_requester_application_version(version_info::GetVersionNumber());
124 webapk->set_android_abi(getCurrentAbi()); 125 webapk->set_android_abi(getCurrentAbi());
125 webapk->set_stale_manifest(is_manifest_stale); 126 webapk->set_stale_manifest(is_manifest_stale);
126 127
127 webapk::WebAppManifest* web_app_manifest = webapk->mutable_manifest(); 128 webapk::WebAppManifest* web_app_manifest = webapk->mutable_manifest();
128 web_app_manifest->set_name(base::UTF16ToUTF8(shortcut_info.name)); 129 web_app_manifest->set_name(base::UTF16ToUTF8(shortcut_info.name));
129 web_app_manifest->set_short_name(base::UTF16ToUTF8(shortcut_info.short_name)); 130 web_app_manifest->set_short_name(base::UTF16ToUTF8(shortcut_info.short_name));
130 web_app_manifest->set_start_url(shortcut_info.url.spec()); 131 web_app_manifest->set_start_url(shortcut_info.url.spec());
131 web_app_manifest->set_orientation( 132 web_app_manifest->set_orientation(
132 content::WebScreenOrientationLockTypeToString(shortcut_info.orientation)); 133 content::WebScreenOrientationLockTypeToString(shortcut_info.orientation));
133 web_app_manifest->set_display_mode( 134 web_app_manifest->set_display_mode(
134 content::WebDisplayModeToString(shortcut_info.display)); 135 content::WebDisplayModeToString(shortcut_info.display));
135 web_app_manifest->set_background_color( 136 web_app_manifest->set_background_color(
136 ColorToString(shortcut_info.background_color)); 137 ColorToString(shortcut_info.background_color));
137 web_app_manifest->set_theme_color(ColorToString(shortcut_info.theme_color)); 138 web_app_manifest->set_theme_color(ColorToString(shortcut_info.theme_color));
138 139
139 std::string* scope = web_app_manifest->add_scopes(); 140 std::string* scope = web_app_manifest->add_scopes();
140 scope->assign(GetScope(shortcut_info).spec()); 141 scope->assign(GetScope(shortcut_info).spec());
141 142
142 webapk::Image* best_image = web_app_manifest->add_icons(); 143 webapk::Image* best_primary_icon_image = web_app_manifest->add_icons();
143 std::string best_primary_icon_url = 144 std::string best_primary_icon_url =
144 shortcut_info.best_primary_icon_url.spec(); 145 shortcut_info.best_primary_icon_url.spec();
145 best_image->set_src(best_primary_icon_url); 146 best_primary_icon_image->set_src(best_primary_icon_url);
146 auto it = icon_url_to_murmur2_hash.find(best_primary_icon_url); 147 auto it = icon_url_to_murmur2_hash.find(best_primary_icon_url);
147 if (it != icon_url_to_murmur2_hash.end()) 148 if (it != icon_url_to_murmur2_hash.end())
148 best_image->set_hash(it->second); 149 best_primary_icon_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(primary_icon, false, &png_bytes);
151 best_image->set_image_data(&png_bytes.front(), png_bytes.size()); 152 best_primary_icon_image->set_image_data(&png_bytes.front(), png_bytes.size());
153 best_primary_icon_image->add_usages(webapk::Image::PRIMARY_ICON);
154
155 if (!badge_icon.drawsNothing()) {
156 std::string best_badge_icon_url = shortcut_info.best_badge_icon_url.spec();
157 if (best_badge_icon_url == best_primary_icon_url) {
158 best_primary_icon_image->add_usages(webapk::Image::BADGE_ICON);
159 } else {
160 webapk::Image* best_badge_icon_image = web_app_manifest->add_icons();
161 std::string best_badge_icon_url =
dominickn 2017/02/23 02:08:52 This variable looks redundant (you already have it
F 2017/03/30 18:20:12 Oops. Thanks!
162 shortcut_info.best_badge_icon_url.spec();
163 best_badge_icon_image->set_src(best_badge_icon_url);
164 auto it = icon_url_to_murmur2_hash.find(best_badge_icon_url);
165 if (it != icon_url_to_murmur2_hash.end())
166 best_badge_icon_image->set_hash(it->second);
167 std::vector<unsigned char> png_bytes;
168 gfx::PNGCodec::EncodeBGRASkBitmap(badge_icon, false, &png_bytes);
169 best_badge_icon_image->set_image_data(&png_bytes.front(),
170 png_bytes.size());
171 best_badge_icon_image->add_usages(webapk::Image::BADGE_ICON);
172 }
173 }
152 174
153 for (const auto& entry : icon_url_to_murmur2_hash) { 175 for (const auto& entry : icon_url_to_murmur2_hash) {
154 if (entry.first == shortcut_info.best_primary_icon_url.spec()) 176 if (entry.first == shortcut_info.best_primary_icon_url.spec() ||
177 entry.first == shortcut_info.best_badge_icon_url.spec()) {
155 continue; 178 continue;
179 }
156 webapk::Image* image = web_app_manifest->add_icons(); 180 webapk::Image* image = web_app_manifest->add_icons();
157 image->set_src(entry.first); 181 image->set_src(entry.first);
158 image->set_hash(entry.second); 182 image->set_hash(entry.second);
159 } 183 }
160 184
161 return webapk; 185 return webapk;
162 } 186 }
163 187
164 // Calls the callback when the |webapk| request is created. 188 // Calls the callback when the |webapk| request is created.
165 void OnWebApkProtoBuilt( 189 void OnWebApkProtoBuilt(
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
211 235
212 WebApkInstaller::~WebApkInstaller() { 236 WebApkInstaller::~WebApkInstaller() {
213 JNIEnv* env = base::android::AttachCurrentThread(); 237 JNIEnv* env = base::android::AttachCurrentThread();
214 Java_WebApkInstaller_destroy(env, java_ref_); 238 Java_WebApkInstaller_destroy(env, java_ref_);
215 java_ref_.Reset(); 239 java_ref_.Reset();
216 } 240 }
217 241
218 // static 242 // static
219 void WebApkInstaller::InstallAsync(content::BrowserContext* context, 243 void WebApkInstaller::InstallAsync(content::BrowserContext* context,
220 const ShortcutInfo& shortcut_info, 244 const ShortcutInfo& shortcut_info,
221 const SkBitmap& shortcut_icon, 245 const SkBitmap& primary_icon,
246 const SkBitmap& badge_icon,
222 const FinishCallback& finish_callback) { 247 const FinishCallback& finish_callback) {
223 // The installer will delete itself when it is done. 248 // The installer will delete itself when it is done.
224 WebApkInstaller* installer = 249 WebApkInstaller* installer =
225 new WebApkInstaller(context, shortcut_info, shortcut_icon); 250 new WebApkInstaller(context, shortcut_info, primary_icon, badge_icon);
226 installer->InstallAsync(finish_callback); 251 installer->InstallAsync(finish_callback);
227 } 252 }
228 253
229 // static 254 // static
230 void WebApkInstaller::UpdateAsync( 255 void WebApkInstaller::UpdateAsync(
231 content::BrowserContext* context, 256 content::BrowserContext* context,
232 const ShortcutInfo& shortcut_info, 257 const ShortcutInfo& shortcut_info,
233 const SkBitmap& shortcut_icon, 258 const SkBitmap& primary_icon,
234 const std::string& webapk_package, 259 const std::string& webapk_package,
235 int webapk_version, 260 int webapk_version,
236 const std::map<std::string, std::string>& icon_url_to_murmur2_hash, 261 const std::map<std::string, std::string>& icon_url_to_murmur2_hash,
237 bool is_manifest_stale, 262 bool is_manifest_stale,
238 const FinishCallback& finish_callback) { 263 const FinishCallback& finish_callback) {
239 // The installer will delete itself when it is done. 264 // The installer will delete itself when it is done.
240 WebApkInstaller* installer = 265 SkBitmap empty_badge_icon;
241 new WebApkInstaller(context, shortcut_info, shortcut_icon); 266 WebApkInstaller* installer = new WebApkInstaller(
267 context, shortcut_info, primary_icon, empty_badge_icon);
242 installer->UpdateAsync(webapk_package, webapk_version, 268 installer->UpdateAsync(webapk_package, webapk_version,
243 icon_url_to_murmur2_hash, is_manifest_stale, 269 icon_url_to_murmur2_hash, is_manifest_stale,
244 finish_callback); 270 finish_callback);
245 } 271 }
246 272
247 // staic 273 // staic
248 void WebApkInstaller::InstallAsyncForTesting( 274 void WebApkInstaller::InstallAsyncForTesting(
249 WebApkInstaller* installer, 275 WebApkInstaller* installer,
250 const FinishCallback& finish_callback) { 276 const FinishCallback& finish_callback) {
251 installer->InstallAsync(finish_callback); 277 installer->InstallAsync(finish_callback);
(...skipping 26 matching lines...) Expand all
278 else 304 else
279 OnFailure(); 305 OnFailure();
280 } 306 }
281 307
282 void WebApkInstaller::BuildWebApkProtoInBackgroundForTesting( 308 void WebApkInstaller::BuildWebApkProtoInBackgroundForTesting(
283 const base::Callback<void(std::unique_ptr<webapk::WebApk>)>& callback, 309 const base::Callback<void(std::unique_ptr<webapk::WebApk>)>& callback,
284 const std::map<std::string, std::string>& icon_url_to_murmur2_hash, 310 const std::map<std::string, std::string>& icon_url_to_murmur2_hash,
285 bool is_manifest_stale) { 311 bool is_manifest_stale) {
286 base::PostTaskAndReplyWithResult( 312 base::PostTaskAndReplyWithResult(
287 GetBackgroundTaskRunner().get(), FROM_HERE, 313 GetBackgroundTaskRunner().get(), FROM_HERE,
288 base::Bind(&BuildWebApkProtoInBackground, shortcut_info_, shortcut_icon_, 314 base::Bind(&BuildWebApkProtoInBackground, shortcut_info_, primary_icon_,
289 icon_url_to_murmur2_hash, is_manifest_stale), 315 badge_icon_, icon_url_to_murmur2_hash, is_manifest_stale),
290 base::Bind(&OnWebApkProtoBuilt, callback)); 316 base::Bind(&OnWebApkProtoBuilt, callback));
291 } 317 }
292 318
293 // static 319 // static
294 bool WebApkInstaller::Register(JNIEnv* env) { 320 bool WebApkInstaller::Register(JNIEnv* env) {
295 return RegisterNativesImpl(env); 321 return RegisterNativesImpl(env);
296 } 322 }
297 323
298 bool WebApkInstaller::StartInstallingDownloadedWebApk( 324 bool WebApkInstaller::StartInstallingDownloadedWebApk(
299 JNIEnv* env, 325 JNIEnv* env,
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
337 java_url); 363 java_url);
338 } else { 364 } else {
339 return Java_WebApkInstaller_updateAsyncFromGooglePlay( 365 return Java_WebApkInstaller_updateAsyncFromGooglePlay(
340 env, java_ref_, java_webapk_package, version, java_title, java_token, 366 env, java_ref_, java_webapk_package, version, java_title, java_token,
341 java_url); 367 java_url);
342 } 368 }
343 } 369 }
344 370
345 WebApkInstaller::WebApkInstaller(content::BrowserContext* browser_context, 371 WebApkInstaller::WebApkInstaller(content::BrowserContext* browser_context,
346 const ShortcutInfo& shortcut_info, 372 const ShortcutInfo& shortcut_info,
347 const SkBitmap& shortcut_icon) 373 const SkBitmap& primary_icon,
374 const SkBitmap& badge_icon)
348 : request_context_getter_( 375 : request_context_getter_(
349 Profile::FromBrowserContext(browser_context)->GetRequestContext()), 376 Profile::FromBrowserContext(browser_context)->GetRequestContext()),
350 shortcut_info_(shortcut_info), 377 shortcut_info_(shortcut_info),
351 shortcut_icon_(shortcut_icon), 378 primary_icon_(primary_icon),
379 badge_icon_(badge_icon),
352 server_url_(GetServerUrl()), 380 server_url_(GetServerUrl()),
353 webapk_download_url_timeout_ms_(kWebApkDownloadUrlTimeoutMs), 381 webapk_download_url_timeout_ms_(kWebApkDownloadUrlTimeoutMs),
354 download_timeout_ms_(kDownloadTimeoutMs), 382 download_timeout_ms_(kDownloadTimeoutMs),
355 task_type_(UNDEFINED), 383 task_type_(UNDEFINED),
356 weak_ptr_factory_(this) { 384 weak_ptr_factory_(this) {
357 CreateJavaRef(); 385 CreateJavaRef();
358 } 386 }
359 387
360 void WebApkInstaller::CreateJavaRef() { 388 void WebApkInstaller::CreateJavaRef() {
361 JNIEnv* env = base::android::AttachCurrentThread(); 389 JNIEnv* env = base::android::AttachCurrentThread();
362 java_ref_.Reset( 390 java_ref_.Reset(
363 Java_WebApkInstaller_create(env, reinterpret_cast<intptr_t>(this))); 391 Java_WebApkInstaller_create(env, reinterpret_cast<intptr_t>(this)));
364 } 392 }
365 393
366 void WebApkInstaller::InstallAsync(const FinishCallback& finish_callback) { 394 void WebApkInstaller::InstallAsync(const FinishCallback& finish_callback) {
367 finish_callback_ = finish_callback; 395 finish_callback_ = finish_callback;
368 task_type_ = INSTALL; 396 task_type_ = INSTALL;
369 397
370 // We need to take the hash of the bitmap at the icon URL prior to any 398 // We need to take the hash of the bitmap at the icon URL prior to any
371 // transformations being applied to the bitmap (such as encoding/decoding 399 // transformations being applied to the bitmap (such as encoding/decoding
372 // the bitmap). The icon hash is used to determine whether the icon that 400 // the bitmap). The icon hash is used to determine whether the icon that
373 // the user sees matches the icon of a WebAPK that the WebAPK server 401 // the user sees matches the icon of a WebAPK that the WebAPK server
374 // generated for another user. (The icon can be dynamically generated.) 402 // generated for another user. (The icon can be dynamically generated.)
375 // 403 //
376 // We redownload the icon in order to take the Murmur2 hash. The redownload 404 // We redownload the icon in order to take the Murmur2 hash. The redownload
377 // should be fast because the icon should be in the HTTP cache. 405 // should be fast because the icon should be in the HTTP cache.
378 DownloadAppIconAndComputeMurmur2Hash(); 406 DownloadPrimaryIconAndComputeMurmur2Hash();
379 } 407 }
380 408
381 void WebApkInstaller::UpdateAsync( 409 void WebApkInstaller::UpdateAsync(
382 const std::string& webapk_package, 410 const std::string& webapk_package,
383 int webapk_version, 411 int webapk_version,
384 const std::map<std::string, std::string>& icon_url_to_murmur2_hash, 412 const std::map<std::string, std::string>& icon_url_to_murmur2_hash,
385 bool is_manifest_stale, 413 bool is_manifest_stale,
386 const FinishCallback& finish_callback) { 414 const FinishCallback& finish_callback) {
387 webapk_package_ = webapk_package; 415 webapk_package_ = webapk_package;
388 webapk_version_ = webapk_version; 416 webapk_version_ = webapk_version;
389 finish_callback_ = finish_callback; 417 finish_callback_ = finish_callback;
390 task_type_ = UPDATE; 418 task_type_ = UPDATE;
391 419
392 base::PostTaskAndReplyWithResult( 420 base::PostTaskAndReplyWithResult(
393 GetBackgroundTaskRunner().get(), FROM_HERE, 421 GetBackgroundTaskRunner().get(), FROM_HERE,
394 base::Bind(&BuildWebApkProtoInBackground, shortcut_info_, shortcut_icon_, 422 base::Bind(&BuildWebApkProtoInBackground, shortcut_info_, primary_icon_,
395 icon_url_to_murmur2_hash, is_manifest_stale), 423 badge_icon_, icon_url_to_murmur2_hash, is_manifest_stale),
396 base::Bind(&WebApkInstaller::SendUpdateWebApkRequest, 424 base::Bind(&WebApkInstaller::SendUpdateWebApkRequest,
397 weak_ptr_factory_.GetWeakPtr())); 425 weak_ptr_factory_.GetWeakPtr()));
398 } 426 }
399 427
400 void WebApkInstaller::OnURLFetchComplete(const net::URLFetcher* source) { 428 void WebApkInstaller::OnURLFetchComplete(const net::URLFetcher* source) {
401 timer_.Stop(); 429 timer_.Stop();
402 430
403 if (!source->GetStatus().is_success() || 431 if (!source->GetStatus().is_success() ||
404 source->GetResponseCode() != net::HTTP_OK) { 432 source->GetResponseCode() != net::HTTP_OK) {
405 LOG(WARNING) << base::StringPrintf( 433 LOG(WARNING) << base::StringPrintf(
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
440 // asynchronously after the install or upidate has either failed or 468 // asynchronously after the install or upidate has either failed or
441 // completed. 469 // completed.
442 InstallOrUpdateWebApkFromGooglePlay(response->package_name(), version, 470 InstallOrUpdateWebApkFromGooglePlay(response->package_name(), version,
443 response->token()); 471 response->token());
444 return; 472 return;
445 } 473 }
446 474
447 OnGotWebApkDownloadUrl(signed_download_url, response->package_name()); 475 OnGotWebApkDownloadUrl(signed_download_url, response->package_name());
448 } 476 }
449 477
450 void WebApkInstaller::DownloadAppIconAndComputeMurmur2Hash() { 478 void WebApkInstaller::DownloadPrimaryIconAndComputeMurmur2Hash() {
451 // Safeguard. WebApkIconHasher crashes if asked to fetch an invalid URL. 479 // Safeguard. WebApkIconHasher crashes if asked to fetch an invalid URL.
452 if (!shortcut_info_.best_primary_icon_url.is_valid()) { 480 if (!shortcut_info_.best_primary_icon_url.is_valid()) {
453 OnFailure(); 481 OnFailure();
454 return; 482 return;
455 } 483 }
456 484
457 timer_.Start( 485 timer_.Start(
458 FROM_HERE, base::TimeDelta::FromMilliseconds(download_timeout_ms_), 486 FROM_HERE, base::TimeDelta::FromMilliseconds(download_timeout_ms_),
459 base::Bind(&WebApkInstaller::OnTimeout, weak_ptr_factory_.GetWeakPtr())); 487 base::Bind(&WebApkInstaller::OnTimeout, weak_ptr_factory_.GetWeakPtr()));
460 488
461 icon_hasher_.reset(new WebApkIconHasher()); 489 icon_hasher_.reset(new WebApkIconHasher());
462 icon_hasher_->DownloadAndComputeMurmur2Hash( 490 icon_hasher_->DownloadAndComputeMurmur2Hash(
463 request_context_getter_, shortcut_info_.best_primary_icon_url, 491 request_context_getter_, shortcut_info_.best_primary_icon_url,
464 base::Bind(&WebApkInstaller::OnGotIconMurmur2Hash, 492 base::Bind(&WebApkInstaller::OnGotPrimaryIconMurmur2Hash,
465 weak_ptr_factory_.GetWeakPtr())); 493 weak_ptr_factory_.GetWeakPtr()));
466 } 494 }
467 495
468 void WebApkInstaller::OnGotIconMurmur2Hash( 496 void WebApkInstaller::OnGotPrimaryIconMurmur2Hash(
469 const std::string& icon_murmur2_hash) { 497 const std::string& primary_icon_hash) {
470 timer_.Stop(); 498 timer_.Stop();
471 icon_hasher_.reset(); 499 icon_hasher_.reset();
472 500
473 // An empty hash indicates that |icon_hasher_| encountered an error. 501 // An empty hash indicates that |icon_hasher_| encountered an error.
474 if (icon_murmur2_hash.empty()) { 502 if (primary_icon_hash.empty()) {
475 OnFailure(); 503 OnFailure();
476 return; 504 return;
477 } 505 }
478 506
507 primary_icon_hash_ = primary_icon_hash;
508
509 if (!shortcut_info_.best_badge_icon_url.is_empty() ||
510 shortcut_info_.best_badge_icon_url.spec() ==
511 shortcut_info_.best_primary_icon_url.spec()) {
512 DownloadBadgeIconAndComputeMurmur2Hash();
513 } else {
514 MapIconUrlToMurmur2Hash();
515 }
516 }
517
518 void WebApkInstaller::DownloadBadgeIconAndComputeMurmur2Hash() {
dominickn 2017/02/23 02:08:52 Instead of mostly duplicating DownloadPrimaryIconA
F 2017/03/30 18:20:12 After the refactoring of WebApkIconHasher, this ha
519 // Safeguard. WebApkIconHasher crashes if asked to fetch an invalid URL.
520 if (!shortcut_info_.best_badge_icon_url.is_valid()) {
521 OnFailure();
522 return;
523 }
524
525 timer_.Start(
526 FROM_HERE, base::TimeDelta::FromMilliseconds(download_timeout_ms_),
527 base::Bind(&WebApkInstaller::OnTimeout, weak_ptr_factory_.GetWeakPtr()));
528
529 icon_hasher_.reset(new WebApkIconHasher());
530 icon_hasher_->DownloadAndComputeMurmur2Hash(
531 request_context_getter_, shortcut_info_.best_badge_icon_url,
532 base::Bind(&WebApkInstaller::OnGotBadgeIconMurmur2Hash,
533 weak_ptr_factory_.GetWeakPtr()));
534 }
535
536 void WebApkInstaller::OnGotBadgeIconMurmur2Hash(
537 const std::string& badge_icon_hash) {
538 timer_.Stop();
539 icon_hasher_.reset();
540
541 // An empty hash indicates that |icon_hasher_| encountered an error.
542 if (badge_icon_hash.empty()) {
543 OnFailure();
544 return;
545 }
546
547 badge_icon_hash_ = badge_icon_hash;
548
549 MapIconUrlToMurmur2Hash();
550 }
551
552 void WebApkInstaller::MapIconUrlToMurmur2Hash() {
479 std::map<std::string, std::string> icon_url_to_murmur2_hash; 553 std::map<std::string, std::string> icon_url_to_murmur2_hash;
480 for (const std::string& icon_url : shortcut_info_.icon_urls) { 554 for (const std::string& icon_url : shortcut_info_.icon_urls) {
481 if (icon_url != shortcut_info_.best_primary_icon_url.spec()) 555 if (icon_url == shortcut_info_.best_primary_icon_url.spec())
556 icon_url_to_murmur2_hash[icon_url] = primary_icon_hash_;
557 else if (icon_url == shortcut_info_.best_badge_icon_url.spec())
558 icon_url_to_murmur2_hash[icon_url] = badge_icon_hash_;
559 else
482 icon_url_to_murmur2_hash[icon_url] = ""; 560 icon_url_to_murmur2_hash[icon_url] = "";
483 else
484 icon_url_to_murmur2_hash[icon_url] = icon_murmur2_hash;
485 } 561 }
486 562
487 base::PostTaskAndReplyWithResult( 563 base::PostTaskAndReplyWithResult(
488 GetBackgroundTaskRunner().get(), FROM_HERE, 564 GetBackgroundTaskRunner().get(), FROM_HERE,
489 base::Bind(&BuildWebApkProtoInBackground, shortcut_info_, shortcut_icon_, 565 base::Bind(&BuildWebApkProtoInBackground, shortcut_info_, primary_icon_,
490 icon_url_to_murmur2_hash, false /* is_manifest_stale */), 566 badge_icon_, icon_url_to_murmur2_hash,
567 false /* is_manifest_stale */),
491 base::Bind(&WebApkInstaller::SendCreateWebApkRequest, 568 base::Bind(&WebApkInstaller::SendCreateWebApkRequest,
492 weak_ptr_factory_.GetWeakPtr())); 569 weak_ptr_factory_.GetWeakPtr()));
493 } 570 }
494 571
495 void WebApkInstaller::SendCreateWebApkRequest( 572 void WebApkInstaller::SendCreateWebApkRequest(
496 std::unique_ptr<webapk::WebApk> webapk) { 573 std::unique_ptr<webapk::WebApk> webapk) {
497 SendRequest(std::move(webapk), server_url_); 574 SendRequest(std::move(webapk), server_url_);
498 } 575 }
499 576
500 void WebApkInstaller::SendUpdateWebApkRequest( 577 void WebApkInstaller::SendUpdateWebApkRequest(
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after
630 707
631 void WebApkInstaller::OnSuccess() { 708 void WebApkInstaller::OnSuccess() {
632 finish_callback_.Run(true, webapk_package_); 709 finish_callback_.Run(true, webapk_package_);
633 delete this; 710 delete this;
634 } 711 }
635 712
636 void WebApkInstaller::OnFailure() { 713 void WebApkInstaller::OnFailure() {
637 finish_callback_.Run(false, webapk_package_); 714 finish_callback_.Run(false, webapk_package_);
638 delete this; 715 delete this;
639 } 716 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698