summaryrefslogtreecommitdiffstats
path: root/chrome/browser/extensions/api/serial/serial_io_handler_posix.h
blob: 1128e68dbaac4d1372caa266d499dcd94408652d (plain)
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
// Copyright 2013 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 CHROME_BROWSER_EXTENSIONS_API_SERIAL_SERIAL_IO_HANDLER_POSIX_H_
#define CHROME_BROWSER_EXTENSIONS_API_SERIAL_SERIAL_IO_HANDLER_POSIX_H_

#include "base/message_loop/message_loop.h"
#include "chrome/browser/extensions/api/serial/serial_io_handler.h"

namespace extensions {

class SerialIoHandlerPosix : public SerialIoHandler,
                             public base::MessageLoopForIO::Watcher {
 protected:
  // SerialIoHandler impl.
  virtual void ReadImpl() OVERRIDE;
  virtual void WriteImpl() OVERRIDE;
  virtual void CancelReadImpl() OVERRIDE;
  virtual void CancelWriteImpl() OVERRIDE;
  virtual bool Flush() const OVERRIDE;
  virtual bool GetControlSignals(
      api::serial::DeviceControlSignals* control_signals) const OVERRIDE;
  virtual bool SetControlSignals(
      const api::serial::HostControlSignals& control_signals) OVERRIDE;
  virtual bool ConfigurePort(
      const api::serial::ConnectionOptions& options) OVERRIDE;
  virtual bool GetPortInfo(api::serial::ConnectionInfo* info) const OVERRIDE;
  virtual bool PostOpen() OVERRIDE;

 private:
  friend class SerialIoHandler;

  SerialIoHandlerPosix();
  virtual ~SerialIoHandlerPosix();

  // base::MessageLoopForIO::Watcher implementation.
  virtual void OnFileCanWriteWithoutBlocking(int fd) OVERRIDE;
  virtual void OnFileCanReadWithoutBlocking(int fd) OVERRIDE;

  void EnsureWatchingReads();
  void EnsureWatchingWrites();

  base::MessageLoopForIO::FileDescriptorWatcher file_read_watcher_;
  base::MessageLoopForIO::FileDescriptorWatcher file_write_watcher_;

  // Flags indicating if the message loop is watching the device for IO events.
  bool is_watching_reads_;
  bool is_watching_writes_;

  DISALLOW_COPY_AND_ASSIGN(SerialIoHandlerPosix);
};

}  // namespace extensions

#endif  // CHROME_BROWSER_EXTENSIONS_API_SERIAL_SERIAL_IO_HANDLER_POSIX_H_