1 // Copyright 2014 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 #ifndef TESTS_NACL_IO_TEST_FAKE_PEPPER_INTERFACE_URL_LOADER_H_
6 #define TESTS_NACL_IO_TEST_FAKE_PEPPER_INTERFACE_URL_LOADER_H_
12 #include "fake_ppapi/fake_core_interface.h"
13 #include "fake_ppapi/fake_var_interface.h"
14 #include "fake_ppapi/fake_var_manager.h"
15 #include "nacl_io/pepper_interface_dummy.h"
16 #include "sdk_util/macros.h"
18 class FakeURLLoaderEntity {
20 explicit FakeURLLoaderEntity(const std::string& body);
22 const std::string& body() const { return body_; }
28 class FakeURLLoaderServer {
30 FakeURLLoaderServer();
33 bool AddEntity(const std::string& url,
34 const std::string& body,
35 FakeURLLoaderEntity** out_entity);
36 bool AddError(const std::string& url,
37 int http_status_code);
38 FakeURLLoaderEntity* GetEntity(const std::string& url);
39 // Returns 0 if the url is not in the error map.
40 int GetError(const std::string& url);
42 // The maximum number of bytes that ReadResponseBody will send. If 0, then
43 // send as many as are requested.
44 void set_max_read_size(size_t max_read_size) {
45 max_read_size_ = max_read_size;
48 // Whether to add the "Content-Length" header.
49 void set_send_content_length(bool send_content_length) {
50 send_content_length_ = send_content_length;
53 // Whether to allow partial reads (via the "Range" request header).
54 void set_allow_partial(bool allow_partial) { allow_partial_ = allow_partial; }
56 size_t max_read_size() const { return max_read_size_; }
57 bool send_content_length() const { return send_content_length_; }
58 bool allow_partial() const { return allow_partial_; }
61 typedef std::map<std::string, FakeURLLoaderEntity> EntityMap;
62 typedef std::map<std::string, int> ErrorMap;
63 EntityMap entity_map_;
65 size_t max_read_size_;
66 bool send_content_length_;
70 class FakeURLLoaderInterface : public nacl_io::URLLoaderInterface {
72 explicit FakeURLLoaderInterface(FakeCoreInterface* core_interface);
74 virtual PP_Resource Create(PP_Instance instance);
75 virtual int32_t Open(PP_Resource loader,
76 PP_Resource request_info,
77 PP_CompletionCallback callback);
78 virtual PP_Resource GetResponseInfo(PP_Resource loader);
79 virtual int32_t ReadResponseBody(PP_Resource loader,
81 int32_t bytes_to_read,
82 PP_CompletionCallback callback);
83 virtual void Close(PP_Resource loader);
86 FakeCoreInterface* core_interface_; // Weak reference.
88 DISALLOW_COPY_AND_ASSIGN(FakeURLLoaderInterface);
91 class FakeURLRequestInfoInterface : public nacl_io::URLRequestInfoInterface {
93 FakeURLRequestInfoInterface(FakeCoreInterface* core_interface,
94 FakeVarInterface* var_interface);
96 virtual PP_Resource Create(PP_Instance instance);
97 virtual PP_Bool SetProperty(PP_Resource request,
98 PP_URLRequestProperty property,
102 FakeCoreInterface* core_interface_; // Weak reference.
103 FakeVarInterface* var_interface_; // Weak reference.
105 DISALLOW_COPY_AND_ASSIGN(FakeURLRequestInfoInterface);
108 class FakeURLResponseInfoInterface : public nacl_io::URLResponseInfoInterface {
110 FakeURLResponseInfoInterface(FakeCoreInterface* core_interface,
111 FakeVarInterface* var_interface);
113 virtual PP_Var GetProperty(PP_Resource response,
114 PP_URLResponseProperty property);
117 FakeCoreInterface* core_interface_; // Weak reference.
118 FakeVarInterface* var_interface_; // Weak reference.
120 DISALLOW_COPY_AND_ASSIGN(FakeURLResponseInfoInterface);
123 class FakePepperInterfaceURLLoader : public nacl_io::PepperInterfaceDummy {
125 FakePepperInterfaceURLLoader();
126 FakePepperInterfaceURLLoader(const FakeURLLoaderServer& server);
127 ~FakePepperInterfaceURLLoader();
129 virtual PP_Instance GetInstance() { return instance_; }
130 virtual nacl_io::CoreInterface* GetCoreInterface();
131 virtual nacl_io::VarInterface* GetVarInterface();
132 virtual nacl_io::URLLoaderInterface* GetURLLoaderInterface();
133 virtual nacl_io::URLRequestInfoInterface* GetURLRequestInfoInterface();
134 virtual nacl_io::URLResponseInfoInterface* GetURLResponseInfoInterface();
136 FakeURLLoaderServer* server_template() { return &server_template_; }
141 FakeResourceManager resource_manager_;
142 FakeCoreInterface core_interface_;
143 FakeVarInterface var_interface_;
144 FakeVarManager var_manager_;
145 FakeURLLoaderServer server_template_;
146 FakeURLLoaderInterface url_loader_interface_;
147 FakeURLRequestInfoInterface url_request_info_interface_;
148 FakeURLResponseInfoInterface url_response_info_interface_;
149 PP_Instance instance_;
151 DISALLOW_COPY_AND_ASSIGN(FakePepperInterfaceURLLoader);
154 #endif // TESTS_NACL_IO_TEST_FAKE_PEPPER_INTERFACE_URL_LOADER_H_