[email protected] | 1c7e5c9 | 2012-03-22 00:58:28 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 5a3f6285 | 2010-11-10 21:43:01 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "ppapi/cpp/url_loader.h" |
| 6 | |
| 7 | #include "ppapi/c/ppb_url_loader.h" |
| 8 | #include "ppapi/c/pp_errors.h" |
[email protected] | 5a3f6285 | 2010-11-10 21:43:01 | [diff] [blame] | 9 | #include "ppapi/cpp/completion_callback.h" |
[email protected] | 25ffe85f | 2011-07-13 01:04:59 | [diff] [blame] | 10 | #include "ppapi/cpp/file_ref.h" |
[email protected] | 09af0f7 | 2012-02-27 20:23:19 | [diff] [blame] | 11 | #include "ppapi/cpp/instance_handle.h" |
[email protected] | 5a3f6285 | 2010-11-10 21:43:01 | [diff] [blame] | 12 | #include "ppapi/cpp/module.h" |
| 13 | #include "ppapi/cpp/module_impl.h" |
| 14 | #include "ppapi/cpp/url_request_info.h" |
| 15 | #include "ppapi/cpp/url_response_info.h" |
| 16 | |
[email protected] | 6b7550a | 2010-12-20 19:03:07 | [diff] [blame] | 17 | namespace pp { |
| 18 | |
[email protected] | 5a3f6285 | 2010-11-10 21:43:01 | [diff] [blame] | 19 | namespace { |
| 20 | |
[email protected] | 1c7e5c9 | 2012-03-22 00:58:28 | [diff] [blame] | 21 | template <> const char* interface_name<PPB_URLLoader_1_0>() { |
| 22 | return PPB_URLLOADER_INTERFACE_1_0; |
[email protected] | 6b7550a | 2010-12-20 19:03:07 | [diff] [blame] | 23 | } |
[email protected] | 5a3f6285 | 2010-11-10 21:43:01 | [diff] [blame] | 24 | |
| 25 | } // namespace |
| 26 | |
[email protected] | 5a3f6285 | 2010-11-10 21:43:01 | [diff] [blame] | 27 | URLLoader::URLLoader(PP_Resource resource) : Resource(resource) { |
| 28 | } |
| 29 | |
[email protected] | 09af0f7 | 2012-02-27 20:23:19 | [diff] [blame] | 30 | URLLoader::URLLoader(const InstanceHandle& instance) { |
[email protected] | 1c7e5c9 | 2012-03-22 00:58:28 | [diff] [blame] | 31 | if (!has_interface<PPB_URLLoader_1_0>()) |
[email protected] | 5a3f6285 | 2010-11-10 21:43:01 | [diff] [blame] | 32 | return; |
[email protected] | 1c7e5c9 | 2012-03-22 00:58:28 | [diff] [blame] | 33 | PassRefFromConstructor(get_interface<PPB_URLLoader_1_0>()->Create( |
[email protected] | 6b7550a | 2010-12-20 19:03:07 | [diff] [blame] | 34 | instance.pp_instance())); |
[email protected] | 5a3f6285 | 2010-11-10 21:43:01 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | URLLoader::URLLoader(const URLLoader& other) : Resource(other) { |
| 38 | } |
| 39 | |
| 40 | int32_t URLLoader::Open(const URLRequestInfo& request_info, |
| 41 | const CompletionCallback& cc) { |
[email protected] | 1c7e5c9 | 2012-03-22 00:58:28 | [diff] [blame] | 42 | if (!has_interface<PPB_URLLoader_1_0>()) |
[email protected] | 917e86a | 2011-06-30 21:42:37 | [diff] [blame] | 43 | return cc.MayForce(PP_ERROR_NOINTERFACE); |
[email protected] | 1c7e5c9 | 2012-03-22 00:58:28 | [diff] [blame] | 44 | return get_interface<PPB_URLLoader_1_0>()->Open(pp_resource(), |
[email protected] | 6b7550a | 2010-12-20 19:03:07 | [diff] [blame] | 45 | request_info.pp_resource(), |
| 46 | cc.pp_completion_callback()); |
[email protected] | 5a3f6285 | 2010-11-10 21:43:01 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | int32_t URLLoader::FollowRedirect(const CompletionCallback& cc) { |
[email protected] | 1c7e5c9 | 2012-03-22 00:58:28 | [diff] [blame] | 50 | if (!has_interface<PPB_URLLoader_1_0>()) |
[email protected] | 917e86a | 2011-06-30 21:42:37 | [diff] [blame] | 51 | return cc.MayForce(PP_ERROR_NOINTERFACE); |
[email protected] | 1c7e5c9 | 2012-03-22 00:58:28 | [diff] [blame] | 52 | return get_interface<PPB_URLLoader_1_0>()->FollowRedirect( |
[email protected] | 6b7550a | 2010-12-20 19:03:07 | [diff] [blame] | 53 | pp_resource(), cc.pp_completion_callback()); |
[email protected] | 5a3f6285 | 2010-11-10 21:43:01 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | bool URLLoader::GetUploadProgress(int64_t* bytes_sent, |
| 57 | int64_t* total_bytes_to_be_sent) const { |
[email protected] | 1c7e5c9 | 2012-03-22 00:58:28 | [diff] [blame] | 58 | if (!has_interface<PPB_URLLoader_1_0>()) |
[email protected] | 5a3f6285 | 2010-11-10 21:43:01 | [diff] [blame] | 59 | return false; |
[email protected] | 1c7e5c9 | 2012-03-22 00:58:28 | [diff] [blame] | 60 | return PP_ToBool(get_interface<PPB_URLLoader_1_0>()->GetUploadProgress( |
[email protected] | 6b7550a | 2010-12-20 19:03:07 | [diff] [blame] | 61 | pp_resource(), bytes_sent, total_bytes_to_be_sent)); |
[email protected] | 5a3f6285 | 2010-11-10 21:43:01 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | bool URLLoader::GetDownloadProgress( |
| 65 | int64_t* bytes_received, |
| 66 | int64_t* total_bytes_to_be_received) const { |
[email protected] | 1c7e5c9 | 2012-03-22 00:58:28 | [diff] [blame] | 67 | if (!has_interface<PPB_URLLoader_1_0>()) |
[email protected] | 5a3f6285 | 2010-11-10 21:43:01 | [diff] [blame] | 68 | return false; |
[email protected] | 1c7e5c9 | 2012-03-22 00:58:28 | [diff] [blame] | 69 | return PP_ToBool(get_interface<PPB_URLLoader_1_0>()->GetDownloadProgress( |
[email protected] | 8a855a0 | 2011-07-08 05:22:45 | [diff] [blame] | 70 | pp_resource(), bytes_received, total_bytes_to_be_received)); |
[email protected] | 5a3f6285 | 2010-11-10 21:43:01 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | URLResponseInfo URLLoader::GetResponseInfo() const { |
[email protected] | 1c7e5c9 | 2012-03-22 00:58:28 | [diff] [blame] | 74 | if (!has_interface<PPB_URLLoader_1_0>()) |
[email protected] | 5a3f6285 | 2010-11-10 21:43:01 | [diff] [blame] | 75 | return URLResponseInfo(); |
[email protected] | 09af0f7 | 2012-02-27 20:23:19 | [diff] [blame] | 76 | return URLResponseInfo(PASS_REF, |
[email protected] | 1c7e5c9 | 2012-03-22 00:58:28 | [diff] [blame] | 77 | get_interface<PPB_URLLoader_1_0>()->GetResponseInfo( |
[email protected] | 6b7550a | 2010-12-20 19:03:07 | [diff] [blame] | 78 | pp_resource())); |
[email protected] | 5a3f6285 | 2010-11-10 21:43:01 | [diff] [blame] | 79 | } |
| 80 | |
[email protected] | 748ce71 | 2011-03-28 22:15:13 | [diff] [blame] | 81 | int32_t URLLoader::ReadResponseBody(void* buffer, |
[email protected] | 5a3f6285 | 2010-11-10 21:43:01 | [diff] [blame] | 82 | int32_t bytes_to_read, |
| 83 | const CompletionCallback& cc) { |
[email protected] | 1c7e5c9 | 2012-03-22 00:58:28 | [diff] [blame] | 84 | if (!has_interface<PPB_URLLoader_1_0>()) |
[email protected] | 917e86a | 2011-06-30 21:42:37 | [diff] [blame] | 85 | return cc.MayForce(PP_ERROR_NOINTERFACE); |
[email protected] | 1c7e5c9 | 2012-03-22 00:58:28 | [diff] [blame] | 86 | return get_interface<PPB_URLLoader_1_0>()->ReadResponseBody( |
[email protected] | 6b7550a | 2010-12-20 19:03:07 | [diff] [blame] | 87 | pp_resource(), buffer, bytes_to_read, cc.pp_completion_callback()); |
[email protected] | 5a3f6285 | 2010-11-10 21:43:01 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | int32_t URLLoader::FinishStreamingToFile(const CompletionCallback& cc) { |
[email protected] | 1c7e5c9 | 2012-03-22 00:58:28 | [diff] [blame] | 91 | if (!has_interface<PPB_URLLoader_1_0>()) |
[email protected] | 917e86a | 2011-06-30 21:42:37 | [diff] [blame] | 92 | return cc.MayForce(PP_ERROR_NOINTERFACE); |
[email protected] | 1c7e5c9 | 2012-03-22 00:58:28 | [diff] [blame] | 93 | return get_interface<PPB_URLLoader_1_0>()->FinishStreamingToFile( |
[email protected] | 6b7550a | 2010-12-20 19:03:07 | [diff] [blame] | 94 | pp_resource(), cc.pp_completion_callback()); |
[email protected] | 5a3f6285 | 2010-11-10 21:43:01 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | void URLLoader::Close() { |
[email protected] | 1c7e5c9 | 2012-03-22 00:58:28 | [diff] [blame] | 98 | if (!has_interface<PPB_URLLoader_1_0>()) |
[email protected] | 5a3f6285 | 2010-11-10 21:43:01 | [diff] [blame] | 99 | return; |
[email protected] | 1c7e5c9 | 2012-03-22 00:58:28 | [diff] [blame] | 100 | get_interface<PPB_URLLoader_1_0>()->Close(pp_resource()); |
[email protected] | 5a3f6285 | 2010-11-10 21:43:01 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | } // namespace pp |