• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #include "android_webview/browser/aw_web_resource_response.h"
6 
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"
11 
12 namespace android_webview {
13 
14 namespace {
15 
16 class StreamReaderJobDelegateImpl
17     : public AndroidStreamReaderURLRequestJob::Delegate {
18  public:
StreamReaderJobDelegateImpl(scoped_ptr<AwWebResourceResponse> aw_web_resource_response)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_);
23   }
24 
OpenInputStream(JNIEnv * env,const GURL & url)25   virtual scoped_ptr<InputStream> OpenInputStream(JNIEnv* env,
26                                                   const GURL& url) OVERRIDE {
27     return aw_web_resource_response_->GetInputStream(env).Pass();
28   }
29 
OnInputStreamOpenFailed(net::URLRequest * request,bool * restart)30   virtual void OnInputStreamOpenFailed(net::URLRequest* request,
31                                        bool* restart) OVERRIDE {
32     *restart = false;
33   }
34 
GetMimeType(JNIEnv * env,net::URLRequest * request,android_webview::InputStream * stream,std::string * mime_type)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);
40   }
41 
GetCharset(JNIEnv * env,net::URLRequest * request,android_webview::InputStream * stream,std::string * charset)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);
47   }
48 
AppendResponseHeaders(JNIEnv * env,net::HttpResponseHeaders * headers)49   virtual void AppendResponseHeaders(
50       JNIEnv* env,
51       net::HttpResponseHeaders* headers) OVERRIDE {
52     int status_code;
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);
61     }
62     aw_web_resource_response_->GetResponseHeaders(env, headers);
63   }
64 
65  private:
66   scoped_ptr<AwWebResourceResponse> aw_web_resource_response_;
67 };
68 
69 }  // namespace
70 
71 // static
CreateJobFor(scoped_ptr<AwWebResourceResponse> aw_web_resource_response,net::URLRequest * request,net::NetworkDelegate * network_delegate)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);
77   DCHECK(request);
78   DCHECK(network_delegate);
79 
80   return new AndroidStreamReaderURLRequestJob(
81       request,
82       network_delegate,
83       make_scoped_ptr(
84           new StreamReaderJobDelegateImpl(aw_web_resource_response.Pass()))
85           .PassAs<AndroidStreamReaderURLRequestJob::Delegate>());
86 }
87 
88 }  // namespace android_webview
89