summaryrefslogtreecommitdiffstats
path: root/remoting/host/native_messaging/native_messaging_channel.cc
blob: 186b33b41b3f0e5c2971022e58447be22a6a0d56 (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
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
103
104
105
106
107
108
109
110
// 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.

#include "remoting/host/native_messaging/native_messaging_channel.h"

#include "base/basictypes.h"
#include "base/bind.h"
#include "base/callback.h"
#include "base/callback_helpers.h"
#include "base/location.h"
#include "base/values.h"

#if defined(OS_POSIX)
#include <unistd.h>
#endif

namespace {

base::PlatformFile DuplicatePlatformFile(base::PlatformFile handle) {
  base::PlatformFile result;
#if defined(OS_WIN)
  if (!DuplicateHandle(GetCurrentProcess(),
                       handle,
                       GetCurrentProcess(),
                       &result,
                       0,
                       FALSE,
                       DUPLICATE_CLOSE_SOURCE | DUPLICATE_SAME_ACCESS)) {
    PLOG(ERROR) << "Failed to duplicate handle " << handle;
    return base::kInvalidPlatformFileValue;
  }
  return result;
#elif defined(OS_POSIX)
  result = dup(handle);
  base::ClosePlatformFile(handle);
  return result;
#else
#error Not implemented.
#endif
}

}  // namespace

namespace remoting {

NativeMessagingChannel::NativeMessagingChannel(
    base::PlatformFile input,
    base::PlatformFile output)
    : native_messaging_reader_(DuplicatePlatformFile(input)),
      native_messaging_writer_(new NativeMessagingWriter(
          DuplicatePlatformFile(output))),
      weak_factory_(this) {
  weak_ptr_ = weak_factory_.GetWeakPtr();
}

NativeMessagingChannel::~NativeMessagingChannel() {
}

void NativeMessagingChannel::Start(const SendMessageCallback& received_message,
                                   const base::Closure& quit_closure) {
  DCHECK(CalledOnValidThread());
  DCHECK(received_message_.is_null());
  DCHECK(quit_closure_.is_null());

  received_message_ = received_message;
  quit_closure_ = quit_closure;

  native_messaging_reader_.Start(
      base::Bind(&NativeMessagingChannel::ProcessMessage, weak_ptr_),
      base::Bind(&NativeMessagingChannel::Shutdown, weak_ptr_));
}

void NativeMessagingChannel::ProcessMessage(scoped_ptr<base::Value> message) {
  DCHECK(CalledOnValidThread());

  if (message->GetType() != base::Value::TYPE_DICTIONARY) {
    LOG(ERROR) << "Expected DictionaryValue";
    Shutdown();
    return;
  }

  scoped_ptr<base::DictionaryValue> message_dict(
      static_cast<base::DictionaryValue*>(message.release()));
  received_message_.Run(message_dict.Pass());
}

void NativeMessagingChannel::SendMessage(
    scoped_ptr<base::DictionaryValue> message) {
  DCHECK(CalledOnValidThread());

  bool success = message && native_messaging_writer_;
  if (success)
    success = native_messaging_writer_->WriteMessage(*message);

  if (!success) {
    // Close the write pipe so no more responses will be sent.
    native_messaging_writer_.reset();
    Shutdown();
  }
}

void NativeMessagingChannel::Shutdown() {
  DCHECK(CalledOnValidThread());

  if (!quit_closure_.is_null())
    base::ResetAndReturn(&quit_closure_).Run();
}

}  // namespace remoting