Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / chrome / service / service_ipc_server.cc
blob59a59081986c145401d684ebd8fdeb4f2829b83a
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(
14 Client* client,
15 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner,
16 const IPC::ChannelHandle& channel_handle,
17 base::WaitableEvent* shutdown_event)
18 : client_(client),
19 io_task_runner_(io_task_runner),
20 channel_handle_(channel_handle),
21 shutdown_event_(shutdown_event),
22 ipc_client_connected_(false) {
23 DCHECK(client);
24 DCHECK(shutdown_event);
27 bool ServiceIPCServer::Init() {
28 #ifdef IPC_MESSAGE_LOG_ENABLED
29 IPC::Logging::GetInstance()->SetIPCSender(this);
30 #endif
31 CreateChannel();
32 return true;
35 void ServiceIPCServer::CreateChannel() {
36 channel_.reset(); // Tear down the existing channel, if any.
37 channel_ = IPC::SyncChannel::Create(
38 channel_handle_,
39 IPC::Channel::MODE_NAMED_SERVER,
40 this /* listener */,
41 io_task_runner_,
42 true /* create_pipe_now */,
43 shutdown_event_);
46 ServiceIPCServer::~ServiceIPCServer() {
47 #ifdef IPC_MESSAGE_LOG_ENABLED
48 IPC::Logging::GetInstance()->SetIPCSender(NULL);
49 #endif
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()) {
65 #if defined(OS_WIN)
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
68 // Linux.
69 CreateChannel();
70 #endif
72 } else {
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()) {
83 delete msg;
84 return false;
87 return channel_->Send(msg);
90 bool ServiceIPCServer::OnMessageReceived(const IPC::Message& msg) {
91 bool handled = true;
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()
110 return handled;
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();