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

Side by Side Diff: content/browser/geolocation/wifi_data_provider.h

Issue 474433003: Cleaner organization of WifiDataProvider code. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rabase and rename arguments to manager Created 6 years, 4 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 | Annotate | Revision Log
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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 // A wifi data provider provides wifi data from the device that is used by a
6 // NetworkLocationProvider to obtain a position fix. We use a singleton
7 // instance of the wifi data provider, which is used by multiple
8 // NetworkLocationProvider objects.
9 //
10 // This file provides WifiDataProvider, which provides static methods to
11 // access the singleton instance. The singleton instance uses a private
12 // implementation to abstract across platforms and also to allow mock providers
13 // to be used for testing.
14 //
15 // This file also provides WifiDataProviderImplBase, a base class which
16 // provides common functionality for the private implementations.
17
18 #ifndef CONTENT_BROWSER_GEOLOCATION_WIFI_DATA_PROVIDER_H_ 5 #ifndef CONTENT_BROWSER_GEOLOCATION_WIFI_DATA_PROVIDER_H_
19 #define CONTENT_BROWSER_GEOLOCATION_WIFI_DATA_PROVIDER_H_ 6 #define CONTENT_BROWSER_GEOLOCATION_WIFI_DATA_PROVIDER_H_
20 7
21 #include <set> 8 #include <set>
22 9
23 #include "base/basictypes.h" 10 #include "base/basictypes.h"
24 #include "base/bind.h" 11 #include "base/bind.h"
25 #include "base/callback.h" 12 #include "base/callback.h"
26 #include "base/memory/ref_counted.h" 13 #include "base/memory/ref_counted.h"
27 #include "base/message_loop/message_loop.h" 14 #include "base/message_loop/message_loop.h"
28 #include "base/strings/string16.h" 15 #include "base/strings/string16.h"
29 #include "base/strings/string_util.h" 16 #include "base/strings/string_util.h"
30 #include "content/browser/geolocation/wifi_data.h" 17 #include "content/browser/geolocation/wifi_data.h"
31 #include "content/common/content_export.h" 18 #include "content/common/content_export.h"
32 19
33 namespace content { 20 namespace content {
34 21
35 class WifiDataProvider; 22 class WifiDataProviderManager;
36 23
37 // See class WifiDataProvider for the public client API. 24 class CONTENT_EXPORT WifiDataProvider
38 // WifiDataProvider uses containment to hide platform-specific implementation 25 : public base::RefCountedThreadSafe<WifiDataProvider> {
39 // details from common code. This class provides common functionality for these
40 // contained implementation classes. This is a modified pimpl pattern.
41 class CONTENT_EXPORT WifiDataProviderImplBase
42 : public base::RefCountedThreadSafe<WifiDataProviderImplBase> {
43 public: 26 public:
44 WifiDataProviderImplBase(); 27 WifiDataProvider();
45 28
46 // Tells the provider to start looking for data. Callbacks will start 29 // Tells the provider to start looking for data. Callbacks will start
47 // receiving notifications after this call. 30 // receiving notifications after this call.
48 virtual void StartDataProvider() = 0; 31 virtual void StartDataProvider() = 0;
49 32
50 // Tells the provider to stop looking for data. Callbacks will stop 33 // Tells the provider to stop looking for data. Callbacks will stop
51 // receiving notifications after this call. 34 // receiving notifications after this call.
52 virtual void StopDataProvider() = 0; 35 virtual void StopDataProvider() = 0;
53 36
54 // Provides whatever data the provider has, which may be nothing. Return 37 // Provides whatever data the provider has, which may be nothing. Return
55 // value indicates whether this is all the data the provider could ever 38 // value indicates whether this is all the data the provider could ever
56 // obtain. 39 // obtain.
57 virtual bool GetData(WifiData* data) = 0; 40 virtual bool GetData(WifiData* data) = 0;
58 41
59 // Sets the container of this class, which is of type WifiDataProvider. 42 // Sets the container of this class, which is of type WifiDataProviderManager.
60 // This is required to pass as a parameter when calling a callback. 43 // This is required to pass as a parameter when calling a callback.
61 void SetContainer(WifiDataProvider* container); 44 void SetContainer(WifiDataProviderManager* container);
62 45
63 typedef base::Callback<void(WifiDataProvider*)> WifiDataUpdateCallback; 46 typedef base::Callback<void(WifiDataProviderManager*)> WifiDataUpdateCallback;
64 47
65 void AddCallback(WifiDataUpdateCallback* callback); 48 void AddCallback(WifiDataUpdateCallback* callback);
66 49
67 bool RemoveCallback(WifiDataUpdateCallback* callback); 50 bool RemoveCallback(WifiDataUpdateCallback* callback);
68 51
69 bool has_callbacks() const; 52 bool has_callbacks() const;
70 53
71 protected: 54 protected:
72 friend class base::RefCountedThreadSafe<WifiDataProviderImplBase>; 55 friend class base::RefCountedThreadSafe<WifiDataProvider>;
73 virtual ~WifiDataProviderImplBase(); 56 virtual ~WifiDataProvider();
74 57
75 typedef std::set<WifiDataUpdateCallback*> CallbackSet; 58 typedef std::set<WifiDataUpdateCallback*> CallbackSet;
76 59
77 // Runs all callbacks via a posted task, so we can unwind callstack here and 60 // Runs all callbacks via a posted task, so we can unwind callstack here and
78 // avoid client reentrancy. 61 // avoid client reentrancy.
79 void RunCallbacks(); 62 void RunCallbacks();
80 63
81 bool CalledOnClientThread() const; 64 bool CalledOnClientThread() const;
82 65
83 base::MessageLoop* client_loop() const; 66 base::MessageLoop* client_loop() const;
84 67
85 private: 68 private:
86 void DoRunCallbacks(); 69 void DoRunCallbacks();
87 70
88 WifiDataProvider* container_; 71 WifiDataProviderManager* container_;
89 72
90 // Reference to the client's message loop. All callbacks should happen in this 73 // Reference to the client's message loop. All callbacks should happen in this
91 // context. 74 // context.
92 base::MessageLoop* client_loop_; 75 base::MessageLoop* client_loop_;
93 76
94 CallbackSet callbacks_; 77 CallbackSet callbacks_;
95 78
96 DISALLOW_COPY_AND_ASSIGN(WifiDataProviderImplBase);
97 };
98
99 // A wifi data provider
100 //
101 // We use a singleton instance of this class which is shared by multiple network
102 // location providers. These location providers access the instance through the
103 // Register and Unregister methods.
104 class CONTENT_EXPORT WifiDataProvider {
105 public:
106 // Sets the factory function which will be used by Register to create the
107 // implementation used by the singleton instance. This factory approach is
108 // used both to abstract accross platform-specific implementations and to
109 // inject mock implementations for testing.
110 typedef WifiDataProviderImplBase* (*ImplFactoryFunction)(void);
111 static void SetFactory(ImplFactoryFunction factory_function_in);
112
113 // Resets the factory function to the default.
114 static void ResetFactory();
115
116 typedef base::Callback<void(WifiDataProvider*)> WifiDataUpdateCallback;
117
118 // Registers a callback, which will be run whenever new data is available.
119 // Instantiates the singleton if necessary, and always returns it.
120 static WifiDataProvider* Register(WifiDataUpdateCallback* callback);
121
122 // Removes a callback. If this is the last callback, deletes the singleton
123 // instance. Return value indicates success.
124 static bool Unregister(WifiDataUpdateCallback* callback);
125
126 // Provides whatever data the provider has, which may be nothing. Return
127 // value indicates whether this is all the data the provider could ever
128 // obtain.
129 bool GetData(WifiData* data);
130
131 private:
132 // Private constructor and destructor, callers access singleton through
133 // Register and Unregister.
134 WifiDataProvider();
135 virtual ~WifiDataProvider();
136
137 void AddCallback(WifiDataUpdateCallback* callback);
138 bool RemoveCallback(WifiDataUpdateCallback* callback);
139 bool has_callbacks() const;
140
141 void StartDataProvider();
142 void StopDataProvider();
143
144 static WifiDataProviderImplBase* DefaultFactoryFunction();
145
146 // The singleton-like instance of this class. (Not 'true' singleton, as it
147 // may go through multiple create/destroy/create cycles per process instance,
148 // e.g. when under test).
149 static WifiDataProvider* instance_;
150
151 // The factory function used to create the singleton instance.
152 static ImplFactoryFunction factory_function_;
153
154 // The internal implementation.
155 scoped_refptr<WifiDataProviderImplBase> impl_;
156
157 DISALLOW_COPY_AND_ASSIGN(WifiDataProvider); 79 DISALLOW_COPY_AND_ASSIGN(WifiDataProvider);
158 }; 80 };
159 81
160 } // namespace content 82 } // namespace content
161 83
162 #endif // CONTENT_BROWSER_GEOLOCATION_WIFI_DATA_PROVIDER_H_ 84 #endif // CONTENT_BROWSER_GEOLOCATION_WIFI_DATA_PROVIDER_H_
OLDNEW
« no previous file with comments | « content/browser/geolocation/network_location_provider_unittest.cc ('k') | content/browser/geolocation/wifi_data_provider.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698