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