| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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_settings_helper.h" | 5 #include "chrome/browser/banners/app_banner_settings_helper.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <algorithm> | 9 #include <algorithm> |
| 10 #include <string> | 10 #include <string> |
| (...skipping 22 matching lines...) Expand all Loading... |
| 33 namespace { | 33 namespace { |
| 34 | 34 |
| 35 // Max number of apps (including ServiceWorker based web apps) that a particular | 35 // Max number of apps (including ServiceWorker based web apps) that a particular |
| 36 // site may show a banner for. | 36 // site may show a banner for. |
| 37 const size_t kMaxAppsPerSite = 3; | 37 const size_t kMaxAppsPerSite = 3; |
| 38 | 38 |
| 39 // Oldest could show banner event we care about, in days. | 39 // Oldest could show banner event we care about, in days. |
| 40 const unsigned int kOldestCouldShowBannerEventInDays = 14; | 40 const unsigned int kOldestCouldShowBannerEventInDays = 14; |
| 41 | 41 |
| 42 // Number of days that showing the banner will prevent it being seen again for. | 42 // Number of days that showing the banner will prevent it being seen again for. |
| 43 const unsigned int kMinimumDaysBetweenBannerShows = 60; | 43 const unsigned int kMinimumDaysBetweenBannerShows = 14; |
| 44 | 44 |
| 45 const unsigned int kNumberOfMinutesInADay = 1440; | 45 const unsigned int kNumberOfMinutesInADay = 1440; |
| 46 | 46 |
| 47 // Number of days that the banner being blocked will prevent it being seen again | 47 // Number of days that the banner being blocked will prevent it being seen again |
| 48 // for. | 48 // for. |
| 49 const unsigned int kMinimumBannerBlockedToBannerShown = 90; | 49 const unsigned int kMinimumBannerBlockedToBannerShown = 90; |
| 50 | 50 |
| 51 // Default scores assigned to direct and indirect navigations respectively. | 51 // Default scores assigned to direct and indirect navigations respectively. |
| 52 const unsigned int kDefaultDirectNavigationEngagement = 1; | 52 const unsigned int kDefaultDirectNavigationEngagement = 1; |
| 53 const unsigned int kDefaultIndirectNavigationEngagement = 1; | 53 const unsigned int kDefaultIndirectNavigationEngagement = 1; |
| (...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 192 kBannerParamsKey, kBannerParamsMinutesKey); | 192 kBannerParamsKey, kBannerParamsMinutesKey); |
| 193 if (!param.empty()) { | 193 if (!param.empty()) { |
| 194 int minimum_minutes = 0; | 194 int minimum_minutes = 0; |
| 195 if (base::StringToInt(param, &minimum_minutes)) | 195 if (base::StringToInt(param, &minimum_minutes)) |
| 196 AppBannerSettingsHelper::SetMinimumMinutesBetweenVisits(minimum_minutes); | 196 AppBannerSettingsHelper::SetMinimumMinutesBetweenVisits(minimum_minutes); |
| 197 } | 197 } |
| 198 } | 198 } |
| 199 | 199 |
| 200 } // namespace | 200 } // namespace |
| 201 | 201 |
| 202 // Key to store instant apps events. |
| 203 const char AppBannerSettingsHelper::kInstantAppsKey[] = "instantapps"; |
| 204 |
| 202 void AppBannerSettingsHelper::ClearHistoryForURLs( | 205 void AppBannerSettingsHelper::ClearHistoryForURLs( |
| 203 Profile* profile, | 206 Profile* profile, |
| 204 const std::set<GURL>& origin_urls) { | 207 const std::set<GURL>& origin_urls) { |
| 205 HostContentSettingsMap* settings = | 208 HostContentSettingsMap* settings = |
| 206 HostContentSettingsMapFactory::GetForProfile(profile); | 209 HostContentSettingsMapFactory::GetForProfile(profile); |
| 207 for (const GURL& origin_url : origin_urls) { | 210 for (const GURL& origin_url : origin_urls) { |
| 208 settings->SetWebsiteSettingDefaultScope(origin_url, GURL(), | 211 settings->SetWebsiteSettingDefaultScope(origin_url, GURL(), |
| 209 CONTENT_SETTINGS_TYPE_APP_BANNER, | 212 CONTENT_SETTINGS_TYPE_APP_BANNER, |
| 210 std::string(), nullptr); | 213 std::string(), nullptr); |
| 211 settings->FlushLossyWebsiteSettings(); | 214 settings->FlushLossyWebsiteSettings(); |
| (...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 387 } | 390 } |
| 388 | 391 |
| 389 // Never show a banner when the package name or URL is empty. | 392 // Never show a banner when the package name or URL is empty. |
| 390 if (package_name_or_start_url.empty()) | 393 if (package_name_or_start_url.empty()) |
| 391 return PACKAGE_NAME_OR_START_URL_EMPTY; | 394 return PACKAGE_NAME_OR_START_URL_EMPTY; |
| 392 | 395 |
| 393 // Don't show if it has been added to the homescreen. | 396 // Don't show if it has been added to the homescreen. |
| 394 base::Time added_time = | 397 base::Time added_time = |
| 395 GetSingleBannerEvent(web_contents, origin_url, package_name_or_start_url, | 398 GetSingleBannerEvent(web_contents, origin_url, package_name_or_start_url, |
| 396 APP_BANNER_EVENT_DID_ADD_TO_HOMESCREEN); | 399 APP_BANNER_EVENT_DID_ADD_TO_HOMESCREEN); |
| 397 if (!added_time.is_null()) { | 400 if (!added_time.is_null()) |
| 398 banners::TrackDisplayEvent(banners::DISPLAY_EVENT_INSTALLED_PREVIOUSLY); | |
| 399 return ALREADY_INSTALLED; | 401 return ALREADY_INSTALLED; |
| 400 } | |
| 401 | 402 |
| 402 base::Time blocked_time = | 403 base::Time blocked_time = |
| 403 GetSingleBannerEvent(web_contents, origin_url, package_name_or_start_url, | 404 GetSingleBannerEvent(web_contents, origin_url, package_name_or_start_url, |
| 404 APP_BANNER_EVENT_DID_BLOCK); | 405 APP_BANNER_EVENT_DID_BLOCK); |
| 405 | 406 |
| 406 // Null times are in the distant past, so the delta between real times and | 407 // Null times are in the distant past, so the delta between real times and |
| 407 // null events will always be greater than the limits. | 408 // null events will always be greater than the limits. |
| 408 if (time - blocked_time < | 409 if (time - blocked_time < |
| 409 base::TimeDelta::FromDays(kMinimumBannerBlockedToBannerShown)) { | 410 base::TimeDelta::FromDays(kMinimumBannerBlockedToBannerShown)) { |
| 410 banners::TrackDisplayEvent(banners::DISPLAY_EVENT_BLOCKED_PREVIOUSLY); | |
| 411 return PREVIOUSLY_BLOCKED; | 411 return PREVIOUSLY_BLOCKED; |
| 412 } | 412 } |
| 413 | 413 |
| 414 base::Time shown_time = | 414 base::Time shown_time = |
| 415 GetSingleBannerEvent(web_contents, origin_url, package_name_or_start_url, | 415 GetSingleBannerEvent(web_contents, origin_url, package_name_or_start_url, |
| 416 APP_BANNER_EVENT_DID_SHOW); | 416 APP_BANNER_EVENT_DID_SHOW); |
| 417 if (time - shown_time < | 417 if (time - shown_time < |
| 418 base::TimeDelta::FromDays(kMinimumDaysBetweenBannerShows)) { | 418 base::TimeDelta::FromDays(kMinimumDaysBetweenBannerShows)) { |
| 419 banners::TrackDisplayEvent(banners::DISPLAY_EVENT_IGNORED_PREVIOUSLY); | |
| 420 return PREVIOUSLY_IGNORED; | 419 return PREVIOUSLY_IGNORED; |
| 421 } | 420 } |
| 422 | 421 |
| 423 // If we have gotten this far and want to use site engagement, the banner flow | 422 // If we have gotten this far and want to use site engagement, the banner flow |
| 424 // was triggered by the site engagement service informing the banner manager | 423 // was triggered by the site engagement service informing the banner manager |
| 425 // that sufficient engagement has been accumulated. Hence there is no need to | 424 // that sufficient engagement has been accumulated. Hence there is no need to |
| 426 // check the total amount of engagement. | 425 // check the total amount of engagement. |
| 427 // TODO(dominickn): just return true here and remove all of the following code | 426 // TODO(dominickn): just return true here and remove all of the following code |
| 428 // in this method when app banners have fully migrated to using site | 427 // in this method when app banners have fully migrated to using site |
| 429 // engagement as a trigger condition. See crbug.com/616322. | 428 // engagement as a trigger condition. See crbug.com/616322. |
| 430 if (ShouldUseSiteEngagementScore()) | 429 // Do not do engagement checks for instant app banners. |
| 430 if (ShouldUseSiteEngagementScore() || |
| 431 package_name_or_start_url == kInstantAppsKey) { |
| 431 return NO_ERROR_DETECTED; | 432 return NO_ERROR_DETECTED; |
| 433 } |
| 432 | 434 |
| 433 double total_engagement = 0; | 435 double total_engagement = 0; |
| 434 std::vector<BannerEvent> could_show_events = GetCouldShowBannerEvents( | 436 std::vector<BannerEvent> could_show_events = GetCouldShowBannerEvents( |
| 435 web_contents, origin_url, package_name_or_start_url); | 437 web_contents, origin_url, package_name_or_start_url); |
| 436 | 438 |
| 437 for (const auto& event : could_show_events) | 439 for (const auto& event : could_show_events) |
| 438 total_engagement += event.engagement; | 440 total_engagement += event.engagement; |
| 439 | 441 |
| 440 if (!HasSufficientEngagement(total_engagement)) { | 442 if (!HasSufficientEngagement(total_engagement)) |
| 441 banners::TrackDisplayEvent(banners::DISPLAY_EVENT_NOT_VISITED_ENOUGH); | |
| 442 return INSUFFICIENT_ENGAGEMENT; | 443 return INSUFFICIENT_ENGAGEMENT; |
| 443 } | |
| 444 | 444 |
| 445 return NO_ERROR_DETECTED; | 445 return NO_ERROR_DETECTED; |
| 446 } | 446 } |
| 447 | 447 |
| 448 std::vector<AppBannerSettingsHelper::BannerEvent> | 448 std::vector<AppBannerSettingsHelper::BannerEvent> |
| 449 AppBannerSettingsHelper::GetCouldShowBannerEvents( | 449 AppBannerSettingsHelper::GetCouldShowBannerEvents( |
| 450 content::WebContents* web_contents, | 450 content::WebContents* web_contents, |
| 451 const GURL& origin_url, | 451 const GURL& origin_url, |
| 452 const std::string& package_name_or_start_url) { | 452 const std::string& package_name_or_start_url) { |
| 453 std::vector<BannerEvent> result; | 453 std::vector<BannerEvent> result; |
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 558 // homescreen recently, return true. | 558 // homescreen recently, return true. |
| 559 for (base::DictionaryValue::Iterator it(*origin_dict); !it.IsAtEnd(); | 559 for (base::DictionaryValue::Iterator it(*origin_dict); !it.IsAtEnd(); |
| 560 it.Advance()) { | 560 it.Advance()) { |
| 561 if (it.value().IsType(base::Value::TYPE_DICTIONARY)) { | 561 if (it.value().IsType(base::Value::TYPE_DICTIONARY)) { |
| 562 const base::DictionaryValue* value; | 562 const base::DictionaryValue* value; |
| 563 it.value().GetAsDictionary(&value); | 563 it.value().GetAsDictionary(&value); |
| 564 | 564 |
| 565 std::string event_key( | 565 std::string event_key( |
| 566 kBannerEventKeys[APP_BANNER_EVENT_DID_ADD_TO_HOMESCREEN]); | 566 kBannerEventKeys[APP_BANNER_EVENT_DID_ADD_TO_HOMESCREEN]); |
| 567 double internal_time; | 567 double internal_time; |
| 568 if (!value->GetDouble(event_key, &internal_time)) | 568 if (it.key() == kInstantAppsKey || |
| 569 !value->GetDouble(event_key, &internal_time)) { |
| 569 continue; | 570 continue; |
| 571 } |
| 570 | 572 |
| 571 base::Time added_time = base::Time::FromInternalValue(internal_time); | 573 base::Time added_time = base::Time::FromInternalValue(internal_time); |
| 572 | 574 |
| 573 if ((now - added_time) <= | 575 if ((now - added_time) <= |
| 574 base::TimeDelta::FromDays(kRecentLastLaunchInDays)) { | 576 base::TimeDelta::FromDays(kRecentLastLaunchInDays)) { |
| 575 return true; | 577 return true; |
| 576 } | 578 } |
| 577 } | 579 } |
| 578 } | 580 } |
| 579 | 581 |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 646 return true; | 648 return true; |
| 647 } | 649 } |
| 648 | 650 |
| 649 // Assume any value which is not "0" or "false" indicates that we should use | 651 // Assume any value which is not "0" or "false" indicates that we should use |
| 650 // site engagement. | 652 // site engagement. |
| 651 std::string param = variations::GetVariationParamValue( | 653 std::string param = variations::GetVariationParamValue( |
| 652 kBannerParamsKey, kBannerSiteEngagementParamsKey); | 654 kBannerParamsKey, kBannerSiteEngagementParamsKey); |
| 653 | 655 |
| 654 return (!param.empty() && param != "0" && param != "false"); | 656 return (!param.empty() && param != "0" && param != "false"); |
| 655 } | 657 } |
| OLD | NEW |