OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 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 COMPONENTS_ARC_ARC_BRIDGE_SERVICE_H_ | 5 #ifndef COMPONENTS_ARC_ARC_BRIDGE_SERVICE_H_ |
6 #define COMPONENTS_ARC_ARC_BRIDGE_SERVICE_H_ | 6 #define COMPONENTS_ARC_ARC_BRIDGE_SERVICE_H_ |
7 | 7 |
| 8 #include "base/files/scoped_file.h" |
8 #include "base/macros.h" | 9 #include "base/macros.h" |
9 #include "base/observer_list.h" | 10 #include "base/observer_list.h" |
10 #include "base/sequenced_task_runner.h" | 11 #include "base/sequenced_task_runner.h" |
11 #include "components/arc/common/arc_message_types.h" | 12 #include "components/arc/common/arc_message_types.h" |
12 #include "ipc/ipc_channel_proxy.h" | |
13 #include "ipc/ipc_listener.h" | |
14 #include "ipc/ipc_message.h" | |
15 | 13 |
16 namespace arc { | 14 namespace arc { |
17 | 15 |
18 // The Chrome-side service that handles ARC instances and ARC bridge creation. | 16 // The Chrome-side service that handles ARC instances and ARC bridge creation. |
19 // This service handles the lifetime of ARC instances and sets up the | 17 // This service handles the lifetime of ARC instances and sets up the |
20 // communication channel (the ARC bridge) used to send and receive messages. | 18 // communication channel (the ARC bridge) used to send and receive messages. |
21 class ArcBridgeService : public IPC::Listener { | 19 class ArcBridgeService { |
22 public: | 20 public: |
23 // The possible states of the bridge. In the normal flow, the state changes | 21 // The possible states of the bridge. In the normal flow, the state changes |
24 // in the following sequence: | 22 // in the following sequence: |
25 // | 23 // |
26 // STOPPED | 24 // STOPPED |
27 // SetAvailable(true) + HandleStartup() -> SocketConnect() -> | 25 // SetAvailable(true) + HandleStartup() -> SocketConnect() -> |
28 // CONNECTING | 26 // CONNECTING |
29 // Connect() -> | 27 // Connect() -> |
30 // CONNECTED | 28 // CONNECTED |
31 // SocketConnectAfterSetSocketPermissions() -> | 29 // SocketConnectAfterSetSocketPermissions() -> |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
64 public: | 62 public: |
65 // Called whenever the state of the bridge has changed. | 63 // Called whenever the state of the bridge has changed. |
66 virtual void OnStateChanged(State state) {} | 64 virtual void OnStateChanged(State state) {} |
67 | 65 |
68 // Called when the instance has reached a boot phase | 66 // Called when the instance has reached a boot phase |
69 virtual void OnInstanceBootPhase(InstanceBootPhase phase) {} | 67 virtual void OnInstanceBootPhase(InstanceBootPhase phase) {} |
70 | 68 |
71 // Called whenever ARC's availability has changed for this system. | 69 // Called whenever ARC's availability has changed for this system. |
72 virtual void OnAvailableChanged(bool available) {} | 70 virtual void OnAvailableChanged(bool available) {} |
73 | 71 |
| 72 // Called whenever ARC sends information about available apps. |
| 73 virtual void OnAppsRefreshed(const std::vector<std::string>& name, |
| 74 const std::vector<std::string>& packages, |
| 75 const std::vector<std::string>& activities) {} |
| 76 |
| 77 // Called whenever ARC sends app icon data for specific scale factor. |
| 78 virtual void OnAppIcon(const std::string& package, |
| 79 const std::string& activity, |
| 80 int scale_factor, |
| 81 const std::vector<uint8_t>& icon_png_data) {} |
| 82 |
74 protected: | 83 protected: |
75 virtual ~Observer() {} | 84 virtual ~Observer() {} |
76 }; | 85 }; |
77 | 86 |
78 ArcBridgeService( | 87 ArcBridgeService(); |
79 const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner, | 88 virtual ~ArcBridgeService(); |
80 const scoped_refptr<base::SequencedTaskRunner>& file_task_runner); | |
81 ~ArcBridgeService() override; | |
82 | 89 |
83 // Gets the global instance of the ARC Bridge Service. This can only be | 90 // Gets the global instance of the ARC Bridge Service. This can only be |
84 // called on the thread that this class was created on. | 91 // called on the thread that this class was created on. |
85 static ArcBridgeService* Get(); | 92 static ArcBridgeService* Get(); |
86 | 93 |
87 // DetectAvailability() should be called once D-Bus is available. It will | |
88 // call CheckArcAvailability() on the session_manager. This can only be | |
89 // called on the thread that this class was created on. | |
90 void DetectAvailability(); | |
91 | |
92 // HandleStartup() should be called upon profile startup. This will only | 94 // HandleStartup() should be called upon profile startup. This will only |
93 // launch an instance if the instance service is available and it is enabled. | 95 // launch an instance if the instance service is available and it is enabled. |
94 // This can only be called on the thread that this class was created on. | 96 // This can only be called on the thread that this class was created on. |
95 void HandleStartup(); | 97 virtual void HandleStartup() = 0; |
96 | 98 |
97 // Shutdown() should be called when the browser is shutting down. This can | 99 // Shutdown() should be called when the browser is shutting down. This can |
98 // only be called on the thread that this class was created on. | 100 // only be called on the thread that this class was created on. |
99 void Shutdown(); | 101 virtual void Shutdown() = 0; |
100 | 102 |
101 // Adds or removes observers. This can only be called on the thread that this | 103 // Adds or removes observers. This can only be called on the thread that this |
102 // class was created on. | 104 // class was created on. |
103 void AddObserver(Observer* observer); | 105 void AddObserver(Observer* observer); |
104 void RemoveObserver(Observer* observer); | 106 void RemoveObserver(Observer* observer); |
105 | 107 |
106 // Gets the current state of the bridge service. | 108 // Gets the current state of the bridge service. |
107 State state() const { return state_; } | 109 State state() const { return state_; } |
108 | 110 |
109 // Gets if ARC is available in this system. | 111 // Gets if ARC is available in this system. |
110 bool available() const { return available_; } | 112 bool available() const { return available_; } |
111 | 113 |
112 // Requests registration of an input device on the ARC instance. | 114 // Requests registration of an input device on the ARC instance. |
113 // TODO(denniskempin): Make this interface more typesafe. | 115 // TODO(denniskempin): Make this interface more typesafe. |
114 // |name| should be the displayable name of the emulated device (e.g. "Chrome | 116 // |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") | 117 // 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. | 118 // and |fd| a file descriptor that emulates the kernel events of the device. |
117 // This can only be called on the thread that this class was created on. | 119 // This can only be called on the thread that this class was created on. |
118 bool RegisterInputDevice(const std::string& name, | 120 virtual bool RegisterInputDevice(const std::string& name, |
119 const std::string& device_type, | 121 const std::string& device_type, |
120 base::ScopedFD fd); | 122 base::ScopedFD fd) = 0; |
121 | 123 |
122 private: | 124 // Requests to refresh an app list. |
123 friend class ArcBridgeTest; | 125 virtual bool RefreshApps() = 0; |
124 FRIEND_TEST_ALL_PREFIXES(ArcBridgeTest, Basic); | |
125 FRIEND_TEST_ALL_PREFIXES(ArcBridgeTest, ShutdownMidStartup); | |
126 | 126 |
127 // If all pre-requisites are true (ARC is available, it has been enabled, and | 127 // Requests to launch an app. |
128 // the session has started), and ARC is stopped, start ARC. If ARC is running | 128 virtual bool LaunchApp(const std::string& package, |
129 // and the pre-requisites stop being true, stop ARC. | 129 const std::string& activity) = 0; |
130 void PrerequisitesChanged(); | |
131 | 130 |
132 // Binds to the socket specified by |socket_path|. | 131 // Request to load icon of specific scale_factor. |
133 void SocketConnect(const base::FilePath& socket_path); | 132 virtual bool RequestIcon(const std::string& package, |
| 133 const std::string& activity, |
| 134 int scale_factor) = 0; |
134 | 135 |
135 // Binds to the socket specified by |socket_path| after creating its parent | 136 protected: |
136 // directory is present. | |
137 void SocketConnectAfterEnsureParentDirectory( | |
138 const base::FilePath& socket_path, | |
139 bool directory_present); | |
140 | |
141 // Internal connection method. Separated to make testing easier. | |
142 bool Connect(const IPC::ChannelHandle& handle, IPC::Channel::Mode mode); | |
143 | |
144 // Finishes connecting after setting socket permissions. | |
145 void SocketConnectAfterSetSocketPermissions(const base::FilePath& socket_path, | |
146 bool socket_permissions_success); | |
147 | |
148 // Stops the running instance. | |
149 void StopInstance(); | |
150 | |
151 // Called when the instance has reached a boot phase | |
152 void OnInstanceBootPhase(InstanceBootPhase phase); | |
153 | |
154 // Changes the current state and notifies all observers. | 137 // Changes the current state and notifies all observers. |
155 void SetState(State state); | 138 void SetState(State state); |
156 | 139 |
157 // IPC::Listener: | 140 // Changes the current availability and notifies all observers. |
158 bool OnMessageReceived(const IPC::Message& message) override; | 141 void SetAvailable(bool availability); |
159 | |
160 // DBus callbacks. | |
161 void OnArcAvailable(bool available); | |
162 void OnInstanceStarted(bool success); | |
163 void OnInstanceStopped(bool success); | |
164 | 142 |
165 scoped_refptr<base::SequencedTaskRunner> origin_task_runner_; | 143 scoped_refptr<base::SequencedTaskRunner> origin_task_runner_; |
166 scoped_refptr<base::SingleThreadTaskRunner> ipc_task_runner_; | |
167 scoped_refptr<base::SequencedTaskRunner> file_task_runner_; | |
168 | |
169 scoped_ptr<IPC::ChannelProxy> ipc_channel_; | |
170 | 144 |
171 base::ObserverList<Observer> observer_list_; | 145 base::ObserverList<Observer> observer_list_; |
172 | 146 |
173 // If the user's session has started. | |
174 bool session_started_; | |
175 | |
176 // If the ARC instance service is available. | 147 // If the ARC instance service is available. |
177 bool available_; | 148 bool available_; |
178 | 149 |
179 // The current state of the bridge. | 150 // The current state of the bridge. |
180 ArcBridgeService::State state_; | 151 ArcBridgeService::State state_; |
181 | 152 |
182 // WeakPtrFactory to use callbacks. | |
183 base::WeakPtrFactory<ArcBridgeService> weak_factory_; | |
184 | |
185 DISALLOW_COPY_AND_ASSIGN(ArcBridgeService); | 153 DISALLOW_COPY_AND_ASSIGN(ArcBridgeService); |
186 }; | 154 }; |
187 | 155 |
188 } // namespace arc | 156 } // namespace arc |
189 | 157 |
190 #endif // COMPONENTS_ARC_ARC_BRIDGE_SERVICE_H_ | 158 #endif // COMPONENTS_ARC_ARC_BRIDGE_SERVICE_H_ |
OLD | NEW |