summaryrefslogtreecommitdiffstats
path: root/chromeos/binder/command_broker.h
blob: 96a3893cedf9289276b9d4d5325177e054a078a8 (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
57
58
59
60
61
62
63
64
65
66
// Copyright 2015 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 CHROMEOS_BINDER_COMMAND_BROKER_H_
#define CHROMEOS_BINDER_COMMAND_BROKER_H_

#include <utility>

#include "base/basictypes.h"
#include "base/macros.h"
#include "base/memory/scoped_ptr.h"
#include "base/threading/thread_checker.h"
#include "chromeos/binder/command_stream.h"

namespace binder {

class Driver;
class TransactionData;

// Issues appropriate outgoing commands to perform required tasks, and
// dispatches incoming commands to appropriate objects.
// Usually this class lives as long as the corresponding thread.
// TODO(hashimoto): Add code to handle incoming commands (e.g. transactions).
class CommandBroker : public CommandStream::IncomingCommandHandler {
 public:
  explicit CommandBroker(Driver* driver);
  ~CommandBroker() override;

  // Performs transaction with the remote object specified by the handle.
  // Returns true on success. If not one-way transaction, this method blocks
  // until the target object sends a reply.
  bool Transact(int32 handle,
                const TransactionData& request,
                scoped_ptr<TransactionData>* reply);

  // CommandStream::IncomingCommandHandler override:
  void OnReply(scoped_ptr<TransactionData> data) override;
  void OnDeadReply() override;
  void OnTransactionComplete() override;
  void OnFailedReply() override;

 private:
  enum ResponseType {
    RESPONSE_TYPE_NONE,
    RESPONSE_TYPE_TRANSACTION_COMPLETE,
    RESPONSE_TYPE_TRANSACTION_REPLY,
    RESPONSE_TYPE_FAILED,
    RESPONSE_TYPE_DEAD,
  };

  // Waits for a response to the previous transaction.
  ResponseType WaitForResponse(scoped_ptr<TransactionData>* data);

  base::ThreadChecker thread_checker_;
  CommandStream command_stream_;

  ResponseType response_type_ = RESPONSE_TYPE_NONE;
  scoped_ptr<TransactionData> response_data_;

  DISALLOW_COPY_AND_ASSIGN(CommandBroker);
};

}  // namespace binder

#endif  // CHROMEOS_BINDER_COMMAND_BROKER_H_