| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2010 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 CHROME_BROWSER_GEOLOCATION_GATEWAY_DATA_PROVIDER_COMMON_H_ | |
| 6 #define CHROME_BROWSER_GEOLOCATION_GATEWAY_DATA_PROVIDER_COMMON_H_ | |
| 7 #pragma once | |
| 8 | |
| 9 #include "base/logging.h" | |
| 10 #include "base/scoped_ptr.h" | |
| 11 #include "base/string16.h" | |
| 12 #include "base/task.h" | |
| 13 #include "base/threading/thread.h" | |
| 14 #include "content/browser/geolocation/device_data_provider.h" | |
| 15 | |
| 16 namespace { | |
| 17 const int kDefaultInterval = 20 * 60 * 1000; // 20 mins | |
| 18 const int kNoRouterInterval = 30 * 1000; // 30s | |
| 19 } | |
| 20 | |
| 21 // Allows sharing and mocking of the update polling policy function. | |
| 22 class GatewayPollingPolicyInterface { | |
| 23 public: | |
| 24 virtual ~GatewayPollingPolicyInterface() {} | |
| 25 | |
| 26 // Returns the polling interval to be used when we are connected to a router | |
| 27 // via Ethernet. | |
| 28 virtual int PollingInterval() = 0; | |
| 29 // Returns the polling interval to be used when there are no adapters | |
| 30 // connected to a router via Ethernet. | |
| 31 virtual int NoRouterInterval() = 0; | |
| 32 }; | |
| 33 | |
| 34 // Base class to promote sharing between platform specific gateway data | |
| 35 // providers. | |
| 36 // TODO(allanwoj): This class and WifiDataProviderCommon are extremely similar | |
| 37 // so we might as well derive all data providers from a single base class | |
| 38 // generalised for different network data types. | |
| 39 class GatewayDataProviderCommon | |
| 40 : public GatewayDataProviderImplBase, | |
| 41 private base::Thread { | |
| 42 public: | |
| 43 // Interface to abstract the low level data OS library call, and to allow | |
| 44 // mocking (hence public). | |
| 45 class GatewayApiInterface { | |
| 46 public: | |
| 47 virtual ~GatewayApiInterface() {} | |
| 48 // Gets wifi data for all visible access points. | |
| 49 virtual bool GetRouterData(GatewayData::RouterDataSet* data) = 0; | |
| 50 }; | |
| 51 | |
| 52 GatewayDataProviderCommon(); | |
| 53 | |
| 54 // GatewayDataProviderImplBase implementation. | |
| 55 virtual bool StartDataProvider(); | |
| 56 virtual void StopDataProvider(); | |
| 57 virtual bool GetData(GatewayData *data); | |
| 58 | |
| 59 protected: | |
| 60 virtual ~GatewayDataProviderCommon(); | |
| 61 | |
| 62 // Returns ownership. Will be called from the worker thread. | |
| 63 virtual GatewayApiInterface* NewGatewayApi() = 0; | |
| 64 // Returns ownership. Will be called from the worker thread. | |
| 65 virtual GatewayPollingPolicyInterface* NewPollingPolicy(); | |
| 66 | |
| 67 private: | |
| 68 class GenericGatewayPollingPolicy : public GatewayPollingPolicyInterface { | |
| 69 public: | |
| 70 GenericGatewayPollingPolicy() {} | |
| 71 | |
| 72 // GatewayPollingPolicyInterface | |
| 73 virtual int PollingInterval() { return kDefaultInterval; } | |
| 74 virtual int NoRouterInterval() { return kNoRouterInterval; } | |
| 75 }; | |
| 76 | |
| 77 // Thread implementation. | |
| 78 virtual void Init(); | |
| 79 virtual void CleanUp(); | |
| 80 | |
| 81 // Task which run in the child thread. | |
| 82 void DoRouterScanTask(); | |
| 83 // Will schedule a scan; i.e. enqueue DoRouterScanTask deferred task. | |
| 84 void ScheduleNextScan(int interval); | |
| 85 | |
| 86 base::Lock data_mutex_; | |
| 87 // Whether we've successfully completed a scan for gateway data (or the | |
| 88 // polling thread has terminated early). | |
| 89 bool is_first_scan_complete_; | |
| 90 // Underlying OS gateway API. | |
| 91 scoped_ptr<GatewayApiInterface> gateway_api_; | |
| 92 GatewayData gateway_data_; | |
| 93 // Controls the polling update interval. | |
| 94 scoped_ptr<GatewayPollingPolicyInterface> polling_policy_; | |
| 95 // Holder for the tasks which run on the thread; takes care of cleanup. | |
| 96 ScopedRunnableMethodFactory<GatewayDataProviderCommon> task_factory_; | |
| 97 | |
| 98 DISALLOW_COPY_AND_ASSIGN(GatewayDataProviderCommon); | |
| 99 }; | |
| 100 | |
| 101 #endif // CHROME_BROWSER_GEOLOCATION_GATEWAY_DATA_PROVIDER_COMMON_H_ | |
| OLD | NEW |