[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] | 912256b3 | 2009-09-18 09:47:35 | [diff] [blame] | 10 | #include "chrome/browser/extensions/execute_code_in_tab_function.h" |
[email protected] | bfdffe2b | 2009-04-24 22:05:35 | [diff] [blame] | 11 | #include "chrome/browser/extensions/extension_bookmarks_module.h" |
[email protected] | f9391485 | 2009-05-26 06:05:40 | [diff] [blame] | 12 | #include "chrome/browser/extensions/extension_bookmarks_module_constants.h" |
[email protected] | ec9ac0df | 2009-10-01 18:06:47 | [diff] [blame] | 13 | #include "chrome/browser/extensions/extension_browser_actions_api.h" |
[email protected] | bfdffe2b | 2009-04-24 22:05:35 | [diff] [blame] | 14 | #include "chrome/browser/extensions/extension_function.h" |
[email protected] | 198bcfe | 2009-09-09 22:56:28 | [diff] [blame] | 15 | #include "chrome/browser/extensions/extension_i18n_api.h" |
[email protected] | e916901c | 2009-05-07 00:14:31 | [diff] [blame] | 16 | #include "chrome/browser/extensions/extension_message_service.h" |
[email protected] | f7f3a5f | 2009-05-01 22:02:34 | [diff] [blame] | 17 | #include "chrome/browser/extensions/extension_page_actions_module.h" |
[email protected] | f9391485 | 2009-05-26 06:05:40 | [diff] [blame] | 18 | #include "chrome/browser/extensions/extension_page_actions_module_constants.h" |
[email protected] | 4577622 | 2009-07-15 20:21:58 | [diff] [blame] | 19 | #include "chrome/browser/extensions/extension_process_manager.h" |
[email protected] | bfdffe2b | 2009-04-24 22:05:35 | [diff] [blame] | 20 | #include "chrome/browser/extensions/extension_tabs_module.h" |
[email protected] | f9391485 | 2009-05-26 06:05:40 | [diff] [blame] | 21 | #include "chrome/browser/extensions/extension_tabs_module_constants.h" |
[email protected] | 25fd1b2e | 2009-08-17 20:57:14 | [diff] [blame] | 22 | #include "chrome/browser/extensions/extension_test_api.h" |
[email protected] | 9c45b718 | 2009-08-04 16:44:43 | [diff] [blame] | 23 | #include "chrome/browser/extensions/extension_toolstrip_api.h" |
[email protected] | e916901c | 2009-05-07 00:14:31 | [diff] [blame] | 24 | #include "chrome/browser/profile.h" |
[email protected] | bfdffe2b | 2009-04-24 22:05:35 | [diff] [blame] | 25 | #include "chrome/browser/renderer_host/render_process_host.h" |
| 26 | #include "chrome/browser/renderer_host/render_view_host.h" |
[email protected] | 3550635 | 2009-08-07 18:58:19 | [diff] [blame] | 27 | #include "chrome/common/render_messages.h" |
[email protected] | bfdffe2b | 2009-04-24 22:05:35 | [diff] [blame] | 28 | #include "chrome/common/result_codes.h" |
[email protected] | 9c45b718 | 2009-08-04 16:44:43 | [diff] [blame] | 29 | #include "chrome/common/url_constants.h" |
[email protected] | bfdffe2b | 2009-04-24 22:05:35 | [diff] [blame] | 30 | |
| 31 | // FactoryRegistry ------------------------------------------------------------- |
| 32 | |
| 33 | namespace { |
| 34 | |
[email protected] | b83e460 | 2009-05-15 22:58:33 | [diff] [blame] | 35 | // Template for defining ExtensionFunctionFactory. |
| 36 | template<class T> |
| 37 | ExtensionFunction* NewExtensionFunction() { |
| 38 | return new T(); |
| 39 | } |
[email protected] | bfdffe2b | 2009-04-24 22:05:35 | [diff] [blame] | 40 | |
[email protected] | b83e460 | 2009-05-15 22:58:33 | [diff] [blame] | 41 | // Contains a list of all known extension functions and allows clients to |
| 42 | // create instances of them. |
[email protected] | bfdffe2b | 2009-04-24 22:05:35 | [diff] [blame] | 43 | class FactoryRegistry { |
| 44 | public: |
| 45 | static FactoryRegistry* instance(); |
[email protected] | b83e460 | 2009-05-15 22:58:33 | [diff] [blame] | 46 | FactoryRegistry() { ResetFunctions(); } |
| 47 | |
| 48 | // Resets all functions to their default values. |
| 49 | void ResetFunctions(); |
| 50 | |
| 51 | // Adds all function names to 'names'. |
[email protected] | bfdffe2b | 2009-04-24 22:05:35 | [diff] [blame] | 52 | void GetAllNames(std::vector<std::string>* names); |
[email protected] | b83e460 | 2009-05-15 22:58:33 | [diff] [blame] | 53 | |
| 54 | // Allows overriding of specific functions (e.g. for testing). Functions |
| 55 | // must be previously registered. Returns true if successful. |
| 56 | bool OverrideFunction(const std::string& name, |
| 57 | ExtensionFunctionFactory factory); |
| 58 | |
| 59 | // Factory method for the ExtensionFunction registered as 'name'. |
[email protected] | bfdffe2b | 2009-04-24 22:05:35 | [diff] [blame] | 60 | ExtensionFunction* NewFunction(const std::string& name); |
| 61 | |
| 62 | private: |
| 63 | typedef std::map<std::string, ExtensionFunctionFactory> FactoryMap; |
| 64 | FactoryMap factories_; |
| 65 | }; |
| 66 | |
[email protected] | bfdffe2b | 2009-04-24 22:05:35 | [diff] [blame] | 67 | FactoryRegistry* FactoryRegistry::instance() { |
| 68 | return Singleton<FactoryRegistry>::get(); |
| 69 | } |
| 70 | |
[email protected] | b83e460 | 2009-05-15 22:58:33 | [diff] [blame] | 71 | void FactoryRegistry::ResetFunctions() { |
[email protected] | bfdffe2b | 2009-04-24 22:05:35 | [diff] [blame] | 72 | // Register all functions here. |
| 73 | |
[email protected] | f9391485 | 2009-05-26 06:05:40 | [diff] [blame] | 74 | namespace bookmarks = extension_bookmarks_module_constants; |
[email protected] | 198bcfe | 2009-09-09 22:56:28 | [diff] [blame] | 75 | namespace i18n = extension_i18n_api_functions; |
[email protected] | 25fd1b2e | 2009-08-17 20:57:14 | [diff] [blame] | 76 | namespace page_actions = extension_page_actions_module_constants; |
[email protected] | ec9ac0df | 2009-10-01 18:06:47 | [diff] [blame] | 77 | namespace browser_actions = extension_browser_actions_api_constants; |
[email protected] | 25fd1b2e | 2009-08-17 20:57:14 | [diff] [blame] | 78 | namespace tabs = extension_tabs_module_constants; |
| 79 | namespace test = extension_test_api_functions; |
[email protected] | 9c45b718 | 2009-08-04 16:44:43 | [diff] [blame] | 80 | namespace toolstrip = extension_toolstrip_api_functions; |
[email protected] | f9391485 | 2009-05-26 06:05:40 | [diff] [blame] | 81 | |
[email protected] | e515f5d | 2009-05-05 03:05:00 | [diff] [blame] | 82 | // Windows |
[email protected] | afac6fe | 2009-06-04 14:58:18 | [diff] [blame] | 83 | factories_[tabs::kGetWindowFunction] = |
[email protected] | f9391485 | 2009-05-26 06:05:40 | [diff] [blame] | 84 | &NewExtensionFunction<GetWindowFunction>; |
| 85 | factories_[tabs::kGetCurrentWindowFunction] = |
[email protected] | e515f5d | 2009-05-05 03:05:00 | [diff] [blame] | 86 | &NewExtensionFunction<GetCurrentWindowFunction>; |
[email protected] | f9391485 | 2009-05-26 06:05:40 | [diff] [blame] | 87 | factories_[tabs::kGetLastFocusedWindowFunction] = |
[email protected] | c661918 | 2009-05-12 14:59:32 | [diff] [blame] | 88 | &NewExtensionFunction<GetLastFocusedWindowFunction>; |
[email protected] | f9391485 | 2009-05-26 06:05:40 | [diff] [blame] | 89 | factories_[tabs::kGetAllWindowsFunction] = |
| 90 | &NewExtensionFunction<GetAllWindowsFunction>; |
[email protected] | afac6fe | 2009-06-04 14:58:18 | [diff] [blame] | 91 | factories_[tabs::kCreateWindowFunction] = |
[email protected] | f9391485 | 2009-05-26 06:05:40 | [diff] [blame] | 92 | &NewExtensionFunction<CreateWindowFunction>; |
[email protected] | afac6fe | 2009-06-04 14:58:18 | [diff] [blame] | 93 | factories_[tabs::kUpdateWindowFunction] = |
[email protected] | f9391485 | 2009-05-26 06:05:40 | [diff] [blame] | 94 | &NewExtensionFunction<UpdateWindowFunction>; |
[email protected] | afac6fe | 2009-06-04 14:58:18 | [diff] [blame] | 95 | factories_[tabs::kRemoveWindowFunction] = |
[email protected] | f9391485 | 2009-05-26 06:05:40 | [diff] [blame] | 96 | &NewExtensionFunction<RemoveWindowFunction>; |
[email protected] | b83e460 | 2009-05-15 22:58:33 | [diff] [blame] | 97 | |
[email protected] | e515f5d | 2009-05-05 03:05:00 | [diff] [blame] | 98 | // Tabs |
[email protected] | afac6fe | 2009-06-04 14:58:18 | [diff] [blame] | 99 | factories_[tabs::kGetTabFunction] = |
[email protected] | f9391485 | 2009-05-26 06:05:40 | [diff] [blame] | 100 | &NewExtensionFunction<GetTabFunction>; |
| 101 | factories_[tabs::kGetSelectedTabFunction] = |
[email protected] | b83e460 | 2009-05-15 22:58:33 | [diff] [blame] | 102 | &NewExtensionFunction<GetSelectedTabFunction>; |
[email protected] | f9391485 | 2009-05-26 06:05:40 | [diff] [blame] | 103 | factories_[tabs::kGetAllTabsInWindowFunction] = |
[email protected] | e515f5d | 2009-05-05 03:05:00 | [diff] [blame] | 104 | &NewExtensionFunction<GetAllTabsInWindowFunction>; |
[email protected] | afac6fe | 2009-06-04 14:58:18 | [diff] [blame] | 105 | factories_[tabs::kCreateTabFunction] = |
[email protected] | f9391485 | 2009-05-26 06:05:40 | [diff] [blame] | 106 | &NewExtensionFunction<CreateTabFunction>; |
[email protected] | afac6fe | 2009-06-04 14:58:18 | [diff] [blame] | 107 | factories_[tabs::kUpdateTabFunction] = |
[email protected] | f9391485 | 2009-05-26 06:05:40 | [diff] [blame] | 108 | &NewExtensionFunction<UpdateTabFunction>; |
[email protected] | afac6fe | 2009-06-04 14:58:18 | [diff] [blame] | 109 | factories_[tabs::kMoveTabFunction] = |
[email protected] | f9391485 | 2009-05-26 06:05:40 | [diff] [blame] | 110 | &NewExtensionFunction<MoveTabFunction>; |
[email protected] | afac6fe | 2009-06-04 14:58:18 | [diff] [blame] | 111 | factories_[tabs::kRemoveTabFunction] = |
[email protected] | f9391485 | 2009-05-26 06:05:40 | [diff] [blame] | 112 | &NewExtensionFunction<RemoveTabFunction>; |
[email protected] | 5bf3935 | 2009-07-16 20:43:01 | [diff] [blame] | 113 | factories_[tabs::kDetectTabLanguageFunction] = |
| 114 | &NewExtensionFunction<DetectTabLanguageFunction>; |
[email protected] | 3947c4d | 2009-08-13 00:04:06 | [diff] [blame] | 115 | factories_[tabs::kCaptureVisibleTabFunction] = |
| 116 | &NewExtensionFunction<CaptureVisibleTabFunction>; |
[email protected] | 912256b3 | 2009-09-18 09:47:35 | [diff] [blame] | 117 | factories_[tabs::kExecuteScriptFunction] = |
| 118 | &NewExtensionFunction<ExecuteCodeInTabFunction>; |
| 119 | factories_[tabs::kInsertCSSFunction] = |
| 120 | &NewExtensionFunction<ExecuteCodeInTabFunction>; |
[email protected] | bfdffe2b | 2009-04-24 22:05:35 | [diff] [blame] | 121 | |
[email protected] | f7f3a5f | 2009-05-01 22:02:34 | [diff] [blame] | 122 | // Page Actions. |
[email protected] | f9391485 | 2009-05-26 06:05:40 | [diff] [blame] | 123 | factories_[page_actions::kEnablePageActionFunction] = |
[email protected] | f7f3a5f | 2009-05-01 22:02:34 | [diff] [blame] | 124 | &NewExtensionFunction<EnablePageActionFunction>; |
[email protected] | f9b1a7b | 2009-06-22 17:26:38 | [diff] [blame] | 125 | factories_[page_actions::kDisablePageActionFunction] = |
| 126 | &NewExtensionFunction<DisablePageActionFunction>; |
[email protected] | f7f3a5f | 2009-05-01 22:02:34 | [diff] [blame] | 127 | |
[email protected] | ec9ac0df | 2009-10-01 18:06:47 | [diff] [blame] | 128 | // Browser Actions. |
| 129 | factories_[browser_actions::kSetNameFunction] = |
| 130 | &NewExtensionFunction<BrowserActionSetNameFunction>; |
| 131 | factories_[browser_actions::kSetIconFunction] = |
| 132 | &NewExtensionFunction<BrowserActionSetIconFunction>; |
[email protected] | 5132483c | 2009-10-03 19:39:24 | [diff] [blame] | 133 | factories_[browser_actions::kSetBadgeTextFunction] = |
| 134 | &NewExtensionFunction<BrowserActionSetBadgeTextFunction>; |
| 135 | factories_[browser_actions::kSetBadgeBackgroundColorFunction] = |
| 136 | &NewExtensionFunction<BrowserActionSetBadgeBackgroundColorFunction>; |
[email protected] | ec9ac0df | 2009-10-01 18:06:47 | [diff] [blame] | 137 | |
[email protected] | f7f3a5f | 2009-05-01 22:02:34 | [diff] [blame] | 138 | // Bookmarks. |
[email protected] | f9391485 | 2009-05-26 06:05:40 | [diff] [blame] | 139 | factories_[bookmarks::kGetBookmarksFunction] = |
| 140 | &NewExtensionFunction<GetBookmarksFunction>; |
| 141 | factories_[bookmarks::kGetBookmarkChildrenFunction] = |
[email protected] | 309934c4 | 2009-04-29 20:28:46 | [diff] [blame] | 142 | &NewExtensionFunction<GetBookmarkChildrenFunction>; |
[email protected] | f9391485 | 2009-05-26 06:05:40 | [diff] [blame] | 143 | factories_[bookmarks::kGetBookmarkTreeFunction] = |
[email protected] | 309934c4 | 2009-04-29 20:28:46 | [diff] [blame] | 144 | &NewExtensionFunction<GetBookmarkTreeFunction>; |
[email protected] | f9391485 | 2009-05-26 06:05:40 | [diff] [blame] | 145 | factories_[bookmarks::kSearchBookmarksFunction] = |
[email protected] | bfdffe2b | 2009-04-24 22:05:35 | [diff] [blame] | 146 | &NewExtensionFunction<SearchBookmarksFunction>; |
[email protected] | f9391485 | 2009-05-26 06:05:40 | [diff] [blame] | 147 | factories_[bookmarks::kRemoveBookmarkFunction] = |
[email protected] | b83e460 | 2009-05-15 22:58:33 | [diff] [blame] | 148 | &NewExtensionFunction<RemoveBookmarkFunction>; |
[email protected] | 434c21e | 2009-07-16 22:20:00 | [diff] [blame] | 149 | factories_[bookmarks::kRemoveBookmarkTreeFunction] = |
| 150 | &NewExtensionFunction<RemoveBookmarkFunction>; |
[email protected] | f9391485 | 2009-05-26 06:05:40 | [diff] [blame] | 151 | factories_[bookmarks::kCreateBookmarkFunction] = |
[email protected] | b83e460 | 2009-05-15 22:58:33 | [diff] [blame] | 152 | &NewExtensionFunction<CreateBookmarkFunction>; |
[email protected] | f9391485 | 2009-05-26 06:05:40 | [diff] [blame] | 153 | factories_[bookmarks::kMoveBookmarkFunction] = |
| 154 | &NewExtensionFunction<MoveBookmarkFunction>; |
[email protected] | ea7c4fb | 2009-09-01 15:49:37 | [diff] [blame] | 155 | factories_[bookmarks::kUpdateBookmarkFunction] = |
| 156 | &NewExtensionFunction<UpdateBookmarkFunction>; |
[email protected] | 9c45b718 | 2009-08-04 16:44:43 | [diff] [blame] | 157 | |
| 158 | // Toolstrips. |
| 159 | factories_[toolstrip::kExpandFunction] = |
| 160 | &NewExtensionFunction<ToolstripExpandFunction>; |
| 161 | factories_[toolstrip::kCollapseFunction] = |
| 162 | &NewExtensionFunction<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. |
| 165 | factories_[i18n::kGetAcceptLanguagesFunction] = |
| 166 | &NewExtensionFunction<GetAcceptLanguagesFunction>; |
| 167 | |
[email protected] | 25fd1b2e | 2009-08-17 20:57:14 | [diff] [blame] | 168 | // Test. |
| 169 | factories_[test::kPassFunction] = |
| 170 | &NewExtensionFunction<ExtensionTestPassFunction>; |
| 171 | factories_[test::kFailFunction] = |
| 172 | &NewExtensionFunction<ExtensionTestFailFunction>; |
[email protected] | 9b6d31d | 2009-09-01 01:44:03 | [diff] [blame] | 173 | factories_[test::kLogFunction] = |
| 174 | &NewExtensionFunction<ExtensionTestLogFunction>; |
[email protected] | bfdffe2b | 2009-04-24 22:05:35 | [diff] [blame] | 175 | } |
| 176 | |
[email protected] | b83e460 | 2009-05-15 22:58:33 | [diff] [blame] | 177 | void FactoryRegistry::GetAllNames(std::vector<std::string>* names) { |
| 178 | for (FactoryMap::iterator iter = factories_.begin(); |
| 179 | iter != factories_.end(); ++iter) { |
[email protected] | bfdffe2b | 2009-04-24 22:05:35 | [diff] [blame] | 180 | names->push_back(iter->first); |
| 181 | } |
| 182 | } |
| 183 | |
[email protected] | b83e460 | 2009-05-15 22:58:33 | [diff] [blame] | 184 | bool FactoryRegistry::OverrideFunction(const std::string& name, |
| 185 | ExtensionFunctionFactory factory) { |
| 186 | FactoryMap::iterator iter = factories_.find(name); |
| 187 | if (iter == factories_.end()) { |
| 188 | return false; |
| 189 | } else { |
| 190 | iter->second = factory; |
| 191 | return true; |
| 192 | } |
| 193 | } |
| 194 | |
[email protected] | bfdffe2b | 2009-04-24 22:05:35 | [diff] [blame] | 195 | ExtensionFunction* FactoryRegistry::NewFunction(const std::string& name) { |
| 196 | FactoryMap::iterator iter = factories_.find(name); |
| 197 | DCHECK(iter != factories_.end()); |
[email protected] | b83e460 | 2009-05-15 22:58:33 | [diff] [blame] | 198 | ExtensionFunction* function = iter->second(); |
[email protected] | 76a3db85 | 2009-07-24 02:14:56 | [diff] [blame] | 199 | function->set_name(name); |
[email protected] | b83e460 | 2009-05-15 22:58:33 | [diff] [blame] | 200 | return function; |
[email protected] | bfdffe2b | 2009-04-24 22:05:35 | [diff] [blame] | 201 | } |
| 202 | |
[email protected] | b83e460 | 2009-05-15 22:58:33 | [diff] [blame] | 203 | }; // namespace |
[email protected] | bfdffe2b | 2009-04-24 22:05:35 | [diff] [blame] | 204 | |
| 205 | // ExtensionFunctionDispatcher ------------------------------------------------- |
| 206 | |
| 207 | void ExtensionFunctionDispatcher::GetAllFunctionNames( |
| 208 | std::vector<std::string>* names) { |
| 209 | FactoryRegistry::instance()->GetAllNames(names); |
| 210 | } |
| 211 | |
[email protected] | b83e460 | 2009-05-15 22:58:33 | [diff] [blame] | 212 | bool ExtensionFunctionDispatcher::OverrideFunction( |
| 213 | const std::string& name, ExtensionFunctionFactory factory) { |
| 214 | return FactoryRegistry::instance()->OverrideFunction(name, factory); |
| 215 | } |
| 216 | |
| 217 | void ExtensionFunctionDispatcher::ResetFunctions() { |
| 218 | FactoryRegistry::instance()->ResetFunctions(); |
| 219 | } |
| 220 | |
[email protected] | 811bfe3 | 2009-07-01 08:46:25 | [diff] [blame] | 221 | std::set<ExtensionFunctionDispatcher*>* |
| 222 | ExtensionFunctionDispatcher::all_instances() { |
| 223 | static std::set<ExtensionFunctionDispatcher*> instances; |
| 224 | return &instances; |
| 225 | } |
| 226 | |
[email protected] | bfdffe2b | 2009-04-24 22:05:35 | [diff] [blame] | 227 | ExtensionFunctionDispatcher::ExtensionFunctionDispatcher( |
| 228 | RenderViewHost* render_view_host, |
[email protected] | 7eecaed5 | 2009-05-07 21:44:12 | [diff] [blame] | 229 | Delegate* delegate, |
[email protected] | 811bfe3 | 2009-07-01 08:46:25 | [diff] [blame] | 230 | const GURL& url) |
[email protected] | bfdffe2b | 2009-04-24 22:05:35 | [diff] [blame] | 231 | : render_view_host_(render_view_host), |
[email protected] | 7eecaed5 | 2009-05-07 21:44:12 | [diff] [blame] | 232 | delegate_(delegate), |
[email protected] | 811bfe3 | 2009-07-01 08:46:25 | [diff] [blame] | 233 | url_(url), |
[email protected] | 32dda36 | 2009-06-05 19:07:01 | [diff] [blame] | 234 | ALLOW_THIS_IN_INITIALIZER_LIST(peer_(new Peer(this))) { |
[email protected] | 9c45b718 | 2009-08-04 16:44:43 | [diff] [blame] | 235 | // TODO(erikkay) should we do something for these errors in Release? |
| 236 | DCHECK(url.SchemeIs(chrome::kExtensionScheme)); |
[email protected] | 3550635 | 2009-08-07 18:58:19 | [diff] [blame] | 237 | |
| 238 | Extension* extension = |
| 239 | profile()->GetExtensionsService()->GetExtensionByURL(url); |
| 240 | DCHECK(extension); |
[email protected] | 9c45b718 | 2009-08-04 16:44:43 | [diff] [blame] | 241 | |
[email protected] | 811bfe3 | 2009-07-01 08:46:25 | [diff] [blame] | 242 | all_instances()->insert(this); |
[email protected] | 0f605396 | 2009-07-09 19:26:35 | [diff] [blame] | 243 | |
[email protected] | 4577622 | 2009-07-15 20:21:58 | [diff] [blame] | 244 | // Notify the ExtensionProcessManager that the view was created. |
| 245 | ExtensionProcessManager* epm = profile()->GetExtensionProcessManager(); |
| 246 | epm->RegisterExtensionProcess(extension_id(), |
[email protected] | 76543b9 | 2009-08-31 17:27:45 | [diff] [blame] | 247 | render_view_host->process()->id()); |
[email protected] | 3550635 | 2009-08-07 18:58:19 | [diff] [blame] | 248 | |
| 249 | // Update the extension permissions. Doing this each time we create an EFD |
| 250 | // ensures that new processes are informed of permissions for newly installed |
| 251 | // extensions. |
[email protected] | cccf9093 | 2009-08-23 17:56:25 | [diff] [blame] | 252 | render_view_host->Send(new ViewMsg_Extension_SetAPIPermissions( |
[email protected] | 3550635 | 2009-08-07 18:58:19 | [diff] [blame] | 253 | extension->id(), extension->api_permissions())); |
[email protected] | cccf9093 | 2009-08-23 17:56:25 | [diff] [blame] | 254 | render_view_host->Send(new ViewMsg_Extension_SetHostPermissions( |
| 255 | extension->url(), extension->host_permissions())); |
[email protected] | bfdffe2b | 2009-04-24 22:05:35 | [diff] [blame] | 256 | } |
| 257 | |
[email protected] | 32dda36 | 2009-06-05 19:07:01 | [diff] [blame] | 258 | ExtensionFunctionDispatcher::~ExtensionFunctionDispatcher() { |
[email protected] | 811bfe3 | 2009-07-01 08:46:25 | [diff] [blame] | 259 | all_instances()->erase(this); |
[email protected] | 32dda36 | 2009-06-05 19:07:01 | [diff] [blame] | 260 | peer_->dispatcher_ = NULL; |
| 261 | } |
| 262 | |
[email protected] | 7eecaed5 | 2009-05-07 21:44:12 | [diff] [blame] | 263 | Browser* ExtensionFunctionDispatcher::GetBrowser() { |
[email protected] | 4ba60e2 | 2009-08-14 21:02:11 | [diff] [blame] | 264 | return delegate_->GetBrowser(); |
[email protected] | 7eecaed5 | 2009-05-07 21:44:12 | [diff] [blame] | 265 | } |
| 266 | |
[email protected] | 9c45b718 | 2009-08-04 16:44:43 | [diff] [blame] | 267 | ExtensionHost* ExtensionFunctionDispatcher::GetExtensionHost() { |
[email protected] | 9c45b718 | 2009-08-04 16:44:43 | [diff] [blame] | 268 | return delegate_->GetExtensionHost(); |
| 269 | } |
| 270 | |
[email protected] | c7ad50f | 2009-09-11 06:28:15 | [diff] [blame] | 271 | Extension* ExtensionFunctionDispatcher::GetExtension() { |
| 272 | ExtensionsService* service = profile()->GetExtensionsService(); |
| 273 | DCHECK(service); |
| 274 | |
| 275 | Extension* extension = service->GetExtensionById(extension_id()); |
| 276 | DCHECK(extension); |
| 277 | |
| 278 | return extension; |
| 279 | } |
| 280 | |
[email protected] | bfdffe2b | 2009-04-24 22:05:35 | [diff] [blame] | 281 | void ExtensionFunctionDispatcher::HandleRequest(const std::string& name, |
[email protected] | e4dad9fb | 2009-10-06 18:15:58 | [diff] [blame^] | 282 | const Value* args, |
[email protected] | c661918 | 2009-05-12 14:59:32 | [diff] [blame] | 283 | int request_id, |
| 284 | bool has_callback) { |
[email protected] | 32dda36 | 2009-06-05 19:07:01 | [diff] [blame] | 285 | scoped_refptr<ExtensionFunction> function( |
[email protected] | bfdffe2b | 2009-04-24 22:05:35 | [diff] [blame] | 286 | FactoryRegistry::instance()->NewFunction(name)); |
[email protected] | 32dda36 | 2009-06-05 19:07:01 | [diff] [blame] | 287 | function->set_dispatcher_peer(peer_); |
[email protected] | b83e460 | 2009-05-15 22:58:33 | [diff] [blame] | 288 | function->SetArgs(args); |
[email protected] | c661918 | 2009-05-12 14:59:32 | [diff] [blame] | 289 | function->set_request_id(request_id); |
| 290 | function->set_has_callback(has_callback); |
[email protected] | bfdffe2b | 2009-04-24 22:05:35 | [diff] [blame] | 291 | function->Run(); |
| 292 | } |
| 293 | |
[email protected] | c661918 | 2009-05-12 14:59:32 | [diff] [blame] | 294 | void ExtensionFunctionDispatcher::SendResponse(ExtensionFunction* function, |
| 295 | bool success) { |
[email protected] | c661918 | 2009-05-12 14:59:32 | [diff] [blame] | 296 | render_view_host_->SendExtensionResponse(function->request_id(), success, |
[email protected] | b83e460 | 2009-05-15 22:58:33 | [diff] [blame] | 297 | function->GetResult(), function->GetError()); |
[email protected] | bfdffe2b | 2009-04-24 22:05:35 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | void ExtensionFunctionDispatcher::HandleBadMessage(ExtensionFunction* api) { |
[email protected] | 25fd1b2e | 2009-08-17 20:57:14 | [diff] [blame] | 301 | LOG(ERROR) << "bad extension message " << |
[email protected] | 76543b9 | 2009-08-31 17:27:45 | [diff] [blame] | 302 | api->name() << |
[email protected] | bfdffe2b | 2009-04-24 22:05:35 | [diff] [blame] | 303 | " : terminating renderer."; |
| 304 | if (RenderProcessHost::run_renderer_in_process()) { |
| 305 | // In single process mode it is better if we don't suicide but just crash. |
| 306 | CHECK(false); |
| 307 | } else { |
| 308 | NOTREACHED(); |
| 309 | base::KillProcess(render_view_host_->process()->process().handle(), |
| 310 | ResultCodes::KILLED_BAD_MESSAGE, false); |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | Profile* ExtensionFunctionDispatcher::profile() { |
| 315 | return render_view_host_->process()->profile(); |
| 316 | } |