DevTools: cut host and port from webSocketDebuggerUrl in addition to ws:// prefix
[chromium-blink-merge.git] / content / browser / devtools / protocol / security_handler.cc
blobd2b3f102a9e478775ccbd929191d33450dbd5627
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 kSecurityStateHttp;
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 explanations->push_back(SecurityStateExplanation::Create()
47 ->set_security_state(security_style)
48 ->set_summary(it.summary)
49 ->set_description(it.description));
53 } // namespace
55 SecurityHandler::SecurityHandler()
56 : enabled_(false),
57 host_(nullptr) {
60 SecurityHandler::~SecurityHandler() {
63 void SecurityHandler::SetClient(scoped_ptr<Client> client) {
64 client_.swap(client);
67 void SecurityHandler::AttachToRenderFrameHost() {
68 DCHECK(host_);
69 WebContents* web_contents = WebContents::FromRenderFrameHost(host_);
70 WebContentsObserver::Observe(web_contents);
72 // Send an initial SecurityStyleChanged event.
73 DCHECK(enabled_);
74 SecurityStyleExplanations security_style_explanations;
75 SecurityStyle security_style =
76 web_contents->GetDelegate()->GetSecurityStyle(
77 web_contents, &security_style_explanations);
78 SecurityStyleChanged(security_style, security_style_explanations);
81 void SecurityHandler::SetRenderFrameHost(RenderFrameHost* host) {
82 host_ = host;
83 if (enabled_ && host_)
84 AttachToRenderFrameHost();
87 void SecurityHandler::SecurityStyleChanged(
88 SecurityStyle security_style,
89 const SecurityStyleExplanations& security_style_explanations) {
90 DCHECK(enabled_);
92 const std::string security_state =
93 SecurityStyleToProtocolSecurityState(security_style);
95 std::vector<scoped_refptr<SecurityStateExplanation>> explanations;
96 AddExplanations(kSecurityStateInsecure,
97 security_style_explanations.broken_explanations,
98 &explanations);
99 AddExplanations(kSecurityStateWarning,
100 security_style_explanations.warning_explanations,
101 &explanations);
103 client_->SecurityStateChanged(SecurityStateChangedParams::Create()
104 ->set_security_state(security_state)
105 ->set_explanations(explanations));
108 Response SecurityHandler::Enable() {
109 enabled_ = true;
110 if (host_)
111 AttachToRenderFrameHost();
113 return Response::OK();
116 Response SecurityHandler::Disable() {
117 enabled_ = false;
118 WebContentsObserver::Observe(nullptr);
119 return Response::OK();
122 } // namespace security
123 } // namespace devtools
124 } // namespace content