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