initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1 | // Copyright 2008, Google Inc. |
| 2 | // All rights reserved. |
| 3 | // |
| 4 | // Redistribution and use in source and binary forms, with or without |
| 5 | // modification, are permitted provided that the following conditions are |
| 6 | // met: |
| 7 | // |
| 8 | // * Redistributions of source code must retain the above copyright |
| 9 | // notice, this list of conditions and the following disclaimer. |
| 10 | // * Redistributions in binary form must reproduce the above |
| 11 | // copyright notice, this list of conditions and the following disclaimer |
| 12 | // in the documentation and/or other materials provided with the |
| 13 | // distribution. |
| 14 | // * Neither the name of Google Inc. nor the names of its |
| 15 | // contributors may be used to endorse or promote products derived from |
| 16 | // this software without specific prior written permission. |
| 17 | // |
| 18 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | |
| 30 | #include "chrome/renderer/render_view.h" |
| 31 | |
| 32 | #include <algorithm> |
| 33 | #include <string> |
| 34 | #include <vector> |
| 35 | |
| 36 | #include "base/command_line.h" |
| 37 | #include "base/gfx/bitmap_header.h" |
[email protected] | b49cbcf2 | 2008-08-14 17:47:00 | [diff] [blame] | 38 | #include "base/gfx/bitmap_platform_device_win.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 39 | #include "base/gfx/image_operations.h" |
| 40 | #include "base/gfx/native_theme.h" |
| 41 | #include "base/gfx/vector_canvas.h" |
| 42 | #include "base/gfx/png_encoder.h" |
| 43 | #include "base/string_piece.h" |
| 44 | #include "base/string_util.h" |
| 45 | #include "chrome/app/theme/theme_resources.h" |
| 46 | #include "chrome/common/chrome_switches.h" |
| 47 | #include "chrome/common/gfx/emf.h" |
| 48 | #include "chrome/common/gfx/favicon_size.h" |
| 49 | #include "chrome/common/gfx/color_utils.h" |
| 50 | #include "chrome/common/jstemplate_builder.h" |
| 51 | #include "chrome/common/l10n_util.h" |
| 52 | #include "chrome/common/resource_bundle.h" |
| 53 | #include "chrome/common/text_zoom.h" |
| 54 | #include "chrome/common/thumbnail_score.h" |
[email protected] | 173de1b | 2008-08-15 18:36:46 | [diff] [blame] | 55 | #include "chrome/common/chrome_plugin_lib.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 56 | #include "chrome/renderer/about_handler.h" |
[email protected] | 173de1b | 2008-08-15 18:36:46 | [diff] [blame] | 57 | #include "chrome/renderer/chrome_plugin_host.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 58 | #include "chrome/renderer/debug_message_handler.h" |
| 59 | #include "chrome/renderer/localized_error.h" |
| 60 | #include "chrome/renderer/renderer_resources.h" |
| 61 | #include "chrome/renderer/visitedlink_slave.h" |
| 62 | #include "chrome/renderer/webplugin_delegate_proxy.h" |
| 63 | #include "chrome/views/message_box_view.h" |
| 64 | #include "net/base/escape.h" |
| 65 | #include "net/base/net_errors.h" |
| 66 | #include "webkit/default_plugin/default_plugin_shared.h" |
| 67 | #include "webkit/glue/dom_operations.h" |
| 68 | #include "webkit/glue/dom_serializer.h" |
| 69 | #include "webkit/glue/password_form.h" |
| 70 | #include "webkit/glue/plugins/plugin_list.h" |
| 71 | #include "webkit/glue/searchable_form_data.h" |
| 72 | #include "webkit/glue/webdatasource.h" |
| 73 | #include "webkit/glue/webdropdata.h" |
| 74 | #include "webkit/glue/weberror.h" |
| 75 | #include "webkit/glue/webframe.h" |
| 76 | #include "webkit/glue/webhistoryitem.h" |
| 77 | #include "webkit/glue/webinputevent.h" |
| 78 | #include "webkit/glue/webkit_glue.h" |
| 79 | #include "webkit/glue/webpreferences.h" |
| 80 | #include "webkit/glue/webresponse.h" |
| 81 | #include "webkit/glue/weburlrequest.h" |
| 82 | #include "webkit/glue/webview.h" |
| 83 | #include "webkit/glue/plugins/webplugin_delegate_impl.h" |
| 84 | #include "webkit/port/platform/graphics/PlatformContextSkia.h" |
| 85 | |
| 86 | #include "generated_resources.h" |
| 87 | |
| 88 | //----------------------------------------------------------------------------- |
| 89 | |
| 90 | // define to write the time necessary for thumbnail/DOM text retrieval, |
| 91 | // respectively, into the system debug log |
| 92 | // #define TIME_BITMAP_RETRIEVAL |
| 93 | // #define TIME_TEXT_RETRIEVAL |
| 94 | |
| 95 | // maximum number of characters in the document to index, any text beyond this |
| 96 | // point will be clipped |
| 97 | static const int kMaxIndexChars = 65535; |
| 98 | |
| 99 | // Size of the thumbnails that we'll generate |
| 100 | static const int kThumbnailWidth = 196; |
| 101 | static const int kThumbnailHeight = 136; |
| 102 | |
| 103 | // Delay in milliseconds that we'll wait before capturing the page contents |
| 104 | // and thumbnail. |
| 105 | static const int kDelayForCaptureMs = 500; |
| 106 | |
| 107 | // Typically, we capture the page data once the page is loaded. |
| 108 | // Sometimes, the page never finishes to load, preventing the page capture |
| 109 | // To workaround this problem, we always perform a capture after the following |
| 110 | // delay. |
| 111 | static const int kDelayForForcedCaptureMs = 6000; |
| 112 | |
| 113 | // How often we will sync the navigation state when the user is changing form |
| 114 | // elements or scroll position. |
| 115 | const TimeDelta kDelayForNavigationSync = TimeDelta::FromSeconds(5); |
| 116 | |
| 117 | // The next available page ID to use. This ensures that the page IDs are |
| 118 | // globally unique in the renderer. |
| 119 | static int32 next_page_id_ = 1; |
| 120 | |
| 121 | static const char* const kUnreachableWebDataURL = |
| 122 | "chrome-resource://chromewebdata/"; |
| 123 | |
| 124 | namespace { |
| 125 | |
| 126 | // Associated with browser-initiated navigations to hold tracking data. |
| 127 | class RenderViewExtraRequestData : public WebRequest::ExtraData { |
| 128 | public: |
| 129 | RenderViewExtraRequestData(int32 pending_page_id, |
| 130 | PageTransition::Type transition, |
| 131 | const GURL& url) |
| 132 | : pending_page_id_(pending_page_id), |
| 133 | transition_type(transition), |
| 134 | request_committed(false) { |
| 135 | } |
| 136 | |
| 137 | // Contains the page_id for this navigation or -1 if there is none yet. |
| 138 | int32 pending_page_id() const { return pending_page_id_; } |
| 139 | |
| 140 | // Is this a new navigation? |
| 141 | bool is_new_navigation() const { return pending_page_id_ == -1; } |
| 142 | |
| 143 | // Contains the transition type that the browser specified when it |
| 144 | // initiated the load. |
| 145 | PageTransition::Type transition_type; |
| 146 | |
| 147 | // True if we have already processed the "DidCommitLoad" event for this |
| 148 | // request. Used by session history. |
| 149 | bool request_committed; |
| 150 | |
| 151 | private: |
| 152 | int32 pending_page_id_; |
| 153 | |
| 154 | DISALLOW_EVIL_CONSTRUCTORS(RenderViewExtraRequestData); |
| 155 | }; |
| 156 | |
| 157 | } // namespace |
| 158 | |
| 159 | /////////////////////////////////////////////////////////////////////////////// |
| 160 | |
| 161 | RenderView::RenderView() |
| 162 | : RenderWidget(), |
| 163 | is_loading_(false), |
| 164 | page_id_(-1), |
| 165 | last_page_id_sent_to_browser_(-1), |
| 166 | last_indexed_page_id_(-1), |
| 167 | method_factory_(this), |
| 168 | nav_state_sync_timer_(kDelayForNavigationSync), |
| 169 | opened_by_user_gesture_(true), |
| 170 | enable_dom_automation_(false), |
| 171 | enable_dom_ui_bindings_(false), |
| 172 | target_url_status_(TARGET_NONE), |
| 173 | printed_document_width_(0), |
| 174 | first_default_plugin_(NULL), |
| 175 | navigation_gesture_(NavigationGestureUnknown), |
| 176 | history_back_list_count_(0), |
| 177 | history_forward_list_count_(0), |
| 178 | disable_popup_blocking_(false), |
| 179 | has_unload_listener_(false) { |
| 180 | resource_dispatcher_ = new ResourceDispatcher(this); |
| 181 | nav_state_sync_timer_.set_task( |
| 182 | method_factory_.NewRunnableMethod(&RenderView::SyncNavigationState)); |
[email protected] | 3a453fa | 2008-08-15 18:46:34 | [diff] [blame] | 183 | #ifdef CHROME_PERSONALIZATION |
| 184 | personalization_ = Personalization::CreateRendererPersonalization(); |
| 185 | #endif |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | RenderView::~RenderView() { |
| 189 | resource_dispatcher_->ClearMessageSender(); |
| 190 | // Clear any back-pointers that might still be held by plugins. |
| 191 | PluginDelegateList::iterator it = plugin_delegates_.begin(); |
| 192 | while (it != plugin_delegates_.end()) { |
| 193 | (*it)->DropRenderView(); |
| 194 | it = plugin_delegates_.erase(it); |
| 195 | } |
| 196 | |
| 197 | RenderThread::current()->RemoveFilter(debug_message_handler_); |
[email protected] | 3a453fa | 2008-08-15 18:46:34 | [diff] [blame] | 198 | |
| 199 | #ifdef CHROME_PERSONALIZATION |
| 200 | Personalization::CleanupRendererPersonalization(personalization_); |
| 201 | personalization_ = NULL; |
| 202 | #endif |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | /*static*/ |
| 206 | RenderView* RenderView::Create(HWND parent_hwnd, |
| 207 | HANDLE modal_dialog_event, |
| 208 | int32 opener_id, |
| 209 | const WebPreferences& webkit_prefs, |
| 210 | int32 routing_id) { |
| 211 | DCHECK(routing_id != MSG_ROUTING_NONE); |
| 212 | scoped_refptr<RenderView> view = new RenderView(); |
| 213 | view->Init(parent_hwnd, |
| 214 | modal_dialog_event, |
| 215 | opener_id, |
| 216 | webkit_prefs, |
| 217 | routing_id); // adds reference |
| 218 | return view; |
| 219 | } |
| 220 | |
| 221 | /*static*/ |
| 222 | void RenderView::SetNextPageID(int32 next_page_id) { |
| 223 | // This method should only be called during process startup, and the given |
| 224 | // page id had better not exceed our current next page id! |
| 225 | DCHECK(next_page_id_ == 1); |
| 226 | DCHECK(next_page_id >= next_page_id_); |
| 227 | next_page_id_ = next_page_id; |
| 228 | } |
| 229 | |
| 230 | void RenderView::PluginDestroyed(WebPluginDelegateProxy* proxy) { |
| 231 | PluginDelegateList::iterator it = |
| 232 | std::find(plugin_delegates_.begin(), plugin_delegates_.end(), proxy); |
| 233 | DCHECK(it != plugin_delegates_.end()); |
| 234 | plugin_delegates_.erase(it); |
| 235 | // If the plugin is deleted, we need to clear our reference in case user |
| 236 | // clicks the info bar to install. Unfortunately we are getting |
| 237 | // PluginDestroyed in single process mode. However, that is not a huge |
| 238 | // concern. |
| 239 | if (proxy == first_default_plugin_) |
| 240 | first_default_plugin_ = NULL; |
| 241 | } |
| 242 | |
| 243 | void RenderView::PluginCrashed(const std::wstring& plugin_path) { |
| 244 | Send(new ViewHostMsg_CrashedPlugin(routing_id_, plugin_path)); |
| 245 | } |
| 246 | |
| 247 | |
| 248 | void RenderView::JSOutOfMemory() { |
| 249 | Send(new ViewHostMsg_JSOutOfMemory(routing_id_)); |
| 250 | } |
| 251 | |
| 252 | void RenderView::Init(HWND parent_hwnd, |
| 253 | HANDLE modal_dialog_event, |
| 254 | int32 opener_id, |
| 255 | const WebPreferences& webkit_prefs, |
| 256 | int32 routing_id) { |
| 257 | DCHECK(!webview()); |
| 258 | |
| 259 | if (opener_id != MSG_ROUTING_NONE) |
| 260 | opener_id_ = opener_id; |
| 261 | |
| 262 | // Avoid a leak here by not assigning, since WebView::Create addrefs for us. |
| 263 | WebWidget* view = WebView::Create(this, webkit_prefs); |
| 264 | webwidget_.swap(&view); |
| 265 | |
| 266 | // Don't let WebCore keep a B/F list - we have our own. |
| 267 | // We let it keep 1 entry because FrameLoader::goToItem expects an item in the |
| 268 | // backForwardList, which is used only in ASSERTs. |
| 269 | webview()->SetBackForwardListSize(1); |
| 270 | |
| 271 | routing_id_ = routing_id; |
| 272 | RenderThread::current()->AddRoute(routing_id_, this); |
| 273 | // Take a reference on behalf of the RenderThread. This will be balanced |
| 274 | // when we receive ViewMsg_Close. |
| 275 | AddRef(); |
| 276 | |
| 277 | // If this is a popup, we must wait for the CreatingNew_ACK message before |
| 278 | // completing initialization. Otherwise, we can finish it now. |
| 279 | if (opener_id == MSG_ROUTING_NONE) { |
| 280 | did_show_ = true; |
| 281 | CompleteInit(parent_hwnd); |
| 282 | } |
| 283 | |
| 284 | host_window_ = parent_hwnd; |
| 285 | modal_dialog_event_.Set(modal_dialog_event); |
| 286 | |
| 287 | CommandLine command_line; |
| 288 | enable_dom_automation_ = |
| 289 | command_line.HasSwitch(switches::kDomAutomationController); |
| 290 | disable_popup_blocking_ = |
| 291 | command_line.HasSwitch(switches::kDisablePopupBlocking); |
| 292 | |
| 293 | debug_message_handler_ = new DebugMessageHandler(this); |
| 294 | RenderThread::current()->AddFilter(debug_message_handler_); |
| 295 | } |
| 296 | |
| 297 | void RenderView::OnMessageReceived(const IPC::Message& message) { |
| 298 | // Let the resource dispatcher intercept resource messages first. |
| 299 | if (resource_dispatcher_->OnMessageReceived(message)) |
| 300 | return; |
| 301 | IPC_BEGIN_MESSAGE_MAP(RenderView, message) |
| 302 | IPC_MESSAGE_HANDLER(ViewMsg_CreatingNew_ACK, OnCreatingNewAck) |
| 303 | IPC_MESSAGE_HANDLER(ViewMsg_CaptureThumbnail, SendThumbnail) |
| 304 | IPC_MESSAGE_HANDLER(ViewMsg_GetPrintedPagesCount, OnGetPrintedPagesCount) |
| 305 | IPC_MESSAGE_HANDLER(ViewMsg_PrintPages, OnPrintPages) |
| 306 | IPC_MESSAGE_HANDLER(ViewMsg_Navigate, OnNavigate) |
| 307 | IPC_MESSAGE_HANDLER(ViewMsg_Stop, OnStop) |
| 308 | IPC_MESSAGE_HANDLER(ViewMsg_LoadAlternateHTMLText, OnLoadAlternateHTMLText) |
| 309 | IPC_MESSAGE_HANDLER(ViewMsg_StopFinding, OnStopFinding) |
| 310 | IPC_MESSAGE_HANDLER(ViewMsg_Undo, OnUndo) |
| 311 | IPC_MESSAGE_HANDLER(ViewMsg_Redo, OnRedo) |
| 312 | IPC_MESSAGE_HANDLER(ViewMsg_Cut, OnCut) |
| 313 | IPC_MESSAGE_HANDLER(ViewMsg_Copy, OnCopy) |
| 314 | IPC_MESSAGE_HANDLER(ViewMsg_Paste, OnPaste) |
| 315 | IPC_MESSAGE_HANDLER(ViewMsg_Replace, OnReplace) |
| 316 | IPC_MESSAGE_HANDLER(ViewMsg_Delete, OnDelete) |
| 317 | IPC_MESSAGE_HANDLER(ViewMsg_SelectAll, OnSelectAll) |
| 318 | IPC_MESSAGE_HANDLER(ViewMsg_CopyImageAt, OnCopyImageAt) |
| 319 | IPC_MESSAGE_HANDLER(ViewMsg_Find, OnFind) |
| 320 | IPC_MESSAGE_HANDLER(ViewMsg_AlterTextSize, OnAlterTextSize) |
| 321 | IPC_MESSAGE_HANDLER(ViewMsg_SetPageEncoding, OnSetPageEncoding) |
| 322 | IPC_MESSAGE_HANDLER(ViewMsg_InspectElement, OnInspectElement) |
| 323 | IPC_MESSAGE_HANDLER(ViewMsg_ShowJavaScriptConsole, OnShowJavaScriptConsole) |
| 324 | IPC_MESSAGE_HANDLER(ViewMsg_DownloadImage, OnDownloadImage) |
| 325 | IPC_MESSAGE_HANDLER(ViewMsg_ScriptEvalRequest, OnScriptEvalRequest) |
| 326 | IPC_MESSAGE_HANDLER(ViewMsg_AddMessageToConsole, OnAddMessageToConsole) |
| 327 | IPC_MESSAGE_HANDLER(ViewMsg_DebugAttach, OnDebugAttach) |
| 328 | IPC_MESSAGE_HANDLER(ViewMsg_ReservePageIDRange, OnReservePageIDRange) |
| 329 | IPC_MESSAGE_HANDLER(ViewMsg_UploadFile, OnUploadFileRequest) |
| 330 | IPC_MESSAGE_HANDLER(ViewMsg_FormFill, OnFormFill) |
| 331 | IPC_MESSAGE_HANDLER(ViewMsg_FillPasswordForm, OnFillPasswordForm) |
| 332 | IPC_MESSAGE_HANDLER(ViewMsg_DragTargetDragEnter, OnDragTargetDragEnter) |
| 333 | IPC_MESSAGE_HANDLER(ViewMsg_DragTargetDragOver, OnDragTargetDragOver) |
| 334 | IPC_MESSAGE_HANDLER(ViewMsg_DragTargetDragLeave, OnDragTargetDragLeave) |
| 335 | IPC_MESSAGE_HANDLER(ViewMsg_DragTargetDrop, OnDragTargetDrop) |
| 336 | IPC_MESSAGE_HANDLER(ViewMsg_AllowDomAutomationBindings, |
| 337 | OnAllowDomAutomationBindings) |
| 338 | IPC_MESSAGE_HANDLER(ViewMsg_AllowDOMUIBindings, OnAllowDOMUIBindings) |
| 339 | IPC_MESSAGE_HANDLER(ViewMsg_SetDOMUIProperty, OnSetDOMUIProperty) |
| 340 | IPC_MESSAGE_HANDLER(ViewMsg_DragSourceEndedOrMoved, OnDragSourceEndedOrMoved) |
| 341 | IPC_MESSAGE_HANDLER(ViewMsg_DragSourceSystemDragEnded, |
| 342 | OnDragSourceSystemDragEnded) |
| 343 | IPC_MESSAGE_HANDLER(ViewMsg_SetInitialFocus, OnSetInitialFocus) |
| 344 | IPC_MESSAGE_HANDLER(ViewMsg_FindReplyACK, OnFindReplyAck) |
| 345 | IPC_MESSAGE_HANDLER(ViewMsg_UpdateTargetURL_ACK, OnUpdateTargetURLAck) |
| 346 | IPC_MESSAGE_HANDLER(ViewMsg_UpdateWebPreferences, OnUpdateWebPreferences) |
| 347 | IPC_MESSAGE_HANDLER(ViewMsg_SetAltErrorPageURL, OnSetAltErrorPageURL) |
| 348 | IPC_MESSAGE_HANDLER(ViewMsg_InstallMissingPlugin, OnInstallMissingPlugin) |
| 349 | IPC_MESSAGE_HANDLER(ViewMsg_RunFileChooserResponse, OnFileChooserResponse) |
| 350 | IPC_MESSAGE_HANDLER(ViewMsg_EnableViewSourceMode, OnEnableViewSourceMode) |
| 351 | IPC_MESSAGE_HANDLER(ViewMsg_UpdateBackForwardListCount, |
| 352 | OnUpdateBackForwardListCount) |
| 353 | IPC_MESSAGE_HANDLER(ViewMsg_GetAllSavableResourceLinksForCurrentPage, |
| 354 | OnGetAllSavableResourceLinksForCurrentPage) |
| 355 | IPC_MESSAGE_HANDLER(ViewMsg_GetSerializedHtmlDataForCurrentPageWithLocalLinks, |
| 356 | OnGetSerializedHtmlDataForCurrentPageWithLocalLinks) |
| 357 | IPC_MESSAGE_HANDLER(ViewMsg_GetApplicationInfo, OnGetApplicationInfo) |
| 358 | IPC_MESSAGE_HANDLER(ViewMsg_ShouldClose, OnMsgShouldClose) |
| 359 | IPC_MESSAGE_HANDLER(ViewMsg_ClosePage, OnClosePage) |
| 360 | IPC_MESSAGE_HANDLER(ViewMsg_ThemeChanged, OnThemeChanged) |
[email protected] | 3ac14a05 | 2008-08-15 21:22:15 | [diff] [blame^] | 361 | IPC_MESSAGE_HANDLER(ViewMsg_PostMessage, OnPostMessage) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 362 | // Have the super handle all other messages. |
| 363 | IPC_MESSAGE_UNHANDLED(RenderWidget::OnMessageReceived(message)) |
| 364 | IPC_END_MESSAGE_MAP() |
| 365 | } |
| 366 | |
| 367 | // Got a response from the browser after the renderer decided to create a new |
| 368 | // view. |
| 369 | void RenderView::OnCreatingNewAck(HWND parent) { |
| 370 | CompleteInit(parent); |
| 371 | } |
| 372 | |
| 373 | void RenderView::SendThumbnail() { |
| 374 | WebFrame* main_frame = webview()->GetMainFrame(); |
| 375 | if (!main_frame) |
| 376 | return; |
| 377 | |
| 378 | // get the URL for this page |
| 379 | GURL url(main_frame->GetURL()); |
| 380 | if (url.is_empty()) |
| 381 | return; |
| 382 | |
| 383 | if (size_.IsEmpty()) |
| 384 | return; // Don't create an empty thumbnail! |
| 385 | |
| 386 | ThumbnailScore score; |
| 387 | SkBitmap thumbnail; |
| 388 | CaptureThumbnail(main_frame, kThumbnailWidth, kThumbnailHeight, &thumbnail, |
| 389 | &score); |
| 390 | // send the thumbnail message to the browser process |
| 391 | IPC::Message* thumbnail_msg = new IPC::Message(routing_id_, |
| 392 | ViewHostMsg_Thumbnail::ID, IPC::Message::PRIORITY_NORMAL); |
| 393 | IPC::ParamTraits<GURL>::Write(thumbnail_msg, url); |
| 394 | IPC::ParamTraits<ThumbnailScore>::Write(thumbnail_msg, score); |
| 395 | IPC::ParamTraits<SkBitmap>::Write(thumbnail_msg, thumbnail); |
| 396 | Send(thumbnail_msg); |
| 397 | } |
| 398 | |
| 399 | int RenderView::SwitchFrameToPrintMediaType(const ViewMsg_Print_Params& params, |
| 400 | WebFrame* frame) { |
| 401 | float ratio = static_cast<float>(params.desired_dpi / params.dpi); |
| 402 | float paper_width = params.printable_size.width() * ratio; |
| 403 | float paper_height = params.printable_size.height() * ratio; |
| 404 | float minLayoutWidth = static_cast<float>(paper_width * params.min_shrink); |
| 405 | float maxLayoutWidth = static_cast<float>(paper_width * params.max_shrink); |
| 406 | |
| 407 | // Safari uses: 765 & 1224. Margins aren't exactly the same either. |
| 408 | // Scale = 2.222 for MDI printer. |
| 409 | int pages; |
| 410 | if (!frame->SetPrintingMode(true, |
| 411 | minLayoutWidth, |
| 412 | maxLayoutWidth, |
| 413 | &printed_document_width_)) { |
| 414 | NOTREACHED(); |
| 415 | pages = 0; |
| 416 | } else { |
| 417 | // Force to recalculate the height, otherwise it reuse the current window |
| 418 | // height as the default. |
| 419 | float effective_shrink = printed_document_width_ / paper_width; |
| 420 | gfx::Size page_size(printed_document_width_, |
| 421 | static_cast<int>(paper_height * effective_shrink) - 1); |
| 422 | WebView* view = frame->GetView(); |
| 423 | if (view) { |
| 424 | // Hack around an issue where if the current view height is higher than |
| 425 | // the page height, empty pages will be printed even if the bottom of the |
| 426 | // web page is empty. |
| 427 | printing_view_size_ = view->GetSize(); |
| 428 | view->Resize(page_size); |
| 429 | view->Layout(); |
| 430 | } |
| 431 | pages = frame->ComputePageRects(params.printable_size); |
| 432 | DCHECK(pages); |
| 433 | } |
| 434 | return pages; |
| 435 | } |
| 436 | |
| 437 | void RenderView::SwitchFrameToDisplayMediaType(WebFrame* frame) { |
| 438 | // Set the layout back to "normal" document; i.e. CSS media type = "screen". |
| 439 | frame->SetPrintingMode(false, 0, 0, NULL); |
| 440 | WebView* view = frame->GetView(); |
| 441 | if (view) { |
| 442 | // Restore from the hack described at SwitchFrameToPrintMediaType(). |
| 443 | view->Resize(printing_view_size_); |
| 444 | view->Layout(); |
| 445 | printing_view_size_.SetSize(0, 0); |
| 446 | } |
| 447 | printed_document_width_ = 0; |
| 448 | } |
| 449 | |
| 450 | void RenderView::OnPrintPage(const ViewMsg_PrintPage_Params& params) { |
| 451 | DCHECK(webview()); |
| 452 | if (webview()) |
| 453 | PrintPage(params, webview()->GetMainFrame()); |
| 454 | } |
| 455 | |
| 456 | void RenderView::PrintPage(const ViewMsg_PrintPage_Params& params, |
| 457 | WebFrame* frame) { |
| 458 | if (printed_document_width_ <= 0) { |
| 459 | NOTREACHED(); |
| 460 | return; |
| 461 | } |
| 462 | |
| 463 | // Generate a memory-based EMF file. The EMF will use the current screen's |
| 464 | // DPI. |
| 465 | gfx::Emf emf; |
| 466 | |
| 467 | emf.CreateDc(NULL, NULL); |
| 468 | HDC hdc = emf.hdc(); |
| 469 | DCHECK(hdc); |
[email protected] | b49cbcf2 | 2008-08-14 17:47:00 | [diff] [blame] | 470 | gfx::PlatformDeviceWin::InitializeDC(hdc); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 471 | |
| 472 | gfx::Rect rect; |
| 473 | frame->GetPageRect(params.page_number, &rect); |
| 474 | DCHECK(rect.height()); |
| 475 | DCHECK(rect.width()); |
| 476 | double shrink = static_cast<double>(printed_document_width_) / |
| 477 | params.params.printable_size.width(); |
| 478 | // This check would fire each time the page would get truncated on the |
| 479 | // right. This is not worth a DCHECK() but should be looked into, for |
| 480 | // example, wouldn't be worth trying in landscape? |
| 481 | // DCHECK_LE(rect.width(), printed_document_width_); |
| 482 | |
| 483 | // Buffer one page at a time. |
| 484 | int src_size_x = printed_document_width_; |
| 485 | int src_size_y = |
| 486 | static_cast<int>(ceil(params.params.printable_size.height() * |
| 487 | shrink)); |
| 488 | #if 0 |
| 489 | // TODO(maruel): This code is kept for testing until the 100% GDI drawing |
| 490 | // code is stable. maruels use this code's output as a reference when the |
| 491 | // GDI drawing code fails. |
| 492 | |
| 493 | // Mix of Skia and GDI based. |
[email protected] | b49cbcf2 | 2008-08-14 17:47:00 | [diff] [blame] | 494 | gfx::PlatformCanvasWin canvas(src_size_x, src_size_y, true); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 495 | canvas.drawARGB(255, 255, 255, 255, SkPorterDuff::kSrc_Mode); |
| 496 | PlatformContextSkia context(&canvas); |
| 497 | if (!frame->SpoolPage(params.page_number, &context)) { |
| 498 | NOTREACHED() << "Printing page " << params.page_number << " failed."; |
| 499 | return; |
| 500 | } |
| 501 | |
| 502 | // Create a BMP v4 header that we can serialize. |
| 503 | BITMAPV4HEADER bitmap_header; |
| 504 | gfx::CreateBitmapV4Header(src_size_x, src_size_y, &bitmap_header); |
| 505 | const SkBitmap& src_bmp = canvas.getDevice()->accessBitmap(true); |
| 506 | SkAutoLockPixels src_lock(src_bmp); |
| 507 | int retval = StretchDIBits(hdc, |
| 508 | 0, |
| 509 | 0, |
| 510 | src_size_x, src_size_y, |
| 511 | 0, 0, |
| 512 | src_size_x, src_size_y, |
| 513 | src_bmp.getPixels(), |
| 514 | reinterpret_cast<BITMAPINFO*>(&bitmap_header), |
| 515 | DIB_RGB_COLORS, |
| 516 | SRCCOPY); |
| 517 | DCHECK(retval != GDI_ERROR); |
| 518 | #else |
| 519 | // 100% GDI based. |
| 520 | gfx::VectorCanvas canvas(hdc, src_size_x, src_size_y); |
| 521 | PlatformContextSkia context(&canvas); |
| 522 | // Set the clipping region to be sure to not overflow. |
| 523 | SkRect clip_rect; |
| 524 | clip_rect.set(0, 0, SkIntToScalar(src_size_x), SkIntToScalar(src_size_y)); |
| 525 | canvas.clipRect(clip_rect); |
| 526 | if (!frame->SpoolPage(params.page_number, &context)) { |
| 527 | NOTREACHED() << "Printing page " << params.page_number << " failed."; |
| 528 | return; |
| 529 | } |
| 530 | #endif |
| 531 | |
| 532 | // Done printing. Close the device context to retrieve the compiled EMF. |
| 533 | if (!emf.CloseDc()) { |
| 534 | NOTREACHED() << "EMF failed"; |
| 535 | } |
| 536 | |
| 537 | // Get the size of the compiled EMF. |
| 538 | unsigned buf_size = emf.GetDataSize(); |
| 539 | DCHECK(buf_size > 128); |
| 540 | ViewHostMsg_DidPrintPage_Params page_params; |
| 541 | page_params.data_size = 0; |
| 542 | page_params.emf_data_handle = NULL; |
| 543 | page_params.page_number = params.page_number; |
| 544 | page_params.document_cookie = params.params.document_cookie; |
| 545 | page_params.actual_shrink = shrink; |
| 546 | SharedMemory shared_buf; |
| 547 | |
| 548 | // http://msdn2.microsoft.com/en-us/library/ms535522.aspx |
| 549 | // Windows 2000/XP: When a page in a spooled file exceeds approximately 350 |
| 550 | // MB, it can fail to print and not send an error message. |
| 551 | if (buf_size < 350*1024*1024) { |
| 552 | // Allocate a shared memory buffer to hold the generated EMF data. |
| 553 | if (shared_buf.Create(L"", false, false, buf_size) && |
| 554 | shared_buf.Map(buf_size)) { |
| 555 | // Copy the bits into shared memory. |
| 556 | if (emf.GetData(shared_buf.memory(), buf_size)) { |
| 557 | page_params.emf_data_handle = shared_buf.handle(); |
| 558 | page_params.data_size = buf_size; |
| 559 | } else { |
| 560 | NOTREACHED() << "GetData() failed"; |
| 561 | } |
| 562 | shared_buf.Unmap(); |
| 563 | } else { |
| 564 | NOTREACHED() << "Buffer allocation failed"; |
| 565 | } |
| 566 | } else { |
| 567 | NOTREACHED() << "Buffer too large: " << buf_size; |
| 568 | } |
| 569 | emf.CloseEmf(); |
| 570 | if (Send(new ViewHostMsg_DuplicateSection(routing_id_, |
| 571 | page_params.emf_data_handle, |
| 572 | &page_params.emf_data_handle))) { |
| 573 | Send(new ViewHostMsg_DidPrintPage(routing_id_, page_params)); |
| 574 | } |
| 575 | } |
| 576 | |
| 577 | void RenderView::OnGetPrintedPagesCount(const ViewMsg_Print_Params& params) { |
| 578 | DCHECK(webview()); |
| 579 | if (!webview()) { |
| 580 | Send(new ViewHostMsg_DidGetPrintedPagesCount(routing_id_, |
| 581 | params.document_cookie, |
| 582 | 0)); |
| 583 | return; |
| 584 | } |
| 585 | WebFrame* frame = webview()->GetMainFrame(); |
| 586 | int expected_pages = SwitchFrameToPrintMediaType(params, frame); |
| 587 | Send(new ViewHostMsg_DidGetPrintedPagesCount(routing_id_, |
| 588 | params.document_cookie, |
| 589 | expected_pages)); |
| 590 | SwitchFrameToDisplayMediaType(frame); |
| 591 | } |
| 592 | |
| 593 | void RenderView::OnPrintPages(const ViewMsg_PrintPages_Params& params) { |
| 594 | DCHECK(webview()); |
| 595 | if (webview()) |
| 596 | PrintPages(params, webview()->GetMainFrame()); |
| 597 | } |
| 598 | |
| 599 | void RenderView::PrintPages(const ViewMsg_PrintPages_Params& params, |
| 600 | WebFrame* frame) { |
| 601 | int pages = SwitchFrameToPrintMediaType(params.params, frame); |
| 602 | Send(new ViewHostMsg_DidGetPrintedPagesCount(routing_id_, |
| 603 | params.params.document_cookie, |
| 604 | pages)); |
| 605 | if (pages) { |
| 606 | ViewMsg_PrintPage_Params page_params; |
| 607 | page_params.params = params.params; |
| 608 | if (params.pages.empty()) { |
| 609 | for (int i = 0; i < pages; ++i) { |
| 610 | page_params.page_number = i; |
| 611 | PrintPage(page_params, frame); |
| 612 | } |
| 613 | } else { |
| 614 | for (size_t i = 0; i < params.pages.size(); ++i) { |
| 615 | page_params.page_number = params.pages[i]; |
| 616 | PrintPage(page_params, frame); |
| 617 | } |
| 618 | } |
| 619 | } |
| 620 | SwitchFrameToDisplayMediaType(frame); |
| 621 | } |
| 622 | |
| 623 | void RenderView::CapturePageInfo(int load_id, bool preliminary_capture) { |
| 624 | if (load_id != page_id_) |
| 625 | return; // this capture call is no longer relevant due to navigation |
| 626 | if (load_id == last_indexed_page_id_) |
| 627 | return; // we already indexed this page |
| 628 | |
| 629 | if (!webview()) |
| 630 | return; |
| 631 | |
| 632 | WebFrame* main_frame = webview()->GetMainFrame(); |
| 633 | if (!main_frame) |
| 634 | return; |
| 635 | |
| 636 | // Don't index/capture pages that are in view source mode. |
| 637 | if (main_frame->GetInViewSourceMode()) |
| 638 | return; |
| 639 | |
| 640 | // Don't index/capture pages that failed to load. This only checks the top |
| 641 | // level frame so the thumbnail may contain a frame that failed to load. |
| 642 | WebDataSource* ds = main_frame->GetDataSource(); |
| 643 | if (ds && ds->HasUnreachableURL()) |
| 644 | return; |
| 645 | |
| 646 | if (!preliminary_capture) |
| 647 | last_indexed_page_id_ = load_id; |
| 648 | |
| 649 | // get the URL for this page |
| 650 | GURL url(main_frame->GetURL()); |
| 651 | if (url.is_empty()) |
| 652 | return; |
| 653 | |
| 654 | // full text |
| 655 | std::wstring contents; |
| 656 | CaptureText(main_frame, &contents); |
| 657 | if (contents.size()) { |
| 658 | // Send the text to the browser for indexing. |
| 659 | Send(new ViewHostMsg_PageContents(url, load_id, contents)); |
| 660 | } |
| 661 | |
| 662 | // thumbnail |
| 663 | SendThumbnail(); |
| 664 | } |
| 665 | |
| 666 | void RenderView::CaptureText(WebFrame* frame, std::wstring* contents) { |
| 667 | contents->clear(); |
| 668 | if (!frame) |
| 669 | return; |
| 670 | |
| 671 | #ifdef TIME_TEXT_RETRIEVAL |
| 672 | double begin = time_util::GetHighResolutionTimeNow(); |
| 673 | #endif |
| 674 | |
| 675 | // get the contents of the frame |
| 676 | frame->GetContentAsPlainText(kMaxIndexChars, contents); |
| 677 | |
| 678 | #ifdef TIME_TEXT_RETRIEVAL |
| 679 | double end = time_util::GetHighResolutionTimeNow(); |
| 680 | char buf[128]; |
| 681 | sprintf_s(buf, "%d chars retrieved for indexing in %gms\n", |
| 682 | contents.size(), (end - begin)*1000); |
| 683 | OutputDebugStringA(buf); |
| 684 | #endif |
| 685 | |
| 686 | // When the contents are clipped to the maximum, we don't want to have a |
| 687 | // partial word indexed at the end that might have been clipped. Therefore, |
| 688 | // terminate the string at the last space to ensure no words are clipped. |
| 689 | if (contents->size() == kMaxIndexChars) { |
| 690 | size_t last_space_index = contents->find_last_of(kWhitespaceWide); |
| 691 | if (last_space_index == std::wstring::npos) |
| 692 | return; // don't index if we got a huge block of text with no spaces |
| 693 | contents->resize(last_space_index); |
| 694 | } |
| 695 | } |
| 696 | |
| 697 | void RenderView::CaptureThumbnail(WebFrame* frame, |
| 698 | int w, |
| 699 | int h, |
| 700 | SkBitmap* thumbnail, |
| 701 | ThumbnailScore* score) { |
| 702 | #ifdef TIME_BITMAP_RETRIEVAL |
| 703 | double begin = time_util::GetHighResolutionTimeNow(); |
| 704 | #endif |
| 705 | |
[email protected] | b49cbcf2 | 2008-08-14 17:47:00 | [diff] [blame] | 706 | gfx::BitmapPlatformDeviceWin device(frame->CaptureImage(true)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 707 | const SkBitmap& src_bmp = device.accessBitmap(false); |
| 708 | |
| 709 | SkRect dest_rect; |
| 710 | dest_rect.set(0, 0, SkIntToScalar(w), SkIntToScalar(h)); |
| 711 | float dest_aspect = dest_rect.width() / dest_rect.height(); |
| 712 | |
| 713 | // Get the src rect so that we can preserve the aspect ratio while filling |
| 714 | // the destination. |
| 715 | SkIRect src_rect; |
| 716 | if (src_bmp.width() < dest_rect.width() || |
| 717 | src_bmp.height() < dest_rect.height()) { |
| 718 | // Source image is smaller: we clip the part of source image within the |
| 719 | // dest rect, and then stretch it to fill the dest rect. We don't respect |
| 720 | // the aspect ratio in this case. |
| 721 | src_rect.set(0, 0, static_cast<S16CPU>(dest_rect.width()), |
| 722 | static_cast<S16CPU>(dest_rect.height())); |
| 723 | score->good_clipping = false; |
| 724 | } else { |
| 725 | float src_aspect = static_cast<float>(src_bmp.width()) / src_bmp.height(); |
| 726 | if (src_aspect > dest_aspect) { |
| 727 | // Wider than tall, clip horizontally: we center the smaller thumbnail in |
| 728 | // the wider screen. |
| 729 | S16CPU new_width = static_cast<S16CPU>(src_bmp.height() * dest_aspect); |
| 730 | S16CPU x_offset = (src_bmp.width() - new_width) / 2; |
| 731 | src_rect.set(x_offset, 0, new_width + x_offset, src_bmp.height()); |
| 732 | score->good_clipping = false; |
| 733 | } else { |
| 734 | src_rect.set(0, 0, src_bmp.width(), |
| 735 | static_cast<S16CPU>(src_bmp.width() / dest_aspect)); |
| 736 | score->good_clipping = true; |
| 737 | } |
| 738 | } |
| 739 | |
| 740 | score->at_top = (frame->ScrollOffset().height() == 0); |
| 741 | |
| 742 | SkBitmap subset; |
| 743 | device.accessBitmap(false).extractSubset(&subset, src_rect); |
| 744 | |
| 745 | // Resample the subset that we want to get it the right size. |
| 746 | *thumbnail = gfx::ImageOperations::Resize( |
| 747 | subset, gfx::ImageOperations::RESIZE_LANCZOS3, gfx::Size(w, h)); |
| 748 | |
| 749 | score->boring_score = CalculateBoringScore(thumbnail); |
| 750 | |
| 751 | #ifdef TIME_BITMAP_RETRIEVAL |
| 752 | double end = time_util::GetHighResolutionTimeNow(); |
| 753 | char buf[128]; |
| 754 | sprintf_s(buf, "thumbnail in %gms\n", (end - begin) * 1000); |
| 755 | OutputDebugStringA(buf); |
| 756 | #endif |
| 757 | } |
| 758 | |
| 759 | double RenderView::CalculateBoringScore(SkBitmap* bitmap) { |
| 760 | int histogram[256] = {0}; |
| 761 | color_utils::BuildLumaHistogram(bitmap, histogram); |
| 762 | |
| 763 | int color_count = *std::max_element(histogram, histogram + 256); |
| 764 | int pixel_count = bitmap->width() * bitmap->height(); |
| 765 | return static_cast<double>(color_count) / pixel_count; |
| 766 | } |
| 767 | |
| 768 | void RenderView::OnNavigate(const ViewMsg_Navigate_Params& params) { |
| 769 | if (!webview()) |
| 770 | return; |
| 771 | |
| 772 | AboutHandler::MaybeHandle(params.url); |
| 773 | |
| 774 | bool is_reload = params.reload; |
| 775 | |
| 776 | WebFrame* main_frame = webview()->GetMainFrame(); |
| 777 | if (is_reload && !main_frame->HasCurrentState()) { |
| 778 | // We cannot reload if we do not have any history state. This happens, for |
| 779 | // example, when recovering from a crash. Our workaround here is a bit of |
| 780 | // a hack since it means that reload after a crashed tab does not cause an |
| 781 | // end-to-end cache validation. |
| 782 | is_reload = false; |
| 783 | } |
| 784 | |
| 785 | WebRequestCachePolicy cache_policy; |
| 786 | if (is_reload) { |
| 787 | cache_policy = WebRequestReloadIgnoringCacheData; |
| 788 | } else if (params.page_id != -1 || main_frame->GetInViewSourceMode()) { |
| 789 | cache_policy = WebRequestReturnCacheDataElseLoad; |
| 790 | } else { |
| 791 | cache_policy = WebRequestUseProtocolCachePolicy; |
| 792 | } |
| 793 | |
| 794 | scoped_ptr<WebRequest> request(WebRequest::Create(params.url)); |
| 795 | request->SetCachePolicy(cache_policy); |
| 796 | request->SetExtraData(new RenderViewExtraRequestData( |
| 797 | params.page_id, params.transition, params.url)); |
| 798 | |
| 799 | // If we are reloading, then WebKit will use the state of the current page. |
| 800 | // Otherwise, we give it the state to navigate to. |
| 801 | if (!is_reload) |
| 802 | request->SetHistoryState(params.state); |
| 803 | |
| 804 | main_frame->LoadRequest(request.get()); |
| 805 | } |
| 806 | |
| 807 | // Stop loading the current page |
| 808 | void RenderView::OnStop() { |
| 809 | if (webview()) |
| 810 | webview()->StopLoading(); |
| 811 | } |
| 812 | |
| 813 | void RenderView::OnLoadAlternateHTMLText(const std::string& html_contents, |
| 814 | bool new_navigation, |
| 815 | const GURL& display_url, |
| 816 | const std::string& security_info) { |
| 817 | if (!webview()) |
| 818 | return; |
| 819 | |
| 820 | scoped_ptr<WebRequest> request(WebRequest::Create( |
| 821 | GURL(kUnreachableWebDataURL))); |
| 822 | request->SetSecurityInfo(security_info); |
| 823 | |
| 824 | webview()->GetMainFrame()->LoadAlternateHTMLString(request.get(), |
| 825 | html_contents, |
| 826 | display_url, |
| 827 | !new_navigation); |
| 828 | } |
| 829 | |
| 830 | void RenderView::OnCopyImageAt(int x, int y) { |
| 831 | webview()->CopyImageAt(x, y); |
| 832 | } |
| 833 | |
| 834 | void RenderView::OnInspectElement(int x, int y) { |
| 835 | webview()->InspectElement(x, y); |
| 836 | } |
| 837 | |
| 838 | void RenderView::OnShowJavaScriptConsole() { |
| 839 | webview()->ShowJavaScriptConsole(); |
| 840 | } |
| 841 | |
| 842 | void RenderView::OnStopFinding(bool clear_selection) { |
| 843 | WebView* view = webview(); |
| 844 | if (!view) |
| 845 | return; |
| 846 | |
| 847 | if (clear_selection) |
| 848 | view->GetFocusedFrame()->ClearSelection(); |
| 849 | |
| 850 | WebFrame* frame = view->GetMainFrame(); |
| 851 | while (frame) { |
| 852 | frame->StopFinding(); |
| 853 | frame = view->GetNextFrameAfter(frame, false); |
| 854 | } |
| 855 | } |
| 856 | |
| 857 | void RenderView::OnFindReplyAck() { |
| 858 | // Check if there is any queued up request waiting to be sent. |
| 859 | if (queued_find_reply_message_.get()) { |
| 860 | // Send the search result over to the browser process. |
| 861 | Send(queued_find_reply_message_.get()); |
| 862 | queued_find_reply_message_.release(); |
| 863 | } |
| 864 | } |
| 865 | |
| 866 | void RenderView::OnUpdateTargetURLAck() { |
| 867 | // Check if there is a targeturl waiting to be sent. |
| 868 | if (target_url_status_ == TARGET_PENDING) { |
| 869 | Send(new ViewHostMsg_UpdateTargetURL(routing_id_, page_id_, |
| 870 | pending_target_url_)); |
| 871 | } |
| 872 | |
| 873 | target_url_status_ = TARGET_NONE; |
| 874 | } |
| 875 | |
| 876 | void RenderView::OnUndo() { |
| 877 | if (!webview()) |
| 878 | return; |
| 879 | |
| 880 | webview()->GetFocusedFrame()->Undo(); |
| 881 | } |
| 882 | |
| 883 | void RenderView::OnRedo() { |
| 884 | if (!webview()) |
| 885 | return; |
| 886 | |
| 887 | webview()->GetFocusedFrame()->Redo(); |
| 888 | } |
| 889 | |
| 890 | void RenderView::OnCut() { |
| 891 | if (!webview()) |
| 892 | return; |
| 893 | |
| 894 | webview()->GetFocusedFrame()->Cut(); |
| 895 | } |
| 896 | |
| 897 | void RenderView::OnCopy() { |
| 898 | if (!webview()) |
| 899 | return; |
| 900 | |
| 901 | webview()->GetFocusedFrame()->Copy(); |
| 902 | } |
| 903 | |
| 904 | void RenderView::OnPaste() { |
| 905 | if (!webview()) |
| 906 | return; |
| 907 | |
| 908 | webview()->GetFocusedFrame()->Paste(); |
| 909 | } |
| 910 | |
| 911 | void RenderView::OnReplace(const std::wstring& text) { |
| 912 | if (!webview()) |
| 913 | return; |
| 914 | |
| 915 | webview()->GetFocusedFrame()->Replace(text); |
| 916 | } |
| 917 | |
| 918 | void RenderView::OnDelete() { |
| 919 | if (!webview()) |
| 920 | return; |
| 921 | |
| 922 | webview()->GetFocusedFrame()->Delete(); |
| 923 | } |
| 924 | |
| 925 | void RenderView::OnSelectAll() { |
| 926 | if (!webview()) |
| 927 | return; |
| 928 | |
| 929 | webview()->GetFocusedFrame()->SelectAll(); |
| 930 | } |
| 931 | |
| 932 | void RenderView::OnSetInitialFocus(bool reverse) { |
| 933 | if (!webview()) |
| 934 | return; |
| 935 | webview()->SetInitialFocus(reverse); |
| 936 | } |
| 937 | |
| 938 | /////////////////////////////////////////////////////////////////////////////// |
| 939 | |
| 940 | // Tell the embedding application that the URL of the active page has changed |
| 941 | void RenderView::UpdateURL(WebFrame* frame) { |
| 942 | WebDataSource* ds = frame->GetDataSource(); |
| 943 | DCHECK(ds); |
| 944 | |
| 945 | const WebRequest& request = ds->GetRequest(); |
| 946 | const WebRequest& initial_request = ds->GetInitialRequest(); |
| 947 | const WebResponse& response = ds->GetResponse(); |
| 948 | |
| 949 | // We don't hold a reference to the extra data. The request's reference will |
| 950 | // be sufficient because we won't modify it during our call. MAY BE NULL. |
| 951 | RenderViewExtraRequestData* extra_data = |
| 952 | static_cast<RenderViewExtraRequestData*>(request.GetExtraData()); |
| 953 | |
| 954 | ViewHostMsg_FrameNavigate_Params params; |
| 955 | params.is_post = false; |
| 956 | params.page_id = page_id_; |
| 957 | if (!request.GetSecurityInfo().empty()) { |
| 958 | // SSL state specified in the request takes precedence over the one in the |
| 959 | // response. |
| 960 | // So far this is only intended for error pages that are not expected to be |
| 961 | // over ssl, so we should not get any clash. |
| 962 | DCHECK(response.GetSecurityInfo().empty()); |
| 963 | params.security_info = request.GetSecurityInfo(); |
| 964 | } else { |
| 965 | params.security_info = response.GetSecurityInfo(); |
| 966 | } |
| 967 | |
| 968 | // Set the URL to be displayed in the browser UI to the user. |
| 969 | if (ds->HasUnreachableURL()) { |
| 970 | params.url = ds->GetUnreachableURL(); |
| 971 | } else { |
| 972 | params.url = request.GetURL(); |
| 973 | } |
| 974 | |
| 975 | params.redirects = ds->GetRedirectChain(); |
| 976 | params.should_update_history = !ds->HasUnreachableURL(); |
| 977 | |
| 978 | const SearchableFormData* searchable_form_data = |
| 979 | frame->GetDataSource()->GetSearchableFormData(); |
| 980 | if (searchable_form_data) { |
| 981 | params.searchable_form_url = searchable_form_data->url(); |
| 982 | params.searchable_form_element_name = searchable_form_data->element_name(); |
| 983 | params.searchable_form_encoding = searchable_form_data->encoding(); |
| 984 | } |
| 985 | |
| 986 | const PasswordForm* password_form_data = |
| 987 | frame->GetDataSource()->GetPasswordFormData(); |
| 988 | if (password_form_data) |
| 989 | params.password_form = *password_form_data; |
| 990 | |
| 991 | params.gesture = navigation_gesture_; |
| 992 | navigation_gesture_ = NavigationGestureUnknown; |
| 993 | |
| 994 | if (webview()->GetMainFrame() == frame) { |
| 995 | // Top-level navigation. |
| 996 | |
| 997 | // Update contents MIME type for main frame. |
| 998 | std::wstring mime_type = ds->GetResponseMimeType(); |
| 999 | params.contents_mime_type = WideToASCII(mime_type); |
| 1000 | |
| 1001 | // We assume top level navigations initiated by the renderer are link |
| 1002 | // clicks. |
| 1003 | params.transition = extra_data ? |
| 1004 | extra_data->transition_type : PageTransition::LINK; |
| 1005 | if (!PageTransition::IsMainFrame(params.transition)) { |
| 1006 | // If the main frame does a load, it should not be reported as a subframe |
| 1007 | // navigation. This can occur in the following case: |
| 1008 | // 1. You're on a site with frames. |
| 1009 | // 2. You do a subframe navigation. This is stored with transition type |
| 1010 | // MANUAL_SUBFRAME. |
| 1011 | // 3. You navigate to some non-frame site, say, google.com. |
| 1012 | // 4. You navigate back to the page from step 2. Since it was initially |
| 1013 | // MANUAL_SUBFRAME, it will be that same transition type here. |
| 1014 | // We don't want that, because any navigation that changes the toplevel |
| 1015 | // frame should be tracked as a toplevel navigation (this allows us to |
| 1016 | // update the URL bar, etc). |
| 1017 | params.transition = PageTransition::LINK; |
| 1018 | } |
| 1019 | |
| 1020 | if (params.transition == PageTransition::LINK && |
| 1021 | frame->GetDataSource()->IsFormSubmit()) { |
| 1022 | params.transition = PageTransition::FORM_SUBMIT; |
| 1023 | } |
| 1024 | |
| 1025 | // If we have a valid consumed client redirect source, |
| 1026 | // the page contained a client redirect (meta refresh, document.loc...), |
| 1027 | // so we set the referrer and transition to match. |
| 1028 | if (completed_client_redirect_src_.is_valid()) { |
[email protected] | 77e09a9 | 2008-08-01 18:11:04 | [diff] [blame] | 1029 | DCHECK(completed_client_redirect_src_ == params.redirects[0]); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1030 | params.referrer = completed_client_redirect_src_; |
| 1031 | params.transition = static_cast<PageTransition::Type>( |
| 1032 | params.transition | PageTransition::CLIENT_REDIRECT); |
| 1033 | } else { |
| 1034 | // Bug 654101: the referrer will be empty on https->http transitions. It |
| 1035 | // would be nice if we could get the real referrer from somewhere. |
| 1036 | params.referrer = GURL(initial_request.GetHttpReferrer()); |
| 1037 | } |
| 1038 | |
| 1039 | std::wstring method = request.GetHttpMethod(); |
| 1040 | if (method == L"POST") |
| 1041 | params.is_post = true; |
| 1042 | |
| 1043 | Send(new ViewHostMsg_FrameNavigate(routing_id_, params)); |
| 1044 | } else { |
| 1045 | // Subframe navigation: the type depends on whether this navigation |
| 1046 | // generated a new session history entry. When they do generate a session |
| 1047 | // history entry, it means the user initiated the navigation and we should |
| 1048 | // mark it as such. This test checks if this is the first time UpdateURL |
| 1049 | // has been called since WillNavigateToURL was called to initiate the load. |
| 1050 | if (page_id_ > last_page_id_sent_to_browser_) |
| 1051 | params.transition = PageTransition::MANUAL_SUBFRAME; |
| 1052 | else |
| 1053 | params.transition = PageTransition::AUTO_SUBFRAME; |
| 1054 | |
| 1055 | // The browser should never initiate a subframe navigation. |
| 1056 | DCHECK(!extra_data); |
| 1057 | Send(new ViewHostMsg_FrameNavigate(routing_id_, params)); |
| 1058 | } |
| 1059 | |
| 1060 | last_page_id_sent_to_browser_ = |
| 1061 | std::max(last_page_id_sent_to_browser_, page_id_); |
| 1062 | |
| 1063 | // If we end up reusing this WebRequest (for example, due to a #ref click), |
| 1064 | // we don't want the transition type to persist. |
| 1065 | if (extra_data) |
| 1066 | extra_data->transition_type = PageTransition::LINK; // Just clear it. |
| 1067 | } |
| 1068 | |
| 1069 | // Tell the embedding application that the title of the active page has changed |
| 1070 | void RenderView::UpdateTitle(WebFrame* frame, const std::wstring& title) { |
| 1071 | // Ignore all but top level navigations... |
| 1072 | if (webview()->GetMainFrame() == frame) |
| 1073 | Send(new ViewHostMsg_UpdateTitle(routing_id_, page_id_, title)); |
| 1074 | } |
| 1075 | |
| 1076 | void RenderView::UpdateEncoding(WebFrame* frame, |
| 1077 | const std::wstring& encoding_name) { |
| 1078 | // Only update main frame's encoding_name. |
| 1079 | if (webview()->GetMainFrame() == frame && |
| 1080 | last_encoding_name_ != encoding_name) { |
| 1081 | // save the encoding name for later comparing. |
| 1082 | last_encoding_name_ = encoding_name; |
| 1083 | |
| 1084 | Send(new ViewHostMsg_UpdateEncoding(routing_id_, |
| 1085 | last_encoding_name_)); |
| 1086 | } |
| 1087 | } |
| 1088 | |
| 1089 | void RenderView::UpdateSessionHistory(WebFrame* frame) { |
| 1090 | // If we have a valid page ID at this point, then it corresponds to the page |
| 1091 | // we are navigating away from. Otherwise, this is the first navigation, so |
| 1092 | // there is no past session history to record. |
| 1093 | if (page_id_ == -1) |
| 1094 | return; |
| 1095 | |
| 1096 | GURL url; |
| 1097 | std::wstring title; |
| 1098 | std::string state; |
| 1099 | if (!webview()->GetMainFrame()->GetPreviousState(&url, &title, &state)) |
| 1100 | return; |
| 1101 | |
| 1102 | Send(new ViewHostMsg_UpdateState(routing_id_, page_id_, url, title, state)); |
| 1103 | } |
| 1104 | |
| 1105 | /////////////////////////////////////////////////////////////////////////////// |
| 1106 | // WebViewDelegate |
| 1107 | |
| 1108 | void RenderView::DidStartLoading(WebView* webview) { |
| 1109 | if (is_loading_) { |
| 1110 | DLOG(WARNING) << "DidStartLoading called while loading"; |
| 1111 | return; |
| 1112 | } |
| 1113 | |
| 1114 | is_loading_ = true; |
| 1115 | // Clear the pointer so that we can assign it only when there is an unknown |
| 1116 | // plugin on a page. |
| 1117 | first_default_plugin_ = NULL; |
| 1118 | |
| 1119 | Send(new ViewHostMsg_DidStartLoading(routing_id_, page_id_)); |
| 1120 | } |
| 1121 | |
| 1122 | void RenderView::DidStopLoading(WebView* webview) { |
| 1123 | if (!is_loading_) { |
| 1124 | DLOG(WARNING) << "DidStopLoading called while not loading"; |
| 1125 | return; |
| 1126 | } |
| 1127 | |
| 1128 | is_loading_ = false; |
| 1129 | |
| 1130 | // NOTE: For now we're doing the safest thing, and sending out notification |
| 1131 | // when done loading. This currently isn't an issue as the favicon is only |
| 1132 | // displayed when done loading. Ideally we would send notification when |
| 1133 | // finished parsing the head, but webkit doesn't support that yet. |
| 1134 | // The feed discovery code would also benefit from access to the head. |
| 1135 | GURL favicon_url(webview->GetMainFrame()->GetFavIconURL()); |
| 1136 | if (!favicon_url.is_empty()) |
| 1137 | Send(new ViewHostMsg_UpdateFavIconURL(routing_id_, page_id_, favicon_url)); |
| 1138 | |
| 1139 | AddGURLSearchProvider(webview->GetMainFrame()->GetOSDDURL(), |
| 1140 | true); // autodetected |
| 1141 | |
| 1142 | Send(new ViewHostMsg_DidStopLoading(routing_id_, page_id_)); |
| 1143 | |
| 1144 | MessageLoop::current()->PostDelayedTask(FROM_HERE, |
| 1145 | method_factory_.NewRunnableMethod(&RenderView::CapturePageInfo, page_id_, |
| 1146 | false), |
| 1147 | kDelayForCaptureMs); |
| 1148 | |
| 1149 | // The page is loaded. Try to process the file we need to upload if any. |
| 1150 | ProcessPendingUpload(); |
| 1151 | |
| 1152 | // Since the page is done loading, we are sure we don't need to try |
| 1153 | // again. |
| 1154 | ResetPendingUpload(); |
| 1155 | } |
| 1156 | |
| 1157 | void RenderView::DidStartProvisionalLoadForFrame( |
| 1158 | WebView* webview, |
| 1159 | WebFrame* frame, |
| 1160 | NavigationGesture gesture) { |
[email protected] | 77e09a9 | 2008-08-01 18:11:04 | [diff] [blame] | 1161 | if (webview->GetMainFrame() == frame) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1162 | navigation_gesture_ = gesture; |
[email protected] | 77e09a9 | 2008-08-01 18:11:04 | [diff] [blame] | 1163 | |
| 1164 | // Make sure redirect tracking state is clear for the new load. |
| 1165 | completed_client_redirect_src_ = GURL(); |
| 1166 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1167 | |
| 1168 | Send(new ViewHostMsg_DidStartProvisionalLoadForFrame( |
| 1169 | routing_id_, webview->GetMainFrame() == frame, |
| 1170 | frame->GetProvisionalDataSource()->GetRequest().GetURL())); |
| 1171 | } |
| 1172 | |
| 1173 | bool RenderView::DidLoadResourceFromMemoryCache(WebView* webview, |
| 1174 | const WebRequest& request, |
| 1175 | const WebResponse& response, |
| 1176 | WebFrame* frame) { |
| 1177 | // Let the browser know we loaded a resource from the memory cache. This |
| 1178 | // message is needed to display the correct SSL indicators. |
| 1179 | Send(new ViewHostMsg_DidLoadResourceFromMemoryCache(routing_id_, |
| 1180 | request.GetURL(), response.GetSecurityInfo())); |
| 1181 | |
| 1182 | return false; |
| 1183 | } |
| 1184 | |
| 1185 | void RenderView::DidReceiveProvisionalLoadServerRedirect(WebView* webview, |
| 1186 | WebFrame* frame) { |
| 1187 | if (frame == webview->GetMainFrame()) { |
| 1188 | // Received a redirect on the main frame. |
| 1189 | WebDataSource* data_source = |
| 1190 | webview->GetMainFrame()->GetProvisionalDataSource(); |
| 1191 | if (!data_source) { |
| 1192 | // Should only be invoked when we have a data source. |
| 1193 | NOTREACHED(); |
| 1194 | return; |
| 1195 | } |
| 1196 | const std::vector<GURL>& redirects = data_source->GetRedirectChain(); |
| 1197 | if (redirects.size() >= 2) { |
| 1198 | Send(new ViewHostMsg_DidRedirectProvisionalLoad( |
| 1199 | routing_id_, page_id_, redirects[redirects.size() - 2], |
| 1200 | redirects[redirects.size() - 1])); |
| 1201 | } |
| 1202 | } |
| 1203 | } |
| 1204 | |
| 1205 | void RenderView::DidFailProvisionalLoadWithError(WebView* webview, |
| 1206 | const WebError& error, |
| 1207 | WebFrame* frame) { |
| 1208 | // Notify the browser that we failed a provisional load with an error. |
| 1209 | // |
| 1210 | // Note: It is important this notification occur before DidStopLoading so the |
| 1211 | // SSL manager can react to the provisional load failure before being |
| 1212 | // notified the load stopped. |
| 1213 | // |
| 1214 | WebDataSource* ds = frame->GetProvisionalDataSource(); |
| 1215 | DCHECK(ds); |
| 1216 | |
| 1217 | const WebRequest& failed_request = ds->GetRequest(); |
| 1218 | |
| 1219 | bool show_repost_interstitial = |
| 1220 | (error.GetErrorCode() == net::ERR_CACHE_MISS && |
| 1221 | LowerCaseEqualsASCII(failed_request.GetHttpMethod(), "post")); |
| 1222 | Send(new ViewHostMsg_DidFailProvisionalLoadWithError( |
| 1223 | routing_id_, frame == webview->GetMainFrame(), |
| 1224 | error.GetErrorCode(), error.GetFailedURL(), |
| 1225 | show_repost_interstitial)); |
| 1226 | |
| 1227 | // TODO(darin): This should not be necessary! |
| 1228 | if (is_loading_) |
| 1229 | DidStopLoading(webview); |
| 1230 | |
| 1231 | // Don't display an error page if this is simply a cancelled load. Aside |
| 1232 | // from being dumb, WebCore doesn't expect it and it will cause a crash. |
| 1233 | if (error.GetErrorCode() == net::ERR_ABORTED) |
| 1234 | return; |
| 1235 | |
| 1236 | // If this is a failed back/forward/reload navigation, then we need to do a |
| 1237 | // 'replace' load. This is necessary to avoid messing up session history. |
| 1238 | // Otherwise, we do a normal load, which simulates a 'go' navigation as far |
| 1239 | // as session history is concerned. |
| 1240 | RenderViewExtraRequestData* extra_data = |
| 1241 | static_cast<RenderViewExtraRequestData*>(failed_request.GetExtraData()); |
| 1242 | bool replace = extra_data && !extra_data->is_new_navigation(); |
| 1243 | |
| 1244 | const GURL& failed_url = error.GetFailedURL(); |
| 1245 | const GURL& error_page_url = GetAlternateErrorPageURL(failed_url, |
| 1246 | WebViewDelegate::DNS_ERROR); |
| 1247 | if (error.GetErrorCode() == net::ERR_NAME_NOT_RESOLVED && |
| 1248 | error_page_url.is_valid()) { |
| 1249 | // Ask the WebFrame to fetch the alternate error page for us. |
| 1250 | frame->LoadAlternateHTMLErrorPage(&failed_request, error, error_page_url, |
| 1251 | replace, GURL(kUnreachableWebDataURL)); |
| 1252 | } else { |
| 1253 | LoadNavigationErrorPage(frame, &failed_request, error, std::string(), |
| 1254 | replace); |
| 1255 | } |
| 1256 | } |
| 1257 | |
| 1258 | void RenderView::LoadNavigationErrorPage(WebFrame* frame, |
| 1259 | const WebRequest* failed_request, |
| 1260 | const WebError& error, |
| 1261 | const std::string& html, |
| 1262 | bool replace) { |
| 1263 | const GURL& failed_url = error.GetFailedURL(); |
| 1264 | |
| 1265 | std::string alt_html; |
| 1266 | if (html.empty()) { |
| 1267 | // Use a local error page. |
| 1268 | int resource_id; |
| 1269 | DictionaryValue error_strings; |
| 1270 | if (error.GetErrorCode() == net::ERR_CACHE_MISS && |
| 1271 | LowerCaseEqualsASCII(failed_request->GetHttpMethod(), "post")) { |
| 1272 | GetFormRepostErrorValues(failed_url, &error_strings); |
| 1273 | resource_id = IDR_ERROR_NO_DETAILS_HTML; |
| 1274 | } else { |
| 1275 | GetLocalizedErrorValues(error, &error_strings); |
| 1276 | resource_id = IDR_NET_ERROR_HTML; |
| 1277 | } |
| 1278 | error_strings.SetString(L"textdirection", |
| 1279 | (l10n_util::GetTextDirection() == l10n_util::RIGHT_TO_LEFT) ? |
| 1280 | L"rtl" : L"ltr"); |
| 1281 | |
| 1282 | alt_html = GetAltHTMLForTemplate(error_strings, resource_id); |
| 1283 | } else { |
| 1284 | alt_html = html; |
| 1285 | } |
| 1286 | |
| 1287 | // Use a data: URL as the site URL to prevent against XSS attacks. |
| 1288 | scoped_ptr<WebRequest> request(failed_request->Clone()); |
| 1289 | request->SetURL(GURL(kUnreachableWebDataURL)); |
| 1290 | |
| 1291 | frame->LoadAlternateHTMLString(request.get(), alt_html, failed_url, |
| 1292 | replace); |
| 1293 | } |
| 1294 | |
| 1295 | void RenderView::DidCommitLoadForFrame(WebView *webview, WebFrame* frame, |
| 1296 | bool is_new_navigation) { |
| 1297 | const WebRequest& request = |
| 1298 | webview->GetMainFrame()->GetDataSource()->GetRequest(); |
| 1299 | RenderViewExtraRequestData* extra_data = |
| 1300 | static_cast<RenderViewExtraRequestData*>(request.GetExtraData()); |
| 1301 | |
| 1302 | if (is_new_navigation) { |
| 1303 | // When we perform a new navigation, we need to update the previous session |
| 1304 | // history entry with state for the page we are leaving. |
| 1305 | UpdateSessionHistory(frame); |
| 1306 | |
| 1307 | // We bump our Page ID to correspond with the new session history entry. |
| 1308 | page_id_ = next_page_id_++; |
| 1309 | |
| 1310 | MessageLoop::current()->PostDelayedTask(FROM_HERE, |
| 1311 | method_factory_.NewRunnableMethod(&RenderView::CapturePageInfo, |
| 1312 | page_id_, true), |
| 1313 | kDelayForForcedCaptureMs); |
| 1314 | } else { |
| 1315 | // Inspect the extra_data on the main frame (set in our Navigate method) to |
| 1316 | // see if the navigation corresponds to a session history navigation... |
| 1317 | // Note: |frame| may or may not be the toplevel frame, but for the case |
| 1318 | // of capturing session history, the first committed frame suffices. We |
| 1319 | // keep track of whether we've seen this commit before so that only capture |
| 1320 | // session history once per navigation. |
| 1321 | if (extra_data && !extra_data->is_new_navigation() && |
| 1322 | !extra_data->request_committed) { |
| 1323 | // This is a successful session history navigation! |
| 1324 | UpdateSessionHistory(frame); |
| 1325 | |
| 1326 | page_id_ = extra_data->pending_page_id(); |
| 1327 | } |
| 1328 | } |
| 1329 | |
| 1330 | // Remember that we've already processed this request, so we don't update |
| 1331 | // the session history again. We do this regardless of whether this is |
| 1332 | // a session history navigation, because if we attempted a session history |
| 1333 | // navigation without valid HistoryItem state, WebCore will think it is a |
| 1334 | // new navigation. |
| 1335 | if (extra_data) |
| 1336 | extra_data->request_committed = true; |
| 1337 | |
| 1338 | UpdateURL(frame); |
| 1339 | |
| 1340 | // If this committed load was initiated by a client redirect, we're |
| 1341 | // at the last stop now, so clear it. |
| 1342 | completed_client_redirect_src_ = GURL(); |
| 1343 | |
| 1344 | // Check whether we have new encoding name. |
| 1345 | UpdateEncoding(frame, webview->GetMainFrameEncodingName()); |
| 1346 | } |
| 1347 | |
| 1348 | void RenderView::DidReceiveTitle(WebView* webview, |
| 1349 | const std::wstring& title, |
| 1350 | WebFrame* frame) { |
| 1351 | UpdateTitle(frame, title); |
| 1352 | |
| 1353 | // Also check whether we have new encoding name. |
| 1354 | UpdateEncoding(frame, webview->GetMainFrameEncodingName()); |
| 1355 | } |
| 1356 | |
| 1357 | void RenderView::DidFinishLoadForFrame(WebView* webview, WebFrame* frame) { |
| 1358 | } |
| 1359 | |
| 1360 | void RenderView::DidFailLoadWithError(WebView* webview, |
| 1361 | const WebError& error, |
| 1362 | WebFrame* frame) { |
| 1363 | } |
| 1364 | |
| 1365 | void RenderView::DidFinishDocumentLoadForFrame(WebView* webview, |
| 1366 | WebFrame* frame) { |
| 1367 | // Check whether we have new encoding name. |
| 1368 | UpdateEncoding(frame, webview->GetMainFrameEncodingName()); |
| 1369 | } |
| 1370 | |
| 1371 | void RenderView::DidHandleOnloadEventsForFrame(WebView* webview, |
| 1372 | WebFrame* frame) { |
| 1373 | } |
| 1374 | |
| 1375 | void RenderView::DidChangeLocationWithinPageForFrame(WebView* webview, |
| 1376 | WebFrame* frame, |
| 1377 | bool is_new_navigation) { |
| 1378 | DidCommitLoadForFrame(webview, frame, is_new_navigation); |
| 1379 | } |
| 1380 | |
| 1381 | void RenderView::DidReceiveIconForFrame(WebView* webview, |
| 1382 | WebFrame* frame) { |
| 1383 | } |
| 1384 | |
| 1385 | void RenderView::WillPerformClientRedirect(WebView* webview, |
| 1386 | WebFrame* frame, |
| 1387 | const GURL& src_url, |
| 1388 | const GURL& dest_url, |
| 1389 | unsigned int delay_seconds, |
| 1390 | unsigned int fire_date) { |
| 1391 | } |
| 1392 | |
| 1393 | void RenderView::DidCancelClientRedirect(WebView* webview, |
| 1394 | WebFrame* frame) { |
| 1395 | } |
| 1396 | |
| 1397 | void RenderView::DidCompleteClientRedirect(WebView* webview, |
| 1398 | WebFrame* frame, |
| 1399 | const GURL& source) { |
| 1400 | if (webview->GetMainFrame() == frame) |
| 1401 | completed_client_redirect_src_ = source; |
| 1402 | } |
| 1403 | |
| 1404 | void RenderView::BindDOMAutomationController(WebFrame* webframe) { |
| 1405 | dom_automation_controller_.set_message_sender(this); |
| 1406 | dom_automation_controller_.set_routing_id(routing_id_); |
| 1407 | dom_automation_controller_.BindToJavascript(webframe, |
| 1408 | L"domAutomationController"); |
| 1409 | } |
| 1410 | |
| 1411 | void RenderView::WindowObjectCleared(WebFrame* webframe) { |
| 1412 | external_js_object_.set_render_view(this); |
| 1413 | external_js_object_.BindToJavascript(webframe, L"external"); |
| 1414 | if (enable_dom_automation_) |
| 1415 | BindDOMAutomationController(webframe); |
| 1416 | if (enable_dom_ui_bindings_) { |
| 1417 | dom_ui_bindings_.set_message_sender(this); |
| 1418 | dom_ui_bindings_.set_routing_id(routing_id_); |
| 1419 | dom_ui_bindings_.BindToJavascript(webframe, L"chrome"); |
| 1420 | } |
[email protected] | 9a2051d | 2008-08-15 20:12:42 | [diff] [blame] | 1421 | |
| 1422 | external_host_bindings_.set_message_sender(this); |
| 1423 | external_host_bindings_.set_routing_id(routing_id_); |
| 1424 | external_host_bindings_.BindToJavascript(webframe, L"externalHost"); |
| 1425 | |
[email protected] | 3a453fa | 2008-08-15 18:46:34 | [diff] [blame] | 1426 | #ifdef CHROME_PERSONALIZATION |
| 1427 | Personalization::ConfigureRendererPersonalization(personalization_, this, |
| 1428 | routing_id_, webframe); |
| 1429 | #endif |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1430 | } |
| 1431 | |
| 1432 | WindowOpenDisposition RenderView::DispositionForNavigationAction( |
| 1433 | WebView* webview, |
| 1434 | WebFrame* frame, |
| 1435 | const WebRequest* request, |
| 1436 | WebNavigationType type, |
| 1437 | WindowOpenDisposition disposition, |
| 1438 | bool is_redirect) { |
| 1439 | // Webkit is asking whether to navigate to a new URL. |
| 1440 | // This is fine normally, except if we're showing UI from one security |
| 1441 | // context and they're trying to navigate to a different context. |
| 1442 | const GURL& url = request->GetURL(); |
| 1443 | // We only care about navigations that are within the current tab (as opposed |
| 1444 | // to, for example, opening a new window). |
| 1445 | // But we sometimes navigate to about:blank to clear a tab, and we want to |
| 1446 | // still allow that. |
| 1447 | if (disposition == CURRENT_TAB && !(url.SchemeIs("about"))) { |
| 1448 | // GetExtraData is NULL when we did not issue the request ourselves (see |
| 1449 | // OnNavigate), and so such a request may correspond to a link-click, |
| 1450 | // script, or drag-n-drop initiated navigation. |
| 1451 | if (frame == webview->GetMainFrame() && !request->GetExtraData()) { |
| 1452 | // When we received such unsolicited navigations, we sometimes want to |
| 1453 | // punt them up to the browser to handle. |
| 1454 | if (enable_dom_ui_bindings_ || |
| 1455 | frame->GetInViewSourceMode() || |
| 1456 | url.SchemeIs("view-source")) { |
| 1457 | OpenURL(webview, url, disposition); |
| 1458 | return IGNORE_ACTION; // Suppress the load here. |
| 1459 | } |
| 1460 | } |
| 1461 | } |
| 1462 | |
| 1463 | // Detect when a page is "forking" a new tab that can be safely rendered in |
| 1464 | // its own process. This is done by sites like Gmail that try to open links |
| 1465 | // in new windows without script connections back to the original page. We |
| 1466 | // treat such cases as browser navigations (in which we will create a new |
| 1467 | // renderer for a cross-site navigation), rather than WebKit navigations. |
| 1468 | // |
| 1469 | // We use the following heuristic to decide whether to fork a new page in its |
| 1470 | // own process: |
| 1471 | // The parent page must open a new tab to about:blank, set the new tab's |
| 1472 | // window.opener to null, and then redirect the tab to a cross-site URL using |
| 1473 | // JavaScript. |
| 1474 | bool is_fork = |
| 1475 | // Must start from a tab showing about:blank, which is later redirected. |
| 1476 | frame->GetURL() == GURL("about:blank") && |
| 1477 | // Must be the first real navigation of the tab. |
| 1478 | GetHistoryBackListCount() < 1 && |
| 1479 | GetHistoryForwardListCount() < 1 && |
| 1480 | // The parent page must have set the child's window.opener to null before |
| 1481 | // redirecting to the desired URL. |
| 1482 | frame->GetOpener() == NULL && |
| 1483 | // Must be a top-level frame. |
| 1484 | frame->GetParent() == NULL && |
| 1485 | // Must not have issued the request from this page. GetExtraData is NULL |
| 1486 | // when the navigation is being done by something outside the page. |
| 1487 | !request->GetExtraData() && |
| 1488 | // Must be targeted at the current tab. |
| 1489 | disposition == CURRENT_TAB && |
| 1490 | // Must be a JavaScript navigation, which appears as "other". |
| 1491 | type == WebNavigationTypeOther; |
| 1492 | if (is_fork) { |
| 1493 | // Open the URL via the browser, not via WebKit. |
| 1494 | OpenURL(webview, url, disposition); |
| 1495 | return IGNORE_ACTION; |
| 1496 | } |
| 1497 | |
| 1498 | return disposition; |
| 1499 | } |
| 1500 | |
| 1501 | void RenderView::RunJavaScriptAlert(WebView* webview, |
| 1502 | const std::wstring& message) { |
| 1503 | RunJavaScriptMessage(MessageBoxView::kIsJavascriptAlert, |
| 1504 | message, |
| 1505 | std::wstring(), |
| 1506 | NULL); |
| 1507 | } |
| 1508 | |
| 1509 | bool RenderView::RunJavaScriptConfirm(WebView* webview, |
| 1510 | const std::wstring& message) { |
| 1511 | return RunJavaScriptMessage(MessageBoxView::kIsJavascriptConfirm, |
| 1512 | message, |
| 1513 | std::wstring(), |
| 1514 | NULL); |
| 1515 | } |
| 1516 | |
| 1517 | bool RenderView::RunJavaScriptPrompt(WebView* webview, |
| 1518 | const std::wstring& message, |
| 1519 | const std::wstring& default_value, |
| 1520 | std::wstring* result) { |
| 1521 | return RunJavaScriptMessage(MessageBoxView::kIsJavascriptPrompt, |
| 1522 | message, |
| 1523 | default_value, |
| 1524 | result); |
| 1525 | } |
| 1526 | |
| 1527 | bool RenderView::RunJavaScriptMessage(int type, |
| 1528 | const std::wstring& message, |
| 1529 | const std::wstring& default_value, |
| 1530 | std::wstring* result) { |
| 1531 | bool success = false; |
| 1532 | std::wstring result_temp; |
| 1533 | if (!result) |
| 1534 | result = &result_temp; |
| 1535 | IPC::SyncMessage* msg = new ViewHostMsg_RunJavaScriptMessage( |
| 1536 | routing_id_, message, default_value, type, &success, result); |
| 1537 | |
| 1538 | msg->set_pump_messages_event(modal_dialog_event_); |
| 1539 | Send(msg); |
| 1540 | |
| 1541 | return success; |
| 1542 | } |
| 1543 | |
| 1544 | void RenderView::AddGURLSearchProvider(const GURL& osd_url, bool autodetected) { |
| 1545 | if (!osd_url.is_empty()) |
| 1546 | Send(new ViewHostMsg_PageHasOSDD(routing_id_, page_id_, osd_url, |
| 1547 | autodetected)); |
| 1548 | } |
| 1549 | |
| 1550 | bool RenderView::RunBeforeUnloadConfirm(WebView* webview, |
| 1551 | const std::wstring& message) { |
| 1552 | bool success = false; |
| 1553 | // This is an ignored return value, but is included so we can accept the same |
| 1554 | // response as RunJavaScriptMessage. |
| 1555 | std::wstring ignored_result; |
| 1556 | IPC::SyncMessage* msg = new ViewHostMsg_RunBeforeUnloadConfirm( |
| 1557 | routing_id_, message, &success, &ignored_result); |
| 1558 | |
| 1559 | msg->set_pump_messages_event(modal_dialog_event_); |
| 1560 | Send(msg); |
| 1561 | |
| 1562 | return success; |
| 1563 | } |
| 1564 | |
| 1565 | void RenderView::OnUnloadListenerChanged(WebView* webview, WebFrame* webframe) { |
| 1566 | bool has_listener = false; |
| 1567 | if (!has_unload_listener_) { |
| 1568 | has_listener = webframe->HasUnloadListener(); |
| 1569 | } else { |
| 1570 | WebFrame* frame = webview->GetMainFrame(); |
| 1571 | while (frame != NULL) { |
| 1572 | if (frame->HasUnloadListener()) { |
| 1573 | has_listener = true; |
| 1574 | break; |
| 1575 | } |
| 1576 | frame = webview->GetNextFrameAfter(frame, false); |
| 1577 | } |
| 1578 | } |
| 1579 | if (has_listener != has_unload_listener_) { |
| 1580 | has_unload_listener_ = has_listener; |
| 1581 | Send(new ViewHostMsg_UnloadListenerChanged(routing_id_, has_listener)); |
| 1582 | } |
| 1583 | } |
| 1584 | |
| 1585 | void RenderView::ShowModalHTMLDialog(const GURL& url, int width, int height, |
| 1586 | const std::string& json_arguments, |
| 1587 | std::string* json_retval) { |
| 1588 | IPC::SyncMessage* msg = new ViewHostMsg_ShowModalHTMLDialog( |
| 1589 | routing_id_, url, width, height, json_arguments, json_retval); |
| 1590 | |
| 1591 | msg->set_pump_messages_event(modal_dialog_event_); |
| 1592 | Send(msg); |
| 1593 | } |
| 1594 | |
| 1595 | uint32 RenderView::GetCPBrowsingContext() { |
| 1596 | uint32 context = 0; |
| 1597 | Send(new ViewHostMsg_GetCPBrowsingContext(&context)); |
| 1598 | return context; |
| 1599 | } |
| 1600 | |
| 1601 | // Tell the browser to display a destination link. |
| 1602 | void RenderView::UpdateTargetURL(WebView* webview, const GURL& url) { |
| 1603 | if (url != target_url_) { |
| 1604 | if (target_url_status_ == TARGET_INFLIGHT || |
| 1605 | target_url_status_ == TARGET_PENDING) { |
| 1606 | // If we have a request in-flight, save the URL to be sent when we |
| 1607 | // receive an ACK to the in-flight request. We can happily overwrite |
| 1608 | // any existing pending sends. |
| 1609 | pending_target_url_ = url; |
| 1610 | target_url_status_ = TARGET_PENDING; |
| 1611 | } else { |
| 1612 | Send(new ViewHostMsg_UpdateTargetURL(routing_id_, page_id_, url)); |
| 1613 | target_url_ = url; |
| 1614 | target_url_status_ = TARGET_INFLIGHT; |
| 1615 | } |
| 1616 | } |
| 1617 | } |
| 1618 | |
| 1619 | void RenderView::RunFileChooser(const std::wstring& default_filename, |
| 1620 | WebFileChooserCallback* file_chooser) { |
| 1621 | if (file_chooser_.get()) { |
| 1622 | // TODO(brettw): bug 1235154: This should be a synchronous message to deal |
| 1623 | // with the fact that web pages can programatically trigger this. With the |
| 1624 | // asnychronous messages, we can get an additional call when one is pending, |
| 1625 | // which this test is for. For now, we just ignore the additional file |
| 1626 | // chooser request. WebKit doesn't do anything to expect the callback, so |
| 1627 | // we can just ignore calling it. |
| 1628 | delete file_chooser; |
| 1629 | return; |
| 1630 | } |
| 1631 | file_chooser_.reset(file_chooser); |
| 1632 | Send(new ViewHostMsg_RunFileChooser(routing_id_, default_filename)); |
| 1633 | } |
| 1634 | |
| 1635 | void RenderView::AddMessageToConsole(WebView* webview, |
| 1636 | const std::wstring& message, |
| 1637 | unsigned int line_no, |
| 1638 | const std::wstring& source_id) { |
| 1639 | Send(new ViewHostMsg_AddMessageToConsole(routing_id_, message, |
| 1640 | static_cast<int32>(line_no), |
| 1641 | source_id)); |
| 1642 | } |
| 1643 | |
| 1644 | void RenderView::AddSearchProvider(const std::string& url) { |
| 1645 | AddGURLSearchProvider(GURL(url), |
| 1646 | false); // not autodetected |
| 1647 | } |
| 1648 | |
| 1649 | void RenderView::DebuggerOutput(const std::wstring& out) { |
| 1650 | Send(new ViewHostMsg_DebuggerOutput(routing_id_, out)); |
| 1651 | } |
| 1652 | |
| 1653 | WebView* RenderView::CreateWebView(WebView* webview, bool user_gesture) { |
| 1654 | int32 routing_id = MSG_ROUTING_NONE; |
| 1655 | HANDLE modal_dialog_event; |
| 1656 | bool result = RenderThread::current()->Send( |
| 1657 | new ViewHostMsg_CreateView(routing_id_, user_gesture, &routing_id, |
| 1658 | &modal_dialog_event)); |
| 1659 | if (routing_id == MSG_ROUTING_NONE) { |
| 1660 | DCHECK(modal_dialog_event == NULL); |
| 1661 | return NULL; |
| 1662 | } |
| 1663 | |
| 1664 | // The WebView holds a reference to this new RenderView |
| 1665 | const WebPreferences& prefs = webview->GetPreferences(); |
| 1666 | RenderView* view = RenderView::Create(NULL, modal_dialog_event, routing_id_, |
| 1667 | prefs, routing_id); |
| 1668 | view->set_opened_by_user_gesture(user_gesture); |
| 1669 | |
| 1670 | // Copy over the alternate error page URL so we can have alt error pages in |
| 1671 | // the new render view (we don't need the browser to send the URL back down). |
| 1672 | view->alternate_error_page_url_ = alternate_error_page_url_; |
| 1673 | |
| 1674 | return view->webview(); |
| 1675 | } |
| 1676 | |
| 1677 | WebWidget* RenderView::CreatePopupWidget(WebView* webview) { |
| 1678 | RenderWidget* widget = RenderWidget::Create(routing_id_); |
| 1679 | return widget->webwidget(); |
| 1680 | } |
| 1681 | |
[email protected] | 173de1b | 2008-08-15 18:36:46 | [diff] [blame] | 1682 | static bool ShouldLoadPluginInProcess(const std::string& mime_type, |
| 1683 | bool* is_gears) { |
| 1684 | if (RenderProcess::ShouldLoadPluginsInProcess()) |
| 1685 | return true; |
| 1686 | |
| 1687 | if (mime_type == "application/x-googlegears") { |
| 1688 | *is_gears = true; |
| 1689 | CommandLine cmd; |
| 1690 | return cmd.HasSwitch(switches::kGearsInRenderer); |
| 1691 | } |
| 1692 | |
| 1693 | return false; |
| 1694 | } |
| 1695 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1696 | WebPluginDelegate* RenderView::CreatePluginDelegate( |
| 1697 | WebView* webview, |
| 1698 | const GURL& url, |
| 1699 | const std::string& mime_type, |
| 1700 | const std::string& clsid, |
| 1701 | std::string* actual_mime_type) { |
[email protected] | 173de1b | 2008-08-15 18:36:46 | [diff] [blame] | 1702 | bool is_gears = false; |
| 1703 | if (ShouldLoadPluginInProcess(mime_type, &is_gears)) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1704 | std::wstring path; |
| 1705 | RenderThread::current()->Send( |
| 1706 | new ViewHostMsg_GetPluginPath(url, mime_type, clsid, &path, |
| 1707 | actual_mime_type)); |
| 1708 | if (path.empty()) |
| 1709 | return NULL; |
| 1710 | |
| 1711 | std::string mime_type_to_use; |
| 1712 | if (actual_mime_type && !actual_mime_type->empty()) |
| 1713 | mime_type_to_use = *actual_mime_type; |
| 1714 | else |
| 1715 | mime_type_to_use = mime_type; |
| 1716 | |
[email protected] | 173de1b | 2008-08-15 18:36:46 | [diff] [blame] | 1717 | if (is_gears) |
| 1718 | ChromePluginLib::Create(path, GetCPBrowserFuncsForRenderer()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1719 | return WebPluginDelegateImpl::Create(path, mime_type_to_use, host_window_); |
| 1720 | } |
| 1721 | |
| 1722 | WebPluginDelegateProxy* proxy = |
| 1723 | WebPluginDelegateProxy::Create(url, mime_type, clsid, this); |
| 1724 | if (!proxy) |
| 1725 | return NULL; |
| 1726 | |
| 1727 | // We hold onto the proxy so we can poke it when we are painting. See our |
| 1728 | // DidPaint implementation below. |
| 1729 | plugin_delegates_.push_back(proxy); |
| 1730 | |
| 1731 | return proxy; |
| 1732 | } |
| 1733 | |
| 1734 | void RenderView::OnMissingPluginStatus(WebPluginDelegate* delegate, |
| 1735 | int status) { |
| 1736 | if (first_default_plugin_ == NULL) { |
| 1737 | // Show the InfoBar for the first available plugin. |
| 1738 | if (status == default_plugin::MISSING_PLUGIN_AVAILABLE) { |
| 1739 | first_default_plugin_ = delegate; |
| 1740 | Send(new ViewHostMsg_MissingPluginStatus(routing_id_, status)); |
| 1741 | } |
| 1742 | } else { |
| 1743 | // Closes the InfoBar if user clicks on the plugin (instead of the InfoBar) |
| 1744 | // to start the download/install. |
| 1745 | if (status == default_plugin::MISSING_PLUGIN_USER_STARTED_DOWNLOAD) { |
| 1746 | Send(new ViewHostMsg_MissingPluginStatus(routing_id_, status)); |
| 1747 | } |
| 1748 | } |
| 1749 | } |
| 1750 | |
| 1751 | void RenderView::OpenURL(WebView* webview, const GURL& url, |
| 1752 | WindowOpenDisposition disposition) { |
| 1753 | Send(new ViewHostMsg_OpenURL(routing_id_, url, disposition)); |
| 1754 | } |
| 1755 | |
| 1756 | // We are supposed to get a single call to Show for a newly created RenderView |
| 1757 | // that was created via RenderView::CreateWebView. So, we wait until this |
| 1758 | // point to dispatch the ShowView message. |
| 1759 | // |
| 1760 | // This method provides us with the information about how to display the newly |
| 1761 | // created RenderView (i.e., as a constrained popup or as a new tab). |
| 1762 | // |
| 1763 | void RenderView::Show(WebWidget* webwidget, WindowOpenDisposition disposition) { |
| 1764 | DCHECK(!did_show_) << "received extraneous Show call"; |
| 1765 | DCHECK(opener_id_ != MSG_ROUTING_NONE); |
| 1766 | |
| 1767 | if (did_show_) |
| 1768 | return; |
| 1769 | did_show_ = true; |
| 1770 | |
| 1771 | // NOTE: initial_pos_ may still have its default values at this point, but |
| 1772 | // that's okay. It'll be ignored if disposition is not NEW_POPUP, or the |
| 1773 | // browser process will impose a default position otherwise. |
| 1774 | Send(new ViewHostMsg_ShowView( |
| 1775 | opener_id_, routing_id_, disposition, initial_pos_, |
| 1776 | WasOpenedByUserGestureHelper())); |
| 1777 | } |
| 1778 | |
| 1779 | void RenderView::RunModal(WebWidget* webwidget) { |
| 1780 | DCHECK(did_show_) << "should already have shown the view"; |
| 1781 | |
| 1782 | IPC::SyncMessage* msg = new ViewHostMsg_RunModal(routing_id_); |
| 1783 | |
| 1784 | msg->set_pump_messages_event(modal_dialog_event_); |
| 1785 | Send(msg); |
| 1786 | } |
| 1787 | |
| 1788 | void RenderView::SyncNavigationState() { |
| 1789 | if (!webview()) |
| 1790 | return; |
| 1791 | |
| 1792 | GURL url; |
| 1793 | std::wstring title; |
| 1794 | std::string state; |
| 1795 | if (!webview()->GetMainFrame()->GetCurrentState(&url, &title, &state)) |
| 1796 | return; |
| 1797 | |
| 1798 | Send(new ViewHostMsg_UpdateState(routing_id_, page_id_, url, title, state)); |
| 1799 | } |
| 1800 | |
| 1801 | void RenderView::ShowContextMenu(WebView* webview, |
| 1802 | ContextNode::Type type, |
| 1803 | int x, |
| 1804 | int y, |
| 1805 | const GURL& link_url, |
| 1806 | const GURL& image_url, |
| 1807 | const GURL& page_url, |
| 1808 | const GURL& frame_url, |
| 1809 | const std::wstring& selection_text, |
| 1810 | const std::wstring& misspelled_word, |
| 1811 | int edit_flags) { |
| 1812 | ViewHostMsg_ContextMenu_Params params; |
| 1813 | params.type = type; |
| 1814 | params.x = x; |
| 1815 | params.y = y; |
| 1816 | params.image_url = image_url; |
| 1817 | params.link_url = link_url; |
| 1818 | params.page_url = page_url; |
| 1819 | params.frame_url = frame_url; |
| 1820 | params.selection_text = selection_text; |
| 1821 | params.misspelled_word = misspelled_word; |
| 1822 | params.edit_flags = edit_flags; |
| 1823 | Send(new ViewHostMsg_ContextMenu(routing_id_, params)); |
| 1824 | } |
| 1825 | |
| 1826 | void RenderView::StartDragging(WebView* webview, const WebDropData& drop_data) { |
| 1827 | Send(new ViewHostMsg_StartDragging(routing_id_, drop_data)); |
| 1828 | } |
| 1829 | |
| 1830 | void RenderView::TakeFocus(WebView* webview, bool reverse) { |
| 1831 | Send(new ViewHostMsg_TakeFocus(routing_id_, reverse)); |
| 1832 | } |
| 1833 | |
| 1834 | void RenderView::DidDownloadImage(int id, |
| 1835 | const GURL& image_url, |
| 1836 | bool errored, |
| 1837 | const SkBitmap& image) { |
| 1838 | Send(new ViewHostMsg_DidDownloadImage(routing_id_, id, image_url, errored, |
| 1839 | image)); |
| 1840 | } |
| 1841 | |
| 1842 | |
| 1843 | void RenderView::OnDownloadImage(int id, |
| 1844 | const GURL& image_url, |
| 1845 | int image_size) { |
| 1846 | if (!webview()->DownloadImage(id, image_url, image_size)) |
| 1847 | Send(new ViewHostMsg_DidDownloadImage(routing_id_, id, image_url, true, |
| 1848 | SkBitmap())); |
| 1849 | } |
| 1850 | |
| 1851 | void RenderView::OnGetApplicationInfo(int page_id) { |
| 1852 | webkit_glue::WebApplicationInfo app_info; |
| 1853 | if (page_id == page_id_) |
| 1854 | webkit_glue::GetApplicationInfo(webview(), &app_info); |
| 1855 | |
| 1856 | // Prune out any data URLs in the set of icons. The browser process expects |
| 1857 | // any icon with a data URL to have originated from a favicon. We don't want |
| 1858 | // to decode arbitrary data URLs in the browser process. See |
| 1859 | // http://b/issue?id=1162972 |
| 1860 | for (size_t i = 0; i < app_info.icons.size(); ++i) { |
| 1861 | if (app_info.icons[i].url.SchemeIs("data")) { |
| 1862 | app_info.icons.erase(app_info.icons.begin() + i); |
| 1863 | --i; |
| 1864 | } |
| 1865 | } |
| 1866 | |
| 1867 | Send(new ViewHostMsg_DidGetApplicationInfo(routing_id_, page_id, app_info)); |
| 1868 | } |
| 1869 | |
| 1870 | GURL RenderView::GetAlternateErrorPageURL(const GURL& failedURL, |
| 1871 | ErrorPageType error_type) { |
| 1872 | if (failedURL.SchemeIsSecure()) { |
| 1873 | // If the URL that failed was secure, then the embedding web page was not |
| 1874 | // expecting a network attacker to be able to manipulate its contents. As |
| 1875 | // we fetch alternate error pages over HTTP, we would be allowing a network |
| 1876 | // attacker to manipulate the contents of the response if we tried to use |
| 1877 | // the link doctor here. |
| 1878 | return GURL::EmptyGURL(); |
| 1879 | } |
| 1880 | |
| 1881 | // Grab the base URL from the browser process. |
| 1882 | if (!alternate_error_page_url_.is_valid()) |
| 1883 | return GURL::EmptyGURL(); |
| 1884 | |
| 1885 | // Strip query params from the failed URL. |
| 1886 | GURL::Replacements remove_params; |
| 1887 | remove_params.ClearUsername(); |
| 1888 | remove_params.ClearPassword(); |
| 1889 | remove_params.ClearQuery(); |
| 1890 | remove_params.ClearRef(); |
| 1891 | const GURL url_to_send = failedURL.ReplaceComponents(remove_params); |
| 1892 | |
| 1893 | // Construct the query params to send to link doctor. |
| 1894 | std::string params(alternate_error_page_url_.query()); |
| 1895 | params.append("&url="); |
| 1896 | params.append(EscapeQueryParamValue(url_to_send.spec())); |
| 1897 | params.append("&sourceid=chrome"); |
| 1898 | params.append("&error="); |
| 1899 | switch (error_type) { |
| 1900 | case DNS_ERROR: |
| 1901 | params.append("dnserror"); |
| 1902 | break; |
| 1903 | |
| 1904 | case HTTP_404: |
| 1905 | params.append("http404"); |
| 1906 | break; |
| 1907 | |
| 1908 | default: |
| 1909 | NOTREACHED() << "unknown ErrorPageType"; |
| 1910 | } |
| 1911 | |
| 1912 | // OK, build the final url to return. |
| 1913 | GURL::Replacements link_doctor_params; |
| 1914 | link_doctor_params.SetQueryStr(params); |
| 1915 | GURL url = alternate_error_page_url_.ReplaceComponents(link_doctor_params); |
| 1916 | return url; |
| 1917 | } |
| 1918 | |
| 1919 | void RenderView::OnFind(const FindInPageRequest& request) { |
| 1920 | WebFrame* main_frame = webview()->GetMainFrame(); |
| 1921 | WebFrame* frame_after_main = webview()->GetNextFrameAfter(main_frame, true); |
| 1922 | WebFrame* focused_frame = webview()->GetFocusedFrame(); |
| 1923 | WebFrame* search_frame = focused_frame; // start searching focused frame. |
| 1924 | |
| 1925 | bool multi_frame = (frame_after_main != main_frame); |
| 1926 | |
| 1927 | // If we have multiple frames, we don't want to wrap the search within the |
| 1928 | // frame, so we check here if we only have main_frame in the chain. |
| 1929 | bool wrap_within_frame = !multi_frame; |
| 1930 | |
| 1931 | gfx::Rect selection_rect; |
| 1932 | bool result = false; |
| 1933 | |
| 1934 | do { |
| 1935 | if (request.find_next) |
| 1936 | result = search_frame->FindNext(request, wrap_within_frame); |
| 1937 | else |
| 1938 | result = search_frame->Find(request, wrap_within_frame, &selection_rect); |
| 1939 | |
| 1940 | if (!result) { |
| 1941 | // don't leave text selected as you move to the next frame. |
| 1942 | search_frame->ClearSelection(); |
| 1943 | |
| 1944 | // Find the next frame, but skip the invisible ones. |
| 1945 | do { |
| 1946 | // What is the next frame to search? (we might be going backwards). Note |
| 1947 | // that we specify wrap=true so that search_frame never becomes NULL. |
| 1948 | search_frame = request.forward ? |
| 1949 | webview()->GetNextFrameAfter(search_frame, true) : |
| 1950 | webview()->GetPreviousFrameBefore(search_frame, true); |
| 1951 | } while (!search_frame->Visible() && search_frame != focused_frame); |
| 1952 | |
| 1953 | // make sure selection doesn't affect the search operation in new frame. |
| 1954 | search_frame->ClearSelection(); |
| 1955 | |
| 1956 | // If we have multiple frames and we have wrapped back around to the |
| 1957 | // focused frame, we need to search it once more allowing wrap within |
| 1958 | // the frame, otherwise it will report 'no match' if the focused frame has |
| 1959 | // reported matches, but no frames after the focused_frame contain a |
| 1960 | // match for the search word(s). |
| 1961 | if (multi_frame && search_frame == focused_frame) { |
| 1962 | if (request.find_next) |
| 1963 | result = search_frame->FindNext(request, true); // Force wrapping. |
| 1964 | else |
| 1965 | result = search_frame->Find(request, true, // Force wrapping. |
| 1966 | &selection_rect); |
| 1967 | } |
| 1968 | } |
| 1969 | |
| 1970 | // TODO(jcampan): http://b/issue?id=1157486 Remove StoreForFocus call once |
| 1971 | // we have the fix for 792423. |
| 1972 | search_frame->GetView()->StoreFocusForFrame(search_frame); |
| 1973 | webview()->SetFocusedFrame(search_frame); |
| 1974 | } while (!result && search_frame != focused_frame); |
| 1975 | |
| 1976 | // Make sure we don't leave any frame focused or the focus won't be restored |
| 1977 | // properly in WebViewImpl::SetFocus(). Note that we are talking here about |
| 1978 | // focused on the SelectionController, not FocusController. |
| 1979 | // webview()->GetFocusedFrame() will still return the last focused frame (as |
| 1980 | // it queries the FocusController). |
| 1981 | // TODO(jcampan): http://b/issue?id=1157486 Remove next line once we have the |
| 1982 | // fix for 792423. |
| 1983 | webview()->SetFocusedFrame(NULL); |
| 1984 | |
| 1985 | // We send back word that we found some matches, because we don't want to lag |
| 1986 | // when notifying the user that we found something. At this point we only know |
| 1987 | // that we found 1 match, but the scoping effort will tell us more. However, |
| 1988 | // if this is a FindNext request, the scoping effort is already under way, or |
| 1989 | // done already, so we have partial results. In that case we set it to -1 so |
| 1990 | // that it gets ignored by the FindInPageController. |
| 1991 | int match_count = result ? 1 : 0; // 1 here means possibly more coming. |
| 1992 | if (request.find_next) |
| 1993 | match_count = -1; |
| 1994 | |
| 1995 | // If we find no matches (or if this is Find Next) then this will be our last |
| 1996 | // status update. Otherwise the scoping effort will send more results. |
| 1997 | bool final_status_update = !result || request.find_next; |
| 1998 | |
| 1999 | // Send the search result over to the browser process. |
| 2000 | Send(new ViewHostMsg_Find_Reply(routing_id_, request.request_id, |
| 2001 | match_count, |
| 2002 | selection_rect, |
| 2003 | -1, // Don't update active match ordinal. |
| 2004 | final_status_update)); |
| 2005 | |
| 2006 | if (!request.find_next) { |
| 2007 | // Scoping effort begins, starting with the mainframe. |
| 2008 | search_frame = main_frame; |
| 2009 | |
| 2010 | main_frame->ResetMatchCount(); |
| 2011 | |
| 2012 | do { |
| 2013 | // Cancel all old scoping requests before starting a new one. |
| 2014 | search_frame->CancelPendingScopingEffort(); |
| 2015 | |
| 2016 | // We don't start another scoping effort unless at least one match has |
| 2017 | // been found. |
| 2018 | if (result) { |
| 2019 | // Start new scoping request. If the scoping function determines that it |
| 2020 | // needs to scope, it will defer until later. |
| 2021 | search_frame->ScopeStringMatches(request, |
| 2022 | true); // reset the tickmarks |
| 2023 | } |
| 2024 | |
| 2025 | // Iterate to the next frame. The frame will not necessarily scope, for |
| 2026 | // example if it is not visible. |
| 2027 | search_frame = webview()->GetNextFrameAfter(search_frame, true); |
| 2028 | } while (search_frame != main_frame); |
| 2029 | } |
| 2030 | } |
| 2031 | |
| 2032 | void RenderView::ReportFindInPageMatchCount(int count, int request_id, |
| 2033 | bool final_update) { |
| 2034 | // If we have a message that has been queued up, then we should just replace |
| 2035 | // it. The ACK from the browser will make sure it gets sent when the browser |
| 2036 | // wants it. |
| 2037 | if (queued_find_reply_message_.get()) { |
| 2038 | IPC::Message* msg = new ViewHostMsg_Find_Reply( |
| 2039 | routing_id_, |
| 2040 | request_id, |
| 2041 | count, |
| 2042 | gfx::Rect(0, 0, 0, 0), |
| 2043 | -1, // Don't update active match ordinal. |
| 2044 | final_update); |
| 2045 | queued_find_reply_message_.reset(msg); |
| 2046 | } else { |
| 2047 | // Send the search result over to the browser process. |
| 2048 | Send(new ViewHostMsg_Find_Reply( |
| 2049 | routing_id_, |
| 2050 | request_id, |
| 2051 | count, |
| 2052 | gfx::Rect(0, 0, 0, 0), |
| 2053 | -1, // // Don't update active match ordinal. |
| 2054 | final_update)); |
| 2055 | } |
| 2056 | } |
| 2057 | |
| 2058 | void RenderView::ReportFindInPageSelection(int request_id, |
| 2059 | int active_match_ordinal, |
| 2060 | const gfx::Rect& selection_rect) { |
| 2061 | // Send the search result over to the browser process. |
| 2062 | Send(new ViewHostMsg_Find_Reply(routing_id_, |
| 2063 | request_id, |
| 2064 | -1, |
| 2065 | selection_rect, |
| 2066 | active_match_ordinal, |
| 2067 | false)); |
| 2068 | } |
| 2069 | |
| 2070 | bool RenderView::WasOpenedByUserGesture(WebView* webview) const { |
| 2071 | return WasOpenedByUserGestureHelper(); |
| 2072 | } |
| 2073 | |
| 2074 | bool RenderView::WasOpenedByUserGestureHelper() const { |
| 2075 | // If pop-up blocking has been disabled, then treat all new windows as if |
| 2076 | // they were opened by a user gesture. This will prevent them from being |
| 2077 | // blocked. This is a bit of a hack, there should be a more straightforward |
| 2078 | // way to disable pop-up blocking. |
| 2079 | if (disable_popup_blocking_) |
| 2080 | return true; |
| 2081 | |
| 2082 | return opened_by_user_gesture_; |
| 2083 | } |
| 2084 | |
| 2085 | void RenderView::SpellCheck(const std::wstring& word, int& misspell_location, |
| 2086 | int& misspell_length) { |
| 2087 | Send(new ViewHostMsg_SpellCheck(routing_id_, word, &misspell_location, |
| 2088 | &misspell_length)); |
| 2089 | } |
| 2090 | |
| 2091 | void RenderView::SetInputMethodState(bool enabled) { |
| 2092 | // Save the updated IME status and mark the input focus has been updated. |
| 2093 | // The IME status is to be sent to a browser process next time when |
| 2094 | // the input caret is rendered. |
| 2095 | ime_control_updated_ = true; |
| 2096 | ime_control_new_state_ = enabled; |
| 2097 | } |
| 2098 | |
| 2099 | void RenderView::ScriptedPrint(WebFrame* frame) { |
| 2100 | // Retrieve the default print settings to calculate the expected number of |
| 2101 | // pages. |
| 2102 | ViewMsg_Print_Params default_settings; |
| 2103 | IPC::SyncMessage* msg = |
| 2104 | new ViewHostMsg_GetDefaultPrintSettings(routing_id_, &default_settings); |
| 2105 | if (Send(msg)) { |
| 2106 | msg = NULL; |
| 2107 | // Continue only if the settings are valid. |
| 2108 | if (default_settings.dpi && default_settings.document_cookie) { |
| 2109 | int expected_pages_count = SwitchFrameToPrintMediaType(default_settings, |
| 2110 | frame); |
| 2111 | DCHECK(expected_pages_count); |
| 2112 | SwitchFrameToDisplayMediaType(frame); |
| 2113 | |
| 2114 | // Ask the browser to show UI to retrieve the final print settings. |
| 2115 | ViewMsg_PrintPages_Params print_settings; |
| 2116 | // host_window_ may be NULL at this point if the current window is a popup |
| 2117 | // and the print() command has been issued from the parent. The receiver |
| 2118 | // of this message has to deal with this. |
| 2119 | msg = new ViewHostMsg_ScriptedPrint(routing_id_, |
| 2120 | host_window_, |
| 2121 | default_settings.document_cookie, |
| 2122 | expected_pages_count, |
| 2123 | &print_settings); |
| 2124 | if (Send(msg)) { |
| 2125 | msg = NULL; |
| 2126 | |
| 2127 | // If the settings are invalid, early quit. |
| 2128 | if (print_settings.params.dpi && |
| 2129 | print_settings.params.document_cookie) { |
| 2130 | // Render the printed pages. It will implicitly revert the document to |
| 2131 | // display CSS media type. |
| 2132 | PrintPages(print_settings, frame); |
| 2133 | // All went well. |
| 2134 | return; |
| 2135 | } else { |
| 2136 | // The user cancelled. |
| 2137 | } |
| 2138 | } else { |
| 2139 | // Send() failed. |
| 2140 | NOTREACHED(); |
| 2141 | } |
| 2142 | } else { |
| 2143 | // The user cancelled. |
| 2144 | } |
| 2145 | } else { |
| 2146 | // Send() failed. |
| 2147 | NOTREACHED(); |
| 2148 | } |
| 2149 | // TODO(maruel): bug 1123882 Alert the user that printing failed. |
| 2150 | } |
| 2151 | |
| 2152 | void RenderView::WebInspectorOpened(int num_resources) { |
| 2153 | Send(new ViewHostMsg_InspectElement_Reply(routing_id_, num_resources)); |
| 2154 | } |
| 2155 | |
| 2156 | void RenderView::UserMetricsRecordAction(const std::wstring& action) { |
| 2157 | Send(new ViewHostMsg_UserMetricsRecordAction(routing_id_, action)); |
| 2158 | } |
| 2159 | |
| 2160 | void RenderView::DnsPrefetch(const std::vector<std::string>& host_names) { |
| 2161 | Send(new ViewHostMsg_DnsPrefetch(host_names)); |
| 2162 | } |
| 2163 | |
| 2164 | void RenderView::OnAlterTextSize(int size) { |
| 2165 | switch (size) { |
| 2166 | case text_zoom::TEXT_SMALLER: |
| 2167 | webview()->MakeTextSmaller(); |
| 2168 | break; |
| 2169 | case text_zoom::TEXT_STANDARD: |
| 2170 | webview()->MakeTextStandardSize(); |
| 2171 | break; |
| 2172 | case text_zoom::TEXT_LARGER: |
| 2173 | webview()->MakeTextLarger(); |
| 2174 | break; |
| 2175 | default: |
| 2176 | NOTREACHED(); |
| 2177 | } |
| 2178 | } |
| 2179 | |
| 2180 | void RenderView::OnSetPageEncoding(const std::wstring& encoding_name) { |
| 2181 | webview()->SetPageEncoding(encoding_name); |
| 2182 | } |
| 2183 | |
| 2184 | void RenderView::OnPasswordFormsSeen(WebView* webview, |
| 2185 | const std::vector<PasswordForm>& forms) { |
| 2186 | Send(new ViewHostMsg_PasswordFormsSeen(routing_id_, forms)); |
| 2187 | } |
| 2188 | |
| 2189 | WebHistoryItem* RenderView::GetHistoryEntryAtOffset(int offset) { |
| 2190 | // This doesn't work in the multi-process case because we don't want to |
| 2191 | // hang, as it might lead to deadlocks. Use GoToEntryAtOffsetAsync. |
| 2192 | return NULL; |
| 2193 | } |
| 2194 | |
| 2195 | void RenderView::GoToEntryAtOffsetAsync(int offset) { |
| 2196 | Send(new ViewHostMsg_GoToEntryAtOffset(routing_id_, offset)); |
| 2197 | } |
| 2198 | |
| 2199 | int RenderView::GetHistoryBackListCount() { |
| 2200 | return history_back_list_count_; |
| 2201 | } |
| 2202 | |
| 2203 | int RenderView::GetHistoryForwardListCount() { |
| 2204 | return history_forward_list_count_; |
| 2205 | } |
| 2206 | |
| 2207 | void RenderView::OnNavStateChanged(WebView* webview) { |
| 2208 | nav_state_sync_timer_.Start(); |
| 2209 | } |
| 2210 | |
| 2211 | void RenderView::SetTooltipText(WebView* webview, |
| 2212 | const std::wstring& tooltip_text) { |
| 2213 | Send(new ViewHostMsg_SetTooltipText(routing_id_, tooltip_text)); |
| 2214 | } |
| 2215 | |
| 2216 | void RenderView::DownloadUrl(const GURL& url, const GURL& referrer) { |
| 2217 | Send(new ViewHostMsg_DownloadUrl(routing_id_, url, referrer)); |
| 2218 | } |
| 2219 | |
| 2220 | WebFrame* RenderView::GetChildFrame(const std::wstring& frame_xpath) const { |
| 2221 | WebFrame* web_frame; |
| 2222 | if (frame_xpath.empty()) { |
| 2223 | web_frame = webview()->GetMainFrame(); |
| 2224 | } else { |
| 2225 | web_frame = webview()->GetMainFrame()->GetChildFrame(frame_xpath); |
| 2226 | } |
| 2227 | |
| 2228 | return web_frame; |
| 2229 | } |
| 2230 | |
| 2231 | void RenderView::EvaluateScriptUrl(const std::wstring& frame_xpath, |
| 2232 | const std::wstring& js_url) { |
| 2233 | WebFrame* web_frame = GetChildFrame(frame_xpath); |
| 2234 | if (!web_frame) |
| 2235 | return; |
| 2236 | |
| 2237 | scoped_ptr<WebRequest> request(WebRequest::Create(GURL(js_url))); |
| 2238 | web_frame->LoadRequest(request.get()); |
| 2239 | } |
| 2240 | |
| 2241 | void RenderView::OnScriptEvalRequest(const std::wstring& frame_xpath, |
| 2242 | const std::wstring& jscript) { |
| 2243 | EvaluateScriptUrl(frame_xpath, jscript); |
| 2244 | } |
| 2245 | |
| 2246 | void RenderView::OnAddMessageToConsole(const std::wstring& frame_xpath, |
| 2247 | const std::wstring& msg, |
| 2248 | ConsoleMessageLevel level) { |
| 2249 | WebFrame* web_frame = GetChildFrame(frame_xpath); |
| 2250 | if (!web_frame) |
| 2251 | return; |
| 2252 | |
| 2253 | web_frame->AddMessageToConsole(msg, level); |
| 2254 | } |
| 2255 | |
| 2256 | void RenderView::OnDebugAttach() { |
| 2257 | EvaluateScriptUrl(L"", L"javascript:void(0)"); |
| 2258 | Send(new ViewHostMsg_DidDebugAttach(routing_id_)); |
| 2259 | // Tell the plugin host to stop accepting messages in order to avoid |
| 2260 | // hangs while the renderer is paused. |
| 2261 | // TODO(1243929): It might be an improvement to add more plumbing to do this |
| 2262 | // when the renderer is actually paused vs. just the debugger being attached. |
| 2263 | PluginChannelHost::SetListening(false); |
| 2264 | } |
| 2265 | |
| 2266 | void RenderView::OnDebugDetach() { |
| 2267 | // Tell the plugin host to start accepting plugin messages again. |
| 2268 | PluginChannelHost::SetListening(true); |
| 2269 | } |
| 2270 | |
| 2271 | void RenderView::OnAllowDomAutomationBindings(bool allow_bindings) { |
| 2272 | enable_dom_automation_ = allow_bindings; |
| 2273 | } |
| 2274 | |
| 2275 | void RenderView::OnAllowDOMUIBindings() { |
| 2276 | enable_dom_ui_bindings_ = true; |
| 2277 | } |
| 2278 | |
| 2279 | void RenderView::OnSetDOMUIProperty(const std::string& name, |
| 2280 | const std::string& value) { |
| 2281 | DCHECK(enable_dom_ui_bindings_); |
| 2282 | dom_ui_bindings_.SetProperty(name, value); |
| 2283 | } |
| 2284 | |
| 2285 | void RenderView::OnReservePageIDRange(int size_of_range) { |
| 2286 | next_page_id_ += size_of_range + 1; |
| 2287 | } |
| 2288 | |
| 2289 | void RenderView::OnDragSourceEndedOrMoved(int client_x, |
| 2290 | int client_y, |
| 2291 | int screen_x, |
| 2292 | int screen_y, |
| 2293 | bool ended) { |
| 2294 | if (ended) |
| 2295 | webview()->DragSourceEndedAt(client_x, client_y, screen_x, screen_y); |
| 2296 | else |
| 2297 | webview()->DragSourceMovedTo(client_x, client_y, screen_x, screen_y); |
| 2298 | } |
| 2299 | |
| 2300 | void RenderView::OnDragSourceSystemDragEnded() { |
| 2301 | webview()->DragSourceSystemDragEnded(); |
| 2302 | } |
| 2303 | |
| 2304 | void RenderView::OnUploadFileRequest(const ViewMsg_UploadFile_Params& p) { |
| 2305 | webkit_glue::FileUploadData* f = new webkit_glue::FileUploadData; |
| 2306 | f->file_path = p.file_path; |
| 2307 | f->form_name = p.form; |
| 2308 | f->file_name = p.file; |
| 2309 | f->submit_name = p.submit; |
| 2310 | |
| 2311 | // Build the other form values map. |
| 2312 | if (!p.other_values.empty()) { |
| 2313 | std::vector<std::wstring> e; |
| 2314 | std::vector<std::wstring> kvp; |
| 2315 | std::vector<std::wstring>::iterator i; |
| 2316 | |
| 2317 | SplitString(p.other_values, L'\n', &e); |
| 2318 | for (i = e.begin(); i != e.end(); ++i) { |
| 2319 | SplitString(*i, L'=', &kvp); |
| 2320 | if (kvp.size() == 2) |
| 2321 | f->other_form_values[kvp[0]] = kvp[1]; |
| 2322 | kvp.clear(); |
| 2323 | } |
| 2324 | } |
| 2325 | |
| 2326 | pending_upload_data_.reset(f); |
| 2327 | ProcessPendingUpload(); |
| 2328 | } |
| 2329 | |
| 2330 | void RenderView::ProcessPendingUpload() { |
| 2331 | webkit_glue::FileUploadData* f = pending_upload_data_.get(); |
| 2332 | if (f && webview() && webkit_glue::FillFormToUploadFile(webview(), *f)) |
| 2333 | ResetPendingUpload(); |
| 2334 | } |
| 2335 | |
| 2336 | void RenderView::ResetPendingUpload() { |
| 2337 | pending_upload_data_.reset(); |
| 2338 | } |
| 2339 | |
| 2340 | void RenderView::OnFormFill(const FormData& form) { |
| 2341 | webkit_glue::FillForm(this->webview(), form); |
| 2342 | } |
| 2343 | |
| 2344 | void RenderView::OnFillPasswordForm( |
| 2345 | const PasswordFormDomManager::FillData& form_data) { |
| 2346 | webkit_glue::FillPasswordForm(this->webview(), form_data); |
| 2347 | } |
| 2348 | |
| 2349 | void RenderView::OnDragTargetDragEnter(const WebDropData& drop_data, |
| 2350 | const gfx::Point& client_pt, const gfx::Point& screen_pt) { |
| 2351 | bool is_drop_target = webview()->DragTargetDragEnter(drop_data, |
| 2352 | client_pt.x(), client_pt.y(), screen_pt.x(), screen_pt.y()); |
| 2353 | |
| 2354 | Send(new ViewHostMsg_UpdateDragCursor(routing_id_, is_drop_target)); |
| 2355 | } |
| 2356 | |
| 2357 | void RenderView::OnDragTargetDragOver(const gfx::Point& client_pt, |
| 2358 | const gfx::Point& screen_pt) { |
| 2359 | bool is_drop_target = webview()->DragTargetDragOver(client_pt.x(), |
| 2360 | client_pt.y(), screen_pt.x(), screen_pt.y()); |
| 2361 | |
| 2362 | Send(new ViewHostMsg_UpdateDragCursor(routing_id_, is_drop_target)); |
| 2363 | } |
| 2364 | |
| 2365 | void RenderView::OnDragTargetDragLeave() { |
| 2366 | webview()->DragTargetDragLeave(); |
| 2367 | } |
| 2368 | |
| 2369 | void RenderView::OnDragTargetDrop(const gfx::Point& client_pt, |
| 2370 | const gfx::Point& screen_pt) { |
| 2371 | webview()->DragTargetDrop(client_pt.x(), client_pt.y(), screen_pt.x(), |
| 2372 | screen_pt.y()); |
| 2373 | } |
| 2374 | |
| 2375 | void RenderView::OnUpdateWebPreferences(const WebPreferences& prefs) { |
| 2376 | webview()->SetPreferences(prefs); |
| 2377 | } |
| 2378 | |
| 2379 | void RenderView::OnSetAltErrorPageURL(const GURL& url) { |
| 2380 | alternate_error_page_url_ = url; |
| 2381 | } |
| 2382 | |
| 2383 | void RenderView::DidPaint() { |
| 2384 | PluginDelegateList::iterator it = plugin_delegates_.begin(); |
| 2385 | while (it != plugin_delegates_.end()) { |
| 2386 | (*it)->FlushGeometryUpdates(); |
| 2387 | ++it; |
| 2388 | } |
| 2389 | } |
| 2390 | |
| 2391 | void RenderView::OnInstallMissingPlugin() { |
| 2392 | // This could happen when the first default plugin is deleted. |
| 2393 | if (first_default_plugin_ == NULL) |
| 2394 | return; |
| 2395 | first_default_plugin_->InstallMissingPlugin(); |
| 2396 | } |
| 2397 | |
| 2398 | void RenderView::OnFileChooserResponse(const std::wstring& file_name) { |
| 2399 | file_chooser_->OnFileChoose(file_name); |
| 2400 | file_chooser_.reset(); |
| 2401 | } |
| 2402 | |
| 2403 | void RenderView::OnEnableViewSourceMode() { |
| 2404 | if (!webview()) |
| 2405 | return; |
| 2406 | WebFrame* main_frame = webview()->GetMainFrame(); |
| 2407 | if (!main_frame) |
| 2408 | return; |
| 2409 | |
| 2410 | main_frame->SetInViewSourceMode(true); |
| 2411 | } |
| 2412 | |
| 2413 | void RenderView::OnUpdateBackForwardListCount(int back_list_count, |
| 2414 | int forward_list_count) { |
| 2415 | history_back_list_count_ = back_list_count; |
| 2416 | history_forward_list_count_ = forward_list_count; |
| 2417 | } |
| 2418 | |
| 2419 | void RenderView::OnGetAllSavableResourceLinksForCurrentPage( |
| 2420 | const GURL& page_url) { |
| 2421 | // Prepare list to storage all savable resource links. |
| 2422 | std::vector<GURL> resources_list; |
| 2423 | std::vector<GURL> referrers_list; |
| 2424 | std::vector<GURL> frames_list; |
| 2425 | webkit_glue::SavableResourcesResult result(&resources_list, |
| 2426 | &referrers_list, |
| 2427 | &frames_list); |
| 2428 | |
| 2429 | if (!webkit_glue::GetAllSavableResourceLinksForCurrentPage(webview(), |
| 2430 | page_url, |
| 2431 | &result)) { |
| 2432 | // If something is wrong when collecting all savable resource links, |
| 2433 | // send empty list to embedder(browser) to tell it failed. |
| 2434 | referrers_list.clear(); |
| 2435 | resources_list.clear(); |
| 2436 | frames_list.clear(); |
| 2437 | } |
| 2438 | |
| 2439 | // Send result of all savable resource links to embedder. |
| 2440 | Send(new ViewHostMsg_SendCurrentPageAllSavableResourceLinks(routing_id_, |
| 2441 | resources_list, |
| 2442 | referrers_list, |
| 2443 | frames_list)); |
| 2444 | } |
| 2445 | |
| 2446 | void RenderView::OnGetSerializedHtmlDataForCurrentPageWithLocalLinks( |
| 2447 | const std::vector<std::wstring>& links, |
| 2448 | const std::vector<std::wstring>& local_paths, |
| 2449 | const std::wstring& local_directory_name) { |
| 2450 | webkit_glue::DomSerializer dom_serializer(webview()->GetMainFrame(), |
| 2451 | true, |
| 2452 | this, |
| 2453 | links, |
| 2454 | local_paths, |
| 2455 | local_directory_name); |
| 2456 | dom_serializer.SerializeDom(); |
| 2457 | } |
| 2458 | |
| 2459 | void RenderView::DidSerializeDataForFrame(const GURL& frame_url, |
| 2460 | const std::string& data, PageSavingSerializationStatus status) { |
| 2461 | Send(new ViewHostMsg_SendSerializedHtmlData(routing_id_, |
| 2462 | frame_url, data, static_cast<int32>(status))); |
| 2463 | } |
| 2464 | |
[email protected] | 04b4a6c | 2008-08-02 00:44:47 | [diff] [blame] | 2465 | void RenderView::OnMsgShouldClose() { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 2466 | bool should_close = webview()->ShouldClose(); |
[email protected] | 04b4a6c | 2008-08-02 00:44:47 | [diff] [blame] | 2467 | Send(new ViewHostMsg_ShouldClose_ACK(routing_id_, should_close)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 2468 | } |
| 2469 | |
| 2470 | void RenderView::OnClosePage(int new_render_process_host_id, |
[email protected] | 04b4a6c | 2008-08-02 00:44:47 | [diff] [blame] | 2471 | int new_request_id) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 2472 | // TODO(creis): We'd rather use webview()->Close() here, but that currently |
| 2473 | // sets the WebView's delegate_ to NULL, preventing any JavaScript dialogs |
| 2474 | // in the onunload handler from appearing. For now, we're bypassing that and |
| 2475 | // calling the FrameLoader's CloseURL method directly. This should be |
| 2476 | // revisited to avoid having two ways to close a page. Having a single way |
| 2477 | // to close that can run onunload is also useful for fixing |
| 2478 | // http://b/issue?id=753080. |
| 2479 | WebFrame* main_frame = webview()->GetMainFrame(); |
| 2480 | if (main_frame) |
| 2481 | main_frame->ClosePage(); |
| 2482 | |
| 2483 | Send(new ViewHostMsg_ClosePage_ACK(routing_id_, |
| 2484 | new_render_process_host_id, |
[email protected] | 04b4a6c | 2008-08-02 00:44:47 | [diff] [blame] | 2485 | new_request_id)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 2486 | } |
| 2487 | |
| 2488 | void RenderView::OnThemeChanged() { |
| 2489 | gfx::NativeTheme::instance()->CloseHandles(); |
| 2490 | gfx::Rect view_rect(0, 0, size_.width(), size_.height()); |
| 2491 | DidInvalidateRect(webwidget_, view_rect); |
| 2492 | } |
| 2493 | |
[email protected] | 3ac14a05 | 2008-08-15 21:22:15 | [diff] [blame^] | 2494 | void RenderView::OnPostMessage(const std::string& target, |
| 2495 | const std::string& message) { |
| 2496 | if (message.empty()) |
| 2497 | return; |
| 2498 | |
| 2499 | WebFrame* main_frame = webview()->GetMainFrame(); |
| 2500 | if (!main_frame) |
| 2501 | return; |
| 2502 | |
| 2503 | std::string script = "javascript:"; |
| 2504 | script += target; |
| 2505 | script += "("; |
| 2506 | script += "'"; |
| 2507 | script += message; |
| 2508 | script += "'"; |
| 2509 | script += ");void(0);"; |
| 2510 | |
| 2511 | GURL script_url(script); |
| 2512 | scoped_ptr<WebRequest> request(WebRequest::Create(script_url)); |
| 2513 | // TODO(iyengar) |
| 2514 | // Need a mechanism to send results back. |
| 2515 | main_frame->LoadRequest(request.get()); |
| 2516 | } |
| 2517 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 2518 | std::string RenderView::GetAltHTMLForTemplate( |
| 2519 | const DictionaryValue& error_strings, int template_resource_id) const { |
| 2520 | const StringPiece template_html( |
| 2521 | ResourceBundle::GetSharedInstance().GetRawDataResource( |
| 2522 | template_resource_id)); |
| 2523 | |
| 2524 | if (template_html.empty()) { |
| 2525 | NOTREACHED() << "unable to load template. ID: " << template_resource_id; |
| 2526 | return ""; |
| 2527 | } |
| 2528 | // "t" is the id of the templates root node. |
| 2529 | return jstemplate_builder::GetTemplateHtml( |
| 2530 | template_html, &error_strings, "t"); |
| 2531 | } |