1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
|
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ui/toolbar/chrome_toolbar_model_delegate.h"
#include "base/logging.h"
#include "chrome/browser/autocomplete/chrome_autocomplete_scheme_classifier.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/search/search.h"
#include "chrome/browser/ssl/security_state_tab_helper.h"
#include "chrome/common/pref_names.h"
#include "chrome/common/url_constants.h"
#include "components/google/core/browser/google_util.h"
#include "components/omnibox/browser/autocomplete_input.h"
#include "components/prefs/pref_service.h"
#include "components/security_state/core/security_state.h"
#include "content/public/browser/navigation_controller.h"
#include "content/public/browser/navigation_entry.h"
#include "content/public/browser/ssl_status.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/url_constants.h"
#include "extensions/common/constants.h"
#include "ui/gfx/vector_icons_public.h"
ChromeToolbarModelDelegate::ChromeToolbarModelDelegate() {}
ChromeToolbarModelDelegate::~ChromeToolbarModelDelegate() {}
base::string16 ChromeToolbarModelDelegate::FormattedStringWithEquivalentMeaning(
const GURL& url,
const base::string16& formatted_url) const {
return AutocompleteInput::FormattedStringWithEquivalentMeaning(
url, formatted_url, ChromeAutocompleteSchemeClassifier(GetProfile()));
}
bool ChromeToolbarModelDelegate::GetURL(GURL* url) const {
DCHECK(url);
content::NavigationEntry* entry = GetNavigationEntry();
if (!entry)
return false;
*url = ShouldDisplayURL() ? entry->GetVirtualURL() : GURL();
return true;
}
bool ChromeToolbarModelDelegate::ShouldDisplayURL() const {
// Note: The order here is important.
// - The WebUI test must come before the extension scheme test because there
// can be WebUIs that have extension schemes (e.g. the bookmark manager). In
// that case, we should prefer what the WebUI instance says.
// - The view-source test must come before the NTP test because of the case
// of view-source:chrome://newtab, which should display its URL despite what
// chrome://newtab says.
content::NavigationEntry* entry = GetNavigationEntry();
if (entry) {
if (entry->IsViewSourceMode() ||
entry->GetPageType() == content::PAGE_TYPE_INTERSTITIAL) {
return true;
}
GURL url = entry->GetURL();
GURL virtual_url = entry->GetVirtualURL();
if (url.SchemeIs(content::kChromeUIScheme) ||
virtual_url.SchemeIs(content::kChromeUIScheme)) {
if (!url.SchemeIs(content::kChromeUIScheme))
url = virtual_url;
return url.host() != chrome::kChromeUINewTabHost;
}
}
return !search::IsInstantNTP(GetActiveWebContents());
}
security_state::SecurityLevel ChromeToolbarModelDelegate::GetSecurityLevel()
const {
content::WebContents* web_contents = GetActiveWebContents();
// If there is no active WebContents (which can happen during toolbar
// initialization), assume no security style.
if (!web_contents)
return security_state::NONE;
auto* helper = SecurityStateTabHelper::FromWebContents(web_contents);
security_state::SecurityInfo security_info;
helper->GetSecurityInfo(&security_info);
return security_info.security_level;
}
scoped_refptr<net::X509Certificate> ChromeToolbarModelDelegate::GetCertificate()
const {
content::NavigationEntry* entry = GetNavigationEntry();
if (!entry)
return scoped_refptr<net::X509Certificate>();
return entry->GetSSL().certificate;
}
bool ChromeToolbarModelDelegate::FailsMalwareCheck() const {
content::WebContents* web_contents = GetActiveWebContents();
// If there is no active WebContents (which can happen during toolbar
// initialization), so nothing can fail.
if (!web_contents)
return false;
security_state::SecurityInfo security_info;
SecurityStateTabHelper::FromWebContents(web_contents)
->GetSecurityInfo(&security_info);
return security_info.malicious_content_status !=
security_state::MALICIOUS_CONTENT_STATUS_NONE;
}
gfx::VectorIconId ChromeToolbarModelDelegate::GetVectorIconOverride() const {
#if !defined(OS_ANDROID)
GURL url;
GetURL(&url);
if (url.SchemeIs(content::kChromeUIScheme))
return gfx::VectorIconId::LOCATION_BAR_PRODUCT;
if (url.SchemeIs(extensions::kExtensionScheme))
return gfx::VectorIconId::OMNIBOX_EXTENSION_APP;
#endif
return gfx::VectorIconId::VECTOR_ICON_NONE;
}
content::NavigationController*
ChromeToolbarModelDelegate::GetNavigationController() const {
// This |current_tab| can be null during the initialization of the toolbar
// during window creation (i.e. before any tabs have been added to the
// window).
content::WebContents* current_tab = GetActiveWebContents();
return current_tab ? ¤t_tab->GetController() : nullptr;
}
content::NavigationEntry* ChromeToolbarModelDelegate::GetNavigationEntry()
const {
content::NavigationController* controller = GetNavigationController();
return controller ? controller->GetVisibleEntry() : nullptr;
}
Profile* ChromeToolbarModelDelegate::GetProfile() const {
content::NavigationController* controller = GetNavigationController();
return controller
? Profile::FromBrowserContext(controller->GetBrowserContext())
: nullptr;
}
|