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_manager.h" | 5 #include "chrome/browser/banners/app_banner_manager.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/callback.h" | 10 #include "base/callback.h" |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 22 #include "content/public/browser/navigation_handle.h" | 22 #include "content/public/browser/navigation_handle.h" |
| 23 #include "content/public/browser/render_frame_host.h" | 23 #include "content/public/browser/render_frame_host.h" |
| 24 #include "content/public/browser/web_contents.h" | 24 #include "content/public/browser/web_contents.h" |
| 25 #include "mojo/public/cpp/bindings/interface_request.h" | 25 #include "mojo/public/cpp/bindings/interface_request.h" |
| 26 #include "services/service_manager/public/cpp/interface_provider.h" | 26 #include "services/service_manager/public/cpp/interface_provider.h" |
| 27 #include "third_party/WebKit/public/platform/modules/installation/installation.m ojom.h" | 27 #include "third_party/WebKit/public/platform/modules/installation/installation.m ojom.h" |
| 28 #include "third_party/skia/include/core/SkBitmap.h" | 28 #include "third_party/skia/include/core/SkBitmap.h" |
| 29 | 29 |
| 30 namespace { | 30 namespace { |
| 31 | 31 |
| 32 int gCurrentRequestID = -1; | 32 const int kInvalidIdealBadgeIconSizeInPx = -1; |
| 33 int gTimeDeltaInDaysForTesting = 0; | 33 |
| 34 int g_current_request_id = -1; | |
|
dominickn
2017/02/14 00:01:16
The change to this is unrelated to this CL. Can yo
F
2017/02/14 19:22:42
Done.
| |
| 35 int g_time_delta_in_days_for_testing = 0; | |
| 34 | 36 |
| 35 InstallableParams ParamsToGetManifest() { | 37 InstallableParams ParamsToGetManifest() { |
| 36 return InstallableParams(); | 38 return InstallableParams(); |
| 37 } | 39 } |
| 38 | 40 |
| 39 // Returns an InstallableParams object that requests all checks necessary for | 41 // Returns an InstallableParams object that requests all checks necessary for |
| 40 // a web app banner. | 42 // a web app banner. |
| 41 InstallableParams ParamsToPerformInstallableCheck(int ideal_icon_size_in_px, | 43 InstallableParams ParamsToPerformInstallableCheck( |
| 42 int minimum_icon_size_in_px) { | 44 int ideal_primary_icon_size_in_px, |
| 45 int minimum_primary_icon_size_in_px, | |
| 46 int ideal_badge_icon_size_in_px) { | |
| 43 InstallableParams params; | 47 InstallableParams params; |
| 44 params.ideal_primary_icon_size_in_px = ideal_icon_size_in_px; | 48 params.ideal_primary_icon_size_in_px = ideal_primary_icon_size_in_px; |
| 45 params.minimum_primary_icon_size_in_px = minimum_icon_size_in_px; | 49 params.minimum_primary_icon_size_in_px = minimum_primary_icon_size_in_px; |
| 46 params.check_installable = true; | 50 params.check_installable = true; |
| 47 params.fetch_valid_primary_icon = true; | 51 params.fetch_valid_primary_icon = true; |
| 48 | 52 if (ideal_badge_icon_size_in_px != kInvalidIdealBadgeIconSizeInPx) { |
| 53 params.fetch_valid_badge_icon = true; | |
| 54 params.ideal_badge_icon_size_in_px = ideal_badge_icon_size_in_px; | |
| 55 params.minimum_badge_icon_size_in_px = ideal_badge_icon_size_in_px; | |
| 56 } | |
| 49 return params; | 57 return params; |
| 50 } | 58 } |
| 51 | 59 |
| 52 } // anonymous namespace | 60 } // anonymous namespace |
| 53 | 61 |
| 54 namespace banners { | 62 namespace banners { |
| 55 | 63 |
| 56 // static | 64 // static |
| 57 base::Time AppBannerManager::GetCurrentTime() { | 65 base::Time AppBannerManager::GetCurrentTime() { |
| 58 return base::Time::Now() + | 66 return base::Time::Now() + |
| 59 base::TimeDelta::FromDays(gTimeDeltaInDaysForTesting); | 67 base::TimeDelta::FromDays(g_time_delta_in_days_for_testing); |
| 60 } | 68 } |
| 61 | 69 |
| 62 // static | 70 // static |
| 63 void AppBannerManager::SetTimeDeltaForTesting(int days) { | 71 void AppBannerManager::SetTimeDeltaForTesting(int days) { |
| 64 gTimeDeltaInDaysForTesting = days; | 72 g_time_delta_in_days_for_testing = days; |
| 65 } | 73 } |
| 66 | 74 |
| 67 // static | 75 // static |
| 68 void AppBannerManager::SetTotalEngagementToTrigger(double engagement) { | 76 void AppBannerManager::SetTotalEngagementToTrigger(double engagement) { |
| 69 AppBannerSettingsHelper::SetTotalEngagementToTrigger(engagement); | 77 AppBannerSettingsHelper::SetTotalEngagementToTrigger(engagement); |
| 70 } | 78 } |
| 71 | 79 |
| 72 // static | 80 // static |
| 73 bool AppBannerManager::URLsAreForTheSamePage(const GURL& first, | 81 bool AppBannerManager::URLsAreForTheSamePage(const GURL& first, |
| 74 const GURL& second) { | 82 const GURL& second) { |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 129 | 137 |
| 130 void AppBannerManager::OnInstall() { | 138 void AppBannerManager::OnInstall() { |
| 131 blink::mojom::InstallationServicePtr installation_service; | 139 blink::mojom::InstallationServicePtr installation_service; |
| 132 web_contents()->GetMainFrame()->GetRemoteInterfaces()->GetInterface( | 140 web_contents()->GetMainFrame()->GetRemoteInterfaces()->GetInterface( |
| 133 mojo::MakeRequest(&installation_service)); | 141 mojo::MakeRequest(&installation_service)); |
| 134 DCHECK(installation_service); | 142 DCHECK(installation_service); |
| 135 installation_service->OnInstall(); | 143 installation_service->OnInstall(); |
| 136 } | 144 } |
| 137 | 145 |
| 138 void AppBannerManager::SendBannerAccepted(int request_id) { | 146 void AppBannerManager::SendBannerAccepted(int request_id) { |
| 139 if (request_id != gCurrentRequestID) | 147 if (request_id != g_current_request_id) |
| 140 return; | 148 return; |
| 141 | 149 |
| 142 DCHECK(event_.is_bound()); | 150 DCHECK(event_.is_bound()); |
| 143 event_->BannerAccepted(GetBannerType()); | 151 event_->BannerAccepted(GetBannerType()); |
| 144 } | 152 } |
| 145 | 153 |
| 146 void AppBannerManager::SendBannerDismissed(int request_id) { | 154 void AppBannerManager::SendBannerDismissed(int request_id) { |
| 147 if (request_id != gCurrentRequestID) | 155 if (request_id != g_current_request_id) |
| 148 return; | 156 return; |
| 149 | 157 |
| 150 DCHECK(event_.is_bound()); | 158 DCHECK(event_.is_bound()); |
| 151 event_->BannerDismissed(); | 159 event_->BannerDismissed(); |
| 152 } | 160 } |
| 153 | 161 |
| 154 base::Closure AppBannerManager::FetchWebappSplashScreenImageCallback( | 162 base::Closure AppBannerManager::FetchWebappSplashScreenImageCallback( |
| 155 const std::string& webapp_id) { | 163 const std::string& webapp_id) { |
| 156 return base::Closure(); | 164 return base::Closure(); |
| 157 } | 165 } |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 190 } | 198 } |
| 191 | 199 |
| 192 std::string AppBannerManager::GetStatusParam(InstallableStatusCode code) { | 200 std::string AppBannerManager::GetStatusParam(InstallableStatusCode code) { |
| 193 if (code == NO_ACCEPTABLE_ICON || code == MANIFEST_MISSING_SUITABLE_ICON) { | 201 if (code == NO_ACCEPTABLE_ICON || code == MANIFEST_MISSING_SUITABLE_ICON) { |
| 194 return base::IntToString(InstallableManager::GetMinimumIconSizeInPx()); | 202 return base::IntToString(InstallableManager::GetMinimumIconSizeInPx()); |
| 195 } | 203 } |
| 196 | 204 |
| 197 return std::string(); | 205 return std::string(); |
| 198 } | 206 } |
| 199 | 207 |
| 200 int AppBannerManager::GetIdealIconSizeInPx() { | 208 int AppBannerManager::GetIdealPrimaryIconSizeInPx() { |
| 201 return InstallableManager::GetMinimumIconSizeInPx(); | 209 return InstallableManager::GetMinimumIconSizeInPx(); |
| 202 } | 210 } |
| 203 | 211 |
| 204 int AppBannerManager::GetMinimumIconSizeInPx() { | 212 int AppBannerManager::GetMinimumPrimaryIconSizeInPx() { |
| 205 return InstallableManager::GetMinimumIconSizeInPx(); | 213 return InstallableManager::GetMinimumIconSizeInPx(); |
| 206 } | 214 } |
| 207 | 215 |
| 216 int AppBannerManager::GetIdealBadgeIconSizeInPx() { | |
| 217 return kInvalidIdealBadgeIconSizeInPx; | |
| 218 } | |
| 219 | |
| 208 base::WeakPtr<AppBannerManager> AppBannerManager::GetWeakPtr() { | 220 base::WeakPtr<AppBannerManager> AppBannerManager::GetWeakPtr() { |
| 209 return weak_factory_.GetWeakPtr(); | 221 return weak_factory_.GetWeakPtr(); |
| 210 } | 222 } |
| 211 | 223 |
| 212 bool AppBannerManager::IsDebugMode() const { | 224 bool AppBannerManager::IsDebugMode() const { |
| 213 return is_debug_mode_ || | 225 return is_debug_mode_ || |
| 214 base::CommandLine::ForCurrentProcess()->HasSwitch( | 226 base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 215 switches::kBypassAppBannerEngagementChecks); | 227 switches::kBypassAppBannerEngagementChecks); |
| 216 } | 228 } |
| 217 | 229 |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 240 : manifest_.name.string(); | 252 : manifest_.name.string(); |
| 241 | 253 |
| 242 PerformInstallableCheck(); | 254 PerformInstallableCheck(); |
| 243 } | 255 } |
| 244 | 256 |
| 245 void AppBannerManager::PerformInstallableCheck() { | 257 void AppBannerManager::PerformInstallableCheck() { |
| 246 if (!CheckIfShouldShowBanner()) | 258 if (!CheckIfShouldShowBanner()) |
| 247 return; | 259 return; |
| 248 | 260 |
| 249 // Fetch and verify the other required information. | 261 // Fetch and verify the other required information. |
| 250 manager_->GetData(ParamsToPerformInstallableCheck(GetIdealIconSizeInPx(), | 262 manager_->GetData( |
| 251 GetMinimumIconSizeInPx()), | 263 ParamsToPerformInstallableCheck(GetIdealPrimaryIconSizeInPx(), |
| 252 base::Bind(&AppBannerManager::OnDidPerformInstallableCheck, | 264 GetMinimumPrimaryIconSizeInPx(), |
| 253 GetWeakPtr())); | 265 GetIdealBadgeIconSizeInPx()), |
| 266 base::Bind(&AppBannerManager::OnDidPerformInstallableCheck, | |
| 267 GetWeakPtr())); | |
| 254 } | 268 } |
| 255 | 269 |
| 256 void AppBannerManager::OnDidPerformInstallableCheck( | 270 void AppBannerManager::OnDidPerformInstallableCheck( |
| 257 const InstallableData& data) { | 271 const InstallableData& data) { |
| 258 if (data.is_installable) | 272 if (data.is_installable) |
| 259 TrackDisplayEvent(DISPLAY_EVENT_WEB_APP_BANNER_REQUESTED); | 273 TrackDisplayEvent(DISPLAY_EVENT_WEB_APP_BANNER_REQUESTED); |
| 260 | 274 |
| 261 if (data.error_code != NO_ERROR_DETECTED) { | 275 if (data.error_code != NO_ERROR_DETECTED) { |
| 262 if (data.error_code == NO_MATCHING_SERVICE_WORKER) | 276 if (data.error_code == NO_MATCHING_SERVICE_WORKER) |
| 263 TrackDisplayEvent(DISPLAY_EVENT_LACKS_SERVICE_WORKER); | 277 TrackDisplayEvent(DISPLAY_EVENT_LACKS_SERVICE_WORKER); |
| 264 | 278 |
| 265 ReportStatus(web_contents(), data.error_code); | 279 ReportStatus(web_contents(), data.error_code); |
| 266 Stop(); | 280 Stop(); |
| 267 } | 281 } |
| 268 | 282 |
| 269 if (!is_active_) | 283 if (!is_active_) |
| 270 return; | 284 return; |
| 271 | 285 |
| 272 DCHECK(data.is_installable); | 286 DCHECK(data.is_installable); |
| 273 DCHECK(!data.primary_icon_url.is_empty()); | 287 DCHECK(!data.primary_icon_url.is_empty()); |
| 274 DCHECK(data.primary_icon); | 288 DCHECK(data.primary_icon); |
| 275 | 289 |
| 276 icon_url_ = data.primary_icon_url; | 290 primary_icon_url_ = data.primary_icon_url; |
| 277 icon_.reset(new SkBitmap(*data.primary_icon)); | 291 primary_icon_.reset(new SkBitmap(*data.primary_icon)); |
| 292 | |
| 293 if (data.badge_icon) { | |
| 294 DCHECK(!data.badge_icon_url.is_empty()); | |
| 295 | |
| 296 badge_icon_url_ = data.badge_icon_url; | |
| 297 badge_icon_.reset(new SkBitmap(*data.badge_icon)); | |
| 298 } | |
| 278 | 299 |
| 279 SendBannerPromptRequest(); | 300 SendBannerPromptRequest(); |
| 280 } | 301 } |
| 281 | 302 |
| 282 void AppBannerManager::RecordDidShowBanner(const std::string& event_name) { | 303 void AppBannerManager::RecordDidShowBanner(const std::string& event_name) { |
| 283 content::WebContents* contents = web_contents(); | 304 content::WebContents* contents = web_contents(); |
| 284 DCHECK(contents); | 305 DCHECK(contents); |
| 285 | 306 |
| 286 AppBannerSettingsHelper::RecordBannerEvent( | 307 AppBannerSettingsHelper::RecordBannerEvent( |
| 287 contents, validated_url_, GetAppIdentifier(), | 308 contents, validated_url_, GetAppIdentifier(), |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 334 is_active_ = false; | 355 is_active_ = false; |
| 335 was_canceled_by_page_ = false; | 356 was_canceled_by_page_ = false; |
| 336 page_requested_prompt_ = false; | 357 page_requested_prompt_ = false; |
| 337 need_to_log_status_ = false; | 358 need_to_log_status_ = false; |
| 338 } | 359 } |
| 339 | 360 |
| 340 void AppBannerManager::SendBannerPromptRequest() { | 361 void AppBannerManager::SendBannerPromptRequest() { |
| 341 RecordCouldShowBanner(); | 362 RecordCouldShowBanner(); |
| 342 | 363 |
| 343 TrackBeforeInstallEvent(BEFORE_INSTALL_EVENT_CREATED); | 364 TrackBeforeInstallEvent(BEFORE_INSTALL_EVENT_CREATED); |
| 344 event_request_id_ = ++gCurrentRequestID; | 365 event_request_id_ = ++g_current_request_id; |
| 345 | 366 |
| 346 web_contents()->GetMainFrame()->GetRemoteInterfaces()->GetInterface( | 367 web_contents()->GetMainFrame()->GetRemoteInterfaces()->GetInterface( |
| 347 mojo::MakeRequest(&controller_)); | 368 mojo::MakeRequest(&controller_)); |
| 348 | 369 |
| 349 controller_->BannerPromptRequest( | 370 controller_->BannerPromptRequest( |
| 350 binding_.CreateInterfacePtrAndBind(), mojo::MakeRequest(&event_), | 371 binding_.CreateInterfacePtrAndBind(), mojo::MakeRequest(&event_), |
| 351 {GetBannerType()}, | 372 {GetBannerType()}, |
| 352 base::Bind(&AppBannerManager::OnBannerPromptReply, GetWeakPtr())); | 373 base::Bind(&AppBannerManager::OnBannerPromptReply, GetWeakPtr())); |
| 353 } | 374 } |
| 354 | 375 |
| (...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 517 was_canceled_by_page_ = false; | 538 was_canceled_by_page_ = false; |
| 518 } else { | 539 } else { |
| 519 TrackBeforeInstallEvent(BEFORE_INSTALL_EVENT_NO_ACTION); | 540 TrackBeforeInstallEvent(BEFORE_INSTALL_EVENT_NO_ACTION); |
| 520 } | 541 } |
| 521 | 542 |
| 522 AppBannerSettingsHelper::RecordMinutesFromFirstVisitToShow( | 543 AppBannerSettingsHelper::RecordMinutesFromFirstVisitToShow( |
| 523 contents, validated_url_, GetAppIdentifier(), GetCurrentTime()); | 544 contents, validated_url_, GetAppIdentifier(), GetCurrentTime()); |
| 524 | 545 |
| 525 DCHECK(!manifest_url_.is_empty()); | 546 DCHECK(!manifest_url_.is_empty()); |
| 526 DCHECK(!manifest_.IsEmpty()); | 547 DCHECK(!manifest_.IsEmpty()); |
| 527 DCHECK(!icon_url_.is_empty()); | 548 DCHECK(!primary_icon_url_.is_empty()); |
| 528 DCHECK(icon_.get()); | 549 DCHECK(primary_icon_.get()); |
| 529 | 550 |
| 530 TrackBeforeInstallEvent(BEFORE_INSTALL_EVENT_COMPLETE); | 551 TrackBeforeInstallEvent(BEFORE_INSTALL_EVENT_COMPLETE); |
| 531 ShowBanner(); | 552 ShowBanner(); |
| 532 is_active_ = false; | 553 is_active_ = false; |
| 533 } | 554 } |
| 534 | 555 |
| 535 void AppBannerManager::DisplayAppBanner() { | 556 void AppBannerManager::DisplayAppBanner() { |
| 536 if (was_canceled_by_page_) { | 557 if (was_canceled_by_page_) { |
| 537 // Simulate a non-canceled OnBannerPromptReply to show the delayed banner. | 558 // Simulate a non-canceled OnBannerPromptReply to show the delayed banner. |
| 538 // Don't reset |was_canceled_by_page_| yet for metrics purposes. | 559 // Don't reset |was_canceled_by_page_| yet for metrics purposes. |
| 539 OnBannerPromptReply(blink::mojom::AppBannerPromptReply::NONE, referrer_); | 560 OnBannerPromptReply(blink::mojom::AppBannerPromptReply::NONE, referrer_); |
| 540 } else { | 561 } else { |
| 541 // Log that the prompt request was made for when we get the prompt reply. | 562 // Log that the prompt request was made for when we get the prompt reply. |
| 542 page_requested_prompt_ = true; | 563 page_requested_prompt_ = true; |
| 543 } | 564 } |
| 544 } | 565 } |
| 545 | 566 |
| 546 } // namespace banners | 567 } // namespace banners |
| OLD | NEW |