blob: 1be87a777b05b5f1f04f23b6a7717a5933a697a9 [file] [log] [blame]
[email protected]1c7e5c92012-03-22 00:58:281// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]5a3f62852010-11-10 21:43:012// 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]5a3f62852010-11-10 21:43:019#include "ppapi/cpp/completion_callback.h"
[email protected]25ffe85f2011-07-13 01:04:5910#include "ppapi/cpp/file_ref.h"
[email protected]09af0f72012-02-27 20:23:1911#include "ppapi/cpp/instance_handle.h"
[email protected]5a3f62852010-11-10 21:43:0112#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]6b7550a2010-12-20 19:03:0717namespace pp {
18
[email protected]5a3f62852010-11-10 21:43:0119namespace {
20
[email protected]1c7e5c92012-03-22 00:58:2821template <> const char* interface_name<PPB_URLLoader_1_0>() {
22 return PPB_URLLOADER_INTERFACE_1_0;
[email protected]6b7550a2010-12-20 19:03:0723}
[email protected]5a3f62852010-11-10 21:43:0124
25} // namespace
26
[email protected]5a3f62852010-11-10 21:43:0127URLLoader::URLLoader(PP_Resource resource) : Resource(resource) {
28}
29
[email protected]09af0f72012-02-27 20:23:1930URLLoader::URLLoader(const InstanceHandle& instance) {
[email protected]1c7e5c92012-03-22 00:58:2831 if (!has_interface<PPB_URLLoader_1_0>())
[email protected]5a3f62852010-11-10 21:43:0132 return;
[email protected]1c7e5c92012-03-22 00:58:2833 PassRefFromConstructor(get_interface<PPB_URLLoader_1_0>()->Create(
[email protected]6b7550a2010-12-20 19:03:0734 instance.pp_instance()));
[email protected]5a3f62852010-11-10 21:43:0135}
36
37URLLoader::URLLoader(const URLLoader& other) : Resource(other) {
38}
39
40int32_t URLLoader::Open(const URLRequestInfo& request_info,
41 const CompletionCallback& cc) {
[email protected]1c7e5c92012-03-22 00:58:2842 if (!has_interface<PPB_URLLoader_1_0>())
[email protected]917e86a2011-06-30 21:42:3743 return cc.MayForce(PP_ERROR_NOINTERFACE);
[email protected]1c7e5c92012-03-22 00:58:2844 return get_interface<PPB_URLLoader_1_0>()->Open(pp_resource(),
[email protected]6b7550a2010-12-20 19:03:0745 request_info.pp_resource(),
46 cc.pp_completion_callback());
[email protected]5a3f62852010-11-10 21:43:0147}
48
49int32_t URLLoader::FollowRedirect(const CompletionCallback& cc) {
[email protected]1c7e5c92012-03-22 00:58:2850 if (!has_interface<PPB_URLLoader_1_0>())
[email protected]917e86a2011-06-30 21:42:3751 return cc.MayForce(PP_ERROR_NOINTERFACE);
[email protected]1c7e5c92012-03-22 00:58:2852 return get_interface<PPB_URLLoader_1_0>()->FollowRedirect(
[email protected]6b7550a2010-12-20 19:03:0753 pp_resource(), cc.pp_completion_callback());
[email protected]5a3f62852010-11-10 21:43:0154}
55
56bool URLLoader::GetUploadProgress(int64_t* bytes_sent,
57 int64_t* total_bytes_to_be_sent) const {
[email protected]1c7e5c92012-03-22 00:58:2858 if (!has_interface<PPB_URLLoader_1_0>())
[email protected]5a3f62852010-11-10 21:43:0159 return false;
[email protected]1c7e5c92012-03-22 00:58:2860 return PP_ToBool(get_interface<PPB_URLLoader_1_0>()->GetUploadProgress(
[email protected]6b7550a2010-12-20 19:03:0761 pp_resource(), bytes_sent, total_bytes_to_be_sent));
[email protected]5a3f62852010-11-10 21:43:0162}
63
64bool URLLoader::GetDownloadProgress(
65 int64_t* bytes_received,
66 int64_t* total_bytes_to_be_received) const {
[email protected]1c7e5c92012-03-22 00:58:2867 if (!has_interface<PPB_URLLoader_1_0>())
[email protected]5a3f62852010-11-10 21:43:0168 return false;
[email protected]1c7e5c92012-03-22 00:58:2869 return PP_ToBool(get_interface<PPB_URLLoader_1_0>()->GetDownloadProgress(
[email protected]8a855a02011-07-08 05:22:4570 pp_resource(), bytes_received, total_bytes_to_be_received));
[email protected]5a3f62852010-11-10 21:43:0171}
72
73URLResponseInfo URLLoader::GetResponseInfo() const {
[email protected]1c7e5c92012-03-22 00:58:2874 if (!has_interface<PPB_URLLoader_1_0>())
[email protected]5a3f62852010-11-10 21:43:0175 return URLResponseInfo();
[email protected]09af0f72012-02-27 20:23:1976 return URLResponseInfo(PASS_REF,
[email protected]1c7e5c92012-03-22 00:58:2877 get_interface<PPB_URLLoader_1_0>()->GetResponseInfo(
[email protected]6b7550a2010-12-20 19:03:0778 pp_resource()));
[email protected]5a3f62852010-11-10 21:43:0179}
80
[email protected]748ce712011-03-28 22:15:1381int32_t URLLoader::ReadResponseBody(void* buffer,
[email protected]5a3f62852010-11-10 21:43:0182 int32_t bytes_to_read,
83 const CompletionCallback& cc) {
[email protected]1c7e5c92012-03-22 00:58:2884 if (!has_interface<PPB_URLLoader_1_0>())
[email protected]917e86a2011-06-30 21:42:3785 return cc.MayForce(PP_ERROR_NOINTERFACE);
[email protected]1c7e5c92012-03-22 00:58:2886 return get_interface<PPB_URLLoader_1_0>()->ReadResponseBody(
[email protected]6b7550a2010-12-20 19:03:0787 pp_resource(), buffer, bytes_to_read, cc.pp_completion_callback());
[email protected]5a3f62852010-11-10 21:43:0188}
89
90int32_t URLLoader::FinishStreamingToFile(const CompletionCallback& cc) {
[email protected]1c7e5c92012-03-22 00:58:2891 if (!has_interface<PPB_URLLoader_1_0>())
[email protected]917e86a2011-06-30 21:42:3792 return cc.MayForce(PP_ERROR_NOINTERFACE);
[email protected]1c7e5c92012-03-22 00:58:2893 return get_interface<PPB_URLLoader_1_0>()->FinishStreamingToFile(
[email protected]6b7550a2010-12-20 19:03:0794 pp_resource(), cc.pp_completion_callback());
[email protected]5a3f62852010-11-10 21:43:0195}
96
97void URLLoader::Close() {
[email protected]1c7e5c92012-03-22 00:58:2898 if (!has_interface<PPB_URLLoader_1_0>())
[email protected]5a3f62852010-11-10 21:43:0199 return;
[email protected]1c7e5c92012-03-22 00:58:28100 get_interface<PPB_URLLoader_1_0>()->Close(pp_resource());
[email protected]5a3f62852010-11-10 21:43:01101}
102
103} // namespace pp