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

Side by Side Diff: chrome/browser/banners/app_banner_data_fetcher.cc

Issue 1261143004: Implement manifest icon downloader (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add scaling to non correct icons Created 5 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
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/banners/app_banner_data_fetcher.h" 5 #include "chrome/browser/banners/app_banner_data_fetcher.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/strings/string_util.h" 8 #include "base/strings/string_util.h"
9 #include "base/strings/utf_string_conversions.h" 9 #include "base/strings/utf_string_conversions.h"
10 #include "chrome/browser/banners/app_banner_debug_log.h" 10 #include "chrome/browser/banners/app_banner_debug_log.h"
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
72 AppBannerDataFetcher::AppBannerDataFetcher( 72 AppBannerDataFetcher::AppBannerDataFetcher(
73 content::WebContents* web_contents, 73 content::WebContents* web_contents,
74 base::WeakPtr<Delegate> delegate, 74 base::WeakPtr<Delegate> delegate,
75 int ideal_icon_size) 75 int ideal_icon_size)
76 : WebContentsObserver(web_contents), 76 : WebContentsObserver(web_contents),
77 ideal_icon_size_(ideal_icon_size), 77 ideal_icon_size_(ideal_icon_size),
78 weak_delegate_(delegate), 78 weak_delegate_(delegate),
79 is_active_(false), 79 is_active_(false),
80 was_canceled_by_page_(false), 80 was_canceled_by_page_(false),
81 page_requested_prompt_(false), 81 page_requested_prompt_(false),
82 event_request_id_(-1) { 82 event_request_id_(-1),
83 icon_downloader_(new ManifestIconDownloader(web_contents)) {
83 } 84 }
84 85
85 void AppBannerDataFetcher::Start(const GURL& validated_url, 86 void AppBannerDataFetcher::Start(const GURL& validated_url,
86 ui::PageTransition transition_type) { 87 ui::PageTransition transition_type) {
87 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 88 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
88 89
89 content::WebContents* web_contents = GetWebContents(); 90 content::WebContents* web_contents = GetWebContents();
90 DCHECK(web_contents); 91 DCHECK(web_contents);
91 92
92 is_active_ = true; 93 is_active_ = true;
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after
211 if (!web_contents() || web_contents()->IsBeingDestroyed()) 212 if (!web_contents() || web_contents()->IsBeingDestroyed())
212 return nullptr; 213 return nullptr;
213 return web_contents(); 214 return web_contents();
214 } 215 }
215 216
216 std::string AppBannerDataFetcher::GetAppIdentifier() { 217 std::string AppBannerDataFetcher::GetAppIdentifier() {
217 DCHECK(!web_app_data_.IsEmpty()); 218 DCHECK(!web_app_data_.IsEmpty());
218 return web_app_data_.start_url.spec(); 219 return web_app_data_.start_url.spec();
219 } 220 }
220 221
221 bool AppBannerDataFetcher::FetchIcon(const GURL& image_url) {
222 content::WebContents* web_contents = GetWebContents();
223 DCHECK(web_contents);
224
225 // Begin asynchronously fetching the app icon. AddRef() is done before the
226 // fetch to ensure that the AppBannerDataFetcher isn't deleted before the
227 // BitmapFetcher has called OnFetchComplete() (where the references are
228 // decremented).
229 AddRef();
230 Profile* profile =
231 Profile::FromBrowserContext(web_contents->GetBrowserContext());
232 bitmap_fetcher_.reset(new chrome::BitmapFetcher(image_url, this));
233 bitmap_fetcher_->Init(
234 profile->GetRequestContext(),
235 std::string(),
236 net::URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE,
237 net::LOAD_NORMAL);
238 bitmap_fetcher_->Start();
239 return true;
240 }
241
242 void AppBannerDataFetcher::RecordDidShowBanner(const std::string& event_name) { 222 void AppBannerDataFetcher::RecordDidShowBanner(const std::string& event_name) {
243 content::WebContents* web_contents = GetWebContents(); 223 content::WebContents* web_contents = GetWebContents();
244 DCHECK(web_contents); 224 DCHECK(web_contents);
245 225
246 AppBannerSettingsHelper::RecordBannerEvent( 226 AppBannerSettingsHelper::RecordBannerEvent(
247 web_contents, validated_url_, GetAppIdentifier(), 227 web_contents, validated_url_, GetAppIdentifier(),
248 AppBannerSettingsHelper::APP_BANNER_EVENT_DID_SHOW, 228 AppBannerSettingsHelper::APP_BANNER_EVENT_DID_SHOW,
249 GetCurrentTime()); 229 GetCurrentTime());
250 rappor::SampleDomainAndRegistryFromGURL(g_browser_process->rappor_service(), 230 rappor::SampleDomainAndRegistryFromGURL(g_browser_process->rappor_service(),
251 event_name, 231 event_name,
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
309 289
310 void AppBannerDataFetcher::OnDidCheckHasServiceWorker( 290 void AppBannerDataFetcher::OnDidCheckHasServiceWorker(
311 bool has_service_worker) { 291 bool has_service_worker) {
312 content::WebContents* web_contents = GetWebContents(); 292 content::WebContents* web_contents = GetWebContents();
313 if (!CheckFetcherIsStillAlive(web_contents)) { 293 if (!CheckFetcherIsStillAlive(web_contents)) {
314 Cancel(); 294 Cancel();
315 return; 295 return;
316 } 296 }
317 297
318 if (has_service_worker) { 298 if (has_service_worker) {
319 // Create an infobar to promote the manifest's app. 299 OnHasServiceWorker(web_contents);
gone 2015/07/31 18:09:19 Not sure why you pulled this out. The function wa
Lalit Maganti 2015/08/03 08:58:29 For future use.
gone 2015/08/03 23:27:58 Is the future use going to be a bound callback?
Lalit Maganti 2015/08/04 08:59:52 No. I plan to override the method and use it to do
320 GURL icon_url = 300 return;
321 ManifestIconSelector::FindBestMatchingIcon(
322 web_app_data_.icons,
323 ideal_icon_size_,
324 gfx::Screen::GetScreenFor(web_contents->GetNativeView()));
325 if (!icon_url.is_empty()) {
326 FetchIcon(icon_url);
327 return;
328 }
329 OutputDeveloperNotShownMessage(web_contents, kCannotDetermineBestIcon);
330 } else {
331 TrackDisplayEvent(DISPLAY_EVENT_LACKS_SERVICE_WORKER);
332 OutputDeveloperNotShownMessage(web_contents, kNoMatchingServiceWorker);
333 } 301 }
334 302
303 TrackDisplayEvent(DISPLAY_EVENT_LACKS_SERVICE_WORKER);
304 OutputDeveloperNotShownMessage(web_contents, kNoMatchingServiceWorker);
335 Cancel(); 305 Cancel();
336 } 306 }
337 307
338 void AppBannerDataFetcher::OnFetchComplete(const GURL& url, 308 void AppBannerDataFetcher::OnHasServiceWorker(content::WebContents* web_contents ) {
339 const SkBitmap* icon) { 309 GURL icon_url =
340 if (is_active_) 310 ManifestIconSelector::FindBestMatchingIcon(
341 RequestShowBanner(icon); 311 web_app_data_.icons,
312 ideal_icon_size_,
313 gfx::Screen::GetScreenFor(web_contents->GetNativeView()));
342 314
343 Release(); 315 if (FetchAppIcon(web_contents, icon_url)) return;
316
317 OutputDeveloperNotShownMessage(web_contents, kCannotDetermineBestIcon);
318 Cancel();
319 }
320
321 bool AppBannerDataFetcher::FetchAppIcon(content::WebContents* web_contents,
gone 2015/07/31 18:09:20 Was there anything else that was going to call thi
Lalit Maganti 2015/08/03 08:58:28 For future use.
322 const GURL& icon_url) {
323 return icon_downloader_->Download(
324 icon_url,
325 ideal_icon_size_,
326 base::Bind(&AppBannerDataFetcher::OnAppIconFetched,
327 this));
328 }
329
330 void AppBannerDataFetcher::OnAppIconFetched(const SkBitmap& bitmap) {
331 if (!is_active_) return;
332
333 content::WebContents* web_contents = GetWebContents();
334 if (!CheckFetcherIsStillAlive(web_contents)) {
335 Cancel();
336 return;
337 }
338 if (bitmap.drawsNothing()) {
339 OutputDeveloperNotShownMessage(web_contents, kNoIconAvailable);
340 Cancel();
341 return;
342 }
343
344 RequestShowBanner(web_contents, bitmap);
344 } 345 }
345 346
346 bool AppBannerDataFetcher::IsWebAppInstalled( 347 bool AppBannerDataFetcher::IsWebAppInstalled(
347 content::BrowserContext* browser_context, 348 content::BrowserContext* browser_context,
348 const GURL& start_url) { 349 const GURL& start_url) {
349 return false; 350 return false;
350 } 351 }
351 352
352 void AppBannerDataFetcher::RequestShowBanner(const SkBitmap* icon) { 353 void AppBannerDataFetcher::RequestShowBanner(content::WebContents* web_contents,
353 content::WebContents* web_contents = GetWebContents(); 354 const SkBitmap& icon) {
gone 2015/07/31 18:09:20 * Why can't you call GetWebContents() here? That'
Lalit Maganti 2015/08/03 08:58:28 1. I can I guess. 2. Yeah I don't need this so I c
Lalit Maganti 2015/08/04 17:26:45 Done.
354 if (!CheckFetcherIsStillAlive(web_contents)) {
355 Cancel();
356 return;
357 }
358 if (!icon) {
359 OutputDeveloperNotShownMessage(web_contents, kNoIconAvailable);
360 Cancel();
361 return;
362 }
363
364 RecordCouldShowBanner(); 355 RecordCouldShowBanner();
365 if (!CheckIfShouldShowBanner()) { 356 if (!CheckIfShouldShowBanner()) {
366 // At this point, the only possible case is that the banner has been added 357 // At this point, the only possible case is that the banner has been added
367 // to the homescreen, given all of the other checks that have been made. 358 // to the homescreen, given all of the other checks that have been made.
368 OutputDeveloperNotShownMessage(web_contents, kBannerAlreadyAdded); 359 OutputDeveloperNotShownMessage(web_contents, kBannerAlreadyAdded);
369 Cancel(); 360 Cancel();
370 return; 361 return;
371 } 362 }
372 363
373 app_icon_.reset(new SkBitmap(*icon)); 364 app_icon_.reset(new SkBitmap(icon));
374 event_request_id_ = ++gCurrentRequestID; 365 event_request_id_ = ++gCurrentRequestID;
375 web_contents->GetMainFrame()->Send( 366 web_contents->GetMainFrame()->Send(
376 new ChromeViewMsg_AppBannerPromptRequest( 367 new ChromeViewMsg_AppBannerPromptRequest(
377 web_contents->GetMainFrame()->GetRoutingID(), 368 web_contents->GetMainFrame()->GetRoutingID(),
378 event_request_id_, 369 event_request_id_,
379 GetBannerType())); 370 GetBannerType()));
380 } 371 }
381 372
382 void AppBannerDataFetcher::RecordCouldShowBanner() { 373 void AppBannerDataFetcher::RecordCouldShowBanner() {
383 content::WebContents* web_contents = GetWebContents(); 374 content::WebContents* web_contents = GetWebContents();
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
427 return false; 418 return false;
428 } 419 }
429 if (!DoesManifestContainRequiredIcon(manifest)) { 420 if (!DoesManifestContainRequiredIcon(manifest)) {
430 OutputDeveloperNotShownMessage(web_contents, kManifestMissingSuitableIcon); 421 OutputDeveloperNotShownMessage(web_contents, kManifestMissingSuitableIcon);
431 return false; 422 return false;
432 } 423 }
433 return true; 424 return true;
434 } 425 }
435 426
436 } // namespace banners 427 } // namespace banners
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698