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 #include "webkit/plugins/ppapi/url_response_info_util.h"
7 #include "base/logging.h"
8 #include "third_party/WebKit/Source/Platform/chromium/public/WebHTTPHeaderVisitor.h"
9 #include "third_party/WebKit/Source/Platform/chromium/public/WebString.h"
10 #include "third_party/WebKit/Source/Platform/chromium/public/WebURL.h"
11 #include "third_party/WebKit/Source/Platform/chromium/public/WebURLResponse.h"
12 #include "webkit/base/file_path_string_conversions.h"
13 #include "webkit/plugins/ppapi/ppb_file_ref_impl.h"
14 #include "webkit/glue/webkit_glue.h"
16 using WebKit::WebHTTPHeaderVisitor
;
17 using WebKit::WebString
;
18 using WebKit::WebURLResponse
;
25 class HeaderFlattener
: public WebHTTPHeaderVisitor
{
27 const std::string
& buffer() const { return buffer_
; }
29 virtual void visitHeader(const WebString
& name
, const WebString
& value
) {
32 buffer_
.append(name
.utf8());
34 buffer_
.append(value
.utf8());
41 bool IsRedirect(int32_t status
) {
42 return status
>= 300 && status
<= 399;
47 ::ppapi::URLResponseInfoData
DataFromWebURLResponse(
48 PP_Instance pp_instance
,
49 const WebURLResponse
& response
) {
50 ::ppapi::URLResponseInfoData data
;
52 data
.url
= response
.url().spec();
53 data
.status_code
= response
.httpStatusCode();
54 data
.status_text
= response
.httpStatusText().utf8();
55 if (IsRedirect(data
.status_code
)) {
56 data
.redirect_url
= response
.httpHeaderField(
57 WebString::fromUTF8("Location")).utf8();
60 HeaderFlattener flattener
;
61 response
.visitHTTPHeaderFields(&flattener
);
62 data
.headers
= flattener
.buffer();
64 WebString file_path
= response
.downloadFilePath();
65 if (!file_path
.isEmpty()) {
66 scoped_refptr
<PPB_FileRef_Impl
> file_ref(
67 PPB_FileRef_Impl::CreateExternal(
69 webkit_base::WebStringToFilePath(file_path
),
71 data
.body_as_file_ref
= file_ref
->GetCreateInfo();
72 file_ref
->GetReference(); // The returned data has one ref for the plugin.