blob: f5589b8a191a1dc751ad9698005ed0b22ee01263 [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]96d185d2009-04-24 03:28:5422#include "chrome/browser/renderer_host/web_cache_manager.h"
[email protected]1caa1522010-12-01 20:23:3523#include "chrome/browser/ui/app_modal_dialogs/message_box_handler.h"
[email protected]7ec6bef2011-02-26 02:22:2924#include "chrome/browser/ui/browser_dialogs.h"
[email protected]1698f612011-03-11 11:28:1525#include "chrome/common/chrome_constants.h"
[email protected]420ae012009-04-24 05:16:3226#include "chrome/common/render_messages.h"
[email protected]567812d2011-02-24 17:40:5027#include "content/browser/child_process_security_policy.h"
[email protected]1fd1a502011-03-30 16:55:5628#include "content/browser/content_browser_client.h"
[email protected]567812d2011-02-24 17:40:5029#include "content/browser/host_zoom_map.h"
30#include "content/browser/in_process_webkit/session_storage_namespace.h"
[email protected]35e251d2011-05-24 21:01:0431#include "content/browser/load_notification_details.h"
[email protected]567812d2011-02-24 17:40:5032#include "content/browser/renderer_host/render_process_host.h"
33#include "content/browser/renderer_host/render_view_host.h"
34#include "content/browser/renderer_host/render_widget_host_view.h"
35#include "content/browser/renderer_host/resource_request_details.h"
36#include "content/browser/site_instance.h"
[email protected]0dd3a0ab2011-02-18 08:17:4437#include "content/browser/tab_contents/interstitial_page.h"
38#include "content/browser/tab_contents/navigation_entry.h"
39#include "content/browser/tab_contents/provisional_load_details.h"
40#include "content/browser/tab_contents/tab_contents_delegate.h"
41#include "content/browser/tab_contents/tab_contents_observer.h"
42#include "content/browser/tab_contents/tab_contents_view.h"
[email protected]105bb0f2011-05-24 17:12:1443#include "content/browser/tab_contents/title_updated_details.h"
[email protected]afd1e522011-04-27 23:29:5944#include "content/browser/user_metrics.h"
[email protected]1fd1a502011-03-30 16:55:5645#include "content/browser/webui/web_ui_factory.h"
[email protected]9966325b2011-04-18 05:00:1046#include "content/common/bindings_policy.h"
[email protected]1fd1a502011-03-30 16:55:5647#include "content/common/content_client.h"
[email protected]763ec4ca2011-04-29 15:48:1248#include "content/common/content_restriction.h"
[email protected]4dd57932011-03-17 06:06:1249#include "content/common/navigation_types.h"
[email protected]7f070d42011-03-09 20:25:3250#include "content/common/notification_service.h"
[email protected]0f012df82011-05-19 14:15:2951#include "content/common/url_constants.h"
[email protected]2c5569662011-03-22 20:45:0252#include "content/common/view_messages.h"
[email protected]d686e812009-06-03 19:10:2953#include "net/base/net_util.h"
[email protected]420ae012009-04-24 05:16:3254#include "net/base/registry_controlled_domain.h"
[email protected]abe2c032011-03-31 18:49:3455#include "net/url_request/url_request_context_getter.h"
[email protected]8bd0fe62011-01-17 06:44:3756#include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h"
[email protected]42ce29d2011-01-20 23:19:4657#include "ui/base/resource/resource_bundle.h"
[email protected]08397d52011-02-05 01:53:3858#include "ui/gfx/codec/png_codec.h"
[email protected]d5e311b2010-07-20 17:15:0359#include "webkit/glue/password_form.h"
[email protected]3c733bde2010-12-21 19:56:3160#include "webkit/glue/webpreferences.h"
61
62#if defined(OS_MACOSX)
[email protected]b9b751f22011-03-25 14:04:1263#include "ui/gfx/surface/io_surface_support_mac.h"
[email protected]3c733bde2010-12-21 19:56:3164#endif // defined(OS_MACOSX)
[email protected]3e45ba92009-02-20 21:09:0065
[email protected]420ae012009-04-24 05:16:3266// Cross-Site Navigations
67//
68// If a TabContents is told to navigate to a different web site (as determined
69// by SiteInstance), it will replace its current RenderViewHost with a new
70// RenderViewHost dedicated to the new SiteInstance. This works as follows:
71//
72// - Navigate determines whether the destination is cross-site, and if so,
73// it creates a pending_render_view_host_ and moves into the PENDING
74// RendererState.
75// - The pending RVH is "suspended," so that no navigation messages are sent to
76// its renderer until the onbeforeunload JavaScript handler has a chance to
77// run in the current RVH.
78// - The pending RVH tells CrossSiteRequestManager (a thread-safe singleton)
79// that it has a pending cross-site request. ResourceDispatcherHost will
80// check for this when the response arrives.
81// - The current RVH runs its onbeforeunload handler. If it returns false, we
82// cancel all the pending logic and go back to NORMAL. Otherwise we allow
83// the pending RVH to send the navigation request to its renderer.
84// - ResourceDispatcherHost receives a ResourceRequest on the IO thread. It
85// checks CrossSiteRequestManager to see that the RVH responsible has a
86// pending cross-site request, and then installs a CrossSiteEventHandler.
87// - When RDH receives a response, the BufferedEventHandler determines whether
88// it is a download. If so, it sends a message to the new renderer causing
89// it to cancel the request, and the download proceeds in the download
90// thread. For now, we stay in a PENDING state (with a pending RVH) until
[email protected]57c6a652009-05-04 07:58:3491// the next DidNavigate event for this TabContents. This isn't ideal, but it
[email protected]420ae012009-04-24 05:16:3292// doesn't affect any functionality.
93// - After RDH receives a response and determines that it is safe and not a
94// download, it pauses the response to first run the old page's onunload
95// handler. It does this by asynchronously calling the OnCrossSiteResponse
[email protected]992db4c2011-05-12 15:37:1596// method of TabContents on the UI thread, which sends a SwapOut message
[email protected]420ae012009-04-24 05:16:3297// to the current RVH.
[email protected]992db4c2011-05-12 15:37:1598// - Once the onunload handler is finished, a SwapOut_ACK message is sent to
[email protected]420ae012009-04-24 05:16:3299// the ResourceDispatcherHost, who unpauses the response. Data is then sent
100// to the pending RVH.
101// - The pending renderer sends a FrameNavigate message that invokes the
102// DidNavigate method. This replaces the current RVH with the
103// pending RVH and goes back to the NORMAL RendererState.
[email protected]992db4c2011-05-12 15:37:15104// - The previous renderer is kept swapped out in RenderViewHostManager in case
105// the user goes back. The process only stays live if another tab is using
106// it, but if so, the existing frame relationships will be maintained.
[email protected]420ae012009-04-24 05:16:32107
108namespace {
109
110// Amount of time we wait between when a key event is received and the renderer
111// is queried for its state and pushed to the NavigationEntry.
112const int kQueryStateDelay = 5000;
113
[email protected]6ebdc9b2010-09-27 16:55:57114const int kSyncWaitDelay = 40;
115
[email protected]420ae012009-04-24 05:16:32116#if defined(OS_WIN)
117
118BOOL CALLBACK InvalidateWindow(HWND hwnd, LPARAM lparam) {
119 // Note: erase is required to properly paint some widgets borders. This can
120 // be seen with textfields.
121 InvalidateRect(hwnd, NULL, TRUE);
122 return TRUE;
123}
124#endif
125
[email protected]2c5569662011-03-22 20:45:02126ViewMsg_Navigate_Type::Value GetNavigationType(
[email protected]1ccb3568d2010-02-19 10:51:16127 Profile* profile, const NavigationEntry& entry,
128 NavigationController::ReloadType reload_type) {
129 switch (reload_type) {
130 case NavigationController::RELOAD:
[email protected]2c5569662011-03-22 20:45:02131 return ViewMsg_Navigate_Type::RELOAD;
[email protected]1ccb3568d2010-02-19 10:51:16132 case NavigationController::RELOAD_IGNORING_CACHE:
[email protected]2c5569662011-03-22 20:45:02133 return ViewMsg_Navigate_Type::RELOAD_IGNORING_CACHE;
[email protected]1ccb3568d2010-02-19 10:51:16134 case NavigationController::NO_RELOAD:
135 break; // Fall through to rest of function.
136 }
[email protected]5e369672009-11-03 23:48:30137
138 if (entry.restore_type() == NavigationEntry::RESTORE_LAST_SESSION &&
139 profile->DidLastSessionExitCleanly())
[email protected]2c5569662011-03-22 20:45:02140 return ViewMsg_Navigate_Type::RESTORE;
[email protected]5e369672009-11-03 23:48:30141
[email protected]2c5569662011-03-22 20:45:02142 return ViewMsg_Navigate_Type::NORMAL;
[email protected]5e369672009-11-03 23:48:30143}
144
[email protected]876bc832010-09-07 16:29:54145void MakeNavigateParams(const NavigationEntry& entry,
146 const NavigationController& controller,
[email protected]1ccb3568d2010-02-19 10:51:16147 NavigationController::ReloadType reload_type,
148 ViewMsg_Navigate_Params* params) {
[email protected]056de2d2009-06-26 16:41:34149 params->page_id = entry.page_id();
[email protected]876bc832010-09-07 16:29:54150 params->pending_history_list_offset = controller.GetIndexOfEntry(&entry);
[email protected]3cc72b12010-03-18 23:03:00151 params->current_history_list_offset = controller.last_committed_entry_index();
152 params->current_history_list_length = controller.entry_count();
[email protected]056de2d2009-06-26 16:41:34153 params->url = entry.url();
154 params->referrer = entry.referrer();
155 params->transition = entry.transition_type();
156 params->state = entry.content_state();
[email protected]3cc72b12010-03-18 23:03:00157 params->navigation_type =
158 GetNavigationType(controller.profile(), entry, reload_type);
[email protected]056de2d2009-06-26 16:41:34159 params->request_time = base::Time::Now();
160}
161
[email protected]420ae012009-04-24 05:16:32162} // namespace
163
[email protected]f4f50ef2011-01-21 19:01:19164
165// TabContents ----------------------------------------------------------------
[email protected]420ae012009-04-24 05:16:32166
[email protected]420ae012009-04-24 05:16:32167TabContents::TabContents(Profile* profile,
168 SiteInstance* site_instance,
169 int routing_id,
[email protected]6ee12c42010-09-14 09:36:07170 const TabContents* base_tab_contents,
171 SessionStorageNamespace* session_storage_namespace)
[email protected]b680ad22009-04-15 23:19:42172 : delegate_(NULL),
[email protected]6ee12c42010-09-14 09:36:07173 ALLOW_THIS_IN_INITIALIZER_LIST(controller_(
174 this, profile, session_storage_namespace)),
[email protected]66ba4932009-06-04 19:22:13175 ALLOW_THIS_IN_INITIALIZER_LIST(view_(
[email protected]d82ed61e2009-06-16 02:46:22176 TabContentsView::Create(this))),
177 ALLOW_THIS_IN_INITIALIZER_LIST(render_manager_(this, this)),
[email protected]6f821dd2010-02-23 00:54:06178 bookmark_drag_(NULL),
[email protected]d5f942ba2008-09-26 19:30:34179 is_loading_(false),
[email protected]443b80e2010-12-14 00:42:23180 crashed_status_(base::TERMINATION_STATUS_STILL_RUNNING),
181 crashed_error_code_(0),
[email protected]d5f942ba2008-09-26 19:30:34182 waiting_for_response_(false),
[email protected]d5f942ba2008-09-26 19:30:34183 max_page_id_(-1),
[email protected]fdd61c62009-04-22 19:22:57184 load_state_(net::LOAD_STATE_IDLE),
[email protected]094e5b22009-09-25 04:23:56185 upload_size_(0),
186 upload_position_(0),
[email protected]f17a0ee2010-05-17 17:38:47187 displayed_insecure_content_(false),
[email protected]fdd61c62009-04-22 19:22:57188 capturing_contents_(false),
189 is_being_destroyed_(false),
190 notify_disconnection_(false),
[email protected]fdd61c62009-04-22 19:22:57191#if defined(OS_WIN)
192 message_box_active_(CreateEvent(NULL, TRUE, FALSE, NULL)),
193#endif
[email protected]c2e74fe82009-09-03 17:57:44194 suppress_javascript_messages_(false),
[email protected]7ab1e7d62009-10-14 23:32:01195 is_showing_before_unload_dialog_(false),
[email protected]1fd1a502011-03-30 16:55:56196 opener_web_ui_type_(WebUI::kNoWebUI),
[email protected]ebf40a72010-07-22 01:46:38197 closed_by_user_gesture_(false),
[email protected]b75b8292010-10-01 07:28:25198 minimum_zoom_percent_(
199 static_cast<int>(WebKit::WebView::minTextSizeMultiplier * 100)),
200 maximum_zoom_percent_(
201 static_cast<int>(WebKit::WebView::maxTextSizeMultiplier * 100)),
[email protected]9e823662010-10-13 23:36:00202 temporary_zoom_settings_(false),
[email protected]4850a7f2011-03-08 23:36:59203 content_restrictions_(0) {
[email protected]7ff431e2010-01-07 18:18:54204
[email protected]12636df2009-09-28 22:32:21205 render_manager_.Init(profile, site_instance, routing_id);
[email protected]420ae012009-04-24 05:16:32206
[email protected]34ac70502009-09-25 17:07:23207 // We have the initial size of the view be based on the size of the passed in
208 // tab contents (normally a tab from the same window).
209 view_->CreateView(base_tab_contents ?
210 base_tab_contents->view()->GetContainerSize() : gfx::Size());
[email protected]420ae012009-04-24 05:16:32211
[email protected]420ae012009-04-24 05:16:32212 registrar_.Add(this, NotificationType::RENDER_WIDGET_HOST_DESTROYED,
213 NotificationService::AllSources());
[email protected]88152052010-10-01 04:05:18214
[email protected]b5a1d11c2011-02-17 03:09:42215 // Can only add observers after render_manager_.Init() is called, since that's
216 // what sets up the render_view_host which TabContentObserver's constructor
217 // uses to get the routing_id.
218 AddObservers();
[email protected]332af7732009-03-11 13:21:35219}
initial.commit09911bf2008-07-26 23:55:29220
221TabContents::~TabContents() {
[email protected]420ae012009-04-24 05:16:32222 is_being_destroyed_ = true;
223
[email protected]bfe4c1582009-10-02 18:11:09224 // We don't want any notifications while we're running our destructor.
[email protected]420ae012009-04-24 05:16:32225 registrar_.RemoveAll();
[email protected]420ae012009-04-24 05:16:32226
[email protected]420ae012009-04-24 05:16:32227 NotifyDisconnected();
[email protected]7ec6bef2011-02-26 02:22:29228 browser::HideHungRendererDialog(this);
[email protected]420ae012009-04-24 05:16:32229
[email protected]420ae012009-04-24 05:16:32230 // First cleanly close all child windows.
231 // TODO(mpcomplete): handle case if MaybeCloseChildWindows() already asked
232 // some of these to close. CloseWindows is async, so it might get called
233 // twice before it runs.
[email protected]8cc8d492010-02-02 10:40:49234 CloseConstrainedWindows();
[email protected]420ae012009-04-24 05:16:32235
[email protected]420ae012009-04-24 05:16:32236 // Notify any observer that have a reference on this tab contents.
237 NotificationService::current()->Notify(
238 NotificationType::TAB_CONTENTS_DESTROYED,
239 Source<TabContents>(this),
240 NotificationService::NoDetails());
241
242 // TODO(brettw) this should be moved to the view.
243#if defined(OS_WIN)
244 // If we still have a window handle, destroy it. GetNativeView can return
245 // NULL if this contents was part of a window that closed.
[email protected]50bd6452010-11-27 19:39:42246 if (GetNativeView()) {
247 RenderViewHost* host = render_view_host();
[email protected]b9a1fb42011-04-12 23:16:44248 if (host && host->view())
[email protected]50bd6452010-11-27 19:39:42249 host->view()->WillWmDestroy();
[email protected]50bd6452010-11-27 19:39:42250 }
[email protected]420ae012009-04-24 05:16:32251#endif
[email protected]7ab1e7d62009-10-14 23:32:01252
253 // OnCloseStarted isn't called in unit tests.
254 if (!tab_close_start_time_.is_null()) {
255 UMA_HISTOGRAM_TIMES("Tab.Close",
256 base::TimeTicks::Now() - tab_close_start_time_);
257 }
[email protected]b5a1d11c2011-02-17 03:09:42258
[email protected]07d490bc2011-03-07 17:05:26259 FOR_EACH_OBSERVER(TabContentsObserver, observers_, TabContentsDestroyed());
[email protected]232a5812011-03-04 22:42:08260
261 net::NetworkChangeNotifier::RemoveOnlineStateObserver(this);
[email protected]b5a1d11c2011-02-17 03:09:42262}
263
264void TabContents::AddObservers() {
[email protected]232a5812011-03-04 22:42:08265 net::NetworkChangeNotifier::AddOnlineStateObserver(this);
initial.commit09911bf2008-07-26 23:55:29266}
267
[email protected]724159a2010-12-30 01:11:18268bool TabContents::OnMessageReceived(const IPC::Message& message) {
[email protected]f82d57b52011-04-27 19:13:17269 if (web_ui() && web_ui()->OnMessageReceived(message))
270 return true;
271
[email protected]585b30362011-01-28 02:30:17272 ObserverListBase<TabContentsObserver>::Iterator it(observers_);
273 TabContentsObserver* observer;
[email protected]0f180ee2011-01-21 18:23:10274 while ((observer = it.GetNext()) != NULL)
275 if (observer->OnMessageReceived(message))
[email protected]403415a2011-01-10 18:57:53276 return true;
[email protected]403415a2011-01-10 18:57:53277
[email protected]724159a2010-12-30 01:11:18278 bool handled = true;
279 bool message_is_ok = true;
280 IPC_BEGIN_MESSAGE_MAP_EX(TabContents, message, message_is_ok)
281 IPC_MESSAGE_HANDLER(ViewHostMsg_DidStartProvisionalLoadForFrame,
282 OnDidStartProvisionalLoadForFrame)
283 IPC_MESSAGE_HANDLER(ViewHostMsg_DidRedirectProvisionalLoad,
284 OnDidRedirectProvisionalLoad)
285 IPC_MESSAGE_HANDLER(ViewHostMsg_DidFailProvisionalLoadWithError,
286 OnDidFailProvisionalLoadWithError)
287 IPC_MESSAGE_HANDLER(ViewHostMsg_DidLoadResourceFromMemoryCache,
288 OnDidLoadResourceFromMemoryCache)
289 IPC_MESSAGE_HANDLER(ViewHostMsg_DidDisplayInsecureContent,
290 OnDidDisplayInsecureContent)
291 IPC_MESSAGE_HANDLER(ViewHostMsg_DidRunInsecureContent,
292 OnDidRunInsecureContent)
293 IPC_MESSAGE_HANDLER(ViewHostMsg_DocumentLoadedInFrame,
294 OnDocumentLoadedInFrame)
295 IPC_MESSAGE_HANDLER(ViewHostMsg_DidFinishLoad, OnDidFinishLoad)
[email protected]c8f73ab2011-01-22 00:05:17296 IPC_MESSAGE_HANDLER(ViewHostMsg_UpdateContentRestrictions,
297 OnUpdateContentRestrictions)
[email protected]7d472472011-01-22 01:30:25298 IPC_MESSAGE_HANDLER(ViewHostMsg_GoToEntryAtOffset, OnGoToEntryAtOffset)
[email protected]216813952011-05-19 22:21:26299 IPC_MESSAGE_HANDLER(ViewHostMsg_UpdateZoomLimits, OnUpdateZoomLimits)
300 IPC_MESSAGE_HANDLER(ViewHostMsg_FocusedNodeChanged, OnFocusedNodeChanged)
[email protected]724159a2010-12-30 01:11:18301 IPC_MESSAGE_UNHANDLED(handled = false)
302 IPC_END_MESSAGE_MAP_EX()
303
304 if (!message_is_ok) {
305 UserMetrics::RecordAction(UserMetricsAction("BadMessageTerminate_RVD"));
306 GetRenderProcessHost()->ReceivedBadMessage();
307 }
308
309 return handled;
310}
311
[email protected]13367f72009-07-08 16:39:02312// Returns true if contains content rendered by an extension.
313bool TabContents::HostsExtension() const {
314 return GetURL().SchemeIs(chrome::kExtensionScheme);
[email protected]d5f942ba2008-09-26 19:30:34315}
316
[email protected]8cb5d5b2010-02-09 11:36:16317RenderProcessHost* TabContents::GetRenderProcessHost() const {
318 return render_manager_.current_host()->process();
319}
320
[email protected]d5f942ba2008-09-26 19:30:34321const GURL& TabContents::GetURL() const {
322 // We may not have a navigation entry yet
[email protected]ce3fa3c2009-04-20 19:55:57323 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]ebe89e062009-08-13 23:16:54324 return entry ? entry->virtual_url() : GURL::EmptyGURL();
[email protected]d5f942ba2008-09-26 19:30:34325}
326
[email protected]96d185d2009-04-24 03:28:54327const string16& TabContents::GetTitle() const {
[email protected]4c6092c5b2009-06-06 00:23:55328 // Transient entries take precedence. They are used for interstitial pages
329 // that are shown on top of existing pages.
330 NavigationEntry* entry = controller_.GetTransientEntry();
[email protected]b5cca982011-05-26 04:42:08331 std::string accept_languages =
332 content::GetContentClient()->browser()->GetAcceptLangs(this);
[email protected]45d0ef7f2011-01-05 13:46:23333 if (entry) {
[email protected]b5cca982011-05-26 04:42:08334 return entry->GetTitleForDisplay(accept_languages);
[email protected]45d0ef7f2011-01-05 13:46:23335 }
[email protected]7ade2732011-02-10 00:13:58336 WebUI* our_web_ui = render_manager_.pending_web_ui() ?
337 render_manager_.pending_web_ui() : render_manager_.web_ui();
338 if (our_web_ui) {
[email protected]96d185d2009-04-24 03:28:54339 // Don't override the title in view source mode.
[email protected]4c6092c5b2009-06-06 00:23:55340 entry = controller_.GetActiveEntry();
[email protected]96d185d2009-04-24 03:28:54341 if (!(entry && entry->IsViewSourceMode())) {
[email protected]e0112912011-02-02 22:54:35342 // Give the Web UI the chance to override our title.
[email protected]7ade2732011-02-10 00:13:58343 const string16& title = our_web_ui->overridden_title();
[email protected]96d185d2009-04-24 03:28:54344 if (!title.empty())
345 return title;
346 }
347 }
348
349 // We use the title for the last committed entry rather than a pending
350 // navigation entry. For example, when the user types in a URL, we want to
351 // keep the old page's title until the new load has committed and we get a new
352 // title.
[email protected]96d185d2009-04-24 03:28:54353 entry = controller_.GetLastCommittedEntry();
[email protected]45d0ef7f2011-01-05 13:46:23354 if (entry) {
[email protected]b5cca982011-05-26 04:42:08355 return entry->GetTitleForDisplay(accept_languages);
[email protected]45d0ef7f2011-01-05 13:46:23356 }
[email protected]96d185d2009-04-24 03:28:54357 return EmptyString16();
358}
359
[email protected]d5f942ba2008-09-26 19:30:34360int32 TabContents::GetMaxPageID() {
361 if (GetSiteInstance())
362 return GetSiteInstance()->max_page_id();
363 else
364 return max_page_id_;
365}
366
367void TabContents::UpdateMaxPageID(int32 page_id) {
368 // Ensure both the SiteInstance and RenderProcessHost update their max page
[email protected]57c6a652009-05-04 07:58:34369 // IDs in sync. Only TabContents will also have site instances, except during
[email protected]d5f942ba2008-09-26 19:30:34370 // testing.
371 if (GetSiteInstance())
372 GetSiteInstance()->UpdateMaxPageID(page_id);
[email protected]8cb5d5b2010-02-09 11:36:16373 GetRenderProcessHost()->UpdateMaxPageID(page_id);
[email protected]d5f942ba2008-09-26 19:30:34374}
375
[email protected]96d185d2009-04-24 03:28:54376SiteInstance* TabContents::GetSiteInstance() const {
377 return render_manager_.current_host()->site_instance();
378}
379
[email protected]96d185d2009-04-24 03:28:54380bool TabContents::ShouldDisplayURL() {
[email protected]8ab24cc2009-11-13 20:56:15381 // Don't hide the url in view source mode and with interstitials.
[email protected]96d185d2009-04-24 03:28:54382 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]8ab24cc2009-11-13 20:56:15383 if (entry && (entry->IsViewSourceMode() ||
[email protected]cccd3762010-11-12 18:40:01384 entry->page_type() == INTERSTITIAL_PAGE)) {
[email protected]96d185d2009-04-24 03:28:54385 return true;
[email protected]8ab24cc2009-11-13 20:56:15386 }
[email protected]83e3895b2009-06-11 00:07:16387
[email protected]2b9a06402011-02-16 04:57:10388 // We always display the URL for non-WebUI URLs to prevent spoofing.
[email protected]1fd1a502011-03-30 16:55:56389 if (entry && !content::WebUIFactory::Get()->HasWebUIScheme(entry->url()))
[email protected]2b9a06402011-02-16 04:57:10390 return true;
391
[email protected]d0980792011-02-13 19:41:40392 WebUI* web_ui = GetWebUIForCurrentState();
[email protected]7ade2732011-02-10 00:13:58393 if (web_ui)
394 return !web_ui->should_hide_url();
[email protected]96d185d2009-04-24 03:28:54395 return true;
396}
397
[email protected]585b30362011-01-28 02:30:17398void TabContents::AddObserver(TabContentsObserver* observer) {
399 observers_.AddObserver(observer);
[email protected]3c9e1872010-11-18 16:17:49400}
401
[email protected]585b30362011-01-28 02:30:17402void TabContents::RemoveObserver(TabContentsObserver* observer) {
403 observers_.RemoveObserver(observer);
[email protected]3c9e1872010-11-18 16:17:49404}
405
[email protected]443b80e2010-12-14 00:42:23406void TabContents::SetIsCrashed(base::TerminationStatus status, int error_code) {
407 if (status == crashed_status_)
[email protected]d5f942ba2008-09-26 19:30:34408 return;
409
[email protected]443b80e2010-12-14 00:42:23410 crashed_status_ = status;
411 crashed_error_code_ = error_code;
[email protected]c9cd2222009-05-06 05:16:50412 NotifyNavigationStateChanged(INVALIDATE_TAB);
[email protected]d5f942ba2008-09-26 19:30:34413}
414
415void TabContents::NotifyNavigationStateChanged(unsigned changed_flags) {
416 if (delegate_)
417 delegate_->NavigationStateChanged(this, changed_flags);
418}
419
[email protected]96d185d2009-04-24 03:28:54420void TabContents::DidBecomeSelected() {
421 controller_.SetActive(true);
[email protected]8cb5d5b2010-02-09 11:36:16422 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
423 if (rwhv) {
424 rwhv->DidBecomeSelected();
[email protected]789e9152009-08-04 21:59:43425#if defined(OS_MACOSX)
[email protected]8cb5d5b2010-02-09 11:36:16426 rwhv->SetActive(true);
[email protected]789e9152009-08-04 21:59:43427#endif
428 }
[email protected]96d185d2009-04-24 03:28:54429
[email protected]8cb5d5b2010-02-09 11:36:16430 WebCacheManager::GetInstance()->ObserveActivity(GetRenderProcessHost()->id());
[email protected]5ac20162010-11-24 23:33:11431 last_selected_time_ = base::TimeTicks::Now();
[email protected]96d185d2009-04-24 03:28:54432}
433
434void TabContents::WasHidden() {
435 if (!capturing_contents()) {
436 // |render_view_host()| can be NULL if the user middle clicks a link to open
437 // a tab in then background, then closes the tab before selecting it. This
438 // is because closing the tab calls TabContents::Destroy(), which removes
439 // the |render_view_host()|; then when we actually destroy the window,
440 // OnWindowPosChanged() notices and calls HideContents() (which calls us).
[email protected]8cb5d5b2010-02-09 11:36:16441 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
442 if (rwhv)
443 rwhv->WasHidden();
[email protected]96d185d2009-04-24 03:28:54444 }
445
446 NotificationService::current()->Notify(
447 NotificationType::TAB_CONTENTS_HIDDEN,
448 Source<TabContents>(this),
449 NotificationService::NoDetails());
450}
451
[email protected]d5f942ba2008-09-26 19:30:34452void TabContents::Activate() {
453 if (delegate_)
454 delegate_->ActivateContents(this);
455}
456
[email protected]ea42e7782010-08-23 23:58:12457void TabContents::Deactivate() {
458 if (delegate_)
459 delegate_->DeactivateContents(this);
460}
461
[email protected]96d185d2009-04-24 03:28:54462void TabContents::ShowContents() {
[email protected]8cb5d5b2010-02-09 11:36:16463 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
464 if (rwhv)
465 rwhv->DidBecomeSelected();
[email protected]96d185d2009-04-24 03:28:54466}
467
468void TabContents::HideContents() {
469 // TODO(pkasting): http://b/1239839 Right now we purposefully don't call
470 // our superclass HideContents(), because some callers want to be very picky
471 // about the order in which these get called. In addition to making the code
472 // here practically impossible to understand, this also means we end up
473 // calling TabContents::WasHidden() twice if callers call both versions of
[email protected]57c6a652009-05-04 07:58:34474 // HideContents() on a TabContents.
[email protected]96d185d2009-04-24 03:28:54475 WasHidden();
476}
477
[email protected]5aab5e22010-12-08 22:13:29478bool TabContents::NeedToFireBeforeUnload() {
479 // TODO(creis): Should we fire even for interstitial pages?
480 return notify_disconnection() &&
481 !showing_interstitial_page() &&
482 !render_view_host()->SuddenTerminationAllowed();
483}
484
[email protected]c0588052008-10-27 23:01:50485void TabContents::OpenURL(const GURL& url, const GURL& referrer,
[email protected]d5f942ba2008-09-26 19:30:34486 WindowOpenDisposition disposition,
487 PageTransition::Type transition) {
[email protected]1c642b52011-04-15 09:05:49488 if (delegate_) {
[email protected]c0588052008-10-27 23:01:50489 delegate_->OpenURLFromTab(this, url, referrer, disposition, transition);
[email protected]1c642b52011-04-15 09:05:49490 // Notify observers.
491 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
492 DidOpenURL(url, referrer, disposition, transition));
493 }
[email protected]d5f942ba2008-09-26 19:30:34494}
495
[email protected]1ccb3568d2010-02-19 10:51:16496bool TabContents::NavigateToPendingEntry(
497 NavigationController::ReloadType reload_type) {
[email protected]876bc832010-09-07 16:29:54498 return NavigateToEntry(*controller_.pending_entry(), reload_type);
499}
[email protected]96d185d2009-04-24 03:28:54500
[email protected]876bc832010-09-07 16:29:54501bool TabContents::NavigateToEntry(
502 const NavigationEntry& entry,
503 NavigationController::ReloadType reload_type) {
[email protected]96d185d2009-04-24 03:28:54504 RenderViewHost* dest_render_view_host = render_manager_.Navigate(entry);
505 if (!dest_render_view_host)
506 return false; // Unable to create the desired render view host.
507
[email protected]770dd8b2010-05-24 18:11:39508 if (delegate_ && delegate_->ShouldEnablePreferredSizeNotifications()) {
[email protected]216813952011-05-19 22:21:26509 dest_render_view_host->Send(new ViewMsg_EnablePreferredSizeChangedMode(
510 dest_render_view_host->routing_id(),
511 kPreferredSizeWidth | kPreferredSizeHeightThisIsSlow));
[email protected]770dd8b2010-05-24 18:11:39512 }
[email protected]9fb325e2010-05-06 18:23:24513
[email protected]80a8fad2011-01-29 04:02:38514 // For security, we should never send non-Web-UI URLs to a Web UI renderer.
[email protected]05fdd492010-11-15 17:52:07515 // Double check that here.
[email protected]54ec6472010-04-09 19:39:58516 int enabled_bindings = dest_render_view_host->enabled_bindings();
[email protected]1fd1a502011-03-30 16:55:56517 bool is_allowed_in_web_ui_renderer = content::GetContentClient()->
518 browser()->GetWebUIFactory()->IsURLAcceptableForWebUI(profile(),
519 entry.url());
[email protected]c09163a2011-02-15 00:05:55520 CHECK(!BindingsPolicy::is_web_ui_enabled(enabled_bindings) ||
521 is_allowed_in_web_ui_renderer);
[email protected]54ec6472010-04-09 19:39:58522
[email protected]96d185d2009-04-24 03:28:54523 // Tell DevTools agent that it is attached prior to the navigation.
[email protected]d9f9b792009-06-24 13:17:12524 DevToolsManager* devtools_manager = DevToolsManager::GetInstance();
[email protected]7e8e6b62009-05-08 11:28:32525 if (devtools_manager) { // NULL in unit tests.
[email protected]3cc72b12010-03-18 23:03:00526 devtools_manager->OnNavigatingToPendingEntry(render_view_host(),
527 dest_render_view_host,
528 entry.url());
[email protected]7e8e6b62009-05-08 11:28:32529 }
[email protected]96d185d2009-04-24 03:28:54530
531 // Used for page load time metrics.
532 current_load_start_ = base::TimeTicks::Now();
533
534 // Navigate in the desired RenderViewHost.
[email protected]056de2d2009-06-26 16:41:34535 ViewMsg_Navigate_Params navigate_params;
[email protected]876bc832010-09-07 16:29:54536 MakeNavigateParams(entry, controller_, reload_type, &navigate_params);
[email protected]52c68652010-12-07 17:47:04537 if (delegate_) {
538 navigate_params.extra_headers =
539 delegate_->GetNavigationHeaders(navigate_params.url);
540 }
[email protected]056de2d2009-06-26 16:41:34541 dest_render_view_host->Navigate(navigate_params);
[email protected]96d185d2009-04-24 03:28:54542
543 if (entry.page_id() == -1) {
544 // HACK!! This code suppresses javascript: URLs from being added to
545 // session history, which is what we want to do for javascript: URLs that
546 // do not generate content. What we really need is a message from the
547 // renderer telling us that a new page was not created. The same message
548 // could be used for mailto: URLs and the like.
549 if (entry.url().SchemeIs(chrome::kJavaScriptScheme))
550 return false;
551 }
552
[email protected]3c9e1872010-11-18 16:17:49553 // Notify observers about navigation.
[email protected]b375c5d2011-05-03 21:15:04554 FOR_EACH_OBSERVER(TabContentsObserver,
555 observers_,
556 NavigateToPendingEntry(entry.url(), reload_type));
[email protected]96d185d2009-04-24 03:28:54557
[email protected]d5f942ba2008-09-26 19:30:34558 return true;
559}
560
[email protected]96d185d2009-04-24 03:28:54561void TabContents::Stop() {
562 render_manager_.Stop();
[email protected]2e3f4572011-03-25 19:24:47563 FOR_EACH_OBSERVER(TabContentsObserver, observers_, StopNavigation());
[email protected]96d185d2009-04-24 03:28:54564}
565
[email protected]96d185d2009-04-24 03:28:54566TabContents* TabContents::Clone() {
567 // We create a new SiteInstance so that the new tab won't share processes
568 // with the old one. This can be changed in the future if we need it to share
569 // processes for some reason.
[email protected]420ae012009-04-24 05:16:32570 TabContents* tc = new TabContents(profile(),
[email protected]96d185d2009-04-24 03:28:54571 SiteInstance::CreateSiteInstance(profile()),
[email protected]6ee12c42010-09-14 09:36:07572 MSG_ROUTING_NONE, this, NULL);
[email protected]96d185d2009-04-24 03:28:54573 tc->controller().CopyStateFrom(controller_);
574 return tc;
575}
576
[email protected]4d677202009-07-19 07:37:12577void TabContents::ShowPageInfo(const GURL& url,
578 const NavigationEntry::SSLStatus& ssl,
579 bool show_history) {
580 if (!delegate_)
581 return;
582
[email protected]bb678332009-07-21 00:15:50583 delegate_->ShowPageInfo(profile(), url, ssl, show_history);
[email protected]4d677202009-07-19 07:37:12584}
585
initial.commit09911bf2008-07-26 23:55:29586ConstrainedWindow* TabContents::CreateConstrainedDialog(
[email protected]e8382172009-06-19 22:16:28587 ConstrainedWindowDelegate* delegate) {
initial.commit09911bf2008-07-26 23:55:29588 ConstrainedWindow* window =
[email protected]e8382172009-06-19 22:16:28589 ConstrainedWindow::CreateConstrainedDialog(this, delegate);
[email protected]aed59602011-02-28 22:57:33590 AddConstrainedDialog(window);
591 return window;
592}
593
594void TabContents::AddConstrainedDialog(ConstrainedWindow* window) {
initial.commit09911bf2008-07-26 23:55:29595 child_windows_.push_back(window);
[email protected]fa1cf0b82010-01-15 21:49:44596
597 if (child_windows_.size() == 1) {
598 window->ShowConstrainedWindow();
599 BlockTabContent(true);
600 }
initial.commit09911bf2008-07-26 23:55:29601}
602
[email protected]fa1cf0b82010-01-15 21:49:44603void TabContents::BlockTabContent(bool blocked) {
[email protected]8dccd7cb2010-02-25 05:19:55604 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
[email protected]1b072562010-12-13 19:37:45605 // 70% opaque grey.
606 SkColor greyish = SkColorSetARGB(178, 0, 0, 0);
[email protected]8dccd7cb2010-02-25 05:19:55607 if (rwhv)
[email protected]1b072562010-12-13 19:37:45608 rwhv->SetVisuallyDeemphasized(blocked ? &greyish : NULL, false);
[email protected]a7eccac2011-04-15 03:05:19609 // RenderViewHost may be NULL during shutdown.
610 if (render_view_host())
611 render_view_host()->set_ignore_input_events(blocked);
[email protected]fa1cf0b82010-01-15 21:49:44612 if (delegate_)
613 delegate_->SetTabContentBlocked(this, blocked);
614}
615
[email protected]e7cfdbd2011-04-22 14:41:37616void TabContents::AddNewContents(TabContents* new_contents,
617 WindowOpenDisposition disposition,
618 const gfx::Rect& initial_pos,
619 bool user_gesture) {
initial.commit09911bf2008-07-26 23:55:29620 if (!delegate_)
621 return;
622
[email protected]e7cfdbd2011-04-22 14:41:37623 delegate_->AddNewContents(this, new_contents, disposition, initial_pos,
624 user_gesture);
[email protected]96d185d2009-04-24 03:28:54625}
626
[email protected]5c9e97a2009-09-09 23:48:30627gfx::NativeView TabContents::GetContentNativeView() const {
[email protected]96d185d2009-04-24 03:28:54628 return view_->GetContentNativeView();
629}
630
631gfx::NativeView TabContents::GetNativeView() const {
632 return view_->GetNativeView();
633}
634
635void TabContents::GetContainerBounds(gfx::Rect *out) const {
636 view_->GetContainerBounds(out);
637}
638
639void TabContents::Focus() {
640 view_->Focus();
641}
642
[email protected]90daadb42009-06-08 21:27:28643void TabContents::FocusThroughTabTraversal(bool reverse) {
[email protected]7e383692009-06-12 19:14:54644 if (showing_interstitial_page()) {
645 render_manager_.interstitial_page()->FocusThroughTabTraversal(reverse);
646 return;
647 }
[email protected]96d185d2009-04-24 03:28:54648 render_view_host()->SetInitialFocus(reverse);
649}
650
651bool TabContents::FocusLocationBarByDefault() {
[email protected]d0980792011-02-13 19:41:40652 WebUI* web_ui = GetWebUIForCurrentState();
[email protected]7ade2732011-02-10 00:13:58653 if (web_ui)
654 return web_ui->focus_location_bar_by_default();
[email protected]3e3f0eb2009-06-22 18:33:43655 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]e0d481582009-09-15 21:06:25656 if (entry && entry->url() == GURL(chrome::kAboutBlankURL))
[email protected]3e3f0eb2009-06-22 18:33:43657 return true;
[email protected]96d185d2009-04-24 03:28:54658 return false;
659}
660
[email protected]a26dc362010-04-23 01:48:58661void TabContents::SetFocusToLocationBar(bool select_all) {
[email protected]a11aaf22010-03-30 00:03:38662 if (delegate())
[email protected]a26dc362010-04-23 01:48:58663 delegate()->SetFocusToLocationBar(select_all);
[email protected]a11aaf22010-03-30 00:03:38664}
665
[email protected]20ede072009-10-06 00:28:35666bool TabContents::ShouldShowBookmarkBar() {
[email protected]aa9196f2009-10-22 16:20:27667 if (showing_interstitial_page())
668 return false;
669
[email protected]d0980792011-02-13 19:41:40670 // See GetWebUIForCurrentState() comment for more info. This case is very
[email protected]96d185d2009-04-24 03:28:54671 // similar, but for non-first loads, we want to use the committed entry. This
672 // is so the bookmarks bar disappears at the same time the page does.
673 if (controller_.GetLastCommittedEntry()) {
[email protected]e0112912011-02-02 22:54:35674 // Not the first load, always use the committed Web UI.
[email protected]7ade2732011-02-10 00:13:58675 return (render_manager_.web_ui() == NULL) ?
676 false : render_manager_.web_ui()->force_bookmark_bar_visible();
[email protected]96d185d2009-04-24 03:28:54677 }
678
679 // When it's the first load, we know either the pending one or the committed
[email protected]d0980792011-02-13 19:41:40680 // one will have the Web UI in it (see GetWebUIForCurrentState), and only one
[email protected]96d185d2009-04-24 03:28:54681 // of them will be valid, so we can just check both.
[email protected]7ade2732011-02-10 00:13:58682 if (render_manager_.pending_web_ui())
683 return render_manager_.pending_web_ui()->force_bookmark_bar_visible();
684 return (render_manager_.web_ui() == NULL) ?
685 false : render_manager_.web_ui()->force_bookmark_bar_visible();
[email protected]96d185d2009-04-24 03:28:54686}
687
[email protected]d5f942ba2008-09-26 19:30:34688void TabContents::WillClose(ConstrainedWindow* window) {
[email protected]d6219912010-11-17 19:47:37689 ConstrainedWindowList::iterator i(
690 std::find(child_windows_.begin(), child_windows_.end(), window));
691 bool removed_topmost_window = i == child_windows_.begin();
692 if (i != child_windows_.end())
693 child_windows_.erase(i);
694 if (child_windows_.empty()) {
[email protected]fa1cf0b82010-01-15 21:49:44695 BlockTabContent(false);
[email protected]d6219912010-11-17 19:47:37696 } else {
697 if (removed_topmost_window)
698 child_windows_[0]->ShowConstrainedWindow();
699 BlockTabContent(true);
[email protected]fa1cf0b82010-01-15 21:49:44700 }
[email protected]66ba4932009-06-04 19:22:13701}
[email protected]d5f942ba2008-09-26 19:30:34702
[email protected]420ae012009-04-24 05:16:32703bool TabContents::IsActiveEntry(int32 page_id) {
704 NavigationEntry* active_entry = controller_.GetActiveEntry();
705 return (active_entry != NULL &&
706 active_entry->site_instance() == GetSiteInstance() &&
707 active_entry->page_id() == page_id);
708}
709
[email protected]b2fe07d12010-02-09 14:38:08710void TabContents::SetOverrideEncoding(const std::string& encoding) {
[email protected]8cb5d5b2010-02-09 11:36:16711 set_encoding(encoding);
[email protected]216813952011-05-19 22:21:26712 render_view_host()->Send(new ViewMsg_SetPageEncoding(
713 render_view_host()->routing_id(), encoding));
[email protected]8cb5d5b2010-02-09 11:36:16714}
715
[email protected]b2fe07d12010-02-09 14:38:08716void TabContents::ResetOverrideEncoding() {
[email protected]8cb5d5b2010-02-09 11:36:16717 reset_encoding();
[email protected]216813952011-05-19 22:21:26718 render_view_host()->Send(new ViewMsg_ResetPageEncodingToDefault(
719 render_view_host()->routing_id()));
[email protected]8cb5d5b2010-02-09 11:36:16720}
721
[email protected]7ab1e7d62009-10-14 23:32:01722void TabContents::OnCloseStarted() {
723 if (tab_close_start_time_.is_null())
724 tab_close_start_time_ = base::TimeTicks::Now();
725}
726
[email protected]46624bf2010-06-09 16:04:19727bool TabContents::ShouldAcceptDragAndDrop() const {
728#if defined(OS_CHROMEOS)
729 // ChromeOS panels (pop-ups) do not take drag-n-drop.
730 // See http://crosbug.com/2413
[email protected]b35b26b32011-05-05 20:35:14731 if (delegate() && delegate()->IsPopupOrPanel(this))
[email protected]b83af492010-10-09 03:21:20732 return false;
733 return true;
[email protected]46624bf2010-06-09 16:04:19734#else
735 return true;
736#endif
737}
738
[email protected]7813bd72011-02-05 02:19:34739void TabContents::SystemDragEnded() {
740 if (render_view_host())
741 render_view_host()->DragSourceSystemDragEnded();
742 if (delegate())
743 delegate()->DragEnded();
744}
745
[email protected]d0b8d092010-10-25 04:05:17746double TabContents::GetZoomLevel() const {
[email protected]b75b8292010-10-01 07:28:25747 HostZoomMap* zoom_map = profile()->GetHostZoomMap();
748 if (!zoom_map)
[email protected]d0b8d092010-10-25 04:05:17749 return 0;
[email protected]b75b8292010-10-01 07:28:25750
751 double zoom_level;
752 if (temporary_zoom_settings_) {
753 zoom_level = zoom_map->GetTemporaryZoomLevel(
754 GetRenderProcessHost()->id(), render_view_host()->routing_id());
755 } else {
756 zoom_level = zoom_map->GetZoomLevel(GetURL());
757 }
[email protected]d0b8d092010-10-25 04:05:17758 return zoom_level;
759}
[email protected]b75b8292010-10-01 07:28:25760
[email protected]d0b8d092010-10-25 04:05:17761int TabContents::GetZoomPercent(bool* enable_increment,
762 bool* enable_decrement) {
763 *enable_decrement = *enable_increment = false;
[email protected]b75b8292010-10-01 07:28:25764 int percent = static_cast<int>(
[email protected]d0b8d092010-10-25 04:05:17765 WebKit::WebView::zoomLevelToZoomFactor(GetZoomLevel()) * 100);
[email protected]b75b8292010-10-01 07:28:25766 *enable_decrement = percent > minimum_zoom_percent_;
767 *enable_increment = percent < maximum_zoom_percent_;
768 return percent;
769}
770
[email protected]3c733bde2010-12-21 19:56:31771void TabContents::ViewSource() {
[email protected]1788e772010-12-15 16:40:50772 if (!delegate_)
773 return;
774
775 NavigationEntry* active_entry = controller().GetActiveEntry();
776 if (!active_entry)
777 return;
778
779 delegate_->ViewSourceForTab(this, active_entry->url());
[email protected]77d8d622010-12-15 10:30:12780}
781
[email protected]932b7a12011-03-09 12:50:27782void TabContents::ViewFrameSource(const GURL& url,
783 const std::string& content_state) {
784 if (!delegate_)
785 return;
786
787 delegate_->ViewSourceForFrame(this, url, content_state);
788}
789
[email protected]c40d6232011-03-25 00:16:21790void TabContents::SetContentRestrictions(int restrictions) {
791 content_restrictions_ = restrictions;
792 delegate()->ContentRestrictionsChanged(this);
793}
794
[email protected]724159a2010-12-30 01:11:18795void TabContents::OnDidStartProvisionalLoadForFrame(int64 frame_id,
796 bool is_main_frame,
[email protected]eacb080b2011-05-22 19:40:26797 bool has_opener_set,
[email protected]724159a2010-12-30 01:11:18798 const GURL& url) {
799 bool is_error_page = (url.spec() == chrome::kUnreachableWebDataURL);
800 GURL validated_url(url);
801 render_view_host()->FilterURL(ChildProcessSecurityPolicy::GetInstance(),
802 GetRenderProcessHost()->id(), &validated_url);
803
[email protected]8093a542011-05-13 07:29:32804 RenderViewHost* rvh =
805 render_manager_.pending_render_view_host() ?
806 render_manager_.pending_render_view_host() : render_view_host();
[email protected]0d60f0192011-04-14 12:40:10807 // Notify observers about the start of the provisional load.
808 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
809 DidStartProvisionalLoadForFrame(frame_id, is_main_frame,
[email protected]8093a542011-05-13 07:29:32810 validated_url, is_error_page, rvh));
[email protected]0d60f0192011-04-14 12:40:10811
[email protected]724159a2010-12-30 01:11:18812 if (is_main_frame) {
[email protected]4850a7f2011-03-08 23:36:59813 // Notify observers about the provisional change in the main frame URL.
814 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
[email protected]eacb080b2011-05-22 19:40:26815 ProvisionalChangeToMainFrameUrl(url, has_opener_set));
[email protected]724159a2010-12-30 01:11:18816 }
817}
818
819void TabContents::OnDidRedirectProvisionalLoad(int32 page_id,
[email protected]eacb080b2011-05-22 19:40:26820 bool has_opener_set,
[email protected]724159a2010-12-30 01:11:18821 const GURL& source_url,
822 const GURL& target_url) {
[email protected]18ba2b12011-04-06 16:35:49823 // TODO(creis): Remove this method and have the pre-rendering code listen to
824 // the ResourceDispatcherHost's RESOURCE_RECEIVED_REDIRECT notification
825 // instead. See http://crbug.com/78512.
[email protected]724159a2010-12-30 01:11:18826 NavigationEntry* entry;
827 if (page_id == -1)
828 entry = controller_.pending_entry();
829 else
830 entry = controller_.GetEntryWithPageID(GetSiteInstance(), page_id);
831 if (!entry || entry->url() != source_url)
832 return;
[email protected]e7d50892011-01-19 21:47:38833
[email protected]4850a7f2011-03-08 23:36:59834 // Notify observers about the provisional change in the main frame URL.
835 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
[email protected]eacb080b2011-05-22 19:40:26836 ProvisionalChangeToMainFrameUrl(target_url,
837 has_opener_set));
[email protected]724159a2010-12-30 01:11:18838}
839
840void TabContents::OnDidFailProvisionalLoadWithError(
841 int64 frame_id,
842 bool is_main_frame,
843 int error_code,
844 const GURL& url,
845 bool showing_repost_interstitial) {
846 VLOG(1) << "Failed Provisional Load: " << url.possibly_invalid_spec()
847 << ", error_code: " << error_code
848 << " is_main_frame: " << is_main_frame
849 << " showing_repost_interstitial: " << showing_repost_interstitial
850 << " frame_id: " << frame_id;
851 GURL validated_url(url);
852 render_view_host()->FilterURL(ChildProcessSecurityPolicy::GetInstance(),
853 GetRenderProcessHost()->id(), &validated_url);
854
855 if (net::ERR_ABORTED == error_code) {
856 // EVIL HACK ALERT! Ignore failed loads when we're showing interstitials.
857 // This means that the interstitial won't be torn down properly, which is
858 // bad. But if we have an interstitial, go back to another tab type, and
859 // then load the same interstitial again, we could end up getting the first
860 // interstitial's "failed" message (as a result of the cancel) when we're on
861 // the second one.
862 //
863 // We can't tell this apart, so we think we're tearing down the current page
864 // which will cause a crash later one. There is also some code in
865 // RenderViewHostManager::RendererAbortedProvisionalLoad that is commented
866 // out because of this problem.
867 //
868 // http://code.google.com/p/chromium/issues/detail?id=2855
869 // Because this will not tear down the interstitial properly, if "back" is
870 // back to another tab type, the interstitial will still be somewhat alive
871 // in the previous tab type. If you navigate somewhere that activates the
872 // tab with the interstitial again, you'll see a flash before the new load
873 // commits of the interstitial page.
874 if (showing_interstitial_page()) {
875 LOG(WARNING) << "Discarding message during interstitial.";
876 return;
877 }
878
879 // This will discard our pending entry if we cancelled the load (e.g., if we
880 // decided to download the file instead of load it). Only discard the
881 // pending entry if the URLs match, otherwise the user initiated a navigate
882 // before the page loaded so that the discard would discard the wrong entry.
883 NavigationEntry* pending_entry = controller_.pending_entry();
884 if (pending_entry && pending_entry->url() == validated_url) {
[email protected]c95fa8b2011-04-28 20:26:16885 DidCancelLoading();
[email protected]724159a2010-12-30 01:11:18886 }
887
888 render_manager_.RendererAbortedProvisionalLoad(render_view_host());
889 }
890
891 // Send out a notification that we failed a provisional load with an error.
892 ProvisionalLoadDetails details(
893 is_main_frame, controller_.IsURLInPageNavigation(validated_url),
[email protected]b9e8ea62011-03-04 06:29:09894 validated_url, std::string(), false, frame_id);
[email protected]724159a2010-12-30 01:11:18895 details.set_error_code(error_code);
896
897 NotificationService::current()->Notify(
898 NotificationType::FAIL_PROVISIONAL_LOAD_WITH_ERROR,
899 Source<NavigationController>(&controller_),
900 Details<ProvisionalLoadDetails>(&details));
[email protected]0d60f0192011-04-14 12:40:10901
902 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
903 DidFailProvisionalLoad(frame_id, is_main_frame,
904 validated_url, error_code));
[email protected]724159a2010-12-30 01:11:18905}
906
907void TabContents::OnDidLoadResourceFromMemoryCache(
908 const GURL& url,
[email protected]724159a2010-12-30 01:11:18909 const std::string& security_info) {
[email protected]28620862011-03-22 23:07:19910 base::StatsCounter cache("WebKit.CacheHit");
[email protected]724159a2010-12-30 01:11:18911 cache.Increment();
912
913 // Send out a notification that we loaded a resource from our memory cache.
914 int cert_id = 0, cert_status = 0, security_bits = -1, connection_status = 0;
915 SSLManager::DeserializeSecurityInfo(security_info,
916 &cert_id, &cert_status,
917 &security_bits,
918 &connection_status);
[email protected]7a8c55e2011-02-15 08:21:16919 LoadFromMemoryCacheDetails details(url, GetRenderProcessHost()->id(),
920 cert_id, cert_status);
[email protected]724159a2010-12-30 01:11:18921
922 NotificationService::current()->Notify(
923 NotificationType::LOAD_FROM_MEMORY_CACHE,
924 Source<NavigationController>(&controller_),
925 Details<LoadFromMemoryCacheDetails>(&details));
926}
927
928void TabContents::OnDidDisplayInsecureContent() {
[email protected]221a5d92011-04-28 18:23:16929 UserMetrics::RecordAction(UserMetricsAction("SSL.DisplayedInsecureContent"));
[email protected]724159a2010-12-30 01:11:18930 displayed_insecure_content_ = true;
931 SSLManager::NotifySSLInternalStateChanged();
932}
933
934void TabContents::OnDidRunInsecureContent(
[email protected]92771112011-01-20 00:13:02935 const std::string& security_origin, const GURL& target_url) {
[email protected]9941cf52011-02-08 16:17:23936 LOG(INFO) << security_origin << " ran insecure content from "
937 << target_url.possibly_invalid_spec();
[email protected]221a5d92011-04-28 18:23:16938 UserMetrics::RecordAction(UserMetricsAction("SSL.RanInsecureContent"));
[email protected]724159a2010-12-30 01:11:18939 controller_.ssl_manager()->DidRunInsecureContent(security_origin);
940}
941
942void TabContents::OnDocumentLoadedInFrame(int64 frame_id) {
943 controller_.DocumentLoadedInFrame();
[email protected]0d60f0192011-04-14 12:40:10944 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
945 DocumentLoadedInFrame(frame_id));
[email protected]724159a2010-12-30 01:11:18946}
947
948void TabContents::OnDidFinishLoad(int64 frame_id) {
[email protected]0d60f0192011-04-14 12:40:10949 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
950 DidFinishLoad(frame_id));
[email protected]724159a2010-12-30 01:11:18951}
952
[email protected]c8f73ab2011-01-22 00:05:17953void TabContents::OnUpdateContentRestrictions(int restrictions) {
[email protected]c40d6232011-03-25 00:16:21954 SetContentRestrictions(restrictions);
[email protected]c8f73ab2011-01-22 00:05:17955}
956
[email protected]216813952011-05-19 22:21:26957void TabContents::OnGoToEntryAtOffset(int offset) {
958 if (!delegate_ || delegate_->OnGoToEntryOffset(offset)) {
959 NavigationEntry* entry = controller_.GetEntryAtOffset(offset);
960 if (!entry)
961 return;
962 // Note that we don't call NavigationController::GotToOffset() as we don't
963 // want to create a pending navigation entry (it might end up lingering
964 // http://crbug.com/51680).
965 entry->set_transition_type(entry->transition_type() |
966 PageTransition::FORWARD_BACK);
967 NavigateToEntry(*entry, NavigationController::NO_RELOAD);
968 }
969}
970
971void TabContents::OnUpdateZoomLimits(int minimum_percent,
972 int maximum_percent,
973 bool remember) {
974 minimum_zoom_percent_ = minimum_percent;
975 maximum_zoom_percent_ = maximum_percent;
976 temporary_zoom_settings_ = !remember;
977}
978
979void TabContents::OnFocusedNodeChanged(bool is_editable_node) {
980 NotificationService::current()->Notify(
981 NotificationType::FOCUS_CHANGED_IN_PAGE,
982 Source<TabContents>(this),
983 Details<const bool>(&is_editable_node));
984}
985
[email protected]96d185d2009-04-24 03:28:54986// Notifies the RenderWidgetHost instance about the fact that the page is
987// loading, or done loading and calls the base implementation.
988void TabContents::SetIsLoading(bool is_loading,
989 LoadNotificationDetails* details) {
990 if (is_loading == is_loading_)
991 return;
992
993 if (!is_loading) {
994 load_state_ = net::LOAD_STATE_IDLE;
995 load_state_host_.clear();
[email protected]094e5b22009-09-25 04:23:56996 upload_size_ = 0;
997 upload_position_ = 0;
[email protected]96d185d2009-04-24 03:28:54998 }
999
1000 render_manager_.SetIsLoading(is_loading);
1001
1002 is_loading_ = is_loading;
1003 waiting_for_response_ = is_loading;
1004
[email protected]6ebdc9b2010-09-27 16:55:571005 if (delegate_)
1006 delegate_->LoadingStateChanged(this);
[email protected]329581b2009-04-28 06:52:351007 NotifyNavigationStateChanged(INVALIDATE_LOAD);
[email protected]96d185d2009-04-24 03:28:541008
1009 NotificationType type = is_loading ? NotificationType::LOAD_START :
1010 NotificationType::LOAD_STOP;
[email protected]ce5c4502009-05-06 16:46:111011 NotificationDetails det = NotificationService::NoDetails();
[email protected]96d185d2009-04-24 03:28:541012 if (details)
1013 det = Details<LoadNotificationDetails>(details);
1014 NotificationService::current()->Notify(type,
1015 Source<NavigationController>(&controller_),
1016 det);
1017}
1018
[email protected]d0980792011-02-13 19:41:401019WebUI* TabContents::GetWebUIForCurrentState() {
[email protected]7ade2732011-02-10 00:13:581020 // When there is a pending navigation entry, we want to use the pending WebUI
[email protected]96d185d2009-04-24 03:28:541021 // that goes along with it to control the basic flags. For example, we want to
1022 // show the pending URL in the URL bar, so we want the display_url flag to
1023 // be from the pending entry.
1024 //
1025 // The confusion comes because there are multiple possibilities for the
1026 // initial load in a tab as a side effect of the way the RenderViewHostManager
1027 // works.
1028 //
[email protected]e0112912011-02-02 22:54:351029 // - For the very first tab the load looks "normal". The new tab Web UI is
[email protected]96d185d2009-04-24 03:28:541030 // the pending one, and we want it to apply here.
1031 //
1032 // - For subsequent new tabs, they'll get a new SiteInstance which will then
1033 // get switched to the one previously associated with the new tab pages.
[email protected]7ade2732011-02-10 00:13:581034 // This switching will cause the manager to commit the RVH/WebUI. So we'll
[email protected]e0112912011-02-02 22:54:351035 // have a committed Web UI in this case.
[email protected]96d185d2009-04-24 03:28:541036 //
1037 // This condition handles all of these cases:
1038 //
1039 // - First load in first tab: no committed nav entry + pending nav entry +
1040 // pending dom ui:
[email protected]e0112912011-02-02 22:54:351041 // -> Use pending Web UI if any.
[email protected]96d185d2009-04-24 03:28:541042 //
1043 // - First load in second tab: no committed nav entry + pending nav entry +
[email protected]e0112912011-02-02 22:54:351044 // no pending Web UI:
1045 // -> Use the committed Web UI if any.
[email protected]96d185d2009-04-24 03:28:541046 //
1047 // - Second navigation in any tab: committed nav entry + pending nav entry:
[email protected]e0112912011-02-02 22:54:351048 // -> Use pending Web UI if any.
[email protected]96d185d2009-04-24 03:28:541049 //
1050 // - Normal state with no load: committed nav entry + no pending nav entry:
[email protected]e0112912011-02-02 22:54:351051 // -> Use committed Web UI.
[email protected]96d185d2009-04-24 03:28:541052 if (controller_.pending_entry() &&
1053 (controller_.GetLastCommittedEntry() ||
[email protected]7ade2732011-02-10 00:13:581054 render_manager_.pending_web_ui()))
1055 return render_manager_.pending_web_ui();
1056 return render_manager_.web_ui();
[email protected]96d185d2009-04-24 03:28:541057}
[email protected]420ae012009-04-24 05:16:321058
[email protected]1fd1a502011-03-30 16:55:561059WebUI::TypeID TabContents::GetWebUITypeForCurrentState() {
1060 return content::WebUIFactory::Get()->GetWebUIType(profile(), GetURL());
1061}
1062
[email protected]420ae012009-04-24 05:16:321063void TabContents::DidNavigateMainFramePostCommit(
1064 const NavigationController::LoadCommittedDetails& details,
1065 const ViewHostMsg_FrameNavigate_Params& params) {
[email protected]1fd1a502011-03-30 16:55:561066 if (opener_web_ui_type_ != WebUI::kNoWebUI) {
[email protected]80a8fad2011-01-29 04:02:381067 // If this is a window.open navigation, use the same WebUI as the renderer
[email protected]c2e74fe82009-09-03 17:57:441068 // that opened the window, as long as both renderers have the same
1069 // privileges.
[email protected]1fd1a502011-03-30 16:55:561070 if (delegate_ && opener_web_ui_type_ == GetWebUITypeForCurrentState()) {
1071 WebUI* web_ui = content::GetContentClient()->browser()->
1072 GetWebUIFactory()->CreateWebUIForURL(this, GetURL());
[email protected]7ade2732011-02-10 00:13:581073 // web_ui might be NULL if the URL refers to a non-existent extension.
1074 if (web_ui) {
[email protected]d0980792011-02-13 19:41:401075 render_manager_.SetWebUIPostCommit(web_ui);
[email protected]7ade2732011-02-10 00:13:581076 web_ui->RenderViewCreated(render_view_host());
[email protected]c2e74fe82009-09-03 17:57:441077 }
1078 }
[email protected]1fd1a502011-03-30 16:55:561079 opener_web_ui_type_ = WebUI::kNoWebUI;
[email protected]c2e74fe82009-09-03 17:57:441080 }
1081
[email protected]420ae012009-04-24 05:16:321082 if (details.is_user_initiated_main_frame_load()) {
1083 // Clear the status bubble. This is a workaround for a bug where WebKit
1084 // doesn't let us know that the cursor left an element during a
1085 // transition (this is also why the mouse cursor remains as a hand after
1086 // clicking on a link); see bugs 1184641 and 980803. We don't want to
1087 // clear the bubble when a user navigates to a named anchor in the same
1088 // page.
1089 UpdateTargetURL(details.entry->page_id(), GURL());
[email protected]420ae012009-04-24 05:16:321090 }
1091
[email protected]a6e82fc2010-02-24 22:28:141092 if (!details.is_in_page) {
[email protected]f17a0ee2010-05-17 17:38:471093 // Once the main frame is navigated, we're no longer considered to have
1094 // displayed insecure content.
1095 displayed_insecure_content_ = false;
[email protected]aece2c7f2009-08-27 20:43:171096 }
[email protected]ce5c4502009-05-06 16:46:111097
[email protected]8cc8d492010-02-02 10:40:491098 // Close constrained windows if necessary.
1099 if (!net::RegistryControlledDomainService::SameDomainOrHost(
1100 details.previous_url, details.entry->url()))
1101 CloseConstrainedWindows();
1102
[email protected]3c9e1872010-11-18 16:17:491103 // Notify observers about navigation.
[email protected]585b30362011-01-28 02:30:171104 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
[email protected]3c9e1872010-11-18 16:17:491105 DidNavigateMainFramePostCommit(details, params));
[email protected]420ae012009-04-24 05:16:321106}
1107
1108void TabContents::DidNavigateAnyFramePostCommit(
1109 RenderViewHost* render_view_host,
1110 const NavigationController::LoadCommittedDetails& details,
1111 const ViewHostMsg_FrameNavigate_Params& params) {
1112 // If we navigate, start showing messages again. This does nothing to prevent
1113 // a malicious script from spamming messages, since the script could just
1114 // reload the page to stop blocking.
1115 suppress_javascript_messages_ = false;
1116
[email protected]3c9e1872010-11-18 16:17:491117 // Notify observers about navigation.
[email protected]585b30362011-01-28 02:30:171118 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
[email protected]3c9e1872010-11-18 16:17:491119 DidNavigateAnyFramePostCommit(details, params));
[email protected]420ae012009-04-24 05:16:321120}
1121
[email protected]8cc8d492010-02-02 10:40:491122void TabContents::CloseConstrainedWindows() {
1123 // Clear out any constrained windows since we are leaving this page entirely.
[email protected]aed59602011-02-28 22:57:331124 // To ensure that we iterate over every element in child_windows_ we
1125 // need to use a copy of child_windows_. Otherwise if
1126 // window->CloseConstrainedWindow() modifies child_windows_ we could end up
1127 // skipping some elements.
1128 ConstrainedWindowList child_windows_copy(child_windows_);
1129 for (ConstrainedWindowList::iterator it = child_windows_copy.begin();
1130 it != child_windows_copy.end(); ++it) {
1131 ConstrainedWindow* window = *it;
[email protected]fa1cf0b82010-01-15 21:49:441132 if (window) {
[email protected]420ae012009-04-24 05:16:321133 window->CloseConstrainedWindow();
[email protected]fa1cf0b82010-01-15 21:49:441134 BlockTabContent(false);
1135 }
[email protected]420ae012009-04-24 05:16:321136 }
1137}
1138
[email protected]420ae012009-04-24 05:16:321139void TabContents::UpdateMaxPageIDIfNecessary(SiteInstance* site_instance,
1140 RenderViewHost* rvh) {
1141 // If we are creating a RVH for a restored controller, then we might
1142 // have more page IDs than the SiteInstance's current max page ID. We must
1143 // make sure that the max page ID is larger than any restored page ID.
1144 // Note that it is ok for conflicting page IDs to exist in another tab
1145 // (i.e., NavigationController), but if any page ID is larger than the max,
1146 // the back/forward list will get confused.
1147 int max_restored_page_id = controller_.max_restored_page_id();
1148 if (max_restored_page_id > 0) {
1149 int curr_max_page_id = site_instance->max_page_id();
1150 if (max_restored_page_id > curr_max_page_id) {
1151 // Need to update the site instance immediately.
1152 site_instance->UpdateMaxPageID(max_restored_page_id);
1153
1154 // Also tell the renderer to update its internal representation. We
1155 // need to reserve enough IDs to make all restored page IDs less than
1156 // the max.
1157 if (curr_max_page_id < 0)
1158 curr_max_page_id = 0;
[email protected]216813952011-05-19 22:21:261159 rvh->Send(new ViewMsg_ReservePageIDRange(
1160 rvh->routing_id(), max_restored_page_id - curr_max_page_id));
[email protected]420ae012009-04-24 05:16:321161 }
1162 }
1163}
1164
[email protected]6b2f7a82011-04-25 19:30:511165bool TabContents::UpdateTitleForEntry(NavigationEntry* entry,
1166 const std::wstring& title) {
[email protected]420ae012009-04-24 05:16:321167 // For file URLs without a title, use the pathname instead. In the case of a
1168 // synthesized title, we don't want the update to count toward the "one set
1169 // per page of the title to history."
[email protected]6b2f7a82011-04-25 19:30:511170 string16 final_title;
[email protected]420ae012009-04-24 05:16:321171 bool explicit_set;
[email protected]6b2f7a82011-04-25 19:30:511172 if (entry->url().SchemeIsFile() && title.empty()) {
1173 final_title = UTF8ToUTF16(entry->url().ExtractFileName());
[email protected]420ae012009-04-24 05:16:321174 explicit_set = false; // Don't count synthetic titles toward the set limit.
1175 } else {
[email protected]6b2f7a82011-04-25 19:30:511176 TrimWhitespace(WideToUTF16Hack(title), TRIM_ALL, &final_title);
[email protected]420ae012009-04-24 05:16:321177 explicit_set = true;
1178 }
1179
[email protected]e53668962010-06-23 15:35:251180 if (final_title == entry->title())
[email protected]420ae012009-04-24 05:16:321181 return false; // Nothing changed, don't bother.
1182
[email protected]e53668962010-06-23 15:35:251183 entry->set_title(final_title);
[email protected]420ae012009-04-24 05:16:321184
[email protected]420ae012009-04-24 05:16:321185 // Lastly, set the title for the view.
[email protected]6b2f7a82011-04-25 19:30:511186 view_->SetPageTitle(UTF16ToWideHack(final_title));
[email protected]420ae012009-04-24 05:16:321187
[email protected]105bb0f2011-05-24 17:12:141188 TitleUpdatedDetails details(entry, explicit_set);
1189
[email protected]cbc0e1b2010-04-12 18:33:041190 NotificationService::current()->Notify(
1191 NotificationType::TAB_CONTENTS_TITLE_UPDATED,
1192 Source<TabContents>(this),
[email protected]105bb0f2011-05-24 17:12:141193 Details<TitleUpdatedDetails>(&details));
[email protected]cbc0e1b2010-04-12 18:33:041194
[email protected]420ae012009-04-24 05:16:321195 return true;
1196}
1197
1198void TabContents::NotifySwapped() {
1199 // After sending out a swap notification, we need to send a disconnect
1200 // notification so that clients that pick up a pointer to |this| can NULL the
1201 // pointer. See Bug 1230284.
1202 notify_disconnection_ = true;
1203 NotificationService::current()->Notify(
[email protected]57c6a652009-05-04 07:58:341204 NotificationType::TAB_CONTENTS_SWAPPED,
[email protected]d82ed61e2009-06-16 02:46:221205 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:321206 NotificationService::NoDetails());
1207}
1208
1209void TabContents::NotifyConnected() {
1210 notify_disconnection_ = true;
1211 NotificationService::current()->Notify(
[email protected]57c6a652009-05-04 07:58:341212 NotificationType::TAB_CONTENTS_CONNECTED,
[email protected]d82ed61e2009-06-16 02:46:221213 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:321214 NotificationService::NoDetails());
1215}
1216
1217void TabContents::NotifyDisconnected() {
1218 if (!notify_disconnection_)
1219 return;
1220
1221 notify_disconnection_ = false;
1222 NotificationService::current()->Notify(
[email protected]57c6a652009-05-04 07:58:341223 NotificationType::TAB_CONTENTS_DISCONNECTED,
[email protected]d82ed61e2009-06-16 02:46:221224 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:321225 NotificationService::NoDetails());
1226}
1227
[email protected]8d3347f2009-07-09 22:00:211228RenderViewHostDelegate::View* TabContents::GetViewDelegate() {
[email protected]420ae012009-04-24 05:16:321229 return view_.get();
1230}
1231
[email protected]8d3347f2009-07-09 22:00:211232RenderViewHostDelegate::RendererManagement*
1233TabContents::GetRendererManagementDelegate() {
1234 return &render_manager_;
1235}
1236
[email protected]ced90ae12010-02-20 02:06:161237RenderViewHostDelegate::BookmarkDrag* TabContents::GetBookmarkDragDelegate() {
1238 return bookmark_drag_;
1239}
1240
1241void TabContents::SetBookmarkDragDelegate(
1242 RenderViewHostDelegate::BookmarkDrag* bookmark_drag) {
1243 bookmark_drag_ = bookmark_drag;
1244}
1245
[email protected]93623c5d2009-12-10 21:40:321246RendererPreferences TabContents::GetRendererPrefs(Profile* profile) const {
[email protected]840b1512009-07-21 16:53:461247 return renderer_preferences_;
[email protected]80d96fa2009-06-10 22:34:511248}
1249
[email protected]57c6a652009-05-04 07:58:341250TabContents* TabContents::GetAsTabContents() {
1251 return this;
1252}
1253
[email protected]7b291f92009-08-14 05:43:531254ViewType::Type TabContents::GetRenderViewType() const {
1255 return ViewType::TAB_CONTENTS;
1256}
1257
1258int TabContents::GetBrowserWindowID() const {
1259 return controller().window_id().id();
1260}
1261
[email protected]420ae012009-04-24 05:16:321262void TabContents::RenderViewCreated(RenderViewHost* render_view_host) {
[email protected]2e4633c2009-07-09 16:58:061263 NotificationService::current()->Notify(
1264 NotificationType::RENDER_VIEW_HOST_CREATED_FOR_TAB,
1265 Source<TabContents>(this),
1266 Details<RenderViewHost>(render_view_host));
[email protected]420ae012009-04-24 05:16:321267 NavigationEntry* entry = controller_.GetActiveEntry();
1268 if (!entry)
1269 return;
1270
1271 // When we're creating views, we're still doing initial setup, so we always
[email protected]e0112912011-02-02 22:54:351272 // use the pending Web UI rather than any possibly existing committed one.
[email protected]1fd1a502011-03-30 16:55:561273 if (render_manager_.pending_web_ui())
[email protected]7ade2732011-02-10 00:13:581274 render_manager_.pending_web_ui()->RenderViewCreated(render_view_host);
[email protected]420ae012009-04-24 05:16:321275
1276 if (entry->IsViewSourceMode()) {
1277 // Put the renderer in view source mode.
1278 render_view_host->Send(
1279 new ViewMsg_EnableViewSourceMode(render_view_host->routing_id()));
1280 }
[email protected]0666aef2009-05-13 19:48:081281
1282 view()->RenderViewCreated(render_view_host);
[email protected]11f764bb2011-05-25 19:45:261283
1284 FOR_EACH_OBSERVER(
1285 TabContentsObserver, observers_, RenderViewCreated(render_view_host));
[email protected]420ae012009-04-24 05:16:321286}
1287
1288void TabContents::RenderViewReady(RenderViewHost* rvh) {
1289 if (rvh != render_view_host()) {
1290 // Don't notify the world, since this came from a renderer in the
1291 // background.
1292 return;
1293 }
1294
1295 NotifyConnected();
[email protected]9d8a4642009-07-29 17:25:301296 bool was_crashed = is_crashed();
[email protected]443b80e2010-12-14 00:42:231297 SetIsCrashed(base::TERMINATION_STATUS_STILL_RUNNING, 0);
[email protected]9d8a4642009-07-29 17:25:301298
1299 // Restore the focus to the tab (otherwise the focus will be on the top
1300 // window).
[email protected]484ae5912010-09-29 19:16:141301 if (was_crashed && !FocusLocationBarByDefault() &&
1302 (!delegate_ || delegate_->ShouldFocusPageAfterCrash())) {
[email protected]9d8a4642009-07-29 17:25:301303 Focus();
[email protected]484ae5912010-09-29 19:16:141304 }
[email protected]420ae012009-04-24 05:16:321305}
1306
[email protected]443b80e2010-12-14 00:42:231307void TabContents::RenderViewGone(RenderViewHost* rvh,
1308 base::TerminationStatus status,
1309 int error_code) {
[email protected]420ae012009-04-24 05:16:321310 if (rvh != render_view_host()) {
1311 // The pending page's RenderViewHost is gone.
1312 return;
1313 }
1314
[email protected]2e3f4572011-03-25 19:24:471315 // Let observers know first and give them a chance to act.
1316 FOR_EACH_OBSERVER(TabContentsObserver, observers_, RenderViewGone());
1317
[email protected]420ae012009-04-24 05:16:321318 SetIsLoading(false, NULL);
1319 NotifyDisconnected();
[email protected]443b80e2010-12-14 00:42:231320 SetIsCrashed(status, error_code);
[email protected]420ae012009-04-24 05:16:321321
[email protected]8e537302009-06-10 21:19:581322 // Tell the view that we've crashed so it can prepare the sad tab page.
[email protected]cf68b092010-01-15 20:05:551323 // Only do this if we're not in browser shutdown, so that TabContents
1324 // objects that are not in a browser (e.g., HTML dialogs) and thus are
1325 // visible do not flash a sad tab page.
1326 if (browser_shutdown::GetShutdownType() == browser_shutdown::NOT_VALID)
[email protected]a8f024392011-01-13 21:50:161327 view_->OnTabCrashed(status, error_code);
[email protected]420ae012009-04-24 05:16:321328
1329 // Hide any visible hung renderer warning for this web contents' process.
[email protected]7ec6bef2011-02-26 02:22:291330 browser::HideHungRendererDialog(this);
[email protected]420ae012009-04-24 05:16:321331}
1332
[email protected]2e4633c2009-07-09 16:58:061333void TabContents::RenderViewDeleted(RenderViewHost* rvh) {
[email protected]fb7b79f72009-11-06 18:00:481334 render_manager_.RenderViewDeleted(rvh);
[email protected]2e4633c2009-07-09 16:58:061335}
1336
[email protected]420ae012009-04-24 05:16:321337void TabContents::DidNavigate(RenderViewHost* rvh,
1338 const ViewHostMsg_FrameNavigate_Params& params) {
[email protected]8030f012009-09-25 18:09:371339 int extra_invalidate_flags = 0;
1340
1341 if (PageTransition::IsMainFrame(params.transition)) {
[email protected]20ede072009-10-06 00:28:351342 bool was_bookmark_bar_visible = ShouldShowBookmarkBar();
[email protected]8030f012009-09-25 18:09:371343
[email protected]420ae012009-04-24 05:16:321344 render_manager_.DidNavigateMainFrame(rvh);
1345
[email protected]20ede072009-10-06 00:28:351346 if (was_bookmark_bar_visible != ShouldShowBookmarkBar())
[email protected]8030f012009-09-25 18:09:371347 extra_invalidate_flags |= INVALIDATE_BOOKMARK_BAR;
[email protected]8030f012009-09-25 18:09:371348 }
1349
[email protected]420ae012009-04-24 05:16:321350 // Update the site of the SiteInstance if it doesn't have one yet.
1351 if (!GetSiteInstance()->has_site())
1352 GetSiteInstance()->SetSite(params.url);
1353
1354 // Need to update MIME type here because it's referred to in
1355 // UpdateNavigationCommands() called by RendererDidNavigate() to
1356 // determine whether or not to enable the encoding menu.
1357 // It's updated only for the main frame. For a subframe,
1358 // RenderView::UpdateURL does not set params.contents_mime_type.
1359 // (see http://code.google.com/p/chromium/issues/detail?id=2929 )
1360 // TODO(jungshik): Add a test for the encoding menu to avoid
1361 // regressing it again.
1362 if (PageTransition::IsMainFrame(params.transition))
1363 contents_mime_type_ = params.contents_mime_type;
1364
1365 NavigationController::LoadCommittedDetails details;
[email protected]8030f012009-09-25 18:09:371366 bool did_navigate = controller_.RendererDidNavigate(
1367 params, extra_invalidate_flags, &details);
[email protected]3e90d4a2009-07-03 17:38:391368
[email protected]a9c0bfe2010-09-17 08:35:221369 // Send notification about committed provisional loads. This notification is
1370 // different from the NAV_ENTRY_COMMITTED notification which doesn't include
1371 // the actual URL navigated to and isn't sent for AUTO_SUBFRAME navigations.
1372 if (details.type != NavigationType::NAV_IGNORE) {
[email protected]dabb0d12010-10-05 12:50:071373 // For AUTO_SUBFRAME navigations, an event for the main frame is generated
1374 // that is not recorded in the navigation history. For the purpose of
1375 // tracking navigation events, we treat this event as a sub frame navigation
1376 // event.
1377 bool is_main_frame = did_navigate ? details.is_main_frame : false;
[email protected]0d60f0192011-04-14 12:40:101378 PageTransition::Type transition_type = params.transition;
[email protected]a9c0bfe2010-09-17 08:35:221379 // Whether or not a page transition was triggered by going backward or
1380 // forward in the history is only stored in the navigation controller's
1381 // entry list.
1382 if (did_navigate &&
1383 (controller_.GetActiveEntry()->transition_type() &
1384 PageTransition::FORWARD_BACK)) {
[email protected]0d60f0192011-04-14 12:40:101385 transition_type = params.transition | PageTransition::FORWARD_BACK;
[email protected]a9c0bfe2010-09-17 08:35:221386 }
[email protected]0d60f0192011-04-14 12:40:101387 // Notify observers about the commit of the provisional load.
1388 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
1389 DidCommitProvisionalLoadForFrame(params.frame_id,
1390 is_main_frame, params.url, transition_type));
[email protected]a9c0bfe2010-09-17 08:35:221391 }
1392
[email protected]76543b92009-08-31 17:27:451393 if (!did_navigate)
1394 return; // No navigation happened.
[email protected]420ae012009-04-24 05:16:321395
1396 // DO NOT ADD MORE STUFF TO THIS FUNCTION! Your component should either listen
1397 // for the appropriate notification (best) or you can add it to
1398 // DidNavigateMainFramePostCommit / DidNavigateAnyFramePostCommit (only if
1399 // necessary, please).
1400
1401 // Run post-commit tasks.
1402 if (details.is_main_frame)
1403 DidNavigateMainFramePostCommit(details, params);
1404 DidNavigateAnyFramePostCommit(rvh, details, params);
1405}
1406
1407void TabContents::UpdateState(RenderViewHost* rvh,
1408 int32 page_id,
1409 const std::string& state) {
[email protected]992db4c2011-05-12 15:37:151410 // Ensure that this state update comes from either the active RVH or one of
1411 // the swapped out RVHs. We don't expect to hear from any other RVHs.
1412 DCHECK(rvh == render_view_host() || render_manager_.IsSwappedOut(rvh));
[email protected]420ae012009-04-24 05:16:321413
1414 // We must be prepared to handle state updates for any page, these occur
1415 // when the user is scrolling and entering form data, as well as when we're
1416 // leaving a page, in which case our state may have already been moved to
1417 // the next page. The navigation controller will look up the appropriate
1418 // NavigationEntry and update it when it is notified via the delegate.
1419
1420 int entry_index = controller_.GetEntryIndexWithPageID(
[email protected]992db4c2011-05-12 15:37:151421 rvh->site_instance(), page_id);
[email protected]420ae012009-04-24 05:16:321422 if (entry_index < 0)
1423 return;
1424 NavigationEntry* entry = controller_.GetEntryAtIndex(entry_index);
1425
1426 if (state == entry->content_state())
1427 return; // Nothing to update.
1428 entry->set_content_state(state);
1429 controller_.NotifyEntryChanged(entry, entry_index);
1430}
1431
1432void TabContents::UpdateTitle(RenderViewHost* rvh,
[email protected]6b2f7a82011-04-25 19:30:511433 int32 page_id, const std::wstring& title) {
[email protected]420ae012009-04-24 05:16:321434 // If we have a title, that's a pretty good indication that we've started
1435 // getting useful data.
1436 SetNotWaitingForResponse();
1437
1438 DCHECK(rvh == render_view_host());
[email protected]371cc8b2010-09-01 16:03:481439 NavigationEntry* entry = controller_.GetEntryWithPageID(rvh->site_instance(),
[email protected]ec0b6c42010-08-26 03:16:581440 page_id);
[email protected]40ead7b62011-04-23 03:17:281441 if (!entry || !UpdateTitleForEntry(entry, title))
[email protected]420ae012009-04-24 05:16:321442 return;
1443
1444 // Broadcast notifications when the UI should be updated.
1445 if (entry == controller_.GetEntryAtOffset(0))
[email protected]f1cd5e82009-10-23 17:19:031446 NotifyNavigationStateChanged(INVALIDATE_TITLE);
[email protected]420ae012009-04-24 05:16:321447}
1448
[email protected]420ae012009-04-24 05:16:321449void TabContents::UpdateEncoding(RenderViewHost* render_view_host,
[email protected]41fc0322009-09-04 22:23:401450 const std::string& encoding) {
[email protected]420ae012009-04-24 05:16:321451 set_encoding(encoding);
1452}
1453
1454void TabContents::UpdateTargetURL(int32 page_id, const GURL& url) {
1455 if (delegate())
1456 delegate()->UpdateTargetURL(this, url);
1457}
1458
[email protected]c4e98902010-06-01 10:20:141459void TabContents::UpdateInspectorSetting(const std::string& key,
1460 const std::string& value) {
[email protected]24d27d72010-10-12 16:30:051461 RenderViewHostDelegateHelper::UpdateInspectorSetting(profile(), key, value);
[email protected]0df30122009-06-03 12:13:081462}
1463
[email protected]45aafc92010-06-07 14:18:531464void TabContents::ClearInspectorSettings() {
[email protected]24d27d72010-10-12 16:30:051465 RenderViewHostDelegateHelper::ClearInspectorSettings(profile());
[email protected]45aafc92010-06-07 14:18:531466}
1467
[email protected]420ae012009-04-24 05:16:321468void TabContents::Close(RenderViewHost* rvh) {
[email protected]07707302009-11-06 00:50:291469 // The UI may be in an event-tracking loop, such as between the
1470 // mouse-down and mouse-up in text selection or a button click.
1471 // Defer the close until after tracking is complete, so that we
1472 // don't free objects out from under the UI.
1473 // TODO(shess): This could probably be integrated with the
1474 // IsDoingDrag() test below. Punting for now because I need more
1475 // research to understand how this impacts platforms other than Mac.
1476 // TODO(shess): This could get more fine-grained. For instance,
1477 // closing a tab in another window while selecting text in the
1478 // current window's Omnibox should be just fine.
1479 if (view()->IsEventTracking()) {
1480 view()->CloseTabAfterEventTracking();
1481 return;
1482 }
1483
[email protected]24a4d1062009-07-10 23:10:421484 // If we close the tab while we're in the middle of a drag, we'll crash.
1485 // Instead, cancel the drag and close it as soon as the drag ends.
1486 if (view()->IsDoingDrag()) {
1487 view()->CancelDragAndCloseTab();
1488 return;
1489 }
1490
[email protected]420ae012009-04-24 05:16:321491 // Ignore this if it comes from a RenderViewHost that we aren't showing.
1492 if (delegate() && rvh == render_view_host())
1493 delegate()->CloseContents(this);
1494}
1495
1496void TabContents::RequestMove(const gfx::Rect& new_bounds) {
[email protected]b35b26b32011-05-05 20:35:141497 if (delegate() && delegate()->IsPopupOrPanel(this))
[email protected]420ae012009-04-24 05:16:321498 delegate()->MoveContents(this, new_bounds);
1499}
1500
[email protected]7ab1e7d62009-10-14 23:32:011501void TabContents::DidStartLoading() {
[email protected]420ae012009-04-24 05:16:321502 SetIsLoading(true, NULL);
[email protected]3c9e1872010-11-18 16:17:491503
[email protected]c40d6232011-03-25 00:16:211504 if (delegate() && content_restrictions_) {
1505 content_restrictions_ = 0;
[email protected]de4761b2010-12-07 21:05:211506 delegate()->ContentRestrictionsChanged(this);
[email protected]9e823662010-10-13 23:36:001507 }
[email protected]3c9e1872010-11-18 16:17:491508
1509 // Notify observers about navigation.
[email protected]585b30362011-01-28 02:30:171510 FOR_EACH_OBSERVER(TabContentsObserver, observers_, DidStartLoading());
[email protected]420ae012009-04-24 05:16:321511}
1512
[email protected]7ab1e7d62009-10-14 23:32:011513void TabContents::DidStopLoading() {
[email protected]420ae012009-04-24 05:16:321514 scoped_ptr<LoadNotificationDetails> details;
1515
1516 NavigationEntry* entry = controller_.GetActiveEntry();
1517 // An entry may not exist for a stop when loading an initial blank page or
1518 // if an iframe injected by script into a blank page finishes loading.
1519 if (entry) {
[email protected]420ae012009-04-24 05:16:321520 base::TimeDelta elapsed = base::TimeTicks::Now() - current_load_start_;
1521
1522 details.reset(new LoadNotificationDetails(
[email protected]ebe89e062009-08-13 23:16:541523 entry->virtual_url(),
[email protected]420ae012009-04-24 05:16:321524 entry->transition_type(),
1525 elapsed,
1526 &controller_,
1527 controller_.GetCurrentEntryIndex()));
1528 }
1529
[email protected]420ae012009-04-24 05:16:321530 SetIsLoading(false, details.get());
[email protected]3c9e1872010-11-18 16:17:491531
1532 // Notify observers about navigation.
[email protected]585b30362011-01-28 02:30:171533 FOR_EACH_OBSERVER(TabContentsObserver, observers_, DidStopLoading());
[email protected]420ae012009-04-24 05:16:321534}
1535
[email protected]c95fa8b2011-04-28 20:26:161536void TabContents::DidCancelLoading() {
1537 controller_.DiscardNonCommittedEntries();
1538
1539 // Update the URL display.
1540 NotifyNavigationStateChanged(TabContents::INVALIDATE_URL);
1541}
1542
[email protected]1a3c3cb2010-12-16 21:03:401543void TabContents::DidChangeLoadProgress(double progress) {
1544 if (delegate())
1545 delegate()->LoadProgressChanged(progress);
1546}
1547
[email protected]25497492010-09-11 15:15:081548void TabContents::DocumentOnLoadCompletedInMainFrame(
1549 RenderViewHost* render_view_host,
1550 int32 page_id) {
1551 NotificationService::current()->Notify(
1552 NotificationType::LOAD_COMPLETED_MAIN_FRAME,
1553 Source<TabContents>(this),
1554 Details<int>(&page_id));
1555}
1556
[email protected]420ae012009-04-24 05:16:321557void TabContents::RequestOpenURL(const GURL& url, const GURL& referrer,
1558 WindowOpenDisposition disposition) {
[email protected]7ade2732011-02-10 00:13:581559 if (render_manager_.web_ui()) {
[email protected]e0112912011-02-02 22:54:351560 // When we're a Web UI, it will provide a page transition type for us (this
[email protected]420ae012009-04-24 05:16:321561 // is so the new tab page can specify AUTO_BOOKMARK for automatically
1562 // generated suggestions).
1563 //
[email protected]e0112912011-02-02 22:54:351564 // Note also that we hide the referrer for Web UI pages. We don't really
[email protected]60e448982009-05-06 04:21:161565 // want web sites to see a referrer of "chrome://blah" (and some
1566 // chrome: URLs might have search terms or other stuff we don't want to
[email protected]420ae012009-04-24 05:16:321567 // send to the site), so we send no referrer.
1568 OpenURL(url, GURL(), disposition,
[email protected]7ade2732011-02-10 00:13:581569 render_manager_.web_ui()->link_transition_type());
[email protected]420ae012009-04-24 05:16:321570 } else {
1571 OpenURL(url, referrer, disposition, PageTransition::LINK);
1572 }
1573}
1574
[email protected]420ae012009-04-24 05:16:321575void TabContents::RunJavaScriptMessage(
[email protected]992db4c2011-05-12 15:37:151576 const RenderViewHost* rvh,
[email protected]420ae012009-04-24 05:16:321577 const std::wstring& message,
1578 const std::wstring& default_prompt,
1579 const GURL& frame_url,
1580 const int flags,
1581 IPC::Message* reply_msg,
1582 bool* did_suppress_message) {
1583 // Suppress javascript messages when requested and when inside a constrained
1584 // popup window (because that activates them and breaks them out of the
1585 // constrained window jail).
[email protected]91ed49a2009-07-07 21:29:551586 // Also suppress messages when showing an interstitial. The interstitial is
1587 // shown over the previous page, we don't want the hidden page dialogs to
1588 // interfere with the interstitial.
[email protected]8f55e802010-12-06 18:11:501589 bool suppress_this_message =
[email protected]992db4c2011-05-12 15:37:151590 rvh->is_swapped_out() ||
[email protected]8f55e802010-12-06 18:11:501591 suppress_javascript_messages_ ||
1592 showing_interstitial_page() ||
1593 (delegate() && delegate()->ShouldSuppressDialogs());
[email protected]420ae012009-04-24 05:16:321594 if (delegate())
1595 suppress_this_message |=
[email protected]b6c874582009-05-08 19:38:311596 (delegate()->GetConstrainingContents(this) != this);
[email protected]420ae012009-04-24 05:16:321597
1598 *did_suppress_message = suppress_this_message;
1599
1600 if (!suppress_this_message) {
1601 base::TimeDelta time_since_last_message(
1602 base::TimeTicks::Now() - last_javascript_message_dismissal_);
1603 bool show_suppress_checkbox = false;
1604 // Show a checkbox offering to suppress further messages if this message is
1605 // being displayed within kJavascriptMessageExpectedDelay of the last one.
1606 if (time_since_last_message <
[email protected]1698f612011-03-11 11:28:151607 base::TimeDelta::FromMilliseconds(
1608 chrome::kJavascriptMessageExpectedDelay))
[email protected]420ae012009-04-24 05:16:321609 show_suppress_checkbox = true;
1610
[email protected]d6ad7442010-09-05 19:06:521611 RunJavascriptMessageBox(profile(), this, frame_url, flags, message,
1612 default_prompt, show_suppress_checkbox, reply_msg);
[email protected]420ae012009-04-24 05:16:321613 } else {
1614 // If we are suppressing messages, just reply as is if the user immediately
1615 // pressed "Cancel".
[email protected]beb440c2009-11-06 04:08:541616 OnMessageBoxClosed(reply_msg, false, std::wstring());
[email protected]420ae012009-04-24 05:16:321617 }
1618}
1619
[email protected]992db4c2011-05-12 15:37:151620void TabContents::RunBeforeUnloadConfirm(const RenderViewHost* rvh,
1621 const std::wstring& message,
[email protected]420ae012009-04-24 05:16:321622 IPC::Message* reply_msg) {
[email protected]5aab5e22010-12-08 22:13:291623 if (delegate())
1624 delegate()->WillRunBeforeUnloadConfirm();
[email protected]992db4c2011-05-12 15:37:151625 bool suppress_this_message = rvh->is_swapped_out() ||
1626 (delegate() && delegate()->ShouldSuppressDialogs());
1627 if (suppress_this_message) {
[email protected]8f55e802010-12-06 18:11:501628 render_view_host()->JavaScriptMessageBoxClosed(reply_msg, true,
1629 std::wstring());
1630 return;
1631 }
[email protected]7ab1e7d62009-10-14 23:32:011632 is_showing_before_unload_dialog_ = true;
[email protected]d82ed61e2009-06-16 02:46:221633 RunBeforeUnloadDialog(this, message, reply_msg);
[email protected]420ae012009-04-24 05:16:321634}
1635
[email protected]420ae012009-04-24 05:16:321636WebPreferences TabContents::GetWebkitPrefs() {
[email protected]6c32ce72010-03-08 05:18:061637 Profile* profile = render_view_host()->process()->profile();
[email protected]7ade2732011-02-10 00:13:581638 bool is_web_ui = false;
[email protected]447021c2010-09-08 21:29:081639 WebPreferences web_prefs =
[email protected]7ade2732011-02-10 00:13:581640 RenderViewHostDelegateHelper::GetWebkitPrefs(profile, is_web_ui);
[email protected]447021c2010-09-08 21:29:081641
[email protected]9beff752010-09-22 19:35:431642 // Force accelerated compositing and 2d canvas off for chrome: and
1643 // chrome-extension: pages.
[email protected]27c790d42010-10-22 09:28:431644 if (GetURL().SchemeIs(chrome::kChromeDevToolsScheme) ||
1645 GetURL().SchemeIs(chrome::kChromeUIScheme)) {
[email protected]447021c2010-09-08 21:29:081646 web_prefs.accelerated_compositing_enabled = false;
[email protected]9beff752010-09-22 19:35:431647 web_prefs.accelerated_2d_canvas_enabled = false;
[email protected]447021c2010-09-08 21:29:081648 }
1649
[email protected]39553442010-09-09 00:01:551650#if defined(OS_MACOSX)
1651 // Disable accelerated compositing if IOSurface's are not supported,
1652 // as is the case in 10.5.
1653 if (!IOSurfaceSupport::Initialize())
1654 web_prefs.accelerated_compositing_enabled = false;
1655#endif
1656
[email protected]447021c2010-09-08 21:29:081657 return web_prefs;
[email protected]420ae012009-04-24 05:16:321658}
1659
[email protected]7d472472011-01-22 01:30:251660void TabContents::OnUserGesture() {
[email protected]ddb85052011-05-18 14:40:271661 // Notify observers.
1662 FOR_EACH_OBSERVER(TabContentsObserver, observers_, DidGetUserGesture());
[email protected]7d472472011-01-22 01:30:251663}
1664
[email protected]fa1cf0b82010-01-15 21:49:441665void TabContents::OnIgnoredUIEvent() {
1666 if (constrained_window_count()) {
1667 ConstrainedWindow* window = *constrained_window_begin();
1668 window->FocusConstrainedWindow();
1669 }
1670}
1671
[email protected]420ae012009-04-24 05:16:321672void TabContents::OnCrossSiteResponse(int new_render_process_host_id,
1673 int new_request_id) {
[email protected]57c6a652009-05-04 07:58:341674 // Allows the TabContents to react when a cross-site response is ready to be
[email protected]420ae012009-04-24 05:16:321675 // delivered to a pending RenderViewHost. We must first run the onunload
1676 // handler of the old RenderViewHost before we can allow it to proceed.
1677 render_manager_.OnCrossSiteResponse(new_render_process_host_id,
1678 new_request_id);
1679}
1680
[email protected]420ae012009-04-24 05:16:321681void TabContents::RendererUnresponsive(RenderViewHost* rvh,
1682 bool is_during_unload) {
[email protected]992db4c2011-05-12 15:37:151683 // Don't show hung renderer dialog for a swapped out RVH.
1684 if (rvh != render_view_host())
1685 return;
1686
[email protected]420ae012009-04-24 05:16:321687 if (is_during_unload) {
1688 // Hang occurred while firing the beforeunload/unload handler.
1689 // Pretend the handler fired so tab closing continues as if it had.
[email protected]829e7612009-04-25 01:15:111690 rvh->set_sudden_termination_allowed(true);
[email protected]420ae012009-04-24 05:16:321691
1692 if (!render_manager_.ShouldCloseTabOnUnresponsiveRenderer())
1693 return;
1694
1695 // If the tab hangs in the beforeunload/unload handler there's really
1696 // nothing we can do to recover. Pretend the unload listeners have
1697 // all fired and close the tab. If the hang is in the beforeunload handler
1698 // then the user will not have the option of cancelling the close.
1699 Close(rvh);
1700 return;
1701 }
1702
[email protected]61b50092011-02-03 18:02:191703 if (render_view_host() && render_view_host()->IsRenderViewLive() &&
1704 (!delegate() || delegate()->ShouldShowHungRendererDialog())) {
[email protected]7ec6bef2011-02-26 02:22:291705 browser::ShowHungRendererDialog(this);
[email protected]61b50092011-02-03 18:02:191706 }
[email protected]420ae012009-04-24 05:16:321707}
1708
1709void TabContents::RendererResponsive(RenderViewHost* render_view_host) {
[email protected]7ec6bef2011-02-26 02:22:291710 browser::HideHungRendererDialog(this);
[email protected]420ae012009-04-24 05:16:321711}
1712
1713void TabContents::LoadStateChanged(const GURL& url,
[email protected]094e5b22009-09-25 04:23:561714 net::LoadState load_state,
1715 uint64 upload_position,
1716 uint64 upload_size) {
[email protected]420ae012009-04-24 05:16:321717 load_state_ = load_state;
[email protected]094e5b22009-09-25 04:23:561718 upload_position_ = upload_position;
1719 upload_size_ = upload_size;
[email protected]04866c42011-05-03 20:03:501720 load_state_host_ = net::IDNToUnicode(url.host(),
[email protected]b5cca982011-05-26 04:42:081721 content::GetContentClient()->browser()->GetAcceptLangs(this));
[email protected]420ae012009-04-24 05:16:321722 if (load_state_ == net::LOAD_STATE_READING_RESPONSE)
1723 SetNotWaitingForResponse();
1724 if (is_loading())
[email protected]c9cd2222009-05-06 05:16:501725 NotifyNavigationStateChanged(INVALIDATE_LOAD | INVALIDATE_TAB);
[email protected]420ae012009-04-24 05:16:321726}
1727
[email protected]1ee614862009-07-06 20:29:181728bool TabContents::IsExternalTabContainer() const {
1729 if (!delegate())
1730 return false;
1731
1732 return delegate()->IsExternalTabContainer();
1733}
1734
[email protected]7d472472011-01-22 01:30:251735void TabContents::WorkerCrashed() {
[email protected]0dd3a0ab2011-02-18 08:17:441736 if (delegate())
1737 delegate()->WorkerCrashed();
[email protected]7d472472011-01-22 01:30:251738}
1739
[email protected]f364d1392011-04-08 21:03:101740void TabContents::RequestDesktopNotificationPermission(
1741 const GURL& source_origin, int callback_context) {
1742 DesktopNotificationService* service =
[email protected]dcb72d52011-04-13 12:36:531743 DesktopNotificationServiceFactory::GetForProfile(profile());
[email protected]f364d1392011-04-08 21:03:101744 service->RequestPermission(
1745 source_origin, GetRenderProcessHost()->id(),
1746 render_view_host()->routing_id(), callback_context, this);
1747}
1748
[email protected]420ae012009-04-24 05:16:321749void TabContents::BeforeUnloadFiredFromRenderManager(
1750 bool proceed,
1751 bool* proceed_to_fire_unload) {
1752 if (delegate())
1753 delegate()->BeforeUnloadFired(this, proceed, proceed_to_fire_unload);
1754}
1755
[email protected]3a3d47472010-07-15 21:03:541756void TabContents::DidStartLoadingFromRenderManager(
1757 RenderViewHost* render_view_host) {
1758 DidStartLoading();
1759}
1760
1761void TabContents::RenderViewGoneFromRenderManager(
1762 RenderViewHost* render_view_host) {
[email protected]443b80e2010-12-14 00:42:231763 DCHECK(crashed_status_ != base::TERMINATION_STATUS_STILL_RUNNING);
1764 RenderViewGone(render_view_host, crashed_status_, crashed_error_code_);
[email protected]3a3d47472010-07-15 21:03:541765}
1766
[email protected]420ae012009-04-24 05:16:321767void TabContents::UpdateRenderViewSizeForRenderManager() {
1768 // TODO(brettw) this is a hack. See TabContentsView::SizeContents.
[email protected]0323ee42010-02-17 22:03:261769 gfx::Size size = view_->GetContainerSize();
1770 // 0x0 isn't a valid window size (minimal window size is 1x1) but it may be
1771 // here during container initialization and normal window size will be set
1772 // later. In case of tab duplication this resizing to 0x0 prevents setting
1773 // normal size later so just ignore it.
1774 if (!size.IsEmpty())
1775 view_->SizeContents(size);
[email protected]420ae012009-04-24 05:16:321776}
1777
[email protected]3a3d47472010-07-15 21:03:541778void TabContents::NotifySwappedFromRenderManager() {
1779 NotifySwapped();
1780}
1781
1782NavigationController& TabContents::GetControllerForRenderManager() {
1783 return controller();
1784}
1785
[email protected]d0980792011-02-13 19:41:401786WebUI* TabContents::CreateWebUIForRenderManager(const GURL& url) {
[email protected]1fd1a502011-03-30 16:55:561787 return content::WebUIFactory::Get()->CreateWebUIForURL(this, url);
[email protected]420ae012009-04-24 05:16:321788}
1789
1790NavigationEntry*
1791TabContents::GetLastCommittedNavigationEntryForRenderManager() {
1792 return controller_.GetLastCommittedEntry();
1793}
1794
1795bool TabContents::CreateRenderViewForRenderManager(
1796 RenderViewHost* render_view_host) {
[email protected]420ae012009-04-24 05:16:321797 RenderWidgetHostView* rwh_view = view_->CreateViewForWidget(render_view_host);
[email protected]1a98a932009-11-17 00:12:521798
[email protected]420ae012009-04-24 05:16:321799 // Now that the RenderView has been created, we need to tell it its size.
1800 rwh_view->SetSize(view_->GetContainerSize());
1801
[email protected]a4127722011-04-27 23:13:521802 if (!render_view_host->CreateRenderView(string16()))
1803 return false;
1804
[email protected]420ae012009-04-24 05:16:321805 UpdateMaxPageIDIfNecessary(render_view_host->site_instance(),
1806 render_view_host);
1807 return true;
1808}
1809
1810void TabContents::Observe(NotificationType type,
1811 const NotificationSource& source,
1812 const NotificationDetails& details) {
1813 switch (type.value) {
[email protected]420ae012009-04-24 05:16:321814 case NotificationType::RENDER_WIDGET_HOST_DESTROYED:
1815 view_->RenderWidgetHostDestroyed(Source<RenderWidgetHost>(source).ptr());
1816 break;
[email protected]420ae012009-04-24 05:16:321817 default:
1818 NOTREACHED();
1819 }
1820}
[email protected]aacd7cc2009-09-02 14:51:101821
[email protected]beb440c2009-11-06 04:08:541822gfx::NativeWindow TabContents::GetMessageBoxRootWindow() {
1823 return view_->GetTopLevelNativeWindow();
1824}
1825
1826void TabContents::OnMessageBoxClosed(IPC::Message* reply_msg,
1827 bool success,
1828 const std::wstring& prompt) {
1829 last_javascript_message_dismissal_ = base::TimeTicks::Now();
1830 if (is_showing_before_unload_dialog_ && !success) {
1831 // If a beforeunload dialog is canceled, we need to stop the throbber from
1832 // spinning, since we forced it to start spinning in Navigate.
1833 DidStopLoading();
1834
1835 tab_close_start_time_ = base::TimeTicks();
1836 }
1837 is_showing_before_unload_dialog_ = false;
1838 render_view_host()->JavaScriptMessageBoxClosed(reply_msg, success, prompt);
1839}
1840
1841void TabContents::SetSuppressMessageBoxes(bool suppress_message_boxes) {
1842 set_suppress_javascript_messages(suppress_message_boxes);
1843}
1844
[email protected]3a3d47472010-07-15 21:03:541845TabContents* TabContents::AsTabContents() {
1846 return this;
1847}
1848
1849ExtensionHost* TabContents::AsExtensionHost() {
1850 return NULL;
1851}
1852
[email protected]41fc0322009-09-04 22:23:401853void TabContents::set_encoding(const std::string& encoding) {
[email protected]763ec4ca2011-04-29 15:48:121854 encoding_ = content::GetContentClient()->browser()->
1855 GetCanonicalEncodingNameByAliasName(encoding);
[email protected]aacd7cc2009-09-02 14:51:101856}
[email protected]f45d2a72010-03-08 23:28:351857
[email protected]33f74972010-12-08 16:40:361858void TabContents::SwapInRenderViewHost(RenderViewHost* rvh) {
1859 render_manager_.SwapInRenderViewHost(rvh);
1860}
1861
1862void TabContents::CreateViewAndSetSizeForRVH(RenderViewHost* rvh) {
1863 RenderWidgetHostView* rwh_view = view()->CreateViewForWidget(rvh);
1864 rwh_view->SetSize(view()->GetContainerSize());
1865}
[email protected]e7d50892011-01-19 21:47:381866
[email protected]232a5812011-03-04 22:42:081867void TabContents::OnOnlineStateChanged(bool online) {
1868 render_view_host()->Send(new ViewMsg_NetworkStateChanged(
1869 render_view_host()->routing_id(), online));
1870}