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

Unified 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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/android/webapk/webapk_installer.cc
diff --git a/chrome/browser/android/webapk/webapk_installer.cc b/chrome/browser/android/webapk/webapk_installer.cc
index 059d85f9889a83f75c5d33e645579daa3b0fe827..aa15ae333ad97f9777daec70c795d4463c493edd 100644
--- a/chrome/browser/android/webapk/webapk_installer.cc
+++ b/chrome/browser/android/webapk/webapk_installer.cc
@@ -111,7 +111,8 @@ std::string getCurrentAbi() {
// Must be called on a worker thread because it encodes an SkBitmap.
std::unique_ptr<webapk::WebApk> BuildWebApkProtoInBackground(
const ShortcutInfo& shortcut_info,
- const SkBitmap& shortcut_icon,
+ const SkBitmap& primary_icon,
+ const SkBitmap& badge_icon,
const std::map<std::string, std::string>& icon_url_to_murmur2_hash,
bool is_manifest_stale) {
DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread());
@@ -139,20 +140,43 @@ std::unique_ptr<webapk::WebApk> BuildWebApkProtoInBackground(
std::string* scope = web_app_manifest->add_scopes();
scope->assign(GetScope(shortcut_info).spec());
- webapk::Image* best_image = web_app_manifest->add_icons();
+ webapk::Image* best_primary_icon_image = web_app_manifest->add_icons();
std::string best_primary_icon_url =
shortcut_info.best_primary_icon_url.spec();
- best_image->set_src(best_primary_icon_url);
+ best_primary_icon_image->set_src(best_primary_icon_url);
auto it = icon_url_to_murmur2_hash.find(best_primary_icon_url);
if (it != icon_url_to_murmur2_hash.end())
- best_image->set_hash(it->second);
+ best_primary_icon_image->set_hash(it->second);
std::vector<unsigned char> png_bytes;
- gfx::PNGCodec::EncodeBGRASkBitmap(shortcut_icon, false, &png_bytes);
- best_image->set_image_data(&png_bytes.front(), png_bytes.size());
+ gfx::PNGCodec::EncodeBGRASkBitmap(primary_icon, false, &png_bytes);
+ best_primary_icon_image->set_image_data(&png_bytes.front(), png_bytes.size());
+ best_primary_icon_image->add_usages(webapk::Image::PRIMARY_ICON);
+
+ if (!badge_icon.drawsNothing()) {
+ std::string best_badge_icon_url = shortcut_info.best_badge_icon_url.spec();
+ if (best_badge_icon_url == best_primary_icon_url) {
+ best_primary_icon_image->add_usages(webapk::Image::BADGE_ICON);
+ } else {
+ webapk::Image* best_badge_icon_image = web_app_manifest->add_icons();
+ 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!
+ shortcut_info.best_badge_icon_url.spec();
+ best_badge_icon_image->set_src(best_badge_icon_url);
+ auto it = icon_url_to_murmur2_hash.find(best_badge_icon_url);
+ if (it != icon_url_to_murmur2_hash.end())
+ best_badge_icon_image->set_hash(it->second);
+ std::vector<unsigned char> png_bytes;
+ gfx::PNGCodec::EncodeBGRASkBitmap(badge_icon, false, &png_bytes);
+ best_badge_icon_image->set_image_data(&png_bytes.front(),
+ png_bytes.size());
+ best_badge_icon_image->add_usages(webapk::Image::BADGE_ICON);
+ }
+ }
for (const auto& entry : icon_url_to_murmur2_hash) {
- if (entry.first == shortcut_info.best_primary_icon_url.spec())
+ if (entry.first == shortcut_info.best_primary_icon_url.spec() ||
+ entry.first == shortcut_info.best_badge_icon_url.spec()) {
continue;
+ }
webapk::Image* image = web_app_manifest->add_icons();
image->set_src(entry.first);
image->set_hash(entry.second);
@@ -218,11 +242,12 @@ WebApkInstaller::~WebApkInstaller() {
// static
void WebApkInstaller::InstallAsync(content::BrowserContext* context,
const ShortcutInfo& shortcut_info,
- const SkBitmap& shortcut_icon,
+ const SkBitmap& primary_icon,
+ const SkBitmap& badge_icon,
const FinishCallback& finish_callback) {
// The installer will delete itself when it is done.
WebApkInstaller* installer =
- new WebApkInstaller(context, shortcut_info, shortcut_icon);
+ new WebApkInstaller(context, shortcut_info, primary_icon, badge_icon);
installer->InstallAsync(finish_callback);
}
@@ -230,15 +255,16 @@ void WebApkInstaller::InstallAsync(content::BrowserContext* context,
void WebApkInstaller::UpdateAsync(
content::BrowserContext* context,
const ShortcutInfo& shortcut_info,
- const SkBitmap& shortcut_icon,
+ const SkBitmap& primary_icon,
const std::string& webapk_package,
int webapk_version,
const std::map<std::string, std::string>& icon_url_to_murmur2_hash,
bool is_manifest_stale,
const FinishCallback& finish_callback) {
// The installer will delete itself when it is done.
- WebApkInstaller* installer =
- new WebApkInstaller(context, shortcut_info, shortcut_icon);
+ SkBitmap empty_badge_icon;
+ WebApkInstaller* installer = new WebApkInstaller(
+ context, shortcut_info, primary_icon, empty_badge_icon);
installer->UpdateAsync(webapk_package, webapk_version,
icon_url_to_murmur2_hash, is_manifest_stale,
finish_callback);
@@ -285,8 +311,8 @@ void WebApkInstaller::BuildWebApkProtoInBackgroundForTesting(
bool is_manifest_stale) {
base::PostTaskAndReplyWithResult(
GetBackgroundTaskRunner().get(), FROM_HERE,
- base::Bind(&BuildWebApkProtoInBackground, shortcut_info_, shortcut_icon_,
- icon_url_to_murmur2_hash, is_manifest_stale),
+ base::Bind(&BuildWebApkProtoInBackground, shortcut_info_, primary_icon_,
+ badge_icon_, icon_url_to_murmur2_hash, is_manifest_stale),
base::Bind(&OnWebApkProtoBuilt, callback));
}
@@ -344,11 +370,13 @@ bool WebApkInstaller::InstallOrUpdateWebApkFromGooglePlay(
WebApkInstaller::WebApkInstaller(content::BrowserContext* browser_context,
const ShortcutInfo& shortcut_info,
- const SkBitmap& shortcut_icon)
+ const SkBitmap& primary_icon,
+ const SkBitmap& badge_icon)
: request_context_getter_(
Profile::FromBrowserContext(browser_context)->GetRequestContext()),
shortcut_info_(shortcut_info),
- shortcut_icon_(shortcut_icon),
+ primary_icon_(primary_icon),
+ badge_icon_(badge_icon),
server_url_(GetServerUrl()),
webapk_download_url_timeout_ms_(kWebApkDownloadUrlTimeoutMs),
download_timeout_ms_(kDownloadTimeoutMs),
@@ -375,7 +403,7 @@ void WebApkInstaller::InstallAsync(const FinishCallback& finish_callback) {
//
// We redownload the icon in order to take the Murmur2 hash. The redownload
// should be fast because the icon should be in the HTTP cache.
- DownloadAppIconAndComputeMurmur2Hash();
+ DownloadPrimaryIconAndComputeMurmur2Hash();
}
void WebApkInstaller::UpdateAsync(
@@ -391,8 +419,8 @@ void WebApkInstaller::UpdateAsync(
base::PostTaskAndReplyWithResult(
GetBackgroundTaskRunner().get(), FROM_HERE,
- base::Bind(&BuildWebApkProtoInBackground, shortcut_info_, shortcut_icon_,
- icon_url_to_murmur2_hash, is_manifest_stale),
+ base::Bind(&BuildWebApkProtoInBackground, shortcut_info_, primary_icon_,
+ badge_icon_, icon_url_to_murmur2_hash, is_manifest_stale),
base::Bind(&WebApkInstaller::SendUpdateWebApkRequest,
weak_ptr_factory_.GetWeakPtr()));
}
@@ -447,7 +475,7 @@ void WebApkInstaller::OnURLFetchComplete(const net::URLFetcher* source) {
OnGotWebApkDownloadUrl(signed_download_url, response->package_name());
}
-void WebApkInstaller::DownloadAppIconAndComputeMurmur2Hash() {
+void WebApkInstaller::DownloadPrimaryIconAndComputeMurmur2Hash() {
// Safeguard. WebApkIconHasher crashes if asked to fetch an invalid URL.
if (!shortcut_info_.best_primary_icon_url.is_valid()) {
OnFailure();
@@ -461,33 +489,82 @@ void WebApkInstaller::DownloadAppIconAndComputeMurmur2Hash() {
icon_hasher_.reset(new WebApkIconHasher());
icon_hasher_->DownloadAndComputeMurmur2Hash(
request_context_getter_, shortcut_info_.best_primary_icon_url,
- base::Bind(&WebApkInstaller::OnGotIconMurmur2Hash,
+ base::Bind(&WebApkInstaller::OnGotPrimaryIconMurmur2Hash,
+ weak_ptr_factory_.GetWeakPtr()));
+}
+
+void WebApkInstaller::OnGotPrimaryIconMurmur2Hash(
+ const std::string& primary_icon_hash) {
+ timer_.Stop();
+ icon_hasher_.reset();
+
+ // An empty hash indicates that |icon_hasher_| encountered an error.
+ if (primary_icon_hash.empty()) {
+ OnFailure();
+ return;
+ }
+
+ primary_icon_hash_ = primary_icon_hash;
+
+ if (!shortcut_info_.best_badge_icon_url.is_empty() ||
+ shortcut_info_.best_badge_icon_url.spec() ==
+ shortcut_info_.best_primary_icon_url.spec()) {
+ DownloadBadgeIconAndComputeMurmur2Hash();
+ } else {
+ MapIconUrlToMurmur2Hash();
+ }
+}
+
+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
+ // Safeguard. WebApkIconHasher crashes if asked to fetch an invalid URL.
+ if (!shortcut_info_.best_badge_icon_url.is_valid()) {
+ OnFailure();
+ return;
+ }
+
+ timer_.Start(
+ FROM_HERE, base::TimeDelta::FromMilliseconds(download_timeout_ms_),
+ base::Bind(&WebApkInstaller::OnTimeout, weak_ptr_factory_.GetWeakPtr()));
+
+ icon_hasher_.reset(new WebApkIconHasher());
+ icon_hasher_->DownloadAndComputeMurmur2Hash(
+ request_context_getter_, shortcut_info_.best_badge_icon_url,
+ base::Bind(&WebApkInstaller::OnGotBadgeIconMurmur2Hash,
weak_ptr_factory_.GetWeakPtr()));
}
-void WebApkInstaller::OnGotIconMurmur2Hash(
- const std::string& icon_murmur2_hash) {
+void WebApkInstaller::OnGotBadgeIconMurmur2Hash(
+ const std::string& badge_icon_hash) {
timer_.Stop();
icon_hasher_.reset();
// An empty hash indicates that |icon_hasher_| encountered an error.
- if (icon_murmur2_hash.empty()) {
+ if (badge_icon_hash.empty()) {
OnFailure();
return;
}
+ badge_icon_hash_ = badge_icon_hash;
+
+ MapIconUrlToMurmur2Hash();
+}
+
+void WebApkInstaller::MapIconUrlToMurmur2Hash() {
std::map<std::string, std::string> icon_url_to_murmur2_hash;
for (const std::string& icon_url : shortcut_info_.icon_urls) {
- if (icon_url != shortcut_info_.best_primary_icon_url.spec())
- icon_url_to_murmur2_hash[icon_url] = "";
+ if (icon_url == shortcut_info_.best_primary_icon_url.spec())
+ icon_url_to_murmur2_hash[icon_url] = primary_icon_hash_;
+ else if (icon_url == shortcut_info_.best_badge_icon_url.spec())
+ icon_url_to_murmur2_hash[icon_url] = badge_icon_hash_;
else
- icon_url_to_murmur2_hash[icon_url] = icon_murmur2_hash;
+ icon_url_to_murmur2_hash[icon_url] = "";
}
base::PostTaskAndReplyWithResult(
GetBackgroundTaskRunner().get(), FROM_HERE,
- base::Bind(&BuildWebApkProtoInBackground, shortcut_info_, shortcut_icon_,
- icon_url_to_murmur2_hash, false /* is_manifest_stale */),
+ base::Bind(&BuildWebApkProtoInBackground, shortcut_info_, primary_icon_,
+ badge_icon_, icon_url_to_murmur2_hash,
+ false /* is_manifest_stale */),
base::Bind(&WebApkInstaller::SendCreateWebApkRequest,
weak_ptr_factory_.GetWeakPtr()));
}

Powered by Google App Engine
This is Rietveld 408576698