blob: 03c0fdd9c18c14403df5d371351297b9bfeca2c1 [file] [log] [blame]
[email protected]61d68ef12011-01-13 14:02:561// Copyright (c) 2011 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// 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
[email protected]0dd3a0ab2011-02-18 08:17:445#include "content/browser/tab_contents/tab_contents.h"
[email protected]39526562011-02-05 03:41:516
[email protected]b75b8292010-10-01 07:28:257#include <cmath>
8
[email protected]36fb2c7c2011-04-04 15:49:089#include "base/command_line.h"
[email protected]835d7c82010-10-14 04:38:3810#include "base/metrics/histogram.h"
[email protected]724159a2010-12-30 01:11:1811#include "base/metrics/stats_counters.h"
[email protected]96d185d2009-04-24 03:28:5412#include "base/string16.h"
[email protected]996fd702009-09-04 19:12:3713#include "base/string_util.h"
[email protected]96d185d2009-04-24 03:28:5414#include "base/time.h"
[email protected]3c733bde2010-12-21 19:56:3115#include "base/utf_string_conversions.h"
[email protected]3d7474ff2011-07-27 17:47:3716#include "content/browser/browser_context.h"
[email protected]567812d2011-02-24 17:40:5017#include "content/browser/child_process_security_policy.h"
[email protected]b46442d7e2011-06-29 02:16:0618#include "content/browser/debugger/devtools_manager.h"
[email protected]71bf3f5e2011-08-15 21:05:2219#include "content/browser/download/download_manager.h"
[email protected]8bd9e562011-08-16 23:55:4620#include "content/browser/download/download_stats.h"
[email protected]567812d2011-02-24 17:40:5021#include "content/browser/host_zoom_map.h"
22#include "content/browser/in_process_webkit/session_storage_namespace.h"
[email protected]37a72af2011-06-13 05:42:0123#include "content/browser/load_from_memory_cache_details.h"
[email protected]35e251d2011-05-24 21:01:0424#include "content/browser/load_notification_details.h"
[email protected]567812d2011-02-24 17:40:5025#include "content/browser/renderer_host/render_process_host.h"
26#include "content/browser/renderer_host/render_view_host.h"
27#include "content/browser/renderer_host/render_widget_host_view.h"
[email protected]686493142011-07-15 21:47:2228#include "content/browser/renderer_host/resource_dispatcher_host.h"
[email protected]567812d2011-02-24 17:40:5029#include "content/browser/renderer_host/resource_request_details.h"
30#include "content/browser/site_instance.h"
[email protected]0dd3a0ab2011-02-18 08:17:4431#include "content/browser/tab_contents/interstitial_page.h"
[email protected]8286f51a2011-05-31 17:39:1332#include "content/browser/tab_contents/navigation_details.h"
[email protected]0dd3a0ab2011-02-18 08:17:4433#include "content/browser/tab_contents/navigation_entry.h"
34#include "content/browser/tab_contents/provisional_load_details.h"
35#include "content/browser/tab_contents/tab_contents_delegate.h"
36#include "content/browser/tab_contents/tab_contents_observer.h"
37#include "content/browser/tab_contents/tab_contents_view.h"
[email protected]105bb0f2011-05-24 17:12:1438#include "content/browser/tab_contents/title_updated_details.h"
[email protected]afd1e522011-04-27 23:29:5939#include "content/browser/user_metrics.h"
[email protected]1fd1a502011-03-30 16:55:5640#include "content/browser/webui/web_ui_factory.h"
[email protected]ce9751942011-09-21 01:57:2441#include "content/common/intents_messages.h"
[email protected]2c5569662011-03-22 20:45:0242#include "content/common/view_messages.h"
[email protected]87f3c082011-10-19 18:07:4443#include "content/public/browser/content_browser_client.h"
[email protected]610cbb82011-10-18 16:35:0844#include "content/public/browser/navigation_types.h"
[email protected]54087fe2011-10-28 22:02:4845#include "content/public/browser/notification_service.h"
[email protected]e091df82011-10-11 18:13:2146#include "content/public/common/bindings_policy.h"
[email protected]54087fe2011-10-28 22:02:4847#include "content/public/common/content_constants.h"
[email protected]4573fbd2011-10-31 20:25:1848#include "content/public/common/content_restriction.h"
[email protected]a1d29162011-10-14 17:14:0349#include "content/public/common/url_constants.h"
[email protected]d686e812009-06-03 19:10:2950#include "net/base/net_util.h"
[email protected]abe2c032011-03-31 18:49:3451#include "net/url_request/url_request_context_getter.h"
[email protected]8bd0fe62011-01-17 06:44:3752#include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h"
[email protected]08397d52011-02-05 01:53:3853#include "ui/gfx/codec/png_codec.h"
[email protected]d5e311b2010-07-20 17:15:0354#include "webkit/glue/password_form.h"
[email protected]ce9751942011-09-21 01:57:2455#include "webkit/glue/web_intent_data.h"
[email protected]3c733bde2010-12-21 19:56:3156#include "webkit/glue/webpreferences.h"
57
58#if defined(OS_MACOSX)
[email protected]b9b751f22011-03-25 14:04:1259#include "ui/gfx/surface/io_surface_support_mac.h"
[email protected]3c733bde2010-12-21 19:56:3160#endif // defined(OS_MACOSX)
[email protected]3e45ba92009-02-20 21:09:0061
[email protected]420ae012009-04-24 05:16:3262// Cross-Site Navigations
63//
64// If a TabContents is told to navigate to a different web site (as determined
65// by SiteInstance), it will replace its current RenderViewHost with a new
66// RenderViewHost dedicated to the new SiteInstance. This works as follows:
67//
68// - Navigate determines whether the destination is cross-site, and if so,
[email protected]a2750082011-09-01 12:29:4669// it creates a pending_render_view_host_.
[email protected]420ae012009-04-24 05:16:3270// - The pending RVH is "suspended," so that no navigation messages are sent to
71// its renderer until the onbeforeunload JavaScript handler has a chance to
72// run in the current RVH.
73// - The pending RVH tells CrossSiteRequestManager (a thread-safe singleton)
74// that it has a pending cross-site request. ResourceDispatcherHost will
75// check for this when the response arrives.
76// - The current RVH runs its onbeforeunload handler. If it returns false, we
[email protected]a2750082011-09-01 12:29:4677// cancel all the pending logic. Otherwise we allow the pending RVH to send
78// the navigation request to its renderer.
79// - ResourceDispatcherHost receives a ResourceRequest on the IO thread for the
80// main resource load on the pending RVH. It checks CrossSiteRequestManager
81// to see that it is a cross-site request, and installs a
82// CrossSiteResourceHandler.
83// - When RDH receives a response, the BufferedResourceHandler determines
84// whether it is a download. If so, it sends a message to the new renderer
85// causing it to cancel the request, and the download proceeds. For now, the
86// pending RVH remains until the next DidNavigate event for this TabContents.
87// This isn't ideal, but it doesn't affect any functionality.
[email protected]420ae012009-04-24 05:16:3288// - After RDH receives a response and determines that it is safe and not a
89// download, it pauses the response to first run the old page's onunload
90// handler. It does this by asynchronously calling the OnCrossSiteResponse
[email protected]992db4c2011-05-12 15:37:1591// method of TabContents on the UI thread, which sends a SwapOut message
[email protected]420ae012009-04-24 05:16:3292// to the current RVH.
[email protected]992db4c2011-05-12 15:37:1593// - Once the onunload handler is finished, a SwapOut_ACK message is sent to
[email protected]420ae012009-04-24 05:16:3294// the ResourceDispatcherHost, who unpauses the response. Data is then sent
95// to the pending RVH.
96// - The pending renderer sends a FrameNavigate message that invokes the
97// DidNavigate method. This replaces the current RVH with the
[email protected]a2750082011-09-01 12:29:4698// pending RVH.
[email protected]992db4c2011-05-12 15:37:1599// - The previous renderer is kept swapped out in RenderViewHostManager in case
100// the user goes back. The process only stays live if another tab is using
101// it, but if so, the existing frame relationships will be maintained.
[email protected]420ae012009-04-24 05:16:32102
103namespace {
104
105// Amount of time we wait between when a key event is received and the renderer
106// is queried for its state and pushed to the NavigationEntry.
107const int kQueryStateDelay = 5000;
108
[email protected]6ebdc9b2010-09-27 16:55:57109const int kSyncWaitDelay = 40;
110
[email protected]ca406032011-07-19 21:53:05111static const char kDotGoogleDotCom[] = ".google.com";
112
[email protected]420ae012009-04-24 05:16:32113#if defined(OS_WIN)
114
115BOOL CALLBACK InvalidateWindow(HWND hwnd, LPARAM lparam) {
116 // Note: erase is required to properly paint some widgets borders. This can
117 // be seen with textfields.
118 InvalidateRect(hwnd, NULL, TRUE);
119 return TRUE;
120}
121#endif
122
[email protected]2c5569662011-03-22 20:45:02123ViewMsg_Navigate_Type::Value GetNavigationType(
[email protected]3d7474ff2011-07-27 17:47:37124 content::BrowserContext* browser_context, const NavigationEntry& entry,
[email protected]1ccb3568d2010-02-19 10:51:16125 NavigationController::ReloadType reload_type) {
126 switch (reload_type) {
127 case NavigationController::RELOAD:
[email protected]2c5569662011-03-22 20:45:02128 return ViewMsg_Navigate_Type::RELOAD;
[email protected]1ccb3568d2010-02-19 10:51:16129 case NavigationController::RELOAD_IGNORING_CACHE:
[email protected]2c5569662011-03-22 20:45:02130 return ViewMsg_Navigate_Type::RELOAD_IGNORING_CACHE;
[email protected]1ccb3568d2010-02-19 10:51:16131 case NavigationController::NO_RELOAD:
132 break; // Fall through to rest of function.
133 }
[email protected]5e369672009-11-03 23:48:30134
135 if (entry.restore_type() == NavigationEntry::RESTORE_LAST_SESSION &&
[email protected]3d7474ff2011-07-27 17:47:37136 browser_context->DidLastSessionExitCleanly())
[email protected]2c5569662011-03-22 20:45:02137 return ViewMsg_Navigate_Type::RESTORE;
[email protected]5e369672009-11-03 23:48:30138
[email protected]2c5569662011-03-22 20:45:02139 return ViewMsg_Navigate_Type::NORMAL;
[email protected]5e369672009-11-03 23:48:30140}
141
[email protected]876bc832010-09-07 16:29:54142void MakeNavigateParams(const NavigationEntry& entry,
143 const NavigationController& controller,
[email protected]6c6b02d2011-09-02 03:36:47144 TabContentsDelegate* delegate,
[email protected]1ccb3568d2010-02-19 10:51:16145 NavigationController::ReloadType reload_type,
146 ViewMsg_Navigate_Params* params) {
[email protected]056de2d2009-06-26 16:41:34147 params->page_id = entry.page_id();
[email protected]876bc832010-09-07 16:29:54148 params->pending_history_list_offset = controller.GetIndexOfEntry(&entry);
[email protected]3cc72b12010-03-18 23:03:00149 params->current_history_list_offset = controller.last_committed_entry_index();
150 params->current_history_list_length = controller.entry_count();
[email protected]056de2d2009-06-26 16:41:34151 params->url = entry.url();
152 params->referrer = entry.referrer();
153 params->transition = entry.transition_type();
154 params->state = entry.content_state();
[email protected]3cc72b12010-03-18 23:03:00155 params->navigation_type =
[email protected]3d7474ff2011-07-27 17:47:37156 GetNavigationType(controller.browser_context(), entry, reload_type);
[email protected]056de2d2009-06-26 16:41:34157 params->request_time = base::Time::Now();
[email protected]6c6b02d2011-09-02 03:36:47158 params->extra_headers = entry.extra_headers();
159
160 if (delegate)
161 delegate->AddNavigationHeaders(params->url, &params->extra_headers);
[email protected]056de2d2009-06-26 16:41:34162}
163
[email protected]420ae012009-04-24 05:16:32164} // namespace
165
[email protected]f4f50ef2011-01-21 19:01:19166
167// TabContents ----------------------------------------------------------------
[email protected]420ae012009-04-24 05:16:32168
[email protected]3d7474ff2011-07-27 17:47:37169TabContents::TabContents(content::BrowserContext* browser_context,
[email protected]420ae012009-04-24 05:16:32170 SiteInstance* site_instance,
171 int routing_id,
[email protected]6ee12c42010-09-14 09:36:07172 const TabContents* base_tab_contents,
173 SessionStorageNamespace* session_storage_namespace)
[email protected]b680ad22009-04-15 23:19:42174 : delegate_(NULL),
[email protected]6ee12c42010-09-14 09:36:07175 ALLOW_THIS_IN_INITIALIZER_LIST(controller_(
[email protected]3d7474ff2011-07-27 17:47:37176 this, browser_context, session_storage_namespace)),
[email protected]66ba4932009-06-04 19:22:13177 ALLOW_THIS_IN_INITIALIZER_LIST(view_(
[email protected]74313b42011-08-24 16:51:32178 content::GetContentClient()->browser()->CreateTabContentsView(this))),
[email protected]d82ed61e2009-06-16 02:46:22179 ALLOW_THIS_IN_INITIALIZER_LIST(render_manager_(this, this)),
[email protected]d5f942ba2008-09-26 19:30:34180 is_loading_(false),
[email protected]443b80e2010-12-14 00:42:23181 crashed_status_(base::TERMINATION_STATUS_STILL_RUNNING),
182 crashed_error_code_(0),
[email protected]d5f942ba2008-09-26 19:30:34183 waiting_for_response_(false),
[email protected]d5f942ba2008-09-26 19:30:34184 max_page_id_(-1),
[email protected]9c235f042011-08-10 22:28:21185 load_state_(net::LOAD_STATE_IDLE, string16()),
[email protected]094e5b22009-09-25 04:23:56186 upload_size_(0),
187 upload_position_(0),
[email protected]f17a0ee2010-05-17 17:38:47188 displayed_insecure_content_(false),
[email protected]fdd61c62009-04-22 19:22:57189 capturing_contents_(false),
190 is_being_destroyed_(false),
191 notify_disconnection_(false),
[email protected]2e5b90c2011-08-16 21:11:55192 dialog_creator_(NULL),
[email protected]fdd61c62009-04-22 19:22:57193#if defined(OS_WIN)
194 message_box_active_(CreateEvent(NULL, TRUE, FALSE, NULL)),
195#endif
[email protected]7ab1e7d62009-10-14 23:32:01196 is_showing_before_unload_dialog_(false),
[email protected]1fd1a502011-03-30 16:55:56197 opener_web_ui_type_(WebUI::kNoWebUI),
[email protected]ebf40a72010-07-22 01:46:38198 closed_by_user_gesture_(false),
[email protected]b75b8292010-10-01 07:28:25199 minimum_zoom_percent_(
200 static_cast<int>(WebKit::WebView::minTextSizeMultiplier * 100)),
201 maximum_zoom_percent_(
202 static_cast<int>(WebKit::WebView::maxTextSizeMultiplier * 100)),
[email protected]9e823662010-10-13 23:36:00203 temporary_zoom_settings_(false),
[email protected]32ded2212011-11-10 18:51:43204 content_restrictions_(0),
205 view_type_(content::VIEW_TYPE_TAB_CONTENTS) {
[email protected]28b3a812011-11-08 01:03:43206
[email protected]3d7474ff2011-07-27 17:47:37207 render_manager_.Init(browser_context, site_instance, routing_id);
[email protected]420ae012009-04-24 05:16:32208
[email protected]34ac70502009-09-25 17:07:23209 // We have the initial size of the view be based on the size of the passed in
210 // tab contents (normally a tab from the same window).
211 view_->CreateView(base_tab_contents ?
212 base_tab_contents->view()->GetContainerSize() : gfx::Size());
[email protected]483623eb2011-10-25 09:30:00213
214#if defined(ENABLE_JAVA_BRIDGE)
215 java_bridge_dispatcher_host_manager_.reset(
216 new JavaBridgeDispatcherHostManager(this));
217#endif
[email protected]332af7732009-03-11 13:21:35218}
initial.commit09911bf2008-07-26 23:55:29219
220TabContents::~TabContents() {
[email protected]420ae012009-04-24 05:16:32221 is_being_destroyed_ = true;
222
[email protected]3ab9cb82011-06-03 18:02:07223 // Clear out any JavaScript state.
[email protected]2e5b90c2011-08-16 21:11:55224 if (dialog_creator_)
225 dialog_creator_->ResetJavaScriptState(this);
[email protected]3ab9cb82011-06-03 18:02:07226
[email protected]420ae012009-04-24 05:16:32227 NotifyDisconnected();
[email protected]420ae012009-04-24 05:16:32228
[email protected]420ae012009-04-24 05:16:32229 // Notify any observer that have a reference on this tab contents.
[email protected]ad50def52011-10-19 23:17:07230 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:27231 content::NOTIFICATION_TAB_CONTENTS_DESTROYED,
[email protected]6c2381d2011-10-19 02:52:53232 content::Source<TabContents>(this),
[email protected]ad50def52011-10-19 23:17:07233 content::NotificationService::NoDetails());
[email protected]420ae012009-04-24 05:16:32234
235 // TODO(brettw) this should be moved to the view.
236#if defined(OS_WIN)
237 // If we still have a window handle, destroy it. GetNativeView can return
238 // NULL if this contents was part of a window that closed.
[email protected]50bd6452010-11-27 19:39:42239 if (GetNativeView()) {
240 RenderViewHost* host = render_view_host();
[email protected]b9a1fb42011-04-12 23:16:44241 if (host && host->view())
[email protected]50bd6452010-11-27 19:39:42242 host->view()->WillWmDestroy();
[email protected]50bd6452010-11-27 19:39:42243 }
[email protected]420ae012009-04-24 05:16:32244#endif
[email protected]7ab1e7d62009-10-14 23:32:01245
246 // OnCloseStarted isn't called in unit tests.
247 if (!tab_close_start_time_.is_null()) {
248 UMA_HISTOGRAM_TIMES("Tab.Close",
249 base::TimeTicks::Now() - tab_close_start_time_);
250 }
[email protected]b5a1d11c2011-02-17 03:09:42251
[email protected]07d490bc2011-03-07 17:05:26252 FOR_EACH_OBSERVER(TabContentsObserver, observers_, TabContentsDestroyed());
[email protected]232a5812011-03-04 22:42:08253
[email protected]1de2b8b2011-06-29 19:38:46254 set_delegate(NULL);
255}
256
257// TODO(cbentzel): Either remove the debugging code, or rename to SetDelegate.
258void TabContents::set_delegate(TabContentsDelegate* delegate) {
259 if (delegate == delegate_)
260 return;
261 if (delegate_)
262 delegate_->Detach(this);
263 delegate_ = delegate;
264 if (delegate_)
265 delegate_->Attach(this);
[email protected]b5a1d11c2011-02-17 03:09:42266}
267
[email protected]724159a2010-12-30 01:11:18268bool TabContents::OnMessageReceived(const IPC::Message& message) {
[email protected]f82d57b52011-04-27 19:13:17269 if (web_ui() && web_ui()->OnMessageReceived(message))
270 return true;
271
[email protected]585b30362011-01-28 02:30:17272 ObserverListBase<TabContentsObserver>::Iterator it(observers_);
273 TabContentsObserver* observer;
[email protected]0f180ee2011-01-21 18:23:10274 while ((observer = it.GetNext()) != NULL)
275 if (observer->OnMessageReceived(message))
[email protected]403415a2011-01-10 18:57:53276 return true;
[email protected]403415a2011-01-10 18:57:53277
[email protected]724159a2010-12-30 01:11:18278 bool handled = true;
279 bool message_is_ok = true;
280 IPC_BEGIN_MESSAGE_MAP_EX(TabContents, message, message_is_ok)
[email protected]63c239322011-10-31 23:56:30281 IPC_MESSAGE_HANDLER(IntentsHostMsg_RegisterIntentHandler,
282 OnRegisterIntentHandler)
[email protected]ce9751942011-09-21 01:57:24283 IPC_MESSAGE_HANDLER(IntentsHostMsg_WebIntentDispatch,
284 OnWebIntentDispatch)
[email protected]724159a2010-12-30 01:11:18285 IPC_MESSAGE_HANDLER(ViewHostMsg_DidStartProvisionalLoadForFrame,
286 OnDidStartProvisionalLoadForFrame)
287 IPC_MESSAGE_HANDLER(ViewHostMsg_DidRedirectProvisionalLoad,
288 OnDidRedirectProvisionalLoad)
289 IPC_MESSAGE_HANDLER(ViewHostMsg_DidFailProvisionalLoadWithError,
290 OnDidFailProvisionalLoadWithError)
291 IPC_MESSAGE_HANDLER(ViewHostMsg_DidLoadResourceFromMemoryCache,
292 OnDidLoadResourceFromMemoryCache)
293 IPC_MESSAGE_HANDLER(ViewHostMsg_DidDisplayInsecureContent,
294 OnDidDisplayInsecureContent)
295 IPC_MESSAGE_HANDLER(ViewHostMsg_DidRunInsecureContent,
296 OnDidRunInsecureContent)
297 IPC_MESSAGE_HANDLER(ViewHostMsg_DocumentLoadedInFrame,
298 OnDocumentLoadedInFrame)
299 IPC_MESSAGE_HANDLER(ViewHostMsg_DidFinishLoad, OnDidFinishLoad)
[email protected]c8f73ab2011-01-22 00:05:17300 IPC_MESSAGE_HANDLER(ViewHostMsg_UpdateContentRestrictions,
301 OnUpdateContentRestrictions)
[email protected]7d472472011-01-22 01:30:25302 IPC_MESSAGE_HANDLER(ViewHostMsg_GoToEntryAtOffset, OnGoToEntryAtOffset)
[email protected]216813952011-05-19 22:21:26303 IPC_MESSAGE_HANDLER(ViewHostMsg_UpdateZoomLimits, OnUpdateZoomLimits)
304 IPC_MESSAGE_HANDLER(ViewHostMsg_FocusedNodeChanged, OnFocusedNodeChanged)
[email protected]c7dd2f62011-07-18 15:57:59305 IPC_MESSAGE_HANDLER(ViewHostMsg_SaveURLAs, OnSaveURL)
[email protected]3a29a6e2011-08-24 18:26:21306 IPC_MESSAGE_HANDLER(ViewHostMsg_EnumerateDirectory, OnEnumerateDirectory)
[email protected]7d189022011-08-25 22:54:20307 IPC_MESSAGE_HANDLER(ViewHostMsg_JSOutOfMemory, OnJSOutOfMemory)
308 IPC_MESSAGE_HANDLER(ViewHostMsg_RegisterProtocolHandler,
309 OnRegisterProtocolHandler)
[email protected]b888919c2011-09-02 00:32:16310 IPC_MESSAGE_HANDLER(ViewHostMsg_Find_Reply, OnFindReply)
[email protected]d952a052011-09-06 18:42:45311 IPC_MESSAGE_HANDLER(ViewHostMsg_CrashedPlugin, OnCrashedPlugin)
[email protected]7fc4bbb2011-09-08 21:23:10312 IPC_MESSAGE_HANDLER(ViewHostMsg_AppCacheAccessed, OnAppCacheAccessed)
[email protected]724159a2010-12-30 01:11:18313 IPC_MESSAGE_UNHANDLED(handled = false)
314 IPC_END_MESSAGE_MAP_EX()
315
316 if (!message_is_ok) {
317 UserMetrics::RecordAction(UserMetricsAction("BadMessageTerminate_RVD"));
318 GetRenderProcessHost()->ReceivedBadMessage();
319 }
320
321 return handled;
322}
323
[email protected]6c2e472f2011-08-24 23:26:18324void TabContents::RunFileChooser(
325 RenderViewHost* render_view_host,
326 const ViewHostMsg_RunFileChooser_Params& params) {
327 delegate()->RunFileChooser(this, params);
328}
329
[email protected]8cb5d5b2010-02-09 11:36:16330RenderProcessHost* TabContents::GetRenderProcessHost() const {
[email protected]4d0df262011-10-03 20:05:03331 if (render_manager_.current_host())
332 return render_manager_.current_host()->process();
333 else
334 return NULL;
[email protected]8cb5d5b2010-02-09 11:36:16335}
336
[email protected]d5f942ba2008-09-26 19:30:34337const GURL& TabContents::GetURL() const {
338 // We may not have a navigation entry yet
[email protected]ce3fa3c2009-04-20 19:55:57339 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]ebe89e062009-08-13 23:16:54340 return entry ? entry->virtual_url() : GURL::EmptyGURL();
[email protected]d5f942ba2008-09-26 19:30:34341}
342
[email protected]96d185d2009-04-24 03:28:54343const string16& TabContents::GetTitle() const {
[email protected]4c6092c5b2009-06-06 00:23:55344 // Transient entries take precedence. They are used for interstitial pages
345 // that are shown on top of existing pages.
346 NavigationEntry* entry = controller_.GetTransientEntry();
[email protected]b5cca982011-05-26 04:42:08347 std::string accept_languages =
348 content::GetContentClient()->browser()->GetAcceptLangs(this);
[email protected]45d0ef7f2011-01-05 13:46:23349 if (entry) {
[email protected]b5cca982011-05-26 04:42:08350 return entry->GetTitleForDisplay(accept_languages);
[email protected]45d0ef7f2011-01-05 13:46:23351 }
[email protected]7ade2732011-02-10 00:13:58352 WebUI* our_web_ui = render_manager_.pending_web_ui() ?
353 render_manager_.pending_web_ui() : render_manager_.web_ui();
354 if (our_web_ui) {
[email protected]96d185d2009-04-24 03:28:54355 // Don't override the title in view source mode.
[email protected]4c6092c5b2009-06-06 00:23:55356 entry = controller_.GetActiveEntry();
[email protected]96d185d2009-04-24 03:28:54357 if (!(entry && entry->IsViewSourceMode())) {
[email protected]e0112912011-02-02 22:54:35358 // Give the Web UI the chance to override our title.
[email protected]7ade2732011-02-10 00:13:58359 const string16& title = our_web_ui->overridden_title();
[email protected]96d185d2009-04-24 03:28:54360 if (!title.empty())
361 return title;
362 }
363 }
364
365 // We use the title for the last committed entry rather than a pending
366 // navigation entry. For example, when the user types in a URL, we want to
367 // keep the old page's title until the new load has committed and we get a new
368 // title.
[email protected]96d185d2009-04-24 03:28:54369 entry = controller_.GetLastCommittedEntry();
[email protected]45d0ef7f2011-01-05 13:46:23370 if (entry) {
[email protected]b5cca982011-05-26 04:42:08371 return entry->GetTitleForDisplay(accept_languages);
[email protected]45d0ef7f2011-01-05 13:46:23372 }
[email protected]987fc3a2011-05-26 14:18:09373
374 // |page_title_when_no_navigation_entry_| is finally used
375 // if no title cannot be retrieved.
376 return page_title_when_no_navigation_entry_;
[email protected]96d185d2009-04-24 03:28:54377}
378
[email protected]d5f942ba2008-09-26 19:30:34379int32 TabContents::GetMaxPageID() {
380 if (GetSiteInstance())
381 return GetSiteInstance()->max_page_id();
382 else
383 return max_page_id_;
384}
385
386void TabContents::UpdateMaxPageID(int32 page_id) {
387 // Ensure both the SiteInstance and RenderProcessHost update their max page
[email protected]57c6a652009-05-04 07:58:34388 // IDs in sync. Only TabContents will also have site instances, except during
[email protected]d5f942ba2008-09-26 19:30:34389 // testing.
390 if (GetSiteInstance())
391 GetSiteInstance()->UpdateMaxPageID(page_id);
[email protected]8cb5d5b2010-02-09 11:36:16392 GetRenderProcessHost()->UpdateMaxPageID(page_id);
[email protected]d5f942ba2008-09-26 19:30:34393}
394
[email protected]96d185d2009-04-24 03:28:54395SiteInstance* TabContents::GetSiteInstance() const {
396 return render_manager_.current_host()->site_instance();
397}
398
[email protected]77362eb2011-08-01 17:18:38399SiteInstance* TabContents::GetPendingSiteInstance() const {
400 RenderViewHost* dest_rvh = render_manager_.pending_render_view_host() ?
401 render_manager_.pending_render_view_host() :
402 render_manager_.current_host();
403 return dest_rvh->site_instance();
404}
405
[email protected]585b30362011-01-28 02:30:17406void TabContents::AddObserver(TabContentsObserver* observer) {
407 observers_.AddObserver(observer);
[email protected]3c9e1872010-11-18 16:17:49408}
409
[email protected]585b30362011-01-28 02:30:17410void TabContents::RemoveObserver(TabContentsObserver* observer) {
411 observers_.RemoveObserver(observer);
[email protected]3c9e1872010-11-18 16:17:49412}
413
[email protected]443b80e2010-12-14 00:42:23414void TabContents::SetIsCrashed(base::TerminationStatus status, int error_code) {
415 if (status == crashed_status_)
[email protected]d5f942ba2008-09-26 19:30:34416 return;
417
[email protected]443b80e2010-12-14 00:42:23418 crashed_status_ = status;
419 crashed_error_code_ = error_code;
[email protected]c9cd2222009-05-06 05:16:50420 NotifyNavigationStateChanged(INVALIDATE_TAB);
[email protected]d5f942ba2008-09-26 19:30:34421}
422
423void TabContents::NotifyNavigationStateChanged(unsigned changed_flags) {
424 if (delegate_)
425 delegate_->NavigationStateChanged(this, changed_flags);
426}
427
[email protected]96d185d2009-04-24 03:28:54428void TabContents::DidBecomeSelected() {
429 controller_.SetActive(true);
[email protected]8cb5d5b2010-02-09 11:36:16430 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
431 if (rwhv) {
432 rwhv->DidBecomeSelected();
[email protected]789e9152009-08-04 21:59:43433#if defined(OS_MACOSX)
[email protected]8cb5d5b2010-02-09 11:36:16434 rwhv->SetActive(true);
[email protected]789e9152009-08-04 21:59:43435#endif
436 }
[email protected]96d185d2009-04-24 03:28:54437
[email protected]5ac20162010-11-24 23:33:11438 last_selected_time_ = base::TimeTicks::Now();
[email protected]3e69bc82011-05-26 23:22:38439
440 FOR_EACH_OBSERVER(TabContentsObserver, observers_, DidBecomeSelected());
[email protected]96d185d2009-04-24 03:28:54441}
442
443void TabContents::WasHidden() {
444 if (!capturing_contents()) {
445 // |render_view_host()| can be NULL if the user middle clicks a link to open
446 // a tab in then background, then closes the tab before selecting it. This
447 // is because closing the tab calls TabContents::Destroy(), which removes
448 // the |render_view_host()|; then when we actually destroy the window,
449 // OnWindowPosChanged() notices and calls HideContents() (which calls us).
[email protected]8cb5d5b2010-02-09 11:36:16450 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
451 if (rwhv)
452 rwhv->WasHidden();
[email protected]96d185d2009-04-24 03:28:54453 }
454
[email protected]ad50def52011-10-19 23:17:07455 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:27456 content::NOTIFICATION_TAB_CONTENTS_HIDDEN,
[email protected]6c2381d2011-10-19 02:52:53457 content::Source<TabContents>(this),
[email protected]ad50def52011-10-19 23:17:07458 content::NotificationService::NoDetails());
[email protected]96d185d2009-04-24 03:28:54459}
460
[email protected]d5f942ba2008-09-26 19:30:34461void TabContents::Activate() {
462 if (delegate_)
463 delegate_->ActivateContents(this);
464}
465
[email protected]ea42e7782010-08-23 23:58:12466void TabContents::Deactivate() {
467 if (delegate_)
468 delegate_->DeactivateContents(this);
469}
470
[email protected]63954792011-07-11 04:17:48471void TabContents::LostCapture() {
472 if (delegate_)
473 delegate_->LostCapture();
474}
475
476bool TabContents::PreHandleKeyboardEvent(const NativeWebKeyboardEvent& event,
477 bool* is_keyboard_shortcut) {
478 return delegate_ &&
479 delegate_->PreHandleKeyboardEvent(event, is_keyboard_shortcut);
480}
481
482void TabContents::HandleKeyboardEvent(const NativeWebKeyboardEvent& event) {
483 if (delegate_)
484 delegate_->HandleKeyboardEvent(event);
485}
486
[email protected]32ded2212011-11-10 18:51:43487void TabContents::HandleMouseDown() {
488 if (delegate_)
489 delegate_->HandleMouseDown();
490}
491
[email protected]63954792011-07-11 04:17:48492void TabContents::HandleMouseUp() {
493 if (delegate_)
494 delegate_->HandleMouseUp();
495}
496
497void TabContents::HandleMouseActivate() {
498 if (delegate_)
499 delegate_->HandleMouseActivate();
500}
501
[email protected]8a5e0ca2011-08-25 06:30:47502void TabContents::ToggleFullscreenMode(bool enter_fullscreen) {
503 if (delegate_)
504 delegate_->ToggleFullscreenModeForTab(this, enter_fullscreen);
505}
506
[email protected]5d5f7af2011-10-01 01:38:12507bool TabContents::IsFullscreenForCurrentTab() const {
508 return delegate_ ? delegate_->IsFullscreenForTab(this) : false;
509}
510
[email protected]e9621112011-10-17 05:38:37511void TabContents::RequestToLockMouse() {
512 if (delegate_) {
513 delegate_->RequestToLockMouse(this);
514 } else {
515 GotResponseToLockMouseRequest(false);
516 }
517}
518
519void TabContents::LostMouseLock() {
520 if (delegate_)
521 delegate_->LostMouseLock();
522}
523
[email protected]0548c5352011-09-07 00:33:33524void TabContents::UpdatePreferredSize(const gfx::Size& pref_size) {
525 if (delegate_)
526 delegate_->UpdatePreferredSize(this, pref_size);
527}
528
[email protected]32ded2212011-11-10 18:51:43529void TabContents::WebUISend(RenderViewHost* render_view_host,
530 const GURL& source_url,
531 const std::string& name,
532 const base::ListValue& args) {
533 if (delegate_)
534 delegate_->WebUISend(this, source_url, name, args);
535}
536
[email protected]96d185d2009-04-24 03:28:54537void TabContents::ShowContents() {
[email protected]8cb5d5b2010-02-09 11:36:16538 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
539 if (rwhv)
540 rwhv->DidBecomeSelected();
[email protected]96d185d2009-04-24 03:28:54541}
542
543void TabContents::HideContents() {
544 // TODO(pkasting): http://b/1239839 Right now we purposefully don't call
545 // our superclass HideContents(), because some callers want to be very picky
546 // about the order in which these get called. In addition to making the code
547 // here practically impossible to understand, this also means we end up
548 // calling TabContents::WasHidden() twice if callers call both versions of
[email protected]57c6a652009-05-04 07:58:34549 // HideContents() on a TabContents.
[email protected]96d185d2009-04-24 03:28:54550 WasHidden();
551}
552
[email protected]5aab5e22010-12-08 22:13:29553bool TabContents::NeedToFireBeforeUnload() {
554 // TODO(creis): Should we fire even for interstitial pages?
555 return notify_disconnection() &&
556 !showing_interstitial_page() &&
557 !render_view_host()->SuddenTerminationAllowed();
558}
559
[email protected]00c37fc2011-08-02 00:22:50560// TODO(adriansc): Remove this method once refactoring changed all call sites.
[email protected]992e4542011-07-20 23:09:25561TabContents* TabContents::OpenURL(const GURL& url,
562 const GURL& referrer,
563 WindowOpenDisposition disposition,
[email protected]2905f742011-10-13 03:51:58564 content::PageTransition transition) {
[email protected]e47ae9472011-10-13 19:48:34565 return OpenURL(OpenURLParams(url, referrer, disposition, transition,
566 false));
[email protected]00c37fc2011-08-02 00:22:50567}
568
569TabContents* TabContents::OpenURL(const OpenURLParams& params) {
[email protected]1c642b52011-04-15 09:05:49570 if (delegate_) {
[email protected]00c37fc2011-08-02 00:22:50571 TabContents* new_contents = delegate_->OpenURLFromTab(this, params);
[email protected]1c642b52011-04-15 09:05:49572 // Notify observers.
573 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
[email protected]00c37fc2011-08-02 00:22:50574 DidOpenURL(params.url, params.referrer,
575 params.disposition, params.transition));
[email protected]992e4542011-07-20 23:09:25576 return new_contents;
[email protected]1c642b52011-04-15 09:05:49577 }
[email protected]992e4542011-07-20 23:09:25578 return NULL;
[email protected]d5f942ba2008-09-26 19:30:34579}
580
[email protected]1ccb3568d2010-02-19 10:51:16581bool TabContents::NavigateToPendingEntry(
582 NavigationController::ReloadType reload_type) {
[email protected]dd11de52011-11-03 22:54:27583 return NavigateToEntry(*controller_.pending_entry(), reload_type);
[email protected]876bc832010-09-07 16:29:54584}
[email protected]96d185d2009-04-24 03:28:54585
[email protected]876bc832010-09-07 16:29:54586bool TabContents::NavigateToEntry(
[email protected]dd11de52011-11-03 22:54:27587 const NavigationEntry& entry,
[email protected]876bc832010-09-07 16:29:54588 NavigationController::ReloadType reload_type) {
[email protected]19e81142011-10-03 16:19:40589 // The renderer will reject IPC messages with URLs longer than
590 // this limit, so don't attempt to navigate with a longer URL.
[email protected]dd11de52011-11-03 22:54:27591 if (entry.url().spec().size() > content::kMaxURLChars)
[email protected]19e81142011-10-03 16:19:40592 return false;
593
[email protected]dd11de52011-11-03 22:54:27594 RenderViewHost* dest_render_view_host = render_manager_.Navigate(entry);
[email protected]ffc906f2011-10-04 22:55:40595 if (!dest_render_view_host)
596 return false; // Unable to create the desired render view host.
597
[email protected]80a8fad2011-01-29 04:02:38598 // For security, we should never send non-Web-UI URLs to a Web UI renderer.
[email protected]05fdd492010-11-15 17:52:07599 // Double check that here.
[email protected]54ec6472010-04-09 19:39:58600 int enabled_bindings = dest_render_view_host->enabled_bindings();
[email protected]1fd1a502011-03-30 16:55:56601 bool is_allowed_in_web_ui_renderer = content::GetContentClient()->
[email protected]3d7474ff2011-07-27 17:47:37602 browser()->GetWebUIFactory()->IsURLAcceptableForWebUI(browser_context(),
[email protected]dd11de52011-11-03 22:54:27603 entry.url());
[email protected]32ded2212011-11-10 18:51:43604#if defined(OS_CHROMEOS)
605 is_allowed_in_web_ui_renderer |= entry.url().SchemeIs(chrome::kDataScheme);
606#endif
[email protected]e091df82011-10-11 18:13:21607 CHECK(!(enabled_bindings & content::BINDINGS_POLICY_WEB_UI) ||
[email protected]c09163a2011-02-15 00:05:55608 is_allowed_in_web_ui_renderer);
[email protected]54ec6472010-04-09 19:39:58609
[email protected]96d185d2009-04-24 03:28:54610 // Tell DevTools agent that it is attached prior to the navigation.
[email protected]d9f9b792009-06-24 13:17:12611 DevToolsManager* devtools_manager = DevToolsManager::GetInstance();
[email protected]7e8e6b62009-05-08 11:28:32612 if (devtools_manager) { // NULL in unit tests.
[email protected]3cc72b12010-03-18 23:03:00613 devtools_manager->OnNavigatingToPendingEntry(render_view_host(),
614 dest_render_view_host,
[email protected]dd11de52011-11-03 22:54:27615 entry.url());
[email protected]7e8e6b62009-05-08 11:28:32616 }
[email protected]96d185d2009-04-24 03:28:54617
618 // Used for page load time metrics.
619 current_load_start_ = base::TimeTicks::Now();
620
621 // Navigate in the desired RenderViewHost.
[email protected]056de2d2009-06-26 16:41:34622 ViewMsg_Navigate_Params navigate_params;
[email protected]dd11de52011-11-03 22:54:27623 MakeNavigateParams(entry, controller_, delegate_, reload_type,
[email protected]6c6b02d2011-09-02 03:36:47624 &navigate_params);
[email protected]056de2d2009-06-26 16:41:34625 dest_render_view_host->Navigate(navigate_params);
[email protected]96d185d2009-04-24 03:28:54626
[email protected]dd11de52011-11-03 22:54:27627 if (entry.page_id() == -1) {
[email protected]96d185d2009-04-24 03:28:54628 // HACK!! This code suppresses javascript: URLs from being added to
629 // session history, which is what we want to do for javascript: URLs that
630 // do not generate content. What we really need is a message from the
631 // renderer telling us that a new page was not created. The same message
632 // could be used for mailto: URLs and the like.
[email protected]dd11de52011-11-03 22:54:27633 if (entry.url().SchemeIs(chrome::kJavaScriptScheme))
[email protected]96d185d2009-04-24 03:28:54634 return false;
635 }
636
[email protected]3c9e1872010-11-18 16:17:49637 // Notify observers about navigation.
[email protected]b375c5d2011-05-03 21:15:04638 FOR_EACH_OBSERVER(TabContentsObserver,
639 observers_,
[email protected]dd11de52011-11-03 22:54:27640 NavigateToPendingEntry(entry.url(), reload_type));
[email protected]96d185d2009-04-24 03:28:54641
[email protected]09b29342011-06-24 19:18:48642 if (delegate_)
643 delegate_->DidNavigateToPendingEntry(this);
644
[email protected]d5f942ba2008-09-26 19:30:34645 return true;
646}
647
[email protected]9e1ad4b2011-08-14 16:49:19648void TabContents::SetHistoryLengthAndPrune(const SiteInstance* site_instance,
649 int history_length,
650 int32 minimum_page_id) {
[email protected]5229253b2011-08-16 14:36:41651 // SetHistoryLengthAndPrune doesn't work when there are pending cross-site
652 // navigations. Callers should ensure that this is the case.
[email protected]9e1ad4b2011-08-14 16:49:19653 if (render_manager_.pending_render_view_host()) {
654 NOTREACHED();
[email protected]796931a92011-08-10 01:32:14655 return;
[email protected]9e1ad4b2011-08-14 16:49:19656 }
657 RenderViewHost* rvh = render_view_host();
658 if (!rvh) {
659 NOTREACHED();
660 return;
661 }
662 if (site_instance && rvh->site_instance() != site_instance) {
663 NOTREACHED();
664 return;
665 }
666 rvh->Send(new ViewMsg_SetHistoryLengthAndPrune(rvh->routing_id(),
667 history_length,
668 minimum_page_id));
[email protected]796931a92011-08-10 01:32:14669}
670
[email protected]96d185d2009-04-24 03:28:54671void TabContents::Stop() {
672 render_manager_.Stop();
[email protected]2e3f4572011-03-25 19:24:47673 FOR_EACH_OBSERVER(TabContentsObserver, observers_, StopNavigation());
[email protected]96d185d2009-04-24 03:28:54674}
675
[email protected]96d185d2009-04-24 03:28:54676TabContents* TabContents::Clone() {
677 // We create a new SiteInstance so that the new tab won't share processes
678 // with the old one. This can be changed in the future if we need it to share
679 // processes for some reason.
[email protected]3d7474ff2011-07-27 17:47:37680 TabContents* tc = new TabContents(
681 browser_context(),
682 SiteInstance::CreateSiteInstance(browser_context()),
683 MSG_ROUTING_NONE, this, NULL);
[email protected]96d185d2009-04-24 03:28:54684 tc->controller().CopyStateFrom(controller_);
685 return tc;
686}
687
[email protected]4d677202009-07-19 07:37:12688void TabContents::ShowPageInfo(const GURL& url,
689 const NavigationEntry::SSLStatus& ssl,
690 bool show_history) {
691 if (!delegate_)
692 return;
693
[email protected]3d7474ff2011-07-27 17:47:37694 delegate_->ShowPageInfo(browser_context(), url, ssl, show_history);
[email protected]4d677202009-07-19 07:37:12695}
696
[email protected]e7cfdbd2011-04-22 14:41:37697void TabContents::AddNewContents(TabContents* new_contents,
698 WindowOpenDisposition disposition,
699 const gfx::Rect& initial_pos,
700 bool user_gesture) {
initial.commit09911bf2008-07-26 23:55:29701 if (!delegate_)
702 return;
703
[email protected]e7cfdbd2011-04-22 14:41:37704 delegate_->AddNewContents(this, new_contents, disposition, initial_pos,
705 user_gesture);
[email protected]96d185d2009-04-24 03:28:54706}
707
[email protected]5c9e97a2009-09-09 23:48:30708gfx::NativeView TabContents::GetContentNativeView() const {
[email protected]96d185d2009-04-24 03:28:54709 return view_->GetContentNativeView();
710}
711
712gfx::NativeView TabContents::GetNativeView() const {
713 return view_->GetNativeView();
714}
715
716void TabContents::GetContainerBounds(gfx::Rect *out) const {
717 view_->GetContainerBounds(out);
718}
719
720void TabContents::Focus() {
721 view_->Focus();
722}
723
[email protected]90daadb42009-06-08 21:27:28724void TabContents::FocusThroughTabTraversal(bool reverse) {
[email protected]7e383692009-06-12 19:14:54725 if (showing_interstitial_page()) {
726 render_manager_.interstitial_page()->FocusThroughTabTraversal(reverse);
727 return;
728 }
[email protected]96d185d2009-04-24 03:28:54729 render_view_host()->SetInitialFocus(reverse);
730}
731
732bool TabContents::FocusLocationBarByDefault() {
[email protected]d0980792011-02-13 19:41:40733 WebUI* web_ui = GetWebUIForCurrentState();
[email protected]7ade2732011-02-10 00:13:58734 if (web_ui)
735 return web_ui->focus_location_bar_by_default();
[email protected]3e3f0eb2009-06-22 18:33:43736 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]e0d481582009-09-15 21:06:25737 if (entry && entry->url() == GURL(chrome::kAboutBlankURL))
[email protected]3e3f0eb2009-06-22 18:33:43738 return true;
[email protected]96d185d2009-04-24 03:28:54739 return false;
740}
741
[email protected]a26dc362010-04-23 01:48:58742void TabContents::SetFocusToLocationBar(bool select_all) {
[email protected]a11aaf22010-03-30 00:03:38743 if (delegate())
[email protected]a26dc362010-04-23 01:48:58744 delegate()->SetFocusToLocationBar(select_all);
[email protected]a11aaf22010-03-30 00:03:38745}
746
[email protected]686493142011-07-15 21:47:22747bool TabContents::CanDownload(int request_id) {
748 TabContentsDelegate* d = delegate();
749 if (d)
750 return d->CanDownload(this, request_id);
751 return true;
752}
753
754void TabContents::OnStartDownload(DownloadItem* download) {
755 TabContentsDelegate* d = delegate();
756 if (d)
757 d->OnStartDownload(this, download);
758}
759
[email protected]c7dd2f62011-07-18 15:57:59760void TabContents::OnSavePage() {
761 // If we can not save the page, try to download it.
762 if (!SavePackage::IsSavableContents(contents_mime_type())) {
[email protected]3d7474ff2011-07-27 17:47:37763 DownloadManager* dlm = browser_context()->GetDownloadManager();
[email protected]c7dd2f62011-07-18 15:57:59764 const GURL& current_page_url = GetURL();
765 if (dlm && current_page_url.is_valid()) {
766 dlm->DownloadUrl(current_page_url, GURL(), "", this);
[email protected]8bd9e562011-08-16 23:55:46767 download_stats::RecordDownloadCount(
768 download_stats::INITIATED_BY_SAVE_PACKAGE_FAILURE_COUNT);
[email protected]c7dd2f62011-07-18 15:57:59769 return;
770 }
771 }
772
773 Stop();
774
775 // Create the save package and possibly prompt the user for the name to save
776 // the page as. The user prompt is an asynchronous operation that runs on
777 // another thread.
778 save_package_ = new SavePackage(this);
779 save_package_->GetSaveInfo();
780}
781
782// Used in automated testing to bypass prompting the user for file names.
783// Instead, the names and paths are hard coded rather than running them through
784// file name sanitation and extension / mime checking.
785bool TabContents::SavePage(const FilePath& main_file, const FilePath& dir_path,
786 SavePackage::SavePackageType save_type) {
787 // Stop the page from navigating.
788 Stop();
789
790 save_package_ = new SavePackage(this, save_type, main_file, dir_path);
791 return save_package_->Init();
792}
793
794void TabContents::OnSaveURL(const GURL& url) {
[email protected]3d7474ff2011-07-27 17:47:37795 DownloadManager* dlm = browser_context()->GetDownloadManager();
[email protected]c7dd2f62011-07-18 15:57:59796 dlm->DownloadUrl(url, GetURL(), "", this);
797}
798
[email protected]420ae012009-04-24 05:16:32799bool TabContents::IsActiveEntry(int32 page_id) {
800 NavigationEntry* active_entry = controller_.GetActiveEntry();
801 return (active_entry != NULL &&
802 active_entry->site_instance() == GetSiteInstance() &&
803 active_entry->page_id() == page_id);
804}
805
[email protected]b2fe07d12010-02-09 14:38:08806void TabContents::SetOverrideEncoding(const std::string& encoding) {
[email protected]8cb5d5b2010-02-09 11:36:16807 set_encoding(encoding);
[email protected]216813952011-05-19 22:21:26808 render_view_host()->Send(new ViewMsg_SetPageEncoding(
809 render_view_host()->routing_id(), encoding));
[email protected]8cb5d5b2010-02-09 11:36:16810}
811
[email protected]b2fe07d12010-02-09 14:38:08812void TabContents::ResetOverrideEncoding() {
[email protected]8cb5d5b2010-02-09 11:36:16813 reset_encoding();
[email protected]216813952011-05-19 22:21:26814 render_view_host()->Send(new ViewMsg_ResetPageEncodingToDefault(
815 render_view_host()->routing_id()));
[email protected]8cb5d5b2010-02-09 11:36:16816}
817
[email protected]7ab1e7d62009-10-14 23:32:01818void TabContents::OnCloseStarted() {
819 if (tab_close_start_time_.is_null())
820 tab_close_start_time_ = base::TimeTicks::Now();
821}
822
[email protected]46624bf2010-06-09 16:04:19823bool TabContents::ShouldAcceptDragAndDrop() const {
824#if defined(OS_CHROMEOS)
825 // ChromeOS panels (pop-ups) do not take drag-n-drop.
826 // See http://crosbug.com/2413
[email protected]b35b26b32011-05-05 20:35:14827 if (delegate() && delegate()->IsPopupOrPanel(this))
[email protected]b83af492010-10-09 03:21:20828 return false;
829 return true;
[email protected]46624bf2010-06-09 16:04:19830#else
831 return true;
832#endif
833}
834
[email protected]7813bd72011-02-05 02:19:34835void TabContents::SystemDragEnded() {
836 if (render_view_host())
837 render_view_host()->DragSourceSystemDragEnded();
838 if (delegate())
839 delegate()->DragEnded();
840}
841
[email protected]d0b8d092010-10-25 04:05:17842double TabContents::GetZoomLevel() const {
[email protected]3d7474ff2011-07-27 17:47:37843 HostZoomMap* zoom_map = browser_context()->GetHostZoomMap();
[email protected]b75b8292010-10-01 07:28:25844 if (!zoom_map)
[email protected]d0b8d092010-10-25 04:05:17845 return 0;
[email protected]b75b8292010-10-01 07:28:25846
847 double zoom_level;
848 if (temporary_zoom_settings_) {
849 zoom_level = zoom_map->GetTemporaryZoomLevel(
850 GetRenderProcessHost()->id(), render_view_host()->routing_id());
851 } else {
[email protected]2ae88d12011-10-14 09:11:19852 GURL url;
853 NavigationEntry* active_entry = controller().GetActiveEntry();
854 // Since zoom map is updated using rewritten URL, use rewritten URL
855 // to get the zoom level.
856 url = active_entry ? active_entry->url() : GURL::EmptyGURL();
857 zoom_level = zoom_map->GetZoomLevel(net::GetHostOrSpecFromURL(url));
[email protected]b75b8292010-10-01 07:28:25858 }
[email protected]d0b8d092010-10-25 04:05:17859 return zoom_level;
860}
[email protected]b75b8292010-10-01 07:28:25861
[email protected]d0b8d092010-10-25 04:05:17862int TabContents::GetZoomPercent(bool* enable_increment,
863 bool* enable_decrement) {
864 *enable_decrement = *enable_increment = false;
[email protected]b75b8292010-10-01 07:28:25865 int percent = static_cast<int>(
[email protected]d0b8d092010-10-25 04:05:17866 WebKit::WebView::zoomLevelToZoomFactor(GetZoomLevel()) * 100);
[email protected]b75b8292010-10-01 07:28:25867 *enable_decrement = percent > minimum_zoom_percent_;
868 *enable_increment = percent < maximum_zoom_percent_;
869 return percent;
870}
871
[email protected]3c733bde2010-12-21 19:56:31872void TabContents::ViewSource() {
[email protected]1788e772010-12-15 16:40:50873 if (!delegate_)
874 return;
875
876 NavigationEntry* active_entry = controller().GetActiveEntry();
877 if (!active_entry)
878 return;
879
880 delegate_->ViewSourceForTab(this, active_entry->url());
[email protected]77d8d622010-12-15 10:30:12881}
882
[email protected]932b7a12011-03-09 12:50:27883void TabContents::ViewFrameSource(const GURL& url,
884 const std::string& content_state) {
885 if (!delegate_)
886 return;
887
888 delegate_->ViewSourceForFrame(this, url, content_state);
889}
890
[email protected]c40d6232011-03-25 00:16:21891void TabContents::SetContentRestrictions(int restrictions) {
892 content_restrictions_ = restrictions;
893 delegate()->ContentRestrictionsChanged(this);
894}
895
[email protected]63c239322011-10-31 23:56:30896void TabContents::OnRegisterIntentHandler(const string16& action,
897 const string16& type,
898 const string16& href,
899 const string16& title,
900 const string16& disposition) {
901 delegate()->RegisterIntentHandler(
902 this, action, type, href, title, disposition);
903}
904
905void TabContents::OnWebIntentDispatch(const IPC::Message& message,
906 const webkit_glue::WebIntentData& intent,
907 int intent_id) {
908 delegate()->WebIntentDispatch(this, message.routing_id(), intent, intent_id);
909}
910
[email protected]724159a2010-12-30 01:11:18911void TabContents::OnDidStartProvisionalLoadForFrame(int64 frame_id,
912 bool is_main_frame,
[email protected]57b9396c2011-10-07 19:11:59913 const GURL& opener_url,
[email protected]724159a2010-12-30 01:11:18914 const GURL& url) {
915 bool is_error_page = (url.spec() == chrome::kUnreachableWebDataURL);
916 GURL validated_url(url);
917 render_view_host()->FilterURL(ChildProcessSecurityPolicy::GetInstance(),
918 GetRenderProcessHost()->id(), &validated_url);
919
[email protected]8093a542011-05-13 07:29:32920 RenderViewHost* rvh =
921 render_manager_.pending_render_view_host() ?
922 render_manager_.pending_render_view_host() : render_view_host();
[email protected]0d60f0192011-04-14 12:40:10923 // Notify observers about the start of the provisional load.
924 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
925 DidStartProvisionalLoadForFrame(frame_id, is_main_frame,
[email protected]8093a542011-05-13 07:29:32926 validated_url, is_error_page, rvh));
[email protected]0d60f0192011-04-14 12:40:10927
[email protected]724159a2010-12-30 01:11:18928 if (is_main_frame) {
[email protected]4850a7f2011-03-08 23:36:59929 // Notify observers about the provisional change in the main frame URL.
930 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
[email protected]57b9396c2011-10-07 19:11:59931 ProvisionalChangeToMainFrameUrl(url, opener_url));
[email protected]724159a2010-12-30 01:11:18932 }
933}
934
935void TabContents::OnDidRedirectProvisionalLoad(int32 page_id,
[email protected]57b9396c2011-10-07 19:11:59936 const GURL& opener_url,
[email protected]724159a2010-12-30 01:11:18937 const GURL& source_url,
938 const GURL& target_url) {
[email protected]18ba2b12011-04-06 16:35:49939 // TODO(creis): Remove this method and have the pre-rendering code listen to
940 // the ResourceDispatcherHost's RESOURCE_RECEIVED_REDIRECT notification
941 // instead. See http://crbug.com/78512.
[email protected]724159a2010-12-30 01:11:18942 NavigationEntry* entry;
943 if (page_id == -1)
944 entry = controller_.pending_entry();
945 else
946 entry = controller_.GetEntryWithPageID(GetSiteInstance(), page_id);
947 if (!entry || entry->url() != source_url)
948 return;
[email protected]e7d50892011-01-19 21:47:38949
[email protected]4850a7f2011-03-08 23:36:59950 // Notify observers about the provisional change in the main frame URL.
951 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
[email protected]eacb080b2011-05-22 19:40:26952 ProvisionalChangeToMainFrameUrl(target_url,
[email protected]57b9396c2011-10-07 19:11:59953 opener_url));
[email protected]724159a2010-12-30 01:11:18954}
955
956void TabContents::OnDidFailProvisionalLoadWithError(
[email protected]d7b175e2011-10-11 15:31:58957 const ViewHostMsg_DidFailProvisionalLoadWithError_Params& params) {
958 VLOG(1) << "Failed Provisional Load: " << params.url.possibly_invalid_spec()
959 << ", error_code: " << params.error_code
960 << ", error_description: " << params.error_description
961 << ", is_main_frame: " << params.is_main_frame
962 << ", showing_repost_interstitial: " <<
963 params.showing_repost_interstitial
964 << ", frame_id: " << params.frame_id;
965 GURL validated_url(params.url);
[email protected]724159a2010-12-30 01:11:18966 render_view_host()->FilterURL(ChildProcessSecurityPolicy::GetInstance(),
967 GetRenderProcessHost()->id(), &validated_url);
968
[email protected]d7b175e2011-10-11 15:31:58969 if (net::ERR_ABORTED == params.error_code) {
[email protected]724159a2010-12-30 01:11:18970 // EVIL HACK ALERT! Ignore failed loads when we're showing interstitials.
971 // This means that the interstitial won't be torn down properly, which is
972 // bad. But if we have an interstitial, go back to another tab type, and
973 // then load the same interstitial again, we could end up getting the first
974 // interstitial's "failed" message (as a result of the cancel) when we're on
975 // the second one.
976 //
977 // We can't tell this apart, so we think we're tearing down the current page
978 // which will cause a crash later one. There is also some code in
979 // RenderViewHostManager::RendererAbortedProvisionalLoad that is commented
980 // out because of this problem.
981 //
982 // http://code.google.com/p/chromium/issues/detail?id=2855
983 // Because this will not tear down the interstitial properly, if "back" is
984 // back to another tab type, the interstitial will still be somewhat alive
985 // in the previous tab type. If you navigate somewhere that activates the
986 // tab with the interstitial again, you'll see a flash before the new load
987 // commits of the interstitial page.
988 if (showing_interstitial_page()) {
989 LOG(WARNING) << "Discarding message during interstitial.";
990 return;
991 }
992
[email protected]02102f82011-06-13 20:37:02993 // Discard our pending entry if the load canceled (e.g. if we decided to
994 // download the file instead of load it). We do not verify that the URL
995 // being canceled matches the pending entry's URL because they will not
996 // match if a redirect occurred (in which case we do not want to leave a
997 // stale redirect URL showing). This means that we also cancel the pending
998 // entry if the user started a new navigation. As a result, the navigation
999 // controller may not remember that a load is in progress, but the
1000 // navigation will still commit even if there is no pending entry.
[email protected]724159a2010-12-30 01:11:181001 NavigationEntry* pending_entry = controller_.pending_entry();
[email protected]02102f82011-06-13 20:37:021002 if (pending_entry)
[email protected]c95fa8b2011-04-28 20:26:161003 DidCancelLoading();
[email protected]724159a2010-12-30 01:11:181004
1005 render_manager_.RendererAbortedProvisionalLoad(render_view_host());
1006 }
1007
1008 // Send out a notification that we failed a provisional load with an error.
1009 ProvisionalLoadDetails details(
[email protected]d7b175e2011-10-11 15:31:581010 params.is_main_frame,
1011 controller_.IsURLInPageNavigation(validated_url),
1012 validated_url,
1013 std::string(),
1014 false,
1015 params.frame_id);
1016 details.set_error_code(params.error_code);
[email protected]724159a2010-12-30 01:11:181017
[email protected]ad50def52011-10-19 23:17:071018 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271019 content::NOTIFICATION_FAIL_PROVISIONAL_LOAD_WITH_ERROR,
[email protected]6c2381d2011-10-19 02:52:531020 content::Source<NavigationController>(&controller_),
1021 content::Details<ProvisionalLoadDetails>(&details));
[email protected]0d60f0192011-04-14 12:40:101022
[email protected]d7b175e2011-10-11 15:31:581023 FOR_EACH_OBSERVER(TabContentsObserver,
1024 observers_,
1025 DidFailProvisionalLoad(params.frame_id,
1026 params.is_main_frame,
1027 validated_url,
1028 params.error_code,
1029 params.error_description));
[email protected]724159a2010-12-30 01:11:181030}
1031
1032void TabContents::OnDidLoadResourceFromMemoryCache(
1033 const GURL& url,
[email protected]70435962011-08-02 20:13:281034 const std::string& security_info,
1035 const std::string& http_method,
1036 ResourceType::Type resource_type) {
[email protected]28620862011-03-22 23:07:191037 base::StatsCounter cache("WebKit.CacheHit");
[email protected]724159a2010-12-30 01:11:181038 cache.Increment();
1039
1040 // Send out a notification that we loaded a resource from our memory cache.
[email protected]70d66502011-09-23 00:55:081041 int cert_id = 0;
1042 net::CertStatus cert_status = 0;
1043 int security_bits = -1;
1044 int connection_status = 0;
[email protected]724159a2010-12-30 01:11:181045 SSLManager::DeserializeSecurityInfo(security_info,
1046 &cert_id, &cert_status,
1047 &security_bits,
1048 &connection_status);
[email protected]7a8c55e2011-02-15 08:21:161049 LoadFromMemoryCacheDetails details(url, GetRenderProcessHost()->id(),
1050 cert_id, cert_status);
[email protected]724159a2010-12-30 01:11:181051
[email protected]ad50def52011-10-19 23:17:071052 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271053 content::NOTIFICATION_LOAD_FROM_MEMORY_CACHE,
[email protected]6c2381d2011-10-19 02:52:531054 content::Source<NavigationController>(&controller_),
1055 content::Details<LoadFromMemoryCacheDetails>(&details));
[email protected]724159a2010-12-30 01:11:181056}
1057
1058void TabContents::OnDidDisplayInsecureContent() {
[email protected]221a5d92011-04-28 18:23:161059 UserMetrics::RecordAction(UserMetricsAction("SSL.DisplayedInsecureContent"));
[email protected]724159a2010-12-30 01:11:181060 displayed_insecure_content_ = true;
[email protected]9678d8f2011-08-05 04:05:071061 SSLManager::NotifySSLInternalStateChanged(&controller());
[email protected]724159a2010-12-30 01:11:181062}
1063
1064void TabContents::OnDidRunInsecureContent(
[email protected]92771112011-01-20 00:13:021065 const std::string& security_origin, const GURL& target_url) {
[email protected]9941cf52011-02-08 16:17:231066 LOG(INFO) << security_origin << " ran insecure content from "
1067 << target_url.possibly_invalid_spec();
[email protected]221a5d92011-04-28 18:23:161068 UserMetrics::RecordAction(UserMetricsAction("SSL.RanInsecureContent"));
[email protected]ca406032011-07-19 21:53:051069 if (EndsWith(security_origin, kDotGoogleDotCom, false)) {
1070 UserMetrics::RecordAction(
1071 UserMetricsAction("SSL.RanInsecureContentGoogle"));
1072 }
[email protected]724159a2010-12-30 01:11:181073 controller_.ssl_manager()->DidRunInsecureContent(security_origin);
[email protected]f2b58c4d2011-06-01 23:29:411074 displayed_insecure_content_ = true;
[email protected]9678d8f2011-08-05 04:05:071075 SSLManager::NotifySSLInternalStateChanged(&controller());
[email protected]724159a2010-12-30 01:11:181076}
1077
1078void TabContents::OnDocumentLoadedInFrame(int64 frame_id) {
1079 controller_.DocumentLoadedInFrame();
[email protected]0d60f0192011-04-14 12:40:101080 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
1081 DocumentLoadedInFrame(frame_id));
[email protected]724159a2010-12-30 01:11:181082}
1083
1084void TabContents::OnDidFinishLoad(int64 frame_id) {
[email protected]0d60f0192011-04-14 12:40:101085 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
1086 DidFinishLoad(frame_id));
[email protected]724159a2010-12-30 01:11:181087}
1088
[email protected]c8f73ab2011-01-22 00:05:171089void TabContents::OnUpdateContentRestrictions(int restrictions) {
[email protected]c40d6232011-03-25 00:16:211090 SetContentRestrictions(restrictions);
[email protected]c8f73ab2011-01-22 00:05:171091}
1092
[email protected]216813952011-05-19 22:21:261093void TabContents::OnGoToEntryAtOffset(int offset) {
1094 if (!delegate_ || delegate_->OnGoToEntryOffset(offset)) {
1095 NavigationEntry* entry = controller_.GetEntryAtOffset(offset);
1096 if (!entry)
1097 return;
1098 // Note that we don't call NavigationController::GotToOffset() as we don't
1099 // want to create a pending navigation entry (it might end up lingering
1100 // http://crbug.com/51680).
[email protected]2905f742011-10-13 03:51:581101 entry->set_transition_type(
1102 content::PageTransitionFromInt(
1103 entry->transition_type() |
1104 content::PAGE_TRANSITION_FORWARD_BACK));
[email protected]dd11de52011-11-03 22:54:271105 NavigateToEntry(*entry, NavigationController::NO_RELOAD);
[email protected]a13cc362011-07-28 21:29:571106
1107 // If the entry is being restored and doesn't have a SiteInstance yet, fill
1108 // it in now that we know. This allows us to find the entry when it commits.
1109 if (!entry->site_instance() &&
1110 entry->restore_type() != NavigationEntry::RESTORE_NONE) {
[email protected]77362eb2011-08-01 17:18:381111 entry->set_site_instance(GetPendingSiteInstance());
[email protected]a13cc362011-07-28 21:29:571112 }
[email protected]216813952011-05-19 22:21:261113 }
1114}
1115
1116void TabContents::OnUpdateZoomLimits(int minimum_percent,
1117 int maximum_percent,
1118 bool remember) {
1119 minimum_zoom_percent_ = minimum_percent;
1120 maximum_zoom_percent_ = maximum_percent;
1121 temporary_zoom_settings_ = !remember;
1122}
1123
1124void TabContents::OnFocusedNodeChanged(bool is_editable_node) {
[email protected]ad50def52011-10-19 23:17:071125 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271126 content::NOTIFICATION_FOCUS_CHANGED_IN_PAGE,
[email protected]6c2381d2011-10-19 02:52:531127 content::Source<TabContents>(this),
1128 content::Details<const bool>(&is_editable_node));
[email protected]216813952011-05-19 22:21:261129}
1130
[email protected]3a29a6e2011-08-24 18:26:211131void TabContents::OnEnumerateDirectory(int request_id,
1132 const FilePath& path) {
1133 delegate()->EnumerateDirectory(this, request_id, path);
1134}
1135
[email protected]7d189022011-08-25 22:54:201136void TabContents::OnJSOutOfMemory() {
1137 delegate()->JSOutOfMemory(this);
1138}
1139
1140void TabContents::OnRegisterProtocolHandler(const std::string& protocol,
1141 const GURL& url,
1142 const string16& title) {
1143 delegate()->RegisterProtocolHandler(this, protocol, url, title);
1144}
1145
[email protected]b888919c2011-09-02 00:32:161146void TabContents::OnFindReply(int request_id,
1147 int number_of_matches,
1148 const gfx::Rect& selection_rect,
1149 int active_match_ordinal,
1150 bool final_update) {
1151 delegate()->FindReply(this, request_id, number_of_matches, selection_rect,
1152 active_match_ordinal, final_update);
[email protected]4e6239c2011-09-23 00:11:001153 // Send a notification to the renderer that we are ready to receive more
1154 // results from the scoping effort of the Find operation. The FindInPage
1155 // scoping is asynchronous and periodically sends results back up to the
1156 // browser using IPC. In an effort to not spam the browser we have the
1157 // browser send an ACK for each FindReply message and have the renderer
1158 // queue up the latest status message while waiting for this ACK.
1159 render_view_host()->Send(
1160 new ViewMsg_FindReplyACK(render_view_host()->routing_id()));
[email protected]b888919c2011-09-02 00:32:161161}
1162
[email protected]d952a052011-09-06 18:42:451163void TabContents::OnCrashedPlugin(const FilePath& plugin_path) {
1164 delegate()->CrashedPlugin(this, plugin_path);
1165}
1166
[email protected]7fc4bbb2011-09-08 21:23:101167void TabContents::OnAppCacheAccessed(const GURL& manifest_url,
1168 bool blocked_by_policy) {
1169 // Notify observers about navigation.
1170 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
1171 AppCacheAccessed(manifest_url, blocked_by_policy));
1172}
1173
[email protected]96d185d2009-04-24 03:28:541174// Notifies the RenderWidgetHost instance about the fact that the page is
1175// loading, or done loading and calls the base implementation.
1176void TabContents::SetIsLoading(bool is_loading,
1177 LoadNotificationDetails* details) {
1178 if (is_loading == is_loading_)
1179 return;
1180
1181 if (!is_loading) {
[email protected]9c235f042011-08-10 22:28:211182 load_state_ = net::LoadStateWithParam(net::LOAD_STATE_IDLE, string16());
[email protected]96d185d2009-04-24 03:28:541183 load_state_host_.clear();
[email protected]094e5b22009-09-25 04:23:561184 upload_size_ = 0;
1185 upload_position_ = 0;
[email protected]96d185d2009-04-24 03:28:541186 }
1187
1188 render_manager_.SetIsLoading(is_loading);
1189
1190 is_loading_ = is_loading;
1191 waiting_for_response_ = is_loading;
1192
[email protected]6ebdc9b2010-09-27 16:55:571193 if (delegate_)
1194 delegate_->LoadingStateChanged(this);
[email protected]329581b2009-04-28 06:52:351195 NotifyNavigationStateChanged(INVALIDATE_LOAD);
[email protected]96d185d2009-04-24 03:28:541196
[email protected]432115822011-07-10 15:52:271197 int type = is_loading ? content::NOTIFICATION_LOAD_START :
1198 content::NOTIFICATION_LOAD_STOP;
[email protected]ad50def52011-10-19 23:17:071199 content::NotificationDetails det = content::NotificationService::NoDetails();
[email protected]96d185d2009-04-24 03:28:541200 if (details)
[email protected]6c2381d2011-10-19 02:52:531201 det = content::Details<LoadNotificationDetails>(details);
[email protected]ad50def52011-10-19 23:17:071202 content::NotificationService::current()->Notify(type,
[email protected]6c2381d2011-10-19 02:52:531203 content::Source<NavigationController>(&controller_),
[email protected]96d185d2009-04-24 03:28:541204 det);
1205}
1206
[email protected]d0980792011-02-13 19:41:401207WebUI* TabContents::GetWebUIForCurrentState() {
[email protected]7ade2732011-02-10 00:13:581208 // When there is a pending navigation entry, we want to use the pending WebUI
[email protected]96d185d2009-04-24 03:28:541209 // that goes along with it to control the basic flags. For example, we want to
1210 // show the pending URL in the URL bar, so we want the display_url flag to
1211 // be from the pending entry.
1212 //
1213 // The confusion comes because there are multiple possibilities for the
1214 // initial load in a tab as a side effect of the way the RenderViewHostManager
1215 // works.
1216 //
[email protected]e0112912011-02-02 22:54:351217 // - For the very first tab the load looks "normal". The new tab Web UI is
[email protected]96d185d2009-04-24 03:28:541218 // the pending one, and we want it to apply here.
1219 //
1220 // - For subsequent new tabs, they'll get a new SiteInstance which will then
1221 // get switched to the one previously associated with the new tab pages.
[email protected]7ade2732011-02-10 00:13:581222 // This switching will cause the manager to commit the RVH/WebUI. So we'll
[email protected]e0112912011-02-02 22:54:351223 // have a committed Web UI in this case.
[email protected]96d185d2009-04-24 03:28:541224 //
1225 // This condition handles all of these cases:
1226 //
1227 // - First load in first tab: no committed nav entry + pending nav entry +
1228 // pending dom ui:
[email protected]e0112912011-02-02 22:54:351229 // -> Use pending Web UI if any.
[email protected]96d185d2009-04-24 03:28:541230 //
1231 // - First load in second tab: no committed nav entry + pending nav entry +
[email protected]e0112912011-02-02 22:54:351232 // no pending Web UI:
1233 // -> Use the committed Web UI if any.
[email protected]96d185d2009-04-24 03:28:541234 //
1235 // - Second navigation in any tab: committed nav entry + pending nav entry:
[email protected]e0112912011-02-02 22:54:351236 // -> Use pending Web UI if any.
[email protected]96d185d2009-04-24 03:28:541237 //
1238 // - Normal state with no load: committed nav entry + no pending nav entry:
[email protected]e0112912011-02-02 22:54:351239 // -> Use committed Web UI.
[email protected]96d185d2009-04-24 03:28:541240 if (controller_.pending_entry() &&
1241 (controller_.GetLastCommittedEntry() ||
[email protected]7ade2732011-02-10 00:13:581242 render_manager_.pending_web_ui()))
1243 return render_manager_.pending_web_ui();
1244 return render_manager_.web_ui();
[email protected]96d185d2009-04-24 03:28:541245}
[email protected]420ae012009-04-24 05:16:321246
[email protected]1fd1a502011-03-30 16:55:561247WebUI::TypeID TabContents::GetWebUITypeForCurrentState() {
[email protected]3d7474ff2011-07-27 17:47:371248 return content::WebUIFactory::Get()->GetWebUIType(browser_context(),
1249 GetURL());
[email protected]1fd1a502011-03-30 16:55:561250}
1251
[email protected]420ae012009-04-24 05:16:321252void TabContents::DidNavigateMainFramePostCommit(
[email protected]8286f51a2011-05-31 17:39:131253 const content::LoadCommittedDetails& details,
[email protected]420ae012009-04-24 05:16:321254 const ViewHostMsg_FrameNavigate_Params& params) {
[email protected]1fd1a502011-03-30 16:55:561255 if (opener_web_ui_type_ != WebUI::kNoWebUI) {
[email protected]80a8fad2011-01-29 04:02:381256 // If this is a window.open navigation, use the same WebUI as the renderer
[email protected]c2e74fe82009-09-03 17:57:441257 // that opened the window, as long as both renderers have the same
1258 // privileges.
[email protected]1fd1a502011-03-30 16:55:561259 if (delegate_ && opener_web_ui_type_ == GetWebUITypeForCurrentState()) {
1260 WebUI* web_ui = content::GetContentClient()->browser()->
1261 GetWebUIFactory()->CreateWebUIForURL(this, GetURL());
[email protected]7ade2732011-02-10 00:13:581262 // web_ui might be NULL if the URL refers to a non-existent extension.
1263 if (web_ui) {
[email protected]d0980792011-02-13 19:41:401264 render_manager_.SetWebUIPostCommit(web_ui);
[email protected]7ade2732011-02-10 00:13:581265 web_ui->RenderViewCreated(render_view_host());
[email protected]c2e74fe82009-09-03 17:57:441266 }
1267 }
[email protected]1fd1a502011-03-30 16:55:561268 opener_web_ui_type_ = WebUI::kNoWebUI;
[email protected]c2e74fe82009-09-03 17:57:441269 }
1270
[email protected]4e697b042011-07-08 06:44:561271 if (details.is_navigation_to_different_page()) {
[email protected]420ae012009-04-24 05:16:321272 // Clear the status bubble. This is a workaround for a bug where WebKit
1273 // doesn't let us know that the cursor left an element during a
1274 // transition (this is also why the mouse cursor remains as a hand after
1275 // clicking on a link); see bugs 1184641 and 980803. We don't want to
1276 // clear the bubble when a user navigates to a named anchor in the same
1277 // page.
1278 UpdateTargetURL(details.entry->page_id(), GURL());
[email protected]420ae012009-04-24 05:16:321279 }
1280
[email protected]a6e82fc2010-02-24 22:28:141281 if (!details.is_in_page) {
[email protected]f17a0ee2010-05-17 17:38:471282 // Once the main frame is navigated, we're no longer considered to have
1283 // displayed insecure content.
1284 displayed_insecure_content_ = false;
[email protected]aece2c7f2009-08-27 20:43:171285 }
[email protected]ce5c4502009-05-06 16:46:111286
[email protected]3c9e1872010-11-18 16:17:491287 // Notify observers about navigation.
[email protected]585b30362011-01-28 02:30:171288 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
[email protected]3c9e1872010-11-18 16:17:491289 DidNavigateMainFramePostCommit(details, params));
[email protected]420ae012009-04-24 05:16:321290}
1291
1292void TabContents::DidNavigateAnyFramePostCommit(
1293 RenderViewHost* render_view_host,
[email protected]8286f51a2011-05-31 17:39:131294 const content::LoadCommittedDetails& details,
[email protected]420ae012009-04-24 05:16:321295 const ViewHostMsg_FrameNavigate_Params& params) {
[email protected]3ab9cb82011-06-03 18:02:071296 // If we navigate, reset JavaScript state. This does nothing to prevent
[email protected]420ae012009-04-24 05:16:321297 // a malicious script from spamming messages, since the script could just
1298 // reload the page to stop blocking.
[email protected]2e5b90c2011-08-16 21:11:551299 if (dialog_creator_) {
1300 dialog_creator_->ResetJavaScriptState(this);
1301 dialog_creator_ = NULL;
1302 }
[email protected]420ae012009-04-24 05:16:321303
[email protected]3c9e1872010-11-18 16:17:491304 // Notify observers about navigation.
[email protected]585b30362011-01-28 02:30:171305 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
[email protected]3c9e1872010-11-18 16:17:491306 DidNavigateAnyFramePostCommit(details, params));
[email protected]420ae012009-04-24 05:16:321307}
1308
[email protected]420ae012009-04-24 05:16:321309void TabContents::UpdateMaxPageIDIfNecessary(SiteInstance* site_instance,
1310 RenderViewHost* rvh) {
1311 // If we are creating a RVH for a restored controller, then we might
1312 // have more page IDs than the SiteInstance's current max page ID. We must
1313 // make sure that the max page ID is larger than any restored page ID.
1314 // Note that it is ok for conflicting page IDs to exist in another tab
1315 // (i.e., NavigationController), but if any page ID is larger than the max,
1316 // the back/forward list will get confused.
1317 int max_restored_page_id = controller_.max_restored_page_id();
1318 if (max_restored_page_id > 0) {
1319 int curr_max_page_id = site_instance->max_page_id();
1320 if (max_restored_page_id > curr_max_page_id) {
1321 // Need to update the site instance immediately.
1322 site_instance->UpdateMaxPageID(max_restored_page_id);
1323
1324 // Also tell the renderer to update its internal representation. We
1325 // need to reserve enough IDs to make all restored page IDs less than
1326 // the max.
1327 if (curr_max_page_id < 0)
1328 curr_max_page_id = 0;
[email protected]216813952011-05-19 22:21:261329 rvh->Send(new ViewMsg_ReservePageIDRange(
1330 rvh->routing_id(), max_restored_page_id - curr_max_page_id));
[email protected]420ae012009-04-24 05:16:321331 }
1332 }
1333}
1334
[email protected]6b2f7a82011-04-25 19:30:511335bool TabContents::UpdateTitleForEntry(NavigationEntry* entry,
[email protected]acafd272011-07-26 17:35:571336 const string16& title) {
[email protected]420ae012009-04-24 05:16:321337 // For file URLs without a title, use the pathname instead. In the case of a
1338 // synthesized title, we don't want the update to count toward the "one set
1339 // per page of the title to history."
[email protected]6b2f7a82011-04-25 19:30:511340 string16 final_title;
[email protected]420ae012009-04-24 05:16:321341 bool explicit_set;
[email protected]987fc3a2011-05-26 14:18:091342 if (entry && entry->url().SchemeIsFile() && title.empty()) {
[email protected]6b2f7a82011-04-25 19:30:511343 final_title = UTF8ToUTF16(entry->url().ExtractFileName());
[email protected]420ae012009-04-24 05:16:321344 explicit_set = false; // Don't count synthetic titles toward the set limit.
1345 } else {
[email protected]acafd272011-07-26 17:35:571346 TrimWhitespace(title, TRIM_ALL, &final_title);
[email protected]420ae012009-04-24 05:16:321347 explicit_set = true;
1348 }
1349
[email protected]987fc3a2011-05-26 14:18:091350 // If a page is created via window.open and never navigated,
1351 // there will be no navigation entry. In this situation,
1352 // |page_title_when_no_navigaiton_entry_| will be used for page title.
1353 if (entry) {
1354 if (final_title == entry->title())
1355 return false; // Nothing changed, don't bother.
[email protected]420ae012009-04-24 05:16:321356
[email protected]987fc3a2011-05-26 14:18:091357 entry->set_title(final_title);
1358 } else {
1359 if (page_title_when_no_navigation_entry_ == final_title)
1360 return false; // Nothing changed, don't bother.
1361
1362 page_title_when_no_navigation_entry_ = final_title;
1363 }
[email protected]420ae012009-04-24 05:16:321364
[email protected]420ae012009-04-24 05:16:321365 // Lastly, set the title for the view.
[email protected]790e2fd2011-09-21 20:28:231366 view_->SetPageTitle(final_title);
[email protected]420ae012009-04-24 05:16:321367
[email protected]105bb0f2011-05-24 17:12:141368 TitleUpdatedDetails details(entry, explicit_set);
1369
[email protected]ad50def52011-10-19 23:17:071370 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271371 content::NOTIFICATION_TAB_CONTENTS_TITLE_UPDATED,
[email protected]6c2381d2011-10-19 02:52:531372 content::Source<TabContents>(this),
1373 content::Details<TitleUpdatedDetails>(&details));
[email protected]cbc0e1b2010-04-12 18:33:041374
[email protected]420ae012009-04-24 05:16:321375 return true;
1376}
1377
1378void TabContents::NotifySwapped() {
1379 // After sending out a swap notification, we need to send a disconnect
1380 // notification so that clients that pick up a pointer to |this| can NULL the
1381 // pointer. See Bug 1230284.
1382 notify_disconnection_ = true;
[email protected]ad50def52011-10-19 23:17:071383 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271384 content::NOTIFICATION_TAB_CONTENTS_SWAPPED,
[email protected]6c2381d2011-10-19 02:52:531385 content::Source<TabContents>(this),
[email protected]ad50def52011-10-19 23:17:071386 content::NotificationService::NoDetails());
[email protected]420ae012009-04-24 05:16:321387}
1388
1389void TabContents::NotifyConnected() {
1390 notify_disconnection_ = true;
[email protected]ad50def52011-10-19 23:17:071391 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271392 content::NOTIFICATION_TAB_CONTENTS_CONNECTED,
[email protected]6c2381d2011-10-19 02:52:531393 content::Source<TabContents>(this),
[email protected]ad50def52011-10-19 23:17:071394 content::NotificationService::NoDetails());
[email protected]420ae012009-04-24 05:16:321395}
1396
1397void TabContents::NotifyDisconnected() {
1398 if (!notify_disconnection_)
1399 return;
1400
1401 notify_disconnection_ = false;
[email protected]ad50def52011-10-19 23:17:071402 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271403 content::NOTIFICATION_TAB_CONTENTS_DISCONNECTED,
[email protected]6c2381d2011-10-19 02:52:531404 content::Source<TabContents>(this),
[email protected]ad50def52011-10-19 23:17:071405 content::NotificationService::NoDetails());
[email protected]420ae012009-04-24 05:16:321406}
1407
[email protected]8d3347f2009-07-09 22:00:211408RenderViewHostDelegate::View* TabContents::GetViewDelegate() {
[email protected]420ae012009-04-24 05:16:321409 return view_.get();
1410}
1411
[email protected]8d3347f2009-07-09 22:00:211412RenderViewHostDelegate::RendererManagement*
1413TabContents::GetRendererManagementDelegate() {
1414 return &render_manager_;
1415}
1416
[email protected]daf82f82011-10-31 22:35:311417content::RendererPreferences TabContents::GetRendererPrefs(
[email protected]3d7474ff2011-07-27 17:47:371418 content::BrowserContext* browser_context) const {
[email protected]840b1512009-07-21 16:53:461419 return renderer_preferences_;
[email protected]80d96fa2009-06-10 22:34:511420}
1421
[email protected]57c6a652009-05-04 07:58:341422TabContents* TabContents::GetAsTabContents() {
1423 return this;
1424}
1425
[email protected]da4dfc42011-10-12 15:53:561426content::ViewType TabContents::GetRenderViewType() const {
[email protected]32ded2212011-11-10 18:51:431427 return view_type_;
[email protected]7b291f92009-08-14 05:43:531428}
1429
[email protected]420ae012009-04-24 05:16:321430void TabContents::RenderViewCreated(RenderViewHost* render_view_host) {
[email protected]ad50def52011-10-19 23:17:071431 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271432 content::NOTIFICATION_RENDER_VIEW_HOST_CREATED_FOR_TAB,
[email protected]6c2381d2011-10-19 02:52:531433 content::Source<TabContents>(this),
1434 content::Details<RenderViewHost>(render_view_host));
[email protected]420ae012009-04-24 05:16:321435 NavigationEntry* entry = controller_.GetActiveEntry();
1436 if (!entry)
1437 return;
1438
1439 // When we're creating views, we're still doing initial setup, so we always
[email protected]e0112912011-02-02 22:54:351440 // use the pending Web UI rather than any possibly existing committed one.
[email protected]1fd1a502011-03-30 16:55:561441 if (render_manager_.pending_web_ui())
[email protected]7ade2732011-02-10 00:13:581442 render_manager_.pending_web_ui()->RenderViewCreated(render_view_host);
[email protected]420ae012009-04-24 05:16:321443
1444 if (entry->IsViewSourceMode()) {
1445 // Put the renderer in view source mode.
1446 render_view_host->Send(
1447 new ViewMsg_EnableViewSourceMode(render_view_host->routing_id()));
1448 }
[email protected]0666aef2009-05-13 19:48:081449
1450 view()->RenderViewCreated(render_view_host);
[email protected]11f764bb2011-05-25 19:45:261451
1452 FOR_EACH_OBSERVER(
1453 TabContentsObserver, observers_, RenderViewCreated(render_view_host));
[email protected]420ae012009-04-24 05:16:321454}
1455
1456void TabContents::RenderViewReady(RenderViewHost* rvh) {
1457 if (rvh != render_view_host()) {
1458 // Don't notify the world, since this came from a renderer in the
1459 // background.
1460 return;
1461 }
1462
1463 NotifyConnected();
[email protected]9d8a4642009-07-29 17:25:301464 bool was_crashed = is_crashed();
[email protected]443b80e2010-12-14 00:42:231465 SetIsCrashed(base::TERMINATION_STATUS_STILL_RUNNING, 0);
[email protected]9d8a4642009-07-29 17:25:301466
1467 // Restore the focus to the tab (otherwise the focus will be on the top
1468 // window).
[email protected]484ae5912010-09-29 19:16:141469 if (was_crashed && !FocusLocationBarByDefault() &&
1470 (!delegate_ || delegate_->ShouldFocusPageAfterCrash())) {
[email protected]9d8a4642009-07-29 17:25:301471 Focus();
[email protected]484ae5912010-09-29 19:16:141472 }
[email protected]32ded2212011-11-10 18:51:431473
1474 FOR_EACH_OBSERVER(TabContentsObserver, observers_, RenderViewReady());
[email protected]420ae012009-04-24 05:16:321475}
1476
[email protected]443b80e2010-12-14 00:42:231477void TabContents::RenderViewGone(RenderViewHost* rvh,
1478 base::TerminationStatus status,
1479 int error_code) {
[email protected]420ae012009-04-24 05:16:321480 if (rvh != render_view_host()) {
1481 // The pending page's RenderViewHost is gone.
1482 return;
1483 }
1484
1485 SetIsLoading(false, NULL);
1486 NotifyDisconnected();
[email protected]443b80e2010-12-14 00:42:231487 SetIsCrashed(status, error_code);
[email protected]b3e3f87a2011-10-01 23:37:541488 view()->OnTabCrashed(crashed_status(), crashed_error_code());
[email protected]420ae012009-04-24 05:16:321489
[email protected]55452902011-06-01 21:57:471490 FOR_EACH_OBSERVER(TabContentsObserver, observers_, RenderViewGone());
[email protected]420ae012009-04-24 05:16:321491}
1492
[email protected]2e4633c2009-07-09 16:58:061493void TabContents::RenderViewDeleted(RenderViewHost* rvh) {
[email protected]fb7b79f72009-11-06 18:00:481494 render_manager_.RenderViewDeleted(rvh);
[email protected]483623eb2011-10-25 09:30:001495 FOR_EACH_OBSERVER(TabContentsObserver, observers_, RenderViewDeleted(rvh));
[email protected]2e4633c2009-07-09 16:58:061496}
1497
[email protected]420ae012009-04-24 05:16:321498void TabContents::DidNavigate(RenderViewHost* rvh,
1499 const ViewHostMsg_FrameNavigate_Params& params) {
[email protected]2905f742011-10-13 03:51:581500 if (content::PageTransitionIsMainFrame(params.transition))
[email protected]420ae012009-04-24 05:16:321501 render_manager_.DidNavigateMainFrame(rvh);
[email protected]8030f012009-09-25 18:09:371502
[email protected]420ae012009-04-24 05:16:321503 // Update the site of the SiteInstance if it doesn't have one yet.
1504 if (!GetSiteInstance()->has_site())
1505 GetSiteInstance()->SetSite(params.url);
1506
1507 // Need to update MIME type here because it's referred to in
1508 // UpdateNavigationCommands() called by RendererDidNavigate() to
1509 // determine whether or not to enable the encoding menu.
1510 // It's updated only for the main frame. For a subframe,
1511 // RenderView::UpdateURL does not set params.contents_mime_type.
1512 // (see http://code.google.com/p/chromium/issues/detail?id=2929 )
1513 // TODO(jungshik): Add a test for the encoding menu to avoid
1514 // regressing it again.
[email protected]2905f742011-10-13 03:51:581515 if (content::PageTransitionIsMainFrame(params.transition))
[email protected]420ae012009-04-24 05:16:321516 contents_mime_type_ = params.contents_mime_type;
1517
[email protected]8286f51a2011-05-31 17:39:131518 content::LoadCommittedDetails details;
[email protected]93f230e02011-06-01 14:40:001519 bool did_navigate = controller_.RendererDidNavigate(params, &details);
[email protected]3e90d4a2009-07-03 17:38:391520
[email protected]a9c0bfe2010-09-17 08:35:221521 // Send notification about committed provisional loads. This notification is
1522 // different from the NAV_ENTRY_COMMITTED notification which doesn't include
1523 // the actual URL navigated to and isn't sent for AUTO_SUBFRAME navigations.
[email protected]610cbb82011-10-18 16:35:081524 if (details.type != content::NAVIGATION_TYPE_NAV_IGNORE) {
[email protected]dabb0d12010-10-05 12:50:071525 // For AUTO_SUBFRAME navigations, an event for the main frame is generated
1526 // that is not recorded in the navigation history. For the purpose of
1527 // tracking navigation events, we treat this event as a sub frame navigation
1528 // event.
1529 bool is_main_frame = did_navigate ? details.is_main_frame : false;
[email protected]2905f742011-10-13 03:51:581530 content::PageTransition transition_type = params.transition;
[email protected]a9c0bfe2010-09-17 08:35:221531 // Whether or not a page transition was triggered by going backward or
1532 // forward in the history is only stored in the navigation controller's
1533 // entry list.
1534 if (did_navigate &&
1535 (controller_.GetActiveEntry()->transition_type() &
[email protected]2905f742011-10-13 03:51:581536 content::PAGE_TRANSITION_FORWARD_BACK)) {
1537 transition_type = content::PageTransitionFromInt(
1538 params.transition | content::PAGE_TRANSITION_FORWARD_BACK);
[email protected]a9c0bfe2010-09-17 08:35:221539 }
[email protected]0d60f0192011-04-14 12:40:101540 // Notify observers about the commit of the provisional load.
1541 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
1542 DidCommitProvisionalLoadForFrame(params.frame_id,
1543 is_main_frame, params.url, transition_type));
[email protected]a9c0bfe2010-09-17 08:35:221544 }
1545
[email protected]76543b92009-08-31 17:27:451546 if (!did_navigate)
1547 return; // No navigation happened.
[email protected]420ae012009-04-24 05:16:321548
1549 // DO NOT ADD MORE STUFF TO THIS FUNCTION! Your component should either listen
1550 // for the appropriate notification (best) or you can add it to
1551 // DidNavigateMainFramePostCommit / DidNavigateAnyFramePostCommit (only if
1552 // necessary, please).
1553
1554 // Run post-commit tasks.
[email protected]93f230e02011-06-01 14:40:001555 if (details.is_main_frame) {
[email protected]420ae012009-04-24 05:16:321556 DidNavigateMainFramePostCommit(details, params);
[email protected]09b29342011-06-24 19:18:481557 if (delegate())
1558 delegate()->DidNavigateMainFramePostCommit(this);
[email protected]93f230e02011-06-01 14:40:001559 }
[email protected]420ae012009-04-24 05:16:321560 DidNavigateAnyFramePostCommit(rvh, details, params);
1561}
1562
1563void TabContents::UpdateState(RenderViewHost* rvh,
1564 int32 page_id,
1565 const std::string& state) {
[email protected]992db4c2011-05-12 15:37:151566 // Ensure that this state update comes from either the active RVH or one of
1567 // the swapped out RVHs. We don't expect to hear from any other RVHs.
1568 DCHECK(rvh == render_view_host() || render_manager_.IsSwappedOut(rvh));
[email protected]420ae012009-04-24 05:16:321569
1570 // We must be prepared to handle state updates for any page, these occur
1571 // when the user is scrolling and entering form data, as well as when we're
1572 // leaving a page, in which case our state may have already been moved to
1573 // the next page. The navigation controller will look up the appropriate
1574 // NavigationEntry and update it when it is notified via the delegate.
1575
1576 int entry_index = controller_.GetEntryIndexWithPageID(
[email protected]992db4c2011-05-12 15:37:151577 rvh->site_instance(), page_id);
[email protected]420ae012009-04-24 05:16:321578 if (entry_index < 0)
1579 return;
1580 NavigationEntry* entry = controller_.GetEntryAtIndex(entry_index);
1581
1582 if (state == entry->content_state())
1583 return; // Nothing to update.
1584 entry->set_content_state(state);
1585 controller_.NotifyEntryChanged(entry, entry_index);
1586}
1587
1588void TabContents::UpdateTitle(RenderViewHost* rvh,
[email protected]acafd272011-07-26 17:35:571589 int32 page_id,
[email protected]a49e10b2011-08-01 23:57:461590 const string16& title,
1591 base::i18n::TextDirection title_direction) {
[email protected]420ae012009-04-24 05:16:321592 // If we have a title, that's a pretty good indication that we've started
1593 // getting useful data.
1594 SetNotWaitingForResponse();
1595
1596 DCHECK(rvh == render_view_host());
[email protected]371cc8b2010-09-01 16:03:481597 NavigationEntry* entry = controller_.GetEntryWithPageID(rvh->site_instance(),
[email protected]ec0b6c42010-08-26 03:16:581598 page_id);
[email protected]987fc3a2011-05-26 14:18:091599
[email protected]a49e10b2011-08-01 23:57:461600 // TODO(evan): make use of title_direction.
1601 // http://code.google.com/p/chromium/issues/detail?id=27094
[email protected]987fc3a2011-05-26 14:18:091602 if (!UpdateTitleForEntry(entry, title))
[email protected]420ae012009-04-24 05:16:321603 return;
1604
1605 // Broadcast notifications when the UI should be updated.
1606 if (entry == controller_.GetEntryAtOffset(0))
[email protected]f1cd5e82009-10-23 17:19:031607 NotifyNavigationStateChanged(INVALIDATE_TITLE);
[email protected]420ae012009-04-24 05:16:321608}
1609
[email protected]420ae012009-04-24 05:16:321610void TabContents::UpdateEncoding(RenderViewHost* render_view_host,
[email protected]41fc0322009-09-04 22:23:401611 const std::string& encoding) {
[email protected]420ae012009-04-24 05:16:321612 set_encoding(encoding);
1613}
1614
1615void TabContents::UpdateTargetURL(int32 page_id, const GURL& url) {
1616 if (delegate())
[email protected]d952a052011-09-06 18:42:451617 delegate()->UpdateTargetURL(this, page_id, url);
[email protected]420ae012009-04-24 05:16:321618}
1619
[email protected]420ae012009-04-24 05:16:321620void TabContents::Close(RenderViewHost* rvh) {
[email protected]07707302009-11-06 00:50:291621 // The UI may be in an event-tracking loop, such as between the
1622 // mouse-down and mouse-up in text selection or a button click.
1623 // Defer the close until after tracking is complete, so that we
1624 // don't free objects out from under the UI.
1625 // TODO(shess): This could probably be integrated with the
1626 // IsDoingDrag() test below. Punting for now because I need more
1627 // research to understand how this impacts platforms other than Mac.
1628 // TODO(shess): This could get more fine-grained. For instance,
1629 // closing a tab in another window while selecting text in the
1630 // current window's Omnibox should be just fine.
1631 if (view()->IsEventTracking()) {
1632 view()->CloseTabAfterEventTracking();
1633 return;
1634 }
1635
[email protected]24a4d1062009-07-10 23:10:421636 // If we close the tab while we're in the middle of a drag, we'll crash.
1637 // Instead, cancel the drag and close it as soon as the drag ends.
1638 if (view()->IsDoingDrag()) {
1639 view()->CancelDragAndCloseTab();
1640 return;
1641 }
1642
[email protected]420ae012009-04-24 05:16:321643 // Ignore this if it comes from a RenderViewHost that we aren't showing.
1644 if (delegate() && rvh == render_view_host())
1645 delegate()->CloseContents(this);
1646}
1647
1648void TabContents::RequestMove(const gfx::Rect& new_bounds) {
[email protected]b35b26b32011-05-05 20:35:141649 if (delegate() && delegate()->IsPopupOrPanel(this))
[email protected]420ae012009-04-24 05:16:321650 delegate()->MoveContents(this, new_bounds);
1651}
1652
[email protected]7ab1e7d62009-10-14 23:32:011653void TabContents::DidStartLoading() {
[email protected]420ae012009-04-24 05:16:321654 SetIsLoading(true, NULL);
[email protected]3c9e1872010-11-18 16:17:491655
[email protected]c40d6232011-03-25 00:16:211656 if (delegate() && content_restrictions_) {
1657 content_restrictions_ = 0;
[email protected]de4761b2010-12-07 21:05:211658 delegate()->ContentRestrictionsChanged(this);
[email protected]9e823662010-10-13 23:36:001659 }
[email protected]3c9e1872010-11-18 16:17:491660
1661 // Notify observers about navigation.
[email protected]585b30362011-01-28 02:30:171662 FOR_EACH_OBSERVER(TabContentsObserver, observers_, DidStartLoading());
[email protected]420ae012009-04-24 05:16:321663}
1664
[email protected]7ab1e7d62009-10-14 23:32:011665void TabContents::DidStopLoading() {
[email protected]420ae012009-04-24 05:16:321666 scoped_ptr<LoadNotificationDetails> details;
1667
1668 NavigationEntry* entry = controller_.GetActiveEntry();
1669 // An entry may not exist for a stop when loading an initial blank page or
1670 // if an iframe injected by script into a blank page finishes loading.
1671 if (entry) {
[email protected]420ae012009-04-24 05:16:321672 base::TimeDelta elapsed = base::TimeTicks::Now() - current_load_start_;
1673
1674 details.reset(new LoadNotificationDetails(
[email protected]ebe89e062009-08-13 23:16:541675 entry->virtual_url(),
[email protected]420ae012009-04-24 05:16:321676 entry->transition_type(),
1677 elapsed,
1678 &controller_,
1679 controller_.GetCurrentEntryIndex()));
1680 }
1681
[email protected]420ae012009-04-24 05:16:321682 SetIsLoading(false, details.get());
[email protected]3c9e1872010-11-18 16:17:491683
1684 // Notify observers about navigation.
[email protected]585b30362011-01-28 02:30:171685 FOR_EACH_OBSERVER(TabContentsObserver, observers_, DidStopLoading());
[email protected]420ae012009-04-24 05:16:321686}
1687
[email protected]c95fa8b2011-04-28 20:26:161688void TabContents::DidCancelLoading() {
1689 controller_.DiscardNonCommittedEntries();
1690
1691 // Update the URL display.
1692 NotifyNavigationStateChanged(TabContents::INVALIDATE_URL);
1693}
1694
[email protected]1a3c3cb2010-12-16 21:03:401695void TabContents::DidChangeLoadProgress(double progress) {
1696 if (delegate())
1697 delegate()->LoadProgressChanged(progress);
1698}
1699
[email protected]25497492010-09-11 15:15:081700void TabContents::DocumentOnLoadCompletedInMainFrame(
1701 RenderViewHost* render_view_host,
1702 int32 page_id) {
[email protected]ad50def52011-10-19 23:17:071703 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271704 content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME,
[email protected]6c2381d2011-10-19 02:52:531705 content::Source<TabContents>(this),
1706 content::Details<int>(&page_id));
[email protected]25497492010-09-11 15:15:081707}
1708
[email protected]ae5184d62011-10-06 19:25:581709void TabContents::RequestOpenURL(const GURL& url,
1710 const GURL& referrer,
1711 WindowOpenDisposition disposition,
1712 int64 source_frame_id) {
1713 TabContents* new_contents = NULL;
[email protected]2905f742011-10-13 03:51:581714 content::PageTransition transition_type = content::PAGE_TRANSITION_LINK;
[email protected]7ade2732011-02-10 00:13:581715 if (render_manager_.web_ui()) {
[email protected]e0112912011-02-02 22:54:351716 // When we're a Web UI, it will provide a page transition type for us (this
[email protected]420ae012009-04-24 05:16:321717 // is so the new tab page can specify AUTO_BOOKMARK for automatically
1718 // generated suggestions).
1719 //
[email protected]e0112912011-02-02 22:54:351720 // Note also that we hide the referrer for Web UI pages. We don't really
[email protected]60e448982009-05-06 04:21:161721 // want web sites to see a referrer of "chrome://blah" (and some
1722 // chrome: URLs might have search terms or other stuff we don't want to
[email protected]420ae012009-04-24 05:16:321723 // send to the site), so we send no referrer.
[email protected]ae5184d62011-10-06 19:25:581724 new_contents = OpenURL(url, GURL(), disposition,
[email protected]7ade2732011-02-10 00:13:581725 render_manager_.web_ui()->link_transition_type());
[email protected]ae5184d62011-10-06 19:25:581726 transition_type = render_manager_.web_ui()->link_transition_type();
[email protected]420ae012009-04-24 05:16:321727 } else {
[email protected]e47ae9472011-10-13 19:48:341728 new_contents = OpenURL(OpenURLParams(
1729 url, referrer, disposition, content::PAGE_TRANSITION_LINK,
1730 true /* is_renderer_initiated */));
[email protected]ae5184d62011-10-06 19:25:581731 }
1732 if (new_contents) {
1733 // Notify observers.
1734 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
1735 DidOpenRequestedURL(new_contents,
1736 url,
1737 referrer,
1738 disposition,
1739 transition_type,
1740 source_frame_id));
[email protected]420ae012009-04-24 05:16:321741 }
1742}
1743
[email protected]420ae012009-04-24 05:16:321744void TabContents::RunJavaScriptMessage(
[email protected]992db4c2011-05-12 15:37:151745 const RenderViewHost* rvh,
[email protected]4f5ce842011-05-27 19:34:411746 const string16& message,
1747 const string16& default_prompt,
[email protected]420ae012009-04-24 05:16:321748 const GURL& frame_url,
1749 const int flags,
1750 IPC::Message* reply_msg,
1751 bool* did_suppress_message) {
[email protected]3ab9cb82011-06-03 18:02:071752 // Suppress JavaScript dialogs when requested. Also suppress messages when
1753 // showing an interstitial as it's shown over the previous page and we don't
1754 // want the hidden page's dialogs to interfere with the interstitial.
[email protected]8f55e802010-12-06 18:11:501755 bool suppress_this_message =
[email protected]992db4c2011-05-12 15:37:151756 rvh->is_swapped_out() ||
[email protected]8f55e802010-12-06 18:11:501757 showing_interstitial_page() ||
[email protected]3ab9cb82011-06-03 18:02:071758 !delegate_ ||
1759 delegate_->ShouldSuppressDialogs();
[email protected]420ae012009-04-24 05:16:321760
1761 if (!suppress_this_message) {
[email protected]b627d9a2011-06-28 14:06:341762 content::JavaScriptDialogCreator::TitleType title_type;
1763 string16 title;
1764
1765 if (!frame_url.has_host()) {
1766 title_type = content::JavaScriptDialogCreator::DIALOG_TITLE_NONE;
1767 } else {
1768 title_type = content::JavaScriptDialogCreator::DIALOG_TITLE_FORMATTED_URL;
1769 title = net::FormatUrl(
1770 frame_url.GetOrigin(),
1771 content::GetContentClient()->browser()->GetAcceptLangs(this));
1772 }
1773
[email protected]2e5b90c2011-08-16 21:11:551774 dialog_creator_ = delegate_->GetJavaScriptDialogCreator();
1775 dialog_creator_->RunJavaScriptDialog(this,
1776 title_type,
1777 title,
1778 flags,
1779 message,
1780 default_prompt,
1781 reply_msg,
1782 &suppress_this_message);
[email protected]420ae012009-04-24 05:16:321783 }
[email protected]3ab9cb82011-06-03 18:02:071784
1785 if (suppress_this_message) {
1786 // If we are suppressing messages, just reply as if the user immediately
1787 // pressed "Cancel".
1788 OnDialogClosed(reply_msg, false, string16());
1789 }
1790
1791 *did_suppress_message = suppress_this_message;
[email protected]420ae012009-04-24 05:16:321792}
1793
[email protected]992db4c2011-05-12 15:37:151794void TabContents::RunBeforeUnloadConfirm(const RenderViewHost* rvh,
[email protected]4f5ce842011-05-27 19:34:411795 const string16& message,
[email protected]420ae012009-04-24 05:16:321796 IPC::Message* reply_msg) {
[email protected]5aab5e22010-12-08 22:13:291797 if (delegate())
1798 delegate()->WillRunBeforeUnloadConfirm();
[email protected]3ab9cb82011-06-03 18:02:071799
1800 bool suppress_this_message =
1801 rvh->is_swapped_out() ||
1802 !delegate_ ||
1803 delegate_->ShouldSuppressDialogs();
[email protected]992db4c2011-05-12 15:37:151804 if (suppress_this_message) {
[email protected]4f5ce842011-05-27 19:34:411805 render_view_host()->JavaScriptDialogClosed(reply_msg, true, string16());
[email protected]8f55e802010-12-06 18:11:501806 return;
1807 }
[email protected]3ab9cb82011-06-03 18:02:071808
[email protected]7ab1e7d62009-10-14 23:32:011809 is_showing_before_unload_dialog_ = true;
[email protected]2e5b90c2011-08-16 21:11:551810 dialog_creator_ = delegate_->GetJavaScriptDialogCreator();
1811 dialog_creator_->RunBeforeUnloadDialog(this,
1812 message,
1813 reply_msg);
[email protected]420ae012009-04-24 05:16:321814}
1815
[email protected]420ae012009-04-24 05:16:321816WebPreferences TabContents::GetWebkitPrefs() {
[email protected]447021c2010-09-08 21:29:081817 WebPreferences web_prefs =
[email protected]181a95ee2011-07-12 19:26:361818 content::GetContentClient()->browser()->GetWebkitPrefs(
[email protected]5092c282011-10-29 21:48:371819 render_view_host());
[email protected]447021c2010-09-08 21:29:081820
[email protected]b8299c12011-06-03 19:52:281821 // Force accelerated compositing and 2d canvas off for chrome:, about: and
[email protected]2ef216b2011-08-01 19:25:211822 // chrome-devtools: pages (unless it's specifically allowed).
1823 if ((GetURL().SchemeIs(chrome::kChromeDevToolsScheme) ||
[email protected]30b8e0ee2011-09-12 14:45:571824#if !defined(TOUCH_UI)
1825 // Allow accelerated compositing for keyboard and log in screen.
[email protected]b8299c12011-06-03 19:52:281826 GetURL().SchemeIs(chrome::kChromeUIScheme) ||
[email protected]30b8e0ee2011-09-12 14:45:571827#endif
[email protected]cd4a5bd22011-09-13 06:46:301828 (GetURL().SchemeIs(chrome::kAboutScheme) &&
1829 GetURL().spec() != chrome::kAboutBlankURL)) &&
[email protected]2ef216b2011-08-01 19:25:211830 !web_prefs.allow_webui_compositing) {
[email protected]447021c2010-09-08 21:29:081831 web_prefs.accelerated_compositing_enabled = false;
[email protected]9beff752010-09-22 19:35:431832 web_prefs.accelerated_2d_canvas_enabled = false;
[email protected]447021c2010-09-08 21:29:081833 }
1834
[email protected]2f7830b32011-08-26 18:22:251835#if defined(TOUCH_UI)
1836 web_prefs.force_compositing_mode = true;
1837#endif
1838
[email protected]447021c2010-09-08 21:29:081839 return web_prefs;
[email protected]420ae012009-04-24 05:16:321840}
1841
[email protected]7d472472011-01-22 01:30:251842void TabContents::OnUserGesture() {
[email protected]ddb85052011-05-18 14:40:271843 // Notify observers.
1844 FOR_EACH_OBSERVER(TabContentsObserver, observers_, DidGetUserGesture());
[email protected]686493142011-07-15 21:47:221845
1846 ResourceDispatcherHost* rdh =
1847 content::GetContentClient()->browser()->GetResourceDispatcherHost();
1848 if (rdh) // NULL in unittests.
[email protected]8ec98cd22011-08-09 22:31:301849 rdh->OnUserGesture(this);
[email protected]7d472472011-01-22 01:30:251850}
1851
[email protected]fa1cf0b82010-01-15 21:49:441852void TabContents::OnIgnoredUIEvent() {
[email protected]31145092011-09-30 01:49:441853 // Notify observers.
1854 FOR_EACH_OBSERVER(TabContentsObserver, observers_, DidGetIgnoredUIEvent());
[email protected]fa1cf0b82010-01-15 21:49:441855}
1856
[email protected]420ae012009-04-24 05:16:321857void TabContents::RendererUnresponsive(RenderViewHost* rvh,
1858 bool is_during_unload) {
[email protected]992db4c2011-05-12 15:37:151859 // Don't show hung renderer dialog for a swapped out RVH.
1860 if (rvh != render_view_host())
1861 return;
1862
[email protected]e5fc1632011-08-08 07:51:531863 // Ignore renderer unresponsive event if debugger is attached to the tab
1864 // since the event may be a result of the renderer sitting on a breakpoint.
1865 // See http://crbug.com/65458
1866 DevToolsManager* devtools_manager = DevToolsManager::GetInstance();
1867 if (devtools_manager &&
1868 devtools_manager->GetDevToolsClientHostFor(rvh) != NULL)
1869 return;
1870
[email protected]420ae012009-04-24 05:16:321871 if (is_during_unload) {
1872 // Hang occurred while firing the beforeunload/unload handler.
1873 // Pretend the handler fired so tab closing continues as if it had.
[email protected]829e7612009-04-25 01:15:111874 rvh->set_sudden_termination_allowed(true);
[email protected]420ae012009-04-24 05:16:321875
1876 if (!render_manager_.ShouldCloseTabOnUnresponsiveRenderer())
1877 return;
1878
1879 // If the tab hangs in the beforeunload/unload handler there's really
1880 // nothing we can do to recover. Pretend the unload listeners have
1881 // all fired and close the tab. If the hang is in the beforeunload handler
1882 // then the user will not have the option of cancelling the close.
1883 Close(rvh);
1884 return;
1885 }
1886
[email protected]55452902011-06-01 21:57:471887 if (!render_view_host() || !render_view_host()->IsRenderViewLive())
1888 return;
1889
1890 if (delegate())
1891 delegate()->RendererUnresponsive(this);
[email protected]420ae012009-04-24 05:16:321892}
1893
1894void TabContents::RendererResponsive(RenderViewHost* render_view_host) {
[email protected]55452902011-06-01 21:57:471895 if (delegate())
1896 delegate()->RendererResponsive(this);
[email protected]420ae012009-04-24 05:16:321897}
1898
1899void TabContents::LoadStateChanged(const GURL& url,
[email protected]9c235f042011-08-10 22:28:211900 const net::LoadStateWithParam& load_state,
[email protected]094e5b22009-09-25 04:23:561901 uint64 upload_position,
1902 uint64 upload_size) {
[email protected]420ae012009-04-24 05:16:321903 load_state_ = load_state;
[email protected]094e5b22009-09-25 04:23:561904 upload_position_ = upload_position;
1905 upload_size_ = upload_size;
[email protected]04866c42011-05-03 20:03:501906 load_state_host_ = net::IDNToUnicode(url.host(),
[email protected]b5cca982011-05-26 04:42:081907 content::GetContentClient()->browser()->GetAcceptLangs(this));
[email protected]9c235f042011-08-10 22:28:211908 if (load_state_.state == net::LOAD_STATE_READING_RESPONSE)
[email protected]420ae012009-04-24 05:16:321909 SetNotWaitingForResponse();
[email protected]f5d978c2011-07-21 14:43:511910 if (IsLoading())
[email protected]c9cd2222009-05-06 05:16:501911 NotifyNavigationStateChanged(INVALIDATE_LOAD | INVALIDATE_TAB);
[email protected]420ae012009-04-24 05:16:321912}
1913
[email protected]7d472472011-01-22 01:30:251914void TabContents::WorkerCrashed() {
[email protected]0dd3a0ab2011-02-18 08:17:441915 if (delegate())
[email protected]55452902011-06-01 21:57:471916 delegate()->WorkerCrashed(this);
[email protected]7d472472011-01-22 01:30:251917}
1918
[email protected]420ae012009-04-24 05:16:321919void TabContents::BeforeUnloadFiredFromRenderManager(
1920 bool proceed,
1921 bool* proceed_to_fire_unload) {
1922 if (delegate())
1923 delegate()->BeforeUnloadFired(this, proceed, proceed_to_fire_unload);
1924}
1925
[email protected]3a3d47472010-07-15 21:03:541926void TabContents::DidStartLoadingFromRenderManager(
1927 RenderViewHost* render_view_host) {
1928 DidStartLoading();
1929}
1930
1931void TabContents::RenderViewGoneFromRenderManager(
1932 RenderViewHost* render_view_host) {
[email protected]443b80e2010-12-14 00:42:231933 DCHECK(crashed_status_ != base::TERMINATION_STATUS_STILL_RUNNING);
1934 RenderViewGone(render_view_host, crashed_status_, crashed_error_code_);
[email protected]3a3d47472010-07-15 21:03:541935}
1936
[email protected]420ae012009-04-24 05:16:321937void TabContents::UpdateRenderViewSizeForRenderManager() {
1938 // TODO(brettw) this is a hack. See TabContentsView::SizeContents.
[email protected]0323ee42010-02-17 22:03:261939 gfx::Size size = view_->GetContainerSize();
1940 // 0x0 isn't a valid window size (minimal window size is 1x1) but it may be
1941 // here during container initialization and normal window size will be set
1942 // later. In case of tab duplication this resizing to 0x0 prevents setting
1943 // normal size later so just ignore it.
1944 if (!size.IsEmpty())
1945 view_->SizeContents(size);
[email protected]420ae012009-04-24 05:16:321946}
1947
[email protected]3a3d47472010-07-15 21:03:541948void TabContents::NotifySwappedFromRenderManager() {
1949 NotifySwapped();
1950}
1951
1952NavigationController& TabContents::GetControllerForRenderManager() {
1953 return controller();
1954}
1955
[email protected]d0980792011-02-13 19:41:401956WebUI* TabContents::CreateWebUIForRenderManager(const GURL& url) {
[email protected]1fd1a502011-03-30 16:55:561957 return content::WebUIFactory::Get()->CreateWebUIForURL(this, url);
[email protected]420ae012009-04-24 05:16:321958}
1959
1960NavigationEntry*
1961TabContents::GetLastCommittedNavigationEntryForRenderManager() {
1962 return controller_.GetLastCommittedEntry();
1963}
1964
1965bool TabContents::CreateRenderViewForRenderManager(
1966 RenderViewHost* render_view_host) {
[email protected]420ae012009-04-24 05:16:321967 RenderWidgetHostView* rwh_view = view_->CreateViewForWidget(render_view_host);
[email protected]1a98a932009-11-17 00:12:521968
[email protected]420ae012009-04-24 05:16:321969 // Now that the RenderView has been created, we need to tell it its size.
1970 rwh_view->SetSize(view_->GetContainerSize());
1971
[email protected]a4127722011-04-27 23:13:521972 if (!render_view_host->CreateRenderView(string16()))
1973 return false;
1974
[email protected]f8233cc2011-05-31 20:24:501975#if defined(OS_LINUX)
1976 // Force a ViewMsg_Resize to be sent, needed to make plugins show up on
1977 // linux. See crbug.com/83941.
1978 if (RenderWidgetHost* render_widget_host = rwh_view->GetRenderWidgetHost())
1979 render_widget_host->WasResized();
1980#endif
1981
[email protected]420ae012009-04-24 05:16:321982 UpdateMaxPageIDIfNecessary(render_view_host->site_instance(),
1983 render_view_host);
1984 return true;
1985}
1986
[email protected]3ab9cb82011-06-03 18:02:071987void TabContents::OnDialogClosed(IPC::Message* reply_msg,
1988 bool success,
1989 const string16& user_input) {
[email protected]beb440c2009-11-06 04:08:541990 if (is_showing_before_unload_dialog_ && !success) {
1991 // If a beforeunload dialog is canceled, we need to stop the throbber from
1992 // spinning, since we forced it to start spinning in Navigate.
1993 DidStopLoading();
1994
1995 tab_close_start_time_ = base::TimeTicks();
1996 }
1997 is_showing_before_unload_dialog_ = false;
[email protected]4f5ce842011-05-27 19:34:411998 render_view_host()->JavaScriptDialogClosed(reply_msg,
1999 success,
[email protected]3ab9cb82011-06-03 18:02:072000 user_input);
[email protected]beb440c2009-11-06 04:08:542001}
2002
[email protected]3ab9cb82011-06-03 18:02:072003gfx::NativeWindow TabContents::GetDialogRootWindow() {
2004 return view_->GetTopLevelNativeWindow();
[email protected]beb440c2009-11-06 04:08:542005}
2006
[email protected]a1e97f02011-06-30 14:04:342007void TabContents::OnDialogShown() {
2008 Activate();
[email protected]3a3d47472010-07-15 21:03:542009}
2010
[email protected]41fc0322009-09-04 22:23:402011void TabContents::set_encoding(const std::string& encoding) {
[email protected]763ec4ca2011-04-29 15:48:122012 encoding_ = content::GetContentClient()->browser()->
2013 GetCanonicalEncodingNameByAliasName(encoding);
[email protected]aacd7cc2009-09-02 14:51:102014}
[email protected]f45d2a72010-03-08 23:28:352015
[email protected]33f74972010-12-08 16:40:362016void TabContents::CreateViewAndSetSizeForRVH(RenderViewHost* rvh) {
2017 RenderWidgetHostView* rwh_view = view()->CreateViewForWidget(rvh);
2018 rwh_view->SetSize(view()->GetContainerSize());
2019}
[email protected]e9621112011-10-17 05:38:372020
2021bool TabContents::GotResponseToLockMouseRequest(bool allowed) {
2022 return render_view_host() ?
2023 render_view_host()->GotResponseToLockMouseRequest(allowed) : false;
2024}