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