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