[email protected] | 64c82073 | 2012-01-05 20:50:34 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | bfdffe2b | 2009-04-24 22:05:35 | [diff] [blame] | 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] | 4b64d71 | 2013-01-17 17:53:17 | [diff] [blame] | 7 | #include "base/bind.h" |
[email protected] | ffbec69 | 2012-02-26 20:26:42 | [diff] [blame] | 8 | #include "base/json/json_string_value_serializer.h" |
[email protected] | 5bc248a | 2012-04-04 23:38:11 | [diff] [blame] | 9 | #include "base/lazy_instance.h" |
[email protected] | 35548ab | 2013-05-15 08:59:47 | [diff] [blame] | 10 | #include "base/logging.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 11 | #include "base/memory/ref_counted.h" |
[email protected] | d09a4ce1c | 2013-07-24 17:37:02 | [diff] [blame] | 12 | #include "base/process/process.h" |
[email protected] | bfdffe2b | 2009-04-24 22:05:35 | [diff] [blame] | 13 | #include "base/values.h" |
[email protected] | 17d40f0 | 2010-07-01 01:18:06 | [diff] [blame] | 14 | #include "build/build_config.h" |
[email protected] | 05602fa3 | 2013-08-01 06:30:43 | [diff] [blame] | 15 | #include "chrome/browser/extensions/activity_log/activity_action_constants.h" |
[email protected] | 17263b2 | 2013-05-16 03:29:22 | [diff] [blame] | 16 | #include "chrome/browser/extensions/activity_log/activity_log.h" |
[email protected] | 05602fa3 | 2013-08-01 06:30:43 | [diff] [blame] | 17 | #include "chrome/browser/extensions/api/activity_log_private/activity_log_private_api.h" |
[email protected] | ae33d32 | 2012-03-19 22:24:35 | [diff] [blame] | 18 | #include "chrome/browser/extensions/extension_function_registry.h" |
[email protected] | 2f69b38 | 2011-02-19 00:34:25 | [diff] [blame] | 19 | #include "chrome/browser/extensions/extension_service.h" |
[email protected] | efb4b08 | 2012-10-17 22:28:28 | [diff] [blame] | 20 | #include "chrome/browser/extensions/extension_system.h" |
[email protected] | a7ff4b7 | 2013-10-17 20:56:02 | [diff] [blame] | 21 | #include "chrome/browser/extensions/extension_util.h" |
[email protected] | 8f9d4eb | 2011-02-05 01:39:10 | [diff] [blame] | 22 | #include "chrome/browser/extensions/extension_web_ui.h" |
[email protected] | d13950e | 2009-12-04 01:43:02 | [diff] [blame] | 23 | #include "chrome/browser/extensions/extensions_quota_service.h" |
[email protected] | 83820d4 | 2011-11-12 22:03:11 | [diff] [blame] | 24 | #include "chrome/browser/extensions/process_map.h" |
[email protected] | ed2b100 | 2011-05-25 14:12:10 | [diff] [blame] | 25 | #include "chrome/browser/external_protocol/external_protocol_handler.h" |
[email protected] | 8ecad5e | 2010-12-02 21:18:33 | [diff] [blame] | 26 | #include "chrome/browser/profiles/profile.h" |
[email protected] | c357acb4 | 2011-06-09 20:52:42 | [diff] [blame] | 27 | #include "chrome/browser/renderer_host/chrome_render_message_filter.h" |
[email protected] | 44c49c9 | 2011-03-28 16:17:23 | [diff] [blame] | 28 | #include "chrome/common/extensions/extension_messages.h" |
[email protected] | 615d88f | 2011-12-13 01:47:44 | [diff] [blame] | 29 | #include "chrome/common/extensions/extension_set.h" |
[email protected] | 9c45b718 | 2009-08-04 16:44:43 | [diff] [blame] | 30 | #include "chrome/common/url_constants.h" |
[email protected] | 4b64d71 | 2013-01-17 17:53:17 | [diff] [blame] | 31 | #include "content/public/browser/browser_thread.h" |
[email protected] | c333e79 | 2012-01-06 16:57:39 | [diff] [blame] | 32 | #include "content/public/browser/render_process_host.h" |
[email protected] | 9c1662b | 2012-03-06 15:44:33 | [diff] [blame] | 33 | #include "content/public/browser/render_view_host.h" |
[email protected] | d6ec84a | 2013-11-01 13:07:38 | [diff] [blame^] | 34 | #include "content/public/browser/user_metrics.h" |
[email protected] | ab2a7e3c | 2013-10-22 03:41:36 | [diff] [blame] | 35 | #include "content/public/browser/web_contents.h" |
| 36 | #include "content/public/browser/web_contents_observer.h" |
[email protected] | 35548ab | 2013-05-15 08:59:47 | [diff] [blame] | 37 | #include "content/public/common/result_codes.h" |
[email protected] | d6ec84a | 2013-11-01 13:07:38 | [diff] [blame^] | 38 | #include "extensions/common/extension_api.h" |
[email protected] | f82d57b5 | 2011-04-27 19:13:17 | [diff] [blame] | 39 | #include "ipc/ipc_message.h" |
| 40 | #include "ipc/ipc_message_macros.h" |
[email protected] | d0fcff7 | 2013-07-23 02:45:43 | [diff] [blame] | 41 | #include "webkit/common/resource_type.h" |
[email protected] | 61b55b6 | 2011-03-24 09:03:10 | [diff] [blame] | 42 | |
[email protected] | 1c321ee | 2012-05-21 03:02:34 | [diff] [blame] | 43 | using extensions::Extension; |
[email protected] | 83820d4 | 2011-11-12 22:03:11 | [diff] [blame] | 44 | using extensions::ExtensionAPI; |
[email protected] | b5b26b7 | 2013-08-02 00:25:11 | [diff] [blame] | 45 | using extensions::Feature; |
[email protected] | eaabba2 | 2012-03-07 15:02:11 | [diff] [blame] | 46 | using content::RenderViewHost; |
[email protected] | 83820d4 | 2011-11-12 22:03:11 | [diff] [blame] | 47 | |
[email protected] | 5bc248a | 2012-04-04 23:38:11 | [diff] [blame] | 48 | namespace { |
| 49 | |
[email protected] | e5a440c | 2013-06-04 21:55:12 | [diff] [blame] | 50 | void LogSuccess(const std::string& extension_id, |
[email protected] | 4b64d71 | 2013-01-17 17:53:17 | [diff] [blame] | 51 | const std::string& api_name, |
[email protected] | aeca23f | 2013-06-21 22:34:41 | [diff] [blame] | 52 | scoped_ptr<base::ListValue> args, |
[email protected] | 4b64d71 | 2013-01-17 17:53:17 | [diff] [blame] | 53 | Profile* profile) { |
| 54 | // The ActivityLog can only be accessed from the main (UI) thread. If we're |
| 55 | // running on the wrong thread, re-dispatch from the main thread. |
| 56 | if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
| 57 | BrowserThread::PostTask(BrowserThread::UI, |
| 58 | FROM_HERE, |
| 59 | base::Bind(&LogSuccess, |
[email protected] | e5a440c | 2013-06-04 21:55:12 | [diff] [blame] | 60 | extension_id, |
[email protected] | 4b64d71 | 2013-01-17 17:53:17 | [diff] [blame] | 61 | api_name, |
[email protected] | c02087b51 | 2013-02-04 03:09:20 | [diff] [blame] | 62 | base::Passed(&args), |
[email protected] | 4b64d71 | 2013-01-17 17:53:17 | [diff] [blame] | 63 | profile)); |
| 64 | } else { |
| 65 | extensions::ActivityLog* activity_log = |
| 66 | extensions::ActivityLog::GetInstance(profile); |
[email protected] | 05602fa3 | 2013-08-01 06:30:43 | [diff] [blame] | 67 | scoped_refptr<extensions::Action> action = |
| 68 | new extensions::Action(extension_id, |
| 69 | base::Time::Now(), |
| 70 | extensions::Action::ACTION_API_CALL, |
| 71 | api_name); |
| 72 | action->set_args(args.Pass()); |
| 73 | activity_log->LogAction(action); |
[email protected] | efd7599 | 2011-12-15 22:42:42 | [diff] [blame] | 74 | } |
| 75 | } |
| 76 | |
[email protected] | 5bc248a | 2012-04-04 23:38:11 | [diff] [blame] | 77 | // Separate copy of ExtensionAPI used for IO thread extension functions. We need |
| 78 | // this because ExtensionAPI has mutable data. It should be possible to remove |
| 79 | // this once all the extension APIs are updated to the feature system. |
| 80 | struct Static { |
| 81 | Static() |
| 82 | : api(extensions::ExtensionAPI::CreateWithDefaultConfiguration()) { |
| 83 | } |
| 84 | scoped_ptr<extensions::ExtensionAPI> api; |
| 85 | }; |
| 86 | base::LazyInstance<Static> g_global_io_data = LAZY_INSTANCE_INITIALIZER; |
| 87 | |
[email protected] | 35548ab | 2013-05-15 08:59:47 | [diff] [blame] | 88 | // Kills the specified process because it sends us a malformed message. |
| 89 | void KillBadMessageSender(base::ProcessHandle process) { |
| 90 | NOTREACHED(); |
| 91 | content::RecordAction(content::UserMetricsAction("BadMessageTerminate_EFD")); |
| 92 | if (process) |
| 93 | base::KillProcess(process, content::RESULT_CODE_KILLED_BAD_MESSAGE, false); |
| 94 | } |
| 95 | |
| 96 | void CommonResponseCallback(IPC::Sender* ipc_sender, |
| 97 | int routing_id, |
| 98 | base::ProcessHandle peer_process, |
| 99 | int request_id, |
| 100 | ExtensionFunction::ResponseType type, |
| 101 | const base::ListValue& results, |
| 102 | const std::string& error) { |
| 103 | DCHECK(ipc_sender); |
| 104 | |
| 105 | if (type == ExtensionFunction::BAD_MESSAGE) { |
| 106 | // The renderer has done validation before sending extension api requests. |
| 107 | // Therefore, we should never receive a request that is invalid in a way |
| 108 | // that JSON validation in the renderer should have caught. It could be an |
| 109 | // attacker trying to exploit the browser, so we crash the renderer instead. |
| 110 | LOG(ERROR) << |
| 111 | "Terminating renderer because of malformed extension message."; |
| 112 | if (content::RenderProcessHost::run_renderer_in_process()) { |
| 113 | // In single process mode it is better if we don't suicide but just crash. |
| 114 | CHECK(false); |
| 115 | } else { |
| 116 | KillBadMessageSender(peer_process); |
| 117 | } |
| 118 | |
| 119 | return; |
| 120 | } |
| 121 | |
| 122 | ipc_sender->Send(new ExtensionMsg_Response( |
| 123 | routing_id, request_id, type == ExtensionFunction::SUCCEEDED, results, |
| 124 | error)); |
| 125 | } |
| 126 | |
| 127 | void IOThreadResponseCallback( |
| 128 | const base::WeakPtr<ChromeRenderMessageFilter>& ipc_sender, |
| 129 | int routing_id, |
| 130 | int request_id, |
| 131 | ExtensionFunction::ResponseType type, |
| 132 | const base::ListValue& results, |
| 133 | const std::string& error) { |
[email protected] | e8dad9b | 2013-06-04 04:43:45 | [diff] [blame] | 134 | if (!ipc_sender.get()) |
[email protected] | 35548ab | 2013-05-15 08:59:47 | [diff] [blame] | 135 | return; |
| 136 | |
[email protected] | e8dad9b | 2013-06-04 04:43:45 | [diff] [blame] | 137 | CommonResponseCallback(ipc_sender.get(), |
| 138 | routing_id, |
[email protected] | 950be55 | 2013-07-10 19:13:02 | [diff] [blame] | 139 | ipc_sender->PeerHandle(), |
[email protected] | e8dad9b | 2013-06-04 04:43:45 | [diff] [blame] | 140 | request_id, |
| 141 | type, |
| 142 | results, |
| 143 | error); |
[email protected] | 35548ab | 2013-05-15 08:59:47 | [diff] [blame] | 144 | } |
| 145 | |
[email protected] | 5bc248a | 2012-04-04 23:38:11 | [diff] [blame] | 146 | } // namespace |
| 147 | |
[email protected] | 35548ab | 2013-05-15 08:59:47 | [diff] [blame] | 148 | class ExtensionFunctionDispatcher::UIThreadResponseCallbackWrapper |
[email protected] | ab2a7e3c | 2013-10-22 03:41:36 | [diff] [blame] | 149 | : public content::WebContentsObserver { |
[email protected] | 35548ab | 2013-05-15 08:59:47 | [diff] [blame] | 150 | public: |
| 151 | UIThreadResponseCallbackWrapper( |
| 152 | const base::WeakPtr<ExtensionFunctionDispatcher>& dispatcher, |
| 153 | RenderViewHost* render_view_host) |
[email protected] | ab2a7e3c | 2013-10-22 03:41:36 | [diff] [blame] | 154 | : content::WebContentsObserver( |
| 155 | content::WebContents::FromRenderViewHost(render_view_host)), |
[email protected] | 35548ab | 2013-05-15 08:59:47 | [diff] [blame] | 156 | dispatcher_(dispatcher), |
[email protected] | ab2a7e3c | 2013-10-22 03:41:36 | [diff] [blame] | 157 | render_view_host_(render_view_host), |
[email protected] | 35548ab | 2013-05-15 08:59:47 | [diff] [blame] | 158 | weak_ptr_factory_(this) { |
| 159 | } |
| 160 | |
| 161 | virtual ~UIThreadResponseCallbackWrapper() { |
| 162 | } |
| 163 | |
[email protected] | ab2a7e3c | 2013-10-22 03:41:36 | [diff] [blame] | 164 | // content::WebContentsObserver overrides. |
| 165 | virtual void RenderViewDeleted( |
[email protected] | 35548ab | 2013-05-15 08:59:47 | [diff] [blame] | 166 | RenderViewHost* render_view_host) OVERRIDE { |
| 167 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | ab2a7e3c | 2013-10-22 03:41:36 | [diff] [blame] | 168 | if (render_view_host != render_view_host_) |
| 169 | return; |
| 170 | |
[email protected] | e8dad9b | 2013-06-04 04:43:45 | [diff] [blame] | 171 | if (dispatcher_.get()) { |
| 172 | dispatcher_->ui_thread_response_callback_wrappers_ |
| 173 | .erase(render_view_host); |
[email protected] | 35548ab | 2013-05-15 08:59:47 | [diff] [blame] | 174 | } |
| 175 | |
[email protected] | ab2a7e3c | 2013-10-22 03:41:36 | [diff] [blame] | 176 | delete this; |
[email protected] | 35548ab | 2013-05-15 08:59:47 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | ExtensionFunction::ResponseCallback CreateCallback(int request_id) { |
| 180 | return base::Bind( |
| 181 | &UIThreadResponseCallbackWrapper::OnExtensionFunctionCompleted, |
| 182 | weak_ptr_factory_.GetWeakPtr(), |
| 183 | request_id); |
| 184 | } |
| 185 | |
| 186 | private: |
| 187 | void OnExtensionFunctionCompleted(int request_id, |
| 188 | ExtensionFunction::ResponseType type, |
| 189 | const base::ListValue& results, |
| 190 | const std::string& error) { |
| 191 | CommonResponseCallback( |
[email protected] | ab2a7e3c | 2013-10-22 03:41:36 | [diff] [blame] | 192 | render_view_host_, render_view_host_->GetRoutingID(), |
| 193 | render_view_host_->GetProcess()->GetHandle(), request_id, type, |
[email protected] | 35548ab | 2013-05-15 08:59:47 | [diff] [blame] | 194 | results, error); |
| 195 | } |
| 196 | |
| 197 | base::WeakPtr<ExtensionFunctionDispatcher> dispatcher_; |
[email protected] | ab2a7e3c | 2013-10-22 03:41:36 | [diff] [blame] | 198 | content::RenderViewHost* render_view_host_; |
[email protected] | 35548ab | 2013-05-15 08:59:47 | [diff] [blame] | 199 | base::WeakPtrFactory<UIThreadResponseCallbackWrapper> weak_ptr_factory_; |
| 200 | |
| 201 | DISALLOW_COPY_AND_ASSIGN(UIThreadResponseCallbackWrapper); |
| 202 | }; |
| 203 | |
[email protected] | 44f4b13 | 2012-07-17 20:36:57 | [diff] [blame] | 204 | extensions::WindowController* |
| 205 | ExtensionFunctionDispatcher::Delegate::GetExtensionWindowController() |
[email protected] | d72d3a6 | 2012-05-10 03:45:08 | [diff] [blame] | 206 | const { |
| 207 | return NULL; |
| 208 | } |
| 209 | |
| 210 | content::WebContents* |
[email protected] | 44f4b13 | 2012-07-17 20:36:57 | [diff] [blame] | 211 | ExtensionFunctionDispatcher::Delegate::GetAssociatedWebContents() const { |
[email protected] | d72d3a6 | 2012-05-10 03:45:08 | [diff] [blame] | 212 | return NULL; |
| 213 | } |
[email protected] | 5bc248a | 2012-04-04 23:38:11 | [diff] [blame] | 214 | |
[email protected] | 1ce88e8 | 2013-06-28 05:17:10 | [diff] [blame] | 215 | content::WebContents* |
| 216 | ExtensionFunctionDispatcher::Delegate::GetVisibleWebContents() const { |
| 217 | return GetAssociatedWebContents(); |
| 218 | } |
| 219 | |
[email protected] | bfdffe2b | 2009-04-24 22:05:35 | [diff] [blame] | 220 | void ExtensionFunctionDispatcher::GetAllFunctionNames( |
| 221 | std::vector<std::string>* names) { |
[email protected] | ae33d32 | 2012-03-19 22:24:35 | [diff] [blame] | 222 | ExtensionFunctionRegistry::GetInstance()->GetAllNames(names); |
[email protected] | bfdffe2b | 2009-04-24 22:05:35 | [diff] [blame] | 223 | } |
| 224 | |
[email protected] | b83e460 | 2009-05-15 22:58:33 | [diff] [blame] | 225 | bool ExtensionFunctionDispatcher::OverrideFunction( |
| 226 | const std::string& name, ExtensionFunctionFactory factory) { |
[email protected] | ae33d32 | 2012-03-19 22:24:35 | [diff] [blame] | 227 | return ExtensionFunctionRegistry::GetInstance()->OverrideFunction(name, |
| 228 | factory); |
[email protected] | b83e460 | 2009-05-15 22:58:33 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | void ExtensionFunctionDispatcher::ResetFunctions() { |
[email protected] | ae33d32 | 2012-03-19 22:24:35 | [diff] [blame] | 232 | ExtensionFunctionRegistry::GetInstance()->ResetFunctions(); |
[email protected] | b83e460 | 2009-05-15 22:58:33 | [diff] [blame] | 233 | } |
| 234 | |
[email protected] | c357acb4 | 2011-06-09 20:52:42 | [diff] [blame] | 235 | // static |
| 236 | void ExtensionFunctionDispatcher::DispatchOnIOThread( |
[email protected] | fd50e7b | 2011-11-03 09:20:25 | [diff] [blame] | 237 | ExtensionInfoMap* extension_info_map, |
[email protected] | 67351452 | 2011-07-13 18:17:18 | [diff] [blame] | 238 | void* profile, |
[email protected] | c357acb4 | 2011-06-09 20:52:42 | [diff] [blame] | 239 | int render_process_id, |
| 240 | base::WeakPtr<ChromeRenderMessageFilter> ipc_sender, |
[email protected] | 74e21e7 | 2012-07-09 21:20:53 | [diff] [blame] | 241 | int routing_id, |
[email protected] | c357acb4 | 2011-06-09 20:52:42 | [diff] [blame] | 242 | const ExtensionHostMsg_Request_Params& params) { |
| 243 | const Extension* extension = |
[email protected] | 83820d4 | 2011-11-12 22:03:11 | [diff] [blame] | 244 | extension_info_map->extensions().GetByID(params.extension_id); |
[email protected] | 4b64d71 | 2013-01-17 17:53:17 | [diff] [blame] | 245 | Profile* profile_cast = static_cast<Profile*>(profile); |
[email protected] | 35548ab | 2013-05-15 08:59:47 | [diff] [blame] | 246 | |
| 247 | ExtensionFunction::ResponseCallback callback( |
| 248 | base::Bind(&IOThreadResponseCallback, ipc_sender, routing_id, |
| 249 | params.request_id)); |
| 250 | |
[email protected] | 6f37144 | 2011-11-09 06:45:46 | [diff] [blame] | 251 | scoped_refptr<ExtensionFunction> function( |
| 252 | CreateExtensionFunction(params, extension, render_process_id, |
[email protected] | 5bc248a | 2012-04-04 23:38:11 | [diff] [blame] | 253 | extension_info_map->process_map(), |
| 254 | g_global_io_data.Get().api.get(), |
[email protected] | 35548ab | 2013-05-15 08:59:47 | [diff] [blame] | 255 | profile, callback)); |
[email protected] | 4b64d71 | 2013-01-17 17:53:17 | [diff] [blame] | 256 | scoped_ptr<ListValue> args(params.arguments.DeepCopy()); |
| 257 | |
[email protected] | ecc854a | 2013-08-22 10:12:42 | [diff] [blame] | 258 | if (!function.get()) |
[email protected] | c357acb4 | 2011-06-09 20:52:42 | [diff] [blame] | 259 | return; |
| 260 | |
| 261 | IOThreadExtensionFunction* function_io = |
| 262 | function->AsIOThreadExtensionFunction(); |
| 263 | if (!function_io) { |
| 264 | NOTREACHED(); |
| 265 | return; |
| 266 | } |
[email protected] | 44295a1 | 2013-06-05 08:45:46 | [diff] [blame] | 267 | function_io->set_ipc_sender(ipc_sender, routing_id); |
[email protected] | c357acb4 | 2011-06-09 20:52:42 | [diff] [blame] | 268 | function_io->set_extension_info_map(extension_info_map); |
| 269 | function->set_include_incognito( |
| 270 | extension_info_map->IsIncognitoEnabled(extension->id())); |
[email protected] | fd50e7b | 2011-11-03 09:20:25 | [diff] [blame] | 271 | |
[email protected] | ecc854a | 2013-08-22 10:12:42 | [diff] [blame] | 272 | if (!CheckPermissions(function.get(), extension, params, callback)) |
[email protected] | d2fe22ff | 2012-10-03 00:40:07 | [diff] [blame] | 273 | return; |
[email protected] | d2fe22ff | 2012-10-03 00:40:07 | [diff] [blame] | 274 | |
[email protected] | 3629691 | 2012-03-20 11:08:49 | [diff] [blame] | 275 | ExtensionsQuotaService* quota = extension_info_map->GetQuotaService(); |
[email protected] | 85231d7 | 2012-08-31 09:45:29 | [diff] [blame] | 276 | std::string violation_error = quota->Assess(extension->id(), |
[email protected] | dc24976f | 2013-06-02 21:15:09 | [diff] [blame] | 277 | function.get(), |
[email protected] | 85231d7 | 2012-08-31 09:45:29 | [diff] [blame] | 278 | ¶ms.arguments, |
| 279 | base::TimeTicks::Now()); |
| 280 | if (violation_error.empty()) { |
[email protected] | e5a440c | 2013-06-04 21:55:12 | [diff] [blame] | 281 | LogSuccess(extension->id(), |
[email protected] | 4b64d71 | 2013-01-17 17:53:17 | [diff] [blame] | 282 | params.name, |
| 283 | args.Pass(), |
| 284 | profile_cast); |
[email protected] | fd50e7b | 2011-11-03 09:20:25 | [diff] [blame] | 285 | function->Run(); |
| 286 | } else { |
[email protected] | 85231d7 | 2012-08-31 09:45:29 | [diff] [blame] | 287 | function->OnQuotaExceeded(violation_error); |
[email protected] | fd50e7b | 2011-11-03 09:20:25 | [diff] [blame] | 288 | } |
[email protected] | c357acb4 | 2011-06-09 20:52:42 | [diff] [blame] | 289 | } |
| 290 | |
[email protected] | c5dbef0 | 2011-05-13 05:06:09 | [diff] [blame] | 291 | ExtensionFunctionDispatcher::ExtensionFunctionDispatcher(Profile* profile, |
| 292 | Delegate* delegate) |
| 293 | : profile_(profile), |
[email protected] | 55ce33071 | 2011-05-24 19:04:27 | [diff] [blame] | 294 | delegate_(delegate) { |
[email protected] | bfdffe2b | 2009-04-24 22:05:35 | [diff] [blame] | 295 | } |
| 296 | |
[email protected] | 32dda36 | 2009-06-05 19:07:01 | [diff] [blame] | 297 | ExtensionFunctionDispatcher::~ExtensionFunctionDispatcher() { |
[email protected] | 32dda36 | 2009-06-05 19:07:01 | [diff] [blame] | 298 | } |
| 299 | |
[email protected] | c5dbef0 | 2011-05-13 05:06:09 | [diff] [blame] | 300 | void ExtensionFunctionDispatcher::Dispatch( |
| 301 | const ExtensionHostMsg_Request_Params& params, |
| 302 | RenderViewHost* render_view_host) { |
[email protected] | 35548ab | 2013-05-15 08:59:47 | [diff] [blame] | 303 | UIThreadResponseCallbackWrapperMap::const_iterator |
| 304 | iter = ui_thread_response_callback_wrappers_.find(render_view_host); |
| 305 | UIThreadResponseCallbackWrapper* callback_wrapper = NULL; |
| 306 | if (iter == ui_thread_response_callback_wrappers_.end()) { |
| 307 | callback_wrapper = new UIThreadResponseCallbackWrapper(AsWeakPtr(), |
| 308 | render_view_host); |
| 309 | ui_thread_response_callback_wrappers_[render_view_host] = callback_wrapper; |
| 310 | } else { |
| 311 | callback_wrapper = iter->second; |
| 312 | } |
| 313 | |
| 314 | DispatchWithCallback(params, render_view_host, |
| 315 | callback_wrapper->CreateCallback(params.request_id)); |
| 316 | } |
| 317 | |
| 318 | void ExtensionFunctionDispatcher::DispatchWithCallback( |
| 319 | const ExtensionHostMsg_Request_Params& params, |
| 320 | RenderViewHost* render_view_host, |
| 321 | const ExtensionFunction::ResponseCallback& callback) { |
| 322 | // TODO(yzshen): There is some shared logic between this method and |
| 323 | // DispatchOnIOThread(). It is nice to deduplicate. |
[email protected] | c5dbef0 | 2011-05-13 05:06:09 | [diff] [blame] | 324 | ExtensionService* service = profile()->GetExtensionService(); |
[email protected] | efb4b08 | 2012-10-17 22:28:28 | [diff] [blame] | 325 | ExtensionProcessManager* process_manager = |
| 326 | extensions::ExtensionSystem::Get(profile())->process_manager(); |
[email protected] | 6f37144 | 2011-11-09 06:45:46 | [diff] [blame] | 327 | extensions::ProcessMap* process_map = service->process_map(); |
| 328 | if (!service || !process_map) |
[email protected] | c5dbef0 | 2011-05-13 05:06:09 | [diff] [blame] | 329 | return; |
| 330 | |
[email protected] | 615d88f | 2011-12-13 01:47:44 | [diff] [blame] | 331 | const Extension* extension = service->extensions()->GetByID( |
| 332 | params.extension_id); |
[email protected] | c5dbef0 | 2011-05-13 05:06:09 | [diff] [blame] | 333 | if (!extension) |
[email protected] | be9915fb | 2013-07-18 09:28:55 | [diff] [blame] | 334 | extension = service->extensions()->GetHostedAppByURL(params.source_url); |
[email protected] | c5dbef0 | 2011-05-13 05:06:09 | [diff] [blame] | 335 | |
[email protected] | 8add541 | 2011-10-01 21:02:14 | [diff] [blame] | 336 | scoped_refptr<ExtensionFunction> function( |
[email protected] | 74e21e7 | 2012-07-09 21:20:53 | [diff] [blame] | 337 | CreateExtensionFunction(params, extension, |
[email protected] | 9f76c1e | 2012-03-05 15:15:58 | [diff] [blame] | 338 | render_view_host->GetProcess()->GetID(), |
[email protected] | 6f37144 | 2011-11-09 06:45:46 | [diff] [blame] | 339 | *(service->process_map()), |
[email protected] | 5bc248a | 2012-04-04 23:38:11 | [diff] [blame] | 340 | extensions::ExtensionAPI::GetSharedInstance(), |
[email protected] | 35548ab | 2013-05-15 08:59:47 | [diff] [blame] | 341 | profile(), callback)); |
[email protected] | 4b64d71 | 2013-01-17 17:53:17 | [diff] [blame] | 342 | scoped_ptr<ListValue> args(params.arguments.DeepCopy()); |
| 343 | |
[email protected] | ecc854a | 2013-08-22 10:12:42 | [diff] [blame] | 344 | if (!function.get()) |
[email protected] | f82d57b5 | 2011-04-27 19:13:17 | [diff] [blame] | 345 | return; |
[email protected] | f82d57b5 | 2011-04-27 19:13:17 | [diff] [blame] | 346 | |
[email protected] | a2aef2e | 2011-05-26 22:48:12 | [diff] [blame] | 347 | UIThreadExtensionFunction* function_ui = |
| 348 | function->AsUIThreadExtensionFunction(); |
| 349 | if (!function_ui) { |
| 350 | NOTREACHED(); |
| 351 | return; |
| 352 | } |
[email protected] | 35548ab | 2013-05-15 08:59:47 | [diff] [blame] | 353 | function_ui->SetRenderViewHost(render_view_host); |
[email protected] | a2aef2e | 2011-05-26 22:48:12 | [diff] [blame] | 354 | function_ui->set_dispatcher(AsWeakPtr()); |
[email protected] | 21a4008 | 2013-10-28 21:19:23 | [diff] [blame] | 355 | function_ui->set_context(profile_); |
[email protected] | a7ff4b7 | 2013-10-17 20:56:02 | [diff] [blame] | 356 | function->set_include_incognito(extension_util::CanCrossIncognito(extension, |
| 357 | service)); |
[email protected] | cb0ce1e02 | 2010-03-10 19:54:41 | [diff] [blame] | 358 | |
[email protected] | ecc854a | 2013-08-22 10:12:42 | [diff] [blame] | 359 | if (!CheckPermissions(function.get(), extension, params, callback)) |
[email protected] | d2fe22ff | 2012-10-03 00:40:07 | [diff] [blame] | 360 | return; |
[email protected] | d2fe22ff | 2012-10-03 00:40:07 | [diff] [blame] | 361 | |
[email protected] | d13950e | 2009-12-04 01:43:02 | [diff] [blame] | 362 | ExtensionsQuotaService* quota = service->quota_service(); |
[email protected] | 85231d7 | 2012-08-31 09:45:29 | [diff] [blame] | 363 | std::string violation_error = quota->Assess(extension->id(), |
[email protected] | dc24976f | 2013-06-02 21:15:09 | [diff] [blame] | 364 | function.get(), |
[email protected] | 85231d7 | 2012-08-31 09:45:29 | [diff] [blame] | 365 | ¶ms.arguments, |
| 366 | base::TimeTicks::Now()); |
| 367 | if (violation_error.empty()) { |
[email protected] | d070ec6 | 2010-07-27 21:28:26 | [diff] [blame] | 368 | // See crbug.com/39178. |
| 369 | ExternalProtocolHandler::PermitLaunchUrl(); |
[email protected] | e5a440c | 2013-06-04 21:55:12 | [diff] [blame] | 370 | LogSuccess(extension->id(), params.name, args.Pass(), profile()); |
[email protected] | d13950e | 2009-12-04 01:43:02 | [diff] [blame] | 371 | function->Run(); |
| 372 | } else { |
[email protected] | 85231d7 | 2012-08-31 09:45:29 | [diff] [blame] | 373 | function->OnQuotaExceeded(violation_error); |
[email protected] | d13950e | 2009-12-04 01:43:02 | [diff] [blame] | 374 | } |
[email protected] | 720ad131 | 2012-02-27 23:07:36 | [diff] [blame] | 375 | |
[email protected] | efb4b08 | 2012-10-17 22:28:28 | [diff] [blame] | 376 | // Note: do not access |this| after this point. We may have been deleted |
| 377 | // if function->Run() ended up closing the tab that owns us. |
| 378 | |
[email protected] | 5734e88 | 2012-05-04 22:17:56 | [diff] [blame] | 379 | // Check if extension was uninstalled by management.uninstall. |
| 380 | if (!service->extensions()->GetByID(params.extension_id)) |
| 381 | return; |
| 382 | |
[email protected] | 720ad131 | 2012-02-27 23:07:36 | [diff] [blame] | 383 | // We only adjust the keepalive count for UIThreadExtensionFunction for |
| 384 | // now, largely for simplicity's sake. This is OK because currently, only |
| 385 | // the webRequest API uses IOThreadExtensionFunction, and that API is not |
| 386 | // compatible with lazy background pages. |
[email protected] | efb4b08 | 2012-10-17 22:28:28 | [diff] [blame] | 387 | process_manager->IncrementLazyKeepaliveCount(extension); |
[email protected] | 720ad131 | 2012-02-27 23:07:36 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | void ExtensionFunctionDispatcher::OnExtensionFunctionCompleted( |
| 391 | const Extension* extension) { |
[email protected] | be93bba0 | 2012-10-24 16:44:03 | [diff] [blame] | 392 | extensions::ExtensionSystem::Get(profile())->process_manager()-> |
| 393 | DecrementLazyKeepaliveCount(extension); |
[email protected] | bfdffe2b | 2009-04-24 22:05:35 | [diff] [blame] | 394 | } |
| 395 | |
[email protected] | c357acb4 | 2011-06-09 20:52:42 | [diff] [blame] | 396 | // static |
[email protected] | d2fe22ff | 2012-10-03 00:40:07 | [diff] [blame] | 397 | bool ExtensionFunctionDispatcher::CheckPermissions( |
| 398 | ExtensionFunction* function, |
| 399 | const Extension* extension, |
| 400 | const ExtensionHostMsg_Request_Params& params, |
[email protected] | 35548ab | 2013-05-15 08:59:47 | [diff] [blame] | 401 | const ExtensionFunction::ResponseCallback& callback) { |
[email protected] | d2fe22ff | 2012-10-03 00:40:07 | [diff] [blame] | 402 | if (!function->HasPermission()) { |
| 403 | LOG(ERROR) << "Extension " << extension->id() << " does not have " |
| 404 | << "permission to function: " << params.name; |
[email protected] | 35548ab | 2013-05-15 08:59:47 | [diff] [blame] | 405 | SendAccessDenied(callback); |
[email protected] | d2fe22ff | 2012-10-03 00:40:07 | [diff] [blame] | 406 | return false; |
| 407 | } |
| 408 | return true; |
| 409 | } |
| 410 | |
[email protected] | f3354211 | 2013-02-04 16:52:38 | [diff] [blame] | 411 | namespace { |
| 412 | |
| 413 | // Only COMPONENT hosted apps may call extension APIs, and they are limited |
| 414 | // to just the permissions they explicitly request. They should not have access |
| 415 | // to extension APIs like eg chrome.runtime, chrome.windows, etc. that normally |
| 416 | // are available without permission. |
[email protected] | b5b26b7 | 2013-08-02 00:25:11 | [diff] [blame] | 417 | // TODO(mpcomplete): move this to ExtensionFunction::HasPermission (or remove |
| 418 | // it altogether). |
[email protected] | f3354211 | 2013-02-04 16:52:38 | [diff] [blame] | 419 | bool AllowHostedAppAPICall(const Extension& extension, |
| 420 | const GURL& source_url, |
| 421 | const std::string& function_name) { |
| 422 | if (extension.location() != extensions::Manifest::COMPONENT) |
| 423 | return false; |
| 424 | |
| 425 | if (!extension.web_extent().MatchesURL(source_url)) |
| 426 | return false; |
| 427 | |
[email protected] | b5b26b7 | 2013-08-02 00:25:11 | [diff] [blame] | 428 | Feature::Availability availability = |
| 429 | ExtensionAPI::GetSharedInstance()->IsAvailable( |
| 430 | function_name, &extension, Feature::BLESSED_EXTENSION_CONTEXT, |
| 431 | source_url); |
| 432 | return availability.is_available(); |
[email protected] | f3354211 | 2013-02-04 16:52:38 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | } // namespace |
| 436 | |
| 437 | |
[email protected] | d2fe22ff | 2012-10-03 00:40:07 | [diff] [blame] | 438 | // static |
[email protected] | c357acb4 | 2011-06-09 20:52:42 | [diff] [blame] | 439 | ExtensionFunction* ExtensionFunctionDispatcher::CreateExtensionFunction( |
| 440 | const ExtensionHostMsg_Request_Params& params, |
| 441 | const Extension* extension, |
[email protected] | 6f37144 | 2011-11-09 06:45:46 | [diff] [blame] | 442 | int requesting_process_id, |
| 443 | const extensions::ProcessMap& process_map, |
[email protected] | 5bc248a | 2012-04-04 23:38:11 | [diff] [blame] | 444 | extensions::ExtensionAPI* api, |
[email protected] | 67351452 | 2011-07-13 18:17:18 | [diff] [blame] | 445 | void* profile, |
[email protected] | 35548ab | 2013-05-15 08:59:47 | [diff] [blame] | 446 | const ExtensionFunction::ResponseCallback& callback) { |
[email protected] | c357acb4 | 2011-06-09 20:52:42 | [diff] [blame] | 447 | if (!extension) { |
[email protected] | 6f37144 | 2011-11-09 06:45:46 | [diff] [blame] | 448 | LOG(ERROR) << "Specified extension does not exist."; |
[email protected] | 35548ab | 2013-05-15 08:59:47 | [diff] [blame] | 449 | SendAccessDenied(callback); |
[email protected] | 6f37144 | 2011-11-09 06:45:46 | [diff] [blame] | 450 | return NULL; |
| 451 | } |
| 452 | |
[email protected] | f3354211 | 2013-02-04 16:52:38 | [diff] [blame] | 453 | // Most hosted apps can't call APIs. |
| 454 | bool allowed = true; |
| 455 | if (extension->is_hosted_app()) |
[email protected] | 35548ab | 2013-05-15 08:59:47 | [diff] [blame] | 456 | allowed = AllowHostedAppAPICall(*extension, params.source_url, params.name); |
[email protected] | f3354211 | 2013-02-04 16:52:38 | [diff] [blame] | 457 | |
| 458 | // Privileged APIs can only be called from the process the extension |
| 459 | // is running in. |
| 460 | if (allowed && api->IsPrivileged(params.name)) |
| 461 | allowed = process_map.Contains(extension->id(), requesting_process_id); |
| 462 | |
| 463 | if (!allowed) { |
| 464 | LOG(ERROR) << "Extension API call disallowed - name:" << params.name |
| 465 | << " pid:" << requesting_process_id |
[email protected] | 6f37144 | 2011-11-09 06:45:46 | [diff] [blame] | 466 | << " from URL " << params.source_url.spec(); |
[email protected] | 35548ab | 2013-05-15 08:59:47 | [diff] [blame] | 467 | SendAccessDenied(callback); |
[email protected] | c357acb4 | 2011-06-09 20:52:42 | [diff] [blame] | 468 | return NULL; |
| 469 | } |
| 470 | |
[email protected] | c357acb4 | 2011-06-09 20:52:42 | [diff] [blame] | 471 | ExtensionFunction* function = |
[email protected] | ae33d32 | 2012-03-19 22:24:35 | [diff] [blame] | 472 | ExtensionFunctionRegistry::GetInstance()->NewFunction(params.name); |
[email protected] | 42681ec8 | 2013-04-09 12:40:14 | [diff] [blame] | 473 | if (!function) { |
| 474 | LOG(ERROR) << "Unknown Extension API - " << params.name; |
[email protected] | 35548ab | 2013-05-15 08:59:47 | [diff] [blame] | 475 | SendAccessDenied(callback); |
[email protected] | 42681ec8 | 2013-04-09 12:40:14 | [diff] [blame] | 476 | return NULL; |
| 477 | } |
| 478 | |
[email protected] | c357acb4 | 2011-06-09 20:52:42 | [diff] [blame] | 479 | function->SetArgs(¶ms.arguments); |
| 480 | function->set_source_url(params.source_url); |
| 481 | function->set_request_id(params.request_id); |
| 482 | function->set_has_callback(params.has_callback); |
| 483 | function->set_user_gesture(params.user_gesture); |
| 484 | function->set_extension(extension); |
[email protected] | 637bf32 | 2011-10-01 20:46:32 | [diff] [blame] | 485 | function->set_profile_id(profile); |
[email protected] | 35548ab | 2013-05-15 08:59:47 | [diff] [blame] | 486 | function->set_response_callback(callback); |
[email protected] | 3d0e226 | 2012-08-02 15:32:16 | [diff] [blame] | 487 | |
[email protected] | c357acb4 | 2011-06-09 20:52:42 | [diff] [blame] | 488 | return function; |
| 489 | } |
| 490 | |
| 491 | // static |
[email protected] | c5dbef0 | 2011-05-13 05:06:09 | [diff] [blame] | 492 | void ExtensionFunctionDispatcher::SendAccessDenied( |
[email protected] | 35548ab | 2013-05-15 08:59:47 | [diff] [blame] | 493 | const ExtensionFunction::ResponseCallback& callback) { |
[email protected] | 602542d | 2012-04-20 02:48:01 | [diff] [blame] | 494 | ListValue empty_list; |
[email protected] | 35548ab | 2013-05-15 08:59:47 | [diff] [blame] | 495 | callback.Run(ExtensionFunction::FAILED, empty_list, |
| 496 | "Access to extension API denied."); |
[email protected] | bfdffe2b | 2009-04-24 22:05:35 | [diff] [blame] | 497 | } |