Rename Animate as Begin(Main)Frame
[chromium-blink-merge.git] / content / browser / geolocation / wifi_data_provider.h
blobbfb06ec54446962381dfaea0818ce3edf6b07a69
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
3 // found in the LICENSE file.
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.
15 // This file also provides WifiDataProviderImplBase, a base class which
16 // provides common functionality for the private implementations.
18 #ifndef CONTENT_BROWSER_GEOLOCATION_WIFI_DATA_PROVIDER_H_
19 #define CONTENT_BROWSER_GEOLOCATION_WIFI_DATA_PROVIDER_H_
21 #include <set>
23 #include "base/basictypes.h"
24 #include "base/bind.h"
25 #include "base/callback.h"
26 #include "base/memory/ref_counted.h"
27 #include "base/message_loop/message_loop.h"
28 #include "base/strings/string16.h"
29 #include "base/strings/string_util.h"
30 #include "content/browser/geolocation/wifi_data.h"
31 #include "content/common/content_export.h"
33 namespace content {
35 class WifiDataProvider;
37 // See class WifiDataProvider for the public client API.
38 // WifiDataProvider uses containment to hide platform-specific implementation
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:
44 WifiDataProviderImplBase();
46 // Tells the provider to start looking for data. Callbacks will start
47 // receiving notifications after this call.
48 virtual void StartDataProvider() = 0;
50 // Tells the provider to stop looking for data. Callbacks will stop
51 // receiving notifications after this call.
52 virtual void StopDataProvider() = 0;
54 // Provides whatever data the provider has, which may be nothing. Return
55 // value indicates whether this is all the data the provider could ever
56 // obtain.
57 virtual bool GetData(WifiData* data) = 0;
59 // Sets the container of this class, which is of type WifiDataProvider.
60 // This is required to pass as a parameter when calling a callback.
61 void SetContainer(WifiDataProvider* container);
63 typedef base::Callback<void(WifiDataProvider*)> WifiDataUpdateCallback;
65 void AddCallback(WifiDataUpdateCallback* callback);
67 bool RemoveCallback(WifiDataUpdateCallback* callback);
69 bool has_callbacks() const;
71 protected:
72 friend class base::RefCountedThreadSafe<WifiDataProviderImplBase>;
73 virtual ~WifiDataProviderImplBase();
75 typedef std::set<WifiDataUpdateCallback*> CallbackSet;
77 // Runs all callbacks via a posted task, so we can unwind callstack here and
78 // avoid client reentrancy.
79 void RunCallbacks();
81 bool CalledOnClientThread() const;
83 base::MessageLoop* client_loop() const;
85 private:
86 void DoRunCallbacks();
88 WifiDataProvider* container_;
90 // Reference to the client's message loop. All callbacks should happen in this
91 // context.
92 base::MessageLoop* client_loop_;
94 CallbackSet callbacks_;
96 DISALLOW_COPY_AND_ASSIGN(WifiDataProviderImplBase);
99 // A wifi data provider
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);
113 // Resets the factory function to the default.
114 static void ResetFactory();
116 typedef base::Callback<void(WifiDataProvider*)> WifiDataUpdateCallback;
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);
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);
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);
131 private:
132 // Private constructor and destructor, callers access singleton through
133 // Register and Unregister.
134 WifiDataProvider();
135 virtual ~WifiDataProvider();
137 void AddCallback(WifiDataUpdateCallback* callback);
138 bool RemoveCallback(WifiDataUpdateCallback* callback);
139 bool has_callbacks() const;
141 void StartDataProvider();
142 void StopDataProvider();
144 static WifiDataProviderImplBase* DefaultFactoryFunction();
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_;
151 // The factory function used to create the singleton instance.
152 static ImplFactoryFunction factory_function_;
154 // The internal implementation.
155 scoped_refptr<WifiDataProviderImplBase> impl_;
157 DISALLOW_COPY_AND_ASSIGN(WifiDataProvider);
160 } // namespace content
162 #endif // CONTENT_BROWSER_GEOLOCATION_WIFI_DATA_PROVIDER_H_