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.
4 
5 #ifndef CHROME_SERVICE_SERVICE_IPC_SERVER_H_
6 #define CHROME_SERVICE_SERVICE_IPC_SERVER_H_
7 
8 #include <stdint.h>
9 
10 #include <memory>
11 #include <vector>
12 
13 #include "base/macros.h"
14 #include "base/single_thread_task_runner.h"
15 #include "chrome/common/service_process.mojom.h"
16 #include "mojo/public/cpp/bindings/pending_receiver.h"
17 #include "mojo/public/cpp/bindings/receiver.h"
18 #include "mojo/public/cpp/bindings/receiver_set.h"
19 #include "mojo/public/cpp/system/message_pipe.h"
20 #include "services/service_manager/public/cpp/binder_registry.h"
21 #include "services/service_manager/public/mojom/interface_provider.mojom.h"
22 
23 namespace base {
24 
25 class HistogramDeltaSerialization;
26 class WaitableEvent;
27 
28 }  // namespace base
29 
30 // This class handles IPC commands for the service process.
31 class ServiceIPCServer : public service_manager::mojom::InterfaceProvider,
32                          public chrome::mojom::ServiceProcess {
33  public:
34   class Client {
35    public:
~Client()36     virtual ~Client() {}
37 
38     // Called when the service process must shut down.
39     virtual void OnShutdown() = 0;
40 
41     // Called when a product update is available.
42     virtual void OnUpdateAvailable() = 0;
43 
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;
47 
48     // Called to create a message pipe to use for an IPC Channel connection.
49     virtual mojo::ScopedMessagePipeHandle CreateChannelMessagePipe() = 0;
50   };
51 
52   ServiceIPCServer(
53       Client* client,
54       const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner,
55       base::WaitableEvent* shutdown_event);
56   ~ServiceIPCServer() override;
57 
58   bool Init();
59 
binder_registry()60   service_manager::BinderRegistry& binder_registry() {
61     return binder_registry_;
62   }
63 
is_ipc_client_connected()64   bool is_ipc_client_connected() const { return ipc_client_connected_; }
65 
66  private:
67   friend class ServiceIPCServerTest;
68   friend class MockServiceIPCServer;
69 
70   // Helper method to create the sync channel.
71   void CreateChannel();
72 
73   void OnChannelError();
74 
75   // chrome::mojom::ServiceProcess:
76   void Hello(HelloCallback callback) override;
77   void GetHistograms(GetHistogramsCallback callback) override;
78   void UpdateAvailable() override;
79   void ShutDown() override;
80 
81   // service_manager::mojom::InterfaceProvider:
82   void GetInterface(const std::string& interface_name,
83                     mojo::ScopedMessagePipeHandle pipe) override;
84 
85   void HandleServiceProcessConnection(
86       mojo::PendingReceiver<chrome::mojom::ServiceProcess> receiver);
87 
88   Client* client_;
89   scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_;
90   base::WaitableEvent* shutdown_event_;
91 
92   // Indicates whether an IPC client is currently connected to the channel.
93   bool ipc_client_connected_ = false;
94 
95   // Calculates histograms deltas.
96   std::unique_ptr<base::HistogramDeltaSerialization>
97       histogram_delta_serializer_;
98 
99   mojo::Receiver<service_manager::mojom::InterfaceProvider> receiver_{this};
100   mojo::ReceiverSet<chrome::mojom::ServiceProcess> service_process_receivers_;
101 
102   service_manager::BinderRegistry binder_registry_;
103 
104   DISALLOW_COPY_AND_ASSIGN(ServiceIPCServer);
105 };
106 
107 #endif  // CHROME_SERVICE_SERVICE_IPC_SERVER_H_
108