OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011 The Native Client 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 #include "native_client/tests/fake_browser_ppapi/fake_url_loader.h" | |
6 | |
7 #include <string> | |
8 | |
9 #include "native_client/src/include/nacl_macros.h" | |
10 #include "native_client/src/include/portability.h" | |
11 | |
12 #include "native_client/tests/fake_browser_ppapi/fake_file_ref.h" | |
13 #include "native_client/tests/fake_browser_ppapi/fake_resource.h" | |
14 #include "native_client/tests/fake_browser_ppapi/fake_url_request_info.h" | |
15 #include "native_client/tests/fake_browser_ppapi/fake_url_response_info.h" | |
16 #include "native_client/tests/fake_browser_ppapi/utility.h" | |
17 | |
18 #include "ppapi/c/pp_errors.h" | |
19 #include "ppapi/c/pp_completion_callback.h" | |
20 #include "ppapi/c/pp_resource.h" | |
21 | |
22 using fake_browser_ppapi::DebugPrintf; | |
23 | |
24 namespace fake_browser_ppapi { | |
25 | |
26 std::string g_nacl_ppapi_url_path = NACL_NO_URL; | |
27 std::string g_nacl_ppapi_local_path = NACL_NO_FILE_PATH; | |
28 | |
29 namespace { | |
30 | |
31 PP_Resource Create(PP_Instance instance_id) { | |
32 DebugPrintf("URLLoader::Create: instance_id=%"NACL_PRId32"\n", instance_id); | |
33 URLLoader* loader = new URLLoader(instance_id); | |
34 PP_Resource resource_id = TrackResource(loader); | |
35 DebugPrintf("URLLoader::Create: resource_id=%"NACL_PRId32"\n", resource_id); | |
36 return resource_id; | |
37 } | |
38 | |
39 PP_Bool IsURLLoader(PP_Resource resource_id) { | |
40 DebugPrintf("URLLoader::IsURLLoader: resource_id=%"NACL_PRId32"\n", | |
41 resource_id); | |
42 NACL_UNIMPLEMENTED(); | |
43 return PP_FALSE; | |
44 } | |
45 | |
46 int32_t Open(PP_Resource loader_id, | |
47 PP_Resource request_id, | |
48 struct PP_CompletionCallback callback) { | |
49 DebugPrintf("URLLoader::Open: loader_id=%"NACL_PRIu64 | |
50 " request_id=%"NACL_PRId32"\n", loader_id, request_id); | |
51 | |
52 URLLoader* loader = GetResource(loader_id)->AsURLLoader(); | |
53 URLRequestInfo* request = GetResource(request_id)->AsURLRequestInfo(); | |
54 if (loader == NULL || request == NULL) | |
55 return PP_ERROR_BADRESOURCE; | |
56 loader->set_request(request); | |
57 | |
58 // We use stream-as-file mode only, so Open will be aimed at that. | |
59 CHECK(request->stream_to_file()); | |
60 | |
61 // We fake-open the url. main.cc must provide the full url for this to work. | |
62 if (g_nacl_ppapi_url_path == NACL_NO_URL) | |
63 return PP_ERROR_FAILED; | |
64 | |
65 // Set up the response. | |
66 URLResponseInfo* response = new URLResponseInfo(request->instance_id()); | |
67 if (request->url().find(g_nacl_ppapi_url_path) == 0) { | |
68 // It was already an absolute URL. | |
69 response->set_url(request->url()); | |
70 } else { | |
71 response->set_url(g_nacl_ppapi_url_path + request->url()); | |
72 } | |
73 response->set_status_code(NACL_HTTP_STATUS_OK); | |
74 loader->set_response(response); | |
75 PP_Resource response_id = TrackResource(response); | |
76 DebugPrintf("URLLoader::Open: response_id=%"NACL_PRId32"\n", response_id); | |
77 | |
78 // Invoke the callback right away to simplify mocking. | |
79 if (callback.func == NULL) | |
80 return PP_ERROR_BADARGUMENT; | |
81 PP_RunCompletionCallback(&callback, PP_OK); | |
82 return PP_OK_COMPLETIONPENDING; // Fake successful async call. | |
83 } | |
84 | |
85 int32_t FollowRedirect(PP_Resource loader_id, | |
86 struct PP_CompletionCallback callback) { | |
87 DebugPrintf("URLLoader::FollowRedirect: loader_id=%"NACL_PRId32"\n", | |
88 loader_id); | |
89 UNREFERENCED_PARAMETER(callback); | |
90 NACL_UNIMPLEMENTED(); | |
91 return PP_ERROR_BADRESOURCE; | |
92 } | |
93 | |
94 PP_Bool GetUploadProgress(PP_Resource loader_id, | |
95 int64_t* bytes_sent, | |
96 int64_t* total_bytes_to_be_sent) { | |
97 DebugPrintf("URLLoader::GetUploadProgress: loader_id=%"NACL_PRId32"\n", | |
98 loader_id); | |
99 UNREFERENCED_PARAMETER(bytes_sent); | |
100 UNREFERENCED_PARAMETER(total_bytes_to_be_sent); | |
101 NACL_UNIMPLEMENTED(); | |
102 return PP_FALSE; | |
103 } | |
104 | |
105 PP_Bool GetDownloadProgress(PP_Resource loader_id, | |
106 int64_t* bytes_received, | |
107 int64_t* total_bytes_to_be_received) { | |
108 DebugPrintf("URLLoader::GetDownloadProgress: loader_id=%"NACL_PRId32"\n", | |
109 loader_id); | |
110 UNREFERENCED_PARAMETER(bytes_received); | |
111 UNREFERENCED_PARAMETER(total_bytes_to_be_received); | |
112 NACL_UNIMPLEMENTED(); | |
113 return PP_FALSE; | |
114 } | |
115 | |
116 PP_Resource GetResponseInfo(PP_Resource loader_id) { | |
117 DebugPrintf("URLLoader::GetResponseInfo: loader_id=%"NACL_PRId32"\n", | |
118 loader_id); | |
119 URLLoader* loader = GetResource(loader_id)->AsURLLoader(); | |
120 if (loader == NULL) | |
121 return PP_ERROR_BADRESOURCE; | |
122 | |
123 URLResponseInfo* response = loader->response(); | |
124 CHECK(response != NULL); | |
125 return response->resource_id(); | |
126 } | |
127 | |
128 int32_t ReadResponseBody(PP_Resource loader_id, | |
129 void* buffer, | |
130 int32_t bytes_to_read, | |
131 struct PP_CompletionCallback callback) { | |
132 DebugPrintf("URLLoader::ReadResponseBody: loader_id=%"NACL_PRId32"\n", | |
133 loader_id); | |
134 UNREFERENCED_PARAMETER(buffer); | |
135 UNREFERENCED_PARAMETER(bytes_to_read); | |
136 UNREFERENCED_PARAMETER(callback); | |
137 NACL_UNIMPLEMENTED(); | |
138 return PP_ERROR_BADRESOURCE; | |
139 } | |
140 | |
141 int32_t FinishStreamingToFile(PP_Resource loader_id, | |
142 struct PP_CompletionCallback callback) { | |
143 DebugPrintf("URLLoader::FinishStreamingToFile: loader_id=%"NACL_PRId32"\n", | |
144 loader_id); | |
145 | |
146 URLLoader* loader = GetResource(loader_id)->AsURLLoader(); | |
147 if (loader == NULL) | |
148 return PP_ERROR_BADRESOURCE; | |
149 URLRequestInfo* request = loader->request(); | |
150 URLResponseInfo* response = loader->response(); | |
151 CHECK(request != NULL && response != NULL); | |
152 | |
153 // We fake-stream the file. main.cc must provide the path for this to work. | |
154 // Note that will only work if embed uses a relative nexe url. | |
155 // TODO(polina): generalize this to work with full urls as well? | |
156 if (g_nacl_ppapi_local_path == NACL_NO_FILE_PATH) | |
157 return PP_ERROR_FAILED; | |
158 std::string local_file = | |
159 g_nacl_ppapi_local_path + "/" + | |
160 request->url().substr(g_nacl_ppapi_url_path.size() + 1); | |
161 | |
162 // Set up the the file object corresponding to the response body. | |
163 FileRef* file_ref = new FileRef(local_file); | |
164 response->set_body_as_file_ref(file_ref); | |
165 PP_Resource file_ref_id = TrackResource(file_ref); | |
166 DebugPrintf("URLLoader::FinishStreamingToFile: file_ref_id=%"NACL_PRId32"\n", | |
167 file_ref_id); | |
168 | |
169 // Call the callback right away to simplify mocking. | |
170 if (callback.func == NULL) | |
171 return PP_ERROR_BADARGUMENT; | |
172 PP_RunCompletionCallback(&callback, PP_OK); | |
173 return PP_OK_COMPLETIONPENDING; // Fake successful async call. | |
174 } | |
175 | |
176 void Close(PP_Resource loader_id) { | |
177 DebugPrintf("URLLoader::ReadResponseBody: loader_id=%"NACL_PRId32"\n", | |
178 loader_id); | |
179 NACL_UNIMPLEMENTED(); | |
180 } | |
181 | |
182 } // namespace | |
183 | |
184 | |
185 const PPB_URLLoader* URLLoader::GetInterface() { | |
186 static const PPB_URLLoader url_loader_interface = { | |
187 Create, | |
188 IsURLLoader, | |
189 Open, | |
190 FollowRedirect, | |
191 GetUploadProgress, | |
192 GetDownloadProgress, | |
193 GetResponseInfo, | |
194 ReadResponseBody, | |
195 FinishStreamingToFile, | |
196 Close | |
197 }; | |
198 return &url_loader_interface; | |
199 } | |
200 | |
201 } // namespace fake_browser_ppapi | |
OLD | NEW |