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