| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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/browser/chrome_plugin_host.h" | 5 #include "chrome/browser/chrome_plugin_host.h" |
| 6 | 6 |
| 7 #include <set> | 7 #include <set> |
| 8 | 8 |
| 9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
| 10 #include "base/file_util.h" | 10 #include "base/file_util.h" |
| (...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 138 // This class manages a network request made by the plugin, also acting as | 138 // This class manages a network request made by the plugin, also acting as |
| 139 // the URLRequest delegate. | 139 // the URLRequest delegate. |
| 140 // NOTE: All methods must be called on the IO thread. | 140 // NOTE: All methods must be called on the IO thread. |
| 141 class PluginRequestHandler : public PluginHelper, public URLRequest::Delegate { | 141 class PluginRequestHandler : public PluginHelper, public URLRequest::Delegate { |
| 142 public: | 142 public: |
| 143 static PluginRequestHandler* FromCPRequest(CPRequest* request) { | 143 static PluginRequestHandler* FromCPRequest(CPRequest* request) { |
| 144 return ScopableCPRequest::GetData<PluginRequestHandler*>(request); | 144 return ScopableCPRequest::GetData<PluginRequestHandler*>(request); |
| 145 } | 145 } |
| 146 | 146 |
| 147 PluginRequestHandler(ChromePluginLib* plugin, ScopableCPRequest* cprequest) | 147 PluginRequestHandler(ChromePluginLib* plugin, ScopableCPRequest* cprequest) |
| 148 : PluginHelper(plugin), cprequest_(cprequest) { | 148 : PluginHelper(plugin), cprequest_(cprequest), user_buffer_(NULL) { |
| 149 cprequest_->data = this; // see FromCPRequest(). | 149 cprequest_->data = this; // see FromCPRequest(). |
| 150 | 150 |
| 151 URLRequestContext* context = CPBrowsingContextManager::Instance()-> | 151 URLRequestContext* context = CPBrowsingContextManager::Instance()-> |
| 152 ToURLRequestContext(cprequest_->context); | 152 ToURLRequestContext(cprequest_->context); |
| 153 // TODO(mpcomplete): remove fallback case when Gears support is prevalent. | 153 // TODO(mpcomplete): remove fallback case when Gears support is prevalent. |
| 154 if (!context) | 154 if (!context) |
| 155 context = Profile::GetDefaultRequestContext(); | 155 context = Profile::GetDefaultRequestContext(); |
| 156 | 156 |
| 157 GURL gurl(cprequest_->url); | 157 GURL gurl(cprequest_->url); |
| 158 request_.reset(new URLRequest(gurl, this)); | 158 request_.reset(new URLRequest(gurl, this)); |
| 159 request_->set_context(context); | 159 request_->set_context(context); |
| 160 request_->set_method(cprequest_->method); | 160 request_->set_method(cprequest_->method); |
| 161 request_->set_load_flags(PluginResponseUtils::CPLoadFlagsToNetFlags(0)); | 161 request_->set_load_flags(PluginResponseUtils::CPLoadFlagsToNetFlags(0)); |
| 162 } | 162 } |
| 163 | 163 |
| 164 URLRequest* request() { return request_.get(); } | 164 URLRequest* request() { return request_.get(); } |
| 165 | 165 |
| 166 // Wraper of URLRequest::Read() |
| 167 bool Read(char* dest, int dest_size, int *bytes_read) { |
| 168 CHECK(!my_buffer_.get()); |
| 169 // We'll use our own buffer until the read actually completes. |
| 170 user_buffer_ = dest; |
| 171 my_buffer_ = new net::IOBuffer(dest_size); |
| 172 |
| 173 if (request_->Read(my_buffer_, dest_size, bytes_read)) { |
| 174 memcpy(dest, my_buffer_->data(), *bytes_read); |
| 175 my_buffer_ = NULL; |
| 176 return true; |
| 177 } |
| 178 |
| 179 if (!request_->status().is_io_pending()) |
| 180 my_buffer_ = NULL; |
| 181 |
| 182 return false; |
| 183 } |
| 184 |
| 166 // URLRequest::Delegate | 185 // URLRequest::Delegate |
| 167 virtual void OnReceivedRedirect(URLRequest* request, const GURL& new_url) { | 186 virtual void OnReceivedRedirect(URLRequest* request, const GURL& new_url) { |
| 168 plugin_->functions().response_funcs->received_redirect( | 187 plugin_->functions().response_funcs->received_redirect( |
| 169 cprequest_.get(), new_url.spec().c_str()); | 188 cprequest_.get(), new_url.spec().c_str()); |
| 170 } | 189 } |
| 171 | 190 |
| 172 virtual void OnResponseStarted(URLRequest* request) { | 191 virtual void OnResponseStarted(URLRequest* request) { |
| 173 // TODO(mpcomplete): better error codes | 192 // TODO(mpcomplete): better error codes |
| 174 CPError result = | 193 CPError result = |
| 175 request_->status().is_success() ? CPERR_SUCCESS : CPERR_FAILURE; | 194 request_->status().is_success() ? CPERR_SUCCESS : CPERR_FAILURE; |
| 176 plugin_->functions().response_funcs->start_completed( | 195 plugin_->functions().response_funcs->start_completed( |
| 177 cprequest_.get(), result); | 196 cprequest_.get(), result); |
| 178 } | 197 } |
| 179 | 198 |
| 180 virtual void OnReadCompleted(URLRequest* request, int bytes_read) { | 199 virtual void OnReadCompleted(URLRequest* request, int bytes_read) { |
| 181 // TODO(mpcomplete): better error codes | 200 CHECK(my_buffer_.get()); |
| 182 if (bytes_read < 0) | 201 CHECK(user_buffer_); |
| 202 if (bytes_read > 0) { |
| 203 memcpy(user_buffer_, my_buffer_->data(), bytes_read); |
| 204 } else if (bytes_read < 0) { |
| 205 // TODO(mpcomplete): better error codes |
| 183 bytes_read = CPERR_FAILURE; | 206 bytes_read = CPERR_FAILURE; |
| 207 } |
| 208 my_buffer_ = NULL; |
| 184 plugin_->functions().response_funcs->read_completed( | 209 plugin_->functions().response_funcs->read_completed( |
| 185 cprequest_.get(), bytes_read); | 210 cprequest_.get(), bytes_read); |
| 186 } | 211 } |
| 187 | 212 |
| 188 private: | 213 private: |
| 189 scoped_ptr<ScopableCPRequest> cprequest_; | 214 scoped_ptr<ScopableCPRequest> cprequest_; |
| 190 scoped_ptr<URLRequest> request_; | 215 scoped_ptr<URLRequest> request_; |
| 216 scoped_refptr<net::IOBuffer> my_buffer_; |
| 217 char* user_buffer_; |
| 191 }; | 218 }; |
| 192 | 219 |
| 193 // This class manages plugins that want to handle UI commands. Right now, we | 220 // This class manages plugins that want to handle UI commands. Right now, we |
| 194 // only allow 1 plugin to do this, so there's only ever 1 instance of this | 221 // only allow 1 plugin to do this, so there's only ever 1 instance of this |
| 195 // class at once. | 222 // class at once. |
| 196 // NOTE: All methods must be called on the IO thread. | 223 // NOTE: All methods must be called on the IO thread. |
| 197 class PluginCommandHandler : public PluginHelper { | 224 class PluginCommandHandler : public PluginHelper { |
| 198 public: | 225 public: |
| 199 static void HandleCommand(int command, | 226 static void HandleCommand(int command, |
| 200 CPCommandInterface* data, | 227 CPCommandInterface* data, |
| (...skipping 406 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 607 return PluginResponseUtils::GetResponseInfo( | 634 return PluginResponseUtils::GetResponseInfo( |
| 608 handler->request()->response_headers(), type, buf, buf_size); | 635 handler->request()->response_headers(), type, buf, buf_size); |
| 609 } | 636 } |
| 610 | 637 |
| 611 int STDCALL CPR_Read(CPRequest* request, void* buf, uint32 buf_size) { | 638 int STDCALL CPR_Read(CPRequest* request, void* buf, uint32 buf_size) { |
| 612 CHECK(ChromePluginLib::IsPluginThread()); | 639 CHECK(ChromePluginLib::IsPluginThread()); |
| 613 PluginRequestHandler* handler = PluginRequestHandler::FromCPRequest(request); | 640 PluginRequestHandler* handler = PluginRequestHandler::FromCPRequest(request); |
| 614 CHECK(handler); | 641 CHECK(handler); |
| 615 | 642 |
| 616 int bytes_read; | 643 int bytes_read; |
| 617 if (handler->request()->Read(static_cast<char*>(buf), buf_size, &bytes_read)) | 644 if (handler->Read(static_cast<char*>(buf), buf_size, &bytes_read)) |
| 618 return bytes_read; // 0 == CPERR_SUCESS | 645 return bytes_read; // 0 == CPERR_SUCESS |
| 619 | 646 |
| 620 if (handler->request()->status().is_io_pending()) | 647 if (handler->request()->status().is_io_pending()) |
| 621 return CPERR_IO_PENDING; | 648 return CPERR_IO_PENDING; |
| 622 | 649 |
| 623 return CPERR_FAILURE; | 650 return CPERR_FAILURE; |
| 624 } | 651 } |
| 625 | 652 |
| 626 CPBool STDCALL CPB_IsPluginProcessRunning(CPID id) { | 653 CPBool STDCALL CPB_IsPluginProcessRunning(CPID id) { |
| 627 CHECK(ChromePluginLib::IsPluginThread()); | 654 CHECK(ChromePluginLib::IsPluginThread()); |
| (...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 764 void CPHandleCommand(int command, CPCommandInterface* data, | 791 void CPHandleCommand(int command, CPCommandInterface* data, |
| 765 CPBrowsingContext context) { | 792 CPBrowsingContext context) { |
| 766 // Sadly if we try and pass context through, we seem to break cl's little | 793 // Sadly if we try and pass context through, we seem to break cl's little |
| 767 // brain trying to compile the Tuple3 ctor. This cast works. | 794 // brain trying to compile the Tuple3 ctor. This cast works. |
| 768 int32 context_as_int32 = static_cast<int32>(context); | 795 int32 context_as_int32 = static_cast<int32>(context); |
| 769 // Plugins can only be accessed on the IO thread. | 796 // Plugins can only be accessed on the IO thread. |
| 770 g_browser_process->io_thread()->message_loop()->PostTask(FROM_HERE, | 797 g_browser_process->io_thread()->message_loop()->PostTask(FROM_HERE, |
| 771 NewRunnableFunction(PluginCommandHandler::HandleCommand, | 798 NewRunnableFunction(PluginCommandHandler::HandleCommand, |
| 772 command, data, context_as_int32)); | 799 command, data, context_as_int32)); |
| 773 } | 800 } |
| OLD | NEW |