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. 4 5 #ifndef TESTS_NACL_IO_TEST_FAKE_PEPPER_INTERFACE_URL_LOADER_H_ 6 #define TESTS_NACL_IO_TEST_FAKE_PEPPER_INTERFACE_URL_LOADER_H_ 7 8 #include <map> 9 #include <string> 10 #include <vector> 11 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" 17 18 class FakeURLLoaderEntity { 19 public: 20 explicit FakeURLLoaderEntity(const std::string& body); 21 FakeURLLoaderEntity(const std::string& to_repeat, off_t size); 22 23 const std::string& body() const { return body_; } 24 off_t size() { return size_; } 25 26 size_t Read(void* buffer, size_t count, off_t offset); 27 28 private: 29 std::string body_; 30 off_t size_; 31 bool repeat_; 32 }; 33 34 class FakeURLLoaderServer { 35 public: 36 FakeURLLoaderServer(); 37 38 void Clear(); 39 bool AddEntity(const std::string& url, 40 const std::string& body, 41 FakeURLLoaderEntity** out_entity); 42 bool AddEntity(const std::string& url, 43 const std::string& body, 44 off_t size, 45 FakeURLLoaderEntity** out_entity); 46 bool AddError(const std::string& url, 47 int http_status_code); 48 FakeURLLoaderEntity* GetEntity(const std::string& url); 49 // Returns 0 if the url is not in the error map. 50 int GetError(const std::string& url); 51 52 // The maximum number of bytes that ReadResponseBody will send. If 0, then 53 // send as many as are requested. 54 void set_max_read_size(size_t max_read_size) { 55 max_read_size_ = max_read_size; 56 } 57 58 // Whether to add the "Content-Length" header. 59 void set_send_content_length(bool send_content_length) { 60 send_content_length_ = send_content_length; 61 } 62 63 // Whether to allow partial reads (via the "Range" request header). 64 void set_allow_partial(bool allow_partial) { allow_partial_ = allow_partial; } 65 66 size_t max_read_size() const { return max_read_size_; } 67 bool send_content_length() const { return send_content_length_; } 68 bool allow_partial() const { return allow_partial_; } 69 70 private: 71 typedef std::map<std::string, FakeURLLoaderEntity> EntityMap; 72 typedef std::map<std::string, int> ErrorMap; 73 EntityMap entity_map_; 74 ErrorMap error_map_; 75 size_t max_read_size_; 76 bool send_content_length_; 77 bool allow_partial_; 78 }; 79 80 class FakeURLLoaderInterface : public nacl_io::URLLoaderInterface { 81 public: 82 explicit FakeURLLoaderInterface(FakeCoreInterface* core_interface); 83 84 virtual PP_Resource Create(PP_Instance instance); 85 virtual int32_t Open(PP_Resource loader, 86 PP_Resource request_info, 87 PP_CompletionCallback callback); 88 virtual PP_Resource GetResponseInfo(PP_Resource loader); 89 virtual int32_t ReadResponseBody(PP_Resource loader, 90 void* buffer, 91 int32_t bytes_to_read, 92 PP_CompletionCallback callback); 93 virtual void Close(PP_Resource loader); 94 95 private: 96 FakeCoreInterface* core_interface_; // Weak reference. 97 98 DISALLOW_COPY_AND_ASSIGN(FakeURLLoaderInterface); 99 }; 100 101 class FakeURLRequestInfoInterface : public nacl_io::URLRequestInfoInterface { 102 public: 103 FakeURLRequestInfoInterface(FakeCoreInterface* core_interface, 104 FakeVarInterface* var_interface); 105 106 virtual PP_Resource Create(PP_Instance instance); 107 virtual PP_Bool SetProperty(PP_Resource request, 108 PP_URLRequestProperty property, 109 PP_Var value); 110 111 private: 112 FakeCoreInterface* core_interface_; // Weak reference. 113 FakeVarInterface* var_interface_; // Weak reference. 114 115 DISALLOW_COPY_AND_ASSIGN(FakeURLRequestInfoInterface); 116 }; 117 118 class FakeURLResponseInfoInterface : public nacl_io::URLResponseInfoInterface { 119 public: 120 FakeURLResponseInfoInterface(FakeCoreInterface* core_interface, 121 FakeVarInterface* var_interface); 122 123 virtual PP_Var GetProperty(PP_Resource response, 124 PP_URLResponseProperty property); 125 126 private: 127 FakeCoreInterface* core_interface_; // Weak reference. 128 FakeVarInterface* var_interface_; // Weak reference. 129 130 DISALLOW_COPY_AND_ASSIGN(FakeURLResponseInfoInterface); 131 }; 132 133 class FakePepperInterfaceURLLoader : public nacl_io::PepperInterfaceDummy { 134 public: 135 FakePepperInterfaceURLLoader(); 136 FakePepperInterfaceURLLoader(const FakeURLLoaderServer& server); 137 ~FakePepperInterfaceURLLoader(); 138 139 virtual PP_Instance GetInstance() { return instance_; } 140 virtual nacl_io::CoreInterface* GetCoreInterface(); 141 virtual nacl_io::VarInterface* GetVarInterface(); 142 virtual nacl_io::URLLoaderInterface* GetURLLoaderInterface(); 143 virtual nacl_io::URLRequestInfoInterface* GetURLRequestInfoInterface(); 144 virtual nacl_io::URLResponseInfoInterface* GetURLResponseInfoInterface(); 145 146 FakeURLLoaderServer* server_template() { return &server_template_; } 147 148 private: 149 void Init(); 150 151 FakeResourceManager resource_manager_; 152 FakeCoreInterface core_interface_; 153 FakeVarInterface var_interface_; 154 FakeVarManager var_manager_; 155 FakeURLLoaderServer server_template_; 156 FakeURLLoaderInterface url_loader_interface_; 157 FakeURLRequestInfoInterface url_request_info_interface_; 158 FakeURLResponseInfoInterface url_response_info_interface_; 159 PP_Instance instance_; 160 161 DISALLOW_COPY_AND_ASSIGN(FakePepperInterfaceURLLoader); 162 }; 163 164 #endif // TESTS_NACL_IO_TEST_FAKE_PEPPER_INTERFACE_URL_LOADER_H_ 165