[refactor] More post-NSS WebCrypto cleanups (utility functions).
[chromium-blink-merge.git] / content / browser / devtools / protocol / security_handler.cc
blob50cf64f659b727484696bdc25eef763275bc09ea
1 // Copyright 2015 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/devtools/protocol/security_handler.h"
7 #include <string>
9 #include "content/browser/devtools/protocol/devtools_protocol_dispatcher.h"
10 #include "content/public/browser/security_style_explanations.h"
11 #include "content/public/browser/web_contents.h"
12 #include "content/public/browser/web_contents_delegate.h"
14 namespace content {
15 namespace devtools {
16 namespace security {
18 typedef DevToolsProtocolClient::Response Response;
20 namespace {
22 std::string SecurityStyleToProtocolSecurityState(
23 SecurityStyle security_style) {
24 switch (security_style) {
25 case SECURITY_STYLE_UNKNOWN:
26 return kSecurityStateUnknown;
27 case SECURITY_STYLE_UNAUTHENTICATED:
28 return kSecurityStateNeutral;
29 case SECURITY_STYLE_AUTHENTICATION_BROKEN:
30 return kSecurityStateInsecure;
31 case SECURITY_STYLE_WARNING:
32 return kSecurityStateWarning;
33 case SECURITY_STYLE_AUTHENTICATED:
34 return kSecurityStateSecure;
35 default:
36 NOTREACHED();
37 return kSecurityStateUnknown;
41 void AddExplanations(
42 const std::string& security_style,
43 const std::vector<SecurityStyleExplanation>& explanations_to_add,
44 std::vector<scoped_refptr<SecurityStateExplanation>>* explanations) {
45 for (const auto& it : explanations_to_add) {
46 scoped_refptr<SecurityStateExplanation> explanation =
47 SecurityStateExplanation::Create()->set_security_state(security_style)
48 ->set_summary(it.summary)
49 ->set_description(it.description);
50 if (it.cert_id > 0)
51 explanation->set_certificate_id(it.cert_id);
52 explanations->push_back(explanation);
56 } // namespace
58 SecurityHandler::SecurityHandler()
59 : enabled_(false),
60 host_(nullptr) {
63 SecurityHandler::~SecurityHandler() {
66 void SecurityHandler::SetClient(scoped_ptr<Client> client) {
67 client_.swap(client);
70 void SecurityHandler::AttachToRenderFrameHost() {
71 DCHECK(host_);
72 WebContents* web_contents = WebContents::FromRenderFrameHost(host_);
73 WebContentsObserver::Observe(web_contents);
75 // Send an initial SecurityStyleChanged event.
76 DCHECK(enabled_);
77 SecurityStyleExplanations security_style_explanations;
78 SecurityStyle security_style =
79 web_contents->GetDelegate()->GetSecurityStyle(
80 web_contents, &security_style_explanations);
81 SecurityStyleChanged(security_style, security_style_explanations);
84 void SecurityHandler::SetRenderFrameHost(RenderFrameHost* host) {
85 host_ = host;
86 if (enabled_ && host_)
87 AttachToRenderFrameHost();
90 void SecurityHandler::SecurityStyleChanged(
91 SecurityStyle security_style,
92 const SecurityStyleExplanations& security_style_explanations) {
93 DCHECK(enabled_);
95 const std::string security_state =
96 SecurityStyleToProtocolSecurityState(security_style);
98 std::vector<scoped_refptr<SecurityStateExplanation>> explanations;
99 AddExplanations(kSecurityStateInsecure,
100 security_style_explanations.broken_explanations,
101 &explanations);
102 AddExplanations(kSecurityStateWarning,
103 security_style_explanations.warning_explanations,
104 &explanations);
105 AddExplanations(kSecurityStateSecure,
106 security_style_explanations.secure_explanations,
107 &explanations);
109 scoped_refptr<MixedContentStatus> mixed_content_status =
110 MixedContentStatus::Create()
111 ->set_ran_insecure_content(
112 security_style_explanations.ran_insecure_content)
113 ->set_displayed_insecure_content(
114 security_style_explanations.displayed_insecure_content)
115 ->set_ran_insecure_content_style(SecurityStyleToProtocolSecurityState(
116 security_style_explanations.ran_insecure_content_style))
117 ->set_displayed_insecure_content_style(
118 SecurityStyleToProtocolSecurityState(
119 security_style_explanations
120 .displayed_insecure_content_style));
122 client_->SecurityStateChanged(
123 SecurityStateChangedParams::Create()
124 ->set_security_state(security_state)
125 ->set_scheme_is_cryptographic(
126 security_style_explanations.scheme_is_cryptographic)
127 ->set_mixed_content_status(mixed_content_status)
128 ->set_explanations(explanations));
131 Response SecurityHandler::Enable() {
132 enabled_ = true;
133 if (host_)
134 AttachToRenderFrameHost();
136 return Response::OK();
139 Response SecurityHandler::Disable() {
140 enabled_ = false;
141 WebContentsObserver::Observe(nullptr);
142 return Response::OK();
145 } // namespace security
146 } // namespace devtools
147 } // namespace content