Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / content / child / resource_dispatcher.h
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.
4
5 // See http://dev.chromium.org/developers/design-documents/multi-process-resource-loading
6
7 #ifndef CONTENT_CHILD_RESOURCE_DISPATCHER_H_
8 #define CONTENT_CHILD_RESOURCE_DISPATCHER_H_
9
10 #include <deque>
11 #include <string>
12
13 #include "base/containers/hash_tables.h"
14 #include "base/memory/linked_ptr.h"
15 #include "base/memory/shared_memory.h"
16 #include "base/memory/weak_ptr.h"
17 #include "base/time/time.h"
18 #include "content/common/content_export.h"
19 #include "ipc/ipc_listener.h"
20 #include "ipc/ipc_sender.h"
21 #include "net/base/request_priority.h"
22 #include "webkit/common/resource_type.h"
23
24 struct ResourceMsg_RequestCompleteData;
25
26 namespace webkit_glue {
27 class ResourceLoaderBridge;
28 struct ResourceResponseInfo;
29 }
30
31 namespace content {
32 class RequestPeer;
33 class ResourceDispatcherDelegate;
34 struct RequestInfo;
35 struct ResourceResponseHead;
36 struct SiteIsolationResponseMetaData;
37
38 // This class serves as a communication interface between the
39 // ResourceDispatcherHost in the browser process and the ResourceLoaderBridge in
40 // the child process.  It can be used from any child process.
41 class CONTENT_EXPORT ResourceDispatcher : public IPC::Listener {
42  public:
43   explicit ResourceDispatcher(IPC::Sender* sender);
44   virtual ~ResourceDispatcher();
45
46   // IPC::Listener implementation.
47   virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
48
49   // Creates a ResourceLoaderBridge for this type of dispatcher, this is so
50   // this can be tested regardless of the ResourceLoaderBridge::Create
51   // implementation.
52   webkit_glue::ResourceLoaderBridge* CreateBridge(
53       const RequestInfo& request_info);
54
55   // Adds a request from the |pending_requests_| list, returning the new
56   // requests' ID.
57   int AddPendingRequest(RequestPeer* callback,
58                         ResourceType::Type resource_type,
59                         int origin_pid,
60                         const GURL& frame_origin,
61                         const GURL& request_url,
62                         bool download_to_file);
63
64   // Removes a request from the |pending_requests_| list, returning true if the
65   // request was found and removed.
66   bool RemovePendingRequest(int request_id);
67
68   // Cancels a request in the |pending_requests_| list.
69   void CancelPendingRequest(int request_id);
70
71   // Toggles the is_deferred attribute for the specified request.
72   void SetDefersLoading(int request_id, bool value);
73
74   // Indicates the priority of the specified request changed.
75   void DidChangePriority(int routing_id,
76                          int request_id,
77                          net::RequestPriority new_priority,
78                          int intra_priority_value);
79
80   IPC::Sender* message_sender() const { return message_sender_; }
81
82   // This does not take ownership of the delegate. It is expected that the
83   // delegate have a longer lifetime than the ResourceDispatcher.
84   void set_delegate(ResourceDispatcherDelegate* delegate) {
85     delegate_ = delegate;
86   }
87
88   // Remembers IO thread timestamp for next resource message.
89   void set_io_timestamp(base::TimeTicks io_timestamp) {
90     io_timestamp_ = io_timestamp;
91   }
92
93  private:
94   friend class ResourceDispatcherTest;
95
96   typedef std::deque<IPC::Message*> MessageQueue;
97   struct PendingRequestInfo {
98     PendingRequestInfo();
99
100     PendingRequestInfo(RequestPeer* peer,
101                        ResourceType::Type resource_type,
102                        int origin_pid,
103                        const GURL& frame_origin,
104                        const GURL& request_url,
105                        bool download_to_file);
106
107     ~PendingRequestInfo();
108
109     RequestPeer* peer;
110     ResourceType::Type resource_type;
111     // The PID of the original process which issued this request. This gets
112     // non-zero only for a request proxied by another renderer, particularly
113     // requests from plugins.
114     int origin_pid;
115     MessageQueue deferred_message_queue;
116     bool is_deferred;
117     bool is_canceled;
118     // Original requested url.
119     GURL url;
120     // The security origin of the frame that initiates this request.
121     GURL frame_origin;
122     // The url of the latest response even in case of redirection.
123     GURL response_url;
124     bool download_to_file;
125     linked_ptr<IPC::Message> pending_redirect_message;
126     base::TimeTicks request_start;
127     base::TimeTicks response_start;
128     base::TimeTicks completion_time;
129     linked_ptr<base::SharedMemory> buffer;
130     linked_ptr<SiteIsolationResponseMetaData> site_isolation_metadata;
131     bool blocked_response;
132     int buffer_size;
133   };
134   typedef base::hash_map<int, PendingRequestInfo> PendingRequestList;
135
136   // Helper to lookup the info based on the request_id.
137   // May return NULL if the request as been canceled from the client side.
138   PendingRequestInfo* GetPendingRequestInfo(int request_id);
139
140   // Follows redirect, if any, for the given request.
141   void FollowPendingRedirect(int request_id, PendingRequestInfo& request_info);
142
143   // Message response handlers, called by the message handler for this process.
144   void OnUploadProgress(int request_id, int64 position, int64 size);
145   void OnReceivedResponse(int request_id, const ResourceResponseHead&);
146   void OnReceivedCachedMetadata(int request_id, const std::vector<char>& data);
147   void OnReceivedRedirect(int request_id,
148                           const GURL& new_url,
149                           const ResourceResponseHead& response_head);
150   void OnSetDataBuffer(int request_id,
151                        base::SharedMemoryHandle shm_handle,
152                        int shm_size,
153                        base::ProcessId renderer_pid);
154   void OnReceivedData(int request_id,
155                       int data_offset,
156                       int data_length,
157                       int encoded_data_length);
158   void OnDownloadedData(int request_id, int data_len, int encoded_data_length);
159   void OnRequestComplete(
160       int request_id,
161       const ResourceMsg_RequestCompleteData& request_complete_data);
162
163   // Dispatch the message to one of the message response handlers.
164   void DispatchMessage(const IPC::Message& message);
165
166   // Dispatch any deferred messages for the given request, provided it is not
167   // again in the deferred state.
168   void FlushDeferredMessages(int request_id);
169
170   void ToResourceResponseInfo(
171       const PendingRequestInfo& request_info,
172       const ResourceResponseHead& browser_info,
173       webkit_glue::ResourceResponseInfo* renderer_info) const;
174
175   base::TimeTicks ToRendererCompletionTime(
176       const PendingRequestInfo& request_info,
177       const base::TimeTicks& browser_completion_time) const;
178
179   // Returns timestamp provided by IO thread. If no timestamp is supplied,
180   // then current time is returned. Saved timestamp is reset, so following
181   // invocations will return current time until set_io_timestamp is called.
182   base::TimeTicks ConsumeIOTimestamp();
183
184   // Returns true if the message passed in is a resource related message.
185   static bool IsResourceDispatcherMessage(const IPC::Message& message);
186
187   // ViewHostMsg_Resource_DataReceived is not POD, it has a shared memory
188   // handle in it that we should cleanup it up nicely. This method accepts any
189   // message and determine whether the message is
190   // ViewHostMsg_Resource_DataReceived and clean up the shared memory handle.
191   static void ReleaseResourcesInDataMessage(const IPC::Message& message);
192
193   // Iterate through a message queue and clean up the messages by calling
194   // ReleaseResourcesInDataMessage and removing them from the queue. Intended
195   // for use on deferred message queues that are no longer needed.
196   static void ReleaseResourcesInMessageQueue(MessageQueue* queue);
197
198   IPC::Sender* message_sender_;
199
200   // All pending requests issued to the host
201   PendingRequestList pending_requests_;
202
203   base::WeakPtrFactory<ResourceDispatcher> weak_factory_;
204
205   ResourceDispatcherDelegate* delegate_;
206
207   // IO thread timestamp for ongoing IPC message.
208   base::TimeTicks io_timestamp_;
209
210   DISALLOW_COPY_AND_ASSIGN(ResourceDispatcher);
211 };
212
213 }  // namespace content
214
215 #endif  // CONTENT_CHILD_RESOURCE_DISPATCHER_H_