pepper_socket_utils.cc revision 2a99a7e74a7f215066514fe81d2bfa6639d9eddd
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
19namespace content {
20namespace pepper_socket_utils {
21
22SocketPermissionRequest 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
35bool CanUseSocketAPIs(bool external_plugin,
36                      const SocketPermissionRequest& params,
37                      RenderViewHost* render_view_host) {
38  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
39
40  if (!external_plugin) {
41    // Always allow socket APIs for out-process plugins (other than external
42    // plugins instantiated by the embeeder through
43    // BrowserPpapiHost::CreateExternalPluginProcess).
44    return true;
45  }
46
47  if (!render_view_host)
48    return false;
49  SiteInstance* site_instance = render_view_host->GetSiteInstance();
50  if (!site_instance)
51    return false;
52  if (!GetContentClient()->browser()->AllowPepperSocketAPI(
53          site_instance->GetBrowserContext(),
54          site_instance->GetSiteURL(),
55          params)) {
56    LOG(ERROR) << "Host " << site_instance->GetSiteURL().host()
57               << " cannot use socket API or destination is not allowed";
58    return false;
59  }
60
61  return true;
62}
63
64}  // namespace pepper_socket_utils
65}  // namespace content
66