Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / service / service_ipc_server.h
blob4ad4b7282951e984cead91600415d2e9333dd17e
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 #ifndef CHROME_SERVICE_SERVICE_IPC_SERVER_H_
6 #define CHROME_SERVICE_SERVICE_IPC_SERVER_H_
8 #include <vector>
10 #include "base/memory/scoped_ptr.h"
11 #include "base/memory/scoped_vector.h"
12 #include "ipc/ipc_channel_handle.h"
13 #include "ipc/ipc_listener.h"
14 #include "ipc/ipc_sync_channel.h"
15 #include "ipc/ipc_sender.h"
17 namespace base {
19 class HistogramDeltaSerialization;
20 class WaitableEvent;
22 } // namespace base
24 // This class handles IPC commands for the service process.
25 class ServiceIPCServer : public IPC::Listener, public IPC::Sender {
26 public:
27 class MessageHandler {
28 public:
29 virtual ~MessageHandler() {}
30 // Must return true if the message is handled.
31 virtual bool HandleMessage(const IPC::Message& message) = 0;
34 class Client {
35 public:
36 virtual ~Client() {}
38 // Called when the service process must shut down.
39 virtual void OnShutdown() = 0;
41 // Called when a product update is available.
42 virtual void OnUpdateAvailable() = 0;
44 // Called when the IPC channel is closed. A return value of true indicates
45 // that the IPC server should continue listening for new connections.
46 virtual bool OnIPCClientDisconnect() = 0;
49 ServiceIPCServer(
50 Client* client,
51 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner,
52 const IPC::ChannelHandle& handle,
53 base::WaitableEvent* shutdown_event);
54 ~ServiceIPCServer() override;
56 bool Init();
58 // IPC::Sender implementation.
59 bool Send(IPC::Message* msg) override;
61 // Registers a MessageHandler with the ServiceIPCServer. When an IPC message
62 // is received that is not handled by the ServiceIPCServer itself, the
63 // handlers will be called to handle the message in first-add first-call order
64 // until it is handled or there are no more handlers.
65 void AddMessageHandler(scoped_ptr<MessageHandler> handler);
67 bool is_ipc_client_connected() const { return ipc_client_connected_; }
69 private:
70 friend class ServiceIPCServerTest;
71 friend class MockServiceIPCServer;
73 // IPC::Listener implementation.
74 bool OnMessageReceived(const IPC::Message& msg) override;
75 void OnChannelConnected(int32 peer_pid) override;
76 void OnChannelError() override;
78 // IPC message handlers.
79 void OnGetHistograms();
80 void OnShutdown();
81 void OnUpdateAvailable();
83 // Helper method to create the sync channel.
84 void CreateChannel();
86 Client* client_;
87 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_;
88 IPC::ChannelHandle channel_handle_;
89 scoped_ptr<IPC::SyncChannel> channel_;
90 base::WaitableEvent* shutdown_event_;
91 ScopedVector<MessageHandler> message_handlers_;
93 // Indicates whether an IPC client is currently connected to the channel.
94 bool ipc_client_connected_;
96 // Calculates histograms deltas.
97 scoped_ptr<base::HistogramDeltaSerialization> histogram_delta_serializer_;
99 DISALLOW_COPY_AND_ASSIGN(ServiceIPCServer);
102 #endif // CHROME_SERVICE_SERVICE_IPC_SERVER_H_