Home | History | Annotate | Download | only in pepper
      1 // Copyright (c) 2012 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_socket_utils.h"
      6 
      7 #include <string>
      8 #include <vector>
      9 
     10 #include "base/logging.h"
     11 #include "content/public/browser/browser_thread.h"
     12 #include "content/public/browser/content_browser_client.h"
     13 #include "content/public/browser/render_view_host.h"
     14 #include "content/public/browser/site_instance.h"
     15 #include "content/public/common/content_client.h"
     16 #include "ppapi/c/private/ppb_net_address_private.h"
     17 #include "ppapi/shared_impl/private/net_address_private_impl.h"
     18 
     19 namespace content {
     20 namespace pepper_socket_utils {
     21 
     22 SocketPermissionRequest CreateSocketPermissionRequest(
     23     SocketPermissionRequest::OperationType type,
     24     const PP_NetAddress_Private& net_addr) {
     25   std::string host = ppapi::NetAddressPrivateImpl::DescribeNetAddress(net_addr,
     26                                                                       false);
     27   int port = 0;
     28   std::vector<unsigned char> address;
     29   ppapi::NetAddressPrivateImpl::NetAddressToIPEndPoint(net_addr,
     30                                                        &address,
     31                                                        &port);
     32   return SocketPermissionRequest(type, host, port);
     33 }
     34 
     35 bool CanUseSocketAPIs(bool external_plugin,
     36                       bool private_api,
     37                       const SocketPermissionRequest& params,
     38                       int render_process_id,
     39                       int render_view_id) {
     40   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
     41   RenderViewHost* render_view_host = RenderViewHost::FromID(render_process_id,
     42                                                             render_view_id);
     43   return render_view_host && CanUseSocketAPIs(external_plugin,
     44                                               private_api,
     45                                               params,
     46                                               render_view_host);
     47 }
     48 
     49 bool CanUseSocketAPIs(bool external_plugin,
     50                       bool private_api,
     51                       const SocketPermissionRequest& params,
     52                       RenderViewHost* render_view_host) {
     53   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
     54 
     55   if (!external_plugin) {
     56     // Always allow socket APIs for out-process plugins (other than external
     57     // plugins instantiated by the embeeder through
     58     // BrowserPpapiHost::CreateExternalPluginProcess).
     59     return true;
     60   }
     61 
     62   if (!render_view_host)
     63     return false;
     64   SiteInstance* site_instance = render_view_host->GetSiteInstance();
     65   if (!site_instance)
     66     return false;
     67   if (!GetContentClient()->browser()->AllowPepperSocketAPI(
     68           site_instance->GetBrowserContext(),
     69           site_instance->GetSiteURL(),
     70           private_api,
     71           params)) {
     72     LOG(ERROR) << "Host " << site_instance->GetSiteURL().host()
     73                << " cannot use socket API or destination is not allowed";
     74     return false;
     75   }
     76 
     77   return true;
     78 }
     79 
     80 }  // namespace pepper_socket_utils
     81 }  // namespace content
     82