Home | History | Annotate | Download | only in device_orientation
      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_DEVICE_ORIENTATION_PROVIDER_IMPL_H_
      6 #define CONTENT_BROWSER_DEVICE_ORIENTATION_PROVIDER_IMPL_H_
      7 
      8 #include <map>
      9 #include <set>
     10 #include <vector>
     11 
     12 #include "base/memory/scoped_ptr.h"
     13 #include "base/memory/weak_ptr.h"
     14 #include "content/browser/device_orientation/data_fetcher.h"
     15 #include "content/browser/device_orientation/device_data.h"
     16 #include "content/browser/device_orientation/provider.h"
     17 #include "content/common/content_export.h"
     18 
     19 namespace base {
     20 class MessageLoop;
     21 }
     22 
     23 namespace content {
     24 
     25 class ProviderImpl : public Provider {
     26  public:
     27   typedef DataFetcher* (*DataFetcherFactory)();
     28 
     29   // Create a ProviderImpl that uses the factory to create a DataFetcher that
     30   // can provide data. A NULL DataFetcherFactory indicates that there are no
     31   // DataFetchers for this OS.
     32   CONTENT_EXPORT ProviderImpl(DataFetcherFactory factory);
     33 
     34   // From Provider.
     35   virtual void AddObserver(Observer* observer) OVERRIDE;
     36   virtual void RemoveObserver(Observer* observer) OVERRIDE;
     37 
     38  private:
     39   class PollingThread;
     40 
     41   virtual ~ProviderImpl();
     42 
     43   // Starts or Stops the provider. Called from creator_loop_.
     44   void Start(DeviceData::Type type);
     45   void Stop();
     46 
     47   void ScheduleInitializePollingThread(DeviceData::Type device_data_type);
     48   void ScheduleDoAddPollingDataType(DeviceData::Type type);
     49 
     50   // Method for notifying observers of a data update.
     51   // Runs on the creator_thread_.
     52   void DoNotify(const scoped_refptr<const DeviceData>& data,
     53                 DeviceData::Type device_data_type);
     54 
     55   static bool ShouldFireEvent(const DeviceData* old_data,
     56       const DeviceData* new_data, DeviceData::Type device_data_type);
     57 
     58   // The Message Loop on which this object was created.
     59   // Typically the I/O loop, but may be something else during testing.
     60   base::MessageLoop* creator_loop_;
     61 
     62   // Members below are only to be used from the creator_loop_.
     63   DataFetcherFactory factory_;
     64   std::set<Observer*> observers_;
     65   std::map<DeviceData::Type, scoped_refptr<const DeviceData> >
     66       last_notifications_map_;
     67 
     68   // When polling_thread_ is running, members below are only to be used
     69   // from that thread.
     70   base::WeakPtrFactory<ProviderImpl> weak_factory_;
     71 
     72   // Polling is done on this background thread. PollingThread is owned by
     73   // the ProviderImpl object. But its deletion doesn't happen synchronously
     74   // along with deletion of the ProviderImpl. Thus this should be a raw
     75   // pointer instead of scoped_ptr.
     76   PollingThread* polling_thread_;
     77 };
     78 
     79 }  // namespace content
     80 
     81 #endif  // CONTENT_BROWSER_DEVICE_ORIENTATION_PROVIDER_IMPL_H_
     82