OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 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. | |
4 | |
5 #include "ui/viewer/viewer_ipc_server.h" | |
6 | |
7 #include "ui/viewer/viewer_messages.h" | |
8 #include "ui/viewer/viewer_process.h" | |
9 #include "ipc/ipc_logging.h" | |
10 | |
11 ViewerIPCServer::ViewerIPCServer(const IPC::ChannelHandle& channel_handle) | |
12 : client_connected_(false), channel_handle_(channel_handle) { | |
13 } | |
14 | |
15 ViewerIPCServer::~ViewerIPCServer() { | |
16 #ifdef IPC_MESSAGE_LOG_ENABLED | |
17 IPC::Logging::GetInstance()->SetIPCSender(NULL); | |
18 #endif | |
19 | |
20 channel_->RemoveFilter(sync_message_filter_.get()); | |
21 } | |
22 | |
23 bool ViewerIPCServer::Init() { | |
24 #ifdef IPC_MESSAGE_LOG_ENABLED | |
25 IPC::Logging::GetInstance()->SetIPCSender(this); | |
26 #endif | |
27 sync_message_filter_ = | |
28 new IPC::SyncMessageFilter(g_viewer_process->shutdown_event()); | |
29 CreateChannel(); | |
30 return true; | |
31 } | |
32 | |
33 bool ViewerIPCServer::Send(IPC::Message* msg) { | |
34 if (!channel_.get()) { | |
35 delete msg; | |
36 return false; | |
37 } | |
38 | |
39 return channel_->Send(msg); | |
40 } | |
41 | |
42 bool ViewerIPCServer::OnMessageReceived(const IPC::Message& msg) { | |
43 bool handled = true; | |
44 // When we get a message, always mark the client as connected. The | |
45 // ChannelProxy::Context is only letting OnChannelConnected get called once, | |
46 // so on the Mac and Linux, we never would set client_connected_ to true | |
47 // again on subsequent connections. | |
48 client_connected_ = true; | |
49 IPC_BEGIN_MESSAGE_MAP(ViewerIPCServer, msg) | |
50 IPC_MESSAGE_HANDLER(ViewerMsg_PbufferHandle, | |
51 OnPbufferHandle) | |
52 IPC_MESSAGE_UNHANDLED(handled = false) | |
53 IPC_END_MESSAGE_MAP() | |
54 return handled; | |
55 } | |
56 | |
57 void ViewerIPCServer::OnChannelConnected(int32 peer_pid) { | |
58 DCHECK(!client_connected_); | |
59 client_connected_ = true; | |
60 } | |
61 | |
62 void ViewerIPCServer::OnChannelError() { | |
63 // When a client (typically a browser process) disconnects, the pipe is | |
64 // closed and we get an OnChannelError. Since we want to keep servicing | |
65 // client requests, we will recreate the channel. | |
66 bool client_was_connected = client_connected_; | |
67 client_connected_ = false; | |
68 if (client_was_connected) { | |
69 if (g_viewer_process->HandleClientDisconnect()) { | |
70 #if defined(OS_WIN) | |
71 // On Windows, once an error on a named pipe occurs, the named pipe is no | |
72 // longer valid and must be re-created. This is not the case on Mac or | |
73 // Linux. | |
74 CreateChannel(); | |
75 #endif | |
76 } | |
77 } else { | |
78 // If the client was never even connected we had an error connecting. | |
79 if (!client_connected_) { | |
80 LOG(ERROR) << "Unable to open viewer ipc channel " | |
81 << "named: " << channel_handle_.name; | |
82 } | |
83 } | |
84 } | |
85 | |
86 void ViewerIPCServer::OnPbufferHandle(const std::string& todo) { | |
87 NOTREACHED(); | |
88 } | |
89 | |
90 void ViewerIPCServer::CreateChannel() { | |
91 channel_.reset(NULL); // Tear down the existing channel, if any. | |
92 channel_.reset(new IPC::SyncChannel(channel_handle_, | |
93 IPC::Channel::MODE_NAMED_SERVER, this, | |
94 g_viewer_process->io_message_loop_proxy(), true, | |
95 g_viewer_process->shutdown_event())); | |
96 DCHECK(sync_message_filter_.get()); | |
97 channel_->AddFilter(sync_message_filter_.get()); | |
98 } | |
OLD | NEW |