| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 #import "ios/chrome/browser/geolocation/omnibox_geolocation_controller.h" | 5 #import "ios/chrome/browser/geolocation/omnibox_geolocation_controller.h" |
| 6 | 6 |
| 7 #import <CoreLocation/CoreLocation.h> | 7 #import <CoreLocation/CoreLocation.h> |
| 8 #import <UIKit/UIKit.h> | 8 #import <UIKit/UIKit.h> |
| 9 | 9 |
| 10 #include <string> | 10 #include <string> |
| 11 | 11 |
| 12 #import "base/ios/weak_nsobject.h" | |
| 13 #include "base/logging.h" | 12 #include "base/logging.h" |
| 14 #include "base/mac/scoped_nsobject.h" | |
| 15 #include "base/metrics/histogram_macros.h" | 13 #include "base/metrics/histogram_macros.h" |
| 16 #include "base/version.h" | 14 #include "base/version.h" |
| 17 #include "components/google/core/browser/google_util.h" | 15 #include "components/google/core/browser/google_util.h" |
| 18 #include "components/version_info/version_info.h" | 16 #include "components/version_info/version_info.h" |
| 19 #include "ios/chrome/browser/browser_state/chrome_browser_state.h" | 17 #include "ios/chrome/browser/browser_state/chrome_browser_state.h" |
| 20 #import "ios/chrome/browser/geolocation/CLLocation+OmniboxGeolocation.h" | 18 #import "ios/chrome/browser/geolocation/CLLocation+OmniboxGeolocation.h" |
| 21 #import "ios/chrome/browser/geolocation/CLLocation+XGeoHeader.h" | 19 #import "ios/chrome/browser/geolocation/CLLocation+XGeoHeader.h" |
| 22 #import "ios/chrome/browser/geolocation/location_manager.h" | 20 #import "ios/chrome/browser/geolocation/location_manager.h" |
| 23 #import "ios/chrome/browser/geolocation/omnibox_geolocation_authorization_alert.
h" | 21 #import "ios/chrome/browser/geolocation/omnibox_geolocation_authorization_alert.
h" |
| 24 #import "ios/chrome/browser/geolocation/omnibox_geolocation_config.h" | 22 #import "ios/chrome/browser/geolocation/omnibox_geolocation_config.h" |
| 25 #import "ios/chrome/browser/geolocation/omnibox_geolocation_controller+Testing.h
" | 23 #import "ios/chrome/browser/geolocation/omnibox_geolocation_controller+Testing.h
" |
| 26 #import "ios/chrome/browser/geolocation/omnibox_geolocation_local_state.h" | 24 #import "ios/chrome/browser/geolocation/omnibox_geolocation_local_state.h" |
| 27 #import "ios/chrome/browser/tabs/tab.h" | 25 #import "ios/chrome/browser/tabs/tab.h" |
| 28 #include "ios/web/public/navigation_item.h" | 26 #include "ios/web/public/navigation_item.h" |
| 29 #import "ios/web/public/navigation_manager.h" | 27 #import "ios/web/public/navigation_manager.h" |
| 30 #include "url/gurl.h" | 28 #include "url/gurl.h" |
| 31 | 29 |
| 30 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 31 #error "This file requires ARC support." |
| 32 #endif |
| 33 |
| 32 namespace { | 34 namespace { |
| 33 | 35 |
| 34 // Values for the histogram that records whether we sent the X-Geo header for | 36 // Values for the histogram that records whether we sent the X-Geo header for |
| 35 // an Omnibox query or why we did not do so. These match the definition of | 37 // an Omnibox query or why we did not do so. These match the definition of |
| 36 // GeolocationHeaderSentOrNot in Chromium | 38 // GeolocationHeaderSentOrNot in Chromium |
| 37 // src-internal/tools/histograms/histograms.xml. | 39 // src-internal/tools/histograms/histograms.xml. |
| 38 typedef enum { | 40 typedef enum { |
| 39 // The user disabled location for Google.com (not used by Chrome iOS). | 41 // The user disabled location for Google.com (not used by Chrome iOS). |
| 40 kHeaderStateNotSentAuthorizationGoogleDenied = 0, | 42 kHeaderStateNotSentAuthorizationGoogleDenied = 0, |
| 41 // The user has not yet determined Chrome's access to the current device | 43 // The user has not yet determined Chrome's access to the current device |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 90 | 92 |
| 91 // Name of the histogram recording AuthorizationAction for a new user. | 93 // Name of the histogram recording AuthorizationAction for a new user. |
| 92 const char* const kGeolocationAuthorizationActionNewUser = | 94 const char* const kGeolocationAuthorizationActionNewUser = |
| 93 "Geolocation.AuthorizationActionNewUser"; | 95 "Geolocation.AuthorizationActionNewUser"; |
| 94 | 96 |
| 95 } // anonymous namespace | 97 } // anonymous namespace |
| 96 | 98 |
| 97 @interface OmniboxGeolocationController ()< | 99 @interface OmniboxGeolocationController ()< |
| 98 LocationManagerDelegate, | 100 LocationManagerDelegate, |
| 99 OmniboxGeolocationAuthorizationAlertDelegate> { | 101 OmniboxGeolocationAuthorizationAlertDelegate> { |
| 100 base::scoped_nsobject<OmniboxGeolocationLocalState> localState_; | 102 OmniboxGeolocationLocalState* localState_; |
| 101 base::scoped_nsobject<LocationManager> locationManager_; | 103 LocationManager* locationManager_; |
| 102 base::scoped_nsobject<OmniboxGeolocationAuthorizationAlert> | 104 OmniboxGeolocationAuthorizationAlert* authorizationAlert_; |
| 103 authorizationAlert_; | 105 __weak Tab* weakTabToReload_; |
| 104 base::WeakNSObject<Tab> weakTabToReload_; | |
| 105 | 106 |
| 106 // Records whether we have deliberately presented the system prompt, so that | 107 // Records whether we have deliberately presented the system prompt, so that |
| 107 // we can record the user's action in | 108 // we can record the user's action in |
| 108 // locationManagerDidChangeAuthorizationStatus:. | 109 // locationManagerDidChangeAuthorizationStatus:. |
| 109 BOOL systemPrompt_; | 110 BOOL systemPrompt_; |
| 110 | 111 |
| 111 // Records whether we are prompting for a new user, so that we can record the | 112 // Records whether we are prompting for a new user, so that we can record the |
| 112 // user's action to the right histogram (either | 113 // user's action to the right histogram (either |
| 113 // kGeolocationAuthorizationActionExistingUser or | 114 // kGeolocationAuthorizationActionExistingUser or |
| 114 // kGeolocationAuthorizationActionNewUser). | 115 // kGeolocationAuthorizationActionNewUser). |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 184 // TODO(crbug.com/661996): Remove the now useless | 185 // TODO(crbug.com/661996): Remove the now useless |
| 185 // kAuthorizationStateNotDeterminedSystemPrompt from | 186 // kAuthorizationStateNotDeterminedSystemPrompt from |
| 186 // omnibox_geolocation_local_state.h. | 187 // omnibox_geolocation_local_state.h. |
| 187 systemPrompt_ = YES; | 188 systemPrompt_ = YES; |
| 188 self.localState.authorizationState = | 189 self.localState.authorizationState = |
| 189 geolocation::kAuthorizationStateNotDeterminedSystemPrompt; | 190 geolocation::kAuthorizationStateNotDeterminedSystemPrompt; |
| 190 | 191 |
| 191 // Turn on location updates, so that iOS will prompt the user. | 192 // Turn on location updates, so that iOS will prompt the user. |
| 192 [self startUpdatingLocation]; | 193 [self startUpdatingLocation]; |
| 193 | 194 |
| 194 weakTabToReload_.reset(); | 195 weakTabToReload_ = nil; |
| 195 newUser_ = newUser; | 196 newUser_ = newUser; |
| 196 } | 197 } |
| 197 } | 198 } |
| 198 | 199 |
| 199 - (void)locationBarDidBecomeFirstResponder: | 200 - (void)locationBarDidBecomeFirstResponder: |
| 200 (ios::ChromeBrowserState*)browserState { | 201 (ios::ChromeBrowserState*)browserState { |
| 201 if (self.enabled && browserState && !browserState->IsOffTheRecord()) { | 202 if (self.enabled && browserState && !browserState->IsOffTheRecord()) { |
| 202 [self startUpdatingLocation]; | 203 [self startUpdatingLocation]; |
| 203 } | 204 } |
| 204 } | 205 } |
| (...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 336 // Set |systemPrompt_|, so that | 337 // Set |systemPrompt_|, so that |
| 337 // locationManagerDidChangeAuthorizationStatus: will know that any | 338 // locationManagerDidChangeAuthorizationStatus: will know that any |
| 338 // CLAuthorizationStatus changes are coming from this specific prompt. | 339 // CLAuthorizationStatus changes are coming from this specific prompt. |
| 339 systemPrompt_ = YES; | 340 systemPrompt_ = YES; |
| 340 self.localState.authorizationState = | 341 self.localState.authorizationState = |
| 341 geolocation::kAuthorizationStateNotDeterminedSystemPrompt; | 342 geolocation::kAuthorizationStateNotDeterminedSystemPrompt; |
| 342 [self startUpdatingLocation]; | 343 [self startUpdatingLocation]; |
| 343 | 344 |
| 344 // Save this tab in case we're able to transition to | 345 // Save this tab in case we're able to transition to |
| 345 // kAuthorizationStateAuthorized. | 346 // kAuthorizationStateAuthorized. |
| 346 weakTabToReload_.reset(tab); | 347 weakTabToReload_ = tab; |
| 347 break; | 348 break; |
| 348 | 349 |
| 349 case kCLAuthorizationStatusRestricted: | 350 case kCLAuthorizationStatusRestricted: |
| 350 case kCLAuthorizationStatusDenied: | 351 case kCLAuthorizationStatusDenied: |
| 351 break; | 352 break; |
| 352 | 353 |
| 353 case kCLAuthorizationStatusAuthorizedAlways: | 354 case kCLAuthorizationStatusAuthorizedAlways: |
| 354 case kCLAuthorizationStatusAuthorizedWhenInUse: | 355 case kCLAuthorizationStatusAuthorizedWhenInUse: |
| 355 // We might be in state kAuthorizationStateNotDeterminedSystemPrompt here | 356 // We might be in state kAuthorizationStateNotDeterminedSystemPrompt here |
| 356 // if we presented the iOS system location alert when | 357 // if we presented the iOS system location alert when |
| (...skipping 21 matching lines...) Expand all Loading... |
| 378 #pragma mark - Private | 379 #pragma mark - Private |
| 379 | 380 |
| 380 - (BOOL)enabled { | 381 - (BOOL)enabled { |
| 381 return self.locationManager.locationServicesEnabled && | 382 return self.locationManager.locationServicesEnabled && |
| 382 self.localState.authorizationState == | 383 self.localState.authorizationState == |
| 383 geolocation::kAuthorizationStateAuthorized; | 384 geolocation::kAuthorizationStateAuthorized; |
| 384 } | 385 } |
| 385 | 386 |
| 386 - (OmniboxGeolocationLocalState*)localState { | 387 - (OmniboxGeolocationLocalState*)localState { |
| 387 if (!localState_) { | 388 if (!localState_) { |
| 388 localState_.reset([[OmniboxGeolocationLocalState alloc] | 389 localState_ = [[OmniboxGeolocationLocalState alloc] |
| 389 initWithLocationManager:self.locationManager]); | 390 initWithLocationManager:self.locationManager]; |
| 390 } | 391 } |
| 391 return localState_; | 392 return localState_; |
| 392 } | 393 } |
| 393 | 394 |
| 394 - (LocationManager*)locationManager { | 395 - (LocationManager*)locationManager { |
| 395 if (!locationManager_) { | 396 if (!locationManager_) { |
| 396 locationManager_.reset([[LocationManager alloc] init]); | 397 locationManager_ = [[LocationManager alloc] init]; |
| 397 [locationManager_ setDelegate:self]; | 398 [locationManager_ setDelegate:self]; |
| 398 } | 399 } |
| 399 return locationManager_; | 400 return locationManager_; |
| 400 } | 401 } |
| 401 | 402 |
| 402 - (BOOL)URLIsEligibleQueryURL:(const GURL&)url | 403 - (BOOL)URLIsEligibleQueryURL:(const GURL&)url |
| 403 transition:(ui::PageTransition)transition { | 404 transition:(ui::PageTransition)transition { |
| 404 return [self URLIsQueryURL:url transition:transition] && | 405 return [self URLIsQueryURL:url transition:transition] && |
| 405 [[OmniboxGeolocationConfig sharedInstance] URLHasEligibleDomain:url]; | 406 [[OmniboxGeolocationConfig sharedInstance] URLHasEligibleDomain:url]; |
| 406 } | 407 } |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 469 return YES; | 470 return YES; |
| 470 | 471 |
| 471 base::Version currentVersion(version_info::GetVersionNumber()); | 472 base::Version currentVersion(version_info::GetVersionNumber()); |
| 472 DCHECK(currentVersion.IsValid()); | 473 DCHECK(currentVersion.IsValid()); |
| 473 return currentVersion.components()[0] != previousVersion.components()[0]; | 474 return currentVersion.components()[0] != previousVersion.components()[0]; |
| 474 } | 475 } |
| 475 | 476 |
| 476 - (void)showAuthorizationAlertForTab:(Tab*)tab { | 477 - (void)showAuthorizationAlertForTab:(Tab*)tab { |
| 477 // Save this tab in case we're able to transition to | 478 // Save this tab in case we're able to transition to |
| 478 // kAuthorizationStateAuthorized. | 479 // kAuthorizationStateAuthorized. |
| 479 weakTabToReload_.reset(tab); | 480 weakTabToReload_ = tab; |
| 480 | 481 |
| 481 authorizationAlert_.reset( | 482 authorizationAlert_ = |
| 482 [[OmniboxGeolocationAuthorizationAlert alloc] initWithDelegate:self]); | 483 [[OmniboxGeolocationAuthorizationAlert alloc] initWithDelegate:self]; |
| 483 [authorizationAlert_ showAuthorizationAlert]; | 484 [authorizationAlert_ showAuthorizationAlert]; |
| 484 | 485 |
| 485 self.localState.lastAuthorizationAlertVersion = | 486 self.localState.lastAuthorizationAlertVersion = |
| 486 version_info::GetVersionNumber(); | 487 version_info::GetVersionNumber(); |
| 487 } | 488 } |
| 488 | 489 |
| 489 - (void)recordHeaderState:(HeaderState)headerState { | 490 - (void)recordHeaderState:(HeaderState)headerState { |
| 490 UMA_HISTOGRAM_ENUMERATION(kGeolocationHeaderSentOrNotHistogram, headerState, | 491 UMA_HISTOGRAM_ENUMERATION(kGeolocationHeaderSentOrNotHistogram, headerState, |
| 491 kHeaderStateCount); | 492 kHeaderStateCount); |
| 492 } | 493 } |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 524 | 525 |
| 525 [self recordAuthorizationAction:kAuthorizationActionPermanentlyDenied]; | 526 [self recordAuthorizationAction:kAuthorizationActionPermanentlyDenied]; |
| 526 break; | 527 break; |
| 527 | 528 |
| 528 case kCLAuthorizationStatusAuthorizedAlways: | 529 case kCLAuthorizationStatusAuthorizedAlways: |
| 529 case kCLAuthorizationStatusAuthorizedWhenInUse: | 530 case kCLAuthorizationStatusAuthorizedWhenInUse: |
| 530 self.localState.authorizationState = | 531 self.localState.authorizationState = |
| 531 geolocation::kAuthorizationStateAuthorized; | 532 geolocation::kAuthorizationStateAuthorized; |
| 532 systemPrompt_ = NO; | 533 systemPrompt_ = NO; |
| 533 | 534 |
| 534 base::scoped_nsobject<Tab> tab([weakTabToReload_ retain]); | 535 Tab* tab = weakTabToReload_; |
| 535 [self addLocationAndReloadTab:tab]; | 536 [self addLocationAndReloadTab:tab]; |
| 536 weakTabToReload_.reset(); | 537 weakTabToReload_ = nil; |
| 537 | 538 |
| 538 [self recordAuthorizationAction:kAuthorizationActionAuthorized]; | 539 [self recordAuthorizationAction:kAuthorizationActionAuthorized]; |
| 539 break; | 540 break; |
| 540 } | 541 } |
| 541 } | 542 } |
| 542 } | 543 } |
| 543 | 544 |
| 544 #pragma mark - OmniboxGeolocationAuthorizationAlertDelegate | 545 #pragma mark - OmniboxGeolocationAuthorizationAlertDelegate |
| 545 | 546 |
| 546 - (void)authorizationAlertDidAuthorize: | 547 - (void)authorizationAlertDidAuthorize: |
| 547 (OmniboxGeolocationAuthorizationAlert*)authorizationAlert { | 548 (OmniboxGeolocationAuthorizationAlert*)authorizationAlert { |
| 548 self.localState.authorizationState = | 549 self.localState.authorizationState = |
| 549 geolocation::kAuthorizationStateAuthorized; | 550 geolocation::kAuthorizationStateAuthorized; |
| 550 | 551 |
| 551 base::scoped_nsobject<Tab> tab([weakTabToReload_ retain]); | 552 Tab* tab = weakTabToReload_; |
| 552 [self addLocationAndReloadTab:tab]; | 553 [self addLocationAndReloadTab:tab]; |
| 553 | 554 |
| 554 // Just resetting |authorizationAlert_| leads to a user-after-free crash | 555 authorizationAlert_ = nil; |
| 555 // presumably due to a UIKit bug. Making authorizationAlert_ autorelease | 556 weakTabToReload_ = nil; |
| 556 // will keep it alive long enough to avoid the crash. See crbug.com/381235 | |
| 557 authorizationAlert_.autorelease(); | |
| 558 weakTabToReload_.reset(); | |
| 559 | 557 |
| 560 [self recordAuthorizationAction:kAuthorizationActionAuthorized]; | 558 [self recordAuthorizationAction:kAuthorizationActionAuthorized]; |
| 561 } | 559 } |
| 562 | 560 |
| 563 - (void)authorizationAlertDidCancel: | 561 - (void)authorizationAlertDidCancel: |
| 564 (OmniboxGeolocationAuthorizationAlert*)authorizationAlert { | 562 (OmniboxGeolocationAuthorizationAlert*)authorizationAlert { |
| 565 // Leave authorization state as undetermined (not kAuthorizationStateDenied). | 563 // Leave authorization state as undetermined (not kAuthorizationStateDenied). |
| 566 // We won't use location, but we'll still be able to prompt at the next | 564 // We won't use location, but we'll still be able to prompt at the next |
| 567 // application update. | 565 // application update. |
| 568 | 566 |
| 569 // Just resetting |authorizationAlert_| leads to a user-after-free crash | 567 authorizationAlert_ = nil; |
| 570 // presumably due to a UIKit bug. Making authorizationAlert_ autorelease | 568 weakTabToReload_ = nil; |
| 571 // will keep it alive long enough to avoid the crash. See crbug.com/381235 | |
| 572 authorizationAlert_.autorelease(); | |
| 573 weakTabToReload_.reset(); | |
| 574 | 569 |
| 575 [self recordAuthorizationAction:kAuthorizationActionDenied]; | 570 [self recordAuthorizationAction:kAuthorizationActionDenied]; |
| 576 } | 571 } |
| 577 | 572 |
| 578 #pragma mark - OmniboxGeolocationController+Testing | 573 #pragma mark - OmniboxGeolocationController+Testing |
| 579 | 574 |
| 580 - (void)setLocalState:(OmniboxGeolocationLocalState*)localState { | 575 - (void)setLocalState:(OmniboxGeolocationLocalState*)localState { |
| 581 localState_.reset([localState retain]); | 576 localState_ = localState; |
| 582 } | 577 } |
| 583 | 578 |
| 584 - (void)setLocationManager:(LocationManager*)locationManager { | 579 - (void)setLocationManager:(LocationManager*)locationManager { |
| 585 locationManager_.reset([locationManager retain]); | 580 locationManager_ = locationManager; |
| 586 } | 581 } |
| 587 | 582 |
| 588 @end | 583 @end |
| OLD | NEW |