[email protected] | f34e7963 | 2010-03-17 02:34:08 | [diff] [blame] | 1 | // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
[email protected] | bfdffe2b | 2009-04-24 22:05:35 | [diff] [blame] | 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 "chrome/browser/extensions/extension_function_dispatcher.h" |
| 6 | |
[email protected] | 745feedb | 2010-08-02 04:08:07 | [diff] [blame] | 7 | #include <map> |
| 8 | |
[email protected] | bfdffe2b | 2009-04-24 22:05:35 | [diff] [blame] | 9 | #include "base/process_util.h" |
| 10 | #include "base/singleton.h" |
[email protected] | 584b8e3f | 2010-04-10 00:23:37 | [diff] [blame] | 11 | #include "base/ref_counted.h" |
[email protected] | bfdffe2b | 2009-04-24 22:05:35 | [diff] [blame] | 12 | #include "base/values.h" |
[email protected] | 17d40f0 | 2010-07-01 01:18:06 | [diff] [blame] | 13 | #include "build/build_config.h" |
[email protected] | 0ec9203 | 2010-03-24 19:59:41 | [diff] [blame] | 14 | #include "chrome/browser/browser_list.h" |
[email protected] | a95631cb | 2009-12-10 01:59:11 | [diff] [blame] | 15 | #include "chrome/browser/browser_window.h" |
[email protected] | 584b8e3f | 2010-04-10 00:23:37 | [diff] [blame] | 16 | #include "chrome/browser/dom_ui/chrome_url_data_manager.h" |
| 17 | #include "chrome/browser/dom_ui/dom_ui_favicon_source.h" |
[email protected] | d070ec6 | 2010-07-27 21:28:26 | [diff] [blame] | 18 | #include "chrome/browser/external_protocol_handler.h" |
[email protected] | 912256b3 | 2009-09-18 09:47:35 | [diff] [blame] | 19 | #include "chrome/browser/extensions/execute_code_in_tab_function.h" |
[email protected] | 5cbe1e2 | 2010-01-30 01:18:56 | [diff] [blame] | 20 | #include "chrome/browser/extensions/extension_accessibility_api.h" |
[email protected] | 9dd97bc | 2010-01-14 01:40:04 | [diff] [blame] | 21 | #include "chrome/browser/extensions/extension_bookmark_manager_api.h" |
[email protected] | bfdffe2b | 2009-04-24 22:05:35 | [diff] [blame] | 22 | #include "chrome/browser/extensions/extension_bookmarks_module.h" |
[email protected] | ec9ac0df | 2009-10-01 18:06:47 | [diff] [blame] | 23 | #include "chrome/browser/extensions/extension_browser_actions_api.h" |
[email protected] | 44625595 | 2010-03-17 20:41:58 | [diff] [blame] | 24 | #include "chrome/browser/extensions/extension_clipboard_api.h" |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 25 | #include "chrome/browser/extensions/extension_context_menu_api.h" |
[email protected] | 898bbd3 | 2010-05-18 18:52:29 | [diff] [blame] | 26 | #include "chrome/browser/extensions/extension_cookies_api.h" |
[email protected] | b2725756 | 2009-11-16 23:28:26 | [diff] [blame] | 27 | #include "chrome/browser/extensions/extension_dom_ui.h" |
[email protected] | bfdffe2b | 2009-04-24 22:05:35 | [diff] [blame] | 28 | #include "chrome/browser/extensions/extension_function.h" |
[email protected] | de768a83 | 2009-10-30 05:25:01 | [diff] [blame] | 29 | #include "chrome/browser/extensions/extension_history_api.h" |
[email protected] | f520541 | 2010-03-16 00:19:34 | [diff] [blame] | 30 | #include "chrome/browser/extensions/extension_idle_api.h" |
[email protected] | 198bcfe | 2009-09-09 22:56:28 | [diff] [blame] | 31 | #include "chrome/browser/extensions/extension_i18n_api.h" |
[email protected] | f34e7963 | 2010-03-17 02:34:08 | [diff] [blame] | 32 | #include "chrome/browser/extensions/extension_infobar_module.h" |
[email protected] | d683385 | 2010-08-20 18:00:45 | [diff] [blame^] | 33 | #if defined(TOOLKIT_VIEWS) |
| 34 | #include "chrome/browser/extensions/extension_input_api.h" |
| 35 | #endif |
[email protected] | e916901c | 2009-05-07 00:14:31 | [diff] [blame] | 36 | #include "chrome/browser/extensions/extension_message_service.h" |
[email protected] | 438772df | 2010-02-26 18:08:43 | [diff] [blame] | 37 | #include "chrome/browser/extensions/extension_metrics_module.h" |
[email protected] | 56ad379 | 2010-05-28 17:45:33 | [diff] [blame] | 38 | #include "chrome/browser/extensions/extension_omnibox_api.h" |
[email protected] | f7f3a5f | 2009-05-01 22:02:34 | [diff] [blame] | 39 | #include "chrome/browser/extensions/extension_page_actions_module.h" |
[email protected] | 1c1c77a5 | 2009-11-03 00:37:31 | [diff] [blame] | 40 | #include "chrome/browser/extensions/extension_popup_api.h" |
[email protected] | 4577622 | 2009-07-15 20:21:58 | [diff] [blame] | 41 | #include "chrome/browser/extensions/extension_process_manager.h" |
[email protected] | 381162b | 2010-01-28 17:29:35 | [diff] [blame] | 42 | #include "chrome/browser/extensions/extension_processes_api.h" |
[email protected] | a9c23a5 | 2010-08-04 09:13:44 | [diff] [blame] | 43 | #include "chrome/browser/extensions/extension_proxy_api.h" |
[email protected] | 17d40f0 | 2010-07-01 01:18:06 | [diff] [blame] | 44 | #include "chrome/browser/extensions/extension_rlz_module.h" |
[email protected] | 8b8e7c9 | 2010-08-19 18:05:56 | [diff] [blame] | 45 | #include "chrome/browser/extensions/extension_sidebar_api.h" |
[email protected] | bfdffe2b | 2009-04-24 22:05:35 | [diff] [blame] | 46 | #include "chrome/browser/extensions/extension_tabs_module.h" |
[email protected] | 25fd1b2e | 2009-08-17 20:57:14 | [diff] [blame] | 47 | #include "chrome/browser/extensions/extension_test_api.h" |
[email protected] | 8abe0a3 | 2010-08-12 00:40:22 | [diff] [blame] | 48 | #if defined(OS_CHROMEOS) |
| 49 | #include "chrome/browser/extensions/extension_tts_api.h" |
| 50 | #endif |
[email protected] | d13950e | 2009-12-04 01:43:02 | [diff] [blame] | 51 | #include "chrome/browser/extensions/extensions_quota_service.h" |
[email protected] | b1748b1d8 | 2009-11-30 20:32:56 | [diff] [blame] | 52 | #include "chrome/browser/extensions/extensions_service.h" |
[email protected] | e916901c | 2009-05-07 00:14:31 | [diff] [blame] | 53 | #include "chrome/browser/profile.h" |
[email protected] | bfdffe2b | 2009-04-24 22:05:35 | [diff] [blame] | 54 | #include "chrome/browser/renderer_host/render_process_host.h" |
| 55 | #include "chrome/browser/renderer_host/render_view_host.h" |
[email protected] | 3550635 | 2009-08-07 18:58:19 | [diff] [blame] | 56 | #include "chrome/common/render_messages.h" |
[email protected] | bfdffe2b | 2009-04-24 22:05:35 | [diff] [blame] | 57 | #include "chrome/common/result_codes.h" |
[email protected] | 9c45b718 | 2009-08-04 16:44:43 | [diff] [blame] | 58 | #include "chrome/common/url_constants.h" |
[email protected] | bfdffe2b | 2009-04-24 22:05:35 | [diff] [blame] | 59 | |
[email protected] | d683385 | 2010-08-20 18:00:45 | [diff] [blame^] | 60 | |
[email protected] | bfdffe2b | 2009-04-24 22:05:35 | [diff] [blame] | 61 | // FactoryRegistry ------------------------------------------------------------- |
| 62 | |
| 63 | namespace { |
| 64 | |
[email protected] | b83e460 | 2009-05-15 22:58:33 | [diff] [blame] | 65 | // Template for defining ExtensionFunctionFactory. |
| 66 | template<class T> |
| 67 | ExtensionFunction* NewExtensionFunction() { |
| 68 | return new T(); |
| 69 | } |
[email protected] | bfdffe2b | 2009-04-24 22:05:35 | [diff] [blame] | 70 | |
[email protected] | b83e460 | 2009-05-15 22:58:33 | [diff] [blame] | 71 | // Contains a list of all known extension functions and allows clients to |
| 72 | // create instances of them. |
[email protected] | bfdffe2b | 2009-04-24 22:05:35 | [diff] [blame] | 73 | class FactoryRegistry { |
| 74 | public: |
| 75 | static FactoryRegistry* instance(); |
[email protected] | b83e460 | 2009-05-15 22:58:33 | [diff] [blame] | 76 | FactoryRegistry() { ResetFunctions(); } |
| 77 | |
| 78 | // Resets all functions to their default values. |
| 79 | void ResetFunctions(); |
| 80 | |
| 81 | // Adds all function names to 'names'. |
[email protected] | bfdffe2b | 2009-04-24 22:05:35 | [diff] [blame] | 82 | void GetAllNames(std::vector<std::string>* names); |
[email protected] | b83e460 | 2009-05-15 22:58:33 | [diff] [blame] | 83 | |
| 84 | // Allows overriding of specific functions (e.g. for testing). Functions |
| 85 | // must be previously registered. Returns true if successful. |
| 86 | bool OverrideFunction(const std::string& name, |
| 87 | ExtensionFunctionFactory factory); |
| 88 | |
| 89 | // Factory method for the ExtensionFunction registered as 'name'. |
[email protected] | bfdffe2b | 2009-04-24 22:05:35 | [diff] [blame] | 90 | ExtensionFunction* NewFunction(const std::string& name); |
| 91 | |
| 92 | private: |
[email protected] | 61424c06 | 2009-10-14 23:14:59 | [diff] [blame] | 93 | template<class T> |
| 94 | void RegisterFunction() { |
| 95 | factories_[T::function_name()] = &NewExtensionFunction<T>; |
| 96 | } |
| 97 | |
[email protected] | bfdffe2b | 2009-04-24 22:05:35 | [diff] [blame] | 98 | typedef std::map<std::string, ExtensionFunctionFactory> FactoryMap; |
| 99 | FactoryMap factories_; |
| 100 | }; |
| 101 | |
[email protected] | bfdffe2b | 2009-04-24 22:05:35 | [diff] [blame] | 102 | FactoryRegistry* FactoryRegistry::instance() { |
| 103 | return Singleton<FactoryRegistry>::get(); |
| 104 | } |
| 105 | |
[email protected] | b83e460 | 2009-05-15 22:58:33 | [diff] [blame] | 106 | void FactoryRegistry::ResetFunctions() { |
[email protected] | bfdffe2b | 2009-04-24 22:05:35 | [diff] [blame] | 107 | // Register all functions here. |
| 108 | |
[email protected] | e515f5d | 2009-05-05 03:05:00 | [diff] [blame] | 109 | // Windows |
[email protected] | 61424c06 | 2009-10-14 23:14:59 | [diff] [blame] | 110 | RegisterFunction<GetWindowFunction>(); |
| 111 | RegisterFunction<GetCurrentWindowFunction>(); |
| 112 | RegisterFunction<GetLastFocusedWindowFunction>(); |
| 113 | RegisterFunction<GetAllWindowsFunction>(); |
| 114 | RegisterFunction<CreateWindowFunction>(); |
| 115 | RegisterFunction<UpdateWindowFunction>(); |
| 116 | RegisterFunction<RemoveWindowFunction>(); |
[email protected] | b83e460 | 2009-05-15 22:58:33 | [diff] [blame] | 117 | |
[email protected] | e515f5d | 2009-05-05 03:05:00 | [diff] [blame] | 118 | // Tabs |
[email protected] | 61424c06 | 2009-10-14 23:14:59 | [diff] [blame] | 119 | RegisterFunction<GetTabFunction>(); |
[email protected] | e3eafb29 | 2010-04-14 21:30:41 | [diff] [blame] | 120 | RegisterFunction<GetCurrentTabFunction>(); |
[email protected] | 61424c06 | 2009-10-14 23:14:59 | [diff] [blame] | 121 | RegisterFunction<GetSelectedTabFunction>(); |
| 122 | RegisterFunction<GetAllTabsInWindowFunction>(); |
| 123 | RegisterFunction<CreateTabFunction>(); |
| 124 | RegisterFunction<UpdateTabFunction>(); |
| 125 | RegisterFunction<MoveTabFunction>(); |
| 126 | RegisterFunction<RemoveTabFunction>(); |
| 127 | RegisterFunction<DetectTabLanguageFunction>(); |
| 128 | RegisterFunction<CaptureVisibleTabFunction>(); |
| 129 | RegisterFunction<TabsExecuteScriptFunction>(); |
| 130 | RegisterFunction<TabsInsertCSSFunction>(); |
[email protected] | bfdffe2b | 2009-04-24 22:05:35 | [diff] [blame] | 131 | |
[email protected] | f7f3a5f | 2009-05-01 22:02:34 | [diff] [blame] | 132 | // Page Actions. |
[email protected] | 61424c06 | 2009-10-14 23:14:59 | [diff] [blame] | 133 | RegisterFunction<EnablePageActionFunction>(); |
| 134 | RegisterFunction<DisablePageActionFunction>(); |
[email protected] | 744ef17 | 2009-10-16 21:53:46 | [diff] [blame] | 135 | RegisterFunction<PageActionShowFunction>(); |
| 136 | RegisterFunction<PageActionHideFunction>(); |
| 137 | RegisterFunction<PageActionSetIconFunction>(); |
| 138 | RegisterFunction<PageActionSetTitleFunction>(); |
[email protected] | e478d670 | 2010-01-28 00:10:29 | [diff] [blame] | 139 | RegisterFunction<PageActionSetPopupFunction>(); |
[email protected] | f7f3a5f | 2009-05-01 22:02:34 | [diff] [blame] | 140 | |
[email protected] | ec9ac0df | 2009-10-01 18:06:47 | [diff] [blame] | 141 | // Browser Actions. |
[email protected] | 61424c06 | 2009-10-14 23:14:59 | [diff] [blame] | 142 | RegisterFunction<BrowserActionSetIconFunction>(); |
[email protected] | 1288ba0 | 2009-10-15 00:02:24 | [diff] [blame] | 143 | RegisterFunction<BrowserActionSetTitleFunction>(); |
[email protected] | 61424c06 | 2009-10-14 23:14:59 | [diff] [blame] | 144 | RegisterFunction<BrowserActionSetBadgeTextFunction>(); |
| 145 | RegisterFunction<BrowserActionSetBadgeBackgroundColorFunction>(); |
[email protected] | 85ae959 | 2010-02-03 20:58:50 | [diff] [blame] | 146 | RegisterFunction<BrowserActionSetPopupFunction>(); |
[email protected] | ec9ac0df | 2009-10-01 18:06:47 | [diff] [blame] | 147 | |
[email protected] | f7f3a5f | 2009-05-01 22:02:34 | [diff] [blame] | 148 | // Bookmarks. |
[email protected] | 61424c06 | 2009-10-14 23:14:59 | [diff] [blame] | 149 | RegisterFunction<GetBookmarksFunction>(); |
| 150 | RegisterFunction<GetBookmarkChildrenFunction>(); |
[email protected] | a3c94c71 | 2009-12-18 19:23:55 | [diff] [blame] | 151 | RegisterFunction<GetBookmarkRecentFunction>(); |
[email protected] | 61424c06 | 2009-10-14 23:14:59 | [diff] [blame] | 152 | RegisterFunction<GetBookmarkTreeFunction>(); |
| 153 | RegisterFunction<SearchBookmarksFunction>(); |
| 154 | RegisterFunction<RemoveBookmarkFunction>(); |
| 155 | RegisterFunction<RemoveTreeBookmarkFunction>(); |
| 156 | RegisterFunction<CreateBookmarkFunction>(); |
| 157 | RegisterFunction<MoveBookmarkFunction>(); |
| 158 | RegisterFunction<UpdateBookmarkFunction>(); |
[email protected] | 9c45b718 | 2009-08-04 16:44:43 | [diff] [blame] | 159 | |
[email protected] | f34e7963 | 2010-03-17 02:34:08 | [diff] [blame] | 160 | // Infobars. |
| 161 | RegisterFunction<ShowInfoBarFunction>(); |
| 162 | |
[email protected] | 9dd97bc | 2010-01-14 01:40:04 | [diff] [blame] | 163 | // BookmarkManager |
| 164 | RegisterFunction<CopyBookmarkManagerFunction>(); |
| 165 | RegisterFunction<CutBookmarkManagerFunction>(); |
| 166 | RegisterFunction<PasteBookmarkManagerFunction>(); |
[email protected] | 03b3bbf | 2010-01-29 23:54:57 | [diff] [blame] | 167 | RegisterFunction<CanPasteBookmarkManagerFunction>(); |
[email protected] | cb6cf79 | 2010-01-28 00:04:56 | [diff] [blame] | 168 | RegisterFunction<ImportBookmarksFunction>(); |
| 169 | RegisterFunction<ExportBookmarksFunction>(); |
[email protected] | d406e2e | 2010-01-30 21:45:18 | [diff] [blame] | 170 | RegisterFunction<SortChildrenBookmarkManagerFunction>(); |
[email protected] | 9dd97bc | 2010-01-14 01:40:04 | [diff] [blame] | 171 | RegisterFunction<BookmarkManagerGetStringsFunction>(); |
[email protected] | ced90ae1 | 2010-02-20 02:06:16 | [diff] [blame] | 172 | RegisterFunction<StartDragBookmarkManagerFunction>(); |
| 173 | RegisterFunction<DropBookmarkManagerFunction>(); |
[email protected] | 9b07185 | 2010-04-02 06:45:31 | [diff] [blame] | 174 | RegisterFunction<GetSubtreeBookmarkManagerFunction>(); |
[email protected] | 9dd97bc | 2010-01-14 01:40:04 | [diff] [blame] | 175 | |
[email protected] | de768a83 | 2009-10-30 05:25:01 | [diff] [blame] | 176 | // History |
| 177 | RegisterFunction<AddUrlHistoryFunction>(); |
| 178 | RegisterFunction<DeleteAllHistoryFunction>(); |
| 179 | RegisterFunction<DeleteRangeHistoryFunction>(); |
| 180 | RegisterFunction<DeleteUrlHistoryFunction>(); |
| 181 | RegisterFunction<GetVisitsHistoryFunction>(); |
| 182 | RegisterFunction<SearchHistoryFunction>(); |
| 183 | |
[email protected] | f520541 | 2010-03-16 00:19:34 | [diff] [blame] | 184 | // Idle |
| 185 | RegisterFunction<ExtensionIdleQueryStateFunction>(); |
| 186 | |
[email protected] | 198bcfe | 2009-09-09 22:56:28 | [diff] [blame] | 187 | // I18N. |
[email protected] | 61424c06 | 2009-10-14 23:14:59 | [diff] [blame] | 188 | RegisterFunction<GetAcceptLanguagesFunction>(); |
[email protected] | 198bcfe | 2009-09-09 22:56:28 | [diff] [blame] | 189 | |
[email protected] | 1c1c77a5 | 2009-11-03 00:37:31 | [diff] [blame] | 190 | // Popup API. |
| 191 | RegisterFunction<PopupShowFunction>(); |
| 192 | |
[email protected] | 381162b | 2010-01-28 17:29:35 | [diff] [blame] | 193 | // Processes. |
| 194 | RegisterFunction<GetProcessForTabFunction>(); |
| 195 | |
[email protected] | 438772df | 2010-02-26 18:08:43 | [diff] [blame] | 196 | // Metrics. |
[email protected] | cf25e4d | 2010-03-12 21:19:34 | [diff] [blame] | 197 | RegisterFunction<MetricsRecordUserActionFunction>(); |
| 198 | RegisterFunction<MetricsRecordValueFunction>(); |
| 199 | RegisterFunction<MetricsRecordPercentageFunction>(); |
| 200 | RegisterFunction<MetricsRecordCountFunction>(); |
| 201 | RegisterFunction<MetricsRecordSmallCountFunction>(); |
| 202 | RegisterFunction<MetricsRecordMediumCountFunction>(); |
| 203 | RegisterFunction<MetricsRecordTimeFunction>(); |
| 204 | RegisterFunction<MetricsRecordMediumTimeFunction>(); |
| 205 | RegisterFunction<MetricsRecordLongTimeFunction>(); |
[email protected] | 438772df | 2010-02-26 18:08:43 | [diff] [blame] | 206 | |
[email protected] | 17d40f0 | 2010-07-01 01:18:06 | [diff] [blame] | 207 | // RLZ. |
| 208 | #if defined(OS_WIN) |
| 209 | RegisterFunction<RlzRecordProductEventFunction>(); |
| 210 | RegisterFunction<RlzGetAccessPointRlzFunction>(); |
[email protected] | 3a612be | 2010-07-17 03:11:21 | [diff] [blame] | 211 | RegisterFunction<RlzSendFinancialPingFunction>(); |
[email protected] | 17d40f0 | 2010-07-01 01:18:06 | [diff] [blame] | 212 | RegisterFunction<RlzClearProductStateFunction>(); |
| 213 | #endif |
| 214 | |
[email protected] | 898bbd3 | 2010-05-18 18:52:29 | [diff] [blame] | 215 | // Cookies. |
| 216 | RegisterFunction<GetCookieFunction>(); |
| 217 | RegisterFunction<GetAllCookiesFunction>(); |
| 218 | RegisterFunction<SetCookieFunction>(); |
| 219 | RegisterFunction<RemoveCookieFunction>(); |
| 220 | RegisterFunction<GetAllCookieStoresFunction>(); |
| 221 | |
[email protected] | 25fd1b2e | 2009-08-17 20:57:14 | [diff] [blame] | 222 | // Test. |
[email protected] | 61424c06 | 2009-10-14 23:14:59 | [diff] [blame] | 223 | RegisterFunction<ExtensionTestPassFunction>(); |
| 224 | RegisterFunction<ExtensionTestFailFunction>(); |
| 225 | RegisterFunction<ExtensionTestLogFunction>(); |
[email protected] | d13950e | 2009-12-04 01:43:02 | [diff] [blame] | 226 | RegisterFunction<ExtensionTestQuotaResetFunction>(); |
[email protected] | db7331a | 2010-02-25 22:10:50 | [diff] [blame] | 227 | RegisterFunction<ExtensionTestCreateIncognitoTabFunction>(); |
[email protected] | 745feedb | 2010-08-02 04:08:07 | [diff] [blame] | 228 | RegisterFunction<ExtensionTestSendMessageFunction>(); |
[email protected] | 5cbe1e2 | 2010-01-30 01:18:56 | [diff] [blame] | 229 | |
| 230 | // Accessibility. |
| 231 | RegisterFunction<GetFocusedControlFunction>(); |
| 232 | RegisterFunction<SetAccessibilityEnabledFunction>(); |
[email protected] | 44625595 | 2010-03-17 20:41:58 | [diff] [blame] | 233 | |
[email protected] | 8abe0a3 | 2010-08-12 00:40:22 | [diff] [blame] | 234 | #if defined(OS_CHROMEOS) |
| 235 | // Text-to-speech. |
| 236 | RegisterFunction<ExtensionTtsSpeakFunction>(); |
| 237 | RegisterFunction<ExtensionTtsStopSpeakingFunction>(); |
| 238 | RegisterFunction<ExtensionTtsIsSpeakingFunction>(); |
| 239 | #endif |
| 240 | |
[email protected] | 44625595 | 2010-03-17 20:41:58 | [diff] [blame] | 241 | // Clipboard. |
| 242 | RegisterFunction<ExecuteCopyClipboardFunction>(); |
| 243 | RegisterFunction<ExecuteCutClipboardFunction>(); |
| 244 | RegisterFunction<ExecutePasteClipboardFunction>(); |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 245 | |
| 246 | // Context Menus. |
| 247 | RegisterFunction<CreateContextMenuFunction>(); |
[email protected] | 66dbfb2c | 2010-05-12 20:20:15 | [diff] [blame] | 248 | RegisterFunction<UpdateContextMenuFunction>(); |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 249 | RegisterFunction<RemoveContextMenuFunction>(); |
[email protected] | 66dbfb2c | 2010-05-12 20:20:15 | [diff] [blame] | 250 | RegisterFunction<RemoveAllContextMenusFunction>(); |
[email protected] | 56ad379 | 2010-05-28 17:45:33 | [diff] [blame] | 251 | |
| 252 | // Omnibox. |
| 253 | RegisterFunction<OmniboxSendSuggestionsFunction>(); |
[email protected] | a9c23a5 | 2010-08-04 09:13:44 | [diff] [blame] | 254 | |
| 255 | // Proxies. |
| 256 | RegisterFunction<UseCustomProxySettingsFunction>(); |
[email protected] | 8b8e7c9 | 2010-08-19 18:05:56 | [diff] [blame] | 257 | |
| 258 | // Sidebar. |
| 259 | RegisterFunction<CollapseSidebarFunction>(); |
| 260 | RegisterFunction<ExpandSidebarFunction>(); |
| 261 | RegisterFunction<GetStateSidebarFunction>(); |
| 262 | RegisterFunction<HideSidebarFunction>(); |
| 263 | RegisterFunction<NavigateSidebarFunction>(); |
| 264 | RegisterFunction<SetBadgeTextSidebarFunction>(); |
| 265 | RegisterFunction<SetIconSidebarFunction>(); |
| 266 | RegisterFunction<SetTitleSidebarFunction>(); |
| 267 | RegisterFunction<ShowSidebarFunction>(); |
[email protected] | d683385 | 2010-08-20 18:00:45 | [diff] [blame^] | 268 | |
| 269 | #if defined(TOOLKIT_VIEWS) |
| 270 | // Input. |
| 271 | RegisterFunction<SendKeyboardEventInputFunction>(); |
| 272 | #endif |
[email protected] | bfdffe2b | 2009-04-24 22:05:35 | [diff] [blame] | 273 | } |
| 274 | |
[email protected] | b83e460 | 2009-05-15 22:58:33 | [diff] [blame] | 275 | void FactoryRegistry::GetAllNames(std::vector<std::string>* names) { |
| 276 | for (FactoryMap::iterator iter = factories_.begin(); |
| 277 | iter != factories_.end(); ++iter) { |
[email protected] | bfdffe2b | 2009-04-24 22:05:35 | [diff] [blame] | 278 | names->push_back(iter->first); |
| 279 | } |
| 280 | } |
| 281 | |
[email protected] | b83e460 | 2009-05-15 22:58:33 | [diff] [blame] | 282 | bool FactoryRegistry::OverrideFunction(const std::string& name, |
| 283 | ExtensionFunctionFactory factory) { |
| 284 | FactoryMap::iterator iter = factories_.find(name); |
| 285 | if (iter == factories_.end()) { |
| 286 | return false; |
| 287 | } else { |
| 288 | iter->second = factory; |
| 289 | return true; |
| 290 | } |
| 291 | } |
| 292 | |
[email protected] | bfdffe2b | 2009-04-24 22:05:35 | [diff] [blame] | 293 | ExtensionFunction* FactoryRegistry::NewFunction(const std::string& name) { |
| 294 | FactoryMap::iterator iter = factories_.find(name); |
| 295 | DCHECK(iter != factories_.end()); |
[email protected] | b83e460 | 2009-05-15 22:58:33 | [diff] [blame] | 296 | ExtensionFunction* function = iter->second(); |
[email protected] | 76a3db85 | 2009-07-24 02:14:56 | [diff] [blame] | 297 | function->set_name(name); |
[email protected] | b83e460 | 2009-05-15 22:58:33 | [diff] [blame] | 298 | return function; |
[email protected] | bfdffe2b | 2009-04-24 22:05:35 | [diff] [blame] | 299 | } |
| 300 | |
[email protected] | b83e460 | 2009-05-15 22:58:33 | [diff] [blame] | 301 | }; // namespace |
[email protected] | bfdffe2b | 2009-04-24 22:05:35 | [diff] [blame] | 302 | |
[email protected] | bfdffe2b | 2009-04-24 22:05:35 | [diff] [blame] | 303 | // ExtensionFunctionDispatcher ------------------------------------------------- |
| 304 | |
| 305 | void ExtensionFunctionDispatcher::GetAllFunctionNames( |
| 306 | std::vector<std::string>* names) { |
| 307 | FactoryRegistry::instance()->GetAllNames(names); |
| 308 | } |
| 309 | |
[email protected] | b83e460 | 2009-05-15 22:58:33 | [diff] [blame] | 310 | bool ExtensionFunctionDispatcher::OverrideFunction( |
| 311 | const std::string& name, ExtensionFunctionFactory factory) { |
| 312 | return FactoryRegistry::instance()->OverrideFunction(name, factory); |
| 313 | } |
| 314 | |
| 315 | void ExtensionFunctionDispatcher::ResetFunctions() { |
| 316 | FactoryRegistry::instance()->ResetFunctions(); |
| 317 | } |
| 318 | |
[email protected] | a91afcb | 2010-03-25 21:15:02 | [diff] [blame] | 319 | ExtensionFunctionDispatcher* ExtensionFunctionDispatcher::Create( |
| 320 | RenderViewHost* render_view_host, |
| 321 | Delegate* delegate, |
| 322 | const GURL& url) { |
| 323 | ExtensionsService* service = |
| 324 | render_view_host->process()->profile()->GetExtensionsService(); |
| 325 | DCHECK(service); |
| 326 | |
| 327 | Extension* extension = service->GetExtensionByURL(url); |
| 328 | if (extension) |
| 329 | return new ExtensionFunctionDispatcher(render_view_host, delegate, |
| 330 | extension, url); |
| 331 | else |
| 332 | return NULL; |
| 333 | } |
| 334 | |
[email protected] | bfdffe2b | 2009-04-24 22:05:35 | [diff] [blame] | 335 | ExtensionFunctionDispatcher::ExtensionFunctionDispatcher( |
| 336 | RenderViewHost* render_view_host, |
[email protected] | 7eecaed5 | 2009-05-07 21:44:12 | [diff] [blame] | 337 | Delegate* delegate, |
[email protected] | a91afcb | 2010-03-25 21:15:02 | [diff] [blame] | 338 | Extension* extension, |
[email protected] | 811bfe3 | 2009-07-01 08:46:25 | [diff] [blame] | 339 | const GURL& url) |
[email protected] | ebc1b68 | 2010-03-06 00:22:30 | [diff] [blame] | 340 | : profile_(render_view_host->process()->profile()), |
[email protected] | 68f0791 | 2010-03-05 18:33:58 | [diff] [blame] | 341 | render_view_host_(render_view_host), |
[email protected] | 7eecaed5 | 2009-05-07 21:44:12 | [diff] [blame] | 342 | delegate_(delegate), |
[email protected] | 811bfe3 | 2009-07-01 08:46:25 | [diff] [blame] | 343 | url_(url), |
[email protected] | 32dda36 | 2009-06-05 19:07:01 | [diff] [blame] | 344 | ALLOW_THIS_IN_INITIALIZER_LIST(peer_(new Peer(this))) { |
[email protected] | 9c45b718 | 2009-08-04 16:44:43 | [diff] [blame] | 345 | // TODO(erikkay) should we do something for these errors in Release? |
| 346 | DCHECK(url.SchemeIs(chrome::kExtensionScheme)); |
[email protected] | 3550635 | 2009-08-07 18:58:19 | [diff] [blame] | 347 | DCHECK(extension); |
[email protected] | 9c45b718 | 2009-08-04 16:44:43 | [diff] [blame] | 348 | |
[email protected] | 4577622 | 2009-07-15 20:21:58 | [diff] [blame] | 349 | // Notify the ExtensionProcessManager that the view was created. |
| 350 | ExtensionProcessManager* epm = profile()->GetExtensionProcessManager(); |
| 351 | epm->RegisterExtensionProcess(extension_id(), |
[email protected] | 76543b9 | 2009-08-31 17:27:45 | [diff] [blame] | 352 | render_view_host->process()->id()); |
[email protected] | 3550635 | 2009-08-07 18:58:19 | [diff] [blame] | 353 | |
[email protected] | db7331a | 2010-02-25 22:10:50 | [diff] [blame] | 354 | bool incognito_enabled = |
[email protected] | cb0ce1e02 | 2010-03-10 19:54:41 | [diff] [blame] | 355 | profile()->GetExtensionsService()->IsIncognitoEnabled(extension); |
[email protected] | db7331a | 2010-02-25 22:10:50 | [diff] [blame] | 356 | |
[email protected] | 584b8e3f | 2010-04-10 00:23:37 | [diff] [blame] | 357 | // If the extension has permission to load chrome://favicon/ resources we need |
| 358 | // to make sure that the DOMUIFavIconSource is registered with the |
| 359 | // ChromeURLDataManager. |
| 360 | if (extension->HasHostPermission(GURL(chrome::kChromeUIFavIconURL))) { |
| 361 | DOMUIFavIconSource* favicon_source = new DOMUIFavIconSource(profile_); |
| 362 | ChromeThread::PostTask( |
| 363 | ChromeThread::IO, FROM_HERE, |
| 364 | NewRunnableMethod(Singleton<ChromeURLDataManager>::get(), |
| 365 | &ChromeURLDataManager::AddDataSource, |
| 366 | make_scoped_refptr(favicon_source))); |
| 367 | } |
| 368 | |
[email protected] | 3550635 | 2009-08-07 18:58:19 | [diff] [blame] | 369 | // Update the extension permissions. Doing this each time we create an EFD |
| 370 | // ensures that new processes are informed of permissions for newly installed |
| 371 | // extensions. |
[email protected] | cccf9093 | 2009-08-23 17:56:25 | [diff] [blame] | 372 | render_view_host->Send(new ViewMsg_Extension_SetAPIPermissions( |
[email protected] | 3550635 | 2009-08-07 18:58:19 | [diff] [blame] | 373 | extension->id(), extension->api_permissions())); |
[email protected] | cccf9093 | 2009-08-23 17:56:25 | [diff] [blame] | 374 | render_view_host->Send(new ViewMsg_Extension_SetHostPermissions( |
| 375 | extension->url(), extension->host_permissions())); |
[email protected] | db7331a | 2010-02-25 22:10:50 | [diff] [blame] | 376 | render_view_host->Send(new ViewMsg_Extension_ExtensionSetIncognitoEnabled( |
| 377 | extension->id(), incognito_enabled)); |
[email protected] | 68f0791 | 2010-03-05 18:33:58 | [diff] [blame] | 378 | |
| 379 | NotificationService::current()->Notify( |
| 380 | NotificationType::EXTENSION_FUNCTION_DISPATCHER_CREATED, |
| 381 | Source<Profile>(profile_), |
| 382 | Details<ExtensionFunctionDispatcher>(this)); |
[email protected] | bfdffe2b | 2009-04-24 22:05:35 | [diff] [blame] | 383 | } |
| 384 | |
[email protected] | 32dda36 | 2009-06-05 19:07:01 | [diff] [blame] | 385 | ExtensionFunctionDispatcher::~ExtensionFunctionDispatcher() { |
[email protected] | 32dda36 | 2009-06-05 19:07:01 | [diff] [blame] | 386 | peer_->dispatcher_ = NULL; |
[email protected] | 68f0791 | 2010-03-05 18:33:58 | [diff] [blame] | 387 | |
| 388 | NotificationService::current()->Notify( |
| 389 | NotificationType::EXTENSION_FUNCTION_DISPATCHER_DESTROYED, |
| 390 | Source<Profile>(profile_), |
| 391 | Details<ExtensionFunctionDispatcher>(this)); |
[email protected] | 32dda36 | 2009-06-05 19:07:01 | [diff] [blame] | 392 | } |
| 393 | |
[email protected] | 0ec9203 | 2010-03-24 19:59:41 | [diff] [blame] | 394 | Browser* ExtensionFunctionDispatcher::GetCurrentBrowser( |
| 395 | bool include_incognito) { |
| 396 | Browser* browser = delegate_->GetBrowser(); |
[email protected] | 7eecaed5 | 2009-05-07 21:44:12 | [diff] [blame] | 397 | |
[email protected] | 0ec9203 | 2010-03-24 19:59:41 | [diff] [blame] | 398 | // If the delegate has an associated browser and that browser is in the right |
| 399 | // incognito state, we can return it. |
| 400 | if (browser) { |
| 401 | if (include_incognito || !browser->profile()->IsOffTheRecord()) |
| 402 | return browser; |
| 403 | } |
[email protected] | 9c45b718 | 2009-08-04 16:44:43 | [diff] [blame] | 404 | |
[email protected] | 0ec9203 | 2010-03-24 19:59:41 | [diff] [blame] | 405 | // Otherwise, try to default to a reasonable browser. |
| 406 | Profile* profile = render_view_host()->process()->profile(); |
| 407 | |
| 408 | // Make sure we don't return an incognito browser without proper access. |
| 409 | if (!include_incognito) |
| 410 | profile = profile->GetOriginalProfile(); |
| 411 | |
[email protected] | aa916d57 | 2010-08-13 02:59:31 | [diff] [blame] | 412 | browser = BrowserList::FindBrowserWithType(profile, Browser::TYPE_NORMAL, |
[email protected] | 62b0b53 | 2010-03-26 22:44:31 | [diff] [blame] | 413 | include_incognito); |
[email protected] | 0ec9203 | 2010-03-24 19:59:41 | [diff] [blame] | 414 | |
[email protected] | 0ec9203 | 2010-03-24 19:59:41 | [diff] [blame] | 415 | // NOTE(rafaelw): This can return NULL in some circumstances. In particular, |
[email protected] | 6d7a604 | 2010-08-12 20:12:42 | [diff] [blame] | 416 | // a background_page onload chrome.tabs api call can make it into here |
| 417 | // before the browser is sufficiently initialized to return here. |
[email protected] | 0ec9203 | 2010-03-24 19:59:41 | [diff] [blame] | 418 | // A similar situation may arise during shutdown. |
| 419 | // TODO(rafaelw): Delay creation of background_page until the browser |
| 420 | // is available. http://code.google.com/p/chromium/issues/detail?id=13284 |
| 421 | return browser; |
[email protected] | b2725756 | 2009-11-16 23:28:26 | [diff] [blame] | 422 | } |
| 423 | |
[email protected] | 8b8e7c9 | 2010-08-19 18:05:56 | [diff] [blame] | 424 | void ExtensionFunctionDispatcher::HandleRequest( |
| 425 | const ViewHostMsg_DomMessage_Params& params) { |
[email protected] | 32dda36 | 2009-06-05 19:07:01 | [diff] [blame] | 426 | scoped_refptr<ExtensionFunction> function( |
[email protected] | 8b8e7c9 | 2010-08-19 18:05:56 | [diff] [blame] | 427 | FactoryRegistry::instance()->NewFunction(params.name)); |
[email protected] | 32dda36 | 2009-06-05 19:07:01 | [diff] [blame] | 428 | function->set_dispatcher_peer(peer_); |
[email protected] | a7664e1 | 2010-04-08 20:37:43 | [diff] [blame] | 429 | function->set_profile(profile_); |
| 430 | function->set_extension_id(extension_id()); |
[email protected] | 8b8e7c9 | 2010-08-19 18:05:56 | [diff] [blame] | 431 | function->SetArgs(¶ms.arguments); |
| 432 | function->set_source_url(params.source_url); |
| 433 | function->set_request_id(params.request_id); |
| 434 | function->set_has_callback(params.has_callback); |
| 435 | function->set_user_gesture(params.user_gesture); |
[email protected] | d13950e | 2009-12-04 01:43:02 | [diff] [blame] | 436 | ExtensionsService* service = profile()->GetExtensionsService(); |
| 437 | DCHECK(service); |
[email protected] | cb0ce1e02 | 2010-03-10 19:54:41 | [diff] [blame] | 438 | Extension* extension = service->GetExtensionById(extension_id(), false); |
| 439 | DCHECK(extension); |
| 440 | function->set_include_incognito(service->IsIncognitoEnabled(extension)); |
| 441 | |
[email protected] | d13950e | 2009-12-04 01:43:02 | [diff] [blame] | 442 | ExtensionsQuotaService* quota = service->quota_service(); |
[email protected] | 8b8e7c9 | 2010-08-19 18:05:56 | [diff] [blame] | 443 | if (quota->Assess(extension_id(), function, ¶ms.arguments, |
| 444 | base::TimeTicks::Now())) { |
[email protected] | d070ec6 | 2010-07-27 21:28:26 | [diff] [blame] | 445 | // See crbug.com/39178. |
| 446 | ExternalProtocolHandler::PermitLaunchUrl(); |
| 447 | |
[email protected] | d13950e | 2009-12-04 01:43:02 | [diff] [blame] | 448 | function->Run(); |
| 449 | } else { |
| 450 | render_view_host_->SendExtensionResponse(function->request_id(), false, |
| 451 | std::string(), QuotaLimitHeuristic::kGenericOverQuotaError); |
| 452 | } |
[email protected] | bfdffe2b | 2009-04-24 22:05:35 | [diff] [blame] | 453 | } |
| 454 | |
[email protected] | c661918 | 2009-05-12 14:59:32 | [diff] [blame] | 455 | void ExtensionFunctionDispatcher::SendResponse(ExtensionFunction* function, |
| 456 | bool success) { |
[email protected] | c661918 | 2009-05-12 14:59:32 | [diff] [blame] | 457 | render_view_host_->SendExtensionResponse(function->request_id(), success, |
[email protected] | b83e460 | 2009-05-15 22:58:33 | [diff] [blame] | 458 | function->GetResult(), function->GetError()); |
[email protected] | bfdffe2b | 2009-04-24 22:05:35 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | void ExtensionFunctionDispatcher::HandleBadMessage(ExtensionFunction* api) { |
[email protected] | 25fd1b2e | 2009-08-17 20:57:14 | [diff] [blame] | 462 | LOG(ERROR) << "bad extension message " << |
[email protected] | 76543b9 | 2009-08-31 17:27:45 | [diff] [blame] | 463 | api->name() << |
[email protected] | bfdffe2b | 2009-04-24 22:05:35 | [diff] [blame] | 464 | " : terminating renderer."; |
| 465 | if (RenderProcessHost::run_renderer_in_process()) { |
| 466 | // In single process mode it is better if we don't suicide but just crash. |
| 467 | CHECK(false); |
| 468 | } else { |
| 469 | NOTREACHED(); |
[email protected] | 201b273 | 2009-11-13 18:57:46 | [diff] [blame] | 470 | base::KillProcess(render_view_host_->process()->GetHandle(), |
[email protected] | bfdffe2b | 2009-04-24 22:05:35 | [diff] [blame] | 471 | ResultCodes::KILLED_BAD_MESSAGE, false); |
| 472 | } |
| 473 | } |
| 474 | |
| 475 | Profile* ExtensionFunctionDispatcher::profile() { |
[email protected] | 68f0791 | 2010-03-05 18:33:58 | [diff] [blame] | 476 | return profile_; |
[email protected] | bfdffe2b | 2009-04-24 22:05:35 | [diff] [blame] | 477 | } |