OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome_frame/npapi_url_request.h" | 5 #include "chrome_frame/npapi_url_request.h" |
6 | 6 |
7 #include "base/string_number_conversions.h" | 7 #include "base/string_number_conversions.h" |
8 #include "base/threading/platform_thread.h" | 8 #include "base/threading/platform_thread.h" |
9 #include "chrome_frame/np_browser_functions.h" | 9 #include "chrome_frame/np_browser_functions.h" |
10 #include "chrome_frame/np_utils.h" | 10 #include "chrome_frame/np_utils.h" |
11 #include "net/base/net_errors.h" | 11 #include "net/base/net_errors.h" |
(...skipping 11 matching lines...) Expand all Loading... |
23 NPError OnStreamCreated(const char* mime_type, NPStream* stream); | 23 NPError OnStreamCreated(const char* mime_type, NPStream* stream); |
24 NPError OnStreamDestroyed(NPReason reason); | 24 NPError OnStreamDestroyed(NPReason reason); |
25 int OnWriteReady(); | 25 int OnWriteReady(); |
26 int OnWrite(void* buffer, int len); | 26 int OnWrite(void* buffer, int len); |
27 | 27 |
28 // Thread unsafe implementation of ref counting, since | 28 // Thread unsafe implementation of ref counting, since |
29 // this will be called on the plugin UI thread only. | 29 // this will be called on the plugin UI thread only. |
30 virtual unsigned long API_CALL AddRef(); | 30 virtual unsigned long API_CALL AddRef(); |
31 virtual unsigned long API_CALL Release(); | 31 virtual unsigned long API_CALL Release(); |
32 | 32 |
33 const URLRequestStatus& status() const { | 33 const net::URLRequestStatus& status() const { |
34 return status_; | 34 return status_; |
35 } | 35 } |
36 | 36 |
37 NPP instance() const { | 37 NPP instance() const { |
38 return instance_; | 38 return instance_; |
39 } | 39 } |
40 | 40 |
41 private: | 41 private: |
42 unsigned long ref_count_; | 42 unsigned long ref_count_; |
43 NPP instance_; | 43 NPP instance_; |
44 NPStream* stream_; | 44 NPStream* stream_; |
45 size_t pending_read_size_; | 45 size_t pending_read_size_; |
46 URLRequestStatus status_; | 46 net::URLRequestStatus status_; |
47 | 47 |
48 base::PlatformThreadId thread_; | 48 base::PlatformThreadId thread_; |
49 static int instance_count_; | 49 static int instance_count_; |
50 DISALLOW_COPY_AND_ASSIGN(NPAPIUrlRequest); | 50 DISALLOW_COPY_AND_ASSIGN(NPAPIUrlRequest); |
51 }; | 51 }; |
52 | 52 |
53 int NPAPIUrlRequest::instance_count_ = 0; | 53 int NPAPIUrlRequest::instance_count_ = 0; |
54 | 54 |
55 NPAPIUrlRequest::NPAPIUrlRequest(NPP instance) | 55 NPAPIUrlRequest::NPAPIUrlRequest(NPP instance) |
56 : ref_count_(0), instance_(instance), stream_(NULL), | 56 : ref_count_(0), instance_(instance), stream_(NULL), |
57 pending_read_size_(0), | 57 pending_read_size_(0), |
58 status_(URLRequestStatus::FAILED, net::ERR_FAILED), | 58 status_(net::URLRequestStatus::FAILED, net::ERR_FAILED), |
59 thread_(base::PlatformThread::CurrentId()) { | 59 thread_(base::PlatformThread::CurrentId()) { |
60 DVLOG(1) << "Created request. Count: " << ++instance_count_; | 60 DVLOG(1) << "Created request. Count: " << ++instance_count_; |
61 } | 61 } |
62 | 62 |
63 NPAPIUrlRequest::~NPAPIUrlRequest() { | 63 NPAPIUrlRequest::~NPAPIUrlRequest() { |
64 DVLOG(1) << "Deleted request. Count: " << --instance_count_; | 64 DVLOG(1) << "Deleted request. Count: " << --instance_count_; |
65 } | 65 } |
66 | 66 |
67 // NPAPIUrlRequest member defines. | 67 // NPAPIUrlRequest member defines. |
68 bool NPAPIUrlRequest::Start() { | 68 bool NPAPIUrlRequest::Start() { |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
105 int os_error = net::ERR_FAILED; | 105 int os_error = net::ERR_FAILED; |
106 switch (result) { | 106 switch (result) { |
107 case NPERR_INVALID_URL: | 107 case NPERR_INVALID_URL: |
108 os_error = net::ERR_INVALID_URL; | 108 os_error = net::ERR_INVALID_URL; |
109 break; | 109 break; |
110 default: | 110 default: |
111 break; | 111 break; |
112 } | 112 } |
113 | 113 |
114 delegate_->OnResponseEnd(id(), | 114 delegate_->OnResponseEnd(id(), |
115 URLRequestStatus(URLRequestStatus::FAILED, os_error)); | 115 net::URLRequestStatus(net::URLRequestStatus::FAILED, os_error)); |
116 return false; | 116 return false; |
117 } | 117 } |
118 | 118 |
119 return true; | 119 return true; |
120 } | 120 } |
121 | 121 |
122 void NPAPIUrlRequest::Stop() { | 122 void NPAPIUrlRequest::Stop() { |
123 DVLOG(1) << "Finished request: Url - " << url() | 123 DVLOG(1) << "Finished request: Url - " << url() |
124 << " result: " << static_cast<int>(status_.status()); | 124 << " result: " << static_cast<int>(status_.status()); |
125 | 125 |
126 status_.set_status(URLRequestStatus::CANCELED); | 126 status_.set_status(net::URLRequestStatus::CANCELED); |
127 if (stream_) { | 127 if (stream_) { |
128 npapi::DestroyStream(instance_, stream_, NPRES_USER_BREAK); | 128 npapi::DestroyStream(instance_, stream_, NPRES_USER_BREAK); |
129 stream_ = NULL; | 129 stream_ = NULL; |
130 } | 130 } |
131 } | 131 } |
132 | 132 |
133 bool NPAPIUrlRequest::Read(int bytes_to_read) { | 133 bool NPAPIUrlRequest::Read(int bytes_to_read) { |
134 pending_read_size_ = bytes_to_read; | 134 pending_read_size_ = bytes_to_read; |
135 return true; | 135 return true; |
136 } | 136 } |
137 | 137 |
138 NPError NPAPIUrlRequest::OnStreamCreated(const char* mime_type, | 138 NPError NPAPIUrlRequest::OnStreamCreated(const char* mime_type, |
139 NPStream* stream) { | 139 NPStream* stream) { |
140 stream_ = stream; | 140 stream_ = stream; |
141 status_.set_status(URLRequestStatus::IO_PENDING); | 141 status_.set_status(net::URLRequestStatus::IO_PENDING); |
142 // TODO(iyengar) | 142 // TODO(iyengar) |
143 // Add support for passing persistent cookies and information about any URL | 143 // Add support for passing persistent cookies and information about any URL |
144 // redirects to Chrome. | 144 // redirects to Chrome. |
145 delegate_->OnResponseStarted(id(), mime_type, stream->headers, stream->end, | 145 delegate_->OnResponseStarted(id(), mime_type, stream->headers, stream->end, |
146 base::Time::FromTimeT(stream->lastmodified), std::string(), 0); | 146 base::Time::FromTimeT(stream->lastmodified), std::string(), 0); |
147 return NPERR_NO_ERROR; | 147 return NPERR_NO_ERROR; |
148 } | 148 } |
149 | 149 |
150 NPError NPAPIUrlRequest::OnStreamDestroyed(NPReason reason) { | 150 NPError NPAPIUrlRequest::OnStreamDestroyed(NPReason reason) { |
151 // If the request has been aborted, then ignore the |reason| argument. | 151 // If the request has been aborted, then ignore the |reason| argument. |
152 // Normally the execution flow is such than NPRES_USER_BREAK will be passed | 152 // Normally the execution flow is such than NPRES_USER_BREAK will be passed |
153 // when the stream is aborted, but sometimes NPRES_NETWORK_ERROR is passed | 153 // when the stream is aborted, but sometimes NPRES_NETWORK_ERROR is passed |
154 // instead. To prevent Chrome from receiving a notification of a failed | 154 // instead. To prevent Chrome from receiving a notification of a failed |
155 // network connection, when Chrome actually canceled the request, we ignore | 155 // network connection, when Chrome actually canceled the request, we ignore |
156 // the status here. | 156 // the status here. |
157 if (URLRequestStatus::CANCELED != status_.status()) { | 157 if (net::URLRequestStatus::CANCELED != status_.status()) { |
158 switch (reason) { | 158 switch (reason) { |
159 case NPRES_DONE: | 159 case NPRES_DONE: |
160 status_.set_status(URLRequestStatus::SUCCESS); | 160 status_.set_status(net::URLRequestStatus::SUCCESS); |
161 status_.set_os_error(0); | 161 status_.set_os_error(0); |
162 break; | 162 break; |
163 case NPRES_USER_BREAK: | 163 case NPRES_USER_BREAK: |
164 status_.set_status(URLRequestStatus::CANCELED); | 164 status_.set_status(net::URLRequestStatus::CANCELED); |
165 status_.set_os_error(net::ERR_ABORTED); | 165 status_.set_os_error(net::ERR_ABORTED); |
166 break; | 166 break; |
167 case NPRES_NETWORK_ERR: | 167 case NPRES_NETWORK_ERR: |
168 default: | 168 default: |
169 status_.set_status(URLRequestStatus::FAILED); | 169 status_.set_status(net::URLRequestStatus::FAILED); |
170 status_.set_os_error(net::ERR_CONNECTION_CLOSED); | 170 status_.set_os_error(net::ERR_CONNECTION_CLOSED); |
171 break; | 171 break; |
172 } | 172 } |
173 } | 173 } |
174 | 174 |
175 delegate_->OnResponseEnd(id(), status_); | 175 delegate_->OnResponseEnd(id(), status_); |
176 return NPERR_NO_ERROR; | 176 return NPERR_NO_ERROR; |
177 } | 177 } |
178 | 178 |
179 int NPAPIUrlRequest::OnWriteReady() { | 179 int NPAPIUrlRequest::OnWriteReady() { |
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
327 int redirect_status) { | 327 int redirect_status) { |
328 delegate_->OnResponseStarted(request_id, mime_type, headers, size, | 328 delegate_->OnResponseStarted(request_id, mime_type, headers, size, |
329 last_modified, redirect_url, redirect_status); | 329 last_modified, redirect_url, redirect_status); |
330 } | 330 } |
331 | 331 |
332 void NPAPIUrlRequestManager::OnReadComplete(int request_id, | 332 void NPAPIUrlRequestManager::OnReadComplete(int request_id, |
333 const std::string& data) { | 333 const std::string& data) { |
334 delegate_->OnReadComplete(request_id, data); | 334 delegate_->OnReadComplete(request_id, data); |
335 } | 335 } |
336 | 336 |
337 void NPAPIUrlRequestManager::OnResponseEnd(int request_id, | 337 void NPAPIUrlRequestManager::OnResponseEnd( |
338 const URLRequestStatus& status) { | 338 int request_id, |
| 339 const net::URLRequestStatus& status) { |
339 // Delete from map. | 340 // Delete from map. |
340 RequestMap::iterator it = request_map_.find(request_id); | 341 RequestMap::iterator it = request_map_.find(request_id); |
341 DCHECK(request_map_.end() != it); | 342 DCHECK(request_map_.end() != it); |
342 scoped_refptr<NPAPIUrlRequest> request = (*it).second; | 343 scoped_refptr<NPAPIUrlRequest> request = (*it).second; |
343 request_map_.erase(it); | 344 request_map_.erase(it); |
344 | 345 |
345 // Inform delegate unless canceled. | 346 // Inform delegate unless canceled. |
346 if (status.status() != URLRequestStatus::CANCELED) | 347 if (status.status() != net::URLRequestStatus::CANCELED) |
347 delegate_->OnResponseEnd(request_id, status); | 348 delegate_->OnResponseEnd(request_id, status); |
348 } | 349 } |
349 | 350 |
350 void NPAPIUrlRequestManager::OnCookiesRetrieved(bool success, const GURL& url, | 351 void NPAPIUrlRequestManager::OnCookiesRetrieved(bool success, const GURL& url, |
351 const std::string& cookie_string, int cookie_id) { | 352 const std::string& cookie_string, int cookie_id) { |
352 delegate_->OnCookiesRetrieved(success, url, cookie_string, cookie_id); | 353 delegate_->OnCookiesRetrieved(success, url, cookie_string, cookie_id); |
353 } | 354 } |
354 | 355 |
355 // Notifications from browser. Find the NPAPIUrlRequest and forward to it. | 356 // Notifications from browser. Find the NPAPIUrlRequest and forward to it. |
356 NPError NPAPIUrlRequestManager::NewStream(NPMIMEType type, | 357 NPError NPAPIUrlRequestManager::NewStream(NPMIMEType type, |
357 NPStream* stream, | 358 NPStream* stream, |
358 NPBool seekable, | 359 NPBool seekable, |
359 uint16* stream_type) { | 360 uint16* stream_type) { |
360 NPAPIUrlRequest* request = RequestFromNotifyData(stream->notifyData); | 361 NPAPIUrlRequest* request = RequestFromNotifyData(stream->notifyData); |
361 if (!request) | 362 if (!request) |
362 return NPERR_NO_ERROR; | 363 return NPERR_NO_ERROR; |
363 | 364 |
364 // This stream is being constructed for a request that has already been | 365 // This stream is being constructed for a request that has already been |
365 // canceled. Signal its immediate termination. | 366 // canceled. Signal its immediate termination. |
366 if (URLRequestStatus::CANCELED == request->status().status()) { | 367 if (net::URLRequestStatus::CANCELED == request->status().status()) { |
367 return npapi::DestroyStream(request->instance(), | 368 return npapi::DestroyStream(request->instance(), |
368 stream, NPRES_USER_BREAK); | 369 stream, NPRES_USER_BREAK); |
369 } | 370 } |
370 | 371 |
371 DCHECK(request_map_.find(request->id()) != request_map_.end()); | 372 DCHECK(request_map_.find(request->id()) != request_map_.end()); |
372 // We need to return the requested stream mode if we are returning a success | 373 // We need to return the requested stream mode if we are returning a success |
373 // code. If we don't do this it causes Opera to blow up. | 374 // code. If we don't do this it causes Opera to blow up. |
374 *stream_type = NP_NORMAL; | 375 *stream_type = NP_NORMAL; |
375 return request->OnStreamCreated(type, stream); | 376 return request->OnStreamCreated(type, stream); |
376 } | 377 } |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
418 } | 419 } |
419 } | 420 } |
420 | 421 |
421 scoped_refptr<NPAPIUrlRequest> NPAPIUrlRequestManager::LookupRequest( | 422 scoped_refptr<NPAPIUrlRequest> NPAPIUrlRequestManager::LookupRequest( |
422 int request_id) { | 423 int request_id) { |
423 RequestMap::iterator index = request_map_.find(request_id); | 424 RequestMap::iterator index = request_map_.find(request_id); |
424 if (index != request_map_.end()) | 425 if (index != request_map_.end()) |
425 return index->second; | 426 return index->second; |
426 return NULL; | 427 return NULL; |
427 } | 428 } |
OLD | NEW |