1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
|
// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/test/chrome_plugin/test_chrome_plugin.h"
#include "base/at_exit.h"
#include "base/basictypes.h"
#include "base/logging.h"
#include "base/message_loop.h"
#include "base/string_util.h"
#include "chrome/common/chrome_plugin_api.h"
#include "googleurl/src/gurl.h"
static CPID g_cpid;
static CPBrowserFuncs g_cpbrowser_funcs;
static CPRequestFuncs g_cprequest_funcs;
static CPResponseFuncs g_cpresponse_funcs;
static TestFuncParams::BrowserFuncs g_cptest_funcs;
// Create a global AtExitManager so that our code can use code from base that
// uses Singletons, for example. We don't care about static constructors here.
static base::AtExitManager global_at_exit_manager;
const TestResponsePayload* FindPayload(const char* url) {
for (int i = 0; i < arraysize(kChromeTestPluginPayloads); ++i) {
if (strcmp(kChromeTestPluginPayloads[i].url, url) == 0)
return &kChromeTestPluginPayloads[i];
}
return NULL;
}
std::string GetPayloadHeaders(const TestResponsePayload* payload) {
return StringPrintf(
"HTTP/1.1 200 OK%c"
"Content-type: %s%c"
"%c", 0, payload->mime_type, 0, 0);
}
void STDCALL InvokeLaterCallback(void* data) {
Task* task = static_cast<Task*>(data);
task->Run();
delete task;
}
// ResponseStream: Manages the streaming of the payload data.
class ResponseStream : public base::RefCounted<ResponseStream> {
public:
ResponseStream(const TestResponsePayload* payload, CPRequest* request);
~ResponseStream() {
request_->pdata = NULL;
}
void Init();
int GetResponseInfo(CPResponseInfoType type, void* buf, uint32 buf_size);
int ReadData(void* buf, uint32 buf_size);
private:
// Called asynchronously via InvokeLater.
void ResponseStarted();
int ReadCompleted(void* buf, uint32 buf_size);
enum ReadyStates {
READY_INVALID = 0,
READY_WAITING = 1,
READY_GOT_HEADERS = 2,
READY_GOT_DATA = 3,
};
const TestResponsePayload* payload_;
uint32 offset_;
int ready_state_;
CPRequest* request_;
};
ResponseStream::ResponseStream(const TestResponsePayload* payload,
CPRequest* request)
: payload_(payload), offset_(0), request_(request),
ready_state_(READY_INVALID) {
}
void ResponseStream::Init() {
if (payload_->async) {
// simulate an asynchronous start complete
ready_state_ = READY_WAITING;
g_cptest_funcs.invoke_later(
InvokeLaterCallback,
// downcast to Task before void, since we upcast from void to Task.
static_cast<Task*>(
NewRunnableMethod(this, &ResponseStream::ResponseStarted)),
500);
} else {
ready_state_ = READY_GOT_DATA;
}
}
int ResponseStream::GetResponseInfo(CPResponseInfoType type, void* buf,
uint32 buf_size) {
if (ready_state_ < READY_GOT_HEADERS)
return CPERR_FAILURE;
switch (type) {
case CPRESPONSEINFO_HTTP_STATUS:
if (buf) {
int status = payload_->status;
memcpy(buf, &payload_->status, buf_size);
}
break;
case CPRESPONSEINFO_HTTP_RAW_HEADERS: {
std::string headers = GetPayloadHeaders(payload_);
if (buf_size < headers.size()+1)
return static_cast<int>(headers.size()+1);
if (buf)
memcpy(buf, headers.c_str(), headers.size()+1);
break;
}
default:
return CPERR_INVALID_VERSION;
}
return CPERR_SUCCESS;
}
int ResponseStream::ReadData(void* buf, uint32 buf_size) {
if (ready_state_ < READY_GOT_DATA) {
// simulate an asynchronous read complete
g_cptest_funcs.invoke_later(
InvokeLaterCallback,
// downcast to Task before void, since we upcast from void to Task.
static_cast<Task*>(
NewRunnableMethod(this, &ResponseStream::ReadCompleted,
buf, buf_size)),
500);
return CPERR_IO_PENDING;
}
// synchronously complete the read
return ReadCompleted(buf, buf_size);
}
void ResponseStream::ResponseStarted() {
ready_state_ = READY_GOT_HEADERS;
g_cpresponse_funcs.start_completed(request_, CPERR_SUCCESS);
}
int ResponseStream::ReadCompleted(void* buf, uint32 buf_size) {
uint32 size = static_cast<uint32>(strlen(payload_->body));
uint32 avail = size - offset_;
uint32 count = buf_size;
if (count > avail)
count = avail;
if (count) {
memcpy(buf, payload_->body + offset_, count);
}
offset_ += count;
if (ready_state_ < READY_GOT_DATA) {
ready_state_ = READY_GOT_DATA;
g_cpresponse_funcs.read_completed(request_, static_cast<int>(count));
}
return count;
}
// CPP Funcs
CPError STDCALL CPP_Shutdown() {
return CPERR_SUCCESS;
}
CPBool STDCALL CPP_ShouldInterceptRequest(CPRequest* request) {
DCHECK(StrNCaseCmp(request->url, kChromeTestPluginProtocol,
arraysize(kChromeTestPluginProtocol) - 1) == 0);
return FindPayload(request->url) != NULL;
}
CPError STDCALL CPR_StartRequest(CPRequest* request) {
const TestResponsePayload* payload = FindPayload(request->url);
DCHECK(payload);
ResponseStream* stream = new ResponseStream(payload, request);
stream->AddRef(); // Released in CPR_EndRequest
stream->Init();
request->pdata = stream;
return payload->async ? CPERR_IO_PENDING : CPERR_SUCCESS;
}
void STDCALL CPR_EndRequest(CPRequest* request, CPError reason) {
ResponseStream* stream = static_cast<ResponseStream*>(request->pdata);
request->pdata = NULL;
stream->Release(); // balances AddRef in CPR_StartRequest
}
void STDCALL CPR_SetExtraRequestHeaders(CPRequest* request,
const char* headers) {
// doesn't affect us
}
void STDCALL CPR_SetRequestLoadFlags(CPRequest* request, uint32 flags) {
// doesn't affect us
}
void STDCALL CPR_AppendDataToUpload(CPRequest* request, const char* bytes,
int bytes_len) {
// doesn't affect us
}
CPError STDCALL CPR_AppendFileToUpload(CPRequest* request, const char* filepath,
uint64 offset, uint64 length) {
// doesn't affect us
return CPERR_FAILURE;
}
int STDCALL CPR_GetResponseInfo(CPRequest* request, CPResponseInfoType type,
void* buf, uint32 buf_size) {
ResponseStream* stream = static_cast<ResponseStream*>(request->pdata);
return stream->GetResponseInfo(type, buf, buf_size);
}
int STDCALL CPR_Read(CPRequest* request, void* buf, uint32 buf_size) {
ResponseStream* stream = static_cast<ResponseStream*>(request->pdata);
return stream->ReadData(buf, buf_size);
}
// RequestResponse: manages the retrieval of response data from the host
class RequestResponse {
public:
RequestResponse(const std::string& raw_headers)
: raw_headers_(raw_headers), offset_(0) {}
void StartReading(CPRequest* request);
void ReadCompleted(CPRequest* request, int bytes_read);
private:
std::string raw_headers_;
std::string body_;
int offset_;
};
void RequestResponse::StartReading(CPRequest* request) {
int rv = 0;
const uint32 kReadSize = 4096;
do {
body_.resize(offset_ + kReadSize);
rv = g_cprequest_funcs.read(request, &body_[offset_], kReadSize);
if (rv > 0)
offset_ += rv;
} while (rv > 0);
if (rv != CPERR_IO_PENDING) {
// Either an error occurred, or we are done.
ReadCompleted(request, rv);
}
}
void RequestResponse::ReadCompleted(CPRequest* request, int bytes_read) {
if (bytes_read > 0) {
offset_ += bytes_read;
StartReading(request);
return;
}
body_.resize(offset_);
bool success = (bytes_read == 0);
g_cptest_funcs.test_complete(request, success, raw_headers_, body_);
g_cprequest_funcs.end_request(request, CPERR_CANCELLED);
delete this;
}
void STDCALL CPRR_ReceivedRedirect(CPRequest* request, const char* new_url) {
}
void STDCALL CPRR_StartCompleted(CPRequest* request, CPError result) {
DCHECK(!request->pdata);
std::string raw_headers;
int size = g_cprequest_funcs.get_response_info(
request, CPRESPONSEINFO_HTTP_RAW_HEADERS, NULL, 0);
int rv = size < 0 ? size : g_cprequest_funcs.get_response_info(
request, CPRESPONSEINFO_HTTP_RAW_HEADERS,
WriteInto(&raw_headers, size+1), size);
if (rv != CPERR_SUCCESS) {
g_cptest_funcs.test_complete(request, false, std::string(), std::string());
g_cprequest_funcs.end_request(request, CPERR_CANCELLED);
return;
}
RequestResponse* response = new RequestResponse(raw_headers);
request->pdata = response;
response->StartReading(request);
}
void STDCALL CPRR_ReadCompleted(CPRequest* request, int bytes_read) {
RequestResponse* response =
reinterpret_cast<RequestResponse*>(request->pdata);
response->ReadCompleted(request, bytes_read);
}
int STDCALL CPT_MakeRequest(const char* method, const GURL& url) {
CPRequest* request = NULL;
if (g_cpbrowser_funcs.create_request(g_cpid, NULL, method, url.spec().c_str(),
&request) != CPERR_SUCCESS ||
!request) {
return CPERR_FAILURE;
}
g_cprequest_funcs.set_request_load_flags(request,
CPREQUESTLOAD_DISABLE_INTERCEPT);
if (strcmp(method, "POST") == 0) {
g_cprequest_funcs.set_extra_request_headers(
request, "Content-Type: text/plain");
g_cprequest_funcs.append_data_to_upload(
request, kChromeTestPluginPostData,
arraysize(kChromeTestPluginPostData) - 1);
}
int rv = g_cprequest_funcs.start_request(request);
if (rv == CPERR_SUCCESS) {
CPRR_StartCompleted(request, CPERR_SUCCESS);
} else if (rv != CPERR_IO_PENDING) {
g_cprequest_funcs.end_request(request, CPERR_CANCELLED);
return CPERR_FAILURE;
}
return CPERR_SUCCESS;
}
// DLL entry points
CPError STDCALL CP_Initialize(CPID id, const CPBrowserFuncs* bfuncs,
CPPluginFuncs* pfuncs) {
if (bfuncs == NULL || pfuncs == NULL)
return CPERR_FAILURE;
if (CP_GET_MAJOR_VERSION(bfuncs->version) > CP_MAJOR_VERSION)
return CPERR_INVALID_VERSION;
if (bfuncs->size < sizeof(CPBrowserFuncs) ||
pfuncs->size < sizeof(CPPluginFuncs))
return CPERR_INVALID_VERSION;
pfuncs->version = CP_VERSION;
pfuncs->shutdown = CPP_Shutdown;
pfuncs->should_intercept_request = CPP_ShouldInterceptRequest;
static CPRequestFuncs request_funcs;
request_funcs.start_request = CPR_StartRequest;
request_funcs.end_request = CPR_EndRequest;
request_funcs.set_extra_request_headers = CPR_SetExtraRequestHeaders;
request_funcs.set_request_load_flags = CPR_SetRequestLoadFlags;
request_funcs.append_data_to_upload = CPR_AppendDataToUpload;
request_funcs.get_response_info = CPR_GetResponseInfo;
request_funcs.read = CPR_Read;
request_funcs.append_file_to_upload = CPR_AppendFileToUpload;
pfuncs->request_funcs = &request_funcs;
static CPResponseFuncs response_funcs;
response_funcs.received_redirect = CPRR_ReceivedRedirect;
response_funcs.start_completed = CPRR_StartCompleted;
response_funcs.read_completed = CPRR_ReadCompleted;
pfuncs->response_funcs = &response_funcs;
g_cpid = id;
g_cpbrowser_funcs = *bfuncs;
g_cprequest_funcs = *bfuncs->request_funcs;
g_cpresponse_funcs = *bfuncs->response_funcs;
g_cpbrowser_funcs = *bfuncs;
const char* protocols[] = {kChromeTestPluginProtocol};
g_cpbrowser_funcs.enable_request_intercept(g_cpid, protocols, 1);
return CPERR_SUCCESS;
}
int STDCALL CP_Test(void* vparam) {
TestFuncParams* param = reinterpret_cast<TestFuncParams*>(vparam);
param->pfuncs.test_make_request = CPT_MakeRequest;
g_cptest_funcs = param->bfuncs;
return CPERR_SUCCESS;
}
|