summaryrefslogtreecommitdiffstats
path: root/chrome/renderer/chrome_render_view_observer.h
blob: e565dfec21f23fdea8774346126385bc0e8272fb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_RENDERER_CHROME_RENDER_VIEW_OBSERVER_H_
#define CHROME_RENDERER_CHROME_RENDER_VIEW_OBSERVER_H_
#pragma once

#include <vector>

#include "base/task.h"
#include "base/scoped_ptr.h"
#include "content/renderer/render_view.h"
#include "content/renderer/render_view_observer.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebPageSerializerClient.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebPermissionClient.h"

class ContentSettingsObserver;
class DomAutomationController;
class ExtensionDispatcher;
class ExternalHostBindings;
class FilePath;
class GURL;
class SkBitmap;
class TranslateHelper;
struct ThumbnailScore;
struct ViewMsg_Navigate_Params;

namespace WebKit {
class WebView;
}

namespace safe_browsing {
class PhishingClassifierDelegate;
}

namespace webkit_glue {
class ImageResourceFetcher;
}

// This class holds the Chrome specific parts of RenderView, and has the same
// lifetime.
class ChromeRenderViewObserver : public RenderViewObserver,
                                 public WebKit::WebPageSerializerClient,
                                 public WebKit::WebPermissionClient {
 public:
  // translate_helper and/or phishing_classifier can be NULL.
  ChromeRenderViewObserver(
      RenderView* render_view,
      ContentSettingsObserver* content_settings,
      ExtensionDispatcher* extension_dispatcher,
      TranslateHelper* translate_helper,
      safe_browsing::PhishingClassifierDelegate* phishing_classifier);
  virtual ~ChromeRenderViewObserver();

 private:
  // RenderViewObserver implementation.
  virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
  virtual void DidStopLoading() OVERRIDE;
  virtual void DidChangeIcons(WebKit::WebFrame* frame) OVERRIDE;
  virtual void DidCommitProvisionalLoad(WebKit::WebFrame* frame,
                                        bool is_new_navigation) OVERRIDE;
  virtual void DidClearWindowObject(WebKit::WebFrame* frame) OVERRIDE;

  // WebKit::WebPageSerializerClient implementation.
  virtual void didSerializeDataForFrame(
      const WebKit::WebURL& frame_url,
      const WebKit::WebCString& data,
      PageSerializationStatus status) OVERRIDE;

  // WebKit::WebPermissionClient implementation.
  virtual bool allowDatabase(WebKit::WebFrame* frame,
                             const WebKit::WebString& name,
                             const WebKit::WebString& display_name,
                             unsigned long estimated_size) OVERRIDE;
  virtual bool allowImages(WebKit::WebFrame* frame,
                           bool enabled_per_settings) OVERRIDE;
  virtual bool allowPlugins(WebKit::WebFrame* frame,
                            bool enabled_per_settings) OVERRIDE;
  virtual bool allowScript(WebKit::WebFrame* frame,
                           bool enabled_per_settings) OVERRIDE;
  virtual bool allowScriptExtension(WebKit::WebFrame* frame,
                                    const WebKit::WebString& extension_name,
                                    int extension_group) OVERRIDE;
  // TODO(jam): add OVERRIDE once WebKit is rolled.
  virtual bool allowStorage(WebKit::WebFrame* frame, bool local);
  virtual bool allowReadFromClipboard(WebKit::WebFrame* frame,
                                      bool default_value) OVERRIDE;
  virtual bool allowWriteToClipboard(WebKit::WebFrame* frame,
                                     bool default_value) OVERRIDE;
  virtual void didNotAllowPlugins(WebKit::WebFrame* frame) OVERRIDE;
  virtual void didNotAllowScript(WebKit::WebFrame* frame) OVERRIDE;

  void OnCaptureSnapshot();
  void OnHandleMessageFromExternalHost(const std::string& message,
                                       const std::string& origin,
                                       const std::string& target);
  void OnJavaScriptStressTestControl(int cmd, int param);
  void OnGetAllSavableResourceLinksForCurrentPage(const GURL& page_url);
  void OnGetSerializedHtmlDataForCurrentPageWithLocalLinks(
      const std::vector<GURL>& links,
      const std::vector<FilePath>& local_paths,
      const FilePath& local_directory_name);
  void OnDownloadFavicon(int id, const GURL& image_url, int image_size);
  void OnEnableViewSourceMode();
  void OnNavigate(const ViewMsg_Navigate_Params& params);
  void OnSetIsPrerendering(bool is_prerendering);

  // Captures the thumbnail and text contents for indexing for the given load
  // ID. If the view's load ID is different than the parameter, this call is
  // a NOP. Typically called on a timer, so the load ID may have changed in the
  // meantime.
  void CapturePageInfo(int load_id, bool preliminary_capture);

  // Retrieves the text from the given frame contents, the page text up to the
  // maximum amount kMaxIndexChars will be placed into the given buffer.
  void CaptureText(WebKit::WebFrame* frame, string16* contents);

  void CaptureThumbnail();

  // Creates a thumbnail of |frame|'s contents resized to (|w|, |h|)
  // and puts that in |thumbnail|. Thumbnail metadata goes in |score|.
  bool CaptureFrameThumbnail(WebKit::WebView* view, int w, int h,
                             SkBitmap* thumbnail,
                             ThumbnailScore* score);

  // Capture a snapshot of a view.  This is used to allow an extension
  // to get a snapshot of a tab using chrome.tabs.captureVisibleTab().
  bool CaptureSnapshot(WebKit::WebView* view, SkBitmap* snapshot);

  // Exposes the DOMAutomationController object that allows JS to send
  // information to the browser process.
  void BindDOMAutomationController(WebKit::WebFrame* webframe);

  ExternalHostBindings* GetExternalHostBindings();

  // This callback is triggered when DownloadFavicon completes, either
  // succesfully or with a failure. See DownloadFavicon for more
  // details.
  void DidDownloadFavicon(webkit_glue::ImageResourceFetcher* fetcher,
                          const SkBitmap& image);

  // Requests to download a favicon image. When done, the RenderView
  // is notified by way of DidDownloadFavicon. Returns true if the
  // request was successfully started, false otherwise. id is used to
  // uniquely identify the request and passed back to the
  // DidDownloadFavicon method. If the image has multiple frames, the
  // frame whose size is image_size is returned. If the image doesn't
  // have a frame at the specified size, the first is returned.
  bool DownloadFavicon(int id, const GURL& image_url, int image_size);

  // Decodes a data: URL image or returns an empty image in case of failure.
  SkBitmap ImageFromDataUrl(const GURL&) const;

  // Have the same lifetime as us.
  ContentSettingsObserver* content_settings_;
  ExtensionDispatcher* extension_dispatcher_;
  TranslateHelper* translate_helper_;
  safe_browsing::PhishingClassifierDelegate* phishing_classifier_;

  // Page_id from the last page we indexed. This prevents us from indexing the
  // same page twice in a row.
  int32 last_indexed_page_id_;

  // Allows JS to access DOM automation. The JS object is only exposed when the
  // DOM automation bindings are enabled.
  scoped_ptr<DomAutomationController> dom_automation_controller_;

  // External host exposed through automation controller.
  scoped_ptr<ExternalHostBindings> external_host_bindings_;

  ScopedRunnableMethodFactory<ChromeRenderViewObserver>
      page_info_method_factory_;

  // ImageResourceFetchers schedule via DownloadImage.
  RenderView::ImageResourceFetcherList image_fetchers_;

  DISALLOW_COPY_AND_ASSIGN(ChromeRenderViewObserver);
};

#endif  // CHROME_RENDERER_CHROME_RENDER_VIEW_OBSERVER_H_