1 // Copyright 2019 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 
5 #include "weblayer/browser/webui/web_ui_controller_factory.h"
6 
7 #include "base/memory/ptr_util.h"
8 #include "content/public/browser/web_ui.h"
9 #include "url/gurl.h"
10 #include "weblayer/browser/webui/weblayer_internals_ui.h"
11 
12 namespace weblayer {
13 namespace {
14 
15 const content::WebUI::TypeID kWebLayerID = &kWebLayerID;
16 
17 // A function for creating a new WebUI. The caller owns the return value, which
18 // may be nullptr (for example, if the URL refers to an non-existent extension).
19 typedef content::WebUIController* (
20     *WebUIFactoryFunctionPointer)(content::WebUI* web_ui, const GURL& url);
21 
22 // Template for defining WebUIFactoryFunctionPointer.
23 template <class T>
NewWebUI(content::WebUI * web_ui,const GURL & url)24 content::WebUIController* NewWebUI(content::WebUI* web_ui, const GURL& url) {
25   return new T(web_ui);
26 }
27 
GetWebUIFactoryFunctionPointer(const GURL & url)28 WebUIFactoryFunctionPointer GetWebUIFactoryFunctionPointer(const GURL& url) {
29   if (url.host() == kChromeUIWebLayerHost) {
30     return &NewWebUI<WebLayerInternalsUI>;
31   }
32 
33   return nullptr;
34 }
35 
GetWebUITypeID(const GURL & url)36 content::WebUI::TypeID GetWebUITypeID(const GURL& url) {
37   if (url.host() == kChromeUIWebLayerHost) {
38     return kWebLayerID;
39   }
40 
41   return content::WebUI::kNoWebUI;
42 }
43 
44 }  // namespace
45 
46 // static
GetInstance()47 WebUIControllerFactory* WebUIControllerFactory::GetInstance() {
48   static base::NoDestructor<WebUIControllerFactory> instance;
49   return instance.get();
50 }
51 
WebUIControllerFactory()52 WebUIControllerFactory::WebUIControllerFactory() {}
53 
~WebUIControllerFactory()54 WebUIControllerFactory::~WebUIControllerFactory() {}
55 
GetWebUIType(content::BrowserContext * browser_context,const GURL & url)56 content::WebUI::TypeID WebUIControllerFactory::GetWebUIType(
57     content::BrowserContext* browser_context,
58     const GURL& url) {
59   return GetWebUITypeID(url);
60 }
61 
UseWebUIForURL(content::BrowserContext * browser_context,const GURL & url)62 bool WebUIControllerFactory::UseWebUIForURL(
63     content::BrowserContext* browser_context,
64     const GURL& url) {
65   return GetWebUIType(browser_context, url) != content::WebUI::kNoWebUI;
66 }
67 
UseWebUIBindingsForURL(content::BrowserContext * browser_context,const GURL & url)68 bool WebUIControllerFactory::UseWebUIBindingsForURL(
69     content::BrowserContext* browser_context,
70     const GURL& url) {
71   return UseWebUIForURL(browser_context, url);
72 }
73 
74 std::unique_ptr<content::WebUIController>
CreateWebUIControllerForURL(content::WebUI * web_ui,const GURL & url)75 WebUIControllerFactory::CreateWebUIControllerForURL(content::WebUI* web_ui,
76                                                     const GURL& url) {
77   WebUIFactoryFunctionPointer function = GetWebUIFactoryFunctionPointer(url);
78   if (!function)
79     return nullptr;
80 
81   return base::WrapUnique((*function)(web_ui, url));
82 }
83 
84 }  // namespace weblayer
85