Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / android_webview / browser / net / android_stream_reader_url_request_job.h
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 HttpResponseHeaders;
30 class HttpResponseInfo;
31 class URLRequest;
32 }
33
34 class InputStreamReaderWrapper;
35
36 // A request job that reads data from a Java InputStream.
37 class AndroidStreamReaderURLRequestJob : public net::URLRequestJob {
38  public:
39   /*
40    * We use a delegate so that we can share code for this job in slightly
41    * different contexts.
42    */
43   class Delegate {
44    public:
45     // This method is called from a worker thread, not from the IO thread.
46     virtual scoped_ptr<android_webview::InputStream> OpenInputStream(
47         JNIEnv* env,
48         const GURL& url) = 0;
49
50     // This method is called on the Job's thread if the result of calling
51     // OpenInputStream was null.
52     // Setting the |restart| parameter to true will cause the request to be
53     // restarted with a new job.
54     virtual void OnInputStreamOpenFailed(
55         net::URLRequest* request,
56         bool* restart) = 0;
57
58     virtual bool GetMimeType(
59         JNIEnv* env,
60         net::URLRequest* request,
61         android_webview::InputStream* stream,
62         std::string* mime_type) = 0;
63
64     virtual bool GetCharset(
65         JNIEnv* env,
66         net::URLRequest* request,
67         android_webview::InputStream* stream,
68         std::string* charset) = 0;
69
70     virtual void AppendResponseHeaders(JNIEnv* env,
71                                        net::HttpResponseHeaders* headers) = 0;
72
73     virtual ~Delegate() {}
74   };
75
76   AndroidStreamReaderURLRequestJob(
77       net::URLRequest* request,
78       net::NetworkDelegate* network_delegate,
79       scoped_ptr<Delegate> delegate);
80
81   // URLRequestJob:
82   virtual void Start() override;
83   virtual void Kill() override;
84   virtual bool ReadRawData(net::IOBuffer* buf,
85                            int buf_size,
86                            int* bytes_read) override;
87   virtual void SetExtraRequestHeaders(
88       const net::HttpRequestHeaders& headers) override;
89   virtual bool GetMimeType(std::string* mime_type) const override;
90   virtual bool GetCharset(std::string* charset) override;
91   virtual int GetResponseCode() const override;
92   virtual void GetResponseInfo(net::HttpResponseInfo* info) override;
93
94  protected:
95   virtual ~AndroidStreamReaderURLRequestJob();
96
97   // Gets the TaskRunner for the worker thread.
98   // Overridden in unittests.
99   virtual base::TaskRunner* GetWorkerThreadRunner();
100
101   // Creates an InputStreamReader instance.
102   // Overridden in unittests to return a mock.
103   virtual scoped_ptr<android_webview::InputStreamReader>
104       CreateStreamReader(android_webview::InputStream* stream);
105
106  private:
107   void HeadersComplete(int status_code, const std::string& status_text);
108
109   void OnInputStreamOpened(
110       scoped_ptr<Delegate> delegate,
111       scoped_ptr<android_webview::InputStream> input_stream);
112   void OnReaderSeekCompleted(int content_size);
113   void OnReaderReadCompleted(int bytes_read);
114
115   net::HttpByteRange byte_range_;
116   scoped_ptr<net::HttpResponseInfo> response_info_;
117   scoped_ptr<Delegate> delegate_;
118   scoped_refptr<InputStreamReaderWrapper> input_stream_reader_wrapper_;
119   base::ThreadChecker thread_checker_;
120
121   base::WeakPtrFactory<AndroidStreamReaderURLRequestJob> weak_factory_;
122
123   DISALLOW_COPY_AND_ASSIGN(AndroidStreamReaderURLRequestJob);
124 };
125
126 #endif  // ANDROID_WEBVIEW_NATIVE_ANDROID_STREAM_READER_URL_REQUEST_JOB_H_