OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "content/browser/renderer_host/pepper/pepper_network_proxy_host.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "content/public/browser/browser_context.h" |
| 9 #include "content/public/browser/browser_ppapi_host.h" |
| 10 #include "content/public/browser/browser_thread.h" |
| 11 #include "content/public/browser/render_process_host.h" |
| 12 #include "net/base/net_errors.h" |
| 13 #include "net/proxy/proxy_info.h" |
| 14 #include "net/url_request/url_request_context.h" |
| 15 #include "net/url_request/url_request_context_getter.h" |
| 16 #include "ppapi/c/pp_errors.h" |
| 17 #include "ppapi/host/dispatch_host_message.h" |
| 18 #include "ppapi/host/ppapi_host.h" |
| 19 #include "ppapi/proxy/ppapi_messages.h" |
| 20 |
| 21 namespace content { |
| 22 |
| 23 namespace { |
| 24 |
| 25 scoped_refptr<net::URLRequestContextGetter> |
| 26 GetURLRequestContextGetterOnUIThread(int render_process_id) { |
| 27 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 28 |
| 29 scoped_refptr<net::URLRequestContextGetter> context_getter; |
| 30 RenderProcessHost* render_process_host = |
| 31 RenderProcessHost::FromID(render_process_id); |
| 32 if (render_process_host && render_process_host->GetBrowserContext()) { |
| 33 context_getter = render_process_host->GetBrowserContext()-> |
| 34 GetRequestContextForRenderProcess(render_process_id); |
| 35 } |
| 36 return context_getter; |
| 37 } |
| 38 |
| 39 } // namespace |
| 40 |
| 41 PepperNetworkProxyHost::PepperNetworkProxyHost(BrowserPpapiHost* host, |
| 42 PP_Instance instance, |
| 43 PP_Resource resource) |
| 44 : ResourceHost(host->GetPpapiHost(), instance, resource), |
| 45 proxy_service_(NULL), |
| 46 waiting_for_proxy_service_(true), |
| 47 weak_factory_(this) { |
| 48 int render_process_id(0), render_view_id_unused(0); |
| 49 host->GetRenderViewIDsForInstance(instance, |
| 50 &render_process_id, |
| 51 &render_view_id_unused); |
| 52 BrowserThread::PostTaskAndReplyWithResult( |
| 53 BrowserThread::UI, FROM_HERE, |
| 54 base::Bind(&GetURLRequestContextGetterOnUIThread, render_process_id), |
| 55 base::Bind(&PepperNetworkProxyHost::DidGetURLRequestContextGetter, |
| 56 weak_factory_.GetWeakPtr())); |
| 57 } |
| 58 |
| 59 PepperNetworkProxyHost::~PepperNetworkProxyHost() { |
| 60 while (!pending_requests_.empty()) { |
| 61 // If the proxy_service_ is NULL, we shouldn't have any outstanding |
| 62 // requests. |
| 63 DCHECK(proxy_service_); |
| 64 net::ProxyService::PacRequest* request = pending_requests_.front(); |
| 65 proxy_service_->CancelPacRequest(request); |
| 66 pending_requests_.pop(); |
| 67 } |
| 68 } |
| 69 |
| 70 void PepperNetworkProxyHost::DidGetURLRequestContextGetter( |
| 71 scoped_refptr<net::URLRequestContextGetter> context_getter) { |
| 72 if (context_getter->GetURLRequestContext()) |
| 73 proxy_service_ = context_getter->GetURLRequestContext()->proxy_service(); |
| 74 waiting_for_proxy_service_ = false; |
| 75 if (!proxy_service_) { |
| 76 DLOG_IF(WARNING, proxy_service_) |
| 77 << "Failed to find a ProxyService for Pepper plugin."; |
| 78 } |
| 79 TryToSendUnsentRequests(); |
| 80 } |
| 81 |
| 82 int32_t PepperNetworkProxyHost::OnResourceMessageReceived( |
| 83 const IPC::Message& msg, |
| 84 ppapi::host::HostMessageContext* context) { |
| 85 IPC_BEGIN_MESSAGE_MAP(PepperNetworkProxyHost, msg) |
| 86 PPAPI_DISPATCH_HOST_RESOURCE_CALL( |
| 87 PpapiHostMsg_NetworkProxy_GetProxyForURL, OnMsgGetProxyForURL) |
| 88 IPC_END_MESSAGE_MAP() |
| 89 return PP_ERROR_FAILED; |
| 90 } |
| 91 |
| 92 int32_t PepperNetworkProxyHost::OnMsgGetProxyForURL( |
| 93 ppapi::host::HostMessageContext* context, |
| 94 const std::string& url) { |
| 95 GURL gurl(url); |
| 96 if (gurl.is_valid()) { |
| 97 UnsentRequest request = { gurl, context->MakeReplyMessageContext() }; |
| 98 unsent_requests_.push(request); |
| 99 TryToSendUnsentRequests(); |
| 100 } else { |
| 101 SendFailureReply(PP_ERROR_BADARGUMENT, context->MakeReplyMessageContext()); |
| 102 } |
| 103 return PP_OK_COMPLETIONPENDING; |
| 104 } |
| 105 |
| 106 void PepperNetworkProxyHost::TryToSendUnsentRequests() { |
| 107 if (waiting_for_proxy_service_) |
| 108 return; |
| 109 |
| 110 while (!unsent_requests_.empty()) { |
| 111 const UnsentRequest& request = unsent_requests_.front(); |
| 112 if (!proxy_service_) { |
| 113 SendFailureReply(PP_ERROR_FAILED, request.reply_context); |
| 114 } else { |
| 115 // Everything looks valid, so try to resolve the proxy. |
| 116 net::ProxyInfo* proxy_info = new net::ProxyInfo; |
| 117 net::ProxyService::PacRequest* pending_request = NULL; |
| 118 base::Callback<void (int)> callback = |
| 119 base::Bind(&PepperNetworkProxyHost::OnResolveProxyCompleted, |
| 120 weak_factory_.GetWeakPtr(), |
| 121 request.reply_context, |
| 122 base::Owned(proxy_info)); |
| 123 int result = proxy_service_->ResolveProxy(request.url, |
| 124 proxy_info, |
| 125 callback, |
| 126 &pending_request, |
| 127 net::BoundNetLog()); |
| 128 pending_requests_.push(pending_request); |
| 129 // If it was handled synchronously, we must run the callback now; |
| 130 // proxy_service_ won't run it for us in this case. |
| 131 if (result != net::ERR_IO_PENDING) |
| 132 callback.Run(result); |
| 133 } |
| 134 unsent_requests_.pop(); |
| 135 } |
| 136 } |
| 137 |
| 138 void PepperNetworkProxyHost::OnResolveProxyCompleted( |
| 139 ppapi::host::ReplyMessageContext context, |
| 140 net::ProxyInfo* proxy_info, |
| 141 int result) { |
| 142 pending_requests_.pop(); |
| 143 |
| 144 if (result != net::OK) { |
| 145 // TODO(dmichael): Add appropriate error codes to yzshen's conversion |
| 146 // function, and call that function here. |
| 147 context.params.set_result(PP_ERROR_FAILED); |
| 148 } |
| 149 host()->SendReply(context, |
| 150 PpapiPluginMsg_NetworkProxy_GetProxyForURLReply( |
| 151 proxy_info->ToPacString())); |
| 152 } |
| 153 |
| 154 void PepperNetworkProxyHost::SendFailureReply( |
| 155 int32_t error, |
| 156 ppapi::host::ReplyMessageContext context) { |
| 157 context.params.set_result(error); |
| 158 host()->SendReply(context, |
| 159 PpapiPluginMsg_NetworkProxy_GetProxyForURLReply( |
| 160 std::string())); |
| 161 } |
| 162 |
| 163 } // namespace content |
OLD | NEW |