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

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: Rebase 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"
11 #include "chrome/browser/banners/app_banner_debug_log.h" 11 #include "chrome/browser/banners/app_banner_debug_log.h"
12 #include "chrome/browser/banners/app_banner_metrics.h" 12 #include "chrome/browser/banners/app_banner_metrics.h"
13 #include "chrome/browser/banners/app_banner_settings_helper.h" 13 #include "chrome/browser/banners/app_banner_settings_helper.h"
14 #include "chrome/browser/bitmap_fetcher/bitmap_fetcher.h"
15 #include "chrome/browser/browser_process.h" 14 #include "chrome/browser/browser_process.h"
15 #include "chrome/browser/manifest/manifest_icon_downloader.h"
16 #include "chrome/browser/manifest/manifest_icon_selector.h" 16 #include "chrome/browser/manifest/manifest_icon_selector.h"
17 #include "chrome/browser/profiles/profile.h" 17 #include "chrome/browser/profiles/profile.h"
18 #include "chrome/common/chrome_switches.h" 18 #include "chrome/common/chrome_switches.h"
19 #include "chrome/common/render_messages.h" 19 #include "chrome/common/render_messages.h"
20 #include "components/rappor/rappor_utils.h" 20 #include "components/rappor/rappor_utils.h"
21 #include "content/public/browser/browser_context.h" 21 #include "content/public/browser/browser_context.h"
22 #include "content/public/browser/browser_thread.h" 22 #include "content/public/browser/browser_thread.h"
23 #include "content/public/browser/navigation_details.h" 23 #include "content/public/browser/navigation_details.h"
24 #include "content/public/browser/render_frame_host.h" 24 #include "content/public/browser/render_frame_host.h"
25 #include "content/public/browser/service_worker_context.h" 25 #include "content/public/browser/service_worker_context.h"
(...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after
213 if (!web_contents() || web_contents()->IsBeingDestroyed()) 213 if (!web_contents() || web_contents()->IsBeingDestroyed())
214 return nullptr; 214 return nullptr;
215 return web_contents(); 215 return web_contents();
216 } 216 }
217 217
218 std::string AppBannerDataFetcher::GetAppIdentifier() { 218 std::string AppBannerDataFetcher::GetAppIdentifier() {
219 DCHECK(!web_app_data_.IsEmpty()); 219 DCHECK(!web_app_data_.IsEmpty());
220 return web_app_data_.start_url.spec(); 220 return web_app_data_.start_url.spec();
221 } 221 }
222 222
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) { 223 void AppBannerDataFetcher::RecordDidShowBanner(const std::string& event_name) {
245 content::WebContents* web_contents = GetWebContents(); 224 content::WebContents* web_contents = GetWebContents();
246 DCHECK(web_contents); 225 DCHECK(web_contents);
247 226
248 AppBannerSettingsHelper::RecordBannerEvent( 227 AppBannerSettingsHelper::RecordBannerEvent(
249 web_contents, validated_url_, GetAppIdentifier(), 228 web_contents, validated_url_, GetAppIdentifier(),
250 AppBannerSettingsHelper::APP_BANNER_EVENT_DID_SHOW, 229 AppBannerSettingsHelper::APP_BANNER_EVENT_DID_SHOW,
251 GetCurrentTime()); 230 GetCurrentTime());
252 rappor::SampleDomainAndRegistryFromGURL(g_browser_process->rappor_service(), 231 rappor::SampleDomainAndRegistryFromGURL(g_browser_process->rappor_service(),
253 event_name, 232 event_name,
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
312 } 291 }
313 292
314 void AppBannerDataFetcher::OnDidCheckHasServiceWorker( 293 void AppBannerDataFetcher::OnDidCheckHasServiceWorker(
315 bool has_service_worker) { 294 bool has_service_worker) {
316 content::WebContents* web_contents = GetWebContents(); 295 content::WebContents* web_contents = GetWebContents();
317 if (!CheckFetcherIsStillAlive(web_contents)) { 296 if (!CheckFetcherIsStillAlive(web_contents)) {
318 Cancel(); 297 Cancel();
319 return; 298 return;
320 } 299 }
321 300
322 if (has_service_worker) { 301 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); 302 TrackDisplayEvent(DISPLAY_EVENT_LACKS_SERVICE_WORKER);
336 OutputDeveloperNotShownMessage(web_contents, kNoMatchingServiceWorker); 303 OutputDeveloperNotShownMessage(web_contents, kNoMatchingServiceWorker);
304 Cancel();
305 return;
337 } 306 }
338 307
339 Cancel(); 308 OnHasServiceWorker(web_contents);
340 } 309 }
341 310
342 void AppBannerDataFetcher::OnFetchComplete(const GURL& url, 311 void AppBannerDataFetcher::OnHasServiceWorker(
343 const SkBitmap* icon) { 312 content::WebContents* web_contents) {
344 if (is_active_) 313 GURL icon_url =
345 RequestShowBanner(icon); 314 ManifestIconSelector::FindBestMatchingIcon(
315 web_app_data_.icons,
316 ideal_icon_size_,
317 gfx::Screen::GetScreenFor(web_contents->GetNativeView()));
346 318
347 Release(); 319 if (!FetchAppIcon(web_contents, icon_url)) {
320 OutputDeveloperNotShownMessage(web_contents, kCannotDetermineBestIcon);
321 Cancel();
322 }
348 } 323 }
349 324
350 bool AppBannerDataFetcher::IsWebAppInstalled( 325 bool AppBannerDataFetcher::FetchAppIcon(content::WebContents* web_contents,
351 content::BrowserContext* browser_context, 326 const GURL& icon_url) {
352 const GURL& start_url) { 327 return ManifestIconDownloader::Download(
353 return false; 328 web_contents,
329 icon_url,
330 ideal_icon_size_,
331 base::Bind(&AppBannerDataFetcher::OnAppIconFetched,
332 this));
354 } 333 }
355 334
356 void AppBannerDataFetcher::RequestShowBanner(const SkBitmap* icon) { 335 void AppBannerDataFetcher::OnAppIconFetched(const SkBitmap& bitmap) {
336 if (!is_active_) return;
337
357 content::WebContents* web_contents = GetWebContents(); 338 content::WebContents* web_contents = GetWebContents();
358 if (!CheckFetcherIsStillAlive(web_contents)) { 339 if (!CheckFetcherIsStillAlive(web_contents)) {
359 Cancel(); 340 Cancel();
360 return; 341 return;
361 } 342 }
362 if (!icon) { 343 if (bitmap.drawsNothing()) {
363 OutputDeveloperNotShownMessage(web_contents, kNoIconAvailable); 344 OutputDeveloperNotShownMessage(web_contents, kNoIconAvailable);
364 Cancel(); 345 Cancel();
365 return; 346 return;
366 } 347 }
367 348
368 RecordCouldShowBanner(); 349 RecordCouldShowBanner();
369 if (!CheckIfShouldShowBanner()) { 350 if (!CheckIfShouldShowBanner()) {
370 // At this point, the only possible case is that the banner has been added 351 // 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. 352 // to the homescreen, given all of the other checks that have been made.
372 OutputDeveloperNotShownMessage(web_contents, kBannerAlreadyAdded); 353 OutputDeveloperNotShownMessage(web_contents, kBannerAlreadyAdded);
373 Cancel(); 354 Cancel();
374 return; 355 return;
375 } 356 }
376 357
377 app_icon_.reset(new SkBitmap(*icon)); 358 app_icon_.reset(new SkBitmap(bitmap));
378 event_request_id_ = ++gCurrentRequestID; 359 event_request_id_ = ++gCurrentRequestID;
379 web_contents->GetMainFrame()->Send( 360 web_contents->GetMainFrame()->Send(
380 new ChromeViewMsg_AppBannerPromptRequest( 361 new ChromeViewMsg_AppBannerPromptRequest(
381 web_contents->GetMainFrame()->GetRoutingID(), 362 web_contents->GetMainFrame()->GetRoutingID(),
382 event_request_id_, 363 event_request_id_,
383 GetBannerType())); 364 GetBannerType()));
365 }
366
367 bool AppBannerDataFetcher::IsWebAppInstalled(
368 content::BrowserContext* browser_context,
369 const GURL& start_url) {
370 return false;
384 } 371 }
385 372
386 void AppBannerDataFetcher::RecordCouldShowBanner() { 373 void AppBannerDataFetcher::RecordCouldShowBanner() {
387 content::WebContents* web_contents = GetWebContents(); 374 content::WebContents* web_contents = GetWebContents();
388 DCHECK(web_contents); 375 DCHECK(web_contents);
389 376
390 AppBannerSettingsHelper::RecordBannerCouldShowEvent( 377 AppBannerSettingsHelper::RecordBannerCouldShowEvent(
391 web_contents, validated_url_, GetAppIdentifier(), 378 web_contents, validated_url_, GetAppIdentifier(),
392 GetCurrentTime(), transition_type_); 379 GetCurrentTime(), transition_type_);
393 } 380 }
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
431 return false; 418 return false;
432 } 419 }
433 if (!DoesManifestContainRequiredIcon(manifest)) { 420 if (!DoesManifestContainRequiredIcon(manifest)) {
434 OutputDeveloperNotShownMessage(web_contents, kManifestMissingSuitableIcon); 421 OutputDeveloperNotShownMessage(web_contents, kManifestMissingSuitableIcon);
435 return false; 422 return false;
436 } 423 }
437 return true; 424 return true;
438 } 425 }
439 426
440 } // namespace banners 427 } // namespace banners
OLDNEW
« no previous file with comments | « chrome/browser/banners/app_banner_data_fetcher.h ('k') | chrome/browser/manifest/manifest_icon_downloader.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698