summaryrefslogtreecommitdiffstats
path: root/chrome/service/service_ipc_server.cc
blob: de82ad09237f4903ef0d874275260fd749e42e7d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/service/service_ipc_server.h"

#include "base/metrics/histogram_delta_serialization.h"
#include "build/build_config.h"
#include "chrome/common/service_messages.h"
#include "ipc/ipc_logging.h"

ServiceIPCServer::ServiceIPCServer(
    Client* client,
    const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner,
    const IPC::ChannelHandle& channel_handle,
    base::WaitableEvent* shutdown_event)
    : client_(client),
      io_task_runner_(io_task_runner),
      channel_handle_(channel_handle),
      shutdown_event_(shutdown_event),
      ipc_client_connected_(false) {
  DCHECK(client);
  DCHECK(shutdown_event);
}

bool ServiceIPCServer::Init() {
#ifdef IPC_MESSAGE_LOG_ENABLED
  IPC::Logging::GetInstance()->SetIPCSender(this);
#endif
  CreateChannel();
  return true;
}

void ServiceIPCServer::CreateChannel() {
  channel_.reset();  // Tear down the existing channel, if any.
  channel_ = IPC::SyncChannel::Create(
      channel_handle_,
      IPC::Channel::MODE_NAMED_SERVER,
      this /* listener */,
      io_task_runner_,
      true /* create_pipe_now */,
      shutdown_event_);
}

ServiceIPCServer::~ServiceIPCServer() {
#ifdef IPC_MESSAGE_LOG_ENABLED
  IPC::Logging::GetInstance()->SetIPCSender(NULL);
#endif
}

void ServiceIPCServer::OnChannelConnected(int32_t peer_pid) {
  DCHECK(!ipc_client_connected_);
  ipc_client_connected_ = true;
}

void ServiceIPCServer::OnChannelError() {
  // When an IPC client (typically a browser process) disconnects, the pipe is
  // closed and we get an OnChannelError. If we want to keep servicing requests,
  // we will recreate the channel if necessary.
  bool client_was_connected = ipc_client_connected_;
  ipc_client_connected_ = false;
  if (client_was_connected) {
    if (client_->OnIPCClientDisconnect()) {
#if defined(OS_WIN)
      // On Windows, once an error on a named pipe occurs, the named pipe is no
      // longer valid and must be re-created. This is not the case on Mac or
      // Linux.
      CreateChannel();
#endif
    }
  } else {
    // If the client was never even connected we had an error connecting.
    if (!ipc_client_connected_) {
      LOG(ERROR) << "Unable to open service ipc channel "
                 << "named: " << channel_handle_.name;
    }
  }
}

bool ServiceIPCServer::Send(IPC::Message* msg) {
  if (!channel_.get()) {
    delete msg;
    return false;
  }

  return channel_->Send(msg);
}

void ServiceIPCServer::AddMessageHandler(scoped_ptr<MessageHandler> handler) {
  message_handlers_.push_back(handler.release());
}

bool ServiceIPCServer::OnMessageReceived(const IPC::Message& msg) {
  bool handled = true;
  // When we get a message, always mark the IPC client as connected. The
  // ChannelProxy::Context is only letting OnChannelConnected get called once,
  // so on Mac and Linux, we never would set ipc_client_connected_ to true
  // again on subsequent connections.
  ipc_client_connected_ = true;
  IPC_BEGIN_MESSAGE_MAP(ServiceIPCServer, msg)
    IPC_MESSAGE_HANDLER(ServiceMsg_GetHistograms, OnGetHistograms)
    IPC_MESSAGE_HANDLER(ServiceMsg_Shutdown, OnShutdown);
    IPC_MESSAGE_HANDLER(ServiceMsg_UpdateAvailable, OnUpdateAvailable);
    IPC_MESSAGE_UNHANDLED(handled = false)
  IPC_END_MESSAGE_MAP()

  if (!handled) {
    // Make a copy of the handlers to prevent modification during iteration.
    std::vector<MessageHandler*> temp_handlers = message_handlers_.get();
    for (const auto& handler : temp_handlers) {
      handled = handler->HandleMessage(msg);
      if (handled)
        break;
    }
  }

  return handled;
}

void ServiceIPCServer::OnGetHistograms() {
  if (!histogram_delta_serializer_) {
    histogram_delta_serializer_.reset(
        new base::HistogramDeltaSerialization("ServiceProcess"));
  }
  std::vector<std::string> deltas;
  // "false" to PerpareAndSerializeDeltas() indicates to *not* include
  // histograms held in persistent storage on the assumption that they will be
  // visible to the recipient through other means.
  histogram_delta_serializer_->PrepareAndSerializeDeltas(&deltas, false);
  channel_->Send(new ServiceHostMsg_Histograms(deltas));
}

void ServiceIPCServer::OnShutdown() {
  client_->OnShutdown();
}

void ServiceIPCServer::OnUpdateAvailable() {
  client_->OnUpdateAvailable();
}