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

Side by Side Diff: chrome/browser/android/webapps/add_to_homescreen_data_fetcher.cc

Issue 1997313002: Make favicon and manifest icon processing more consistent for add to homescreen. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Better variable naming Created 4 years, 4 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
« no previous file with comments | « chrome/browser/android/webapps/add_to_homescreen_data_fetcher.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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/webapps/add_to_homescreen_data_fetcher.h" 5 #include "chrome/browser/android/webapps/add_to_homescreen_data_fetcher.h"
6 6
7 #include <vector> 7 #include <vector>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/callback.h" 10 #include "base/callback.h"
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after
101 101
102 // Sanitize received_web_app_info. 102 // Sanitize received_web_app_info.
103 WebApplicationInfo web_app_info = received_web_app_info; 103 WebApplicationInfo web_app_info = received_web_app_info;
104 web_app_info.title = 104 web_app_info.title =
105 web_app_info.title.substr(0, chrome::kMaxMetaTagAttributeLength); 105 web_app_info.title.substr(0, chrome::kMaxMetaTagAttributeLength);
106 web_app_info.description = 106 web_app_info.description =
107 web_app_info.description.substr(0, chrome::kMaxMetaTagAttributeLength); 107 web_app_info.description.substr(0, chrome::kMaxMetaTagAttributeLength);
108 108
109 // Simply set the user-editable title to be the page's title 109 // Simply set the user-editable title to be the page's title
110 shortcut_info_.user_title = web_app_info.title.empty() 110 shortcut_info_.user_title = web_app_info.title.empty()
111 ? web_contents()->GetTitle() 111 ? web_contents()->GetTitle()
112 : web_app_info.title; 112 : web_app_info.title;
113 shortcut_info_.short_name = shortcut_info_.user_title; 113 shortcut_info_.short_name = shortcut_info_.user_title;
114 shortcut_info_.name = shortcut_info_.user_title; 114 shortcut_info_.name = shortcut_info_.user_title;
115 115
116 if (web_app_info.mobile_capable == WebApplicationInfo::MOBILE_CAPABLE || 116 if (web_app_info.mobile_capable == WebApplicationInfo::MOBILE_CAPABLE ||
117 web_app_info.mobile_capable == WebApplicationInfo::MOBILE_CAPABLE_APPLE) { 117 web_app_info.mobile_capable == WebApplicationInfo::MOBILE_CAPABLE_APPLE) {
118 shortcut_info_.display = blink::WebDisplayModeStandalone; 118 shortcut_info_.display = blink::WebDisplayModeStandalone;
119 } 119 }
120 120
121 // Record what type of shortcut was added by the user. 121 // Record what type of shortcut was added by the user.
122 switch (web_app_info.mobile_capable) { 122 switch (web_app_info.mobile_capable) {
(...skipping 29 matching lines...) Expand all
152 base::Bind(&AddToHomescreenDataFetcher::OnDidPerformInstallableCheck, 152 base::Bind(&AddToHomescreenDataFetcher::OnDidPerformInstallableCheck,
153 this)); 153 this));
154 } 154 }
155 155
156 void AddToHomescreenDataFetcher::OnDidPerformInstallableCheck( 156 void AddToHomescreenDataFetcher::OnDidPerformInstallableCheck(
157 const InstallableData& data) { 157 const InstallableData& data) {
158 if (!web_contents() || !weak_observer_) 158 if (!web_contents() || !weak_observer_)
159 return; 159 return;
160 160
161 if (!data.manifest.IsEmpty()) { 161 if (!data.manifest.IsEmpty()) {
162 content::RecordAction( 162 content::RecordAction(
163 base::UserMetricsAction("webapps.AddShortcut.Manifest")); 163 base::UserMetricsAction("webapps.AddShortcut.Manifest"));
164 shortcut_info_.UpdateFromManifest(data.manifest); 164 shortcut_info_.UpdateFromManifest(data.manifest);
165 shortcut_info_.manifest_url = data.manifest_url; 165 shortcut_info_.manifest_url = data.manifest_url;
166 } 166 }
167 167
168 // Save the splash screen URL for the later download. 168 // Save the splash screen URL for the later download.
169 splash_screen_url_ = ManifestIconSelector::FindBestMatchingIcon( 169 splash_screen_url_ = ManifestIconSelector::FindBestMatchingIcon(
170 data.manifest.icons, ideal_splash_image_size_in_dp_, 170 data.manifest.icons, ideal_splash_image_size_in_dp_,
171 minimum_splash_image_size_in_dp_); 171 minimum_splash_image_size_in_dp_);
172 172
173 weak_observer_->OnUserTitleAvailable(shortcut_info_.user_title); 173 weak_observer_->OnUserTitleAvailable(shortcut_info_.user_title);
174 174
175 if (data.icon && !data.icon->drawsNothing()) { 175 if (data.icon && !data.icon->drawsNothing()) {
176 // TODO(hanxi): implement WebAPK path if shortcut_info_.url has a secure 176 // TODO(hanxi): implement WebAPK path if shortcut_info_.url has a secure
177 // scheme and data.is_installable is true. 177 // scheme and data.is_installable is true.
178 NotifyObserver(data.icon_url, *(data.icon)); 178 shortcut_info_.icon_url = data.icon_url;
179
180 // base::Bind copies the arguments internally, so it is safe to pass it
181 // data.icon (which is not owned by us).
182 content::BrowserThread::GetBlockingPool()
183 ->PostWorkerTaskWithShutdownBehavior(
184 FROM_HERE,
185 base::Bind(
186 &AddToHomescreenDataFetcher::CreateLauncherIconInBackground,
187 this, *(data.icon)),
188 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN);
179 return; 189 return;
180 } 190 }
181 191
182 FetchFavicon(); 192 FetchFavicon();
183 } 193 }
184 194
185 bool AddToHomescreenDataFetcher::OnMessageReceived( 195 bool AddToHomescreenDataFetcher::OnMessageReceived(
186 const IPC::Message& message) { 196 const IPC::Message& message) {
187 if (!is_waiting_for_web_application_info_) 197 if (!is_waiting_for_web_application_info_)
188 return false; 198 return false;
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
225 Profile::FromBrowserContext(web_contents()->GetBrowserContext()), 235 Profile::FromBrowserContext(web_contents()->GetBrowserContext()),
226 ServiceAccessType::EXPLICIT_ACCESS); 236 ServiceAccessType::EXPLICIT_ACCESS);
227 237
228 // Using favicon if its size is not smaller than platform required size, 238 // Using favicon if its size is not smaller than platform required size,
229 // otherwise using the largest icon among all avaliable icons. 239 // otherwise using the largest icon among all avaliable icons.
230 int ideal_icon_size_in_px = 240 int ideal_icon_size_in_px =
231 ideal_icon_size_in_dp_ * 241 ideal_icon_size_in_dp_ *
232 display::Screen::GetScreen()->GetPrimaryDisplay().device_scale_factor(); 242 display::Screen::GetScreen()->GetPrimaryDisplay().device_scale_factor();
233 int threshold_to_get_any_largest_icon = ideal_icon_size_in_px - 1; 243 int threshold_to_get_any_largest_icon = ideal_icon_size_in_px - 1;
234 favicon_service->GetLargestRawFaviconForPageURL( 244 favicon_service->GetLargestRawFaviconForPageURL(
235 shortcut_info_.url, 245 shortcut_info_.url, icon_types, threshold_to_get_any_largest_icon,
236 icon_types,
237 threshold_to_get_any_largest_icon,
238 base::Bind(&AddToHomescreenDataFetcher::OnFaviconFetched, this), 246 base::Bind(&AddToHomescreenDataFetcher::OnFaviconFetched, this),
239 &favicon_task_tracker_); 247 &favicon_task_tracker_);
240 } 248 }
241 249
242 void AddToHomescreenDataFetcher::OnFaviconFetched( 250 void AddToHomescreenDataFetcher::OnFaviconFetched(
243 const favicon_base::FaviconRawBitmapResult& bitmap_result) { 251 const favicon_base::FaviconRawBitmapResult& bitmap_result) {
244 if (!web_contents() || !weak_observer_ || is_icon_saved_) 252 if (!web_contents() || !weak_observer_ || is_icon_saved_)
245 return; 253 return;
246 254
247 content::BrowserThread::GetBlockingPool()->PostWorkerTaskWithShutdownBehavior( 255 content::BrowserThread::GetBlockingPool()->PostWorkerTaskWithShutdownBehavior(
248 FROM_HERE, 256 FROM_HERE, base::Bind(&AddToHomescreenDataFetcher::
249 base::Bind(&AddToHomescreenDataFetcher::CreateLauncherIconInBackground, 257 CreateLauncherIconFromFaviconInBackground,
250 this, shortcut_info_.url, bitmap_result), 258 this, bitmap_result),
251 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN); 259 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN);
252 } 260 }
253 261
254 void AddToHomescreenDataFetcher::CreateLauncherIconInBackground( 262 void AddToHomescreenDataFetcher::CreateLauncherIconFromFaviconInBackground(
255 const GURL& page_url,
256 const favicon_base::FaviconRawBitmapResult& bitmap_result) { 263 const favicon_base::FaviconRawBitmapResult& bitmap_result) {
257 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); 264 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread());
258 265
259 SkBitmap icon_bitmap; 266 SkBitmap raw_icon;
260 if (bitmap_result.is_valid()) { 267 if (bitmap_result.is_valid()) {
261 gfx::PNGCodec::Decode(bitmap_result.bitmap_data->front(), 268 gfx::PNGCodec::Decode(bitmap_result.bitmap_data->front(),
262 bitmap_result.bitmap_data->size(), 269 bitmap_result.bitmap_data->size(), &raw_icon);
263 &icon_bitmap);
264 } 270 }
265 271
272 shortcut_info_.icon_url = bitmap_result.icon_url;
273 CreateLauncherIconInBackground(raw_icon);
274 }
275
276 void AddToHomescreenDataFetcher::CreateLauncherIconInBackground(
277 const SkBitmap& raw_icon) {
278 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread());
279
280 SkBitmap icon;
266 bool is_generated = false; 281 bool is_generated = false;
267 if (weak_observer_) { 282 if (weak_observer_) {
268 icon_bitmap = weak_observer_->FinalizeLauncherIconInBackground( 283 icon = weak_observer_->FinalizeLauncherIconInBackground(
269 icon_bitmap, page_url, &is_generated); 284 raw_icon, shortcut_info_.url, &is_generated);
270 } 285 }
271 286
272 GURL icon_url = is_generated ? GURL() : bitmap_result.icon_url; 287 if (is_generated)
288 shortcut_info_.icon_url = GURL();
289
273 content::BrowserThread::PostTask( 290 content::BrowserThread::PostTask(
274 content::BrowserThread::UI, FROM_HERE, 291 content::BrowserThread::UI, FROM_HERE,
275 base::Bind(&AddToHomescreenDataFetcher::NotifyObserver, this, icon_url, 292 base::Bind(&AddToHomescreenDataFetcher::NotifyObserver, this, icon));
276 icon_bitmap));
277 } 293 }
278 294
279 void AddToHomescreenDataFetcher::NotifyObserver(const GURL& icon_url, 295 void AddToHomescreenDataFetcher::NotifyObserver(const SkBitmap& icon) {
280 const SkBitmap& bitmap) {
281 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 296 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
282 if (!web_contents() || !weak_observer_ || is_icon_saved_) 297 if (!web_contents() || !weak_observer_ || is_icon_saved_)
283 return; 298 return;
284 299
285 is_icon_saved_ = true; 300 is_icon_saved_ = true;
286 shortcut_info_.icon_url = icon_url; 301 shortcut_icon_ = icon;
287 shortcut_icon_ = bitmap;
288 is_ready_ = true; 302 is_ready_ = true;
289 weak_observer_->OnDataAvailable(shortcut_info_, shortcut_icon_); 303 weak_observer_->OnDataAvailable(shortcut_info_, shortcut_icon_);
290 } 304 }
OLDNEW
« no previous file with comments | « chrome/browser/android/webapps/add_to_homescreen_data_fetcher.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698