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