[M108 Aura Migration][NaCl][PPAPI] URLResponseInfo response code 0
[platform/framework/web/chromium-efl.git] / content / renderer / pepper / url_response_info_util.cc
1 // Copyright 2013 The Chromium Authors
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 "content/renderer/pepper/url_response_info_util.h"
6
7 #include <stdint.h>
8
9 #include "ppapi/shared_impl/url_response_info_data.h"
10 #include "third_party/blink/public/platform/web_http_header_visitor.h"
11 #include "third_party/blink/public/platform/web_string.h"
12 #include "third_party/blink/public/platform/web_url.h"
13 #include "third_party/blink/public/platform/web_url_response.h"
14
15 using blink::WebHTTPHeaderVisitor;
16 using blink::WebString;
17 using blink::WebURLResponse;
18
19 namespace content {
20
21 namespace {
22
23 class HeadersToString : public WebHTTPHeaderVisitor {
24  public:
25   HeadersToString() {}
26   ~HeadersToString() override {}
27
28   const std::string& buffer() const { return buffer_; }
29
30   void VisitHeader(const WebString& name, const WebString& value) override {
31     if (!buffer_.empty())
32       buffer_.append("\n");
33     buffer_.append(name.Utf8());
34     buffer_.append(": ");
35     buffer_.append(value.Utf8());
36   }
37
38  private:
39   std::string buffer_;
40 };
41
42 bool IsRedirect(int32_t status) { return status >= 300 && status <= 399; }
43
44 }  // namespace
45
46 ppapi::URLResponseInfoData DataFromWebURLResponse(
47     const WebURLResponse& response) {
48   ppapi::URLResponseInfoData data;
49   data.url = response.CurrentRequestUrl().GetString().Utf8();
50   data.status_code = response.HttpStatusCode();
51 #if BUILDFLAG(IS_TIZEN_TV)
52   // Tizen API responses code 0 instead of HTTP 200 indicating OK, when file
53   // is loaded using file:// protocol.
54   GURL url = response.CurrentRequestUrl();
55   if (url.SchemeIsFile()) {
56     if (data.status_code == 0)
57       data.status_code = 200;
58   }
59 #endif
60   data.status_text = response.HttpStatusText().Utf8();
61   if (IsRedirect(data.status_code)) {
62     data.redirect_url =
63         response.HttpHeaderField(WebString::FromUTF8("Location")).Utf8();
64   }
65
66   HeadersToString headers_to_string;
67   response.VisitHttpHeaderFields(&headers_to_string);
68   data.headers = headers_to_string.buffer();
69   return data;
70 }
71
72 }  // namespace content