Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / content / browser / renderer_host / pepper / pepper_socket_utils.cc
blob718eec2f0971ba85873ce279798780f3c1978f5d
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 =
30 ppapi::NetAddressPrivateImpl::DescribeNetAddress(net_addr, false);
31 uint16 port = 0;
32 std::vector<unsigned char> address;
33 ppapi::NetAddressPrivateImpl::NetAddressToIPEndPoint(
34 net_addr, &address, &port);
35 return SocketPermissionRequest(type, host, port);
38 bool CanUseSocketAPIs(bool external_plugin,
39 bool private_api,
40 const SocketPermissionRequest* params,
41 int render_process_id,
42 int render_frame_id) {
43 DCHECK_CURRENTLY_ON(BrowserThread::UI);
44 if (!external_plugin) {
45 // Always allow socket APIs for out-process plugins (other than external
46 // plugins instantiated by the embeeder through
47 // BrowserPpapiHost::CreateExternalPluginProcess).
48 return true;
51 RenderFrameHost* render_frame_host =
52 RenderFrameHost::FromID(render_process_id, render_frame_id);
53 if (!render_frame_host)
54 return false;
55 SiteInstance* site_instance = render_frame_host->GetSiteInstance();
56 if (!site_instance)
57 return false;
58 if (!GetContentClient()->browser()->AllowPepperSocketAPI(
59 site_instance->GetBrowserContext(),
60 site_instance->GetSiteURL(),
61 private_api,
62 params)) {
63 LOG(ERROR) << "Host " << site_instance->GetSiteURL().host()
64 << " cannot use socket API or destination is not allowed";
65 return false;
68 return true;
71 bool GetCertificateFields(const net::X509Certificate& cert,
72 ppapi::PPB_X509Certificate_Fields* fields) {
73 const net::CertPrincipal& issuer = cert.issuer();
74 fields->SetField(PP_X509CERTIFICATE_PRIVATE_ISSUER_COMMON_NAME,
75 new base::StringValue(issuer.common_name));
76 fields->SetField(PP_X509CERTIFICATE_PRIVATE_ISSUER_LOCALITY_NAME,
77 new base::StringValue(issuer.locality_name));
78 fields->SetField(PP_X509CERTIFICATE_PRIVATE_ISSUER_STATE_OR_PROVINCE_NAME,
79 new base::StringValue(issuer.state_or_province_name));
80 fields->SetField(PP_X509CERTIFICATE_PRIVATE_ISSUER_COUNTRY_NAME,
81 new base::StringValue(issuer.country_name));
82 fields->SetField(
83 PP_X509CERTIFICATE_PRIVATE_ISSUER_ORGANIZATION_NAME,
84 new base::StringValue(base::JoinString(issuer.organization_names, "\n")));
85 fields->SetField(
86 PP_X509CERTIFICATE_PRIVATE_ISSUER_ORGANIZATION_UNIT_NAME,
87 new base::StringValue(
88 base::JoinString(issuer.organization_unit_names, "\n")));
90 const net::CertPrincipal& subject = cert.subject();
91 fields->SetField(PP_X509CERTIFICATE_PRIVATE_SUBJECT_COMMON_NAME,
92 new base::StringValue(subject.common_name));
93 fields->SetField(PP_X509CERTIFICATE_PRIVATE_SUBJECT_LOCALITY_NAME,
94 new base::StringValue(subject.locality_name));
95 fields->SetField(PP_X509CERTIFICATE_PRIVATE_SUBJECT_STATE_OR_PROVINCE_NAME,
96 new base::StringValue(subject.state_or_province_name));
97 fields->SetField(PP_X509CERTIFICATE_PRIVATE_SUBJECT_COUNTRY_NAME,
98 new base::StringValue(subject.country_name));
99 fields->SetField(
100 PP_X509CERTIFICATE_PRIVATE_SUBJECT_ORGANIZATION_NAME,
101 new base::StringValue(
102 base::JoinString(subject.organization_names, "\n")));
103 fields->SetField(
104 PP_X509CERTIFICATE_PRIVATE_SUBJECT_ORGANIZATION_UNIT_NAME,
105 new base::StringValue(
106 base::JoinString(subject.organization_unit_names, "\n")));
108 const std::string& serial_number = cert.serial_number();
109 fields->SetField(PP_X509CERTIFICATE_PRIVATE_SERIAL_NUMBER,
110 base::BinaryValue::CreateWithCopiedBuffer(
111 serial_number.data(), serial_number.length()));
112 fields->SetField(PP_X509CERTIFICATE_PRIVATE_VALIDITY_NOT_BEFORE,
113 new base::FundamentalValue(cert.valid_start().ToDoubleT()));
114 fields->SetField(PP_X509CERTIFICATE_PRIVATE_VALIDITY_NOT_AFTER,
115 new base::FundamentalValue(cert.valid_expiry().ToDoubleT()));
116 std::string der;
117 net::X509Certificate::GetDEREncoded(cert.os_cert_handle(), &der);
118 fields->SetField(
119 PP_X509CERTIFICATE_PRIVATE_RAW,
120 base::BinaryValue::CreateWithCopiedBuffer(der.data(), der.length()));
121 return true;
124 bool GetCertificateFields(const char* der,
125 uint32_t length,
126 ppapi::PPB_X509Certificate_Fields* fields) {
127 scoped_refptr<net::X509Certificate> cert =
128 net::X509Certificate::CreateFromBytes(der, length);
129 if (!cert.get())
130 return false;
131 return GetCertificateFields(*cert.get(), fields);
134 } // namespace pepper_socket_utils
135 } // namespace content