Chromium Code Reviews| 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 explicit CastSocket(); |
| 64 ~CastSocket() override {} | 62 virtual ~CastSocket() {} |
| 65 | 63 |
| 66 // Used by BrowserContextKeyedAPIFactory. | 64 // Used by BrowserContextKeyedAPIFactory. |
| 67 static const char* service_name() { return "CastSocketImplManager"; } | 65 static const char* service_name() { return "CastSocketImplManager"; } |
| 68 | 66 |
| 69 // Connects the channel to the peer. If successful, the channel will be in | 67 // 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|. | 68 // READY_STATE_OPEN. DO NOT delete the CastSocket object in |callback|. |
| 71 // Instead use Close(). | 69 // Instead use Close(). |
| 72 // |callback| will be invoked with any ChannelError that occurred, or | 70 // |callback| will be invoked with any ChannelError that occurred, or |
| 73 // CHANNEL_ERROR_NONE if successful. | 71 // CHANNEL_ERROR_NONE if successful. |
| 74 // If the CastSocket is destroyed while the connection is pending, |callback| | 72 // If the CastSocket is destroyed while the connection is pending, |callback| |
| 75 // will be invoked with CHANNEL_ERROR_UNKNOWN. In this case, invoking | 73 // will be invoked with CHANNEL_ERROR_UNKNOWN. In this case, invoking |
| 76 // |callback| must not result in any re-entrancy behavior. | 74 // |callback| must not result in any re-entrancy behavior. |
| 77 // |delegate| receives message receipt and error events. | 75 // |delegate| receives message receipt and error events. |
| 78 // Ownership of |delegate| is transferred to this CastSocket. | 76 // Ownership of |delegate| is transferred to this CastSocket. |
| 79 virtual void Connect(std::unique_ptr<CastTransport::Delegate> delegate, | 77 virtual void Connect(std::unique_ptr<CastTransport::Delegate> delegate, |
| 80 base::Callback<void(ChannelError)> callback) = 0; | 78 base::Callback<void(ChannelError)> callback) = 0; |
| 81 | 79 |
| 82 // Closes the channel if not already closed. On completion, the channel will | 80 // Closes the channel if not already closed. On completion, the channel will |
| 83 // be in READY_STATE_CLOSED. | 81 // be in READY_STATE_CLOSED. |
| 84 // | 82 // |
| 85 // It is fine to delete this object in |callback|. | 83 // It is fine to delete this object in |callback|. |
| 86 virtual void Close(const net::CompletionCallback& callback) = 0; | 84 virtual void Close(const net::CompletionCallback& callback) = 0; |
| 87 | 85 |
| 88 // The IP endpoint for the destination of the channel. | 86 // The IP endpoint for the destination of the channel. |
| 89 virtual const net::IPEndPoint& ip_endpoint() const = 0; | 87 virtual const net::IPEndPoint& ip_endpoint() const = 0; |
| 90 | 88 |
| 91 // Channel id generated by the ApiResourceManager. | 89 // Channel id generated by the CastChannelService. |
| 92 virtual int id() const = 0; | 90 virtual int id() const = 0; |
| 93 | 91 |
| 94 // Sets the channel id generated by ApiResourceManager. | 92 // Sets the channel id generated by CastChannelService. |
| 95 virtual void set_id(int id) = 0; | 93 virtual void set_id(int id) = 0; |
| 96 | 94 |
| 97 // The authentication level requested for the channel. | 95 // The authentication level requested for the channel. |
| 98 virtual ChannelAuthType channel_auth() const = 0; | 96 virtual ChannelAuthType channel_auth() const = 0; |
| 99 | 97 |
| 100 // The ready state of the channel. | 98 // The ready state of the channel. |
| 101 virtual ReadyState ready_state() const = 0; | 99 virtual ReadyState ready_state() const = 0; |
| 102 | 100 |
| 103 // Returns the last error that occurred on this channel, or | 101 // Returns the last error that occurred on this channel, or |
| 104 // CHANNEL_ERROR_NONE if no error has occurred. | 102 // CHANNEL_ERROR_NONE if no error has occurred. |
| 105 virtual ChannelError error_state() const = 0; | 103 virtual ChannelError error_state() const = 0; |
| 106 | 104 |
| 107 // True when keep-alive signaling is handled for this socket. | 105 // True when keep-alive signaling is handled for this socket. |
| 108 virtual bool keep_alive() const = 0; | 106 virtual bool keep_alive() const = 0; |
| 109 | 107 |
| 110 // Whether the channel is audio only as identified by the device | 108 // Whether the channel is audio only as identified by the device |
| 111 // certificate during channel authentication. | 109 // certificate during channel authentication. |
| 112 virtual bool audio_only() const = 0; | 110 virtual bool audio_only() const = 0; |
| 113 | 111 |
| 112 virtual const std::string& owner_extension_id() const = 0; | |
|
mark a. foltz
2017/05/18 19:12:05
Is this still necessary? I think this is only use
zhaobin
2017/05/24 01:51:40
Still needed when dispatch UI event...
https://cs
imcheng
2017/05/24 20:24:32
Can we just use extension_->id() (which is used to
zhaobin
2017/05/24 22:15:07
Done.
| |
| 113 | |
| 114 // Marks a socket as invalid due to an error, and sends an OnError | 114 // Marks a socket as invalid due to an error, and sends an OnError |
| 115 // event to |delegate_|. | 115 // event to |delegate_|. |
| 116 // The OnError event receipient is responsible for closing the socket in the | 116 // The OnError event receipient is responsible for closing the socket in the |
| 117 // event of an error. | 117 // event of an error. |
| 118 // Setting the error state does not close the socket if it is open. | 118 // Setting the error state does not close the socket if it is open. |
| 119 virtual void SetErrorState(ChannelError error_state) = 0; | 119 virtual void SetErrorState(ChannelError error_state) = 0; |
| 120 | 120 |
| 121 // Returns a pointer to the socket's message transport layer. Can be used to | 121 // Returns a pointer to the socket's message transport layer. Can be used to |
| 122 // send and receive CastMessages over the socket. | 122 // send and receive CastMessages over the socket. |
| 123 virtual CastTransport* transport() const = 0; | 123 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 }; | 124 }; |
| 131 | 125 |
| 132 // This class implements a channel between Chrome and a Cast device using a TCP | 126 // 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 | 127 // 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. | 128 // Cast device. All CastSocketImpl objects must be used only on the IO thread. |
| 135 // | 129 // |
| 136 // NOTE: Not called "CastChannel" to reduce confusion with the generated API | 130 // NOTE: Not called "CastChannel" to reduce confusion with the generated API |
| 137 // code. | 131 // code. |
| 138 class CastSocketImpl : public CastSocket { | 132 class CastSocketImpl : public CastSocket { |
| 139 public: | 133 public: |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 178 CastTransport* transport() const override; | 172 CastTransport* transport() const override; |
| 179 void Close(const net::CompletionCallback& callback) override; | 173 void Close(const net::CompletionCallback& callback) override; |
| 180 const net::IPEndPoint& ip_endpoint() const override; | 174 const net::IPEndPoint& ip_endpoint() const override; |
| 181 int id() const override; | 175 int id() const override; |
| 182 void set_id(int channel_id) override; | 176 void set_id(int channel_id) override; |
| 183 ChannelAuthType channel_auth() const override; | 177 ChannelAuthType channel_auth() const override; |
| 184 ReadyState ready_state() const override; | 178 ReadyState ready_state() const override; |
| 185 ChannelError error_state() const override; | 179 ChannelError error_state() const override; |
| 186 bool keep_alive() const override; | 180 bool keep_alive() const override; |
| 187 bool audio_only() const override; | 181 bool audio_only() const override; |
| 182 const std::string& owner_extension_id() const override; | |
| 188 | 183 |
| 189 // Required by ApiResourceManager. | 184 // Required by ApiResourceManager. |
| 190 static const char* service_name() { return "CastSocketManager"; } | 185 static const char* service_name() { return "CastSocketManager"; } |
| 191 | 186 |
| 192 protected: | 187 protected: |
| 193 // CastTransport::Delegate methods for receiving handshake messages. | 188 // CastTransport::Delegate methods for receiving handshake messages. |
| 194 class AuthTransportDelegate : public CastTransport::Delegate { | 189 class AuthTransportDelegate : public CastTransport::Delegate { |
| 195 public: | 190 public: |
| 196 explicit AuthTransportDelegate(CastSocketImpl* socket); | 191 explicit AuthTransportDelegate(CastSocketImpl* socket); |
| 197 | 192 |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 222 // capability must not have a certificate with audio only policy. | 217 // capability must not have a certificate with audio only policy. |
| 223 bool VerifyChannelPolicy(const AuthResult& result); | 218 bool VerifyChannelPolicy(const AuthResult& result); |
| 224 | 219 |
| 225 private: | 220 private: |
| 226 FRIEND_TEST_ALL_PREFIXES(CastSocketTest, TestConnectAuthMessageCorrupted); | 221 FRIEND_TEST_ALL_PREFIXES(CastSocketTest, TestConnectAuthMessageCorrupted); |
| 227 FRIEND_TEST_ALL_PREFIXES(CastSocketTest, | 222 FRIEND_TEST_ALL_PREFIXES(CastSocketTest, |
| 228 TestConnectChallengeReplyReceiveError); | 223 TestConnectChallengeReplyReceiveError); |
| 229 FRIEND_TEST_ALL_PREFIXES(CastSocketTest, | 224 FRIEND_TEST_ALL_PREFIXES(CastSocketTest, |
| 230 TestConnectChallengeVerificationFails); | 225 TestConnectChallengeVerificationFails); |
| 231 friend class AuthTransportDelegate; | 226 friend class AuthTransportDelegate; |
| 232 friend class ApiResourceManager<CastSocketImpl>; | |
| 233 friend class CastSocketTest; | 227 friend class CastSocketTest; |
| 234 friend class TestCastSocket; | 228 friend class TestCastSocket; |
| 235 | 229 |
| 236 void SetErrorState(ChannelError error_state) override; | 230 void SetErrorState(ChannelError error_state) override; |
| 237 | 231 |
| 238 // Frees resources and cancels pending callbacks. |ready_state_| will be set | 232 // 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 | 233 // READY_STATE_CLOSED on completion. A no-op if |ready_state_| is already |
| 240 // READY_STATE_CLOSED. | 234 // READY_STATE_CLOSED. |
| 241 void CloseInternal(); | 235 void CloseInternal(); |
| 242 | 236 |
| (...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 401 // information. | 395 // information. |
| 402 AuthTransportDelegate* auth_delegate_; | 396 AuthTransportDelegate* auth_delegate_; |
| 403 | 397 |
| 404 DISALLOW_COPY_AND_ASSIGN(CastSocketImpl); | 398 DISALLOW_COPY_AND_ASSIGN(CastSocketImpl); |
| 405 }; | 399 }; |
| 406 } // namespace cast_channel | 400 } // namespace cast_channel |
| 407 } // namespace api | 401 } // namespace api |
| 408 } // namespace extensions | 402 } // namespace extensions |
| 409 | 403 |
| 410 #endif // EXTENSIONS_BROWSER_API_CAST_CHANNEL_CAST_SOCKET_H_ | 404 #endif // EXTENSIONS_BROWSER_API_CAST_CHANNEL_CAST_SOCKET_H_ |
| OLD | NEW |