Chromium Code Reviews| 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 "ios/chrome/browser/physical_web/start_physical_web_discovery.h" | 5 #include "ios/chrome/browser/physical_web/start_physical_web_discovery.h" |
| 6 | 6 |
| 7 #import <CoreLocation/CoreLocation.h> | 7 #import <CoreLocation/CoreLocation.h> |
| 8 | 8 |
| 9 #include "base/strings/utf_string_conversions.h" | |
|
sdefresne
2016/12/15 10:46:46
I think you can remove this include now that you u
mattreynolds
2016/12/15 18:43:24
Done.
| |
| 9 #include "components/physical_web/data_source/physical_web_data_source.h" | 10 #include "components/physical_web/data_source/physical_web_data_source.h" |
| 11 #include "components/search_engines/search_terms_data.h" | |
| 12 #include "components/search_engines/template_url_service.h" | |
| 10 #include "ios/chrome/browser/application_context.h" | 13 #include "ios/chrome/browser/application_context.h" |
| 11 #include "ios/chrome/browser/browser_state/chrome_browser_state.h" | 14 #include "ios/chrome/browser/browser_state/chrome_browser_state.h" |
| 12 #import "ios/chrome/browser/experimental_flags.h" | 15 #import "ios/chrome/browser/experimental_flags.h" |
| 13 #import "ios/chrome/browser/geolocation/omnibox_geolocation_config.h" | 16 #import "ios/chrome/browser/geolocation/omnibox_geolocation_config.h" |
| 14 #include "ios/chrome/browser/physical_web/physical_web_constants.h" | 17 #include "ios/chrome/browser/physical_web/physical_web_constants.h" |
| 15 #include "ios/chrome/browser/pref_names.h" | 18 #include "ios/chrome/browser/pref_names.h" |
| 19 #include "ios/chrome/browser/search_engines/template_url_service_factory.h" | |
| 16 #include "url/gurl.h" | 20 #include "url/gurl.h" |
| 17 | 21 |
| 18 void StartPhysicalWebDiscovery(PrefService* pref_service, bool is_incognito) { | 22 void StartPhysicalWebDiscovery(PrefService* pref_service, |
| 23 ios::ChromeBrowserState* browser_state) { | |
| 19 // Do not scan if the Physical Web feature is disabled by a command line flag | 24 // Do not scan if the Physical Web feature is disabled by a command line flag |
| 20 // or Chrome Variations experiment. | 25 // or Chrome Variations experiment. |
| 21 if (!experimental_flags::IsPhysicalWebEnabled()) { | 26 if (!experimental_flags::IsPhysicalWebEnabled()) { |
| 22 return; | 27 return; |
| 23 } | 28 } |
| 24 | 29 |
| 25 // If the preference is in the default state, check if conditions allow us to | 30 // If the preference is in the default state, check if conditions allow us to |
| 26 // auto-enable it. The preference can be auto-enabled if the omnibox would | 31 // auto-enable it. The preference can be auto-enabled if the omnibox would |
| 27 // send the X-Geo geolocation header in search queries to www.google.com. | 32 // send the X-Geo geolocation header in search queries to www.google.com. |
| 28 int preference_state = | 33 int preference_state = |
| 29 pref_service->GetInteger(prefs::kIosPhysicalWebEnabled); | 34 pref_service->GetInteger(prefs::kIosPhysicalWebEnabled); |
| 30 if (preference_state == physical_web::kPhysicalWebOnboarding) { | 35 if (preference_state == physical_web::kPhysicalWebOnboarding) { |
| 36 // Check whether the user is in Incognito mode. Physical Web will only be | |
| 37 // auto-enabled if the user is not in Incognito. | |
| 38 bool is_incognito = browser_state->IsOffTheRecord(); | |
| 39 | |
| 31 // Check that Location Services is enabled. | 40 // Check that Location Services is enabled. |
| 32 bool location_services_enabled = | 41 bool location_services_enabled = |
| 33 [CLLocationManager locationServicesEnabled]; | 42 [CLLocationManager locationServicesEnabled]; |
| 34 | 43 |
| 35 // Check that the user has authorized Chrome to use location. | 44 // Check that the user has authorized Chrome to use location. |
| 36 CLAuthorizationStatus auth_status = [CLLocationManager authorizationStatus]; | 45 CLAuthorizationStatus auth_status = [CLLocationManager authorizationStatus]; |
| 37 bool location_authorized = | 46 bool location_authorized = |
| 38 auth_status == kCLAuthorizationStatusAuthorizedWhenInUse || | 47 auth_status == kCLAuthorizationStatusAuthorizedWhenInUse || |
| 39 auth_status == kCLAuthorizationStatusAuthorizedAlways; | 48 auth_status == kCLAuthorizationStatusAuthorizedAlways; |
| 40 | 49 |
| 41 // Check that the user has not revoked the geolocation permission for | 50 // Check that the user has not revoked the geolocation permission for |
| 42 // Google. | 51 // Google. |
| 43 bool geolocation_eligible = [[OmniboxGeolocationConfig sharedInstance] | 52 bool geolocation_eligible = [[OmniboxGeolocationConfig sharedInstance] |
| 44 URLHasEligibleDomain:GURL("https://www.google.com")]; | 53 URLHasEligibleDomain:GURL("https://www.google.com")]; |
| 45 | 54 |
| 46 if (!is_incognito && location_services_enabled && location_authorized && | 55 // Check that Google Search is configured as the default search engine. |
| 47 geolocation_eligible) { | 56 const TemplateURL* default_search_provider = |
| 48 pref_service->SetInteger(prefs::kIosPhysicalWebEnabled, | 57 ios::TemplateURLServiceFactory::GetForBrowserState(browser_state) |
| 49 physical_web::kPhysicalWebOn); | 58 ->GetDefaultSearchProvider(); |
| 50 preference_state = | 59 bool google_search_enabled = |
| 51 pref_service->GetInteger(prefs::kIosPhysicalWebEnabled); | 60 default_search_provider->IsGoogleSearchURLWithReplaceableKeyword( |
| 52 } | 61 SearchTermsData()); |
| 62 | |
| 63 bool auto_enable = !is_incognito && location_services_enabled && | |
| 64 location_authorized && geolocation_eligible && | |
| 65 google_search_enabled; | |
| 66 | |
| 67 pref_service->SetInteger(prefs::kIosPhysicalWebEnabled, | |
| 68 auto_enable ? physical_web::kPhysicalWebOn | |
| 69 : physical_web::kPhysicalWebOff); | |
| 70 preference_state = pref_service->GetInteger(prefs::kIosPhysicalWebEnabled); | |
| 53 } | 71 } |
| 54 | 72 |
| 55 // Scan only if the feature is enabled. | 73 // Scan only if the feature is enabled. |
| 56 if (preference_state == physical_web::kPhysicalWebOn) { | 74 if (preference_state == physical_web::kPhysicalWebOn) { |
| 57 GetApplicationContext()->GetPhysicalWebDataSource()->StartDiscovery(true); | 75 GetApplicationContext()->GetPhysicalWebDataSource()->StartDiscovery(true); |
| 58 } else { | 76 } else { |
| 59 GetApplicationContext()->GetPhysicalWebDataSource()->StopDiscovery(); | 77 GetApplicationContext()->GetPhysicalWebDataSource()->StopDiscovery(); |
| 60 } | 78 } |
| 61 } | 79 } |
| 62 | 80 |
| 63 void StartPhysicalWebDiscovery(PrefService* pref_service, | |
| 64 ios::ChromeBrowserState* browser_state) { | |
| 65 StartPhysicalWebDiscovery(pref_service, browser_state->IsOffTheRecord()); | |
| 66 } | |
| OLD | NEW |