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