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(const IPC::ChannelHandle
& channel_handle
)
14 : channel_handle_(channel_handle
), client_connected_(false) {
17 bool ServiceIPCServer::Init() {
18 #ifdef IPC_MESSAGE_LOG_ENABLED
19 IPC::Logging::GetInstance()->SetIPCSender(this);
21 sync_message_filter_
=
22 new IPC::SyncMessageFilter(g_service_process
->shutdown_event());
27 void ServiceIPCServer::CreateChannel() {
28 channel_
.reset(NULL
); // Tear down the existing channel, if any.
29 channel_
= IPC::SyncChannel::Create(
30 channel_handle_
, IPC::Channel::MODE_NAMED_SERVER
, this,
31 g_service_process
->io_thread()->task_runner().get(), true,
32 g_service_process
->shutdown_event());
33 DCHECK(sync_message_filter_
.get());
34 channel_
->AddFilter(sync_message_filter_
.get());
37 ServiceIPCServer::~ServiceIPCServer() {
38 #ifdef IPC_MESSAGE_LOG_ENABLED
39 IPC::Logging::GetInstance()->SetIPCSender(NULL
);
42 channel_
->RemoveFilter(sync_message_filter_
.get());
45 void ServiceIPCServer::OnChannelConnected(int32 peer_pid
) {
46 DCHECK(!client_connected_
);
47 client_connected_
= true;
50 void ServiceIPCServer::OnChannelError() {
51 // When a client (typically a browser process) disconnects, the pipe is
52 // closed and we get an OnChannelError. Since we want to keep servicing
53 // client requests, we will recreate the channel.
54 bool client_was_connected
= client_connected_
;
55 client_connected_
= false;
56 // TODO(sanjeevr): Instead of invoking the service process for such handlers,
57 // define a Client interface that the ServiceProcess can implement.
58 if (client_was_connected
) {
59 if (g_service_process
->HandleClientDisconnect()) {
61 // On Windows, once an error on a named pipe occurs, the named pipe is no
62 // longer valid and must be re-created. This is not the case on Mac or
68 // If the client was never even connected we had an error connecting.
69 if (!client_connected_
) {
70 LOG(ERROR
) << "Unable to open service ipc channel "
71 << "named: " << channel_handle_
.name
;
76 bool ServiceIPCServer::Send(IPC::Message
* msg
) {
77 if (!channel_
.get()) {
82 return channel_
->Send(msg
);
85 bool ServiceIPCServer::OnMessageReceived(const IPC::Message
& msg
) {
87 // When we get a message, always mark the client as connected. The
88 // ChannelProxy::Context is only letting OnChannelConnected get called once,
89 // so on the Mac and Linux, we never would set client_connected_ to true
90 // again on subsequent connections.
91 client_connected_
= true;
92 IPC_BEGIN_MESSAGE_MAP(ServiceIPCServer
, msg
)
93 IPC_MESSAGE_HANDLER(ServiceMsg_EnableCloudPrintProxyWithRobot
,
94 OnEnableCloudPrintProxyWithRobot
)
95 IPC_MESSAGE_HANDLER(ServiceMsg_DisableCloudPrintProxy
,
96 OnDisableCloudPrintProxy
)
97 IPC_MESSAGE_HANDLER(ServiceMsg_GetCloudPrintProxyInfo
,
98 OnGetCloudPrintProxyInfo
)
99 IPC_MESSAGE_HANDLER(ServiceMsg_GetHistograms
, OnGetHistograms
)
100 IPC_MESSAGE_HANDLER(ServiceMsg_GetPrinters
, OnGetPrinters
)
101 IPC_MESSAGE_HANDLER(ServiceMsg_Shutdown
, OnShutdown
);
102 IPC_MESSAGE_HANDLER(ServiceMsg_UpdateAvailable
, OnUpdateAvailable
);
103 IPC_MESSAGE_UNHANDLED(handled
= false)
104 IPC_END_MESSAGE_MAP()
108 void ServiceIPCServer::OnEnableCloudPrintProxyWithRobot(
109 const std::string
& robot_auth_code
,
110 const std::string
& robot_email
,
111 const std::string
& user_email
,
112 const base::DictionaryValue
& user_settings
) {
113 g_service_process
->GetCloudPrintProxy()->EnableForUserWithRobot(
114 robot_auth_code
, robot_email
, user_email
, user_settings
);
117 void ServiceIPCServer::OnGetCloudPrintProxyInfo() {
118 cloud_print::CloudPrintProxyInfo info
;
119 g_service_process
->GetCloudPrintProxy()->GetProxyInfo(&info
);
120 channel_
->Send(new ServiceHostMsg_CloudPrintProxy_Info(info
));
123 void ServiceIPCServer::OnGetHistograms() {
124 if (!histogram_delta_serializer_
) {
125 histogram_delta_serializer_
.reset(
126 new base::HistogramDeltaSerialization("ServiceProcess"));
128 std::vector
<std::string
> deltas
;
129 histogram_delta_serializer_
->PrepareAndSerializeDeltas(&deltas
);
130 channel_
->Send(new ServiceHostMsg_Histograms(deltas
));
133 void ServiceIPCServer::OnGetPrinters() {
134 std::vector
<std::string
> printers
;
135 g_service_process
->GetCloudPrintProxy()->GetPrinters(&printers
);
136 channel_
->Send(new ServiceHostMsg_Printers(printers
));
139 void ServiceIPCServer::OnDisableCloudPrintProxy() {
140 // User disabled CloudPrint proxy explicitly. Delete printers
141 // registered from this proxy and disable proxy.
142 g_service_process
->GetCloudPrintProxy()->
143 UnregisterPrintersAndDisableForUser();
146 void ServiceIPCServer::OnShutdown() {
147 g_service_process
->Shutdown();
150 void ServiceIPCServer::OnUpdateAvailable() {
151 g_service_process
->SetUpdateAvailable();