Home | History | Annotate | Download | only in webview
      1 // Copyright 2013 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 CHROME_BROWSER_GUESTVIEW_WEBVIEW_WEBVIEW_GUEST_H_
      6 #define CHROME_BROWSER_GUESTVIEW_WEBVIEW_WEBVIEW_GUEST_H_
      7 
      8 #include "base/observer_list.h"
      9 #include "chrome/browser/extensions/tab_helper.h"
     10 #include "chrome/browser/guestview/guestview.h"
     11 #include "content/public/browser/notification_registrar.h"
     12 #include "content/public/browser/web_contents_observer.h"
     13 
     14 namespace extensions {
     15 class ScriptExecutor;
     16 }  // namespace extensions
     17 
     18 // A WebViewGuest is a WebContentsObserver on the guest WebContents of a
     19 // <webview> tag. It provides the browser-side implementation of the <webview>
     20 // API and manages the lifetime of <webview> extension events. WebViewGuest is
     21 // created on attachment. That is, when a guest WebContents is associated with
     22 // a particular embedder WebContents. This happens on either initial navigation
     23 // or through the use of the New Window API, when a new window is attached to
     24 // a particular <webview>.
     25 class WebViewGuest : public GuestView,
     26                      public content::NotificationObserver,
     27                      public content::WebContentsObserver {
     28  public:
     29   WebViewGuest(content::WebContents* guest_web_contents,
     30                const std::string& extension_id);
     31 
     32   static WebViewGuest* From(int embedder_process_id, int instance_id);
     33   static WebViewGuest* FromWebContents(content::WebContents* contents);
     34 
     35   // GuestView implementation.
     36   virtual void Attach(content::WebContents* embedder_web_contents,
     37                       const base::DictionaryValue& args) OVERRIDE;
     38   virtual GuestView::Type GetViewType() const OVERRIDE;
     39   virtual WebViewGuest* AsWebView() OVERRIDE;
     40   virtual AdViewGuest* AsAdView() OVERRIDE;
     41 
     42   // GuestDelegate implementation.
     43   virtual void AddMessageToConsole(int32 level,
     44                                    const base::string16& message,
     45                                    int32 line_no,
     46                                    const base::string16& source_id) OVERRIDE;
     47   virtual void LoadProgressed(double progress) OVERRIDE;
     48   virtual void Close() OVERRIDE;
     49   virtual void DidAttach() OVERRIDE;
     50   virtual void EmbedderDestroyed() OVERRIDE;
     51   virtual void GuestProcessGone(base::TerminationStatus status) OVERRIDE;
     52   virtual bool HandleKeyboardEvent(
     53       const content::NativeWebKeyboardEvent& event) OVERRIDE;
     54   virtual bool IsDragAndDropEnabled() OVERRIDE;
     55   virtual bool IsOverridingUserAgent() const OVERRIDE;
     56   virtual void LoadAbort(bool is_top_level,
     57                          const GURL& url,
     58                          const std::string& error_type) OVERRIDE;
     59   virtual void RendererResponsive() OVERRIDE;
     60   virtual void RendererUnresponsive() OVERRIDE;
     61   virtual bool RequestPermission(
     62       BrowserPluginPermissionType permission_type,
     63       const base::DictionaryValue& request_info,
     64       const PermissionResponseCallback& callback,
     65       bool allowed_by_default) OVERRIDE;
     66   virtual GURL ResolveURL(const std::string& src) OVERRIDE;
     67   virtual void SizeChanged(const gfx::Size& old_size, const gfx::Size& new_size)
     68       OVERRIDE;
     69 
     70   // NotificationObserver implementation.
     71   virtual void Observe(int type,
     72                        const content::NotificationSource& source,
     73                        const content::NotificationDetails& details) OVERRIDE;
     74 
     75   // If possible, navigate the guest to |relative_index| entries away from the
     76   // current navigation entry.
     77   void Go(int relative_index);
     78 
     79   // Reload the guest.
     80   void Reload();
     81 
     82   enum PermissionResponseAction {
     83     DENY,
     84     ALLOW,
     85     DEFAULT
     86   };
     87 
     88   enum SetPermissionResult {
     89     SET_PERMISSION_INVALID,
     90     SET_PERMISSION_ALLOWED,
     91     SET_PERMISSION_DENIED
     92   };
     93 
     94   // Responds to the permission request |request_id| with |action| and
     95   // |user_input|. Returns whether there was a pending request for the provided
     96   // |request_id|.
     97   SetPermissionResult SetPermission(int request_id,
     98                                     PermissionResponseAction action,
     99                                     const std::string& user_input);
    100 
    101   // Overrides the user agent for this guest.
    102   // This affects subsequent guest navigations.
    103   void SetUserAgentOverride(const std::string& user_agent_override);
    104 
    105   // Stop loading the guest.
    106   void Stop();
    107 
    108   // Kill the guest process.
    109   void Terminate();
    110 
    111   // Clears data in the storage partition of this guest.
    112   //
    113   // Partition data that are newer than |removal_since| will be removed.
    114   // |removal_mask| corresponds to bitmask in StoragePartition::RemoveDataMask.
    115   bool ClearData(const base::Time remove_since,
    116                  uint32 removal_mask,
    117                  const base::Closure& callback);
    118 
    119   extensions::ScriptExecutor* script_executor() {
    120     return script_executor_.get();
    121   }
    122 
    123  private:
    124   virtual ~WebViewGuest();
    125 
    126   // A map to store the callback for a request keyed by the request's id.
    127   struct PermissionResponseInfo {
    128     PermissionResponseCallback callback;
    129     BrowserPluginPermissionType permission_type;
    130     bool allowed_by_default;
    131     PermissionResponseInfo();
    132     PermissionResponseInfo(const PermissionResponseCallback& callback,
    133                            BrowserPluginPermissionType permission_type,
    134                            bool allowed_by_default);
    135     ~PermissionResponseInfo();
    136   };
    137 
    138   static void RecordUserInitiatedUMA(const PermissionResponseInfo& info,
    139                                      bool allow);
    140   // WebContentsObserver implementation.
    141   virtual void DidCommitProvisionalLoadForFrame(
    142       int64 frame_id,
    143       const base::string16& frame_unique_name,
    144       bool is_main_frame,
    145       const GURL& url,
    146       content::PageTransition transition_type,
    147       content::RenderViewHost* render_view_host) OVERRIDE;
    148   virtual void DidFailProvisionalLoad(
    149       int64 frame_id,
    150       const base::string16& frame_unique_name,
    151       bool is_main_frame,
    152       const GURL& validated_url,
    153       int error_code,
    154       const base::string16& error_description,
    155       content::RenderViewHost* render_view_host) OVERRIDE;
    156   virtual void DidStartProvisionalLoadForFrame(
    157       int64 frame_id,
    158       int64 parent_frame_id,
    159       bool is_main_frame,
    160       const GURL& validated_url,
    161       bool is_error_page,
    162       bool is_iframe_srcdoc,
    163       content::RenderViewHost* render_view_host) OVERRIDE;
    164   virtual void DidStopLoading(
    165       content::RenderViewHost* render_view_host) OVERRIDE;
    166   virtual void WebContentsDestroyed(
    167       content::WebContents* web_contents) OVERRIDE;
    168   virtual void UserAgentOverrideSet(const std::string& user_agent) OVERRIDE;
    169 
    170   // Called after the load handler is called in the guest's main frame.
    171   void LoadHandlerCalled();
    172 
    173   // Called when a redirect notification occurs.
    174   void LoadRedirect(const GURL& old_url,
    175                     const GURL& new_url,
    176                     bool is_top_level);
    177 
    178   static bool AllowChromeExtensionURLs();
    179 
    180   void AddWebViewToExtensionRendererState();
    181   static void RemoveWebViewFromExtensionRendererState(
    182       content::WebContents* web_contents);
    183 
    184   ObserverList<extensions::TabHelper::ScriptExecutionObserver>
    185       script_observers_;
    186   scoped_ptr<extensions::ScriptExecutor> script_executor_;
    187 
    188   content::NotificationRegistrar notification_registrar_;
    189 
    190   // A counter to generate a unique request id for a permission request.
    191   // We only need the ids to be unique for a given WebViewGuest.
    192   int next_permission_request_id_;
    193 
    194   typedef std::map<int, PermissionResponseInfo> RequestMap;
    195   RequestMap pending_permission_requests_;
    196 
    197   // True if the user agent is overridden.
    198   bool is_overriding_user_agent_;
    199 
    200   // Indicates that the page needs to be reloaded once it has been attached to
    201   // an embedder.
    202   bool pending_reload_on_attachment_;
    203 
    204   DISALLOW_COPY_AND_ASSIGN(WebViewGuest);
    205 };
    206 
    207 #endif  // CHROME_BROWSER_GUESTVIEW_WEBVIEW_WEBVIEW_GUEST_H_
    208