Home | History | Annotate | Download | only in browser
      1 // Copyright 2014 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 EXTENSIONS_BROWSER_EXTENSION_HOST_H_
      6 #define EXTENSIONS_BROWSER_EXTENSION_HOST_H_
      7 
      8 #include <string>
      9 #include <vector>
     10 
     11 #include "base/logging.h"
     12 #include "base/memory/scoped_ptr.h"
     13 #include "base/timer/elapsed_timer.h"
     14 #include "content/public/browser/notification_observer.h"
     15 #include "content/public/browser/notification_registrar.h"
     16 #include "content/public/browser/web_contents_delegate.h"
     17 #include "content/public/browser/web_contents_observer.h"
     18 #include "extensions/browser/extension_function_dispatcher.h"
     19 #include "extensions/common/stack_frame.h"
     20 #include "extensions/common/view_type.h"
     21 
     22 class PrefsTabHelper;
     23 
     24 namespace content {
     25 class BrowserContext;
     26 class RenderProcessHost;
     27 class RenderWidgetHostView;
     28 class SiteInstance;
     29 }
     30 
     31 namespace extensions {
     32 class Extension;
     33 class ExtensionHostDelegate;
     34 class WindowController;
     35 
     36 // This class is the browser component of an extension component's RenderView.
     37 // It handles setting up the renderer process, if needed, with special
     38 // privileges available to extensions.  It may have a view to be shown in the
     39 // browser UI, or it may be hidden.
     40 //
     41 // If you are adding code that only affects visible extension views (and not
     42 // invisible background pages) you should add it to ExtensionViewHost.
     43 class ExtensionHost : public content::WebContentsDelegate,
     44                       public content::WebContentsObserver,
     45                       public ExtensionFunctionDispatcher::Delegate,
     46                       public content::NotificationObserver {
     47  public:
     48   class ProcessCreationQueue;
     49 
     50   ExtensionHost(const Extension* extension,
     51                 content::SiteInstance* site_instance,
     52                 const GURL& url, ViewType host_type);
     53   virtual ~ExtensionHost();
     54 
     55   const Extension* extension() const { return extension_; }
     56   const std::string& extension_id() const { return extension_id_; }
     57   content::WebContents* host_contents() const { return host_contents_.get(); }
     58   content::RenderViewHost* render_view_host() const;
     59   content::RenderProcessHost* render_process_host() const;
     60   bool did_stop_loading() const { return did_stop_loading_; }
     61   bool document_element_available() const {
     62     return document_element_available_;
     63   }
     64 
     65   content::BrowserContext* browser_context() { return browser_context_; }
     66 
     67   ViewType extension_host_type() const { return extension_host_type_; }
     68   const GURL& GetURL() const;
     69 
     70   // Returns true if the render view is initialized and didn't crash.
     71   bool IsRenderViewLive() const;
     72 
     73   // Prepares to initializes our RenderViewHost by creating its RenderView and
     74   // navigating to this host's url. Uses host_view for the RenderViewHost's view
     75   // (can be NULL). This happens delayed to avoid locking the UI.
     76   void CreateRenderViewSoon();
     77 
     78   // content::WebContentsObserver
     79   virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
     80   virtual void RenderViewCreated(
     81       content::RenderViewHost* render_view_host) OVERRIDE;
     82   virtual void RenderViewDeleted(
     83       content::RenderViewHost* render_view_host) OVERRIDE;
     84   virtual void RenderViewReady() OVERRIDE;
     85   virtual void RenderProcessGone(base::TerminationStatus status) OVERRIDE;
     86   virtual void DocumentAvailableInMainFrame() OVERRIDE;
     87   virtual void DidStopLoading(
     88       content::RenderViewHost* render_view_host) OVERRIDE;
     89 
     90   // content::WebContentsDelegate
     91   virtual content::JavaScriptDialogManager*
     92       GetJavaScriptDialogManager() OVERRIDE;
     93   virtual void AddNewContents(content::WebContents* source,
     94                               content::WebContents* new_contents,
     95                               WindowOpenDisposition disposition,
     96                               const gfx::Rect& initial_pos,
     97                               bool user_gesture,
     98                               bool* was_blocked) OVERRIDE;
     99   virtual void CloseContents(content::WebContents* contents) OVERRIDE;
    100   virtual void RequestMediaAccessPermission(
    101       content::WebContents* web_contents,
    102       const content::MediaStreamRequest& request,
    103       const content::MediaResponseCallback& callback) OVERRIDE;
    104   virtual bool CheckMediaAccessPermission(
    105       content::WebContents* web_contents,
    106       const GURL& security_origin,
    107       content::MediaStreamType type) OVERRIDE;
    108   virtual bool IsNeverVisible(content::WebContents* web_contents) OVERRIDE;
    109 
    110   // content::NotificationObserver
    111   virtual void Observe(int type,
    112                        const content::NotificationSource& source,
    113                        const content::NotificationDetails& details) OVERRIDE;
    114 
    115  protected:
    116   content::NotificationRegistrar* registrar() { return &registrar_; }
    117 
    118   // Called after the extension page finishes loading but before the
    119   // EXTENSION_HOST_DID_STOP_LOADING notification is sent.
    120   virtual void OnDidStopLoading();
    121 
    122   // Called once when the document first becomes available.
    123   virtual void OnDocumentAvailable();
    124 
    125   // Navigates to the initial page.
    126   virtual void LoadInitialURL();
    127 
    128   // Returns true if we're hosting a background page.
    129   virtual bool IsBackgroundPage() const;
    130 
    131   // Closes this host (results in deletion).
    132   void Close();
    133 
    134  private:
    135   friend class ProcessCreationQueue;
    136 
    137   // Actually create the RenderView for this host. See CreateRenderViewSoon.
    138   void CreateRenderViewNow();
    139 
    140   // Message handlers.
    141   void OnRequest(const ExtensionHostMsg_Request_Params& params);
    142   void OnEventAck();
    143   void OnIncrementLazyKeepaliveCount();
    144   void OnDecrementLazyKeepaliveCount();
    145 
    146   // Delegate for functionality that cannot exist in the extensions module.
    147   scoped_ptr<ExtensionHostDelegate> delegate_;
    148 
    149   // The extension that we're hosting in this view.
    150   const Extension* extension_;
    151 
    152   // Id of extension that we're hosting in this view.
    153   const std::string extension_id_;
    154 
    155   // The browser context that this host is tied to.
    156   content::BrowserContext* browser_context_;
    157 
    158   // The host for our HTML content.
    159   scoped_ptr<content::WebContents> host_contents_;
    160 
    161   // A weak pointer to the current or pending RenderViewHost. We don't access
    162   // this through the host_contents because we want to deal with the pending
    163   // host, so we can send messages to it before it finishes loading.
    164   content::RenderViewHost* render_view_host_;
    165 
    166   // Whether the RenderWidget has reported that it has stopped loading.
    167   bool did_stop_loading_;
    168 
    169   // True if the main frame has finished parsing.
    170   bool document_element_available_;
    171 
    172   // The original URL of the page being hosted.
    173   GURL initial_url_;
    174 
    175   content::NotificationRegistrar registrar_;
    176 
    177   ExtensionFunctionDispatcher extension_function_dispatcher_;
    178 
    179   // The type of view being hosted.
    180   ViewType extension_host_type_;
    181 
    182   // Used to measure how long it's been since the host was created.
    183   base::ElapsedTimer since_created_;
    184 
    185   DISALLOW_COPY_AND_ASSIGN(ExtensionHost);
    186 };
    187 
    188 }  // namespace extensions
    189 
    190 #endif  // EXTENSIONS_BROWSER_EXTENSION_HOST_H_
    191