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

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: Addressed feedback 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 -> CONNECTING -> CONNECTED -> STARTING -> READY
34 //
35 // When Shutdown() is called, if it is not already stopped or in the ERROR
Shuhei Takahashi 2015/10/29 00:13:08 ERROR state has been removed.
Luis Héctor Chávez 2015/10/29 23:43:53 Done.
36 // state, the state changes in the following way:
37 //
38 // (ANY) -> STOPPING -> STOPPED
39 enum State {
40 // ARC is not currently running.
41 STOPPED,
42
43 // The request to connect has been sent.
44 CONNECTING,
45
46 // The bridge has connected to the socket, but has not started the ARC
47 // instance.
48 CONNECTED,
49
50 // The ARC bridge has been set up and ARC is starting up.
51 STARTING,
52
53 // The ARC instance has been fully initialized and is now ready for user
54 // interaction.
55 READY,
56
57 // The ARC instance has started shutting down.
58 STOPPING,
59 };
60
61 class Observer {
62 public:
63 // Called whenever the state of the bridge has changed.
64 virtual void OnStateChanged(State state) {}
65
66 // Called whenever ARC's enabled status has changed for this session.
67 virtual void OnEnabledChanged(bool enabled) {}
68
69 protected:
70 virtual ~Observer() {}
71 };
72
73 explicit ArcBridgeService(
74 const scoped_refptr<base::SequencedTaskRunner>& file_task_runner);
75 virtual ~ArcBridgeService();
76
77 // Registers ARC preferences.
78 static void RegisterPrefs(PrefRegistrySimple* registry);
79
80 // Reads from the preference store if ARC has been enabled.
81 static bool GetEnabledPref(PrefService* pref_service);
82
83 // HandleStartup() should be called upon profile startup. This will only
84 // launch an instance if the instance service is available and it is enabled.
85 void HandleStartup();
86
87 // Shuts down the running instance, if any. This is safe to call even if
88 // there is no instance running.
89 void Shutdown();
90
91 // Adds or removes observers.
92 void AddObserver(Observer* observer);
93 void RemoveObserver(Observer* observer);
94
95 // Gets the current state of the bridge service.
96 State GetState() const { return state_; }
97
98 // Enables or disables the bridge for this session.
99 void SetEnabled(bool enabled);
100
101 // Gets if the bridge is enabled for this session.
102 bool IsEnabled() const { return enabled_; }
103
104 // Requests registration of an input device on the ARC instance.
105 // TODO(denniskempin): Make this interface more typesafe.
106 // |name| should be the displayable name of the emulated device (e.g. "Chrome
107 // OS Keyboard"), |device_type| the name of the device type (e.g. "keyboard")
108 // and |fd| a file descriptor that emulates the kernel events of the device.
109 bool RegisterInputDevice(const std::string& name,
110 const std::string& device_type,
111 base::ScopedFD fd);
112
113 private:
114 friend class arc::ArcBridgeTest;
115
116 // Binds to the socket specified by |socket_path|.
117 void SocketConnect(const base::FilePath& socket_path);
118
119 // Binds to the socket specified by |socket_path| after creating its parent
120 // directory is present.
121 void SocketConnectAfterEnsureParentDirectory(
122 const base::FilePath& socket_path, bool directory_present);
hidehiko 2015/10/29 16:29:47 nit: 4 indent. http://google-styleguide.googlecode
Luis Héctor Chávez 2015/10/29 23:43:53 Done.
123
124 // Internal connection method. Separated to make testing easier.
125 bool Connect(const IPC::ChannelHandle& handle, IPC::Channel::Mode mode);
126
127 // Finishes connecting after setting socket permissions.
128 void FinishConnectAfterSetSocketPermissions(const base::FilePath& socket_path,
hidehiko 2015/10/29 16:29:47 Maybe: SocketConnectAfterSetSocketPermissions, as
Luis Héctor Chávez 2015/10/29 23:43:53 Done.
129 bool socket_permissions_success);
130
131 // Called when the ARC instance has finished setting up and is ready for user
132 // interaction.
133 void OnInstanceReady();
134
135 // Changes the current state and notify all observers.
136 void SetState(State state);
137
138 // Finishes shutdown after deleting the socket file.
139 void FinishShutdownAfterSocketDeleted(bool socket_deleted);
hidehiko 2015/10/29 16:29:47 ditto: ShutdownAfterSocketDeleted.
Luis Héctor Chávez 2015/10/29 23:43:53 Done.
140
141 // IPC::Listener:
142 bool OnMessageReceived(const IPC::Message& message) override;
143
144 // DBus callbacks.
145 void OnInstanceStarted(chromeos::DBusMethodCallStatus status);
146 void OnInstanceStopped(chromeos::DBusMethodCallStatus status);
147
148 // Thread in which IPC messaging is performed.
149 base::Thread ipc_thread_;
150
151 // Task runner on which incoming messages are handled.
152 scoped_refptr<base::SequencedTaskRunner> origin_task_runner_;
153
154 // Task runner on which file operations are performed.
155 scoped_refptr<base::SequencedTaskRunner> file_task_runner_;
156
157 // The channel through which messages are sent.
158 scoped_ptr<IPC::ChannelProxy> ipc_channel_;
159
160 // List of observers to notify of state changes.
161 scoped_refptr<base::ObserverListThreadSafe<Observer>> observer_list_;
162
163 // If the ARC instance service is available.
164 bool available_;
165
166 // If ARC has been enabled by policy and user choice.
167 bool enabled_;
168
169 // The current state of the bridge.
170 ArcBridgeService::State state_;
171
172 // WeakPtrFactory to use callbacks.
173 base::WeakPtrFactory<ArcBridgeService> weak_factory_;
174
175 DISALLOW_COPY_AND_ASSIGN(ArcBridgeService);
176 };
177
178 } // namespace arc
179
180 #endif // COMPONENTS_ARC_ARC_BRIDGE_SERVICE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698