Roll src/third_party/WebKit 05e9c31:d6595eb (svn 198725:198727)
[chromium-blink-merge.git] / ppapi / proxy / url_loader_resource.h
blobf96c1aba2e44ecb047d7c0460418a393d4d835e5
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_
8 #include <deque>
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"
18 namespace ppapi {
19 namespace proxy {
21 class URLResponseInfoResource;
23 class PPAPI_PROXY_EXPORT URLLoaderResource
24 : public PluginResource,
25 public NON_EXPORTED_BASE(thunk::PPB_URLLoader_API) {
26 public:
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,
35 PP_Instance instance,
36 int pending_main_document_loader_id,
37 const URLResponseInfoData& data);
39 ~URLLoaderResource() override;
41 // Resource 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,
48 int requestor_pid,
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(
58 void* buffer,
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;
72 private:
73 enum Mode {
74 // The plugin has not called Open() yet.
75 MODE_WAITING_TO_OPEN,
77 // The plugin is waiting for the Open() or FollowRedirect callback.
78 MODE_OPENING,
80 // We've started to receive data and may receive more.
81 MODE_STREAMING_DATA,
83 // All data has been streamed or there was an error.
84 MODE_LOAD_COMPLETE
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,
93 int32_t result);
94 void OnPluginMsgUpdateProgress(const ResourceMessageReplyParams& params,
95 int64_t bytes_sent,
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
101 // load.
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();
118 Mode mode_;
119 URLRequestInfoData request_data_;
121 scoped_refptr<TrackedCallback> pending_callback_;
123 PP_URLLoaderTrusted_StatusCallback status_callback_;
125 std::deque<char> buffer_;
126 int64_t bytes_sent_;
127 int64_t total_bytes_to_be_sent_;
128 int64_t bytes_received_;
129 int64_t total_bytes_to_be_received_;
130 char* user_buffer_;
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);
142 } // namespace proxy
143 } // namespace ppapi
145 #endif // PPAPI_PROXY_URL_LOADER_RESOURCE_H_