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