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// From ppb_url_loader.idl modified Tue Aug 20 08:13:36 2013.
6
7#include <string.h>
8
9#include "ppapi/c/pp_completion_callback.h"
10#include "ppapi/c/pp_errors.h"
11#include "ppapi/c/ppb_url_loader.h"
12#include "ppapi/shared_impl/tracked_callback.h"
13#include "ppapi/thunk/enter.h"
14#include "ppapi/thunk/ppapi_thunk_export.h"
15#include "ppapi/thunk/ppb_url_loader_api.h"
16
17namespace ppapi {
18namespace thunk {
19
20namespace {
21
22PP_Resource Create(PP_Instance instance) {
23  VLOG(4) << "PPB_URLLoader::Create()";
24  EnterResourceCreation enter(instance);
25  if (enter.failed())
26    return 0;
27  return enter.functions()->CreateURLLoader(instance);
28}
29
30PP_Bool IsURLLoader(PP_Resource resource) {
31  VLOG(4) << "PPB_URLLoader::IsURLLoader()";
32  EnterResource<PPB_URLLoader_API> enter(resource, false);
33  return PP_FromBool(enter.succeeded());
34}
35
36int32_t Open(PP_Resource loader,
37             PP_Resource request_info,
38             struct PP_CompletionCallback callback) {
39  VLOG(4) << "PPB_URLLoader::Open()";
40  EnterResource<PPB_URLLoader_API> enter(loader, callback, true);
41  if (enter.failed())
42    return enter.retval();
43  return enter.SetResult(enter.object()->Open(request_info, enter.callback()));
44}
45
46int32_t FollowRedirect(PP_Resource loader,
47                       struct PP_CompletionCallback callback) {
48  VLOG(4) << "PPB_URLLoader::FollowRedirect()";
49  EnterResource<PPB_URLLoader_API> enter(loader, callback, true);
50  if (enter.failed())
51    return enter.retval();
52  return enter.SetResult(enter.object()->FollowRedirect(enter.callback()));
53}
54
55PP_Bool GetUploadProgress(PP_Resource loader,
56                          int64_t* bytes_sent,
57                          int64_t* total_bytes_to_be_sent) {
58  VLOG(4) << "PPB_URLLoader::GetUploadProgress()";
59  EnterResource<PPB_URLLoader_API> enter(loader, true);
60  if (enter.failed()) {
61    memset(bytes_sent, 0, sizeof(*bytes_sent));
62    memset(total_bytes_to_be_sent, 0, sizeof(*total_bytes_to_be_sent));
63    return PP_FALSE;
64  }
65  return enter.object()->GetUploadProgress(bytes_sent, total_bytes_to_be_sent);
66}
67
68PP_Bool GetDownloadProgress(PP_Resource loader,
69                            int64_t* bytes_received,
70                            int64_t* total_bytes_to_be_received) {
71  VLOG(4) << "PPB_URLLoader::GetDownloadProgress()";
72  EnterResource<PPB_URLLoader_API> enter(loader, true);
73  if (enter.failed()) {
74    memset(bytes_received, 0, sizeof(*bytes_received));
75    memset(total_bytes_to_be_received, 0, sizeof(*total_bytes_to_be_received));
76    return PP_FALSE;
77  }
78  return enter.object()->GetDownloadProgress(bytes_received,
79                                             total_bytes_to_be_received);
80}
81
82PP_Resource GetResponseInfo(PP_Resource loader) {
83  VLOG(4) << "PPB_URLLoader::GetResponseInfo()";
84  EnterResource<PPB_URLLoader_API> enter(loader, true);
85  if (enter.failed())
86    return 0;
87  return enter.object()->GetResponseInfo();
88}
89
90int32_t ReadResponseBody(PP_Resource loader,
91                         void* buffer,
92                         int32_t bytes_to_read,
93                         struct PP_CompletionCallback callback) {
94  VLOG(4) << "PPB_URLLoader::ReadResponseBody()";
95  EnterResource<PPB_URLLoader_API> enter(loader, callback, true);
96  if (enter.failed())
97    return enter.retval();
98  return enter.SetResult(enter.object()->ReadResponseBody(buffer,
99                                                          bytes_to_read,
100                                                          enter.callback()));
101}
102
103int32_t FinishStreamingToFile(PP_Resource loader,
104                              struct PP_CompletionCallback callback) {
105  VLOG(4) << "PPB_URLLoader::FinishStreamingToFile()";
106  EnterResource<PPB_URLLoader_API> enter(loader, callback, true);
107  if (enter.failed())
108    return enter.retval();
109  return enter.SetResult(enter.object()->FinishStreamingToFile(
110      enter.callback()));
111}
112
113void Close(PP_Resource loader) {
114  VLOG(4) << "PPB_URLLoader::Close()";
115  EnterResource<PPB_URLLoader_API> enter(loader, true);
116  if (enter.failed())
117    return;
118  enter.object()->Close();
119}
120
121const PPB_URLLoader_1_0 g_ppb_urlloader_thunk_1_0 = {
122  &Create,
123  &IsURLLoader,
124  &Open,
125  &FollowRedirect,
126  &GetUploadProgress,
127  &GetDownloadProgress,
128  &GetResponseInfo,
129  &ReadResponseBody,
130  &FinishStreamingToFile,
131  &Close
132};
133
134}  // namespace
135
136PPAPI_THUNK_EXPORT const PPB_URLLoader_1_0* GetPPB_URLLoader_1_0_Thunk() {
137  return &g_ppb_urlloader_thunk_1_0;
138}
139
140}  // namespace thunk
141}  // namespace ppapi
142