Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(670)

Side by Side Diff: android_webview/browser/net/android_stream_reader_url_request_job.h

Issue 11363123: [android_webview] Don't block the IO thread when reading from an InputStream. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fix double-free Created 8 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef ANDROID_WEBVIEW_NATIVE_ANDROID_STREAM_READER_URL_REQUEST_JOB_H_ 5 #ifndef ANDROID_WEBVIEW_NATIVE_ANDROID_STREAM_READER_URL_REQUEST_JOB_H_
6 #define ANDROID_WEBVIEW_NATIVE_ANDROID_STREAM_READER_URL_REQUEST_JOB_H_ 6 #define ANDROID_WEBVIEW_NATIVE_ANDROID_STREAM_READER_URL_REQUEST_JOB_H_
7 7
8 #include "base/android/scoped_java_ref.h" 8 #include "base/android/scoped_java_ref.h"
9 #include "base/location.h"
9 #include "base/memory/scoped_ptr.h" 10 #include "base/memory/scoped_ptr.h"
10 #include "base/memory/weak_ptr.h" 11 #include "base/memory/weak_ptr.h"
11 #include "base/threading/non_thread_safe.h" 12 #include "base/threading/non_thread_safe.h"
12 #include "net/http/http_byte_range.h" 13 #include "net/http/http_byte_range.h"
13 #include "net/url_request/url_request_job.h" 14 #include "net/url_request/url_request_job.h"
14 15
16 namespace android_webview {
17 class InputStream;
18 class InputStreamReader;
19 }
20
21 namespace base {
22 class TaskRunner;
23 }
24
15 namespace net { 25 namespace net {
16 class URLRequest; 26 class URLRequest;
17 } 27 }
18 28
19 // A request job that reads data from a Java InputStream. 29 // A request job that reads data from a Java InputStream.
20 class AndroidStreamReaderURLRequestJob : public net::URLRequestJob { 30 class AndroidStreamReaderURLRequestJob : public net::URLRequestJob {
21 public: 31 public:
22 /* 32 /*
23 * We use a delegate so that we can share code for this job in slightly 33 * We use a delegate so that we can share code for this job in slightly
24 * different contexts. 34 * different contexts.
25 */ 35 */
26 class Delegate { 36 class Delegate {
27 public: 37 public:
28 virtual base::android::ScopedJavaLocalRef<jobject> OpenInputStream( 38 virtual scoped_ptr<android_webview::InputStream> OpenInputStream(
29 JNIEnv* env, 39 JNIEnv* env,
30 net::URLRequest* request) = 0; 40 net::URLRequest* request) = 0;
31 41
32 virtual bool GetMimeType( 42 virtual bool GetMimeType(
33 JNIEnv* env, 43 JNIEnv* env,
34 net::URLRequest* request, 44 net::URLRequest* request,
35 jobject stream, 45 const android_webview::InputStream& stream,
36 std::string* mime_type) = 0; 46 std::string* mime_type) = 0;
37 47
38 virtual bool GetCharset( 48 virtual bool GetCharset(
39 JNIEnv* env, 49 JNIEnv* env,
40 net::URLRequest* request, 50 net::URLRequest* request,
41 jobject stream, 51 const android_webview::InputStream& stream,
42 std::string* charset) = 0; 52 std::string* charset) = 0;
43 53
44 virtual ~Delegate() {} 54 virtual ~Delegate() {}
45 }; 55 };
46 56
47 AndroidStreamReaderURLRequestJob( 57 AndroidStreamReaderURLRequestJob(
48 net::URLRequest* request, 58 net::URLRequest* request,
49 net::NetworkDelegate* network_delegate, 59 net::NetworkDelegate* network_delegate,
50 scoped_ptr<Delegate> delegate); 60 scoped_ptr<Delegate> delegate);
51 61
52 // URLRequestJob: 62 // URLRequestJob:
53 virtual void Start() OVERRIDE; 63 virtual void Start() OVERRIDE;
64 virtual void Kill() OVERRIDE;
54 virtual bool ReadRawData(net::IOBuffer* buf, 65 virtual bool ReadRawData(net::IOBuffer* buf,
55 int buf_size, 66 int buf_size,
56 int* bytes_read) OVERRIDE; 67 int* bytes_read) OVERRIDE;
57 virtual void SetExtraRequestHeaders( 68 virtual void SetExtraRequestHeaders(
58 const net::HttpRequestHeaders& headers) OVERRIDE; 69 const net::HttpRequestHeaders& headers) OVERRIDE;
59 virtual bool GetMimeType(std::string* mime_type) const OVERRIDE; 70 virtual bool GetMimeType(std::string* mime_type) const OVERRIDE;
60 virtual bool GetCharset(std::string* charset) OVERRIDE; 71 virtual bool GetCharset(std::string* charset) OVERRIDE;
61 72
62 protected: 73 protected:
63 virtual ~AndroidStreamReaderURLRequestJob(); 74 virtual ~AndroidStreamReaderURLRequestJob();
64 75
76 // Gets the TaskRunner for the worker thread.
77 // Useful to override in tests.
78 virtual base::TaskRunner* GetWorkerThreadRunner();
79
80 // Creates an InputStreamReader instance.
81 virtual android_webview::InputStreamReader*
joth 2012/11/20 20:46:35 comment ownership is not returned. (I come from a
mkosiba (inactive) 2012/11/21 15:19:47 umm.. why do you think ownership is not returned?
joth 2012/11/21 21:11:00 I've become too used to see scoped_ptr to indicate
82 CreateStreamReader(android_webview::InputStream* stream);
83
65 private: 84 private:
66 // Verify the requested range against the stream size. 85 void StartAsync();
67 bool VerifyRequestedRange(JNIEnv* env);
68 86
69 // Skip to the first byte of the requested read range. 87 void OnReaderSeekCompleted(int content_size);
70 bool SkipToRequestedRange(JNIEnv* env); 88 void OnReaderReadCompleted(int bytes_read);
71
72 void StartAsync();
73 89
74 net::HttpByteRange byte_range_; 90 net::HttpByteRange byte_range_;
75 scoped_ptr<Delegate> delegate_; 91 scoped_ptr<Delegate> delegate_;
76 base::android::ScopedJavaGlobalRef<jobject> stream_; 92 scoped_refptr<android_webview::InputStreamReader> input_stream_reader_;
77 base::android::ScopedJavaGlobalRef<jbyteArray> buffer_; 93 scoped_ptr<android_webview::InputStream> stream_;
78 base::WeakPtrFactory<AndroidStreamReaderURLRequestJob> weak_factory_; 94 base::WeakPtrFactory<AndroidStreamReaderURLRequestJob> weak_factory_;
79 95
80 DISALLOW_COPY_AND_ASSIGN(AndroidStreamReaderURLRequestJob); 96 DISALLOW_COPY_AND_ASSIGN(AndroidStreamReaderURLRequestJob);
81 }; 97 };
82 98
83 bool RegisterAndroidStreamReaderUrlRequestJob(JNIEnv* env);
84
85 #endif // ANDROID_WEBVIEW_NATIVE_ANDROID_STREAM_READER_URL_REQUEST_JOB_H_ 99 #endif // ANDROID_WEBVIEW_NATIVE_ANDROID_STREAM_READER_URL_REQUEST_JOB_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698