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
|
// Copyright 2010, Google Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "chrome_frame/npapi_url_request.h"
#include "base/string_util.h"
#include "chrome_frame/np_browser_functions.h"
#include "net/base/net_errors.h"
class NPAPIUrlRequest : public PluginUrlRequest {
public:
explicit NPAPIUrlRequest(NPP instance);
~NPAPIUrlRequest();
virtual bool Start();
virtual void Stop();
virtual bool Read(int bytes_to_read);
// Called from NPAPI
NPError OnStreamCreated(const char* mime_type, NPStream* stream);
NPError OnStreamDestroyed(NPReason reason);
int OnWriteReady();
int OnWrite(void* buffer, int len);
// Thread unsafe implementation of ref counting, since
// this will be called on the plugin UI thread only.
virtual unsigned long API_CALL AddRef();
virtual unsigned long API_CALL Release();
private:
unsigned long ref_count_;
NPP instance_;
NPStream* stream_;
size_t pending_read_size_;
URLRequestStatus status_;
PlatformThreadId thread_;
static int instance_count_;
DISALLOW_COPY_AND_ASSIGN(NPAPIUrlRequest);
};
int NPAPIUrlRequest::instance_count_ = 0;
NPAPIUrlRequest::NPAPIUrlRequest(NPP instance)
: ref_count_(0), instance_(instance), stream_(NULL),
pending_read_size_(0),
status_(URLRequestStatus::FAILED, net::ERR_FAILED),
thread_(PlatformThread::CurrentId()) {
DLOG(INFO) << "Created request. Count: " << ++instance_count_;
}
NPAPIUrlRequest::~NPAPIUrlRequest() {
DLOG(INFO) << "Deleted request. Count: " << --instance_count_;
}
// NPAPIUrlRequest member defines.
bool NPAPIUrlRequest::Start() {
NPError result = NPERR_GENERIC_ERROR;
DLOG(INFO) << "Starting URL request: " << url();
if (LowerCaseEqualsASCII(method(), "get")) {
// TODO(joshia): if we have extra headers for HTTP GET, then implement
// it using XHR
result = npapi::GetURLNotify(instance_, url().c_str(), NULL, this);
} else if (LowerCaseEqualsASCII(method(), "post")) {
result = npapi::PostURLNotify(instance_, url().c_str(), NULL,
extra_headers().length(), extra_headers().c_str(), false, this);
} else {
NOTREACHED() << "PluginUrlRequest only supports 'GET'/'POST'";
}
if (NPERR_NO_ERROR != result) {
int os_error = net::ERR_FAILED;
switch (result) {
case NPERR_INVALID_URL:
os_error = net::ERR_INVALID_URL;
break;
default:
break;
}
delegate_->OnResponseEnd(id(),
URLRequestStatus(URLRequestStatus::FAILED, os_error));
return false;
}
return true;
}
void NPAPIUrlRequest::Stop() {
DLOG(INFO) << "Finished request: Url - " << url() << " result: "
<< static_cast<int>(status_.status());
if (stream_) {
npapi::DestroyStream(instance_, stream_, NPRES_USER_BREAK);
stream_ = NULL;
}
}
bool NPAPIUrlRequest::Read(int bytes_to_read) {
pending_read_size_ = bytes_to_read;
return true;
}
NPError NPAPIUrlRequest::OnStreamCreated(const char* mime_type,
NPStream* stream) {
stream_ = stream;
status_.set_status(URLRequestStatus::IO_PENDING);
// TODO(iyengar)
// Add support for passing persistent cookies and information about any URL
// redirects to Chrome.
delegate_->OnResponseStarted(id(), mime_type, stream->headers, stream->end,
base::Time::FromTimeT(stream->lastmodified), std::string(), 0);
return NPERR_NO_ERROR;
}
NPError NPAPIUrlRequest::OnStreamDestroyed(NPReason reason) {
URLRequestStatus::Status status = URLRequestStatus::FAILED;
switch (reason) {
case NPRES_DONE:
status_.set_status(URLRequestStatus::SUCCESS);
status_.set_os_error(0);
break;
case NPRES_USER_BREAK:
status_.set_status(URLRequestStatus::CANCELED);
status_.set_os_error(net::ERR_ABORTED);
break;
case NPRES_NETWORK_ERR:
default:
status_.set_status(URLRequestStatus::FAILED);
status_.set_os_error(net::ERR_CONNECTION_CLOSED);
break;
}
delegate_->OnResponseEnd(id(), status_);
return NPERR_NO_ERROR;
}
int NPAPIUrlRequest::OnWriteReady() {
return pending_read_size_;
}
int NPAPIUrlRequest::OnWrite(void* buffer, int len) {
pending_read_size_ = 0;
delegate_->OnReadComplete(id(), buffer, len);
return len;
}
STDMETHODIMP_(ULONG) NPAPIUrlRequest::AddRef() {
DCHECK_EQ(PlatformThread::CurrentId(), thread_);
++ref_count_;
return ref_count_;
}
STDMETHODIMP_(ULONG) NPAPIUrlRequest::Release() {
DCHECK_EQ(PlatformThread::CurrentId(), thread_);
unsigned long ret = --ref_count_;
if (!ret)
delete this;
return ret;
}
NPAPIUrlRequestManager::NPAPIUrlRequestManager() : instance_(NULL) {
}
NPAPIUrlRequestManager::~NPAPIUrlRequestManager() {
StopAll();
}
// PluginUrlRequestManager implementation
bool NPAPIUrlRequestManager::IsThreadSafe() {
return false;
}
void NPAPIUrlRequestManager::StartRequest(int request_id,
const IPC::AutomationURLRequest& request_info) {
scoped_refptr<NPAPIUrlRequest> new_request(new NPAPIUrlRequest(instance_));
DCHECK(new_request);
if (new_request->Initialize(this, request_id, request_info.url,
request_info.method, request_info.referrer,
request_info.extra_request_headers, request_info.upload_data.get(),
enable_frame_busting_)) {
// Add to map.
DCHECK(request_map_.find(request_id) == request_map_.end());
request_map_[request_id] = new_request;
if (new_request->Start()) {
// Keep additional reference on request for NPSTREAM
// This will be released in NPP_UrlNotify
new_request->AddRef();
}
}
}
void NPAPIUrlRequestManager::ReadRequest(int request_id, int bytes_to_read) {
scoped_refptr<NPAPIUrlRequest> request = LookupRequest(request_id);
if (request)
request->Read(bytes_to_read);
}
void NPAPIUrlRequestManager::EndRequest(int request_id) {
scoped_refptr<NPAPIUrlRequest> request = LookupRequest(request_id);
if (request)
request->Stop();
}
void NPAPIUrlRequestManager::StopAll() {
std::vector<scoped_refptr<NPAPIUrlRequest> > request_list;
// We copy the pending requests into a temporary vector as the Stop
// function in the request could also try to delete the request from
// the request map and the iterator could end up being invalid.
for (RequestMap::iterator it = request_map_.begin();
it != request_map_.end(); ++it) {
DCHECK(it->second != NULL);
request_list.push_back(it->second);
}
for (std::vector<scoped_refptr<NPAPIUrlRequest> >::size_type index = 0;
index < request_list.size(); ++index) {
request_list[index]->Stop();
}
}
// PluginRequestDelegate implementation.
// Callbacks from NPAPIUrlRequest. Simply forward to the delegate.
void NPAPIUrlRequestManager::OnResponseStarted(int request_id,
const char* mime_type, const char* headers, int size,
base::Time last_modified, const std::string& redirect_url,
int redirect_status) {
delegate_->OnResponseStarted(request_id, mime_type, headers, size,
last_modified, redirect_url, redirect_status);
}
void NPAPIUrlRequestManager::OnReadComplete(int request_id, const void* buffer,
int len) {
delegate_->OnReadComplete(request_id, buffer, len);
}
void NPAPIUrlRequestManager::OnResponseEnd(int request_id,
const URLRequestStatus& status) {
// Delete from map.
RequestMap::iterator it = request_map_.find(request_id);
DCHECK(request_map_.end() != it);
scoped_refptr<NPAPIUrlRequest> request = (*it).second;
request_map_.erase(it);
// Inform delegate unless canceled.
if (status.status() != URLRequestStatus::CANCELED)
delegate_->OnResponseEnd(request_id, status);
}
// Notifications from browser. Find the NPAPIUrlRequest and forward to it.
NPError NPAPIUrlRequestManager::NewStream(NPMIMEType type,
NPStream* stream,
NPBool seekable,
uint16* stream_type) {
NPAPIUrlRequest* request = RequestFromNotifyData(stream->notifyData);
if (!request)
return NPERR_NO_ERROR;
DCHECK(request_map_.find(request->id()) != request_map_.end());
// We need to return the requested stream mode if we are returning a success
// code. If we don't do this it causes Opera to blow up.
*stream_type = NP_NORMAL;
return request->OnStreamCreated(type, stream);
}
int32 NPAPIUrlRequestManager::WriteReady(NPStream* stream) {
NPAPIUrlRequest* request = RequestFromNotifyData(stream->notifyData);
if (!request)
return 0x7FFFFFFF;
DCHECK(request_map_.find(request->id()) != request_map_.end());
return request->OnWriteReady();
}
int32 NPAPIUrlRequestManager::Write(NPStream* stream, int32 offset,
int32 len, void* buffer) {
NPAPIUrlRequest* request = RequestFromNotifyData(stream->notifyData);
if (!request)
return len;
DCHECK(request_map_.find(request->id()) != request_map_.end());
return request->OnWrite(buffer, len);
}
NPError NPAPIUrlRequestManager::DestroyStream(NPStream* stream,
NPReason reason) {
NPAPIUrlRequest* request = RequestFromNotifyData(stream->notifyData);
if (!request)
return NPERR_NO_ERROR;
DCHECK(request_map_.find(request->id()) != request_map_.end());
return request->OnStreamDestroyed(reason);
}
void NPAPIUrlRequestManager::UrlNotify(const char* url, NPReason reason,
void* notify_data) {
NPAPIUrlRequest* request = RequestFromNotifyData(notify_data);
DCHECK(request != NULL);
if (request) {
request->Stop();
request->Release();
}
}
scoped_refptr<NPAPIUrlRequest> NPAPIUrlRequestManager::LookupRequest(
int request_id) {
RequestMap::iterator index = request_map_.find(request_id);
if (index != request_map_.end())
return index->second;
return NULL;
}
|