Home | History | Annotate | Download | only in net
      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 ANDROID_WEBVIEW_NATIVE_ANDROID_STREAM_READER_URL_REQUEST_JOB_H_
      6 #define ANDROID_WEBVIEW_NATIVE_ANDROID_STREAM_READER_URL_REQUEST_JOB_H_
      7 
      8 #include <string>
      9 
     10 #include "base/android/scoped_java_ref.h"
     11 #include "base/location.h"
     12 #include "base/memory/ref_counted.h"
     13 #include "base/memory/scoped_ptr.h"
     14 #include "base/memory/weak_ptr.h"
     15 #include "base/threading/thread_checker.h"
     16 #include "net/http/http_byte_range.h"
     17 #include "net/url_request/url_request_job.h"
     18 
     19 namespace android_webview {
     20 class InputStream;
     21 class InputStreamReader;
     22 }
     23 
     24 namespace base {
     25 class TaskRunner;
     26 }
     27 
     28 namespace net {
     29 class HttpResponseInfo;
     30 class URLRequest;
     31 }
     32 
     33 class InputStreamReaderWrapper;
     34 
     35 // A request job that reads data from a Java InputStream.
     36 class AndroidStreamReaderURLRequestJob : public net::URLRequestJob {
     37  public:
     38   /*
     39    * We use a delegate so that we can share code for this job in slightly
     40    * different contexts.
     41    */
     42   class Delegate {
     43    public:
     44     // This method is called from a worker thread, not from the IO thread.
     45     virtual scoped_ptr<android_webview::InputStream> OpenInputStream(
     46         JNIEnv* env,
     47         const GURL& url) = 0;
     48 
     49     // This method is called on the Job's thread if the result of calling
     50     // OpenInputStream was null.
     51     // Setting the |restart| parameter to true will cause the request to be
     52     // restarted with a new job.
     53     virtual void OnInputStreamOpenFailed(
     54         net::URLRequest* request,
     55         bool* restart) = 0;
     56 
     57     virtual bool GetMimeType(
     58         JNIEnv* env,
     59         net::URLRequest* request,
     60         android_webview::InputStream* stream,
     61         std::string* mime_type) = 0;
     62 
     63     virtual bool GetCharset(
     64         JNIEnv* env,
     65         net::URLRequest* request,
     66         android_webview::InputStream* stream,
     67         std::string* charset) = 0;
     68 
     69     virtual ~Delegate() {}
     70   };
     71 
     72   AndroidStreamReaderURLRequestJob(
     73       net::URLRequest* request,
     74       net::NetworkDelegate* network_delegate,
     75       scoped_ptr<Delegate> delegate);
     76 
     77   // URLRequestJob:
     78   virtual void Start() OVERRIDE;
     79   virtual void Kill() OVERRIDE;
     80   virtual bool ReadRawData(net::IOBuffer* buf,
     81                            int buf_size,
     82                            int* bytes_read) OVERRIDE;
     83   virtual void SetExtraRequestHeaders(
     84       const net::HttpRequestHeaders& headers) OVERRIDE;
     85   virtual bool GetMimeType(std::string* mime_type) const OVERRIDE;
     86   virtual bool GetCharset(std::string* charset) OVERRIDE;
     87   virtual int GetResponseCode() const OVERRIDE;
     88   virtual void GetResponseInfo(net::HttpResponseInfo* info) OVERRIDE;
     89 
     90  protected:
     91   virtual ~AndroidStreamReaderURLRequestJob();
     92 
     93   // Gets the TaskRunner for the worker thread.
     94   // Overridden in unittests.
     95   virtual base::TaskRunner* GetWorkerThreadRunner();
     96 
     97   // Creates an InputStreamReader instance.
     98   // Overridden in unittests to return a mock.
     99   virtual scoped_ptr<android_webview::InputStreamReader>
    100       CreateStreamReader(android_webview::InputStream* stream);
    101 
    102  private:
    103   void HeadersComplete(int status_code, const std::string& status_text);
    104 
    105   void OnInputStreamOpened(
    106       scoped_ptr<Delegate> delegate,
    107       scoped_ptr<android_webview::InputStream> input_stream);
    108   void OnReaderSeekCompleted(int content_size);
    109   void OnReaderReadCompleted(int bytes_read);
    110 
    111   net::HttpByteRange byte_range_;
    112   scoped_ptr<net::HttpResponseInfo> response_info_;
    113   scoped_ptr<Delegate> delegate_;
    114   scoped_refptr<InputStreamReaderWrapper> input_stream_reader_wrapper_;
    115   base::WeakPtrFactory<AndroidStreamReaderURLRequestJob> weak_factory_;
    116   base::ThreadChecker thread_checker_;
    117 
    118   DISALLOW_COPY_AND_ASSIGN(AndroidStreamReaderURLRequestJob);
    119 };
    120 
    121 #endif  // ANDROID_WEBVIEW_NATIVE_ANDROID_STREAM_READER_URL_REQUEST_JOB_H_
    122