OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/browser/devtools/protocol/security_handler.h" | 5 #include "content/browser/devtools/protocol/security_handler.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "content/browser/frame_host/render_frame_host_impl.h" | 9 #include "content/browser/devtools/protocol/devtools_protocol_dispatcher.h" |
10 #include "content/public/browser/navigation_controller.h" | 10 #include "content/public/browser/navigation_controller.h" |
11 #include "content/public/browser/navigation_entry.h" | 11 #include "content/public/browser/navigation_entry.h" |
12 #include "content/public/browser/security_style_explanations.h" | 12 #include "content/public/browser/security_style_explanations.h" |
13 #include "content/public/browser/ssl_status.h" | 13 #include "content/public/browser/ssl_status.h" |
14 #include "content/public/browser/web_contents.h" | 14 #include "content/public/browser/web_contents.h" |
15 #include "content/public/browser/web_contents_delegate.h" | 15 #include "content/public/browser/web_contents_delegate.h" |
16 | 16 |
17 namespace content { | 17 namespace content { |
18 namespace protocol { | 18 namespace devtools { |
| 19 namespace security { |
19 | 20 |
20 using Explanations = protocol::Array<Security::SecurityStateExplanation>; | 21 typedef DevToolsProtocolClient::Response Response; |
21 | 22 |
22 namespace { | 23 namespace { |
23 | 24 |
24 std::string SecurityStyleToProtocolSecurityState( | 25 std::string SecurityStyleToProtocolSecurityState( |
25 blink::WebSecurityStyle security_style) { | 26 blink::WebSecurityStyle security_style) { |
26 switch (security_style) { | 27 switch (security_style) { |
27 case blink::WebSecurityStyleUnknown: | 28 case blink::WebSecurityStyleUnknown: |
28 return Security::SecurityStateEnum::Unknown; | 29 return kSecurityStateUnknown; |
29 case blink::WebSecurityStyleUnauthenticated: | 30 case blink::WebSecurityStyleUnauthenticated: |
30 return Security::SecurityStateEnum::Neutral; | 31 return kSecurityStateNeutral; |
31 case blink::WebSecurityStyleAuthenticationBroken: | 32 case blink::WebSecurityStyleAuthenticationBroken: |
32 return Security::SecurityStateEnum::Insecure; | 33 return kSecurityStateInsecure; |
33 case blink::WebSecurityStyleWarning: | 34 case blink::WebSecurityStyleWarning: |
34 return Security::SecurityStateEnum::Warning; | 35 return kSecurityStateWarning; |
35 case blink::WebSecurityStyleAuthenticated: | 36 case blink::WebSecurityStyleAuthenticated: |
36 return Security::SecurityStateEnum::Secure; | 37 return kSecurityStateSecure; |
37 default: | 38 default: |
38 NOTREACHED(); | 39 NOTREACHED(); |
39 return Security::SecurityStateEnum::Unknown; | 40 return kSecurityStateUnknown; |
40 } | 41 } |
41 } | 42 } |
42 | 43 |
43 void AddExplanations( | 44 void AddExplanations( |
44 const std::string& security_style, | 45 const std::string& security_style, |
45 const std::vector<SecurityStyleExplanation>& explanations_to_add, | 46 const std::vector<SecurityStyleExplanation>& explanations_to_add, |
46 Explanations* explanations) { | 47 std::vector<scoped_refptr<SecurityStateExplanation>>* explanations) { |
47 for (const auto& it : explanations_to_add) { | 48 for (const auto& it : explanations_to_add) { |
48 explanations->addItem(Security::SecurityStateExplanation::Create() | 49 scoped_refptr<SecurityStateExplanation> explanation = |
49 .SetSecurityState(security_style) | 50 SecurityStateExplanation::Create() |
50 .SetSummary(it.summary) | 51 ->set_security_state(security_style) |
51 .SetDescription(it.description) | 52 ->set_summary(it.summary) |
52 .SetHasCertificate(it.has_certificate) | 53 ->set_description(it.description) |
53 .Build()); | 54 ->set_has_certificate(it.has_certificate); |
| 55 explanations->push_back(explanation); |
54 } | 56 } |
55 } | 57 } |
56 | 58 |
57 } // namespace | 59 } // namespace |
58 | 60 |
59 SecurityHandler::SecurityHandler() | 61 SecurityHandler::SecurityHandler() |
60 : enabled_(false), | 62 : enabled_(false), |
61 host_(nullptr) { | 63 host_(nullptr) { |
62 } | 64 } |
63 | 65 |
64 SecurityHandler::~SecurityHandler() { | 66 SecurityHandler::~SecurityHandler() { |
65 } | 67 } |
66 | 68 |
67 void SecurityHandler::Wire(UberDispatcher* dispatcher) { | 69 void SecurityHandler::SetClient(std::unique_ptr<Client> client) { |
68 frontend_.reset(new Security::Frontend(dispatcher->channel())); | 70 client_.swap(client); |
69 Security::Dispatcher::wire(dispatcher, this); | |
70 } | 71 } |
71 | 72 |
72 void SecurityHandler::AttachToRenderFrameHost() { | 73 void SecurityHandler::AttachToRenderFrameHost() { |
73 DCHECK(host_); | 74 DCHECK(host_); |
74 WebContents* web_contents = WebContents::FromRenderFrameHost(host_); | 75 WebContents* web_contents = WebContents::FromRenderFrameHost(host_); |
75 WebContentsObserver::Observe(web_contents); | 76 WebContentsObserver::Observe(web_contents); |
76 | 77 |
77 // Send an initial DidChangeVisibleSecurityState event. | 78 // Send an initial DidChangeVisibleSecurityState event. |
78 DCHECK(enabled_); | 79 DCHECK(enabled_); |
79 DidChangeVisibleSecurityState(); | 80 DidChangeVisibleSecurityState(); |
80 } | 81 } |
81 | 82 |
82 void SecurityHandler::SetRenderFrameHost(RenderFrameHostImpl* host) { | 83 void SecurityHandler::SetRenderFrameHost(RenderFrameHost* host) { |
83 host_ = host; | 84 host_ = host; |
84 if (enabled_ && host_) | 85 if (enabled_ && host_) |
85 AttachToRenderFrameHost(); | 86 AttachToRenderFrameHost(); |
86 } | 87 } |
87 | 88 |
88 void SecurityHandler::DidChangeVisibleSecurityState() { | 89 void SecurityHandler::DidChangeVisibleSecurityState() { |
89 DCHECK(enabled_); | 90 DCHECK(enabled_); |
90 | 91 |
91 SecurityStyleExplanations security_style_explanations; | 92 SecurityStyleExplanations security_style_explanations; |
92 blink::WebSecurityStyle security_style = | 93 blink::WebSecurityStyle security_style = |
93 web_contents()->GetDelegate()->GetSecurityStyle( | 94 web_contents()->GetDelegate()->GetSecurityStyle( |
94 web_contents(), &security_style_explanations); | 95 web_contents(), &security_style_explanations); |
95 | 96 |
96 const std::string security_state = | 97 const std::string security_state = |
97 SecurityStyleToProtocolSecurityState(security_style); | 98 SecurityStyleToProtocolSecurityState(security_style); |
98 | 99 |
99 std::unique_ptr<Explanations> explanations = Explanations::create(); | 100 std::vector<scoped_refptr<SecurityStateExplanation>> explanations; |
100 AddExplanations(Security::SecurityStateEnum::Insecure, | 101 AddExplanations(kSecurityStateInsecure, |
101 security_style_explanations.broken_explanations, | 102 security_style_explanations.broken_explanations, |
102 explanations.get()); | 103 &explanations); |
103 AddExplanations(Security::SecurityStateEnum::Neutral, | 104 AddExplanations(kSecurityStateNeutral, |
104 security_style_explanations.unauthenticated_explanations, | 105 security_style_explanations.unauthenticated_explanations, |
105 explanations.get()); | 106 &explanations); |
106 AddExplanations(Security::SecurityStateEnum::Secure, | 107 AddExplanations(kSecurityStateSecure, |
107 security_style_explanations.secure_explanations, | 108 security_style_explanations.secure_explanations, |
108 explanations.get()); | 109 &explanations); |
109 AddExplanations(Security::SecurityStateEnum::Info, | 110 AddExplanations(kSecurityStateInfo, |
110 security_style_explanations.info_explanations, | 111 security_style_explanations.info_explanations, &explanations); |
111 explanations.get()); | |
112 | 112 |
113 std::unique_ptr<Security::InsecureContentStatus> insecure_content_status = | 113 scoped_refptr<InsecureContentStatus> insecure_content_status = |
114 Security::InsecureContentStatus::Create() | 114 InsecureContentStatus::Create() |
115 .SetRanMixedContent(security_style_explanations.ran_mixed_content) | 115 ->set_ran_mixed_content(security_style_explanations.ran_mixed_content) |
116 .SetDisplayedMixedContent( | 116 ->set_displayed_mixed_content( |
117 security_style_explanations.displayed_mixed_content) | 117 security_style_explanations.displayed_mixed_content) |
118 .SetRanContentWithCertErrors( | 118 ->set_ran_content_with_cert_errors( |
119 security_style_explanations.ran_content_with_cert_errors) | 119 security_style_explanations.ran_content_with_cert_errors) |
120 .SetDisplayedContentWithCertErrors( | 120 ->set_displayed_content_with_cert_errors( |
121 security_style_explanations.displayed_content_with_cert_errors) | 121 security_style_explanations.displayed_content_with_cert_errors) |
122 .SetRanInsecureContentStyle(SecurityStyleToProtocolSecurityState( | 122 ->set_ran_insecure_content_style(SecurityStyleToProtocolSecurityState( |
123 security_style_explanations.ran_insecure_content_style)) | 123 security_style_explanations.ran_insecure_content_style)) |
124 .SetDisplayedInsecureContentStyle( | 124 ->set_displayed_insecure_content_style( |
125 SecurityStyleToProtocolSecurityState( | 125 SecurityStyleToProtocolSecurityState( |
126 security_style_explanations | 126 security_style_explanations |
127 .displayed_insecure_content_style)) | 127 .displayed_insecure_content_style)); |
128 .Build(); | |
129 | 128 |
130 frontend_->SecurityStateChanged( | 129 client_->SecurityStateChanged( |
131 security_state, | 130 SecurityStateChangedParams::Create() |
132 std::move(explanations), | 131 ->set_security_state(security_state) |
133 std::move(insecure_content_status), | 132 ->set_scheme_is_cryptographic( |
134 security_style_explanations.scheme_is_cryptographic); | 133 security_style_explanations.scheme_is_cryptographic) |
| 134 ->set_insecure_content_status(insecure_content_status) |
| 135 ->set_explanations(explanations)); |
135 } | 136 } |
136 | 137 |
137 Response SecurityHandler::Enable() { | 138 Response SecurityHandler::Enable() { |
138 enabled_ = true; | 139 enabled_ = true; |
139 if (host_) | 140 if (host_) |
140 AttachToRenderFrameHost(); | 141 AttachToRenderFrameHost(); |
141 | 142 |
142 return Response::OK(); | 143 return Response::OK(); |
143 } | 144 } |
144 | 145 |
145 Response SecurityHandler::Disable() { | 146 Response SecurityHandler::Disable() { |
146 enabled_ = false; | 147 enabled_ = false; |
147 WebContentsObserver::Observe(nullptr); | 148 WebContentsObserver::Observe(nullptr); |
148 return Response::OK(); | 149 return Response::OK(); |
149 } | 150 } |
150 | 151 |
151 Response SecurityHandler::ShowCertificateViewer() { | 152 Response SecurityHandler::ShowCertificateViewer() { |
152 if (!host_) | 153 if (!host_) |
153 return Response::InternalError(); | 154 return Response::InternalError("Could not connect to view"); |
154 WebContents* web_contents = WebContents::FromRenderFrameHost(host_); | 155 WebContents* web_contents = WebContents::FromRenderFrameHost(host_); |
155 scoped_refptr<net::X509Certificate> certificate = | 156 scoped_refptr<net::X509Certificate> certificate = |
156 web_contents->GetController().GetVisibleEntry()->GetSSL().certificate; | 157 web_contents->GetController().GetVisibleEntry()->GetSSL().certificate; |
157 if (!certificate) | 158 if (!certificate) |
158 return Response::Error("Could not find certificate"); | 159 return Response::InternalError("Could not find certificate"); |
159 web_contents->GetDelegate()->ShowCertificateViewerInDevTools( | 160 web_contents->GetDelegate()->ShowCertificateViewerInDevTools( |
160 web_contents, certificate); | 161 web_contents, certificate); |
161 return Response::OK(); | 162 return Response::OK(); |
162 } | 163 } |
163 | 164 |
164 } // namespace protocol | 165 } // namespace security |
| 166 } // namespace devtools |
165 } // namespace content | 167 } // namespace content |
OLD | NEW |