OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 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 "content/browser/renderer_host/pepper/pepper_network_proxy_host.h" | 5 #include "content/browser/renderer_host/pepper/pepper_network_proxy_host.h" |
6 | 6 |
7 #include "base/bind.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" |
8 #include "content/public/browser/browser_context.h" | 10 #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/browser_thread.h" |
11 #include "content/public/browser/render_process_host.h" | 12 #include "content/public/browser/render_process_host.h" |
| 13 #include "content/public/common/socket_permission_request.h" |
12 #include "net/base/net_errors.h" | 14 #include "net/base/net_errors.h" |
13 #include "net/proxy/proxy_info.h" | 15 #include "net/proxy/proxy_info.h" |
14 #include "net/url_request/url_request_context.h" | 16 #include "net/url_request/url_request_context.h" |
15 #include "net/url_request/url_request_context_getter.h" | 17 #include "net/url_request/url_request_context_getter.h" |
16 #include "ppapi/c/pp_errors.h" | 18 #include "ppapi/c/pp_errors.h" |
17 #include "ppapi/host/dispatch_host_message.h" | 19 #include "ppapi/host/dispatch_host_message.h" |
18 #include "ppapi/host/ppapi_host.h" | 20 #include "ppapi/host/ppapi_host.h" |
19 #include "ppapi/proxy/ppapi_messages.h" | 21 #include "ppapi/proxy/ppapi_messages.h" |
20 | 22 |
21 namespace content { | 23 namespace content { |
22 | 24 |
23 namespace { | 25 PepperNetworkProxyHost::PepperNetworkProxyHost(BrowserPpapiHostImpl* host, |
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, | 26 PP_Instance instance, |
43 PP_Resource resource) | 27 PP_Resource resource) |
44 : ResourceHost(host->GetPpapiHost(), instance, resource), | 28 : ResourceHost(host->GetPpapiHost(), instance, resource), |
45 proxy_service_(NULL), | 29 proxy_service_(NULL), |
46 waiting_for_proxy_service_(true), | 30 is_allowed_(false), |
| 31 waiting_for_ui_thread_data_(true), |
47 weak_factory_(this) { | 32 weak_factory_(this) { |
48 int render_process_id(0), render_view_id_unused(0); | 33 int render_process_id(0), render_view_id(0); |
49 host->GetRenderViewIDsForInstance(instance, | 34 host->GetRenderViewIDsForInstance(instance, |
50 &render_process_id, | 35 &render_process_id, |
51 &render_view_id_unused); | 36 &render_view_id); |
52 BrowserThread::PostTaskAndReplyWithResult( | 37 BrowserThread::PostTaskAndReplyWithResult( |
53 BrowserThread::UI, FROM_HERE, | 38 BrowserThread::UI, FROM_HERE, |
54 base::Bind(&GetURLRequestContextGetterOnUIThread, render_process_id), | 39 base::Bind(&GetUIThreadDataOnUIThread, |
55 base::Bind(&PepperNetworkProxyHost::DidGetURLRequestContextGetter, | 40 render_process_id, |
| 41 render_view_id, |
| 42 host->external_plugin()), |
| 43 base::Bind(&PepperNetworkProxyHost::DidGetUIThreadData, |
56 weak_factory_.GetWeakPtr())); | 44 weak_factory_.GetWeakPtr())); |
57 } | 45 } |
58 | 46 |
59 PepperNetworkProxyHost::~PepperNetworkProxyHost() { | 47 PepperNetworkProxyHost::~PepperNetworkProxyHost() { |
60 while (!pending_requests_.empty()) { | 48 while (!pending_requests_.empty()) { |
61 // If the proxy_service_ is NULL, we shouldn't have any outstanding | 49 // If the proxy_service_ is NULL, we shouldn't have any outstanding |
62 // requests. | 50 // requests. |
63 DCHECK(proxy_service_); | 51 DCHECK(proxy_service_); |
64 net::ProxyService::PacRequest* request = pending_requests_.front(); | 52 net::ProxyService::PacRequest* request = pending_requests_.front(); |
65 proxy_service_->CancelPacRequest(request); | 53 proxy_service_->CancelPacRequest(request); |
66 pending_requests_.pop(); | 54 pending_requests_.pop(); |
67 } | 55 } |
68 } | 56 } |
69 | 57 |
70 void PepperNetworkProxyHost::DidGetURLRequestContextGetter( | 58 PepperNetworkProxyHost::UIThreadData::UIThreadData() |
71 scoped_refptr<net::URLRequestContextGetter> context_getter) { | 59 : is_allowed(false) { |
72 if (context_getter->GetURLRequestContext()) | 60 } |
73 proxy_service_ = context_getter->GetURLRequestContext()->proxy_service(); | 61 |
74 waiting_for_proxy_service_ = false; | 62 PepperNetworkProxyHost::UIThreadData::~UIThreadData() { |
| 63 } |
| 64 |
| 65 // static |
| 66 PepperNetworkProxyHost::UIThreadData |
| 67 PepperNetworkProxyHost::GetUIThreadDataOnUIThread(int render_process_id, |
| 68 int render_view_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); |
| 77 } |
| 78 |
| 79 RenderViewHost* render_view_host = |
| 80 RenderViewHost::FromID(render_process_id, render_view_id); |
| 81 if (render_view_host) { |
| 82 SocketPermissionRequest request( |
| 83 content::SocketPermissionRequest::RESOLVE_PROXY, std::string(), 0); |
| 84 result.is_allowed = pepper_socket_utils::CanUseSocketAPIs( |
| 85 is_external_plugin, |
| 86 false /* is_private_api */, |
| 87 request, |
| 88 render_view_host); |
| 89 } |
| 90 return result; |
| 91 } |
| 92 |
| 93 void PepperNetworkProxyHost::DidGetUIThreadData( |
| 94 const UIThreadData& ui_thread_data) { |
| 95 is_allowed_ = ui_thread_data.is_allowed; |
| 96 if (ui_thread_data.context_getter && |
| 97 ui_thread_data.context_getter->GetURLRequestContext()) { |
| 98 proxy_service_ = |
| 99 ui_thread_data.context_getter->GetURLRequestContext()->proxy_service(); |
| 100 } |
| 101 waiting_for_ui_thread_data_ = false; |
75 if (!proxy_service_) { | 102 if (!proxy_service_) { |
76 DLOG_IF(WARNING, proxy_service_) | 103 DLOG_IF(WARNING, proxy_service_) |
77 << "Failed to find a ProxyService for Pepper plugin."; | 104 << "Failed to find a ProxyService for Pepper plugin."; |
78 } | 105 } |
79 TryToSendUnsentRequests(); | 106 TryToSendUnsentRequests(); |
80 } | 107 } |
81 | 108 |
82 int32_t PepperNetworkProxyHost::OnResourceMessageReceived( | 109 int32_t PepperNetworkProxyHost::OnResourceMessageReceived( |
83 const IPC::Message& msg, | 110 const IPC::Message& msg, |
84 ppapi::host::HostMessageContext* context) { | 111 ppapi::host::HostMessageContext* context) { |
85 IPC_BEGIN_MESSAGE_MAP(PepperNetworkProxyHost, msg) | 112 IPC_BEGIN_MESSAGE_MAP(PepperNetworkProxyHost, msg) |
86 PPAPI_DISPATCH_HOST_RESOURCE_CALL( | 113 PPAPI_DISPATCH_HOST_RESOURCE_CALL( |
87 PpapiHostMsg_NetworkProxy_GetProxyForURL, OnMsgGetProxyForURL) | 114 PpapiHostMsg_NetworkProxy_GetProxyForURL, OnMsgGetProxyForURL) |
88 IPC_END_MESSAGE_MAP() | 115 IPC_END_MESSAGE_MAP() |
89 return PP_ERROR_FAILED; | 116 return PP_ERROR_FAILED; |
90 } | 117 } |
91 | 118 |
92 int32_t PepperNetworkProxyHost::OnMsgGetProxyForURL( | 119 int32_t PepperNetworkProxyHost::OnMsgGetProxyForURL( |
93 ppapi::host::HostMessageContext* context, | 120 ppapi::host::HostMessageContext* context, |
94 const std::string& url) { | 121 const std::string& url) { |
95 GURL gurl(url); | 122 GURL gurl(url); |
96 if (gurl.is_valid()) { | 123 if (gurl.is_valid()) { |
97 UnsentRequest request = { gurl, context->MakeReplyMessageContext() }; | 124 UnsentRequest request = { gurl, context->MakeReplyMessageContext() }; |
98 unsent_requests_.push(request); | 125 unsent_requests_.push(request); |
99 TryToSendUnsentRequests(); | 126 TryToSendUnsentRequests(); |
100 } else { | 127 } else { |
101 SendFailureReply(PP_ERROR_BADARGUMENT, context->MakeReplyMessageContext()); | 128 SendFailureReply(PP_ERROR_BADARGUMENT, |
| 129 context->MakeReplyMessageContext()); |
102 } | 130 } |
103 return PP_OK_COMPLETIONPENDING; | 131 return PP_OK_COMPLETIONPENDING; |
104 } | 132 } |
105 | 133 |
106 void PepperNetworkProxyHost::TryToSendUnsentRequests() { | 134 void PepperNetworkProxyHost::TryToSendUnsentRequests() { |
107 if (waiting_for_proxy_service_) | 135 if (waiting_for_ui_thread_data_) |
108 return; | 136 return; |
109 | 137 |
110 while (!unsent_requests_.empty()) { | 138 while (!unsent_requests_.empty()) { |
111 const UnsentRequest& request = unsent_requests_.front(); | 139 const UnsentRequest& request = unsent_requests_.front(); |
112 if (!proxy_service_) { | 140 if (!proxy_service_) { |
113 SendFailureReply(PP_ERROR_FAILED, request.reply_context); | 141 SendFailureReply(PP_ERROR_FAILED, request.reply_context); |
| 142 } else if (!is_allowed_) { |
| 143 SendFailureReply(PP_ERROR_NOACCESS, request.reply_context); |
114 } else { | 144 } else { |
115 // Everything looks valid, so try to resolve the proxy. | 145 // Everything looks valid, so try to resolve the proxy. |
116 net::ProxyInfo* proxy_info = new net::ProxyInfo; | 146 net::ProxyInfo* proxy_info = new net::ProxyInfo; |
117 net::ProxyService::PacRequest* pending_request = NULL; | 147 net::ProxyService::PacRequest* pending_request = NULL; |
118 base::Callback<void (int)> callback = | 148 base::Callback<void (int)> callback = |
119 base::Bind(&PepperNetworkProxyHost::OnResolveProxyCompleted, | 149 base::Bind(&PepperNetworkProxyHost::OnResolveProxyCompleted, |
120 weak_factory_.GetWeakPtr(), | 150 weak_factory_.GetWeakPtr(), |
121 request.reply_context, | 151 request.reply_context, |
122 base::Owned(proxy_info)); | 152 base::Owned(proxy_info)); |
123 int result = proxy_service_->ResolveProxy(request.url, | 153 int result = proxy_service_->ResolveProxy(request.url, |
(...skipping 11 matching lines...) Expand all Loading... |
135 } | 165 } |
136 } | 166 } |
137 | 167 |
138 void PepperNetworkProxyHost::OnResolveProxyCompleted( | 168 void PepperNetworkProxyHost::OnResolveProxyCompleted( |
139 ppapi::host::ReplyMessageContext context, | 169 ppapi::host::ReplyMessageContext context, |
140 net::ProxyInfo* proxy_info, | 170 net::ProxyInfo* proxy_info, |
141 int result) { | 171 int result) { |
142 pending_requests_.pop(); | 172 pending_requests_.pop(); |
143 | 173 |
144 if (result != net::OK) { | 174 if (result != net::OK) { |
145 // TODO(dmichael): Add appropriate error codes to yzshen's conversion | 175 // Currently, the only proxy-specific error we could get is |
146 // function, and call that function here. | 176 // MANDATORY_PROXY_CONFIGURATION_FAILED. There's really no action a plugin |
| 177 // can take in this case, so there's no need to distinguish it from other |
| 178 // failures. |
147 context.params.set_result(PP_ERROR_FAILED); | 179 context.params.set_result(PP_ERROR_FAILED); |
148 } | 180 } |
149 host()->SendReply(context, | 181 host()->SendReply(context, |
150 PpapiPluginMsg_NetworkProxy_GetProxyForURLReply( | 182 PpapiPluginMsg_NetworkProxy_GetProxyForURLReply( |
151 proxy_info->ToPacString())); | 183 proxy_info->ToPacString())); |
152 } | 184 } |
153 | 185 |
154 void PepperNetworkProxyHost::SendFailureReply( | 186 void PepperNetworkProxyHost::SendFailureReply( |
155 int32_t error, | 187 int32_t error, |
156 ppapi::host::ReplyMessageContext context) { | 188 ppapi::host::ReplyMessageContext context) { |
157 context.params.set_result(error); | 189 context.params.set_result(error); |
158 host()->SendReply(context, | 190 host()->SendReply(context, |
159 PpapiPluginMsg_NetworkProxy_GetProxyForURLReply( | 191 PpapiPluginMsg_NetworkProxy_GetProxyForURLReply( |
160 std::string())); | 192 std::string())); |
161 } | 193 } |
162 | 194 |
163 } // namespace content | 195 } // namespace content |
OLD | NEW |