1// Copyright (c) 2011 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_PRERENDER_PRERENDER_CONTENTS_H_
6#define CHROME_BROWSER_PRERENDER_PRERENDER_CONTENTS_H_
7#pragma once
8
9#include <string>
10#include <vector>
11
12#include "base/scoped_ptr.h"
13#include "base/time.h"
14#include "chrome/browser/prerender/prerender_final_status.h"
15#include "chrome/browser/tab_contents/render_view_host_delegate_helper.h"
16#include "chrome/browser/ui/app_modal_dialogs/js_modal_dialog.h"
17#include "chrome/common/view_types.h"
18#include "content/browser/renderer_host/render_view_host_delegate.h"
19#include "content/common/notification_registrar.h"
20#include "content/common/window_container_type.h"
21#include "webkit/glue/window_open_disposition.h"
22
23class TabContents;
24struct FaviconURL;
25struct WebPreferences;
26struct ViewHostMsg_FrameNavigate_Params;
27
28namespace base {
29class ProcessMetrics;
30}
31
32namespace gfx {
33class Rect;
34}
35
36namespace prerender {
37
38class PrerenderManager;
39
40// This class is a peer of TabContents. It can host a renderer, but does not
41// have any visible display. Its navigation is not managed by a
42// NavigationController because is has no facility for navigating (other than
43// programatically view window.location.href) or RenderViewHostManager because
44// it is never allowed to navigate across a SiteInstance boundary.
45class PrerenderContents : public RenderViewHostDelegate,
46                          public RenderViewHostDelegate::View,
47                          public NotificationObserver,
48                          public JavaScriptAppModalDialogDelegate {
49 public:
50  // PrerenderContents::Create uses the currently registered Factory to create
51  // the PrerenderContents. Factory is intended for testing.
52  class Factory {
53   public:
54    Factory() {}
55    virtual ~Factory() {}
56
57    virtual PrerenderContents* CreatePrerenderContents(
58        PrerenderManager* prerender_manager, Profile* profile, const GURL& url,
59        const std::vector<GURL>& alias_urls, const GURL& referrer) = 0;
60
61   private:
62    DISALLOW_COPY_AND_ASSIGN(Factory);
63  };
64
65  virtual ~PrerenderContents();
66
67  static Factory* CreateFactory();
68
69  virtual void StartPrerendering();
70
71  // Verifies that the prerendering is not using too many resources, and kills
72  // it if not.
73  void DestroyWhenUsingTooManyResources();
74
75  RenderViewHost* render_view_host() { return render_view_host_; }
76  // Allows replacing of the RenderViewHost owned by this class, including
77  // replacing with a NULL value.  When a caller uses this, the caller will
78  // own (and is responsible for freeing) the old RVH.
79  void set_render_view_host(RenderViewHost* rvh) { render_view_host_ = rvh; }
80  ViewHostMsg_FrameNavigate_Params* navigate_params() {
81    return navigate_params_.get();
82  }
83  string16 title() const { return title_; }
84  int32 page_id() const { return page_id_; }
85  GURL icon_url() const { return icon_url_; }
86  bool has_stopped_loading() const { return has_stopped_loading_; }
87  bool prerendering_has_started() const { return prerendering_has_started_; }
88
89  // Sets the parameter to the value of the associated RenderViewHost's child id
90  // and returns a boolean indicating the validity of that id.
91  virtual bool GetChildId(int* child_id) const;
92
93  // Sets the parameter to the value of the associated RenderViewHost's route id
94  // and returns a boolean indicating the validity of that id.
95  virtual bool GetRouteId(int* route_id) const;
96
97  // Set the final status for how the PrerenderContents was used. This
98  // should only be called once, and should be called before the prerender
99  // contents are destroyed.
100  void set_final_status(FinalStatus final_status);
101  FinalStatus final_status() const;
102
103  base::TimeTicks load_start_time() const { return load_start_time_; }
104
105  // Indicates whether this prerendered page can be used for the provided
106  // URL, i.e. whether there is a match.
107  bool MatchesURL(const GURL& url) const;
108
109  // RenderViewHostDelegate implementation.
110  virtual RenderViewHostDelegate::View* GetViewDelegate();
111  virtual const GURL& GetURL() const;
112  virtual ViewType::Type GetRenderViewType() const;
113  virtual int GetBrowserWindowID() const;
114  virtual void DidNavigate(RenderViewHost* render_view_host,
115                           const ViewHostMsg_FrameNavigate_Params& params);
116  virtual void UpdateTitle(RenderViewHost* render_view_host,
117                           int32 page_id,
118                           const std::wstring& title);
119  virtual WebPreferences GetWebkitPrefs();
120  virtual void RunJavaScriptMessage(const std::wstring& message,
121                                    const std::wstring& default_prompt,
122                                    const GURL& frame_url,
123                                    const int flags,
124                                    IPC::Message* reply_msg,
125                                    bool* did_suppress_message);
126  virtual void Close(RenderViewHost* render_view_host);
127  virtual void DidStopLoading();
128  virtual RendererPreferences GetRendererPrefs(Profile* profile) const;
129
130  // RenderViewHostDelegate::View
131  virtual void CreateNewWindow(
132      int route_id,
133      const ViewHostMsg_CreateWindow_Params& params);
134  virtual void CreateNewWidget(int route_id, WebKit::WebPopupType popup_type);
135  virtual void CreateNewFullscreenWidget(int route_id);
136  virtual void ShowCreatedWindow(int route_id,
137                                 WindowOpenDisposition disposition,
138                                 const gfx::Rect& initial_pos,
139                                 bool user_gesture);
140  virtual void ShowCreatedWidget(int route_id,
141                                 const gfx::Rect& initial_pos);
142  virtual void ShowCreatedFullscreenWidget(int route_id);
143  virtual void ShowContextMenu(const ContextMenuParams& params) {}
144  virtual void ShowPopupMenu(const gfx::Rect& bounds,
145                             int item_height,
146                             double item_font_size,
147                             int selected_item,
148                             const std::vector<WebMenuItem>& items,
149                             bool right_aligned) {}
150  virtual void StartDragging(const WebDropData& drop_data,
151                             WebKit::WebDragOperationsMask allowed_operations,
152                             const SkBitmap& image,
153                             const gfx::Point& image_offset) {}
154  virtual void UpdateDragCursor(WebKit::WebDragOperation operation) {}
155  virtual void GotFocus() {}
156  virtual void TakeFocus(bool reverse) {}
157  virtual void LostCapture() {}
158  virtual void Activate() {}
159  virtual void Deactivate() {}
160  virtual bool PreHandleKeyboardEvent(const NativeWebKeyboardEvent& event,
161                                      bool* is_keyboard_shortcut);
162  virtual void HandleKeyboardEvent(const NativeWebKeyboardEvent& event) {}
163  virtual void HandleMouseMove() {}
164  virtual void HandleMouseDown() {}
165  virtual void HandleMouseLeave() {}
166  virtual void HandleMouseUp() {}
167  virtual void HandleMouseActivate() {}
168  virtual void UpdatePreferredSize(const gfx::Size& new_size) {}
169
170  // NotificationObserver
171  virtual void Observe(NotificationType type,
172                       const NotificationSource& source,
173                       const NotificationDetails& details);
174
175  // Overridden from JavaScriptAppModalDialogDelegate:
176  virtual void OnMessageBoxClosed(IPC::Message* reply_msg,
177                                  bool success,
178                                  const std::wstring& prompt);
179  virtual void SetSuppressMessageBoxes(bool suppress_message_boxes) {}
180  virtual gfx::NativeWindow GetMessageBoxRootWindow();
181  virtual TabContents* AsTabContents();
182  virtual ExtensionHost* AsExtensionHost();
183
184  virtual void UpdateInspectorSetting(const std::string& key,
185                                      const std::string& value);
186  virtual void ClearInspectorSettings();
187
188  virtual void OnJSOutOfMemory();
189  virtual void RendererUnresponsive(RenderViewHost* render_view_host,
190                                    bool is_during_unload);
191
192 protected:
193  PrerenderContents(PrerenderManager* prerender_manager, Profile* profile,
194                    const GURL& url, const std::vector<GURL>& alias_urls,
195                    const GURL& referrer);
196
197  // from RenderViewHostDelegate.
198  virtual bool OnMessageReceived(const IPC::Message& message);
199
200  const GURL& prerender_url() const { return prerender_url_; }
201
202 private:
203  // Needs to be able to call the constructor.
204  friend class PrerenderContentsFactoryImpl;
205
206  // Message handlers.
207  void OnDidStartProvisionalLoadForFrame(int64 frame_id,
208                                         bool main_frame,
209                                         const GURL& url);
210  void OnUpdateFaviconURL(int32 page_id, const std::vector<FaviconURL>& urls);
211  void OnMaybeCancelPrerenderForHTML5Media();
212
213  // Adds an alias URL, for one of the many redirections. Returns whether
214  // the URL is valid.
215  bool AddAliasURL(const GURL& url);
216
217  // Remove |this| from the PrerenderManager, set a final status, and
218  // delete |this|.
219  void Destroy(FinalStatus reason);
220
221  // Returns the ProcessMetrics for the render process, if it exists.
222  base::ProcessMetrics* MaybeGetProcessMetrics();
223
224  // The prerender manager owning this object.
225  PrerenderManager* prerender_manager_;
226
227  // The host for our HTML content.
228  RenderViewHost* render_view_host_;
229
230  // Common implementations of some RenderViewHostDelegate::View methods.
231  RenderViewHostDelegateViewHelper delegate_view_helper_;
232
233  // The URL being prerendered.
234  GURL prerender_url_;
235
236  // The referrer.
237  GURL referrer_;
238
239  // The NavigationParameters of the finished navigation.
240  scoped_ptr<ViewHostMsg_FrameNavigate_Params> navigate_params_;
241
242  // The profile being used
243  Profile* profile_;
244
245  // Information about the title and URL of the page that this class as a
246  // RenderViewHostDelegate has received from the RenderView.
247  // Used to apply to the new RenderViewHost delegate that might eventually
248  // own the contained RenderViewHost when the prerendered page is shown
249  // in a TabContents.
250  string16 title_;
251  int32 page_id_;
252  GURL url_;
253  GURL icon_url_;
254  NotificationRegistrar registrar_;
255
256  // A vector of URLs that this prerendered page matches against.
257  // This array can contain more than element as a result of redirects,
258  // such as HTTP redirects or javascript redirects.
259  std::vector<GURL> alias_urls_;
260
261  bool has_stopped_loading_;
262
263  FinalStatus final_status_;
264
265  bool prerendering_has_started_;
266
267  // Time at which we started to load the URL.  This is used to compute
268  // the time elapsed from initiating a prerender until the time the
269  // (potentially only partially) prerendered page is shown to the user.
270  base::TimeTicks load_start_time_;
271
272  // Process Metrics of the render process associated with the
273  // RenderViewHost for this object.
274  scoped_ptr<base::ProcessMetrics> process_metrics_;
275
276  // Maximum amount of private memory that may be used per PrerenderContents,
277  // in MB.
278  static const int kMaxPrerenderPrivateMB = 100;
279
280  DISALLOW_COPY_AND_ASSIGN(PrerenderContents);
281};
282
283}  // prerender
284
285#endif  // CHROME_BROWSER_PRERENDER_PRERENDER_CONTENTS_H_
286