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