atom_web_ui_controller_factory.cc 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // Copyright (c) 2017 GitHub, Inc.
  2. // Use of this source code is governed by the MIT license that can be
  3. // found in the LICENSE file.
  4. #include "atom/browser/atom_web_ui_controller_factory.h"
  5. #include <string>
  6. #if defined(ENABLE_PDF_VIEWER)
  7. #include "atom/browser/ui/webui/pdf_viewer_ui.h"
  8. #include "atom/common/atom_constants.h"
  9. #include "base/strings/string_split.h"
  10. #include "base/strings/string_util.h"
  11. #include "content/public/browser/web_contents.h"
  12. #include "net/base/escape.h"
  13. #endif // defined(ENABLE_PDF_VIEWER)
  14. namespace atom {
  15. // static
  16. AtomWebUIControllerFactory* AtomWebUIControllerFactory::GetInstance() {
  17. return base::Singleton<AtomWebUIControllerFactory>::get();
  18. }
  19. AtomWebUIControllerFactory::AtomWebUIControllerFactory() {}
  20. AtomWebUIControllerFactory::~AtomWebUIControllerFactory() {}
  21. content::WebUI::TypeID AtomWebUIControllerFactory::GetWebUIType(
  22. content::BrowserContext* browser_context,
  23. const GURL& url) const {
  24. #if defined(ENABLE_PDF_VIEWER)
  25. if (url.host() == kPdfViewerUIHost) {
  26. return const_cast<AtomWebUIControllerFactory*>(this);
  27. }
  28. #endif // defined(ENABLE_PDF_VIEWER)
  29. return content::WebUI::kNoWebUI;
  30. }
  31. bool AtomWebUIControllerFactory::UseWebUIForURL(
  32. content::BrowserContext* browser_context,
  33. const GURL& url) const {
  34. return GetWebUIType(browser_context, url) != content::WebUI::kNoWebUI;
  35. }
  36. bool AtomWebUIControllerFactory::UseWebUIBindingsForURL(
  37. content::BrowserContext* browser_context,
  38. const GURL& url) const {
  39. return UseWebUIForURL(browser_context, url);
  40. }
  41. content::WebUIController*
  42. AtomWebUIControllerFactory::CreateWebUIControllerForURL(content::WebUI* web_ui,
  43. const GURL& url) const {
  44. #if defined(ENABLE_PDF_VIEWER)
  45. if (url.host() == kPdfViewerUIHost) {
  46. base::StringPairs toplevel_params;
  47. base::SplitStringIntoKeyValuePairs(url.query(), '=', '&', &toplevel_params);
  48. std::string src;
  49. const net::UnescapeRule::Type unescape_rules =
  50. net::UnescapeRule::SPACES | net::UnescapeRule::PATH_SEPARATORS |
  51. net::UnescapeRule::URL_SPECIAL_CHARS_EXCEPT_PATH_SEPARATORS |
  52. net::UnescapeRule::REPLACE_PLUS_WITH_SPACE;
  53. for (const auto& param : toplevel_params) {
  54. if (param.first == kPdfPluginSrc) {
  55. src = net::UnescapeURLComponent(param.second, unescape_rules);
  56. }
  57. }
  58. if (url.has_ref()) {
  59. src = src + '#' + url.ref();
  60. }
  61. auto browser_context = web_ui->GetWebContents()->GetBrowserContext();
  62. return new PdfViewerUI(browser_context, web_ui, src);
  63. }
  64. #endif // defined(ENABLE_PDF_VIEWER)
  65. return nullptr;
  66. }
  67. } // namespace atom