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