| OLD | NEW | 
|    1 // Copyright 2016 The Chromium Authors. All rights reserved. |    1 // Copyright 2016 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/android/search_geolocation_disclosure_tab_helper.h" |    5 #include "chrome/browser/android/search_geolocation_disclosure_tab_helper.h" | 
|    6  |    6  | 
|    7 #include "base/feature_list.h" |    7 #include "base/feature_list.h" | 
|    8 #include "base/logging.h" |    8 #include "base/logging.h" | 
 |    9 #include "base/metrics/histogram_macros.h" | 
|    9 #include "base/strings/string_number_conversions.h" |   10 #include "base/strings/string_number_conversions.h" | 
|   10 #include "base/time/time.h" |   11 #include "base/time/time.h" | 
|   11 #include "chrome/browser/android/search_geolocation_disclosure_infobar_delegate.
     h" |   12 #include "chrome/browser/android/search_geolocation_disclosure_infobar_delegate.
     h" | 
|   12 #include "chrome/browser/permissions/permission_manager.h" |   13 #include "chrome/browser/permissions/permission_manager.h" | 
|   13 #include "chrome/browser/profiles/profile.h" |   14 #include "chrome/browser/profiles/profile.h" | 
|   14 #include "chrome/browser/search_engines/template_url_service_factory.h" |   15 #include "chrome/browser/search_engines/template_url_service_factory.h" | 
|   15 #include "chrome/common/chrome_features.h" |   16 #include "chrome/common/chrome_features.h" | 
|   16 #include "chrome/common/pref_names.h" |   17 #include "chrome/common/pref_names.h" | 
|   17 #include "components/pref_registry/pref_registry_syncable.h" |   18 #include "components/pref_registry/pref_registry_syncable.h" | 
|   18 #include "components/prefs/pref_service.h" |   19 #include "components/prefs/pref_service.h" | 
|   19 #include "components/search_engines/template_url.h" |   20 #include "components/search_engines/template_url.h" | 
|   20 #include "components/search_engines/template_url_service.h" |   21 #include "components/search_engines/template_url_service.h" | 
|   21 #include "components/variations/variations_associated_data.h" |   22 #include "components/variations/variations_associated_data.h" | 
|   22 #include "content/public/browser/permission_type.h" |   23 #include "content/public/browser/permission_type.h" | 
|   23 #include "content/public/browser/web_contents.h" |   24 #include "content/public/browser/web_contents.h" | 
|   24 #include "jni/GeolocationHeader_jni.h" |   25 #include "jni/GeolocationHeader_jni.h" | 
 |   26 #include "third_party/WebKit/public/platform/modules/permissions/permission_stat
     us.mojom.h" | 
|   25  |   27  | 
|   26 namespace { |   28 namespace { | 
|   27  |   29  | 
|   28 const int kDefaultMaxShowCount = 3; |   30 const int kDefaultMaxShowCount = 3; | 
|   29 const int kDefaultDaysPerShow = 1; |   31 const int kDefaultDaysPerShow = 1; | 
|   30 const char kMaxShowCountVariation[] = "MaxShowCount"; |   32 const char kMaxShowCountVariation[] = "MaxShowCount"; | 
|   31 const char kDaysPerShowVariation[] = "DaysPerShow"; |   33 const char kDaysPerShowVariation[] = "DaysPerShow"; | 
|   32  |   34  | 
|   33 int GetMaxShowCount() { |   35 int GetMaxShowCount() { | 
|   34   std::string variation = variations::GetVariationParamValueByFeature( |   36   std::string variation = variations::GetVariationParamValueByFeature( | 
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|   73  |   75  | 
|   74 // static |   76 // static | 
|   75 void SearchGeolocationDisclosureTabHelper::RegisterProfilePrefs( |   77 void SearchGeolocationDisclosureTabHelper::RegisterProfilePrefs( | 
|   76     user_prefs::PrefRegistrySyncable* registry) { |   78     user_prefs::PrefRegistrySyncable* registry) { | 
|   77   registry->RegisterBooleanPref(prefs::kSearchGeolocationDisclosureDismissed, |   79   registry->RegisterBooleanPref(prefs::kSearchGeolocationDisclosureDismissed, | 
|   78                                 false); |   80                                 false); | 
|   79   registry->RegisterIntegerPref(prefs::kSearchGeolocationDisclosureShownCount, |   81   registry->RegisterIntegerPref(prefs::kSearchGeolocationDisclosureShownCount, | 
|   80                                 0); |   82                                 0); | 
|   81   registry->RegisterInt64Pref(prefs::kSearchGeolocationDisclosureLastShowDate, |   83   registry->RegisterInt64Pref(prefs::kSearchGeolocationDisclosureLastShowDate, | 
|   82                               0); |   84                               0); | 
 |   85   registry->RegisterBooleanPref( | 
 |   86       prefs::kSearchGeolocationPreDisclosureMetricsRecorded, false); | 
 |   87   registry->RegisterBooleanPref( | 
 |   88       prefs::kSearchGeolocationPostDisclosureMetricsRecorded, false); | 
|   83 } |   89 } | 
|   84  |   90  | 
|   85 void SearchGeolocationDisclosureTabHelper:: |   91 void SearchGeolocationDisclosureTabHelper:: | 
|   86     MaybeShowDefaultSearchGeolocationDisclosure(GURL gurl) { |   92     MaybeShowDefaultSearchGeolocationDisclosure(const GURL& gurl) { | 
|   87   // Don't show the infobar if the user has dismissed it. |  | 
|   88   PrefService* prefs = GetProfile()->GetPrefs(); |  | 
|   89   bool dismissed_already = |  | 
|   90       prefs->GetBoolean(prefs::kSearchGeolocationDisclosureDismissed); |  | 
|   91   if (dismissed_already) |  | 
|   92     return; |  | 
|   93  |  | 
|   94   // Or if they've seen it enough times already. |  | 
|   95   int shown_count = |  | 
|   96       prefs->GetInteger(prefs::kSearchGeolocationDisclosureShownCount); |  | 
|   97   if (shown_count >= GetMaxShowCount()) |  | 
|   98     return; |  | 
|   99  |  | 
|  100   // Or if it has been shown too recently. |  | 
|  101   base::Time last_shown = base::Time::FromInternalValue( |  | 
|  102       prefs->GetInt64(prefs::kSearchGeolocationDisclosureLastShowDate)); |  | 
|  103   if (base::Time::Now() - last_shown < |  | 
|  104       base::TimeDelta::FromDays(GetDaysPerShow())) { |  | 
|  105     return; |  | 
|  106   } |  | 
|  107  |  | 
|  108   // Only show the disclosure for default search navigations from the omnibox. |   93   // Only show the disclosure for default search navigations from the omnibox. | 
|  109   TemplateURLService* template_url_service = |   94   TemplateURLService* template_url_service = | 
|  110       TemplateURLServiceFactory::GetForProfile(GetProfile()); |   95       TemplateURLServiceFactory::GetForProfile(GetProfile()); | 
|  111   bool is_search_url = |   96   bool is_search_url = | 
|  112       template_url_service->IsSearchResultsPageFromDefaultSearchProvider( |   97       template_url_service->IsSearchResultsPageFromDefaultSearchProvider( | 
|  113           gurl); |   98           gurl); | 
|  114   if (!is_search_url) |   99   if (!is_search_url) | 
|  115     return; |  100     return; | 
|  116  |  101  | 
|  117   // Only show the disclosure if Google is the default search engine. |  102   // Only show the disclosure if Google is the default search engine. | 
|  118   TemplateURL* default_search = |  103   TemplateURL* default_search = | 
|  119       template_url_service->GetDefaultSearchProvider(); |  104       template_url_service->GetDefaultSearchProvider(); | 
|  120   if (!default_search || |  105   if (!default_search || | 
|  121       !default_search->url_ref().HasGoogleBaseURLs( |  106       !default_search->url_ref().HasGoogleBaseURLs( | 
|  122           template_url_service->search_terms_data())) { |  107           template_url_service->search_terms_data())) { | 
|  123     return; |  108     return; | 
|  124   } |  109   } | 
|  125  |  110  | 
 |  111   // Don't show the infobar if the user has dismissed it, or they've seen it | 
 |  112   // enough times already. | 
 |  113   PrefService* prefs = GetProfile()->GetPrefs(); | 
 |  114   bool dismissed_already = | 
 |  115       prefs->GetBoolean(prefs::kSearchGeolocationDisclosureDismissed); | 
 |  116   int shown_count = | 
 |  117       prefs->GetInteger(prefs::kSearchGeolocationDisclosureShownCount); | 
 |  118   if (dismissed_already || shown_count >= GetMaxShowCount()) { | 
 |  119     // Record metrics for the state of permissions after the disclosure has been | 
 |  120     // shown. This is not done immediately after showing the last disclosure | 
 |  121     // (i.e. at the end of this function), but on the next omnibox search, to | 
 |  122     // allow the metric to capture changes to settings done by the user as a | 
 |  123     // result of clicking on the Settings link in the disclosure. | 
 |  124     RecordPostDisclosureMetrics(gurl); | 
 |  125     return; | 
 |  126   } | 
 |  127  | 
 |  128   // Or if it has been shown too recently. | 
 |  129   base::Time last_shown = base::Time::FromInternalValue( | 
 |  130       prefs->GetInt64(prefs::kSearchGeolocationDisclosureLastShowDate)); | 
 |  131   if (base::Time::Now() - last_shown < | 
 |  132       base::TimeDelta::FromDays(GetDaysPerShow())) { | 
 |  133     return; | 
 |  134   } | 
 |  135  | 
|  126   // Check that the Chrome app has geolocation permission. |  136   // Check that the Chrome app has geolocation permission. | 
|  127   JNIEnv* env = base::android::AttachCurrentThread(); |  137   JNIEnv* env = base::android::AttachCurrentThread(); | 
|  128   if (!Java_GeolocationHeader_hasGeolocationPermission(env)) |  138   if (!Java_GeolocationHeader_hasGeolocationPermission(env)) | 
|  129     return; |  139     return; | 
|  130  |  140  | 
|  131   // Only show the disclosure if the geolocation permission is set to ASK |  141   // Only show the disclosure if the geolocation permission is set to ASK | 
|  132   // (i.e. has not been explicitly set or revoked). |  142   // (i.e. has not been explicitly set or revoked). | 
|  133   blink::mojom::PermissionStatus status = |  143   blink::mojom::PermissionStatus status = | 
|  134       PermissionManager::Get(GetProfile()) |  144       PermissionManager::Get(GetProfile()) | 
|  135           ->GetPermissionStatus(content::PermissionType::GEOLOCATION, gurl, |  145           ->GetPermissionStatus(content::PermissionType::GEOLOCATION, gurl, | 
|  136                                 gurl); |  146                                 gurl); | 
|  137   if (status != blink::mojom::PermissionStatus::ASK) |  147   if (status != blink::mojom::PermissionStatus::ASK) | 
|  138     return; |  148     return; | 
|  139  |  149  | 
 |  150   // Record metrics for the state of permissions before the disclosure has been | 
 |  151   // shown. | 
 |  152   RecordPreDisclosureMetrics(gurl); | 
 |  153  | 
|  140   // All good, let's show the disclosure and increment the shown count. |  154   // All good, let's show the disclosure and increment the shown count. | 
|  141   SearchGeolocationDisclosureInfoBarDelegate::Create(web_contents(), gurl); |  155   SearchGeolocationDisclosureInfoBarDelegate::Create(web_contents(), gurl); | 
|  142   shown_count++; |  156   shown_count++; | 
|  143   prefs->SetInteger(prefs::kSearchGeolocationDisclosureShownCount, shown_count); |  157   prefs->SetInteger(prefs::kSearchGeolocationDisclosureShownCount, shown_count); | 
|  144   prefs->SetInt64(prefs::kSearchGeolocationDisclosureLastShowDate, |  158   prefs->SetInt64(prefs::kSearchGeolocationDisclosureLastShowDate, | 
|  145                   base::Time::Now().ToInternalValue()); |  159                   base::Time::Now().ToInternalValue()); | 
|  146 } |  160 } | 
|  147  |  161  | 
 |  162 void SearchGeolocationDisclosureTabHelper::RecordPreDisclosureMetrics( | 
 |  163     const GURL& gurl) { | 
 |  164   PrefService* prefs = GetProfile()->GetPrefs(); | 
 |  165   if (!prefs->GetBoolean( | 
 |  166           prefs::kSearchGeolocationPreDisclosureMetricsRecorded)) { | 
 |  167     blink::mojom::PermissionStatus status = | 
 |  168         PermissionManager::Get(GetProfile()) | 
 |  169             ->GetPermissionStatus(content::PermissionType::GEOLOCATION, gurl, | 
 |  170                                   gurl); | 
 |  171     UMA_HISTOGRAM_ENUMERATION("GeolocationDisclosure.PreDisclosurePermission", | 
 |  172                               static_cast<base::HistogramBase::Sample>(status), | 
 |  173                               static_cast<base::HistogramBase::Sample>( | 
 |  174                                   blink::mojom::PermissionStatus::LAST) + | 
 |  175                                   1); | 
 |  176     prefs->SetBoolean(prefs::kSearchGeolocationPreDisclosureMetricsRecorded, | 
 |  177                       true); | 
 |  178   } | 
 |  179 } | 
 |  180  | 
 |  181 void SearchGeolocationDisclosureTabHelper::RecordPostDisclosureMetrics( | 
 |  182     const GURL& gurl) { | 
 |  183   PrefService* prefs = GetProfile()->GetPrefs(); | 
 |  184   if (!prefs->GetBoolean( | 
 |  185           prefs::kSearchGeolocationPostDisclosureMetricsRecorded)) { | 
 |  186     blink::mojom::PermissionStatus status = | 
 |  187         PermissionManager::Get(GetProfile()) | 
 |  188             ->GetPermissionStatus(content::PermissionType::GEOLOCATION, gurl, | 
 |  189                                   gurl); | 
 |  190     UMA_HISTOGRAM_ENUMERATION("GeolocationDisclosure.PostDisclosurePermission", | 
 |  191                               static_cast<base::HistogramBase::Sample>(status), | 
 |  192                               static_cast<base::HistogramBase::Sample>( | 
 |  193                                   blink::mojom::PermissionStatus::LAST) + | 
 |  194                                   1); | 
 |  195     prefs->SetBoolean(prefs::kSearchGeolocationPostDisclosureMetricsRecorded, | 
 |  196                       true); | 
 |  197   } | 
 |  198 } | 
 |  199  | 
|  148 Profile* SearchGeolocationDisclosureTabHelper::GetProfile() { |  200 Profile* SearchGeolocationDisclosureTabHelper::GetProfile() { | 
|  149   return Profile::FromBrowserContext(web_contents()->GetBrowserContext()); |  201   return Profile::FromBrowserContext(web_contents()->GetBrowserContext()); | 
|  150 } |  202 } | 
| OLD | NEW |