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"
9 #include "base/callback.h"
10 #include "base/callback_helpers.h"
11 #include "base/location.h"
12 #include "base/values.h"
20 base::File
DuplicatePlatformFile(base::File file
) {
21 base::PlatformFile result
;
23 if (!DuplicateHandle(GetCurrentProcess(),
24 file
.TakePlatformFile(),
29 DUPLICATE_CLOSE_SOURCE
| DUPLICATE_SAME_ACCESS
)) {
30 PLOG(ERROR
) << "Failed to duplicate handle " << file
.GetPlatformFile();
33 return base::File(result
);
34 #elif defined(OS_POSIX)
35 result
= dup(file
.GetPlatformFile());
36 return base::File(result
);
38 #error Not implemented.
46 PipeMessagingChannel::PipeMessagingChannel(
49 : native_messaging_reader_(DuplicatePlatformFile(input
.Pass())),
50 native_messaging_writer_(new NativeMessagingWriter(
51 DuplicatePlatformFile(output
.Pass()))),
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());
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_
;
85 success
= native_messaging_writer_
->WriteMessage(*message
);
88 // Close the write pipe so no more responses will be sent.
89 native_messaging_writer_
.reset();
94 void PipeMessagingChannel::Shutdown() {
95 DCHECK(CalledOnValidThread());
98 // Set event_handler_ to NULL to indicate the object is in a shutdown cycle.
99 // Since event_handler->OnDisconnect() will destroy the current object,
100 // |event_handler_| will become a dangling pointer after OnDisconnect()
101 // returns. Therefore, we set |event_handler_| to NULL beforehand.
102 EventHandler
* handler
= event_handler_
;
103 event_handler_
= NULL
;
104 handler
->OnDisconnect();
108 } // namespace remoting