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 HttpResponseInfo
;
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 ~Delegate() {}
72 AndroidStreamReaderURLRequestJob(
73 net::URLRequest
* request
,
74 net::NetworkDelegate
* network_delegate
,
75 scoped_ptr
<Delegate
> delegate
);
78 virtual void Start() OVERRIDE
;
79 virtual void Kill() OVERRIDE
;
80 virtual bool ReadRawData(net::IOBuffer
* buf
,
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
;
91 virtual ~AndroidStreamReaderURLRequestJob();
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::WeakPtrFactory
<AndroidStreamReaderURLRequestJob
> weak_factory_
;
116 base::ThreadChecker thread_checker_
;
118 DISALLOW_COPY_AND_ASSIGN(AndroidStreamReaderURLRequestJob
);
121 #endif // ANDROID_WEBVIEW_NATIVE_ANDROID_STREAM_READER_URL_REQUEST_JOB_H_