Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(905)

Side by Side Diff: ios/chrome/browser/physical_web/start_physical_web_discovery.mm

Issue 2458613004: Record the initial state of the Physical Web at startup (Closed)
Patch Set: Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 "components/physical_web/data_source/physical_web_data_source.h" 9 #include "components/physical_web/data_source/physical_web_data_source.h"
10 #include "ios/chrome/browser/application_context.h" 10 #include "ios/chrome/browser/application_context.h"
11 #import "ios/chrome/browser/experimental_flags.h" 11 #import "ios/chrome/browser/experimental_flags.h"
12 #import "ios/chrome/browser/geolocation/omnibox_geolocation_config.h" 12 #import "ios/chrome/browser/geolocation/omnibox_geolocation_config.h"
13 #include "ios/chrome/browser/physical_web/physical_web_constants.h" 13 #include "ios/chrome/browser/physical_web/physical_web_constants.h"
14 #import "ios/chrome/browser/physical_web/physical_web_initial_state_recorder.h"
14 #include "ios/chrome/browser/pref_names.h" 15 #include "ios/chrome/browser/pref_names.h"
15 #include "url/gurl.h" 16 #include "url/gurl.h"
16 17
17 void StartPhysicalWebDiscovery(PrefService* pref_service, bool is_incognito) { 18 void StartPhysicalWebDiscovery(PrefService* pref_service, bool is_incognito) {
18 // Do not scan if the Physical Web feature is disabled by a command line flag 19 // Do not scan if the Physical Web feature is disabled by a command line flag
19 // or Chrome Variations experiment. 20 // or Chrome Variations experiment.
20 if (!experimental_flags::IsPhysicalWebEnabled()) { 21 if (!experimental_flags::IsPhysicalWebEnabled()) {
21 return; 22 return;
22 } 23 }
23 24
24 // If the preference is in the default state, check if conditions allow us to 25 // If the preference is in the default state, check if conditions allow us to
25 // auto-enable it. The preference can be auto-enabled if the omnibox would 26 // auto-enable it. The preference can be auto-enabled if the omnibox would
26 // send the X-Geo geolocation header in search queries to www.google.com. 27 // send the X-Geo geolocation header in search queries to www.google.com.
27 int preference_state = 28 int preference_state =
28 pref_service->GetInteger(prefs::kIosPhysicalWebEnabled); 29 pref_service->GetInteger(prefs::kIosPhysicalWebEnabled);
30
31 // Check that Location Services is enabled.
32 bool location_services_enabled = [CLLocationManager locationServicesEnabled];
33
34 // Check that the user has authorized Chrome to use location.
35 CLAuthorizationStatus auth_status = [CLLocationManager authorizationStatus];
36 bool location_authorized =
37 auth_status == kCLAuthorizationStatusAuthorizedWhenInUse ||
38 auth_status == kCLAuthorizationStatusAuthorizedAlways;
39
40 // Record the initial state of Bluetooth, location, and the Physical Web
41 // preference the first time discovery is started.
42 static dispatch_once_t once_token;
43 dispatch_once(&once_token, ^{
44 [[[PhysicalWebInitialStateRecorder alloc]
45 initWithPreferenceState:preference_state
46 locationServicesEnabled:location_services_enabled
47 locationAuthorized:location_authorized] autorelease];
mattreynolds 2016/10/27 23:56:04 Not sure if this is kosher. The idea is to let the
rohitrao (ping after 24h) 2016/10/31 19:41:08 I think this works, but I don't really like it. T
mattreynolds 2016/11/01 17:17:17 Agreed, I'd rather not leave it like this. The rea
48 });
49
29 if (preference_state == physical_web::kPhysicalWebOnboarding) { 50 if (preference_state == physical_web::kPhysicalWebOnboarding) {
30 // Check that Location Services is enabled.
31 bool location_services_enabled =
32 [CLLocationManager locationServicesEnabled];
33
34 // Check that the user has authorized Chrome to use location.
35 CLAuthorizationStatus auth_status = [CLLocationManager authorizationStatus];
36 bool location_authorized =
37 auth_status == kCLAuthorizationStatusAuthorizedWhenInUse ||
38 auth_status == kCLAuthorizationStatusAuthorizedAlways;
39
40 // Check that the user has not revoked the geolocation permission for 51 // Check that the user has not revoked the geolocation permission for
41 // Google. 52 // Google.
42 bool geolocation_eligible = [[OmniboxGeolocationConfig sharedInstance] 53 bool geolocation_eligible = [[OmniboxGeolocationConfig sharedInstance]
43 URLHasEligibleDomain:GURL("https://www.google.com")]; 54 URLHasEligibleDomain:GURL("https://www.google.com")];
44 55
45 if (!is_incognito && location_services_enabled && location_authorized && 56 if (!is_incognito && location_services_enabled && location_authorized &&
46 geolocation_eligible) { 57 geolocation_eligible) {
47 pref_service->SetInteger(prefs::kIosPhysicalWebEnabled, 58 pref_service->SetInteger(prefs::kIosPhysicalWebEnabled,
48 physical_web::kPhysicalWebOn); 59 physical_web::kPhysicalWebOn);
49 preference_state = 60 preference_state =
50 pref_service->GetInteger(prefs::kIosPhysicalWebEnabled); 61 pref_service->GetInteger(prefs::kIosPhysicalWebEnabled);
51 } 62 }
52 } 63 }
53 64
54 // Scan only if the feature is enabled. 65 // Scan only if the feature is enabled.
55 if (preference_state == physical_web::kPhysicalWebOn) { 66 if (preference_state == physical_web::kPhysicalWebOn) {
56 GetApplicationContext()->GetPhysicalWebDataSource()->StartDiscovery(true); 67 GetApplicationContext()->GetPhysicalWebDataSource()->StartDiscovery(true);
57 } else { 68 } else {
58 GetApplicationContext()->GetPhysicalWebDataSource()->StopDiscovery(); 69 GetApplicationContext()->GetPhysicalWebDataSource()->StopDiscovery();
59 } 70 }
60 } 71 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698