Home | History | Annotate | Download | only in renderer
      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 CHROME_RENDERER_CHROME_RENDER_VIEW_OBSERVER_H_
      6 #define CHROME_RENDERER_CHROME_RENDER_VIEW_OBSERVER_H_
      7 
      8 #include <set>
      9 #include <string>
     10 #include <vector>
     11 
     12 #include "base/memory/linked_ptr.h"
     13 #include "base/memory/scoped_ptr.h"
     14 #include "base/timer/timer.h"
     15 #include "chrome/common/extensions/permissions/api_permission.h"
     16 #include "content/public/common/top_controls_state.h"
     17 #include "content/public/renderer/render_view_observer.h"
     18 #include "third_party/WebKit/public/web/WebPermissionClient.h"
     19 #include "ui/gfx/size.h"
     20 #include "url/gurl.h"
     21 
     22 class ChromeRenderProcessObserver;
     23 class ContentSettingsObserver;
     24 class ExternalHostBindings;
     25 class SkBitmap;
     26 class TranslateHelper;
     27 class WebViewColorOverlay;
     28 class WebViewAnimatingOverlay;
     29 
     30 namespace extensions {
     31 class Dispatcher;
     32 class Extension;
     33 }
     34 
     35 namespace WebKit {
     36 class WebView;
     37 struct WebWindowFeatures;
     38 }
     39 
     40 namespace safe_browsing {
     41 class PhishingClassifierDelegate;
     42 }
     43 
     44 // This class holds the Chrome specific parts of RenderView, and has the same
     45 // lifetime.
     46 class ChromeRenderViewObserver : public content::RenderViewObserver,
     47                                  public WebKit::WebPermissionClient {
     48  public:
     49   // translate_helper can be NULL.
     50   ChromeRenderViewObserver(
     51       content::RenderView* render_view,
     52       ContentSettingsObserver* content_settings,
     53       ChromeRenderProcessObserver* chrome_render_process_observer,
     54       extensions::Dispatcher* extension_dispatcher);
     55   virtual ~ChromeRenderViewObserver();
     56 
     57  private:
     58   // Holds the information received in OnWebUIJavaScript for later use
     59   // to call EvaluateScript() to preload javascript for WebUI tests.
     60   struct WebUIJavaScript {
     61     string16 frame_xpath;
     62     string16 jscript;
     63     int id;
     64     bool notify_result;
     65   };
     66 
     67   // RenderViewObserver implementation.
     68   virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
     69   virtual void DidStartLoading() OVERRIDE;
     70   virtual void DidStopLoading() OVERRIDE;
     71   virtual void DidCommitProvisionalLoad(WebKit::WebFrame* frame,
     72                                         bool is_new_navigation) OVERRIDE;
     73   virtual void DidClearWindowObject(WebKit::WebFrame* frame) OVERRIDE;
     74   virtual void DidHandleGestureEvent(
     75       const WebKit::WebGestureEvent& event) OVERRIDE;
     76 
     77   // WebKit::WebPermissionClient implementation.
     78   virtual bool allowDatabase(WebKit::WebFrame* frame,
     79                              const WebKit::WebString& name,
     80                              const WebKit::WebString& display_name,
     81                              unsigned long estimated_size);
     82   virtual bool allowFileSystem(WebKit::WebFrame* frame);
     83   virtual bool allowImage(WebKit::WebFrame* frame,
     84                           bool enabled_per_settings,
     85                           const WebKit::WebURL& image_url);
     86   virtual bool allowIndexedDB(WebKit::WebFrame* frame,
     87                               const WebKit::WebString& name,
     88                               const WebKit::WebSecurityOrigin& origin);
     89   virtual bool allowPlugins(WebKit::WebFrame* frame,
     90                             bool enabled_per_settings);
     91   virtual bool allowScript(WebKit::WebFrame* frame,
     92                            bool enabled_per_settings);
     93   virtual bool allowScriptFromSource(WebKit::WebFrame* frame,
     94                                      bool enabled_per_settings,
     95                                      const WebKit::WebURL& script_url);
     96   virtual bool allowStorage(WebKit::WebFrame* frame, bool local);
     97   virtual bool allowReadFromClipboard(WebKit::WebFrame* frame,
     98                                       bool default_value);
     99   virtual bool allowWriteToClipboard(WebKit::WebFrame* frame,
    100                                      bool default_value);
    101   virtual bool allowWebComponents(const WebKit::WebDocument&, bool);
    102   virtual bool allowHTMLNotifications(
    103       const WebKit::WebDocument& document);
    104   virtual bool allowMutationEvents(const WebKit::WebDocument&,
    105                                    bool default_value);
    106   virtual bool allowPushState(const WebKit::WebDocument&);
    107   virtual void didNotAllowPlugins(WebKit::WebFrame* frame);
    108   virtual void didNotAllowScript(WebKit::WebFrame* frame);
    109   virtual bool allowDisplayingInsecureContent(
    110       WebKit::WebFrame* frame,
    111       bool allowed_per_settings,
    112       const WebKit::WebSecurityOrigin& context,
    113       const WebKit::WebURL& url);
    114   virtual bool allowRunningInsecureContent(
    115       WebKit::WebFrame* frame,
    116       bool allowed_per_settings,
    117       const WebKit::WebSecurityOrigin& context,
    118       const WebKit::WebURL& url);
    119   virtual void Navigate(const GURL& url) OVERRIDE;
    120 
    121   void OnWebUIJavaScript(const string16& frame_xpath,
    122                          const string16& jscript,
    123                          int id,
    124                          bool notify_result);
    125   void OnHandleMessageFromExternalHost(const std::string& message,
    126                                        const std::string& origin,
    127                                        const std::string& target);
    128   void OnJavaScriptStressTestControl(int cmd, int param);
    129   void OnSetIsPrerendering(bool is_prerendering);
    130   void OnSetAllowDisplayingInsecureContent(bool allow);
    131   void OnSetAllowRunningInsecureContent(bool allow);
    132   void OnSetClientSidePhishingDetection(bool enable_phishing_detection);
    133   void OnSetVisuallyDeemphasized(bool deemphasized);
    134   void OnRequestThumbnailForContextNode(int thumbnail_min_area_pixels,
    135                                         gfx::Size thumbnail_max_size_pixels);
    136   void OnStartFrameSniffer(const string16& frame_name);
    137   void OnGetFPS();
    138   void OnAddStrictSecurityHost(const std::string& host);
    139   void OnNPAPINotSupported();
    140 #if defined(OS_ANDROID)
    141   void OnUpdateTopControlsState(content::TopControlsState constraints,
    142                                 content::TopControlsState current,
    143                                 bool animate);
    144 #endif
    145   void OnSetWindowFeatures(const WebKit::WebWindowFeatures& window_features);
    146 
    147   void CapturePageInfoLater(int page_id,
    148                             bool preliminary_capture,
    149                             base::TimeDelta delay);
    150 
    151   // Captures the thumbnail and text contents for indexing for the given load
    152   // ID.  Kicks off analysis of the captured text.
    153   void CapturePageInfo(int page_id, bool preliminary_capture);
    154 
    155   // Retrieves the text from the given frame contents, the page text up to the
    156   // maximum amount kMaxIndexChars will be placed into the given buffer.
    157   void CaptureText(WebKit::WebFrame* frame, string16* contents);
    158 
    159   ExternalHostBindings* GetExternalHostBindings();
    160 
    161   // Determines if a host is in the strict security host set.
    162   bool IsStrictSecurityHost(const std::string& host);
    163 
    164   // If |origin| corresponds to an installed extension, returns that extension.
    165   // Otherwise returns NULL.
    166   const extensions::Extension* GetExtension(
    167       const WebKit::WebSecurityOrigin& origin) const;
    168 
    169   // Checks if a page contains <meta http-equiv="refresh" ...> tag.
    170   bool HasRefreshMetaTag(WebKit::WebFrame* frame);
    171 
    172   // Save the JavaScript to preload if a ViewMsg_WebUIJavaScript is received.
    173   scoped_ptr<WebUIJavaScript> webui_javascript_;
    174 
    175   // Owned by ChromeContentRendererClient and outlive us.
    176   ChromeRenderProcessObserver* chrome_render_process_observer_;
    177   extensions::Dispatcher* extension_dispatcher_;
    178 
    179   // Have the same lifetime as us.
    180   ContentSettingsObserver* content_settings_;
    181   TranslateHelper* translate_helper_;
    182   safe_browsing::PhishingClassifierDelegate* phishing_classifier_;
    183 
    184   // Page_id from the last page we indexed. This prevents us from indexing the
    185   // same page twice in a row.
    186   int32 last_indexed_page_id_;
    187   // The toplevel URL that was last indexed.  This is used together with the
    188   // page id to decide whether to reindex in certain cases like history
    189   // replacement.
    190   GURL last_indexed_url_;
    191 
    192   // Insecure content may be permitted for the duration of this render view.
    193   bool allow_displaying_insecure_content_;
    194   bool allow_running_insecure_content_;
    195   std::set<std::string> strict_security_hosts_;
    196 
    197   // External host exposed through automation controller.
    198   scoped_ptr<ExternalHostBindings> external_host_bindings_;
    199 
    200   // A color page overlay when visually de-emaphasized.
    201   scoped_ptr<WebViewColorOverlay> dimmed_color_overlay_;
    202 
    203   // Used to delay calling CapturePageInfo.
    204   base::Timer capture_timer_;
    205 
    206   DISALLOW_COPY_AND_ASSIGN(ChromeRenderViewObserver);
    207 };
    208 
    209 #endif  // CHROME_RENDERER_CHROME_RENDER_VIEW_OBSERVER_H_
    210