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.
5 #ifndef ANDROID_WEBVIEW_NATIVE_ANDROID_STREAM_READER_URL_REQUEST_JOB_H_
6 #define ANDROID_WEBVIEW_NATIVE_ANDROID_STREAM_READER_URL_REQUEST_JOB_H_
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"
19 namespace android_webview
{
21 class InputStreamReader
;
29 class HttpResponseHeaders
;
30 class HttpResponseInfo
;
34 class InputStreamReaderWrapper
;
36 // A request job that reads data from a Java InputStream.
37 class AndroidStreamReaderURLRequestJob
: public net::URLRequestJob
{
40 * We use a delegate so that we can share code for this job in slightly
45 // This method is called from a worker thread, not from the IO thread.
46 virtual scoped_ptr
<android_webview::InputStream
> OpenInputStream(
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
,
58 virtual bool GetMimeType(
60 net::URLRequest
* request
,
61 android_webview::InputStream
* stream
,
62 std::string
* mime_type
) = 0;
64 virtual bool GetCharset(
66 net::URLRequest
* request
,
67 android_webview::InputStream
* stream
,
68 std::string
* charset
) = 0;
70 virtual void AppendResponseHeaders(JNIEnv
* env
,
71 net::HttpResponseHeaders
* headers
) = 0;
73 virtual ~Delegate() {}
76 AndroidStreamReaderURLRequestJob(
77 net::URLRequest
* request
,
78 net::NetworkDelegate
* network_delegate
,
79 scoped_ptr
<Delegate
> delegate
);
82 virtual void Start() OVERRIDE
;
83 virtual void Kill() OVERRIDE
;
84 virtual bool ReadRawData(net::IOBuffer
* buf
,
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
;
95 virtual ~AndroidStreamReaderURLRequestJob();
97 // Gets the TaskRunner for the worker thread.
98 // Overridden in unittests.
99 virtual base::TaskRunner
* GetWorkerThreadRunner();
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
);
107 void HeadersComplete(int status_code
, const std::string
& status_text
);
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
);
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::WeakPtrFactory
<AndroidStreamReaderURLRequestJob
> weak_factory_
;
120 base::ThreadChecker thread_checker_
;
122 DISALLOW_COPY_AND_ASSIGN(AndroidStreamReaderURLRequestJob
);
125 #endif // ANDROID_WEBVIEW_NATIVE_ANDROID_STREAM_READER_URL_REQUEST_JOB_H_