Update V8 to version 4.5.107.
[chromium-blink-merge.git] / android_webview / browser / net / android_stream_reader_url_request_job.h
blob903ba61c2850595372d110baaf09118c29fef364
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_
8 #include <string>
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 {
20 class InputStream;
21 class InputStreamReader;
24 namespace base {
25 class TaskRunner;
28 namespace net {
29 class HttpResponseHeaders;
30 class HttpResponseInfo;
31 class URLRequest;
34 class InputStreamReaderWrapper;
36 // A request job that reads data from a Java InputStream.
37 class AndroidStreamReaderURLRequestJob : public net::URLRequestJob {
38 public:
40 * We use a delegate so that we can share code for this job in slightly
41 * different contexts.
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;
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;
58 virtual bool GetMimeType(
59 JNIEnv* env,
60 net::URLRequest* request,
61 android_webview::InputStream* stream,
62 std::string* mime_type) = 0;
64 virtual bool GetCharset(
65 JNIEnv* env,
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);
81 // URLRequestJob:
82 void Start() override;
83 void Kill() 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;
91 protected:
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);
103 private:
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_