fake_pepper_interface_url_loader.h revision 116680a4aac90f2aa7413d9095a592090648e557
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
18class 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
34class 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  // Similar to AddEntity, but also allows partial requests and disallows HEAD
47  // requests.
48  bool SetBlobEntity(const std::string& url,
49                     const std::string& body,
50                     FakeURLLoaderEntity** out_entity);
51  bool AddError(const std::string& url,
52                int http_status_code);
53  FakeURLLoaderEntity* GetEntity(const std::string& url);
54  // Returns 0 if the url is not in the error map.
55  int GetError(const std::string& url);
56
57  // The maximum number of bytes that ReadResponseBody will send. If 0, then
58  // send as many as are requested.
59  void set_max_read_size(size_t max_read_size) {
60    max_read_size_ = max_read_size;
61  }
62
63  // Whether to add the "Content-Length" header.
64  void set_send_content_length(bool send_content_length) {
65    send_content_length_ = send_content_length;
66  }
67
68  // Whether to allow partial reads (via the "Range" request header).
69  void set_allow_partial(bool allow_partial) { allow_partial_ = allow_partial; }
70
71  // Whether to allow HEAD requests.
72  void set_allow_head(bool allow_head) { allow_head_ = allow_head; }
73
74  size_t max_read_size() const { return max_read_size_; }
75  bool send_content_length() const { return send_content_length_; }
76  bool allow_partial() const { return allow_partial_; }
77  bool allow_head() const { return allow_head_; }
78
79 private:
80  typedef std::map<std::string, FakeURLLoaderEntity> EntityMap;
81  typedef std::map<std::string, int> ErrorMap;
82  EntityMap entity_map_;
83  ErrorMap error_map_;
84  size_t max_read_size_;
85  bool send_content_length_;
86  bool allow_partial_;
87  bool allow_head_;
88};
89
90class FakeURLLoaderInterface : public nacl_io::URLLoaderInterface {
91 public:
92  explicit FakeURLLoaderInterface(FakeCoreInterface* core_interface);
93
94  virtual PP_Resource Create(PP_Instance instance);
95  virtual int32_t Open(PP_Resource loader,
96                       PP_Resource request_info,
97                       PP_CompletionCallback callback);
98  virtual PP_Resource GetResponseInfo(PP_Resource loader);
99  virtual int32_t ReadResponseBody(PP_Resource loader,
100                                   void* buffer,
101                                   int32_t bytes_to_read,
102                                   PP_CompletionCallback callback);
103  virtual void Close(PP_Resource loader);
104
105 private:
106  FakeCoreInterface* core_interface_;  // Weak reference.
107
108  DISALLOW_COPY_AND_ASSIGN(FakeURLLoaderInterface);
109};
110
111class FakeURLRequestInfoInterface : public nacl_io::URLRequestInfoInterface {
112 public:
113  FakeURLRequestInfoInterface(FakeCoreInterface* core_interface,
114                              FakeVarInterface* var_interface);
115
116  virtual PP_Resource Create(PP_Instance instance);
117  virtual PP_Bool SetProperty(PP_Resource request,
118                              PP_URLRequestProperty property,
119                              PP_Var value);
120
121 private:
122  FakeCoreInterface* core_interface_;  // Weak reference.
123  FakeVarInterface* var_interface_;  // Weak reference.
124
125  DISALLOW_COPY_AND_ASSIGN(FakeURLRequestInfoInterface);
126};
127
128class FakeURLResponseInfoInterface : public nacl_io::URLResponseInfoInterface {
129 public:
130  FakeURLResponseInfoInterface(FakeCoreInterface* core_interface,
131                               FakeVarInterface* var_interface);
132
133  virtual PP_Var GetProperty(PP_Resource response,
134                             PP_URLResponseProperty property);
135
136 private:
137  FakeCoreInterface* core_interface_;  // Weak reference.
138  FakeVarInterface* var_interface_;  // Weak reference.
139
140  DISALLOW_COPY_AND_ASSIGN(FakeURLResponseInfoInterface);
141};
142
143class FakePepperInterfaceURLLoader : public nacl_io::PepperInterfaceDummy {
144 public:
145  FakePepperInterfaceURLLoader();
146  FakePepperInterfaceURLLoader(const FakeURLLoaderServer& server);
147  ~FakePepperInterfaceURLLoader();
148
149  virtual PP_Instance GetInstance() { return instance_; }
150  virtual nacl_io::CoreInterface* GetCoreInterface();
151  virtual nacl_io::VarInterface* GetVarInterface();
152  virtual nacl_io::URLLoaderInterface* GetURLLoaderInterface();
153  virtual nacl_io::URLRequestInfoInterface* GetURLRequestInfoInterface();
154  virtual nacl_io::URLResponseInfoInterface* GetURLResponseInfoInterface();
155
156  FakeURLLoaderServer* server_template() { return &server_template_; }
157
158 private:
159  void Init();
160
161  FakeResourceManager resource_manager_;
162  FakeCoreInterface core_interface_;
163  FakeVarInterface var_interface_;
164  FakeVarManager var_manager_;
165  FakeURLLoaderServer server_template_;
166  FakeURLLoaderInterface url_loader_interface_;
167  FakeURLRequestInfoInterface url_request_info_interface_;
168  FakeURLResponseInfoInterface url_response_info_interface_;
169  PP_Instance instance_;
170
171  DISALLOW_COPY_AND_ASSIGN(FakePepperInterfaceURLLoader);
172};
173
174#endif  // TESTS_NACL_IO_TEST_FAKE_PEPPER_INTERFACE_URL_LOADER_H_
175