fake_pepper_interface_url_loader.h revision 5d1f7b1de12d16ceb2c938c56701a3e8bfa558f7
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
22  const std::string& body() const { return body_; }
23
24 private:
25  std::string body_;
26};
27
28class FakeURLLoaderServer {
29 public:
30  FakeURLLoaderServer();
31
32  void Clear();
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);
41
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;
46  }
47
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;
51  }
52
53  // Whether to allow partial reads (via the "Range" request header).
54  void set_allow_partial(bool allow_partial) { allow_partial_ = allow_partial; }
55
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_; }
59
60 private:
61  typedef std::map<std::string, FakeURLLoaderEntity> EntityMap;
62  typedef std::map<std::string, int> ErrorMap;
63  EntityMap entity_map_;
64  ErrorMap error_map_;
65  size_t max_read_size_;
66  bool send_content_length_;
67  bool allow_partial_;
68};
69
70class FakeURLLoaderInterface : public nacl_io::URLLoaderInterface {
71 public:
72  explicit FakeURLLoaderInterface(FakeCoreInterface* core_interface);
73
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,
80                                   void* buffer,
81                                   int32_t bytes_to_read,
82                                   PP_CompletionCallback callback);
83  virtual void Close(PP_Resource loader);
84
85 private:
86  FakeCoreInterface* core_interface_;  // Weak reference.
87
88  DISALLOW_COPY_AND_ASSIGN(FakeURLLoaderInterface);
89};
90
91class FakeURLRequestInfoInterface : public nacl_io::URLRequestInfoInterface {
92 public:
93  FakeURLRequestInfoInterface(FakeCoreInterface* core_interface,
94                              FakeVarInterface* var_interface);
95
96  virtual PP_Resource Create(PP_Instance instance);
97  virtual PP_Bool SetProperty(PP_Resource request,
98                              PP_URLRequestProperty property,
99                              PP_Var value);
100
101 private:
102  FakeCoreInterface* core_interface_;  // Weak reference.
103  FakeVarInterface* var_interface_;  // Weak reference.
104
105  DISALLOW_COPY_AND_ASSIGN(FakeURLRequestInfoInterface);
106};
107
108class FakeURLResponseInfoInterface : public nacl_io::URLResponseInfoInterface {
109 public:
110  FakeURLResponseInfoInterface(FakeCoreInterface* core_interface,
111                               FakeVarInterface* var_interface);
112
113  virtual PP_Var GetProperty(PP_Resource response,
114                             PP_URLResponseProperty property);
115
116 private:
117  FakeCoreInterface* core_interface_;  // Weak reference.
118  FakeVarInterface* var_interface_;  // Weak reference.
119
120  DISALLOW_COPY_AND_ASSIGN(FakeURLResponseInfoInterface);
121};
122
123class FakePepperInterfaceURLLoader : public nacl_io::PepperInterfaceDummy {
124 public:
125  FakePepperInterfaceURLLoader();
126  FakePepperInterfaceURLLoader(const FakeURLLoaderServer& server);
127  ~FakePepperInterfaceURLLoader();
128
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();
135
136  FakeURLLoaderServer* server_template() { return &server_template_; }
137
138 private:
139  void Init();
140
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_;
150
151  DISALLOW_COPY_AND_ASSIGN(FakePepperInterfaceURLLoader);
152};
153
154#endif  // TESTS_NACL_IO_TEST_FAKE_PEPPER_INTERFACE_URL_LOADER_H_
155