- add sources.
[platform/framework/web/crosswalk.git] / src / content / browser / geolocation / wifi_data_provider.h
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.
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_
19 #define CONTENT_BROWSER_GEOLOCATION_WIFI_DATA_PROVIDER_H_
20
21 #include <set>
22
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"
32
33 namespace content {
34
35 class WifiDataProvider;
36
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();
45
46   // Tells the provider to start looking for data. Callbacks will start
47   // receiving notifications after this call.
48   virtual void StartDataProvider() = 0;
49
50   // Tells the provider to stop looking for data. Callbacks will stop
51   // receiving notifications after this call.
52   virtual void StopDataProvider() = 0;
53
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;
58
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);
62
63   typedef base::Callback<void(WifiDataProvider*)> WifiDataUpdateCallback;
64
65   void AddCallback(WifiDataUpdateCallback* callback);
66
67   bool RemoveCallback(WifiDataUpdateCallback* callback);
68
69   bool has_callbacks() const;
70
71  protected:
72   friend class base::RefCountedThreadSafe<WifiDataProviderImplBase>;
73   virtual ~WifiDataProviderImplBase();
74
75   typedef std::set<WifiDataUpdateCallback*> CallbackSet;
76
77   // Runs all callbacks via a posted task, so we can unwind callstack here and
78   // avoid client reentrancy.
79   void RunCallbacks();
80
81   bool CalledOnClientThread() const;
82
83   base::MessageLoop* client_loop() const;
84
85  private:
86   void DoRunCallbacks();
87
88   WifiDataProvider* container_;
89
90   // Reference to the client's message loop. All callbacks should happen in this
91   // context.
92   base::MessageLoop* client_loop_;
93
94   CallbackSet callbacks_;
95
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);
158 };
159
160 }  // namespace content
161
162 #endif  // CONTENT_BROWSER_GEOLOCATION_WIFI_DATA_PROVIDER_H_