[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] | 745feedb | 2010-08-02 04:08:07 | [diff] [blame] | 7 | #include <map> |
| 8 | |
[email protected] | 4b64d71 | 2013-01-17 17:53:17 | [diff] [blame] | 9 | #include "base/bind.h" |
[email protected] | ffbec69 | 2012-02-26 20:26:42 | [diff] [blame] | 10 | #include "base/json/json_string_value_serializer.h" |
[email protected] | 5bc248a | 2012-04-04 23:38:11 | [diff] [blame] | 11 | #include "base/lazy_instance.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 12 | #include "base/memory/ref_counted.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] | bfdffe2b | 2009-04-24 22:05:35 | [diff] [blame] | 17 | #include "chrome/browser/extensions/extension_function.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] | f82d57b5 | 2011-04-27 19:13:17 | [diff] [blame] | 34 | #include "ipc/ipc_message.h" |
| 35 | #include "ipc/ipc_message_macros.h" |
[email protected] | 615d88f | 2011-12-13 01:47:44 | [diff] [blame] | 36 | #include "third_party/WebKit/Source/WebKit/chromium/public/WebSecurityOrigin.h" |
[email protected] | ae33d32 | 2012-03-19 22:24:35 | [diff] [blame] | 37 | #include "webkit/glue/resource_type.h" |
[email protected] | 61b55b6 | 2011-03-24 09:03:10 | [diff] [blame] | 38 | |
[email protected] | 1c321ee | 2012-05-21 03:02:34 | [diff] [blame] | 39 | using extensions::Extension; |
[email protected] | 83820d4 | 2011-11-12 22:03:11 | [diff] [blame] | 40 | using extensions::ExtensionAPI; |
[email protected] | eaabba2 | 2012-03-07 15:02:11 | [diff] [blame] | 41 | using content::RenderViewHost; |
[email protected] | 615d88f | 2011-12-13 01:47:44 | [diff] [blame] | 42 | using WebKit::WebSecurityOrigin; |
[email protected] | 83820d4 | 2011-11-12 22:03:11 | [diff] [blame] | 43 | |
[email protected] | 5bc248a | 2012-04-04 23:38:11 | [diff] [blame] | 44 | namespace { |
| 45 | |
[email protected] | efd7599 | 2011-12-15 22:42:42 | [diff] [blame] | 46 | const char kAccessDenied[] = "access denied"; |
| 47 | const char kQuotaExceeded[] = "quota exceeded"; |
| 48 | |
| 49 | void LogSuccess(const Extension* extension, |
[email protected] | 4b64d71 | 2013-01-17 17:53:17 | [diff] [blame] | 50 | const std::string& api_name, |
| 51 | scoped_ptr<ListValue> args, |
| 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, |
| 59 | extension, |
| 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] | 7363376 | 2013-01-18 05:42:56 | [diff] [blame] | 66 | activity_log->LogAPIAction(extension, api_name, args.get(), ""); |
[email protected] | efd7599 | 2011-12-15 22:42:42 | [diff] [blame] | 67 | } |
| 68 | } |
| 69 | |
| 70 | void LogFailure(const Extension* extension, |
[email protected] | 4b64d71 | 2013-01-17 17:53:17 | [diff] [blame] | 71 | const std::string& api_name, |
| 72 | scoped_ptr<ListValue> args, |
| 73 | const char* reason, |
| 74 | Profile* profile) { |
| 75 | // The ActivityLog can only be accessed from the main (UI) thread. If we're |
| 76 | // running on the wrong thread, re-dispatch from the main thread. |
| 77 | if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
| 78 | BrowserThread::PostTask(BrowserThread::UI, |
| 79 | FROM_HERE, |
| 80 | base::Bind(&LogFailure, |
| 81 | extension, |
| 82 | api_name, |
[email protected] | c02087b51 | 2013-02-04 03:09:20 | [diff] [blame] | 83 | base::Passed(&args), |
[email protected] | 4b64d71 | 2013-01-17 17:53:17 | [diff] [blame] | 84 | reason, |
| 85 | profile)); |
| 86 | } else { |
| 87 | extensions::ActivityLog* activity_log = |
| 88 | extensions::ActivityLog::GetInstance(profile); |
[email protected] | 7363376 | 2013-01-18 05:42:56 | [diff] [blame] | 89 | activity_log->LogBlockedAction(extension, |
| 90 | api_name, |
| 91 | args.get(), |
| 92 | reason, |
| 93 | ""); |
[email protected] | efd7599 | 2011-12-15 22:42:42 | [diff] [blame] | 94 | } |
| 95 | } |
| 96 | |
[email protected] | 4b64d71 | 2013-01-17 17:53:17 | [diff] [blame] | 97 | |
[email protected] | 5bc248a | 2012-04-04 23:38:11 | [diff] [blame] | 98 | // Separate copy of ExtensionAPI used for IO thread extension functions. We need |
| 99 | // this because ExtensionAPI has mutable data. It should be possible to remove |
| 100 | // this once all the extension APIs are updated to the feature system. |
| 101 | struct Static { |
| 102 | Static() |
| 103 | : api(extensions::ExtensionAPI::CreateWithDefaultConfiguration()) { |
| 104 | } |
| 105 | scoped_ptr<extensions::ExtensionAPI> api; |
| 106 | }; |
| 107 | base::LazyInstance<Static> g_global_io_data = LAZY_INSTANCE_INITIALIZER; |
| 108 | |
| 109 | } // namespace |
| 110 | |
[email protected] | 44f4b13 | 2012-07-17 20:36:57 | [diff] [blame] | 111 | extensions::WindowController* |
| 112 | ExtensionFunctionDispatcher::Delegate::GetExtensionWindowController() |
[email protected] | d72d3a6 | 2012-05-10 03:45:08 | [diff] [blame] | 113 | const { |
| 114 | return NULL; |
| 115 | } |
| 116 | |
| 117 | content::WebContents* |
[email protected] | 44f4b13 | 2012-07-17 20:36:57 | [diff] [blame] | 118 | ExtensionFunctionDispatcher::Delegate::GetAssociatedWebContents() const { |
[email protected] | d72d3a6 | 2012-05-10 03:45:08 | [diff] [blame] | 119 | return NULL; |
| 120 | } |
[email protected] | 5bc248a | 2012-04-04 23:38:11 | [diff] [blame] | 121 | |
[email protected] | bfdffe2b | 2009-04-24 22:05:35 | [diff] [blame] | 122 | void ExtensionFunctionDispatcher::GetAllFunctionNames( |
| 123 | std::vector<std::string>* names) { |
[email protected] | ae33d32 | 2012-03-19 22:24:35 | [diff] [blame] | 124 | ExtensionFunctionRegistry::GetInstance()->GetAllNames(names); |
[email protected] | bfdffe2b | 2009-04-24 22:05:35 | [diff] [blame] | 125 | } |
| 126 | |
[email protected] | b83e460 | 2009-05-15 22:58:33 | [diff] [blame] | 127 | bool ExtensionFunctionDispatcher::OverrideFunction( |
| 128 | const std::string& name, ExtensionFunctionFactory factory) { |
[email protected] | ae33d32 | 2012-03-19 22:24:35 | [diff] [blame] | 129 | return ExtensionFunctionRegistry::GetInstance()->OverrideFunction(name, |
| 130 | factory); |
[email protected] | b83e460 | 2009-05-15 22:58:33 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | void ExtensionFunctionDispatcher::ResetFunctions() { |
[email protected] | ae33d32 | 2012-03-19 22:24:35 | [diff] [blame] | 134 | ExtensionFunctionRegistry::GetInstance()->ResetFunctions(); |
[email protected] | b83e460 | 2009-05-15 22:58:33 | [diff] [blame] | 135 | } |
| 136 | |
[email protected] | c357acb4 | 2011-06-09 20:52:42 | [diff] [blame] | 137 | // static |
| 138 | void ExtensionFunctionDispatcher::DispatchOnIOThread( |
[email protected] | fd50e7b | 2011-11-03 09:20:25 | [diff] [blame] | 139 | ExtensionInfoMap* extension_info_map, |
[email protected] | 67351452 | 2011-07-13 18:17:18 | [diff] [blame] | 140 | void* profile, |
[email protected] | c357acb4 | 2011-06-09 20:52:42 | [diff] [blame] | 141 | int render_process_id, |
| 142 | base::WeakPtr<ChromeRenderMessageFilter> ipc_sender, |
[email protected] | 74e21e7 | 2012-07-09 21:20:53 | [diff] [blame] | 143 | int routing_id, |
[email protected] | c357acb4 | 2011-06-09 20:52:42 | [diff] [blame] | 144 | const ExtensionHostMsg_Request_Params& params) { |
| 145 | const Extension* extension = |
[email protected] | 83820d4 | 2011-11-12 22:03:11 | [diff] [blame] | 146 | extension_info_map->extensions().GetByID(params.extension_id); |
[email protected] | 4b64d71 | 2013-01-17 17:53:17 | [diff] [blame] | 147 | Profile* profile_cast = static_cast<Profile*>(profile); |
[email protected] | 6f37144 | 2011-11-09 06:45:46 | [diff] [blame] | 148 | scoped_refptr<ExtensionFunction> function( |
| 149 | CreateExtensionFunction(params, extension, render_process_id, |
[email protected] | 5bc248a | 2012-04-04 23:38:11 | [diff] [blame] | 150 | extension_info_map->process_map(), |
| 151 | g_global_io_data.Get().api.get(), |
| 152 | profile, |
[email protected] | 3d0e226 | 2012-08-02 15:32:16 | [diff] [blame] | 153 | ipc_sender, NULL, routing_id)); |
[email protected] | 4b64d71 | 2013-01-17 17:53:17 | [diff] [blame] | 154 | scoped_ptr<ListValue> args(params.arguments.DeepCopy()); |
| 155 | |
[email protected] | efd7599 | 2011-12-15 22:42:42 | [diff] [blame] | 156 | if (!function) { |
[email protected] | 4b64d71 | 2013-01-17 17:53:17 | [diff] [blame] | 157 | LogFailure(extension, |
| 158 | params.name, |
| 159 | args.Pass(), |
| 160 | kAccessDenied, |
| 161 | profile_cast); |
[email protected] | c357acb4 | 2011-06-09 20:52:42 | [diff] [blame] | 162 | return; |
[email protected] | efd7599 | 2011-12-15 22:42:42 | [diff] [blame] | 163 | } |
[email protected] | c357acb4 | 2011-06-09 20:52:42 | [diff] [blame] | 164 | |
| 165 | IOThreadExtensionFunction* function_io = |
| 166 | function->AsIOThreadExtensionFunction(); |
| 167 | if (!function_io) { |
| 168 | NOTREACHED(); |
| 169 | return; |
| 170 | } |
[email protected] | 74e21e7 | 2012-07-09 21:20:53 | [diff] [blame] | 171 | function_io->set_ipc_sender(ipc_sender, routing_id); |
[email protected] | c357acb4 | 2011-06-09 20:52:42 | [diff] [blame] | 172 | function_io->set_extension_info_map(extension_info_map); |
| 173 | function->set_include_incognito( |
| 174 | extension_info_map->IsIncognitoEnabled(extension->id())); |
[email protected] | fd50e7b | 2011-11-03 09:20:25 | [diff] [blame] | 175 | |
[email protected] | d2fe22ff | 2012-10-03 00:40:07 | [diff] [blame] | 176 | if (!CheckPermissions(function, extension, params, ipc_sender, routing_id)) { |
[email protected] | 4b64d71 | 2013-01-17 17:53:17 | [diff] [blame] | 177 | LogFailure(extension, |
| 178 | params.name, |
| 179 | args.Pass(), |
| 180 | kAccessDenied, |
| 181 | profile_cast); |
[email protected] | d2fe22ff | 2012-10-03 00:40:07 | [diff] [blame] | 182 | return; |
| 183 | } |
| 184 | |
[email protected] | 3629691 | 2012-03-20 11:08:49 | [diff] [blame] | 185 | ExtensionsQuotaService* quota = extension_info_map->GetQuotaService(); |
[email protected] | 85231d7 | 2012-08-31 09:45:29 | [diff] [blame] | 186 | std::string violation_error = quota->Assess(extension->id(), |
| 187 | function, |
| 188 | ¶ms.arguments, |
| 189 | base::TimeTicks::Now()); |
| 190 | if (violation_error.empty()) { |
[email protected] | 4b64d71 | 2013-01-17 17:53:17 | [diff] [blame] | 191 | LogSuccess(extension, |
| 192 | params.name, |
| 193 | args.Pass(), |
| 194 | profile_cast); |
[email protected] | fd50e7b | 2011-11-03 09:20:25 | [diff] [blame] | 195 | function->Run(); |
[email protected] | fd50e7b | 2011-11-03 09:20:25 | [diff] [blame] | 196 | } else { |
[email protected] | 4b64d71 | 2013-01-17 17:53:17 | [diff] [blame] | 197 | LogFailure(extension, |
| 198 | params.name, |
| 199 | args.Pass(), |
| 200 | kQuotaExceeded, |
| 201 | profile_cast); |
[email protected] | 85231d7 | 2012-08-31 09:45:29 | [diff] [blame] | 202 | function->OnQuotaExceeded(violation_error); |
[email protected] | fd50e7b | 2011-11-03 09:20:25 | [diff] [blame] | 203 | } |
[email protected] | c357acb4 | 2011-06-09 20:52:42 | [diff] [blame] | 204 | } |
| 205 | |
[email protected] | c5dbef0 | 2011-05-13 05:06:09 | [diff] [blame] | 206 | ExtensionFunctionDispatcher::ExtensionFunctionDispatcher(Profile* profile, |
| 207 | Delegate* delegate) |
| 208 | : profile_(profile), |
[email protected] | 55ce33071 | 2011-05-24 19:04:27 | [diff] [blame] | 209 | delegate_(delegate) { |
[email protected] | bfdffe2b | 2009-04-24 22:05:35 | [diff] [blame] | 210 | } |
| 211 | |
[email protected] | 32dda36 | 2009-06-05 19:07:01 | [diff] [blame] | 212 | ExtensionFunctionDispatcher::~ExtensionFunctionDispatcher() { |
[email protected] | 32dda36 | 2009-06-05 19:07:01 | [diff] [blame] | 213 | } |
| 214 | |
[email protected] | c5dbef0 | 2011-05-13 05:06:09 | [diff] [blame] | 215 | void ExtensionFunctionDispatcher::Dispatch( |
| 216 | const ExtensionHostMsg_Request_Params& params, |
| 217 | RenderViewHost* render_view_host) { |
[email protected] | c5dbef0 | 2011-05-13 05:06:09 | [diff] [blame] | 218 | ExtensionService* service = profile()->GetExtensionService(); |
[email protected] | efb4b08 | 2012-10-17 22:28:28 | [diff] [blame] | 219 | ExtensionProcessManager* process_manager = |
| 220 | extensions::ExtensionSystem::Get(profile())->process_manager(); |
[email protected] | 6f37144 | 2011-11-09 06:45:46 | [diff] [blame] | 221 | extensions::ProcessMap* process_map = service->process_map(); |
| 222 | if (!service || !process_map) |
[email protected] | c5dbef0 | 2011-05-13 05:06:09 | [diff] [blame] | 223 | return; |
| 224 | |
[email protected] | 615d88f | 2011-12-13 01:47:44 | [diff] [blame] | 225 | const Extension* extension = service->extensions()->GetByID( |
| 226 | params.extension_id); |
[email protected] | c5dbef0 | 2011-05-13 05:06:09 | [diff] [blame] | 227 | if (!extension) |
[email protected] | 615d88f | 2011-12-13 01:47:44 | [diff] [blame] | 228 | extension = service->extensions()->GetHostedAppByURL(ExtensionURLInfo( |
| 229 | WebSecurityOrigin::createFromString(params.source_origin), |
| 230 | params.source_url)); |
[email protected] | c5dbef0 | 2011-05-13 05:06:09 | [diff] [blame] | 231 | |
[email protected] | 8add541 | 2011-10-01 21:02:14 | [diff] [blame] | 232 | scoped_refptr<ExtensionFunction> function( |
[email protected] | 74e21e7 | 2012-07-09 21:20:53 | [diff] [blame] | 233 | CreateExtensionFunction(params, extension, |
[email protected] | 9f76c1e | 2012-03-05 15:15:58 | [diff] [blame] | 234 | render_view_host->GetProcess()->GetID(), |
[email protected] | 6f37144 | 2011-11-09 06:45:46 | [diff] [blame] | 235 | *(service->process_map()), |
[email protected] | 5bc248a | 2012-04-04 23:38:11 | [diff] [blame] | 236 | extensions::ExtensionAPI::GetSharedInstance(), |
[email protected] | 3d0e226 | 2012-08-02 15:32:16 | [diff] [blame] | 237 | profile(), render_view_host, render_view_host, |
[email protected] | 74e21e7 | 2012-07-09 21:20:53 | [diff] [blame] | 238 | render_view_host->GetRoutingID())); |
[email protected] | 4b64d71 | 2013-01-17 17:53:17 | [diff] [blame] | 239 | scoped_ptr<ListValue> args(params.arguments.DeepCopy()); |
| 240 | |
[email protected] | efd7599 | 2011-12-15 22:42:42 | [diff] [blame] | 241 | if (!function) { |
[email protected] | 4b64d71 | 2013-01-17 17:53:17 | [diff] [blame] | 242 | LogFailure(extension, |
| 243 | params.name, |
| 244 | args.Pass(), |
| 245 | kAccessDenied, |
| 246 | profile()); |
[email protected] | f82d57b5 | 2011-04-27 19:13:17 | [diff] [blame] | 247 | return; |
[email protected] | efd7599 | 2011-12-15 22:42:42 | [diff] [blame] | 248 | } |
[email protected] | f82d57b5 | 2011-04-27 19:13:17 | [diff] [blame] | 249 | |
[email protected] | a2aef2e | 2011-05-26 22:48:12 | [diff] [blame] | 250 | UIThreadExtensionFunction* function_ui = |
| 251 | function->AsUIThreadExtensionFunction(); |
| 252 | if (!function_ui) { |
| 253 | NOTREACHED(); |
| 254 | return; |
| 255 | } |
[email protected] | a2aef2e | 2011-05-26 22:48:12 | [diff] [blame] | 256 | function_ui->set_dispatcher(AsWeakPtr()); |
| 257 | function_ui->set_profile(profile_); |
[email protected] | 2a8f24e | 2010-11-03 21:37:05 | [diff] [blame] | 258 | function->set_include_incognito(service->CanCrossIncognito(extension)); |
[email protected] | cb0ce1e02 | 2010-03-10 19:54:41 | [diff] [blame] | 259 | |
[email protected] | d2fe22ff | 2012-10-03 00:40:07 | [diff] [blame] | 260 | if (!CheckPermissions(function, extension, params, render_view_host, |
| 261 | render_view_host->GetRoutingID())) { |
[email protected] | 4b64d71 | 2013-01-17 17:53:17 | [diff] [blame] | 262 | LogFailure(extension, |
| 263 | params.name, |
| 264 | args.Pass(), |
| 265 | kAccessDenied, |
| 266 | profile()); |
[email protected] | d2fe22ff | 2012-10-03 00:40:07 | [diff] [blame] | 267 | return; |
| 268 | } |
| 269 | |
[email protected] | d13950e | 2009-12-04 01:43:02 | [diff] [blame] | 270 | ExtensionsQuotaService* quota = service->quota_service(); |
[email protected] | 85231d7 | 2012-08-31 09:45:29 | [diff] [blame] | 271 | std::string violation_error = quota->Assess(extension->id(), |
| 272 | function, |
| 273 | ¶ms.arguments, |
| 274 | base::TimeTicks::Now()); |
| 275 | if (violation_error.empty()) { |
[email protected] | d070ec6 | 2010-07-27 21:28:26 | [diff] [blame] | 276 | // See crbug.com/39178. |
| 277 | ExternalProtocolHandler::PermitLaunchUrl(); |
[email protected] | 4b64d71 | 2013-01-17 17:53:17 | [diff] [blame] | 278 | LogSuccess(extension, params.name, args.Pass(), profile()); |
[email protected] | d13950e | 2009-12-04 01:43:02 | [diff] [blame] | 279 | function->Run(); |
[email protected] | d13950e | 2009-12-04 01:43:02 | [diff] [blame] | 280 | } else { |
[email protected] | 4b64d71 | 2013-01-17 17:53:17 | [diff] [blame] | 281 | LogFailure(extension, |
| 282 | params.name, |
| 283 | args.Pass(), |
| 284 | kQuotaExceeded, |
| 285 | profile()); |
[email protected] | 85231d7 | 2012-08-31 09:45:29 | [diff] [blame] | 286 | function->OnQuotaExceeded(violation_error); |
[email protected] | d13950e | 2009-12-04 01:43:02 | [diff] [blame] | 287 | } |
[email protected] | 720ad131 | 2012-02-27 23:07:36 | [diff] [blame] | 288 | |
[email protected] | efb4b08 | 2012-10-17 22:28:28 | [diff] [blame] | 289 | // Note: do not access |this| after this point. We may have been deleted |
| 290 | // if function->Run() ended up closing the tab that owns us. |
| 291 | |
[email protected] | 5734e88 | 2012-05-04 22:17:56 | [diff] [blame] | 292 | // Check if extension was uninstalled by management.uninstall. |
| 293 | if (!service->extensions()->GetByID(params.extension_id)) |
| 294 | return; |
| 295 | |
[email protected] | 720ad131 | 2012-02-27 23:07:36 | [diff] [blame] | 296 | // We only adjust the keepalive count for UIThreadExtensionFunction for |
| 297 | // now, largely for simplicity's sake. This is OK because currently, only |
| 298 | // the webRequest API uses IOThreadExtensionFunction, and that API is not |
| 299 | // compatible with lazy background pages. |
[email protected] | efb4b08 | 2012-10-17 22:28:28 | [diff] [blame] | 300 | process_manager->IncrementLazyKeepaliveCount(extension); |
[email protected] | 720ad131 | 2012-02-27 23:07:36 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | void ExtensionFunctionDispatcher::OnExtensionFunctionCompleted( |
| 304 | const Extension* extension) { |
[email protected] | be93bba0 | 2012-10-24 16:44:03 | [diff] [blame] | 305 | extensions::ExtensionSystem::Get(profile())->process_manager()-> |
| 306 | DecrementLazyKeepaliveCount(extension); |
[email protected] | bfdffe2b | 2009-04-24 22:05:35 | [diff] [blame] | 307 | } |
| 308 | |
[email protected] | c357acb4 | 2011-06-09 20:52:42 | [diff] [blame] | 309 | // static |
[email protected] | d2fe22ff | 2012-10-03 00:40:07 | [diff] [blame] | 310 | bool ExtensionFunctionDispatcher::CheckPermissions( |
| 311 | ExtensionFunction* function, |
| 312 | const Extension* extension, |
| 313 | const ExtensionHostMsg_Request_Params& params, |
| 314 | IPC::Sender* ipc_sender, |
| 315 | int routing_id) { |
| 316 | if (!function->HasPermission()) { |
| 317 | LOG(ERROR) << "Extension " << extension->id() << " does not have " |
| 318 | << "permission to function: " << params.name; |
| 319 | SendAccessDenied(ipc_sender, routing_id, params.request_id); |
| 320 | return false; |
| 321 | } |
| 322 | return true; |
| 323 | } |
| 324 | |
[email protected] | f3354211 | 2013-02-04 16:52:38 | [diff] [blame^] | 325 | namespace { |
| 326 | |
| 327 | // Only COMPONENT hosted apps may call extension APIs, and they are limited |
| 328 | // to just the permissions they explicitly request. They should not have access |
| 329 | // to extension APIs like eg chrome.runtime, chrome.windows, etc. that normally |
| 330 | // are available without permission. |
| 331 | // TODO(asargent/kalman) - get rid of this when the features system can express |
| 332 | // the "non permission" permissions. |
| 333 | bool AllowHostedAppAPICall(const Extension& extension, |
| 334 | const GURL& source_url, |
| 335 | const std::string& function_name) { |
| 336 | if (extension.location() != extensions::Manifest::COMPONENT) |
| 337 | return false; |
| 338 | |
| 339 | if (!extension.web_extent().MatchesURL(source_url)) |
| 340 | return false; |
| 341 | |
| 342 | // We just allow the hosted app's explicit permissions, plus chrome.test. |
| 343 | scoped_refptr<const extensions::PermissionSet> permissions = |
| 344 | extension.GetActivePermissions(); |
| 345 | return (permissions->HasAccessToFunction(function_name, false) || |
| 346 | StartsWithASCII(function_name, "test.", true /*case_sensitive*/)); |
| 347 | } |
| 348 | |
| 349 | } // namespace |
| 350 | |
| 351 | |
[email protected] | d2fe22ff | 2012-10-03 00:40:07 | [diff] [blame] | 352 | // static |
[email protected] | c357acb4 | 2011-06-09 20:52:42 | [diff] [blame] | 353 | ExtensionFunction* ExtensionFunctionDispatcher::CreateExtensionFunction( |
| 354 | const ExtensionHostMsg_Request_Params& params, |
| 355 | const Extension* extension, |
[email protected] | 6f37144 | 2011-11-09 06:45:46 | [diff] [blame] | 356 | int requesting_process_id, |
| 357 | const extensions::ProcessMap& process_map, |
[email protected] | 5bc248a | 2012-04-04 23:38:11 | [diff] [blame] | 358 | extensions::ExtensionAPI* api, |
[email protected] | 67351452 | 2011-07-13 18:17:18 | [diff] [blame] | 359 | void* profile, |
[email protected] | 74e21e7 | 2012-07-09 21:20:53 | [diff] [blame] | 360 | IPC::Sender* ipc_sender, |
[email protected] | 3d0e226 | 2012-08-02 15:32:16 | [diff] [blame] | 361 | RenderViewHost* render_view_host, |
[email protected] | 74e21e7 | 2012-07-09 21:20:53 | [diff] [blame] | 362 | int routing_id) { |
[email protected] | c357acb4 | 2011-06-09 20:52:42 | [diff] [blame] | 363 | if (!extension) { |
[email protected] | 6f37144 | 2011-11-09 06:45:46 | [diff] [blame] | 364 | LOG(ERROR) << "Specified extension does not exist."; |
[email protected] | 74e21e7 | 2012-07-09 21:20:53 | [diff] [blame] | 365 | SendAccessDenied(ipc_sender, routing_id, params.request_id); |
[email protected] | 6f37144 | 2011-11-09 06:45:46 | [diff] [blame] | 366 | return NULL; |
| 367 | } |
| 368 | |
[email protected] | f3354211 | 2013-02-04 16:52:38 | [diff] [blame^] | 369 | // Most hosted apps can't call APIs. |
| 370 | bool allowed = true; |
| 371 | if (extension->is_hosted_app()) |
| 372 | allowed = AllowHostedAppAPICall(*extension, params.source_url, |
| 373 | params.name); |
| 374 | |
| 375 | // Privileged APIs can only be called from the process the extension |
| 376 | // is running in. |
| 377 | if (allowed && api->IsPrivileged(params.name)) |
| 378 | allowed = process_map.Contains(extension->id(), requesting_process_id); |
| 379 | |
| 380 | if (!allowed) { |
| 381 | LOG(ERROR) << "Extension API call disallowed - name:" << params.name |
| 382 | << " pid:" << requesting_process_id |
[email protected] | 6f37144 | 2011-11-09 06:45:46 | [diff] [blame] | 383 | << " from URL " << params.source_url.spec(); |
[email protected] | 74e21e7 | 2012-07-09 21:20:53 | [diff] [blame] | 384 | SendAccessDenied(ipc_sender, routing_id, params.request_id); |
[email protected] | c357acb4 | 2011-06-09 20:52:42 | [diff] [blame] | 385 | return NULL; |
| 386 | } |
| 387 | |
[email protected] | c357acb4 | 2011-06-09 20:52:42 | [diff] [blame] | 388 | ExtensionFunction* function = |
[email protected] | ae33d32 | 2012-03-19 22:24:35 | [diff] [blame] | 389 | ExtensionFunctionRegistry::GetInstance()->NewFunction(params.name); |
[email protected] | c357acb4 | 2011-06-09 20:52:42 | [diff] [blame] | 390 | function->SetArgs(¶ms.arguments); |
| 391 | function->set_source_url(params.source_url); |
| 392 | function->set_request_id(params.request_id); |
| 393 | function->set_has_callback(params.has_callback); |
| 394 | function->set_user_gesture(params.user_gesture); |
| 395 | function->set_extension(extension); |
[email protected] | 637bf32 | 2011-10-01 20:46:32 | [diff] [blame] | 396 | function->set_profile_id(profile); |
[email protected] | 3d0e226 | 2012-08-02 15:32:16 | [diff] [blame] | 397 | |
| 398 | UIThreadExtensionFunction* function_ui = |
| 399 | function->AsUIThreadExtensionFunction(); |
| 400 | if (function_ui) { |
| 401 | function_ui->SetRenderViewHost(render_view_host); |
| 402 | } |
| 403 | |
[email protected] | c357acb4 | 2011-06-09 20:52:42 | [diff] [blame] | 404 | return function; |
| 405 | } |
| 406 | |
| 407 | // static |
[email protected] | c5dbef0 | 2011-05-13 05:06:09 | [diff] [blame] | 408 | void ExtensionFunctionDispatcher::SendAccessDenied( |
[email protected] | 74e21e7 | 2012-07-09 21:20:53 | [diff] [blame] | 409 | IPC::Sender* ipc_sender, int routing_id, int request_id) { |
[email protected] | 602542d | 2012-04-20 02:48:01 | [diff] [blame] | 410 | ListValue empty_list; |
[email protected] | c357acb4 | 2011-06-09 20:52:42 | [diff] [blame] | 411 | ipc_sender->Send(new ExtensionMsg_Response( |
[email protected] | 74e21e7 | 2012-07-09 21:20:53 | [diff] [blame] | 412 | routing_id, request_id, false, empty_list, |
[email protected] | c5dbef0 | 2011-05-13 05:06:09 | [diff] [blame] | 413 | "Access to extension API denied.")); |
[email protected] | bfdffe2b | 2009-04-24 22:05:35 | [diff] [blame] | 414 | } |