untrusted_web_ui_controller_factory.cc 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. // Copyright 2020 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.
  4. #include "ui/webui/untrusted_web_ui_controller_factory.h"
  5. #include "content/public/browser/web_contents.h"
  6. #include "content/public/browser/web_ui.h"
  7. #include "content/public/browser/web_ui_controller.h"
  8. #include "content/public/browser/webui_config.h"
  9. #include "content/public/common/url_constants.h"
  10. #include "url/gurl.h"
  11. namespace ui {
  12. UntrustedWebUIControllerFactory::UntrustedWebUIControllerFactory() = default;
  13. UntrustedWebUIControllerFactory::~UntrustedWebUIControllerFactory() = default;
  14. content::WebUI::TypeID UntrustedWebUIControllerFactory::GetWebUIType(
  15. content::BrowserContext* browser_context,
  16. const GURL& url) {
  17. auto* config = GetConfigIfWebUIEnabled(browser_context, url);
  18. if (!config)
  19. return content::WebUI::kNoWebUI;
  20. return reinterpret_cast<content::WebUI::TypeID>(config);
  21. }
  22. bool UntrustedWebUIControllerFactory::UseWebUIForURL(
  23. content::BrowserContext* browser_context,
  24. const GURL& url) {
  25. return GetConfigIfWebUIEnabled(browser_context, url);
  26. }
  27. std::unique_ptr<content::WebUIController>
  28. UntrustedWebUIControllerFactory::CreateWebUIControllerForURL(
  29. content::WebUI* web_ui,
  30. const GURL& url) {
  31. auto* browser_context = web_ui->GetWebContents()->GetBrowserContext();
  32. auto* config = GetConfigIfWebUIEnabled(browser_context, url);
  33. if (!config)
  34. return nullptr;
  35. return config->CreateWebUIController(web_ui);
  36. }
  37. content::WebUIConfig* UntrustedWebUIControllerFactory::GetConfigIfWebUIEnabled(
  38. content::BrowserContext* browser_context,
  39. const GURL& url) {
  40. // This factory doesn't support non chrome-untrusted:// WebUIs.
  41. if (!url.SchemeIs(content::kChromeUIUntrustedScheme))
  42. return nullptr;
  43. auto it = GetWebUIConfigMap().find(url.host_piece());
  44. if (it == GetWebUIConfigMap().end())
  45. return nullptr;
  46. if (!it->second->IsWebUIEnabled(browser_context))
  47. return nullptr;
  48. return it->second.get();
  49. }
  50. } // namespace ui