Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 Loading... | |
| 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 Loading... | |
| 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 Loading... | |
| 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(web_contents, icon_url)) return; |
| 317 | |
| 318 OutputDeveloperNotShownMessage(web_contents, kCannotDetermineBestIcon); | |
| 319 Cancel(); | |
| 344 } | 320 } |
| 345 | 321 |
| 346 bool AppBannerDataFetcher::IsWebAppInstalled( | 322 bool AppBannerDataFetcher::FetchAppIcon(content::WebContents* web_contents, |
| 347 content::BrowserContext* browser_context, | 323 const GURL& icon_url) { |
| 348 const GURL& start_url) { | 324 return icon_downloader_->Download( |
|
mlamouri (slow - plz ping)
2015/08/05 08:20:16
nit: add DCHECK(web_contents);
Lalit Maganti
2015/08/05 14:47:13
Removed web contents altogether.
| |
| 349 return false; | 325 icon_url, |
| 326 ideal_icon_size_, | |
| 327 base::Bind(&AppBannerDataFetcher::OnAppIconFetched, | |
| 328 this)); | |
| 350 } | 329 } |
| 351 | 330 |
| 352 void AppBannerDataFetcher::RequestShowBanner(const SkBitmap* icon) { | 331 void AppBannerDataFetcher::OnAppIconFetched(const SkBitmap& bitmap) { |
| 332 if (!is_active_) return; | |
| 333 | |
| 353 content::WebContents* web_contents = GetWebContents(); | 334 content::WebContents* web_contents = GetWebContents(); |
| 354 if (!CheckFetcherIsStillAlive(web_contents)) { | 335 if (!CheckFetcherIsStillAlive(web_contents)) { |
| 355 Cancel(); | 336 Cancel(); |
| 356 return; | 337 return; |
| 357 } | 338 } |
| 358 if (!icon) { | 339 if (bitmap.drawsNothing()) { |
| 359 OutputDeveloperNotShownMessage(web_contents, kNoIconAvailable); | 340 OutputDeveloperNotShownMessage(web_contents, kNoIconAvailable); |
| 360 Cancel(); | 341 Cancel(); |
| 361 return; | 342 return; |
| 362 } | 343 } |
| 363 | |
| 364 RecordCouldShowBanner(); | 344 RecordCouldShowBanner(); |
| 365 if (!CheckIfShouldShowBanner()) { | 345 if (!CheckIfShouldShowBanner()) { |
| 366 // At this point, the only possible case is that the banner has been added | 346 // 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. | 347 // to the homescreen, given all of the other checks that have been made. |
| 368 OutputDeveloperNotShownMessage(web_contents, kBannerAlreadyAdded); | 348 OutputDeveloperNotShownMessage(web_contents, kBannerAlreadyAdded); |
| 369 Cancel(); | 349 Cancel(); |
| 370 return; | 350 return; |
| 371 } | 351 } |
| 372 | 352 |
| 373 app_icon_.reset(new SkBitmap(*icon)); | 353 app_icon_.reset(new SkBitmap(icon)); |
| 374 event_request_id_ = ++gCurrentRequestID; | 354 event_request_id_ = ++gCurrentRequestID; |
| 375 web_contents->GetMainFrame()->Send( | 355 web_contents->GetMainFrame()->Send( |
| 376 new ChromeViewMsg_AppBannerPromptRequest( | 356 new ChromeViewMsg_AppBannerPromptRequest( |
| 377 web_contents->GetMainFrame()->GetRoutingID(), | 357 web_contents->GetMainFrame()->GetRoutingID(), |
| 378 event_request_id_, | 358 event_request_id_, |
| 379 GetBannerType())); | 359 GetBannerType())); |
| 360 } | |
| 361 | |
| 362 bool AppBannerDataFetcher::IsWebAppInstalled( | |
| 363 content::BrowserContext* browser_context, | |
| 364 const GURL& start_url) { | |
| 365 return false; | |
| 380 } | 366 } |
| 381 | 367 |
| 382 void AppBannerDataFetcher::RecordCouldShowBanner() { | 368 void AppBannerDataFetcher::RecordCouldShowBanner() { |
| 383 content::WebContents* web_contents = GetWebContents(); | 369 content::WebContents* web_contents = GetWebContents(); |
| 384 DCHECK(web_contents); | 370 DCHECK(web_contents); |
| 385 | 371 |
| 386 AppBannerSettingsHelper::RecordBannerCouldShowEvent( | 372 AppBannerSettingsHelper::RecordBannerCouldShowEvent( |
| 387 web_contents, validated_url_, GetAppIdentifier(), | 373 web_contents, validated_url_, GetAppIdentifier(), |
| 388 GetCurrentTime(), transition_type_); | 374 GetCurrentTime(), transition_type_); |
| 389 } | 375 } |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 427 return false; | 413 return false; |
| 428 } | 414 } |
| 429 if (!DoesManifestContainRequiredIcon(manifest)) { | 415 if (!DoesManifestContainRequiredIcon(manifest)) { |
| 430 OutputDeveloperNotShownMessage(web_contents, kManifestMissingSuitableIcon); | 416 OutputDeveloperNotShownMessage(web_contents, kManifestMissingSuitableIcon); |
| 431 return false; | 417 return false; |
| 432 } | 418 } |
| 433 return true; | 419 return true; |
| 434 } | 420 } |
| 435 | 421 |
| 436 } // namespace banners | 422 } // namespace banners |
| OLD | NEW |