123730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)// Copyright 2014 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)
523730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)#ifndef EXTENSIONS_BROWSER_EXTENSION_HOST_H_
623730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)#define EXTENSIONS_BROWSER_EXTENSION_HOST_H_
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <string>
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <vector>
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/logging.h"
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/memory/scoped_ptr.h"
134e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#include "base/timer/elapsed_timer.h"
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/notification_observer.h"
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/notification_registrar.h"
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/web_contents_delegate.h"
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/web_contents_observer.h"
18a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "extensions/browser/extension_function_dispatcher.h"
1958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#include "extensions/common/stack_frame.h"
20c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "extensions/common/view_type.h"
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class PrefsTabHelper;
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace content {
251e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)class BrowserContext;
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class RenderProcessHost;
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class RenderWidgetHostView;
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class SiteInstance;
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace extensions {
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class Extension;
3323730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)class ExtensionHostDelegate;
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class WindowController;
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This class is the browser component of an extension component's RenderView.
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// It handles setting up the renderer process, if needed, with special
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// privileges available to extensions.  It may have a view to be shown in the
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// browser UI, or it may be hidden.
4023730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)//
4123730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)// If you are adding code that only affects visible extension views (and not
4223730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)// invisible background pages) you should add it to ExtensionViewHost.
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class ExtensionHost : public content::WebContentsDelegate,
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                      public content::WebContentsObserver,
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                      public ExtensionFunctionDispatcher::Delegate,
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                      public content::NotificationObserver {
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  class ProcessCreationQueue;
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ExtensionHost(const Extension* extension,
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                content::SiteInstance* site_instance,
52c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                const GURL& url, ViewType host_type);
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual ~ExtensionHost();
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const Extension* extension() const { return extension_; }
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const std::string& extension_id() const { return extension_id_; }
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  content::WebContents* host_contents() const { return host_contents_.get(); }
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  content::RenderViewHost* render_view_host() const;
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  content::RenderProcessHost* render_process_host() const;
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool did_stop_loading() const { return did_stop_loading_; }
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool document_element_available() const {
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return document_element_available_;
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
65f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  content::BrowserContext* browser_context() { return browser_context_; }
661e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
67c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  ViewType extension_host_type() const { return extension_host_type_; }
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const GURL& GetURL() const;
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns true if the render view is initialized and didn't crash.
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool IsRenderViewLive() const;
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Prepares to initializes our RenderViewHost by creating its RenderView and
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // navigating to this host's url. Uses host_view for the RenderViewHost's view
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // (can be NULL). This happens delayed to avoid locking the UI.
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void CreateRenderViewSoon();
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // content::WebContentsObserver
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void RenderViewCreated(
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      content::RenderViewHost* render_view_host) OVERRIDE;
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void RenderViewDeleted(
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      content::RenderViewHost* render_view_host) OVERRIDE;
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void RenderViewReady() OVERRIDE;
857dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  virtual void RenderProcessGone(base::TerminationStatus status) OVERRIDE;
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void DocumentAvailableInMainFrame() OVERRIDE;
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void DidStopLoading(
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      content::RenderViewHost* render_view_host) OVERRIDE;
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // content::WebContentsDelegate
912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual content::JavaScriptDialogManager*
922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      GetJavaScriptDialogManager() OVERRIDE;
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void AddNewContents(content::WebContents* source,
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                              content::WebContents* new_contents,
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                              WindowOpenDisposition disposition,
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                              const gfx::Rect& initial_pos,
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                              bool user_gesture,
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                              bool* was_blocked) OVERRIDE;
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void CloseContents(content::WebContents* contents) OVERRIDE;
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void RequestMediaAccessPermission(
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      content::WebContents* web_contents,
1022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      const content::MediaStreamRequest& request,
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      const content::MediaResponseCallback& callback) OVERRIDE;
1040529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  virtual bool IsNeverVisible(content::WebContents* web_contents) OVERRIDE;
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // content::NotificationObserver
1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void Observe(int type,
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                       const content::NotificationSource& source,
1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                       const content::NotificationDetails& details) OVERRIDE;
1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
111f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) protected:
112a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  content::NotificationRegistrar* registrar() { return &registrar_; }
113a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
114f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // Called after the extension page finishes loading but before the
115f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // EXTENSION_HOST_DID_STOP_LOADING notification is sent.
116f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  virtual void OnDidStopLoading();
117f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
118f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // Called once when the document first becomes available.
119f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  virtual void OnDocumentAvailable();
120f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
121a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // Navigates to the initial page.
122a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  virtual void LoadInitialURL();
123a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
124f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // Returns true if we're hosting a background page.
125f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  virtual bool IsBackgroundPage() const;
126f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
127f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // Closes this host (results in deletion).
128f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  void Close();
129f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  friend class ProcessCreationQueue;
1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Actually create the RenderView for this host. See CreateRenderViewSoon.
1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void CreateRenderViewNow();
1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Message handlers.
1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void OnRequest(const ExtensionHostMsg_Request_Params& params);
1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void OnEventAck();
1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void OnIncrementLazyKeepaliveCount();
1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void OnDecrementLazyKeepaliveCount();
14123730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)
14223730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  // Delegate for functionality that cannot exist in the extensions module.
14323730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  scoped_ptr<ExtensionHostDelegate> delegate_;
1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The extension that we're hosting in this view.
1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const Extension* extension_;
1475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Id of extension that we're hosting in this view.
1495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const std::string extension_id_;
1505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
151f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // The browser context that this host is tied to.
152f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  content::BrowserContext* browser_context_;
1535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The host for our HTML content.
1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_ptr<content::WebContents> host_contents_;
1565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // A weak pointer to the current or pending RenderViewHost. We don't access
1585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // this through the host_contents because we want to deal with the pending
1595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // host, so we can send messages to it before it finishes loading.
1605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  content::RenderViewHost* render_view_host_;
1615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Whether the RenderWidget has reported that it has stopped loading.
1635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool did_stop_loading_;
1645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // True if the main frame has finished parsing.
1665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool document_element_available_;
1675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The original URL of the page being hosted.
1695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  GURL initial_url_;
1705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  content::NotificationRegistrar registrar_;
1725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ExtensionFunctionDispatcher extension_function_dispatcher_;
1745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The type of view being hosted.
176c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  ViewType extension_host_type_;
1775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Used to measure how long it's been since the host was created.
1794e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  base::ElapsedTimer since_created_;
1805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(ExtensionHost);
1825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
1835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace extensions
1855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
18623730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)#endif  // EXTENSIONS_BROWSER_EXTENSION_HOST_H_
187