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 void Start() override
;
84 bool ReadRawData(net::IOBuffer
* buf
, int buf_size
, int* bytes_read
) override
;
85 void SetExtraRequestHeaders(const net::HttpRequestHeaders
& headers
) override
;
86 bool GetMimeType(std::string
* mime_type
) const override
;
87 bool GetCharset(std::string
* charset
) override
;
88 int GetResponseCode() const override
;
89 void GetResponseInfo(net::HttpResponseInfo
* info
) override
;
92 ~AndroidStreamReaderURLRequestJob() override
;
94 // Gets the TaskRunner for the worker thread.
95 // Overridden in unittests.
96 virtual base::TaskRunner
* GetWorkerThreadRunner();
98 // Creates an InputStreamReader instance.
99 // Overridden in unittests to return a mock.
100 virtual scoped_ptr
<android_webview::InputStreamReader
>
101 CreateStreamReader(android_webview::InputStream
* stream
);
104 void HeadersComplete(int status_code
, const std::string
& status_text
);
106 void OnInputStreamOpened(
107 scoped_ptr
<Delegate
> delegate
,
108 scoped_ptr
<android_webview::InputStream
> input_stream
);
109 void OnReaderSeekCompleted(int content_size
);
110 void OnReaderReadCompleted(int bytes_read
);
112 net::HttpByteRange byte_range_
;
113 scoped_ptr
<net::HttpResponseInfo
> response_info_
;
114 scoped_ptr
<Delegate
> delegate_
;
115 scoped_refptr
<InputStreamReaderWrapper
> input_stream_reader_wrapper_
;
116 base::ThreadChecker thread_checker_
;
118 base::WeakPtrFactory
<AndroidStreamReaderURLRequestJob
> weak_factory_
;
120 DISALLOW_COPY_AND_ASSIGN(AndroidStreamReaderURLRequestJob
);
123 #endif // ANDROID_WEBVIEW_NATIVE_ANDROID_STREAM_READER_URL_REQUEST_JOB_H_