Home | History | Annotate | Download | only in browser
      1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #ifndef CONTENT_BROWSER_SITE_INSTANCE_IMPL_H_
      6 #define CONTENT_BROWSER_SITE_INSTANCE_IMPL_H_
      7 
      8 #include "content/browser/renderer_host/render_process_host_impl.h"
      9 #include "content/common/content_export.h"
     10 #include "content/public/browser/render_process_host_observer.h"
     11 #include "content/public/browser/site_instance.h"
     12 #include "url/gurl.h"
     13 
     14 namespace content {
     15 class RenderProcessHostFactory;
     16 
     17 class CONTENT_EXPORT SiteInstanceImpl : public SiteInstance,
     18                                         public RenderProcessHostObserver {
     19  public:
     20   // SiteInstance interface overrides.
     21   virtual int32 GetId() OVERRIDE;
     22   virtual bool HasProcess() const OVERRIDE;
     23   virtual RenderProcessHost* GetProcess() OVERRIDE;
     24   virtual const GURL& GetSiteURL() const OVERRIDE;
     25   virtual SiteInstance* GetRelatedSiteInstance(const GURL& url) OVERRIDE;
     26   virtual bool IsRelatedSiteInstance(const SiteInstance* instance) OVERRIDE;
     27   virtual BrowserContext* GetBrowserContext() const OVERRIDE;
     28 
     29   // Set the web site that this SiteInstance is rendering pages for.
     30   // This includes the scheme and registered domain, but not the port.  If the
     31   // URL does not have a valid registered domain, then the full hostname is
     32   // stored.
     33   void SetSite(const GURL& url);
     34   bool HasSite() const;
     35 
     36   // Returns whether there is currently a related SiteInstance (registered with
     37   // BrowsingInstance) for the site of the given url.  If so, we should try to
     38   // avoid dedicating an unused SiteInstance to it (e.g., in a new tab).
     39   bool HasRelatedSiteInstance(const GURL& url);
     40 
     41   // Returns whether this SiteInstance has a process that is the wrong type for
     42   // the given URL.  If so, the browser should force a process swap when
     43   // navigating to the URL.
     44   bool HasWrongProcessForURL(const GURL& url);
     45 
     46   // Increase the number of active views in this SiteInstance. This is
     47   // increased when a view is created, or a currently swapped out view
     48   // is swapped in.
     49   void increment_active_view_count() { active_view_count_++; }
     50 
     51   // Decrease the number of active views in this SiteInstance. This is
     52   // decreased when a view is destroyed, or a currently active view is
     53   // swapped out.
     54   void decrement_active_view_count() { active_view_count_--; }
     55 
     56   // Get the number of active views which belong to this
     57   // SiteInstance. If there is no active view left in this
     58   // SiteInstance, all view in this SiteInstance can be safely
     59   // discarded to save memory.
     60   size_t active_view_count() { return active_view_count_; }
     61 
     62   // Sets the global factory used to create new RenderProcessHosts.  It may be
     63   // NULL, in which case the default BrowserRenderProcessHost will be created
     64   // (this is the behavior if you don't call this function).  The factory must
     65   // be set back to NULL before it's destroyed; ownership is not transferred.
     66   static void set_render_process_host_factory(
     67       const RenderProcessHostFactory* rph_factory);
     68 
     69   // Get the effective URL for the given actual URL.  This allows the
     70   // ContentBrowserClient to override the SiteInstance's site for certain URLs.
     71   // For example, Chrome uses this to replace hosted app URLs with extension
     72   // hosts.
     73   // Only public so that we can make a consistent process swap decision in
     74   // RenderFrameHostManager.
     75   static GURL GetEffectiveURL(BrowserContext* browser_context,
     76                               const GURL& url);
     77 
     78  protected:
     79   friend class BrowsingInstance;
     80   friend class SiteInstance;
     81 
     82   // Virtual to allow tests to extend it.
     83   virtual ~SiteInstanceImpl();
     84 
     85   // Create a new SiteInstance.  Protected to give access to BrowsingInstance
     86   // and tests; most callers should use Create or GetRelatedSiteInstance
     87   // instead.
     88   explicit SiteInstanceImpl(BrowsingInstance* browsing_instance);
     89 
     90  private:
     91   // RenderProcessHostObserver implementation.
     92   virtual void RenderProcessHostDestroyed(RenderProcessHost* host) OVERRIDE;
     93 
     94   // Used to restrict a process' origin access rights.
     95   void LockToOrigin();
     96 
     97   // An object used to construct RenderProcessHosts.
     98   static const RenderProcessHostFactory* g_render_process_host_factory_;
     99 
    100   // The next available SiteInstance ID.
    101   static int32 next_site_instance_id_;
    102 
    103   // A unique ID for this SiteInstance.
    104   int32 id_;
    105 
    106   // The number of active views under this SiteInstance.
    107   size_t active_view_count_;
    108 
    109   // BrowsingInstance to which this SiteInstance belongs.
    110   scoped_refptr<BrowsingInstance> browsing_instance_;
    111 
    112   // Current RenderProcessHost that is rendering pages for this SiteInstance.
    113   // This pointer will only change once the RenderProcessHost is destructed.  It
    114   // will still remain the same even if the process crashes, since in that
    115   // scenario the RenderProcessHost remains the same.
    116   RenderProcessHost* process_;
    117 
    118   // The web site that this SiteInstance is rendering pages for.
    119   GURL site_;
    120 
    121   // Whether SetSite has been called.
    122   bool has_site_;
    123 
    124   DISALLOW_COPY_AND_ASSIGN(SiteInstanceImpl);
    125 };
    126 
    127 }  // namespace content
    128 
    129 #endif  // CONTENT_BROWSER_SITE_INSTANCE_IMPL_H_
    130