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

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: Address comments on review 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/command_line.h" 8 #include "base/command_line.h"
9 #include "base/strings/string_util.h" 9 #include "base/strings/string_util.h"
10 #include "base/strings/utf_string_conversions.h" 10 #include "base/strings/utf_string_conversions.h"
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
74 AppBannerDataFetcher::AppBannerDataFetcher( 74 AppBannerDataFetcher::AppBannerDataFetcher(
75 content::WebContents* web_contents, 75 content::WebContents* web_contents,
76 base::WeakPtr<Delegate> delegate, 76 base::WeakPtr<Delegate> delegate,
77 int ideal_icon_size) 77 int ideal_icon_size)
78 : WebContentsObserver(web_contents), 78 : WebContentsObserver(web_contents),
79 ideal_icon_size_(ideal_icon_size), 79 ideal_icon_size_(ideal_icon_size),
80 weak_delegate_(delegate), 80 weak_delegate_(delegate),
81 is_active_(false), 81 is_active_(false),
82 was_canceled_by_page_(false), 82 was_canceled_by_page_(false),
83 page_requested_prompt_(false), 83 page_requested_prompt_(false),
84 event_request_id_(-1) { 84 event_request_id_(-1),
85 icon_downloader_(new ManifestIconDownloader(web_contents)) {
85 } 86 }
86 87
87 void AppBannerDataFetcher::Start(const GURL& validated_url, 88 void AppBannerDataFetcher::Start(const GURL& validated_url,
88 ui::PageTransition transition_type) { 89 ui::PageTransition transition_type) {
89 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 90 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
90 91
91 content::WebContents* web_contents = GetWebContents(); 92 content::WebContents* web_contents = GetWebContents();
92 DCHECK(web_contents); 93 DCHECK(web_contents);
93 94
94 is_active_ = true; 95 is_active_ = true;
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after
213 if (!web_contents() || web_contents()->IsBeingDestroyed()) 214 if (!web_contents() || web_contents()->IsBeingDestroyed())
214 return nullptr; 215 return nullptr;
215 return web_contents(); 216 return web_contents();
216 } 217 }
217 218
218 std::string AppBannerDataFetcher::GetAppIdentifier() { 219 std::string AppBannerDataFetcher::GetAppIdentifier() {
219 DCHECK(!web_app_data_.IsEmpty()); 220 DCHECK(!web_app_data_.IsEmpty());
220 return web_app_data_.start_url.spec(); 221 return web_app_data_.start_url.spec();
221 } 222 }
222 223
223 bool AppBannerDataFetcher::FetchIcon(const GURL& image_url) {
224 content::WebContents* web_contents = GetWebContents();
225 DCHECK(web_contents);
226
227 // Begin asynchronously fetching the app icon. AddRef() is done before the
228 // fetch to ensure that the AppBannerDataFetcher isn't deleted before the
229 // BitmapFetcher has called OnFetchComplete() (where the references are
230 // decremented).
231 AddRef();
232 Profile* profile =
233 Profile::FromBrowserContext(web_contents->GetBrowserContext());
234 bitmap_fetcher_.reset(new chrome::BitmapFetcher(image_url, this));
235 bitmap_fetcher_->Init(
236 profile->GetRequestContext(),
237 std::string(),
238 net::URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE,
239 net::LOAD_NORMAL);
240 bitmap_fetcher_->Start();
241 return true;
242 }
243
244 void AppBannerDataFetcher::RecordDidShowBanner(const std::string& event_name) { 224 void AppBannerDataFetcher::RecordDidShowBanner(const std::string& event_name) {
245 content::WebContents* web_contents = GetWebContents(); 225 content::WebContents* web_contents = GetWebContents();
246 DCHECK(web_contents); 226 DCHECK(web_contents);
247 227
248 AppBannerSettingsHelper::RecordBannerEvent( 228 AppBannerSettingsHelper::RecordBannerEvent(
249 web_contents, validated_url_, GetAppIdentifier(), 229 web_contents, validated_url_, GetAppIdentifier(),
250 AppBannerSettingsHelper::APP_BANNER_EVENT_DID_SHOW, 230 AppBannerSettingsHelper::APP_BANNER_EVENT_DID_SHOW,
251 GetCurrentTime()); 231 GetCurrentTime());
252 rappor::SampleDomainAndRegistryFromGURL(g_browser_process->rappor_service(), 232 rappor::SampleDomainAndRegistryFromGURL(g_browser_process->rappor_service(),
253 event_name, 233 event_name,
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
312 } 292 }
313 293
314 void AppBannerDataFetcher::OnDidCheckHasServiceWorker( 294 void AppBannerDataFetcher::OnDidCheckHasServiceWorker(
315 bool has_service_worker) { 295 bool has_service_worker) {
316 content::WebContents* web_contents = GetWebContents(); 296 content::WebContents* web_contents = GetWebContents();
317 if (!CheckFetcherIsStillAlive(web_contents)) { 297 if (!CheckFetcherIsStillAlive(web_contents)) {
318 Cancel(); 298 Cancel();
319 return; 299 return;
320 } 300 }
321 301
322 if (has_service_worker) { 302 if (!has_service_worker) {
323 // Create an infobar to promote the manifest's app.
324 GURL icon_url =
325 ManifestIconSelector::FindBestMatchingIcon(
326 web_app_data_.icons,
327 ideal_icon_size_,
328 gfx::Screen::GetScreenFor(web_contents->GetNativeView()));
329 if (!icon_url.is_empty()) {
330 FetchIcon(icon_url);
331 return;
332 }
333 OutputDeveloperNotShownMessage(web_contents, kCannotDetermineBestIcon);
334 } else {
335 TrackDisplayEvent(DISPLAY_EVENT_LACKS_SERVICE_WORKER); 303 TrackDisplayEvent(DISPLAY_EVENT_LACKS_SERVICE_WORKER);
336 OutputDeveloperNotShownMessage(web_contents, kNoMatchingServiceWorker); 304 OutputDeveloperNotShownMessage(web_contents, kNoMatchingServiceWorker);
305 Cancel();
306 return;
337 } 307 }
338 308
339 Cancel(); 309 OnHasServiceWorker(web_contents);
340 } 310 }
341 311
342 void AppBannerDataFetcher::OnFetchComplete(const GURL& url, 312 void AppBannerDataFetcher::OnHasServiceWorker(
343 const SkBitmap* icon) { 313 content::WebContents* web_contents) {
344 if (is_active_) 314 GURL icon_url =
345 RequestShowBanner(icon); 315 ManifestIconSelector::FindBestMatchingIcon(
316 web_app_data_.icons,
317 ideal_icon_size_,
318 gfx::Screen::GetScreenFor(web_contents->GetNativeView()));
346 319
347 Release(); 320 if (!FetchAppIcon(icon_url)) {
321 OutputDeveloperNotShownMessage(web_contents, kCannotDetermineBestIcon);
322 Cancel();
323 }
348 } 324 }
349 325
350 bool AppBannerDataFetcher::IsWebAppInstalled( 326 bool AppBannerDataFetcher::FetchAppIcon(const GURL& icon_url) {
351 content::BrowserContext* browser_context, 327 return icon_downloader_->Download(
352 const GURL& start_url) { 328 icon_url,
353 return false; 329 ideal_icon_size_,
330 base::Bind(&AppBannerDataFetcher::OnAppIconFetched,
331 this));
354 } 332 }
355 333
356 void AppBannerDataFetcher::RequestShowBanner(const SkBitmap* icon) { 334 void AppBannerDataFetcher::OnAppIconFetched(const SkBitmap& bitmap) {
335 if (!is_active_) return;
336
357 content::WebContents* web_contents = GetWebContents(); 337 content::WebContents* web_contents = GetWebContents();
358 if (!CheckFetcherIsStillAlive(web_contents)) { 338 if (!CheckFetcherIsStillAlive(web_contents)) {
359 Cancel(); 339 Cancel();
360 return; 340 return;
361 } 341 }
362 if (!icon) { 342 if (bitmap.drawsNothing()) {
363 OutputDeveloperNotShownMessage(web_contents, kNoIconAvailable); 343 OutputDeveloperNotShownMessage(web_contents, kNoIconAvailable);
364 Cancel(); 344 Cancel();
365 return; 345 return;
366 } 346 }
367 347
368 RecordCouldShowBanner(); 348 RecordCouldShowBanner();
369 if (!CheckIfShouldShowBanner()) { 349 if (!CheckIfShouldShowBanner()) {
370 // At this point, the only possible case is that the banner has been added 350 // At this point, the only possible case is that the banner has been added
371 // to the homescreen, given all of the other checks that have been made. 351 // to the homescreen, given all of the other checks that have been made.
372 OutputDeveloperNotShownMessage(web_contents, kBannerAlreadyAdded); 352 OutputDeveloperNotShownMessage(web_contents, kBannerAlreadyAdded);
373 Cancel(); 353 Cancel();
374 return; 354 return;
375 } 355 }
376 356
377 app_icon_.reset(new SkBitmap(*icon)); 357 app_icon_.reset(new SkBitmap(bitmap));
378 event_request_id_ = ++gCurrentRequestID; 358 event_request_id_ = ++gCurrentRequestID;
379 web_contents->GetMainFrame()->Send( 359 web_contents->GetMainFrame()->Send(
380 new ChromeViewMsg_AppBannerPromptRequest( 360 new ChromeViewMsg_AppBannerPromptRequest(
381 web_contents->GetMainFrame()->GetRoutingID(), 361 web_contents->GetMainFrame()->GetRoutingID(),
382 event_request_id_, 362 event_request_id_,
383 GetBannerType())); 363 GetBannerType()));
364 }
365
366 bool AppBannerDataFetcher::IsWebAppInstalled(
367 content::BrowserContext* browser_context,
368 const GURL& start_url) {
369 return false;
384 } 370 }
385 371
386 void AppBannerDataFetcher::RecordCouldShowBanner() { 372 void AppBannerDataFetcher::RecordCouldShowBanner() {
387 content::WebContents* web_contents = GetWebContents(); 373 content::WebContents* web_contents = GetWebContents();
388 DCHECK(web_contents); 374 DCHECK(web_contents);
389 375
390 AppBannerSettingsHelper::RecordBannerCouldShowEvent( 376 AppBannerSettingsHelper::RecordBannerCouldShowEvent(
391 web_contents, validated_url_, GetAppIdentifier(), 377 web_contents, validated_url_, GetAppIdentifier(),
392 GetCurrentTime(), transition_type_); 378 GetCurrentTime(), transition_type_);
393 } 379 }
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
431 return false; 417 return false;
432 } 418 }
433 if (!DoesManifestContainRequiredIcon(manifest)) { 419 if (!DoesManifestContainRequiredIcon(manifest)) {
434 OutputDeveloperNotShownMessage(web_contents, kManifestMissingSuitableIcon); 420 OutputDeveloperNotShownMessage(web_contents, kManifestMissingSuitableIcon);
435 return false; 421 return false;
436 } 422 }
437 return true; 423 return true;
438 } 424 }
439 425
440 } // namespace banners 426 } // namespace banners
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698