Only grant permissions to new extensions from sync if they have the expected version
[chromium-blink-merge.git] / chrome / browser / browser_about_handler.cc
blobae7e9ba165b311b881f7be0cfb9281b0a435fb68
1 // Copyright (c) 2012 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 "chrome/browser/browser_about_handler.h"
7 #include <string>
9 #include "base/bind.h"
10 #include "base/location.h"
11 #include "base/logging.h"
12 #include "base/single_thread_task_runner.h"
13 #include "base/strings/string_util.h"
14 #include "base/thread_task_runner_handle.h"
15 #include "chrome/browser/lifetime/application_lifetime.h"
16 #include "chrome/browser/ui/browser_dialogs.h"
17 #include "chrome/common/chrome_switches.h"
18 #include "chrome/common/url_constants.h"
19 #include "components/url_formatter/url_fixer.h"
21 bool FixupBrowserAboutURL(GURL* url,
22 content::BrowserContext* browser_context) {
23 // Ensure that any cleanup done by FixupURL happens before the rewriting
24 // phase that determines the virtual URL, by including it in an initial
25 // URLHandler. This prevents minor changes from producing a virtual URL,
26 // which could lead to a URL spoof.
27 *url = url_formatter::FixupURL(url->possibly_invalid_spec(), std::string());
28 return true;
31 bool WillHandleBrowserAboutURL(GURL* url,
32 content::BrowserContext* browser_context) {
33 // TODO(msw): Eliminate "about:*" constants and literals from code and tests,
34 // then hopefully we can remove this forced fixup.
35 FixupBrowserAboutURL(url, browser_context);
37 // Check that about: URLs are fixed up to chrome: by url_formatter::FixupURL.
38 DCHECK((*url == GURL(url::kAboutBlankURL)) ||
39 !url->SchemeIs(url::kAboutScheme));
41 // Only handle chrome://foo/, url_formatter::FixupURL translates about:foo.
42 if (!url->SchemeIs(content::kChromeUIScheme))
43 return false;
45 std::string host(url->host());
46 std::string path;
47 // Replace about with chrome-urls.
48 if (host == chrome::kChromeUIAboutHost)
49 host = chrome::kChromeUIChromeURLsHost;
50 // Replace cache with view-http-cache.
51 if (host == chrome::kChromeUICacheHost) {
52 host = content::kChromeUINetworkViewCacheHost;
53 // Replace sync with sync-internals (for legacy reasons).
54 } else if (host == chrome::kChromeUISyncHost) {
55 host = chrome::kChromeUISyncInternalsHost;
56 // Redirect chrome://extensions.
57 } else if (host == chrome::kChromeUIExtensionsHost) {
58 // If the material design extensions page is enabled, it gets its own host.
59 // Otherwise, it's handled by the uber settings page.
60 if (::switches::MdExtensionsEnabled()) {
61 host = chrome::kChromeUIExtensionsHost;
62 path = url->path();
63 } else {
64 host = chrome::kChromeUIUberHost;
65 path = chrome::kChromeUIExtensionsHost + url->path();
67 // Redirect chrome://settings/extensions (legacy URL).
68 } else if (host == chrome::kChromeUISettingsHost &&
69 url->path() == std::string("/") + chrome::kExtensionsSubPage) {
70 host = chrome::kChromeUIUberHost;
71 path = chrome::kChromeUIExtensionsHost;
72 // Redirect chrome://history.
73 } else if (host == chrome::kChromeUIHistoryHost) {
74 #if defined(OS_ANDROID)
75 // On Android, redirect directly to chrome://history-frame since
76 // uber page is unsupported.
77 host = chrome::kChromeUIHistoryFrameHost;
78 #else
79 host = chrome::kChromeUIUberHost;
80 path = chrome::kChromeUIHistoryHost + url->path();
81 #endif
82 // Redirect chrome://settings
83 } else if (host == chrome::kChromeUISettingsHost) {
84 if (::switches::AboutInSettingsEnabled()) {
85 host = chrome::kChromeUISettingsFrameHost;
86 } else {
87 host = chrome::kChromeUIUberHost;
88 path = chrome::kChromeUISettingsHost + url->path();
90 // Redirect chrome://help
91 } else if (host == chrome::kChromeUIHelpHost) {
92 if (::switches::AboutInSettingsEnabled()) {
93 host = chrome::kChromeUISettingsFrameHost;
94 if (url->path().empty() || url->path() == "/")
95 path = chrome::kChromeUIHelpHost;
96 } else {
97 host = chrome::kChromeUIUberHost;
98 path = chrome::kChromeUIHelpHost + url->path();
102 GURL::Replacements replacements;
103 replacements.SetHostStr(host);
104 if (!path.empty())
105 replacements.SetPathStr(path);
106 *url = url->ReplaceComponents(replacements);
108 // Having re-written the URL, make the chrome: handler process it.
109 return false;
112 bool HandleNonNavigationAboutURL(const GURL& url) {
113 const std::string spec(url.spec());
115 if (base::LowerCaseEqualsASCII(spec, chrome::kChromeUIRestartURL)) {
116 // Call AttemptRestart after chrome::Navigate() completes to avoid access of
117 // gtk objects after they are destroyed by BrowserWindowGtk::Close().
118 base::ThreadTaskRunnerHandle::Get()->PostTask(
119 FROM_HERE, base::Bind(&chrome::AttemptRestart));
120 return true;
121 } else if (base::LowerCaseEqualsASCII(spec, chrome::kChromeUIQuitURL)) {
122 base::ThreadTaskRunnerHandle::Get()->PostTask(
123 FROM_HERE, base::Bind(&chrome::AttemptExit));
124 return true;
127 return false;