OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_SERVICE_SERVICE_IPC_SERVER_H_ | 5 #ifndef CHROME_SERVICE_SERVICE_IPC_SERVER_H_ |
6 #define CHROME_SERVICE_SERVICE_IPC_SERVER_H_ | 6 #define CHROME_SERVICE_SERVICE_IPC_SERVER_H_ |
7 | 7 |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <memory> | 10 #include <memory> |
11 #include <vector> | 11 #include <vector> |
12 | 12 |
13 #include "base/macros.h" | 13 #include "base/macros.h" |
14 #include "base/memory/scoped_vector.h" | 14 #include "base/memory/scoped_vector.h" |
| 15 #include "base/single_thread_task_runner.h" |
15 #include "ipc/ipc_channel_handle.h" | 16 #include "ipc/ipc_channel_handle.h" |
16 #include "ipc/ipc_listener.h" | 17 #include "ipc/ipc_listener.h" |
17 #include "ipc/ipc_sender.h" | 18 #include "ipc/ipc_sender.h" |
18 #include "ipc/ipc_sync_channel.h" | 19 #include "ipc/ipc_sync_channel.h" |
| 20 #include "mojo/public/cpp/system/message_pipe.h" |
19 | 21 |
20 namespace base { | 22 namespace base { |
21 | 23 |
22 class HistogramDeltaSerialization; | 24 class HistogramDeltaSerialization; |
23 class WaitableEvent; | 25 class WaitableEvent; |
24 | 26 |
25 } // namespace base | 27 } // namespace base |
26 | 28 |
27 // This class handles IPC commands for the service process. | 29 // This class handles IPC commands for the service process. |
28 class ServiceIPCServer : public IPC::Listener, public IPC::Sender { | 30 class ServiceIPCServer : public IPC::Listener, public IPC::Sender { |
29 public: | 31 public: |
30 class MessageHandler { | 32 class MessageHandler { |
31 public: | 33 public: |
32 virtual ~MessageHandler() {} | 34 virtual ~MessageHandler() {} |
33 // Must return true if the message is handled. | 35 // Must return true if the message is handled. |
34 virtual bool HandleMessage(const IPC::Message& message) = 0; | 36 virtual bool HandleMessage(const IPC::Message& message) = 0; |
35 }; | 37 }; |
36 | 38 |
37 class Client { | 39 class Client { |
38 public: | 40 public: |
39 virtual ~Client() {} | 41 virtual ~Client() {} |
40 | 42 |
41 // Called when the service process must shut down. | 43 // Called when the service process must shut down. |
42 virtual void OnShutdown() = 0; | 44 virtual void OnShutdown() = 0; |
43 | 45 |
44 // Called when a product update is available. | 46 // Called when a product update is available. |
45 virtual void OnUpdateAvailable() = 0; | 47 virtual void OnUpdateAvailable() = 0; |
46 | 48 |
| 49 // Called when the service process should remain running after being |
| 50 // disconnected for testing. |
| 51 virtual void OnStayAliveForTesting() = 0; |
| 52 |
47 // Called when the IPC channel is closed. A return value of true indicates | 53 // Called when the IPC channel is closed. A return value of true indicates |
48 // that the IPC server should continue listening for new connections. | 54 // that the IPC server should continue listening for new connections. |
49 virtual bool OnIPCClientDisconnect() = 0; | 55 virtual bool OnIPCClientDisconnect() = 0; |
| 56 |
| 57 // Called to create a message pipe to use for an IPC Channel connection. |
| 58 virtual mojo::ScopedMessagePipeHandle CreateChannelMessagePipe() = 0; |
50 }; | 59 }; |
51 | 60 |
52 ServiceIPCServer( | 61 ServiceIPCServer( |
53 Client* client, | 62 Client* client, |
54 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner, | 63 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner, |
55 const IPC::ChannelHandle& handle, | |
56 base::WaitableEvent* shutdown_event); | 64 base::WaitableEvent* shutdown_event); |
57 ~ServiceIPCServer() override; | 65 ~ServiceIPCServer() override; |
58 | 66 |
59 bool Init(); | 67 bool Init(); |
60 | 68 |
61 // IPC::Sender implementation. | 69 // IPC::Sender implementation. |
62 bool Send(IPC::Message* msg) override; | 70 bool Send(IPC::Message* msg) override; |
63 | 71 |
64 // Registers a MessageHandler with the ServiceIPCServer. When an IPC message | 72 // Registers a MessageHandler with the ServiceIPCServer. When an IPC message |
65 // is received that is not handled by the ServiceIPCServer itself, the | 73 // is received that is not handled by the ServiceIPCServer itself, the |
66 // handlers will be called to handle the message in first-add first-call order | 74 // handlers will be called to handle the message in first-add first-call order |
67 // until it is handled or there are no more handlers. | 75 // until it is handled or there are no more handlers. |
68 void AddMessageHandler(std::unique_ptr<MessageHandler> handler); | 76 void AddMessageHandler(std::unique_ptr<MessageHandler> handler); |
69 | 77 |
70 bool is_ipc_client_connected() const { return ipc_client_connected_; } | 78 bool is_ipc_client_connected() const { return ipc_client_connected_; } |
71 | 79 |
72 private: | 80 private: |
73 friend class ServiceIPCServerTest; | 81 friend class ServiceIPCServerTest; |
74 friend class MockServiceIPCServer; | 82 friend class MockServiceIPCServer; |
75 | 83 |
76 // IPC::Listener implementation. | 84 // IPC::Listener implementation. |
77 bool OnMessageReceived(const IPC::Message& msg) override; | 85 bool OnMessageReceived(const IPC::Message& msg) override; |
78 void OnChannelConnected(int32_t peer_pid) override; | 86 void OnChannelConnected(int32_t peer_pid) override; |
79 void OnChannelError() override; | 87 void OnChannelError() override; |
80 | 88 |
81 // IPC message handlers. | 89 // IPC message handlers. |
82 void OnGetHistograms(); | 90 void OnGetHistograms(); |
83 void OnShutdown(); | 91 void OnShutdown(); |
84 void OnUpdateAvailable(); | 92 void OnUpdateAvailable(); |
| 93 void OnStayAliveForTesting(); |
85 | 94 |
86 // Helper method to create the sync channel. | 95 // Helper method to create the sync channel. |
87 void CreateChannel(); | 96 void CreateChannel(); |
88 | 97 |
89 Client* client_; | 98 Client* client_; |
90 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_; | 99 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_; |
91 IPC::ChannelHandle channel_handle_; | |
92 std::unique_ptr<IPC::SyncChannel> channel_; | 100 std::unique_ptr<IPC::SyncChannel> channel_; |
93 base::WaitableEvent* shutdown_event_; | 101 base::WaitableEvent* shutdown_event_; |
94 ScopedVector<MessageHandler> message_handlers_; | 102 ScopedVector<MessageHandler> message_handlers_; |
95 | 103 |
96 // Indicates whether an IPC client is currently connected to the channel. | 104 // Indicates whether an IPC client is currently connected to the channel. |
97 bool ipc_client_connected_; | 105 bool ipc_client_connected_ = false; |
98 | 106 |
99 // Calculates histograms deltas. | 107 // Calculates histograms deltas. |
100 std::unique_ptr<base::HistogramDeltaSerialization> | 108 std::unique_ptr<base::HistogramDeltaSerialization> |
101 histogram_delta_serializer_; | 109 histogram_delta_serializer_; |
102 | 110 |
103 DISALLOW_COPY_AND_ASSIGN(ServiceIPCServer); | 111 DISALLOW_COPY_AND_ASSIGN(ServiceIPCServer); |
104 }; | 112 }; |
105 | 113 |
106 #endif // CHROME_SERVICE_SERVICE_IPC_SERVER_H_ | 114 #endif // CHROME_SERVICE_SERVICE_IPC_SERVER_H_ |
OLD | NEW |