blob: 12d14ffda3dd60f366d7d960a531804dd4554be4 [file] [log] [blame]
[email protected]64c820732012-01-05 20:50:341// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]bfdffe2b2009-04-24 22:05:352// 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]745feedb2010-08-02 04:08:077#include <map>
8
[email protected]ffbec692012-02-26 20:26:429#include "base/json/json_string_value_serializer.h"
[email protected]3b63f8f42011-03-28 01:54:1510#include "base/memory/ref_counted.h"
11#include "base/memory/singleton.h"
[email protected]bfdffe2b2009-04-24 22:05:3512#include "base/process_util.h"
[email protected]bfdffe2b2009-04-24 22:05:3513#include "base/values.h"
[email protected]17d40f02010-07-01 01:18:0614#include "build/build_config.h"
[email protected]a692a952011-11-28 08:23:5415#include "chrome/browser/accessibility/accessibility_extension_api.h"
[email protected]23d5f172011-10-25 05:49:5316#include "chrome/browser/bookmarks/bookmark_extension_api.h"
17#include "chrome/browser/bookmarks/bookmark_manager_extension_api.h"
[email protected]cdcf60242011-11-04 15:53:4318#include "chrome/browser/download/download_extension_api.h"
[email protected]b7309da2012-01-23 16:03:5119#include "chrome/browser/extensions/api/app/app_api.h"
[email protected]006236a22012-02-24 13:20:1720#include "chrome/browser/extensions/api/browsingdata/browsing_data_api.h"
[email protected]8ef650e2012-02-08 10:20:5921#include "chrome/browser/extensions/api/declarative/declarative_api.h"
[email protected]14a38f3f2012-01-12 16:16:5122#include "chrome/browser/extensions/api/dns/dns_api.h"
[email protected]76aeb1772012-01-20 22:14:1623#include "chrome/browser/extensions/api/permissions/permissions_api.h"
[email protected]223a93f12012-01-18 21:17:5324#include "chrome/browser/extensions/api/serial/serial_api.h"
[email protected]55afeb52011-12-06 00:15:3925#include "chrome/browser/extensions/api/socket/socket_api.h"
[email protected]6e672df2012-01-25 10:39:4726#include "chrome/browser/extensions/api/webrequest/webrequest_api.h"
[email protected]912256b32009-09-18 09:47:3527#include "chrome/browser/extensions/execute_code_in_tab_function.h"
[email protected]efd75992011-12-15 22:42:4228#include "chrome/browser/extensions/extension_activity_log.h"
[email protected]ec9ac0df2009-10-01 18:06:4729#include "chrome/browser/extensions/extension_browser_actions_api.h"
[email protected]cdcf60242011-11-04 15:53:4330#include "chrome/browser/extensions/extension_chrome_auth_private_api.h"
[email protected]fa0624262011-06-09 14:17:3831#include "chrome/browser/extensions/extension_content_settings_api.h"
[email protected]2e3b5202010-03-23 06:52:4132#include "chrome/browser/extensions/extension_context_menu_api.h"
[email protected]898bbd32010-05-18 18:52:2933#include "chrome/browser/extensions/extension_cookies_api.h"
[email protected]91ba3312011-03-17 20:39:2234#include "chrome/browser/extensions/extension_debugger_api.h"
[email protected]caedf60c2012-02-21 04:24:4735#include "chrome/browser/extensions/extension_font_settings_api.h"
[email protected]bfdffe2b2009-04-24 22:05:3536#include "chrome/browser/extensions/extension_function.h"
[email protected]2bb51302011-02-18 22:39:2737#include "chrome/browser/extensions/extension_i18n_api.h"
[email protected]2f69b382011-02-19 00:34:2538#include "chrome/browser/extensions/extension_idle_api.h"
[email protected]cffd7892010-08-26 17:43:2839#include "chrome/browser/extensions/extension_management_api.h"
[email protected]438772df2010-02-26 18:08:4340#include "chrome/browser/extensions/extension_metrics_module.h"
[email protected]a65882c2010-11-12 15:15:0941#include "chrome/browser/extensions/extension_module.h"
[email protected]56ad3792010-05-28 17:45:3342#include "chrome/browser/extensions/extension_omnibox_api.h"
[email protected]f7f3a5f2009-05-01 22:02:3443#include "chrome/browser/extensions/extension_page_actions_module.h"
[email protected]9e4430f2011-11-28 07:43:2944#include "chrome/browser/extensions/extension_page_capture_api.h"
[email protected]598bbcc2011-02-24 10:03:2545#include "chrome/browser/extensions/extension_preference_api.h"
[email protected]381162b2010-01-28 17:29:3546#include "chrome/browser/extensions/extension_processes_api.h"
[email protected]a9c23a52010-08-04 09:13:4447#include "chrome/browser/extensions/extension_proxy_api.h"
[email protected]2f69b382011-02-19 00:34:2548#include "chrome/browser/extensions/extension_service.h"
[email protected]bfdffe2b2009-04-24 22:05:3549#include "chrome/browser/extensions/extension_tabs_module.h"
[email protected]25fd1b2e2009-08-17 20:57:1450#include "chrome/browser/extensions/extension_test_api.h"
[email protected]8abe0a32010-08-12 00:40:2251#include "chrome/browser/extensions/extension_tts_api.h"
[email protected]c63f2b72011-07-07 05:25:0052#include "chrome/browser/extensions/extension_tts_engine_api.h"
[email protected]c6e584c2011-05-18 11:58:4453#include "chrome/browser/extensions/extension_web_socket_proxy_private_api.h"
[email protected]8f9d4eb2011-02-05 01:39:1054#include "chrome/browser/extensions/extension_web_ui.h"
[email protected]557a51dd2011-07-26 12:17:1155#include "chrome/browser/extensions/extension_webnavigation_api.h"
[email protected]63cda0c2010-09-01 04:41:2356#include "chrome/browser/extensions/extension_webstore_private_api.h"
[email protected]d13950e2009-12-04 01:43:0257#include "chrome/browser/extensions/extensions_quota_service.h"
[email protected]a4f98922011-12-06 11:32:2358#include "chrome/browser/extensions/system/system_api.h"
[email protected]83820d42011-11-12 22:03:1159#include "chrome/browser/extensions/process_map.h"
[email protected]ae7f1ba2011-11-28 23:07:3160#include "chrome/browser/extensions/settings/settings_api.h"
[email protected]ed2b1002011-05-25 14:12:1061#include "chrome/browser/external_protocol/external_protocol_handler.h"
[email protected]d2e6bd62011-10-24 20:29:0762#include "chrome/browser/history/history_extension_api.h"
[email protected]5eddc3e2011-10-26 04:33:3163#include "chrome/browser/history/top_sites_extension_api.h"
[email protected]cdcf60242011-11-04 15:53:4364#include "chrome/browser/infobars/infobar_extension_api.h"
[email protected]8ecad5e2010-12-02 21:18:3365#include "chrome/browser/profiles/profile.h"
[email protected]c357acb42011-06-09 20:52:4266#include "chrome/browser/renderer_host/chrome_render_message_filter.h"
[email protected]792556c2011-10-25 05:01:0767#include "chrome/browser/rlz/rlz_extension_api.h"
[email protected]b9971dc2011-11-11 15:50:4368#include "chrome/browser/speech/speech_input_extension_api.h"
[email protected]71b73f02011-04-06 15:57:2969#include "chrome/browser/ui/browser_list.h"
[email protected]00070c732011-04-09 15:31:3370#include "chrome/browser/ui/browser_window.h"
[email protected]83820d42011-11-12 22:03:1171#include "chrome/common/extensions/api/extension_api.h"
[email protected]44c49c92011-03-28 16:17:2372#include "chrome/common/extensions/extension_messages.h"
[email protected]615d88f2011-12-13 01:47:4473#include "chrome/common/extensions/extension_set.h"
[email protected]9c45b7182009-08-04 16:44:4374#include "chrome/common/url_constants.h"
[email protected]5de634712011-03-02 00:20:1975#include "content/browser/renderer_host/render_view_host.h"
[email protected]c333e792012-01-06 16:57:3976#include "content/public/browser/render_process_host.h"
[email protected]f82d57b52011-04-27 19:13:1777#include "ipc/ipc_message.h"
78#include "ipc/ipc_message_macros.h"
[email protected]939856a2010-08-24 20:29:0279#include "third_party/skia/include/core/SkBitmap.h"
[email protected]615d88f2011-12-13 01:47:4480#include "third_party/WebKit/Source/WebKit/chromium/public/WebSecurityOrigin.h"
[email protected]bfdffe2b2009-04-24 22:05:3581
[email protected]8f9d4eb2011-02-05 01:39:1082#if defined(TOOLKIT_VIEWS)
83#include "chrome/browser/extensions/extension_input_api.h"
84#endif
[email protected]d6833852010-08-20 18:00:4585
[email protected]9a2aebe02011-12-02 21:54:1486#if defined(OS_CHROMEOS) && defined(USE_VIRTUAL_KEYBOARD)
[email protected]27072cad2011-05-09 19:46:4087#include "chrome/browser/extensions/extension_input_ui_api.h"
88#endif
89
[email protected]61b55b62011-03-24 09:03:1090#if defined(OS_CHROMEOS)
[email protected]8f427982011-12-13 23:40:2391#include "chrome/browser/extensions/api/terminal/terminal_private_api.h"
[email protected]8baf0dbb2011-11-27 22:47:0292#include "chrome/browser/extensions/extension_info_private_api_chromeos.h"
93#include "chrome/browser/extensions/extension_input_ime_api.h"
94#include "chrome/browser/extensions/extension_input_method_api.h"
[email protected]7df1dad2012-02-27 11:19:5195#include "chrome/browser/chromeos/extensions/file_browser_private_api.h"
96#include "chrome/browser/chromeos/media/media_player_extension_api.h"
[email protected]61b55b62011-03-24 09:03:1097#endif
98
[email protected]83820d42011-11-12 22:03:1199using extensions::ExtensionAPI;
[email protected]615d88f2011-12-13 01:47:44100using WebKit::WebSecurityOrigin;
[email protected]83820d42011-11-12 22:03:11101
[email protected]bfdffe2b2009-04-24 22:05:35102// FactoryRegistry -------------------------------------------------------------
103
104namespace {
105
[email protected]b83e4602009-05-15 22:58:33106// Template for defining ExtensionFunctionFactory.
107template<class T>
108ExtensionFunction* NewExtensionFunction() {
109 return new T();
110}
[email protected]bfdffe2b2009-04-24 22:05:35111
[email protected]b83e4602009-05-15 22:58:33112// Contains a list of all known extension functions and allows clients to
113// create instances of them.
[email protected]bfdffe2b2009-04-24 22:05:35114class FactoryRegistry {
115 public:
[email protected]8e8bb6d2010-12-13 08:18:55116 static FactoryRegistry* GetInstance();
[email protected]b83e4602009-05-15 22:58:33117 FactoryRegistry() { ResetFunctions(); }
118
119 // Resets all functions to their default values.
120 void ResetFunctions();
121
122 // Adds all function names to 'names'.
[email protected]bfdffe2b2009-04-24 22:05:35123 void GetAllNames(std::vector<std::string>* names);
[email protected]b83e4602009-05-15 22:58:33124
125 // Allows overriding of specific functions (e.g. for testing). Functions
126 // must be previously registered. Returns true if successful.
127 bool OverrideFunction(const std::string& name,
128 ExtensionFunctionFactory factory);
129
130 // Factory method for the ExtensionFunction registered as 'name'.
[email protected]bfdffe2b2009-04-24 22:05:35131 ExtensionFunction* NewFunction(const std::string& name);
132
133 private:
[email protected]61424c062009-10-14 23:14:59134 template<class T>
135 void RegisterFunction() {
136 factories_[T::function_name()] = &NewExtensionFunction<T>;
137 }
138
[email protected]bfdffe2b2009-04-24 22:05:35139 typedef std::map<std::string, ExtensionFunctionFactory> FactoryMap;
140 FactoryMap factories_;
141};
142
[email protected]8e8bb6d2010-12-13 08:18:55143FactoryRegistry* FactoryRegistry::GetInstance() {
[email protected]bfdffe2b2009-04-24 22:05:35144 return Singleton<FactoryRegistry>::get();
145}
146
[email protected]b83e4602009-05-15 22:58:33147void FactoryRegistry::ResetFunctions() {
[email protected]bfdffe2b2009-04-24 22:05:35148 // Register all functions here.
149
[email protected]e515f5d2009-05-05 03:05:00150 // Windows
[email protected]61424c062009-10-14 23:14:59151 RegisterFunction<GetWindowFunction>();
152 RegisterFunction<GetCurrentWindowFunction>();
153 RegisterFunction<GetLastFocusedWindowFunction>();
154 RegisterFunction<GetAllWindowsFunction>();
155 RegisterFunction<CreateWindowFunction>();
156 RegisterFunction<UpdateWindowFunction>();
157 RegisterFunction<RemoveWindowFunction>();
[email protected]b83e4602009-05-15 22:58:33158
[email protected]e515f5d2009-05-05 03:05:00159 // Tabs
[email protected]61424c062009-10-14 23:14:59160 RegisterFunction<GetTabFunction>();
[email protected]e3eafb292010-04-14 21:30:41161 RegisterFunction<GetCurrentTabFunction>();
[email protected]61424c062009-10-14 23:14:59162 RegisterFunction<GetSelectedTabFunction>();
163 RegisterFunction<GetAllTabsInWindowFunction>();
[email protected]8c3495c2011-09-28 03:32:30164 RegisterFunction<QueryTabsFunction>();
165 RegisterFunction<HighlightTabsFunction>();
[email protected]61424c062009-10-14 23:14:59166 RegisterFunction<CreateTabFunction>();
167 RegisterFunction<UpdateTabFunction>();
[email protected]8c3495c2011-09-28 03:32:30168 RegisterFunction<MoveTabsFunction>();
[email protected]5d935112011-09-07 09:24:54169 RegisterFunction<ReloadTabFunction>();
[email protected]8c3495c2011-09-28 03:32:30170 RegisterFunction<RemoveTabsFunction>();
[email protected]61424c062009-10-14 23:14:59171 RegisterFunction<DetectTabLanguageFunction>();
172 RegisterFunction<CaptureVisibleTabFunction>();
173 RegisterFunction<TabsExecuteScriptFunction>();
174 RegisterFunction<TabsInsertCSSFunction>();
[email protected]bfdffe2b2009-04-24 22:05:35175
[email protected]f7f3a5f2009-05-01 22:02:34176 // Page Actions.
[email protected]61424c062009-10-14 23:14:59177 RegisterFunction<EnablePageActionFunction>();
178 RegisterFunction<DisablePageActionFunction>();
[email protected]744ef172009-10-16 21:53:46179 RegisterFunction<PageActionShowFunction>();
180 RegisterFunction<PageActionHideFunction>();
181 RegisterFunction<PageActionSetIconFunction>();
182 RegisterFunction<PageActionSetTitleFunction>();
[email protected]e478d6702010-01-28 00:10:29183 RegisterFunction<PageActionSetPopupFunction>();
[email protected]f7f3a5f2009-05-01 22:02:34184
[email protected]ec9ac0df2009-10-01 18:06:47185 // Browser Actions.
[email protected]61424c062009-10-14 23:14:59186 RegisterFunction<BrowserActionSetIconFunction>();
[email protected]1288ba02009-10-15 00:02:24187 RegisterFunction<BrowserActionSetTitleFunction>();
[email protected]61424c062009-10-14 23:14:59188 RegisterFunction<BrowserActionSetBadgeTextFunction>();
189 RegisterFunction<BrowserActionSetBadgeBackgroundColorFunction>();
[email protected]85ae9592010-02-03 20:58:50190 RegisterFunction<BrowserActionSetPopupFunction>();
[email protected]ec9ac0df2009-10-01 18:06:47191
[email protected]6093d742011-08-26 16:37:18192 // Browsing Data.
[email protected]8f5090a52012-02-10 11:36:21193 RegisterFunction<RemoveBrowsingDataFunction>();
194 RegisterFunction<RemoveAppCacheFunction>();
195 RegisterFunction<RemoveCacheFunction>();
196 RegisterFunction<RemoveCookiesFunction>();
197 RegisterFunction<RemoveDownloadsFunction>();
198 RegisterFunction<RemoveFileSystemsFunction>();
199 RegisterFunction<RemoveFormDataFunction>();
200 RegisterFunction<RemoveHistoryFunction>();
201 RegisterFunction<RemoveIndexedDBFunction>();
202 RegisterFunction<RemoveLocalStorageFunction>();
203 RegisterFunction<RemoveOriginBoundCertsFunction>();
204 RegisterFunction<RemovePluginDataFunction>();
205 RegisterFunction<RemovePasswordsFunction>();
206 RegisterFunction<RemoveWebSQLFunction>();
[email protected]6093d742011-08-26 16:37:18207
[email protected]f7f3a5f2009-05-01 22:02:34208 // Bookmarks.
[email protected]61424c062009-10-14 23:14:59209 RegisterFunction<GetBookmarksFunction>();
210 RegisterFunction<GetBookmarkChildrenFunction>();
[email protected]a3c94c712009-12-18 19:23:55211 RegisterFunction<GetBookmarkRecentFunction>();
[email protected]61424c062009-10-14 23:14:59212 RegisterFunction<GetBookmarkTreeFunction>();
[email protected]532a8c62011-06-03 21:30:01213 RegisterFunction<GetBookmarkSubTreeFunction>();
[email protected]61424c062009-10-14 23:14:59214 RegisterFunction<SearchBookmarksFunction>();
215 RegisterFunction<RemoveBookmarkFunction>();
216 RegisterFunction<RemoveTreeBookmarkFunction>();
217 RegisterFunction<CreateBookmarkFunction>();
218 RegisterFunction<MoveBookmarkFunction>();
219 RegisterFunction<UpdateBookmarkFunction>();
[email protected]9c45b7182009-08-04 16:44:43220
[email protected]f34e79632010-03-17 02:34:08221 // Infobars.
222 RegisterFunction<ShowInfoBarFunction>();
223
[email protected]9dd97bc2010-01-14 01:40:04224 // BookmarkManager
225 RegisterFunction<CopyBookmarkManagerFunction>();
226 RegisterFunction<CutBookmarkManagerFunction>();
227 RegisterFunction<PasteBookmarkManagerFunction>();
[email protected]03b3bbf2010-01-29 23:54:57228 RegisterFunction<CanPasteBookmarkManagerFunction>();
[email protected]cb6cf792010-01-28 00:04:56229 RegisterFunction<ImportBookmarksFunction>();
230 RegisterFunction<ExportBookmarksFunction>();
[email protected]d406e2e2010-01-30 21:45:18231 RegisterFunction<SortChildrenBookmarkManagerFunction>();
[email protected]9dd97bc2010-01-14 01:40:04232 RegisterFunction<BookmarkManagerGetStringsFunction>();
[email protected]ced90ae12010-02-20 02:06:16233 RegisterFunction<StartDragBookmarkManagerFunction>();
234 RegisterFunction<DropBookmarkManagerFunction>();
[email protected]9b071852010-04-02 06:45:31235 RegisterFunction<GetSubtreeBookmarkManagerFunction>();
[email protected]933ebbe2011-06-30 12:24:01236 RegisterFunction<CanEditBookmarkManagerFunction>();
[email protected]9dd97bc2010-01-14 01:40:04237
[email protected]de768a832009-10-30 05:25:01238 // History
239 RegisterFunction<AddUrlHistoryFunction>();
240 RegisterFunction<DeleteAllHistoryFunction>();
241 RegisterFunction<DeleteRangeHistoryFunction>();
242 RegisterFunction<DeleteUrlHistoryFunction>();
243 RegisterFunction<GetVisitsHistoryFunction>();
244 RegisterFunction<SearchHistoryFunction>();
245
[email protected]f5205412010-03-16 00:19:34246 // Idle
247 RegisterFunction<ExtensionIdleQueryStateFunction>();
248
[email protected]198bcfe2009-09-09 22:56:28249 // I18N.
[email protected]61424c062009-10-14 23:14:59250 RegisterFunction<GetAcceptLanguagesFunction>();
[email protected]198bcfe2009-09-09 22:56:28251
[email protected]381162b2010-01-28 17:29:35252 // Processes.
[email protected]8a661f82010-10-19 21:47:11253 RegisterFunction<GetProcessIdForTabFunction>();
[email protected]381162b2010-01-28 17:29:35254
[email protected]438772df2010-02-26 18:08:43255 // Metrics.
[email protected]cf25e4d2010-03-12 21:19:34256 RegisterFunction<MetricsRecordUserActionFunction>();
257 RegisterFunction<MetricsRecordValueFunction>();
258 RegisterFunction<MetricsRecordPercentageFunction>();
259 RegisterFunction<MetricsRecordCountFunction>();
260 RegisterFunction<MetricsRecordSmallCountFunction>();
261 RegisterFunction<MetricsRecordMediumCountFunction>();
262 RegisterFunction<MetricsRecordTimeFunction>();
263 RegisterFunction<MetricsRecordMediumTimeFunction>();
264 RegisterFunction<MetricsRecordLongTimeFunction>();
[email protected]438772df2010-02-26 18:08:43265
[email protected]17d40f02010-07-01 01:18:06266 // RLZ.
267#if defined(OS_WIN)
268 RegisterFunction<RlzRecordProductEventFunction>();
269 RegisterFunction<RlzGetAccessPointRlzFunction>();
[email protected]3a612be2010-07-17 03:11:21270 RegisterFunction<RlzSendFinancialPingFunction>();
[email protected]17d40f02010-07-01 01:18:06271 RegisterFunction<RlzClearProductStateFunction>();
272#endif
273
[email protected]898bbd32010-05-18 18:52:29274 // Cookies.
275 RegisterFunction<GetCookieFunction>();
276 RegisterFunction<GetAllCookiesFunction>();
277 RegisterFunction<SetCookieFunction>();
278 RegisterFunction<RemoveCookieFunction>();
279 RegisterFunction<GetAllCookieStoresFunction>();
280
[email protected]25fd1b2e2009-08-17 20:57:14281 // Test.
[email protected]61424c062009-10-14 23:14:59282 RegisterFunction<ExtensionTestPassFunction>();
283 RegisterFunction<ExtensionTestFailFunction>();
284 RegisterFunction<ExtensionTestLogFunction>();
[email protected]d13950e2009-12-04 01:43:02285 RegisterFunction<ExtensionTestQuotaResetFunction>();
[email protected]db7331a2010-02-25 22:10:50286 RegisterFunction<ExtensionTestCreateIncognitoTabFunction>();
[email protected]745feedb2010-08-02 04:08:07287 RegisterFunction<ExtensionTestSendMessageFunction>();
[email protected]761e7162010-10-18 19:26:39288 RegisterFunction<ExtensionTestGetConfigFunction>();
[email protected]5cbe1e22010-01-30 01:18:56289
290 // Accessibility.
291 RegisterFunction<GetFocusedControlFunction>();
292 RegisterFunction<SetAccessibilityEnabledFunction>();
[email protected]cde158b2011-12-21 10:22:49293 RegisterFunction<GetAlertsForTabFunction>();
[email protected]446255952010-03-17 20:41:58294
[email protected]8abe0a32010-08-12 00:40:22295 // Text-to-speech.
[email protected]c63f2b72011-07-07 05:25:00296 RegisterFunction<ExtensionTtsEngineSendTtsEventFunction>();
297 RegisterFunction<ExtensionTtsGetVoicesFunction>();
298 RegisterFunction<ExtensionTtsIsSpeakingFunction>();
[email protected]8abe0a32010-08-12 00:40:22299 RegisterFunction<ExtensionTtsSpeakFunction>();
300 RegisterFunction<ExtensionTtsStopSpeakingFunction>();
[email protected]8abe0a32010-08-12 00:40:22301
[email protected]2e3b5202010-03-23 06:52:41302 // Context Menus.
303 RegisterFunction<CreateContextMenuFunction>();
[email protected]66dbfb2c2010-05-12 20:20:15304 RegisterFunction<UpdateContextMenuFunction>();
[email protected]2e3b5202010-03-23 06:52:41305 RegisterFunction<RemoveContextMenuFunction>();
[email protected]66dbfb2c2010-05-12 20:20:15306 RegisterFunction<RemoveAllContextMenusFunction>();
[email protected]56ad3792010-05-28 17:45:33307
308 // Omnibox.
309 RegisterFunction<OmniboxSendSuggestionsFunction>();
[email protected]1bead0712010-11-27 17:41:53310 RegisterFunction<OmniboxSetDefaultSuggestionFunction>();
[email protected]a9c23a52010-08-04 09:13:44311
[email protected]e06a1fe2011-09-29 21:20:44312 // Speech input.
313 RegisterFunction<StartSpeechInputFunction>();
314 RegisterFunction<StopSpeechInputFunction>();
315 RegisterFunction<IsRecordingSpeechInputFunction>();
316
[email protected]d6833852010-08-20 18:00:45317#if defined(TOOLKIT_VIEWS)
318 // Input.
319 RegisterFunction<SendKeyboardEventInputFunction>();
320#endif
[email protected]cffd7892010-08-26 17:43:28321
[email protected]2cc81d32011-10-04 17:03:18322#if defined(USE_VIRTUAL_KEYBOARD)
[email protected]bcde7482011-05-23 19:29:12323 RegisterFunction<HideKeyboardFunction>();
[email protected]3c8faef2011-06-09 22:56:51324 RegisterFunction<SetKeyboardHeightFunction>();
[email protected]bcde7482011-05-23 19:29:12325#endif
326
[email protected]14da67b02011-08-02 05:49:13327#if defined(OS_CHROMEOS)
[email protected]27072cad2011-05-09 19:46:40328 // IME
[email protected]14da67b02011-08-02 05:49:13329 RegisterFunction<SetCompositionFunction>();
330 RegisterFunction<ClearCompositionFunction>();
331 RegisterFunction<CommitTextFunction>();
332 RegisterFunction<SetCandidateWindowPropertiesFunction>();
333 RegisterFunction<SetCandidatesFunction>();
334 RegisterFunction<SetCursorPositionFunction>();
335 RegisterFunction<SetMenuItemsFunction>();
336 RegisterFunction<UpdateMenuItemsFunction>();
[email protected]cd0b04c92011-09-08 05:18:06337
338 RegisterFunction<InputEventHandled>();
[email protected]9a2aebe02011-12-02 21:54:14339#if defined(USE_VIRTUAL_KEYBOARD)
[email protected]27072cad2011-05-09 19:46:40340 RegisterFunction<CandidateClickedInputUiFunction>();
341 RegisterFunction<CursorUpInputUiFunction>();
342 RegisterFunction<CursorDownInputUiFunction>();
343 RegisterFunction<PageUpInputUiFunction>();
344 RegisterFunction<PageDownInputUiFunction>();
345 RegisterFunction<RegisterInputUiFunction>();
346 RegisterFunction<PageUpInputUiFunction>();
347 RegisterFunction<PageDownInputUiFunction>();
348#endif
[email protected]14da67b02011-08-02 05:49:13349#endif
[email protected]27072cad2011-05-09 19:46:40350
[email protected]cffd7892010-08-26 17:43:28351 // Management.
352 RegisterFunction<GetAllExtensionsFunction>();
[email protected]e78543c2010-11-08 22:05:23353 RegisterFunction<GetExtensionByIdFunction>();
[email protected]925a54d2011-07-29 17:21:00354 RegisterFunction<GetPermissionWarningsByIdFunction>();
355 RegisterFunction<GetPermissionWarningsByManifestFunction>();
[email protected]e558ff82010-09-23 20:19:56356 RegisterFunction<LaunchAppFunction>();
[email protected]cffd7892010-08-26 17:43:28357 RegisterFunction<SetEnabledFunction>();
[email protected]cffd7892010-08-26 17:43:28358 RegisterFunction<UninstallFunction>();
[email protected]63cda0c2010-09-01 04:41:23359
[email protected]a65882c2010-11-12 15:15:09360 // Extension module.
361 RegisterFunction<SetUpdateUrlDataFunction>();
[email protected]c7c401d2011-03-16 10:20:01362 RegisterFunction<IsAllowedIncognitoAccessFunction>();
363 RegisterFunction<IsAllowedFileSchemeAccessFunction>();
[email protected]a65882c2010-11-12 15:15:09364
[email protected]63cda0c2010-09-01 04:41:23365 // WebstorePrivate.
[email protected]d6885592010-10-11 19:38:24366 RegisterFunction<GetBrowserLoginFunction>();
[email protected]63cda0c2010-09-01 04:41:23367 RegisterFunction<GetStoreLoginFunction>();
368 RegisterFunction<SetStoreLoginFunction>();
[email protected]bc070bf2012-02-23 16:27:57369 RegisterFunction<InstallBundleFunction>();
[email protected]5349ac6d2011-04-05 22:20:17370 RegisterFunction<BeginInstallWithManifestFunction>();
[email protected]b1f04cc2010-11-10 22:59:30371 RegisterFunction<CompleteInstallFunction>();
[email protected]f66a50a2011-11-02 23:53:46372 RegisterFunction<SilentlyInstallFunction>();
[email protected]979b69c2011-12-08 23:19:55373 RegisterFunction<GetWebGLStatusFunction>();
[email protected]c41fe662011-02-15 01:19:26374
[email protected]557a51dd2011-07-26 12:17:11375 // WebNavigation.
376 RegisterFunction<GetFrameFunction>();
[email protected]5a130672011-09-09 08:42:29377 RegisterFunction<GetAllFramesFunction>();
[email protected]557a51dd2011-07-26 12:17:11378
[email protected]c41fe662011-02-15 01:19:26379 // WebRequest.
380 RegisterFunction<WebRequestAddEventListener>();
[email protected]05cc4e72011-03-08 21:29:48381 RegisterFunction<WebRequestEventHandled>();
[email protected]e81d4d72011-09-29 16:54:31382 RegisterFunction<WebRequestHandlerBehaviorChanged>();
[email protected]598bbcc2011-02-24 10:03:25383
384 // Preferences.
385 RegisterFunction<GetPreferenceFunction>();
386 RegisterFunction<SetPreferenceFunction>();
[email protected]c433bcb2011-02-24 13:10:27387 RegisterFunction<ClearPreferenceFunction>();
[email protected]91ba3312011-03-17 20:39:22388
[email protected]b6b805e92011-04-16 09:24:14389 // ChromeOS-specific part of the API.
390#if defined(OS_CHROMEOS)
391 // Device Customization.
392 RegisterFunction<GetChromeosInfoFunction>();
393
394 // FileBrowserPrivate functions.
[email protected]04ef5152011-11-18 04:05:25395 // TODO(jamescook): Expose these on non-ChromeOS platforms so we can use
396 // the extension-based file picker on Aura. crbug.com/97424
[email protected]94bda202011-04-18 23:31:00397 RegisterFunction<CancelFileDialogFunction>();
[email protected]b6b805e92011-04-16 09:24:14398 RegisterFunction<ExecuteTasksFileBrowserFunction>();
[email protected]94bda202011-04-18 23:31:00399 RegisterFunction<FileDialogStringsFunction>();
[email protected]b6b805e92011-04-16 09:24:14400 RegisterFunction<GetFileTasksFileBrowserFunction>();
[email protected]61334fa2011-06-16 01:01:40401 RegisterFunction<GetVolumeMetadataFunction>();
[email protected]b6b805e92011-04-16 09:24:14402 RegisterFunction<RequestLocalFileSystemFunction>();
[email protected]a6d06642011-06-02 05:55:23403 RegisterFunction<AddFileWatchBrowserFunction>();
404 RegisterFunction<RemoveFileWatchBrowserFunction>();
[email protected]9d4c2c52011-04-07 18:53:10405 RegisterFunction<SelectFileFunction>();
406 RegisterFunction<SelectFilesFunction>();
[email protected]928ae8852011-07-26 01:15:03407 RegisterFunction<AddMountFunction>();
408 RegisterFunction<RemoveMountFunction>();
409 RegisterFunction<GetMountPointsFunction>();
[email protected]694cafac2011-09-21 17:41:53410 RegisterFunction<GetSizeStatsFunction>();
[email protected]3fd9004c2011-08-12 00:58:46411 RegisterFunction<FormatDeviceFunction>();
[email protected]94bda202011-04-18 23:31:00412 RegisterFunction<ViewFilesFunction>();
[email protected]bce94592012-03-02 14:02:01413 RegisterFunction<ToggleFullscreenFunction>();
414 RegisterFunction<IsFullscreenFunction>();
[email protected]f1852b52011-05-17 04:58:08415
[email protected]ce9802042011-05-27 10:11:40416 // Mediaplayer
[email protected]7df1dad2012-02-27 11:19:51417 RegisterFunction<PlayMediaplayerFunction>();
[email protected]ce9802042011-05-27 10:11:40418 RegisterFunction<GetPlaylistMediaplayerFunction>();
[email protected]7df1dad2012-02-27 11:19:51419 RegisterFunction<SetWindowHeightMediaplayerFunction>();
420 RegisterFunction<CloseWindowMediaplayerFunction>();
[email protected]be5bd5742011-07-14 07:21:01421
422 // InputMethod
423 RegisterFunction<GetInputMethodFunction>();
424
[email protected]8f427982011-12-13 23:40:23425 // Terminal
426 RegisterFunction<OpenTerminalProcessFunction>();
427 RegisterFunction<SendInputToTerminalProcessFunction>();
428 RegisterFunction<CloseTerminalProcessFunction>();
[email protected]0d2786c2012-02-14 02:36:31429 RegisterFunction<OnTerminalResizeFunction>();
[email protected]8f427982011-12-13 23:40:23430
[email protected]9a2aebe02011-12-02 21:54:14431#if defined(USE_VIRTUAL_KEYBOARD)
[email protected]f1852b52011-05-17 04:58:08432 // Input
433 RegisterFunction<SendHandwritingStrokeFunction>();
434 RegisterFunction<CancelHandwritingStrokesFunction>();
435#endif
[email protected]61b55b62011-03-24 09:03:10436#endif
437
[email protected]c6e584c2011-05-18 11:58:44438 // Websocket to TCP proxy. Currently noop on anything other than ChromeOS.
439 RegisterFunction<WebSocketProxyPrivateGetPassportForTCPFunction>();
[email protected]5ddffb82011-10-14 17:48:07440 RegisterFunction<WebSocketProxyPrivateGetURLForTCPFunction>();
[email protected]c6e584c2011-05-18 11:58:44441
[email protected]91ba3312011-03-17 20:39:22442 // Debugger
443 RegisterFunction<AttachDebuggerFunction>();
444 RegisterFunction<DetachDebuggerFunction>();
[email protected]c7580b62011-10-26 08:46:30445 RegisterFunction<SendCommandDebuggerFunction>();
[email protected]fa0624262011-06-09 14:17:38446
[email protected]b7f853e282011-08-10 09:24:20447 // Settings
[email protected]0d9a2202011-11-09 13:48:41448 RegisterFunction<extensions::GetSettingsFunction>();
449 RegisterFunction<extensions::SetSettingsFunction>();
450 RegisterFunction<extensions::RemoveSettingsFunction>();
451 RegisterFunction<extensions::ClearSettingsFunction>();
[email protected]7b4989f2012-01-29 11:19:11452 RegisterFunction<extensions::GetBytesInUseSettingsFunction>();
[email protected]b7f853e282011-08-10 09:24:20453
[email protected]fa0624262011-06-09 14:17:38454 // Content settings.
[email protected]49ba4822011-06-15 18:41:56455 RegisterFunction<GetResourceIdentifiersFunction>();
[email protected]fa0624262011-06-09 14:17:38456 RegisterFunction<ClearContentSettingsFunction>();
457 RegisterFunction<GetContentSettingFunction>();
458 RegisterFunction<SetContentSettingFunction>();
[email protected]c1d05aa2011-06-28 02:07:30459
[email protected]caedf60c2012-02-21 04:24:47460 // Font settings.
461 RegisterFunction<GetFontNameFunction>();
[email protected]a3614892012-02-29 06:16:17462 RegisterFunction<SetFontNameFunction>();
[email protected]caedf60c2012-02-21 04:24:47463
[email protected]be9d9c82011-07-13 04:17:31464 // ChromeAuth settings.
465 RegisterFunction<SetCloudPrintCredentialsFunction>();
466
[email protected]c1d05aa2011-06-28 02:07:30467 // Experimental App API.
468 RegisterFunction<AppNotifyFunction>();
469 RegisterFunction<AppClearAllNotificationsFunction>();
[email protected]902fd7b2011-07-27 18:42:31470
471 // Permissions
472 RegisterFunction<ContainsPermissionsFunction>();
473 RegisterFunction<GetAllPermissionsFunction>();
474 RegisterFunction<RemovePermissionsFunction>();
475 RegisterFunction<RequestPermissionsFunction>();
[email protected]21d7a4e302011-08-15 16:17:12476
477 // Downloads
478 RegisterFunction<DownloadsDownloadFunction>();
479 RegisterFunction<DownloadsSearchFunction>();
480 RegisterFunction<DownloadsPauseFunction>();
481 RegisterFunction<DownloadsResumeFunction>();
482 RegisterFunction<DownloadsCancelFunction>();
483 RegisterFunction<DownloadsEraseFunction>();
484 RegisterFunction<DownloadsSetDestinationFunction>();
485 RegisterFunction<DownloadsAcceptDangerFunction>();
486 RegisterFunction<DownloadsShowFunction>();
487 RegisterFunction<DownloadsDragFunction>();
[email protected]45e4fab22012-01-09 19:38:02488 RegisterFunction<DownloadsGetFileIconFunction>();
[email protected]3a605e82011-09-26 17:26:16489
[email protected]9e4430f2011-11-28 07:43:29490 // PageCapture
491 RegisterFunction<PageCaptureSaveAsMHTMLFunction>();
[email protected]6a16dc9b2011-10-17 18:28:16492
[email protected]5eddc3e2011-10-26 04:33:31493 // TopSites
494 RegisterFunction<GetTopSitesFunction>();
[email protected]ae7f1ba2011-11-28 23:07:31495
[email protected]223a93f12012-01-18 21:17:53496 // Serial
497 RegisterFunction<extensions::SerialOpenFunction>();
498 RegisterFunction<extensions::SerialCloseFunction>();
[email protected]58edca52012-02-16 21:51:38499 RegisterFunction<extensions::SerialReadFunction>();
500 RegisterFunction<extensions::SerialWriteFunction>();
[email protected]223a93f12012-01-18 21:17:53501
[email protected]ae7f1ba2011-11-28 23:07:31502 // Sockets
503 RegisterFunction<extensions::SocketCreateFunction>();
[email protected]55afeb52011-12-06 00:15:39504 RegisterFunction<extensions::SocketDestroyFunction>();
[email protected]ae7f1ba2011-11-28 23:07:31505 RegisterFunction<extensions::SocketConnectFunction>();
[email protected]d47c11f2012-01-26 18:15:04506 RegisterFunction<extensions::SocketDisconnectFunction>();
[email protected]64c820732012-01-05 20:50:34507 RegisterFunction<extensions::SocketReadFunction>();
[email protected]55afeb52011-12-06 00:15:39508 RegisterFunction<extensions::SocketWriteFunction>();
[email protected]a4f98922011-12-06 11:32:23509
[email protected]14a38f3f2012-01-12 16:16:51510 // DNS
511 RegisterFunction<extensions::DNSResolveFunction>();
512
[email protected]a4f98922011-12-06 11:32:23513 // System
514 RegisterFunction<extensions::GetIncognitoModeAvailabilityFunction>();
[email protected]1723bdc22011-12-15 05:04:18515 RegisterFunction<extensions::GetUpdateStatusFunction>();
[email protected]8ef650e2012-02-08 10:20:59516
517 // Net
518 RegisterFunction<extensions::AddRulesFunction>();
519 RegisterFunction<extensions::RemoveRulesFunction>();
520 RegisterFunction<extensions::GetRulesFunction>();
[email protected]bfdffe2b2009-04-24 22:05:35521}
522
[email protected]b83e4602009-05-15 22:58:33523void FactoryRegistry::GetAllNames(std::vector<std::string>* names) {
524 for (FactoryMap::iterator iter = factories_.begin();
525 iter != factories_.end(); ++iter) {
[email protected]bfdffe2b2009-04-24 22:05:35526 names->push_back(iter->first);
527 }
528}
529
[email protected]b83e4602009-05-15 22:58:33530bool FactoryRegistry::OverrideFunction(const std::string& name,
531 ExtensionFunctionFactory factory) {
532 FactoryMap::iterator iter = factories_.find(name);
533 if (iter == factories_.end()) {
534 return false;
535 } else {
536 iter->second = factory;
537 return true;
538 }
539}
540
[email protected]bfdffe2b2009-04-24 22:05:35541ExtensionFunction* FactoryRegistry::NewFunction(const std::string& name) {
542 FactoryMap::iterator iter = factories_.find(name);
543 DCHECK(iter != factories_.end());
[email protected]b83e4602009-05-15 22:58:33544 ExtensionFunction* function = iter->second();
[email protected]76a3db852009-07-24 02:14:56545 function->set_name(name);
[email protected]b83e4602009-05-15 22:58:33546 return function;
[email protected]bfdffe2b2009-04-24 22:05:35547}
548
[email protected]efd75992011-12-15 22:42:42549const char kAccessDenied[] = "access denied";
550const char kQuotaExceeded[] = "quota exceeded";
551
552void LogSuccess(const Extension* extension,
553 const ExtensionHostMsg_Request_Params& params) {
554 ExtensionActivityLog* extension_activity_log =
555 ExtensionActivityLog::GetInstance();
556 if (extension_activity_log->HasObservers(extension)) {
557 std::string call_signature = params.name + "(";
558 ListValue::const_iterator it = params.arguments.begin();
559 for (; it != params.arguments.end(); ++it) {
560 std::string arg;
561 JSONStringValueSerializer serializer(&arg);
562 if (serializer.SerializeAndOmitBinaryValues(**it)) {
563 if (it != params.arguments.begin())
564 call_signature += ", ";
565 call_signature += arg;
566 }
567 }
568 call_signature += ")";
569
570 extension_activity_log->Log(
571 extension,
572 ExtensionActivityLog::ACTIVITY_EXTENSION_API_CALL,
573 call_signature);
574 }
575}
576
577void LogFailure(const Extension* extension,
578 const std::string& func_name,
579 const char* reason) {
580 ExtensionActivityLog* extension_activity_log =
581 ExtensionActivityLog::GetInstance();
582 if (extension_activity_log->HasObservers(extension)) {
583 extension_activity_log->Log(
584 extension,
585 ExtensionActivityLog::ACTIVITY_EXTENSION_API_BLOCK,
586 func_name + ": " + reason);
587 }
588}
589
[email protected]b83e4602009-05-15 22:58:33590}; // namespace
[email protected]bfdffe2b2009-04-24 22:05:35591
592// ExtensionFunctionDispatcher -------------------------------------------------
593
594void ExtensionFunctionDispatcher::GetAllFunctionNames(
595 std::vector<std::string>* names) {
[email protected]8e8bb6d2010-12-13 08:18:55596 FactoryRegistry::GetInstance()->GetAllNames(names);
[email protected]bfdffe2b2009-04-24 22:05:35597}
598
[email protected]b83e4602009-05-15 22:58:33599bool ExtensionFunctionDispatcher::OverrideFunction(
600 const std::string& name, ExtensionFunctionFactory factory) {
[email protected]8e8bb6d2010-12-13 08:18:55601 return FactoryRegistry::GetInstance()->OverrideFunction(name, factory);
[email protected]b83e4602009-05-15 22:58:33602}
603
604void ExtensionFunctionDispatcher::ResetFunctions() {
[email protected]8e8bb6d2010-12-13 08:18:55605 FactoryRegistry::GetInstance()->ResetFunctions();
[email protected]b83e4602009-05-15 22:58:33606}
607
[email protected]c357acb42011-06-09 20:52:42608// static
609void ExtensionFunctionDispatcher::DispatchOnIOThread(
[email protected]fd50e7b2011-11-03 09:20:25610 ExtensionInfoMap* extension_info_map,
[email protected]673514522011-07-13 18:17:18611 void* profile,
[email protected]c357acb42011-06-09 20:52:42612 int render_process_id,
613 base::WeakPtr<ChromeRenderMessageFilter> ipc_sender,
614 int routing_id,
615 const ExtensionHostMsg_Request_Params& params) {
616 const Extension* extension =
[email protected]83820d42011-11-12 22:03:11617 extension_info_map->extensions().GetByID(params.extension_id);
[email protected]c357acb42011-06-09 20:52:42618
[email protected]6f371442011-11-09 06:45:46619 scoped_refptr<ExtensionFunction> function(
620 CreateExtensionFunction(params, extension, render_process_id,
621 extension_info_map->process_map(), profile,
622 ipc_sender, routing_id));
[email protected]efd75992011-12-15 22:42:42623 if (!function) {
624 LogFailure(extension, params.name, kAccessDenied);
[email protected]c357acb42011-06-09 20:52:42625 return;
[email protected]efd75992011-12-15 22:42:42626 }
[email protected]c357acb42011-06-09 20:52:42627
628 IOThreadExtensionFunction* function_io =
629 function->AsIOThreadExtensionFunction();
630 if (!function_io) {
631 NOTREACHED();
632 return;
633 }
634 function_io->set_ipc_sender(ipc_sender, routing_id);
635 function_io->set_extension_info_map(extension_info_map);
636 function->set_include_incognito(
637 extension_info_map->IsIncognitoEnabled(extension->id()));
[email protected]fd50e7b2011-11-03 09:20:25638
639 ExtensionsQuotaService* quota = extension_info_map->quota_service();
640 if (quota->Assess(extension->id(), function, &params.arguments,
641 base::TimeTicks::Now())) {
642 function->Run();
[email protected]efd75992011-12-15 22:42:42643 LogSuccess(extension, params);
[email protected]fd50e7b2011-11-03 09:20:25644 } else {
645 function->OnQuotaExceeded();
[email protected]efd75992011-12-15 22:42:42646 LogFailure(extension, params.name, kQuotaExceeded);
[email protected]fd50e7b2011-11-03 09:20:25647 }
[email protected]c357acb42011-06-09 20:52:42648}
649
[email protected]c5dbef02011-05-13 05:06:09650ExtensionFunctionDispatcher::ExtensionFunctionDispatcher(Profile* profile,
651 Delegate* delegate)
652 : profile_(profile),
[email protected]55ce330712011-05-24 19:04:27653 delegate_(delegate) {
[email protected]bfdffe2b2009-04-24 22:05:35654}
655
[email protected]32dda362009-06-05 19:07:01656ExtensionFunctionDispatcher::~ExtensionFunctionDispatcher() {
[email protected]32dda362009-06-05 19:07:01657}
658
[email protected]0ec92032010-03-24 19:59:41659Browser* ExtensionFunctionDispatcher::GetCurrentBrowser(
[email protected]c5dbef02011-05-13 05:06:09660 RenderViewHost* render_view_host, bool include_incognito) {
[email protected]0ec92032010-03-24 19:59:41661 Browser* browser = delegate_->GetBrowser();
[email protected]7eecaed52009-05-07 21:44:12662
[email protected]bc535ee52010-08-31 18:40:32663 // If the delegate has an associated browser, that is always the right answer.
664 if (browser)
665 return browser;
[email protected]9c45b7182009-08-04 16:44:43666
[email protected]bc535ee52010-08-31 18:40:32667 // Otherwise, try to default to a reasonable browser. If |include_incognito|
668 // is true, we will also search browsers in the incognito version of this
669 // profile. Note that the profile may already be incognito, in which case
670 // we will search the incognito version only, regardless of the value of
671 // |include_incognito|.
[email protected]9b62ecf2011-07-27 20:23:08672 Profile* profile = Profile::FromBrowserContext(
[email protected]f3b1a082011-11-18 00:34:30673 render_view_host->process()->GetBrowserContext());
[email protected]8ecca492012-01-19 17:29:42674 browser = BrowserList::FindAnyBrowser(profile, include_incognito);
[email protected]0ec92032010-03-24 19:59:41675
[email protected]0ec92032010-03-24 19:59:41676 // NOTE(rafaelw): This can return NULL in some circumstances. In particular,
[email protected]6d7a6042010-08-12 20:12:42677 // a background_page onload chrome.tabs api call can make it into here
678 // before the browser is sufficiently initialized to return here.
[email protected]0ec92032010-03-24 19:59:41679 // A similar situation may arise during shutdown.
680 // TODO(rafaelw): Delay creation of background_page until the browser
681 // is available. http://code.google.com/p/chromium/issues/detail?id=13284
682 return browser;
[email protected]b27257562009-11-16 23:28:26683}
684
[email protected]c5dbef02011-05-13 05:06:09685void ExtensionFunctionDispatcher::Dispatch(
686 const ExtensionHostMsg_Request_Params& params,
687 RenderViewHost* render_view_host) {
[email protected]c5dbef02011-05-13 05:06:09688 ExtensionService* service = profile()->GetExtensionService();
[email protected]6f371442011-11-09 06:45:46689 extensions::ProcessMap* process_map = service->process_map();
690 if (!service || !process_map)
[email protected]c5dbef02011-05-13 05:06:09691 return;
692
[email protected]615d88f2011-12-13 01:47:44693 const Extension* extension = service->extensions()->GetByID(
694 params.extension_id);
[email protected]c5dbef02011-05-13 05:06:09695 if (!extension)
[email protected]615d88f2011-12-13 01:47:44696 extension = service->extensions()->GetHostedAppByURL(ExtensionURLInfo(
697 WebSecurityOrigin::createFromString(params.source_origin),
698 params.source_url));
[email protected]c5dbef02011-05-13 05:06:09699
[email protected]8add5412011-10-01 21:02:14700 scoped_refptr<ExtensionFunction> function(
[email protected]6f371442011-11-09 06:45:46701 CreateExtensionFunction(params, extension,
[email protected]f3b1a082011-11-18 00:34:30702 render_view_host->process()->GetID(),
[email protected]6f371442011-11-09 06:45:46703 *(service->process_map()),
704 profile(), render_view_host,
[email protected]8add5412011-10-01 21:02:14705 render_view_host->routing_id()));
[email protected]efd75992011-12-15 22:42:42706 if (!function) {
707 LogFailure(extension, params.name, kAccessDenied);
[email protected]f82d57b52011-04-27 19:13:17708 return;
[email protected]efd75992011-12-15 22:42:42709 }
[email protected]f82d57b52011-04-27 19:13:17710
[email protected]a2aef2e2011-05-26 22:48:12711 UIThreadExtensionFunction* function_ui =
712 function->AsUIThreadExtensionFunction();
713 if (!function_ui) {
714 NOTREACHED();
715 return;
716 }
717 function_ui->SetRenderViewHost(render_view_host);
718 function_ui->set_dispatcher(AsWeakPtr());
719 function_ui->set_profile(profile_);
[email protected]2a8f24e2010-11-03 21:37:05720 function->set_include_incognito(service->CanCrossIncognito(extension));
[email protected]cb0ce1e022010-03-10 19:54:41721
[email protected]d13950e2009-12-04 01:43:02722 ExtensionsQuotaService* quota = service->quota_service();
[email protected]c5dbef02011-05-13 05:06:09723 if (quota->Assess(extension->id(), function, &params.arguments,
[email protected]8b8e7c92010-08-19 18:05:56724 base::TimeTicks::Now())) {
[email protected]d070ec62010-07-27 21:28:26725 // See crbug.com/39178.
726 ExternalProtocolHandler::PermitLaunchUrl();
727
[email protected]d13950e2009-12-04 01:43:02728 function->Run();
[email protected]efd75992011-12-15 22:42:42729 LogSuccess(extension, params);
[email protected]d13950e2009-12-04 01:43:02730 } else {
[email protected]fd50e7b2011-11-03 09:20:25731 function->OnQuotaExceeded();
[email protected]efd75992011-12-15 22:42:42732 LogFailure(extension, params.name, kQuotaExceeded);
[email protected]d13950e2009-12-04 01:43:02733 }
[email protected]720ad1312012-02-27 23:07:36734
735 // We only adjust the keepalive count for UIThreadExtensionFunction for
736 // now, largely for simplicity's sake. This is OK because currently, only
737 // the webRequest API uses IOThreadExtensionFunction, and that API is not
738 // compatible with lazy background pages.
739 profile()->GetExtensionProcessManager()->IncrementLazyKeepaliveCount(
740 extension);
741}
742
743void ExtensionFunctionDispatcher::OnExtensionFunctionCompleted(
744 const Extension* extension) {
745 profile()->GetExtensionProcessManager()->DecrementLazyKeepaliveCount(
746 extension);
[email protected]bfdffe2b2009-04-24 22:05:35747}
748
[email protected]c357acb42011-06-09 20:52:42749// static
750ExtensionFunction* ExtensionFunctionDispatcher::CreateExtensionFunction(
751 const ExtensionHostMsg_Request_Params& params,
752 const Extension* extension,
[email protected]6f371442011-11-09 06:45:46753 int requesting_process_id,
754 const extensions::ProcessMap& process_map,
[email protected]673514522011-07-13 18:17:18755 void* profile,
[email protected]c357acb42011-06-09 20:52:42756 IPC::Message::Sender* ipc_sender,
757 int routing_id) {
[email protected]c357acb42011-06-09 20:52:42758 if (!extension) {
[email protected]6f371442011-11-09 06:45:46759 LOG(ERROR) << "Specified extension does not exist.";
760 SendAccessDenied(ipc_sender, routing_id, params.request_id);
761 return NULL;
762 }
763
[email protected]83820d42011-11-12 22:03:11764 if (ExtensionAPI::GetInstance()->IsPrivileged(params.name) &&
765 !process_map.Contains(extension->id(), requesting_process_id)) {
[email protected]6f371442011-11-09 06:45:46766 LOG(ERROR) << "Extension API called from incorrect process "
767 << requesting_process_id
768 << " from URL " << params.source_url.spec();
[email protected]c357acb42011-06-09 20:52:42769 SendAccessDenied(ipc_sender, routing_id, params.request_id);
770 return NULL;
771 }
772
[email protected]0d3e4a22011-06-23 19:02:52773 if (!extension->HasAPIPermission(params.name)) {
[email protected]c357acb42011-06-09 20:52:42774 LOG(ERROR) << "Extension " << extension->id() << " does not have "
775 << "permission to function: " << params.name;
776 SendAccessDenied(ipc_sender, routing_id, params.request_id);
777 return NULL;
778 }
779
780 ExtensionFunction* function =
781 FactoryRegistry::GetInstance()->NewFunction(params.name);
782 function->SetArgs(&params.arguments);
783 function->set_source_url(params.source_url);
784 function->set_request_id(params.request_id);
785 function->set_has_callback(params.has_callback);
786 function->set_user_gesture(params.user_gesture);
787 function->set_extension(extension);
[email protected]637bf322011-10-01 20:46:32788 function->set_profile_id(profile);
[email protected]c357acb42011-06-09 20:52:42789 return function;
790}
791
792// static
[email protected]c5dbef02011-05-13 05:06:09793void ExtensionFunctionDispatcher::SendAccessDenied(
[email protected]c357acb42011-06-09 20:52:42794 IPC::Message::Sender* ipc_sender, int routing_id, int request_id) {
795 ipc_sender->Send(new ExtensionMsg_Response(
796 routing_id, request_id, false, std::string(),
[email protected]c5dbef02011-05-13 05:06:09797 "Access to extension API denied."));
[email protected]bfdffe2b2009-04-24 22:05:35798}