15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved. 25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file. 45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef CONTENT_PUBLIC_BROWSER_SITE_INSTANCE_H_ 65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define CONTENT_PUBLIC_BROWSER_SITE_INSTANCE_H_ 75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/basictypes.h" 95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/memory/ref_counted.h" 105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/common/content_export.h" 117dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch#include "url/gurl.h" 125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace content { 145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class BrowserContext; 155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class RenderProcessHost; 165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/////////////////////////////////////////////////////////////////////////////// 185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// SiteInstance interface. 195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// A SiteInstance represents a group of web pages that may be able to 215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// synchronously script each other, and thus must live in the same renderer 225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// process. 235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// We identify this group using a combination of where the page comes from 255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// (the site) and which tabs have references to each other (the instance). 265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Here, a "site" is similar to the page's origin, but it only includes the 275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// registered domain name and scheme, not the port or subdomains. This accounts 285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// for the fact that changes to document.domain allow similar origin pages with 295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// different ports or subdomains to script each other. An "instance" includes 305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// all tabs that might be able to script each other because of how they were 315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// created (e.g., window.open or targeted links). We represent instances using 325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// the BrowsingInstance class. 335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Process models: 355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// In process-per-site-instance (the current default process model), 375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// SiteInstances are created (1) when the user manually creates a new tab 385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// (which also creates a new BrowsingInstance), and (2) when the user navigates 395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// across site boundaries (which uses the same BrowsingInstance). If the user 405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// navigates within a site, the same SiteInstance is used. 415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// (Caveat: we currently allow renderer-initiated cross-site navigations to 425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// stay in the same SiteInstance, to preserve compatibility in cases like 435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// cross-site iframes that open popups.) 445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// In --process-per-tab, SiteInstances are created when the user manually 465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// creates a new tab, but not when navigating across site boundaries (unless 475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// a process swap is required for security reasons, such as navigating from 485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// a privileged WebUI page to a normal web page). This corresponds to one 495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// process per BrowsingInstance. 505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// In --process-per-site, we consolidate all SiteInstances for a given site into 525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// the same process, throughout the entire browser context. This ensures that 535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// only one process will be used for each site. 545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Each NavigationEntry for a WebContents points to the SiteInstance that 565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// rendered it. Each RenderViewHost also points to the SiteInstance that it is 575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// associated with. A SiteInstance keeps track of the number of these 585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// references and deletes itself when the count goes to zero. This means that 595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// a SiteInstance is only live as long as it is accessible, either from new 605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// tabs with no NavigationEntries or in NavigationEntries in the history. 615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/////////////////////////////////////////////////////////////////////////////// 635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class CONTENT_EXPORT SiteInstance : public base::RefCounted<SiteInstance> { 645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public: 655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Returns a unique ID for this SiteInstance. 665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual int32 GetId() = 0; 675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Whether this SiteInstance has a running process associated with it. 695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // This may return true before the first call to GetProcess(), in cases where 705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // we use process-per-site and there is an existing process available. 715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual bool HasProcess() const = 0; 725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 73f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) // Returns the current RenderProcessHost being used to render pages for this 74f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) // SiteInstance. If there is no RenderProcessHost (because either none has 75f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) // yet been created or there was one but it was cleanly destroyed (e.g. when 76f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) // it is not actively being used)), then this method will create a new 77f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) // RenderProcessHost (and a new ID). Note that renderer process crashes leave 78f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) // the current RenderProcessHost (and ID) in place. 795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // 805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // For sites that require process-per-site mode (e.g., WebUI), this will 815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // ensure only one RenderProcessHost for the site exists/ within the 825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // BrowserContext. 835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual content::RenderProcessHost* GetProcess() = 0; 845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Browser context to which this SiteInstance (and all related 865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // SiteInstances) belongs. 875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual content::BrowserContext* GetBrowserContext() const = 0; 885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Get the web site that this SiteInstance is rendering pages for. 905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // This includes the scheme and registered domain, but not the port. 915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual const GURL& GetSiteURL() const = 0; 925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Gets a SiteInstance for the given URL that shares the current 945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // BrowsingInstance, creating a new SiteInstance if necessary. This ensures 955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // that a BrowsingInstance only has one SiteInstance per site, so that pages 965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // in a BrowsingInstance have the ability to script each other. Callers 975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // should ensure that this SiteInstance becomes ref counted, by storing it in 985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // a scoped_refptr. (By having this method, we can hide the BrowsingInstance 995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // class from the rest of the codebase.) 1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // TODO(creis): This may be an argument to build a pass_refptr<T> class, as 1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Darin suggests. 1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual SiteInstance* GetRelatedSiteInstance(const GURL& url) = 0; 1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Returns whether the given SiteInstance is in the same BrowsingInstance as 1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // this one. If so, JavaScript interactions that are permitted across 1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // origins (e.g., postMessage) should be supported. 1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual bool IsRelatedSiteInstance(const SiteInstance* instance) = 0; 1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 109cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) // Returns the total active WebContents count for this SiteInstance and all 110cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) // related SiteInstances in the same BrowsingInstance. 111cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) virtual size_t GetRelatedActiveContentsCount() = 0; 112cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Factory method to create a new SiteInstance. This will create a new 1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // new BrowsingInstance, so it should only be used when creating a new tab 1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // from scratch (or similar circumstances). Callers should ensure that 1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // this SiteInstance becomes ref counted, by storing it in a scoped_refptr. 1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // 1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // The render process host factory may be NULL. See SiteInstance constructor. 1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // 1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // TODO(creis): This may be an argument to build a pass_refptr<T> class, as 1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Darin suggests. 1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) static SiteInstance* Create(content::BrowserContext* browser_context); 1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Factory method to get the appropriate SiteInstance for the given URL, in 1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // a new BrowsingInstance. Use this instead of Create when you know the URL, 1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // since it allows special site grouping rules to be applied (for example, 1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // to group chrome-ui pages into the same instance). 1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) static SiteInstance* CreateForURL( 1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) content::BrowserContext* browser_context, const GURL& url); 1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Return whether both URLs are part of the same web site, for the purpose of 1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // assigning them to processes accordingly. The decision is currently based 1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // on the registered domain of the URLs (google.com, bbc.co.uk), as well as 1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // the scheme (https, http). This ensures that two pages will be in 1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // the same process if they can communicate with other via JavaScript. 1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // (e.g., docs.google.com and mail.google.com have DOM access to each other 1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // if they both set their document.domain properties to google.com.) 1385f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) // Note that if the destination is a blank page, we consider that to be part 1395f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) // of the same web site for the purposes for process assignment. 1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) static bool IsSameWebSite(content::BrowserContext* browser_context, 1415f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) const GURL& src_url, 1425f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) const GURL& dest_url); 1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // Returns the site for the given URL, which includes only the scheme and 1452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // registered domain. Returns an empty GURL if the URL has no host. 1462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) static GURL GetSiteForURL(BrowserContext* context, const GURL& url); 1472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) protected: 1495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) friend class base::RefCounted<SiteInstance>; 1505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) SiteInstance() {} 1525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual ~SiteInstance() {} 1535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}; 1545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} // namespace content. 1565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif // CONTENT_PUBLIC_BROWSER_SITE_INSTANCE_H_ 158