-
Notifications
You must be signed in to change notification settings - Fork 15k
/
atom_web_ui_controller_factory.cc
99 lines (79 loc) · 3.12 KB
/
atom_web_ui_controller_factory.cc
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
// Copyright (c) 2017 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#include "atom/browser/atom_web_ui_controller_factory.h"
#include <string>
#include "electron/buildflags/buildflags.h"
#if BUILDFLAG(ENABLE_PDF_VIEWER)
#include "atom/browser/ui/webui/pdf_viewer_ui.h"
#include "atom/common/atom_constants.h"
#include "base/strings/string_split.h"
#include "base/strings/string_util.h"
#include "net/base/escape.h"
#endif // BUILDFLAG(ENABLE_PDF_VIEWER)
#include "atom/browser/ui/devtools_ui.h"
#include "content/public/browser/web_contents.h"
namespace atom {
namespace {
const char kChromeUIDevToolsBundledHost[] = "devtools";
} // namespace
// static
AtomWebUIControllerFactory* AtomWebUIControllerFactory::GetInstance() {
return base::Singleton<AtomWebUIControllerFactory>::get();
}
AtomWebUIControllerFactory::AtomWebUIControllerFactory() {}
AtomWebUIControllerFactory::~AtomWebUIControllerFactory() {}
content::WebUI::TypeID AtomWebUIControllerFactory::GetWebUIType(
content::BrowserContext* browser_context,
const GURL& url) const {
#if BUILDFLAG(ENABLE_PDF_VIEWER)
if (url.host() == kPdfViewerUIHost) {
return const_cast<AtomWebUIControllerFactory*>(this);
}
#endif // BUILDFLAG(ENABLE_PDF_VIEWER)
if (url.host() == kChromeUIDevToolsBundledHost) {
return const_cast<AtomWebUIControllerFactory*>(this);
}
return content::WebUI::kNoWebUI;
}
bool AtomWebUIControllerFactory::UseWebUIForURL(
content::BrowserContext* browser_context,
const GURL& url) const {
return GetWebUIType(browser_context, url) != content::WebUI::kNoWebUI;
}
bool AtomWebUIControllerFactory::UseWebUIBindingsForURL(
content::BrowserContext* browser_context,
const GURL& url) const {
return UseWebUIForURL(browser_context, url);
}
std::unique_ptr<content::WebUIController>
AtomWebUIControllerFactory::CreateWebUIControllerForURL(content::WebUI* web_ui,
const GURL& url) const {
#if BUILDFLAG(ENABLE_PDF_VIEWER)
if (url.host() == kPdfViewerUIHost) {
base::StringPairs toplevel_params;
base::SplitStringIntoKeyValuePairs(url.query(), '=', '&', &toplevel_params);
std::string src;
const net::UnescapeRule::Type unescape_rules =
net::UnescapeRule::SPACES | net::UnescapeRule::PATH_SEPARATORS |
net::UnescapeRule::URL_SPECIAL_CHARS_EXCEPT_PATH_SEPARATORS |
net::UnescapeRule::REPLACE_PLUS_WITH_SPACE;
for (const auto& param : toplevel_params) {
if (param.first == kPdfPluginSrc) {
src = net::UnescapeURLComponent(param.second, unescape_rules);
}
}
if (url.has_ref()) {
src = src + '#' + url.ref();
}
auto browser_context = web_ui->GetWebContents()->GetBrowserContext();
return new PdfViewerUI(browser_context, web_ui, src);
}
#endif // BUILDFLAG(ENABLE_PDF_VIEWER)
if (url.host() == kChromeUIDevToolsBundledHost) {
auto* browser_context = web_ui->GetWebContents()->GetBrowserContext();
return std::make_unique<DevToolsUI>(browser_context, web_ui);
}
return std::unique_ptr<content::WebUIController>();
}
} // namespace atom