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" |
13 #include "base/logging.h" | 14 #include "base/logging.h" |
14 #include "base/memory/weak_ptr.h" | 15 #include "base/memory/weak_ptr.h" |
15 #include "base/message_loop/message_loop.h" | 16 #include "base/thread_task_runner_handle.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_loop_(base::MessageLoopProxy::current()) { | 25 provider_task_runner_(base::ThreadTaskRunnerHandle::Get()) { |
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_loop_->BelongsToCurrentThread()) { | 37 if (provider_task_runner_->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_loop_->PostTask( | 43 provider_task_runner_->PostTask( |
44 FROM_HERE, | 44 FROM_HERE, base::Bind(&MockLocationProvider::HandlePositionChanged, |
45 base::Bind(&MockLocationProvider::HandlePositionChanged, | 45 base::Unretained(this), position)); |
46 base::Unretained(this), position)); | |
47 } | 46 } |
48 } | 47 } |
49 | 48 |
50 bool MockLocationProvider::StartProvider(bool high_accuracy) { | 49 bool MockLocationProvider::StartProvider(bool high_accuracy) { |
51 state_ = high_accuracy ? HIGH_ACCURACY : LOW_ACCURACY; | 50 state_ = high_accuracy ? HIGH_ACCURACY : LOW_ACCURACY; |
52 return true; | 51 return true; |
53 } | 52 } |
54 | 53 |
55 void MockLocationProvider::StopProvider() { | 54 void MockLocationProvider::StopProvider() { |
56 state_ = STOPPED; | 55 state_ = STOPPED; |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
97 void OnPermissionGranted() override { | 96 void OnPermissionGranted() override { |
98 MockLocationProvider::OnPermissionGranted(); | 97 MockLocationProvider::OnPermissionGranted(); |
99 if (requires_permission_to_start_) { | 98 if (requires_permission_to_start_) { |
100 UpdateListenersIfNeeded(); | 99 UpdateListenersIfNeeded(); |
101 } | 100 } |
102 } | 101 } |
103 | 102 |
104 void UpdateListenersIfNeeded() { | 103 void UpdateListenersIfNeeded() { |
105 if (!listeners_updated_) { | 104 if (!listeners_updated_) { |
106 listeners_updated_ = true; | 105 listeners_updated_ = true; |
107 base::MessageLoop::current()->PostTask( | 106 base::ThreadTaskRunnerHandle::Get()->PostTask( |
108 FROM_HERE, | 107 FROM_HERE, base::Bind(&MockLocationProvider::HandlePositionChanged, |
109 base::Bind(&MockLocationProvider::HandlePositionChanged, | 108 weak_factory_.GetWeakPtr(), position_)); |
110 weak_factory_.GetWeakPtr(), | |
111 position_)); | |
112 } | 109 } |
113 } | 110 } |
114 | 111 |
115 base::WeakPtrFactory<MockLocationProvider> weak_factory_; | 112 base::WeakPtrFactory<MockLocationProvider> weak_factory_; |
116 const bool requires_permission_to_start_; | 113 const bool requires_permission_to_start_; |
117 bool listeners_updated_; | 114 bool listeners_updated_; |
118 }; | 115 }; |
119 | 116 |
120 LocationProvider* NewMockLocationProvider() { | 117 LocationProvider* NewMockLocationProvider() { |
121 return new MockLocationProvider(&MockLocationProvider::instance_); | 118 return new MockLocationProvider(&MockLocationProvider::instance_); |
122 } | 119 } |
123 | 120 |
124 LocationProvider* NewAutoSuccessMockLocationProvider() { | 121 LocationProvider* NewAutoSuccessMockLocationProvider() { |
125 return new AutoMockLocationProvider(true, false); | 122 return new AutoMockLocationProvider(true, false); |
126 } | 123 } |
127 | 124 |
128 LocationProvider* NewAutoFailMockLocationProvider() { | 125 LocationProvider* NewAutoFailMockLocationProvider() { |
129 return new AutoMockLocationProvider(false, false); | 126 return new AutoMockLocationProvider(false, false); |
130 } | 127 } |
131 | 128 |
132 LocationProvider* NewAutoSuccessMockNetworkLocationProvider() { | 129 LocationProvider* NewAutoSuccessMockNetworkLocationProvider() { |
133 return new AutoMockLocationProvider(true, true); | 130 return new AutoMockLocationProvider(true, true); |
134 } | 131 } |
135 | 132 |
136 } // namespace content | 133 } // namespace content |
OLD | NEW |