Home | History | Annotate | Download | only in socket_stream
      1 // Copyright (c) 2011 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 NET_SOCKET_STREAM_SOCKET_STREAM_JOB_H_
      6 #define NET_SOCKET_STREAM_SOCKET_STREAM_JOB_H_
      7 #pragma once
      8 
      9 #include <string>
     10 
     11 #include "base/memory/ref_counted.h"
     12 #include "base/string16.h"
     13 #include "net/socket_stream/socket_stream.h"
     14 
     15 class GURL;
     16 
     17 namespace net {
     18 
     19 // SocketStreamJob represents full-duplex communication over SocketStream.
     20 // If a protocol (e.g. WebSocket protocol) needs to inspect/modify data
     21 // over SocketStream, you can implement protocol specific job (e.g.
     22 // WebSocketJob) to do some work on data over SocketStream.
     23 // Registers the protocol specific SocketStreamJob by RegisterProtocolFactory
     24 // and call CreateSocketStreamJob to create SocketStreamJob for the URL.
     25 class SocketStreamJob : public base::RefCountedThreadSafe<SocketStreamJob> {
     26  public:
     27   // Callback function implemented by protocol handlers to create new jobs.
     28   typedef SocketStreamJob* (ProtocolFactory)(const GURL& url,
     29                                              SocketStream::Delegate* delegate);
     30 
     31   static ProtocolFactory* RegisterProtocolFactory(const std::string& scheme,
     32                                                   ProtocolFactory* factory);
     33 
     34   static SocketStreamJob* CreateSocketStreamJob(
     35       const GURL& url,
     36       SocketStream::Delegate* delegate,
     37       const URLRequestContext& context);
     38 
     39   SocketStreamJob();
     40   void InitSocketStream(SocketStream* socket) {
     41     socket_ = socket;
     42   }
     43 
     44   virtual SocketStream::UserData* GetUserData(const void* key) const;
     45   virtual void SetUserData(const void* key, SocketStream::UserData* data);
     46 
     47   URLRequestContext* context() const {
     48     return socket_->context();
     49   }
     50   void set_context(URLRequestContext* context) {
     51     socket_->set_context(context);
     52   }
     53 
     54   virtual void Connect();
     55 
     56   virtual bool SendData(const char* data, int len);
     57 
     58   virtual void Close();
     59 
     60   virtual void RestartWithAuth(const string16& username,
     61                                const string16& password);
     62 
     63   virtual void DetachDelegate();
     64 
     65  protected:
     66   friend class WebSocketJobTest;
     67   friend class base::RefCountedThreadSafe<SocketStreamJob>;
     68   virtual ~SocketStreamJob();
     69 
     70   scoped_refptr<SocketStream> socket_;
     71 
     72   DISALLOW_COPY_AND_ASSIGN(SocketStreamJob);
     73 };
     74 
     75 }  // namespace net
     76 
     77 #endif  // NET_SOCKET_STREAM_SOCKET_STREAM_JOB_H_
     78