Clean up extension confirmation prompts and make them consistent between Views and...
[chromium-blink-merge.git] / remoting / host / native_messaging / pipe_messaging_channel.cc
blobbecd71014efabca618ff6561deac1b9af1a6167d
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "remoting/host/native_messaging/pipe_messaging_channel.h"
7 #include "base/basictypes.h"
8 #include "base/bind.h"
9 #include "base/callback.h"
10 #include "base/callback_helpers.h"
11 #include "base/location.h"
12 #include "base/values.h"
14 #if defined(OS_POSIX)
15 #include <unistd.h>
16 #endif
18 namespace {
20 base::File DuplicatePlatformFile(base::File file) {
21 base::PlatformFile result;
22 #if defined(OS_WIN)
23 if (!DuplicateHandle(GetCurrentProcess(),
24 file.TakePlatformFile(),
25 GetCurrentProcess(),
26 &result,
28 FALSE,
29 DUPLICATE_CLOSE_SOURCE | DUPLICATE_SAME_ACCESS)) {
30 PLOG(ERROR) << "Failed to duplicate handle " << file.GetPlatformFile();
31 return base::File();
33 return base::File(result);
34 #elif defined(OS_POSIX)
35 result = dup(file.GetPlatformFile());
36 return base::File(result);
37 #else
38 #error Not implemented.
39 #endif
42 } // namespace
44 namespace remoting {
46 PipeMessagingChannel::PipeMessagingChannel(
47 base::File input,
48 base::File output)
49 : native_messaging_reader_(DuplicatePlatformFile(input.Pass())),
50 native_messaging_writer_(new NativeMessagingWriter(
51 DuplicatePlatformFile(output.Pass()))),
52 event_handler_(nullptr),
53 weak_factory_(this) {
54 weak_ptr_ = weak_factory_.GetWeakPtr();
57 PipeMessagingChannel::~PipeMessagingChannel() {
60 void PipeMessagingChannel::Start(EventHandler* event_handler) {
61 DCHECK(CalledOnValidThread());
62 DCHECK(!event_handler_);
64 event_handler_ = event_handler;
65 DCHECK(event_handler_);
67 native_messaging_reader_.Start(
68 base::Bind(&PipeMessagingChannel::ProcessMessage, weak_ptr_),
69 base::Bind(&PipeMessagingChannel::Shutdown, weak_ptr_));
72 void PipeMessagingChannel::ProcessMessage(scoped_ptr<base::Value> message) {
73 DCHECK(CalledOnValidThread());
75 if (event_handler_)
76 event_handler_->OnMessage(message.Pass());
79 void PipeMessagingChannel::SendMessage(
80 scoped_ptr<base::Value> message) {
81 DCHECK(CalledOnValidThread());
83 bool success = message && native_messaging_writer_;
84 if (success)
85 success = native_messaging_writer_->WriteMessage(*message);
87 if (!success) {
88 // Close the write pipe so no more responses will be sent.
89 native_messaging_writer_.reset();
90 Shutdown();
94 void PipeMessagingChannel::Shutdown() {
95 DCHECK(CalledOnValidThread());
97 if (event_handler_) {
98 // Set |event_handler_| to nullptr to indicate the object is in a shutdown
99 // cycle. Since event_handler->OnDisconnect() will destroy the current
100 // object, |event_handler_| will become a dangling pointer after
101 // OnDisconnect() returns. Therefore, we set |event_handler_| to nullptr
102 // beforehand.
103 EventHandler* handler = event_handler_;
104 event_handler_ = nullptr;
105 handler->OnDisconnect();
109 } // namespace remoting