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 #ifndef CONTENT_BROWSER_GEOLOCATION_WIFI_DATA_PROVIDER_H_ 6 #define CONTENT_BROWSER_GEOLOCATION_WIFI_DATA_PROVIDER_H_ 7 8 #include <set> 9 10 #include "base/basictypes.h" 11 #include "base/bind.h" 12 #include "base/callback.h" 13 #include "base/memory/ref_counted.h" 14 #include "base/message_loop/message_loop.h" 15 #include "base/strings/string16.h" 16 #include "base/strings/string_util.h" 17 #include "content/browser/geolocation/wifi_data.h" 18 #include "content/common/content_export.h" 19 20 namespace content { 21 22 class CONTENT_EXPORT WifiDataProvider 23 : public base::RefCountedThreadSafe<WifiDataProvider> { 24 public: 25 WifiDataProvider(); 26 27 // Tells the provider to start looking for data. Callbacks will start 28 // receiving notifications after this call. 29 virtual void StartDataProvider() = 0; 30 31 // Tells the provider to stop looking for data. Callbacks will stop 32 // receiving notifications after this call. 33 virtual void StopDataProvider() = 0; 34 35 // Provides whatever data the provider has, which may be nothing. Return 36 // value indicates whether this is all the data the provider could ever 37 // obtain. 38 virtual bool GetData(WifiData* data) = 0; 39 40 typedef base::Closure WifiDataUpdateCallback; 41 42 void AddCallback(WifiDataUpdateCallback* callback); 43 44 bool RemoveCallback(WifiDataUpdateCallback* callback); 45 46 bool has_callbacks() const; 47 48 protected: 49 friend class base::RefCountedThreadSafe<WifiDataProvider>; 50 virtual ~WifiDataProvider(); 51 52 typedef std::set<WifiDataUpdateCallback*> CallbackSet; 53 54 // Runs all callbacks via a posted task, so we can unwind callstack here and 55 // avoid client reentrancy. 56 void RunCallbacks(); 57 58 bool CalledOnClientThread() const; 59 60 base::MessageLoop* client_loop() const; 61 62 private: 63 void DoRunCallbacks(); 64 65 // Reference to the client's message loop. All callbacks should happen in this 66 // context. 67 base::MessageLoop* client_loop_; 68 69 CallbackSet callbacks_; 70 71 DISALLOW_COPY_AND_ASSIGN(WifiDataProvider); 72 }; 73 74 } // namespace content 75 76 #endif // CONTENT_BROWSER_GEOLOCATION_WIFI_DATA_PROVIDER_H_ 77