blob: 7003f43ab4c43d38b61dc75ec537ee6458b8f98f [file] [log] [blame]
license.botbf09a502008-08-24 00:55:551// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
5#include "chrome/renderer/render_view.h"
6
7#include <algorithm>
8#include <string>
9#include <vector>
10
11#include "base/command_line.h"
initial.commit09911bf2008-07-26 23:55:2912#include "base/gfx/png_encoder.h"
[email protected]18bcc3c2009-01-27 21:39:1513#include "base/gfx/native_widget_types.h"
initial.commit09911bf2008-07-26 23:55:2914#include "base/string_piece.h"
15#include "base/string_util.h"
[email protected]6c8afae52009-01-22 02:24:5716#include "build/build_config.h"
initial.commit09911bf2008-07-26 23:55:2917#include "chrome/common/chrome_switches.h"
initial.commit09911bf2008-07-26 23:55:2918#include "chrome/common/gfx/favicon_size.h"
19#include "chrome/common/gfx/color_utils.h"
20#include "chrome/common/jstemplate_builder.h"
21#include "chrome/common/l10n_util.h"
[email protected]630e26b2008-10-14 22:55:1722#include "chrome/common/page_zoom.h"
[email protected]e09ba552009-02-05 03:26:2923#include "chrome/common/render_messages.h"
initial.commit09911bf2008-07-26 23:55:2924#include "chrome/common/resource_bundle.h"
initial.commit09911bf2008-07-26 23:55:2925#include "chrome/common/thumbnail_score.h"
26#include "chrome/renderer/about_handler.h"
27#include "chrome/renderer/debug_message_handler.h"
28#include "chrome/renderer/localized_error.h"
[email protected]39008c02009-02-11 23:59:2529#include "chrome/renderer/render_process.h"
initial.commit09911bf2008-07-26 23:55:2930#include "chrome/renderer/renderer_resources.h"
[email protected]0938d3c2009-01-09 20:37:3531#include "chrome/renderer/user_script_slave.h"
initial.commit09911bf2008-07-26 23:55:2932#include "chrome/renderer/visitedlink_slave.h"
[email protected]ec9212f2008-12-18 21:40:3633#include "chrome/renderer/webmediaplayer_delegate_impl.h"
[email protected]ba4b17f2009-02-11 21:32:2934#include "chrome/renderer/webplugin_delegate_proxy.h"
initial.commit09911bf2008-07-26 23:55:2935#include "net/base/escape.h"
36#include "net/base/net_errors.h"
[email protected]c399a8a2008-11-22 19:38:0037#include "skia/ext/bitmap_platform_device.h"
[email protected]83c9e6552008-12-03 16:22:1038#include "skia/ext/image_operations.h"
[email protected]ba4b17f2009-02-11 21:32:2939#include "webkit/default_plugin/default_plugin_shared.h"
initial.commit09911bf2008-07-26 23:55:2940#include "webkit/glue/dom_operations.h"
41#include "webkit/glue/dom_serializer.h"
[email protected]39008c02009-02-11 23:59:2542#include "webkit/glue/glue_accessibility.h"
initial.commit09911bf2008-07-26 23:55:2943#include "webkit/glue/password_form.h"
[email protected]ba4b17f2009-02-11 21:32:2944#include "webkit/glue/plugins/plugin_list.h"
initial.commit09911bf2008-07-26 23:55:2945#include "webkit/glue/searchable_form_data.h"
46#include "webkit/glue/webdatasource.h"
47#include "webkit/glue/webdropdata.h"
48#include "webkit/glue/weberror.h"
49#include "webkit/glue/webframe.h"
50#include "webkit/glue/webhistoryitem.h"
51#include "webkit/glue/webinputevent.h"
52#include "webkit/glue/webkit_glue.h"
53#include "webkit/glue/webpreferences.h"
[email protected]b94d3322009-02-12 19:49:0454#include "webkit/glue/webplugin_delegate.h"
initial.commit09911bf2008-07-26 23:55:2955#include "webkit/glue/webresponse.h"
56#include "webkit/glue/weburlrequest.h"
57#include "webkit/glue/webview.h"
initial.commit09911bf2008-07-26 23:55:2958
59#include "generated_resources.h"
60
[email protected]6c8afae52009-01-22 02:24:5761#if defined(OS_WIN)
62// TODO(port): these files are currently Windows only because they concern:
63// * plugins
64// * printing
65// * theming
66// * views
67#include "base/gfx/gdi_util.h"
68#include "base/gfx/native_theme.h"
69#include "chrome/common/gfx/emf.h"
70#include "chrome/views/message_box_view.h"
71#include "chrome/common/chrome_plugin_lib.h"
72#include "chrome/renderer/chrome_plugin_host.h"
[email protected]6c8afae52009-01-22 02:24:5773#include "skia/ext/vector_canvas.h"
[email protected]6c8afae52009-01-22 02:24:5774#endif
75
[email protected]e1acf6f2008-10-27 20:43:3376using base::TimeDelta;
77
initial.commit09911bf2008-07-26 23:55:2978//-----------------------------------------------------------------------------
79
80// define to write the time necessary for thumbnail/DOM text retrieval,
81// respectively, into the system debug log
82// #define TIME_BITMAP_RETRIEVAL
83// #define TIME_TEXT_RETRIEVAL
84
85// maximum number of characters in the document to index, any text beyond this
86// point will be clipped
[email protected]6c8afae52009-01-22 02:24:5787static const size_t kMaxIndexChars = 65535;
initial.commit09911bf2008-07-26 23:55:2988
89// Size of the thumbnails that we'll generate
90static const int kThumbnailWidth = 196;
91static const int kThumbnailHeight = 136;
92
93// Delay in milliseconds that we'll wait before capturing the page contents
94// and thumbnail.
95static const int kDelayForCaptureMs = 500;
96
97// Typically, we capture the page data once the page is loaded.
98// Sometimes, the page never finishes to load, preventing the page capture
99// To workaround this problem, we always perform a capture after the following
100// delay.
101static const int kDelayForForcedCaptureMs = 6000;
102
[email protected]81a34412009-01-05 19:17:24103// The default value for RenderView.delay_seconds_for_form_state_sync_, see
104// that variable for more.
105const int kDefaultDelaySecondsForFormStateSync = 5;
initial.commit09911bf2008-07-26 23:55:29106
107// The next available page ID to use. This ensures that the page IDs are
108// globally unique in the renderer.
109static int32 next_page_id_ = 1;
110
[email protected]0aa55312008-10-17 21:53:08111// The maximum number of popups that can be spawned from one page.
112static const int kMaximumNumberOfUnacknowledgedPopups = 25;
113
initial.commit09911bf2008-07-26 23:55:29114static const char* const kUnreachableWebDataURL =
[email protected]9527f9c2009-01-29 18:35:42115 "chrome-ui://chromewebdata/";
initial.commit09911bf2008-07-26 23:55:29116
[email protected]50b691c2008-10-31 19:08:35117static const char* const kBackForwardNavigationScheme = "history";
118
initial.commit09911bf2008-07-26 23:55:29119namespace {
120
121// Associated with browser-initiated navigations to hold tracking data.
122class RenderViewExtraRequestData : public WebRequest::ExtraData {
123 public:
124 RenderViewExtraRequestData(int32 pending_page_id,
125 PageTransition::Type transition,
126 const GURL& url)
[email protected]6c8afae52009-01-22 02:24:57127 : transition_type(transition),
128 request_committed(false),
129 pending_page_id_(pending_page_id) {
initial.commit09911bf2008-07-26 23:55:29130 }
131
132 // Contains the page_id for this navigation or -1 if there is none yet.
133 int32 pending_page_id() const { return pending_page_id_; }
134
135 // Is this a new navigation?
136 bool is_new_navigation() const { return pending_page_id_ == -1; }
137
138 // Contains the transition type that the browser specified when it
139 // initiated the load.
140 PageTransition::Type transition_type;
141
142 // True if we have already processed the "DidCommitLoad" event for this
143 // request. Used by session history.
144 bool request_committed;
145
146 private:
147 int32 pending_page_id_;
148
149 DISALLOW_EVIL_CONSTRUCTORS(RenderViewExtraRequestData);
150};
151
152} // namespace
153
154///////////////////////////////////////////////////////////////////////////////
155
[email protected]81a34412009-01-05 19:17:24156RenderView::RenderView(RenderThreadBase* render_thread)
157 : RenderWidget(render_thread, true),
[email protected]e75cb49e2009-01-05 23:13:21158 enable_dom_automation_(false),
159 enable_dom_ui_bindings_(false),
160 enable_external_host_bindings_(false),
161 target_url_status_(TARGET_NONE),
[email protected]81a34412009-01-05 19:17:24162 is_loading_(false),
[email protected]e75cb49e2009-01-05 23:13:21163 navigation_gesture_(NavigationGestureUnknown),
[email protected]81a34412009-01-05 19:17:24164 page_id_(-1),
165 last_page_id_sent_to_browser_(-1),
166 last_indexed_page_id_(-1),
[email protected]81a34412009-01-05 19:17:24167 opened_by_user_gesture_(true),
[email protected]e75cb49e2009-01-05 23:13:21168 method_factory_(this),
[email protected]81a34412009-01-05 19:17:24169 first_default_plugin_(NULL),
[email protected]e75cb49e2009-01-05 23:13:21170 printed_document_width_(0),
[email protected]81a34412009-01-05 19:17:24171 history_back_list_count_(0),
172 history_forward_list_count_(0),
173 disable_popup_blocking_(false),
174 has_unload_listener_(false),
175 decrement_shared_popup_at_destruction_(false),
[email protected]81a34412009-01-05 19:17:24176 waiting_for_create_window_ack_(false),
177 form_field_autofill_request_id_(0),
178 popup_notification_visible_(false),
179 delay_seconds_for_form_state_sync_(kDefaultDelaySecondsForFormStateSync) {
initial.commit09911bf2008-07-26 23:55:29180 resource_dispatcher_ = new ResourceDispatcher(this);
[email protected]3a453fa2008-08-15 18:46:34181#ifdef CHROME_PERSONALIZATION
182 personalization_ = Personalization::CreateRendererPersonalization();
183#endif
initial.commit09911bf2008-07-26 23:55:29184}
185
186RenderView::~RenderView() {
[email protected]0aa55312008-10-17 21:53:08187 if (decrement_shared_popup_at_destruction_)
188 shared_popup_counter_->data--;
189
initial.commit09911bf2008-07-26 23:55:29190 resource_dispatcher_->ClearMessageSender();
191 // Clear any back-pointers that might still be held by plugins.
192 PluginDelegateList::iterator it = plugin_delegates_.begin();
193 while (it != plugin_delegates_.end()) {
194 (*it)->DropRenderView();
195 it = plugin_delegates_.erase(it);
196 }
197
[email protected]81a34412009-01-05 19:17:24198 render_thread_->RemoveFilter(debug_message_handler_);
[email protected]3a453fa2008-08-15 18:46:34199
200#ifdef CHROME_PERSONALIZATION
201 Personalization::CleanupRendererPersonalization(personalization_);
202 personalization_ = NULL;
203#endif
initial.commit09911bf2008-07-26 23:55:29204}
205
206/*static*/
[email protected]0aa55312008-10-17 21:53:08207RenderView* RenderView::Create(
[email protected]81a34412009-01-05 19:17:24208 RenderThreadBase* render_thread,
[email protected]18bcc3c2009-01-27 21:39:15209 gfx::NativeViewId parent_hwnd,
[email protected]1c4947f2009-01-15 22:25:11210 base::WaitableEvent* modal_dialog_event,
[email protected]0aa55312008-10-17 21:53:08211 int32 opener_id,
212 const WebPreferences& webkit_prefs,
213 SharedRenderViewCounter* counter,
214 int32 routing_id) {
initial.commit09911bf2008-07-26 23:55:29215 DCHECK(routing_id != MSG_ROUTING_NONE);
[email protected]81a34412009-01-05 19:17:24216 scoped_refptr<RenderView> view = new RenderView(render_thread);
initial.commit09911bf2008-07-26 23:55:29217 view->Init(parent_hwnd,
218 modal_dialog_event,
219 opener_id,
220 webkit_prefs,
[email protected]0aa55312008-10-17 21:53:08221 counter,
initial.commit09911bf2008-07-26 23:55:29222 routing_id); // adds reference
223 return view;
224}
225
226/*static*/
227void RenderView::SetNextPageID(int32 next_page_id) {
228 // This method should only be called during process startup, and the given
229 // page id had better not exceed our current next page id!
230 DCHECK(next_page_id_ == 1);
231 DCHECK(next_page_id >= next_page_id_);
232 next_page_id_ = next_page_id;
233}
234
235void RenderView::PluginDestroyed(WebPluginDelegateProxy* proxy) {
236 PluginDelegateList::iterator it =
237 std::find(plugin_delegates_.begin(), plugin_delegates_.end(), proxy);
238 DCHECK(it != plugin_delegates_.end());
239 plugin_delegates_.erase(it);
240 // If the plugin is deleted, we need to clear our reference in case user
241 // clicks the info bar to install. Unfortunately we are getting
242 // PluginDestroyed in single process mode. However, that is not a huge
243 // concern.
[email protected]6c8afae52009-01-22 02:24:57244#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:29245 if (proxy == first_default_plugin_)
246 first_default_plugin_ = NULL;
[email protected]6c8afae52009-01-22 02:24:57247#else
248 // TODO(port): because of the headers that we aren't including, the compiler
249 // has only seen a forward decl, not the subclass relation. Thus it doesn't
250 // know that the two pointer types compared above are comparable. Once we
251 // port and include the headers this problem should go away.
252 NOTIMPLEMENTED();
253#endif
initial.commit09911bf2008-07-26 23:55:29254}
255
[email protected]690a99c2009-01-06 16:48:45256void RenderView::PluginCrashed(const FilePath& plugin_path) {
initial.commit09911bf2008-07-26 23:55:29257 Send(new ViewHostMsg_CrashedPlugin(routing_id_, plugin_path));
258}
259
260
261void RenderView::JSOutOfMemory() {
262 Send(new ViewHostMsg_JSOutOfMemory(routing_id_));
263}
264
[email protected]18bcc3c2009-01-27 21:39:15265void RenderView::Init(gfx::NativeViewId parent_hwnd,
[email protected]1c4947f2009-01-15 22:25:11266 base::WaitableEvent* modal_dialog_event,
initial.commit09911bf2008-07-26 23:55:29267 int32 opener_id,
268 const WebPreferences& webkit_prefs,
[email protected]0aa55312008-10-17 21:53:08269 SharedRenderViewCounter* counter,
initial.commit09911bf2008-07-26 23:55:29270 int32 routing_id) {
271 DCHECK(!webview());
272
273 if (opener_id != MSG_ROUTING_NONE)
274 opener_id_ = opener_id;
275
[email protected]0aa55312008-10-17 21:53:08276 if (counter) {
277 shared_popup_counter_ = counter;
278 shared_popup_counter_->data++;
279 decrement_shared_popup_at_destruction_ = true;
280 } else {
281 shared_popup_counter_ = new SharedRenderViewCounter(0);
282 decrement_shared_popup_at_destruction_ = false;
283 }
284
initial.commit09911bf2008-07-26 23:55:29285 // Avoid a leak here by not assigning, since WebView::Create addrefs for us.
286 WebWidget* view = WebView::Create(this, webkit_prefs);
287 webwidget_.swap(&view);
288
289 // Don't let WebCore keep a B/F list - we have our own.
290 // We let it keep 1 entry because FrameLoader::goToItem expects an item in the
291 // backForwardList, which is used only in ASSERTs.
292 webview()->SetBackForwardListSize(1);
293
294 routing_id_ = routing_id;
[email protected]81a34412009-01-05 19:17:24295 render_thread_->AddRoute(routing_id_, this);
initial.commit09911bf2008-07-26 23:55:29296 // Take a reference on behalf of the RenderThread. This will be balanced
297 // when we receive ViewMsg_Close.
298 AddRef();
299
300 // If this is a popup, we must wait for the CreatingNew_ACK message before
301 // completing initialization. Otherwise, we can finish it now.
302 if (opener_id == MSG_ROUTING_NONE) {
303 did_show_ = true;
304 CompleteInit(parent_hwnd);
305 }
306
307 host_window_ = parent_hwnd;
[email protected]1c4947f2009-01-15 22:25:11308 modal_dialog_event_.reset(modal_dialog_event);
initial.commit09911bf2008-07-26 23:55:29309
[email protected]bb975362009-01-21 01:00:22310 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
initial.commit09911bf2008-07-26 23:55:29311 enable_dom_automation_ =
312 command_line.HasSwitch(switches::kDomAutomationController);
313 disable_popup_blocking_ =
314 command_line.HasSwitch(switches::kDisablePopupBlocking);
315
316 debug_message_handler_ = new DebugMessageHandler(this);
[email protected]81a34412009-01-05 19:17:24317 render_thread_->AddFilter(debug_message_handler_);
initial.commit09911bf2008-07-26 23:55:29318}
319
320void RenderView::OnMessageReceived(const IPC::Message& message) {
[email protected]06828b92008-10-20 21:25:46321 // If the current RenderView instance represents a popup, then we
322 // need to wait for ViewMsg_CreatingNew_ACK to be sent by the browser.
323 // As part of this ack we also receive the browser window handle, which
324 // parents any plugins instantiated in this RenderView instance.
325 // Plugins can be instantiated only when we receive the parent window
326 // handle as they are child windows.
[email protected]be645db2009-02-06 20:36:33327 if (waiting_for_create_window_ack_ &&
[email protected]06828b92008-10-20 21:25:46328 resource_dispatcher_->IsResourceMessage(message)) {
329 queued_resource_messages_.push(new IPC::Message(message));
330 return;
331 }
332
initial.commit09911bf2008-07-26 23:55:29333 // Let the resource dispatcher intercept resource messages first.
334 if (resource_dispatcher_->OnMessageReceived(message))
335 return;
[email protected]06828b92008-10-20 21:25:46336
initial.commit09911bf2008-07-26 23:55:29337 IPC_BEGIN_MESSAGE_MAP(RenderView, message)
338 IPC_MESSAGE_HANDLER(ViewMsg_CreatingNew_ACK, OnCreatingNewAck)
339 IPC_MESSAGE_HANDLER(ViewMsg_CaptureThumbnail, SendThumbnail)
initial.commit09911bf2008-07-26 23:55:29340 IPC_MESSAGE_HANDLER(ViewMsg_PrintPages, OnPrintPages)
341 IPC_MESSAGE_HANDLER(ViewMsg_Navigate, OnNavigate)
342 IPC_MESSAGE_HANDLER(ViewMsg_Stop, OnStop)
343 IPC_MESSAGE_HANDLER(ViewMsg_LoadAlternateHTMLText, OnLoadAlternateHTMLText)
344 IPC_MESSAGE_HANDLER(ViewMsg_StopFinding, OnStopFinding)
345 IPC_MESSAGE_HANDLER(ViewMsg_Undo, OnUndo)
346 IPC_MESSAGE_HANDLER(ViewMsg_Redo, OnRedo)
347 IPC_MESSAGE_HANDLER(ViewMsg_Cut, OnCut)
348 IPC_MESSAGE_HANDLER(ViewMsg_Copy, OnCopy)
349 IPC_MESSAGE_HANDLER(ViewMsg_Paste, OnPaste)
350 IPC_MESSAGE_HANDLER(ViewMsg_Replace, OnReplace)
[email protected]bbbd545c2008-12-15 20:18:04351 IPC_MESSAGE_HANDLER(ViewMsg_ToggleSpellCheck, OnToggleSpellCheck)
initial.commit09911bf2008-07-26 23:55:29352 IPC_MESSAGE_HANDLER(ViewMsg_Delete, OnDelete)
353 IPC_MESSAGE_HANDLER(ViewMsg_SelectAll, OnSelectAll)
354 IPC_MESSAGE_HANDLER(ViewMsg_CopyImageAt, OnCopyImageAt)
355 IPC_MESSAGE_HANDLER(ViewMsg_Find, OnFind)
[email protected]630e26b2008-10-14 22:55:17356 IPC_MESSAGE_HANDLER(ViewMsg_Zoom, OnZoom)
initial.commit09911bf2008-07-26 23:55:29357 IPC_MESSAGE_HANDLER(ViewMsg_SetPageEncoding, OnSetPageEncoding)
358 IPC_MESSAGE_HANDLER(ViewMsg_InspectElement, OnInspectElement)
359 IPC_MESSAGE_HANDLER(ViewMsg_ShowJavaScriptConsole, OnShowJavaScriptConsole)
360 IPC_MESSAGE_HANDLER(ViewMsg_DownloadImage, OnDownloadImage)
361 IPC_MESSAGE_HANDLER(ViewMsg_ScriptEvalRequest, OnScriptEvalRequest)
362 IPC_MESSAGE_HANDLER(ViewMsg_AddMessageToConsole, OnAddMessageToConsole)
363 IPC_MESSAGE_HANDLER(ViewMsg_DebugAttach, OnDebugAttach)
[email protected]88010e082008-08-29 11:07:40364 IPC_MESSAGE_HANDLER(ViewMsg_DebugDetach, OnDebugDetach)
initial.commit09911bf2008-07-26 23:55:29365 IPC_MESSAGE_HANDLER(ViewMsg_ReservePageIDRange, OnReservePageIDRange)
366 IPC_MESSAGE_HANDLER(ViewMsg_UploadFile, OnUploadFileRequest)
367 IPC_MESSAGE_HANDLER(ViewMsg_FormFill, OnFormFill)
368 IPC_MESSAGE_HANDLER(ViewMsg_FillPasswordForm, OnFillPasswordForm)
369 IPC_MESSAGE_HANDLER(ViewMsg_DragTargetDragEnter, OnDragTargetDragEnter)
370 IPC_MESSAGE_HANDLER(ViewMsg_DragTargetDragOver, OnDragTargetDragOver)
371 IPC_MESSAGE_HANDLER(ViewMsg_DragTargetDragLeave, OnDragTargetDragLeave)
372 IPC_MESSAGE_HANDLER(ViewMsg_DragTargetDrop, OnDragTargetDrop)
373 IPC_MESSAGE_HANDLER(ViewMsg_AllowDomAutomationBindings,
374 OnAllowDomAutomationBindings)
[email protected]18cb2572008-08-21 20:34:45375 IPC_MESSAGE_HANDLER(ViewMsg_AllowBindings, OnAllowBindings)
initial.commit09911bf2008-07-26 23:55:29376 IPC_MESSAGE_HANDLER(ViewMsg_SetDOMUIProperty, OnSetDOMUIProperty)
[email protected]266eb6f2008-09-30 23:56:50377 IPC_MESSAGE_HANDLER(ViewMsg_DragSourceEndedOrMoved,
378 OnDragSourceEndedOrMoved)
initial.commit09911bf2008-07-26 23:55:29379 IPC_MESSAGE_HANDLER(ViewMsg_DragSourceSystemDragEnded,
380 OnDragSourceSystemDragEnded)
381 IPC_MESSAGE_HANDLER(ViewMsg_SetInitialFocus, OnSetInitialFocus)
382 IPC_MESSAGE_HANDLER(ViewMsg_FindReplyACK, OnFindReplyAck)
383 IPC_MESSAGE_HANDLER(ViewMsg_UpdateTargetURL_ACK, OnUpdateTargetURLAck)
384 IPC_MESSAGE_HANDLER(ViewMsg_UpdateWebPreferences, OnUpdateWebPreferences)
385 IPC_MESSAGE_HANDLER(ViewMsg_SetAltErrorPageURL, OnSetAltErrorPageURL)
386 IPC_MESSAGE_HANDLER(ViewMsg_InstallMissingPlugin, OnInstallMissingPlugin)
387 IPC_MESSAGE_HANDLER(ViewMsg_RunFileChooserResponse, OnFileChooserResponse)
388 IPC_MESSAGE_HANDLER(ViewMsg_EnableViewSourceMode, OnEnableViewSourceMode)
389 IPC_MESSAGE_HANDLER(ViewMsg_UpdateBackForwardListCount,
390 OnUpdateBackForwardListCount)
391 IPC_MESSAGE_HANDLER(ViewMsg_GetAllSavableResourceLinksForCurrentPage,
392 OnGetAllSavableResourceLinksForCurrentPage)
393 IPC_MESSAGE_HANDLER(ViewMsg_GetSerializedHtmlDataForCurrentPageWithLocalLinks,
394 OnGetSerializedHtmlDataForCurrentPageWithLocalLinks)
395 IPC_MESSAGE_HANDLER(ViewMsg_GetApplicationInfo, OnGetApplicationInfo)
[email protected]266eb6f2008-09-30 23:56:50396 IPC_MESSAGE_HANDLER(ViewMsg_GetAccessibilityInfo, OnGetAccessibilityInfo)
397 IPC_MESSAGE_HANDLER(ViewMsg_ClearAccessibilityInfo,
398 OnClearAccessibilityInfo)
initial.commit09911bf2008-07-26 23:55:29399 IPC_MESSAGE_HANDLER(ViewMsg_ShouldClose, OnMsgShouldClose)
400 IPC_MESSAGE_HANDLER(ViewMsg_ClosePage, OnClosePage)
401 IPC_MESSAGE_HANDLER(ViewMsg_ThemeChanged, OnThemeChanged)
[email protected]3c17b9c2008-08-26 02:08:00402#ifdef CHROME_PERSONALIZATION
[email protected]1cc879642008-08-26 01:27:35403 IPC_MESSAGE_HANDLER(ViewMsg_PersonalizationEvent, OnPersonalizationEvent)
[email protected]3c17b9c2008-08-26 02:08:00404#endif
[email protected]18cb2572008-08-21 20:34:45405 IPC_MESSAGE_HANDLER(ViewMsg_HandleMessageFromExternalHost,
406 OnMessageFromExternalHost)
[email protected]0aa55312008-10-17 21:53:08407 IPC_MESSAGE_HANDLER(ViewMsg_DisassociateFromPopupCount,
408 OnDisassociateFromPopupCount)
[email protected]0ebf3872008-11-07 21:35:03409 IPC_MESSAGE_HANDLER(ViewMsg_AutofillSuggestions,
410 OnReceivedAutofillSuggestions)
[email protected]634a6f92008-12-01 21:39:31411 IPC_MESSAGE_HANDLER(ViewMsg_PopupNotificationVisiblityChanged,
412 OnPopupNotificationVisiblityChanged)
413
initial.commit09911bf2008-07-26 23:55:29414 // Have the super handle all other messages.
415 IPC_MESSAGE_UNHANDLED(RenderWidget::OnMessageReceived(message))
416 IPC_END_MESSAGE_MAP()
417}
418
419// Got a response from the browser after the renderer decided to create a new
420// view.
[email protected]18bcc3c2009-01-27 21:39:15421void RenderView::OnCreatingNewAck(gfx::NativeViewId parent) {
initial.commit09911bf2008-07-26 23:55:29422 CompleteInit(parent);
[email protected]06828b92008-10-20 21:25:46423 waiting_for_create_window_ack_ = false;
424
425 while (!queued_resource_messages_.empty()) {
426 IPC::Message* queued_msg = queued_resource_messages_.front();
427 queued_resource_messages_.pop();
428 resource_dispatcher_->OnMessageReceived(*queued_msg);
429 delete queued_msg;
430 }
initial.commit09911bf2008-07-26 23:55:29431}
432
433void RenderView::SendThumbnail() {
434 WebFrame* main_frame = webview()->GetMainFrame();
435 if (!main_frame)
436 return;
437
438 // get the URL for this page
439 GURL url(main_frame->GetURL());
440 if (url.is_empty())
441 return;
442
443 if (size_.IsEmpty())
444 return; // Don't create an empty thumbnail!
445
446 ThumbnailScore score;
447 SkBitmap thumbnail;
[email protected]b6e4bec2008-11-12 01:17:15448 if (!CaptureThumbnail(main_frame, kThumbnailWidth, kThumbnailHeight,
449 &thumbnail, &score))
450 return;
451
initial.commit09911bf2008-07-26 23:55:29452 // send the thumbnail message to the browser process
[email protected]674741932009-02-04 23:44:46453 Send(new ViewHostMsg_Thumbnail(routing_id_, url, score, thumbnail));
initial.commit09911bf2008-07-26 23:55:29454}
455
456int RenderView::SwitchFrameToPrintMediaType(const ViewMsg_Print_Params& params,
457 WebFrame* frame) {
458 float ratio = static_cast<float>(params.desired_dpi / params.dpi);
459 float paper_width = params.printable_size.width() * ratio;
460 float paper_height = params.printable_size.height() * ratio;
461 float minLayoutWidth = static_cast<float>(paper_width * params.min_shrink);
462 float maxLayoutWidth = static_cast<float>(paper_width * params.max_shrink);
463
464 // Safari uses: 765 & 1224. Margins aren't exactly the same either.
465 // Scale = 2.222 for MDI printer.
466 int pages;
467 if (!frame->SetPrintingMode(true,
468 minLayoutWidth,
469 maxLayoutWidth,
470 &printed_document_width_)) {
471 NOTREACHED();
472 pages = 0;
473 } else {
[email protected]98c74f82008-12-01 14:34:42474 DCHECK_GT(printed_document_width_, 0);
initial.commit09911bf2008-07-26 23:55:29475 // Force to recalculate the height, otherwise it reuse the current window
476 // height as the default.
477 float effective_shrink = printed_document_width_ / paper_width;
478 gfx::Size page_size(printed_document_width_,
479 static_cast<int>(paper_height * effective_shrink) - 1);
480 WebView* view = frame->GetView();
481 if (view) {
482 // Hack around an issue where if the current view height is higher than
483 // the page height, empty pages will be printed even if the bottom of the
484 // web page is empty.
485 printing_view_size_ = view->GetSize();
486 view->Resize(page_size);
487 view->Layout();
488 }
489 pages = frame->ComputePageRects(params.printable_size);
490 DCHECK(pages);
491 }
492 return pages;
493}
494
495void RenderView::SwitchFrameToDisplayMediaType(WebFrame* frame) {
496 // Set the layout back to "normal" document; i.e. CSS media type = "screen".
497 frame->SetPrintingMode(false, 0, 0, NULL);
498 WebView* view = frame->GetView();
499 if (view) {
500 // Restore from the hack described at SwitchFrameToPrintMediaType().
501 view->Resize(printing_view_size_);
502 view->Layout();
503 printing_view_size_.SetSize(0, 0);
504 }
505 printed_document_width_ = 0;
506}
507
initial.commit09911bf2008-07-26 23:55:29508void RenderView::PrintPage(const ViewMsg_PrintPage_Params& params,
509 WebFrame* frame) {
[email protected]6c8afae52009-01-22 02:24:57510#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:29511 if (printed_document_width_ <= 0) {
512 NOTREACHED();
513 return;
514 }
515
516 // Generate a memory-based EMF file. The EMF will use the current screen's
517 // DPI.
518 gfx::Emf emf;
519
520 emf.CreateDc(NULL, NULL);
521 HDC hdc = emf.hdc();
522 DCHECK(hdc);
[email protected]21f527e2008-12-17 23:29:40523 skia::PlatformDeviceWin::InitializeDC(hdc);
initial.commit09911bf2008-07-26 23:55:29524
525 gfx::Rect rect;
526 frame->GetPageRect(params.page_number, &rect);
527 DCHECK(rect.height());
528 DCHECK(rect.width());
529 double shrink = static_cast<double>(printed_document_width_) /
530 params.params.printable_size.width();
531 // This check would fire each time the page would get truncated on the
532 // right. This is not worth a DCHECK() but should be looked into, for
533 // example, wouldn't be worth trying in landscape?
534 // DCHECK_LE(rect.width(), printed_document_width_);
535
536 // Buffer one page at a time.
537 int src_size_x = printed_document_width_;
538 int src_size_y =
539 static_cast<int>(ceil(params.params.printable_size.height() *
540 shrink));
541#if 0
542 // TODO(maruel): This code is kept for testing until the 100% GDI drawing
543 // code is stable. maruels use this code's output as a reference when the
544 // GDI drawing code fails.
545
546 // Mix of Skia and GDI based.
[email protected]21f527e2008-12-17 23:29:40547 skia::PlatformCanvasWin canvas(src_size_x, src_size_y, true);
initial.commit09911bf2008-07-26 23:55:29548 canvas.drawARGB(255, 255, 255, 255, SkPorterDuff::kSrc_Mode);
549 PlatformContextSkia context(&canvas);
550 if (!frame->SpoolPage(params.page_number, &context)) {
551 NOTREACHED() << "Printing page " << params.page_number << " failed.";
552 return;
553 }
554
555 // Create a BMP v4 header that we can serialize.
556 BITMAPV4HEADER bitmap_header;
557 gfx::CreateBitmapV4Header(src_size_x, src_size_y, &bitmap_header);
558 const SkBitmap& src_bmp = canvas.getDevice()->accessBitmap(true);
559 SkAutoLockPixels src_lock(src_bmp);
560 int retval = StretchDIBits(hdc,
561 0,
562 0,
563 src_size_x, src_size_y,
564 0, 0,
565 src_size_x, src_size_y,
566 src_bmp.getPixels(),
567 reinterpret_cast<BITMAPINFO*>(&bitmap_header),
568 DIB_RGB_COLORS,
569 SRCCOPY);
570 DCHECK(retval != GDI_ERROR);
571#else
572 // 100% GDI based.
[email protected]21f527e2008-12-17 23:29:40573 skia::VectorCanvas canvas(hdc, src_size_x, src_size_y);
initial.commit09911bf2008-07-26 23:55:29574 // Set the clipping region to be sure to not overflow.
575 SkRect clip_rect;
576 clip_rect.set(0, 0, SkIntToScalar(src_size_x), SkIntToScalar(src_size_y));
577 canvas.clipRect(clip_rect);
[email protected]99c72202008-10-31 03:49:49578 if (!frame->SpoolPage(params.page_number, &canvas)) {
initial.commit09911bf2008-07-26 23:55:29579 NOTREACHED() << "Printing page " << params.page_number << " failed.";
580 return;
581 }
582#endif
583
584 // Done printing. Close the device context to retrieve the compiled EMF.
585 if (!emf.CloseDc()) {
586 NOTREACHED() << "EMF failed";
587 }
588
589 // Get the size of the compiled EMF.
590 unsigned buf_size = emf.GetDataSize();
591 DCHECK(buf_size > 128);
592 ViewHostMsg_DidPrintPage_Params page_params;
593 page_params.data_size = 0;
594 page_params.emf_data_handle = NULL;
595 page_params.page_number = params.page_number;
596 page_params.document_cookie = params.params.document_cookie;
597 page_params.actual_shrink = shrink;
[email protected]176aa482008-11-14 03:25:15598 base::SharedMemory shared_buf;
initial.commit09911bf2008-07-26 23:55:29599
600 // http://msdn2.microsoft.com/en-us/library/ms535522.aspx
601 // Windows 2000/XP: When a page in a spooled file exceeds approximately 350
602 // MB, it can fail to print and not send an error message.
603 if (buf_size < 350*1024*1024) {
604 // Allocate a shared memory buffer to hold the generated EMF data.
605 if (shared_buf.Create(L"", false, false, buf_size) &&
606 shared_buf.Map(buf_size)) {
607 // Copy the bits into shared memory.
608 if (emf.GetData(shared_buf.memory(), buf_size)) {
609 page_params.emf_data_handle = shared_buf.handle();
610 page_params.data_size = buf_size;
611 } else {
612 NOTREACHED() << "GetData() failed";
613 }
614 shared_buf.Unmap();
615 } else {
616 NOTREACHED() << "Buffer allocation failed";
617 }
618 } else {
619 NOTREACHED() << "Buffer too large: " << buf_size;
620 }
621 emf.CloseEmf();
622 if (Send(new ViewHostMsg_DuplicateSection(routing_id_,
623 page_params.emf_data_handle,
624 &page_params.emf_data_handle))) {
625 Send(new ViewHostMsg_DidPrintPage(routing_id_, page_params));
626 }
[email protected]6c8afae52009-01-22 02:24:57627#else // defined(OS_WIN)
628 // TODO(port) implement printing
629 NOTIMPLEMENTED();
630#endif
initial.commit09911bf2008-07-26 23:55:29631}
632
[email protected]068637222009-01-29 16:58:07633void RenderView::OnPrintPages() {
initial.commit09911bf2008-07-26 23:55:29634 DCHECK(webview());
[email protected]068637222009-01-29 16:58:07635 if (webview()) {
636 // The renderer own the control flow as if it was a window.print() call.
637 ScriptedPrint(webview()->GetMainFrame());
initial.commit09911bf2008-07-26 23:55:29638 }
initial.commit09911bf2008-07-26 23:55:29639}
640
641void RenderView::PrintPages(const ViewMsg_PrintPages_Params& params,
642 WebFrame* frame) {
643 int pages = SwitchFrameToPrintMediaType(params.params, frame);
644 Send(new ViewHostMsg_DidGetPrintedPagesCount(routing_id_,
645 params.params.document_cookie,
646 pages));
647 if (pages) {
648 ViewMsg_PrintPage_Params page_params;
649 page_params.params = params.params;
650 if (params.pages.empty()) {
651 for (int i = 0; i < pages; ++i) {
652 page_params.page_number = i;
653 PrintPage(page_params, frame);
654 }
655 } else {
656 for (size_t i = 0; i < params.pages.size(); ++i) {
657 page_params.page_number = params.pages[i];
658 PrintPage(page_params, frame);
659 }
660 }
661 }
662 SwitchFrameToDisplayMediaType(frame);
663}
664
665void RenderView::CapturePageInfo(int load_id, bool preliminary_capture) {
666 if (load_id != page_id_)
667 return; // this capture call is no longer relevant due to navigation
668 if (load_id == last_indexed_page_id_)
669 return; // we already indexed this page
670
671 if (!webview())
672 return;
673
674 WebFrame* main_frame = webview()->GetMainFrame();
675 if (!main_frame)
676 return;
677
678 // Don't index/capture pages that are in view source mode.
679 if (main_frame->GetInViewSourceMode())
680 return;
681
682 // Don't index/capture pages that failed to load. This only checks the top
683 // level frame so the thumbnail may contain a frame that failed to load.
684 WebDataSource* ds = main_frame->GetDataSource();
685 if (ds && ds->HasUnreachableURL())
686 return;
687
688 if (!preliminary_capture)
689 last_indexed_page_id_ = load_id;
690
691 // get the URL for this page
692 GURL url(main_frame->GetURL());
693 if (url.is_empty())
694 return;
695
696 // full text
697 std::wstring contents;
698 CaptureText(main_frame, &contents);
699 if (contents.size()) {
700 // Send the text to the browser for indexing.
701 Send(new ViewHostMsg_PageContents(url, load_id, contents));
702 }
703
704 // thumbnail
705 SendThumbnail();
706}
707
708void RenderView::CaptureText(WebFrame* frame, std::wstring* contents) {
709 contents->clear();
710 if (!frame)
711 return;
712
[email protected]0faf0bd92008-09-09 20:53:27713 // Don't index any https pages. People generally don't want their bank
714 // accounts, etc. indexed on their computer, especially since some of these
715 // things are not marked cachable.
716 // TODO(brettw) we may want to consider more elaborate heuristics such as
717 // the cachability of the page. We may also want to consider subframes (this
718 // test will still index subframes if the subframe is SSL).
719 if (frame->GetURL().SchemeIsSecure())
720 return;
721
initial.commit09911bf2008-07-26 23:55:29722#ifdef TIME_TEXT_RETRIEVAL
723 double begin = time_util::GetHighResolutionTimeNow();
724#endif
725
726 // get the contents of the frame
727 frame->GetContentAsPlainText(kMaxIndexChars, contents);
728
729#ifdef TIME_TEXT_RETRIEVAL
730 double end = time_util::GetHighResolutionTimeNow();
731 char buf[128];
732 sprintf_s(buf, "%d chars retrieved for indexing in %gms\n",
733 contents.size(), (end - begin)*1000);
734 OutputDebugStringA(buf);
735#endif
736
737 // When the contents are clipped to the maximum, we don't want to have a
738 // partial word indexed at the end that might have been clipped. Therefore,
739 // terminate the string at the last space to ensure no words are clipped.
740 if (contents->size() == kMaxIndexChars) {
741 size_t last_space_index = contents->find_last_of(kWhitespaceWide);
742 if (last_space_index == std::wstring::npos)
743 return; // don't index if we got a huge block of text with no spaces
744 contents->resize(last_space_index);
745 }
746}
747
[email protected]b6e4bec2008-11-12 01:17:15748bool RenderView::CaptureThumbnail(WebFrame* frame,
initial.commit09911bf2008-07-26 23:55:29749 int w,
750 int h,
751 SkBitmap* thumbnail,
752 ThumbnailScore* score) {
753#ifdef TIME_BITMAP_RETRIEVAL
754 double begin = time_util::GetHighResolutionTimeNow();
755#endif
756
[email protected]21f527e2008-12-17 23:29:40757 scoped_ptr<skia::BitmapPlatformDevice> device;
[email protected]b6e4bec2008-11-12 01:17:15758 if (!frame->CaptureImage(&device, true))
759 return false;
760
761 const SkBitmap& src_bmp = device->accessBitmap(false);
initial.commit09911bf2008-07-26 23:55:29762
763 SkRect dest_rect;
764 dest_rect.set(0, 0, SkIntToScalar(w), SkIntToScalar(h));
765 float dest_aspect = dest_rect.width() / dest_rect.height();
766
767 // Get the src rect so that we can preserve the aspect ratio while filling
768 // the destination.
769 SkIRect src_rect;
770 if (src_bmp.width() < dest_rect.width() ||
771 src_bmp.height() < dest_rect.height()) {
772 // Source image is smaller: we clip the part of source image within the
773 // dest rect, and then stretch it to fill the dest rect. We don't respect
774 // the aspect ratio in this case.
775 src_rect.set(0, 0, static_cast<S16CPU>(dest_rect.width()),
776 static_cast<S16CPU>(dest_rect.height()));
777 score->good_clipping = false;
778 } else {
779 float src_aspect = static_cast<float>(src_bmp.width()) / src_bmp.height();
780 if (src_aspect > dest_aspect) {
781 // Wider than tall, clip horizontally: we center the smaller thumbnail in
782 // the wider screen.
783 S16CPU new_width = static_cast<S16CPU>(src_bmp.height() * dest_aspect);
784 S16CPU x_offset = (src_bmp.width() - new_width) / 2;
785 src_rect.set(x_offset, 0, new_width + x_offset, src_bmp.height());
786 score->good_clipping = false;
787 } else {
788 src_rect.set(0, 0, src_bmp.width(),
789 static_cast<S16CPU>(src_bmp.width() / dest_aspect));
790 score->good_clipping = true;
791 }
792 }
793
794 score->at_top = (frame->ScrollOffset().height() == 0);
795
796 SkBitmap subset;
[email protected]b6e4bec2008-11-12 01:17:15797 device->accessBitmap(false).extractSubset(&subset, src_rect);
initial.commit09911bf2008-07-26 23:55:29798
799 // Resample the subset that we want to get it the right size.
[email protected]465b34b72008-12-12 20:19:14800 *thumbnail = skia::ImageOperations::Resize(
801 subset, skia::ImageOperations::RESIZE_LANCZOS3, w, h);
initial.commit09911bf2008-07-26 23:55:29802
803 score->boring_score = CalculateBoringScore(thumbnail);
804
805#ifdef TIME_BITMAP_RETRIEVAL
806 double end = time_util::GetHighResolutionTimeNow();
807 char buf[128];
808 sprintf_s(buf, "thumbnail in %gms\n", (end - begin) * 1000);
809 OutputDebugStringA(buf);
810#endif
[email protected]b6e4bec2008-11-12 01:17:15811 return true;
initial.commit09911bf2008-07-26 23:55:29812}
813
814double RenderView::CalculateBoringScore(SkBitmap* bitmap) {
815 int histogram[256] = {0};
816 color_utils::BuildLumaHistogram(bitmap, histogram);
817
818 int color_count = *std::max_element(histogram, histogram + 256);
819 int pixel_count = bitmap->width() * bitmap->height();
820 return static_cast<double>(color_count) / pixel_count;
821}
822
823void RenderView::OnNavigate(const ViewMsg_Navigate_Params& params) {
824 if (!webview())
825 return;
826
827 AboutHandler::MaybeHandle(params.url);
828
829 bool is_reload = params.reload;
830
831 WebFrame* main_frame = webview()->GetMainFrame();
[email protected]606843fa2008-12-02 19:08:56832 if (is_reload && !main_frame->HasCurrentHistoryState()) {
initial.commit09911bf2008-07-26 23:55:29833 // We cannot reload if we do not have any history state. This happens, for
834 // example, when recovering from a crash. Our workaround here is a bit of
835 // a hack since it means that reload after a crashed tab does not cause an
836 // end-to-end cache validation.
837 is_reload = false;
838 }
839
840 WebRequestCachePolicy cache_policy;
841 if (is_reload) {
842 cache_policy = WebRequestReloadIgnoringCacheData;
843 } else if (params.page_id != -1 || main_frame->GetInViewSourceMode()) {
844 cache_policy = WebRequestReturnCacheDataElseLoad;
845 } else {
846 cache_policy = WebRequestUseProtocolCachePolicy;
847 }
848
849 scoped_ptr<WebRequest> request(WebRequest::Create(params.url));
850 request->SetCachePolicy(cache_policy);
851 request->SetExtraData(new RenderViewExtraRequestData(
852 params.page_id, params.transition, params.url));
853
854 // If we are reloading, then WebKit will use the state of the current page.
855 // Otherwise, we give it the state to navigate to.
856 if (!is_reload)
857 request->SetHistoryState(params.state);
858
[email protected]4c6f2c92008-10-28 20:26:15859 if (params.referrer.is_valid()) {
[email protected]8e3c1a72008-11-25 01:13:32860 request->SetHttpHeaderValue("Referer",
861 params.referrer.spec());
[email protected]c0588052008-10-27 23:01:50862 }
863
initial.commit09911bf2008-07-26 23:55:29864 main_frame->LoadRequest(request.get());
865}
866
867// Stop loading the current page
868void RenderView::OnStop() {
869 if (webview())
870 webview()->StopLoading();
871}
872
873void RenderView::OnLoadAlternateHTMLText(const std::string& html_contents,
874 bool new_navigation,
875 const GURL& display_url,
876 const std::string& security_info) {
877 if (!webview())
878 return;
879
880 scoped_ptr<WebRequest> request(WebRequest::Create(
881 GURL(kUnreachableWebDataURL)));
882 request->SetSecurityInfo(security_info);
883
884 webview()->GetMainFrame()->LoadAlternateHTMLString(request.get(),
885 html_contents,
886 display_url,
887 !new_navigation);
888}
889
890void RenderView::OnCopyImageAt(int x, int y) {
891 webview()->CopyImageAt(x, y);
892}
893
894void RenderView::OnInspectElement(int x, int y) {
895 webview()->InspectElement(x, y);
896}
897
898void RenderView::OnShowJavaScriptConsole() {
899 webview()->ShowJavaScriptConsole();
900}
901
902void RenderView::OnStopFinding(bool clear_selection) {
903 WebView* view = webview();
904 if (!view)
905 return;
906
907 if (clear_selection)
908 view->GetFocusedFrame()->ClearSelection();
909
910 WebFrame* frame = view->GetMainFrame();
911 while (frame) {
[email protected]65134c432008-09-26 21:47:20912 frame->StopFinding(clear_selection);
initial.commit09911bf2008-07-26 23:55:29913 frame = view->GetNextFrameAfter(frame, false);
914 }
915}
916
917void RenderView::OnFindReplyAck() {
918 // Check if there is any queued up request waiting to be sent.
919 if (queued_find_reply_message_.get()) {
920 // Send the search result over to the browser process.
921 Send(queued_find_reply_message_.get());
922 queued_find_reply_message_.release();
923 }
924}
925
926void RenderView::OnUpdateTargetURLAck() {
927 // Check if there is a targeturl waiting to be sent.
928 if (target_url_status_ == TARGET_PENDING) {
929 Send(new ViewHostMsg_UpdateTargetURL(routing_id_, page_id_,
930 pending_target_url_));
931 }
932
933 target_url_status_ = TARGET_NONE;
934}
935
936void RenderView::OnUndo() {
937 if (!webview())
938 return;
939
940 webview()->GetFocusedFrame()->Undo();
941}
942
943void RenderView::OnRedo() {
944 if (!webview())
945 return;
946
947 webview()->GetFocusedFrame()->Redo();
948}
949
950void RenderView::OnCut() {
951 if (!webview())
952 return;
953
954 webview()->GetFocusedFrame()->Cut();
955}
956
957void RenderView::OnCopy() {
958 if (!webview())
959 return;
960
961 webview()->GetFocusedFrame()->Copy();
962}
963
964void RenderView::OnPaste() {
965 if (!webview())
966 return;
967
968 webview()->GetFocusedFrame()->Paste();
969}
970
971void RenderView::OnReplace(const std::wstring& text) {
972 if (!webview())
973 return;
974
975 webview()->GetFocusedFrame()->Replace(text);
976}
977
[email protected]bbbd545c2008-12-15 20:18:04978void RenderView::OnToggleSpellCheck() {
979 if (!webview())
980 return;
981
982 webview()->GetFocusedFrame()->ToggleSpellCheck();
983}
984
initial.commit09911bf2008-07-26 23:55:29985void RenderView::OnDelete() {
986 if (!webview())
987 return;
988
989 webview()->GetFocusedFrame()->Delete();
990}
991
992void RenderView::OnSelectAll() {
993 if (!webview())
994 return;
995
996 webview()->GetFocusedFrame()->SelectAll();
997}
998
999void RenderView::OnSetInitialFocus(bool reverse) {
1000 if (!webview())
1001 return;
1002 webview()->SetInitialFocus(reverse);
1003}
1004
1005///////////////////////////////////////////////////////////////////////////////
1006
1007// Tell the embedding application that the URL of the active page has changed
1008void RenderView::UpdateURL(WebFrame* frame) {
1009 WebDataSource* ds = frame->GetDataSource();
1010 DCHECK(ds);
1011
1012 const WebRequest& request = ds->GetRequest();
1013 const WebRequest& initial_request = ds->GetInitialRequest();
1014 const WebResponse& response = ds->GetResponse();
1015
1016 // We don't hold a reference to the extra data. The request's reference will
1017 // be sufficient because we won't modify it during our call. MAY BE NULL.
1018 RenderViewExtraRequestData* extra_data =
1019 static_cast<RenderViewExtraRequestData*>(request.GetExtraData());
1020
1021 ViewHostMsg_FrameNavigate_Params params;
1022 params.is_post = false;
1023 params.page_id = page_id_;
[email protected]8a3422c92008-09-24 17:42:421024 params.is_content_filtered = response.IsContentFiltered();
initial.commit09911bf2008-07-26 23:55:291025 if (!request.GetSecurityInfo().empty()) {
1026 // SSL state specified in the request takes precedence over the one in the
1027 // response.
1028 // So far this is only intended for error pages that are not expected to be
1029 // over ssl, so we should not get any clash.
1030 DCHECK(response.GetSecurityInfo().empty());
1031 params.security_info = request.GetSecurityInfo();
1032 } else {
1033 params.security_info = response.GetSecurityInfo();
1034 }
1035
1036 // Set the URL to be displayed in the browser UI to the user.
1037 if (ds->HasUnreachableURL()) {
1038 params.url = ds->GetUnreachableURL();
1039 } else {
1040 params.url = request.GetURL();
1041 }
1042
1043 params.redirects = ds->GetRedirectChain();
1044 params.should_update_history = !ds->HasUnreachableURL();
1045
1046 const SearchableFormData* searchable_form_data =
1047 frame->GetDataSource()->GetSearchableFormData();
1048 if (searchable_form_data) {
1049 params.searchable_form_url = searchable_form_data->url();
1050 params.searchable_form_element_name = searchable_form_data->element_name();
1051 params.searchable_form_encoding = searchable_form_data->encoding();
1052 }
1053
1054 const PasswordForm* password_form_data =
1055 frame->GetDataSource()->GetPasswordFormData();
1056 if (password_form_data)
1057 params.password_form = *password_form_data;
1058
1059 params.gesture = navigation_gesture_;
1060 navigation_gesture_ = NavigationGestureUnknown;
1061
1062 if (webview()->GetMainFrame() == frame) {
1063 // Top-level navigation.
1064
1065 // Update contents MIME type for main frame.
1066 std::wstring mime_type = ds->GetResponseMimeType();
1067 params.contents_mime_type = WideToASCII(mime_type);
1068
1069 // We assume top level navigations initiated by the renderer are link
1070 // clicks.
1071 params.transition = extra_data ?
1072 extra_data->transition_type : PageTransition::LINK;
1073 if (!PageTransition::IsMainFrame(params.transition)) {
1074 // If the main frame does a load, it should not be reported as a subframe
1075 // navigation. This can occur in the following case:
1076 // 1. You're on a site with frames.
1077 // 2. You do a subframe navigation. This is stored with transition type
1078 // MANUAL_SUBFRAME.
1079 // 3. You navigate to some non-frame site, say, google.com.
1080 // 4. You navigate back to the page from step 2. Since it was initially
1081 // MANUAL_SUBFRAME, it will be that same transition type here.
1082 // We don't want that, because any navigation that changes the toplevel
1083 // frame should be tracked as a toplevel navigation (this allows us to
1084 // update the URL bar, etc).
1085 params.transition = PageTransition::LINK;
1086 }
1087
1088 if (params.transition == PageTransition::LINK &&
1089 frame->GetDataSource()->IsFormSubmit()) {
1090 params.transition = PageTransition::FORM_SUBMIT;
1091 }
1092
1093 // If we have a valid consumed client redirect source,
1094 // the page contained a client redirect (meta refresh, document.loc...),
1095 // so we set the referrer and transition to match.
1096 if (completed_client_redirect_src_.is_valid()) {
[email protected]77e09a92008-08-01 18:11:041097 DCHECK(completed_client_redirect_src_ == params.redirects[0]);
initial.commit09911bf2008-07-26 23:55:291098 params.referrer = completed_client_redirect_src_;
1099 params.transition = static_cast<PageTransition::Type>(
1100 params.transition | PageTransition::CLIENT_REDIRECT);
1101 } else {
1102 // Bug 654101: the referrer will be empty on https->http transitions. It
1103 // would be nice if we could get the real referrer from somewhere.
1104 params.referrer = GURL(initial_request.GetHttpReferrer());
1105 }
1106
[email protected]8e3c1a72008-11-25 01:13:321107 std::string method = request.GetHttpMethod();
1108 if (method == "POST")
initial.commit09911bf2008-07-26 23:55:291109 params.is_post = true;
1110
1111 Send(new ViewHostMsg_FrameNavigate(routing_id_, params));
1112 } else {
1113 // Subframe navigation: the type depends on whether this navigation
1114 // generated a new session history entry. When they do generate a session
1115 // history entry, it means the user initiated the navigation and we should
1116 // mark it as such. This test checks if this is the first time UpdateURL
1117 // has been called since WillNavigateToURL was called to initiate the load.
1118 if (page_id_ > last_page_id_sent_to_browser_)
1119 params.transition = PageTransition::MANUAL_SUBFRAME;
1120 else
1121 params.transition = PageTransition::AUTO_SUBFRAME;
1122
1123 // The browser should never initiate a subframe navigation.
1124 DCHECK(!extra_data);
1125 Send(new ViewHostMsg_FrameNavigate(routing_id_, params));
1126 }
1127
1128 last_page_id_sent_to_browser_ =
1129 std::max(last_page_id_sent_to_browser_, page_id_);
1130
1131 // If we end up reusing this WebRequest (for example, due to a #ref click),
1132 // we don't want the transition type to persist.
1133 if (extra_data)
1134 extra_data->transition_type = PageTransition::LINK; // Just clear it.
[email protected]266eb6f2008-09-30 23:56:501135
[email protected]6c8afae52009-01-22 02:24:571136#if defined(OS_WIN)
[email protected]266eb6f2008-09-30 23:56:501137 if (glue_accessibility_.get()) {
[email protected]be645db2009-02-06 20:36:331138 // Clear accessibility info cache.
[email protected]266eb6f2008-09-30 23:56:501139 glue_accessibility_->ClearIAccessibleMap(-1, true);
1140 }
[email protected]6c8afae52009-01-22 02:24:571141#else
1142 // TODO(port): accessibility not yet implemented
1143 NOTIMPLEMENTED();
1144#endif
initial.commit09911bf2008-07-26 23:55:291145}
1146
1147// Tell the embedding application that the title of the active page has changed
1148void RenderView::UpdateTitle(WebFrame* frame, const std::wstring& title) {
1149 // Ignore all but top level navigations...
1150 if (webview()->GetMainFrame() == frame)
1151 Send(new ViewHostMsg_UpdateTitle(routing_id_, page_id_, title));
1152}
1153
1154void RenderView::UpdateEncoding(WebFrame* frame,
[email protected]e38f40152008-09-12 23:08:301155 const std::wstring& encoding_name) {
initial.commit09911bf2008-07-26 23:55:291156 // Only update main frame's encoding_name.
1157 if (webview()->GetMainFrame() == frame &&
1158 last_encoding_name_ != encoding_name) {
[email protected]e38f40152008-09-12 23:08:301159 // Save the encoding name for later comparing.
initial.commit09911bf2008-07-26 23:55:291160 last_encoding_name_ = encoding_name;
1161
[email protected]e38f40152008-09-12 23:08:301162 Send(new ViewHostMsg_UpdateEncoding(routing_id_, last_encoding_name_));
initial.commit09911bf2008-07-26 23:55:291163 }
1164}
1165
[email protected]f4d34b52008-11-24 23:05:011166// Sends the previous session history state to the browser so it will be saved
1167// before we navigate to a new page. This must be called *before* the page ID
1168// has been updated so we know what it was.
initial.commit09911bf2008-07-26 23:55:291169void RenderView::UpdateSessionHistory(WebFrame* frame) {
1170 // If we have a valid page ID at this point, then it corresponds to the page
1171 // we are navigating away from. Otherwise, this is the first navigation, so
1172 // there is no past session history to record.
1173 if (page_id_ == -1)
1174 return;
1175
initial.commit09911bf2008-07-26 23:55:291176 std::string state;
[email protected]606843fa2008-12-02 19:08:561177 if (!webview()->GetMainFrame()->GetPreviousHistoryState(&state))
initial.commit09911bf2008-07-26 23:55:291178 return;
[email protected]606843fa2008-12-02 19:08:561179 Send(new ViewHostMsg_UpdateState(routing_id_, page_id_, state));
initial.commit09911bf2008-07-26 23:55:291180}
1181
1182///////////////////////////////////////////////////////////////////////////////
1183// WebViewDelegate
1184
1185void RenderView::DidStartLoading(WebView* webview) {
1186 if (is_loading_) {
1187 DLOG(WARNING) << "DidStartLoading called while loading";
1188 return;
1189 }
1190
1191 is_loading_ = true;
1192 // Clear the pointer so that we can assign it only when there is an unknown
1193 // plugin on a page.
1194 first_default_plugin_ = NULL;
1195
1196 Send(new ViewHostMsg_DidStartLoading(routing_id_, page_id_));
1197}
1198
1199void RenderView::DidStopLoading(WebView* webview) {
1200 if (!is_loading_) {
1201 DLOG(WARNING) << "DidStopLoading called while not loading";
1202 return;
1203 }
1204
1205 is_loading_ = false;
1206
1207 // NOTE: For now we're doing the safest thing, and sending out notification
1208 // when done loading. This currently isn't an issue as the favicon is only
1209 // displayed when done loading. Ideally we would send notification when
1210 // finished parsing the head, but webkit doesn't support that yet.
1211 // The feed discovery code would also benefit from access to the head.
1212 GURL favicon_url(webview->GetMainFrame()->GetFavIconURL());
1213 if (!favicon_url.is_empty())
1214 Send(new ViewHostMsg_UpdateFavIconURL(routing_id_, page_id_, favicon_url));
1215
1216 AddGURLSearchProvider(webview->GetMainFrame()->GetOSDDURL(),
1217 true); // autodetected
1218
1219 Send(new ViewHostMsg_DidStopLoading(routing_id_, page_id_));
1220
1221 MessageLoop::current()->PostDelayedTask(FROM_HERE,
1222 method_factory_.NewRunnableMethod(&RenderView::CapturePageInfo, page_id_,
1223 false),
1224 kDelayForCaptureMs);
1225
1226 // The page is loaded. Try to process the file we need to upload if any.
1227 ProcessPendingUpload();
1228
1229 // Since the page is done loading, we are sure we don't need to try
1230 // again.
1231 ResetPendingUpload();
1232}
1233
1234void RenderView::DidStartProvisionalLoadForFrame(
1235 WebView* webview,
1236 WebFrame* frame,
1237 NavigationGesture gesture) {
[email protected]77e09a92008-08-01 18:11:041238 if (webview->GetMainFrame() == frame) {
initial.commit09911bf2008-07-26 23:55:291239 navigation_gesture_ = gesture;
[email protected]266eb6f2008-09-30 23:56:501240
[email protected]77e09a92008-08-01 18:11:041241 // Make sure redirect tracking state is clear for the new load.
1242 completed_client_redirect_src_ = GURL();
1243 }
initial.commit09911bf2008-07-26 23:55:291244
1245 Send(new ViewHostMsg_DidStartProvisionalLoadForFrame(
1246 routing_id_, webview->GetMainFrame() == frame,
1247 frame->GetProvisionalDataSource()->GetRequest().GetURL()));
1248}
1249
1250bool RenderView::DidLoadResourceFromMemoryCache(WebView* webview,
1251 const WebRequest& request,
1252 const WebResponse& response,
1253 WebFrame* frame) {
1254 // Let the browser know we loaded a resource from the memory cache. This
1255 // message is needed to display the correct SSL indicators.
1256 Send(new ViewHostMsg_DidLoadResourceFromMemoryCache(routing_id_,
1257 request.GetURL(), response.GetSecurityInfo()));
1258
1259 return false;
1260}
1261
1262void RenderView::DidReceiveProvisionalLoadServerRedirect(WebView* webview,
1263 WebFrame* frame) {
1264 if (frame == webview->GetMainFrame()) {
1265 // Received a redirect on the main frame.
1266 WebDataSource* data_source =
1267 webview->GetMainFrame()->GetProvisionalDataSource();
1268 if (!data_source) {
1269 // Should only be invoked when we have a data source.
1270 NOTREACHED();
1271 return;
1272 }
1273 const std::vector<GURL>& redirects = data_source->GetRedirectChain();
1274 if (redirects.size() >= 2) {
1275 Send(new ViewHostMsg_DidRedirectProvisionalLoad(
1276 routing_id_, page_id_, redirects[redirects.size() - 2],
1277 redirects[redirects.size() - 1]));
1278 }
1279 }
1280}
1281
1282void RenderView::DidFailProvisionalLoadWithError(WebView* webview,
1283 const WebError& error,
1284 WebFrame* frame) {
1285 // Notify the browser that we failed a provisional load with an error.
1286 //
1287 // Note: It is important this notification occur before DidStopLoading so the
1288 // SSL manager can react to the provisional load failure before being
1289 // notified the load stopped.
1290 //
1291 WebDataSource* ds = frame->GetProvisionalDataSource();
1292 DCHECK(ds);
1293
1294 const WebRequest& failed_request = ds->GetRequest();
1295
1296 bool show_repost_interstitial =
1297 (error.GetErrorCode() == net::ERR_CACHE_MISS &&
1298 LowerCaseEqualsASCII(failed_request.GetHttpMethod(), "post"));
1299 Send(new ViewHostMsg_DidFailProvisionalLoadWithError(
1300 routing_id_, frame == webview->GetMainFrame(),
1301 error.GetErrorCode(), error.GetFailedURL(),
1302 show_repost_interstitial));
1303
initial.commit09911bf2008-07-26 23:55:291304 // Don't display an error page if this is simply a cancelled load. Aside
1305 // from being dumb, WebCore doesn't expect it and it will cause a crash.
1306 if (error.GetErrorCode() == net::ERR_ABORTED)
1307 return;
1308
1309 // If this is a failed back/forward/reload navigation, then we need to do a
1310 // 'replace' load. This is necessary to avoid messing up session history.
1311 // Otherwise, we do a normal load, which simulates a 'go' navigation as far
1312 // as session history is concerned.
1313 RenderViewExtraRequestData* extra_data =
1314 static_cast<RenderViewExtraRequestData*>(failed_request.GetExtraData());
1315 bool replace = extra_data && !extra_data->is_new_navigation();
1316
[email protected]5df266ac2008-10-15 19:50:131317 // Use the alternate error page service if this is a DNS failure or
1318 // connection failure. ERR_CONNECTION_FAILED can be dropped once we no longer
1319 // use winhttp.
1320 int ec = error.GetErrorCode();
1321 if (ec == net::ERR_NAME_NOT_RESOLVED ||
1322 ec == net::ERR_CONNECTION_FAILED ||
1323 ec == net::ERR_CONNECTION_REFUSED ||
1324 ec == net::ERR_ADDRESS_UNREACHABLE ||
1325 ec == net::ERR_TIMED_OUT) {
1326 const GURL& failed_url = error.GetFailedURL();
1327 const GURL& error_page_url = GetAlternateErrorPageURL(failed_url,
1328 ec == net::ERR_NAME_NOT_RESOLVED ? WebViewDelegate::DNS_ERROR
1329 : WebViewDelegate::CONNECTION_ERROR);
1330 if (error_page_url.is_valid()) {
1331 // Ask the WebFrame to fetch the alternate error page for us.
1332 frame->LoadAlternateHTMLErrorPage(&failed_request, error, error_page_url,
1333 replace, GURL(kUnreachableWebDataURL));
1334 return;
1335 }
initial.commit09911bf2008-07-26 23:55:291336 }
[email protected]5df266ac2008-10-15 19:50:131337
[email protected]be645db2009-02-06 20:36:331338 // Fallback to a local error page.
[email protected]5df266ac2008-10-15 19:50:131339 LoadNavigationErrorPage(frame, &failed_request, error, std::string(),
1340 replace);
initial.commit09911bf2008-07-26 23:55:291341}
1342
1343void RenderView::LoadNavigationErrorPage(WebFrame* frame,
1344 const WebRequest* failed_request,
1345 const WebError& error,
1346 const std::string& html,
1347 bool replace) {
1348 const GURL& failed_url = error.GetFailedURL();
1349
1350 std::string alt_html;
1351 if (html.empty()) {
1352 // Use a local error page.
1353 int resource_id;
1354 DictionaryValue error_strings;
1355 if (error.GetErrorCode() == net::ERR_CACHE_MISS &&
1356 LowerCaseEqualsASCII(failed_request->GetHttpMethod(), "post")) {
1357 GetFormRepostErrorValues(failed_url, &error_strings);
1358 resource_id = IDR_ERROR_NO_DETAILS_HTML;
1359 } else {
1360 GetLocalizedErrorValues(error, &error_strings);
1361 resource_id = IDR_NET_ERROR_HTML;
1362 }
1363 error_strings.SetString(L"textdirection",
1364 (l10n_util::GetTextDirection() == l10n_util::RIGHT_TO_LEFT) ?
1365 L"rtl" : L"ltr");
1366
1367 alt_html = GetAltHTMLForTemplate(error_strings, resource_id);
1368 } else {
1369 alt_html = html;
1370 }
1371
1372 // Use a data: URL as the site URL to prevent against XSS attacks.
1373 scoped_ptr<WebRequest> request(failed_request->Clone());
1374 request->SetURL(GURL(kUnreachableWebDataURL));
1375
1376 frame->LoadAlternateHTMLString(request.get(), alt_html, failed_url,
1377 replace);
1378}
1379
1380void RenderView::DidCommitLoadForFrame(WebView *webview, WebFrame* frame,
1381 bool is_new_navigation) {
1382 const WebRequest& request =
1383 webview->GetMainFrame()->GetDataSource()->GetRequest();
1384 RenderViewExtraRequestData* extra_data =
1385 static_cast<RenderViewExtraRequestData*>(request.GetExtraData());
1386
1387 if (is_new_navigation) {
1388 // When we perform a new navigation, we need to update the previous session
1389 // history entry with state for the page we are leaving.
1390 UpdateSessionHistory(frame);
1391
1392 // We bump our Page ID to correspond with the new session history entry.
1393 page_id_ = next_page_id_++;
1394
1395 MessageLoop::current()->PostDelayedTask(FROM_HERE,
1396 method_factory_.NewRunnableMethod(&RenderView::CapturePageInfo,
1397 page_id_, true),
1398 kDelayForForcedCaptureMs);
1399 } else {
1400 // Inspect the extra_data on the main frame (set in our Navigate method) to
1401 // see if the navigation corresponds to a session history navigation...
1402 // Note: |frame| may or may not be the toplevel frame, but for the case
1403 // of capturing session history, the first committed frame suffices. We
1404 // keep track of whether we've seen this commit before so that only capture
1405 // session history once per navigation.
[email protected]f4d34b52008-11-24 23:05:011406 //
1407 // Note that we need to check if the page ID changed. In the case of a
1408 // reload, the page ID doesn't change, and UpdateSessionHistory gets the
1409 // previous URL and the current page ID, which would be wrong.
initial.commit09911bf2008-07-26 23:55:291410 if (extra_data && !extra_data->is_new_navigation() &&
[email protected]f4d34b52008-11-24 23:05:011411 !extra_data->request_committed &&
1412 page_id_ != extra_data->pending_page_id()) {
initial.commit09911bf2008-07-26 23:55:291413 // This is a successful session history navigation!
1414 UpdateSessionHistory(frame);
initial.commit09911bf2008-07-26 23:55:291415 page_id_ = extra_data->pending_page_id();
1416 }
1417 }
1418
1419 // Remember that we've already processed this request, so we don't update
1420 // the session history again. We do this regardless of whether this is
1421 // a session history navigation, because if we attempted a session history
1422 // navigation without valid HistoryItem state, WebCore will think it is a
1423 // new navigation.
1424 if (extra_data)
1425 extra_data->request_committed = true;
1426
1427 UpdateURL(frame);
1428
1429 // If this committed load was initiated by a client redirect, we're
1430 // at the last stop now, so clear it.
1431 completed_client_redirect_src_ = GURL();
1432
1433 // Check whether we have new encoding name.
1434 UpdateEncoding(frame, webview->GetMainFrameEncodingName());
1435}
1436
1437void RenderView::DidReceiveTitle(WebView* webview,
1438 const std::wstring& title,
1439 WebFrame* frame) {
1440 UpdateTitle(frame, title);
1441
1442 // Also check whether we have new encoding name.
1443 UpdateEncoding(frame, webview->GetMainFrameEncodingName());
1444}
1445
1446void RenderView::DidFinishLoadForFrame(WebView* webview, WebFrame* frame) {
1447}
1448
1449void RenderView::DidFailLoadWithError(WebView* webview,
1450 const WebError& error,
1451 WebFrame* frame) {
1452}
1453
1454void RenderView::DidFinishDocumentLoadForFrame(WebView* webview,
1455 WebFrame* frame) {
1456 // Check whether we have new encoding name.
1457 UpdateEncoding(frame, webview->GetMainFrameEncodingName());
[email protected]1e0f70402008-10-16 23:57:471458
[email protected]0938d3c2009-01-09 20:37:351459 // Inject any user scripts. Do not inject into chrome UI pages, but do inject
1460 // into any other document.
[email protected]f08a247c2009-01-28 09:22:441461 const GURL &gurl = frame->GetURL();
1462 if (g_render_thread && // Will be NULL when testing.
1463 (gurl.SchemeIs("file") ||
1464 gurl.SchemeIs("http") ||
1465 gurl.SchemeIs("https"))) {
1466 g_render_thread->user_script_slave()->InjectScripts(frame);
[email protected]1e0f70402008-10-16 23:57:471467 }
initial.commit09911bf2008-07-26 23:55:291468}
1469
1470void RenderView::DidHandleOnloadEventsForFrame(WebView* webview,
1471 WebFrame* frame) {
1472}
1473
1474void RenderView::DidChangeLocationWithinPageForFrame(WebView* webview,
1475 WebFrame* frame,
1476 bool is_new_navigation) {
1477 DidCommitLoadForFrame(webview, frame, is_new_navigation);
[email protected]de56f3782008-10-01 22:31:351478 const std::wstring& title =
1479 webview->GetMainFrame()->GetDataSource()->GetPageTitle();
1480 UpdateTitle(frame, title);
initial.commit09911bf2008-07-26 23:55:291481}
1482
1483void RenderView::DidReceiveIconForFrame(WebView* webview,
1484 WebFrame* frame) {
1485}
1486
1487void RenderView::WillPerformClientRedirect(WebView* webview,
1488 WebFrame* frame,
1489 const GURL& src_url,
1490 const GURL& dest_url,
1491 unsigned int delay_seconds,
1492 unsigned int fire_date) {
1493}
1494
1495void RenderView::DidCancelClientRedirect(WebView* webview,
1496 WebFrame* frame) {
1497}
1498
1499void RenderView::DidCompleteClientRedirect(WebView* webview,
1500 WebFrame* frame,
1501 const GURL& source) {
1502 if (webview->GetMainFrame() == frame)
1503 completed_client_redirect_src_ = source;
1504}
1505
1506void RenderView::BindDOMAutomationController(WebFrame* webframe) {
1507 dom_automation_controller_.set_message_sender(this);
1508 dom_automation_controller_.set_routing_id(routing_id_);
1509 dom_automation_controller_.BindToJavascript(webframe,
1510 L"domAutomationController");
1511}
1512
1513void RenderView::WindowObjectCleared(WebFrame* webframe) {
1514 external_js_object_.set_render_view(this);
1515 external_js_object_.BindToJavascript(webframe, L"external");
1516 if (enable_dom_automation_)
1517 BindDOMAutomationController(webframe);
1518 if (enable_dom_ui_bindings_) {
1519 dom_ui_bindings_.set_message_sender(this);
1520 dom_ui_bindings_.set_routing_id(routing_id_);
1521 dom_ui_bindings_.BindToJavascript(webframe, L"chrome");
1522 }
[email protected]18cb2572008-08-21 20:34:451523 if (enable_external_host_bindings_) {
1524 external_host_bindings_.set_message_sender(this);
1525 external_host_bindings_.set_routing_id(routing_id_);
1526 external_host_bindings_.BindToJavascript(webframe, L"externalHost");
1527 }
[email protected]9a2051d2008-08-15 20:12:421528
[email protected]3a453fa2008-08-15 18:46:341529#ifdef CHROME_PERSONALIZATION
1530 Personalization::ConfigureRendererPersonalization(personalization_, this,
1531 routing_id_, webframe);
1532#endif
initial.commit09911bf2008-07-26 23:55:291533}
1534
1535WindowOpenDisposition RenderView::DispositionForNavigationAction(
1536 WebView* webview,
1537 WebFrame* frame,
1538 const WebRequest* request,
1539 WebNavigationType type,
1540 WindowOpenDisposition disposition,
1541 bool is_redirect) {
1542 // Webkit is asking whether to navigate to a new URL.
1543 // This is fine normally, except if we're showing UI from one security
1544 // context and they're trying to navigate to a different context.
1545 const GURL& url = request->GetURL();
1546 // We only care about navigations that are within the current tab (as opposed
1547 // to, for example, opening a new window).
1548 // But we sometimes navigate to about:blank to clear a tab, and we want to
1549 // still allow that.
1550 if (disposition == CURRENT_TAB && !(url.SchemeIs("about"))) {
1551 // GetExtraData is NULL when we did not issue the request ourselves (see
1552 // OnNavigate), and so such a request may correspond to a link-click,
1553 // script, or drag-n-drop initiated navigation.
1554 if (frame == webview->GetMainFrame() && !request->GetExtraData()) {
1555 // When we received such unsolicited navigations, we sometimes want to
1556 // punt them up to the browser to handle.
1557 if (enable_dom_ui_bindings_ ||
1558 frame->GetInViewSourceMode() ||
1559 url.SchemeIs("view-source")) {
[email protected]c0588052008-10-27 23:01:501560 OpenURL(webview, url, GURL(), disposition);
initial.commit09911bf2008-07-26 23:55:291561 return IGNORE_ACTION; // Suppress the load here.
[email protected]50b691c2008-10-31 19:08:351562 } else if (url.SchemeIs(kBackForwardNavigationScheme)) {
1563 std::string offset_str = url.ExtractFileName();
1564 int offset;
1565 if (StringToInt(offset_str, &offset)) {
[email protected]0c0383772008-11-04 00:48:311566 GoToEntryAtOffset(offset);
[email protected]50b691c2008-10-31 19:08:351567 return IGNORE_ACTION; // The browser process handles this one.
1568 }
initial.commit09911bf2008-07-26 23:55:291569 }
1570 }
1571 }
1572
1573 // Detect when a page is "forking" a new tab that can be safely rendered in
1574 // its own process. This is done by sites like Gmail that try to open links
1575 // in new windows without script connections back to the original page. We
1576 // treat such cases as browser navigations (in which we will create a new
1577 // renderer for a cross-site navigation), rather than WebKit navigations.
1578 //
1579 // We use the following heuristic to decide whether to fork a new page in its
1580 // own process:
1581 // The parent page must open a new tab to about:blank, set the new tab's
1582 // window.opener to null, and then redirect the tab to a cross-site URL using
1583 // JavaScript.
1584 bool is_fork =
1585 // Must start from a tab showing about:blank, which is later redirected.
1586 frame->GetURL() == GURL("about:blank") &&
1587 // Must be the first real navigation of the tab.
1588 GetHistoryBackListCount() < 1 &&
1589 GetHistoryForwardListCount() < 1 &&
1590 // The parent page must have set the child's window.opener to null before
1591 // redirecting to the desired URL.
1592 frame->GetOpener() == NULL &&
1593 // Must be a top-level frame.
1594 frame->GetParent() == NULL &&
1595 // Must not have issued the request from this page. GetExtraData is NULL
1596 // when the navigation is being done by something outside the page.
1597 !request->GetExtraData() &&
1598 // Must be targeted at the current tab.
1599 disposition == CURRENT_TAB &&
1600 // Must be a JavaScript navigation, which appears as "other".
1601 type == WebNavigationTypeOther;
1602 if (is_fork) {
1603 // Open the URL via the browser, not via WebKit.
[email protected]c0588052008-10-27 23:01:501604 OpenURL(webview, url, GURL(), disposition);
initial.commit09911bf2008-07-26 23:55:291605 return IGNORE_ACTION;
1606 }
1607
1608 return disposition;
1609}
1610
[email protected]6c8afae52009-01-22 02:24:571611#if defined(OS_POSIX)
1612// TODO(port): remove this massive hack
1613// WARNING: massive hack. We can't include message_box_view.h because that
1614// tries to pull in the rest of views. So we just define a fake MessageBoxView
1615// here with the constants that we require.
1616
1617class MessageBoxView {
1618 public:
1619 static const int kFlagHasOKButton = 0x1;
1620 static const int kFlagHasCancelButton = 0x2;
1621 static const int kFlagHasPromptField = 0x4;
1622 static const int kFlagHasMessage = 0x8;
1623
1624 static const int kIsConfirmMessageBox = kFlagHasMessage |
1625 kFlagHasOKButton |
1626 kFlagHasCancelButton;
1627 static const int kIsJavascriptAlert = kFlagHasOKButton | kFlagHasMessage;
1628 static const int kIsJavascriptConfirm = kIsJavascriptAlert |
1629 kFlagHasCancelButton;
1630 static const int kIsJavascriptPrompt = kIsJavascriptConfirm |
1631 kFlagHasPromptField;
1632};
1633#endif
1634
initial.commit09911bf2008-07-26 23:55:291635void RenderView::RunJavaScriptAlert(WebView* webview,
1636 const std::wstring& message) {
1637 RunJavaScriptMessage(MessageBoxView::kIsJavascriptAlert,
1638 message,
1639 std::wstring(),
1640 NULL);
1641}
1642
1643bool RenderView::RunJavaScriptConfirm(WebView* webview,
1644 const std::wstring& message) {
1645 return RunJavaScriptMessage(MessageBoxView::kIsJavascriptConfirm,
1646 message,
1647 std::wstring(),
1648 NULL);
1649}
1650
1651bool RenderView::RunJavaScriptPrompt(WebView* webview,
1652 const std::wstring& message,
1653 const std::wstring& default_value,
1654 std::wstring* result) {
1655 return RunJavaScriptMessage(MessageBoxView::kIsJavascriptPrompt,
1656 message,
1657 default_value,
1658 result);
1659}
1660
1661bool RenderView::RunJavaScriptMessage(int type,
1662 const std::wstring& message,
1663 const std::wstring& default_value,
1664 std::wstring* result) {
1665 bool success = false;
1666 std::wstring result_temp;
1667 if (!result)
1668 result = &result_temp;
1669 IPC::SyncMessage* msg = new ViewHostMsg_RunJavaScriptMessage(
1670 routing_id_, message, default_value, type, &success, result);
1671
[email protected]1c4947f2009-01-15 22:25:111672 msg->set_pump_messages_event(modal_dialog_event_.get());
initial.commit09911bf2008-07-26 23:55:291673 Send(msg);
1674
1675 return success;
1676}
1677
1678void RenderView::AddGURLSearchProvider(const GURL& osd_url, bool autodetected) {
1679 if (!osd_url.is_empty())
1680 Send(new ViewHostMsg_PageHasOSDD(routing_id_, page_id_, osd_url,
1681 autodetected));
1682}
1683
1684bool RenderView::RunBeforeUnloadConfirm(WebView* webview,
1685 const std::wstring& message) {
1686 bool success = false;
1687 // This is an ignored return value, but is included so we can accept the same
1688 // response as RunJavaScriptMessage.
1689 std::wstring ignored_result;
1690 IPC::SyncMessage* msg = new ViewHostMsg_RunBeforeUnloadConfirm(
1691 routing_id_, message, &success, &ignored_result);
1692
[email protected]1c4947f2009-01-15 22:25:111693 msg->set_pump_messages_event(modal_dialog_event_.get());
initial.commit09911bf2008-07-26 23:55:291694 Send(msg);
1695
1696 return success;
1697}
1698
[email protected]0578a502008-11-10 19:34:431699void RenderView::EnableSuddenTermination() {
1700 Send(new ViewHostMsg_UnloadListenerChanged(routing_id_, false));
1701}
1702
1703void RenderView::DisableSuddenTermination() {
1704 Send(new ViewHostMsg_UnloadListenerChanged(routing_id_, true));
initial.commit09911bf2008-07-26 23:55:291705}
1706
[email protected]0ebf3872008-11-07 21:35:031707void RenderView::QueryFormFieldAutofill(const std::wstring& field_name,
1708 const std::wstring& text,
1709 int64 node_id) {
1710 static int message_id_counter = 0;
1711 form_field_autofill_request_id_ = message_id_counter++;
1712 Send(new ViewHostMsg_QueryFormFieldAutofill(routing_id_,
1713 field_name, text,
1714 node_id,
1715 form_field_autofill_request_id_));
1716}
1717
1718void RenderView::OnReceivedAutofillSuggestions(
1719 int64 node_id,
1720 int request_id,
[email protected]8d0f15c2008-11-11 01:01:091721 const std::vector<std::wstring>& suggestions,
[email protected]0ebf3872008-11-07 21:35:031722 int default_suggestion_index) {
1723 if (!webview() || request_id != form_field_autofill_request_id_)
1724 return;
1725
1726 webview()->AutofillSuggestionsForNode(node_id, suggestions,
1727 default_suggestion_index);
1728}
1729
[email protected]634a6f92008-12-01 21:39:311730void RenderView::OnPopupNotificationVisiblityChanged(bool visible) {
1731 popup_notification_visible_ = visible;
1732}
1733
initial.commit09911bf2008-07-26 23:55:291734void RenderView::ShowModalHTMLDialog(const GURL& url, int width, int height,
1735 const std::string& json_arguments,
1736 std::string* json_retval) {
1737 IPC::SyncMessage* msg = new ViewHostMsg_ShowModalHTMLDialog(
1738 routing_id_, url, width, height, json_arguments, json_retval);
1739
[email protected]1c4947f2009-01-15 22:25:111740 msg->set_pump_messages_event(modal_dialog_event_.get());
initial.commit09911bf2008-07-26 23:55:291741 Send(msg);
1742}
1743
1744uint32 RenderView::GetCPBrowsingContext() {
1745 uint32 context = 0;
1746 Send(new ViewHostMsg_GetCPBrowsingContext(&context));
1747 return context;
1748}
1749
1750// Tell the browser to display a destination link.
1751void RenderView::UpdateTargetURL(WebView* webview, const GURL& url) {
1752 if (url != target_url_) {
1753 if (target_url_status_ == TARGET_INFLIGHT ||
1754 target_url_status_ == TARGET_PENDING) {
1755 // If we have a request in-flight, save the URL to be sent when we
1756 // receive an ACK to the in-flight request. We can happily overwrite
1757 // any existing pending sends.
1758 pending_target_url_ = url;
1759 target_url_status_ = TARGET_PENDING;
1760 } else {
1761 Send(new ViewHostMsg_UpdateTargetURL(routing_id_, page_id_, url));
1762 target_url_ = url;
1763 target_url_status_ = TARGET_INFLIGHT;
1764 }
1765 }
1766}
1767
[email protected]b62d1a8c2009-01-13 23:54:571768void RenderView::RunFileChooser(bool multi_select,
1769 const std::wstring& title,
1770 const std::wstring& default_filename,
1771 const std::wstring& filter,
initial.commit09911bf2008-07-26 23:55:291772 WebFileChooserCallback* file_chooser) {
1773 if (file_chooser_.get()) {
1774 // TODO(brettw): bug 1235154: This should be a synchronous message to deal
1775 // with the fact that web pages can programatically trigger this. With the
1776 // asnychronous messages, we can get an additional call when one is pending,
1777 // which this test is for. For now, we just ignore the additional file
1778 // chooser request. WebKit doesn't do anything to expect the callback, so
1779 // we can just ignore calling it.
1780 delete file_chooser;
1781 return;
1782 }
1783 file_chooser_.reset(file_chooser);
[email protected]b62d1a8c2009-01-13 23:54:571784 Send(new ViewHostMsg_RunFileChooser(routing_id_, multi_select, title,
1785 default_filename, filter));
initial.commit09911bf2008-07-26 23:55:291786}
1787
1788void RenderView::AddMessageToConsole(WebView* webview,
1789 const std::wstring& message,
1790 unsigned int line_no,
1791 const std::wstring& source_id) {
1792 Send(new ViewHostMsg_AddMessageToConsole(routing_id_, message,
1793 static_cast<int32>(line_no),
1794 source_id));
1795}
1796
1797void RenderView::AddSearchProvider(const std::string& url) {
1798 AddGURLSearchProvider(GURL(url),
1799 false); // not autodetected
1800}
1801
1802void RenderView::DebuggerOutput(const std::wstring& out) {
1803 Send(new ViewHostMsg_DebuggerOutput(routing_id_, out));
1804}
1805
1806WebView* RenderView::CreateWebView(WebView* webview, bool user_gesture) {
[email protected]0aa55312008-10-17 21:53:081807 // Check to make sure we aren't overloading on popups.
1808 if (shared_popup_counter_->data > kMaximumNumberOfUnacknowledgedPopups)
1809 return NULL;
1810
[email protected]634a6f92008-12-01 21:39:311811 // This window can't be closed from a window.close() call until we receive a
1812 // message from the Browser process explicitly allowing it.
1813 popup_notification_visible_ = true;
1814
initial.commit09911bf2008-07-26 23:55:291815 int32 routing_id = MSG_ROUTING_NONE;
[email protected]6c8afae52009-01-22 02:24:571816
[email protected]18bcc3c2009-01-27 21:39:151817 ModalDialogEvent modal_dialog_event;
[email protected]6c8afae52009-01-22 02:24:571818 render_thread_->Send(
[email protected]15787f8f2008-10-17 15:29:031819 new ViewHostMsg_CreateWindow(routing_id_, user_gesture, &routing_id,
1820 &modal_dialog_event));
initial.commit09911bf2008-07-26 23:55:291821 if (routing_id == MSG_ROUTING_NONE) {
initial.commit09911bf2008-07-26 23:55:291822 return NULL;
1823 }
1824
1825 // The WebView holds a reference to this new RenderView
1826 const WebPreferences& prefs = webview->GetPreferences();
[email protected]6c8afae52009-01-22 02:24:571827 base::WaitableEvent* waitable_event = new base::WaitableEvent
1828#if defined(OS_WIN)
[email protected]18bcc3c2009-01-27 21:39:151829 (modal_dialog_event.event);
[email protected]6c8afae52009-01-22 02:24:571830#else
1831 (true, false);
1832#endif
[email protected]81a34412009-01-05 19:17:241833 RenderView* view = RenderView::Create(render_thread_,
[email protected]1c4947f2009-01-15 22:25:111834 NULL, waitable_event, routing_id_,
[email protected]0aa55312008-10-17 21:53:081835 prefs, shared_popup_counter_,
1836 routing_id);
initial.commit09911bf2008-07-26 23:55:291837 view->set_opened_by_user_gesture(user_gesture);
[email protected]06828b92008-10-20 21:25:461838 view->set_waiting_for_create_window_ack(true);
initial.commit09911bf2008-07-26 23:55:291839
1840 // Copy over the alternate error page URL so we can have alt error pages in
1841 // the new render view (we don't need the browser to send the URL back down).
1842 view->alternate_error_page_url_ = alternate_error_page_url_;
1843
1844 return view->webview();
1845}
1846
[email protected]0ebf3872008-11-07 21:35:031847WebWidget* RenderView::CreatePopupWidget(WebView* webview,
[email protected]cfd727f2009-01-09 20:21:111848 bool activatable) {
[email protected]8085dbc82008-09-26 22:53:441849 RenderWidget* widget = RenderWidget::Create(routing_id_,
[email protected]81a34412009-01-05 19:17:241850 render_thread_,
[email protected]cfd727f2009-01-09 20:21:111851 activatable);
initial.commit09911bf2008-07-26 23:55:291852 return widget->webwidget();
1853}
1854
[email protected]6c8afae52009-01-22 02:24:571855#if defined(OS_WIN)
1856// TODO(port): This is only used on Windows since the plugin code is #ifdefed
1857// out for other platforms currently
1858
[email protected]173de1b2008-08-15 18:36:461859static bool ShouldLoadPluginInProcess(const std::string& mime_type,
1860 bool* is_gears) {
1861 if (RenderProcess::ShouldLoadPluginsInProcess())
1862 return true;
1863
1864 if (mime_type == "application/x-googlegears") {
1865 *is_gears = true;
[email protected]bb975362009-01-21 01:00:221866 return CommandLine::ForCurrentProcess()->HasSwitch(
1867 switches::kGearsInRenderer);
[email protected]173de1b2008-08-15 18:36:461868 }
1869
1870 return false;
1871}
[email protected]6c8afae52009-01-22 02:24:571872#endif
[email protected]173de1b2008-08-15 18:36:461873
initial.commit09911bf2008-07-26 23:55:291874WebPluginDelegate* RenderView::CreatePluginDelegate(
1875 WebView* webview,
1876 const GURL& url,
1877 const std::string& mime_type,
1878 const std::string& clsid,
1879 std::string* actual_mime_type) {
[email protected]6c8afae52009-01-22 02:24:571880#if defined(OS_WIN)
[email protected]173de1b2008-08-15 18:36:461881 bool is_gears = false;
1882 if (ShouldLoadPluginInProcess(mime_type, &is_gears)) {
[email protected]690a99c2009-01-06 16:48:451883 FilePath path;
[email protected]81a34412009-01-05 19:17:241884 render_thread_->Send(
initial.commit09911bf2008-07-26 23:55:291885 new ViewHostMsg_GetPluginPath(url, mime_type, clsid, &path,
1886 actual_mime_type));
[email protected]690a99c2009-01-06 16:48:451887 if (path.value().empty())
initial.commit09911bf2008-07-26 23:55:291888 return NULL;
1889
1890 std::string mime_type_to_use;
1891 if (actual_mime_type && !actual_mime_type->empty())
1892 mime_type_to_use = *actual_mime_type;
1893 else
1894 mime_type_to_use = mime_type;
1895
[email protected]173de1b2008-08-15 18:36:461896 if (is_gears)
1897 ChromePluginLib::Create(path, GetCPBrowserFuncsForRenderer());
[email protected]b94d3322009-02-12 19:49:041898 return WebPluginDelegate::Create(path,
1899 mime_type_to_use,
1900 gfx::NativeViewFromId(host_window_));
initial.commit09911bf2008-07-26 23:55:291901 }
1902
1903 WebPluginDelegateProxy* proxy =
1904 WebPluginDelegateProxy::Create(url, mime_type, clsid, this);
1905 if (!proxy)
1906 return NULL;
1907
1908 // We hold onto the proxy so we can poke it when we are painting. See our
1909 // DidPaint implementation below.
1910 plugin_delegates_.push_back(proxy);
1911
1912 return proxy;
[email protected]6c8afae52009-01-22 02:24:571913#else
1914 // TODO(port): Plugins currently not supported
1915 NOTIMPLEMENTED();
1916 return NULL;
1917#endif
initial.commit09911bf2008-07-26 23:55:291918}
1919
[email protected]ec9212f2008-12-18 21:40:361920webkit_glue::WebMediaPlayerDelegate* RenderView::CreateMediaPlayerDelegate() {
[email protected]be645db2009-02-06 20:36:331921#if defined(OS_WIN)
[email protected]d43ed912009-02-03 04:52:531922 return new WebMediaPlayerDelegateImpl(this);
[email protected]be645db2009-02-06 20:36:331923#else
1924 // TODO(port)
1925 NOTIMPLEMENTED();
1926 return NULL;
1927#endif
[email protected]ec9212f2008-12-18 21:40:361928}
1929
initial.commit09911bf2008-07-26 23:55:291930void RenderView::OnMissingPluginStatus(WebPluginDelegate* delegate,
1931 int status) {
[email protected]6c8afae52009-01-22 02:24:571932#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:291933 if (first_default_plugin_ == NULL) {
1934 // Show the InfoBar for the first available plugin.
1935 if (status == default_plugin::MISSING_PLUGIN_AVAILABLE) {
1936 first_default_plugin_ = delegate;
1937 Send(new ViewHostMsg_MissingPluginStatus(routing_id_, status));
1938 }
1939 } else {
1940 // Closes the InfoBar if user clicks on the plugin (instead of the InfoBar)
1941 // to start the download/install.
1942 if (status == default_plugin::MISSING_PLUGIN_USER_STARTED_DOWNLOAD) {
1943 Send(new ViewHostMsg_MissingPluginStatus(routing_id_, status));
1944 }
1945 }
[email protected]6c8afae52009-01-22 02:24:571946#else
1947 // TODO(port): plugins current not supported
1948 NOTIMPLEMENTED();
1949#endif
initial.commit09911bf2008-07-26 23:55:291950}
1951
1952void RenderView::OpenURL(WebView* webview, const GURL& url,
[email protected]c0588052008-10-27 23:01:501953 const GURL& referrer,
initial.commit09911bf2008-07-26 23:55:291954 WindowOpenDisposition disposition) {
[email protected]c0588052008-10-27 23:01:501955 Send(new ViewHostMsg_OpenURL(routing_id_, url, referrer, disposition));
initial.commit09911bf2008-07-26 23:55:291956}
1957
1958// We are supposed to get a single call to Show for a newly created RenderView
1959// that was created via RenderView::CreateWebView. So, we wait until this
1960// point to dispatch the ShowView message.
1961//
1962// This method provides us with the information about how to display the newly
1963// created RenderView (i.e., as a constrained popup or as a new tab).
1964//
1965void RenderView::Show(WebWidget* webwidget, WindowOpenDisposition disposition) {
1966 DCHECK(!did_show_) << "received extraneous Show call";
1967 DCHECK(opener_id_ != MSG_ROUTING_NONE);
1968
1969 if (did_show_)
1970 return;
1971 did_show_ = true;
1972
1973 // NOTE: initial_pos_ may still have its default values at this point, but
1974 // that's okay. It'll be ignored if disposition is not NEW_POPUP, or the
1975 // browser process will impose a default position otherwise.
1976 Send(new ViewHostMsg_ShowView(
1977 opener_id_, routing_id_, disposition, initial_pos_,
1978 WasOpenedByUserGestureHelper()));
1979}
1980
[email protected]634a6f92008-12-01 21:39:311981void RenderView::CloseWidgetSoon(WebWidget* webwidget) {
1982 if (popup_notification_visible_ == false)
1983 RenderWidget::CloseWidgetSoon(webwidget);
1984}
1985
initial.commit09911bf2008-07-26 23:55:291986void RenderView::RunModal(WebWidget* webwidget) {
1987 DCHECK(did_show_) << "should already have shown the view";
1988
1989 IPC::SyncMessage* msg = new ViewHostMsg_RunModal(routing_id_);
1990
[email protected]1c4947f2009-01-15 22:25:111991 msg->set_pump_messages_event(modal_dialog_event_.get());
initial.commit09911bf2008-07-26 23:55:291992 Send(msg);
1993}
1994
1995void RenderView::SyncNavigationState() {
1996 if (!webview())
1997 return;
1998
initial.commit09911bf2008-07-26 23:55:291999 std::string state;
[email protected]606843fa2008-12-02 19:08:562000 if (!webview()->GetMainFrame()->GetCurrentHistoryState(&state))
initial.commit09911bf2008-07-26 23:55:292001 return;
[email protected]606843fa2008-12-02 19:08:562002 Send(new ViewHostMsg_UpdateState(routing_id_, page_id_, state));
initial.commit09911bf2008-07-26 23:55:292003}
2004
2005void RenderView::ShowContextMenu(WebView* webview,
[email protected]124646932009-01-28 18:39:022006 ContextNode node,
initial.commit09911bf2008-07-26 23:55:292007 int x,
2008 int y,
2009 const GURL& link_url,
2010 const GURL& image_url,
2011 const GURL& page_url,
2012 const GURL& frame_url,
2013 const std::wstring& selection_text,
2014 const std::wstring& misspelled_word,
[email protected]6aa376b2008-09-23 18:49:522015 int edit_flags,
2016 const std::string& security_info) {
[email protected]e09ba552009-02-05 03:26:292017 ContextMenuParams params;
[email protected]124646932009-01-28 18:39:022018 params.node = node;
initial.commit09911bf2008-07-26 23:55:292019 params.x = x;
2020 params.y = y;
2021 params.image_url = image_url;
2022 params.link_url = link_url;
2023 params.page_url = page_url;
2024 params.frame_url = frame_url;
2025 params.selection_text = selection_text;
2026 params.misspelled_word = misspelled_word;
[email protected]be645db2009-02-06 20:36:332027 params.spellcheck_enabled =
[email protected]bbbd545c2008-12-15 20:18:042028 webview->GetFocusedFrame()->SpellCheckEnabled();
initial.commit09911bf2008-07-26 23:55:292029 params.edit_flags = edit_flags;
[email protected]6aa376b2008-09-23 18:49:522030 params.security_info = security_info;
initial.commit09911bf2008-07-26 23:55:292031 Send(new ViewHostMsg_ContextMenu(routing_id_, params));
2032}
2033
2034void RenderView::StartDragging(WebView* webview, const WebDropData& drop_data) {
2035 Send(new ViewHostMsg_StartDragging(routing_id_, drop_data));
2036}
2037
2038void RenderView::TakeFocus(WebView* webview, bool reverse) {
2039 Send(new ViewHostMsg_TakeFocus(routing_id_, reverse));
2040}
2041
2042void RenderView::DidDownloadImage(int id,
2043 const GURL& image_url,
2044 bool errored,
2045 const SkBitmap& image) {
2046 Send(new ViewHostMsg_DidDownloadImage(routing_id_, id, image_url, errored,
2047 image));
2048}
2049
2050
2051void RenderView::OnDownloadImage(int id,
2052 const GURL& image_url,
2053 int image_size) {
2054 if (!webview()->DownloadImage(id, image_url, image_size))
2055 Send(new ViewHostMsg_DidDownloadImage(routing_id_, id, image_url, true,
2056 SkBitmap()));
2057}
2058
2059void RenderView::OnGetApplicationInfo(int page_id) {
2060 webkit_glue::WebApplicationInfo app_info;
2061 if (page_id == page_id_)
2062 webkit_glue::GetApplicationInfo(webview(), &app_info);
2063
2064 // Prune out any data URLs in the set of icons. The browser process expects
2065 // any icon with a data URL to have originated from a favicon. We don't want
2066 // to decode arbitrary data URLs in the browser process. See
2067 // http://b/issue?id=1162972
2068 for (size_t i = 0; i < app_info.icons.size(); ++i) {
2069 if (app_info.icons[i].url.SchemeIs("data")) {
2070 app_info.icons.erase(app_info.icons.begin() + i);
2071 --i;
2072 }
2073 }
2074
2075 Send(new ViewHostMsg_DidGetApplicationInfo(routing_id_, page_id, app_info));
2076}
2077
2078GURL RenderView::GetAlternateErrorPageURL(const GURL& failedURL,
2079 ErrorPageType error_type) {
2080 if (failedURL.SchemeIsSecure()) {
2081 // If the URL that failed was secure, then the embedding web page was not
2082 // expecting a network attacker to be able to manipulate its contents. As
2083 // we fetch alternate error pages over HTTP, we would be allowing a network
2084 // attacker to manipulate the contents of the response if we tried to use
2085 // the link doctor here.
2086 return GURL::EmptyGURL();
2087 }
2088
2089 // Grab the base URL from the browser process.
2090 if (!alternate_error_page_url_.is_valid())
2091 return GURL::EmptyGURL();
2092
2093 // Strip query params from the failed URL.
2094 GURL::Replacements remove_params;
2095 remove_params.ClearUsername();
2096 remove_params.ClearPassword();
2097 remove_params.ClearQuery();
2098 remove_params.ClearRef();
2099 const GURL url_to_send = failedURL.ReplaceComponents(remove_params);
2100
2101 // Construct the query params to send to link doctor.
2102 std::string params(alternate_error_page_url_.query());
2103 params.append("&url=");
2104 params.append(EscapeQueryParamValue(url_to_send.spec()));
2105 params.append("&sourceid=chrome");
2106 params.append("&error=");
2107 switch (error_type) {
2108 case DNS_ERROR:
2109 params.append("dnserror");
2110 break;
2111
2112 case HTTP_404:
2113 params.append("http404");
2114 break;
2115
[email protected]5df266ac2008-10-15 19:50:132116 case CONNECTION_ERROR:
[email protected]e1f934b2009-01-26 20:41:332117 params.append("connectionfailure");
[email protected]5df266ac2008-10-15 19:50:132118 break;
2119
initial.commit09911bf2008-07-26 23:55:292120 default:
2121 NOTREACHED() << "unknown ErrorPageType";
2122 }
2123
2124 // OK, build the final url to return.
2125 GURL::Replacements link_doctor_params;
2126 link_doctor_params.SetQueryStr(params);
2127 GURL url = alternate_error_page_url_.ReplaceComponents(link_doctor_params);
2128 return url;
2129}
2130
2131void RenderView::OnFind(const FindInPageRequest& request) {
2132 WebFrame* main_frame = webview()->GetMainFrame();
2133 WebFrame* frame_after_main = webview()->GetNextFrameAfter(main_frame, true);
2134 WebFrame* focused_frame = webview()->GetFocusedFrame();
2135 WebFrame* search_frame = focused_frame; // start searching focused frame.
2136
2137 bool multi_frame = (frame_after_main != main_frame);
2138
2139 // If we have multiple frames, we don't want to wrap the search within the
2140 // frame, so we check here if we only have main_frame in the chain.
2141 bool wrap_within_frame = !multi_frame;
2142
2143 gfx::Rect selection_rect;
2144 bool result = false;
2145
2146 do {
[email protected]884db412008-11-24 23:46:502147 result = search_frame->Find(request, wrap_within_frame, &selection_rect);
initial.commit09911bf2008-07-26 23:55:292148
2149 if (!result) {
2150 // don't leave text selected as you move to the next frame.
2151 search_frame->ClearSelection();
2152
2153 // Find the next frame, but skip the invisible ones.
2154 do {
2155 // What is the next frame to search? (we might be going backwards). Note
2156 // that we specify wrap=true so that search_frame never becomes NULL.
2157 search_frame = request.forward ?
2158 webview()->GetNextFrameAfter(search_frame, true) :
2159 webview()->GetPreviousFrameBefore(search_frame, true);
2160 } while (!search_frame->Visible() && search_frame != focused_frame);
2161
[email protected]884db412008-11-24 23:46:502162 // Make sure selection doesn't affect the search operation in new frame.
initial.commit09911bf2008-07-26 23:55:292163 search_frame->ClearSelection();
2164
2165 // If we have multiple frames and we have wrapped back around to the
2166 // focused frame, we need to search it once more allowing wrap within
2167 // the frame, otherwise it will report 'no match' if the focused frame has
2168 // reported matches, but no frames after the focused_frame contain a
2169 // match for the search word(s).
2170 if (multi_frame && search_frame == focused_frame) {
[email protected]884db412008-11-24 23:46:502171 result = search_frame->Find(request, true, // Force wrapping.
2172 &selection_rect);
initial.commit09911bf2008-07-26 23:55:292173 }
2174 }
2175
2176 // TODO(jcampan): http://b/issue?id=1157486 Remove StoreForFocus call once
2177 // we have the fix for 792423.
2178 search_frame->GetView()->StoreFocusForFrame(search_frame);
2179 webview()->SetFocusedFrame(search_frame);
2180 } while (!result && search_frame != focused_frame);
2181
2182 // Make sure we don't leave any frame focused or the focus won't be restored
2183 // properly in WebViewImpl::SetFocus(). Note that we are talking here about
2184 // focused on the SelectionController, not FocusController.
2185 // webview()->GetFocusedFrame() will still return the last focused frame (as
2186 // it queries the FocusController).
2187 // TODO(jcampan): http://b/issue?id=1157486 Remove next line once we have the
2188 // fix for 792423.
2189 webview()->SetFocusedFrame(NULL);
2190
2191 // We send back word that we found some matches, because we don't want to lag
2192 // when notifying the user that we found something. At this point we only know
2193 // that we found 1 match, but the scoping effort will tell us more. However,
2194 // if this is a FindNext request, the scoping effort is already under way, or
2195 // done already, so we have partial results. In that case we set it to -1 so
2196 // that it gets ignored by the FindInPageController.
2197 int match_count = result ? 1 : 0; // 1 here means possibly more coming.
2198 if (request.find_next)
2199 match_count = -1;
2200
2201 // If we find no matches (or if this is Find Next) then this will be our last
2202 // status update. Otherwise the scoping effort will send more results.
2203 bool final_status_update = !result || request.find_next;
2204
2205 // Send the search result over to the browser process.
2206 Send(new ViewHostMsg_Find_Reply(routing_id_, request.request_id,
2207 match_count,
2208 selection_rect,
2209 -1, // Don't update active match ordinal.
2210 final_status_update));
2211
2212 if (!request.find_next) {
2213 // Scoping effort begins, starting with the mainframe.
2214 search_frame = main_frame;
2215
2216 main_frame->ResetMatchCount();
2217
2218 do {
2219 // Cancel all old scoping requests before starting a new one.
2220 search_frame->CancelPendingScopingEffort();
2221
2222 // We don't start another scoping effort unless at least one match has
2223 // been found.
2224 if (result) {
2225 // Start new scoping request. If the scoping function determines that it
2226 // needs to scope, it will defer until later.
2227 search_frame->ScopeStringMatches(request,
2228 true); // reset the tickmarks
2229 }
2230
2231 // Iterate to the next frame. The frame will not necessarily scope, for
2232 // example if it is not visible.
2233 search_frame = webview()->GetNextFrameAfter(search_frame, true);
2234 } while (search_frame != main_frame);
2235 }
2236}
2237
2238void RenderView::ReportFindInPageMatchCount(int count, int request_id,
2239 bool final_update) {
2240 // If we have a message that has been queued up, then we should just replace
2241 // it. The ACK from the browser will make sure it gets sent when the browser
2242 // wants it.
2243 if (queued_find_reply_message_.get()) {
2244 IPC::Message* msg = new ViewHostMsg_Find_Reply(
2245 routing_id_,
2246 request_id,
2247 count,
2248 gfx::Rect(0, 0, 0, 0),
2249 -1, // Don't update active match ordinal.
2250 final_update);
2251 queued_find_reply_message_.reset(msg);
2252 } else {
2253 // Send the search result over to the browser process.
2254 Send(new ViewHostMsg_Find_Reply(
2255 routing_id_,
2256 request_id,
2257 count,
2258 gfx::Rect(0, 0, 0, 0),
2259 -1, // // Don't update active match ordinal.
2260 final_update));
2261 }
2262}
2263
2264void RenderView::ReportFindInPageSelection(int request_id,
2265 int active_match_ordinal,
2266 const gfx::Rect& selection_rect) {
2267 // Send the search result over to the browser process.
2268 Send(new ViewHostMsg_Find_Reply(routing_id_,
2269 request_id,
2270 -1,
2271 selection_rect,
2272 active_match_ordinal,
2273 false));
2274}
2275
2276bool RenderView::WasOpenedByUserGesture(WebView* webview) const {
2277 return WasOpenedByUserGestureHelper();
2278}
2279
2280bool RenderView::WasOpenedByUserGestureHelper() const {
2281 // If pop-up blocking has been disabled, then treat all new windows as if
2282 // they were opened by a user gesture. This will prevent them from being
2283 // blocked. This is a bit of a hack, there should be a more straightforward
2284 // way to disable pop-up blocking.
2285 if (disable_popup_blocking_)
2286 return true;
2287
2288 return opened_by_user_gesture_;
2289}
2290
2291void RenderView::SpellCheck(const std::wstring& word, int& misspell_location,
2292 int& misspell_length) {
2293 Send(new ViewHostMsg_SpellCheck(routing_id_, word, &misspell_location,
2294 &misspell_length));
2295}
2296
2297void RenderView::SetInputMethodState(bool enabled) {
2298 // Save the updated IME status and mark the input focus has been updated.
2299 // The IME status is to be sent to a browser process next time when
2300 // the input caret is rendered.
[email protected]9f23f592008-11-17 08:36:342301 if (!ime_control_busy_) {
2302 ime_control_updated_ = true;
2303 ime_control_new_state_ = enabled;
2304 }
initial.commit09911bf2008-07-26 23:55:292305}
2306
2307void RenderView::ScriptedPrint(WebFrame* frame) {
[email protected]6c8afae52009-01-22 02:24:572308#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:292309 // Retrieve the default print settings to calculate the expected number of
2310 // pages.
2311 ViewMsg_Print_Params default_settings;
2312 IPC::SyncMessage* msg =
2313 new ViewHostMsg_GetDefaultPrintSettings(routing_id_, &default_settings);
2314 if (Send(msg)) {
2315 msg = NULL;
2316 // Continue only if the settings are valid.
2317 if (default_settings.dpi && default_settings.document_cookie) {
2318 int expected_pages_count = SwitchFrameToPrintMediaType(default_settings,
2319 frame);
2320 DCHECK(expected_pages_count);
2321 SwitchFrameToDisplayMediaType(frame);
2322
2323 // Ask the browser to show UI to retrieve the final print settings.
2324 ViewMsg_PrintPages_Params print_settings;
2325 // host_window_ may be NULL at this point if the current window is a popup
2326 // and the print() command has been issued from the parent. The receiver
2327 // of this message has to deal with this.
2328 msg = new ViewHostMsg_ScriptedPrint(routing_id_,
2329 host_window_,
2330 default_settings.document_cookie,
2331 expected_pages_count,
2332 &print_settings);
2333 if (Send(msg)) {
2334 msg = NULL;
2335
2336 // If the settings are invalid, early quit.
2337 if (print_settings.params.dpi &&
2338 print_settings.params.document_cookie) {
2339 // Render the printed pages. It will implicitly revert the document to
2340 // display CSS media type.
2341 PrintPages(print_settings, frame);
2342 // All went well.
2343 return;
2344 } else {
2345 // The user cancelled.
2346 }
2347 } else {
2348 // Send() failed.
2349 NOTREACHED();
2350 }
2351 } else {
2352 // The user cancelled.
2353 }
2354 } else {
2355 // Send() failed.
2356 NOTREACHED();
2357 }
2358 // TODO(maruel): bug 1123882 Alert the user that printing failed.
[email protected]6c8afae52009-01-22 02:24:572359#else // defined(OS_WIN)
2360 // TODO(port): print not implemented
2361 NOTIMPLEMENTED();
2362#endif
initial.commit09911bf2008-07-26 23:55:292363}
2364
2365void RenderView::WebInspectorOpened(int num_resources) {
2366 Send(new ViewHostMsg_InspectElement_Reply(routing_id_, num_resources));
2367}
2368
2369void RenderView::UserMetricsRecordAction(const std::wstring& action) {
2370 Send(new ViewHostMsg_UserMetricsRecordAction(routing_id_, action));
2371}
2372
2373void RenderView::DnsPrefetch(const std::vector<std::string>& host_names) {
2374 Send(new ViewHostMsg_DnsPrefetch(host_names));
2375}
2376
[email protected]630e26b2008-10-14 22:55:172377void RenderView::OnZoom(int function) {
2378 static const bool kZoomIsTextOnly = false;
2379 switch (function) {
2380 case PageZoom::SMALLER:
2381 webview()->ZoomOut(kZoomIsTextOnly);
initial.commit09911bf2008-07-26 23:55:292382 break;
[email protected]630e26b2008-10-14 22:55:172383 case PageZoom::STANDARD:
2384 webview()->ResetZoom();
initial.commit09911bf2008-07-26 23:55:292385 break;
[email protected]630e26b2008-10-14 22:55:172386 case PageZoom::LARGER:
2387 webview()->ZoomIn(kZoomIsTextOnly);
initial.commit09911bf2008-07-26 23:55:292388 break;
2389 default:
2390 NOTREACHED();
2391 }
2392}
2393
[email protected]e38f40152008-09-12 23:08:302394void RenderView::OnSetPageEncoding(const std::wstring& encoding_name) {
initial.commit09911bf2008-07-26 23:55:292395 webview()->SetPageEncoding(encoding_name);
2396}
2397
2398void RenderView::OnPasswordFormsSeen(WebView* webview,
2399 const std::vector<PasswordForm>& forms) {
2400 Send(new ViewHostMsg_PasswordFormsSeen(routing_id_, forms));
2401}
2402
[email protected]8d0f15c2008-11-11 01:01:092403void RenderView::OnAutofillFormSubmitted(WebView* webview,
2404 const AutofillForm& form) {
2405 Send(new ViewHostMsg_AutofillFormSubmitted(routing_id_, form));
2406}
2407
initial.commit09911bf2008-07-26 23:55:292408WebHistoryItem* RenderView::GetHistoryEntryAtOffset(int offset) {
[email protected]50b691c2008-10-31 19:08:352409 // Our history list is kept in the browser process on the UI thread. Since
2410 // we can't make a sync IPC call to that thread without risking deadlock,
2411 // we use a trick: construct a fake history item of the form:
2412 // history://go/OFFSET
2413 // When WebCore tells us to navigate to it, we tell the browser process to
2414 // do a back/forward navigation instead.
2415
2416 GURL url(StringPrintf("%s://go/%d", kBackForwardNavigationScheme, offset));
2417 history_navigation_item_ = WebHistoryItem::Create(url, L"", "", NULL);
2418 return history_navigation_item_.get();
initial.commit09911bf2008-07-26 23:55:292419}
2420
[email protected]0c0383772008-11-04 00:48:312421void RenderView::GoToEntryAtOffset(int offset) {
[email protected]f46aff62008-10-16 07:58:052422 history_back_list_count_ += offset;
2423 history_forward_list_count_ -= offset;
2424
initial.commit09911bf2008-07-26 23:55:292425 Send(new ViewHostMsg_GoToEntryAtOffset(routing_id_, offset));
2426}
2427
2428int RenderView::GetHistoryBackListCount() {
2429 return history_back_list_count_;
2430}
2431
2432int RenderView::GetHistoryForwardListCount() {
2433 return history_forward_list_count_;
2434}
2435
2436void RenderView::OnNavStateChanged(WebView* webview) {
[email protected]81a34412009-01-05 19:17:242437 if (!nav_state_sync_timer_.IsRunning()) {
2438 nav_state_sync_timer_.Start(
2439 TimeDelta::FromSeconds(delay_seconds_for_form_state_sync_), this,
2440 &RenderView::SyncNavigationState);
2441 }
initial.commit09911bf2008-07-26 23:55:292442}
2443
2444void RenderView::SetTooltipText(WebView* webview,
2445 const std::wstring& tooltip_text) {
2446 Send(new ViewHostMsg_SetTooltipText(routing_id_, tooltip_text));
2447}
2448
2449void RenderView::DownloadUrl(const GURL& url, const GURL& referrer) {
2450 Send(new ViewHostMsg_DownloadUrl(routing_id_, url, referrer));
2451}
2452
2453WebFrame* RenderView::GetChildFrame(const std::wstring& frame_xpath) const {
2454 WebFrame* web_frame;
2455 if (frame_xpath.empty()) {
2456 web_frame = webview()->GetMainFrame();
2457 } else {
2458 web_frame = webview()->GetMainFrame()->GetChildFrame(frame_xpath);
2459 }
2460
2461 return web_frame;
2462}
2463
[email protected]f29acf52008-11-03 20:08:332464void RenderView::EvaluateScript(const std::wstring& frame_xpath,
2465 const std::wstring& script) {
initial.commit09911bf2008-07-26 23:55:292466 WebFrame* web_frame = GetChildFrame(frame_xpath);
2467 if (!web_frame)
2468 return;
2469
[email protected]49fed3252009-01-17 00:06:272470 web_frame->ExecuteJavaScript(WideToUTF8(script),
2471 GURL(), // script url
2472 1); // base line number
initial.commit09911bf2008-07-26 23:55:292473}
2474
2475void RenderView::OnScriptEvalRequest(const std::wstring& frame_xpath,
2476 const std::wstring& jscript) {
[email protected]f29acf52008-11-03 20:08:332477 EvaluateScript(frame_xpath, jscript);
initial.commit09911bf2008-07-26 23:55:292478}
2479
2480void RenderView::OnAddMessageToConsole(const std::wstring& frame_xpath,
2481 const std::wstring& msg,
2482 ConsoleMessageLevel level) {
2483 WebFrame* web_frame = GetChildFrame(frame_xpath);
2484 if (!web_frame)
2485 return;
2486
2487 web_frame->AddMessageToConsole(msg, level);
2488}
2489
[email protected]6c8afae52009-01-22 02:24:572490#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:292491void RenderView::OnDebugAttach() {
initial.commit09911bf2008-07-26 23:55:292492 Send(new ViewHostMsg_DidDebugAttach(routing_id_));
2493 // Tell the plugin host to stop accepting messages in order to avoid
2494 // hangs while the renderer is paused.
2495 // TODO(1243929): It might be an improvement to add more plumbing to do this
2496 // when the renderer is actually paused vs. just the debugger being attached.
2497 PluginChannelHost::SetListening(false);
2498}
2499
2500void RenderView::OnDebugDetach() {
2501 // Tell the plugin host to start accepting plugin messages again.
2502 PluginChannelHost::SetListening(true);
2503}
[email protected]6c8afae52009-01-22 02:24:572504#else // defined(OS_WIN)
2505// TODO(port): plugins not yet supported
2506void RenderView::OnDebugAttach() { NOTIMPLEMENTED(); }
2507void RenderView::OnDebugDetach() { NOTIMPLEMENTED(); }
2508#endif
initial.commit09911bf2008-07-26 23:55:292509
2510void RenderView::OnAllowDomAutomationBindings(bool allow_bindings) {
2511 enable_dom_automation_ = allow_bindings;
2512}
2513
[email protected]18cb2572008-08-21 20:34:452514void RenderView::OnAllowBindings(bool enable_dom_ui_bindings,
[email protected]266eb6f2008-09-30 23:56:502515 bool enable_external_host_bindings) {
[email protected]18cb2572008-08-21 20:34:452516 enable_dom_ui_bindings_ = enable_dom_ui_bindings;
2517 enable_external_host_bindings_ = enable_external_host_bindings;
initial.commit09911bf2008-07-26 23:55:292518}
2519
2520void RenderView::OnSetDOMUIProperty(const std::string& name,
2521 const std::string& value) {
2522 DCHECK(enable_dom_ui_bindings_);
2523 dom_ui_bindings_.SetProperty(name, value);
2524}
2525
2526void RenderView::OnReservePageIDRange(int size_of_range) {
2527 next_page_id_ += size_of_range + 1;
2528}
2529
2530void RenderView::OnDragSourceEndedOrMoved(int client_x,
2531 int client_y,
2532 int screen_x,
2533 int screen_y,
2534 bool ended) {
2535 if (ended)
2536 webview()->DragSourceEndedAt(client_x, client_y, screen_x, screen_y);
2537 else
2538 webview()->DragSourceMovedTo(client_x, client_y, screen_x, screen_y);
2539}
2540
2541void RenderView::OnDragSourceSystemDragEnded() {
2542 webview()->DragSourceSystemDragEnded();
2543}
2544
2545void RenderView::OnUploadFileRequest(const ViewMsg_UploadFile_Params& p) {
2546 webkit_glue::FileUploadData* f = new webkit_glue::FileUploadData;
2547 f->file_path = p.file_path;
2548 f->form_name = p.form;
2549 f->file_name = p.file;
2550 f->submit_name = p.submit;
2551
2552 // Build the other form values map.
2553 if (!p.other_values.empty()) {
2554 std::vector<std::wstring> e;
2555 std::vector<std::wstring> kvp;
2556 std::vector<std::wstring>::iterator i;
2557
2558 SplitString(p.other_values, L'\n', &e);
2559 for (i = e.begin(); i != e.end(); ++i) {
2560 SplitString(*i, L'=', &kvp);
2561 if (kvp.size() == 2)
2562 f->other_form_values[kvp[0]] = kvp[1];
2563 kvp.clear();
2564 }
2565 }
2566
2567 pending_upload_data_.reset(f);
2568 ProcessPendingUpload();
2569}
2570
2571void RenderView::ProcessPendingUpload() {
2572 webkit_glue::FileUploadData* f = pending_upload_data_.get();
2573 if (f && webview() && webkit_glue::FillFormToUploadFile(webview(), *f))
2574 ResetPendingUpload();
2575}
2576
2577void RenderView::ResetPendingUpload() {
2578 pending_upload_data_.reset();
2579}
2580
2581void RenderView::OnFormFill(const FormData& form) {
2582 webkit_glue::FillForm(this->webview(), form);
2583}
2584
2585void RenderView::OnFillPasswordForm(
2586 const PasswordFormDomManager::FillData& form_data) {
2587 webkit_glue::FillPasswordForm(this->webview(), form_data);
2588}
2589
2590void RenderView::OnDragTargetDragEnter(const WebDropData& drop_data,
2591 const gfx::Point& client_pt, const gfx::Point& screen_pt) {
2592 bool is_drop_target = webview()->DragTargetDragEnter(drop_data,
2593 client_pt.x(), client_pt.y(), screen_pt.x(), screen_pt.y());
2594
2595 Send(new ViewHostMsg_UpdateDragCursor(routing_id_, is_drop_target));
2596}
2597
2598void RenderView::OnDragTargetDragOver(const gfx::Point& client_pt,
2599 const gfx::Point& screen_pt) {
2600 bool is_drop_target = webview()->DragTargetDragOver(client_pt.x(),
2601 client_pt.y(), screen_pt.x(), screen_pt.y());
2602
2603 Send(new ViewHostMsg_UpdateDragCursor(routing_id_, is_drop_target));
2604}
2605
2606void RenderView::OnDragTargetDragLeave() {
2607 webview()->DragTargetDragLeave();
2608}
2609
2610void RenderView::OnDragTargetDrop(const gfx::Point& client_pt,
2611 const gfx::Point& screen_pt) {
2612 webview()->DragTargetDrop(client_pt.x(), client_pt.y(), screen_pt.x(),
2613 screen_pt.y());
2614}
2615
2616void RenderView::OnUpdateWebPreferences(const WebPreferences& prefs) {
2617 webview()->SetPreferences(prefs);
2618}
2619
2620void RenderView::OnSetAltErrorPageURL(const GURL& url) {
2621 alternate_error_page_url_ = url;
2622}
2623
2624void RenderView::DidPaint() {
2625 PluginDelegateList::iterator it = plugin_delegates_.begin();
2626 while (it != plugin_delegates_.end()) {
2627 (*it)->FlushGeometryUpdates();
2628 ++it;
2629 }
2630}
2631
2632void RenderView::OnInstallMissingPlugin() {
2633 // This could happen when the first default plugin is deleted.
2634 if (first_default_plugin_ == NULL)
2635 return;
2636 first_default_plugin_->InstallMissingPlugin();
2637}
2638
[email protected]b62d1a8c2009-01-13 23:54:572639void RenderView::OnFileChooserResponse(
2640 const std::vector<std::wstring>& file_names) {
2641 file_chooser_->OnFileChoose(file_names);
initial.commit09911bf2008-07-26 23:55:292642 file_chooser_.reset();
2643}
2644
2645void RenderView::OnEnableViewSourceMode() {
2646 if (!webview())
2647 return;
2648 WebFrame* main_frame = webview()->GetMainFrame();
2649 if (!main_frame)
2650 return;
2651
2652 main_frame->SetInViewSourceMode(true);
2653}
2654
2655void RenderView::OnUpdateBackForwardListCount(int back_list_count,
2656 int forward_list_count) {
2657 history_back_list_count_ = back_list_count;
2658 history_forward_list_count_ = forward_list_count;
2659}
2660
[email protected]266eb6f2008-09-30 23:56:502661void RenderView::OnGetAccessibilityInfo(
[email protected]e09ba552009-02-05 03:26:292662 const AccessibilityInParams& in_params,
2663 AccessibilityOutParams* out_params) {
[email protected]6c8afae52009-01-22 02:24:572664#if defined(OS_WIN)
[email protected]266eb6f2008-09-30 23:56:502665 if (!glue_accessibility_.get())
2666 glue_accessibility_.reset(new GlueAccessibility());
2667
2668 if (!glue_accessibility_->
2669 GetAccessibilityInfo(webview(), in_params, out_params)) {
2670 return;
2671 }
[email protected]6c8afae52009-01-22 02:24:572672#else // defined(OS_WIN)
2673 // TODO(port): accessibility not yet implemented
2674 NOTIMPLEMENTED();
2675#endif
[email protected]266eb6f2008-09-30 23:56:502676}
2677
2678void RenderView::OnClearAccessibilityInfo(int iaccessible_id, bool clear_all) {
[email protected]6c8afae52009-01-22 02:24:572679#if defined(OS_WIN)
[email protected]266eb6f2008-09-30 23:56:502680 if (!glue_accessibility_.get()) {
2681 // If accessibility is not activated, ignore clearing message.
2682 return;
2683 }
2684
2685 if (!glue_accessibility_->ClearIAccessibleMap(iaccessible_id, clear_all))
2686 return;
[email protected]6c8afae52009-01-22 02:24:572687#else // defined(OS_WIN)
2688 // TODO(port): accessibility not yet implemented
2689 NOTIMPLEMENTED();
2690#endif
[email protected]266eb6f2008-09-30 23:56:502691}
2692
initial.commit09911bf2008-07-26 23:55:292693void RenderView::OnGetAllSavableResourceLinksForCurrentPage(
2694 const GURL& page_url) {
2695 // Prepare list to storage all savable resource links.
2696 std::vector<GURL> resources_list;
2697 std::vector<GURL> referrers_list;
2698 std::vector<GURL> frames_list;
2699 webkit_glue::SavableResourcesResult result(&resources_list,
2700 &referrers_list,
2701 &frames_list);
2702
2703 if (!webkit_glue::GetAllSavableResourceLinksForCurrentPage(webview(),
2704 page_url,
2705 &result)) {
2706 // If something is wrong when collecting all savable resource links,
2707 // send empty list to embedder(browser) to tell it failed.
2708 referrers_list.clear();
2709 resources_list.clear();
2710 frames_list.clear();
2711 }
2712
2713 // Send result of all savable resource links to embedder.
2714 Send(new ViewHostMsg_SendCurrentPageAllSavableResourceLinks(routing_id_,
2715 resources_list,
2716 referrers_list,
2717 frames_list));
2718}
2719
2720void RenderView::OnGetSerializedHtmlDataForCurrentPageWithLocalLinks(
[email protected]f6b48532009-02-12 01:56:322721 const std::vector<GURL>& links,
initial.commit09911bf2008-07-26 23:55:292722 const std::vector<std::wstring>& local_paths,
2723 const std::wstring& local_directory_name) {
2724 webkit_glue::DomSerializer dom_serializer(webview()->GetMainFrame(),
2725 true,
2726 this,
2727 links,
2728 local_paths,
2729 local_directory_name);
2730 dom_serializer.SerializeDom();
2731}
2732
2733void RenderView::DidSerializeDataForFrame(const GURL& frame_url,
2734 const std::string& data, PageSavingSerializationStatus status) {
2735 Send(new ViewHostMsg_SendSerializedHtmlData(routing_id_,
2736 frame_url, data, static_cast<int32>(status)));
2737}
2738
[email protected]04b4a6c2008-08-02 00:44:472739void RenderView::OnMsgShouldClose() {
initial.commit09911bf2008-07-26 23:55:292740 bool should_close = webview()->ShouldClose();
[email protected]04b4a6c2008-08-02 00:44:472741 Send(new ViewHostMsg_ShouldClose_ACK(routing_id_, should_close));
initial.commit09911bf2008-07-26 23:55:292742}
2743
2744void RenderView::OnClosePage(int new_render_process_host_id,
[email protected]04b4a6c2008-08-02 00:44:472745 int new_request_id) {
initial.commit09911bf2008-07-26 23:55:292746 // TODO(creis): We'd rather use webview()->Close() here, but that currently
2747 // sets the WebView's delegate_ to NULL, preventing any JavaScript dialogs
2748 // in the onunload handler from appearing. For now, we're bypassing that and
2749 // calling the FrameLoader's CloseURL method directly. This should be
2750 // revisited to avoid having two ways to close a page. Having a single way
2751 // to close that can run onunload is also useful for fixing
2752 // http://b/issue?id=753080.
2753 WebFrame* main_frame = webview()->GetMainFrame();
2754 if (main_frame)
2755 main_frame->ClosePage();
2756
2757 Send(new ViewHostMsg_ClosePage_ACK(routing_id_,
2758 new_render_process_host_id,
[email protected]04b4a6c2008-08-02 00:44:472759 new_request_id));
initial.commit09911bf2008-07-26 23:55:292760}
2761
2762void RenderView::OnThemeChanged() {
[email protected]6c8afae52009-01-22 02:24:572763#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:292764 gfx::NativeTheme::instance()->CloseHandles();
2765 gfx::Rect view_rect(0, 0, size_.width(), size_.height());
2766 DidInvalidateRect(webwidget_, view_rect);
[email protected]6c8afae52009-01-22 02:24:572767#else // defined(OS_WIN)
2768 // TODO(port): we don't support theming on non-Windows platforms yet
2769 NOTIMPLEMENTED();
2770#endif
initial.commit09911bf2008-07-26 23:55:292771}
2772
[email protected]f386cca792008-08-26 02:02:182773#ifdef CHROME_PERSONALIZATION
[email protected]1cc879642008-08-26 01:27:352774void RenderView::OnPersonalizationEvent(std::string event_name,
2775 std::string event_args) {
2776 Personalization::HandleViewMsgPersonalizationEvent(personalization_,
2777 webview(),
2778 event_name,
2779 event_args);
2780}
[email protected]f386cca792008-08-26 02:02:182781#endif
[email protected]1cc879642008-08-26 01:27:352782
2783void RenderView::TransitionToCommittedForNewPage() {
[email protected]f386cca792008-08-26 02:02:182784#ifdef CHROME_PERSONALIZATION
[email protected]1cc879642008-08-26 01:27:352785 Personalization::HandleTransitionToCommittedForNewPage(personalization_);
[email protected]f386cca792008-08-26 02:02:182786#endif
[email protected]1cc879642008-08-26 01:27:352787}
2788
[email protected]f46aff62008-10-16 07:58:052789void RenderView::DidAddHistoryItem() {
[email protected]f8901082008-10-31 23:34:032790 // We don't want to update the history length for the start page
2791 // navigation.
2792 WebFrame* main_frame = webview()->GetMainFrame();
2793 DCHECK(main_frame != NULL);
2794
2795 WebDataSource* ds = main_frame->GetDataSource();
2796 DCHECK(ds != NULL);
2797
2798 const WebRequest& request = ds->GetRequest();
2799 RenderViewExtraRequestData* extra_data =
2800 static_cast<RenderViewExtraRequestData*>(request.GetExtraData());
2801
2802 if (extra_data && extra_data->transition_type == PageTransition::START_PAGE)
2803 return;
2804
[email protected]f46aff62008-10-16 07:58:052805 history_back_list_count_++;
2806 history_forward_list_count_ = 0;
2807}
2808
[email protected]18cb2572008-08-21 20:34:452809void RenderView::OnMessageFromExternalHost(
2810 const std::string& target, const std::string& message) {
[email protected]3ac14a052008-08-15 21:22:152811 if (message.empty())
2812 return;
2813
2814 WebFrame* main_frame = webview()->GetMainFrame();
2815 if (!main_frame)
2816 return;
2817
2818 std::string script = "javascript:";
2819 script += target;
2820 script += "(";
2821 script += "'";
2822 script += message;
2823 script += "'";
2824 script += ");void(0);";
2825
2826 GURL script_url(script);
2827 scoped_ptr<WebRequest> request(WebRequest::Create(script_url));
2828 // TODO(iyengar)
2829 // Need a mechanism to send results back.
2830 main_frame->LoadRequest(request.get());
2831}
2832
[email protected]0aa55312008-10-17 21:53:082833void RenderView::OnDisassociateFromPopupCount() {
2834 if (decrement_shared_popup_at_destruction_)
2835 shared_popup_counter_->data--;
2836 shared_popup_counter_ = new SharedRenderViewCounter(0);
2837 decrement_shared_popup_at_destruction_ = false;
2838}
2839
initial.commit09911bf2008-07-26 23:55:292840std::string RenderView::GetAltHTMLForTemplate(
2841 const DictionaryValue& error_strings, int template_resource_id) const {
2842 const StringPiece template_html(
2843 ResourceBundle::GetSharedInstance().GetRawDataResource(
2844 template_resource_id));
2845
2846 if (template_html.empty()) {
2847 NOTREACHED() << "unable to load template. ID: " << template_resource_id;
2848 return "";
2849 }
[email protected]be645db2009-02-06 20:36:332850#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:292851 // "t" is the id of the templates root node.
2852 return jstemplate_builder::GetTemplateHtml(
2853 template_html, &error_strings, "t");
[email protected]be645db2009-02-06 20:36:332854#else
2855 // TODO(port)
2856 NOTIMPLEMENTED();
2857 return std::string();
2858#endif // OS_WIN
initial.commit09911bf2008-07-26 23:55:292859}