Home | History | Annotate | Download | only in pepper
      1 // Copyright (c) 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 #ifndef CONTENT_RENDERER_PEPPER_PEPPER_URL_LOADER_HOST_H_
      6 #define CONTENT_RENDERER_PEPPER_PEPPER_URL_LOADER_HOST_H_
      7 
      8 #include <vector>
      9 
     10 #include "base/memory/scoped_vector.h"
     11 #include "base/memory/weak_ptr.h"
     12 #include "content/common/content_export.h"
     13 #include "ppapi/host/resource_host.h"
     14 #include "ppapi/proxy/resource_message_params.h"
     15 #include "ppapi/shared_impl/url_request_info_data.h"
     16 #include "ppapi/shared_impl/url_response_info_data.h"
     17 #include "third_party/WebKit/public/platform/WebURLLoaderClient.h"
     18 
     19 namespace WebKit {
     20 class WebFrame;
     21 class WebURLLoader;
     22 }
     23 
     24 namespace content {
     25 
     26 class RendererPpapiHostImpl;
     27 
     28 class PepperURLLoaderHost
     29     : public ppapi::host::ResourceHost,
     30       public WebKit::WebURLLoaderClient {
     31  public:
     32   // If main_document_loader is true, PP_Resource must be 0 since it will be
     33   // pending until the plugin resource attaches to it.
     34   PepperURLLoaderHost(RendererPpapiHostImpl* host,
     35                       bool main_document_loader,
     36                       PP_Instance instance,
     37                       PP_Resource resource);
     38   virtual ~PepperURLLoaderHost();
     39 
     40   // ResourceHost implementation.
     41   virtual int32_t OnResourceMessageReceived(
     42       const IPC::Message& msg,
     43       ppapi::host::HostMessageContext* context) OVERRIDE;
     44 
     45   // WebKit::WebURLLoaderClient implementation.
     46   virtual void willSendRequest(WebKit::WebURLLoader* loader,
     47                                WebKit::WebURLRequest& new_request,
     48                                const WebKit::WebURLResponse& redir_response);
     49   virtual void didSendData(WebKit::WebURLLoader* loader,
     50                            unsigned long long bytes_sent,
     51                            unsigned long long total_bytes_to_be_sent);
     52   virtual void didReceiveResponse(WebKit::WebURLLoader* loader,
     53                                   const WebKit::WebURLResponse& response);
     54   virtual void didDownloadData(WebKit::WebURLLoader* loader,
     55                                int data_length);
     56   virtual void didReceiveData(WebKit::WebURLLoader* loader,
     57                               const char* data,
     58                               int data_length,
     59                               int encoded_data_length);
     60   virtual void didFinishLoading(WebKit::WebURLLoader* loader,
     61                                 double finish_time);
     62   virtual void didFail(WebKit::WebURLLoader* loader,
     63                        const WebKit::WebURLError& error);
     64 
     65  private:
     66   // ResourceHost protected overrides.
     67   virtual void DidConnectPendingHostToResource() OVERRIDE;
     68 
     69   // IPC messages
     70   int32_t OnHostMsgOpen(ppapi::host::HostMessageContext* context,
     71                         const ppapi::URLRequestInfoData& request_data);
     72   int32_t InternalOnHostMsgOpen(ppapi::host::HostMessageContext* context,
     73                                 const ppapi::URLRequestInfoData& request_data);
     74   int32_t OnHostMsgSetDeferLoading(ppapi::host::HostMessageContext* context,
     75                                    bool defers_loading);
     76   int32_t OnHostMsgClose(ppapi::host::HostMessageContext* context);
     77   int32_t OnHostMsgGrantUniversalAccess(
     78       ppapi::host::HostMessageContext* context);
     79 
     80   // Sends or queues an unsolicited message to the plugin resource. This
     81   // handles cases where messages must be reordered for the plugin and
     82   // the case where we have created a pending host resource and the
     83   // plugin has not connected to us yet.
     84   //
     85   // Takes ownership of the given pointer.
     86   void SendUpdateToPlugin(IPC::Message* msg);
     87 
     88   // Sends or queues an unsolicited message to the plugin resource. This is
     89   // used inside SendUpdateToPlugin for messages that are already ordered
     90   // properly.
     91   //
     92   // Takes ownership of the given pointer.
     93   void SendOrderedUpdateToPlugin(IPC::Message* msg);
     94 
     95   void Close();
     96 
     97   // Returns the frame for the current request.
     98   WebKit::WebFrame* GetFrame();
     99 
    100   // Calls SetDefersLoading on the current load. This encapsulates the logic
    101   // differences between document loads and regular ones.
    102   void SetDefersLoading(bool defers_loading);
    103 
    104   // Converts a WebURLResponse to a URLResponseInfo and saves it.
    105   void SaveResponse(const WebKit::WebURLResponse& response);
    106   void DidDataFromWebURLResponse(const ppapi::URLResponseInfoData& data);
    107 
    108   // Sends the UpdateProgress message (if necessary) to the plugin.
    109   void UpdateProgress();
    110 
    111   // Non-owning pointer.
    112   RendererPpapiHostImpl* renderer_ppapi_host_;
    113 
    114   // If true, then the plugin instance is a full-frame plugin and we're just
    115   // wrapping the main document's loader (i.e. loader_ is null).
    116   bool main_document_loader_;
    117 
    118   // The data that generated the request.
    119   ppapi::URLRequestInfoData request_data_;
    120 
    121   // Set to true when this loader can ignore same originl policy.
    122   bool has_universal_access_;
    123 
    124   // The loader associated with this request. MAY BE NULL.
    125   //
    126   // This will be NULL if the load hasn't been opened yet, or if this is a main
    127   // document loader (when registered as a mime type). Therefore, you should
    128   // always NULL check this value before using it. In the case of a main
    129   // document load, you would call the functions on the document to cancel the
    130   // load, etc. since there is no loader.
    131   scoped_ptr<WebKit::WebURLLoader> loader_;
    132 
    133   int64_t bytes_sent_;
    134   int64_t total_bytes_to_be_sent_;
    135   int64_t bytes_received_;
    136   int64_t total_bytes_to_be_received_;
    137 
    138   // Messages sent while the resource host is pending. These will be forwarded
    139   // to the plugin when the plugin side connects. The pointers are owned by
    140   // this object and must be deleted.
    141   ScopedVector<IPC::Message> pending_replies_;
    142   ScopedVector<IPC::Message> out_of_order_replies_;
    143 
    144   // True when there's a pending DataFromURLResponse call which will send a
    145   // PpapiPluginMsg_URLLoader_ReceivedResponse to the plugin, which introduces
    146   // ordering constraints on following messages to the plugin.
    147   bool pending_response_;
    148 
    149   base::WeakPtrFactory<PepperURLLoaderHost> weak_factory_;
    150 
    151   DISALLOW_COPY_AND_ASSIGN(PepperURLLoaderHost);
    152 };
    153 
    154 }  // namespace content
    155 
    156 #endif  // CONTENT_RENDERER_PEPPER_PEPPER_URL_LOADER_HOST_H_
    157