| 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_metrics.h" | 11 #include "chrome/browser/banners/app_banner_metrics.h" |
| 11 #include "chrome/browser/banners/app_banner_settings_helper.h" | 12 #include "chrome/browser/banners/app_banner_settings_helper.h" |
| 12 #include "chrome/browser/bitmap_fetcher/bitmap_fetcher.h" | 13 #include "chrome/browser/bitmap_fetcher/bitmap_fetcher.h" |
| 13 #include "chrome/browser/browser_process.h" | 14 #include "chrome/browser/browser_process.h" |
| 14 #include "chrome/browser/infobars/infobar_service.h" | 15 #include "chrome/browser/infobars/infobar_service.h" |
| 15 #include "chrome/browser/manifest/manifest_icon_selector.h" | 16 #include "chrome/browser/manifest/manifest_icon_selector.h" |
| 16 #include "chrome/browser/profiles/profile.h" | 17 #include "chrome/browser/profiles/profile.h" |
| 17 #include "chrome/common/render_messages.h" | 18 #include "chrome/common/render_messages.h" |
| 18 #include "components/infobars/core/infobar.h" | 19 #include "components/infobars/core/infobar.h" |
| 19 #include "components/rappor/rappor_utils.h" | 20 #include "components/rappor/rappor_utils.h" |
| (...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 133 IPC_END_MESSAGE_MAP() | 134 IPC_END_MESSAGE_MAP() |
| 134 | 135 |
| 135 return handled; | 136 return handled; |
| 136 } | 137 } |
| 137 | 138 |
| 138 void AppBannerDataFetcher::OnBannerPromptReply( | 139 void AppBannerDataFetcher::OnBannerPromptReply( |
| 139 content::RenderFrameHost* render_frame_host, | 140 content::RenderFrameHost* render_frame_host, |
| 140 int request_id, | 141 int request_id, |
| 141 blink::WebAppBannerPromptReply reply) { | 142 blink::WebAppBannerPromptReply reply) { |
| 142 content::WebContents* web_contents = GetWebContents(); | 143 content::WebContents* web_contents = GetWebContents(); |
| 143 if (!is_active_ || !web_contents || request_id != event_request_id_) { | 144 if (!CheckFetcherIsStillAlive(web_contents) || |
| 145 request_id != event_request_id_) { |
| 144 Cancel(); | 146 Cancel(); |
| 145 return; | 147 return; |
| 146 } | 148 } |
| 147 | 149 |
| 148 // The renderer might have requested the prompt to be canceled. | 150 // The renderer might have requested the prompt to be canceled. |
| 149 if (reply == blink::WebAppBannerPromptReply::Cancel) { | 151 if (reply == blink::WebAppBannerPromptReply::Cancel) { |
| 152 OutputDeveloperNotShownMessage(web_contents, kRendererRequestCancel); |
| 150 Cancel(); | 153 Cancel(); |
| 151 return; | 154 return; |
| 152 } | 155 } |
| 153 | 156 |
| 154 // Definitely going to show the banner now. | 157 // Definitely going to show the banner now. |
| 155 FOR_EACH_OBSERVER(Observer, observer_list_, | 158 FOR_EACH_OBSERVER(Observer, observer_list_, |
| 156 OnDecidedWhetherToShow(this, true)); | 159 OnDecidedWhetherToShow(this, true)); |
| 157 | 160 |
| 158 infobars::InfoBar* infobar = CreateBanner(app_icon_.get(), app_title_); | 161 infobars::InfoBar* infobar = CreateBanner(app_icon_.get(), app_title_); |
| 159 if (infobar) { | 162 if (infobar) { |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 224 GetCurrentTime()); | 227 GetCurrentTime()); |
| 225 rappor::SampleDomainAndRegistryFromGURL(g_browser_process->rappor_service(), | 228 rappor::SampleDomainAndRegistryFromGURL(g_browser_process->rappor_service(), |
| 226 event_name, | 229 event_name, |
| 227 web_contents->GetURL()); | 230 web_contents->GetURL()); |
| 228 banners::TrackDisplayEvent(DISPLAY_EVENT_CREATED); | 231 banners::TrackDisplayEvent(DISPLAY_EVENT_CREATED); |
| 229 } | 232 } |
| 230 | 233 |
| 231 void AppBannerDataFetcher::OnDidGetManifest( | 234 void AppBannerDataFetcher::OnDidGetManifest( |
| 232 const content::Manifest& manifest) { | 235 const content::Manifest& manifest) { |
| 233 content::WebContents* web_contents = GetWebContents(); | 236 content::WebContents* web_contents = GetWebContents(); |
| 234 if (!is_active_ || !web_contents || manifest.IsEmpty()) { | 237 if (!CheckFetcherIsStillAlive(web_contents)) { |
| 235 Cancel(); | 238 Cancel(); |
| 236 return; | 239 return; |
| 237 } | 240 } |
| 241 if (manifest.IsEmpty()) { |
| 242 OutputDeveloperNotShownMessage(web_contents, kManifestEmpty); |
| 243 Cancel(); |
| 244 return; |
| 245 } |
| 238 | 246 |
| 239 if (manifest.prefer_related_applications && | 247 if (manifest.prefer_related_applications && |
| 240 manifest.related_applications.size()) { | 248 manifest.related_applications.size()) { |
| 241 for (const auto& application : manifest.related_applications) { | 249 for (const auto& application : manifest.related_applications) { |
| 242 std::string platform = base::UTF16ToUTF8(application.platform.string()); | 250 std::string platform = base::UTF16ToUTF8(application.platform.string()); |
| 243 std::string id = base::UTF16ToUTF8(application.id.string()); | 251 std::string id = base::UTF16ToUTF8(application.id.string()); |
| 244 if (weak_delegate_->HandleNonWebApp(platform, application.url, id)) | 252 if (weak_delegate_->HandleNonWebApp(platform, application.url, id)) |
| 245 return; | 253 return; |
| 246 } | 254 } |
| 247 } | 255 } |
| 248 | 256 |
| 249 if (!IsManifestValidForWebApp(manifest)) { | 257 if (!IsManifestValidForWebApp(manifest, web_contents)) { |
| 250 Cancel(); | 258 Cancel(); |
| 251 return; | 259 return; |
| 252 } | 260 } |
| 253 | 261 |
| 254 banners::TrackDisplayEvent(DISPLAY_EVENT_BANNER_REQUESTED); | 262 banners::TrackDisplayEvent(DISPLAY_EVENT_BANNER_REQUESTED); |
| 255 | 263 |
| 256 web_app_data_ = manifest; | 264 web_app_data_ = manifest; |
| 257 app_title_ = web_app_data_.name.string(); | 265 app_title_ = web_app_data_.name.string(); |
| 258 | 266 |
| 259 // Check to see if there is a single service worker controlling this page | 267 // Check to see if there is a single service worker controlling this page |
| 260 // and the manifest's start url. | 268 // and the manifest's start url. |
| 261 Profile* profile = | 269 Profile* profile = |
| 262 Profile::FromBrowserContext(web_contents->GetBrowserContext()); | 270 Profile::FromBrowserContext(web_contents->GetBrowserContext()); |
| 263 content::StoragePartition* storage_partition = | 271 content::StoragePartition* storage_partition = |
| 264 content::BrowserContext::GetStoragePartition( | 272 content::BrowserContext::GetStoragePartition( |
| 265 profile, web_contents->GetSiteInstance()); | 273 profile, web_contents->GetSiteInstance()); |
| 266 DCHECK(storage_partition); | 274 DCHECK(storage_partition); |
| 267 | 275 |
| 268 storage_partition->GetServiceWorkerContext()->CheckHasServiceWorker( | 276 storage_partition->GetServiceWorkerContext()->CheckHasServiceWorker( |
| 269 validated_url_, manifest.start_url, | 277 validated_url_, manifest.start_url, |
| 270 base::Bind(&AppBannerDataFetcher::OnDidCheckHasServiceWorker, | 278 base::Bind(&AppBannerDataFetcher::OnDidCheckHasServiceWorker, |
| 271 this)); | 279 this)); |
| 272 } | 280 } |
| 273 | 281 |
| 274 void AppBannerDataFetcher::OnDidCheckHasServiceWorker( | 282 void AppBannerDataFetcher::OnDidCheckHasServiceWorker( |
| 275 bool has_service_worker) { | 283 bool has_service_worker) { |
| 276 content::WebContents* web_contents = GetWebContents(); | 284 content::WebContents* web_contents = GetWebContents(); |
| 277 if (!is_active_ || !web_contents) { | 285 if (!CheckFetcherIsStillAlive(web_contents)) { |
| 278 Cancel(); | 286 Cancel(); |
| 279 return; | 287 return; |
| 280 } | 288 } |
| 281 | 289 |
| 282 if (has_service_worker) { | 290 if (has_service_worker) { |
| 283 // Create an infobar to promote the manifest's app. | 291 // Create an infobar to promote the manifest's app. |
| 284 GURL icon_url = | 292 GURL icon_url = |
| 285 ManifestIconSelector::FindBestMatchingIcon( | 293 ManifestIconSelector::FindBestMatchingIcon( |
| 286 web_app_data_.icons, | 294 web_app_data_.icons, |
| 287 ideal_icon_size_, | 295 ideal_icon_size_, |
| 288 gfx::Screen::GetScreenFor(web_contents->GetNativeView())); | 296 gfx::Screen::GetScreenFor(web_contents->GetNativeView())); |
| 289 if (!icon_url.is_empty()) { | 297 if (!icon_url.is_empty()) { |
| 290 FetchIcon(icon_url); | 298 FetchIcon(icon_url); |
| 291 return; | 299 return; |
| 292 } | 300 } |
| 301 OutputDeveloperNotShownMessage(web_contents, kCannotDetermineBestIcon); |
| 293 } else { | 302 } else { |
| 294 TrackDisplayEvent(DISPLAY_EVENT_LACKS_SERVICE_WORKER); | 303 TrackDisplayEvent(DISPLAY_EVENT_LACKS_SERVICE_WORKER); |
| 304 OutputDeveloperNotShownMessage(web_contents, kNoMatchingServiceWorker); |
| 295 } | 305 } |
| 296 | 306 |
| 297 Cancel(); | 307 Cancel(); |
| 298 } | 308 } |
| 299 | 309 |
| 300 void AppBannerDataFetcher::OnFetchComplete(const GURL& url, | 310 void AppBannerDataFetcher::OnFetchComplete(const GURL& url, |
| 301 const SkBitmap* icon) { | 311 const SkBitmap* icon) { |
| 302 if (is_active_) | 312 if (is_active_) |
| 303 ShowBanner(icon); | 313 ShowBanner(icon); |
| 304 | 314 |
| 305 Release(); | 315 Release(); |
| 306 } | 316 } |
| 307 | 317 |
| 308 void AppBannerDataFetcher::ShowBanner(const SkBitmap* icon) { | 318 void AppBannerDataFetcher::ShowBanner(const SkBitmap* icon) { |
| 309 content::WebContents* web_contents = GetWebContents(); | 319 content::WebContents* web_contents = GetWebContents(); |
| 310 if (!is_active_ || !web_contents || !icon) { | 320 if (!CheckFetcherIsStillAlive(web_contents)) { |
| 321 Cancel(); |
| 322 return; |
| 323 } |
| 324 if (!icon) { |
| 325 OutputDeveloperNotShownMessage(web_contents, kNoIconAvailable); |
| 311 Cancel(); | 326 Cancel(); |
| 312 return; | 327 return; |
| 313 } | 328 } |
| 314 | 329 |
| 315 RecordCouldShowBanner(); | 330 RecordCouldShowBanner(); |
| 316 if (!CheckIfShouldShowBanner()) { | 331 if (!CheckIfShouldShowBanner()) { |
| 332 // At this point, the only possible case is that the banner has been added |
| 333 // to the homescreen, given all of the other checks that have been made. |
| 334 OutputDeveloperNotShownMessage(web_contents, kBannerAlreadyAdded); |
| 317 Cancel(); | 335 Cancel(); |
| 318 return; | 336 return; |
| 319 } | 337 } |
| 320 | 338 |
| 321 app_icon_.reset(new SkBitmap(*icon)); | 339 app_icon_.reset(new SkBitmap(*icon)); |
| 322 event_request_id_ = ++gCurrentRequestID; | 340 event_request_id_ = ++gCurrentRequestID; |
| 323 web_contents->GetMainFrame()->Send( | 341 web_contents->GetMainFrame()->Send( |
| 324 new ChromeViewMsg_AppBannerPromptRequest( | 342 new ChromeViewMsg_AppBannerPromptRequest( |
| 325 web_contents->GetMainFrame()->GetRoutingID(), | 343 web_contents->GetMainFrame()->GetRoutingID(), |
| 326 event_request_id_, | 344 event_request_id_, |
| (...skipping 11 matching lines...) Expand all Loading... |
| 338 } | 356 } |
| 339 | 357 |
| 340 bool AppBannerDataFetcher::CheckIfShouldShowBanner() { | 358 bool AppBannerDataFetcher::CheckIfShouldShowBanner() { |
| 341 content::WebContents* web_contents = GetWebContents(); | 359 content::WebContents* web_contents = GetWebContents(); |
| 342 DCHECK(web_contents); | 360 DCHECK(web_contents); |
| 343 | 361 |
| 344 return AppBannerSettingsHelper::ShouldShowBanner( | 362 return AppBannerSettingsHelper::ShouldShowBanner( |
| 345 web_contents, validated_url_, GetAppIdentifier(), GetCurrentTime()); | 363 web_contents, validated_url_, GetAppIdentifier(), GetCurrentTime()); |
| 346 } | 364 } |
| 347 | 365 |
| 366 bool AppBannerDataFetcher::CheckFetcherIsStillAlive( |
| 367 content::WebContents* web_contents) { |
| 368 if (!is_active_) { |
| 369 OutputDeveloperNotShownMessage(web_contents, |
| 370 kUserNavigatedBeforeBannerShown); |
| 371 return false; |
| 372 } |
| 373 if (!web_contents) { |
| 374 return false; // We cannot show a message if |web_contents| is null |
| 375 } |
| 376 return true; |
| 377 } |
| 378 |
| 348 // static | 379 // static |
| 349 bool AppBannerDataFetcher::IsManifestValidForWebApp( | 380 bool AppBannerDataFetcher::IsManifestValidForWebApp( |
| 350 const content::Manifest& manifest) { | 381 const content::Manifest& manifest, |
| 351 if (manifest.IsEmpty()) | 382 content::WebContents* web_contents) { |
| 383 if (manifest.IsEmpty()) { |
| 384 OutputDeveloperNotShownMessage(web_contents, kManifestEmpty); |
| 352 return false; | 385 return false; |
| 353 if (!manifest.start_url.is_valid()) | 386 } |
| 387 if (!manifest.start_url.is_valid()) { |
| 388 OutputDeveloperNotShownMessage(web_contents, kStartURLNotValid); |
| 354 return false; | 389 return false; |
| 355 if (manifest.name.is_null() && manifest.short_name.is_null()) | 390 } |
| 391 if (manifest.name.is_null() && manifest.short_name.is_null()) { |
| 392 OutputDeveloperNotShownMessage(web_contents, |
| 393 kManifestMissingNameOrShortName); |
| 356 return false; | 394 return false; |
| 357 if (!DoesManifestContainRequiredIcon(manifest)) | 395 } |
| 396 if (!DoesManifestContainRequiredIcon(manifest)) { |
| 397 OutputDeveloperNotShownMessage(web_contents, kManifestMissingSuitableIcon); |
| 358 return false; | 398 return false; |
| 399 } |
| 359 return true; | 400 return true; |
| 360 } | 401 } |
| 361 | 402 |
| 362 } // namespace banners | 403 } // namespace banners |
| OLD | NEW |