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.
5 #include "ppapi/c/pp_completion_callback.h"
6 #include "ppapi/c/pp_errors.h"
7 #include "ppapi/shared_impl/tracked_callback.h"
8 #include "ppapi/thunk/enter.h"
9 #include "ppapi/thunk/thunk.h"
10 #include "ppapi/thunk/ppb_url_loader_api.h"
11 #include "ppapi/thunk/resource_creation_api.h"
18 typedef EnterResource
<PPB_URLLoader_API
> EnterURLLoader
;
20 PP_Resource
Create(PP_Instance instance
) {
21 EnterResourceCreation
enter(instance
);
24 return enter
.functions()->CreateURLLoader(instance
);
27 PP_Bool
IsURLLoader(PP_Resource resource
) {
28 EnterURLLoader
enter(resource
, false);
29 return PP_FromBool(enter
.succeeded());
32 int32_t Open(PP_Resource loader
,
33 PP_Resource request_id
,
34 PP_CompletionCallback callback
) {
35 EnterURLLoader
enter(loader
, callback
, true);
37 return enter
.retval();
38 return enter
.SetResult(enter
.object()->Open(request_id
, enter
.callback()));
41 int32_t FollowRedirect(PP_Resource loader
,
42 PP_CompletionCallback callback
) {
43 EnterURLLoader
enter(loader
, callback
, true);
45 return enter
.retval();
46 return enter
.SetResult(enter
.object()->FollowRedirect(enter
.callback()));
49 PP_Bool
GetUploadProgress(PP_Resource loader
,
51 int64_t* total_bytes_to_be_sent
) {
52 EnterURLLoader
enter(loader
, true);
55 *total_bytes_to_be_sent
= 0;
58 return enter
.object()->GetUploadProgress(bytes_sent
,
59 total_bytes_to_be_sent
);
62 PP_Bool
GetDownloadProgress(PP_Resource loader
,
63 int64_t* bytes_received
,
64 int64_t* total_bytes_to_be_received
) {
65 EnterURLLoader
enter(loader
, true);
68 *total_bytes_to_be_received
= 0;
71 return enter
.object()->GetDownloadProgress(bytes_received
,
72 total_bytes_to_be_received
);
75 PP_Resource
GetResponseInfo(PP_Resource loader
) {
76 EnterURLLoader
enter(loader
, true);
79 return enter
.object()->GetResponseInfo();
82 int32_t ReadResponseBody(PP_Resource loader
,
84 int32_t bytes_to_read
,
85 PP_CompletionCallback callback
) {
86 EnterURLLoader
enter(loader
, callback
, true);
88 return enter
.retval();
89 return enter
.SetResult(enter
.object()->ReadResponseBody(buffer
, bytes_to_read
,
93 int32_t FinishStreamingToFile(PP_Resource loader
,
94 PP_CompletionCallback callback
) {
95 EnterURLLoader
enter(loader
, callback
, true);
97 return enter
.retval();
98 return enter
.SetResult(
99 enter
.object()->FinishStreamingToFile(enter
.callback()));
102 void Close(PP_Resource loader
) {
103 EnterURLLoader
enter(loader
, true);
104 if (enter
.succeeded())
105 enter
.object()->Close();
108 void GrantUniversalAccess(PP_Resource loader
) {
109 EnterURLLoader
enter(loader
, true);
110 if (enter
.succeeded())
111 enter
.object()->GrantUniversalAccess();
114 void SetStatusCallback(PP_Resource loader
,
115 PP_URLLoaderTrusted_StatusCallback cb
) {
116 EnterURLLoader
enter(loader
, true);
117 if (enter
.succeeded())
118 enter
.object()->SetStatusCallback(cb
);
121 const PPB_URLLoader g_ppb_urlloader_thunk
= {
127 &GetDownloadProgress
,
130 &FinishStreamingToFile
,
134 const PPB_URLLoaderTrusted g_ppb_urlloader_trusted_thunk
= {
135 &GrantUniversalAccess
,
141 const PPB_URLLoader_1_0
* GetPPB_URLLoader_1_0_Thunk() {
142 return &g_ppb_urlloader_thunk
;
145 const PPB_URLLoaderTrusted_0_3
* GetPPB_URLLoaderTrusted_0_3_Thunk() {
146 return &g_ppb_urlloader_trusted_thunk
;