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"
24 class HttpResponseHeaders
;
25 class HttpResponseInfo
;
29 namespace android_webview
{
32 class InputStreamReader
;
33 class InputStreamReaderWrapper
;
35 // A request job that reads data from a Java InputStream.
36 class AndroidStreamReaderURLRequestJob
: public net::URLRequestJob
{
39 * We use a delegate so that we can share code for this job in slightly
44 // This method is called from a worker thread, not from the IO thread.
45 virtual scoped_ptr
<android_webview::InputStream
> OpenInputStream(
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
,
57 virtual bool GetMimeType(
59 net::URLRequest
* request
,
60 android_webview::InputStream
* stream
,
61 std::string
* mime_type
) = 0;
63 virtual bool GetCharset(
65 net::URLRequest
* request
,
66 android_webview::InputStream
* stream
,
67 std::string
* charset
) = 0;
69 virtual void AppendResponseHeaders(JNIEnv
* env
,
70 net::HttpResponseHeaders
* headers
) = 0;
72 virtual ~Delegate() {}
75 AndroidStreamReaderURLRequestJob(
76 net::URLRequest
* request
,
77 net::NetworkDelegate
* network_delegate
,
78 scoped_ptr
<Delegate
> delegate
);
81 void Start() override
;
83 bool ReadRawData(net::IOBuffer
* buf
, int buf_size
, int* bytes_read
) override
;
84 void SetExtraRequestHeaders(const net::HttpRequestHeaders
& headers
) override
;
85 bool GetMimeType(std::string
* mime_type
) const override
;
86 bool GetCharset(std::string
* charset
) override
;
87 int GetResponseCode() const override
;
88 void GetResponseInfo(net::HttpResponseInfo
* info
) override
;
91 ~AndroidStreamReaderURLRequestJob() override
;
93 // Gets the TaskRunner for the worker thread.
94 // Overridden in unittests.
95 virtual base::TaskRunner
* GetWorkerThreadRunner();
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
);
103 void HeadersComplete(int status_code
, const std::string
& status_text
);
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
);
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::ThreadChecker thread_checker_
;
117 base::WeakPtrFactory
<AndroidStreamReaderURLRequestJob
> weak_factory_
;
119 DISALLOW_COPY_AND_ASSIGN(AndroidStreamReaderURLRequestJob
);
122 } // namespace android_webview
124 #endif // ANDROID_WEBVIEW_NATIVE_ANDROID_STREAM_READER_URL_REQUEST_JOB_H_