OLD | NEW |
(Empty) | |
| 1 // Copyright 2009, Google Inc. |
| 2 // All rights reserved. |
| 3 // |
| 4 // Redistribution and use in source and binary forms, with or without |
| 5 // modification, are permitted provided that the following conditions are |
| 6 // met: |
| 7 // |
| 8 // * Redistributions of source code must retain the above copyright |
| 9 // notice, this list of conditions and the following disclaimer. |
| 10 // * Redistributions in binary form must reproduce the above |
| 11 // copyright notice, this list of conditions and the following disclaimer |
| 12 // in the documentation and/or other materials provided with the |
| 13 // distribution. |
| 14 // * Neither the name of Google Inc. nor the names of its |
| 15 // contributors may be used to endorse or promote products derived from |
| 16 // this software without specific prior written permission. |
| 17 // |
| 18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 |
| 30 #include "chrome_frame/npapi_url_request.h" |
| 31 |
| 32 #include "base/string_util.h" |
| 33 #include "chrome_frame/np_browser_functions.h" |
| 34 #include "net/base/net_errors.h" |
| 35 |
| 36 int NPAPIUrlRequest::instance_count_ = 0; |
| 37 |
| 38 NPAPIUrlRequest::NPAPIUrlRequest(NPP instance) |
| 39 : ref_count_(0), instance_(instance), stream_(NULL), |
| 40 pending_read_size_(0), |
| 41 status_(URLRequestStatus::FAILED, net::ERR_FAILED), |
| 42 thread_(PlatformThread::CurrentId()) { |
| 43 DLOG(INFO) << "Created request. Count: " << ++instance_count_; |
| 44 } |
| 45 |
| 46 NPAPIUrlRequest::~NPAPIUrlRequest() { |
| 47 DLOG(INFO) << "Deleted request. Count: " << --instance_count_; |
| 48 } |
| 49 |
| 50 // NPAPIUrlRequest member defines. |
| 51 bool NPAPIUrlRequest::Start() { |
| 52 NPError result = NPERR_GENERIC_ERROR; |
| 53 DLOG(INFO) << "Starting URL request: " << url(); |
| 54 if (LowerCaseEqualsASCII(method(), "get")) { |
| 55 // TODO(joshia): if we have extra headers for HTTP GET, then implement |
| 56 // it using XHR |
| 57 result = npapi::GetURLNotify(instance_, url().c_str(), NULL, this); |
| 58 } else if (LowerCaseEqualsASCII(method(), "post")) { |
| 59 result = npapi::PostURLNotify(instance_, url().c_str(), NULL, |
| 60 extra_headers().length(), extra_headers().c_str(), false, this); |
| 61 } else { |
| 62 NOTREACHED() << "PluginUrlRequest only supports 'GET'/'POST'"; |
| 63 } |
| 64 |
| 65 if (NPERR_NO_ERROR == result) { |
| 66 request_handler()->AddRequest(this); |
| 67 } else { |
| 68 int os_error = net::ERR_FAILED; |
| 69 switch (result) { |
| 70 case NPERR_INVALID_URL: |
| 71 os_error = net::ERR_INVALID_URL; |
| 72 break; |
| 73 default: |
| 74 break; |
| 75 } |
| 76 |
| 77 OnResponseEnd(URLRequestStatus(URLRequestStatus::FAILED, os_error)); |
| 78 return false; |
| 79 } |
| 80 |
| 81 return true; |
| 82 } |
| 83 |
| 84 void NPAPIUrlRequest::Stop() { |
| 85 DLOG(INFO) << "Finished request: Url - " << url() << " result: " |
| 86 << static_cast<int>(status_.status()); |
| 87 if (stream_) { |
| 88 npapi::DestroyStream(instance_, stream_, NPRES_USER_BREAK); |
| 89 stream_ = NULL; |
| 90 } |
| 91 |
| 92 request_handler()->RemoveRequest(this); |
| 93 if (!status_.is_io_pending()) |
| 94 OnResponseEnd(status_); |
| 95 } |
| 96 |
| 97 bool NPAPIUrlRequest::Read(int bytes_to_read) { |
| 98 pending_read_size_ = bytes_to_read; |
| 99 return true; |
| 100 } |
| 101 |
| 102 bool NPAPIUrlRequest::OnStreamCreated(const char* mime_type, NPStream* stream) { |
| 103 stream_ = stream; |
| 104 status_.set_status(URLRequestStatus::IO_PENDING); |
| 105 // TODO(iyengar) |
| 106 // Add support for passing persistent cookies and information about any URL |
| 107 // redirects to Chrome. |
| 108 OnResponseStarted(mime_type, stream->headers, stream->end, |
| 109 base::Time::FromTimeT(stream->lastmodified), std::string(), |
| 110 std::string(), 0); |
| 111 return true; |
| 112 } |
| 113 |
| 114 void NPAPIUrlRequest::OnStreamDestroyed(NPReason reason) { |
| 115 URLRequestStatus::Status status = URLRequestStatus::FAILED; |
| 116 switch (reason) { |
| 117 case NPRES_DONE: |
| 118 status_.set_status(URLRequestStatus::SUCCESS); |
| 119 status_.set_os_error(0); |
| 120 break; |
| 121 case NPRES_USER_BREAK: |
| 122 status_.set_status(URLRequestStatus::CANCELED); |
| 123 status_.set_os_error(net::ERR_ABORTED); |
| 124 break; |
| 125 case NPRES_NETWORK_ERR: |
| 126 default: |
| 127 status_.set_status(URLRequestStatus::FAILED); |
| 128 status_.set_os_error(net::ERR_CONNECTION_CLOSED); |
| 129 break; |
| 130 } |
| 131 } |
| 132 |
| 133 int NPAPIUrlRequest::OnWriteReady() { |
| 134 return pending_read_size_; |
| 135 } |
| 136 |
| 137 int NPAPIUrlRequest::OnWrite(void* buffer, int len) { |
| 138 pending_read_size_ = 0; |
| 139 OnReadComplete(buffer, len); |
| 140 return len; |
| 141 } |
| 142 |
| 143 STDMETHODIMP_(ULONG) NPAPIUrlRequest::AddRef() { |
| 144 DCHECK_EQ(PlatformThread::CurrentId(), thread_); |
| 145 ++ref_count_; |
| 146 return ref_count_; |
| 147 } |
| 148 |
| 149 STDMETHODIMP_(ULONG) NPAPIUrlRequest::Release() { |
| 150 DCHECK_EQ(PlatformThread::CurrentId(), thread_); |
| 151 unsigned long ret = --ref_count_; |
| 152 if (!ret) |
| 153 delete this; |
| 154 |
| 155 return ret; |
| 156 } |
| 157 |
OLD | NEW |