summaryrefslogtreecommitdiffstats
path: root/remoting/host/native_messaging/native_messaging_pipe.cc
blob: 13abc35d4ddc821e7d99a3bb04193dbbb5be63a4 (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
// 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.

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

#include "base/callback_helpers.h"
#include "base/json/json_reader.h"
#include "base/json/json_writer.h"
#include "base/values.h"

namespace remoting {

NativeMessagingPipe::NativeMessagingPipe() {
}

NativeMessagingPipe::~NativeMessagingPipe() {
}

void NativeMessagingPipe::Start(
    scoped_ptr<extensions::NativeMessageHost> host,
    scoped_ptr<extensions::NativeMessagingChannel> channel,
    const base::Closure& quit_closure) {
  host_ = host.Pass();
  channel_ = channel.Pass();
  quit_closure_ = quit_closure;
  channel_->Start(this);
}

void NativeMessagingPipe::OnMessage(scoped_ptr<base::Value> message) {
  std::string message_json;
  base::JSONWriter::Write(message.get(), &message_json);
  host_->OnMessage(message_json);
}

void NativeMessagingPipe::OnDisconnect() {
  if (!quit_closure_.is_null())
    base::ResetAndReturn(&quit_closure_).Run();
}

void NativeMessagingPipe::PostMessageFromNativeHost(
    const std::string& message) {
  scoped_ptr<base::Value> json(base::JSONReader::Read(message));
  channel_->SendMessage(json.Pass());
}

void NativeMessagingPipe::CloseChannel(const std::string& error_message) {
  if (!quit_closure_.is_null())
    base::ResetAndReturn(&quit_closure_).Run();
}

}  // namespace remoting