2017-01-17 14:23:13 +00:00
|
|
|
// 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"
|
|
|
|
|
2017-01-18 17:53:54 +00:00
|
|
|
#include <string>
|
|
|
|
|
2017-01-21 14:57:29 +00:00
|
|
|
#include "atom/browser/ui/webui/pdf_viewer_ui.h"
|
2017-02-03 09:36:53 +00:00
|
|
|
#include "atom/common/atom_constants.h"
|
2017-01-17 17:10:15 +00:00
|
|
|
#include "base/strings/string_split.h"
|
2017-01-18 17:53:54 +00:00
|
|
|
#include "base/strings/string_util.h"
|
2017-01-17 14:23:13 +00:00
|
|
|
#include "content/public/browser/web_contents.h"
|
2017-07-14 04:09:14 +00:00
|
|
|
#include "net/base/escape.h"
|
2017-01-17 14:23:13 +00:00
|
|
|
|
|
|
|
namespace atom {
|
|
|
|
|
|
|
|
// 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 {
|
2017-02-03 09:36:53 +00:00
|
|
|
if (url.host() == kPdfViewerUIHost) {
|
2017-01-17 14:23:13 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
content::WebUIController*
|
|
|
|
AtomWebUIControllerFactory::CreateWebUIControllerForURL(content::WebUI* web_ui,
|
|
|
|
const GURL& url) const {
|
2017-02-03 09:36:53 +00:00
|
|
|
if (url.host() == kPdfViewerUIHost) {
|
2017-01-17 17:10:15 +00:00
|
|
|
base::StringPairs toplevel_params;
|
|
|
|
base::SplitStringIntoKeyValuePairs(url.query(), '=', '&', &toplevel_params);
|
2018-02-14 08:21:46 +00:00
|
|
|
std::string src;
|
2017-07-14 04:09:14 +00:00
|
|
|
|
|
|
|
const net::UnescapeRule::Type unescape_rules =
|
|
|
|
net::UnescapeRule::SPACES | net::UnescapeRule::PATH_SEPARATORS |
|
|
|
|
net::UnescapeRule::URL_SPECIAL_CHARS_EXCEPT_PATH_SEPARATORS |
|
2017-07-15 02:32:46 +00:00
|
|
|
net::UnescapeRule::REPLACE_PLUS_WITH_SPACE;
|
2017-07-14 04:09:14 +00:00
|
|
|
|
2017-01-17 17:10:15 +00:00
|
|
|
for (const auto& param : toplevel_params) {
|
2017-02-27 06:10:49 +00:00
|
|
|
if (param.first == kPdfPluginSrc) {
|
2017-07-14 04:09:14 +00:00
|
|
|
src = net::UnescapeURLComponent(param.second, unescape_rules);
|
2017-01-17 17:10:15 +00:00
|
|
|
}
|
|
|
|
}
|
2017-06-08 00:12:52 +00:00
|
|
|
if (url.has_ref()) {
|
|
|
|
src = src + '#' + url.ref();
|
|
|
|
}
|
2017-01-17 14:23:13 +00:00
|
|
|
auto browser_context = web_ui->GetWebContents()->GetBrowserContext();
|
2017-02-27 06:10:49 +00:00
|
|
|
return new PdfViewerUI(browser_context, web_ui, src);
|
2017-01-17 14:23:13 +00:00
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace atom
|