aw_web_ui_controller_factory.cc 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // Copyright 2017 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 "android_webview/browser/aw_web_ui_controller_factory.h"
  5. #include "base/memory/ptr_util.h"
  6. #include "components/safe_browsing/content/browser/web_ui/safe_browsing_ui.h"
  7. #include "components/safe_browsing/core/common/web_ui_constants.h"
  8. #include "content/public/browser/web_ui.h"
  9. #include "url/gurl.h"
  10. using content::WebUI;
  11. using content::WebUIController;
  12. namespace {
  13. const WebUI::TypeID kSafeBrowsingID = &kSafeBrowsingID;
  14. // A function for creating a new WebUI. The caller owns the return value, which
  15. // may be nullptr (for example, if the URL refers to an non-existent extension).
  16. typedef WebUIController* (*WebUIFactoryFunctionPointer)(WebUI* web_ui,
  17. const GURL& url);
  18. // Template for defining WebUIFactoryFunctionPointer.
  19. template <class T>
  20. WebUIController* NewWebUI(WebUI* web_ui, const GURL& url) {
  21. return new T(web_ui);
  22. }
  23. WebUIFactoryFunctionPointer GetWebUIFactoryFunctionPointer(const GURL& url) {
  24. // WebUI pages here must remain in the base module instead of being moved to
  25. // the Developer UI Dynamic Feature Module (DevUI DFM). Therefore the hosts
  26. // here must not appear in IsWebUiHostInDevUiDfm().
  27. if (url.host() == safe_browsing::kChromeUISafeBrowsingHost) {
  28. return &NewWebUI<safe_browsing::SafeBrowsingUI>;
  29. }
  30. return nullptr;
  31. }
  32. WebUI::TypeID GetWebUITypeID(const GURL& url) {
  33. if (url.host() == safe_browsing::kChromeUISafeBrowsingHost) {
  34. return kSafeBrowsingID;
  35. }
  36. return WebUI::kNoWebUI;
  37. }
  38. } // namespace
  39. namespace android_webview {
  40. // static
  41. AwWebUIControllerFactory* AwWebUIControllerFactory::GetInstance() {
  42. return base::Singleton<AwWebUIControllerFactory>::get();
  43. }
  44. AwWebUIControllerFactory::AwWebUIControllerFactory() {}
  45. AwWebUIControllerFactory::~AwWebUIControllerFactory() {}
  46. WebUI::TypeID AwWebUIControllerFactory::GetWebUIType(
  47. content::BrowserContext* browser_context,
  48. const GURL& url) {
  49. return GetWebUITypeID(url);
  50. }
  51. bool AwWebUIControllerFactory::UseWebUIForURL(
  52. content::BrowserContext* browser_context,
  53. const GURL& url) {
  54. return GetWebUIType(browser_context, url) != WebUI::kNoWebUI;
  55. }
  56. std::unique_ptr<WebUIController>
  57. AwWebUIControllerFactory::CreateWebUIControllerForURL(WebUI* web_ui,
  58. const GURL& url) {
  59. WebUIFactoryFunctionPointer function = GetWebUIFactoryFunctionPointer(url);
  60. if (!function)
  61. return nullptr;
  62. return base::WrapUnique((*function)(web_ui, url));
  63. }
  64. } // namespace android_webview