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