// Copyright (c) 2010 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #include "chrome/browser/dom_ui/dom_ui_factory.h" #include "chrome/browser/chrome_thread.h" #include "chrome/browser/dom_ui/bookmarks_ui.h" #include "chrome/browser/dom_ui/downloads_ui.h" #include "chrome/browser/dom_ui/devtools_ui.h" #include "chrome/browser/dom_ui/filebrowse_ui.h" #include "chrome/browser/dom_ui/history_ui.h" #include "chrome/browser/dom_ui/html_dialog_ui.h" #include "chrome/browser/dom_ui/mediaplayer_ui.h" #include "chrome/browser/dom_ui/net_internals_ui.h" #include "chrome/browser/dom_ui/new_tab_ui.h" #include "chrome/browser/dom_ui/plugins_ui.h" #include "chrome/browser/dom_ui/print_ui.h" #include "chrome/browser/extensions/extension_dom_ui.h" #include "chrome/browser/extensions/extensions_service.h" #include "chrome/browser/extensions/extensions_ui.h" #include "chrome/browser/profile.h" #include "chrome/browser/tab_contents/tab_contents.h" #include "chrome/common/url_constants.h" #include "googleurl/src/gurl.h" 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 DOMUI* NewDOMUI(TabContents* contents, const GURL& url) { return new T(contents); } // Special case for extensions. template<> DOMUI* NewDOMUI(TabContents* contents, const GURL& url) { // Don't use a DOMUI for non-existent extensions or for incognito tabs. The // latter restriction is because we require extensions to run within a single // process. ExtensionsService* service = contents->profile()->GetExtensionsService(); bool valid_extension = (service && service->GetExtensionById(url.host(), false)); if (valid_extension && !contents->profile()->IsOffTheRecord()) 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. static DOMUIFactoryFunction GetDOMUIFactoryFunction(const GURL& url) { // Currently, any gears: URL means an HTML dialog. if (url.SchemeIs(chrome::kGearsScheme)) return &NewDOMUI; if (url.SchemeIs(chrome::kExtensionScheme)) return &NewDOMUI; // TODO(mhm) Make sure this ifdef is removed once print is complete. #if !defined(GOOGLE_CHROME_BUILD) if (url.SchemeIs(chrome::kPrintScheme)) return &NewDOMUI; #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 NULL; if (url.host() == chrome::kSyncResourcesHost) return &NewDOMUI; // Special case the new tab page. In older versions of Chrome, the new tab // page was hosted at chrome-internal:. 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)) return &NewDOMUI; // We must compare hosts only since some of the DOM UIs append extra stuff // after the host name. if (url.host() == chrome::kChromeUIBookmarksHost) return &NewDOMUI; if (url.host() == chrome::kChromeUIDevToolsHost) return &NewDOMUI; if (url.host() == chrome::kChromeUIDownloadsHost) return &NewDOMUI; if (url.host() == chrome::kChromeUIExtensionsHost) return &NewDOMUI; if (url.host() == chrome::kChromeUIHistoryHost) return &NewDOMUI; if (url.host() == chrome::kChromeUINetInternalsHost) return &NewDOMUI; if (url.host() == chrome::kChromeUIPluginsHost) return &NewDOMUI; #if defined(OS_CHROMEOS) if (url.host() == chrome::kChromeUIFileBrowseHost) return &NewDOMUI; if (url.host() == chrome::kChromeUIMediaplayerHost) return &NewDOMUI; #endif return NULL; } // static DOMUITypeID DOMUIFactory::GetDOMUIType(const GURL& url) { DOMUIFactoryFunction function = GetDOMUIFactoryFunction(url); return function ? reinterpret_cast(function) : kNoDOMUI; } // static bool DOMUIFactory::HasDOMUIScheme(const GURL& url) { return url.SchemeIs(chrome::kChromeInternalScheme) || url.SchemeIs(chrome::kChromeUIScheme) || url.SchemeIs(chrome::kExtensionScheme) || url.SchemeIs(chrome::kPrintScheme); } // static bool DOMUIFactory::UseDOMUIForURL(const GURL& url) { return GetDOMUIFactoryFunction(url) != NULL; } // static DOMUI* DOMUIFactory::CreateDOMUIForURL(TabContents* tab_contents, const GURL& url) { DOMUIFactoryFunction function = GetDOMUIFactoryFunction(url); if (!function) return NULL; return (*function)(tab_contents, url); } // static RefCountedMemory* DOMUIFactory::GetFaviconResourceBytes(Profile* profile, const GURL& page_url) { if (page_url.SchemeIs(chrome::kExtensionScheme)) return ExtensionDOMUI::GetFaviconResourceBytes(profile, page_url); if (!HasDOMUIScheme(page_url)) return NULL; if (page_url.host() == chrome::kChromeUIBookmarksHost) return BookmarksUI::GetFaviconResourceBytes(); if (page_url.host() == chrome::kChromeUIDownloadsHost) return DownloadsUI::GetFaviconResourceBytes(); if (page_url.host() == chrome::kChromeUIExtensionsHost) return ExtensionsUI::GetFaviconResourceBytes(); if (page_url.host() == chrome::kChromeUIHistoryHost) return HistoryUI::GetFaviconResourceBytes(); if (page_url.host() == chrome::kChromeUIPluginsHost) return PluginsUI::GetFaviconResourceBytes(); return NULL; }