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