Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(710)

Side by Side Diff: content/browser/geolocation/geolocation_provider.cc

Issue 10103029: Add device location reporting (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Comments addressed. Created 8 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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.h" 5 #include "content/browser/geolocation/geolocation_provider.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/bind_helpers.h"
9 #include "base/memory/singleton.h" 8 #include "base/memory/singleton.h"
10 #include "base/threading/thread_restrictions.h"
11 #include "content/browser/geolocation/location_arbitrator.h" 9 #include "content/browser/geolocation/location_arbitrator.h"
10 #include "content/public/browser/browser_thread.h"
12 11
13 GeolocationProvider* GeolocationProvider::GetInstance() { 12 GeolocationProvider* GeolocationProvider::GetInstance() {
13 DCHECK(OnIOThread());
14 return Singleton<GeolocationProvider>::get(); 14 return Singleton<GeolocationProvider>::get();
15 } 15 }
16 16
17 GeolocationProvider::GeolocationProvider() 17 GeolocationProvider::GeolocationProvider()
18 : base::Thread("Geolocation"), 18 : base::Thread("Geolocation"),
19 client_loop_(base::MessageLoopProxy::current()),
20 is_permission_granted_(false), 19 is_permission_granted_(false),
21 arbitrator_(NULL) { 20 arbitrator_(NULL) {
21 DCHECK(OnIOThread());
22 } 22 }
23 23
24 GeolocationProvider::~GeolocationProvider() { 24 GeolocationProvider::~GeolocationProvider() {
25 DCHECK(observers_.empty()); // observers must unregister. 25 DCHECK(observers_.empty()); // observers must unregister.
26 Stop(); 26 Stop();
27 DCHECK(!arbitrator_); 27 DCHECK(!arbitrator_);
28 } 28 }
29 29
30 void GeolocationProvider::AddObserver(GeolocationObserver* observer, 30 void GeolocationProvider::AddObserver(GeolocationObserver* observer,
31 const GeolocationObserverOptions& update_options) { 31 const GeolocationObserverOptions& update_options) {
32 DCHECK(OnClientThread()); 32 DCHECK(OnIOThread());
33 observers_[observer] = update_options; 33 observers_[observer] = update_options;
34 OnObserversChanged(); 34 OnObserversChanged();
35 if (position_.IsInitialized()) 35 if (position_.IsInitialized())
36 observer->OnLocationUpdate(position_); 36 observer->OnLocationUpdate(position_);
37 } 37 }
38 38
39 bool GeolocationProvider::RemoveObserver(GeolocationObserver* observer) { 39 bool GeolocationProvider::RemoveObserver(GeolocationObserver* observer) {
40 DCHECK(OnClientThread()); 40 DCHECK(OnIOThread());
41 size_t remove = observers_.erase(observer); 41 size_t remove = observers_.erase(observer);
42 OnObserversChanged(); 42 OnObserversChanged();
43 return remove > 0; 43 return remove > 0;
44 } 44 }
45 45
46 void GeolocationProvider::OnObserversChanged() { 46 void GeolocationProvider::OnObserversChanged() {
47 DCHECK(OnClientThread()); 47 DCHECK(OnIOThread());
48 base::Closure task; 48 base::Closure task;
49 if (observers_.empty()) { 49 if (observers_.empty()) {
50 DCHECK(IsRunning()); 50 DCHECK(IsRunning());
51 task = base::Bind(&GeolocationProvider::StopProviders, 51 task = base::Bind(&GeolocationProvider::StopProviders,
52 base::Unretained(this)); 52 base::Unretained(this));
53 } else { 53 } else {
54 if (!IsRunning()) { 54 if (!IsRunning()) {
55 Start(); 55 Start();
56 if (HasPermissionBeenGranted()) 56 if (HasPermissionBeenGranted())
57 InformProvidersPermissionGranted(); 57 InformProvidersPermissionGranted();
58 } 58 }
59 59
60 // The high accuracy requirement may have changed. 60 // The high accuracy requirement may have changed.
61 task = base::Bind(&GeolocationProvider::StartProviders, 61 task = base::Bind(&GeolocationProvider::StartProviders,
62 base::Unretained(this), 62 base::Unretained(this),
63 GeolocationObserverOptions::Collapse(observers_)); 63 GeolocationObserverOptions::Collapse(observers_));
64 } 64 }
65 65
66 message_loop()->PostTask(FROM_HERE, task); 66 message_loop()->PostTask(FROM_HERE, task);
67 } 67 }
68 68
69 void GeolocationProvider::NotifyObservers(const Geoposition& position) { 69 void GeolocationProvider::NotifyObservers(const Geoposition& position) {
70 DCHECK(OnClientThread()); 70 DCHECK(OnIOThread());
71 DCHECK(position.IsInitialized()); 71 DCHECK(position.IsInitialized());
72 position_ = position; 72 position_ = position;
73 ObserverMap::const_iterator it = observers_.begin(); 73 ObserverMap::const_iterator it = observers_.begin();
74 while (it != observers_.end()) { 74 while (it != observers_.end()) {
75 // Advance iterator before callback to guard against synchronous unregister. 75 // Advance iterator before callback to guard against synchronous unregister.
76 GeolocationObserver* observer = it->first; 76 GeolocationObserver* observer = it->first;
77 ++it; 77 ++it;
78 observer->OnLocationUpdate(position_); 78 observer->OnLocationUpdate(position_);
79 } 79 }
80 } 80 }
81 81
82 void GeolocationProvider::StartProviders( 82 void GeolocationProvider::StartProviders(
83 const GeolocationObserverOptions& options) { 83 const GeolocationObserverOptions& options) {
84 DCHECK(OnGeolocationThread()); 84 DCHECK(OnGeolocationThread());
85 DCHECK(arbitrator_); 85 DCHECK(arbitrator_);
86 arbitrator_->StartProviders(options); 86 arbitrator_->StartProviders(options);
87 } 87 }
88 88
89 void GeolocationProvider::StopProviders() { 89 void GeolocationProvider::StopProviders() {
90 DCHECK(OnGeolocationThread()); 90 DCHECK(OnGeolocationThread());
91 DCHECK(arbitrator_); 91 DCHECK(arbitrator_);
92 arbitrator_->StopProviders(); 92 arbitrator_->StopProviders();
93 } 93 }
94 94
95 void GeolocationProvider::OnPermissionGranted() { 95 void GeolocationProvider::OnPermissionGranted() {
96 DCHECK(OnClientThread()); 96 DCHECK(OnIOThread());
97 is_permission_granted_ = true; 97 is_permission_granted_ = true;
98 if (IsRunning()) 98 if (IsRunning())
99 InformProvidersPermissionGranted(); 99 InformProvidersPermissionGranted();
100 } 100 }
101 101
102 void GeolocationProvider::InformProvidersPermissionGranted() { 102 void GeolocationProvider::InformProvidersPermissionGranted() {
103 DCHECK(IsRunning()); 103 DCHECK(IsRunning());
104 if (!OnGeolocationThread()) { 104 if (!OnGeolocationThread()) {
105 message_loop()->PostTask( 105 message_loop()->PostTask(
106 FROM_HERE, 106 FROM_HERE,
(...skipping 13 matching lines...) Expand all
120 } 120 }
121 121
122 void GeolocationProvider::CleanUp() { 122 void GeolocationProvider::CleanUp() {
123 DCHECK(OnGeolocationThread()); 123 DCHECK(OnGeolocationThread());
124 delete arbitrator_; 124 delete arbitrator_;
125 arbitrator_ = NULL; 125 arbitrator_ = NULL;
126 } 126 }
127 127
128 void GeolocationProvider::OnLocationUpdate(const Geoposition& position) { 128 void GeolocationProvider::OnLocationUpdate(const Geoposition& position) {
129 DCHECK(OnGeolocationThread()); 129 DCHECK(OnGeolocationThread());
130 client_loop_->PostTask( 130 content::BrowserThread::PostTask(
131 content::BrowserThread::IO,
131 FROM_HERE, 132 FROM_HERE,
132 base::Bind(&GeolocationProvider::NotifyObservers, 133 base::Bind(&GeolocationProvider::NotifyObservers,
133 base::Unretained(this), position)); 134 base::Unretained(this), position));
134 } 135 }
135 136
136 bool GeolocationProvider::HasPermissionBeenGranted() const { 137 bool GeolocationProvider::HasPermissionBeenGranted() const {
137 DCHECK(OnClientThread()); 138 DCHECK(OnIOThread());
138 return is_permission_granted_; 139 return is_permission_granted_;
139 } 140 }
140 141
141 bool GeolocationProvider::OnClientThread() const { 142 // static
142 return client_loop_->BelongsToCurrentThread(); 143 bool GeolocationProvider::OnIOThread() {
144 return content::BrowserThread::CurrentlyOn(content::BrowserThread::IO);
143 } 145 }
144 146
145 bool GeolocationProvider::OnGeolocationThread() const { 147 bool GeolocationProvider::OnGeolocationThread() const {
146 return MessageLoop::current() == message_loop(); 148 return MessageLoop::current() == message_loop();
147 } 149 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698