Separate Simple Backend creation from initialization.
[chromium-blink-merge.git] / ppapi / thunk / ppb_url_loader_thunk.cc
blob23113cd1ce7aff0d65276b46e71df536cabc3db1
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"
13 namespace ppapi {
14 namespace thunk {
16 namespace {
18 typedef EnterResource<PPB_URLLoader_API> EnterURLLoader;
20 PP_Resource Create(PP_Instance instance) {
21 EnterResourceCreation enter(instance);
22 if (enter.failed())
23 return 0;
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);
36 if (enter.failed())
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);
44 if (enter.failed())
45 return enter.retval();
46 return enter.SetResult(enter.object()->FollowRedirect(enter.callback()));
49 PP_Bool GetUploadProgress(PP_Resource loader,
50 int64_t* bytes_sent,
51 int64_t* total_bytes_to_be_sent) {
52 EnterURLLoader enter(loader, true);
53 if (enter.failed()) {
54 *bytes_sent = 0;
55 *total_bytes_to_be_sent = 0;
56 return PP_FALSE;
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);
66 if (enter.failed()) {
67 *bytes_received = 0;
68 *total_bytes_to_be_received = 0;
69 return PP_FALSE;
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);
77 if (enter.failed())
78 return 0;
79 return enter.object()->GetResponseInfo();
82 int32_t ReadResponseBody(PP_Resource loader,
83 void* buffer,
84 int32_t bytes_to_read,
85 PP_CompletionCallback callback) {
86 EnterURLLoader enter(loader, callback, true);
87 if (enter.failed())
88 return enter.retval();
89 return enter.SetResult(enter.object()->ReadResponseBody(buffer, bytes_to_read,
90 enter.callback()));
93 int32_t FinishStreamingToFile(PP_Resource loader,
94 PP_CompletionCallback callback) {
95 EnterURLLoader enter(loader, callback, true);
96 if (enter.failed())
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 = {
122 &Create,
123 &IsURLLoader,
124 &Open,
125 &FollowRedirect,
126 &GetUploadProgress,
127 &GetDownloadProgress,
128 &GetResponseInfo,
129 &ReadResponseBody,
130 &FinishStreamingToFile,
131 &Close
134 const PPB_URLLoaderTrusted g_ppb_urlloader_trusted_thunk = {
135 &GrantUniversalAccess,
136 &SetStatusCallback
139 } // namespace
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;
149 } // namespace thunk
150 } // namespace ppapi