blob: 20b0a6c1ede0c503b83090305b5f83bc9545a409 [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]cf68b092010-01-15 20:05:5516#include "chrome/browser/browser_shutdown.h"
[email protected]96d185d2009-04-24 03:28:5417#include "chrome/browser/debugger/devtools_manager.h"
[email protected]420ae012009-04-24 05:16:3218#include "chrome/browser/load_from_memory_cache_details.h"
[email protected]f364d1392011-04-08 21:03:1019#include "chrome/browser/notifications/desktop_notification_service.h"
[email protected]dcb72d52011-04-13 12:36:5320#include "chrome/browser/notifications/desktop_notification_service_factory.h"
[email protected]8ecad5e2010-12-02 21:18:3321#include "chrome/browser/profiles/profile.h"
[email protected]1caa1522010-12-01 20:23:3522#include "chrome/browser/ui/app_modal_dialogs/message_box_handler.h"
[email protected]7ec6bef2011-02-26 02:22:2923#include "chrome/browser/ui/browser_dialogs.h"
[email protected]1698f612011-03-11 11:28:1524#include "chrome/common/chrome_constants.h"
[email protected]567812d2011-02-24 17:40:5025#include "content/browser/child_process_security_policy.h"
[email protected]1fd1a502011-03-30 16:55:5626#include "content/browser/content_browser_client.h"
[email protected]567812d2011-02-24 17:40:5027#include "content/browser/host_zoom_map.h"
28#include "content/browser/in_process_webkit/session_storage_namespace.h"
[email protected]35e251d2011-05-24 21:01:0429#include "content/browser/load_notification_details.h"
[email protected]567812d2011-02-24 17:40:5030#include "content/browser/renderer_host/render_process_host.h"
31#include "content/browser/renderer_host/render_view_host.h"
32#include "content/browser/renderer_host/render_widget_host_view.h"
33#include "content/browser/renderer_host/resource_request_details.h"
34#include "content/browser/site_instance.h"
[email protected]0dd3a0ab2011-02-18 08:17:4435#include "content/browser/tab_contents/interstitial_page.h"
[email protected]8286f51a2011-05-31 17:39:1336#include "content/browser/tab_contents/navigation_details.h"
[email protected]0dd3a0ab2011-02-18 08:17:4437#include "content/browser/tab_contents/navigation_entry.h"
38#include "content/browser/tab_contents/provisional_load_details.h"
39#include "content/browser/tab_contents/tab_contents_delegate.h"
40#include "content/browser/tab_contents/tab_contents_observer.h"
41#include "content/browser/tab_contents/tab_contents_view.h"
[email protected]105bb0f2011-05-24 17:12:1442#include "content/browser/tab_contents/title_updated_details.h"
[email protected]afd1e522011-04-27 23:29:5943#include "content/browser/user_metrics.h"
[email protected]1fd1a502011-03-30 16:55:5644#include "content/browser/webui/web_ui_factory.h"
[email protected]9966325b2011-04-18 05:00:1045#include "content/common/bindings_policy.h"
[email protected]1fd1a502011-03-30 16:55:5646#include "content/common/content_client.h"
[email protected]763ec4ca2011-04-29 15:48:1247#include "content/common/content_restriction.h"
[email protected]4dd57932011-03-17 06:06:1248#include "content/common/navigation_types.h"
[email protected]7f070d42011-03-09 20:25:3249#include "content/common/notification_service.h"
[email protected]0f012df82011-05-19 14:15:2950#include "content/common/url_constants.h"
[email protected]2c5569662011-03-22 20:45:0251#include "content/common/view_messages.h"
[email protected]d686e812009-06-03 19:10:2952#include "net/base/net_util.h"
[email protected]420ae012009-04-24 05:16:3253#include "net/base/registry_controlled_domain.h"
[email protected]abe2c032011-03-31 18:49:3454#include "net/url_request/url_request_context_getter.h"
[email protected]8bd0fe62011-01-17 06:44:3755#include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h"
[email protected]42ce29d2011-01-20 23:19:4656#include "ui/base/resource/resource_bundle.h"
[email protected]08397d52011-02-05 01:53:3857#include "ui/gfx/codec/png_codec.h"
[email protected]d5e311b2010-07-20 17:15:0358#include "webkit/glue/password_form.h"
[email protected]3c733bde2010-12-21 19:56:3159#include "webkit/glue/webpreferences.h"
60
61#if defined(OS_MACOSX)
[email protected]b9b751f22011-03-25 14:04:1262#include "ui/gfx/surface/io_surface_support_mac.h"
[email protected]3c733bde2010-12-21 19:56:3163#endif // defined(OS_MACOSX)
[email protected]3e45ba92009-02-20 21:09:0064
[email protected]420ae012009-04-24 05:16:3265// Cross-Site Navigations
66//
67// If a TabContents is told to navigate to a different web site (as determined
68// by SiteInstance), it will replace its current RenderViewHost with a new
69// RenderViewHost dedicated to the new SiteInstance. This works as follows:
70//
71// - Navigate determines whether the destination is cross-site, and if so,
72// it creates a pending_render_view_host_ and moves into the PENDING
73// RendererState.
74// - The pending RVH is "suspended," so that no navigation messages are sent to
75// its renderer until the onbeforeunload JavaScript handler has a chance to
76// run in the current RVH.
77// - The pending RVH tells CrossSiteRequestManager (a thread-safe singleton)
78// that it has a pending cross-site request. ResourceDispatcherHost will
79// check for this when the response arrives.
80// - The current RVH runs its onbeforeunload handler. If it returns false, we
81// cancel all the pending logic and go back to NORMAL. Otherwise we allow
82// the pending RVH to send the navigation request to its renderer.
83// - ResourceDispatcherHost receives a ResourceRequest on the IO thread. It
84// checks CrossSiteRequestManager to see that the RVH responsible has a
85// pending cross-site request, and then installs a CrossSiteEventHandler.
86// - When RDH receives a response, the BufferedEventHandler determines whether
87// it is a download. If so, it sends a message to the new renderer causing
88// it to cancel the request, and the download proceeds in the download
89// thread. For now, we stay in a PENDING state (with a pending RVH) until
[email protected]57c6a652009-05-04 07:58:3490// the next DidNavigate event for this TabContents. This isn't ideal, but it
[email protected]420ae012009-04-24 05:16:3291// doesn't affect any functionality.
92// - After RDH receives a response and determines that it is safe and not a
93// download, it pauses the response to first run the old page's onunload
94// handler. It does this by asynchronously calling the OnCrossSiteResponse
[email protected]992db4c2011-05-12 15:37:1595// method of TabContents on the UI thread, which sends a SwapOut message
[email protected]420ae012009-04-24 05:16:3296// to the current RVH.
[email protected]992db4c2011-05-12 15:37:1597// - Once the onunload handler is finished, a SwapOut_ACK message is sent to
[email protected]420ae012009-04-24 05:16:3298// the ResourceDispatcherHost, who unpauses the response. Data is then sent
99// to the pending RVH.
100// - The pending renderer sends a FrameNavigate message that invokes the
101// DidNavigate method. This replaces the current RVH with the
102// pending RVH and goes back to the NORMAL RendererState.
[email protected]992db4c2011-05-12 15:37:15103// - The previous renderer is kept swapped out in RenderViewHostManager in case
104// the user goes back. The process only stays live if another tab is using
105// it, but if so, the existing frame relationships will be maintained.
[email protected]420ae012009-04-24 05:16:32106
107namespace {
108
109// Amount of time we wait between when a key event is received and the renderer
110// is queried for its state and pushed to the NavigationEntry.
111const int kQueryStateDelay = 5000;
112
[email protected]6ebdc9b2010-09-27 16:55:57113const int kSyncWaitDelay = 40;
114
[email protected]420ae012009-04-24 05:16:32115#if defined(OS_WIN)
116
117BOOL CALLBACK InvalidateWindow(HWND hwnd, LPARAM lparam) {
118 // Note: erase is required to properly paint some widgets borders. This can
119 // be seen with textfields.
120 InvalidateRect(hwnd, NULL, TRUE);
121 return TRUE;
122}
123#endif
124
[email protected]2c5569662011-03-22 20:45:02125ViewMsg_Navigate_Type::Value GetNavigationType(
[email protected]1ccb3568d2010-02-19 10:51:16126 Profile* profile, const NavigationEntry& entry,
127 NavigationController::ReloadType reload_type) {
128 switch (reload_type) {
129 case NavigationController::RELOAD:
[email protected]2c5569662011-03-22 20:45:02130 return ViewMsg_Navigate_Type::RELOAD;
[email protected]1ccb3568d2010-02-19 10:51:16131 case NavigationController::RELOAD_IGNORING_CACHE:
[email protected]2c5569662011-03-22 20:45:02132 return ViewMsg_Navigate_Type::RELOAD_IGNORING_CACHE;
[email protected]1ccb3568d2010-02-19 10:51:16133 case NavigationController::NO_RELOAD:
134 break; // Fall through to rest of function.
135 }
[email protected]5e369672009-11-03 23:48:30136
137 if (entry.restore_type() == NavigationEntry::RESTORE_LAST_SESSION &&
138 profile->DidLastSessionExitCleanly())
[email protected]2c5569662011-03-22 20:45:02139 return ViewMsg_Navigate_Type::RESTORE;
[email protected]5e369672009-11-03 23:48:30140
[email protected]2c5569662011-03-22 20:45:02141 return ViewMsg_Navigate_Type::NORMAL;
[email protected]5e369672009-11-03 23:48:30142}
143
[email protected]876bc832010-09-07 16:29:54144void MakeNavigateParams(const NavigationEntry& entry,
145 const NavigationController& controller,
[email protected]1ccb3568d2010-02-19 10:51:16146 NavigationController::ReloadType reload_type,
147 ViewMsg_Navigate_Params* params) {
[email protected]056de2d2009-06-26 16:41:34148 params->page_id = entry.page_id();
[email protected]876bc832010-09-07 16:29:54149 params->pending_history_list_offset = controller.GetIndexOfEntry(&entry);
[email protected]3cc72b12010-03-18 23:03:00150 params->current_history_list_offset = controller.last_committed_entry_index();
151 params->current_history_list_length = controller.entry_count();
[email protected]056de2d2009-06-26 16:41:34152 params->url = entry.url();
153 params->referrer = entry.referrer();
154 params->transition = entry.transition_type();
155 params->state = entry.content_state();
[email protected]3cc72b12010-03-18 23:03:00156 params->navigation_type =
157 GetNavigationType(controller.profile(), entry, reload_type);
[email protected]056de2d2009-06-26 16:41:34158 params->request_time = base::Time::Now();
159}
160
[email protected]420ae012009-04-24 05:16:32161} // namespace
162
[email protected]f4f50ef2011-01-21 19:01:19163
164// TabContents ----------------------------------------------------------------
[email protected]420ae012009-04-24 05:16:32165
[email protected]420ae012009-04-24 05:16:32166TabContents::TabContents(Profile* profile,
167 SiteInstance* site_instance,
168 int routing_id,
[email protected]6ee12c42010-09-14 09:36:07169 const TabContents* base_tab_contents,
170 SessionStorageNamespace* session_storage_namespace)
[email protected]b680ad22009-04-15 23:19:42171 : delegate_(NULL),
[email protected]6ee12c42010-09-14 09:36:07172 ALLOW_THIS_IN_INITIALIZER_LIST(controller_(
173 this, profile, session_storage_namespace)),
[email protected]66ba4932009-06-04 19:22:13174 ALLOW_THIS_IN_INITIALIZER_LIST(view_(
[email protected]d82ed61e2009-06-16 02:46:22175 TabContentsView::Create(this))),
176 ALLOW_THIS_IN_INITIALIZER_LIST(render_manager_(this, this)),
[email protected]6f821dd2010-02-23 00:54:06177 bookmark_drag_(NULL),
[email protected]d5f942ba2008-09-26 19:30:34178 is_loading_(false),
[email protected]443b80e2010-12-14 00:42:23179 crashed_status_(base::TERMINATION_STATUS_STILL_RUNNING),
180 crashed_error_code_(0),
[email protected]d5f942ba2008-09-26 19:30:34181 waiting_for_response_(false),
[email protected]d5f942ba2008-09-26 19:30:34182 max_page_id_(-1),
[email protected]fdd61c62009-04-22 19:22:57183 load_state_(net::LOAD_STATE_IDLE),
[email protected]094e5b22009-09-25 04:23:56184 upload_size_(0),
185 upload_position_(0),
[email protected]f17a0ee2010-05-17 17:38:47186 displayed_insecure_content_(false),
[email protected]fdd61c62009-04-22 19:22:57187 capturing_contents_(false),
188 is_being_destroyed_(false),
189 notify_disconnection_(false),
[email protected]fdd61c62009-04-22 19:22:57190#if defined(OS_WIN)
191 message_box_active_(CreateEvent(NULL, TRUE, FALSE, NULL)),
192#endif
[email protected]c2e74fe82009-09-03 17:57:44193 suppress_javascript_messages_(false),
[email protected]7ab1e7d62009-10-14 23:32:01194 is_showing_before_unload_dialog_(false),
[email protected]1fd1a502011-03-30 16:55:56195 opener_web_ui_type_(WebUI::kNoWebUI),
[email protected]ebf40a72010-07-22 01:46:38196 closed_by_user_gesture_(false),
[email protected]b75b8292010-10-01 07:28:25197 minimum_zoom_percent_(
198 static_cast<int>(WebKit::WebView::minTextSizeMultiplier * 100)),
199 maximum_zoom_percent_(
200 static_cast<int>(WebKit::WebView::maxTextSizeMultiplier * 100)),
[email protected]9e823662010-10-13 23:36:00201 temporary_zoom_settings_(false),
[email protected]4850a7f2011-03-08 23:36:59202 content_restrictions_(0) {
[email protected]7ff431e2010-01-07 18:18:54203
[email protected]12636df2009-09-28 22:32:21204 render_manager_.Init(profile, site_instance, routing_id);
[email protected]420ae012009-04-24 05:16:32205
[email protected]34ac70502009-09-25 17:07:23206 // We have the initial size of the view be based on the size of the passed in
207 // tab contents (normally a tab from the same window).
208 view_->CreateView(base_tab_contents ?
209 base_tab_contents->view()->GetContainerSize() : gfx::Size());
[email protected]420ae012009-04-24 05:16:32210
[email protected]420ae012009-04-24 05:16:32211 registrar_.Add(this, NotificationType::RENDER_WIDGET_HOST_DESTROYED,
212 NotificationService::AllSources());
[email protected]88152052010-10-01 04:05:18213
[email protected]b5a1d11c2011-02-17 03:09:42214 // Can only add observers after render_manager_.Init() is called, since that's
215 // what sets up the render_view_host which TabContentObserver's constructor
216 // uses to get the routing_id.
217 AddObservers();
[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]bfe4c1582009-10-02 18:11:09223 // We don't want any notifications while we're running our destructor.
[email protected]420ae012009-04-24 05:16:32224 registrar_.RemoveAll();
[email protected]420ae012009-04-24 05:16:32225
[email protected]420ae012009-04-24 05:16:32226 NotifyDisconnected();
[email protected]7ec6bef2011-02-26 02:22:29227 browser::HideHungRendererDialog(this);
[email protected]420ae012009-04-24 05:16:32228
[email protected]420ae012009-04-24 05:16:32229 // First cleanly close all child windows.
230 // TODO(mpcomplete): handle case if MaybeCloseChildWindows() already asked
231 // some of these to close. CloseWindows is async, so it might get called
232 // twice before it runs.
[email protected]8cc8d492010-02-02 10:40:49233 CloseConstrainedWindows();
[email protected]420ae012009-04-24 05:16:32234
[email protected]420ae012009-04-24 05:16:32235 // Notify any observer that have a reference on this tab contents.
236 NotificationService::current()->Notify(
237 NotificationType::TAB_CONTENTS_DESTROYED,
238 Source<TabContents>(this),
239 NotificationService::NoDetails());
240
241 // TODO(brettw) this should be moved to the view.
242#if defined(OS_WIN)
243 // If we still have a window handle, destroy it. GetNativeView can return
244 // NULL if this contents was part of a window that closed.
[email protected]50bd6452010-11-27 19:39:42245 if (GetNativeView()) {
246 RenderViewHost* host = render_view_host();
[email protected]b9a1fb42011-04-12 23:16:44247 if (host && host->view())
[email protected]50bd6452010-11-27 19:39:42248 host->view()->WillWmDestroy();
[email protected]50bd6452010-11-27 19:39:42249 }
[email protected]420ae012009-04-24 05:16:32250#endif
[email protected]7ab1e7d62009-10-14 23:32:01251
252 // OnCloseStarted isn't called in unit tests.
253 if (!tab_close_start_time_.is_null()) {
254 UMA_HISTOGRAM_TIMES("Tab.Close",
255 base::TimeTicks::Now() - tab_close_start_time_);
256 }
[email protected]b5a1d11c2011-02-17 03:09:42257
[email protected]07d490bc2011-03-07 17:05:26258 FOR_EACH_OBSERVER(TabContentsObserver, observers_, TabContentsDestroyed());
[email protected]232a5812011-03-04 22:42:08259
260 net::NetworkChangeNotifier::RemoveOnlineStateObserver(this);
[email protected]b5a1d11c2011-02-17 03:09:42261}
262
263void TabContents::AddObservers() {
[email protected]232a5812011-03-04 22:42:08264 net::NetworkChangeNotifier::AddOnlineStateObserver(this);
initial.commit09911bf2008-07-26 23:55:29265}
266
[email protected]724159a2010-12-30 01:11:18267bool TabContents::OnMessageReceived(const IPC::Message& message) {
[email protected]f82d57b52011-04-27 19:13:17268 if (web_ui() && web_ui()->OnMessageReceived(message))
269 return true;
270
[email protected]585b30362011-01-28 02:30:17271 ObserverListBase<TabContentsObserver>::Iterator it(observers_);
272 TabContentsObserver* observer;
[email protected]0f180ee2011-01-21 18:23:10273 while ((observer = it.GetNext()) != NULL)
274 if (observer->OnMessageReceived(message))
[email protected]403415a2011-01-10 18:57:53275 return true;
[email protected]403415a2011-01-10 18:57:53276
[email protected]724159a2010-12-30 01:11:18277 bool handled = true;
278 bool message_is_ok = true;
279 IPC_BEGIN_MESSAGE_MAP_EX(TabContents, message, message_is_ok)
280 IPC_MESSAGE_HANDLER(ViewHostMsg_DidStartProvisionalLoadForFrame,
281 OnDidStartProvisionalLoadForFrame)
282 IPC_MESSAGE_HANDLER(ViewHostMsg_DidRedirectProvisionalLoad,
283 OnDidRedirectProvisionalLoad)
284 IPC_MESSAGE_HANDLER(ViewHostMsg_DidFailProvisionalLoadWithError,
285 OnDidFailProvisionalLoadWithError)
286 IPC_MESSAGE_HANDLER(ViewHostMsg_DidLoadResourceFromMemoryCache,
287 OnDidLoadResourceFromMemoryCache)
288 IPC_MESSAGE_HANDLER(ViewHostMsg_DidDisplayInsecureContent,
289 OnDidDisplayInsecureContent)
290 IPC_MESSAGE_HANDLER(ViewHostMsg_DidRunInsecureContent,
291 OnDidRunInsecureContent)
292 IPC_MESSAGE_HANDLER(ViewHostMsg_DocumentLoadedInFrame,
293 OnDocumentLoadedInFrame)
294 IPC_MESSAGE_HANDLER(ViewHostMsg_DidFinishLoad, OnDidFinishLoad)
[email protected]c8f73ab2011-01-22 00:05:17295 IPC_MESSAGE_HANDLER(ViewHostMsg_UpdateContentRestrictions,
296 OnUpdateContentRestrictions)
[email protected]7d472472011-01-22 01:30:25297 IPC_MESSAGE_HANDLER(ViewHostMsg_GoToEntryAtOffset, OnGoToEntryAtOffset)
[email protected]216813952011-05-19 22:21:26298 IPC_MESSAGE_HANDLER(ViewHostMsg_UpdateZoomLimits, OnUpdateZoomLimits)
299 IPC_MESSAGE_HANDLER(ViewHostMsg_FocusedNodeChanged, OnFocusedNodeChanged)
[email protected]724159a2010-12-30 01:11:18300 IPC_MESSAGE_UNHANDLED(handled = false)
301 IPC_END_MESSAGE_MAP_EX()
302
303 if (!message_is_ok) {
304 UserMetrics::RecordAction(UserMetricsAction("BadMessageTerminate_RVD"));
305 GetRenderProcessHost()->ReceivedBadMessage();
306 }
307
308 return handled;
309}
310
[email protected]13367f72009-07-08 16:39:02311// Returns true if contains content rendered by an extension.
312bool TabContents::HostsExtension() const {
313 return GetURL().SchemeIs(chrome::kExtensionScheme);
[email protected]d5f942ba2008-09-26 19:30:34314}
315
[email protected]8cb5d5b2010-02-09 11:36:16316RenderProcessHost* TabContents::GetRenderProcessHost() const {
317 return render_manager_.current_host()->process();
318}
319
[email protected]d5f942ba2008-09-26 19:30:34320const GURL& TabContents::GetURL() const {
321 // We may not have a navigation entry yet
[email protected]ce3fa3c2009-04-20 19:55:57322 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]ebe89e062009-08-13 23:16:54323 return entry ? entry->virtual_url() : GURL::EmptyGURL();
[email protected]d5f942ba2008-09-26 19:30:34324}
325
[email protected]96d185d2009-04-24 03:28:54326const string16& TabContents::GetTitle() const {
[email protected]4c6092c5b2009-06-06 00:23:55327 // Transient entries take precedence. They are used for interstitial pages
328 // that are shown on top of existing pages.
329 NavigationEntry* entry = controller_.GetTransientEntry();
[email protected]b5cca982011-05-26 04:42:08330 std::string accept_languages =
331 content::GetContentClient()->browser()->GetAcceptLangs(this);
[email protected]45d0ef7f2011-01-05 13:46:23332 if (entry) {
[email protected]b5cca982011-05-26 04:42:08333 return entry->GetTitleForDisplay(accept_languages);
[email protected]45d0ef7f2011-01-05 13:46:23334 }
[email protected]7ade2732011-02-10 00:13:58335 WebUI* our_web_ui = render_manager_.pending_web_ui() ?
336 render_manager_.pending_web_ui() : render_manager_.web_ui();
337 if (our_web_ui) {
[email protected]96d185d2009-04-24 03:28:54338 // Don't override the title in view source mode.
[email protected]4c6092c5b2009-06-06 00:23:55339 entry = controller_.GetActiveEntry();
[email protected]96d185d2009-04-24 03:28:54340 if (!(entry && entry->IsViewSourceMode())) {
[email protected]e0112912011-02-02 22:54:35341 // Give the Web UI the chance to override our title.
[email protected]7ade2732011-02-10 00:13:58342 const string16& title = our_web_ui->overridden_title();
[email protected]96d185d2009-04-24 03:28:54343 if (!title.empty())
344 return title;
345 }
346 }
347
348 // We use the title for the last committed entry rather than a pending
349 // navigation entry. For example, when the user types in a URL, we want to
350 // keep the old page's title until the new load has committed and we get a new
351 // title.
[email protected]96d185d2009-04-24 03:28:54352 entry = controller_.GetLastCommittedEntry();
[email protected]45d0ef7f2011-01-05 13:46:23353 if (entry) {
[email protected]b5cca982011-05-26 04:42:08354 return entry->GetTitleForDisplay(accept_languages);
[email protected]45d0ef7f2011-01-05 13:46:23355 }
[email protected]987fc3a2011-05-26 14:18:09356
357 // |page_title_when_no_navigation_entry_| is finally used
358 // if no title cannot be retrieved.
359 return page_title_when_no_navigation_entry_;
[email protected]96d185d2009-04-24 03:28:54360}
361
[email protected]d5f942ba2008-09-26 19:30:34362int32 TabContents::GetMaxPageID() {
363 if (GetSiteInstance())
364 return GetSiteInstance()->max_page_id();
365 else
366 return max_page_id_;
367}
368
369void TabContents::UpdateMaxPageID(int32 page_id) {
370 // Ensure both the SiteInstance and RenderProcessHost update their max page
[email protected]57c6a652009-05-04 07:58:34371 // IDs in sync. Only TabContents will also have site instances, except during
[email protected]d5f942ba2008-09-26 19:30:34372 // testing.
373 if (GetSiteInstance())
374 GetSiteInstance()->UpdateMaxPageID(page_id);
[email protected]8cb5d5b2010-02-09 11:36:16375 GetRenderProcessHost()->UpdateMaxPageID(page_id);
[email protected]d5f942ba2008-09-26 19:30:34376}
377
[email protected]96d185d2009-04-24 03:28:54378SiteInstance* TabContents::GetSiteInstance() const {
379 return render_manager_.current_host()->site_instance();
380}
381
[email protected]96d185d2009-04-24 03:28:54382bool TabContents::ShouldDisplayURL() {
[email protected]8ab24cc2009-11-13 20:56:15383 // Don't hide the url in view source mode and with interstitials.
[email protected]96d185d2009-04-24 03:28:54384 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]8ab24cc2009-11-13 20:56:15385 if (entry && (entry->IsViewSourceMode() ||
[email protected]cccd3762010-11-12 18:40:01386 entry->page_type() == INTERSTITIAL_PAGE)) {
[email protected]96d185d2009-04-24 03:28:54387 return true;
[email protected]8ab24cc2009-11-13 20:56:15388 }
[email protected]83e3895b2009-06-11 00:07:16389
[email protected]2b9a06402011-02-16 04:57:10390 // We always display the URL for non-WebUI URLs to prevent spoofing.
[email protected]1fd1a502011-03-30 16:55:56391 if (entry && !content::WebUIFactory::Get()->HasWebUIScheme(entry->url()))
[email protected]2b9a06402011-02-16 04:57:10392 return true;
393
[email protected]d0980792011-02-13 19:41:40394 WebUI* web_ui = GetWebUIForCurrentState();
[email protected]7ade2732011-02-10 00:13:58395 if (web_ui)
396 return !web_ui->should_hide_url();
[email protected]96d185d2009-04-24 03:28:54397 return true;
398}
399
[email protected]585b30362011-01-28 02:30:17400void TabContents::AddObserver(TabContentsObserver* observer) {
401 observers_.AddObserver(observer);
[email protected]3c9e1872010-11-18 16:17:49402}
403
[email protected]585b30362011-01-28 02:30:17404void TabContents::RemoveObserver(TabContentsObserver* observer) {
405 observers_.RemoveObserver(observer);
[email protected]3c9e1872010-11-18 16:17:49406}
407
[email protected]443b80e2010-12-14 00:42:23408void TabContents::SetIsCrashed(base::TerminationStatus status, int error_code) {
409 if (status == crashed_status_)
[email protected]d5f942ba2008-09-26 19:30:34410 return;
411
[email protected]443b80e2010-12-14 00:42:23412 crashed_status_ = status;
413 crashed_error_code_ = error_code;
[email protected]c9cd2222009-05-06 05:16:50414 NotifyNavigationStateChanged(INVALIDATE_TAB);
[email protected]d5f942ba2008-09-26 19:30:34415}
416
417void TabContents::NotifyNavigationStateChanged(unsigned changed_flags) {
418 if (delegate_)
419 delegate_->NavigationStateChanged(this, changed_flags);
420}
421
[email protected]96d185d2009-04-24 03:28:54422void TabContents::DidBecomeSelected() {
423 controller_.SetActive(true);
[email protected]8cb5d5b2010-02-09 11:36:16424 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
425 if (rwhv) {
426 rwhv->DidBecomeSelected();
[email protected]789e9152009-08-04 21:59:43427#if defined(OS_MACOSX)
[email protected]8cb5d5b2010-02-09 11:36:16428 rwhv->SetActive(true);
[email protected]789e9152009-08-04 21:59:43429#endif
430 }
[email protected]96d185d2009-04-24 03:28:54431
[email protected]5ac20162010-11-24 23:33:11432 last_selected_time_ = base::TimeTicks::Now();
[email protected]3e69bc82011-05-26 23:22:38433
434 FOR_EACH_OBSERVER(TabContentsObserver, observers_, DidBecomeSelected());
[email protected]96d185d2009-04-24 03:28:54435}
436
437void TabContents::WasHidden() {
438 if (!capturing_contents()) {
439 // |render_view_host()| can be NULL if the user middle clicks a link to open
440 // a tab in then background, then closes the tab before selecting it. This
441 // is because closing the tab calls TabContents::Destroy(), which removes
442 // the |render_view_host()|; then when we actually destroy the window,
443 // OnWindowPosChanged() notices and calls HideContents() (which calls us).
[email protected]8cb5d5b2010-02-09 11:36:16444 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
445 if (rwhv)
446 rwhv->WasHidden();
[email protected]96d185d2009-04-24 03:28:54447 }
448
449 NotificationService::current()->Notify(
450 NotificationType::TAB_CONTENTS_HIDDEN,
451 Source<TabContents>(this),
452 NotificationService::NoDetails());
453}
454
[email protected]d5f942ba2008-09-26 19:30:34455void TabContents::Activate() {
456 if (delegate_)
457 delegate_->ActivateContents(this);
458}
459
[email protected]ea42e7782010-08-23 23:58:12460void TabContents::Deactivate() {
461 if (delegate_)
462 delegate_->DeactivateContents(this);
463}
464
[email protected]96d185d2009-04-24 03:28:54465void TabContents::ShowContents() {
[email protected]8cb5d5b2010-02-09 11:36:16466 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
467 if (rwhv)
468 rwhv->DidBecomeSelected();
[email protected]96d185d2009-04-24 03:28:54469}
470
471void TabContents::HideContents() {
472 // TODO(pkasting): http://b/1239839 Right now we purposefully don't call
473 // our superclass HideContents(), because some callers want to be very picky
474 // about the order in which these get called. In addition to making the code
475 // here practically impossible to understand, this also means we end up
476 // calling TabContents::WasHidden() twice if callers call both versions of
[email protected]57c6a652009-05-04 07:58:34477 // HideContents() on a TabContents.
[email protected]96d185d2009-04-24 03:28:54478 WasHidden();
479}
480
[email protected]5aab5e22010-12-08 22:13:29481bool TabContents::NeedToFireBeforeUnload() {
482 // TODO(creis): Should we fire even for interstitial pages?
483 return notify_disconnection() &&
484 !showing_interstitial_page() &&
485 !render_view_host()->SuddenTerminationAllowed();
486}
487
[email protected]c0588052008-10-27 23:01:50488void TabContents::OpenURL(const GURL& url, const GURL& referrer,
[email protected]d5f942ba2008-09-26 19:30:34489 WindowOpenDisposition disposition,
490 PageTransition::Type transition) {
[email protected]1c642b52011-04-15 09:05:49491 if (delegate_) {
[email protected]c0588052008-10-27 23:01:50492 delegate_->OpenURLFromTab(this, url, referrer, disposition, transition);
[email protected]1c642b52011-04-15 09:05:49493 // Notify observers.
494 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
495 DidOpenURL(url, referrer, disposition, transition));
496 }
[email protected]d5f942ba2008-09-26 19:30:34497}
498
[email protected]1ccb3568d2010-02-19 10:51:16499bool TabContents::NavigateToPendingEntry(
500 NavigationController::ReloadType reload_type) {
[email protected]876bc832010-09-07 16:29:54501 return NavigateToEntry(*controller_.pending_entry(), reload_type);
502}
[email protected]96d185d2009-04-24 03:28:54503
[email protected]876bc832010-09-07 16:29:54504bool TabContents::NavigateToEntry(
505 const NavigationEntry& entry,
506 NavigationController::ReloadType reload_type) {
[email protected]96d185d2009-04-24 03:28:54507 RenderViewHost* dest_render_view_host = render_manager_.Navigate(entry);
508 if (!dest_render_view_host)
509 return false; // Unable to create the desired render view host.
510
[email protected]770dd8b2010-05-24 18:11:39511 if (delegate_ && delegate_->ShouldEnablePreferredSizeNotifications()) {
[email protected]216813952011-05-19 22:21:26512 dest_render_view_host->Send(new ViewMsg_EnablePreferredSizeChangedMode(
513 dest_render_view_host->routing_id(),
514 kPreferredSizeWidth | kPreferredSizeHeightThisIsSlow));
[email protected]770dd8b2010-05-24 18:11:39515 }
[email protected]9fb325e2010-05-06 18:23:24516
[email protected]80a8fad2011-01-29 04:02:38517 // For security, we should never send non-Web-UI URLs to a Web UI renderer.
[email protected]05fdd492010-11-15 17:52:07518 // Double check that here.
[email protected]54ec6472010-04-09 19:39:58519 int enabled_bindings = dest_render_view_host->enabled_bindings();
[email protected]1fd1a502011-03-30 16:55:56520 bool is_allowed_in_web_ui_renderer = content::GetContentClient()->
521 browser()->GetWebUIFactory()->IsURLAcceptableForWebUI(profile(),
522 entry.url());
[email protected]c09163a2011-02-15 00:05:55523 CHECK(!BindingsPolicy::is_web_ui_enabled(enabled_bindings) ||
524 is_allowed_in_web_ui_renderer);
[email protected]54ec6472010-04-09 19:39:58525
[email protected]96d185d2009-04-24 03:28:54526 // Tell DevTools agent that it is attached prior to the navigation.
[email protected]d9f9b792009-06-24 13:17:12527 DevToolsManager* devtools_manager = DevToolsManager::GetInstance();
[email protected]7e8e6b62009-05-08 11:28:32528 if (devtools_manager) { // NULL in unit tests.
[email protected]3cc72b12010-03-18 23:03:00529 devtools_manager->OnNavigatingToPendingEntry(render_view_host(),
530 dest_render_view_host,
531 entry.url());
[email protected]7e8e6b62009-05-08 11:28:32532 }
[email protected]96d185d2009-04-24 03:28:54533
534 // Used for page load time metrics.
535 current_load_start_ = base::TimeTicks::Now();
536
537 // Navigate in the desired RenderViewHost.
[email protected]056de2d2009-06-26 16:41:34538 ViewMsg_Navigate_Params navigate_params;
[email protected]876bc832010-09-07 16:29:54539 MakeNavigateParams(entry, controller_, reload_type, &navigate_params);
[email protected]52c68652010-12-07 17:47:04540 if (delegate_) {
541 navigate_params.extra_headers =
542 delegate_->GetNavigationHeaders(navigate_params.url);
543 }
[email protected]056de2d2009-06-26 16:41:34544 dest_render_view_host->Navigate(navigate_params);
[email protected]96d185d2009-04-24 03:28:54545
546 if (entry.page_id() == -1) {
547 // HACK!! This code suppresses javascript: URLs from being added to
548 // session history, which is what we want to do for javascript: URLs that
549 // do not generate content. What we really need is a message from the
550 // renderer telling us that a new page was not created. The same message
551 // could be used for mailto: URLs and the like.
552 if (entry.url().SchemeIs(chrome::kJavaScriptScheme))
553 return false;
554 }
555
[email protected]3c9e1872010-11-18 16:17:49556 // Notify observers about navigation.
[email protected]b375c5d2011-05-03 21:15:04557 FOR_EACH_OBSERVER(TabContentsObserver,
558 observers_,
559 NavigateToPendingEntry(entry.url(), reload_type));
[email protected]96d185d2009-04-24 03:28:54560
[email protected]d5f942ba2008-09-26 19:30:34561 return true;
562}
563
[email protected]96d185d2009-04-24 03:28:54564void TabContents::Stop() {
565 render_manager_.Stop();
[email protected]2e3f4572011-03-25 19:24:47566 FOR_EACH_OBSERVER(TabContentsObserver, observers_, StopNavigation());
[email protected]96d185d2009-04-24 03:28:54567}
568
[email protected]96d185d2009-04-24 03:28:54569TabContents* TabContents::Clone() {
570 // We create a new SiteInstance so that the new tab won't share processes
571 // with the old one. This can be changed in the future if we need it to share
572 // processes for some reason.
[email protected]420ae012009-04-24 05:16:32573 TabContents* tc = new TabContents(profile(),
[email protected]96d185d2009-04-24 03:28:54574 SiteInstance::CreateSiteInstance(profile()),
[email protected]6ee12c42010-09-14 09:36:07575 MSG_ROUTING_NONE, this, NULL);
[email protected]96d185d2009-04-24 03:28:54576 tc->controller().CopyStateFrom(controller_);
577 return tc;
578}
579
[email protected]4d677202009-07-19 07:37:12580void TabContents::ShowPageInfo(const GURL& url,
581 const NavigationEntry::SSLStatus& ssl,
582 bool show_history) {
583 if (!delegate_)
584 return;
585
[email protected]bb678332009-07-21 00:15:50586 delegate_->ShowPageInfo(profile(), url, ssl, show_history);
[email protected]4d677202009-07-19 07:37:12587}
588
initial.commit09911bf2008-07-26 23:55:29589ConstrainedWindow* TabContents::CreateConstrainedDialog(
[email protected]e8382172009-06-19 22:16:28590 ConstrainedWindowDelegate* delegate) {
initial.commit09911bf2008-07-26 23:55:29591 ConstrainedWindow* window =
[email protected]e8382172009-06-19 22:16:28592 ConstrainedWindow::CreateConstrainedDialog(this, delegate);
[email protected]aed59602011-02-28 22:57:33593 AddConstrainedDialog(window);
594 return window;
595}
596
597void TabContents::AddConstrainedDialog(ConstrainedWindow* window) {
initial.commit09911bf2008-07-26 23:55:29598 child_windows_.push_back(window);
[email protected]fa1cf0b82010-01-15 21:49:44599
600 if (child_windows_.size() == 1) {
601 window->ShowConstrainedWindow();
602 BlockTabContent(true);
603 }
initial.commit09911bf2008-07-26 23:55:29604}
605
[email protected]fa1cf0b82010-01-15 21:49:44606void TabContents::BlockTabContent(bool blocked) {
[email protected]8dccd7cb2010-02-25 05:19:55607 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
[email protected]1b072562010-12-13 19:37:45608 // 70% opaque grey.
609 SkColor greyish = SkColorSetARGB(178, 0, 0, 0);
[email protected]8dccd7cb2010-02-25 05:19:55610 if (rwhv)
[email protected]1b072562010-12-13 19:37:45611 rwhv->SetVisuallyDeemphasized(blocked ? &greyish : NULL, false);
[email protected]a7eccac2011-04-15 03:05:19612 // RenderViewHost may be NULL during shutdown.
613 if (render_view_host())
614 render_view_host()->set_ignore_input_events(blocked);
[email protected]fa1cf0b82010-01-15 21:49:44615 if (delegate_)
616 delegate_->SetTabContentBlocked(this, blocked);
617}
618
[email protected]e7cfdbd2011-04-22 14:41:37619void TabContents::AddNewContents(TabContents* new_contents,
620 WindowOpenDisposition disposition,
621 const gfx::Rect& initial_pos,
622 bool user_gesture) {
initial.commit09911bf2008-07-26 23:55:29623 if (!delegate_)
624 return;
625
[email protected]e7cfdbd2011-04-22 14:41:37626 delegate_->AddNewContents(this, new_contents, disposition, initial_pos,
627 user_gesture);
[email protected]96d185d2009-04-24 03:28:54628}
629
[email protected]5c9e97a2009-09-09 23:48:30630gfx::NativeView TabContents::GetContentNativeView() const {
[email protected]96d185d2009-04-24 03:28:54631 return view_->GetContentNativeView();
632}
633
634gfx::NativeView TabContents::GetNativeView() const {
635 return view_->GetNativeView();
636}
637
638void TabContents::GetContainerBounds(gfx::Rect *out) const {
639 view_->GetContainerBounds(out);
640}
641
642void TabContents::Focus() {
643 view_->Focus();
644}
645
[email protected]90daadb42009-06-08 21:27:28646void TabContents::FocusThroughTabTraversal(bool reverse) {
[email protected]7e383692009-06-12 19:14:54647 if (showing_interstitial_page()) {
648 render_manager_.interstitial_page()->FocusThroughTabTraversal(reverse);
649 return;
650 }
[email protected]96d185d2009-04-24 03:28:54651 render_view_host()->SetInitialFocus(reverse);
652}
653
654bool TabContents::FocusLocationBarByDefault() {
[email protected]d0980792011-02-13 19:41:40655 WebUI* web_ui = GetWebUIForCurrentState();
[email protected]7ade2732011-02-10 00:13:58656 if (web_ui)
657 return web_ui->focus_location_bar_by_default();
[email protected]3e3f0eb2009-06-22 18:33:43658 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]e0d481582009-09-15 21:06:25659 if (entry && entry->url() == GURL(chrome::kAboutBlankURL))
[email protected]3e3f0eb2009-06-22 18:33:43660 return true;
[email protected]96d185d2009-04-24 03:28:54661 return false;
662}
663
[email protected]a26dc362010-04-23 01:48:58664void TabContents::SetFocusToLocationBar(bool select_all) {
[email protected]a11aaf22010-03-30 00:03:38665 if (delegate())
[email protected]a26dc362010-04-23 01:48:58666 delegate()->SetFocusToLocationBar(select_all);
[email protected]a11aaf22010-03-30 00:03:38667}
668
[email protected]20ede072009-10-06 00:28:35669bool TabContents::ShouldShowBookmarkBar() {
[email protected]aa9196f2009-10-22 16:20:27670 if (showing_interstitial_page())
671 return false;
672
[email protected]d0980792011-02-13 19:41:40673 // See GetWebUIForCurrentState() comment for more info. This case is very
[email protected]96d185d2009-04-24 03:28:54674 // similar, but for non-first loads, we want to use the committed entry. This
675 // is so the bookmarks bar disappears at the same time the page does.
676 if (controller_.GetLastCommittedEntry()) {
[email protected]e0112912011-02-02 22:54:35677 // Not the first load, always use the committed Web UI.
[email protected]7ade2732011-02-10 00:13:58678 return (render_manager_.web_ui() == NULL) ?
679 false : render_manager_.web_ui()->force_bookmark_bar_visible();
[email protected]96d185d2009-04-24 03:28:54680 }
681
682 // When it's the first load, we know either the pending one or the committed
[email protected]d0980792011-02-13 19:41:40683 // one will have the Web UI in it (see GetWebUIForCurrentState), and only one
[email protected]96d185d2009-04-24 03:28:54684 // of them will be valid, so we can just check both.
[email protected]7ade2732011-02-10 00:13:58685 if (render_manager_.pending_web_ui())
686 return render_manager_.pending_web_ui()->force_bookmark_bar_visible();
687 return (render_manager_.web_ui() == NULL) ?
688 false : render_manager_.web_ui()->force_bookmark_bar_visible();
[email protected]96d185d2009-04-24 03:28:54689}
690
[email protected]d5f942ba2008-09-26 19:30:34691void TabContents::WillClose(ConstrainedWindow* window) {
[email protected]d6219912010-11-17 19:47:37692 ConstrainedWindowList::iterator i(
693 std::find(child_windows_.begin(), child_windows_.end(), window));
694 bool removed_topmost_window = i == child_windows_.begin();
695 if (i != child_windows_.end())
696 child_windows_.erase(i);
697 if (child_windows_.empty()) {
[email protected]fa1cf0b82010-01-15 21:49:44698 BlockTabContent(false);
[email protected]d6219912010-11-17 19:47:37699 } else {
700 if (removed_topmost_window)
701 child_windows_[0]->ShowConstrainedWindow();
702 BlockTabContent(true);
[email protected]fa1cf0b82010-01-15 21:49:44703 }
[email protected]66ba4932009-06-04 19:22:13704}
[email protected]d5f942ba2008-09-26 19:30:34705
[email protected]420ae012009-04-24 05:16:32706bool TabContents::IsActiveEntry(int32 page_id) {
707 NavigationEntry* active_entry = controller_.GetActiveEntry();
708 return (active_entry != NULL &&
709 active_entry->site_instance() == GetSiteInstance() &&
710 active_entry->page_id() == page_id);
711}
712
[email protected]b2fe07d12010-02-09 14:38:08713void TabContents::SetOverrideEncoding(const std::string& encoding) {
[email protected]8cb5d5b2010-02-09 11:36:16714 set_encoding(encoding);
[email protected]216813952011-05-19 22:21:26715 render_view_host()->Send(new ViewMsg_SetPageEncoding(
716 render_view_host()->routing_id(), encoding));
[email protected]8cb5d5b2010-02-09 11:36:16717}
718
[email protected]b2fe07d12010-02-09 14:38:08719void TabContents::ResetOverrideEncoding() {
[email protected]8cb5d5b2010-02-09 11:36:16720 reset_encoding();
[email protected]216813952011-05-19 22:21:26721 render_view_host()->Send(new ViewMsg_ResetPageEncodingToDefault(
722 render_view_host()->routing_id()));
[email protected]8cb5d5b2010-02-09 11:36:16723}
724
[email protected]7ab1e7d62009-10-14 23:32:01725void TabContents::OnCloseStarted() {
726 if (tab_close_start_time_.is_null())
727 tab_close_start_time_ = base::TimeTicks::Now();
728}
729
[email protected]46624bf2010-06-09 16:04:19730bool TabContents::ShouldAcceptDragAndDrop() const {
731#if defined(OS_CHROMEOS)
732 // ChromeOS panels (pop-ups) do not take drag-n-drop.
733 // See http://crosbug.com/2413
[email protected]b35b26b32011-05-05 20:35:14734 if (delegate() && delegate()->IsPopupOrPanel(this))
[email protected]b83af492010-10-09 03:21:20735 return false;
736 return true;
[email protected]46624bf2010-06-09 16:04:19737#else
738 return true;
739#endif
740}
741
[email protected]7813bd72011-02-05 02:19:34742void TabContents::SystemDragEnded() {
743 if (render_view_host())
744 render_view_host()->DragSourceSystemDragEnded();
745 if (delegate())
746 delegate()->DragEnded();
747}
748
[email protected]d0b8d092010-10-25 04:05:17749double TabContents::GetZoomLevel() const {
[email protected]b75b8292010-10-01 07:28:25750 HostZoomMap* zoom_map = profile()->GetHostZoomMap();
751 if (!zoom_map)
[email protected]d0b8d092010-10-25 04:05:17752 return 0;
[email protected]b75b8292010-10-01 07:28:25753
754 double zoom_level;
755 if (temporary_zoom_settings_) {
756 zoom_level = zoom_map->GetTemporaryZoomLevel(
757 GetRenderProcessHost()->id(), render_view_host()->routing_id());
758 } else {
[email protected]13ffa32e2011-05-27 16:37:01759 zoom_level = zoom_map->GetZoomLevel(net::GetHostOrSpecFromURL(GetURL()));
[email protected]b75b8292010-10-01 07:28:25760 }
[email protected]d0b8d092010-10-25 04:05:17761 return zoom_level;
762}
[email protected]b75b8292010-10-01 07:28:25763
[email protected]d0b8d092010-10-25 04:05:17764int TabContents::GetZoomPercent(bool* enable_increment,
765 bool* enable_decrement) {
766 *enable_decrement = *enable_increment = false;
[email protected]b75b8292010-10-01 07:28:25767 int percent = static_cast<int>(
[email protected]d0b8d092010-10-25 04:05:17768 WebKit::WebView::zoomLevelToZoomFactor(GetZoomLevel()) * 100);
[email protected]b75b8292010-10-01 07:28:25769 *enable_decrement = percent > minimum_zoom_percent_;
770 *enable_increment = percent < maximum_zoom_percent_;
771 return percent;
772}
773
[email protected]3c733bde2010-12-21 19:56:31774void TabContents::ViewSource() {
[email protected]1788e772010-12-15 16:40:50775 if (!delegate_)
776 return;
777
778 NavigationEntry* active_entry = controller().GetActiveEntry();
779 if (!active_entry)
780 return;
781
782 delegate_->ViewSourceForTab(this, active_entry->url());
[email protected]77d8d622010-12-15 10:30:12783}
784
[email protected]932b7a12011-03-09 12:50:27785void TabContents::ViewFrameSource(const GURL& url,
786 const std::string& content_state) {
787 if (!delegate_)
788 return;
789
790 delegate_->ViewSourceForFrame(this, url, content_state);
791}
792
[email protected]c40d6232011-03-25 00:16:21793void TabContents::SetContentRestrictions(int restrictions) {
794 content_restrictions_ = restrictions;
795 delegate()->ContentRestrictionsChanged(this);
796}
797
[email protected]724159a2010-12-30 01:11:18798void TabContents::OnDidStartProvisionalLoadForFrame(int64 frame_id,
799 bool is_main_frame,
[email protected]eacb080b2011-05-22 19:40:26800 bool has_opener_set,
[email protected]724159a2010-12-30 01:11:18801 const GURL& url) {
802 bool is_error_page = (url.spec() == chrome::kUnreachableWebDataURL);
803 GURL validated_url(url);
804 render_view_host()->FilterURL(ChildProcessSecurityPolicy::GetInstance(),
805 GetRenderProcessHost()->id(), &validated_url);
806
[email protected]8093a542011-05-13 07:29:32807 RenderViewHost* rvh =
808 render_manager_.pending_render_view_host() ?
809 render_manager_.pending_render_view_host() : render_view_host();
[email protected]0d60f0192011-04-14 12:40:10810 // Notify observers about the start of the provisional load.
811 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
812 DidStartProvisionalLoadForFrame(frame_id, is_main_frame,
[email protected]8093a542011-05-13 07:29:32813 validated_url, is_error_page, rvh));
[email protected]0d60f0192011-04-14 12:40:10814
[email protected]724159a2010-12-30 01:11:18815 if (is_main_frame) {
[email protected]4850a7f2011-03-08 23:36:59816 // Notify observers about the provisional change in the main frame URL.
817 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
[email protected]eacb080b2011-05-22 19:40:26818 ProvisionalChangeToMainFrameUrl(url, has_opener_set));
[email protected]724159a2010-12-30 01:11:18819 }
820}
821
822void TabContents::OnDidRedirectProvisionalLoad(int32 page_id,
[email protected]eacb080b2011-05-22 19:40:26823 bool has_opener_set,
[email protected]724159a2010-12-30 01:11:18824 const GURL& source_url,
825 const GURL& target_url) {
[email protected]18ba2b12011-04-06 16:35:49826 // TODO(creis): Remove this method and have the pre-rendering code listen to
827 // the ResourceDispatcherHost's RESOURCE_RECEIVED_REDIRECT notification
828 // instead. See http://crbug.com/78512.
[email protected]724159a2010-12-30 01:11:18829 NavigationEntry* entry;
830 if (page_id == -1)
831 entry = controller_.pending_entry();
832 else
833 entry = controller_.GetEntryWithPageID(GetSiteInstance(), page_id);
834 if (!entry || entry->url() != source_url)
835 return;
[email protected]e7d50892011-01-19 21:47:38836
[email protected]4850a7f2011-03-08 23:36:59837 // Notify observers about the provisional change in the main frame URL.
838 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
[email protected]eacb080b2011-05-22 19:40:26839 ProvisionalChangeToMainFrameUrl(target_url,
840 has_opener_set));
[email protected]724159a2010-12-30 01:11:18841}
842
843void TabContents::OnDidFailProvisionalLoadWithError(
844 int64 frame_id,
845 bool is_main_frame,
846 int error_code,
847 const GURL& url,
848 bool showing_repost_interstitial) {
849 VLOG(1) << "Failed Provisional Load: " << url.possibly_invalid_spec()
850 << ", error_code: " << error_code
851 << " is_main_frame: " << is_main_frame
852 << " showing_repost_interstitial: " << showing_repost_interstitial
853 << " frame_id: " << frame_id;
854 GURL validated_url(url);
855 render_view_host()->FilterURL(ChildProcessSecurityPolicy::GetInstance(),
856 GetRenderProcessHost()->id(), &validated_url);
857
858 if (net::ERR_ABORTED == error_code) {
859 // EVIL HACK ALERT! Ignore failed loads when we're showing interstitials.
860 // This means that the interstitial won't be torn down properly, which is
861 // bad. But if we have an interstitial, go back to another tab type, and
862 // then load the same interstitial again, we could end up getting the first
863 // interstitial's "failed" message (as a result of the cancel) when we're on
864 // the second one.
865 //
866 // We can't tell this apart, so we think we're tearing down the current page
867 // which will cause a crash later one. There is also some code in
868 // RenderViewHostManager::RendererAbortedProvisionalLoad that is commented
869 // out because of this problem.
870 //
871 // http://code.google.com/p/chromium/issues/detail?id=2855
872 // Because this will not tear down the interstitial properly, if "back" is
873 // back to another tab type, the interstitial will still be somewhat alive
874 // in the previous tab type. If you navigate somewhere that activates the
875 // tab with the interstitial again, you'll see a flash before the new load
876 // commits of the interstitial page.
877 if (showing_interstitial_page()) {
878 LOG(WARNING) << "Discarding message during interstitial.";
879 return;
880 }
881
882 // This will discard our pending entry if we cancelled the load (e.g., if we
883 // decided to download the file instead of load it). Only discard the
884 // pending entry if the URLs match, otherwise the user initiated a navigate
885 // before the page loaded so that the discard would discard the wrong entry.
886 NavigationEntry* pending_entry = controller_.pending_entry();
887 if (pending_entry && pending_entry->url() == validated_url) {
[email protected]c95fa8b2011-04-28 20:26:16888 DidCancelLoading();
[email protected]724159a2010-12-30 01:11:18889 }
890
891 render_manager_.RendererAbortedProvisionalLoad(render_view_host());
892 }
893
894 // Send out a notification that we failed a provisional load with an error.
895 ProvisionalLoadDetails details(
896 is_main_frame, controller_.IsURLInPageNavigation(validated_url),
[email protected]b9e8ea62011-03-04 06:29:09897 validated_url, std::string(), false, frame_id);
[email protected]724159a2010-12-30 01:11:18898 details.set_error_code(error_code);
899
900 NotificationService::current()->Notify(
901 NotificationType::FAIL_PROVISIONAL_LOAD_WITH_ERROR,
902 Source<NavigationController>(&controller_),
903 Details<ProvisionalLoadDetails>(&details));
[email protected]0d60f0192011-04-14 12:40:10904
905 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
906 DidFailProvisionalLoad(frame_id, is_main_frame,
907 validated_url, error_code));
[email protected]724159a2010-12-30 01:11:18908}
909
910void TabContents::OnDidLoadResourceFromMemoryCache(
911 const GURL& url,
[email protected]724159a2010-12-30 01:11:18912 const std::string& security_info) {
[email protected]28620862011-03-22 23:07:19913 base::StatsCounter cache("WebKit.CacheHit");
[email protected]724159a2010-12-30 01:11:18914 cache.Increment();
915
916 // Send out a notification that we loaded a resource from our memory cache.
917 int cert_id = 0, cert_status = 0, security_bits = -1, connection_status = 0;
918 SSLManager::DeserializeSecurityInfo(security_info,
919 &cert_id, &cert_status,
920 &security_bits,
921 &connection_status);
[email protected]7a8c55e2011-02-15 08:21:16922 LoadFromMemoryCacheDetails details(url, GetRenderProcessHost()->id(),
923 cert_id, cert_status);
[email protected]724159a2010-12-30 01:11:18924
925 NotificationService::current()->Notify(
926 NotificationType::LOAD_FROM_MEMORY_CACHE,
927 Source<NavigationController>(&controller_),
928 Details<LoadFromMemoryCacheDetails>(&details));
929}
930
931void TabContents::OnDidDisplayInsecureContent() {
[email protected]221a5d92011-04-28 18:23:16932 UserMetrics::RecordAction(UserMetricsAction("SSL.DisplayedInsecureContent"));
[email protected]724159a2010-12-30 01:11:18933 displayed_insecure_content_ = true;
934 SSLManager::NotifySSLInternalStateChanged();
935}
936
937void TabContents::OnDidRunInsecureContent(
[email protected]92771112011-01-20 00:13:02938 const std::string& security_origin, const GURL& target_url) {
[email protected]9941cf52011-02-08 16:17:23939 LOG(INFO) << security_origin << " ran insecure content from "
940 << target_url.possibly_invalid_spec();
[email protected]221a5d92011-04-28 18:23:16941 UserMetrics::RecordAction(UserMetricsAction("SSL.RanInsecureContent"));
[email protected]724159a2010-12-30 01:11:18942 controller_.ssl_manager()->DidRunInsecureContent(security_origin);
943}
944
945void TabContents::OnDocumentLoadedInFrame(int64 frame_id) {
946 controller_.DocumentLoadedInFrame();
[email protected]0d60f0192011-04-14 12:40:10947 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
948 DocumentLoadedInFrame(frame_id));
[email protected]724159a2010-12-30 01:11:18949}
950
951void TabContents::OnDidFinishLoad(int64 frame_id) {
[email protected]0d60f0192011-04-14 12:40:10952 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
953 DidFinishLoad(frame_id));
[email protected]724159a2010-12-30 01:11:18954}
955
[email protected]c8f73ab2011-01-22 00:05:17956void TabContents::OnUpdateContentRestrictions(int restrictions) {
[email protected]c40d6232011-03-25 00:16:21957 SetContentRestrictions(restrictions);
[email protected]c8f73ab2011-01-22 00:05:17958}
959
[email protected]216813952011-05-19 22:21:26960void TabContents::OnGoToEntryAtOffset(int offset) {
961 if (!delegate_ || delegate_->OnGoToEntryOffset(offset)) {
962 NavigationEntry* entry = controller_.GetEntryAtOffset(offset);
963 if (!entry)
964 return;
965 // Note that we don't call NavigationController::GotToOffset() as we don't
966 // want to create a pending navigation entry (it might end up lingering
967 // http://crbug.com/51680).
968 entry->set_transition_type(entry->transition_type() |
969 PageTransition::FORWARD_BACK);
970 NavigateToEntry(*entry, NavigationController::NO_RELOAD);
971 }
972}
973
974void TabContents::OnUpdateZoomLimits(int minimum_percent,
975 int maximum_percent,
976 bool remember) {
977 minimum_zoom_percent_ = minimum_percent;
978 maximum_zoom_percent_ = maximum_percent;
979 temporary_zoom_settings_ = !remember;
980}
981
982void TabContents::OnFocusedNodeChanged(bool is_editable_node) {
983 NotificationService::current()->Notify(
984 NotificationType::FOCUS_CHANGED_IN_PAGE,
985 Source<TabContents>(this),
986 Details<const bool>(&is_editable_node));
987}
988
[email protected]96d185d2009-04-24 03:28:54989// Notifies the RenderWidgetHost instance about the fact that the page is
990// loading, or done loading and calls the base implementation.
991void TabContents::SetIsLoading(bool is_loading,
992 LoadNotificationDetails* details) {
993 if (is_loading == is_loading_)
994 return;
995
996 if (!is_loading) {
997 load_state_ = net::LOAD_STATE_IDLE;
998 load_state_host_.clear();
[email protected]094e5b22009-09-25 04:23:56999 upload_size_ = 0;
1000 upload_position_ = 0;
[email protected]96d185d2009-04-24 03:28:541001 }
1002
1003 render_manager_.SetIsLoading(is_loading);
1004
1005 is_loading_ = is_loading;
1006 waiting_for_response_ = is_loading;
1007
[email protected]6ebdc9b2010-09-27 16:55:571008 if (delegate_)
1009 delegate_->LoadingStateChanged(this);
[email protected]329581b2009-04-28 06:52:351010 NotifyNavigationStateChanged(INVALIDATE_LOAD);
[email protected]96d185d2009-04-24 03:28:541011
1012 NotificationType type = is_loading ? NotificationType::LOAD_START :
1013 NotificationType::LOAD_STOP;
[email protected]ce5c4502009-05-06 16:46:111014 NotificationDetails det = NotificationService::NoDetails();
[email protected]96d185d2009-04-24 03:28:541015 if (details)
1016 det = Details<LoadNotificationDetails>(details);
1017 NotificationService::current()->Notify(type,
1018 Source<NavigationController>(&controller_),
1019 det);
1020}
1021
[email protected]d0980792011-02-13 19:41:401022WebUI* TabContents::GetWebUIForCurrentState() {
[email protected]7ade2732011-02-10 00:13:581023 // When there is a pending navigation entry, we want to use the pending WebUI
[email protected]96d185d2009-04-24 03:28:541024 // that goes along with it to control the basic flags. For example, we want to
1025 // show the pending URL in the URL bar, so we want the display_url flag to
1026 // be from the pending entry.
1027 //
1028 // The confusion comes because there are multiple possibilities for the
1029 // initial load in a tab as a side effect of the way the RenderViewHostManager
1030 // works.
1031 //
[email protected]e0112912011-02-02 22:54:351032 // - For the very first tab the load looks "normal". The new tab Web UI is
[email protected]96d185d2009-04-24 03:28:541033 // the pending one, and we want it to apply here.
1034 //
1035 // - For subsequent new tabs, they'll get a new SiteInstance which will then
1036 // get switched to the one previously associated with the new tab pages.
[email protected]7ade2732011-02-10 00:13:581037 // This switching will cause the manager to commit the RVH/WebUI. So we'll
[email protected]e0112912011-02-02 22:54:351038 // have a committed Web UI in this case.
[email protected]96d185d2009-04-24 03:28:541039 //
1040 // This condition handles all of these cases:
1041 //
1042 // - First load in first tab: no committed nav entry + pending nav entry +
1043 // pending dom ui:
[email protected]e0112912011-02-02 22:54:351044 // -> Use pending Web UI if any.
[email protected]96d185d2009-04-24 03:28:541045 //
1046 // - First load in second tab: no committed nav entry + pending nav entry +
[email protected]e0112912011-02-02 22:54:351047 // no pending Web UI:
1048 // -> Use the committed Web UI if any.
[email protected]96d185d2009-04-24 03:28:541049 //
1050 // - Second navigation in any tab: committed nav entry + pending nav entry:
[email protected]e0112912011-02-02 22:54:351051 // -> Use pending Web UI if any.
[email protected]96d185d2009-04-24 03:28:541052 //
1053 // - Normal state with no load: committed nav entry + no pending nav entry:
[email protected]e0112912011-02-02 22:54:351054 // -> Use committed Web UI.
[email protected]96d185d2009-04-24 03:28:541055 if (controller_.pending_entry() &&
1056 (controller_.GetLastCommittedEntry() ||
[email protected]7ade2732011-02-10 00:13:581057 render_manager_.pending_web_ui()))
1058 return render_manager_.pending_web_ui();
1059 return render_manager_.web_ui();
[email protected]96d185d2009-04-24 03:28:541060}
[email protected]420ae012009-04-24 05:16:321061
[email protected]1fd1a502011-03-30 16:55:561062WebUI::TypeID TabContents::GetWebUITypeForCurrentState() {
1063 return content::WebUIFactory::Get()->GetWebUIType(profile(), GetURL());
1064}
1065
[email protected]420ae012009-04-24 05:16:321066void TabContents::DidNavigateMainFramePostCommit(
[email protected]8286f51a2011-05-31 17:39:131067 const content::LoadCommittedDetails& details,
[email protected]420ae012009-04-24 05:16:321068 const ViewHostMsg_FrameNavigate_Params& params) {
[email protected]1fd1a502011-03-30 16:55:561069 if (opener_web_ui_type_ != WebUI::kNoWebUI) {
[email protected]80a8fad2011-01-29 04:02:381070 // If this is a window.open navigation, use the same WebUI as the renderer
[email protected]c2e74fe82009-09-03 17:57:441071 // that opened the window, as long as both renderers have the same
1072 // privileges.
[email protected]1fd1a502011-03-30 16:55:561073 if (delegate_ && opener_web_ui_type_ == GetWebUITypeForCurrentState()) {
1074 WebUI* web_ui = content::GetContentClient()->browser()->
1075 GetWebUIFactory()->CreateWebUIForURL(this, GetURL());
[email protected]7ade2732011-02-10 00:13:581076 // web_ui might be NULL if the URL refers to a non-existent extension.
1077 if (web_ui) {
[email protected]d0980792011-02-13 19:41:401078 render_manager_.SetWebUIPostCommit(web_ui);
[email protected]7ade2732011-02-10 00:13:581079 web_ui->RenderViewCreated(render_view_host());
[email protected]c2e74fe82009-09-03 17:57:441080 }
1081 }
[email protected]1fd1a502011-03-30 16:55:561082 opener_web_ui_type_ = WebUI::kNoWebUI;
[email protected]c2e74fe82009-09-03 17:57:441083 }
1084
[email protected]420ae012009-04-24 05:16:321085 if (details.is_user_initiated_main_frame_load()) {
1086 // Clear the status bubble. This is a workaround for a bug where WebKit
1087 // doesn't let us know that the cursor left an element during a
1088 // transition (this is also why the mouse cursor remains as a hand after
1089 // clicking on a link); see bugs 1184641 and 980803. We don't want to
1090 // clear the bubble when a user navigates to a named anchor in the same
1091 // page.
1092 UpdateTargetURL(details.entry->page_id(), GURL());
[email protected]420ae012009-04-24 05:16:321093 }
1094
[email protected]a6e82fc2010-02-24 22:28:141095 if (!details.is_in_page) {
[email protected]f17a0ee2010-05-17 17:38:471096 // Once the main frame is navigated, we're no longer considered to have
1097 // displayed insecure content.
1098 displayed_insecure_content_ = false;
[email protected]aece2c7f2009-08-27 20:43:171099 }
[email protected]ce5c4502009-05-06 16:46:111100
[email protected]8cc8d492010-02-02 10:40:491101 // Close constrained windows if necessary.
1102 if (!net::RegistryControlledDomainService::SameDomainOrHost(
1103 details.previous_url, details.entry->url()))
1104 CloseConstrainedWindows();
1105
[email protected]3c9e1872010-11-18 16:17:491106 // Notify observers about navigation.
[email protected]585b30362011-01-28 02:30:171107 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
[email protected]3c9e1872010-11-18 16:17:491108 DidNavigateMainFramePostCommit(details, params));
[email protected]420ae012009-04-24 05:16:321109}
1110
1111void TabContents::DidNavigateAnyFramePostCommit(
1112 RenderViewHost* render_view_host,
[email protected]8286f51a2011-05-31 17:39:131113 const content::LoadCommittedDetails& details,
[email protected]420ae012009-04-24 05:16:321114 const ViewHostMsg_FrameNavigate_Params& params) {
1115 // If we navigate, start showing messages again. This does nothing to prevent
1116 // a malicious script from spamming messages, since the script could just
1117 // reload the page to stop blocking.
1118 suppress_javascript_messages_ = false;
1119
[email protected]3c9e1872010-11-18 16:17:491120 // Notify observers about navigation.
[email protected]585b30362011-01-28 02:30:171121 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
[email protected]3c9e1872010-11-18 16:17:491122 DidNavigateAnyFramePostCommit(details, params));
[email protected]420ae012009-04-24 05:16:321123}
1124
[email protected]8cc8d492010-02-02 10:40:491125void TabContents::CloseConstrainedWindows() {
1126 // Clear out any constrained windows since we are leaving this page entirely.
[email protected]aed59602011-02-28 22:57:331127 // To ensure that we iterate over every element in child_windows_ we
1128 // need to use a copy of child_windows_. Otherwise if
1129 // window->CloseConstrainedWindow() modifies child_windows_ we could end up
1130 // skipping some elements.
1131 ConstrainedWindowList child_windows_copy(child_windows_);
1132 for (ConstrainedWindowList::iterator it = child_windows_copy.begin();
1133 it != child_windows_copy.end(); ++it) {
1134 ConstrainedWindow* window = *it;
[email protected]fa1cf0b82010-01-15 21:49:441135 if (window) {
[email protected]420ae012009-04-24 05:16:321136 window->CloseConstrainedWindow();
[email protected]fa1cf0b82010-01-15 21:49:441137 BlockTabContent(false);
1138 }
[email protected]420ae012009-04-24 05:16:321139 }
1140}
1141
[email protected]420ae012009-04-24 05:16:321142void TabContents::UpdateMaxPageIDIfNecessary(SiteInstance* site_instance,
1143 RenderViewHost* rvh) {
1144 // If we are creating a RVH for a restored controller, then we might
1145 // have more page IDs than the SiteInstance's current max page ID. We must
1146 // make sure that the max page ID is larger than any restored page ID.
1147 // Note that it is ok for conflicting page IDs to exist in another tab
1148 // (i.e., NavigationController), but if any page ID is larger than the max,
1149 // the back/forward list will get confused.
1150 int max_restored_page_id = controller_.max_restored_page_id();
1151 if (max_restored_page_id > 0) {
1152 int curr_max_page_id = site_instance->max_page_id();
1153 if (max_restored_page_id > curr_max_page_id) {
1154 // Need to update the site instance immediately.
1155 site_instance->UpdateMaxPageID(max_restored_page_id);
1156
1157 // Also tell the renderer to update its internal representation. We
1158 // need to reserve enough IDs to make all restored page IDs less than
1159 // the max.
1160 if (curr_max_page_id < 0)
1161 curr_max_page_id = 0;
[email protected]216813952011-05-19 22:21:261162 rvh->Send(new ViewMsg_ReservePageIDRange(
1163 rvh->routing_id(), max_restored_page_id - curr_max_page_id));
[email protected]420ae012009-04-24 05:16:321164 }
1165 }
1166}
1167
[email protected]6b2f7a82011-04-25 19:30:511168bool TabContents::UpdateTitleForEntry(NavigationEntry* entry,
1169 const std::wstring& title) {
[email protected]420ae012009-04-24 05:16:321170 // For file URLs without a title, use the pathname instead. In the case of a
1171 // synthesized title, we don't want the update to count toward the "one set
1172 // per page of the title to history."
[email protected]6b2f7a82011-04-25 19:30:511173 string16 final_title;
[email protected]420ae012009-04-24 05:16:321174 bool explicit_set;
[email protected]987fc3a2011-05-26 14:18:091175 if (entry && entry->url().SchemeIsFile() && title.empty()) {
[email protected]6b2f7a82011-04-25 19:30:511176 final_title = UTF8ToUTF16(entry->url().ExtractFileName());
[email protected]420ae012009-04-24 05:16:321177 explicit_set = false; // Don't count synthetic titles toward the set limit.
1178 } else {
[email protected]6b2f7a82011-04-25 19:30:511179 TrimWhitespace(WideToUTF16Hack(title), TRIM_ALL, &final_title);
[email protected]420ae012009-04-24 05:16:321180 explicit_set = true;
1181 }
1182
[email protected]987fc3a2011-05-26 14:18:091183 // If a page is created via window.open and never navigated,
1184 // there will be no navigation entry. In this situation,
1185 // |page_title_when_no_navigaiton_entry_| will be used for page title.
1186 if (entry) {
1187 if (final_title == entry->title())
1188 return false; // Nothing changed, don't bother.
[email protected]420ae012009-04-24 05:16:321189
[email protected]987fc3a2011-05-26 14:18:091190 entry->set_title(final_title);
1191 } else {
1192 if (page_title_when_no_navigation_entry_ == final_title)
1193 return false; // Nothing changed, don't bother.
1194
1195 page_title_when_no_navigation_entry_ = final_title;
1196 }
[email protected]420ae012009-04-24 05:16:321197
[email protected]420ae012009-04-24 05:16:321198 // Lastly, set the title for the view.
[email protected]6b2f7a82011-04-25 19:30:511199 view_->SetPageTitle(UTF16ToWideHack(final_title));
[email protected]420ae012009-04-24 05:16:321200
[email protected]105bb0f2011-05-24 17:12:141201 TitleUpdatedDetails details(entry, explicit_set);
1202
[email protected]cbc0e1b2010-04-12 18:33:041203 NotificationService::current()->Notify(
1204 NotificationType::TAB_CONTENTS_TITLE_UPDATED,
1205 Source<TabContents>(this),
[email protected]105bb0f2011-05-24 17:12:141206 Details<TitleUpdatedDetails>(&details));
[email protected]cbc0e1b2010-04-12 18:33:041207
[email protected]420ae012009-04-24 05:16:321208 return true;
1209}
1210
1211void TabContents::NotifySwapped() {
1212 // After sending out a swap notification, we need to send a disconnect
1213 // notification so that clients that pick up a pointer to |this| can NULL the
1214 // pointer. See Bug 1230284.
1215 notify_disconnection_ = true;
1216 NotificationService::current()->Notify(
[email protected]57c6a652009-05-04 07:58:341217 NotificationType::TAB_CONTENTS_SWAPPED,
[email protected]d82ed61e2009-06-16 02:46:221218 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:321219 NotificationService::NoDetails());
1220}
1221
1222void TabContents::NotifyConnected() {
1223 notify_disconnection_ = true;
1224 NotificationService::current()->Notify(
[email protected]57c6a652009-05-04 07:58:341225 NotificationType::TAB_CONTENTS_CONNECTED,
[email protected]d82ed61e2009-06-16 02:46:221226 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:321227 NotificationService::NoDetails());
1228}
1229
1230void TabContents::NotifyDisconnected() {
1231 if (!notify_disconnection_)
1232 return;
1233
1234 notify_disconnection_ = false;
1235 NotificationService::current()->Notify(
[email protected]57c6a652009-05-04 07:58:341236 NotificationType::TAB_CONTENTS_DISCONNECTED,
[email protected]d82ed61e2009-06-16 02:46:221237 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:321238 NotificationService::NoDetails());
1239}
1240
[email protected]8d3347f2009-07-09 22:00:211241RenderViewHostDelegate::View* TabContents::GetViewDelegate() {
[email protected]420ae012009-04-24 05:16:321242 return view_.get();
1243}
1244
[email protected]8d3347f2009-07-09 22:00:211245RenderViewHostDelegate::RendererManagement*
1246TabContents::GetRendererManagementDelegate() {
1247 return &render_manager_;
1248}
1249
[email protected]ced90ae12010-02-20 02:06:161250RenderViewHostDelegate::BookmarkDrag* TabContents::GetBookmarkDragDelegate() {
1251 return bookmark_drag_;
1252}
1253
1254void TabContents::SetBookmarkDragDelegate(
1255 RenderViewHostDelegate::BookmarkDrag* bookmark_drag) {
1256 bookmark_drag_ = bookmark_drag;
1257}
1258
[email protected]93623c5d2009-12-10 21:40:321259RendererPreferences TabContents::GetRendererPrefs(Profile* profile) const {
[email protected]840b1512009-07-21 16:53:461260 return renderer_preferences_;
[email protected]80d96fa2009-06-10 22:34:511261}
1262
[email protected]57c6a652009-05-04 07:58:341263TabContents* TabContents::GetAsTabContents() {
1264 return this;
1265}
1266
[email protected]7b291f92009-08-14 05:43:531267ViewType::Type TabContents::GetRenderViewType() const {
1268 return ViewType::TAB_CONTENTS;
1269}
1270
1271int TabContents::GetBrowserWindowID() const {
1272 return controller().window_id().id();
1273}
1274
[email protected]420ae012009-04-24 05:16:321275void TabContents::RenderViewCreated(RenderViewHost* render_view_host) {
[email protected]2e4633c2009-07-09 16:58:061276 NotificationService::current()->Notify(
1277 NotificationType::RENDER_VIEW_HOST_CREATED_FOR_TAB,
1278 Source<TabContents>(this),
1279 Details<RenderViewHost>(render_view_host));
[email protected]420ae012009-04-24 05:16:321280 NavigationEntry* entry = controller_.GetActiveEntry();
1281 if (!entry)
1282 return;
1283
1284 // When we're creating views, we're still doing initial setup, so we always
[email protected]e0112912011-02-02 22:54:351285 // use the pending Web UI rather than any possibly existing committed one.
[email protected]1fd1a502011-03-30 16:55:561286 if (render_manager_.pending_web_ui())
[email protected]7ade2732011-02-10 00:13:581287 render_manager_.pending_web_ui()->RenderViewCreated(render_view_host);
[email protected]420ae012009-04-24 05:16:321288
1289 if (entry->IsViewSourceMode()) {
1290 // Put the renderer in view source mode.
1291 render_view_host->Send(
1292 new ViewMsg_EnableViewSourceMode(render_view_host->routing_id()));
1293 }
[email protected]0666aef2009-05-13 19:48:081294
1295 view()->RenderViewCreated(render_view_host);
[email protected]11f764bb2011-05-25 19:45:261296
1297 FOR_EACH_OBSERVER(
1298 TabContentsObserver, observers_, RenderViewCreated(render_view_host));
[email protected]420ae012009-04-24 05:16:321299}
1300
1301void TabContents::RenderViewReady(RenderViewHost* rvh) {
1302 if (rvh != render_view_host()) {
1303 // Don't notify the world, since this came from a renderer in the
1304 // background.
1305 return;
1306 }
1307
1308 NotifyConnected();
[email protected]9d8a4642009-07-29 17:25:301309 bool was_crashed = is_crashed();
[email protected]443b80e2010-12-14 00:42:231310 SetIsCrashed(base::TERMINATION_STATUS_STILL_RUNNING, 0);
[email protected]9d8a4642009-07-29 17:25:301311
1312 // Restore the focus to the tab (otherwise the focus will be on the top
1313 // window).
[email protected]484ae5912010-09-29 19:16:141314 if (was_crashed && !FocusLocationBarByDefault() &&
1315 (!delegate_ || delegate_->ShouldFocusPageAfterCrash())) {
[email protected]9d8a4642009-07-29 17:25:301316 Focus();
[email protected]484ae5912010-09-29 19:16:141317 }
[email protected]420ae012009-04-24 05:16:321318}
1319
[email protected]443b80e2010-12-14 00:42:231320void TabContents::RenderViewGone(RenderViewHost* rvh,
1321 base::TerminationStatus status,
1322 int error_code) {
[email protected]420ae012009-04-24 05:16:321323 if (rvh != render_view_host()) {
1324 // The pending page's RenderViewHost is gone.
1325 return;
1326 }
1327
[email protected]2e3f4572011-03-25 19:24:471328 // Let observers know first and give them a chance to act.
1329 FOR_EACH_OBSERVER(TabContentsObserver, observers_, RenderViewGone());
1330
[email protected]420ae012009-04-24 05:16:321331 SetIsLoading(false, NULL);
1332 NotifyDisconnected();
[email protected]443b80e2010-12-14 00:42:231333 SetIsCrashed(status, error_code);
[email protected]420ae012009-04-24 05:16:321334
[email protected]8e537302009-06-10 21:19:581335 // Tell the view that we've crashed so it can prepare the sad tab page.
[email protected]cf68b092010-01-15 20:05:551336 // Only do this if we're not in browser shutdown, so that TabContents
1337 // objects that are not in a browser (e.g., HTML dialogs) and thus are
1338 // visible do not flash a sad tab page.
1339 if (browser_shutdown::GetShutdownType() == browser_shutdown::NOT_VALID)
[email protected]a8f024392011-01-13 21:50:161340 view_->OnTabCrashed(status, error_code);
[email protected]420ae012009-04-24 05:16:321341
1342 // Hide any visible hung renderer warning for this web contents' process.
[email protected]7ec6bef2011-02-26 02:22:291343 browser::HideHungRendererDialog(this);
[email protected]420ae012009-04-24 05:16:321344}
1345
[email protected]2e4633c2009-07-09 16:58:061346void TabContents::RenderViewDeleted(RenderViewHost* rvh) {
[email protected]fb7b79f72009-11-06 18:00:481347 render_manager_.RenderViewDeleted(rvh);
[email protected]2e4633c2009-07-09 16:58:061348}
1349
[email protected]420ae012009-04-24 05:16:321350void TabContents::DidNavigate(RenderViewHost* rvh,
1351 const ViewHostMsg_FrameNavigate_Params& params) {
[email protected]8030f012009-09-25 18:09:371352 int extra_invalidate_flags = 0;
1353
1354 if (PageTransition::IsMainFrame(params.transition)) {
[email protected]20ede072009-10-06 00:28:351355 bool was_bookmark_bar_visible = ShouldShowBookmarkBar();
[email protected]8030f012009-09-25 18:09:371356
[email protected]420ae012009-04-24 05:16:321357 render_manager_.DidNavigateMainFrame(rvh);
1358
[email protected]20ede072009-10-06 00:28:351359 if (was_bookmark_bar_visible != ShouldShowBookmarkBar())
[email protected]8030f012009-09-25 18:09:371360 extra_invalidate_flags |= INVALIDATE_BOOKMARK_BAR;
[email protected]8030f012009-09-25 18:09:371361 }
1362
[email protected]420ae012009-04-24 05:16:321363 // Update the site of the SiteInstance if it doesn't have one yet.
1364 if (!GetSiteInstance()->has_site())
1365 GetSiteInstance()->SetSite(params.url);
1366
1367 // Need to update MIME type here because it's referred to in
1368 // UpdateNavigationCommands() called by RendererDidNavigate() to
1369 // determine whether or not to enable the encoding menu.
1370 // It's updated only for the main frame. For a subframe,
1371 // RenderView::UpdateURL does not set params.contents_mime_type.
1372 // (see http://code.google.com/p/chromium/issues/detail?id=2929 )
1373 // TODO(jungshik): Add a test for the encoding menu to avoid
1374 // regressing it again.
1375 if (PageTransition::IsMainFrame(params.transition))
1376 contents_mime_type_ = params.contents_mime_type;
1377
[email protected]8286f51a2011-05-31 17:39:131378 content::LoadCommittedDetails details;
[email protected]8030f012009-09-25 18:09:371379 bool did_navigate = controller_.RendererDidNavigate(
1380 params, extra_invalidate_flags, &details);
[email protected]3e90d4a2009-07-03 17:38:391381
[email protected]a9c0bfe2010-09-17 08:35:221382 // Send notification about committed provisional loads. This notification is
1383 // different from the NAV_ENTRY_COMMITTED notification which doesn't include
1384 // the actual URL navigated to and isn't sent for AUTO_SUBFRAME navigations.
1385 if (details.type != NavigationType::NAV_IGNORE) {
[email protected]dabb0d12010-10-05 12:50:071386 // For AUTO_SUBFRAME navigations, an event for the main frame is generated
1387 // that is not recorded in the navigation history. For the purpose of
1388 // tracking navigation events, we treat this event as a sub frame navigation
1389 // event.
1390 bool is_main_frame = did_navigate ? details.is_main_frame : false;
[email protected]0d60f0192011-04-14 12:40:101391 PageTransition::Type transition_type = params.transition;
[email protected]a9c0bfe2010-09-17 08:35:221392 // Whether or not a page transition was triggered by going backward or
1393 // forward in the history is only stored in the navigation controller's
1394 // entry list.
1395 if (did_navigate &&
1396 (controller_.GetActiveEntry()->transition_type() &
1397 PageTransition::FORWARD_BACK)) {
[email protected]0d60f0192011-04-14 12:40:101398 transition_type = params.transition | PageTransition::FORWARD_BACK;
[email protected]a9c0bfe2010-09-17 08:35:221399 }
[email protected]0d60f0192011-04-14 12:40:101400 // Notify observers about the commit of the provisional load.
1401 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
1402 DidCommitProvisionalLoadForFrame(params.frame_id,
1403 is_main_frame, params.url, transition_type));
[email protected]a9c0bfe2010-09-17 08:35:221404 }
1405
[email protected]76543b92009-08-31 17:27:451406 if (!did_navigate)
1407 return; // No navigation happened.
[email protected]420ae012009-04-24 05:16:321408
1409 // DO NOT ADD MORE STUFF TO THIS FUNCTION! Your component should either listen
1410 // for the appropriate notification (best) or you can add it to
1411 // DidNavigateMainFramePostCommit / DidNavigateAnyFramePostCommit (only if
1412 // necessary, please).
1413
1414 // Run post-commit tasks.
1415 if (details.is_main_frame)
1416 DidNavigateMainFramePostCommit(details, params);
1417 DidNavigateAnyFramePostCommit(rvh, details, params);
1418}
1419
1420void TabContents::UpdateState(RenderViewHost* rvh,
1421 int32 page_id,
1422 const std::string& state) {
[email protected]992db4c2011-05-12 15:37:151423 // Ensure that this state update comes from either the active RVH or one of
1424 // the swapped out RVHs. We don't expect to hear from any other RVHs.
1425 DCHECK(rvh == render_view_host() || render_manager_.IsSwappedOut(rvh));
[email protected]420ae012009-04-24 05:16:321426
1427 // We must be prepared to handle state updates for any page, these occur
1428 // when the user is scrolling and entering form data, as well as when we're
1429 // leaving a page, in which case our state may have already been moved to
1430 // the next page. The navigation controller will look up the appropriate
1431 // NavigationEntry and update it when it is notified via the delegate.
1432
1433 int entry_index = controller_.GetEntryIndexWithPageID(
[email protected]992db4c2011-05-12 15:37:151434 rvh->site_instance(), page_id);
[email protected]420ae012009-04-24 05:16:321435 if (entry_index < 0)
1436 return;
1437 NavigationEntry* entry = controller_.GetEntryAtIndex(entry_index);
1438
1439 if (state == entry->content_state())
1440 return; // Nothing to update.
1441 entry->set_content_state(state);
1442 controller_.NotifyEntryChanged(entry, entry_index);
1443}
1444
1445void TabContents::UpdateTitle(RenderViewHost* rvh,
[email protected]6b2f7a82011-04-25 19:30:511446 int32 page_id, const std::wstring& title) {
[email protected]420ae012009-04-24 05:16:321447 // If we have a title, that's a pretty good indication that we've started
1448 // getting useful data.
1449 SetNotWaitingForResponse();
1450
1451 DCHECK(rvh == render_view_host());
[email protected]371cc8b2010-09-01 16:03:481452 NavigationEntry* entry = controller_.GetEntryWithPageID(rvh->site_instance(),
[email protected]ec0b6c42010-08-26 03:16:581453 page_id);
[email protected]987fc3a2011-05-26 14:18:091454
1455 if (!UpdateTitleForEntry(entry, title))
[email protected]420ae012009-04-24 05:16:321456 return;
1457
1458 // Broadcast notifications when the UI should be updated.
1459 if (entry == controller_.GetEntryAtOffset(0))
[email protected]f1cd5e82009-10-23 17:19:031460 NotifyNavigationStateChanged(INVALIDATE_TITLE);
[email protected]420ae012009-04-24 05:16:321461}
1462
[email protected]420ae012009-04-24 05:16:321463void TabContents::UpdateEncoding(RenderViewHost* render_view_host,
[email protected]41fc0322009-09-04 22:23:401464 const std::string& encoding) {
[email protected]420ae012009-04-24 05:16:321465 set_encoding(encoding);
1466}
1467
1468void TabContents::UpdateTargetURL(int32 page_id, const GURL& url) {
1469 if (delegate())
1470 delegate()->UpdateTargetURL(this, url);
1471}
1472
[email protected]c4e98902010-06-01 10:20:141473void TabContents::UpdateInspectorSetting(const std::string& key,
1474 const std::string& value) {
[email protected]24d27d72010-10-12 16:30:051475 RenderViewHostDelegateHelper::UpdateInspectorSetting(profile(), key, value);
[email protected]0df30122009-06-03 12:13:081476}
1477
[email protected]45aafc92010-06-07 14:18:531478void TabContents::ClearInspectorSettings() {
[email protected]24d27d72010-10-12 16:30:051479 RenderViewHostDelegateHelper::ClearInspectorSettings(profile());
[email protected]45aafc92010-06-07 14:18:531480}
1481
[email protected]420ae012009-04-24 05:16:321482void TabContents::Close(RenderViewHost* rvh) {
[email protected]07707302009-11-06 00:50:291483 // The UI may be in an event-tracking loop, such as between the
1484 // mouse-down and mouse-up in text selection or a button click.
1485 // Defer the close until after tracking is complete, so that we
1486 // don't free objects out from under the UI.
1487 // TODO(shess): This could probably be integrated with the
1488 // IsDoingDrag() test below. Punting for now because I need more
1489 // research to understand how this impacts platforms other than Mac.
1490 // TODO(shess): This could get more fine-grained. For instance,
1491 // closing a tab in another window while selecting text in the
1492 // current window's Omnibox should be just fine.
1493 if (view()->IsEventTracking()) {
1494 view()->CloseTabAfterEventTracking();
1495 return;
1496 }
1497
[email protected]24a4d1062009-07-10 23:10:421498 // If we close the tab while we're in the middle of a drag, we'll crash.
1499 // Instead, cancel the drag and close it as soon as the drag ends.
1500 if (view()->IsDoingDrag()) {
1501 view()->CancelDragAndCloseTab();
1502 return;
1503 }
1504
[email protected]420ae012009-04-24 05:16:321505 // Ignore this if it comes from a RenderViewHost that we aren't showing.
1506 if (delegate() && rvh == render_view_host())
1507 delegate()->CloseContents(this);
1508}
1509
1510void TabContents::RequestMove(const gfx::Rect& new_bounds) {
[email protected]b35b26b32011-05-05 20:35:141511 if (delegate() && delegate()->IsPopupOrPanel(this))
[email protected]420ae012009-04-24 05:16:321512 delegate()->MoveContents(this, new_bounds);
1513}
1514
[email protected]7ab1e7d62009-10-14 23:32:011515void TabContents::DidStartLoading() {
[email protected]420ae012009-04-24 05:16:321516 SetIsLoading(true, NULL);
[email protected]3c9e1872010-11-18 16:17:491517
[email protected]c40d6232011-03-25 00:16:211518 if (delegate() && content_restrictions_) {
1519 content_restrictions_ = 0;
[email protected]de4761b2010-12-07 21:05:211520 delegate()->ContentRestrictionsChanged(this);
[email protected]9e823662010-10-13 23:36:001521 }
[email protected]3c9e1872010-11-18 16:17:491522
1523 // Notify observers about navigation.
[email protected]585b30362011-01-28 02:30:171524 FOR_EACH_OBSERVER(TabContentsObserver, observers_, DidStartLoading());
[email protected]420ae012009-04-24 05:16:321525}
1526
[email protected]7ab1e7d62009-10-14 23:32:011527void TabContents::DidStopLoading() {
[email protected]420ae012009-04-24 05:16:321528 scoped_ptr<LoadNotificationDetails> details;
1529
1530 NavigationEntry* entry = controller_.GetActiveEntry();
1531 // An entry may not exist for a stop when loading an initial blank page or
1532 // if an iframe injected by script into a blank page finishes loading.
1533 if (entry) {
[email protected]420ae012009-04-24 05:16:321534 base::TimeDelta elapsed = base::TimeTicks::Now() - current_load_start_;
1535
1536 details.reset(new LoadNotificationDetails(
[email protected]ebe89e062009-08-13 23:16:541537 entry->virtual_url(),
[email protected]420ae012009-04-24 05:16:321538 entry->transition_type(),
1539 elapsed,
1540 &controller_,
1541 controller_.GetCurrentEntryIndex()));
1542 }
1543
[email protected]420ae012009-04-24 05:16:321544 SetIsLoading(false, details.get());
[email protected]3c9e1872010-11-18 16:17:491545
1546 // Notify observers about navigation.
[email protected]585b30362011-01-28 02:30:171547 FOR_EACH_OBSERVER(TabContentsObserver, observers_, DidStopLoading());
[email protected]420ae012009-04-24 05:16:321548}
1549
[email protected]c95fa8b2011-04-28 20:26:161550void TabContents::DidCancelLoading() {
1551 controller_.DiscardNonCommittedEntries();
1552
1553 // Update the URL display.
1554 NotifyNavigationStateChanged(TabContents::INVALIDATE_URL);
1555}
1556
[email protected]1a3c3cb2010-12-16 21:03:401557void TabContents::DidChangeLoadProgress(double progress) {
1558 if (delegate())
1559 delegate()->LoadProgressChanged(progress);
1560}
1561
[email protected]25497492010-09-11 15:15:081562void TabContents::DocumentOnLoadCompletedInMainFrame(
1563 RenderViewHost* render_view_host,
1564 int32 page_id) {
1565 NotificationService::current()->Notify(
1566 NotificationType::LOAD_COMPLETED_MAIN_FRAME,
1567 Source<TabContents>(this),
1568 Details<int>(&page_id));
1569}
1570
[email protected]420ae012009-04-24 05:16:321571void TabContents::RequestOpenURL(const GURL& url, const GURL& referrer,
1572 WindowOpenDisposition disposition) {
[email protected]7ade2732011-02-10 00:13:581573 if (render_manager_.web_ui()) {
[email protected]e0112912011-02-02 22:54:351574 // When we're a Web UI, it will provide a page transition type for us (this
[email protected]420ae012009-04-24 05:16:321575 // is so the new tab page can specify AUTO_BOOKMARK for automatically
1576 // generated suggestions).
1577 //
[email protected]e0112912011-02-02 22:54:351578 // Note also that we hide the referrer for Web UI pages. We don't really
[email protected]60e448982009-05-06 04:21:161579 // want web sites to see a referrer of "chrome://blah" (and some
1580 // chrome: URLs might have search terms or other stuff we don't want to
[email protected]420ae012009-04-24 05:16:321581 // send to the site), so we send no referrer.
1582 OpenURL(url, GURL(), disposition,
[email protected]7ade2732011-02-10 00:13:581583 render_manager_.web_ui()->link_transition_type());
[email protected]420ae012009-04-24 05:16:321584 } else {
1585 OpenURL(url, referrer, disposition, PageTransition::LINK);
1586 }
1587}
1588
[email protected]420ae012009-04-24 05:16:321589void TabContents::RunJavaScriptMessage(
[email protected]992db4c2011-05-12 15:37:151590 const RenderViewHost* rvh,
[email protected]4f5ce842011-05-27 19:34:411591 const string16& message,
1592 const string16& default_prompt,
[email protected]420ae012009-04-24 05:16:321593 const GURL& frame_url,
1594 const int flags,
1595 IPC::Message* reply_msg,
1596 bool* did_suppress_message) {
1597 // Suppress javascript messages when requested and when inside a constrained
1598 // popup window (because that activates them and breaks them out of the
1599 // constrained window jail).
[email protected]91ed49a2009-07-07 21:29:551600 // Also suppress messages when showing an interstitial. The interstitial is
1601 // shown over the previous page, we don't want the hidden page dialogs to
1602 // interfere with the interstitial.
[email protected]8f55e802010-12-06 18:11:501603 bool suppress_this_message =
[email protected]992db4c2011-05-12 15:37:151604 rvh->is_swapped_out() ||
[email protected]8f55e802010-12-06 18:11:501605 suppress_javascript_messages_ ||
1606 showing_interstitial_page() ||
1607 (delegate() && delegate()->ShouldSuppressDialogs());
[email protected]420ae012009-04-24 05:16:321608 if (delegate())
1609 suppress_this_message |=
[email protected]b6c874582009-05-08 19:38:311610 (delegate()->GetConstrainingContents(this) != this);
[email protected]420ae012009-04-24 05:16:321611
1612 *did_suppress_message = suppress_this_message;
1613
1614 if (!suppress_this_message) {
1615 base::TimeDelta time_since_last_message(
1616 base::TimeTicks::Now() - last_javascript_message_dismissal_);
1617 bool show_suppress_checkbox = false;
1618 // Show a checkbox offering to suppress further messages if this message is
1619 // being displayed within kJavascriptMessageExpectedDelay of the last one.
1620 if (time_since_last_message <
[email protected]1698f612011-03-11 11:28:151621 base::TimeDelta::FromMilliseconds(
1622 chrome::kJavascriptMessageExpectedDelay))
[email protected]420ae012009-04-24 05:16:321623 show_suppress_checkbox = true;
1624
[email protected]4f5ce842011-05-27 19:34:411625 RunJavascriptMessageBox(profile(),
1626 this,
1627 frame_url,
1628 flags,
1629 UTF16ToWideHack(message),
1630 UTF16ToWideHack(default_prompt),
1631 show_suppress_checkbox,
1632 reply_msg);
[email protected]420ae012009-04-24 05:16:321633 } else {
1634 // If we are suppressing messages, just reply as is if the user immediately
1635 // pressed "Cancel".
[email protected]beb440c2009-11-06 04:08:541636 OnMessageBoxClosed(reply_msg, false, std::wstring());
[email protected]420ae012009-04-24 05:16:321637 }
1638}
1639
[email protected]992db4c2011-05-12 15:37:151640void TabContents::RunBeforeUnloadConfirm(const RenderViewHost* rvh,
[email protected]4f5ce842011-05-27 19:34:411641 const string16& message,
[email protected]420ae012009-04-24 05:16:321642 IPC::Message* reply_msg) {
[email protected]5aab5e22010-12-08 22:13:291643 if (delegate())
1644 delegate()->WillRunBeforeUnloadConfirm();
[email protected]992db4c2011-05-12 15:37:151645 bool suppress_this_message = rvh->is_swapped_out() ||
1646 (delegate() && delegate()->ShouldSuppressDialogs());
1647 if (suppress_this_message) {
[email protected]4f5ce842011-05-27 19:34:411648 render_view_host()->JavaScriptDialogClosed(reply_msg, true, string16());
[email protected]8f55e802010-12-06 18:11:501649 return;
1650 }
[email protected]7ab1e7d62009-10-14 23:32:011651 is_showing_before_unload_dialog_ = true;
[email protected]4f5ce842011-05-27 19:34:411652 RunBeforeUnloadDialog(this, UTF16ToWideHack(message), reply_msg);
[email protected]420ae012009-04-24 05:16:321653}
1654
[email protected]420ae012009-04-24 05:16:321655WebPreferences TabContents::GetWebkitPrefs() {
[email protected]6c32ce72010-03-08 05:18:061656 Profile* profile = render_view_host()->process()->profile();
[email protected]7ade2732011-02-10 00:13:581657 bool is_web_ui = false;
[email protected]447021c2010-09-08 21:29:081658 WebPreferences web_prefs =
[email protected]7ade2732011-02-10 00:13:581659 RenderViewHostDelegateHelper::GetWebkitPrefs(profile, is_web_ui);
[email protected]447021c2010-09-08 21:29:081660
[email protected]9beff752010-09-22 19:35:431661 // Force accelerated compositing and 2d canvas off for chrome: and
1662 // chrome-extension: pages.
[email protected]27c790d42010-10-22 09:28:431663 if (GetURL().SchemeIs(chrome::kChromeDevToolsScheme) ||
1664 GetURL().SchemeIs(chrome::kChromeUIScheme)) {
[email protected]447021c2010-09-08 21:29:081665 web_prefs.accelerated_compositing_enabled = false;
[email protected]9beff752010-09-22 19:35:431666 web_prefs.accelerated_2d_canvas_enabled = false;
[email protected]447021c2010-09-08 21:29:081667 }
1668
[email protected]39553442010-09-09 00:01:551669#if defined(OS_MACOSX)
1670 // Disable accelerated compositing if IOSurface's are not supported,
1671 // as is the case in 10.5.
1672 if (!IOSurfaceSupport::Initialize())
1673 web_prefs.accelerated_compositing_enabled = false;
1674#endif
1675
[email protected]447021c2010-09-08 21:29:081676 return web_prefs;
[email protected]420ae012009-04-24 05:16:321677}
1678
[email protected]7d472472011-01-22 01:30:251679void TabContents::OnUserGesture() {
[email protected]ddb85052011-05-18 14:40:271680 // Notify observers.
1681 FOR_EACH_OBSERVER(TabContentsObserver, observers_, DidGetUserGesture());
[email protected]7d472472011-01-22 01:30:251682}
1683
[email protected]fa1cf0b82010-01-15 21:49:441684void TabContents::OnIgnoredUIEvent() {
1685 if (constrained_window_count()) {
1686 ConstrainedWindow* window = *constrained_window_begin();
1687 window->FocusConstrainedWindow();
1688 }
1689}
1690
[email protected]420ae012009-04-24 05:16:321691void TabContents::OnCrossSiteResponse(int new_render_process_host_id,
1692 int new_request_id) {
[email protected]57c6a652009-05-04 07:58:341693 // Allows the TabContents to react when a cross-site response is ready to be
[email protected]420ae012009-04-24 05:16:321694 // delivered to a pending RenderViewHost. We must first run the onunload
1695 // handler of the old RenderViewHost before we can allow it to proceed.
1696 render_manager_.OnCrossSiteResponse(new_render_process_host_id,
1697 new_request_id);
1698}
1699
[email protected]420ae012009-04-24 05:16:321700void TabContents::RendererUnresponsive(RenderViewHost* rvh,
1701 bool is_during_unload) {
[email protected]992db4c2011-05-12 15:37:151702 // Don't show hung renderer dialog for a swapped out RVH.
1703 if (rvh != render_view_host())
1704 return;
1705
[email protected]420ae012009-04-24 05:16:321706 if (is_during_unload) {
1707 // Hang occurred while firing the beforeunload/unload handler.
1708 // Pretend the handler fired so tab closing continues as if it had.
[email protected]829e7612009-04-25 01:15:111709 rvh->set_sudden_termination_allowed(true);
[email protected]420ae012009-04-24 05:16:321710
1711 if (!render_manager_.ShouldCloseTabOnUnresponsiveRenderer())
1712 return;
1713
1714 // If the tab hangs in the beforeunload/unload handler there's really
1715 // nothing we can do to recover. Pretend the unload listeners have
1716 // all fired and close the tab. If the hang is in the beforeunload handler
1717 // then the user will not have the option of cancelling the close.
1718 Close(rvh);
1719 return;
1720 }
1721
[email protected]61b50092011-02-03 18:02:191722 if (render_view_host() && render_view_host()->IsRenderViewLive() &&
1723 (!delegate() || delegate()->ShouldShowHungRendererDialog())) {
[email protected]7ec6bef2011-02-26 02:22:291724 browser::ShowHungRendererDialog(this);
[email protected]61b50092011-02-03 18:02:191725 }
[email protected]420ae012009-04-24 05:16:321726}
1727
1728void TabContents::RendererResponsive(RenderViewHost* render_view_host) {
[email protected]7ec6bef2011-02-26 02:22:291729 browser::HideHungRendererDialog(this);
[email protected]420ae012009-04-24 05:16:321730}
1731
1732void TabContents::LoadStateChanged(const GURL& url,
[email protected]094e5b22009-09-25 04:23:561733 net::LoadState load_state,
1734 uint64 upload_position,
1735 uint64 upload_size) {
[email protected]420ae012009-04-24 05:16:321736 load_state_ = load_state;
[email protected]094e5b22009-09-25 04:23:561737 upload_position_ = upload_position;
1738 upload_size_ = upload_size;
[email protected]04866c42011-05-03 20:03:501739 load_state_host_ = net::IDNToUnicode(url.host(),
[email protected]b5cca982011-05-26 04:42:081740 content::GetContentClient()->browser()->GetAcceptLangs(this));
[email protected]420ae012009-04-24 05:16:321741 if (load_state_ == net::LOAD_STATE_READING_RESPONSE)
1742 SetNotWaitingForResponse();
1743 if (is_loading())
[email protected]c9cd2222009-05-06 05:16:501744 NotifyNavigationStateChanged(INVALIDATE_LOAD | INVALIDATE_TAB);
[email protected]420ae012009-04-24 05:16:321745}
1746
[email protected]1ee614862009-07-06 20:29:181747bool TabContents::IsExternalTabContainer() const {
1748 if (!delegate())
1749 return false;
1750
1751 return delegate()->IsExternalTabContainer();
1752}
1753
[email protected]7d472472011-01-22 01:30:251754void TabContents::WorkerCrashed() {
[email protected]0dd3a0ab2011-02-18 08:17:441755 if (delegate())
1756 delegate()->WorkerCrashed();
[email protected]7d472472011-01-22 01:30:251757}
1758
[email protected]f364d1392011-04-08 21:03:101759void TabContents::RequestDesktopNotificationPermission(
1760 const GURL& source_origin, int callback_context) {
1761 DesktopNotificationService* service =
[email protected]dcb72d52011-04-13 12:36:531762 DesktopNotificationServiceFactory::GetForProfile(profile());
[email protected]f364d1392011-04-08 21:03:101763 service->RequestPermission(
1764 source_origin, GetRenderProcessHost()->id(),
1765 render_view_host()->routing_id(), callback_context, this);
1766}
1767
[email protected]420ae012009-04-24 05:16:321768void TabContents::BeforeUnloadFiredFromRenderManager(
1769 bool proceed,
1770 bool* proceed_to_fire_unload) {
1771 if (delegate())
1772 delegate()->BeforeUnloadFired(this, proceed, proceed_to_fire_unload);
1773}
1774
[email protected]3a3d47472010-07-15 21:03:541775void TabContents::DidStartLoadingFromRenderManager(
1776 RenderViewHost* render_view_host) {
1777 DidStartLoading();
1778}
1779
1780void TabContents::RenderViewGoneFromRenderManager(
1781 RenderViewHost* render_view_host) {
[email protected]443b80e2010-12-14 00:42:231782 DCHECK(crashed_status_ != base::TERMINATION_STATUS_STILL_RUNNING);
1783 RenderViewGone(render_view_host, crashed_status_, crashed_error_code_);
[email protected]3a3d47472010-07-15 21:03:541784}
1785
[email protected]420ae012009-04-24 05:16:321786void TabContents::UpdateRenderViewSizeForRenderManager() {
1787 // TODO(brettw) this is a hack. See TabContentsView::SizeContents.
[email protected]0323ee42010-02-17 22:03:261788 gfx::Size size = view_->GetContainerSize();
1789 // 0x0 isn't a valid window size (minimal window size is 1x1) but it may be
1790 // here during container initialization and normal window size will be set
1791 // later. In case of tab duplication this resizing to 0x0 prevents setting
1792 // normal size later so just ignore it.
1793 if (!size.IsEmpty())
1794 view_->SizeContents(size);
[email protected]420ae012009-04-24 05:16:321795}
1796
[email protected]3a3d47472010-07-15 21:03:541797void TabContents::NotifySwappedFromRenderManager() {
1798 NotifySwapped();
1799}
1800
1801NavigationController& TabContents::GetControllerForRenderManager() {
1802 return controller();
1803}
1804
[email protected]d0980792011-02-13 19:41:401805WebUI* TabContents::CreateWebUIForRenderManager(const GURL& url) {
[email protected]1fd1a502011-03-30 16:55:561806 return content::WebUIFactory::Get()->CreateWebUIForURL(this, url);
[email protected]420ae012009-04-24 05:16:321807}
1808
1809NavigationEntry*
1810TabContents::GetLastCommittedNavigationEntryForRenderManager() {
1811 return controller_.GetLastCommittedEntry();
1812}
1813
1814bool TabContents::CreateRenderViewForRenderManager(
1815 RenderViewHost* render_view_host) {
[email protected]420ae012009-04-24 05:16:321816 RenderWidgetHostView* rwh_view = view_->CreateViewForWidget(render_view_host);
[email protected]1a98a932009-11-17 00:12:521817
[email protected]420ae012009-04-24 05:16:321818 // Now that the RenderView has been created, we need to tell it its size.
1819 rwh_view->SetSize(view_->GetContainerSize());
1820
[email protected]a4127722011-04-27 23:13:521821 if (!render_view_host->CreateRenderView(string16()))
1822 return false;
1823
[email protected]f8233cc2011-05-31 20:24:501824#if defined(OS_LINUX)
1825 // Force a ViewMsg_Resize to be sent, needed to make plugins show up on
1826 // linux. See crbug.com/83941.
1827 if (RenderWidgetHost* render_widget_host = rwh_view->GetRenderWidgetHost())
1828 render_widget_host->WasResized();
1829#endif
1830
[email protected]420ae012009-04-24 05:16:321831 UpdateMaxPageIDIfNecessary(render_view_host->site_instance(),
1832 render_view_host);
1833 return true;
1834}
1835
1836void TabContents::Observe(NotificationType type,
1837 const NotificationSource& source,
1838 const NotificationDetails& details) {
1839 switch (type.value) {
[email protected]420ae012009-04-24 05:16:321840 case NotificationType::RENDER_WIDGET_HOST_DESTROYED:
1841 view_->RenderWidgetHostDestroyed(Source<RenderWidgetHost>(source).ptr());
1842 break;
[email protected]420ae012009-04-24 05:16:321843 default:
1844 NOTREACHED();
1845 }
1846}
[email protected]aacd7cc2009-09-02 14:51:101847
[email protected]beb440c2009-11-06 04:08:541848gfx::NativeWindow TabContents::GetMessageBoxRootWindow() {
1849 return view_->GetTopLevelNativeWindow();
1850}
1851
1852void TabContents::OnMessageBoxClosed(IPC::Message* reply_msg,
1853 bool success,
[email protected]4f5ce842011-05-27 19:34:411854 const std::wstring& user_input) {
[email protected]beb440c2009-11-06 04:08:541855 last_javascript_message_dismissal_ = base::TimeTicks::Now();
1856 if (is_showing_before_unload_dialog_ && !success) {
1857 // If a beforeunload dialog is canceled, we need to stop the throbber from
1858 // spinning, since we forced it to start spinning in Navigate.
1859 DidStopLoading();
1860
1861 tab_close_start_time_ = base::TimeTicks();
1862 }
1863 is_showing_before_unload_dialog_ = false;
[email protected]4f5ce842011-05-27 19:34:411864 render_view_host()->JavaScriptDialogClosed(reply_msg,
1865 success,
1866 WideToUTF16Hack(user_input));
[email protected]beb440c2009-11-06 04:08:541867}
1868
1869void TabContents::SetSuppressMessageBoxes(bool suppress_message_boxes) {
1870 set_suppress_javascript_messages(suppress_message_boxes);
1871}
1872
[email protected]3a3d47472010-07-15 21:03:541873TabContents* TabContents::AsTabContents() {
1874 return this;
1875}
1876
1877ExtensionHost* TabContents::AsExtensionHost() {
1878 return NULL;
1879}
1880
[email protected]41fc0322009-09-04 22:23:401881void TabContents::set_encoding(const std::string& encoding) {
[email protected]763ec4ca2011-04-29 15:48:121882 encoding_ = content::GetContentClient()->browser()->
1883 GetCanonicalEncodingNameByAliasName(encoding);
[email protected]aacd7cc2009-09-02 14:51:101884}
[email protected]f45d2a72010-03-08 23:28:351885
[email protected]33f74972010-12-08 16:40:361886void TabContents::SwapInRenderViewHost(RenderViewHost* rvh) {
1887 render_manager_.SwapInRenderViewHost(rvh);
1888}
1889
1890void TabContents::CreateViewAndSetSizeForRVH(RenderViewHost* rvh) {
1891 RenderWidgetHostView* rwh_view = view()->CreateViewForWidget(rvh);
1892 rwh_view->SetSize(view()->GetContainerSize());
1893}
[email protected]e7d50892011-01-19 21:47:381894
[email protected]232a5812011-03-04 22:42:081895void TabContents::OnOnlineStateChanged(bool online) {
1896 render_view_host()->Send(new ViewMsg_NetworkStateChanged(
1897 render_view_host()->routing_id(), online));
1898}