Home | History | Annotate | Download | only in geolocation
      1 // Copyright (c) 2012 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_COMMON_H_
      6 #define CONTENT_BROWSER_GEOLOCATION_WIFI_DATA_PROVIDER_COMMON_H_
      7 
      8 #include <assert.h>
      9 
     10 #include "base/logging.h"
     11 #include "base/memory/scoped_ptr.h"
     12 #include "base/memory/weak_ptr.h"
     13 #include "base/strings/string16.h"
     14 #include "base/threading/thread.h"
     15 #include "content/browser/geolocation/device_data_provider.h"
     16 #include "content/common/content_export.h"
     17 
     18 namespace content {
     19 
     20 // Converts a MAC address stored as an array of uint8 to a string.
     21 string16 MacAddressAsString16(const uint8 mac_as_int[6]);
     22 
     23 // Allows sharing and mocking of the update polling policy function.
     24 class PollingPolicyInterface {
     25  public:
     26   virtual ~PollingPolicyInterface() {}
     27   // Calculates the new polling interval for wiFi scans, given the previous
     28   // interval and whether the last scan produced new results.
     29   virtual void UpdatePollingInterval(bool scan_results_differ) = 0;
     30   virtual int PollingInterval() = 0;
     31   virtual int NoWifiInterval() = 0;
     32 };
     33 
     34 // Generic polling policy, constants are compile-time parameterized to allow
     35 // tuning on a per-platform basis.
     36 template<int DEFAULT_INTERVAL,
     37          int NO_CHANGE_INTERVAL,
     38          int TWO_NO_CHANGE_INTERVAL,
     39          int NO_WIFI_INTERVAL>
     40 class GenericPollingPolicy : public PollingPolicyInterface {
     41  public:
     42   GenericPollingPolicy() : polling_interval_(DEFAULT_INTERVAL) {}
     43   // PollingPolicyInterface
     44   virtual void UpdatePollingInterval(bool scan_results_differ) {
     45     if (scan_results_differ) {
     46       polling_interval_ = DEFAULT_INTERVAL;
     47     } else if (polling_interval_ == DEFAULT_INTERVAL) {
     48       polling_interval_ = NO_CHANGE_INTERVAL;
     49     } else {
     50       DCHECK(polling_interval_ == NO_CHANGE_INTERVAL ||
     51              polling_interval_ == TWO_NO_CHANGE_INTERVAL);
     52       polling_interval_ = TWO_NO_CHANGE_INTERVAL;
     53     }
     54   }
     55   virtual int PollingInterval() { return polling_interval_; }
     56   virtual int NoWifiInterval() { return NO_WIFI_INTERVAL; }
     57 
     58  private:
     59   int polling_interval_;
     60 };
     61 
     62 // Base class to promote code sharing between platform specific wifi data
     63 // providers. It's optional for specific platforms to derive this, but if they
     64 // do threading and polling is taken care of by this base class, and all the
     65 // platform need do is provide the underlying WLAN access API and policy policy,
     66 // both of which will be create & accessed in the worker thread (only).
     67 // Also designed this way to promotes ease of testing the cross-platform
     68 // behavior w.r.t. polling & threading.
     69 class CONTENT_EXPORT WifiDataProviderCommon
     70     : public WifiDataProviderImplBase,
     71       private base::Thread {
     72  public:
     73   // Interface to abstract the low level data OS library call, and to allow
     74   // mocking (hence public).
     75   class WlanApiInterface {
     76    public:
     77     virtual ~WlanApiInterface() {}
     78     // Gets wifi data for all visible access points.
     79     virtual bool GetAccessPointData(WifiData::AccessPointDataSet* data) = 0;
     80   };
     81 
     82   WifiDataProviderCommon();
     83 
     84   // WifiDataProviderImplBase implementation
     85   virtual bool StartDataProvider() OVERRIDE;
     86   virtual void StopDataProvider() OVERRIDE;
     87   virtual bool GetData(WifiData* data) OVERRIDE;
     88 
     89  protected:
     90   virtual ~WifiDataProviderCommon();
     91 
     92   // Returns ownership. Will be called from the worker thread.
     93   virtual WlanApiInterface* NewWlanApi() = 0;
     94 
     95   // Returns ownership. Will be called from the worker thread.
     96   virtual PollingPolicyInterface* NewPollingPolicy() = 0;
     97 
     98  private:
     99   // Thread implementation
    100   virtual void Init() OVERRIDE;
    101   virtual void CleanUp() OVERRIDE;
    102 
    103   // Task which run in the child thread.
    104   void DoWifiScanTask();
    105 
    106   // Will schedule a scan; i.e. enqueue DoWifiScanTask deferred task.
    107   void ScheduleNextScan(int interval);
    108 
    109   WifiData wifi_data_;
    110   base::Lock data_mutex_;
    111 
    112   // Whether we've successfully completed a scan for WiFi data (or the polling
    113   // thread has terminated early).
    114   bool is_first_scan_complete_;
    115 
    116   // Underlying OS wifi API.
    117   scoped_ptr<WlanApiInterface> wlan_api_;
    118 
    119   // Controls the polling update interval.
    120   scoped_ptr<PollingPolicyInterface> polling_policy_;
    121 
    122   // Holder for the tasks which run on the thread; takes care of cleanup.
    123   base::WeakPtrFactory<WifiDataProviderCommon> weak_factory_;
    124 
    125   DISALLOW_COPY_AND_ASSIGN(WifiDataProviderCommon);
    126 };
    127 
    128 }  // namespace content
    129 
    130 #endif  // CONTENT_BROWSER_GEOLOCATION_WIFI_DATA_PROVIDER_COMMON_H_
    131