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 #include "device/geolocation/geolocation_provider_impl.h" | 5 #include "content/browser/geolocation/geolocation_provider_impl.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
9 #include "base/callback.h" | 9 #include "base/callback.h" |
10 #include "base/lazy_instance.h" | 10 #include "base/lazy_instance.h" |
11 #include "base/location.h" | 11 #include "base/location.h" |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
13 #include "base/memory/ptr_util.h" | 13 #include "base/memory/ptr_util.h" |
14 #include "base/memory/singleton.h" | 14 #include "base/memory/singleton.h" |
15 #include "base/single_thread_task_runner.h" | 15 #include "base/single_thread_task_runner.h" |
16 #include "base/threading/thread_task_runner_handle.h" | 16 #include "content/browser/geolocation/location_arbitrator_impl.h" |
17 #include "device/geolocation/geolocation_delegate.h" | 17 #include "content/public/browser/browser_thread.h" |
18 #include "device/geolocation/location_arbitrator_impl.h" | 18 #include "content/public/browser/content_browser_client.h" |
| 19 #include "content/public/browser/geolocation_delegate.h" |
19 | 20 |
20 namespace device { | 21 namespace content { |
21 | 22 |
22 namespace { | 23 namespace { |
23 base::LazyInstance<std::unique_ptr<GeolocationDelegate>>::Leaky g_delegate = | 24 base::LazyInstance<std::unique_ptr<GeolocationDelegate>>::Leaky g_delegate = |
24 LAZY_INSTANCE_INITIALIZER; | 25 LAZY_INSTANCE_INITIALIZER; |
25 } // anonymous namespace | 26 } // anonymous namespace |
26 | 27 |
27 // static | 28 // static |
28 GeolocationProvider* GeolocationProvider::GetInstance() { | 29 GeolocationProvider* GeolocationProvider::GetInstance() { |
29 return GeolocationProviderImpl::GetInstance(); | 30 return GeolocationProviderImpl::GetInstance(); |
30 } | 31 } |
31 | 32 |
32 // static | 33 // static |
33 void GeolocationProvider::SetGeolocationDelegate( | 34 void GeolocationProvider::SetGeolocationDelegate( |
34 GeolocationDelegate* delegate) { | 35 GeolocationDelegate* delegate) { |
35 DCHECK(!g_delegate.Get()); | 36 DCHECK(!g_delegate.Get()); |
36 g_delegate.Get().reset(delegate); | 37 g_delegate.Get().reset(delegate); |
37 } | 38 } |
38 | 39 |
39 std::unique_ptr<GeolocationProvider::Subscription> | 40 std::unique_ptr<GeolocationProvider::Subscription> |
40 GeolocationProviderImpl::AddLocationUpdateCallback( | 41 GeolocationProviderImpl::AddLocationUpdateCallback( |
41 const LocationUpdateCallback& callback, | 42 const LocationUpdateCallback& callback, |
42 bool enable_high_accuracy) { | 43 bool enable_high_accuracy) { |
43 DCHECK(main_task_runner_->BelongsToCurrentThread()); | 44 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
44 std::unique_ptr<GeolocationProvider::Subscription> subscription; | 45 std::unique_ptr<GeolocationProvider::Subscription> subscription; |
45 if (enable_high_accuracy) { | 46 if (enable_high_accuracy) { |
46 subscription = high_accuracy_callbacks_.Add(callback); | 47 subscription = high_accuracy_callbacks_.Add(callback); |
47 } else { | 48 } else { |
48 subscription = low_accuracy_callbacks_.Add(callback); | 49 subscription = low_accuracy_callbacks_.Add(callback); |
49 } | 50 } |
50 | 51 |
51 OnClientsChanged(); | 52 OnClientsChanged(); |
52 if (position_.Validate() || | 53 if (position_.Validate() || |
53 position_.error_code != Geoposition::ERROR_CODE_NONE) { | 54 position_.error_code != Geoposition::ERROR_CODE_NONE) { |
54 callback.Run(position_); | 55 callback.Run(position_); |
55 } | 56 } |
56 | 57 |
57 return subscription; | 58 return subscription; |
58 } | 59 } |
59 | 60 |
60 void GeolocationProviderImpl::UserDidOptIntoLocationServices() { | 61 void GeolocationProviderImpl::UserDidOptIntoLocationServices() { |
61 DCHECK(main_task_runner_->BelongsToCurrentThread()); | 62 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
62 bool was_permission_granted = user_did_opt_into_location_services_; | 63 bool was_permission_granted = user_did_opt_into_location_services_; |
63 user_did_opt_into_location_services_ = true; | 64 user_did_opt_into_location_services_ = true; |
64 if (IsRunning() && !was_permission_granted) | 65 if (IsRunning() && !was_permission_granted) |
65 InformProvidersPermissionGranted(); | 66 InformProvidersPermissionGranted(); |
66 } | 67 } |
67 | 68 |
68 void GeolocationProviderImpl::OverrideLocationForTesting( | 69 void GeolocationProviderImpl::OverrideLocationForTesting( |
69 const Geoposition& position) { | 70 const Geoposition& position) { |
70 DCHECK(main_task_runner_->BelongsToCurrentThread()); | 71 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
71 ignore_location_updates_ = true; | 72 ignore_location_updates_ = true; |
72 NotifyClients(position); | 73 NotifyClients(position); |
73 } | 74 } |
74 | 75 |
75 void GeolocationProviderImpl::OnLocationUpdate(const Geoposition& position) { | 76 void GeolocationProviderImpl::OnLocationUpdate(const Geoposition& position) { |
76 DCHECK(OnGeolocationThread()); | 77 DCHECK(OnGeolocationThread()); |
77 // Will be true only in testing. | 78 // Will be true only in testing. |
78 if (ignore_location_updates_) | 79 if (ignore_location_updates_) |
79 return; | 80 return; |
80 main_task_runner_->PostTask( | 81 BrowserThread::PostTask(BrowserThread::UI, |
81 FROM_HERE, base::Bind(&GeolocationProviderImpl::NotifyClients, | 82 FROM_HERE, |
82 base::Unretained(this), position)); | 83 base::Bind(&GeolocationProviderImpl::NotifyClients, |
| 84 base::Unretained(this), position)); |
83 } | 85 } |
84 | 86 |
85 // static | 87 // static |
86 GeolocationProviderImpl* GeolocationProviderImpl::GetInstance() { | 88 GeolocationProviderImpl* GeolocationProviderImpl::GetInstance() { |
| 89 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
87 return base::Singleton<GeolocationProviderImpl>::get(); | 90 return base::Singleton<GeolocationProviderImpl>::get(); |
88 } | 91 } |
89 | 92 |
90 GeolocationProviderImpl::GeolocationProviderImpl() | 93 GeolocationProviderImpl::GeolocationProviderImpl() |
91 : base::Thread("Geolocation"), | 94 : base::Thread("Geolocation"), |
92 user_did_opt_into_location_services_(false), | 95 user_did_opt_into_location_services_(false), |
93 ignore_location_updates_(false), | 96 ignore_location_updates_(false) { |
94 main_task_runner_(base::ThreadTaskRunnerHandle::Get()) { | 97 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
95 DCHECK(main_task_runner_->BelongsToCurrentThread()); | |
96 high_accuracy_callbacks_.set_removal_callback( | 98 high_accuracy_callbacks_.set_removal_callback( |
97 base::Bind(&GeolocationProviderImpl::OnClientsChanged, | 99 base::Bind(&GeolocationProviderImpl::OnClientsChanged, |
98 base::Unretained(this))); | 100 base::Unretained(this))); |
99 low_accuracy_callbacks_.set_removal_callback( | 101 low_accuracy_callbacks_.set_removal_callback( |
100 base::Bind(&GeolocationProviderImpl::OnClientsChanged, | 102 base::Bind(&GeolocationProviderImpl::OnClientsChanged, |
101 base::Unretained(this))); | 103 base::Unretained(this))); |
102 } | 104 } |
103 | 105 |
104 GeolocationProviderImpl::~GeolocationProviderImpl() { | 106 GeolocationProviderImpl::~GeolocationProviderImpl() { |
105 Stop(); | 107 Stop(); |
106 DCHECK(!arbitrator_); | 108 DCHECK(!arbitrator_); |
107 } | 109 } |
108 | 110 |
109 bool GeolocationProviderImpl::OnGeolocationThread() const { | 111 bool GeolocationProviderImpl::OnGeolocationThread() const { |
110 return task_runner()->BelongsToCurrentThread(); | 112 return task_runner()->BelongsToCurrentThread(); |
111 } | 113 } |
112 | 114 |
113 void GeolocationProviderImpl::OnClientsChanged() { | 115 void GeolocationProviderImpl::OnClientsChanged() { |
114 DCHECK(main_task_runner_->BelongsToCurrentThread()); | 116 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
115 base::Closure task; | 117 base::Closure task; |
116 if (high_accuracy_callbacks_.empty() && low_accuracy_callbacks_.empty()) { | 118 if (high_accuracy_callbacks_.empty() && low_accuracy_callbacks_.empty()) { |
117 DCHECK(IsRunning()); | 119 DCHECK(IsRunning()); |
118 if (!ignore_location_updates_) { | 120 if (!ignore_location_updates_) { |
119 // We have no more observers, so we clear the cached geoposition so that | 121 // We have no more observers, so we clear the cached geoposition so that |
120 // when the next observer is added we will not provide a stale position. | 122 // when the next observer is added we will not provide a stale position. |
121 position_ = Geoposition(); | 123 position_ = Geoposition(); |
122 } | 124 } |
123 task = base::Bind(&GeolocationProviderImpl::StopProviders, | 125 task = base::Bind(&GeolocationProviderImpl::StopProviders, |
124 base::Unretained(this)); | 126 base::Unretained(this)); |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
159 base::Bind(&GeolocationProviderImpl::InformProvidersPermissionGranted, | 161 base::Bind(&GeolocationProviderImpl::InformProvidersPermissionGranted, |
160 base::Unretained(this))); | 162 base::Unretained(this))); |
161 return; | 163 return; |
162 } | 164 } |
163 DCHECK(OnGeolocationThread()); | 165 DCHECK(OnGeolocationThread()); |
164 DCHECK(arbitrator_); | 166 DCHECK(arbitrator_); |
165 arbitrator_->OnPermissionGranted(); | 167 arbitrator_->OnPermissionGranted(); |
166 } | 168 } |
167 | 169 |
168 void GeolocationProviderImpl::NotifyClients(const Geoposition& position) { | 170 void GeolocationProviderImpl::NotifyClients(const Geoposition& position) { |
169 DCHECK(main_task_runner_->BelongsToCurrentThread()); | 171 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
170 DCHECK(position.Validate() || | 172 DCHECK(position.Validate() || |
171 position.error_code != Geoposition::ERROR_CODE_NONE); | 173 position.error_code != Geoposition::ERROR_CODE_NONE); |
172 position_ = position; | 174 position_ = position; |
173 high_accuracy_callbacks_.Notify(position_); | 175 high_accuracy_callbacks_.Notify(position_); |
174 low_accuracy_callbacks_.Notify(position_); | 176 low_accuracy_callbacks_.Notify(position_); |
175 } | 177 } |
176 | 178 |
177 void GeolocationProviderImpl::Init() { | 179 void GeolocationProviderImpl::Init() { |
178 DCHECK(OnGeolocationThread()); | 180 DCHECK(OnGeolocationThread()); |
179 DCHECK(!arbitrator_); | 181 DCHECK(!arbitrator_); |
(...skipping 10 matching lines...) Expand all Loading... |
190 LocationArbitratorImpl::LocationUpdateCallback callback = base::Bind( | 192 LocationArbitratorImpl::LocationUpdateCallback callback = base::Bind( |
191 &GeolocationProviderImpl::OnLocationUpdate, base::Unretained(this)); | 193 &GeolocationProviderImpl::OnLocationUpdate, base::Unretained(this)); |
192 // Use the embedder's |g_delegate| or fall back to the default one. | 194 // Use the embedder's |g_delegate| or fall back to the default one. |
193 if (!g_delegate.Get()) | 195 if (!g_delegate.Get()) |
194 g_delegate.Get().reset(new GeolocationDelegate); | 196 g_delegate.Get().reset(new GeolocationDelegate); |
195 | 197 |
196 return base::WrapUnique( | 198 return base::WrapUnique( |
197 new LocationArbitratorImpl(callback, g_delegate.Get().get())); | 199 new LocationArbitratorImpl(callback, g_delegate.Get().get())); |
198 } | 200 } |
199 | 201 |
200 } // namespace device | 202 } // namespace content |
OLD | NEW |