| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 // This file implements a mock location provider and the factory functions for | 5 // This file implements a mock location provider and the factory functions for |
| 6 // various ways of creating it. | 6 // various ways of creating it. |
| 7 | 7 |
| 8 #include "content/browser/geolocation/mock_location_provider.h" | 8 #include "content/browser/geolocation/mock_location_provider.h" |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/bind_helpers.h" | 11 #include "base/bind_helpers.h" |
| 12 #include "base/compiler_specific.h" | 12 #include "base/compiler_specific.h" |
| 13 #include "base/logging.h" | 13 #include "base/logging.h" |
| 14 #include "base/memory/weak_ptr.h" | 14 #include "base/memory/weak_ptr.h" |
| 15 #include "base/message_loop.h" | 15 #include "base/message_loop.h" |
| 16 #include "base/message_loop_proxy.h" | 16 #include "base/message_loop_proxy.h" |
| 17 | 17 |
| 18 MockLocationProvider* MockLocationProvider::instance_ = NULL; | 18 MockLocationProvider* MockLocationProvider::instance_ = NULL; |
| 19 | 19 |
| 20 MockLocationProvider::MockLocationProvider(MockLocationProvider** self_ref) | 20 MockLocationProvider::MockLocationProvider(MockLocationProvider** self_ref) |
| 21 : state_(STOPPED), | 21 : state_(STOPPED), |
| 22 is_permission_granted_(false), |
| 22 self_ref_(self_ref), | 23 self_ref_(self_ref), |
| 23 provider_loop_(base::MessageLoopProxy::current()) { | 24 provider_loop_(base::MessageLoopProxy::current()) { |
| 24 CHECK(self_ref_); | 25 CHECK(self_ref_); |
| 25 CHECK(*self_ref_ == NULL); | 26 CHECK(*self_ref_ == NULL); |
| 26 *self_ref_ = this; | 27 *self_ref_ = this; |
| 27 } | 28 } |
| 28 | 29 |
| 29 MockLocationProvider::~MockLocationProvider() { | 30 MockLocationProvider::~MockLocationProvider() { |
| 30 CHECK(*self_ref_ == this); | 31 CHECK(*self_ref_ == this); |
| 31 *self_ref_ = NULL; | 32 *self_ref_ = NULL; |
| (...skipping 19 matching lines...) Expand all Loading... |
| 51 } | 52 } |
| 52 | 53 |
| 53 void MockLocationProvider::StopProvider() { | 54 void MockLocationProvider::StopProvider() { |
| 54 state_ = STOPPED; | 55 state_ = STOPPED; |
| 55 } | 56 } |
| 56 | 57 |
| 57 void MockLocationProvider::GetPosition(Geoposition* position) { | 58 void MockLocationProvider::GetPosition(Geoposition* position) { |
| 58 *position = position_; | 59 *position = position_; |
| 59 } | 60 } |
| 60 | 61 |
| 61 void MockLocationProvider::OnPermissionGranted(const GURL& requesting_frame) { | 62 void MockLocationProvider::OnPermissionGranted() { |
| 62 permission_granted_url_ = requesting_frame; | 63 is_permission_granted_ = true; |
| 63 } | 64 } |
| 64 | 65 |
| 65 // Mock location provider that automatically calls back its client at most | 66 // Mock location provider that automatically calls back its client at most |
| 66 // once, when StartProvider or OnPermissionGranted is called. Use | 67 // once, when StartProvider or OnPermissionGranted is called. Use |
| 67 // |requires_permission_to_start| to select which event triggers the callback. | 68 // |requires_permission_to_start| to select which event triggers the callback. |
| 68 class AutoMockLocationProvider : public MockLocationProvider { | 69 class AutoMockLocationProvider : public MockLocationProvider { |
| 69 public: | 70 public: |
| 70 AutoMockLocationProvider(bool has_valid_location, | 71 AutoMockLocationProvider(bool has_valid_location, |
| 71 bool requires_permission_to_start) | 72 bool requires_permission_to_start) |
| 72 : MockLocationProvider(&instance_), | 73 : MockLocationProvider(&instance_), |
| (...skipping 12 matching lines...) Expand all Loading... |
| 85 } | 86 } |
| 86 } | 87 } |
| 87 virtual bool StartProvider(bool high_accuracy) { | 88 virtual bool StartProvider(bool high_accuracy) { |
| 88 MockLocationProvider::StartProvider(high_accuracy); | 89 MockLocationProvider::StartProvider(high_accuracy); |
| 89 if (!requires_permission_to_start_) { | 90 if (!requires_permission_to_start_) { |
| 90 UpdateListenersIfNeeded(); | 91 UpdateListenersIfNeeded(); |
| 91 } | 92 } |
| 92 return true; | 93 return true; |
| 93 } | 94 } |
| 94 | 95 |
| 95 void OnPermissionGranted(const GURL& requesting_frame) { | 96 void OnPermissionGranted() { |
| 96 MockLocationProvider::OnPermissionGranted(requesting_frame); | 97 MockLocationProvider::OnPermissionGranted(); |
| 97 if (requires_permission_to_start_) { | 98 if (requires_permission_to_start_) { |
| 98 UpdateListenersIfNeeded(); | 99 UpdateListenersIfNeeded(); |
| 99 } | 100 } |
| 100 } | 101 } |
| 101 | 102 |
| 102 void UpdateListenersIfNeeded() { | 103 void UpdateListenersIfNeeded() { |
| 103 if (!listeners_updated_) { | 104 if (!listeners_updated_) { |
| 104 listeners_updated_ = true; | 105 listeners_updated_ = true; |
| 105 MessageLoop::current()->PostTask( | 106 MessageLoop::current()->PostTask( |
| 106 FROM_HERE, | 107 FROM_HERE, |
| (...skipping 15 matching lines...) Expand all Loading... |
| 122 return new AutoMockLocationProvider(true, false); | 123 return new AutoMockLocationProvider(true, false); |
| 123 } | 124 } |
| 124 | 125 |
| 125 LocationProviderBase* NewAutoFailMockLocationProvider() { | 126 LocationProviderBase* NewAutoFailMockLocationProvider() { |
| 126 return new AutoMockLocationProvider(false, false); | 127 return new AutoMockLocationProvider(false, false); |
| 127 } | 128 } |
| 128 | 129 |
| 129 LocationProviderBase* NewAutoSuccessMockNetworkLocationProvider() { | 130 LocationProviderBase* NewAutoSuccessMockNetworkLocationProvider() { |
| 130 return new AutoMockLocationProvider(true, true); | 131 return new AutoMockLocationProvider(true, true); |
| 131 } | 132 } |
| OLD | NEW |