OLD | NEW |
| (Empty) |
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 #ifndef WEBKIT_PLUGINS_PPAPI_PPB_URL_LOADER_IMPL_H_ | |
6 #define WEBKIT_PLUGINS_PPAPI_PPB_URL_LOADER_IMPL_H_ | |
7 | |
8 #include <deque> | |
9 | |
10 #include "base/memory/ref_counted.h" | |
11 #include "base/memory/scoped_ptr.h" | |
12 #include "ppapi/c/pp_completion_callback.h" | |
13 #include "ppapi/c/trusted/ppb_url_loader_trusted.h" | |
14 #include "ppapi/shared_impl/resource.h" | |
15 #include "ppapi/shared_impl/scoped_pp_resource.h" | |
16 #include "ppapi/shared_impl/tracked_callback.h" | |
17 #include "ppapi/shared_impl/url_request_info_data.h" | |
18 #include "ppapi/thunk/ppb_url_loader_api.h" | |
19 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebURLLoader
Client.h" | |
20 #include "webkit/plugins/ppapi/ppapi_plugin_instance.h" | |
21 | |
22 namespace WebKit { | |
23 class WebURL; | |
24 } | |
25 | |
26 namespace webkit { | |
27 namespace ppapi { | |
28 | |
29 class PPB_URLLoader_Impl : public ::ppapi::Resource, | |
30 public ::ppapi::thunk::PPB_URLLoader_API, | |
31 public WebKit::WebURLLoaderClient { | |
32 public: | |
33 PPB_URLLoader_Impl(PP_Instance instance, bool main_document_loader); | |
34 virtual ~PPB_URLLoader_Impl(); | |
35 | |
36 // Resource overrides. | |
37 virtual ::ppapi::thunk::PPB_URLLoader_API* AsPPB_URLLoader_API() OVERRIDE; | |
38 virtual void InstanceWasDeleted() OVERRIDE; | |
39 | |
40 // PPB_URLLoader_API implementation. | |
41 virtual int32_t Open( | |
42 PP_Resource request_id, | |
43 scoped_refptr< ::ppapi::TrackedCallback> callback) OVERRIDE; | |
44 virtual int32_t Open( | |
45 const ::ppapi::URLRequestInfoData& data, | |
46 int requestor_pid, | |
47 scoped_refptr< ::ppapi::TrackedCallback> callback) OVERRIDE; | |
48 virtual int32_t FollowRedirect( | |
49 scoped_refptr< ::ppapi::TrackedCallback> callback) OVERRIDE; | |
50 virtual PP_Bool GetUploadProgress(int64_t* bytes_sent, | |
51 int64_t* total_bytes_to_be_sent) OVERRIDE; | |
52 virtual PP_Bool GetDownloadProgress( | |
53 int64_t* bytes_received, | |
54 int64_t* total_bytes_to_be_received) OVERRIDE; | |
55 virtual PP_Resource GetResponseInfo() OVERRIDE; | |
56 virtual int32_t ReadResponseBody( | |
57 void* buffer, | |
58 int32_t bytes_to_read, | |
59 scoped_refptr< ::ppapi::TrackedCallback> callback) OVERRIDE; | |
60 virtual int32_t FinishStreamingToFile( | |
61 scoped_refptr< ::ppapi::TrackedCallback> callback) OVERRIDE; | |
62 virtual void Close() OVERRIDE; | |
63 virtual void GrantUniversalAccess() OVERRIDE; | |
64 virtual void SetStatusCallback( | |
65 PP_URLLoaderTrusted_StatusCallback cb) OVERRIDE; | |
66 virtual bool GetResponseInfoData( | |
67 ::ppapi::URLResponseInfoData* data) OVERRIDE; | |
68 | |
69 // WebKit::WebURLLoaderClient implementation. | |
70 virtual void willSendRequest(WebKit::WebURLLoader* loader, | |
71 WebKit::WebURLRequest& new_request, | |
72 const WebKit::WebURLResponse& redir_response); | |
73 virtual void didSendData(WebKit::WebURLLoader* loader, | |
74 unsigned long long bytes_sent, | |
75 unsigned long long total_bytes_to_be_sent); | |
76 virtual void didReceiveResponse(WebKit::WebURLLoader* loader, | |
77 const WebKit::WebURLResponse& response); | |
78 virtual void didDownloadData(WebKit::WebURLLoader* loader, | |
79 int data_length); | |
80 | |
81 virtual void didReceiveData(WebKit::WebURLLoader* loader, | |
82 const char* data, | |
83 int data_length, | |
84 int encoded_data_length); | |
85 virtual void didFinishLoading(WebKit::WebURLLoader* loader, | |
86 double finish_time); | |
87 virtual void didFail(WebKit::WebURLLoader* loader, | |
88 const WebKit::WebURLError& error); | |
89 | |
90 // Returns the number of bytes currently available for synchronous reading | |
91 // in the loader. | |
92 int32_t buffer_size() const { return buffer_.size(); } | |
93 | |
94 private: | |
95 // Check that |callback| is valid (only non-blocking operation is supported) | |
96 // and that no callback is already pending. Returns |PP_OK| if okay, else | |
97 // |PP_ERROR_...| to be returned to the plugin. | |
98 int32_t ValidateCallback(scoped_refptr< ::ppapi::TrackedCallback> callback); | |
99 | |
100 // Sets up |callback| as the pending callback. This should only be called once | |
101 // it is certain that |PP_OK_COMPLETIONPENDING| will be returned. | |
102 void RegisterCallback(scoped_refptr< ::ppapi::TrackedCallback> callback); | |
103 | |
104 void RunCallback(int32_t result); | |
105 | |
106 size_t FillUserBuffer(); | |
107 | |
108 // Converts a WebURLResponse to a URLResponseInfo and saves it. | |
109 void SaveResponse(const WebKit::WebURLResponse& response); | |
110 | |
111 // Calls the status_callback_ (if any) with the current upload and download | |
112 // progress. Call this function if you update any of these values to | |
113 // synchronize an out-of-process plugin's state. | |
114 void UpdateStatus(); | |
115 | |
116 // Returns true if the plugin has requested we record download or upload | |
117 // progress. When false, we don't need to update the counters. We go out of | |
118 // our way not to allow access to this information unless it's requested, | |
119 // even when it would be easier just to return it and not check, so that | |
120 // plugins don't depend on access without setting the flag. | |
121 bool RecordDownloadProgress() const; | |
122 bool RecordUploadProgress() const; | |
123 | |
124 // Calls SetDefersLoading on the current load. This encapsulates the logic | |
125 // differences between document loads and regular ones. | |
126 void SetDefersLoading(bool defers_loading); | |
127 | |
128 void FinishLoading(int32_t done_status); | |
129 | |
130 // If true, then the plugin instance is a full-frame plugin and we're just | |
131 // wrapping the main document's loader (i.e. loader_ is null). | |
132 bool main_document_loader_; | |
133 | |
134 // Keep a copy of the request data. We specifically do this instead of | |
135 // keeping a reference to the request resource, because the plugin might | |
136 // change the request info resource out from under us. | |
137 ::ppapi::URLRequestInfoData request_data_; | |
138 | |
139 // The loader associated with this request. MAY BE NULL. | |
140 // | |
141 // This will be NULL if the load hasn't been opened yet, or if this is a main | |
142 // document loader (when registered as a mime type). Therefore, you should | |
143 // always NULL check this value before using it. In the case of a main | |
144 // document load, you would call the functions on the document to cancel the | |
145 // load, etc. since there is no loader. | |
146 scoped_ptr<WebKit::WebURLLoader> loader_; | |
147 | |
148 scoped_refptr< ::ppapi::TrackedCallback> pending_callback_; | |
149 std::deque<char> buffer_; | |
150 int64_t bytes_sent_; | |
151 int64_t total_bytes_to_be_sent_; | |
152 int64_t bytes_received_; | |
153 int64_t total_bytes_to_be_received_; | |
154 char* user_buffer_; | |
155 size_t user_buffer_size_; | |
156 int32_t done_status_; | |
157 bool is_streaming_to_file_; | |
158 bool is_asynchronous_load_suspended_; | |
159 | |
160 bool has_universal_access_; | |
161 | |
162 PP_URLLoaderTrusted_StatusCallback status_callback_; | |
163 | |
164 // When the response info is received, this stores the data. The | |
165 // ScopedResource maintains the reference to the file ref (if any) in the | |
166 // data object so we don't forget to dereference it. | |
167 scoped_ptr< ::ppapi::URLResponseInfoData > response_info_; | |
168 ::ppapi::ScopedPPResource response_info_file_ref_; | |
169 | |
170 DISALLOW_COPY_AND_ASSIGN(PPB_URLLoader_Impl); | |
171 }; | |
172 | |
173 } // namespace ppapi | |
174 } // namespace webkit | |
175 | |
176 #endif // WEBKIT_PLUGINS_PPAPI_PPB_URL_LOADER_IMPL_H_ | |
OLD | NEW |