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