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.
5 #ifndef PPAPI_PROXY_URL_LOADER_RESOURCE_H_
6 #define PPAPI_PROXY_URL_LOADER_RESOURCE_H_
10 #include "base/basictypes.h"
11 #include "base/compiler_specific.h"
12 #include "ppapi/c/trusted/ppb_url_loader_trusted.h"
13 #include "ppapi/proxy/plugin_resource.h"
14 #include "ppapi/proxy/ppapi_proxy_export.h"
15 #include "ppapi/shared_impl/url_request_info_data.h"
16 #include "ppapi/thunk/ppb_url_loader_api.h"
21 class URLResponseInfoResource
;
23 class PPAPI_PROXY_EXPORT URLLoaderResource
24 : public PluginResource
,
25 public NON_EXPORTED_BASE(thunk::PPB_URLLoader_API
) {
27 // Constructor for plugin-initiated loads.
28 URLLoaderResource(Connection connection
,
29 PP_Instance instance
);
31 // Constructor for renderer-initiated (document) loads. The loader ID is the
32 // pending host ID for the already-created host in the renderer, and the
33 // response data is the response for the already-opened connection.
34 URLLoaderResource(Connection connection
,
36 int pending_main_document_loader_id
,
37 const URLResponseInfoData
& data
);
39 ~URLLoaderResource() override
;
42 thunk::PPB_URLLoader_API
* AsPPB_URLLoader_API() override
;
44 // PPB_URLLoader_API implementation.
45 int32_t Open(PP_Resource request_id
,
46 scoped_refptr
<TrackedCallback
> callback
) override
;
47 int32_t Open(const URLRequestInfoData
& data
,
49 scoped_refptr
<TrackedCallback
> callback
) override
;
50 int32_t FollowRedirect(scoped_refptr
<TrackedCallback
> callback
) override
;
51 PP_Bool
GetUploadProgress(int64_t* bytes_sent
,
52 int64_t* total_bytes_to_be_sent
) override
;
53 PP_Bool
GetDownloadProgress(
54 int64_t* bytes_received
,
55 int64_t* total_bytes_to_be_received
) override
;
56 PP_Resource
GetResponseInfo() override
;
57 int32_t ReadResponseBody(
59 int32_t bytes_to_read
,
60 scoped_refptr
<TrackedCallback
> callback
) override
;
61 int32_t FinishStreamingToFile(
62 scoped_refptr
<TrackedCallback
> callback
) override
;
63 void Close() override
;
64 void GrantUniversalAccess() override
;
65 void RegisterStatusCallback(
66 PP_URLLoaderTrusted_StatusCallback callback
) override
;
68 // PluginResource implementation.
69 void OnReplyReceived(const ResourceMessageReplyParams
& params
,
70 const IPC::Message
& msg
) override
;
74 // The plugin has not called Open() yet.
77 // The plugin is waiting for the Open() or FollowRedirect callback.
80 // We've started to receive data and may receive more.
83 // All data has been streamed or there was an error.
87 // IPC message handlers.
88 void OnPluginMsgReceivedResponse(const ResourceMessageReplyParams
& params
,
89 const URLResponseInfoData
& data
);
90 void OnPluginMsgSendData(const ResourceMessageReplyParams
& params
,
91 const IPC::Message
& message
);
92 void OnPluginMsgFinishedLoading(const ResourceMessageReplyParams
& params
,
94 void OnPluginMsgUpdateProgress(const ResourceMessageReplyParams
& params
,
96 int64_t total_bytes_to_be_sent
,
97 int64_t bytes_received
,
98 int64_t total_bytes_to_be_received
);
100 // Sends the defers loading message to the renderer to block or unblock the
102 void SetDefersLoading(bool defers_loading
);
104 int32_t ValidateCallback(scoped_refptr
<TrackedCallback
> callback
);
106 // Sets up |callback| as the pending callback. This should only be called once
107 // it is certain that |PP_OK_COMPLETIONPENDING| will be returned.
108 void RegisterCallback(scoped_refptr
<TrackedCallback
> callback
);
110 void RunCallback(int32_t result
);
112 // Saves the given response info to response_info_, handling file refs if
113 // necessary. This does not issue any callbacks.
114 void SaveResponseInfo(const URLResponseInfoData
& data
);
116 int32_t FillUserBuffer();
119 URLRequestInfoData request_data_
;
121 scoped_refptr
<TrackedCallback
> pending_callback_
;
123 PP_URLLoaderTrusted_StatusCallback status_callback_
;
125 std::deque
<char> buffer_
;
127 int64_t total_bytes_to_be_sent_
;
128 int64_t bytes_received_
;
129 int64_t total_bytes_to_be_received_
;
131 size_t user_buffer_size_
;
132 int32_t done_status_
;
133 bool is_streaming_to_file_
;
134 bool is_asynchronous_load_suspended_
;
136 // The response info if we've received it.
137 scoped_refptr
<URLResponseInfoResource
> response_info_
;
139 DISALLOW_COPY_AND_ASSIGN(URLLoaderResource
);
145 #endif // PPAPI_PROXY_URL_LOADER_RESOURCE_H_