Home | History | Annotate | Download | only in pepper
      1 // Copyright 2013 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 "content/renderer/pepper/url_response_info_util.h"
      6 
      7 #include "base/bind.h"
      8 #include "base/files/file_path.h"
      9 #include "base/message_loop/message_loop.h"
     10 #include "content/public/renderer/renderer_ppapi_host.h"
     11 #include "content/renderer/pepper/pepper_file_ref_renderer_host.h"
     12 #include "content/renderer/pepper/renderer_ppapi_host_impl.h"
     13 #include "ipc/ipc_message.h"
     14 #include "ppapi/proxy/ppapi_messages.h"
     15 #include "ppapi/shared_impl/url_response_info_data.h"
     16 #include "third_party/WebKit/public/platform/WebCString.h"
     17 #include "third_party/WebKit/public/platform/WebHTTPHeaderVisitor.h"
     18 #include "third_party/WebKit/public/platform/WebString.h"
     19 #include "third_party/WebKit/public/platform/WebURL.h"
     20 #include "third_party/WebKit/public/platform/WebURLResponse.h"
     21 
     22 using blink::WebHTTPHeaderVisitor;
     23 using blink::WebString;
     24 using blink::WebURLResponse;
     25 
     26 namespace content {
     27 
     28 namespace {
     29 
     30 class HeaderFlattener : public WebHTTPHeaderVisitor {
     31  public:
     32   const std::string& buffer() const { return buffer_; }
     33 
     34   virtual void visitHeader(const WebString& name, const WebString& value) {
     35     if (!buffer_.empty())
     36       buffer_.append("\n");
     37     buffer_.append(name.utf8());
     38     buffer_.append(": ");
     39     buffer_.append(value.utf8());
     40   }
     41 
     42  private:
     43   std::string buffer_;
     44 };
     45 
     46 bool IsRedirect(int32_t status) {
     47   return status >= 300 && status <= 399;
     48 }
     49 
     50 void DidCreateResourceHosts(const ppapi::URLResponseInfoData& in_data,
     51                             const base::FilePath& external_path,
     52                             int renderer_pending_host_id,
     53                             const DataFromWebURLResponseCallback& callback,
     54                             const std::vector<int>& browser_pending_host_ids) {
     55   DCHECK(browser_pending_host_ids.size() == 1);
     56   int browser_pending_host_id = 0;
     57 
     58   if (browser_pending_host_ids.size() == 1)
     59     browser_pending_host_id = browser_pending_host_ids[0];
     60 
     61   ppapi::URLResponseInfoData data = in_data;
     62 
     63   data.body_as_file_ref = ppapi::MakeExternalFileRefCreateInfo(
     64       external_path,
     65       std::string(),
     66       browser_pending_host_id,
     67       renderer_pending_host_id);
     68   callback.Run(data);
     69 }
     70 
     71 }  // namespace
     72 
     73 void DataFromWebURLResponse(RendererPpapiHostImpl* host_impl,
     74                             PP_Instance pp_instance,
     75                             const WebURLResponse& response,
     76                             const DataFromWebURLResponseCallback& callback) {
     77   ppapi::URLResponseInfoData data;
     78   data.url = response.url().spec();
     79   data.status_code = response.httpStatusCode();
     80   data.status_text = response.httpStatusText().utf8();
     81   if (IsRedirect(data.status_code)) {
     82     data.redirect_url = response.httpHeaderField(
     83         WebString::fromUTF8("Location")).utf8();
     84   }
     85 
     86   HeaderFlattener flattener;
     87   response.visitHTTPHeaderFields(&flattener);
     88   data.headers = flattener.buffer();
     89 
     90   WebString file_path = response.downloadFilePath();
     91   if (!file_path.isEmpty()) {
     92     base::FilePath external_path = base::FilePath::FromUTF16Unsafe(file_path);
     93     // TODO(teravest): Write a utility function to create resource hosts in the
     94     // renderer and browser.
     95     PepperFileRefRendererHost* renderer_host =
     96         new PepperFileRefRendererHost(host_impl, pp_instance, 0, external_path);
     97     int renderer_pending_host_id =
     98         host_impl->GetPpapiHost()->AddPendingResourceHost(
     99             scoped_ptr<ppapi::host::ResourceHost>(renderer_host));
    100 
    101     std::vector<IPC::Message> create_msgs;
    102     create_msgs.push_back(PpapiHostMsg_FileRef_CreateExternal(external_path));
    103     host_impl->CreateBrowserResourceHosts(
    104         pp_instance,
    105         create_msgs,
    106         base::Bind(&DidCreateResourceHosts,
    107                    data,
    108                    external_path,
    109                    renderer_pending_host_id,
    110                    callback));
    111   } else {
    112     base::MessageLoop::current()->PostTask(
    113         FROM_HERE,
    114         base::Bind(callback, data));
    115   }
    116 }
    117 
    118 }  // namespace content
    119