[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" |
| 10 | #include "chrome/browser/extensions/extension_bookmarks_module.h" |
[email protected] | f9391485 | 2009-05-26 06:05:40 | [diff] [blame] | 11 | #include "chrome/browser/extensions/extension_bookmarks_module_constants.h" |
[email protected] | bfdffe2b | 2009-04-24 22:05:35 | [diff] [blame] | 12 | #include "chrome/browser/extensions/extension_function.h" |
[email protected] | e916901c | 2009-05-07 00:14:31 | [diff] [blame] | 13 | #include "chrome/browser/extensions/extension_message_service.h" |
[email protected] | f7f3a5f | 2009-05-01 22:02:34 | [diff] [blame] | 14 | #include "chrome/browser/extensions/extension_page_actions_module.h" |
[email protected] | f9391485 | 2009-05-26 06:05:40 | [diff] [blame] | 15 | #include "chrome/browser/extensions/extension_page_actions_module_constants.h" |
[email protected] | 4577622 | 2009-07-15 20:21:58 | [diff] [blame^] | 16 | #include "chrome/browser/extensions/extension_process_manager.h" |
[email protected] | bfdffe2b | 2009-04-24 22:05:35 | [diff] [blame] | 17 | #include "chrome/browser/extensions/extension_tabs_module.h" |
[email protected] | f9391485 | 2009-05-26 06:05:40 | [diff] [blame] | 18 | #include "chrome/browser/extensions/extension_tabs_module_constants.h" |
[email protected] | e916901c | 2009-05-07 00:14:31 | [diff] [blame] | 19 | #include "chrome/browser/profile.h" |
[email protected] | bfdffe2b | 2009-04-24 22:05:35 | [diff] [blame] | 20 | #include "chrome/browser/renderer_host/render_process_host.h" |
| 21 | #include "chrome/browser/renderer_host/render_view_host.h" |
| 22 | #include "chrome/common/result_codes.h" |
| 23 | |
| 24 | // FactoryRegistry ------------------------------------------------------------- |
| 25 | |
| 26 | namespace { |
| 27 | |
[email protected] | b83e460 | 2009-05-15 22:58:33 | [diff] [blame] | 28 | // Template for defining ExtensionFunctionFactory. |
| 29 | template<class T> |
| 30 | ExtensionFunction* NewExtensionFunction() { |
| 31 | return new T(); |
| 32 | } |
[email protected] | bfdffe2b | 2009-04-24 22:05:35 | [diff] [blame] | 33 | |
[email protected] | b83e460 | 2009-05-15 22:58:33 | [diff] [blame] | 34 | // Contains a list of all known extension functions and allows clients to |
| 35 | // create instances of them. |
[email protected] | bfdffe2b | 2009-04-24 22:05:35 | [diff] [blame] | 36 | class FactoryRegistry { |
| 37 | public: |
| 38 | static FactoryRegistry* instance(); |
[email protected] | b83e460 | 2009-05-15 22:58:33 | [diff] [blame] | 39 | FactoryRegistry() { ResetFunctions(); } |
| 40 | |
| 41 | // Resets all functions to their default values. |
| 42 | void ResetFunctions(); |
| 43 | |
| 44 | // Adds all function names to 'names'. |
[email protected] | bfdffe2b | 2009-04-24 22:05:35 | [diff] [blame] | 45 | void GetAllNames(std::vector<std::string>* names); |
[email protected] | b83e460 | 2009-05-15 22:58:33 | [diff] [blame] | 46 | |
| 47 | // Allows overriding of specific functions (e.g. for testing). Functions |
| 48 | // must be previously registered. Returns true if successful. |
| 49 | bool OverrideFunction(const std::string& name, |
| 50 | ExtensionFunctionFactory factory); |
| 51 | |
| 52 | // Factory method for the ExtensionFunction registered as 'name'. |
[email protected] | bfdffe2b | 2009-04-24 22:05:35 | [diff] [blame] | 53 | ExtensionFunction* NewFunction(const std::string& name); |
| 54 | |
| 55 | private: |
| 56 | typedef std::map<std::string, ExtensionFunctionFactory> FactoryMap; |
| 57 | FactoryMap factories_; |
| 58 | }; |
| 59 | |
[email protected] | bfdffe2b | 2009-04-24 22:05:35 | [diff] [blame] | 60 | FactoryRegistry* FactoryRegistry::instance() { |
| 61 | return Singleton<FactoryRegistry>::get(); |
| 62 | } |
| 63 | |
[email protected] | b83e460 | 2009-05-15 22:58:33 | [diff] [blame] | 64 | void FactoryRegistry::ResetFunctions() { |
[email protected] | bfdffe2b | 2009-04-24 22:05:35 | [diff] [blame] | 65 | // Register all functions here. |
| 66 | |
[email protected] | f9391485 | 2009-05-26 06:05:40 | [diff] [blame] | 67 | namespace tabs = extension_tabs_module_constants; |
| 68 | namespace page_actions = extension_page_actions_module_constants; |
| 69 | namespace bookmarks = extension_bookmarks_module_constants; |
| 70 | |
[email protected] | e515f5d | 2009-05-05 03:05:00 | [diff] [blame] | 71 | // Windows |
[email protected] | afac6fe | 2009-06-04 14:58:18 | [diff] [blame] | 72 | factories_[tabs::kGetWindowFunction] = |
[email protected] | f9391485 | 2009-05-26 06:05:40 | [diff] [blame] | 73 | &NewExtensionFunction<GetWindowFunction>; |
| 74 | factories_[tabs::kGetCurrentWindowFunction] = |
[email protected] | e515f5d | 2009-05-05 03:05:00 | [diff] [blame] | 75 | &NewExtensionFunction<GetCurrentWindowFunction>; |
[email protected] | f9391485 | 2009-05-26 06:05:40 | [diff] [blame] | 76 | factories_[tabs::kGetLastFocusedWindowFunction] = |
[email protected] | c661918 | 2009-05-12 14:59:32 | [diff] [blame] | 77 | &NewExtensionFunction<GetLastFocusedWindowFunction>; |
[email protected] | f9391485 | 2009-05-26 06:05:40 | [diff] [blame] | 78 | factories_[tabs::kGetAllWindowsFunction] = |
| 79 | &NewExtensionFunction<GetAllWindowsFunction>; |
[email protected] | afac6fe | 2009-06-04 14:58:18 | [diff] [blame] | 80 | factories_[tabs::kCreateWindowFunction] = |
[email protected] | f9391485 | 2009-05-26 06:05:40 | [diff] [blame] | 81 | &NewExtensionFunction<CreateWindowFunction>; |
[email protected] | afac6fe | 2009-06-04 14:58:18 | [diff] [blame] | 82 | factories_[tabs::kUpdateWindowFunction] = |
[email protected] | f9391485 | 2009-05-26 06:05:40 | [diff] [blame] | 83 | &NewExtensionFunction<UpdateWindowFunction>; |
[email protected] | afac6fe | 2009-06-04 14:58:18 | [diff] [blame] | 84 | factories_[tabs::kRemoveWindowFunction] = |
[email protected] | f9391485 | 2009-05-26 06:05:40 | [diff] [blame] | 85 | &NewExtensionFunction<RemoveWindowFunction>; |
[email protected] | b83e460 | 2009-05-15 22:58:33 | [diff] [blame] | 86 | |
[email protected] | e515f5d | 2009-05-05 03:05:00 | [diff] [blame] | 87 | // Tabs |
[email protected] | afac6fe | 2009-06-04 14:58:18 | [diff] [blame] | 88 | factories_[tabs::kGetTabFunction] = |
[email protected] | f9391485 | 2009-05-26 06:05:40 | [diff] [blame] | 89 | &NewExtensionFunction<GetTabFunction>; |
| 90 | factories_[tabs::kGetSelectedTabFunction] = |
[email protected] | b83e460 | 2009-05-15 22:58:33 | [diff] [blame] | 91 | &NewExtensionFunction<GetSelectedTabFunction>; |
[email protected] | f9391485 | 2009-05-26 06:05:40 | [diff] [blame] | 92 | factories_[tabs::kGetAllTabsInWindowFunction] = |
[email protected] | e515f5d | 2009-05-05 03:05:00 | [diff] [blame] | 93 | &NewExtensionFunction<GetAllTabsInWindowFunction>; |
[email protected] | afac6fe | 2009-06-04 14:58:18 | [diff] [blame] | 94 | factories_[tabs::kCreateTabFunction] = |
[email protected] | f9391485 | 2009-05-26 06:05:40 | [diff] [blame] | 95 | &NewExtensionFunction<CreateTabFunction>; |
[email protected] | afac6fe | 2009-06-04 14:58:18 | [diff] [blame] | 96 | factories_[tabs::kUpdateTabFunction] = |
[email protected] | f9391485 | 2009-05-26 06:05:40 | [diff] [blame] | 97 | &NewExtensionFunction<UpdateTabFunction>; |
[email protected] | afac6fe | 2009-06-04 14:58:18 | [diff] [blame] | 98 | factories_[tabs::kMoveTabFunction] = |
[email protected] | f9391485 | 2009-05-26 06:05:40 | [diff] [blame] | 99 | &NewExtensionFunction<MoveTabFunction>; |
[email protected] | afac6fe | 2009-06-04 14:58:18 | [diff] [blame] | 100 | factories_[tabs::kRemoveTabFunction] = |
[email protected] | f9391485 | 2009-05-26 06:05:40 | [diff] [blame] | 101 | &NewExtensionFunction<RemoveTabFunction>; |
[email protected] | 5c426692 | 2009-07-10 16:41:27 | [diff] [blame] | 102 | factories_[tabs::kGetTabLanguageFunction] = |
| 103 | &NewExtensionFunction<GetTabLanguageFunction>; |
[email protected] | bfdffe2b | 2009-04-24 22:05:35 | [diff] [blame] | 104 | |
[email protected] | f7f3a5f | 2009-05-01 22:02:34 | [diff] [blame] | 105 | // Page Actions. |
[email protected] | f9391485 | 2009-05-26 06:05:40 | [diff] [blame] | 106 | factories_[page_actions::kEnablePageActionFunction] = |
[email protected] | f7f3a5f | 2009-05-01 22:02:34 | [diff] [blame] | 107 | &NewExtensionFunction<EnablePageActionFunction>; |
[email protected] | f9b1a7b | 2009-06-22 17:26:38 | [diff] [blame] | 108 | factories_[page_actions::kDisablePageActionFunction] = |
| 109 | &NewExtensionFunction<DisablePageActionFunction>; |
[email protected] | f7f3a5f | 2009-05-01 22:02:34 | [diff] [blame] | 110 | |
| 111 | // Bookmarks. |
[email protected] | f9391485 | 2009-05-26 06:05:40 | [diff] [blame] | 112 | factories_[bookmarks::kGetBookmarksFunction] = |
| 113 | &NewExtensionFunction<GetBookmarksFunction>; |
| 114 | factories_[bookmarks::kGetBookmarkChildrenFunction] = |
[email protected] | 309934c4 | 2009-04-29 20:28:46 | [diff] [blame] | 115 | &NewExtensionFunction<GetBookmarkChildrenFunction>; |
[email protected] | f9391485 | 2009-05-26 06:05:40 | [diff] [blame] | 116 | factories_[bookmarks::kGetBookmarkTreeFunction] = |
[email protected] | 309934c4 | 2009-04-29 20:28:46 | [diff] [blame] | 117 | &NewExtensionFunction<GetBookmarkTreeFunction>; |
[email protected] | f9391485 | 2009-05-26 06:05:40 | [diff] [blame] | 118 | factories_[bookmarks::kSearchBookmarksFunction] = |
[email protected] | bfdffe2b | 2009-04-24 22:05:35 | [diff] [blame] | 119 | &NewExtensionFunction<SearchBookmarksFunction>; |
[email protected] | f9391485 | 2009-05-26 06:05:40 | [diff] [blame] | 120 | factories_[bookmarks::kRemoveBookmarkFunction] = |
[email protected] | b83e460 | 2009-05-15 22:58:33 | [diff] [blame] | 121 | &NewExtensionFunction<RemoveBookmarkFunction>; |
[email protected] | f9391485 | 2009-05-26 06:05:40 | [diff] [blame] | 122 | factories_[bookmarks::kCreateBookmarkFunction] = |
[email protected] | b83e460 | 2009-05-15 22:58:33 | [diff] [blame] | 123 | &NewExtensionFunction<CreateBookmarkFunction>; |
[email protected] | f9391485 | 2009-05-26 06:05:40 | [diff] [blame] | 124 | factories_[bookmarks::kMoveBookmarkFunction] = |
| 125 | &NewExtensionFunction<MoveBookmarkFunction>; |
| 126 | factories_[bookmarks::kSetBookmarkTitleFunction] = |
[email protected] | bfdffe2b | 2009-04-24 22:05:35 | [diff] [blame] | 127 | &NewExtensionFunction<SetBookmarkTitleFunction>; |
| 128 | } |
| 129 | |
[email protected] | b83e460 | 2009-05-15 22:58:33 | [diff] [blame] | 130 | void FactoryRegistry::GetAllNames(std::vector<std::string>* names) { |
| 131 | for (FactoryMap::iterator iter = factories_.begin(); |
| 132 | iter != factories_.end(); ++iter) { |
[email protected] | bfdffe2b | 2009-04-24 22:05:35 | [diff] [blame] | 133 | names->push_back(iter->first); |
| 134 | } |
| 135 | } |
| 136 | |
[email protected] | b83e460 | 2009-05-15 22:58:33 | [diff] [blame] | 137 | bool FactoryRegistry::OverrideFunction(const std::string& name, |
| 138 | ExtensionFunctionFactory factory) { |
| 139 | FactoryMap::iterator iter = factories_.find(name); |
| 140 | if (iter == factories_.end()) { |
| 141 | return false; |
| 142 | } else { |
| 143 | iter->second = factory; |
| 144 | return true; |
| 145 | } |
| 146 | } |
| 147 | |
[email protected] | bfdffe2b | 2009-04-24 22:05:35 | [diff] [blame] | 148 | ExtensionFunction* FactoryRegistry::NewFunction(const std::string& name) { |
| 149 | FactoryMap::iterator iter = factories_.find(name); |
| 150 | DCHECK(iter != factories_.end()); |
[email protected] | b83e460 | 2009-05-15 22:58:33 | [diff] [blame] | 151 | ExtensionFunction* function = iter->second(); |
| 152 | function->SetName(name); |
| 153 | return function; |
[email protected] | bfdffe2b | 2009-04-24 22:05:35 | [diff] [blame] | 154 | } |
| 155 | |
[email protected] | b83e460 | 2009-05-15 22:58:33 | [diff] [blame] | 156 | }; // namespace |
[email protected] | bfdffe2b | 2009-04-24 22:05:35 | [diff] [blame] | 157 | |
| 158 | // ExtensionFunctionDispatcher ------------------------------------------------- |
| 159 | |
| 160 | void ExtensionFunctionDispatcher::GetAllFunctionNames( |
| 161 | std::vector<std::string>* names) { |
| 162 | FactoryRegistry::instance()->GetAllNames(names); |
| 163 | } |
| 164 | |
[email protected] | b83e460 | 2009-05-15 22:58:33 | [diff] [blame] | 165 | bool ExtensionFunctionDispatcher::OverrideFunction( |
| 166 | const std::string& name, ExtensionFunctionFactory factory) { |
| 167 | return FactoryRegistry::instance()->OverrideFunction(name, factory); |
| 168 | } |
| 169 | |
| 170 | void ExtensionFunctionDispatcher::ResetFunctions() { |
| 171 | FactoryRegistry::instance()->ResetFunctions(); |
| 172 | } |
| 173 | |
[email protected] | 811bfe3 | 2009-07-01 08:46:25 | [diff] [blame] | 174 | std::set<ExtensionFunctionDispatcher*>* |
| 175 | ExtensionFunctionDispatcher::all_instances() { |
| 176 | static std::set<ExtensionFunctionDispatcher*> instances; |
| 177 | return &instances; |
| 178 | } |
| 179 | |
[email protected] | bfdffe2b | 2009-04-24 22:05:35 | [diff] [blame] | 180 | ExtensionFunctionDispatcher::ExtensionFunctionDispatcher( |
| 181 | RenderViewHost* render_view_host, |
[email protected] | 7eecaed5 | 2009-05-07 21:44:12 | [diff] [blame] | 182 | Delegate* delegate, |
[email protected] | 811bfe3 | 2009-07-01 08:46:25 | [diff] [blame] | 183 | const GURL& url) |
[email protected] | bfdffe2b | 2009-04-24 22:05:35 | [diff] [blame] | 184 | : render_view_host_(render_view_host), |
[email protected] | 7eecaed5 | 2009-05-07 21:44:12 | [diff] [blame] | 185 | delegate_(delegate), |
[email protected] | 811bfe3 | 2009-07-01 08:46:25 | [diff] [blame] | 186 | url_(url), |
[email protected] | 32dda36 | 2009-06-05 19:07:01 | [diff] [blame] | 187 | ALLOW_THIS_IN_INITIALIZER_LIST(peer_(new Peer(this))) { |
[email protected] | 811bfe3 | 2009-07-01 08:46:25 | [diff] [blame] | 188 | all_instances()->insert(this); |
[email protected] | 0f605396 | 2009-07-09 19:26:35 | [diff] [blame] | 189 | |
| 190 | // Ensure the message service is initialized. |
| 191 | ExtensionMessageService::GetInstance(profile()->GetRequestContext())->Init(); |
[email protected] | 4577622 | 2009-07-15 20:21:58 | [diff] [blame^] | 192 | |
| 193 | // Notify the ExtensionProcessManager that the view was created. |
| 194 | ExtensionProcessManager* epm = profile()->GetExtensionProcessManager(); |
| 195 | epm->RegisterExtensionProcess(extension_id(), |
| 196 | render_view_host->process()->pid()); |
[email protected] | bfdffe2b | 2009-04-24 22:05:35 | [diff] [blame] | 197 | } |
| 198 | |
[email protected] | 32dda36 | 2009-06-05 19:07:01 | [diff] [blame] | 199 | ExtensionFunctionDispatcher::~ExtensionFunctionDispatcher() { |
[email protected] | 811bfe3 | 2009-07-01 08:46:25 | [diff] [blame] | 200 | all_instances()->erase(this); |
[email protected] | 32dda36 | 2009-06-05 19:07:01 | [diff] [blame] | 201 | peer_->dispatcher_ = NULL; |
| 202 | } |
| 203 | |
[email protected] | 7eecaed5 | 2009-05-07 21:44:12 | [diff] [blame] | 204 | Browser* ExtensionFunctionDispatcher::GetBrowser() { |
[email protected] | b83e460 | 2009-05-15 22:58:33 | [diff] [blame] | 205 | DCHECK(delegate_); |
| 206 | |
[email protected] | 7eecaed5 | 2009-05-07 21:44:12 | [diff] [blame] | 207 | Browser* retval = delegate_->GetBrowser(); |
[email protected] | 7eecaed5 | 2009-05-07 21:44:12 | [diff] [blame] | 208 | return retval; |
| 209 | } |
| 210 | |
[email protected] | bfdffe2b | 2009-04-24 22:05:35 | [diff] [blame] | 211 | void ExtensionFunctionDispatcher::HandleRequest(const std::string& name, |
| 212 | const std::string& args, |
[email protected] | c661918 | 2009-05-12 14:59:32 | [diff] [blame] | 213 | int request_id, |
| 214 | bool has_callback) { |
[email protected] | 32dda36 | 2009-06-05 19:07:01 | [diff] [blame] | 215 | scoped_refptr<ExtensionFunction> function( |
[email protected] | bfdffe2b | 2009-04-24 22:05:35 | [diff] [blame] | 216 | FactoryRegistry::instance()->NewFunction(name)); |
[email protected] | 32dda36 | 2009-06-05 19:07:01 | [diff] [blame] | 217 | function->set_dispatcher_peer(peer_); |
[email protected] | b83e460 | 2009-05-15 22:58:33 | [diff] [blame] | 218 | function->SetArgs(args); |
[email protected] | c661918 | 2009-05-12 14:59:32 | [diff] [blame] | 219 | function->set_request_id(request_id); |
| 220 | function->set_has_callback(has_callback); |
[email protected] | bfdffe2b | 2009-04-24 22:05:35 | [diff] [blame] | 221 | function->Run(); |
| 222 | } |
| 223 | |
[email protected] | c661918 | 2009-05-12 14:59:32 | [diff] [blame] | 224 | void ExtensionFunctionDispatcher::SendResponse(ExtensionFunction* function, |
| 225 | bool success) { |
[email protected] | c661918 | 2009-05-12 14:59:32 | [diff] [blame] | 226 | render_view_host_->SendExtensionResponse(function->request_id(), success, |
[email protected] | b83e460 | 2009-05-15 22:58:33 | [diff] [blame] | 227 | function->GetResult(), function->GetError()); |
[email protected] | bfdffe2b | 2009-04-24 22:05:35 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | void ExtensionFunctionDispatcher::HandleBadMessage(ExtensionFunction* api) { |
| 231 | LOG(ERROR) << "bad extension message " << // TODO(erikkay) name? |
| 232 | " : terminating renderer."; |
| 233 | if (RenderProcessHost::run_renderer_in_process()) { |
| 234 | // In single process mode it is better if we don't suicide but just crash. |
| 235 | CHECK(false); |
| 236 | } else { |
| 237 | NOTREACHED(); |
| 238 | base::KillProcess(render_view_host_->process()->process().handle(), |
| 239 | ResultCodes::KILLED_BAD_MESSAGE, false); |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | Profile* ExtensionFunctionDispatcher::profile() { |
| 244 | return render_view_host_->process()->profile(); |
| 245 | } |