OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2015 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 COMPONENTS_ARC_ARC_BRIDGE_SERVICE_H_ | |
6 #define COMPONENTS_ARC_ARC_BRIDGE_SERVICE_H_ | |
7 | |
8 #include "base/macros.h" | |
9 #include "base/observer_list.h" | |
10 #include "base/sequenced_task_runner.h" | |
11 #include "ipc/ipc_channel_proxy.h" | |
12 #include "ipc/ipc_listener.h" | |
13 #include "ipc/ipc_message.h" | |
14 | |
15 namespace arc { | |
16 | |
17 class ArcBridgeTest; | |
jochen (gone - plz use gerrit)
2015/11/12 23:40:59
should not be required
Luis Héctor Chávez
2015/11/13 00:29:29
Done.
| |
18 | |
19 // The Chrome-side service that handles ARC instances and ARC bridge creation. | |
20 // This service handles the lifetime of ARC instances and sets up the | |
21 // communication channel (the ARC bridge) used to send and receive messages. | |
22 class ArcBridgeService : public IPC::Listener { | |
23 public: | |
24 // The possible states of the bridge. In the normal flow, the state changes | |
25 // in the following sequence: | |
26 // | |
27 // STOPPED | |
28 // SetAvailable(true) + HandleStartup() -> SocketConnect() -> | |
29 // CONNECTING | |
30 // Connect() -> | |
31 // CONNECTED | |
32 // SocketConnectAfterSetSocketPermissions() -> | |
33 // STARTING | |
34 // StartInstance() -> OnInstanceReady() -> | |
35 // READY | |
36 // | |
37 // When Shutdown() is called, the state changes depending on the state it was | |
38 // at: | |
39 // | |
40 // CONNECTED/CONNECTING -> STOPPED | |
41 // STARTING/READY -> STOPPING -> StopInstance() -> STOPPED | |
42 enum class State { | |
43 // ARC is not currently running. | |
44 STOPPED, | |
45 | |
46 // The request to connect has been sent. | |
47 CONNECTING, | |
48 | |
49 // The bridge has connected to the socket, but has not started the ARC | |
50 // instance. | |
51 CONNECTED, | |
52 | |
53 // The ARC bridge has been set up and ARC is starting up. | |
54 STARTING, | |
55 | |
56 // The ARC instance has been fully initialized and is now ready for user | |
57 // interaction. | |
58 READY, | |
59 | |
60 // The ARC instance has started shutting down. | |
61 STOPPING, | |
62 }; | |
63 | |
64 class Observer { | |
65 public: | |
66 // Called whenever the state of the bridge has changed. | |
67 virtual void OnStateChanged(State state) {} | |
68 | |
69 // Called whenever ARC's availability has changed for this system. | |
70 virtual void OnAvailableChanged(bool available) {} | |
71 | |
72 protected: | |
73 virtual ~Observer() {} | |
74 }; | |
75 | |
76 ArcBridgeService( | |
77 const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner, | |
78 const scoped_refptr<base::SequencedTaskRunner>& file_task_runner); | |
79 ~ArcBridgeService() override; | |
80 | |
81 // Gets the global instance of the ARC Bridge Service. | |
jochen (gone - plz use gerrit)
2015/11/12 23:40:59
A better comment would point out that you can only
Luis Héctor Chávez
2015/11/13 00:29:29
Done.
| |
82 static ArcBridgeService* Get(); | |
83 | |
84 // DetectAvailability() should be called once D-Bus is available. It will | |
85 // call CheckArcAvailability() on the session_manager. | |
86 void DetectAvailability(); | |
87 | |
88 // HandleStartup() should be called upon profile startup. This will only | |
89 // launch an instance if the instance service is available and it is enabled. | |
90 void HandleStartup(); | |
91 | |
92 // Shutdown() should be called when the browser is shutting down. | |
93 void Shutdown(); | |
94 | |
95 // Adds or removes observers. | |
96 void AddObserver(Observer* observer); | |
97 void RemoveObserver(Observer* observer); | |
98 | |
99 // Gets the current state of the bridge service. | |
100 State state() const { return state_; } | |
101 | |
102 // Gets if ARC is available in this system. | |
103 bool available() const { return available_; } | |
104 | |
105 // Requests registration of an input device on the ARC instance. | |
106 // TODO(denniskempin): Make this interface more typesafe. | |
107 // |name| should be the displayable name of the emulated device (e.g. "Chrome | |
108 // OS Keyboard"), |device_type| the name of the device type (e.g. "keyboard") | |
109 // and |fd| a file descriptor that emulates the kernel events of the device. | |
110 bool RegisterInputDevice(const std::string& name, | |
111 const std::string& device_type, | |
112 base::ScopedFD fd); | |
113 | |
114 private: | |
115 friend class arc::ArcBridgeTest; | |
116 FRIEND_TEST_ALL_PREFIXES(ArcBridgeTest, Basic); | |
117 FRIEND_TEST_ALL_PREFIXES(ArcBridgeTest, ShutdownWhenDisabled); | |
118 | |
119 // If all pre-requisites are true (ARC is available, it has been enabled, and | |
120 // the session has started), and ARC is stopped, start ARC. If ARC is running | |
121 // and the pre-requisites stop being true, stop ARC. | |
122 void PrerequisitesChanged(); | |
123 | |
124 // Binds to the socket specified by |socket_path|. | |
125 void SocketConnect(const base::FilePath& socket_path); | |
126 | |
127 // Binds to the socket specified by |socket_path| after creating its parent | |
128 // directory is present. | |
129 void SocketConnectAfterEnsureParentDirectory( | |
130 const base::FilePath& socket_path, | |
131 bool directory_present); | |
132 | |
133 // Internal connection method. Separated to make testing easier. | |
134 bool Connect(const IPC::ChannelHandle& handle, IPC::Channel::Mode mode); | |
135 | |
136 // Finishes connecting after setting socket permissions. | |
137 void SocketConnectAfterSetSocketPermissions(const base::FilePath& socket_path, | |
138 bool socket_permissions_success); | |
139 | |
140 // Stops the running instance. | |
141 void StopInstance(); | |
142 | |
143 // Called when the ARC instance has finished setting up and is ready for user | |
144 // interaction. | |
145 void OnInstanceReady(); | |
146 | |
147 // Changes the current state and notifies all observers. | |
148 void SetState(State state); | |
149 | |
150 // IPC::Listener: | |
151 bool OnMessageReceived(const IPC::Message& message) override; | |
152 | |
153 // DBus callbacks. | |
154 void OnArcAvailable(bool available); | |
155 void OnInstanceStarted(bool success); | |
156 void OnInstanceStopped(bool success); | |
157 | |
158 // Task runner on which incoming messages are handled. | |
159 scoped_refptr<base::SequencedTaskRunner> origin_task_runner_; | |
160 | |
161 // Task runner on which ipc operations are performed. | |
162 scoped_refptr<base::SingleThreadTaskRunner> ipc_task_runner_; | |
163 | |
164 // Task runner on which file operations are performed. | |
jochen (gone - plz use gerrit)
2015/11/12 23:40:59
i'd encourage you to remove comments that just res
Luis Héctor Chávez
2015/11/13 00:29:29
Done.
| |
165 scoped_refptr<base::SequencedTaskRunner> file_task_runner_; | |
166 | |
167 // The channel through which messages are sent. | |
168 scoped_ptr<IPC::ChannelProxy> ipc_channel_; | |
169 | |
170 // List of observers to notify of state changes. | |
171 base::ObserverList<Observer> observer_list_; | |
172 | |
173 // If the user's session has started. | |
174 bool session_started_; | |
175 | |
176 // If the ARC instance service is available. | |
177 bool available_; | |
178 | |
179 // The current state of the bridge. | |
180 ArcBridgeService::State state_; | |
181 | |
182 // WeakPtrFactory to use callbacks. | |
183 base::WeakPtrFactory<ArcBridgeService> weak_factory_; | |
184 | |
185 DISALLOW_COPY_AND_ASSIGN(ArcBridgeService); | |
186 }; | |
187 | |
188 } // namespace arc | |
189 | |
190 #endif // COMPONENTS_ARC_ARC_BRIDGE_SERVICE_H_ | |
OLD | NEW |