diff options
author | mpcomplete@chromium.org <mpcomplete@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-09-03 17:57:44 +0000 |
---|---|---|
committer | mpcomplete@chromium.org <mpcomplete@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-09-03 17:57:44 +0000 |
commit | c2e74fe834afaeed3bf8d21034bb995437518843 (patch) | |
tree | c9b3876a00bbbb78984b619246a4069ded4c1664 /chrome/browser/dom_ui | |
parent | 3f7116cf4b4e725cc43c7331fed6cd1a1b03bc96 (diff) | |
download | chromium_src-c2e74fe834afaeed3bf8d21034bb995437518843.zip chromium_src-c2e74fe834afaeed3bf8d21034bb995437518843.tar.gz chromium_src-c2e74fe834afaeed3bf8d21034bb995437518843.tar.bz2 |
Revert "Revert "Allow DOMUI pages to call window.open(), giving DOMUI privileges to the new""
Second attempt at r25250. The tests were broken in Release mode due to a race. Should be fixed.
BUG=17636
Review URL: http://codereview.chromium.org/172120
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@25329 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/dom_ui')
-rw-r--r-- | chrome/browser/dom_ui/dom_ui_factory.cc | 139 | ||||
-rw-r--r-- | chrome/browser/dom_ui/dom_ui_factory.h | 13 |
2 files changed, 73 insertions, 79 deletions
diff --git a/chrome/browser/dom_ui/dom_ui_factory.cc b/chrome/browser/dom_ui/dom_ui_factory.cc index f776baa..cdfbd7f 100644 --- a/chrome/browser/dom_ui/dom_ui_factory.cc +++ b/chrome/browser/dom_ui/dom_ui_factory.cc @@ -18,102 +18,83 @@ #include "chrome/common/url_constants.h" #include "googleurl/src/gurl.h" -// Backend for both querying for and creating new DOMUI objects. If you're just -// querying whether there's a DOM UI for the given URL, pass NULL for both the -// web contents and the new_ui. The return value will indiacate whether a DOM UI -// exists for the given URL. -// -// If you want to create a DOM UI, pass non-NULL pointers for both tab_contents -// and new_ui. The *new_ui pointer will be filled with the created UI if it -// succeeds (indicated by a return value of true). The caller owns the *new_ui -// pointer. -static bool CreateDOMUI(const GURL& url, TabContents* tab_contents, - DOMUI** new_ui) { +const DOMUITypeID DOMUIFactory::kNoDOMUI = NULL; + +// A function for creating a new DOMUI. The caller owns the return value, which +// may be NULL (for example, if the URL refers to an non-existent extension). +typedef DOMUI* (*DOMUIFactoryFunction)(TabContents* tab_contents, + const GURL& url); + +// Template for defining DOMUIFactoryFunction. +template<class T> +DOMUI* NewDOMUI(TabContents* contents, const GURL& url) { + return new T(contents); +} + +// Special case for extensions. +template<> +DOMUI* NewDOMUI<ExtensionDOMUI>(TabContents* contents, const GURL& url) { + // Don't use a DOMUI for non-existent extensions. + ExtensionsService* service = contents->profile()->GetExtensionsService(); + bool valid_extension = (service && service->GetExtensionById(url.host())); + if (valid_extension) + return new ExtensionDOMUI(contents); + return NULL; +} + +// Returns a function that can be used to create the right type of DOMUI for a +// tab, based on its URL. Returns NULL if the URL doesn't have DOMUI associated +// with it. Even if the factory function is valid, it may yield a NULL DOMUI +// when invoked for a particular tab - see NewDOMUI<ExtensionDOMUI>. +static DOMUIFactoryFunction GetDOMUIFactoryFunction(const GURL& url) { // Currently, any gears: URL means an HTML dialog. - if (url.SchemeIs(chrome::kGearsScheme)) { - if (new_ui) - *new_ui = new HtmlDialogUI(tab_contents); - return true; - } - - if (url.SchemeIs(chrome::kExtensionScheme)) { - // We assume we have a valid extension unless we have a TabContents which - // can prove us wrong. This can can happen if the user types in a URL - // manually. - bool valid_extension = true; - if (tab_contents) { - // TabContents can be NULL if we're doing a quick UseDOMUIForURL check. - ExtensionsService* service = - tab_contents->profile()->GetExtensionsService(); - valid_extension = (service && service->GetExtensionById(url.host())); - } - if (valid_extension) { - if (new_ui) - *new_ui = new ExtensionDOMUI(tab_contents); - return true; - } - return false; - } + if (url.SchemeIs(chrome::kGearsScheme)) + return &NewDOMUI<HtmlDialogUI>; + + if (url.SchemeIs(chrome::kExtensionScheme)) + return &NewDOMUI<ExtensionDOMUI>; // TODO(mhm) Make sure this ifdef is removed once print is complete. #if !defined(GOOGLE_CHROME_BUILD) - if (url.SchemeIs(chrome::kPrintScheme)) { - if (new_ui) - *new_ui = new PrintUI(tab_contents); - return true; - } + if (url.SchemeIs(chrome::kPrintScheme)) + return &NewDOMUI<PrintUI>; #endif // This will get called a lot to check all URLs, so do a quick check of other // schemes (gears was handled above) to filter out most URLs. if (!url.SchemeIs(chrome::kChromeInternalScheme) && !url.SchemeIs(chrome::kChromeUIScheme)) - return false; + return NULL; - if (url.host() == chrome::kSyncResourcesHost) { - if (new_ui) - *new_ui = new HtmlDialogUI(tab_contents); - return true; - } + if (url.host() == chrome::kSyncResourcesHost) + return &NewDOMUI<HtmlDialogUI>; // Special case the new tab page. In older versions of Chrome, the new tab // page was hosted at chrome-internal:<blah>. This might be in people's saved // sessions or bookmarks, so we say any URL with that scheme triggers the new // tab page. if (url.host() == chrome::kChromeUINewTabHost || - url.SchemeIs(chrome::kChromeInternalScheme)) { - if (new_ui) - *new_ui = new NewTabUI(tab_contents); - return true; - } + url.SchemeIs(chrome::kChromeInternalScheme)) + return &NewDOMUI<NewTabUI>; // We must compare hosts only since some of the DOM UIs append extra stuff // after the host name. - if (url.host() == chrome::kChromeUIHistoryHost) { - if (new_ui) - *new_ui = new HistoryUI(tab_contents); - return true; - } - - if (url.host() == chrome::kChromeUIDownloadsHost) { - if (new_ui) - *new_ui = new DownloadsUI(tab_contents); - return true; - } - - if (url.host() == chrome::kChromeUIExtensionsHost) { - if (new_ui) - *new_ui = new ExtensionsUI(tab_contents); - return true; - } - - if (url.host() == chrome::kChromeUIDevToolsHost) { - if (new_ui) - *new_ui = new DevToolsUI(tab_contents); - return true; - } + if (url.host() == chrome::kChromeUIHistoryHost) + return &NewDOMUI<HistoryUI>; + if (url.host() == chrome::kChromeUIDownloadsHost) + return &NewDOMUI<DownloadsUI>; + if (url.host() == chrome::kChromeUIExtensionsHost) + return &NewDOMUI<ExtensionsUI>; + if (url.host() == chrome::kChromeUIDevToolsHost) + return &NewDOMUI<DevToolsUI>; + + return NULL; +} - return false; +// static +DOMUITypeID DOMUIFactory::GetDOMUIType(const GURL& url) { + DOMUIFactoryFunction function = GetDOMUIFactoryFunction(url); + return function ? reinterpret_cast<DOMUITypeID>(function) : kNoDOMUI; } // static @@ -126,16 +107,16 @@ bool DOMUIFactory::HasDOMUIScheme(const GURL& url) { // static bool DOMUIFactory::UseDOMUIForURL(const GURL& url) { - return CreateDOMUI(url, NULL, NULL); + return GetDOMUIFactoryFunction(url) != NULL; } // static DOMUI* DOMUIFactory::CreateDOMUIForURL(TabContents* tab_contents, const GURL& url) { - DOMUI* dom_ui; - if (!CreateDOMUI(url, tab_contents, &dom_ui)) + DOMUIFactoryFunction function = GetDOMUIFactoryFunction(url); + if (!function) return NULL; - return dom_ui; + return (*function)(tab_contents, url); } // static diff --git a/chrome/browser/dom_ui/dom_ui_factory.h b/chrome/browser/dom_ui/dom_ui_factory.h index e4e36c3..db1500c 100644 --- a/chrome/browser/dom_ui/dom_ui_factory.h +++ b/chrome/browser/dom_ui/dom_ui_factory.h @@ -11,8 +11,21 @@ class DOMUI; class GURL; class TabContents; +// An opaque identifier used to identify a DOMUI. This can only be compared to +// kNoDOMUI or other DOMUI types. See GetDOMUIType. +typedef void* DOMUITypeID; + class DOMUIFactory { public: + // A special DOMUI type that signifies that a given page would not use the + // DOM UI system. + static const DOMUITypeID kNoDOMUI; + + // Returns a type identifier indicating what DOMUI we would use for the + // given URL. This is useful for comparing the potential DOMUIs for two URLs. + // Returns kNoDOMUI if the given URL will not use the DOM UI system. + static DOMUITypeID GetDOMUIType(const GURL& url); + // Returns true if the given URL's scheme would trigger the DOM UI system. // This is a less precise test than UseDONUIForURL, which tells you whether // that specific URL matches a known one. This one is faster and can be used |