OLD | NEW |
1 // Copyright (c) 2012 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/location.h" | |
14 #include "base/logging.h" | 13 #include "base/logging.h" |
15 #include "base/memory/weak_ptr.h" | 14 #include "base/memory/weak_ptr.h" |
16 #include "base/thread_task_runner_handle.h" | 15 #include "base/message_loop/message_loop.h" |
| 16 #include "base/message_loop/message_loop_proxy.h" |
17 | 17 |
18 namespace content { | 18 namespace content { |
19 MockLocationProvider* MockLocationProvider::instance_ = NULL; | 19 MockLocationProvider* MockLocationProvider::instance_ = NULL; |
20 | 20 |
21 MockLocationProvider::MockLocationProvider(MockLocationProvider** self_ref) | 21 MockLocationProvider::MockLocationProvider(MockLocationProvider** self_ref) |
22 : state_(STOPPED), | 22 : state_(STOPPED), |
23 is_permission_granted_(false), | 23 is_permission_granted_(false), |
24 self_ref_(self_ref), | 24 self_ref_(self_ref), |
25 provider_task_runner_(base::ThreadTaskRunnerHandle::Get()) { | 25 provider_loop_(base::MessageLoopProxy::current()) { |
26 CHECK(self_ref_); | 26 CHECK(self_ref_); |
27 CHECK(*self_ref_ == NULL); | 27 CHECK(*self_ref_ == NULL); |
28 *self_ref_ = this; | 28 *self_ref_ = this; |
29 } | 29 } |
30 | 30 |
31 MockLocationProvider::~MockLocationProvider() { | 31 MockLocationProvider::~MockLocationProvider() { |
32 CHECK(*self_ref_ == this); | 32 CHECK(*self_ref_ == this); |
33 *self_ref_ = NULL; | 33 *self_ref_ = NULL; |
34 } | 34 } |
35 | 35 |
36 void MockLocationProvider::HandlePositionChanged(const Geoposition& position) { | 36 void MockLocationProvider::HandlePositionChanged(const Geoposition& position) { |
37 if (provider_task_runner_->BelongsToCurrentThread()) { | 37 if (provider_loop_->BelongsToCurrentThread()) { |
38 // The location arbitrator unit tests rely on this method running | 38 // The location arbitrator unit tests rely on this method running |
39 // synchronously. | 39 // synchronously. |
40 position_ = position; | 40 position_ = position; |
41 NotifyCallback(position_); | 41 NotifyCallback(position_); |
42 } else { | 42 } else { |
43 provider_task_runner_->PostTask( | 43 provider_loop_->PostTask( |
44 FROM_HERE, base::Bind(&MockLocationProvider::HandlePositionChanged, | 44 FROM_HERE, |
45 base::Unretained(this), position)); | 45 base::Bind(&MockLocationProvider::HandlePositionChanged, |
| 46 base::Unretained(this), position)); |
46 } | 47 } |
47 } | 48 } |
48 | 49 |
49 bool MockLocationProvider::StartProvider(bool high_accuracy) { | 50 bool MockLocationProvider::StartProvider(bool high_accuracy) { |
50 state_ = high_accuracy ? HIGH_ACCURACY : LOW_ACCURACY; | 51 state_ = high_accuracy ? HIGH_ACCURACY : LOW_ACCURACY; |
51 return true; | 52 return true; |
52 } | 53 } |
53 | 54 |
54 void MockLocationProvider::StopProvider() { | 55 void MockLocationProvider::StopProvider() { |
55 state_ = STOPPED; | 56 state_ = STOPPED; |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
96 void OnPermissionGranted() override { | 97 void OnPermissionGranted() override { |
97 MockLocationProvider::OnPermissionGranted(); | 98 MockLocationProvider::OnPermissionGranted(); |
98 if (requires_permission_to_start_) { | 99 if (requires_permission_to_start_) { |
99 UpdateListenersIfNeeded(); | 100 UpdateListenersIfNeeded(); |
100 } | 101 } |
101 } | 102 } |
102 | 103 |
103 void UpdateListenersIfNeeded() { | 104 void UpdateListenersIfNeeded() { |
104 if (!listeners_updated_) { | 105 if (!listeners_updated_) { |
105 listeners_updated_ = true; | 106 listeners_updated_ = true; |
106 base::ThreadTaskRunnerHandle::Get()->PostTask( | 107 base::MessageLoop::current()->PostTask( |
107 FROM_HERE, base::Bind(&MockLocationProvider::HandlePositionChanged, | 108 FROM_HERE, |
108 weak_factory_.GetWeakPtr(), position_)); | 109 base::Bind(&MockLocationProvider::HandlePositionChanged, |
| 110 weak_factory_.GetWeakPtr(), |
| 111 position_)); |
109 } | 112 } |
110 } | 113 } |
111 | 114 |
112 base::WeakPtrFactory<MockLocationProvider> weak_factory_; | 115 base::WeakPtrFactory<MockLocationProvider> weak_factory_; |
113 const bool requires_permission_to_start_; | 116 const bool requires_permission_to_start_; |
114 bool listeners_updated_; | 117 bool listeners_updated_; |
115 }; | 118 }; |
116 | 119 |
117 LocationProvider* NewMockLocationProvider() { | 120 LocationProvider* NewMockLocationProvider() { |
118 return new MockLocationProvider(&MockLocationProvider::instance_); | 121 return new MockLocationProvider(&MockLocationProvider::instance_); |
119 } | 122 } |
120 | 123 |
121 LocationProvider* NewAutoSuccessMockLocationProvider() { | 124 LocationProvider* NewAutoSuccessMockLocationProvider() { |
122 return new AutoMockLocationProvider(true, false); | 125 return new AutoMockLocationProvider(true, false); |
123 } | 126 } |
124 | 127 |
125 LocationProvider* NewAutoFailMockLocationProvider() { | 128 LocationProvider* NewAutoFailMockLocationProvider() { |
126 return new AutoMockLocationProvider(false, false); | 129 return new AutoMockLocationProvider(false, false); |
127 } | 130 } |
128 | 131 |
129 LocationProvider* NewAutoSuccessMockNetworkLocationProvider() { | 132 LocationProvider* NewAutoSuccessMockNetworkLocationProvider() { |
130 return new AutoMockLocationProvider(true, true); | 133 return new AutoMockLocationProvider(true, true); |
131 } | 134 } |
132 | 135 |
133 } // namespace content | 136 } // namespace content |
OLD | NEW |