1// Copyright 2014 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 manager, which is used by multiple
8// NetworkLocationProvider objects.
9//
10// This file provides WifiDataProviderManager, which provides static methods to
11// access the singleton instance. The singleton instance uses a private
12// implementation of WifiDataProvider to abstract across platforms and also to
13// allow mock providers to be used for testing.
14
15#ifndef CONTENT_BROWSER_GEOLOCATION_WIFI_DATA_PROVIDER_MANAGER_H_
16#define CONTENT_BROWSER_GEOLOCATION_WIFI_DATA_PROVIDER_MANAGER_H_
17
18#include <set>
19
20#include "base/basictypes.h"
21#include "base/bind.h"
22#include "base/callback.h"
23#include "base/memory/ref_counted.h"
24#include "base/message_loop/message_loop.h"
25#include "base/strings/string16.h"
26#include "base/strings/string_util.h"
27#include "content/browser/geolocation/wifi_data.h"
28#include "content/common/content_export.h"
29
30namespace content {
31
32class WifiDataProvider;
33
34// A manager for wifi data providers.
35//
36// We use a singleton instance of this class which is shared by multiple network
37// location providers. These location providers access the instance through the
38// Register and Unregister methods.
39class CONTENT_EXPORT WifiDataProviderManager {
40 public:
41  typedef WifiDataProvider* (*ImplFactoryFunction)(void);
42
43  // Sets the factory function which will be used by Register to create the
44  // implementation used by the singleton instance. This factory approach is
45  // used both to abstract accross platform-specific implementations and to
46  // inject mock implementations for testing.
47  static void SetFactoryForTesting(ImplFactoryFunction factory_function_in);
48
49  // Resets the factory function to the default.
50  static void ResetFactoryForTesting();
51
52  typedef base::Closure WifiDataUpdateCallback;
53
54  // Registers a callback, which will be run whenever new data is available.
55  // Instantiates the singleton if necessary, and always returns it.
56  static WifiDataProviderManager* Register(WifiDataUpdateCallback* callback);
57
58  // Removes a callback. If this is the last callback, deletes the singleton
59  // instance. Return value indicates success.
60  static bool Unregister(WifiDataUpdateCallback* callback);
61
62  // Provides whatever data the provider has, which may be nothing. Return
63  // value indicates whether this is all the data the provider could ever
64  // obtain.
65  bool GetData(WifiData* data);
66
67 private:
68  // Private constructor and destructor, callers access singleton through
69  // Register and Unregister.
70  WifiDataProviderManager();
71  ~WifiDataProviderManager();
72
73  void AddCallback(WifiDataUpdateCallback* callback);
74  bool RemoveCallback(WifiDataUpdateCallback* callback);
75  bool has_callbacks() const;
76
77  void StartDataProvider();
78  void StopDataProvider();
79
80  static WifiDataProvider* DefaultFactoryFunction();
81
82  // The singleton-like instance of this class. (Not 'true' singleton, as it
83  // may go through multiple create/destroy/create cycles per process instance,
84  // e.g. when under test).
85  static WifiDataProviderManager* instance_;
86
87  // The factory function used to create the singleton instance.
88  static ImplFactoryFunction factory_function_;
89
90  // The internal implementation.
91  scoped_refptr<WifiDataProvider> impl_;
92
93  DISALLOW_COPY_AND_ASSIGN(WifiDataProviderManager);
94};
95
96}  // namespace content
97
98#endif  // CONTENT_BROWSER_GEOLOCATION_WIFI_DATA_PROVIDER_MANAGER_H_
99