| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 CHROME_BROWSER_EXTENSIONS_API_SOCKET_SOCKET_API_H_ | 5 #ifndef CHROME_BROWSER_EXTENSIONS_API_SOCKET_SOCKET_API_H_ |
| 6 #define CHROME_BROWSER_EXTENSIONS_API_SOCKET_SOCKET_API_H_ | 6 #define CHROME_BROWSER_EXTENSIONS_API_SOCKET_SOCKET_API_H_ |
| 7 | 7 |
| 8 #include "base/memory/ref_counted.h" | 8 #include "base/memory/ref_counted.h" |
| 9 #include "chrome/browser/extensions/api/api_function.h" | 9 #include "chrome/browser/extensions/api/api_function.h" |
| 10 #include "chrome/browser/extensions/api/api_resource_manager.h" |
| 10 #include "chrome/common/extensions/api/experimental_socket.h" | 11 #include "chrome/common/extensions/api/experimental_socket.h" |
| 11 #include "net/base/address_list.h" | 12 #include "net/base/address_list.h" |
| 12 #include "net/base/host_resolver.h" | 13 #include "net/base/host_resolver.h" |
| 13 | 14 |
| 14 #include <string> | 15 #include <string> |
| 15 | 16 |
| 17 class IOThread; |
| 18 |
| 16 namespace net { | 19 namespace net { |
| 17 class IOBuffer; | 20 class IOBuffer; |
| 18 } | 21 } |
| 19 | 22 |
| 20 class IOThread; | |
| 21 | |
| 22 namespace extensions { | 23 namespace extensions { |
| 23 | 24 |
| 24 class APIResourceController; | 25 class ApiResourceEventNotifier; |
| 25 class APIResourceEventNotifier; | 26 class Socket; |
| 26 | 27 |
| 27 class SocketExtensionFunction : public AsyncAPIFunction { | 28 class SocketAsyncApiFunction : public AsyncApiFunction { |
| 29 public: |
| 30 SocketAsyncApiFunction(); |
| 31 |
| 28 protected: | 32 protected: |
| 29 virtual ~SocketExtensionFunction() {} | 33 virtual ~SocketAsyncApiFunction(); |
| 30 | 34 |
| 31 // AsyncAPIFunction: | 35 // AsyncApiFunction: |
| 32 virtual void Work() OVERRIDE; | 36 virtual bool PrePrepare() OVERRIDE; |
| 33 virtual bool Respond() OVERRIDE; | 37 virtual bool Respond() OVERRIDE; |
| 38 |
| 39 ApiResourceManager<Socket>* manager_; |
| 34 }; | 40 }; |
| 35 | 41 |
| 36 class SocketExtensionWithDnsLookupFunction : public SocketExtensionFunction { | 42 class SocketExtensionWithDnsLookupFunction : public SocketAsyncApiFunction { |
| 37 protected: | 43 protected: |
| 38 SocketExtensionWithDnsLookupFunction(); | 44 SocketExtensionWithDnsLookupFunction(); |
| 39 virtual ~SocketExtensionWithDnsLookupFunction(); | 45 virtual ~SocketExtensionWithDnsLookupFunction(); |
| 40 | 46 |
| 41 void StartDnsLookup(const std::string& hostname); | 47 void StartDnsLookup(const std::string& hostname); |
| 42 virtual void AfterDnsLookup(int lookup_result) = 0; | 48 virtual void AfterDnsLookup(int lookup_result) = 0; |
| 43 | 49 |
| 44 std::string resolved_address_; | 50 std::string resolved_address_; |
| 45 | 51 |
| 46 private: | 52 private: |
| 47 void OnDnsLookup(int resolve_result); | 53 void OnDnsLookup(int resolve_result); |
| 48 | 54 |
| 49 // This instance is widely available through BrowserProcess, but we need to | 55 // This instance is widely available through BrowserProcess, but we need to |
| 50 // acquire it on the UI thread and then use it on the IO thread, so we keep a | 56 // acquire it on the UI thread and then use it on the IO thread, so we keep a |
| 51 // plain pointer to it here as we move from thread to thread. | 57 // plain pointer to it here as we move from thread to thread. |
| 52 IOThread* io_thread_; | 58 IOThread* io_thread_; |
| 53 | 59 |
| 54 scoped_ptr<net::HostResolver::RequestHandle> request_handle_; | 60 scoped_ptr<net::HostResolver::RequestHandle> request_handle_; |
| 55 scoped_ptr<net::AddressList> addresses_; | 61 scoped_ptr<net::AddressList> addresses_; |
| 56 }; | 62 }; |
| 57 | 63 |
| 58 class SocketCreateFunction : public SocketExtensionFunction { | 64 class SocketCreateFunction : public SocketAsyncApiFunction { |
| 59 public: | 65 public: |
| 60 DECLARE_EXTENSION_FUNCTION_NAME("experimental.socket.create") | 66 DECLARE_EXTENSION_FUNCTION_NAME("experimental.socket.create") |
| 61 | 67 |
| 62 SocketCreateFunction(); | 68 SocketCreateFunction(); |
| 63 | 69 |
| 64 protected: | 70 protected: |
| 65 virtual ~SocketCreateFunction(); | 71 virtual ~SocketCreateFunction(); |
| 66 | 72 |
| 67 // AsyncAPIFunction: | 73 // AsyncApiFunction: |
| 68 virtual bool Prepare() OVERRIDE; | 74 virtual bool Prepare() OVERRIDE; |
| 69 virtual void Work() OVERRIDE; | 75 virtual void Work() OVERRIDE; |
| 70 | 76 |
| 71 private: | 77 private: |
| 72 enum SocketType { | 78 enum SocketType { |
| 73 kSocketTypeInvalid = -1, | 79 kSocketTypeInvalid = -1, |
| 74 kSocketTypeTCP, | 80 kSocketTypeTCP, |
| 75 kSocketTypeUDP | 81 kSocketTypeUDP |
| 76 }; | 82 }; |
| 77 | 83 |
| 78 scoped_ptr<api::experimental_socket::Create::Params> params_; | 84 scoped_ptr<api::experimental_socket::Create::Params> params_; |
| 79 SocketType socket_type_; | 85 SocketType socket_type_; |
| 80 int src_id_; | 86 int src_id_; |
| 81 APIResourceEventNotifier* event_notifier_; | 87 ApiResourceEventNotifier* event_notifier_; |
| 82 }; | 88 }; |
| 83 | 89 |
| 84 class SocketDestroyFunction : public SocketExtensionFunction { | 90 class SocketDestroyFunction : public SocketAsyncApiFunction { |
| 85 public: | 91 public: |
| 86 DECLARE_EXTENSION_FUNCTION_NAME("experimental.socket.destroy") | 92 DECLARE_EXTENSION_FUNCTION_NAME("experimental.socket.destroy") |
| 87 | 93 |
| 88 protected: | 94 protected: |
| 89 virtual ~SocketDestroyFunction() {} | 95 virtual ~SocketDestroyFunction() {} |
| 90 | 96 |
| 91 // AsyncAPIFunction: | 97 // AsyncApiFunction: |
| 92 virtual bool Prepare() OVERRIDE; | 98 virtual bool Prepare() OVERRIDE; |
| 93 virtual void Work() OVERRIDE; | 99 virtual void Work() OVERRIDE; |
| 94 | 100 |
| 95 private: | 101 private: |
| 96 int socket_id_; | 102 int socket_id_; |
| 97 }; | 103 }; |
| 98 | 104 |
| 99 class SocketConnectFunction : public SocketExtensionWithDnsLookupFunction { | 105 class SocketConnectFunction : public SocketExtensionWithDnsLookupFunction { |
| 100 public: | 106 public: |
| 101 DECLARE_EXTENSION_FUNCTION_NAME("experimental.socket.connect") | 107 DECLARE_EXTENSION_FUNCTION_NAME("experimental.socket.connect") |
| 102 | 108 |
| 103 SocketConnectFunction(); | 109 SocketConnectFunction(); |
| 104 | 110 |
| 105 protected: | 111 protected: |
| 106 virtual ~SocketConnectFunction(); | 112 virtual ~SocketConnectFunction(); |
| 107 | 113 |
| 108 // AsyncAPIFunction: | 114 // AsyncApiFunction: |
| 109 virtual bool Prepare() OVERRIDE; | 115 virtual bool Prepare() OVERRIDE; |
| 110 virtual void AsyncWorkStart() OVERRIDE; | 116 virtual void AsyncWorkStart() OVERRIDE; |
| 111 | 117 |
| 112 // SocketExtensionWithDnsLookupFunction: | 118 // SocketExtensionWithDnsLookupFunction: |
| 113 virtual void AfterDnsLookup(int lookup_result) OVERRIDE; | 119 virtual void AfterDnsLookup(int lookup_result) OVERRIDE; |
| 114 | 120 |
| 115 private: | 121 private: |
| 116 void StartConnect(); | 122 void StartConnect(); |
| 117 void OnConnect(int result); | 123 void OnConnect(int result); |
| 118 | 124 |
| 119 int socket_id_; | 125 int socket_id_; |
| 120 std::string hostname_; | 126 std::string hostname_; |
| 121 int port_; | 127 int port_; |
| 122 }; | 128 }; |
| 123 | 129 |
| 124 class SocketDisconnectFunction : public SocketExtensionFunction { | 130 class SocketDisconnectFunction : public SocketAsyncApiFunction { |
| 125 public: | 131 public: |
| 126 DECLARE_EXTENSION_FUNCTION_NAME("experimental.socket.disconnect") | 132 DECLARE_EXTENSION_FUNCTION_NAME("experimental.socket.disconnect") |
| 127 | 133 |
| 128 protected: | 134 protected: |
| 129 virtual ~SocketDisconnectFunction() {} | 135 virtual ~SocketDisconnectFunction() {} |
| 130 | 136 |
| 131 // AsyncAPIFunction: | 137 // AsyncApiFunction: |
| 132 virtual bool Prepare() OVERRIDE; | 138 virtual bool Prepare() OVERRIDE; |
| 133 virtual void Work() OVERRIDE; | 139 virtual void Work() OVERRIDE; |
| 134 | 140 |
| 135 private: | 141 private: |
| 136 int socket_id_; | 142 int socket_id_; |
| 137 }; | 143 }; |
| 138 | 144 |
| 139 class SocketBindFunction : public SocketExtensionFunction { | 145 class SocketBindFunction : public SocketAsyncApiFunction { |
| 140 public: | 146 public: |
| 141 DECLARE_EXTENSION_FUNCTION_NAME("experimental.socket.bind") | 147 DECLARE_EXTENSION_FUNCTION_NAME("experimental.socket.bind") |
| 142 | 148 |
| 143 protected: | 149 protected: |
| 144 virtual ~SocketBindFunction() {} | 150 virtual ~SocketBindFunction() {} |
| 145 | 151 |
| 146 // AsyncAPIFunction: | 152 // AsyncApiFunction: |
| 147 virtual bool Prepare() OVERRIDE; | 153 virtual bool Prepare() OVERRIDE; |
| 148 virtual void Work() OVERRIDE; | 154 virtual void Work() OVERRIDE; |
| 149 | 155 |
| 150 private: | 156 private: |
| 151 int socket_id_; | 157 int socket_id_; |
| 152 std::string address_; | 158 std::string address_; |
| 153 int port_; | 159 int port_; |
| 154 }; | 160 }; |
| 155 | 161 |
| 156 class SocketReadFunction : public SocketExtensionFunction { | 162 class SocketReadFunction : public SocketAsyncApiFunction { |
| 157 public: | 163 public: |
| 158 DECLARE_EXTENSION_FUNCTION_NAME("experimental.socket.read") | 164 DECLARE_EXTENSION_FUNCTION_NAME("experimental.socket.read") |
| 159 | 165 |
| 160 SocketReadFunction(); | 166 SocketReadFunction(); |
| 161 | 167 |
| 162 protected: | 168 protected: |
| 163 virtual ~SocketReadFunction(); | 169 virtual ~SocketReadFunction(); |
| 164 | 170 |
| 165 // AsyncAPIFunction: | 171 // AsyncApiFunction: |
| 166 virtual bool Prepare() OVERRIDE; | 172 virtual bool Prepare() OVERRIDE; |
| 167 virtual void AsyncWorkStart() OVERRIDE; | 173 virtual void AsyncWorkStart() OVERRIDE; |
| 168 void OnCompleted(int result, scoped_refptr<net::IOBuffer> io_buffer); | 174 void OnCompleted(int result, scoped_refptr<net::IOBuffer> io_buffer); |
| 169 | 175 |
| 170 private: | 176 private: |
| 171 scoped_ptr<api::experimental_socket::Read::Params> params_; | 177 scoped_ptr<api::experimental_socket::Read::Params> params_; |
| 172 }; | 178 }; |
| 173 | 179 |
| 174 class SocketWriteFunction : public SocketExtensionFunction { | 180 class SocketWriteFunction : public SocketAsyncApiFunction { |
| 175 public: | 181 public: |
| 176 DECLARE_EXTENSION_FUNCTION_NAME("experimental.socket.write") | 182 DECLARE_EXTENSION_FUNCTION_NAME("experimental.socket.write") |
| 177 | 183 |
| 178 SocketWriteFunction(); | 184 SocketWriteFunction(); |
| 179 | 185 |
| 180 protected: | 186 protected: |
| 181 virtual ~SocketWriteFunction(); | 187 virtual ~SocketWriteFunction(); |
| 182 | 188 |
| 183 // AsyncAPIFunction: | 189 // AsyncApiFunction: |
| 184 virtual bool Prepare() OVERRIDE; | 190 virtual bool Prepare() OVERRIDE; |
| 185 virtual void AsyncWorkStart() OVERRIDE; | 191 virtual void AsyncWorkStart() OVERRIDE; |
| 186 void OnCompleted(int result); | 192 void OnCompleted(int result); |
| 187 | 193 |
| 188 private: | 194 private: |
| 189 int socket_id_; | 195 int socket_id_; |
| 190 scoped_refptr<net::IOBuffer> io_buffer_; | 196 scoped_refptr<net::IOBuffer> io_buffer_; |
| 191 size_t io_buffer_size_; | 197 size_t io_buffer_size_; |
| 192 }; | 198 }; |
| 193 | 199 |
| 194 class SocketRecvFromFunction : public SocketExtensionFunction { | 200 class SocketRecvFromFunction : public SocketAsyncApiFunction { |
| 195 public: | 201 public: |
| 196 DECLARE_EXTENSION_FUNCTION_NAME("experimental.socket.recvFrom") | 202 DECLARE_EXTENSION_FUNCTION_NAME("experimental.socket.recvFrom") |
| 197 | 203 |
| 198 SocketRecvFromFunction(); | 204 SocketRecvFromFunction(); |
| 199 | 205 |
| 200 protected: | 206 protected: |
| 201 virtual ~SocketRecvFromFunction(); | 207 virtual ~SocketRecvFromFunction(); |
| 202 | 208 |
| 203 // AsyncAPIFunction | 209 // AsyncApiFunction |
| 204 virtual bool Prepare() OVERRIDE; | 210 virtual bool Prepare() OVERRIDE; |
| 205 virtual void AsyncWorkStart() OVERRIDE; | 211 virtual void AsyncWorkStart() OVERRIDE; |
| 206 void OnCompleted(int result, | 212 void OnCompleted(int result, |
| 207 scoped_refptr<net::IOBuffer> io_buffer, | 213 scoped_refptr<net::IOBuffer> io_buffer, |
| 208 const std::string& address, | 214 const std::string& address, |
| 209 int port); | 215 int port); |
| 210 | 216 |
| 211 private: | 217 private: |
| 212 scoped_ptr<api::experimental_socket::RecvFrom::Params> params_; | 218 scoped_ptr<api::experimental_socket::RecvFrom::Params> params_; |
| 213 }; | 219 }; |
| 214 | 220 |
| 215 class SocketSendToFunction : public SocketExtensionWithDnsLookupFunction { | 221 class SocketSendToFunction : public SocketExtensionWithDnsLookupFunction { |
| 216 public: | 222 public: |
| 217 DECLARE_EXTENSION_FUNCTION_NAME("experimental.socket.sendTo") | 223 DECLARE_EXTENSION_FUNCTION_NAME("experimental.socket.sendTo") |
| 218 | 224 |
| 219 SocketSendToFunction(); | 225 SocketSendToFunction(); |
| 220 | 226 |
| 221 protected: | 227 protected: |
| 222 virtual ~SocketSendToFunction(); | 228 virtual ~SocketSendToFunction(); |
| 223 | 229 |
| 224 // AsyncAPIFunction: | 230 // AsyncApiFunction: |
| 225 virtual bool Prepare() OVERRIDE; | 231 virtual bool Prepare() OVERRIDE; |
| 226 virtual void AsyncWorkStart() OVERRIDE; | 232 virtual void AsyncWorkStart() OVERRIDE; |
| 227 void OnCompleted(int result); | 233 void OnCompleted(int result); |
| 228 | 234 |
| 229 // SocketExtensionWithDnsLookupFunction: | 235 // SocketExtensionWithDnsLookupFunction: |
| 230 virtual void AfterDnsLookup(int lookup_result) OVERRIDE; | 236 virtual void AfterDnsLookup(int lookup_result) OVERRIDE; |
| 231 | 237 |
| 232 private: | 238 private: |
| 233 void StartSendTo(); | 239 void StartSendTo(); |
| 234 | 240 |
| 235 int socket_id_; | 241 int socket_id_; |
| 236 scoped_refptr<net::IOBuffer> io_buffer_; | 242 scoped_refptr<net::IOBuffer> io_buffer_; |
| 237 size_t io_buffer_size_; | 243 size_t io_buffer_size_; |
| 238 std::string hostname_; | 244 std::string hostname_; |
| 239 int port_; | 245 int port_; |
| 240 }; | 246 }; |
| 241 | 247 |
| 242 class SocketSetKeepAliveFunction : public SocketExtensionFunction { | 248 class SocketSetKeepAliveFunction : public SocketAsyncApiFunction { |
| 243 public: | 249 public: |
| 244 DECLARE_EXTENSION_FUNCTION_NAME("experimental.socket.setKeepAlive") | 250 DECLARE_EXTENSION_FUNCTION_NAME("experimental.socket.setKeepAlive") |
| 245 | 251 |
| 246 SocketSetKeepAliveFunction(); | 252 SocketSetKeepAliveFunction(); |
| 247 | 253 |
| 248 protected: | 254 protected: |
| 249 virtual ~SocketSetKeepAliveFunction(); | 255 virtual ~SocketSetKeepAliveFunction(); |
| 250 | 256 |
| 251 // AsyncAPIFunction: | 257 // AsyncApiFunction: |
| 252 virtual bool Prepare() OVERRIDE; | 258 virtual bool Prepare() OVERRIDE; |
| 253 virtual void Work() OVERRIDE; | 259 virtual void Work() OVERRIDE; |
| 254 | 260 |
| 255 private: | 261 private: |
| 256 scoped_ptr<api::experimental_socket::SetKeepAlive::Params> params_; | 262 scoped_ptr<api::experimental_socket::SetKeepAlive::Params> params_; |
| 257 }; | 263 }; |
| 258 | 264 |
| 259 class SocketSetNoDelayFunction : public SocketExtensionFunction { | 265 class SocketSetNoDelayFunction : public SocketAsyncApiFunction { |
| 260 public: | 266 public: |
| 261 DECLARE_EXTENSION_FUNCTION_NAME("experimental.socket.setNoDelay") | 267 DECLARE_EXTENSION_FUNCTION_NAME("experimental.socket.setNoDelay") |
| 262 | 268 |
| 263 SocketSetNoDelayFunction(); | 269 SocketSetNoDelayFunction(); |
| 264 | 270 |
| 265 protected: | 271 protected: |
| 266 virtual ~SocketSetNoDelayFunction(); | 272 virtual ~SocketSetNoDelayFunction(); |
| 267 | 273 |
| 268 // AsyncAPIFunction: | 274 // AsyncApiFunction: |
| 269 virtual bool Prepare() OVERRIDE; | 275 virtual bool Prepare() OVERRIDE; |
| 270 virtual void Work() OVERRIDE; | 276 virtual void Work() OVERRIDE; |
| 271 | 277 |
| 272 private: | 278 private: |
| 273 scoped_ptr<api::experimental_socket::SetNoDelay::Params> params_; | 279 scoped_ptr<api::experimental_socket::SetNoDelay::Params> params_; |
| 274 }; | 280 }; |
| 275 | 281 |
| 276 } // namespace extensions | 282 } // namespace extensions |
| 277 | 283 |
| 278 #endif // CHROME_BROWSER_EXTENSIONS_API_SOCKET_SOCKET_API_H_ | 284 #endif // CHROME_BROWSER_EXTENSIONS_API_SOCKET_SOCKET_API_H_ |
| OLD | NEW |