1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/extensions/chrome_content_browser_client_extensions_part.h"
#include <set>
#include "base/command_line.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/extensions/browser_permissions_policy_delegate.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/extensions/extension_web_ui.h"
#include "chrome/browser/extensions/extension_webkit_preferences.h"
#include "chrome/browser/media_galleries/fileapi/media_file_system_backend.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_io_data.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/renderer_host/chrome_extension_message_filter.h"
#include "chrome/browser/sync_file_system/local/sync_file_system_backend.h"
#include "chrome/common/chrome_constants.h"
#include "chrome/common/extensions/extension_process_policy.h"
#include "chrome/common/extensions/manifest_handlers/app_isolation_info.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/browser_url_handler.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/browser/site_instance.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/content_switches.h"
#include "extensions/browser/api/web_request/web_request_api.h"
#include "extensions/browser/api/web_request/web_request_api_helpers.h"
#include "extensions/browser/extension_host.h"
#include "extensions/browser/extension_message_filter.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/browser/extension_system.h"
#include "extensions/browser/info_map.h"
#include "extensions/browser/view_type_utils.h"
#include "extensions/common/constants.h"
#include "extensions/common/manifest_handlers/background_info.h"
#include "extensions/common/manifest_handlers/web_accessible_resources_info.h"
#include "extensions/common/switches.h"
using content::BrowserContext;
using content::BrowserThread;
using content::BrowserURLHandler;
using content::RenderViewHost;
using content::SiteInstance;
using content::WebContents;
using content::WebPreferences;
namespace extensions {
namespace {
// Used by the GetPrivilegeRequiredByUrl() and GetProcessPrivilege() functions
// below. Extension, and isolated apps require different privileges to be
// granted to their RenderProcessHosts. This classification allows us to make
// sure URLs are served by hosts with the right set of privileges.
enum RenderProcessHostPrivilege {
PRIV_NORMAL,
PRIV_HOSTED,
PRIV_ISOLATED,
PRIV_EXTENSION,
};
RenderProcessHostPrivilege GetPrivilegeRequiredByUrl(
const GURL& url,
ExtensionRegistry* registry) {
// Default to a normal renderer cause it is lower privileged. This should only
// occur if the URL on a site instance is either malformed, or uninitialized.
// If it is malformed, then there is no need for better privileges anyways.
// If it is uninitialized, but eventually settles on being an a scheme other
// than normal webrenderer, the navigation logic will correct us out of band
// anyways.
if (!url.is_valid())
return PRIV_NORMAL;
if (!url.SchemeIs(kExtensionScheme))
return PRIV_NORMAL;
const Extension* extension =
registry->enabled_extensions().GetByID(url.host());
if (extension && AppIsolationInfo::HasIsolatedStorage(extension))
return PRIV_ISOLATED;
if (extension && extension->is_hosted_app())
return PRIV_HOSTED;
return PRIV_EXTENSION;
}
RenderProcessHostPrivilege GetProcessPrivilege(
content::RenderProcessHost* process_host,
ProcessMap* process_map,
ExtensionRegistry* registry) {
std::set<std::string> extension_ids =
process_map->GetExtensionsInProcess(process_host->GetID());
if (extension_ids.empty())
return PRIV_NORMAL;
for (const std::string& extension_id : extension_ids) {
const Extension* extension =
registry->enabled_extensions().GetByID(extension_id);
if (extension && AppIsolationInfo::HasIsolatedStorage(extension))
return PRIV_ISOLATED;
if (extension && extension->is_hosted_app())
return PRIV_HOSTED;
}
return PRIV_EXTENSION;
}
} // namespace
ChromeContentBrowserClientExtensionsPart::
ChromeContentBrowserClientExtensionsPart() {
permissions_policy_delegate_.reset(new BrowserPermissionsPolicyDelegate());
}
ChromeContentBrowserClientExtensionsPart::
~ChromeContentBrowserClientExtensionsPart() {
}
// static
GURL ChromeContentBrowserClientExtensionsPart::GetEffectiveURL(
Profile* profile, const GURL& url) {
// If the input |url| is part of an installed app, the effective URL is an
// extension URL with the ID of that extension as the host. This has the
// effect of grouping apps together in a common SiteInstance.
ExtensionRegistry* registry = ExtensionRegistry::Get(profile);
if (!registry)
return url;
const Extension* extension =
registry->enabled_extensions().GetHostedAppByURL(url);
if (!extension)
return url;
// Bookmark apps do not use the hosted app process model, and should be
// treated as normal URLs.
if (extension->from_bookmark())
return url;
// If the URL is part of an extension's web extent, convert it to an
// extension URL.
return extension->GetResourceURL(url.path());
}
// static
bool ChromeContentBrowserClientExtensionsPart::ShouldUseProcessPerSite(
Profile* profile, const GURL& effective_url) {
if (!effective_url.SchemeIs(kExtensionScheme))
return false;
ExtensionRegistry* registry = ExtensionRegistry::Get(profile);
if (!registry)
return false;
const Extension* extension =
registry->enabled_extensions().GetByID(effective_url.host());
if (!extension)
return false;
// If the URL is part of a hosted app that does not have the background
// permission, or that does not allow JavaScript access to the background
// page, we want to give each instance its own process to improve
// responsiveness.
if (extension->GetType() == Manifest::TYPE_HOSTED_APP) {
if (!extension->permissions_data()->HasAPIPermission(
APIPermission::kBackground) ||
!BackgroundInfo::AllowJSAccess(extension)) {
return false;
}
}
// Hosted apps that have script access to their background page must use
// process per site, since all instances can make synchronous calls to the
// background window. Other extensions should use process per site as well.
return true;
}
// static
bool ChromeContentBrowserClientExtensionsPart::CanCommitURL(
content::RenderProcessHost* process_host, const GURL& url) {
// We need to let most extension URLs commit in any process, since this can
// be allowed due to web_accessible_resources. Most hosted app URLs may also
// load in any process (e.g., in an iframe). However, the Chrome Web Store
// cannot be loaded in iframes and should never be requested outside its
// process.
ExtensionRegistry* registry =
ExtensionRegistry::Get(process_host->GetBrowserContext());
if (!registry)
return true;
const Extension* new_extension =
registry->enabled_extensions().GetExtensionOrAppByURL(url);
if (new_extension && new_extension->is_hosted_app() &&
new_extension->id() == extensions::kWebStoreAppId &&
!ProcessMap::Get(process_host->GetBrowserContext())
->Contains(new_extension->id(), process_host->GetID())) {
return false;
}
return true;
}
// static
bool ChromeContentBrowserClientExtensionsPart::IsSuitableHost(
Profile* profile,
content::RenderProcessHost* process_host,
const GURL& site_url) {
DCHECK(profile);
ExtensionRegistry* registry = ExtensionRegistry::Get(profile);
ProcessMap* process_map = ProcessMap::Get(profile);
// These may be NULL during tests. In that case, just assume any site can
// share any host.
if (!registry || !process_map)
return true;
// Otherwise, just make sure the process privilege matches the privilege
// required by the site.
RenderProcessHostPrivilege privilege_required =
GetPrivilegeRequiredByUrl(site_url, registry);
return GetProcessPrivilege(process_host, process_map, registry) ==
privilege_required;
}
// static
bool
ChromeContentBrowserClientExtensionsPart::ShouldTryToUseExistingProcessHost(
Profile* profile, const GURL& url) {
// This function is trying to limit the amount of processes used by extensions
// with background pages. It uses a globally set percentage of processes to
// run such extensions and if the limit is exceeded, it returns true, to
// indicate to the content module to group extensions together.
ExtensionRegistry* registry =
profile ? ExtensionRegistry::Get(profile) : NULL;
if (!registry)
return false;
// We have to have a valid extension with background page to proceed.
const Extension* extension =
registry->enabled_extensions().GetExtensionOrAppByURL(url);
if (!extension)
return false;
if (!BackgroundInfo::HasBackgroundPage(extension))
return false;
std::set<int> process_ids;
size_t max_process_count =
content::RenderProcessHost::GetMaxRendererProcessCount();
// Go through all profiles to ensure we have total count of extension
// processes containing background pages, otherwise one profile can
// starve the other.
std::vector<Profile*> profiles = g_browser_process->profile_manager()->
GetLoadedProfiles();
for (size_t i = 0; i < profiles.size(); ++i) {
ProcessManager* epm = ProcessManager::Get(profiles[i]);
for (ProcessManager::const_iterator iter = epm->background_hosts().begin();
iter != epm->background_hosts().end(); ++iter) {
const ExtensionHost* host = *iter;
process_ids.insert(host->render_process_host()->GetID());
}
}
return (process_ids.size() >
(max_process_count * chrome::kMaxShareOfExtensionProcesses));
}
// static
bool ChromeContentBrowserClientExtensionsPart::
ShouldSwapBrowsingInstancesForNavigation(SiteInstance* site_instance,
const GURL& current_url,
const GURL& new_url) {
// If we don't have an ExtensionRegistry, then rely on the SiteInstance logic
// in RenderFrameHostManager to decide when to swap.
ExtensionRegistry* registry =
ExtensionRegistry::Get(site_instance->GetBrowserContext());
if (!registry)
return false;
// We must use a new BrowsingInstance (forcing a process swap and disabling
// scripting by existing tabs) if one of the URLs is an extension and the
// other is not the exact same extension.
//
// We ignore hosted apps here so that other tabs in their BrowsingInstance can
// use postMessage with them. (The exception is the Chrome Web Store, which
// is a hosted app that requires its own BrowsingInstance.) Navigations
// to/from a hosted app will still trigger a SiteInstance swap in
// RenderFrameHostManager.
const Extension* current_extension =
registry->enabled_extensions().GetExtensionOrAppByURL(current_url);
if (current_extension &&
current_extension->is_hosted_app() &&
current_extension->id() != extensions::kWebStoreAppId)
current_extension = NULL;
const Extension* new_extension =
registry->enabled_extensions().GetExtensionOrAppByURL(new_url);
if (new_extension &&
new_extension->is_hosted_app() &&
new_extension->id() != extensions::kWebStoreAppId)
new_extension = NULL;
// First do a process check. We should force a BrowsingInstance swap if the
// current process doesn't know about new_extension, even if current_extension
// is somehow the same as new_extension.
ProcessMap* process_map = ProcessMap::Get(site_instance->GetBrowserContext());
if (new_extension &&
site_instance->HasProcess() &&
!process_map->Contains(
new_extension->id(), site_instance->GetProcess()->GetID()))
return true;
// Otherwise, swap BrowsingInstances if current_extension and new_extension
// differ.
return current_extension != new_extension;
}
// static
bool ChromeContentBrowserClientExtensionsPart::ShouldSwapProcessesForRedirect(
content::ResourceContext* resource_context,
const GURL& current_url,
const GURL& new_url) {
ProfileIOData* io_data = ProfileIOData::FromResourceContext(resource_context);
return CrossesExtensionProcessBoundary(
io_data->GetExtensionInfoMap()->extensions(),
current_url, new_url, false);
}
// static
bool ChromeContentBrowserClientExtensionsPart::ShouldAllowOpenURL(
content::SiteInstance* site_instance,
const GURL& from_url,
const GURL& to_url,
bool* result) {
DCHECK(result);
// Do not allow pages from the web or other extensions navigate to
// non-web-accessible extension resources.
if (to_url.SchemeIs(kExtensionScheme) &&
(from_url.SchemeIsHTTPOrHTTPS() || from_url.SchemeIs(kExtensionScheme))) {
Profile* profile = Profile::FromBrowserContext(
site_instance->GetProcess()->GetBrowserContext());
ExtensionRegistry* registry = ExtensionRegistry::Get(profile);
if (!registry) {
*result = true;
return true;
}
const Extension* extension =
registry->enabled_extensions().GetExtensionOrAppByURL(to_url);
if (!extension) {
*result = true;
return true;
}
const Extension* from_extension =
registry->enabled_extensions().GetExtensionOrAppByURL(
site_instance->GetSiteURL());
if (from_extension && from_extension->id() == extension->id()) {
*result = true;
return true;
}
if (!WebAccessibleResourcesInfo::IsResourceWebAccessible(
extension, to_url.path())) {
*result = false;
return true;
}
}
return false;
}
// static
void ChromeContentBrowserClientExtensionsPart::SetSigninProcess(
content::SiteInstance* site_instance) {
Profile* profile =
Profile::FromBrowserContext(site_instance->GetBrowserContext());
DCHECK(profile);
BrowserThread::PostTask(
BrowserThread::IO,
FROM_HERE,
base::Bind(&InfoMap::SetSigninProcess,
ExtensionSystem::Get(profile)->info_map(),
site_instance->GetProcess()->GetID()));
}
void ChromeContentBrowserClientExtensionsPart::RenderProcessWillLaunch(
content::RenderProcessHost* host) {
int id = host->GetID();
Profile* profile = Profile::FromBrowserContext(host->GetBrowserContext());
host->AddFilter(new ChromeExtensionMessageFilter(id, profile));
host->AddFilter(new ExtensionMessageFilter(id, profile));
extension_web_request_api_helpers::SendExtensionWebRequestStatusToHost(host);
}
void ChromeContentBrowserClientExtensionsPart::SiteInstanceGotProcess(
SiteInstance* site_instance) {
BrowserContext* context = site_instance->GetProcess()->GetBrowserContext();
ExtensionRegistry* registry = ExtensionRegistry::Get(context);
if (!registry)
return;
const Extension* extension =
registry->enabled_extensions().GetExtensionOrAppByURL(
site_instance->GetSiteURL());
if (!extension)
return;
ProcessMap::Get(context)->Insert(extension->id(),
site_instance->GetProcess()->GetID(),
site_instance->GetId());
BrowserThread::PostTask(
BrowserThread::IO, FROM_HERE,
base::Bind(&InfoMap::RegisterExtensionProcess,
ExtensionSystem::Get(context)->info_map(), extension->id(),
site_instance->GetProcess()->GetID(), site_instance->GetId()));
}
void ChromeContentBrowserClientExtensionsPart::SiteInstanceDeleting(
SiteInstance* site_instance) {
BrowserContext* context = site_instance->GetBrowserContext();
ExtensionRegistry* registry = ExtensionRegistry::Get(context);
if (!registry)
return;
const Extension* extension =
registry->enabled_extensions().GetExtensionOrAppByURL(
site_instance->GetSiteURL());
if (!extension)
return;
ProcessMap::Get(context)->Remove(extension->id(),
site_instance->GetProcess()->GetID(),
site_instance->GetId());
BrowserThread::PostTask(
BrowserThread::IO, FROM_HERE,
base::Bind(&InfoMap::UnregisterExtensionProcess,
ExtensionSystem::Get(context)->info_map(), extension->id(),
site_instance->GetProcess()->GetID(), site_instance->GetId()));
}
void ChromeContentBrowserClientExtensionsPart::OverrideWebkitPrefs(
RenderViewHost* rvh,
const GURL& url,
WebPreferences* web_prefs) {
const ExtensionRegistry* registry =
ExtensionRegistry::Get(rvh->GetProcess()->GetBrowserContext());
if (!registry)
return;
// Note: it's not possible for kExtensionsScheme to change during the lifetime
// of the process.
//
// Ensure that we are only granting extension preferences to URLs with
// the correct scheme. Without this check, chrome-guest:// schemes used by
// webview tags as well as hosts that happen to match the id of an
// installed extension would get the wrong preferences.
const GURL& site_url = rvh->GetSiteInstance()->GetSiteURL();
if (!site_url.SchemeIs(kExtensionScheme))
return;
WebContents* web_contents = WebContents::FromRenderViewHost(rvh);
ViewType view_type = GetViewType(web_contents);
const Extension* extension =
registry->enabled_extensions().GetByID(site_url.host());
extension_webkit_preferences::SetPreferences(extension, view_type, web_prefs);
}
void ChromeContentBrowserClientExtensionsPart::BrowserURLHandlerCreated(
BrowserURLHandler* handler) {
handler->AddHandlerPair(&ExtensionWebUI::HandleChromeURLOverride,
BrowserURLHandler::null_handler());
handler->AddHandlerPair(BrowserURLHandler::null_handler(),
&ExtensionWebUI::HandleChromeURLOverrideReverse);
}
void ChromeContentBrowserClientExtensionsPart::
GetAdditionalAllowedSchemesForFileSystem(
std::vector<std::string>* additional_allowed_schemes) {
additional_allowed_schemes->push_back(kExtensionScheme);
}
void ChromeContentBrowserClientExtensionsPart::GetURLRequestAutoMountHandlers(
std::vector<storage::URLRequestAutoMountHandler>* handlers) {
handlers->push_back(
base::Bind(MediaFileSystemBackend::AttemptAutoMountForURLRequest));
}
void ChromeContentBrowserClientExtensionsPart::GetAdditionalFileSystemBackends(
content::BrowserContext* browser_context,
const base::FilePath& storage_partition_path,
ScopedVector<storage::FileSystemBackend>* additional_backends) {
base::SequencedWorkerPool* pool = content::BrowserThread::GetBlockingPool();
additional_backends->push_back(new MediaFileSystemBackend(
storage_partition_path,
pool->GetSequencedTaskRunner(
pool->GetNamedSequenceToken(
MediaFileSystemBackend::kMediaTaskRunnerName)).get()));
additional_backends->push_back(new sync_file_system::SyncFileSystemBackend(
Profile::FromBrowserContext(browser_context)));
}
void ChromeContentBrowserClientExtensionsPart::
AppendExtraRendererCommandLineSwitches(base::CommandLine* command_line,
content::RenderProcessHost* process,
Profile* profile) {
if (!process)
return;
DCHECK(profile);
if (ProcessMap::Get(profile)->Contains(process->GetID())) {
command_line->AppendSwitch(switches::kExtensionProcess);
#if defined(ENABLE_WEBRTC)
command_line->AppendSwitch(::switches::kEnableWebRtcHWH264Encoding);
#endif
if (base::CommandLine::ForCurrentProcess()->HasSwitch(
switches::kEnableMojoSerialService)) {
command_line->AppendSwitch(switches::kEnableMojoSerialService);
}
}
}
} // namespace extensions
|