OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/android/search_geolocation_disclosure_tab_helper.h" |
| 6 |
| 7 #include "base/feature_list.h" |
| 8 #include "base/logging.h" |
| 9 #include "chrome/browser/android/search_geolocation_disclosure_infobar_delegate.
h" |
| 10 #include "chrome/browser/permissions/permission_manager.h" |
| 11 #include "chrome/browser/profiles/profile.h" |
| 12 #include "chrome/browser/search_engines/template_url_service_factory.h" |
| 13 #include "chrome/common/chrome_features.h" |
| 14 #include "components/search_engines/template_url.h" |
| 15 #include "components/search_engines/template_url_service.h" |
| 16 #include "content/public/browser/permission_type.h" |
| 17 #include "content/public/browser/web_contents.h" |
| 18 #include "jni/GeolocationHeader_jni.h" |
| 19 |
| 20 DEFINE_WEB_CONTENTS_USER_DATA_KEY(SearchGeolocationDisclosureTabHelper); |
| 21 |
| 22 SearchGeolocationDisclosureTabHelper::SearchGeolocationDisclosureTabHelper( |
| 23 content::WebContents* contents) |
| 24 : content::WebContentsObserver(contents) { |
| 25 consistent_geolocation_enabled_ = |
| 26 base::FeatureList::IsEnabled(features::kConsistentOmniboxGeolocation); |
| 27 } |
| 28 |
| 29 SearchGeolocationDisclosureTabHelper::~SearchGeolocationDisclosureTabHelper() {} |
| 30 |
| 31 void SearchGeolocationDisclosureTabHelper::NavigationEntryCommitted( |
| 32 const content::LoadCommittedDetails& load_details) { |
| 33 if (consistent_geolocation_enabled_) { |
| 34 MaybeShowDefaultSearchGeolocationDisclosure( |
| 35 web_contents()->GetVisibleURL()); |
| 36 } |
| 37 } |
| 38 |
| 39 void SearchGeolocationDisclosureTabHelper:: |
| 40 MaybeShowDefaultSearchGeolocationDisclosure(GURL gurl) { |
| 41 TemplateURLService* template_url_service = |
| 42 TemplateURLServiceFactory::GetForProfile(GetProfile()); |
| 43 // Only show the disclosure for default search navigations from the omnibox. |
| 44 bool is_search_url = |
| 45 template_url_service->IsSearchResultsPageFromDefaultSearchProvider( |
| 46 gurl); |
| 47 if (!is_search_url) |
| 48 return; |
| 49 |
| 50 // Only show the disclosure if Google is the default search engine. |
| 51 TemplateURL* default_search = |
| 52 template_url_service->GetDefaultSearchProvider(); |
| 53 if (!default_search || |
| 54 !default_search->url_ref().HasGoogleBaseURLs( |
| 55 template_url_service->search_terms_data())) { |
| 56 return; |
| 57 } |
| 58 |
| 59 // Check that the Chrome app has geolocatin permission. |
| 60 JNIEnv* env = base::android::AttachCurrentThread(); |
| 61 if (!Java_GeolocationHeader_hasGeolocationPermission(env)) |
| 62 return; |
| 63 |
| 64 // Only show the disclosure if the geolocation permission is set to ASK |
| 65 // (i.e. has not been explicitly set or revoked). |
| 66 blink::mojom::PermissionStatus status = |
| 67 PermissionManager::Get(GetProfile()) |
| 68 ->GetPermissionStatus(content::PermissionType::GEOLOCATION, gurl, |
| 69 gurl); |
| 70 if (status != blink::mojom::PermissionStatus::ASK) |
| 71 return; |
| 72 |
| 73 // All good, let's show the disclosure. |
| 74 SearchGeolocationDisclosureInfoBarDelegate::Create(web_contents()); |
| 75 } |
| 76 |
| 77 Profile* SearchGeolocationDisclosureTabHelper::GetProfile() { |
| 78 return Profile::FromBrowserContext(web_contents()->GetBrowserContext()); |
| 79 } |
OLD | NEW |