IndexedDBFactory now ForceCloses databases.
[chromium-blink-merge.git] / content / browser / renderer_host / pepper / pepper_socket_utils.cc
blob9fc17c50e947dcf092b3ab318db7268d7675b655
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.
5 #include "content/browser/renderer_host/pepper/pepper_socket_utils.h"
7 #include <string>
8 #include <vector>
10 #include "base/logging.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/strings/string_util.h"
13 #include "content/public/browser/browser_thread.h"
14 #include "content/public/browser/content_browser_client.h"
15 #include "content/public/browser/render_frame_host.h"
16 #include "content/public/browser/site_instance.h"
17 #include "content/public/common/content_client.h"
18 #include "net/cert/x509_certificate.h"
19 #include "ppapi/c/private/ppb_net_address_private.h"
20 #include "ppapi/shared_impl/private/net_address_private_impl.h"
21 #include "ppapi/shared_impl/private/ppb_x509_certificate_private_shared.h"
23 namespace content {
24 namespace pepper_socket_utils {
26 SocketPermissionRequest CreateSocketPermissionRequest(
27 SocketPermissionRequest::OperationType type,
28 const PP_NetAddress_Private& net_addr) {
29 std::string host = ppapi::NetAddressPrivateImpl::DescribeNetAddress(net_addr,
30 false);
31 int port = 0;
32 std::vector<unsigned char> address;
33 ppapi::NetAddressPrivateImpl::NetAddressToIPEndPoint(net_addr,
34 &address,
35 &port);
36 return SocketPermissionRequest(type, host, port);
39 bool CanUseSocketAPIs(bool external_plugin,
40 bool private_api,
41 const SocketPermissionRequest* params,
42 int render_process_id,
43 int render_frame_id) {
44 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
45 if (!external_plugin) {
46 // Always allow socket APIs for out-process plugins (other than external
47 // plugins instantiated by the embeeder through
48 // BrowserPpapiHost::CreateExternalPluginProcess).
49 return true;
52 RenderFrameHost* render_frame_host =
53 RenderFrameHost::FromID(render_process_id, render_frame_id);
54 if (!render_frame_host)
55 return false;
56 SiteInstance* site_instance = render_frame_host->GetSiteInstance();
57 if (!site_instance)
58 return false;
59 if (!GetContentClient()->browser()->AllowPepperSocketAPI(
60 site_instance->GetBrowserContext(),
61 site_instance->GetSiteURL(),
62 private_api,
63 params)) {
64 LOG(ERROR) << "Host " << site_instance->GetSiteURL().host()
65 << " cannot use socket API or destination is not allowed";
66 return false;
69 return true;
72 bool GetCertificateFields(const net::X509Certificate& cert,
73 ppapi::PPB_X509Certificate_Fields* fields) {
74 const net::CertPrincipal& issuer = cert.issuer();
75 fields->SetField(PP_X509CERTIFICATE_PRIVATE_ISSUER_COMMON_NAME,
76 new base::StringValue(issuer.common_name));
77 fields->SetField(PP_X509CERTIFICATE_PRIVATE_ISSUER_LOCALITY_NAME,
78 new base::StringValue(issuer.locality_name));
79 fields->SetField(PP_X509CERTIFICATE_PRIVATE_ISSUER_STATE_OR_PROVINCE_NAME,
80 new base::StringValue(issuer.state_or_province_name));
81 fields->SetField(PP_X509CERTIFICATE_PRIVATE_ISSUER_COUNTRY_NAME,
82 new base::StringValue(issuer.country_name));
83 fields->SetField(PP_X509CERTIFICATE_PRIVATE_ISSUER_ORGANIZATION_NAME,
84 new base::StringValue(JoinString(issuer.organization_names, '\n')));
85 fields->SetField(PP_X509CERTIFICATE_PRIVATE_ISSUER_ORGANIZATION_UNIT_NAME,
86 new base::StringValue(JoinString(issuer.organization_unit_names, '\n')));
88 const net::CertPrincipal& subject = cert.subject();
89 fields->SetField(PP_X509CERTIFICATE_PRIVATE_SUBJECT_COMMON_NAME,
90 new base::StringValue(subject.common_name));
91 fields->SetField(PP_X509CERTIFICATE_PRIVATE_SUBJECT_LOCALITY_NAME,
92 new base::StringValue(subject.locality_name));
93 fields->SetField(PP_X509CERTIFICATE_PRIVATE_SUBJECT_STATE_OR_PROVINCE_NAME,
94 new base::StringValue(subject.state_or_province_name));
95 fields->SetField(PP_X509CERTIFICATE_PRIVATE_SUBJECT_COUNTRY_NAME,
96 new base::StringValue(subject.country_name));
97 fields->SetField(PP_X509CERTIFICATE_PRIVATE_SUBJECT_ORGANIZATION_NAME,
98 new base::StringValue(JoinString(subject.organization_names, '\n')));
99 fields->SetField(PP_X509CERTIFICATE_PRIVATE_SUBJECT_ORGANIZATION_UNIT_NAME,
100 new base::StringValue(JoinString(subject.organization_unit_names, '\n')));
102 const std::string& serial_number = cert.serial_number();
103 fields->SetField(PP_X509CERTIFICATE_PRIVATE_SERIAL_NUMBER,
104 base::BinaryValue::CreateWithCopiedBuffer(serial_number.data(),
105 serial_number.length()));
106 fields->SetField(PP_X509CERTIFICATE_PRIVATE_VALIDITY_NOT_BEFORE,
107 new base::FundamentalValue(cert.valid_start().ToDoubleT()));
108 fields->SetField(PP_X509CERTIFICATE_PRIVATE_VALIDITY_NOT_AFTER,
109 new base::FundamentalValue(cert.valid_expiry().ToDoubleT()));
110 std::string der;
111 net::X509Certificate::GetDEREncoded(cert.os_cert_handle(), &der);
112 fields->SetField(PP_X509CERTIFICATE_PRIVATE_RAW,
113 base::BinaryValue::CreateWithCopiedBuffer(der.data(), der.length()));
114 return true;
117 bool GetCertificateFields(const char* der,
118 uint32_t length,
119 ppapi::PPB_X509Certificate_Fields* fields) {
120 scoped_refptr<net::X509Certificate> cert =
121 net::X509Certificate::CreateFromBytes(der, length);
122 if (!cert.get())
123 return false;
124 return GetCertificateFields(*cert.get(), fields);
127 } // namespace pepper_socket_utils
128 } // namespace content