| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/dom_ui/dom_ui_factory.h" | 5 #include "chrome/browser/dom_ui/dom_ui_factory.h" |
| 6 | 6 |
| 7 #include "chrome/browser/dom_ui/downloads_ui.h" | 7 #include "chrome/browser/dom_ui/downloads_ui.h" |
| 8 #include "chrome/browser/dom_ui/devtools_ui.h" | 8 #include "chrome/browser/dom_ui/devtools_ui.h" |
| 9 #include "chrome/browser/dom_ui/history_ui.h" | 9 #include "chrome/browser/dom_ui/history_ui.h" |
| 10 #include "chrome/browser/dom_ui/html_dialog_ui.h" | 10 #include "chrome/browser/dom_ui/html_dialog_ui.h" |
| 11 #include "chrome/browser/dom_ui/new_tab_ui.h" | 11 #include "chrome/browser/dom_ui/new_tab_ui.h" |
| 12 #include "chrome/browser/dom_ui/print_ui.h" | 12 #include "chrome/browser/dom_ui/print_ui.h" |
| 13 #include "chrome/browser/extensions/extension_dom_ui.h" | 13 #include "chrome/browser/extensions/extension_dom_ui.h" |
| 14 #include "chrome/browser/extensions/extensions_service.h" | 14 #include "chrome/browser/extensions/extensions_service.h" |
| 15 #include "chrome/browser/extensions/extensions_ui.h" | 15 #include "chrome/browser/extensions/extensions_ui.h" |
| 16 #include "chrome/browser/profile.h" | 16 #include "chrome/browser/profile.h" |
| 17 #include "chrome/browser/tab_contents/tab_contents.h" | 17 #include "chrome/browser/tab_contents/tab_contents.h" |
| 18 #include "chrome/common/url_constants.h" | 18 #include "chrome/common/url_constants.h" |
| 19 #include "googleurl/src/gurl.h" | 19 #include "googleurl/src/gurl.h" |
| 20 | 20 |
| 21 // Backend for both querying for and creating new DOMUI objects. If you're just | 21 const DOMUITypeID DOMUIFactory::kNoDOMUI = NULL; |
| 22 // querying whether there's a DOM UI for the given URL, pass NULL for both the | 22 |
| 23 // web contents and the new_ui. The return value will indiacate whether a DOM UI | 23 // A function for creating a new DOMUI. The caller owns the return value, which |
| 24 // exists for the given URL. | 24 // may be NULL (for example, if the URL refers to an non-existent extension). |
| 25 // | 25 typedef DOMUI* (*DOMUIFactoryFunction)(TabContents* tab_contents, |
| 26 // If you want to create a DOM UI, pass non-NULL pointers for both tab_contents | 26 const GURL& url); |
| 27 // and new_ui. The *new_ui pointer will be filled with the created UI if it | 27 |
| 28 // succeeds (indicated by a return value of true). The caller owns the *new_ui | 28 // Template for defining DOMUIFactoryFunction. |
| 29 // pointer. | 29 template<class T> |
| 30 static bool CreateDOMUI(const GURL& url, TabContents* tab_contents, | 30 DOMUI* NewDOMUI(TabContents* contents, const GURL& url) { |
| 31 DOMUI** new_ui) { | 31 return new T(contents); |
| 32 } |
| 33 |
| 34 // Special case for extensions. |
| 35 template<> |
| 36 DOMUI* NewDOMUI<ExtensionDOMUI>(TabContents* contents, const GURL& url) { |
| 37 // Don't use a DOMUI for non-existent extensions. |
| 38 ExtensionsService* service = contents->profile()->GetExtensionsService(); |
| 39 bool valid_extension = (service && service->GetExtensionById(url.host())); |
| 40 if (valid_extension) |
| 41 return new ExtensionDOMUI(contents); |
| 42 return NULL; |
| 43 } |
| 44 |
| 45 // Returns a function that can be used to create the right type of DOMUI for a |
| 46 // tab, based on its URL. Returns NULL if the URL doesn't have DOMUI associated |
| 47 // with it. Even if the factory function is valid, it may yield a NULL DOMUI |
| 48 // when invoked for a particular tab - see NewDOMUI<ExtensionDOMUI>. |
| 49 static DOMUIFactoryFunction GetDOMUIFactoryFunction(const GURL& url) { |
| 32 // Currently, any gears: URL means an HTML dialog. | 50 // Currently, any gears: URL means an HTML dialog. |
| 33 if (url.SchemeIs(chrome::kGearsScheme)) { | 51 if (url.SchemeIs(chrome::kGearsScheme)) |
| 34 if (new_ui) | 52 return &NewDOMUI<HtmlDialogUI>; |
| 35 *new_ui = new HtmlDialogUI(tab_contents); | |
| 36 return true; | |
| 37 } | |
| 38 | 53 |
| 39 if (url.SchemeIs(chrome::kExtensionScheme)) { | 54 if (url.SchemeIs(chrome::kExtensionScheme)) |
| 40 // We assume we have a valid extension unless we have a TabContents which | 55 return &NewDOMUI<ExtensionDOMUI>; |
| 41 // can prove us wrong. This can can happen if the user types in a URL | |
| 42 // manually. | |
| 43 bool valid_extension = true; | |
| 44 if (tab_contents) { | |
| 45 // TabContents can be NULL if we're doing a quick UseDOMUIForURL check. | |
| 46 ExtensionsService* service = | |
| 47 tab_contents->profile()->GetExtensionsService(); | |
| 48 valid_extension = (service && service->GetExtensionById(url.host())); | |
| 49 } | |
| 50 if (valid_extension) { | |
| 51 if (new_ui) | |
| 52 *new_ui = new ExtensionDOMUI(tab_contents); | |
| 53 return true; | |
| 54 } | |
| 55 return false; | |
| 56 } | |
| 57 | 56 |
| 58 // TODO(mhm) Make sure this ifdef is removed once print is complete. | 57 // TODO(mhm) Make sure this ifdef is removed once print is complete. |
| 59 #if !defined(GOOGLE_CHROME_BUILD) | 58 #if !defined(GOOGLE_CHROME_BUILD) |
| 60 if (url.SchemeIs(chrome::kPrintScheme)) { | 59 if (url.SchemeIs(chrome::kPrintScheme)) |
| 61 if (new_ui) | 60 return &NewDOMUI<PrintUI>; |
| 62 *new_ui = new PrintUI(tab_contents); | |
| 63 return true; | |
| 64 } | |
| 65 #endif | 61 #endif |
| 66 | 62 |
| 67 // This will get called a lot to check all URLs, so do a quick check of other | 63 // This will get called a lot to check all URLs, so do a quick check of other |
| 68 // schemes (gears was handled above) to filter out most URLs. | 64 // schemes (gears was handled above) to filter out most URLs. |
| 69 if (!url.SchemeIs(chrome::kChromeInternalScheme) && | 65 if (!url.SchemeIs(chrome::kChromeInternalScheme) && |
| 70 !url.SchemeIs(chrome::kChromeUIScheme)) | 66 !url.SchemeIs(chrome::kChromeUIScheme)) |
| 71 return false; | 67 return NULL; |
| 72 | 68 |
| 73 if (url.host() == chrome::kSyncResourcesHost) { | 69 if (url.host() == chrome::kSyncResourcesHost) |
| 74 if (new_ui) | 70 return &NewDOMUI<HtmlDialogUI>; |
| 75 *new_ui = new HtmlDialogUI(tab_contents); | |
| 76 return true; | |
| 77 } | |
| 78 | 71 |
| 79 // Special case the new tab page. In older versions of Chrome, the new tab | 72 // Special case the new tab page. In older versions of Chrome, the new tab |
| 80 // page was hosted at chrome-internal:<blah>. This might be in people's saved | 73 // page was hosted at chrome-internal:<blah>. This might be in people's saved |
| 81 // sessions or bookmarks, so we say any URL with that scheme triggers the new | 74 // sessions or bookmarks, so we say any URL with that scheme triggers the new |
| 82 // tab page. | 75 // tab page. |
| 83 if (url.host() == chrome::kChromeUINewTabHost || | 76 if (url.host() == chrome::kChromeUINewTabHost || |
| 84 url.SchemeIs(chrome::kChromeInternalScheme)) { | 77 url.SchemeIs(chrome::kChromeInternalScheme)) |
| 85 if (new_ui) | 78 return &NewDOMUI<NewTabUI>; |
| 86 *new_ui = new NewTabUI(tab_contents); | |
| 87 return true; | |
| 88 } | |
| 89 | 79 |
| 90 // We must compare hosts only since some of the DOM UIs append extra stuff | 80 // We must compare hosts only since some of the DOM UIs append extra stuff |
| 91 // after the host name. | 81 // after the host name. |
| 92 if (url.host() == chrome::kChromeUIHistoryHost) { | 82 if (url.host() == chrome::kChromeUIHistoryHost) |
| 93 if (new_ui) | 83 return &NewDOMUI<HistoryUI>; |
| 94 *new_ui = new HistoryUI(tab_contents); | 84 if (url.host() == chrome::kChromeUIDownloadsHost) |
| 95 return true; | 85 return &NewDOMUI<DownloadsUI>; |
| 96 } | 86 if (url.host() == chrome::kChromeUIExtensionsHost) |
| 87 return &NewDOMUI<ExtensionsUI>; |
| 88 if (url.host() == chrome::kChromeUIDevToolsHost) |
| 89 return &NewDOMUI<DevToolsUI>; |
| 97 | 90 |
| 98 if (url.host() == chrome::kChromeUIDownloadsHost) { | 91 return NULL; |
| 99 if (new_ui) | |
| 100 *new_ui = new DownloadsUI(tab_contents); | |
| 101 return true; | |
| 102 } | |
| 103 | |
| 104 if (url.host() == chrome::kChromeUIExtensionsHost) { | |
| 105 if (new_ui) | |
| 106 *new_ui = new ExtensionsUI(tab_contents); | |
| 107 return true; | |
| 108 } | |
| 109 | |
| 110 if (url.host() == chrome::kChromeUIDevToolsHost) { | |
| 111 if (new_ui) | |
| 112 *new_ui = new DevToolsUI(tab_contents); | |
| 113 return true; | |
| 114 } | |
| 115 | |
| 116 return false; | |
| 117 } | 92 } |
| 118 | 93 |
| 119 // static | 94 // static |
| 95 DOMUITypeID DOMUIFactory::GetDOMUIType(const GURL& url) { |
| 96 DOMUIFactoryFunction function = GetDOMUIFactoryFunction(url); |
| 97 return function ? reinterpret_cast<DOMUITypeID>(function) : kNoDOMUI; |
| 98 } |
| 99 |
| 100 // static |
| 120 bool DOMUIFactory::HasDOMUIScheme(const GURL& url) { | 101 bool DOMUIFactory::HasDOMUIScheme(const GURL& url) { |
| 121 return url.SchemeIs(chrome::kChromeInternalScheme) || | 102 return url.SchemeIs(chrome::kChromeInternalScheme) || |
| 122 url.SchemeIs(chrome::kChromeUIScheme) || | 103 url.SchemeIs(chrome::kChromeUIScheme) || |
| 123 url.SchemeIs(chrome::kExtensionScheme) || | 104 url.SchemeIs(chrome::kExtensionScheme) || |
| 124 url.SchemeIs(chrome::kPrintScheme); | 105 url.SchemeIs(chrome::kPrintScheme); |
| 125 } | 106 } |
| 126 | 107 |
| 127 // static | 108 // static |
| 128 bool DOMUIFactory::UseDOMUIForURL(const GURL& url) { | 109 bool DOMUIFactory::UseDOMUIForURL(const GURL& url) { |
| 129 return CreateDOMUI(url, NULL, NULL); | 110 return GetDOMUIFactoryFunction(url) != NULL; |
| 130 } | 111 } |
| 131 | 112 |
| 132 // static | 113 // static |
| 133 DOMUI* DOMUIFactory::CreateDOMUIForURL(TabContents* tab_contents, | 114 DOMUI* DOMUIFactory::CreateDOMUIForURL(TabContents* tab_contents, |
| 134 const GURL& url) { | 115 const GURL& url) { |
| 135 DOMUI* dom_ui; | 116 DOMUIFactoryFunction function = GetDOMUIFactoryFunction(url); |
| 136 if (!CreateDOMUI(url, tab_contents, &dom_ui)) | 117 if (!function) |
| 137 return NULL; | 118 return NULL; |
| 138 return dom_ui; | 119 return (*function)(tab_contents, url); |
| 139 } | 120 } |
| 140 | 121 |
| 141 // static | 122 // static |
| 142 bool DOMUIFactory::GetFaviconResourceBytes(const GURL& page_url, | 123 bool DOMUIFactory::GetFaviconResourceBytes(const GURL& page_url, |
| 143 std::vector<unsigned char>* bytes) { | 124 std::vector<unsigned char>* bytes) { |
| 144 if (!HasDOMUIScheme(page_url)) | 125 if (!HasDOMUIScheme(page_url)) |
| 145 return false; | 126 return false; |
| 146 | 127 |
| 147 if (page_url.host() == chrome::kChromeUIHistoryHost) | 128 if (page_url.host() == chrome::kChromeUIHistoryHost) |
| 148 return HistoryUI::GetFaviconResourceBytes(bytes); | 129 return HistoryUI::GetFaviconResourceBytes(bytes); |
| 149 | 130 |
| 150 if (page_url.host() == chrome::kChromeUIDownloadsHost) | 131 if (page_url.host() == chrome::kChromeUIDownloadsHost) |
| 151 return DownloadsUI::GetFaviconResourceBytes(bytes); | 132 return DownloadsUI::GetFaviconResourceBytes(bytes); |
| 152 | 133 |
| 153 return false; | 134 return false; |
| 154 } | 135 } |
| OLD | NEW |