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