blob: 1b3b412e30faa88e195b125833057f884208a3de [file] [log] [blame]
[email protected]61d68ef12011-01-13 14:02:561// Copyright (c) 2011 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
[email protected]0dd3a0ab2011-02-18 08:17:445#include "content/browser/tab_contents/tab_contents.h"
[email protected]39526562011-02-05 03:41:516
[email protected]b75b8292010-10-01 07:28:257#include <cmath>
8
[email protected]36fb2c7c2011-04-04 15:49:089#include "base/command_line.h"
[email protected]835d7c82010-10-14 04:38:3810#include "base/metrics/histogram.h"
[email protected]724159a2010-12-30 01:11:1811#include "base/metrics/stats_counters.h"
[email protected]96d185d2009-04-24 03:28:5412#include "base/string16.h"
[email protected]996fd702009-09-04 19:12:3713#include "base/string_util.h"
[email protected]96d185d2009-04-24 03:28:5414#include "base/time.h"
[email protected]3c733bde2010-12-21 19:56:3115#include "base/utf_string_conversions.h"
[email protected]3d7474ff2011-07-27 17:47:3716#include "content/browser/browser_context.h"
[email protected]567812d2011-02-24 17:40:5017#include "content/browser/child_process_security_policy.h"
[email protected]1fd1a502011-03-30 16:55:5618#include "content/browser/content_browser_client.h"
[email protected]b46442d7e2011-06-29 02:16:0619#include "content/browser/debugger/devtools_manager.h"
[email protected]71bf3f5e2011-08-15 21:05:2220#include "content/browser/download/download_manager.h"
[email protected]8bd9e562011-08-16 23:55:4621#include "content/browser/download/download_stats.h"
[email protected]567812d2011-02-24 17:40:5022#include "content/browser/host_zoom_map.h"
23#include "content/browser/in_process_webkit/session_storage_namespace.h"
[email protected]37a72af2011-06-13 05:42:0124#include "content/browser/load_from_memory_cache_details.h"
[email protected]35e251d2011-05-24 21:01:0425#include "content/browser/load_notification_details.h"
[email protected]567812d2011-02-24 17:40:5026#include "content/browser/renderer_host/render_process_host.h"
27#include "content/browser/renderer_host/render_view_host.h"
28#include "content/browser/renderer_host/render_widget_host_view.h"
[email protected]686493142011-07-15 21:47:2229#include "content/browser/renderer_host/resource_dispatcher_host.h"
[email protected]567812d2011-02-24 17:40:5030#include "content/browser/renderer_host/resource_request_details.h"
31#include "content/browser/site_instance.h"
[email protected]0dd3a0ab2011-02-18 08:17:4432#include "content/browser/tab_contents/interstitial_page.h"
[email protected]8286f51a2011-05-31 17:39:1333#include "content/browser/tab_contents/navigation_details.h"
[email protected]0dd3a0ab2011-02-18 08:17:4434#include "content/browser/tab_contents/navigation_entry.h"
35#include "content/browser/tab_contents/provisional_load_details.h"
36#include "content/browser/tab_contents/tab_contents_delegate.h"
37#include "content/browser/tab_contents/tab_contents_observer.h"
38#include "content/browser/tab_contents/tab_contents_view.h"
[email protected]105bb0f2011-05-24 17:12:1439#include "content/browser/tab_contents/title_updated_details.h"
[email protected]afd1e522011-04-27 23:29:5940#include "content/browser/user_metrics.h"
[email protected]1fd1a502011-03-30 16:55:5641#include "content/browser/webui/web_ui_factory.h"
[email protected]9966325b2011-04-18 05:00:1042#include "content/common/bindings_policy.h"
[email protected]1fd1a502011-03-30 16:55:5643#include "content/common/content_client.h"
[email protected]763ec4ca2011-04-29 15:48:1244#include "content/common/content_restriction.h"
[email protected]4dd57932011-03-17 06:06:1245#include "content/common/navigation_types.h"
[email protected]7f070d42011-03-09 20:25:3246#include "content/common/notification_service.h"
[email protected]0f012df82011-05-19 14:15:2947#include "content/common/url_constants.h"
[email protected]2c5569662011-03-22 20:45:0248#include "content/common/view_messages.h"
[email protected]d686e812009-06-03 19:10:2949#include "net/base/net_util.h"
[email protected]420ae012009-04-24 05:16:3250#include "net/base/registry_controlled_domain.h"
[email protected]abe2c032011-03-31 18:49:3451#include "net/url_request/url_request_context_getter.h"
[email protected]8bd0fe62011-01-17 06:44:3752#include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h"
[email protected]08397d52011-02-05 01:53:3853#include "ui/gfx/codec/png_codec.h"
[email protected]d5e311b2010-07-20 17:15:0354#include "webkit/glue/password_form.h"
[email protected]3c733bde2010-12-21 19:56:3155#include "webkit/glue/webpreferences.h"
56
57#if defined(OS_MACOSX)
[email protected]b9b751f22011-03-25 14:04:1258#include "ui/gfx/surface/io_surface_support_mac.h"
[email protected]3c733bde2010-12-21 19:56:3159#endif // defined(OS_MACOSX)
[email protected]3e45ba92009-02-20 21:09:0060
[email protected]420ae012009-04-24 05:16:3261// Cross-Site Navigations
62//
63// If a TabContents is told to navigate to a different web site (as determined
64// by SiteInstance), it will replace its current RenderViewHost with a new
65// RenderViewHost dedicated to the new SiteInstance. This works as follows:
66//
67// - Navigate determines whether the destination is cross-site, and if so,
68// it creates a pending_render_view_host_ and moves into the PENDING
69// RendererState.
70// - The pending RVH is "suspended," so that no navigation messages are sent to
71// its renderer until the onbeforeunload JavaScript handler has a chance to
72// run in the current RVH.
73// - The pending RVH tells CrossSiteRequestManager (a thread-safe singleton)
74// that it has a pending cross-site request. ResourceDispatcherHost will
75// check for this when the response arrives.
76// - The current RVH runs its onbeforeunload handler. If it returns false, we
77// cancel all the pending logic and go back to NORMAL. Otherwise we allow
78// the pending RVH to send the navigation request to its renderer.
79// - ResourceDispatcherHost receives a ResourceRequest on the IO thread. It
80// checks CrossSiteRequestManager to see that the RVH responsible has a
81// pending cross-site request, and then installs a CrossSiteEventHandler.
82// - When RDH receives a response, the BufferedEventHandler determines whether
83// it is a download. If so, it sends a message to the new renderer causing
84// it to cancel the request, and the download proceeds in the download
85// thread. For now, we stay in a PENDING state (with a pending RVH) until
[email protected]57c6a652009-05-04 07:58:3486// the next DidNavigate event for this TabContents. This isn't ideal, but it
[email protected]420ae012009-04-24 05:16:3287// doesn't affect any functionality.
88// - After RDH receives a response and determines that it is safe and not a
89// download, it pauses the response to first run the old page's onunload
90// handler. It does this by asynchronously calling the OnCrossSiteResponse
[email protected]992db4c2011-05-12 15:37:1591// method of TabContents on the UI thread, which sends a SwapOut message
[email protected]420ae012009-04-24 05:16:3292// to the current RVH.
[email protected]992db4c2011-05-12 15:37:1593// - Once the onunload handler is finished, a SwapOut_ACK message is sent to
[email protected]420ae012009-04-24 05:16:3294// the ResourceDispatcherHost, who unpauses the response. Data is then sent
95// to the pending RVH.
96// - The pending renderer sends a FrameNavigate message that invokes the
97// DidNavigate method. This replaces the current RVH with the
98// pending RVH and goes back to the NORMAL RendererState.
[email protected]992db4c2011-05-12 15:37:1599// - The previous renderer is kept swapped out in RenderViewHostManager in case
100// the user goes back. The process only stays live if another tab is using
101// it, but if so, the existing frame relationships will be maintained.
[email protected]420ae012009-04-24 05:16:32102
103namespace {
104
105// Amount of time we wait between when a key event is received and the renderer
106// is queried for its state and pushed to the NavigationEntry.
107const int kQueryStateDelay = 5000;
108
[email protected]6ebdc9b2010-09-27 16:55:57109const int kSyncWaitDelay = 40;
110
[email protected]ca406032011-07-19 21:53:05111static const char kDotGoogleDotCom[] = ".google.com";
112
[email protected]420ae012009-04-24 05:16:32113#if defined(OS_WIN)
114
115BOOL CALLBACK InvalidateWindow(HWND hwnd, LPARAM lparam) {
116 // Note: erase is required to properly paint some widgets borders. This can
117 // be seen with textfields.
118 InvalidateRect(hwnd, NULL, TRUE);
119 return TRUE;
120}
121#endif
122
[email protected]2c5569662011-03-22 20:45:02123ViewMsg_Navigate_Type::Value GetNavigationType(
[email protected]3d7474ff2011-07-27 17:47:37124 content::BrowserContext* browser_context, const NavigationEntry& entry,
[email protected]1ccb3568d2010-02-19 10:51:16125 NavigationController::ReloadType reload_type) {
126 switch (reload_type) {
127 case NavigationController::RELOAD:
[email protected]2c5569662011-03-22 20:45:02128 return ViewMsg_Navigate_Type::RELOAD;
[email protected]1ccb3568d2010-02-19 10:51:16129 case NavigationController::RELOAD_IGNORING_CACHE:
[email protected]2c5569662011-03-22 20:45:02130 return ViewMsg_Navigate_Type::RELOAD_IGNORING_CACHE;
[email protected]1ccb3568d2010-02-19 10:51:16131 case NavigationController::NO_RELOAD:
132 break; // Fall through to rest of function.
133 }
[email protected]5e369672009-11-03 23:48:30134
135 if (entry.restore_type() == NavigationEntry::RESTORE_LAST_SESSION &&
[email protected]3d7474ff2011-07-27 17:47:37136 browser_context->DidLastSessionExitCleanly())
[email protected]2c5569662011-03-22 20:45:02137 return ViewMsg_Navigate_Type::RESTORE;
[email protected]5e369672009-11-03 23:48:30138
[email protected]2c5569662011-03-22 20:45:02139 return ViewMsg_Navigate_Type::NORMAL;
[email protected]5e369672009-11-03 23:48:30140}
141
[email protected]876bc832010-09-07 16:29:54142void MakeNavigateParams(const NavigationEntry& entry,
143 const NavigationController& controller,
[email protected]1ccb3568d2010-02-19 10:51:16144 NavigationController::ReloadType reload_type,
145 ViewMsg_Navigate_Params* params) {
[email protected]056de2d2009-06-26 16:41:34146 params->page_id = entry.page_id();
[email protected]876bc832010-09-07 16:29:54147 params->pending_history_list_offset = controller.GetIndexOfEntry(&entry);
[email protected]3cc72b12010-03-18 23:03:00148 params->current_history_list_offset = controller.last_committed_entry_index();
149 params->current_history_list_length = controller.entry_count();
[email protected]056de2d2009-06-26 16:41:34150 params->url = entry.url();
151 params->referrer = entry.referrer();
152 params->transition = entry.transition_type();
153 params->state = entry.content_state();
[email protected]3cc72b12010-03-18 23:03:00154 params->navigation_type =
[email protected]3d7474ff2011-07-27 17:47:37155 GetNavigationType(controller.browser_context(), entry, reload_type);
[email protected]056de2d2009-06-26 16:41:34156 params->request_time = base::Time::Now();
157}
158
[email protected]420ae012009-04-24 05:16:32159} // namespace
160
[email protected]f4f50ef2011-01-21 19:01:19161
162// TabContents ----------------------------------------------------------------
[email protected]420ae012009-04-24 05:16:32163
[email protected]3d7474ff2011-07-27 17:47:37164TabContents::TabContents(content::BrowserContext* browser_context,
[email protected]420ae012009-04-24 05:16:32165 SiteInstance* site_instance,
166 int routing_id,
[email protected]6ee12c42010-09-14 09:36:07167 const TabContents* base_tab_contents,
168 SessionStorageNamespace* session_storage_namespace)
[email protected]b680ad22009-04-15 23:19:42169 : delegate_(NULL),
[email protected]6ee12c42010-09-14 09:36:07170 ALLOW_THIS_IN_INITIALIZER_LIST(controller_(
[email protected]3d7474ff2011-07-27 17:47:37171 this, browser_context, session_storage_namespace)),
[email protected]66ba4932009-06-04 19:22:13172 ALLOW_THIS_IN_INITIALIZER_LIST(view_(
[email protected]74313b42011-08-24 16:51:32173 content::GetContentClient()->browser()->CreateTabContentsView(this))),
[email protected]d82ed61e2009-06-16 02:46:22174 ALLOW_THIS_IN_INITIALIZER_LIST(render_manager_(this, this)),
[email protected]d5f942ba2008-09-26 19:30:34175 is_loading_(false),
[email protected]443b80e2010-12-14 00:42:23176 crashed_status_(base::TERMINATION_STATUS_STILL_RUNNING),
177 crashed_error_code_(0),
[email protected]d5f942ba2008-09-26 19:30:34178 waiting_for_response_(false),
[email protected]d5f942ba2008-09-26 19:30:34179 max_page_id_(-1),
[email protected]9c235f042011-08-10 22:28:21180 load_state_(net::LOAD_STATE_IDLE, string16()),
[email protected]094e5b22009-09-25 04:23:56181 upload_size_(0),
182 upload_position_(0),
[email protected]f17a0ee2010-05-17 17:38:47183 displayed_insecure_content_(false),
[email protected]fdd61c62009-04-22 19:22:57184 capturing_contents_(false),
185 is_being_destroyed_(false),
186 notify_disconnection_(false),
[email protected]2e5b90c2011-08-16 21:11:55187 dialog_creator_(NULL),
[email protected]fdd61c62009-04-22 19:22:57188#if defined(OS_WIN)
189 message_box_active_(CreateEvent(NULL, TRUE, FALSE, NULL)),
190#endif
[email protected]7ab1e7d62009-10-14 23:32:01191 is_showing_before_unload_dialog_(false),
[email protected]1fd1a502011-03-30 16:55:56192 opener_web_ui_type_(WebUI::kNoWebUI),
[email protected]ebf40a72010-07-22 01:46:38193 closed_by_user_gesture_(false),
[email protected]b75b8292010-10-01 07:28:25194 minimum_zoom_percent_(
195 static_cast<int>(WebKit::WebView::minTextSizeMultiplier * 100)),
196 maximum_zoom_percent_(
197 static_cast<int>(WebKit::WebView::maxTextSizeMultiplier * 100)),
[email protected]9e823662010-10-13 23:36:00198 temporary_zoom_settings_(false),
[email protected]4850a7f2011-03-08 23:36:59199 content_restrictions_(0) {
[email protected]7ff431e2010-01-07 18:18:54200
[email protected]3d7474ff2011-07-27 17:47:37201 render_manager_.Init(browser_context, site_instance, routing_id);
[email protected]420ae012009-04-24 05:16:32202
[email protected]34ac70502009-09-25 17:07:23203 // We have the initial size of the view be based on the size of the passed in
204 // tab contents (normally a tab from the same window).
205 view_->CreateView(base_tab_contents ?
206 base_tab_contents->view()->GetContainerSize() : gfx::Size());
[email protected]332af7732009-03-11 13:21:35207}
initial.commit09911bf2008-07-26 23:55:29208
209TabContents::~TabContents() {
[email protected]420ae012009-04-24 05:16:32210 is_being_destroyed_ = true;
211
[email protected]3ab9cb82011-06-03 18:02:07212 // Clear out any JavaScript state.
[email protected]2e5b90c2011-08-16 21:11:55213 if (dialog_creator_)
214 dialog_creator_->ResetJavaScriptState(this);
[email protected]3ab9cb82011-06-03 18:02:07215
[email protected]420ae012009-04-24 05:16:32216 NotifyDisconnected();
[email protected]420ae012009-04-24 05:16:32217
[email protected]420ae012009-04-24 05:16:32218 // First cleanly close all child windows.
219 // TODO(mpcomplete): handle case if MaybeCloseChildWindows() already asked
220 // some of these to close. CloseWindows is async, so it might get called
221 // twice before it runs.
[email protected]8cc8d492010-02-02 10:40:49222 CloseConstrainedWindows();
[email protected]420ae012009-04-24 05:16:32223
[email protected]420ae012009-04-24 05:16:32224 // Notify any observer that have a reference on this tab contents.
225 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:27226 content::NOTIFICATION_TAB_CONTENTS_DESTROYED,
[email protected]420ae012009-04-24 05:16:32227 Source<TabContents>(this),
228 NotificationService::NoDetails());
229
230 // TODO(brettw) this should be moved to the view.
231#if defined(OS_WIN)
232 // If we still have a window handle, destroy it. GetNativeView can return
233 // NULL if this contents was part of a window that closed.
[email protected]50bd6452010-11-27 19:39:42234 if (GetNativeView()) {
235 RenderViewHost* host = render_view_host();
[email protected]b9a1fb42011-04-12 23:16:44236 if (host && host->view())
[email protected]50bd6452010-11-27 19:39:42237 host->view()->WillWmDestroy();
[email protected]50bd6452010-11-27 19:39:42238 }
[email protected]420ae012009-04-24 05:16:32239#endif
[email protected]7ab1e7d62009-10-14 23:32:01240
241 // OnCloseStarted isn't called in unit tests.
242 if (!tab_close_start_time_.is_null()) {
243 UMA_HISTOGRAM_TIMES("Tab.Close",
244 base::TimeTicks::Now() - tab_close_start_time_);
245 }
[email protected]b5a1d11c2011-02-17 03:09:42246
[email protected]07d490bc2011-03-07 17:05:26247 FOR_EACH_OBSERVER(TabContentsObserver, observers_, TabContentsDestroyed());
[email protected]232a5812011-03-04 22:42:08248
[email protected]1de2b8b2011-06-29 19:38:46249 set_delegate(NULL);
250}
251
252// TODO(cbentzel): Either remove the debugging code, or rename to SetDelegate.
253void TabContents::set_delegate(TabContentsDelegate* delegate) {
254 if (delegate == delegate_)
255 return;
256 if (delegate_)
257 delegate_->Detach(this);
258 delegate_ = delegate;
259 if (delegate_)
260 delegate_->Attach(this);
[email protected]b5a1d11c2011-02-17 03:09:42261}
262
[email protected]724159a2010-12-30 01:11:18263bool TabContents::OnMessageReceived(const IPC::Message& message) {
[email protected]f82d57b52011-04-27 19:13:17264 if (web_ui() && web_ui()->OnMessageReceived(message))
265 return true;
266
[email protected]585b30362011-01-28 02:30:17267 ObserverListBase<TabContentsObserver>::Iterator it(observers_);
268 TabContentsObserver* observer;
[email protected]0f180ee2011-01-21 18:23:10269 while ((observer = it.GetNext()) != NULL)
270 if (observer->OnMessageReceived(message))
[email protected]403415a2011-01-10 18:57:53271 return true;
[email protected]403415a2011-01-10 18:57:53272
[email protected]724159a2010-12-30 01:11:18273 bool handled = true;
274 bool message_is_ok = true;
275 IPC_BEGIN_MESSAGE_MAP_EX(TabContents, message, message_is_ok)
276 IPC_MESSAGE_HANDLER(ViewHostMsg_DidStartProvisionalLoadForFrame,
277 OnDidStartProvisionalLoadForFrame)
278 IPC_MESSAGE_HANDLER(ViewHostMsg_DidRedirectProvisionalLoad,
279 OnDidRedirectProvisionalLoad)
280 IPC_MESSAGE_HANDLER(ViewHostMsg_DidFailProvisionalLoadWithError,
281 OnDidFailProvisionalLoadWithError)
282 IPC_MESSAGE_HANDLER(ViewHostMsg_DidLoadResourceFromMemoryCache,
283 OnDidLoadResourceFromMemoryCache)
284 IPC_MESSAGE_HANDLER(ViewHostMsg_DidDisplayInsecureContent,
285 OnDidDisplayInsecureContent)
286 IPC_MESSAGE_HANDLER(ViewHostMsg_DidRunInsecureContent,
287 OnDidRunInsecureContent)
288 IPC_MESSAGE_HANDLER(ViewHostMsg_DocumentLoadedInFrame,
289 OnDocumentLoadedInFrame)
290 IPC_MESSAGE_HANDLER(ViewHostMsg_DidFinishLoad, OnDidFinishLoad)
[email protected]c8f73ab2011-01-22 00:05:17291 IPC_MESSAGE_HANDLER(ViewHostMsg_UpdateContentRestrictions,
292 OnUpdateContentRestrictions)
[email protected]7d472472011-01-22 01:30:25293 IPC_MESSAGE_HANDLER(ViewHostMsg_GoToEntryAtOffset, OnGoToEntryAtOffset)
[email protected]216813952011-05-19 22:21:26294 IPC_MESSAGE_HANDLER(ViewHostMsg_UpdateZoomLimits, OnUpdateZoomLimits)
295 IPC_MESSAGE_HANDLER(ViewHostMsg_FocusedNodeChanged, OnFocusedNodeChanged)
[email protected]c7dd2f62011-07-18 15:57:59296 IPC_MESSAGE_HANDLER(ViewHostMsg_SaveURLAs, OnSaveURL)
[email protected]3a29a6e2011-08-24 18:26:21297 IPC_MESSAGE_HANDLER(ViewHostMsg_EnumerateDirectory, OnEnumerateDirectory)
[email protected]7d189022011-08-25 22:54:20298 IPC_MESSAGE_HANDLER(ViewHostMsg_JSOutOfMemory, OnJSOutOfMemory)
299 IPC_MESSAGE_HANDLER(ViewHostMsg_RegisterProtocolHandler,
300 OnRegisterProtocolHandler)
301 IPC_MESSAGE_HANDLER(ViewHostMsg_RegisterIntentHandler,
302 OnRegisterIntentHandler)
303 IPC_MESSAGE_HANDLER(ViewHostMsg_WebIntentDispatch,
304 OnWebIntentDispatch)
[email protected]724159a2010-12-30 01:11:18305 IPC_MESSAGE_UNHANDLED(handled = false)
306 IPC_END_MESSAGE_MAP_EX()
307
308 if (!message_is_ok) {
309 UserMetrics::RecordAction(UserMetricsAction("BadMessageTerminate_RVD"));
310 GetRenderProcessHost()->ReceivedBadMessage();
311 }
312
313 return handled;
314}
315
[email protected]6c2e472f2011-08-24 23:26:18316void TabContents::RunFileChooser(
317 RenderViewHost* render_view_host,
318 const ViewHostMsg_RunFileChooser_Params& params) {
319 delegate()->RunFileChooser(this, params);
320}
321
[email protected]8cb5d5b2010-02-09 11:36:16322RenderProcessHost* TabContents::GetRenderProcessHost() const {
323 return render_manager_.current_host()->process();
324}
325
[email protected]d5f942ba2008-09-26 19:30:34326const GURL& TabContents::GetURL() const {
327 // We may not have a navigation entry yet
[email protected]ce3fa3c2009-04-20 19:55:57328 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]ebe89e062009-08-13 23:16:54329 return entry ? entry->virtual_url() : GURL::EmptyGURL();
[email protected]d5f942ba2008-09-26 19:30:34330}
331
[email protected]96d185d2009-04-24 03:28:54332const string16& TabContents::GetTitle() const {
[email protected]4c6092c5b2009-06-06 00:23:55333 // Transient entries take precedence. They are used for interstitial pages
334 // that are shown on top of existing pages.
335 NavigationEntry* entry = controller_.GetTransientEntry();
[email protected]b5cca982011-05-26 04:42:08336 std::string accept_languages =
337 content::GetContentClient()->browser()->GetAcceptLangs(this);
[email protected]45d0ef7f2011-01-05 13:46:23338 if (entry) {
[email protected]b5cca982011-05-26 04:42:08339 return entry->GetTitleForDisplay(accept_languages);
[email protected]45d0ef7f2011-01-05 13:46:23340 }
[email protected]7ade2732011-02-10 00:13:58341 WebUI* our_web_ui = render_manager_.pending_web_ui() ?
342 render_manager_.pending_web_ui() : render_manager_.web_ui();
343 if (our_web_ui) {
[email protected]96d185d2009-04-24 03:28:54344 // Don't override the title in view source mode.
[email protected]4c6092c5b2009-06-06 00:23:55345 entry = controller_.GetActiveEntry();
[email protected]96d185d2009-04-24 03:28:54346 if (!(entry && entry->IsViewSourceMode())) {
[email protected]e0112912011-02-02 22:54:35347 // Give the Web UI the chance to override our title.
[email protected]7ade2732011-02-10 00:13:58348 const string16& title = our_web_ui->overridden_title();
[email protected]96d185d2009-04-24 03:28:54349 if (!title.empty())
350 return title;
351 }
352 }
353
354 // We use the title for the last committed entry rather than a pending
355 // navigation entry. For example, when the user types in a URL, we want to
356 // keep the old page's title until the new load has committed and we get a new
357 // title.
[email protected]96d185d2009-04-24 03:28:54358 entry = controller_.GetLastCommittedEntry();
[email protected]45d0ef7f2011-01-05 13:46:23359 if (entry) {
[email protected]b5cca982011-05-26 04:42:08360 return entry->GetTitleForDisplay(accept_languages);
[email protected]45d0ef7f2011-01-05 13:46:23361 }
[email protected]987fc3a2011-05-26 14:18:09362
363 // |page_title_when_no_navigation_entry_| is finally used
364 // if no title cannot be retrieved.
365 return page_title_when_no_navigation_entry_;
[email protected]96d185d2009-04-24 03:28:54366}
367
[email protected]d5f942ba2008-09-26 19:30:34368int32 TabContents::GetMaxPageID() {
369 if (GetSiteInstance())
370 return GetSiteInstance()->max_page_id();
371 else
372 return max_page_id_;
373}
374
375void TabContents::UpdateMaxPageID(int32 page_id) {
376 // Ensure both the SiteInstance and RenderProcessHost update their max page
[email protected]57c6a652009-05-04 07:58:34377 // IDs in sync. Only TabContents will also have site instances, except during
[email protected]d5f942ba2008-09-26 19:30:34378 // testing.
379 if (GetSiteInstance())
380 GetSiteInstance()->UpdateMaxPageID(page_id);
[email protected]8cb5d5b2010-02-09 11:36:16381 GetRenderProcessHost()->UpdateMaxPageID(page_id);
[email protected]d5f942ba2008-09-26 19:30:34382}
383
[email protected]96d185d2009-04-24 03:28:54384SiteInstance* TabContents::GetSiteInstance() const {
385 return render_manager_.current_host()->site_instance();
386}
387
[email protected]77362eb2011-08-01 17:18:38388SiteInstance* TabContents::GetPendingSiteInstance() const {
389 RenderViewHost* dest_rvh = render_manager_.pending_render_view_host() ?
390 render_manager_.pending_render_view_host() :
391 render_manager_.current_host();
392 return dest_rvh->site_instance();
393}
394
[email protected]96d185d2009-04-24 03:28:54395bool TabContents::ShouldDisplayURL() {
[email protected]8ab24cc2009-11-13 20:56:15396 // Don't hide the url in view source mode and with interstitials.
[email protected]96d185d2009-04-24 03:28:54397 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]8ab24cc2009-11-13 20:56:15398 if (entry && (entry->IsViewSourceMode() ||
[email protected]cccd3762010-11-12 18:40:01399 entry->page_type() == INTERSTITIAL_PAGE)) {
[email protected]96d185d2009-04-24 03:28:54400 return true;
[email protected]8ab24cc2009-11-13 20:56:15401 }
[email protected]83e3895b2009-06-11 00:07:16402
[email protected]2b9a06402011-02-16 04:57:10403 // We always display the URL for non-WebUI URLs to prevent spoofing.
[email protected]1fd1a502011-03-30 16:55:56404 if (entry && !content::WebUIFactory::Get()->HasWebUIScheme(entry->url()))
[email protected]2b9a06402011-02-16 04:57:10405 return true;
406
[email protected]d0980792011-02-13 19:41:40407 WebUI* web_ui = GetWebUIForCurrentState();
[email protected]7ade2732011-02-10 00:13:58408 if (web_ui)
409 return !web_ui->should_hide_url();
[email protected]96d185d2009-04-24 03:28:54410 return true;
411}
412
[email protected]f5d978c2011-07-21 14:43:51413bool TabContents::IsLoading() const {
414 return is_loading_ || (web_ui() && web_ui()->IsLoading());
415}
416
[email protected]585b30362011-01-28 02:30:17417void TabContents::AddObserver(TabContentsObserver* observer) {
418 observers_.AddObserver(observer);
[email protected]3c9e1872010-11-18 16:17:49419}
420
[email protected]585b30362011-01-28 02:30:17421void TabContents::RemoveObserver(TabContentsObserver* observer) {
422 observers_.RemoveObserver(observer);
[email protected]3c9e1872010-11-18 16:17:49423}
424
[email protected]443b80e2010-12-14 00:42:23425void TabContents::SetIsCrashed(base::TerminationStatus status, int error_code) {
426 if (status == crashed_status_)
[email protected]d5f942ba2008-09-26 19:30:34427 return;
428
[email protected]443b80e2010-12-14 00:42:23429 crashed_status_ = status;
430 crashed_error_code_ = error_code;
[email protected]c9cd2222009-05-06 05:16:50431 NotifyNavigationStateChanged(INVALIDATE_TAB);
[email protected]d5f942ba2008-09-26 19:30:34432}
433
434void TabContents::NotifyNavigationStateChanged(unsigned changed_flags) {
435 if (delegate_)
436 delegate_->NavigationStateChanged(this, changed_flags);
437}
438
[email protected]96d185d2009-04-24 03:28:54439void TabContents::DidBecomeSelected() {
440 controller_.SetActive(true);
[email protected]8cb5d5b2010-02-09 11:36:16441 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
442 if (rwhv) {
443 rwhv->DidBecomeSelected();
[email protected]789e9152009-08-04 21:59:43444#if defined(OS_MACOSX)
[email protected]8cb5d5b2010-02-09 11:36:16445 rwhv->SetActive(true);
[email protected]789e9152009-08-04 21:59:43446#endif
447 }
[email protected]96d185d2009-04-24 03:28:54448
[email protected]5ac20162010-11-24 23:33:11449 last_selected_time_ = base::TimeTicks::Now();
[email protected]3e69bc82011-05-26 23:22:38450
451 FOR_EACH_OBSERVER(TabContentsObserver, observers_, DidBecomeSelected());
[email protected]96d185d2009-04-24 03:28:54452}
453
454void TabContents::WasHidden() {
455 if (!capturing_contents()) {
456 // |render_view_host()| can be NULL if the user middle clicks a link to open
457 // a tab in then background, then closes the tab before selecting it. This
458 // is because closing the tab calls TabContents::Destroy(), which removes
459 // the |render_view_host()|; then when we actually destroy the window,
460 // OnWindowPosChanged() notices and calls HideContents() (which calls us).
[email protected]8cb5d5b2010-02-09 11:36:16461 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
462 if (rwhv)
463 rwhv->WasHidden();
[email protected]96d185d2009-04-24 03:28:54464 }
465
466 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:27467 content::NOTIFICATION_TAB_CONTENTS_HIDDEN,
[email protected]96d185d2009-04-24 03:28:54468 Source<TabContents>(this),
469 NotificationService::NoDetails());
470}
471
[email protected]d5f942ba2008-09-26 19:30:34472void TabContents::Activate() {
473 if (delegate_)
474 delegate_->ActivateContents(this);
475}
476
[email protected]ea42e7782010-08-23 23:58:12477void TabContents::Deactivate() {
478 if (delegate_)
479 delegate_->DeactivateContents(this);
480}
481
[email protected]63954792011-07-11 04:17:48482void TabContents::LostCapture() {
483 if (delegate_)
484 delegate_->LostCapture();
485}
486
487bool TabContents::PreHandleKeyboardEvent(const NativeWebKeyboardEvent& event,
488 bool* is_keyboard_shortcut) {
489 return delegate_ &&
490 delegate_->PreHandleKeyboardEvent(event, is_keyboard_shortcut);
491}
492
493void TabContents::HandleKeyboardEvent(const NativeWebKeyboardEvent& event) {
494 if (delegate_)
495 delegate_->HandleKeyboardEvent(event);
496}
497
498void TabContents::HandleMouseUp() {
499 if (delegate_)
500 delegate_->HandleMouseUp();
501}
502
503void TabContents::HandleMouseActivate() {
504 if (delegate_)
505 delegate_->HandleMouseActivate();
506}
507
[email protected]8a5e0ca2011-08-25 06:30:47508void TabContents::ToggleFullscreenMode(bool enter_fullscreen) {
509 if (delegate_)
510 delegate_->ToggleFullscreenModeForTab(this, enter_fullscreen);
511}
512
[email protected]96d185d2009-04-24 03:28:54513void TabContents::ShowContents() {
[email protected]8cb5d5b2010-02-09 11:36:16514 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
515 if (rwhv)
516 rwhv->DidBecomeSelected();
[email protected]96d185d2009-04-24 03:28:54517}
518
519void TabContents::HideContents() {
520 // TODO(pkasting): http://b/1239839 Right now we purposefully don't call
521 // our superclass HideContents(), because some callers want to be very picky
522 // about the order in which these get called. In addition to making the code
523 // here practically impossible to understand, this also means we end up
524 // calling TabContents::WasHidden() twice if callers call both versions of
[email protected]57c6a652009-05-04 07:58:34525 // HideContents() on a TabContents.
[email protected]96d185d2009-04-24 03:28:54526 WasHidden();
527}
528
[email protected]5aab5e22010-12-08 22:13:29529bool TabContents::NeedToFireBeforeUnload() {
530 // TODO(creis): Should we fire even for interstitial pages?
531 return notify_disconnection() &&
532 !showing_interstitial_page() &&
533 !render_view_host()->SuddenTerminationAllowed();
534}
535
[email protected]00c37fc2011-08-02 00:22:50536// TODO(adriansc): Remove this method once refactoring changed all call sites.
[email protected]992e4542011-07-20 23:09:25537TabContents* TabContents::OpenURL(const GURL& url,
538 const GURL& referrer,
539 WindowOpenDisposition disposition,
540 PageTransition::Type transition) {
[email protected]00c37fc2011-08-02 00:22:50541 return OpenURL(OpenURLParams(url, referrer, disposition, transition));
542}
543
544TabContents* TabContents::OpenURL(const OpenURLParams& params) {
[email protected]1c642b52011-04-15 09:05:49545 if (delegate_) {
[email protected]00c37fc2011-08-02 00:22:50546 TabContents* new_contents = delegate_->OpenURLFromTab(this, params);
[email protected]1c642b52011-04-15 09:05:49547 // Notify observers.
548 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
[email protected]00c37fc2011-08-02 00:22:50549 DidOpenURL(params.url, params.referrer,
550 params.disposition, params.transition));
[email protected]992e4542011-07-20 23:09:25551 return new_contents;
[email protected]1c642b52011-04-15 09:05:49552 }
[email protected]992e4542011-07-20 23:09:25553 return NULL;
[email protected]d5f942ba2008-09-26 19:30:34554}
555
[email protected]1ccb3568d2010-02-19 10:51:16556bool TabContents::NavigateToPendingEntry(
557 NavigationController::ReloadType reload_type) {
[email protected]876bc832010-09-07 16:29:54558 return NavigateToEntry(*controller_.pending_entry(), reload_type);
559}
[email protected]96d185d2009-04-24 03:28:54560
[email protected]876bc832010-09-07 16:29:54561bool TabContents::NavigateToEntry(
562 const NavigationEntry& entry,
563 NavigationController::ReloadType reload_type) {
[email protected]96d185d2009-04-24 03:28:54564 RenderViewHost* dest_render_view_host = render_manager_.Navigate(entry);
565 if (!dest_render_view_host)
566 return false; // Unable to create the desired render view host.
567
[email protected]80a8fad2011-01-29 04:02:38568 // For security, we should never send non-Web-UI URLs to a Web UI renderer.
[email protected]05fdd492010-11-15 17:52:07569 // Double check that here.
[email protected]54ec6472010-04-09 19:39:58570 int enabled_bindings = dest_render_view_host->enabled_bindings();
[email protected]1fd1a502011-03-30 16:55:56571 bool is_allowed_in_web_ui_renderer = content::GetContentClient()->
[email protected]3d7474ff2011-07-27 17:47:37572 browser()->GetWebUIFactory()->IsURLAcceptableForWebUI(browser_context(),
[email protected]1fd1a502011-03-30 16:55:56573 entry.url());
[email protected]c09163a2011-02-15 00:05:55574 CHECK(!BindingsPolicy::is_web_ui_enabled(enabled_bindings) ||
575 is_allowed_in_web_ui_renderer);
[email protected]54ec6472010-04-09 19:39:58576
[email protected]96d185d2009-04-24 03:28:54577 // Tell DevTools agent that it is attached prior to the navigation.
[email protected]d9f9b792009-06-24 13:17:12578 DevToolsManager* devtools_manager = DevToolsManager::GetInstance();
[email protected]7e8e6b62009-05-08 11:28:32579 if (devtools_manager) { // NULL in unit tests.
[email protected]3cc72b12010-03-18 23:03:00580 devtools_manager->OnNavigatingToPendingEntry(render_view_host(),
581 dest_render_view_host,
582 entry.url());
[email protected]7e8e6b62009-05-08 11:28:32583 }
[email protected]96d185d2009-04-24 03:28:54584
585 // Used for page load time metrics.
586 current_load_start_ = base::TimeTicks::Now();
587
588 // Navigate in the desired RenderViewHost.
[email protected]056de2d2009-06-26 16:41:34589 ViewMsg_Navigate_Params navigate_params;
[email protected]876bc832010-09-07 16:29:54590 MakeNavigateParams(entry, controller_, reload_type, &navigate_params);
[email protected]52c68652010-12-07 17:47:04591 if (delegate_) {
592 navigate_params.extra_headers =
593 delegate_->GetNavigationHeaders(navigate_params.url);
594 }
[email protected]056de2d2009-06-26 16:41:34595 dest_render_view_host->Navigate(navigate_params);
[email protected]96d185d2009-04-24 03:28:54596
597 if (entry.page_id() == -1) {
598 // HACK!! This code suppresses javascript: URLs from being added to
599 // session history, which is what we want to do for javascript: URLs that
600 // do not generate content. What we really need is a message from the
601 // renderer telling us that a new page was not created. The same message
602 // could be used for mailto: URLs and the like.
603 if (entry.url().SchemeIs(chrome::kJavaScriptScheme))
604 return false;
605 }
606
[email protected]3c9e1872010-11-18 16:17:49607 // Notify observers about navigation.
[email protected]b375c5d2011-05-03 21:15:04608 FOR_EACH_OBSERVER(TabContentsObserver,
609 observers_,
610 NavigateToPendingEntry(entry.url(), reload_type));
[email protected]96d185d2009-04-24 03:28:54611
[email protected]09b29342011-06-24 19:18:48612 if (delegate_)
613 delegate_->DidNavigateToPendingEntry(this);
614
[email protected]d5f942ba2008-09-26 19:30:34615 return true;
616}
617
[email protected]9e1ad4b2011-08-14 16:49:19618void TabContents::SetHistoryLengthAndPrune(const SiteInstance* site_instance,
619 int history_length,
620 int32 minimum_page_id) {
[email protected]5229253b2011-08-16 14:36:41621 // SetHistoryLengthAndPrune doesn't work when there are pending cross-site
622 // navigations. Callers should ensure that this is the case.
[email protected]9e1ad4b2011-08-14 16:49:19623 if (render_manager_.pending_render_view_host()) {
624 NOTREACHED();
[email protected]796931a92011-08-10 01:32:14625 return;
[email protected]9e1ad4b2011-08-14 16:49:19626 }
627 RenderViewHost* rvh = render_view_host();
628 if (!rvh) {
629 NOTREACHED();
630 return;
631 }
632 if (site_instance && rvh->site_instance() != site_instance) {
633 NOTREACHED();
634 return;
635 }
636 rvh->Send(new ViewMsg_SetHistoryLengthAndPrune(rvh->routing_id(),
637 history_length,
638 minimum_page_id));
[email protected]796931a92011-08-10 01:32:14639}
640
[email protected]96d185d2009-04-24 03:28:54641void TabContents::Stop() {
642 render_manager_.Stop();
[email protected]2e3f4572011-03-25 19:24:47643 FOR_EACH_OBSERVER(TabContentsObserver, observers_, StopNavigation());
[email protected]96d185d2009-04-24 03:28:54644}
645
[email protected]96d185d2009-04-24 03:28:54646TabContents* TabContents::Clone() {
647 // We create a new SiteInstance so that the new tab won't share processes
648 // with the old one. This can be changed in the future if we need it to share
649 // processes for some reason.
[email protected]3d7474ff2011-07-27 17:47:37650 TabContents* tc = new TabContents(
651 browser_context(),
652 SiteInstance::CreateSiteInstance(browser_context()),
653 MSG_ROUTING_NONE, this, NULL);
[email protected]96d185d2009-04-24 03:28:54654 tc->controller().CopyStateFrom(controller_);
655 return tc;
656}
657
[email protected]4d677202009-07-19 07:37:12658void TabContents::ShowPageInfo(const GURL& url,
659 const NavigationEntry::SSLStatus& ssl,
660 bool show_history) {
661 if (!delegate_)
662 return;
663
[email protected]3d7474ff2011-07-27 17:47:37664 delegate_->ShowPageInfo(browser_context(), url, ssl, show_history);
[email protected]4d677202009-07-19 07:37:12665}
666
[email protected]aed59602011-02-28 22:57:33667void TabContents::AddConstrainedDialog(ConstrainedWindow* window) {
initial.commit09911bf2008-07-26 23:55:29668 child_windows_.push_back(window);
[email protected]fa1cf0b82010-01-15 21:49:44669
670 if (child_windows_.size() == 1) {
671 window->ShowConstrainedWindow();
672 BlockTabContent(true);
673 }
initial.commit09911bf2008-07-26 23:55:29674}
675
[email protected]fa1cf0b82010-01-15 21:49:44676void TabContents::BlockTabContent(bool blocked) {
[email protected]8dccd7cb2010-02-25 05:19:55677 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
[email protected]1b072562010-12-13 19:37:45678 // 70% opaque grey.
679 SkColor greyish = SkColorSetARGB(178, 0, 0, 0);
[email protected]8dccd7cb2010-02-25 05:19:55680 if (rwhv)
[email protected]1b072562010-12-13 19:37:45681 rwhv->SetVisuallyDeemphasized(blocked ? &greyish : NULL, false);
[email protected]a7eccac2011-04-15 03:05:19682 // RenderViewHost may be NULL during shutdown.
683 if (render_view_host())
684 render_view_host()->set_ignore_input_events(blocked);
[email protected]fa1cf0b82010-01-15 21:49:44685 if (delegate_)
686 delegate_->SetTabContentBlocked(this, blocked);
687}
688
[email protected]e7cfdbd2011-04-22 14:41:37689void TabContents::AddNewContents(TabContents* new_contents,
690 WindowOpenDisposition disposition,
691 const gfx::Rect& initial_pos,
692 bool user_gesture) {
initial.commit09911bf2008-07-26 23:55:29693 if (!delegate_)
694 return;
695
[email protected]e7cfdbd2011-04-22 14:41:37696 delegate_->AddNewContents(this, new_contents, disposition, initial_pos,
697 user_gesture);
[email protected]96d185d2009-04-24 03:28:54698}
699
[email protected]5c9e97a2009-09-09 23:48:30700gfx::NativeView TabContents::GetContentNativeView() const {
[email protected]96d185d2009-04-24 03:28:54701 return view_->GetContentNativeView();
702}
703
704gfx::NativeView TabContents::GetNativeView() const {
705 return view_->GetNativeView();
706}
707
708void TabContents::GetContainerBounds(gfx::Rect *out) const {
709 view_->GetContainerBounds(out);
710}
711
712void TabContents::Focus() {
713 view_->Focus();
714}
715
[email protected]90daadb42009-06-08 21:27:28716void TabContents::FocusThroughTabTraversal(bool reverse) {
[email protected]7e383692009-06-12 19:14:54717 if (showing_interstitial_page()) {
718 render_manager_.interstitial_page()->FocusThroughTabTraversal(reverse);
719 return;
720 }
[email protected]96d185d2009-04-24 03:28:54721 render_view_host()->SetInitialFocus(reverse);
722}
723
724bool TabContents::FocusLocationBarByDefault() {
[email protected]d0980792011-02-13 19:41:40725 WebUI* web_ui = GetWebUIForCurrentState();
[email protected]7ade2732011-02-10 00:13:58726 if (web_ui)
727 return web_ui->focus_location_bar_by_default();
[email protected]3e3f0eb2009-06-22 18:33:43728 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]e0d481582009-09-15 21:06:25729 if (entry && entry->url() == GURL(chrome::kAboutBlankURL))
[email protected]3e3f0eb2009-06-22 18:33:43730 return true;
[email protected]96d185d2009-04-24 03:28:54731 return false;
732}
733
[email protected]a26dc362010-04-23 01:48:58734void TabContents::SetFocusToLocationBar(bool select_all) {
[email protected]a11aaf22010-03-30 00:03:38735 if (delegate())
[email protected]a26dc362010-04-23 01:48:58736 delegate()->SetFocusToLocationBar(select_all);
[email protected]a11aaf22010-03-30 00:03:38737}
738
[email protected]686493142011-07-15 21:47:22739bool TabContents::CanDownload(int request_id) {
740 TabContentsDelegate* d = delegate();
741 if (d)
742 return d->CanDownload(this, request_id);
743 return true;
744}
745
746void TabContents::OnStartDownload(DownloadItem* download) {
747 TabContentsDelegate* d = delegate();
748 if (d)
749 d->OnStartDownload(this, download);
750}
751
[email protected]d5f942ba2008-09-26 19:30:34752void TabContents::WillClose(ConstrainedWindow* window) {
[email protected]d6219912010-11-17 19:47:37753 ConstrainedWindowList::iterator i(
754 std::find(child_windows_.begin(), child_windows_.end(), window));
755 bool removed_topmost_window = i == child_windows_.begin();
756 if (i != child_windows_.end())
757 child_windows_.erase(i);
758 if (child_windows_.empty()) {
[email protected]fa1cf0b82010-01-15 21:49:44759 BlockTabContent(false);
[email protected]d6219912010-11-17 19:47:37760 } else {
761 if (removed_topmost_window)
762 child_windows_[0]->ShowConstrainedWindow();
763 BlockTabContent(true);
[email protected]fa1cf0b82010-01-15 21:49:44764 }
[email protected]66ba4932009-06-04 19:22:13765}
[email protected]d5f942ba2008-09-26 19:30:34766
[email protected]c7dd2f62011-07-18 15:57:59767void TabContents::OnSavePage() {
768 // If we can not save the page, try to download it.
769 if (!SavePackage::IsSavableContents(contents_mime_type())) {
[email protected]3d7474ff2011-07-27 17:47:37770 DownloadManager* dlm = browser_context()->GetDownloadManager();
[email protected]c7dd2f62011-07-18 15:57:59771 const GURL& current_page_url = GetURL();
772 if (dlm && current_page_url.is_valid()) {
773 dlm->DownloadUrl(current_page_url, GURL(), "", this);
[email protected]8bd9e562011-08-16 23:55:46774 download_stats::RecordDownloadCount(
775 download_stats::INITIATED_BY_SAVE_PACKAGE_FAILURE_COUNT);
[email protected]c7dd2f62011-07-18 15:57:59776 return;
777 }
778 }
779
780 Stop();
781
782 // Create the save package and possibly prompt the user for the name to save
783 // the page as. The user prompt is an asynchronous operation that runs on
784 // another thread.
785 save_package_ = new SavePackage(this);
786 save_package_->GetSaveInfo();
787}
788
789// Used in automated testing to bypass prompting the user for file names.
790// Instead, the names and paths are hard coded rather than running them through
791// file name sanitation and extension / mime checking.
792bool TabContents::SavePage(const FilePath& main_file, const FilePath& dir_path,
793 SavePackage::SavePackageType save_type) {
794 // Stop the page from navigating.
795 Stop();
796
797 save_package_ = new SavePackage(this, save_type, main_file, dir_path);
798 return save_package_->Init();
799}
800
801void TabContents::OnSaveURL(const GURL& url) {
[email protected]3d7474ff2011-07-27 17:47:37802 DownloadManager* dlm = browser_context()->GetDownloadManager();
[email protected]c7dd2f62011-07-18 15:57:59803 dlm->DownloadUrl(url, GetURL(), "", this);
804}
805
[email protected]420ae012009-04-24 05:16:32806bool TabContents::IsActiveEntry(int32 page_id) {
807 NavigationEntry* active_entry = controller_.GetActiveEntry();
808 return (active_entry != NULL &&
809 active_entry->site_instance() == GetSiteInstance() &&
810 active_entry->page_id() == page_id);
811}
812
[email protected]b2fe07d12010-02-09 14:38:08813void TabContents::SetOverrideEncoding(const std::string& encoding) {
[email protected]8cb5d5b2010-02-09 11:36:16814 set_encoding(encoding);
[email protected]216813952011-05-19 22:21:26815 render_view_host()->Send(new ViewMsg_SetPageEncoding(
816 render_view_host()->routing_id(), encoding));
[email protected]8cb5d5b2010-02-09 11:36:16817}
818
[email protected]b2fe07d12010-02-09 14:38:08819void TabContents::ResetOverrideEncoding() {
[email protected]8cb5d5b2010-02-09 11:36:16820 reset_encoding();
[email protected]216813952011-05-19 22:21:26821 render_view_host()->Send(new ViewMsg_ResetPageEncodingToDefault(
822 render_view_host()->routing_id()));
[email protected]8cb5d5b2010-02-09 11:36:16823}
824
[email protected]7ab1e7d62009-10-14 23:32:01825void TabContents::OnCloseStarted() {
826 if (tab_close_start_time_.is_null())
827 tab_close_start_time_ = base::TimeTicks::Now();
828}
829
[email protected]46624bf2010-06-09 16:04:19830bool TabContents::ShouldAcceptDragAndDrop() const {
831#if defined(OS_CHROMEOS)
832 // ChromeOS panels (pop-ups) do not take drag-n-drop.
833 // See http://crosbug.com/2413
[email protected]b35b26b32011-05-05 20:35:14834 if (delegate() && delegate()->IsPopupOrPanel(this))
[email protected]b83af492010-10-09 03:21:20835 return false;
836 return true;
[email protected]46624bf2010-06-09 16:04:19837#else
838 return true;
839#endif
840}
841
[email protected]7813bd72011-02-05 02:19:34842void TabContents::SystemDragEnded() {
843 if (render_view_host())
844 render_view_host()->DragSourceSystemDragEnded();
845 if (delegate())
846 delegate()->DragEnded();
847}
848
[email protected]d0b8d092010-10-25 04:05:17849double TabContents::GetZoomLevel() const {
[email protected]3d7474ff2011-07-27 17:47:37850 HostZoomMap* zoom_map = browser_context()->GetHostZoomMap();
[email protected]b75b8292010-10-01 07:28:25851 if (!zoom_map)
[email protected]d0b8d092010-10-25 04:05:17852 return 0;
[email protected]b75b8292010-10-01 07:28:25853
854 double zoom_level;
855 if (temporary_zoom_settings_) {
856 zoom_level = zoom_map->GetTemporaryZoomLevel(
857 GetRenderProcessHost()->id(), render_view_host()->routing_id());
858 } else {
[email protected]13ffa32e2011-05-27 16:37:01859 zoom_level = zoom_map->GetZoomLevel(net::GetHostOrSpecFromURL(GetURL()));
[email protected]b75b8292010-10-01 07:28:25860 }
[email protected]d0b8d092010-10-25 04:05:17861 return zoom_level;
862}
[email protected]b75b8292010-10-01 07:28:25863
[email protected]d0b8d092010-10-25 04:05:17864int TabContents::GetZoomPercent(bool* enable_increment,
865 bool* enable_decrement) {
866 *enable_decrement = *enable_increment = false;
[email protected]b75b8292010-10-01 07:28:25867 int percent = static_cast<int>(
[email protected]d0b8d092010-10-25 04:05:17868 WebKit::WebView::zoomLevelToZoomFactor(GetZoomLevel()) * 100);
[email protected]b75b8292010-10-01 07:28:25869 *enable_decrement = percent > minimum_zoom_percent_;
870 *enable_increment = percent < maximum_zoom_percent_;
871 return percent;
872}
873
[email protected]3c733bde2010-12-21 19:56:31874void TabContents::ViewSource() {
[email protected]1788e772010-12-15 16:40:50875 if (!delegate_)
876 return;
877
878 NavigationEntry* active_entry = controller().GetActiveEntry();
879 if (!active_entry)
880 return;
881
882 delegate_->ViewSourceForTab(this, active_entry->url());
[email protected]77d8d622010-12-15 10:30:12883}
884
[email protected]932b7a12011-03-09 12:50:27885void TabContents::ViewFrameSource(const GURL& url,
886 const std::string& content_state) {
887 if (!delegate_)
888 return;
889
890 delegate_->ViewSourceForFrame(this, url, content_state);
891}
892
[email protected]c40d6232011-03-25 00:16:21893void TabContents::SetContentRestrictions(int restrictions) {
894 content_restrictions_ = restrictions;
895 delegate()->ContentRestrictionsChanged(this);
896}
897
[email protected]724159a2010-12-30 01:11:18898void TabContents::OnDidStartProvisionalLoadForFrame(int64 frame_id,
899 bool is_main_frame,
[email protected]eacb080b2011-05-22 19:40:26900 bool has_opener_set,
[email protected]724159a2010-12-30 01:11:18901 const GURL& url) {
902 bool is_error_page = (url.spec() == chrome::kUnreachableWebDataURL);
903 GURL validated_url(url);
904 render_view_host()->FilterURL(ChildProcessSecurityPolicy::GetInstance(),
905 GetRenderProcessHost()->id(), &validated_url);
906
[email protected]8093a542011-05-13 07:29:32907 RenderViewHost* rvh =
908 render_manager_.pending_render_view_host() ?
909 render_manager_.pending_render_view_host() : render_view_host();
[email protected]0d60f0192011-04-14 12:40:10910 // Notify observers about the start of the provisional load.
911 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
912 DidStartProvisionalLoadForFrame(frame_id, is_main_frame,
[email protected]8093a542011-05-13 07:29:32913 validated_url, is_error_page, rvh));
[email protected]0d60f0192011-04-14 12:40:10914
[email protected]724159a2010-12-30 01:11:18915 if (is_main_frame) {
[email protected]4850a7f2011-03-08 23:36:59916 // Notify observers about the provisional change in the main frame URL.
917 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
[email protected]eacb080b2011-05-22 19:40:26918 ProvisionalChangeToMainFrameUrl(url, has_opener_set));
[email protected]724159a2010-12-30 01:11:18919 }
920}
921
922void TabContents::OnDidRedirectProvisionalLoad(int32 page_id,
[email protected]eacb080b2011-05-22 19:40:26923 bool has_opener_set,
[email protected]724159a2010-12-30 01:11:18924 const GURL& source_url,
925 const GURL& target_url) {
[email protected]18ba2b12011-04-06 16:35:49926 // TODO(creis): Remove this method and have the pre-rendering code listen to
927 // the ResourceDispatcherHost's RESOURCE_RECEIVED_REDIRECT notification
928 // instead. See http://crbug.com/78512.
[email protected]724159a2010-12-30 01:11:18929 NavigationEntry* entry;
930 if (page_id == -1)
931 entry = controller_.pending_entry();
932 else
933 entry = controller_.GetEntryWithPageID(GetSiteInstance(), page_id);
934 if (!entry || entry->url() != source_url)
935 return;
[email protected]e7d50892011-01-19 21:47:38936
[email protected]4850a7f2011-03-08 23:36:59937 // Notify observers about the provisional change in the main frame URL.
938 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
[email protected]eacb080b2011-05-22 19:40:26939 ProvisionalChangeToMainFrameUrl(target_url,
940 has_opener_set));
[email protected]724159a2010-12-30 01:11:18941}
942
943void TabContents::OnDidFailProvisionalLoadWithError(
944 int64 frame_id,
945 bool is_main_frame,
946 int error_code,
947 const GURL& url,
948 bool showing_repost_interstitial) {
949 VLOG(1) << "Failed Provisional Load: " << url.possibly_invalid_spec()
950 << ", error_code: " << error_code
951 << " is_main_frame: " << is_main_frame
952 << " showing_repost_interstitial: " << showing_repost_interstitial
953 << " frame_id: " << frame_id;
954 GURL validated_url(url);
955 render_view_host()->FilterURL(ChildProcessSecurityPolicy::GetInstance(),
956 GetRenderProcessHost()->id(), &validated_url);
957
958 if (net::ERR_ABORTED == error_code) {
959 // EVIL HACK ALERT! Ignore failed loads when we're showing interstitials.
960 // This means that the interstitial won't be torn down properly, which is
961 // bad. But if we have an interstitial, go back to another tab type, and
962 // then load the same interstitial again, we could end up getting the first
963 // interstitial's "failed" message (as a result of the cancel) when we're on
964 // the second one.
965 //
966 // We can't tell this apart, so we think we're tearing down the current page
967 // which will cause a crash later one. There is also some code in
968 // RenderViewHostManager::RendererAbortedProvisionalLoad that is commented
969 // out because of this problem.
970 //
971 // http://code.google.com/p/chromium/issues/detail?id=2855
972 // Because this will not tear down the interstitial properly, if "back" is
973 // back to another tab type, the interstitial will still be somewhat alive
974 // in the previous tab type. If you navigate somewhere that activates the
975 // tab with the interstitial again, you'll see a flash before the new load
976 // commits of the interstitial page.
977 if (showing_interstitial_page()) {
978 LOG(WARNING) << "Discarding message during interstitial.";
979 return;
980 }
981
[email protected]02102f82011-06-13 20:37:02982 // Discard our pending entry if the load canceled (e.g. if we decided to
983 // download the file instead of load it). We do not verify that the URL
984 // being canceled matches the pending entry's URL because they will not
985 // match if a redirect occurred (in which case we do not want to leave a
986 // stale redirect URL showing). This means that we also cancel the pending
987 // entry if the user started a new navigation. As a result, the navigation
988 // controller may not remember that a load is in progress, but the
989 // navigation will still commit even if there is no pending entry.
[email protected]724159a2010-12-30 01:11:18990 NavigationEntry* pending_entry = controller_.pending_entry();
[email protected]02102f82011-06-13 20:37:02991 if (pending_entry)
[email protected]c95fa8b2011-04-28 20:26:16992 DidCancelLoading();
[email protected]724159a2010-12-30 01:11:18993
994 render_manager_.RendererAbortedProvisionalLoad(render_view_host());
995 }
996
997 // Send out a notification that we failed a provisional load with an error.
998 ProvisionalLoadDetails details(
999 is_main_frame, controller_.IsURLInPageNavigation(validated_url),
[email protected]b9e8ea62011-03-04 06:29:091000 validated_url, std::string(), false, frame_id);
[email protected]724159a2010-12-30 01:11:181001 details.set_error_code(error_code);
1002
1003 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271004 content::NOTIFICATION_FAIL_PROVISIONAL_LOAD_WITH_ERROR,
[email protected]724159a2010-12-30 01:11:181005 Source<NavigationController>(&controller_),
1006 Details<ProvisionalLoadDetails>(&details));
[email protected]0d60f0192011-04-14 12:40:101007
1008 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
1009 DidFailProvisionalLoad(frame_id, is_main_frame,
1010 validated_url, error_code));
[email protected]724159a2010-12-30 01:11:181011}
1012
1013void TabContents::OnDidLoadResourceFromMemoryCache(
1014 const GURL& url,
[email protected]70435962011-08-02 20:13:281015 const std::string& security_info,
1016 const std::string& http_method,
1017 ResourceType::Type resource_type) {
[email protected]28620862011-03-22 23:07:191018 base::StatsCounter cache("WebKit.CacheHit");
[email protected]724159a2010-12-30 01:11:181019 cache.Increment();
1020
1021 // Send out a notification that we loaded a resource from our memory cache.
1022 int cert_id = 0, cert_status = 0, security_bits = -1, connection_status = 0;
1023 SSLManager::DeserializeSecurityInfo(security_info,
1024 &cert_id, &cert_status,
1025 &security_bits,
1026 &connection_status);
[email protected]7a8c55e2011-02-15 08:21:161027 LoadFromMemoryCacheDetails details(url, GetRenderProcessHost()->id(),
1028 cert_id, cert_status);
[email protected]724159a2010-12-30 01:11:181029
1030 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271031 content::NOTIFICATION_LOAD_FROM_MEMORY_CACHE,
[email protected]724159a2010-12-30 01:11:181032 Source<NavigationController>(&controller_),
1033 Details<LoadFromMemoryCacheDetails>(&details));
1034}
1035
1036void TabContents::OnDidDisplayInsecureContent() {
[email protected]221a5d92011-04-28 18:23:161037 UserMetrics::RecordAction(UserMetricsAction("SSL.DisplayedInsecureContent"));
[email protected]724159a2010-12-30 01:11:181038 displayed_insecure_content_ = true;
[email protected]9678d8f2011-08-05 04:05:071039 SSLManager::NotifySSLInternalStateChanged(&controller());
[email protected]724159a2010-12-30 01:11:181040}
1041
1042void TabContents::OnDidRunInsecureContent(
[email protected]92771112011-01-20 00:13:021043 const std::string& security_origin, const GURL& target_url) {
[email protected]9941cf52011-02-08 16:17:231044 LOG(INFO) << security_origin << " ran insecure content from "
1045 << target_url.possibly_invalid_spec();
[email protected]221a5d92011-04-28 18:23:161046 UserMetrics::RecordAction(UserMetricsAction("SSL.RanInsecureContent"));
[email protected]ca406032011-07-19 21:53:051047 if (EndsWith(security_origin, kDotGoogleDotCom, false)) {
1048 UserMetrics::RecordAction(
1049 UserMetricsAction("SSL.RanInsecureContentGoogle"));
1050 }
[email protected]724159a2010-12-30 01:11:181051 controller_.ssl_manager()->DidRunInsecureContent(security_origin);
[email protected]f2b58c4d2011-06-01 23:29:411052 displayed_insecure_content_ = true;
[email protected]9678d8f2011-08-05 04:05:071053 SSLManager::NotifySSLInternalStateChanged(&controller());
[email protected]724159a2010-12-30 01:11:181054}
1055
1056void TabContents::OnDocumentLoadedInFrame(int64 frame_id) {
1057 controller_.DocumentLoadedInFrame();
[email protected]0d60f0192011-04-14 12:40:101058 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
1059 DocumentLoadedInFrame(frame_id));
[email protected]724159a2010-12-30 01:11:181060}
1061
1062void TabContents::OnDidFinishLoad(int64 frame_id) {
[email protected]0d60f0192011-04-14 12:40:101063 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
1064 DidFinishLoad(frame_id));
[email protected]724159a2010-12-30 01:11:181065}
1066
[email protected]c8f73ab2011-01-22 00:05:171067void TabContents::OnUpdateContentRestrictions(int restrictions) {
[email protected]c40d6232011-03-25 00:16:211068 SetContentRestrictions(restrictions);
[email protected]c8f73ab2011-01-22 00:05:171069}
1070
[email protected]216813952011-05-19 22:21:261071void TabContents::OnGoToEntryAtOffset(int offset) {
1072 if (!delegate_ || delegate_->OnGoToEntryOffset(offset)) {
1073 NavigationEntry* entry = controller_.GetEntryAtOffset(offset);
1074 if (!entry)
1075 return;
1076 // Note that we don't call NavigationController::GotToOffset() as we don't
1077 // want to create a pending navigation entry (it might end up lingering
1078 // http://crbug.com/51680).
1079 entry->set_transition_type(entry->transition_type() |
1080 PageTransition::FORWARD_BACK);
1081 NavigateToEntry(*entry, NavigationController::NO_RELOAD);
[email protected]a13cc362011-07-28 21:29:571082
1083 // If the entry is being restored and doesn't have a SiteInstance yet, fill
1084 // it in now that we know. This allows us to find the entry when it commits.
1085 if (!entry->site_instance() &&
1086 entry->restore_type() != NavigationEntry::RESTORE_NONE) {
[email protected]77362eb2011-08-01 17:18:381087 entry->set_site_instance(GetPendingSiteInstance());
[email protected]a13cc362011-07-28 21:29:571088 }
[email protected]216813952011-05-19 22:21:261089 }
1090}
1091
1092void TabContents::OnUpdateZoomLimits(int minimum_percent,
1093 int maximum_percent,
1094 bool remember) {
1095 minimum_zoom_percent_ = minimum_percent;
1096 maximum_zoom_percent_ = maximum_percent;
1097 temporary_zoom_settings_ = !remember;
1098}
1099
1100void TabContents::OnFocusedNodeChanged(bool is_editable_node) {
1101 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271102 content::NOTIFICATION_FOCUS_CHANGED_IN_PAGE,
[email protected]216813952011-05-19 22:21:261103 Source<TabContents>(this),
1104 Details<const bool>(&is_editable_node));
1105}
1106
[email protected]3a29a6e2011-08-24 18:26:211107void TabContents::OnEnumerateDirectory(int request_id,
1108 const FilePath& path) {
1109 delegate()->EnumerateDirectory(this, request_id, path);
1110}
1111
[email protected]7d189022011-08-25 22:54:201112void TabContents::OnJSOutOfMemory() {
1113 delegate()->JSOutOfMemory(this);
1114}
1115
1116void TabContents::OnRegisterProtocolHandler(const std::string& protocol,
1117 const GURL& url,
1118 const string16& title) {
1119 delegate()->RegisterProtocolHandler(this, protocol, url, title);
1120}
1121
1122void TabContents::OnRegisterIntentHandler(const string16& action,
1123 const string16& type,
1124 const string16& href,
1125 const string16& title) {
1126 delegate()->RegisterIntentHandler(this, action, type, href, title);
1127}
1128
1129void TabContents::OnWebIntentDispatch(const IPC::Message& message,
1130 const string16& action,
1131 const string16& type,
1132 const string16& data,
1133 int intent_id) {
1134 delegate()->WebIntentDispatch(this, message.routing_id(), action, type,
1135 data, intent_id);
1136}
1137
[email protected]96d185d2009-04-24 03:28:541138// Notifies the RenderWidgetHost instance about the fact that the page is
1139// loading, or done loading and calls the base implementation.
1140void TabContents::SetIsLoading(bool is_loading,
1141 LoadNotificationDetails* details) {
1142 if (is_loading == is_loading_)
1143 return;
1144
1145 if (!is_loading) {
[email protected]9c235f042011-08-10 22:28:211146 load_state_ = net::LoadStateWithParam(net::LOAD_STATE_IDLE, string16());
[email protected]96d185d2009-04-24 03:28:541147 load_state_host_.clear();
[email protected]094e5b22009-09-25 04:23:561148 upload_size_ = 0;
1149 upload_position_ = 0;
[email protected]96d185d2009-04-24 03:28:541150 }
1151
1152 render_manager_.SetIsLoading(is_loading);
1153
1154 is_loading_ = is_loading;
1155 waiting_for_response_ = is_loading;
1156
[email protected]6ebdc9b2010-09-27 16:55:571157 if (delegate_)
1158 delegate_->LoadingStateChanged(this);
[email protected]329581b2009-04-28 06:52:351159 NotifyNavigationStateChanged(INVALIDATE_LOAD);
[email protected]96d185d2009-04-24 03:28:541160
[email protected]432115822011-07-10 15:52:271161 int type = is_loading ? content::NOTIFICATION_LOAD_START :
1162 content::NOTIFICATION_LOAD_STOP;
[email protected]ce5c4502009-05-06 16:46:111163 NotificationDetails det = NotificationService::NoDetails();
[email protected]96d185d2009-04-24 03:28:541164 if (details)
1165 det = Details<LoadNotificationDetails>(details);
1166 NotificationService::current()->Notify(type,
1167 Source<NavigationController>(&controller_),
1168 det);
1169}
1170
[email protected]d0980792011-02-13 19:41:401171WebUI* TabContents::GetWebUIForCurrentState() {
[email protected]7ade2732011-02-10 00:13:581172 // When there is a pending navigation entry, we want to use the pending WebUI
[email protected]96d185d2009-04-24 03:28:541173 // that goes along with it to control the basic flags. For example, we want to
1174 // show the pending URL in the URL bar, so we want the display_url flag to
1175 // be from the pending entry.
1176 //
1177 // The confusion comes because there are multiple possibilities for the
1178 // initial load in a tab as a side effect of the way the RenderViewHostManager
1179 // works.
1180 //
[email protected]e0112912011-02-02 22:54:351181 // - For the very first tab the load looks "normal". The new tab Web UI is
[email protected]96d185d2009-04-24 03:28:541182 // the pending one, and we want it to apply here.
1183 //
1184 // - For subsequent new tabs, they'll get a new SiteInstance which will then
1185 // get switched to the one previously associated with the new tab pages.
[email protected]7ade2732011-02-10 00:13:581186 // This switching will cause the manager to commit the RVH/WebUI. So we'll
[email protected]e0112912011-02-02 22:54:351187 // have a committed Web UI in this case.
[email protected]96d185d2009-04-24 03:28:541188 //
1189 // This condition handles all of these cases:
1190 //
1191 // - First load in first tab: no committed nav entry + pending nav entry +
1192 // pending dom ui:
[email protected]e0112912011-02-02 22:54:351193 // -> Use pending Web UI if any.
[email protected]96d185d2009-04-24 03:28:541194 //
1195 // - First load in second tab: no committed nav entry + pending nav entry +
[email protected]e0112912011-02-02 22:54:351196 // no pending Web UI:
1197 // -> Use the committed Web UI if any.
[email protected]96d185d2009-04-24 03:28:541198 //
1199 // - Second navigation in any tab: committed nav entry + pending nav entry:
[email protected]e0112912011-02-02 22:54:351200 // -> Use pending Web UI if any.
[email protected]96d185d2009-04-24 03:28:541201 //
1202 // - Normal state with no load: committed nav entry + no pending nav entry:
[email protected]e0112912011-02-02 22:54:351203 // -> Use committed Web UI.
[email protected]96d185d2009-04-24 03:28:541204 if (controller_.pending_entry() &&
1205 (controller_.GetLastCommittedEntry() ||
[email protected]7ade2732011-02-10 00:13:581206 render_manager_.pending_web_ui()))
1207 return render_manager_.pending_web_ui();
1208 return render_manager_.web_ui();
[email protected]96d185d2009-04-24 03:28:541209}
[email protected]420ae012009-04-24 05:16:321210
[email protected]1fd1a502011-03-30 16:55:561211WebUI::TypeID TabContents::GetWebUITypeForCurrentState() {
[email protected]3d7474ff2011-07-27 17:47:371212 return content::WebUIFactory::Get()->GetWebUIType(browser_context(),
1213 GetURL());
[email protected]1fd1a502011-03-30 16:55:561214}
1215
[email protected]420ae012009-04-24 05:16:321216void TabContents::DidNavigateMainFramePostCommit(
[email protected]8286f51a2011-05-31 17:39:131217 const content::LoadCommittedDetails& details,
[email protected]420ae012009-04-24 05:16:321218 const ViewHostMsg_FrameNavigate_Params& params) {
[email protected]1fd1a502011-03-30 16:55:561219 if (opener_web_ui_type_ != WebUI::kNoWebUI) {
[email protected]80a8fad2011-01-29 04:02:381220 // If this is a window.open navigation, use the same WebUI as the renderer
[email protected]c2e74fe82009-09-03 17:57:441221 // that opened the window, as long as both renderers have the same
1222 // privileges.
[email protected]1fd1a502011-03-30 16:55:561223 if (delegate_ && opener_web_ui_type_ == GetWebUITypeForCurrentState()) {
1224 WebUI* web_ui = content::GetContentClient()->browser()->
1225 GetWebUIFactory()->CreateWebUIForURL(this, GetURL());
[email protected]7ade2732011-02-10 00:13:581226 // web_ui might be NULL if the URL refers to a non-existent extension.
1227 if (web_ui) {
[email protected]d0980792011-02-13 19:41:401228 render_manager_.SetWebUIPostCommit(web_ui);
[email protected]7ade2732011-02-10 00:13:581229 web_ui->RenderViewCreated(render_view_host());
[email protected]c2e74fe82009-09-03 17:57:441230 }
1231 }
[email protected]1fd1a502011-03-30 16:55:561232 opener_web_ui_type_ = WebUI::kNoWebUI;
[email protected]c2e74fe82009-09-03 17:57:441233 }
1234
[email protected]4e697b042011-07-08 06:44:561235 if (details.is_navigation_to_different_page()) {
[email protected]420ae012009-04-24 05:16:321236 // Clear the status bubble. This is a workaround for a bug where WebKit
1237 // doesn't let us know that the cursor left an element during a
1238 // transition (this is also why the mouse cursor remains as a hand after
1239 // clicking on a link); see bugs 1184641 and 980803. We don't want to
1240 // clear the bubble when a user navigates to a named anchor in the same
1241 // page.
1242 UpdateTargetURL(details.entry->page_id(), GURL());
[email protected]420ae012009-04-24 05:16:321243 }
1244
[email protected]a6e82fc2010-02-24 22:28:141245 if (!details.is_in_page) {
[email protected]f17a0ee2010-05-17 17:38:471246 // Once the main frame is navigated, we're no longer considered to have
1247 // displayed insecure content.
1248 displayed_insecure_content_ = false;
[email protected]aece2c7f2009-08-27 20:43:171249 }
[email protected]ce5c4502009-05-06 16:46:111250
[email protected]8cc8d492010-02-02 10:40:491251 // Close constrained windows if necessary.
1252 if (!net::RegistryControlledDomainService::SameDomainOrHost(
1253 details.previous_url, details.entry->url()))
1254 CloseConstrainedWindows();
1255
[email protected]3c9e1872010-11-18 16:17:491256 // Notify observers about navigation.
[email protected]585b30362011-01-28 02:30:171257 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
[email protected]3c9e1872010-11-18 16:17:491258 DidNavigateMainFramePostCommit(details, params));
[email protected]420ae012009-04-24 05:16:321259}
1260
1261void TabContents::DidNavigateAnyFramePostCommit(
1262 RenderViewHost* render_view_host,
[email protected]8286f51a2011-05-31 17:39:131263 const content::LoadCommittedDetails& details,
[email protected]420ae012009-04-24 05:16:321264 const ViewHostMsg_FrameNavigate_Params& params) {
[email protected]3ab9cb82011-06-03 18:02:071265 // If we navigate, reset JavaScript state. This does nothing to prevent
[email protected]420ae012009-04-24 05:16:321266 // a malicious script from spamming messages, since the script could just
1267 // reload the page to stop blocking.
[email protected]2e5b90c2011-08-16 21:11:551268 if (dialog_creator_) {
1269 dialog_creator_->ResetJavaScriptState(this);
1270 dialog_creator_ = NULL;
1271 }
[email protected]420ae012009-04-24 05:16:321272
[email protected]3c9e1872010-11-18 16:17:491273 // Notify observers about navigation.
[email protected]585b30362011-01-28 02:30:171274 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
[email protected]3c9e1872010-11-18 16:17:491275 DidNavigateAnyFramePostCommit(details, params));
[email protected]420ae012009-04-24 05:16:321276}
1277
[email protected]8cc8d492010-02-02 10:40:491278void TabContents::CloseConstrainedWindows() {
1279 // Clear out any constrained windows since we are leaving this page entirely.
[email protected]aed59602011-02-28 22:57:331280 // To ensure that we iterate over every element in child_windows_ we
1281 // need to use a copy of child_windows_. Otherwise if
1282 // window->CloseConstrainedWindow() modifies child_windows_ we could end up
1283 // skipping some elements.
1284 ConstrainedWindowList child_windows_copy(child_windows_);
1285 for (ConstrainedWindowList::iterator it = child_windows_copy.begin();
1286 it != child_windows_copy.end(); ++it) {
1287 ConstrainedWindow* window = *it;
[email protected]fa1cf0b82010-01-15 21:49:441288 if (window) {
[email protected]420ae012009-04-24 05:16:321289 window->CloseConstrainedWindow();
[email protected]fa1cf0b82010-01-15 21:49:441290 BlockTabContent(false);
1291 }
[email protected]420ae012009-04-24 05:16:321292 }
1293}
1294
[email protected]420ae012009-04-24 05:16:321295void TabContents::UpdateMaxPageIDIfNecessary(SiteInstance* site_instance,
1296 RenderViewHost* rvh) {
1297 // If we are creating a RVH for a restored controller, then we might
1298 // have more page IDs than the SiteInstance's current max page ID. We must
1299 // make sure that the max page ID is larger than any restored page ID.
1300 // Note that it is ok for conflicting page IDs to exist in another tab
1301 // (i.e., NavigationController), but if any page ID is larger than the max,
1302 // the back/forward list will get confused.
1303 int max_restored_page_id = controller_.max_restored_page_id();
1304 if (max_restored_page_id > 0) {
1305 int curr_max_page_id = site_instance->max_page_id();
1306 if (max_restored_page_id > curr_max_page_id) {
1307 // Need to update the site instance immediately.
1308 site_instance->UpdateMaxPageID(max_restored_page_id);
1309
1310 // Also tell the renderer to update its internal representation. We
1311 // need to reserve enough IDs to make all restored page IDs less than
1312 // the max.
1313 if (curr_max_page_id < 0)
1314 curr_max_page_id = 0;
[email protected]216813952011-05-19 22:21:261315 rvh->Send(new ViewMsg_ReservePageIDRange(
1316 rvh->routing_id(), max_restored_page_id - curr_max_page_id));
[email protected]420ae012009-04-24 05:16:321317 }
1318 }
1319}
1320
[email protected]6b2f7a82011-04-25 19:30:511321bool TabContents::UpdateTitleForEntry(NavigationEntry* entry,
[email protected]acafd272011-07-26 17:35:571322 const string16& title) {
[email protected]420ae012009-04-24 05:16:321323 // For file URLs without a title, use the pathname instead. In the case of a
1324 // synthesized title, we don't want the update to count toward the "one set
1325 // per page of the title to history."
[email protected]6b2f7a82011-04-25 19:30:511326 string16 final_title;
[email protected]420ae012009-04-24 05:16:321327 bool explicit_set;
[email protected]987fc3a2011-05-26 14:18:091328 if (entry && entry->url().SchemeIsFile() && title.empty()) {
[email protected]6b2f7a82011-04-25 19:30:511329 final_title = UTF8ToUTF16(entry->url().ExtractFileName());
[email protected]420ae012009-04-24 05:16:321330 explicit_set = false; // Don't count synthetic titles toward the set limit.
1331 } else {
[email protected]acafd272011-07-26 17:35:571332 TrimWhitespace(title, TRIM_ALL, &final_title);
[email protected]420ae012009-04-24 05:16:321333 explicit_set = true;
1334 }
1335
[email protected]987fc3a2011-05-26 14:18:091336 // If a page is created via window.open and never navigated,
1337 // there will be no navigation entry. In this situation,
1338 // |page_title_when_no_navigaiton_entry_| will be used for page title.
1339 if (entry) {
1340 if (final_title == entry->title())
1341 return false; // Nothing changed, don't bother.
[email protected]420ae012009-04-24 05:16:321342
[email protected]987fc3a2011-05-26 14:18:091343 entry->set_title(final_title);
1344 } else {
1345 if (page_title_when_no_navigation_entry_ == final_title)
1346 return false; // Nothing changed, don't bother.
1347
1348 page_title_when_no_navigation_entry_ = final_title;
1349 }
[email protected]420ae012009-04-24 05:16:321350
[email protected]420ae012009-04-24 05:16:321351 // Lastly, set the title for the view.
[email protected]6b2f7a82011-04-25 19:30:511352 view_->SetPageTitle(UTF16ToWideHack(final_title));
[email protected]420ae012009-04-24 05:16:321353
[email protected]105bb0f2011-05-24 17:12:141354 TitleUpdatedDetails details(entry, explicit_set);
1355
[email protected]cbc0e1b2010-04-12 18:33:041356 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271357 content::NOTIFICATION_TAB_CONTENTS_TITLE_UPDATED,
[email protected]cbc0e1b2010-04-12 18:33:041358 Source<TabContents>(this),
[email protected]105bb0f2011-05-24 17:12:141359 Details<TitleUpdatedDetails>(&details));
[email protected]cbc0e1b2010-04-12 18:33:041360
[email protected]420ae012009-04-24 05:16:321361 return true;
1362}
1363
1364void TabContents::NotifySwapped() {
1365 // After sending out a swap notification, we need to send a disconnect
1366 // notification so that clients that pick up a pointer to |this| can NULL the
1367 // pointer. See Bug 1230284.
1368 notify_disconnection_ = true;
1369 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271370 content::NOTIFICATION_TAB_CONTENTS_SWAPPED,
[email protected]d82ed61e2009-06-16 02:46:221371 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:321372 NotificationService::NoDetails());
1373}
1374
1375void TabContents::NotifyConnected() {
1376 notify_disconnection_ = true;
1377 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271378 content::NOTIFICATION_TAB_CONTENTS_CONNECTED,
[email protected]d82ed61e2009-06-16 02:46:221379 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:321380 NotificationService::NoDetails());
1381}
1382
1383void TabContents::NotifyDisconnected() {
1384 if (!notify_disconnection_)
1385 return;
1386
1387 notify_disconnection_ = false;
1388 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271389 content::NOTIFICATION_TAB_CONTENTS_DISCONNECTED,
[email protected]d82ed61e2009-06-16 02:46:221390 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:321391 NotificationService::NoDetails());
1392}
1393
[email protected]8d3347f2009-07-09 22:00:211394RenderViewHostDelegate::View* TabContents::GetViewDelegate() {
[email protected]420ae012009-04-24 05:16:321395 return view_.get();
1396}
1397
[email protected]8d3347f2009-07-09 22:00:211398RenderViewHostDelegate::RendererManagement*
1399TabContents::GetRendererManagementDelegate() {
1400 return &render_manager_;
1401}
1402
[email protected]3d7474ff2011-07-27 17:47:371403RendererPreferences TabContents::GetRendererPrefs(
1404 content::BrowserContext* browser_context) const {
[email protected]840b1512009-07-21 16:53:461405 return renderer_preferences_;
[email protected]80d96fa2009-06-10 22:34:511406}
1407
[email protected]57c6a652009-05-04 07:58:341408TabContents* TabContents::GetAsTabContents() {
1409 return this;
1410}
1411
[email protected]7b291f92009-08-14 05:43:531412ViewType::Type TabContents::GetRenderViewType() const {
1413 return ViewType::TAB_CONTENTS;
1414}
1415
[email protected]420ae012009-04-24 05:16:321416void TabContents::RenderViewCreated(RenderViewHost* render_view_host) {
[email protected]2e4633c2009-07-09 16:58:061417 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271418 content::NOTIFICATION_RENDER_VIEW_HOST_CREATED_FOR_TAB,
[email protected]2e4633c2009-07-09 16:58:061419 Source<TabContents>(this),
1420 Details<RenderViewHost>(render_view_host));
[email protected]420ae012009-04-24 05:16:321421 NavigationEntry* entry = controller_.GetActiveEntry();
1422 if (!entry)
1423 return;
1424
1425 // When we're creating views, we're still doing initial setup, so we always
[email protected]e0112912011-02-02 22:54:351426 // use the pending Web UI rather than any possibly existing committed one.
[email protected]1fd1a502011-03-30 16:55:561427 if (render_manager_.pending_web_ui())
[email protected]7ade2732011-02-10 00:13:581428 render_manager_.pending_web_ui()->RenderViewCreated(render_view_host);
[email protected]420ae012009-04-24 05:16:321429
1430 if (entry->IsViewSourceMode()) {
1431 // Put the renderer in view source mode.
1432 render_view_host->Send(
1433 new ViewMsg_EnableViewSourceMode(render_view_host->routing_id()));
1434 }
[email protected]0666aef2009-05-13 19:48:081435
1436 view()->RenderViewCreated(render_view_host);
[email protected]11f764bb2011-05-25 19:45:261437
1438 FOR_EACH_OBSERVER(
1439 TabContentsObserver, observers_, RenderViewCreated(render_view_host));
[email protected]420ae012009-04-24 05:16:321440}
1441
1442void TabContents::RenderViewReady(RenderViewHost* rvh) {
1443 if (rvh != render_view_host()) {
1444 // Don't notify the world, since this came from a renderer in the
1445 // background.
1446 return;
1447 }
1448
1449 NotifyConnected();
[email protected]9d8a4642009-07-29 17:25:301450 bool was_crashed = is_crashed();
[email protected]443b80e2010-12-14 00:42:231451 SetIsCrashed(base::TERMINATION_STATUS_STILL_RUNNING, 0);
[email protected]9d8a4642009-07-29 17:25:301452
1453 // Restore the focus to the tab (otherwise the focus will be on the top
1454 // window).
[email protected]484ae5912010-09-29 19:16:141455 if (was_crashed && !FocusLocationBarByDefault() &&
1456 (!delegate_ || delegate_->ShouldFocusPageAfterCrash())) {
[email protected]9d8a4642009-07-29 17:25:301457 Focus();
[email protected]484ae5912010-09-29 19:16:141458 }
[email protected]420ae012009-04-24 05:16:321459}
1460
[email protected]443b80e2010-12-14 00:42:231461void TabContents::RenderViewGone(RenderViewHost* rvh,
1462 base::TerminationStatus status,
1463 int error_code) {
[email protected]420ae012009-04-24 05:16:321464 if (rvh != render_view_host()) {
1465 // The pending page's RenderViewHost is gone.
1466 return;
1467 }
1468
1469 SetIsLoading(false, NULL);
1470 NotifyDisconnected();
[email protected]443b80e2010-12-14 00:42:231471 SetIsCrashed(status, error_code);
[email protected]420ae012009-04-24 05:16:321472
[email protected]55452902011-06-01 21:57:471473 FOR_EACH_OBSERVER(TabContentsObserver, observers_, RenderViewGone());
[email protected]420ae012009-04-24 05:16:321474}
1475
[email protected]2e4633c2009-07-09 16:58:061476void TabContents::RenderViewDeleted(RenderViewHost* rvh) {
[email protected]fb7b79f72009-11-06 18:00:481477 render_manager_.RenderViewDeleted(rvh);
[email protected]2e4633c2009-07-09 16:58:061478}
1479
[email protected]420ae012009-04-24 05:16:321480void TabContents::DidNavigate(RenderViewHost* rvh,
1481 const ViewHostMsg_FrameNavigate_Params& params) {
[email protected]09b29342011-06-24 19:18:481482 if (PageTransition::IsMainFrame(params.transition))
[email protected]420ae012009-04-24 05:16:321483 render_manager_.DidNavigateMainFrame(rvh);
[email protected]8030f012009-09-25 18:09:371484
[email protected]420ae012009-04-24 05:16:321485 // Update the site of the SiteInstance if it doesn't have one yet.
1486 if (!GetSiteInstance()->has_site())
1487 GetSiteInstance()->SetSite(params.url);
1488
1489 // Need to update MIME type here because it's referred to in
1490 // UpdateNavigationCommands() called by RendererDidNavigate() to
1491 // determine whether or not to enable the encoding menu.
1492 // It's updated only for the main frame. For a subframe,
1493 // RenderView::UpdateURL does not set params.contents_mime_type.
1494 // (see http://code.google.com/p/chromium/issues/detail?id=2929 )
1495 // TODO(jungshik): Add a test for the encoding menu to avoid
1496 // regressing it again.
1497 if (PageTransition::IsMainFrame(params.transition))
1498 contents_mime_type_ = params.contents_mime_type;
1499
[email protected]8286f51a2011-05-31 17:39:131500 content::LoadCommittedDetails details;
[email protected]93f230e02011-06-01 14:40:001501 bool did_navigate = controller_.RendererDidNavigate(params, &details);
[email protected]3e90d4a2009-07-03 17:38:391502
[email protected]a9c0bfe2010-09-17 08:35:221503 // Send notification about committed provisional loads. This notification is
1504 // different from the NAV_ENTRY_COMMITTED notification which doesn't include
1505 // the actual URL navigated to and isn't sent for AUTO_SUBFRAME navigations.
1506 if (details.type != NavigationType::NAV_IGNORE) {
[email protected]dabb0d12010-10-05 12:50:071507 // For AUTO_SUBFRAME navigations, an event for the main frame is generated
1508 // that is not recorded in the navigation history. For the purpose of
1509 // tracking navigation events, we treat this event as a sub frame navigation
1510 // event.
1511 bool is_main_frame = did_navigate ? details.is_main_frame : false;
[email protected]0d60f0192011-04-14 12:40:101512 PageTransition::Type transition_type = params.transition;
[email protected]a9c0bfe2010-09-17 08:35:221513 // Whether or not a page transition was triggered by going backward or
1514 // forward in the history is only stored in the navigation controller's
1515 // entry list.
1516 if (did_navigate &&
1517 (controller_.GetActiveEntry()->transition_type() &
1518 PageTransition::FORWARD_BACK)) {
[email protected]0d60f0192011-04-14 12:40:101519 transition_type = params.transition | PageTransition::FORWARD_BACK;
[email protected]a9c0bfe2010-09-17 08:35:221520 }
[email protected]0d60f0192011-04-14 12:40:101521 // Notify observers about the commit of the provisional load.
1522 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
1523 DidCommitProvisionalLoadForFrame(params.frame_id,
1524 is_main_frame, params.url, transition_type));
[email protected]a9c0bfe2010-09-17 08:35:221525 }
1526
[email protected]76543b92009-08-31 17:27:451527 if (!did_navigate)
1528 return; // No navigation happened.
[email protected]420ae012009-04-24 05:16:321529
1530 // DO NOT ADD MORE STUFF TO THIS FUNCTION! Your component should either listen
1531 // for the appropriate notification (best) or you can add it to
1532 // DidNavigateMainFramePostCommit / DidNavigateAnyFramePostCommit (only if
1533 // necessary, please).
1534
1535 // Run post-commit tasks.
[email protected]93f230e02011-06-01 14:40:001536 if (details.is_main_frame) {
[email protected]420ae012009-04-24 05:16:321537 DidNavigateMainFramePostCommit(details, params);
[email protected]09b29342011-06-24 19:18:481538 if (delegate())
1539 delegate()->DidNavigateMainFramePostCommit(this);
[email protected]93f230e02011-06-01 14:40:001540 }
[email protected]420ae012009-04-24 05:16:321541 DidNavigateAnyFramePostCommit(rvh, details, params);
1542}
1543
1544void TabContents::UpdateState(RenderViewHost* rvh,
1545 int32 page_id,
1546 const std::string& state) {
[email protected]992db4c2011-05-12 15:37:151547 // Ensure that this state update comes from either the active RVH or one of
1548 // the swapped out RVHs. We don't expect to hear from any other RVHs.
1549 DCHECK(rvh == render_view_host() || render_manager_.IsSwappedOut(rvh));
[email protected]420ae012009-04-24 05:16:321550
1551 // We must be prepared to handle state updates for any page, these occur
1552 // when the user is scrolling and entering form data, as well as when we're
1553 // leaving a page, in which case our state may have already been moved to
1554 // the next page. The navigation controller will look up the appropriate
1555 // NavigationEntry and update it when it is notified via the delegate.
1556
1557 int entry_index = controller_.GetEntryIndexWithPageID(
[email protected]992db4c2011-05-12 15:37:151558 rvh->site_instance(), page_id);
[email protected]420ae012009-04-24 05:16:321559 if (entry_index < 0)
1560 return;
1561 NavigationEntry* entry = controller_.GetEntryAtIndex(entry_index);
1562
1563 if (state == entry->content_state())
1564 return; // Nothing to update.
1565 entry->set_content_state(state);
1566 controller_.NotifyEntryChanged(entry, entry_index);
1567}
1568
1569void TabContents::UpdateTitle(RenderViewHost* rvh,
[email protected]acafd272011-07-26 17:35:571570 int32 page_id,
[email protected]a49e10b2011-08-01 23:57:461571 const string16& title,
1572 base::i18n::TextDirection title_direction) {
[email protected]420ae012009-04-24 05:16:321573 // If we have a title, that's a pretty good indication that we've started
1574 // getting useful data.
1575 SetNotWaitingForResponse();
1576
1577 DCHECK(rvh == render_view_host());
[email protected]371cc8b2010-09-01 16:03:481578 NavigationEntry* entry = controller_.GetEntryWithPageID(rvh->site_instance(),
[email protected]ec0b6c42010-08-26 03:16:581579 page_id);
[email protected]987fc3a2011-05-26 14:18:091580
[email protected]a49e10b2011-08-01 23:57:461581 // TODO(evan): make use of title_direction.
1582 // http://code.google.com/p/chromium/issues/detail?id=27094
[email protected]987fc3a2011-05-26 14:18:091583 if (!UpdateTitleForEntry(entry, title))
[email protected]420ae012009-04-24 05:16:321584 return;
1585
1586 // Broadcast notifications when the UI should be updated.
1587 if (entry == controller_.GetEntryAtOffset(0))
[email protected]f1cd5e82009-10-23 17:19:031588 NotifyNavigationStateChanged(INVALIDATE_TITLE);
[email protected]420ae012009-04-24 05:16:321589}
1590
[email protected]420ae012009-04-24 05:16:321591void TabContents::UpdateEncoding(RenderViewHost* render_view_host,
[email protected]41fc0322009-09-04 22:23:401592 const std::string& encoding) {
[email protected]420ae012009-04-24 05:16:321593 set_encoding(encoding);
1594}
1595
1596void TabContents::UpdateTargetURL(int32 page_id, const GURL& url) {
1597 if (delegate())
1598 delegate()->UpdateTargetURL(this, url);
1599}
1600
[email protected]420ae012009-04-24 05:16:321601void TabContents::Close(RenderViewHost* rvh) {
[email protected]07707302009-11-06 00:50:291602 // The UI may be in an event-tracking loop, such as between the
1603 // mouse-down and mouse-up in text selection or a button click.
1604 // Defer the close until after tracking is complete, so that we
1605 // don't free objects out from under the UI.
1606 // TODO(shess): This could probably be integrated with the
1607 // IsDoingDrag() test below. Punting for now because I need more
1608 // research to understand how this impacts platforms other than Mac.
1609 // TODO(shess): This could get more fine-grained. For instance,
1610 // closing a tab in another window while selecting text in the
1611 // current window's Omnibox should be just fine.
1612 if (view()->IsEventTracking()) {
1613 view()->CloseTabAfterEventTracking();
1614 return;
1615 }
1616
[email protected]24a4d1062009-07-10 23:10:421617 // If we close the tab while we're in the middle of a drag, we'll crash.
1618 // Instead, cancel the drag and close it as soon as the drag ends.
1619 if (view()->IsDoingDrag()) {
1620 view()->CancelDragAndCloseTab();
1621 return;
1622 }
1623
[email protected]420ae012009-04-24 05:16:321624 // Ignore this if it comes from a RenderViewHost that we aren't showing.
1625 if (delegate() && rvh == render_view_host())
1626 delegate()->CloseContents(this);
1627}
1628
1629void TabContents::RequestMove(const gfx::Rect& new_bounds) {
[email protected]b35b26b32011-05-05 20:35:141630 if (delegate() && delegate()->IsPopupOrPanel(this))
[email protected]420ae012009-04-24 05:16:321631 delegate()->MoveContents(this, new_bounds);
1632}
1633
[email protected]7ab1e7d62009-10-14 23:32:011634void TabContents::DidStartLoading() {
[email protected]420ae012009-04-24 05:16:321635 SetIsLoading(true, NULL);
[email protected]3c9e1872010-11-18 16:17:491636
[email protected]c40d6232011-03-25 00:16:211637 if (delegate() && content_restrictions_) {
1638 content_restrictions_ = 0;
[email protected]de4761b2010-12-07 21:05:211639 delegate()->ContentRestrictionsChanged(this);
[email protected]9e823662010-10-13 23:36:001640 }
[email protected]3c9e1872010-11-18 16:17:491641
1642 // Notify observers about navigation.
[email protected]585b30362011-01-28 02:30:171643 FOR_EACH_OBSERVER(TabContentsObserver, observers_, DidStartLoading());
[email protected]420ae012009-04-24 05:16:321644}
1645
[email protected]7ab1e7d62009-10-14 23:32:011646void TabContents::DidStopLoading() {
[email protected]420ae012009-04-24 05:16:321647 scoped_ptr<LoadNotificationDetails> details;
1648
1649 NavigationEntry* entry = controller_.GetActiveEntry();
1650 // An entry may not exist for a stop when loading an initial blank page or
1651 // if an iframe injected by script into a blank page finishes loading.
1652 if (entry) {
[email protected]420ae012009-04-24 05:16:321653 base::TimeDelta elapsed = base::TimeTicks::Now() - current_load_start_;
1654
1655 details.reset(new LoadNotificationDetails(
[email protected]ebe89e062009-08-13 23:16:541656 entry->virtual_url(),
[email protected]420ae012009-04-24 05:16:321657 entry->transition_type(),
1658 elapsed,
1659 &controller_,
1660 controller_.GetCurrentEntryIndex()));
1661 }
1662
[email protected]420ae012009-04-24 05:16:321663 SetIsLoading(false, details.get());
[email protected]3c9e1872010-11-18 16:17:491664
1665 // Notify observers about navigation.
[email protected]585b30362011-01-28 02:30:171666 FOR_EACH_OBSERVER(TabContentsObserver, observers_, DidStopLoading());
[email protected]420ae012009-04-24 05:16:321667}
1668
[email protected]c95fa8b2011-04-28 20:26:161669void TabContents::DidCancelLoading() {
1670 controller_.DiscardNonCommittedEntries();
1671
1672 // Update the URL display.
1673 NotifyNavigationStateChanged(TabContents::INVALIDATE_URL);
1674}
1675
[email protected]1a3c3cb2010-12-16 21:03:401676void TabContents::DidChangeLoadProgress(double progress) {
1677 if (delegate())
1678 delegate()->LoadProgressChanged(progress);
1679}
1680
[email protected]25497492010-09-11 15:15:081681void TabContents::DocumentOnLoadCompletedInMainFrame(
1682 RenderViewHost* render_view_host,
1683 int32 page_id) {
1684 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271685 content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME,
[email protected]25497492010-09-11 15:15:081686 Source<TabContents>(this),
1687 Details<int>(&page_id));
1688}
1689
[email protected]420ae012009-04-24 05:16:321690void TabContents::RequestOpenURL(const GURL& url, const GURL& referrer,
1691 WindowOpenDisposition disposition) {
[email protected]7ade2732011-02-10 00:13:581692 if (render_manager_.web_ui()) {
[email protected]e0112912011-02-02 22:54:351693 // When we're a Web UI, it will provide a page transition type for us (this
[email protected]420ae012009-04-24 05:16:321694 // is so the new tab page can specify AUTO_BOOKMARK for automatically
1695 // generated suggestions).
1696 //
[email protected]e0112912011-02-02 22:54:351697 // Note also that we hide the referrer for Web UI pages. We don't really
[email protected]60e448982009-05-06 04:21:161698 // want web sites to see a referrer of "chrome://blah" (and some
1699 // chrome: URLs might have search terms or other stuff we don't want to
[email protected]420ae012009-04-24 05:16:321700 // send to the site), so we send no referrer.
1701 OpenURL(url, GURL(), disposition,
[email protected]7ade2732011-02-10 00:13:581702 render_manager_.web_ui()->link_transition_type());
[email protected]420ae012009-04-24 05:16:321703 } else {
1704 OpenURL(url, referrer, disposition, PageTransition::LINK);
1705 }
1706}
1707
[email protected]420ae012009-04-24 05:16:321708void TabContents::RunJavaScriptMessage(
[email protected]992db4c2011-05-12 15:37:151709 const RenderViewHost* rvh,
[email protected]4f5ce842011-05-27 19:34:411710 const string16& message,
1711 const string16& default_prompt,
[email protected]420ae012009-04-24 05:16:321712 const GURL& frame_url,
1713 const int flags,
1714 IPC::Message* reply_msg,
1715 bool* did_suppress_message) {
[email protected]3ab9cb82011-06-03 18:02:071716 // Suppress JavaScript dialogs when requested. Also suppress messages when
1717 // showing an interstitial as it's shown over the previous page and we don't
1718 // want the hidden page's dialogs to interfere with the interstitial.
[email protected]8f55e802010-12-06 18:11:501719 bool suppress_this_message =
[email protected]992db4c2011-05-12 15:37:151720 rvh->is_swapped_out() ||
[email protected]8f55e802010-12-06 18:11:501721 showing_interstitial_page() ||
[email protected]3ab9cb82011-06-03 18:02:071722 !delegate_ ||
1723 delegate_->ShouldSuppressDialogs();
[email protected]420ae012009-04-24 05:16:321724
1725 if (!suppress_this_message) {
[email protected]b627d9a2011-06-28 14:06:341726 content::JavaScriptDialogCreator::TitleType title_type;
1727 string16 title;
1728
1729 if (!frame_url.has_host()) {
1730 title_type = content::JavaScriptDialogCreator::DIALOG_TITLE_NONE;
1731 } else {
1732 title_type = content::JavaScriptDialogCreator::DIALOG_TITLE_FORMATTED_URL;
1733 title = net::FormatUrl(
1734 frame_url.GetOrigin(),
1735 content::GetContentClient()->browser()->GetAcceptLangs(this));
1736 }
1737
[email protected]2e5b90c2011-08-16 21:11:551738 dialog_creator_ = delegate_->GetJavaScriptDialogCreator();
1739 dialog_creator_->RunJavaScriptDialog(this,
1740 title_type,
1741 title,
1742 flags,
1743 message,
1744 default_prompt,
1745 reply_msg,
1746 &suppress_this_message);
[email protected]420ae012009-04-24 05:16:321747 }
[email protected]3ab9cb82011-06-03 18:02:071748
1749 if (suppress_this_message) {
1750 // If we are suppressing messages, just reply as if the user immediately
1751 // pressed "Cancel".
1752 OnDialogClosed(reply_msg, false, string16());
1753 }
1754
1755 *did_suppress_message = suppress_this_message;
[email protected]420ae012009-04-24 05:16:321756}
1757
[email protected]992db4c2011-05-12 15:37:151758void TabContents::RunBeforeUnloadConfirm(const RenderViewHost* rvh,
[email protected]4f5ce842011-05-27 19:34:411759 const string16& message,
[email protected]420ae012009-04-24 05:16:321760 IPC::Message* reply_msg) {
[email protected]5aab5e22010-12-08 22:13:291761 if (delegate())
1762 delegate()->WillRunBeforeUnloadConfirm();
[email protected]3ab9cb82011-06-03 18:02:071763
1764 bool suppress_this_message =
1765 rvh->is_swapped_out() ||
1766 !delegate_ ||
1767 delegate_->ShouldSuppressDialogs();
[email protected]992db4c2011-05-12 15:37:151768 if (suppress_this_message) {
[email protected]4f5ce842011-05-27 19:34:411769 render_view_host()->JavaScriptDialogClosed(reply_msg, true, string16());
[email protected]8f55e802010-12-06 18:11:501770 return;
1771 }
[email protected]3ab9cb82011-06-03 18:02:071772
[email protected]7ab1e7d62009-10-14 23:32:011773 is_showing_before_unload_dialog_ = true;
[email protected]2e5b90c2011-08-16 21:11:551774 dialog_creator_ = delegate_->GetJavaScriptDialogCreator();
1775 dialog_creator_->RunBeforeUnloadDialog(this,
1776 message,
1777 reply_msg);
[email protected]420ae012009-04-24 05:16:321778}
1779
[email protected]420ae012009-04-24 05:16:321780WebPreferences TabContents::GetWebkitPrefs() {
[email protected]447021c2010-09-08 21:29:081781 WebPreferences web_prefs =
[email protected]181a95ee2011-07-12 19:26:361782 content::GetContentClient()->browser()->GetWebkitPrefs(
[email protected]3d7474ff2011-07-27 17:47:371783 render_view_host()->process()->browser_context(), false);
[email protected]447021c2010-09-08 21:29:081784
[email protected]b8299c12011-06-03 19:52:281785 // Force accelerated compositing and 2d canvas off for chrome:, about: and
[email protected]2ef216b2011-08-01 19:25:211786 // chrome-devtools: pages (unless it's specifically allowed).
1787 if ((GetURL().SchemeIs(chrome::kChromeDevToolsScheme) ||
[email protected]b8299c12011-06-03 19:52:281788 GetURL().SchemeIs(chrome::kChromeUIScheme) ||
[email protected]2ef216b2011-08-01 19:25:211789 GetURL().SchemeIs(chrome::kAboutScheme)) &&
1790 !web_prefs.allow_webui_compositing) {
[email protected]447021c2010-09-08 21:29:081791 web_prefs.accelerated_compositing_enabled = false;
[email protected]9beff752010-09-22 19:35:431792 web_prefs.accelerated_2d_canvas_enabled = false;
[email protected]447021c2010-09-08 21:29:081793 }
1794
[email protected]39553442010-09-09 00:01:551795#if defined(OS_MACOSX)
1796 // Disable accelerated compositing if IOSurface's are not supported,
1797 // as is the case in 10.5.
1798 if (!IOSurfaceSupport::Initialize())
1799 web_prefs.accelerated_compositing_enabled = false;
1800#endif
1801
[email protected]2f7830b32011-08-26 18:22:251802#if defined(TOUCH_UI)
1803 web_prefs.force_compositing_mode = true;
1804#endif
1805
[email protected]447021c2010-09-08 21:29:081806 return web_prefs;
[email protected]420ae012009-04-24 05:16:321807}
1808
[email protected]7d472472011-01-22 01:30:251809void TabContents::OnUserGesture() {
[email protected]ddb85052011-05-18 14:40:271810 // Notify observers.
1811 FOR_EACH_OBSERVER(TabContentsObserver, observers_, DidGetUserGesture());
[email protected]686493142011-07-15 21:47:221812
1813 ResourceDispatcherHost* rdh =
1814 content::GetContentClient()->browser()->GetResourceDispatcherHost();
1815 if (rdh) // NULL in unittests.
[email protected]8ec98cd22011-08-09 22:31:301816 rdh->OnUserGesture(this);
[email protected]7d472472011-01-22 01:30:251817}
1818
[email protected]fa1cf0b82010-01-15 21:49:441819void TabContents::OnIgnoredUIEvent() {
1820 if (constrained_window_count()) {
1821 ConstrainedWindow* window = *constrained_window_begin();
1822 window->FocusConstrainedWindow();
1823 }
1824}
1825
[email protected]420ae012009-04-24 05:16:321826void TabContents::RendererUnresponsive(RenderViewHost* rvh,
1827 bool is_during_unload) {
[email protected]992db4c2011-05-12 15:37:151828 // Don't show hung renderer dialog for a swapped out RVH.
1829 if (rvh != render_view_host())
1830 return;
1831
[email protected]e5fc1632011-08-08 07:51:531832 // Ignore renderer unresponsive event if debugger is attached to the tab
1833 // since the event may be a result of the renderer sitting on a breakpoint.
1834 // See http://crbug.com/65458
1835 DevToolsManager* devtools_manager = DevToolsManager::GetInstance();
1836 if (devtools_manager &&
1837 devtools_manager->GetDevToolsClientHostFor(rvh) != NULL)
1838 return;
1839
[email protected]420ae012009-04-24 05:16:321840 if (is_during_unload) {
1841 // Hang occurred while firing the beforeunload/unload handler.
1842 // Pretend the handler fired so tab closing continues as if it had.
[email protected]829e7612009-04-25 01:15:111843 rvh->set_sudden_termination_allowed(true);
[email protected]420ae012009-04-24 05:16:321844
1845 if (!render_manager_.ShouldCloseTabOnUnresponsiveRenderer())
1846 return;
1847
1848 // If the tab hangs in the beforeunload/unload handler there's really
1849 // nothing we can do to recover. Pretend the unload listeners have
1850 // all fired and close the tab. If the hang is in the beforeunload handler
1851 // then the user will not have the option of cancelling the close.
1852 Close(rvh);
1853 return;
1854 }
1855
[email protected]55452902011-06-01 21:57:471856 if (!render_view_host() || !render_view_host()->IsRenderViewLive())
1857 return;
1858
1859 if (delegate())
1860 delegate()->RendererUnresponsive(this);
[email protected]420ae012009-04-24 05:16:321861}
1862
1863void TabContents::RendererResponsive(RenderViewHost* render_view_host) {
[email protected]55452902011-06-01 21:57:471864 if (delegate())
1865 delegate()->RendererResponsive(this);
[email protected]420ae012009-04-24 05:16:321866}
1867
1868void TabContents::LoadStateChanged(const GURL& url,
[email protected]9c235f042011-08-10 22:28:211869 const net::LoadStateWithParam& load_state,
[email protected]094e5b22009-09-25 04:23:561870 uint64 upload_position,
1871 uint64 upload_size) {
[email protected]420ae012009-04-24 05:16:321872 load_state_ = load_state;
[email protected]094e5b22009-09-25 04:23:561873 upload_position_ = upload_position;
1874 upload_size_ = upload_size;
[email protected]04866c42011-05-03 20:03:501875 load_state_host_ = net::IDNToUnicode(url.host(),
[email protected]b5cca982011-05-26 04:42:081876 content::GetContentClient()->browser()->GetAcceptLangs(this));
[email protected]9c235f042011-08-10 22:28:211877 if (load_state_.state == net::LOAD_STATE_READING_RESPONSE)
[email protected]420ae012009-04-24 05:16:321878 SetNotWaitingForResponse();
[email protected]f5d978c2011-07-21 14:43:511879 if (IsLoading())
[email protected]c9cd2222009-05-06 05:16:501880 NotifyNavigationStateChanged(INVALIDATE_LOAD | INVALIDATE_TAB);
[email protected]420ae012009-04-24 05:16:321881}
1882
[email protected]7d472472011-01-22 01:30:251883void TabContents::WorkerCrashed() {
[email protected]0dd3a0ab2011-02-18 08:17:441884 if (delegate())
[email protected]55452902011-06-01 21:57:471885 delegate()->WorkerCrashed(this);
[email protected]7d472472011-01-22 01:30:251886}
1887
[email protected]420ae012009-04-24 05:16:321888void TabContents::BeforeUnloadFiredFromRenderManager(
1889 bool proceed,
1890 bool* proceed_to_fire_unload) {
1891 if (delegate())
1892 delegate()->BeforeUnloadFired(this, proceed, proceed_to_fire_unload);
1893}
1894
[email protected]3a3d47472010-07-15 21:03:541895void TabContents::DidStartLoadingFromRenderManager(
1896 RenderViewHost* render_view_host) {
1897 DidStartLoading();
1898}
1899
1900void TabContents::RenderViewGoneFromRenderManager(
1901 RenderViewHost* render_view_host) {
[email protected]443b80e2010-12-14 00:42:231902 DCHECK(crashed_status_ != base::TERMINATION_STATUS_STILL_RUNNING);
1903 RenderViewGone(render_view_host, crashed_status_, crashed_error_code_);
[email protected]3a3d47472010-07-15 21:03:541904}
1905
[email protected]420ae012009-04-24 05:16:321906void TabContents::UpdateRenderViewSizeForRenderManager() {
1907 // TODO(brettw) this is a hack. See TabContentsView::SizeContents.
[email protected]0323ee42010-02-17 22:03:261908 gfx::Size size = view_->GetContainerSize();
1909 // 0x0 isn't a valid window size (minimal window size is 1x1) but it may be
1910 // here during container initialization and normal window size will be set
1911 // later. In case of tab duplication this resizing to 0x0 prevents setting
1912 // normal size later so just ignore it.
1913 if (!size.IsEmpty())
1914 view_->SizeContents(size);
[email protected]420ae012009-04-24 05:16:321915}
1916
[email protected]3a3d47472010-07-15 21:03:541917void TabContents::NotifySwappedFromRenderManager() {
1918 NotifySwapped();
1919}
1920
1921NavigationController& TabContents::GetControllerForRenderManager() {
1922 return controller();
1923}
1924
[email protected]d0980792011-02-13 19:41:401925WebUI* TabContents::CreateWebUIForRenderManager(const GURL& url) {
[email protected]1fd1a502011-03-30 16:55:561926 return content::WebUIFactory::Get()->CreateWebUIForURL(this, url);
[email protected]420ae012009-04-24 05:16:321927}
1928
1929NavigationEntry*
1930TabContents::GetLastCommittedNavigationEntryForRenderManager() {
1931 return controller_.GetLastCommittedEntry();
1932}
1933
1934bool TabContents::CreateRenderViewForRenderManager(
1935 RenderViewHost* render_view_host) {
[email protected]420ae012009-04-24 05:16:321936 RenderWidgetHostView* rwh_view = view_->CreateViewForWidget(render_view_host);
[email protected]1a98a932009-11-17 00:12:521937
[email protected]420ae012009-04-24 05:16:321938 // Now that the RenderView has been created, we need to tell it its size.
1939 rwh_view->SetSize(view_->GetContainerSize());
1940
[email protected]a4127722011-04-27 23:13:521941 if (!render_view_host->CreateRenderView(string16()))
1942 return false;
1943
[email protected]f8233cc2011-05-31 20:24:501944#if defined(OS_LINUX)
1945 // Force a ViewMsg_Resize to be sent, needed to make plugins show up on
1946 // linux. See crbug.com/83941.
1947 if (RenderWidgetHost* render_widget_host = rwh_view->GetRenderWidgetHost())
1948 render_widget_host->WasResized();
1949#endif
1950
[email protected]420ae012009-04-24 05:16:321951 UpdateMaxPageIDIfNecessary(render_view_host->site_instance(),
1952 render_view_host);
1953 return true;
1954}
1955
[email protected]3ab9cb82011-06-03 18:02:071956void TabContents::OnDialogClosed(IPC::Message* reply_msg,
1957 bool success,
1958 const string16& user_input) {
[email protected]beb440c2009-11-06 04:08:541959 if (is_showing_before_unload_dialog_ && !success) {
1960 // If a beforeunload dialog is canceled, we need to stop the throbber from
1961 // spinning, since we forced it to start spinning in Navigate.
1962 DidStopLoading();
1963
1964 tab_close_start_time_ = base::TimeTicks();
1965 }
1966 is_showing_before_unload_dialog_ = false;
[email protected]4f5ce842011-05-27 19:34:411967 render_view_host()->JavaScriptDialogClosed(reply_msg,
1968 success,
[email protected]3ab9cb82011-06-03 18:02:071969 user_input);
[email protected]beb440c2009-11-06 04:08:541970}
1971
[email protected]3ab9cb82011-06-03 18:02:071972gfx::NativeWindow TabContents::GetDialogRootWindow() {
1973 return view_->GetTopLevelNativeWindow();
[email protected]beb440c2009-11-06 04:08:541974}
1975
[email protected]a1e97f02011-06-30 14:04:341976void TabContents::OnDialogShown() {
1977 Activate();
[email protected]3a3d47472010-07-15 21:03:541978}
1979
[email protected]41fc0322009-09-04 22:23:401980void TabContents::set_encoding(const std::string& encoding) {
[email protected]763ec4ca2011-04-29 15:48:121981 encoding_ = content::GetContentClient()->browser()->
1982 GetCanonicalEncodingNameByAliasName(encoding);
[email protected]aacd7cc2009-09-02 14:51:101983}
[email protected]f45d2a72010-03-08 23:28:351984
[email protected]33f74972010-12-08 16:40:361985void TabContents::SwapInRenderViewHost(RenderViewHost* rvh) {
1986 render_manager_.SwapInRenderViewHost(rvh);
1987}
1988
1989void TabContents::CreateViewAndSetSizeForRVH(RenderViewHost* rvh) {
1990 RenderWidgetHostView* rwh_view = view()->CreateViewForWidget(rvh);
1991 rwh_view->SetSize(view()->GetContainerSize());
1992}