IndexedDBFactory now ForceCloses databases.
[chromium-blink-merge.git] / content / browser / renderer_host / pepper / pepper_network_proxy_host.cc
blob8f44537ab9123ad299f039facc95ddbb0e48bed2
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.
5 #include "content/browser/renderer_host/pepper/pepper_network_proxy_host.h"
7 #include "base/bind.h"
8 #include "content/browser/renderer_host/pepper/browser_ppapi_host_impl.h"
9 #include "content/browser/renderer_host/pepper/pepper_socket_utils.h"
10 #include "content/public/browser/browser_context.h"
11 #include "content/public/browser/browser_thread.h"
12 #include "content/public/browser/render_process_host.h"
13 #include "content/public/common/socket_permission_request.h"
14 #include "net/base/net_errors.h"
15 #include "net/proxy/proxy_info.h"
16 #include "net/url_request/url_request_context.h"
17 #include "net/url_request/url_request_context_getter.h"
18 #include "ppapi/c/pp_errors.h"
19 #include "ppapi/host/dispatch_host_message.h"
20 #include "ppapi/host/ppapi_host.h"
21 #include "ppapi/proxy/ppapi_messages.h"
23 namespace content {
25 PepperNetworkProxyHost::PepperNetworkProxyHost(BrowserPpapiHostImpl* host,
26 PP_Instance instance,
27 PP_Resource resource)
28 : ResourceHost(host->GetPpapiHost(), instance, resource),
29 proxy_service_(NULL),
30 is_allowed_(false),
31 waiting_for_ui_thread_data_(true),
32 weak_factory_(this) {
33 int render_process_id(0), render_frame_id(0);
34 host->GetRenderFrameIDsForInstance(instance,
35 &render_process_id,
36 &render_frame_id);
37 BrowserThread::PostTaskAndReplyWithResult(
38 BrowserThread::UI, FROM_HERE,
39 base::Bind(&GetUIThreadDataOnUIThread,
40 render_process_id,
41 render_frame_id,
42 host->external_plugin()),
43 base::Bind(&PepperNetworkProxyHost::DidGetUIThreadData,
44 weak_factory_.GetWeakPtr()));
47 PepperNetworkProxyHost::~PepperNetworkProxyHost() {
48 while (!pending_requests_.empty()) {
49 // If the proxy_service_ is NULL, we shouldn't have any outstanding
50 // requests.
51 DCHECK(proxy_service_);
52 net::ProxyService::PacRequest* request = pending_requests_.front();
53 proxy_service_->CancelPacRequest(request);
54 pending_requests_.pop();
58 PepperNetworkProxyHost::UIThreadData::UIThreadData()
59 : is_allowed(false) {
62 PepperNetworkProxyHost::UIThreadData::~UIThreadData() {
65 // static
66 PepperNetworkProxyHost::UIThreadData
67 PepperNetworkProxyHost::GetUIThreadDataOnUIThread(int render_process_id,
68 int render_frame_id,
69 bool is_external_plugin) {
70 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
71 PepperNetworkProxyHost::UIThreadData result;
72 RenderProcessHost* render_process_host =
73 RenderProcessHost::FromID(render_process_id);
74 if (render_process_host && render_process_host->GetBrowserContext()) {
75 result.context_getter = render_process_host->GetBrowserContext()->
76 GetRequestContextForRenderProcess(render_process_id);
79 SocketPermissionRequest request(
80 content::SocketPermissionRequest::RESOLVE_PROXY, std::string(), 0);
81 result.is_allowed = pepper_socket_utils::CanUseSocketAPIs(
82 is_external_plugin,
83 false /* is_private_api */,
84 &request,
85 render_process_id,
86 render_frame_id);
87 return result;
90 void PepperNetworkProxyHost::DidGetUIThreadData(
91 const UIThreadData& ui_thread_data) {
92 is_allowed_ = ui_thread_data.is_allowed;
93 if (ui_thread_data.context_getter.get() &&
94 ui_thread_data.context_getter->GetURLRequestContext()) {
95 proxy_service_ =
96 ui_thread_data.context_getter->GetURLRequestContext()->proxy_service();
98 waiting_for_ui_thread_data_ = false;
99 if (!proxy_service_) {
100 DLOG_IF(WARNING, proxy_service_)
101 << "Failed to find a ProxyService for Pepper plugin.";
103 TryToSendUnsentRequests();
106 int32_t PepperNetworkProxyHost::OnResourceMessageReceived(
107 const IPC::Message& msg,
108 ppapi::host::HostMessageContext* context) {
109 IPC_BEGIN_MESSAGE_MAP(PepperNetworkProxyHost, msg)
110 PPAPI_DISPATCH_HOST_RESOURCE_CALL(
111 PpapiHostMsg_NetworkProxy_GetProxyForURL, OnMsgGetProxyForURL)
112 IPC_END_MESSAGE_MAP()
113 return PP_ERROR_FAILED;
116 int32_t PepperNetworkProxyHost::OnMsgGetProxyForURL(
117 ppapi::host::HostMessageContext* context,
118 const std::string& url) {
119 GURL gurl(url);
120 if (gurl.is_valid()) {
121 UnsentRequest request = { gurl, context->MakeReplyMessageContext() };
122 unsent_requests_.push(request);
123 TryToSendUnsentRequests();
124 } else {
125 SendFailureReply(PP_ERROR_BADARGUMENT,
126 context->MakeReplyMessageContext());
128 return PP_OK_COMPLETIONPENDING;
131 void PepperNetworkProxyHost::TryToSendUnsentRequests() {
132 if (waiting_for_ui_thread_data_)
133 return;
135 while (!unsent_requests_.empty()) {
136 const UnsentRequest& request = unsent_requests_.front();
137 if (!proxy_service_) {
138 SendFailureReply(PP_ERROR_FAILED, request.reply_context);
139 } else if (!is_allowed_) {
140 SendFailureReply(PP_ERROR_NOACCESS, request.reply_context);
141 } else {
142 // Everything looks valid, so try to resolve the proxy.
143 net::ProxyInfo* proxy_info = new net::ProxyInfo;
144 net::ProxyService::PacRequest* pending_request = NULL;
145 base::Callback<void (int)> callback =
146 base::Bind(&PepperNetworkProxyHost::OnResolveProxyCompleted,
147 weak_factory_.GetWeakPtr(),
148 request.reply_context,
149 base::Owned(proxy_info));
150 int result = proxy_service_->ResolveProxy(request.url,
151 proxy_info,
152 callback,
153 &pending_request,
154 net::BoundNetLog());
155 pending_requests_.push(pending_request);
156 // If it was handled synchronously, we must run the callback now;
157 // proxy_service_ won't run it for us in this case.
158 if (result != net::ERR_IO_PENDING)
159 callback.Run(result);
161 unsent_requests_.pop();
165 void PepperNetworkProxyHost::OnResolveProxyCompleted(
166 ppapi::host::ReplyMessageContext context,
167 net::ProxyInfo* proxy_info,
168 int result) {
169 pending_requests_.pop();
171 if (result != net::OK) {
172 // Currently, the only proxy-specific error we could get is
173 // MANDATORY_PROXY_CONFIGURATION_FAILED. There's really no action a plugin
174 // can take in this case, so there's no need to distinguish it from other
175 // failures.
176 context.params.set_result(PP_ERROR_FAILED);
178 host()->SendReply(context,
179 PpapiPluginMsg_NetworkProxy_GetProxyForURLReply(
180 proxy_info->ToPacString()));
183 void PepperNetworkProxyHost::SendFailureReply(
184 int32_t error,
185 ppapi::host::ReplyMessageContext context) {
186 context.params.set_result(error);
187 host()->SendReply(context,
188 PpapiPluginMsg_NetworkProxy_GetProxyForURLReply(
189 std::string()));
192 } // namespace content