Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1142)

Side by Side Diff: components/arc/arc_bridge_service.h

Issue 1412863004: arc-bridge: Add the ARC Bridge Service (Closed) Base URL: https://chromium.googlesource.com/a/chromium/src.git@master
Patch Set: Improved thread-safety Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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_threadsafe.h"
10 #include "base/sequenced_task_runner.h"
11 #include "chromeos/dbus/dbus_method_call_status.h"
12 #include "components/keyed_service/core/keyed_service.h"
13 #include "ipc/ipc_channel_proxy.h"
14 #include "ipc/ipc_listener.h"
15 #include "ipc/ipc_message.h"
16
17 class PrefRegistrySimple;
18 class PrefService;
19
20 namespace arc {
21
22 class ArcBridgeTest;
23
24 // The Chrome-side service that handles ARC instances and ARC bridge creation.
25 // This service handles the lifetime of ARC instances and sets up the
26 // communication channel (the ARC bridge) used to send and receive messages.
27 class ArcBridgeService : public KeyedService,
28 public IPC::Listener {
29 public:
30 // The possible states of the bridge. In the normal flow, the state changes
31 // in the following sequence:
32 //
33 // STOPPED
34 // SetEnabled(true) + HandleStartup() -> SocketConnect() ->
35 // CONNECTING
36 // Connect() ->
37 // CONNECTED
38 // SocketConnectAfterSetsocketPermissions() ->
39 // STARTING
40 // StartInstance() -> OnInstanceReady() ->
41 // READY
42 //
43 // When Shutdown() is called, the state changes depending on the state it was at:
oshima 2015/11/02 19:13:49 80 chars. You may want to add presubmit check for
Luis Héctor Chávez 2015/11/04 17:49:18 Done.
44 //
45 // CONNECTED/CONNECTING -> STOPPED
46 // STARTING/READY -> STOPPING -> StopInstance() -> STOPPED
47 enum State {
48 // ARC is not currently running.
49 STOPPED,
50
51 // The request to connect has been sent.
52 CONNECTING,
53
54 // The bridge has connected to the socket, but has not started the ARC
55 // instance.
56 CONNECTED,
57
58 // The ARC bridge has been set up and ARC is starting up.
59 STARTING,
60
61 // The ARC instance has been fully initialized and is now ready for user
62 // interaction.
63 READY,
64
65 // The ARC instance has started shutting down.
66 STOPPING,
67 };
68
69 class Observer {
70 public:
71 // Called whenever the state of the bridge has changed.
72 virtual void OnStateChanged(State state) {}
73
74 // Called whenever ARC's enabled status has changed for this session.
75 virtual void OnEnabledChanged(bool enabled) {}
76
77 protected:
78 virtual ~Observer() {}
79 };
80
81 explicit ArcBridgeService(
82 const scoped_refptr<base::SequencedTaskRunner>& file_task_runner);
83 virtual ~ArcBridgeService();
84
85 // Registers ARC preferences.
86 static void RegisterPrefs(PrefRegistrySimple* registry);
87
88 // Reads from the preference store if ARC has been enabled.
89 static bool GetEnabledPref(PrefService* pref_service);
90
91 // HandleStartup() should be called upon profile startup. This will only
92 // launch an instance if the instance service is available and it is enabled.
93 void HandleStartup();
94
95 // Shuts down the running instance, if any. This is safe to call even if
96 // there is no instance running.
97 void Shutdown();
98
99 // Adds or removes observers.
100 void AddObserver(Observer* observer);
101 void RemoveObserver(Observer* observer);
102
103 // Gets the current state of the bridge service.
104 State GetState() const { return state_; }
105
106 // Enables or disables the bridge for this session.
107 void SetEnabled(bool enabled);
108
109 // Gets if the bridge is enabled for this session.
110 bool IsEnabled() const { return enabled_; }
111
112 // Requests registration of an input device on the ARC instance.
113 // TODO(denniskempin): Make this interface more typesafe.
114 // |name| should be the displayable name of the emulated device (e.g. "Chrome
115 // OS Keyboard"), |device_type| the name of the device type (e.g. "keyboard")
116 // and |fd| a file descriptor that emulates the kernel events of the device.
117 bool RegisterInputDevice(const std::string& name,
118 const std::string& device_type,
119 base::ScopedFD fd);
120
121 private:
122 friend class arc::ArcBridgeTest;
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, bool directory_present);
131
132 // Internal connection method. Separated to make testing easier.
133 bool Connect(const IPC::ChannelHandle& handle, IPC::Channel::Mode mode);
134
135 // Finishes connecting after setting socket permissions.
136 void SocketConnectAfterSetSocketPermissions(const base::FilePath& socket_path,
137 bool socket_permissions_success);
138
139 // Called when the ARC instance has finished setting up and is ready for user
140 // interaction.
141 void OnInstanceReady();
142
143 // Changes the current state and notify all observers.
144 void SetState(State state);
145
146 // IPC::Listener:
147 bool OnMessageReceived(const IPC::Message& message) override;
148
149 // DBus callbacks.
150 void OnInstanceStarted(chromeos::DBusMethodCallStatus status);
151 void OnInstanceStopped(chromeos::DBusMethodCallStatus status);
152
153 // Thread in which IPC messaging is performed.
154 base::Thread ipc_thread_;
155
156 // Task runner on which incoming messages are handled.
157 scoped_refptr<base::SequencedTaskRunner> origin_task_runner_;
158
159 // Task runner on which file operations are performed.
160 scoped_refptr<base::SequencedTaskRunner> file_task_runner_;
161
162 // The channel through which messages are sent.
163 scoped_ptr<IPC::ChannelProxy> ipc_channel_;
164
165 // A Lock that guards the |ipc_channel_|. Most of the methods are expected
166 // to be run in the same thread (the one in which the ArcBridgeService
167 // instance was created), but all the ones that send IPC messages can be
168 // called from any method. This will prevent the |ipc_channel_| from being
169 // destroyed while a message is being sent.
170 base::Lock ipc_channel_lock_;
171
172 // List of observers to notify of state changes.
173 scoped_refptr<base::ObserverListThreadSafe<Observer>> observer_list_;
174
175 // If the ARC instance service is available.
176 bool available_;
177
178 // If ARC has been enabled by policy and user choice.
179 bool enabled_;
180
181 // The current state of the bridge.
182 ArcBridgeService::State state_;
183
184 // WeakPtrFactory to use callbacks.
185 base::WeakPtrFactory<ArcBridgeService> weak_factory_;
186
187 DISALLOW_COPY_AND_ASSIGN(ArcBridgeService);
188 };
189
190 } // namespace arc
191
192 #endif // COMPONENTS_ARC_ARC_BRIDGE_SERVICE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698