OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 EXTENSIONS_BROWSER_API_CAST_CHANNEL_CAST_SOCKET_H_ | 5 #ifndef EXTENSIONS_BROWSER_API_CAST_CHANNEL_CAST_SOCKET_H_ |
6 #define EXTENSIONS_BROWSER_API_CAST_CHANNEL_CAST_SOCKET_H_ | 6 #define EXTENSIONS_BROWSER_API_CAST_CHANNEL_CAST_SOCKET_H_ |
7 | 7 |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <queue> | 10 #include <queue> |
11 #include <string> | 11 #include <string> |
12 | 12 |
13 #include "base/cancelable_callback.h" | 13 #include "base/cancelable_callback.h" |
14 #include "base/gtest_prod_util.h" | 14 #include "base/gtest_prod_util.h" |
15 #include "base/macros.h" | 15 #include "base/macros.h" |
16 #include "base/memory/ref_counted.h" | 16 #include "base/memory/ref_counted.h" |
17 #include "base/threading/thread_checker.h" | 17 #include "base/threading/thread_checker.h" |
18 #include "base/timer/timer.h" | 18 #include "base/timer/timer.h" |
19 #include "extensions/browser/api/api_resource.h" | |
20 #include "extensions/browser/api/api_resource_manager.h" | |
21 #include "extensions/browser/api/cast_channel/cast_auth_util.h" | 19 #include "extensions/browser/api/cast_channel/cast_auth_util.h" |
22 #include "extensions/browser/api/cast_channel/cast_socket.h" | 20 #include "extensions/browser/api/cast_channel/cast_socket.h" |
23 #include "extensions/browser/api/cast_channel/cast_transport.h" | 21 #include "extensions/browser/api/cast_channel/cast_transport.h" |
24 #include "extensions/common/api/cast_channel.h" | 22 #include "extensions/common/api/cast_channel.h" |
25 #include "extensions/common/api/cast_channel/logging.pb.h" | 23 #include "extensions/common/api/cast_channel/logging.pb.h" |
26 #include "net/base/completion_callback.h" | 24 #include "net/base/completion_callback.h" |
27 #include "net/base/io_buffer.h" | 25 #include "net/base/io_buffer.h" |
28 #include "net/base/ip_endpoint.h" | 26 #include "net/base/ip_endpoint.h" |
29 #include "net/log/net_log_source.h" | 27 #include "net/log/net_log_source.h" |
30 | 28 |
(...skipping 20 matching lines...) Expand all Loading... |
51 enum CastDeviceCapability { | 49 enum CastDeviceCapability { |
52 NONE = 0, | 50 NONE = 0, |
53 VIDEO_OUT = 1 << 0, | 51 VIDEO_OUT = 1 << 0, |
54 VIDEO_IN = 1 << 1, | 52 VIDEO_IN = 1 << 1, |
55 AUDIO_OUT = 1 << 2, | 53 AUDIO_OUT = 1 << 2, |
56 AUDIO_IN = 1 << 3, | 54 AUDIO_IN = 1 << 3, |
57 DEV_MODE = 1 << 4 | 55 DEV_MODE = 1 << 4 |
58 }; | 56 }; |
59 | 57 |
60 // Public interface of the CastSocket class. | 58 // Public interface of the CastSocket class. |
61 class CastSocket : public ApiResource { | 59 class CastSocket { |
62 public: | 60 public: |
63 explicit CastSocket(const std::string& owner_extension_id); | 61 virtual ~CastSocket() {} |
64 ~CastSocket() override {} | |
65 | |
66 // Used by BrowserContextKeyedAPIFactory. | |
67 static const char* service_name() { return "CastSocketImplManager"; } | |
68 | 62 |
69 // Connects the channel to the peer. If successful, the channel will be in | 63 // Connects the channel to the peer. If successful, the channel will be in |
70 // READY_STATE_OPEN. DO NOT delete the CastSocket object in |callback|. | 64 // READY_STATE_OPEN. DO NOT delete the CastSocket object in |callback|. |
71 // Instead use Close(). | 65 // Instead use Close(). |
72 // |callback| will be invoked with any ChannelError that occurred, or | 66 // |callback| will be invoked with any ChannelError that occurred, or |
73 // CHANNEL_ERROR_NONE if successful. | 67 // CHANNEL_ERROR_NONE if successful. |
74 // If the CastSocket is destroyed while the connection is pending, |callback| | 68 // If the CastSocket is destroyed while the connection is pending, |callback| |
75 // will be invoked with CHANNEL_ERROR_UNKNOWN. In this case, invoking | 69 // will be invoked with CHANNEL_ERROR_UNKNOWN. In this case, invoking |
76 // |callback| must not result in any re-entrancy behavior. | 70 // |callback| must not result in any re-entrancy behavior. |
77 // |delegate| receives message receipt and error events. | 71 // |delegate| receives message receipt and error events. |
78 // Ownership of |delegate| is transferred to this CastSocket. | 72 // Ownership of |delegate| is transferred to this CastSocket. |
79 virtual void Connect(std::unique_ptr<CastTransport::Delegate> delegate, | 73 virtual void Connect(std::unique_ptr<CastTransport::Delegate> delegate, |
80 base::Callback<void(ChannelError)> callback) = 0; | 74 base::Callback<void(ChannelError)> callback) = 0; |
81 | 75 |
82 // Closes the channel if not already closed. On completion, the channel will | 76 // Closes the channel if not already closed. On completion, the channel will |
83 // be in READY_STATE_CLOSED. | 77 // be in READY_STATE_CLOSED. |
84 // | 78 // |
85 // It is fine to delete this object in |callback|. | 79 // It is fine to delete this object in |callback|. |
86 virtual void Close(const net::CompletionCallback& callback) = 0; | 80 virtual void Close(const net::CompletionCallback& callback) = 0; |
87 | 81 |
88 // The IP endpoint for the destination of the channel. | 82 // The IP endpoint for the destination of the channel. |
89 virtual const net::IPEndPoint& ip_endpoint() const = 0; | 83 virtual const net::IPEndPoint& ip_endpoint() const = 0; |
90 | 84 |
91 // Channel id generated by the ApiResourceManager. | 85 // Channel id generated by the CastChannelService. |
92 virtual int id() const = 0; | 86 virtual int id() const = 0; |
93 | 87 |
94 // Sets the channel id generated by ApiResourceManager. | 88 // Sets the channel id generated by CastChannelService. |
95 virtual void set_id(int id) = 0; | 89 virtual void set_id(int id) = 0; |
96 | 90 |
97 // The authentication level requested for the channel. | 91 // The authentication level requested for the channel. |
98 virtual ChannelAuthType channel_auth() const = 0; | 92 virtual ChannelAuthType channel_auth() const = 0; |
99 | 93 |
100 // The ready state of the channel. | 94 // The ready state of the channel. |
101 virtual ReadyState ready_state() const = 0; | 95 virtual ReadyState ready_state() const = 0; |
102 | 96 |
103 // Returns the last error that occurred on this channel, or | 97 // Returns the last error that occurred on this channel, or |
104 // CHANNEL_ERROR_NONE if no error has occurred. | 98 // CHANNEL_ERROR_NONE if no error has occurred. |
105 virtual ChannelError error_state() const = 0; | 99 virtual ChannelError error_state() const = 0; |
106 | 100 |
107 // True when keep-alive signaling is handled for this socket. | 101 // True when keep-alive signaling is handled for this socket. |
108 virtual bool keep_alive() const = 0; | 102 virtual bool keep_alive() const = 0; |
109 | 103 |
110 // Whether the channel is audio only as identified by the device | 104 // Whether the channel is audio only as identified by the device |
111 // certificate during channel authentication. | 105 // certificate during channel authentication. |
112 virtual bool audio_only() const = 0; | 106 virtual bool audio_only() const = 0; |
113 | 107 |
114 // Marks a socket as invalid due to an error, and sends an OnError | 108 // Marks a socket as invalid due to an error, and sends an OnError |
115 // event to |delegate_|. | 109 // event to |delegate_|. |
116 // The OnError event receipient is responsible for closing the socket in the | 110 // The OnError event receipient is responsible for closing the socket in the |
117 // event of an error. | 111 // event of an error. |
118 // Setting the error state does not close the socket if it is open. | 112 // Setting the error state does not close the socket if it is open. |
119 virtual void SetErrorState(ChannelError error_state) = 0; | 113 virtual void SetErrorState(ChannelError error_state) = 0; |
120 | 114 |
121 // Returns a pointer to the socket's message transport layer. Can be used to | 115 // Returns a pointer to the socket's message transport layer. Can be used to |
122 // send and receive CastMessages over the socket. | 116 // send and receive CastMessages over the socket. |
123 virtual CastTransport* transport() const = 0; | 117 virtual CastTransport* transport() const = 0; |
124 | |
125 // Tells the ApiResourceManager to retain CastSocket objects even | |
126 // if their corresponding extension is suspended. | |
127 // (CastSockets are still deleted if the extension is removed entirely from | |
128 // the browser.) | |
129 bool IsPersistent() const override; | |
130 }; | 118 }; |
131 | 119 |
132 // This class implements a channel between Chrome and a Cast device using a TCP | 120 // This class implements a channel between Chrome and a Cast device using a TCP |
133 // socket with SSL. The channel may authenticate that the receiver is a genuine | 121 // socket with SSL. The channel may authenticate that the receiver is a genuine |
134 // Cast device. All CastSocketImpl objects must be used only on the IO thread. | 122 // Cast device. All CastSocketImpl objects must be used only on the IO thread. |
135 // | 123 // |
136 // NOTE: Not called "CastChannel" to reduce confusion with the generated API | 124 // NOTE: Not called "CastChannel" to reduce confusion with the generated API |
137 // code. | 125 // code. |
138 class CastSocketImpl : public CastSocket { | 126 class CastSocketImpl : public CastSocket { |
139 public: | 127 public: |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
179 void Close(const net::CompletionCallback& callback) override; | 167 void Close(const net::CompletionCallback& callback) override; |
180 const net::IPEndPoint& ip_endpoint() const override; | 168 const net::IPEndPoint& ip_endpoint() const override; |
181 int id() const override; | 169 int id() const override; |
182 void set_id(int channel_id) override; | 170 void set_id(int channel_id) override; |
183 ChannelAuthType channel_auth() const override; | 171 ChannelAuthType channel_auth() const override; |
184 ReadyState ready_state() const override; | 172 ReadyState ready_state() const override; |
185 ChannelError error_state() const override; | 173 ChannelError error_state() const override; |
186 bool keep_alive() const override; | 174 bool keep_alive() const override; |
187 bool audio_only() const override; | 175 bool audio_only() const override; |
188 | 176 |
189 // Required by ApiResourceManager. | |
190 static const char* service_name() { return "CastSocketManager"; } | |
191 | |
192 protected: | 177 protected: |
193 // CastTransport::Delegate methods for receiving handshake messages. | 178 // CastTransport::Delegate methods for receiving handshake messages. |
194 class AuthTransportDelegate : public CastTransport::Delegate { | 179 class AuthTransportDelegate : public CastTransport::Delegate { |
195 public: | 180 public: |
196 explicit AuthTransportDelegate(CastSocketImpl* socket); | 181 explicit AuthTransportDelegate(CastSocketImpl* socket); |
197 | 182 |
198 // Gets the error state of the channel. | 183 // Gets the error state of the channel. |
199 // Returns CHANNEL_ERROR_NONE if no errors are present. | 184 // Returns CHANNEL_ERROR_NONE if no errors are present. |
200 ChannelError error_state() const; | 185 ChannelError error_state() const; |
201 | 186 |
(...skipping 20 matching lines...) Expand all Loading... |
222 // capability must not have a certificate with audio only policy. | 207 // capability must not have a certificate with audio only policy. |
223 bool VerifyChannelPolicy(const AuthResult& result); | 208 bool VerifyChannelPolicy(const AuthResult& result); |
224 | 209 |
225 private: | 210 private: |
226 FRIEND_TEST_ALL_PREFIXES(CastSocketTest, TestConnectAuthMessageCorrupted); | 211 FRIEND_TEST_ALL_PREFIXES(CastSocketTest, TestConnectAuthMessageCorrupted); |
227 FRIEND_TEST_ALL_PREFIXES(CastSocketTest, | 212 FRIEND_TEST_ALL_PREFIXES(CastSocketTest, |
228 TestConnectChallengeReplyReceiveError); | 213 TestConnectChallengeReplyReceiveError); |
229 FRIEND_TEST_ALL_PREFIXES(CastSocketTest, | 214 FRIEND_TEST_ALL_PREFIXES(CastSocketTest, |
230 TestConnectChallengeVerificationFails); | 215 TestConnectChallengeVerificationFails); |
231 friend class AuthTransportDelegate; | 216 friend class AuthTransportDelegate; |
232 friend class ApiResourceManager<CastSocketImpl>; | |
233 friend class CastSocketTest; | 217 friend class CastSocketTest; |
234 friend class TestCastSocket; | 218 friend class TestCastSocket; |
235 | 219 |
236 void SetErrorState(ChannelError error_state) override; | 220 void SetErrorState(ChannelError error_state) override; |
237 | 221 |
238 // Frees resources and cancels pending callbacks. |ready_state_| will be set | 222 // Frees resources and cancels pending callbacks. |ready_state_| will be set |
239 // READY_STATE_CLOSED on completion. A no-op if |ready_state_| is already | 223 // READY_STATE_CLOSED on completion. A no-op if |ready_state_| is already |
240 // READY_STATE_CLOSED. | 224 // READY_STATE_CLOSED. |
241 void CloseInternal(); | 225 void CloseInternal(); |
242 | 226 |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
298 | 282 |
299 virtual bool CalledOnValidThread() const; | 283 virtual bool CalledOnValidThread() const; |
300 | 284 |
301 virtual base::Timer* GetTimer(); | 285 virtual base::Timer* GetTimer(); |
302 | 286 |
303 void SetConnectState(proto::ConnectionState connect_state); | 287 void SetConnectState(proto::ConnectionState connect_state); |
304 void SetReadyState(ReadyState ready_state); | 288 void SetReadyState(ReadyState ready_state); |
305 | 289 |
306 base::ThreadChecker thread_checker_; | 290 base::ThreadChecker thread_checker_; |
307 | 291 |
308 const std::string owner_extension_id_; | |
309 // The id of the channel. | 292 // The id of the channel. |
310 int channel_id_; | 293 int channel_id_; |
311 // The IP endpoint that the the channel is connected to. | 294 // The IP endpoint that the the channel is connected to. |
312 net::IPEndPoint ip_endpoint_; | 295 net::IPEndPoint ip_endpoint_; |
313 // Receiver authentication requested for the channel. | 296 // Receiver authentication requested for the channel. |
314 ChannelAuthType channel_auth_; | 297 ChannelAuthType channel_auth_; |
315 // The NetLog for this service. | 298 // The NetLog for this service. |
316 net::NetLog* net_log_; | 299 net::NetLog* net_log_; |
317 // The NetLog source for this service. | 300 // The NetLog source for this service. |
318 net::NetLogSource net_log_source_; | 301 net::NetLogSource net_log_source_; |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
401 // information. | 384 // information. |
402 AuthTransportDelegate* auth_delegate_; | 385 AuthTransportDelegate* auth_delegate_; |
403 | 386 |
404 DISALLOW_COPY_AND_ASSIGN(CastSocketImpl); | 387 DISALLOW_COPY_AND_ASSIGN(CastSocketImpl); |
405 }; | 388 }; |
406 } // namespace cast_channel | 389 } // namespace cast_channel |
407 } // namespace api | 390 } // namespace api |
408 } // namespace extensions | 391 } // namespace extensions |
409 | 392 |
410 #endif // EXTENSIONS_BROWSER_API_CAST_CHANNEL_CAST_SOCKET_H_ | 393 #endif // EXTENSIONS_BROWSER_API_CAST_CHANNEL_CAST_SOCKET_H_ |
OLD | NEW |