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