Home | History | Annotate | Download | only in socket
      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
      3 // found in the LICENSE file.
      4 
      5 #ifndef CHROME_BROWSER_EXTENSIONS_API_SOCKET_SOCKET_API_H_
      6 #define CHROME_BROWSER_EXTENSIONS_API_SOCKET_SOCKET_API_H_
      7 
      8 #include "base/gtest_prod_util.h"
      9 #include "base/memory/ref_counted.h"
     10 #include "chrome/browser/extensions/api/api_function.h"
     11 #include "chrome/browser/extensions/api/api_resource_manager.h"
     12 #include "chrome/browser/extensions/extension_function.h"
     13 #include "chrome/common/extensions/api/socket.h"
     14 #include "net/base/address_list.h"
     15 #include "net/dns/host_resolver.h"
     16 #include "net/socket/tcp_client_socket.h"
     17 
     18 #include <string>
     19 
     20 class IOThread;
     21 
     22 namespace net {
     23 class IOBuffer;
     24 }
     25 
     26 namespace extensions {
     27 
     28 class Socket;
     29 
     30 class SocketAsyncApiFunction : public AsyncApiFunction {
     31  public:
     32   SocketAsyncApiFunction();
     33 
     34  protected:
     35   virtual ~SocketAsyncApiFunction();
     36 
     37   // AsyncApiFunction:
     38   virtual bool PrePrepare() OVERRIDE;
     39   virtual bool Respond() OVERRIDE;
     40 
     41   Socket* GetSocket(int api_resource_id);
     42   void RemoveSocket(int api_resource_id);
     43 
     44   ApiResourceManager<Socket>* manager_;
     45 };
     46 
     47 class SocketExtensionWithDnsLookupFunction : public SocketAsyncApiFunction {
     48  protected:
     49   SocketExtensionWithDnsLookupFunction();
     50   virtual ~SocketExtensionWithDnsLookupFunction();
     51 
     52   void StartDnsLookup(const std::string& hostname);
     53   virtual void AfterDnsLookup(int lookup_result) = 0;
     54 
     55   std::string resolved_address_;
     56 
     57  private:
     58   void OnDnsLookup(int resolve_result);
     59 
     60   // This instance is widely available through BrowserProcess, but we need to
     61   // acquire it on the UI thread and then use it on the IO thread, so we keep a
     62   // plain pointer to it here as we move from thread to thread.
     63   IOThread* io_thread_;
     64 
     65   scoped_ptr<net::HostResolver::RequestHandle> request_handle_;
     66   scoped_ptr<net::AddressList> addresses_;
     67 };
     68 
     69 class SocketCreateFunction : public SocketAsyncApiFunction {
     70  public:
     71   DECLARE_EXTENSION_FUNCTION("socket.create", SOCKET_CREATE)
     72 
     73   SocketCreateFunction();
     74 
     75  protected:
     76   virtual ~SocketCreateFunction();
     77 
     78   // AsyncApiFunction:
     79   virtual bool Prepare() OVERRIDE;
     80   virtual void Work() OVERRIDE;
     81 
     82  private:
     83   FRIEND_TEST_ALL_PREFIXES(SocketUnitTest, Create);
     84   enum SocketType {
     85     kSocketTypeInvalid = -1,
     86     kSocketTypeTCP,
     87     kSocketTypeUDP
     88   };
     89 
     90   scoped_ptr<api::socket::Create::Params> params_;
     91   SocketType socket_type_;
     92 };
     93 
     94 class SocketDestroyFunction : public SocketAsyncApiFunction {
     95  public:
     96   DECLARE_EXTENSION_FUNCTION("socket.destroy", SOCKET_DESTROY)
     97 
     98  protected:
     99   virtual ~SocketDestroyFunction() {}
    100 
    101   // AsyncApiFunction:
    102   virtual bool Prepare() OVERRIDE;
    103   virtual void Work() OVERRIDE;
    104 
    105  private:
    106   int socket_id_;
    107 };
    108 
    109 class SocketConnectFunction : public SocketExtensionWithDnsLookupFunction {
    110  public:
    111   DECLARE_EXTENSION_FUNCTION("socket.connect", SOCKET_CONNECT)
    112 
    113   SocketConnectFunction();
    114 
    115  protected:
    116   virtual ~SocketConnectFunction();
    117 
    118   // AsyncApiFunction:
    119   virtual bool Prepare() OVERRIDE;
    120   virtual void AsyncWorkStart() OVERRIDE;
    121 
    122   // SocketExtensionWithDnsLookupFunction:
    123   virtual void AfterDnsLookup(int lookup_result) OVERRIDE;
    124 
    125  private:
    126   void StartConnect();
    127   void OnConnect(int result);
    128 
    129   int socket_id_;
    130   std::string hostname_;
    131   int port_;
    132   Socket* socket_;
    133 };
    134 
    135 class SocketDisconnectFunction : public SocketAsyncApiFunction {
    136  public:
    137   DECLARE_EXTENSION_FUNCTION("socket.disconnect", SOCKET_DISCONNECT)
    138 
    139  protected:
    140   virtual ~SocketDisconnectFunction() {}
    141 
    142   // AsyncApiFunction:
    143   virtual bool Prepare() OVERRIDE;
    144   virtual void Work() OVERRIDE;
    145 
    146  private:
    147   int socket_id_;
    148 };
    149 
    150 class SocketBindFunction : public SocketAsyncApiFunction {
    151  public:
    152   DECLARE_EXTENSION_FUNCTION("socket.bind", SOCKET_BIND)
    153 
    154  protected:
    155   virtual ~SocketBindFunction() {}
    156 
    157   // AsyncApiFunction:
    158   virtual bool Prepare() OVERRIDE;
    159   virtual void Work() OVERRIDE;
    160 
    161  private:
    162   int socket_id_;
    163   std::string address_;
    164   int port_;
    165 };
    166 
    167 class SocketListenFunction : public SocketAsyncApiFunction {
    168  public:
    169   DECLARE_EXTENSION_FUNCTION("socket.listen", SOCKET_LISTEN)
    170 
    171   SocketListenFunction();
    172 
    173  protected:
    174   virtual ~SocketListenFunction();
    175 
    176   // AsyncApiFunction:
    177   virtual bool Prepare() OVERRIDE;
    178   virtual void Work() OVERRIDE;
    179 
    180  private:
    181   scoped_ptr<api::socket::Listen::Params> params_;
    182 };
    183 
    184 class SocketAcceptFunction : public SocketAsyncApiFunction {
    185  public:
    186   DECLARE_EXTENSION_FUNCTION("socket.accept", SOCKET_ACCEPT)
    187 
    188   SocketAcceptFunction();
    189 
    190  protected:
    191   virtual ~SocketAcceptFunction();
    192 
    193   // AsyncApiFunction:
    194   virtual bool Prepare() OVERRIDE;
    195   virtual void AsyncWorkStart() OVERRIDE;
    196 
    197  private:
    198   void OnAccept(int result_code, net::TCPClientSocket *socket);
    199   scoped_ptr<api::socket::Accept::Params> params_;
    200 };
    201 
    202 class SocketReadFunction : public SocketAsyncApiFunction {
    203  public:
    204   DECLARE_EXTENSION_FUNCTION("socket.read", SOCKET_READ)
    205 
    206   SocketReadFunction();
    207 
    208  protected:
    209   virtual ~SocketReadFunction();
    210 
    211   // AsyncApiFunction:
    212   virtual bool Prepare() OVERRIDE;
    213   virtual void AsyncWorkStart() OVERRIDE;
    214   void OnCompleted(int result, scoped_refptr<net::IOBuffer> io_buffer);
    215 
    216  private:
    217   scoped_ptr<api::socket::Read::Params> params_;
    218 };
    219 
    220 class SocketWriteFunction : public SocketAsyncApiFunction {
    221  public:
    222   DECLARE_EXTENSION_FUNCTION("socket.write", SOCKET_WRITE)
    223 
    224   SocketWriteFunction();
    225 
    226  protected:
    227   virtual ~SocketWriteFunction();
    228 
    229   // AsyncApiFunction:
    230   virtual bool Prepare() OVERRIDE;
    231   virtual void AsyncWorkStart() OVERRIDE;
    232   void OnCompleted(int result);
    233 
    234  private:
    235   int socket_id_;
    236   scoped_refptr<net::IOBuffer> io_buffer_;
    237   size_t io_buffer_size_;
    238 };
    239 
    240 class SocketRecvFromFunction : public SocketAsyncApiFunction {
    241  public:
    242   DECLARE_EXTENSION_FUNCTION("socket.recvFrom", SOCKET_RECVFROM)
    243 
    244   SocketRecvFromFunction();
    245 
    246  protected:
    247   virtual ~SocketRecvFromFunction();
    248 
    249   // AsyncApiFunction
    250   virtual bool Prepare() OVERRIDE;
    251   virtual void AsyncWorkStart() OVERRIDE;
    252   void OnCompleted(int result,
    253                    scoped_refptr<net::IOBuffer> io_buffer,
    254                    const std::string& address,
    255                    int port);
    256 
    257  private:
    258   scoped_ptr<api::socket::RecvFrom::Params> params_;
    259 };
    260 
    261 class SocketSendToFunction : public SocketExtensionWithDnsLookupFunction {
    262  public:
    263   DECLARE_EXTENSION_FUNCTION("socket.sendTo", SOCKET_SENDTO)
    264 
    265   SocketSendToFunction();
    266 
    267  protected:
    268   virtual ~SocketSendToFunction();
    269 
    270   // AsyncApiFunction:
    271   virtual bool Prepare() OVERRIDE;
    272   virtual void AsyncWorkStart() OVERRIDE;
    273   void OnCompleted(int result);
    274 
    275   // SocketExtensionWithDnsLookupFunction:
    276   virtual void AfterDnsLookup(int lookup_result) OVERRIDE;
    277 
    278  private:
    279   void StartSendTo();
    280 
    281   int socket_id_;
    282   scoped_refptr<net::IOBuffer> io_buffer_;
    283   size_t io_buffer_size_;
    284   std::string hostname_;
    285   int port_;
    286   Socket* socket_;
    287 };
    288 
    289 class SocketSetKeepAliveFunction : public SocketAsyncApiFunction {
    290  public:
    291   DECLARE_EXTENSION_FUNCTION("socket.setKeepAlive", SOCKET_SETKEEPALIVE)
    292 
    293   SocketSetKeepAliveFunction();
    294 
    295  protected:
    296   virtual ~SocketSetKeepAliveFunction();
    297 
    298   // AsyncApiFunction:
    299   virtual bool Prepare() OVERRIDE;
    300   virtual void Work() OVERRIDE;
    301 
    302  private:
    303   scoped_ptr<api::socket::SetKeepAlive::Params> params_;
    304 };
    305 
    306 class SocketSetNoDelayFunction : public SocketAsyncApiFunction {
    307  public:
    308   DECLARE_EXTENSION_FUNCTION("socket.setNoDelay", SOCKET_SETNODELAY)
    309 
    310   SocketSetNoDelayFunction();
    311 
    312  protected:
    313   virtual ~SocketSetNoDelayFunction();
    314 
    315   // AsyncApiFunction:
    316   virtual bool Prepare() OVERRIDE;
    317   virtual void Work() OVERRIDE;
    318 
    319  private:
    320   scoped_ptr<api::socket::SetNoDelay::Params> params_;
    321 };
    322 
    323 class SocketGetInfoFunction : public SocketAsyncApiFunction {
    324  public:
    325   DECLARE_EXTENSION_FUNCTION("socket.getInfo", SOCKET_GETINFO)
    326 
    327   SocketGetInfoFunction();
    328 
    329  protected:
    330   virtual ~SocketGetInfoFunction();
    331 
    332   // AsyncApiFunction:
    333   virtual bool Prepare() OVERRIDE;
    334   virtual void Work() OVERRIDE;
    335 
    336  private:
    337   scoped_ptr<api::socket::GetInfo::Params> params_;
    338 };
    339 
    340 class SocketGetNetworkListFunction : public AsyncExtensionFunction {
    341  public:
    342   DECLARE_EXTENSION_FUNCTION("socket.getNetworkList", SOCKET_GETNETWORKLIST)
    343 
    344  protected:
    345   virtual ~SocketGetNetworkListFunction() {}
    346   virtual bool RunImpl() OVERRIDE;
    347 
    348  private:
    349   void GetNetworkListOnFileThread();
    350   void HandleGetNetworkListError();
    351   void SendResponseOnUIThread(const net::NetworkInterfaceList& interface_list);
    352 };
    353 
    354 class SocketJoinGroupFunction : public SocketAsyncApiFunction {
    355  public:
    356   DECLARE_EXTENSION_FUNCTION("socket.joinGroup", SOCKET_MULTICAST_JOIN_GROUP)
    357 
    358   SocketJoinGroupFunction();
    359 
    360  protected:
    361   virtual ~SocketJoinGroupFunction();
    362 
    363   // AsyncApiFunction
    364   virtual bool Prepare() OVERRIDE;
    365   virtual void Work() OVERRIDE;
    366 
    367  private:
    368   scoped_ptr<api::socket::JoinGroup::Params> params_;
    369 };
    370 
    371 class SocketLeaveGroupFunction : public SocketAsyncApiFunction {
    372  public:
    373   DECLARE_EXTENSION_FUNCTION("socket.leaveGroup", SOCKET_MULTICAST_LEAVE_GROUP)
    374 
    375   SocketLeaveGroupFunction();
    376 
    377  protected:
    378   virtual ~SocketLeaveGroupFunction();
    379 
    380   // AsyncApiFunction
    381   virtual bool Prepare() OVERRIDE;
    382   virtual void Work() OVERRIDE;
    383 
    384  private:
    385   scoped_ptr<api::socket::LeaveGroup::Params> params_;
    386 };
    387 
    388 class SocketSetMulticastTimeToLiveFunction : public SocketAsyncApiFunction {
    389  public:
    390   DECLARE_EXTENSION_FUNCTION("socket.setMulticastTimeToLive",
    391                              SOCKET_MULTICAST_SET_TIME_TO_LIVE)
    392 
    393   SocketSetMulticastTimeToLiveFunction();
    394 
    395  protected:
    396   virtual ~SocketSetMulticastTimeToLiveFunction();
    397 
    398   // AsyncApiFunction
    399   virtual bool Prepare() OVERRIDE;
    400   virtual void Work() OVERRIDE;
    401 
    402  private:
    403   scoped_ptr<api::socket::SetMulticastTimeToLive::Params> params_;
    404 };
    405 
    406 class SocketSetMulticastLoopbackModeFunction : public SocketAsyncApiFunction {
    407  public:
    408   DECLARE_EXTENSION_FUNCTION("socket.setMulticastLoopbackMode",
    409                              SOCKET_MULTICAST_SET_LOOPBACK_MODE)
    410 
    411   SocketSetMulticastLoopbackModeFunction();
    412 
    413  protected:
    414   virtual ~SocketSetMulticastLoopbackModeFunction();
    415 
    416   // AsyncApiFunction
    417   virtual bool Prepare() OVERRIDE;
    418   virtual void Work() OVERRIDE;
    419 
    420  private:
    421   scoped_ptr<api::socket::SetMulticastLoopbackMode::Params> params_;
    422 };
    423 
    424 class SocketGetJoinedGroupsFunction : public SocketAsyncApiFunction {
    425  public:
    426   DECLARE_EXTENSION_FUNCTION("socket.getJoinedGroups",
    427                              SOCKET_MULTICAST_GET_JOINED_GROUPS)
    428 
    429   SocketGetJoinedGroupsFunction();
    430 
    431  protected:
    432   virtual ~SocketGetJoinedGroupsFunction();
    433 
    434   // AsyncApiFunction
    435   virtual bool Prepare() OVERRIDE;
    436   virtual void Work() OVERRIDE;
    437 
    438  private:
    439   scoped_ptr<api::socket::GetJoinedGroups::Params> params_;
    440 };
    441 }  // namespace extensions
    442 
    443 #endif  // CHROME_BROWSER_EXTENSIONS_API_SOCKET_SOCKET_API_H_
    444