File: data_fetcher_shared_memory_base.h

package info (click to toggle)
chromium-browser 41.0.2272.118-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 2,189,132 kB
  • sloc: cpp: 9,691,462; ansic: 3,341,451; python: 712,689; asm: 518,779; xml: 208,926; java: 169,820; sh: 119,353; perl: 68,907; makefile: 28,311; yacc: 13,305; objc: 11,385; tcl: 3,186; cs: 2,225; sql: 2,217; lex: 2,215; lisp: 1,349; pascal: 1,256; awk: 407; ruby: 155; sed: 53; php: 14; exp: 11
file content (102 lines) | stat: -rw-r--r-- 3,896 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CONTENT_BROWSER_DEVICE_SENSORS_DATA_FETCHER_SHARED_MEMORY_BASE_H_
#define CONTENT_BROWSER_DEVICE_SENSORS_DATA_FETCHER_SHARED_MEMORY_BASE_H_

#include <map>

#include "base/memory/scoped_ptr.h"
#include "base/memory/shared_memory.h"
#include "base/message_loop/message_loop.h"
#include "content/browser/device_sensors/inertial_sensor_consts.h"
#include "content/common/content_export.h"

namespace content {

// Sensor data fetchers should derive from this base class and implement
// the abstract Start() and Stop() methods.
// If the fetcher requires polling it should also implement IsPolling()
// to return true and the Fetch() method which will be called from the
// polling thread to fetch data at regular intervals.
class CONTENT_EXPORT DataFetcherSharedMemoryBase {
 public:
  // Starts updating the shared memory buffer with sensor data at
  // regular intervals. Returns true if the relevant sensors could
  // be successfully activated.
  bool StartFetchingDeviceData(ConsumerType consumer_type);

  // Stops updating the shared memory buffer. Returns true if the
  // relevant sensors could be successfully deactivated.
  bool StopFetchingDeviceData(ConsumerType consumer_type);

  // Should be called before destruction to make sure all active
  // sensors are unregistered.
  void StopFetchingAllDeviceData();

  // Returns the shared memory handle of the device sensor data
  // duplicated into the given process. This method should only be
  // called after a call to StartFetchingDeviceData method with
  // corresponding |consumer_type| parameter.
  base::SharedMemoryHandle GetSharedMemoryHandleForProcess(
      ConsumerType consumer_type, base::ProcessHandle process);

  enum FetcherType {
    // Fetcher runs on the same thread as its creator.
    FETCHER_TYPE_DEFAULT,
    // Fetcher runs on a separate thread calling |Fetch()| at regular intervals.
    FETCHER_TYPE_POLLING_CALLBACK,
    // Fetcher runs on a separate thread, but no callbacks are executed.
    FETCHER_TYPE_SEPARATE_THREAD
  };

 protected:
  class PollingThread;

  DataFetcherSharedMemoryBase();
  virtual ~DataFetcherSharedMemoryBase();

  // Returns the message loop of the polling thread.
  // Returns NULL if there is no polling thread.
  base::MessageLoop* GetPollingMessageLoop() const;

  // If IsPolling() is true this method is called from the |polling_thread_|
  // at regular intervals.
  virtual void Fetch(unsigned consumer_bitmask);

  // Returns the type of thread this fetcher runs on.
  virtual FetcherType GetType() const;

  // Returns the sensor sampling interval. In particular if this fetcher
  // GetType() == FETCHER_TYPE_POLLING_CALLBACK the interval between
  // successive calls to Fetch().
  virtual base::TimeDelta GetInterval() const;

  // Start() method should call InitSharedMemoryBuffer() to get the shared
  // memory pointer. If IsPolling() is true both Start() and Stop() methods
  // are called from the |polling_thread_|.
  virtual bool Start(ConsumerType consumer_type, void* buffer) = 0;
  virtual bool Stop(ConsumerType consumer_type) = 0;

  bool IsPollingTimerRunningForTesting() const;

 private:
  bool InitAndStartPollingThreadIfNecessary();
  base::SharedMemory* GetSharedMemory(ConsumerType consumer_type);
  void* GetSharedMemoryBuffer(ConsumerType consumer_type);

  unsigned started_consumers_;

  scoped_ptr<PollingThread> polling_thread_;

  // Owning pointers. Objects in the map are deleted in dtor.
  typedef std::map<ConsumerType, base::SharedMemory*> SharedMemoryMap;
  SharedMemoryMap shared_memory_map_;

  DISALLOW_COPY_AND_ASSIGN(DataFetcherSharedMemoryBase);
};

}  // namespace content

#endif  // CONTENT_BROWSER_DEVICE_SENSORS_DATA_FETCHER_SHARED_MEMORY_BASE_H_