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.
5 #include "chrome/service/service_ipc_server.h"
7 #include "base/metrics/histogram_delta_serialization.h"
8 #include "chrome/common/service_messages.h"
9 #include "chrome/service/cloud_print/cloud_print_proxy.h"
10 #include "chrome/service/service_process.h"
11 #include "ipc/ipc_logging.h"
13 ServiceIPCServer::ServiceIPCServer(
15 const scoped_refptr
<base::SingleThreadTaskRunner
>& io_task_runner
,
16 const IPC::ChannelHandle
& channel_handle
,
17 base::WaitableEvent
* shutdown_event
)
19 io_task_runner_(io_task_runner
),
20 channel_handle_(channel_handle
),
21 shutdown_event_(shutdown_event
),
22 ipc_client_connected_(false) {
24 DCHECK(shutdown_event
);
27 bool ServiceIPCServer::Init() {
28 #ifdef IPC_MESSAGE_LOG_ENABLED
29 IPC::Logging::GetInstance()->SetIPCSender(this);
35 void ServiceIPCServer::CreateChannel() {
36 channel_
.reset(); // Tear down the existing channel, if any.
37 channel_
= IPC::SyncChannel::Create(
39 IPC::Channel::MODE_NAMED_SERVER
,
42 true /* create_pipe_now */,
46 ServiceIPCServer::~ServiceIPCServer() {
47 #ifdef IPC_MESSAGE_LOG_ENABLED
48 IPC::Logging::GetInstance()->SetIPCSender(NULL
);
52 void ServiceIPCServer::OnChannelConnected(int32 peer_pid
) {
53 DCHECK(!ipc_client_connected_
);
54 ipc_client_connected_
= true;
57 void ServiceIPCServer::OnChannelError() {
58 // When an IPC client (typically a browser process) disconnects, the pipe is
59 // closed and we get an OnChannelError. If we want to keep servicing requests,
60 // we will recreate the channel if necessary.
61 bool client_was_connected
= ipc_client_connected_
;
62 ipc_client_connected_
= false;
63 if (client_was_connected
) {
64 if (client_
->OnIPCClientDisconnect()) {
66 // On Windows, once an error on a named pipe occurs, the named pipe is no
67 // longer valid and must be re-created. This is not the case on Mac or
73 // If the client was never even connected we had an error connecting.
74 if (!ipc_client_connected_
) {
75 LOG(ERROR
) << "Unable to open service ipc channel "
76 << "named: " << channel_handle_
.name
;
81 bool ServiceIPCServer::Send(IPC::Message
* msg
) {
82 if (!channel_
.get()) {
87 return channel_
->Send(msg
);
90 bool ServiceIPCServer::OnMessageReceived(const IPC::Message
& msg
) {
92 // When we get a message, always mark the IPC client as connected. The
93 // ChannelProxy::Context is only letting OnChannelConnected get called once,
94 // so on Mac and Linux, we never would set ipc_client_connected_ to true
95 // again on subsequent connections.
96 ipc_client_connected_
= true;
97 IPC_BEGIN_MESSAGE_MAP(ServiceIPCServer
, msg
)
98 IPC_MESSAGE_HANDLER(ServiceMsg_EnableCloudPrintProxyWithRobot
,
99 OnEnableCloudPrintProxyWithRobot
)
100 IPC_MESSAGE_HANDLER(ServiceMsg_DisableCloudPrintProxy
,
101 OnDisableCloudPrintProxy
)
102 IPC_MESSAGE_HANDLER(ServiceMsg_GetCloudPrintProxyInfo
,
103 OnGetCloudPrintProxyInfo
)
104 IPC_MESSAGE_HANDLER(ServiceMsg_GetHistograms
, OnGetHistograms
)
105 IPC_MESSAGE_HANDLER(ServiceMsg_GetPrinters
, OnGetPrinters
)
106 IPC_MESSAGE_HANDLER(ServiceMsg_Shutdown
, OnShutdown
);
107 IPC_MESSAGE_HANDLER(ServiceMsg_UpdateAvailable
, OnUpdateAvailable
);
108 IPC_MESSAGE_UNHANDLED(handled
= false)
109 IPC_END_MESSAGE_MAP()
113 void ServiceIPCServer::OnEnableCloudPrintProxyWithRobot(
114 const std::string
& robot_auth_code
,
115 const std::string
& robot_email
,
116 const std::string
& user_email
,
117 const base::DictionaryValue
& user_settings
) {
118 g_service_process
->GetCloudPrintProxy()->EnableForUserWithRobot(
119 robot_auth_code
, robot_email
, user_email
, user_settings
);
122 void ServiceIPCServer::OnGetCloudPrintProxyInfo() {
123 cloud_print::CloudPrintProxyInfo info
;
124 g_service_process
->GetCloudPrintProxy()->GetProxyInfo(&info
);
125 channel_
->Send(new ServiceHostMsg_CloudPrintProxy_Info(info
));
128 void ServiceIPCServer::OnGetHistograms() {
129 if (!histogram_delta_serializer_
) {
130 histogram_delta_serializer_
.reset(
131 new base::HistogramDeltaSerialization("ServiceProcess"));
133 std::vector
<std::string
> deltas
;
134 histogram_delta_serializer_
->PrepareAndSerializeDeltas(&deltas
);
135 channel_
->Send(new ServiceHostMsg_Histograms(deltas
));
138 void ServiceIPCServer::OnGetPrinters() {
139 std::vector
<std::string
> printers
;
140 g_service_process
->GetCloudPrintProxy()->GetPrinters(&printers
);
141 channel_
->Send(new ServiceHostMsg_Printers(printers
));
144 void ServiceIPCServer::OnDisableCloudPrintProxy() {
145 // User disabled CloudPrint proxy explicitly. Delete printers
146 // registered from this proxy and disable proxy.
147 g_service_process
->GetCloudPrintProxy()->
148 UnregisterPrintersAndDisableForUser();
151 void ServiceIPCServer::OnShutdown() {
152 client_
->OnShutdown();
155 void ServiceIPCServer::OnUpdateAvailable() {
156 client_
->OnUpdateAvailable();