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

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: Make ShortcutHelper use icon download class too 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);
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(
339 const SkBitmap* icon) { 309 content::WebContents* web_contents) {
340 if (is_active_) 310 GURL icon_url =
341 RequestShowBanner(icon); 311 ManifestIconSelector::FindBestMatchingIcon(
312 web_app_data_.icons,
313 ideal_icon_size_,
314 gfx::Screen::GetScreenFor(web_contents->GetNativeView()));
342 315
343 Release(); 316 if (FetchAppIcon(icon_url)) return;
317
318 OutputDeveloperNotShownMessage(web_contents, kCannotDetermineBestIcon);
319 Cancel();
344 } 320 }
345 321
346 bool AppBannerDataFetcher::IsWebAppInstalled( 322 bool AppBannerDataFetcher::FetchAppIcon(const GURL& icon_url) {
347 content::BrowserContext* browser_context, 323 return icon_downloader_->Download(
348 const GURL& start_url) { 324 icon_url,
349 return false; 325 ideal_icon_size_,
326 base::Bind(&AppBannerDataFetcher::OnAppIconFetched,
327 this));
350 } 328 }
351 329
352 void AppBannerDataFetcher::RequestShowBanner(const SkBitmap* icon) { 330 void AppBannerDataFetcher::OnAppIconFetched(const SkBitmap& bitmap) {
331 if (!is_active_) return;
332
353 content::WebContents* web_contents = GetWebContents(); 333 content::WebContents* web_contents = GetWebContents();
354 if (!CheckFetcherIsStillAlive(web_contents)) { 334 if (!CheckFetcherIsStillAlive(web_contents)) {
355 Cancel(); 335 Cancel();
356 return; 336 return;
357 } 337 }
358 if (!icon) { 338 if (bitmap.drawsNothing()) {
359 OutputDeveloperNotShownMessage(web_contents, kNoIconAvailable); 339 OutputDeveloperNotShownMessage(web_contents, kNoIconAvailable);
360 Cancel(); 340 Cancel();
361 return; 341 return;
362 } 342 }
gone 2015/08/05 17:34:46 nit: put this newline back. the RecordCouldShowBa
363
364 RecordCouldShowBanner(); 343 RecordCouldShowBanner();
365 if (!CheckIfShouldShowBanner()) { 344 if (!CheckIfShouldShowBanner()) {
366 // At this point, the only possible case is that the banner has been added 345 // 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. 346 // to the homescreen, given all of the other checks that have been made.
368 OutputDeveloperNotShownMessage(web_contents, kBannerAlreadyAdded); 347 OutputDeveloperNotShownMessage(web_contents, kBannerAlreadyAdded);
369 Cancel(); 348 Cancel();
370 return; 349 return;
371 } 350 }
372 351
373 app_icon_.reset(new SkBitmap(*icon)); 352 app_icon_.reset(new SkBitmap(bitmap));
374 event_request_id_ = ++gCurrentRequestID; 353 event_request_id_ = ++gCurrentRequestID;
375 web_contents->GetMainFrame()->Send( 354 web_contents->GetMainFrame()->Send(
376 new ChromeViewMsg_AppBannerPromptRequest( 355 new ChromeViewMsg_AppBannerPromptRequest(
377 web_contents->GetMainFrame()->GetRoutingID(), 356 web_contents->GetMainFrame()->GetRoutingID(),
378 event_request_id_, 357 event_request_id_,
379 GetBannerType())); 358 GetBannerType()));
359 }
360
361 bool AppBannerDataFetcher::IsWebAppInstalled(
362 content::BrowserContext* browser_context,
363 const GURL& start_url) {
364 return false;
380 } 365 }
381 366
382 void AppBannerDataFetcher::RecordCouldShowBanner() { 367 void AppBannerDataFetcher::RecordCouldShowBanner() {
383 content::WebContents* web_contents = GetWebContents(); 368 content::WebContents* web_contents = GetWebContents();
384 DCHECK(web_contents); 369 DCHECK(web_contents);
385 370
386 AppBannerSettingsHelper::RecordBannerCouldShowEvent( 371 AppBannerSettingsHelper::RecordBannerCouldShowEvent(
387 web_contents, validated_url_, GetAppIdentifier(), 372 web_contents, validated_url_, GetAppIdentifier(),
388 GetCurrentTime(), transition_type_); 373 GetCurrentTime(), transition_type_);
389 } 374 }
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
427 return false; 412 return false;
428 } 413 }
429 if (!DoesManifestContainRequiredIcon(manifest)) { 414 if (!DoesManifestContainRequiredIcon(manifest)) {
430 OutputDeveloperNotShownMessage(web_contents, kManifestMissingSuitableIcon); 415 OutputDeveloperNotShownMessage(web_contents, kManifestMissingSuitableIcon);
431 return false; 416 return false;
432 } 417 }
433 return true; 418 return true;
434 } 419 }
435 420
436 } // namespace banners 421 } // namespace banners
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698