1 // Copyright 2014 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 #include "android_webview/browser/aw_web_resource_response.h"
7 #include "android_webview/browser/input_stream.h"
8 #include "android_webview/browser/net/android_stream_reader_url_request_job.h"
9 #include "base/strings/string_number_conversions.h"
10 #include "net/http/http_response_headers.h"
12 namespace android_webview
{
16 class StreamReaderJobDelegateImpl
17 : public AndroidStreamReaderURLRequestJob::Delegate
{
19 StreamReaderJobDelegateImpl(
20 scoped_ptr
<AwWebResourceResponse
> aw_web_resource_response
)
21 : aw_web_resource_response_(aw_web_resource_response
.Pass()) {
22 DCHECK(aw_web_resource_response_
);
25 virtual scoped_ptr
<InputStream
> OpenInputStream(JNIEnv
* env
,
26 const GURL
& url
) OVERRIDE
{
27 return aw_web_resource_response_
->GetInputStream(env
).Pass();
30 virtual void OnInputStreamOpenFailed(net::URLRequest
* request
,
31 bool* restart
) OVERRIDE
{
35 virtual bool GetMimeType(JNIEnv
* env
,
36 net::URLRequest
* request
,
37 android_webview::InputStream
* stream
,
38 std::string
* mime_type
) OVERRIDE
{
39 return aw_web_resource_response_
->GetMimeType(env
, mime_type
);
42 virtual bool GetCharset(JNIEnv
* env
,
43 net::URLRequest
* request
,
44 android_webview::InputStream
* stream
,
45 std::string
* charset
) OVERRIDE
{
46 return aw_web_resource_response_
->GetCharset(env
, charset
);
49 virtual void AppendResponseHeaders(
51 net::HttpResponseHeaders
* headers
) OVERRIDE
{
53 std::string reason_phrase
;
54 if (aw_web_resource_response_
->GetStatusInfo(
55 env
, &status_code
, &reason_phrase
)) {
56 std::string
status_line("HTTP/1.1 ");
57 status_line
.append(base::IntToString(status_code
));
58 status_line
.append(" ");
59 status_line
.append(reason_phrase
);
60 headers
->ReplaceStatusLine(status_line
);
62 aw_web_resource_response_
->GetResponseHeaders(env
, headers
);
66 scoped_ptr
<AwWebResourceResponse
> aw_web_resource_response_
;
72 net::URLRequestJob
* AwWebResourceResponse::CreateJobFor(
73 scoped_ptr
<AwWebResourceResponse
> aw_web_resource_response
,
74 net::URLRequest
* request
,
75 net::NetworkDelegate
* network_delegate
) {
76 DCHECK(aw_web_resource_response
);
78 DCHECK(network_delegate
);
80 return new AndroidStreamReaderURLRequestJob(
84 new StreamReaderJobDelegateImpl(aw_web_resource_response
.Pass()))
85 .PassAs
<AndroidStreamReaderURLRequestJob::Delegate
>());
88 } // namespace android_webview