blob: 79fba861be2f1e48c0237d0249565f5eb31c6706 [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,
[email protected]a2750082011-09-01 12:29:4668// it creates a pending_render_view_host_.
[email protected]420ae012009-04-24 05:16:3269// - The pending RVH is "suspended," so that no navigation messages are sent to
70// its renderer until the onbeforeunload JavaScript handler has a chance to
71// run in the current RVH.
72// - The pending RVH tells CrossSiteRequestManager (a thread-safe singleton)
73// that it has a pending cross-site request. ResourceDispatcherHost will
74// check for this when the response arrives.
75// - The current RVH runs its onbeforeunload handler. If it returns false, we
[email protected]a2750082011-09-01 12:29:4676// cancel all the pending logic. Otherwise we allow the pending RVH to send
77// the navigation request to its renderer.
78// - ResourceDispatcherHost receives a ResourceRequest on the IO thread for the
79// main resource load on the pending RVH. It checks CrossSiteRequestManager
80// to see that it is a cross-site request, and installs a
81// CrossSiteResourceHandler.
82// - When RDH receives a response, the BufferedResourceHandler determines
83// whether it is a download. If so, it sends a message to the new renderer
84// causing it to cancel the request, and the download proceeds. For now, the
85// pending RVH remains until the next DidNavigate event for this TabContents.
86// This isn't ideal, but it doesn't affect any functionality.
[email protected]420ae012009-04-24 05:16:3287// - After RDH receives a response and determines that it is safe and not a
88// download, it pauses the response to first run the old page's onunload
89// handler. It does this by asynchronously calling the OnCrossSiteResponse
[email protected]992db4c2011-05-12 15:37:1590// method of TabContents on the UI thread, which sends a SwapOut message
[email protected]420ae012009-04-24 05:16:3291// to the current RVH.
[email protected]992db4c2011-05-12 15:37:1592// - Once the onunload handler is finished, a SwapOut_ACK message is sent to
[email protected]420ae012009-04-24 05:16:3293// the ResourceDispatcherHost, who unpauses the response. Data is then sent
94// to the pending RVH.
95// - The pending renderer sends a FrameNavigate message that invokes the
96// DidNavigate method. This replaces the current RVH with the
[email protected]a2750082011-09-01 12:29:4697// pending RVH.
[email protected]992db4c2011-05-12 15:37:1598// - The previous renderer is kept swapped out in RenderViewHostManager in case
99// the user goes back. The process only stays live if another tab is using
100// it, but if so, the existing frame relationships will be maintained.
[email protected]420ae012009-04-24 05:16:32101
102namespace {
103
104// Amount of time we wait between when a key event is received and the renderer
105// is queried for its state and pushed to the NavigationEntry.
106const int kQueryStateDelay = 5000;
107
[email protected]6ebdc9b2010-09-27 16:55:57108const int kSyncWaitDelay = 40;
109
[email protected]ca406032011-07-19 21:53:05110static const char kDotGoogleDotCom[] = ".google.com";
111
[email protected]420ae012009-04-24 05:16:32112#if defined(OS_WIN)
113
114BOOL CALLBACK InvalidateWindow(HWND hwnd, LPARAM lparam) {
115 // Note: erase is required to properly paint some widgets borders. This can
116 // be seen with textfields.
117 InvalidateRect(hwnd, NULL, TRUE);
118 return TRUE;
119}
120#endif
121
[email protected]2c5569662011-03-22 20:45:02122ViewMsg_Navigate_Type::Value GetNavigationType(
[email protected]3d7474ff2011-07-27 17:47:37123 content::BrowserContext* browser_context, const NavigationEntry& entry,
[email protected]1ccb3568d2010-02-19 10:51:16124 NavigationController::ReloadType reload_type) {
125 switch (reload_type) {
126 case NavigationController::RELOAD:
[email protected]2c5569662011-03-22 20:45:02127 return ViewMsg_Navigate_Type::RELOAD;
[email protected]1ccb3568d2010-02-19 10:51:16128 case NavigationController::RELOAD_IGNORING_CACHE:
[email protected]2c5569662011-03-22 20:45:02129 return ViewMsg_Navigate_Type::RELOAD_IGNORING_CACHE;
[email protected]1ccb3568d2010-02-19 10:51:16130 case NavigationController::NO_RELOAD:
131 break; // Fall through to rest of function.
132 }
[email protected]5e369672009-11-03 23:48:30133
134 if (entry.restore_type() == NavigationEntry::RESTORE_LAST_SESSION &&
[email protected]3d7474ff2011-07-27 17:47:37135 browser_context->DidLastSessionExitCleanly())
[email protected]2c5569662011-03-22 20:45:02136 return ViewMsg_Navigate_Type::RESTORE;
[email protected]5e369672009-11-03 23:48:30137
[email protected]2c5569662011-03-22 20:45:02138 return ViewMsg_Navigate_Type::NORMAL;
[email protected]5e369672009-11-03 23:48:30139}
140
[email protected]876bc832010-09-07 16:29:54141void MakeNavigateParams(const NavigationEntry& entry,
142 const NavigationController& controller,
[email protected]6c6b02d2011-09-02 03:36:47143 TabContentsDelegate* delegate,
[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();
[email protected]6c6b02d2011-09-02 03:36:47157 params->extra_headers = entry.extra_headers();
158
159 if (delegate)
160 delegate->AddNavigationHeaders(params->url, &params->extra_headers);
[email protected]056de2d2009-06-26 16:41:34161}
162
[email protected]420ae012009-04-24 05:16:32163} // namespace
164
[email protected]f4f50ef2011-01-21 19:01:19165
166// TabContents ----------------------------------------------------------------
[email protected]420ae012009-04-24 05:16:32167
[email protected]3d7474ff2011-07-27 17:47:37168TabContents::TabContents(content::BrowserContext* browser_context,
[email protected]420ae012009-04-24 05:16:32169 SiteInstance* site_instance,
170 int routing_id,
[email protected]6ee12c42010-09-14 09:36:07171 const TabContents* base_tab_contents,
172 SessionStorageNamespace* session_storage_namespace)
[email protected]b680ad22009-04-15 23:19:42173 : delegate_(NULL),
[email protected]6ee12c42010-09-14 09:36:07174 ALLOW_THIS_IN_INITIALIZER_LIST(controller_(
[email protected]3d7474ff2011-07-27 17:47:37175 this, browser_context, session_storage_namespace)),
[email protected]66ba4932009-06-04 19:22:13176 ALLOW_THIS_IN_INITIALIZER_LIST(view_(
[email protected]74313b42011-08-24 16:51:32177 content::GetContentClient()->browser()->CreateTabContentsView(this))),
[email protected]d82ed61e2009-06-16 02:46:22178 ALLOW_THIS_IN_INITIALIZER_LIST(render_manager_(this, this)),
[email protected]d5f942ba2008-09-26 19:30:34179 is_loading_(false),
[email protected]443b80e2010-12-14 00:42:23180 crashed_status_(base::TERMINATION_STATUS_STILL_RUNNING),
181 crashed_error_code_(0),
[email protected]d5f942ba2008-09-26 19:30:34182 waiting_for_response_(false),
[email protected]d5f942ba2008-09-26 19:30:34183 max_page_id_(-1),
[email protected]9c235f042011-08-10 22:28:21184 load_state_(net::LOAD_STATE_IDLE, string16()),
[email protected]094e5b22009-09-25 04:23:56185 upload_size_(0),
186 upload_position_(0),
[email protected]f17a0ee2010-05-17 17:38:47187 displayed_insecure_content_(false),
[email protected]fdd61c62009-04-22 19:22:57188 capturing_contents_(false),
189 is_being_destroyed_(false),
190 notify_disconnection_(false),
[email protected]2e5b90c2011-08-16 21:11:55191 dialog_creator_(NULL),
[email protected]fdd61c62009-04-22 19:22:57192#if defined(OS_WIN)
193 message_box_active_(CreateEvent(NULL, TRUE, FALSE, NULL)),
194#endif
[email protected]7ab1e7d62009-10-14 23:32:01195 is_showing_before_unload_dialog_(false),
[email protected]1fd1a502011-03-30 16:55:56196 opener_web_ui_type_(WebUI::kNoWebUI),
[email protected]ebf40a72010-07-22 01:46:38197 closed_by_user_gesture_(false),
[email protected]b75b8292010-10-01 07:28:25198 minimum_zoom_percent_(
199 static_cast<int>(WebKit::WebView::minTextSizeMultiplier * 100)),
200 maximum_zoom_percent_(
201 static_cast<int>(WebKit::WebView::maxTextSizeMultiplier * 100)),
[email protected]9e823662010-10-13 23:36:00202 temporary_zoom_settings_(false),
[email protected]4850a7f2011-03-08 23:36:59203 content_restrictions_(0) {
[email protected]7ff431e2010-01-07 18:18:54204
[email protected]3d7474ff2011-07-27 17:47:37205 render_manager_.Init(browser_context, site_instance, routing_id);
[email protected]420ae012009-04-24 05:16:32206
[email protected]34ac70502009-09-25 17:07:23207 // We have the initial size of the view be based on the size of the passed in
208 // tab contents (normally a tab from the same window).
209 view_->CreateView(base_tab_contents ?
210 base_tab_contents->view()->GetContainerSize() : gfx::Size());
[email protected]332af7732009-03-11 13:21:35211}
initial.commit09911bf2008-07-26 23:55:29212
213TabContents::~TabContents() {
[email protected]420ae012009-04-24 05:16:32214 is_being_destroyed_ = true;
215
[email protected]3ab9cb82011-06-03 18:02:07216 // Clear out any JavaScript state.
[email protected]2e5b90c2011-08-16 21:11:55217 if (dialog_creator_)
218 dialog_creator_->ResetJavaScriptState(this);
[email protected]3ab9cb82011-06-03 18:02:07219
[email protected]420ae012009-04-24 05:16:32220 NotifyDisconnected();
[email protected]420ae012009-04-24 05:16:32221
[email protected]420ae012009-04-24 05:16:32222 // First cleanly close all child windows.
223 // TODO(mpcomplete): handle case if MaybeCloseChildWindows() already asked
224 // some of these to close. CloseWindows is async, so it might get called
225 // twice before it runs.
[email protected]8cc8d492010-02-02 10:40:49226 CloseConstrainedWindows();
[email protected]420ae012009-04-24 05:16:32227
[email protected]420ae012009-04-24 05:16:32228 // Notify any observer that have a reference on this tab contents.
229 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:27230 content::NOTIFICATION_TAB_CONTENTS_DESTROYED,
[email protected]420ae012009-04-24 05:16:32231 Source<TabContents>(this),
232 NotificationService::NoDetails());
233
234 // TODO(brettw) this should be moved to the view.
235#if defined(OS_WIN)
236 // If we still have a window handle, destroy it. GetNativeView can return
237 // NULL if this contents was part of a window that closed.
[email protected]50bd6452010-11-27 19:39:42238 if (GetNativeView()) {
239 RenderViewHost* host = render_view_host();
[email protected]b9a1fb42011-04-12 23:16:44240 if (host && host->view())
[email protected]50bd6452010-11-27 19:39:42241 host->view()->WillWmDestroy();
[email protected]50bd6452010-11-27 19:39:42242 }
[email protected]420ae012009-04-24 05:16:32243#endif
[email protected]7ab1e7d62009-10-14 23:32:01244
245 // OnCloseStarted isn't called in unit tests.
246 if (!tab_close_start_time_.is_null()) {
247 UMA_HISTOGRAM_TIMES("Tab.Close",
248 base::TimeTicks::Now() - tab_close_start_time_);
249 }
[email protected]b5a1d11c2011-02-17 03:09:42250
[email protected]07d490bc2011-03-07 17:05:26251 FOR_EACH_OBSERVER(TabContentsObserver, observers_, TabContentsDestroyed());
[email protected]232a5812011-03-04 22:42:08252
[email protected]1de2b8b2011-06-29 19:38:46253 set_delegate(NULL);
254}
255
256// TODO(cbentzel): Either remove the debugging code, or rename to SetDelegate.
257void TabContents::set_delegate(TabContentsDelegate* delegate) {
258 if (delegate == delegate_)
259 return;
260 if (delegate_)
261 delegate_->Detach(this);
262 delegate_ = delegate;
263 if (delegate_)
264 delegate_->Attach(this);
[email protected]b5a1d11c2011-02-17 03:09:42265}
266
[email protected]724159a2010-12-30 01:11:18267bool TabContents::OnMessageReceived(const IPC::Message& message) {
[email protected]f82d57b52011-04-27 19:13:17268 if (web_ui() && web_ui()->OnMessageReceived(message))
269 return true;
270
[email protected]585b30362011-01-28 02:30:17271 ObserverListBase<TabContentsObserver>::Iterator it(observers_);
272 TabContentsObserver* observer;
[email protected]0f180ee2011-01-21 18:23:10273 while ((observer = it.GetNext()) != NULL)
274 if (observer->OnMessageReceived(message))
[email protected]403415a2011-01-10 18:57:53275 return true;
[email protected]403415a2011-01-10 18:57:53276
[email protected]724159a2010-12-30 01:11:18277 bool handled = true;
278 bool message_is_ok = true;
279 IPC_BEGIN_MESSAGE_MAP_EX(TabContents, message, message_is_ok)
280 IPC_MESSAGE_HANDLER(ViewHostMsg_DidStartProvisionalLoadForFrame,
281 OnDidStartProvisionalLoadForFrame)
282 IPC_MESSAGE_HANDLER(ViewHostMsg_DidRedirectProvisionalLoad,
283 OnDidRedirectProvisionalLoad)
284 IPC_MESSAGE_HANDLER(ViewHostMsg_DidFailProvisionalLoadWithError,
285 OnDidFailProvisionalLoadWithError)
286 IPC_MESSAGE_HANDLER(ViewHostMsg_DidLoadResourceFromMemoryCache,
287 OnDidLoadResourceFromMemoryCache)
288 IPC_MESSAGE_HANDLER(ViewHostMsg_DidDisplayInsecureContent,
289 OnDidDisplayInsecureContent)
290 IPC_MESSAGE_HANDLER(ViewHostMsg_DidRunInsecureContent,
291 OnDidRunInsecureContent)
292 IPC_MESSAGE_HANDLER(ViewHostMsg_DocumentLoadedInFrame,
293 OnDocumentLoadedInFrame)
294 IPC_MESSAGE_HANDLER(ViewHostMsg_DidFinishLoad, OnDidFinishLoad)
[email protected]c8f73ab2011-01-22 00:05:17295 IPC_MESSAGE_HANDLER(ViewHostMsg_UpdateContentRestrictions,
296 OnUpdateContentRestrictions)
[email protected]7d472472011-01-22 01:30:25297 IPC_MESSAGE_HANDLER(ViewHostMsg_GoToEntryAtOffset, OnGoToEntryAtOffset)
[email protected]216813952011-05-19 22:21:26298 IPC_MESSAGE_HANDLER(ViewHostMsg_UpdateZoomLimits, OnUpdateZoomLimits)
299 IPC_MESSAGE_HANDLER(ViewHostMsg_FocusedNodeChanged, OnFocusedNodeChanged)
[email protected]c7dd2f62011-07-18 15:57:59300 IPC_MESSAGE_HANDLER(ViewHostMsg_SaveURLAs, OnSaveURL)
[email protected]3a29a6e2011-08-24 18:26:21301 IPC_MESSAGE_HANDLER(ViewHostMsg_EnumerateDirectory, OnEnumerateDirectory)
[email protected]7d189022011-08-25 22:54:20302 IPC_MESSAGE_HANDLER(ViewHostMsg_JSOutOfMemory, OnJSOutOfMemory)
303 IPC_MESSAGE_HANDLER(ViewHostMsg_RegisterProtocolHandler,
304 OnRegisterProtocolHandler)
305 IPC_MESSAGE_HANDLER(ViewHostMsg_RegisterIntentHandler,
306 OnRegisterIntentHandler)
307 IPC_MESSAGE_HANDLER(ViewHostMsg_WebIntentDispatch,
308 OnWebIntentDispatch)
[email protected]b888919c2011-09-02 00:32:16309 IPC_MESSAGE_HANDLER(ViewHostMsg_Find_Reply, OnFindReply)
[email protected]d952a052011-09-06 18:42:45310 IPC_MESSAGE_HANDLER(ViewHostMsg_CrashedPlugin, OnCrashedPlugin)
[email protected]7fc4bbb2011-09-08 21:23:10311 IPC_MESSAGE_HANDLER(ViewHostMsg_AppCacheAccessed, OnAppCacheAccessed)
[email protected]724159a2010-12-30 01:11:18312 IPC_MESSAGE_UNHANDLED(handled = false)
313 IPC_END_MESSAGE_MAP_EX()
314
315 if (!message_is_ok) {
316 UserMetrics::RecordAction(UserMetricsAction("BadMessageTerminate_RVD"));
317 GetRenderProcessHost()->ReceivedBadMessage();
318 }
319
320 return handled;
321}
322
[email protected]6c2e472f2011-08-24 23:26:18323void TabContents::RunFileChooser(
324 RenderViewHost* render_view_host,
325 const ViewHostMsg_RunFileChooser_Params& params) {
326 delegate()->RunFileChooser(this, params);
327}
328
[email protected]8cb5d5b2010-02-09 11:36:16329RenderProcessHost* TabContents::GetRenderProcessHost() const {
330 return render_manager_.current_host()->process();
331}
332
[email protected]d5f942ba2008-09-26 19:30:34333const GURL& TabContents::GetURL() const {
334 // We may not have a navigation entry yet
[email protected]ce3fa3c2009-04-20 19:55:57335 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]ebe89e062009-08-13 23:16:54336 return entry ? entry->virtual_url() : GURL::EmptyGURL();
[email protected]d5f942ba2008-09-26 19:30:34337}
338
[email protected]96d185d2009-04-24 03:28:54339const string16& TabContents::GetTitle() const {
[email protected]4c6092c5b2009-06-06 00:23:55340 // Transient entries take precedence. They are used for interstitial pages
341 // that are shown on top of existing pages.
342 NavigationEntry* entry = controller_.GetTransientEntry();
[email protected]b5cca982011-05-26 04:42:08343 std::string accept_languages =
344 content::GetContentClient()->browser()->GetAcceptLangs(this);
[email protected]45d0ef7f2011-01-05 13:46:23345 if (entry) {
[email protected]b5cca982011-05-26 04:42:08346 return entry->GetTitleForDisplay(accept_languages);
[email protected]45d0ef7f2011-01-05 13:46:23347 }
[email protected]7ade2732011-02-10 00:13:58348 WebUI* our_web_ui = render_manager_.pending_web_ui() ?
349 render_manager_.pending_web_ui() : render_manager_.web_ui();
350 if (our_web_ui) {
[email protected]96d185d2009-04-24 03:28:54351 // Don't override the title in view source mode.
[email protected]4c6092c5b2009-06-06 00:23:55352 entry = controller_.GetActiveEntry();
[email protected]96d185d2009-04-24 03:28:54353 if (!(entry && entry->IsViewSourceMode())) {
[email protected]e0112912011-02-02 22:54:35354 // Give the Web UI the chance to override our title.
[email protected]7ade2732011-02-10 00:13:58355 const string16& title = our_web_ui->overridden_title();
[email protected]96d185d2009-04-24 03:28:54356 if (!title.empty())
357 return title;
358 }
359 }
360
361 // We use the title for the last committed entry rather than a pending
362 // navigation entry. For example, when the user types in a URL, we want to
363 // keep the old page's title until the new load has committed and we get a new
364 // title.
[email protected]96d185d2009-04-24 03:28:54365 entry = controller_.GetLastCommittedEntry();
[email protected]45d0ef7f2011-01-05 13:46:23366 if (entry) {
[email protected]b5cca982011-05-26 04:42:08367 return entry->GetTitleForDisplay(accept_languages);
[email protected]45d0ef7f2011-01-05 13:46:23368 }
[email protected]987fc3a2011-05-26 14:18:09369
370 // |page_title_when_no_navigation_entry_| is finally used
371 // if no title cannot be retrieved.
372 return page_title_when_no_navigation_entry_;
[email protected]96d185d2009-04-24 03:28:54373}
374
[email protected]d5f942ba2008-09-26 19:30:34375int32 TabContents::GetMaxPageID() {
376 if (GetSiteInstance())
377 return GetSiteInstance()->max_page_id();
378 else
379 return max_page_id_;
380}
381
382void TabContents::UpdateMaxPageID(int32 page_id) {
383 // Ensure both the SiteInstance and RenderProcessHost update their max page
[email protected]57c6a652009-05-04 07:58:34384 // IDs in sync. Only TabContents will also have site instances, except during
[email protected]d5f942ba2008-09-26 19:30:34385 // testing.
386 if (GetSiteInstance())
387 GetSiteInstance()->UpdateMaxPageID(page_id);
[email protected]8cb5d5b2010-02-09 11:36:16388 GetRenderProcessHost()->UpdateMaxPageID(page_id);
[email protected]d5f942ba2008-09-26 19:30:34389}
390
[email protected]96d185d2009-04-24 03:28:54391SiteInstance* TabContents::GetSiteInstance() const {
392 return render_manager_.current_host()->site_instance();
393}
394
[email protected]77362eb2011-08-01 17:18:38395SiteInstance* TabContents::GetPendingSiteInstance() const {
396 RenderViewHost* dest_rvh = render_manager_.pending_render_view_host() ?
397 render_manager_.pending_render_view_host() :
398 render_manager_.current_host();
399 return dest_rvh->site_instance();
400}
401
[email protected]96d185d2009-04-24 03:28:54402bool TabContents::ShouldDisplayURL() {
[email protected]8ab24cc2009-11-13 20:56:15403 // Don't hide the url in view source mode and with interstitials.
[email protected]96d185d2009-04-24 03:28:54404 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]8ab24cc2009-11-13 20:56:15405 if (entry && (entry->IsViewSourceMode() ||
[email protected]cccd3762010-11-12 18:40:01406 entry->page_type() == INTERSTITIAL_PAGE)) {
[email protected]96d185d2009-04-24 03:28:54407 return true;
[email protected]8ab24cc2009-11-13 20:56:15408 }
[email protected]83e3895b2009-06-11 00:07:16409
[email protected]2b9a06402011-02-16 04:57:10410 // We always display the URL for non-WebUI URLs to prevent spoofing.
[email protected]1fd1a502011-03-30 16:55:56411 if (entry && !content::WebUIFactory::Get()->HasWebUIScheme(entry->url()))
[email protected]2b9a06402011-02-16 04:57:10412 return true;
413
[email protected]d0980792011-02-13 19:41:40414 WebUI* web_ui = GetWebUIForCurrentState();
[email protected]7ade2732011-02-10 00:13:58415 if (web_ui)
416 return !web_ui->should_hide_url();
[email protected]96d185d2009-04-24 03:28:54417 return true;
418}
419
[email protected]f5d978c2011-07-21 14:43:51420bool TabContents::IsLoading() const {
421 return is_loading_ || (web_ui() && web_ui()->IsLoading());
422}
423
[email protected]585b30362011-01-28 02:30:17424void TabContents::AddObserver(TabContentsObserver* observer) {
425 observers_.AddObserver(observer);
[email protected]3c9e1872010-11-18 16:17:49426}
427
[email protected]585b30362011-01-28 02:30:17428void TabContents::RemoveObserver(TabContentsObserver* observer) {
429 observers_.RemoveObserver(observer);
[email protected]3c9e1872010-11-18 16:17:49430}
431
[email protected]443b80e2010-12-14 00:42:23432void TabContents::SetIsCrashed(base::TerminationStatus status, int error_code) {
433 if (status == crashed_status_)
[email protected]d5f942ba2008-09-26 19:30:34434 return;
435
[email protected]443b80e2010-12-14 00:42:23436 crashed_status_ = status;
437 crashed_error_code_ = error_code;
[email protected]c9cd2222009-05-06 05:16:50438 NotifyNavigationStateChanged(INVALIDATE_TAB);
[email protected]d5f942ba2008-09-26 19:30:34439}
440
441void TabContents::NotifyNavigationStateChanged(unsigned changed_flags) {
442 if (delegate_)
443 delegate_->NavigationStateChanged(this, changed_flags);
444}
445
[email protected]96d185d2009-04-24 03:28:54446void TabContents::DidBecomeSelected() {
447 controller_.SetActive(true);
[email protected]8cb5d5b2010-02-09 11:36:16448 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
449 if (rwhv) {
450 rwhv->DidBecomeSelected();
[email protected]789e9152009-08-04 21:59:43451#if defined(OS_MACOSX)
[email protected]8cb5d5b2010-02-09 11:36:16452 rwhv->SetActive(true);
[email protected]789e9152009-08-04 21:59:43453#endif
454 }
[email protected]96d185d2009-04-24 03:28:54455
[email protected]5ac20162010-11-24 23:33:11456 last_selected_time_ = base::TimeTicks::Now();
[email protected]3e69bc82011-05-26 23:22:38457
458 FOR_EACH_OBSERVER(TabContentsObserver, observers_, DidBecomeSelected());
[email protected]96d185d2009-04-24 03:28:54459}
460
461void TabContents::WasHidden() {
462 if (!capturing_contents()) {
463 // |render_view_host()| can be NULL if the user middle clicks a link to open
464 // a tab in then background, then closes the tab before selecting it. This
465 // is because closing the tab calls TabContents::Destroy(), which removes
466 // the |render_view_host()|; then when we actually destroy the window,
467 // OnWindowPosChanged() notices and calls HideContents() (which calls us).
[email protected]8cb5d5b2010-02-09 11:36:16468 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
469 if (rwhv)
470 rwhv->WasHidden();
[email protected]96d185d2009-04-24 03:28:54471 }
472
473 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:27474 content::NOTIFICATION_TAB_CONTENTS_HIDDEN,
[email protected]96d185d2009-04-24 03:28:54475 Source<TabContents>(this),
476 NotificationService::NoDetails());
477}
478
[email protected]d5f942ba2008-09-26 19:30:34479void TabContents::Activate() {
480 if (delegate_)
481 delegate_->ActivateContents(this);
482}
483
[email protected]ea42e7782010-08-23 23:58:12484void TabContents::Deactivate() {
485 if (delegate_)
486 delegate_->DeactivateContents(this);
487}
488
[email protected]63954792011-07-11 04:17:48489void TabContents::LostCapture() {
490 if (delegate_)
491 delegate_->LostCapture();
492}
493
494bool TabContents::PreHandleKeyboardEvent(const NativeWebKeyboardEvent& event,
495 bool* is_keyboard_shortcut) {
496 return delegate_ &&
497 delegate_->PreHandleKeyboardEvent(event, is_keyboard_shortcut);
498}
499
500void TabContents::HandleKeyboardEvent(const NativeWebKeyboardEvent& event) {
501 if (delegate_)
502 delegate_->HandleKeyboardEvent(event);
503}
504
505void TabContents::HandleMouseUp() {
506 if (delegate_)
507 delegate_->HandleMouseUp();
508}
509
510void TabContents::HandleMouseActivate() {
511 if (delegate_)
512 delegate_->HandleMouseActivate();
513}
514
[email protected]8a5e0ca2011-08-25 06:30:47515void TabContents::ToggleFullscreenMode(bool enter_fullscreen) {
516 if (delegate_)
517 delegate_->ToggleFullscreenModeForTab(this, enter_fullscreen);
518}
519
[email protected]0548c5352011-09-07 00:33:33520void TabContents::UpdatePreferredSize(const gfx::Size& pref_size) {
521 if (delegate_)
522 delegate_->UpdatePreferredSize(this, pref_size);
523}
524
[email protected]96d185d2009-04-24 03:28:54525void TabContents::ShowContents() {
[email protected]8cb5d5b2010-02-09 11:36:16526 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
527 if (rwhv)
528 rwhv->DidBecomeSelected();
[email protected]96d185d2009-04-24 03:28:54529}
530
531void TabContents::HideContents() {
532 // TODO(pkasting): http://b/1239839 Right now we purposefully don't call
533 // our superclass HideContents(), because some callers want to be very picky
534 // about the order in which these get called. In addition to making the code
535 // here practically impossible to understand, this also means we end up
536 // calling TabContents::WasHidden() twice if callers call both versions of
[email protected]57c6a652009-05-04 07:58:34537 // HideContents() on a TabContents.
[email protected]96d185d2009-04-24 03:28:54538 WasHidden();
539}
540
[email protected]5aab5e22010-12-08 22:13:29541bool TabContents::NeedToFireBeforeUnload() {
542 // TODO(creis): Should we fire even for interstitial pages?
543 return notify_disconnection() &&
544 !showing_interstitial_page() &&
545 !render_view_host()->SuddenTerminationAllowed();
546}
547
[email protected]00c37fc2011-08-02 00:22:50548// TODO(adriansc): Remove this method once refactoring changed all call sites.
[email protected]992e4542011-07-20 23:09:25549TabContents* TabContents::OpenURL(const GURL& url,
550 const GURL& referrer,
551 WindowOpenDisposition disposition,
552 PageTransition::Type transition) {
[email protected]00c37fc2011-08-02 00:22:50553 return OpenURL(OpenURLParams(url, referrer, disposition, transition));
554}
555
556TabContents* TabContents::OpenURL(const OpenURLParams& params) {
[email protected]1c642b52011-04-15 09:05:49557 if (delegate_) {
[email protected]00c37fc2011-08-02 00:22:50558 TabContents* new_contents = delegate_->OpenURLFromTab(this, params);
[email protected]1c642b52011-04-15 09:05:49559 // Notify observers.
560 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
[email protected]00c37fc2011-08-02 00:22:50561 DidOpenURL(params.url, params.referrer,
562 params.disposition, params.transition));
[email protected]992e4542011-07-20 23:09:25563 return new_contents;
[email protected]1c642b52011-04-15 09:05:49564 }
[email protected]992e4542011-07-20 23:09:25565 return NULL;
[email protected]d5f942ba2008-09-26 19:30:34566}
567
[email protected]1ccb3568d2010-02-19 10:51:16568bool TabContents::NavigateToPendingEntry(
569 NavigationController::ReloadType reload_type) {
[email protected]876bc832010-09-07 16:29:54570 return NavigateToEntry(*controller_.pending_entry(), reload_type);
571}
[email protected]96d185d2009-04-24 03:28:54572
[email protected]876bc832010-09-07 16:29:54573bool TabContents::NavigateToEntry(
574 const NavigationEntry& entry,
575 NavigationController::ReloadType reload_type) {
[email protected]96d185d2009-04-24 03:28:54576 RenderViewHost* dest_render_view_host = render_manager_.Navigate(entry);
577 if (!dest_render_view_host)
578 return false; // Unable to create the desired render view host.
579
[email protected]80a8fad2011-01-29 04:02:38580 // For security, we should never send non-Web-UI URLs to a Web UI renderer.
[email protected]05fdd492010-11-15 17:52:07581 // Double check that here.
[email protected]54ec6472010-04-09 19:39:58582 int enabled_bindings = dest_render_view_host->enabled_bindings();
[email protected]1fd1a502011-03-30 16:55:56583 bool is_allowed_in_web_ui_renderer = content::GetContentClient()->
[email protected]3d7474ff2011-07-27 17:47:37584 browser()->GetWebUIFactory()->IsURLAcceptableForWebUI(browser_context(),
[email protected]1fd1a502011-03-30 16:55:56585 entry.url());
[email protected]c09163a2011-02-15 00:05:55586 CHECK(!BindingsPolicy::is_web_ui_enabled(enabled_bindings) ||
587 is_allowed_in_web_ui_renderer);
[email protected]54ec6472010-04-09 19:39:58588
[email protected]96d185d2009-04-24 03:28:54589 // Tell DevTools agent that it is attached prior to the navigation.
[email protected]d9f9b792009-06-24 13:17:12590 DevToolsManager* devtools_manager = DevToolsManager::GetInstance();
[email protected]7e8e6b62009-05-08 11:28:32591 if (devtools_manager) { // NULL in unit tests.
[email protected]3cc72b12010-03-18 23:03:00592 devtools_manager->OnNavigatingToPendingEntry(render_view_host(),
593 dest_render_view_host,
594 entry.url());
[email protected]7e8e6b62009-05-08 11:28:32595 }
[email protected]96d185d2009-04-24 03:28:54596
597 // Used for page load time metrics.
598 current_load_start_ = base::TimeTicks::Now();
599
600 // Navigate in the desired RenderViewHost.
[email protected]056de2d2009-06-26 16:41:34601 ViewMsg_Navigate_Params navigate_params;
[email protected]6c6b02d2011-09-02 03:36:47602 MakeNavigateParams(entry, controller_, delegate_, reload_type,
603 &navigate_params);
[email protected]056de2d2009-06-26 16:41:34604 dest_render_view_host->Navigate(navigate_params);
[email protected]96d185d2009-04-24 03:28:54605
606 if (entry.page_id() == -1) {
607 // HACK!! This code suppresses javascript: URLs from being added to
608 // session history, which is what we want to do for javascript: URLs that
609 // do not generate content. What we really need is a message from the
610 // renderer telling us that a new page was not created. The same message
611 // could be used for mailto: URLs and the like.
612 if (entry.url().SchemeIs(chrome::kJavaScriptScheme))
613 return false;
614 }
615
[email protected]3c9e1872010-11-18 16:17:49616 // Notify observers about navigation.
[email protected]b375c5d2011-05-03 21:15:04617 FOR_EACH_OBSERVER(TabContentsObserver,
618 observers_,
619 NavigateToPendingEntry(entry.url(), reload_type));
[email protected]96d185d2009-04-24 03:28:54620
[email protected]09b29342011-06-24 19:18:48621 if (delegate_)
622 delegate_->DidNavigateToPendingEntry(this);
623
[email protected]d5f942ba2008-09-26 19:30:34624 return true;
625}
626
[email protected]9e1ad4b2011-08-14 16:49:19627void TabContents::SetHistoryLengthAndPrune(const SiteInstance* site_instance,
628 int history_length,
629 int32 minimum_page_id) {
[email protected]5229253b2011-08-16 14:36:41630 // SetHistoryLengthAndPrune doesn't work when there are pending cross-site
631 // navigations. Callers should ensure that this is the case.
[email protected]9e1ad4b2011-08-14 16:49:19632 if (render_manager_.pending_render_view_host()) {
633 NOTREACHED();
[email protected]796931a92011-08-10 01:32:14634 return;
[email protected]9e1ad4b2011-08-14 16:49:19635 }
636 RenderViewHost* rvh = render_view_host();
637 if (!rvh) {
638 NOTREACHED();
639 return;
640 }
641 if (site_instance && rvh->site_instance() != site_instance) {
642 NOTREACHED();
643 return;
644 }
645 rvh->Send(new ViewMsg_SetHistoryLengthAndPrune(rvh->routing_id(),
646 history_length,
647 minimum_page_id));
[email protected]796931a92011-08-10 01:32:14648}
649
[email protected]96d185d2009-04-24 03:28:54650void TabContents::Stop() {
651 render_manager_.Stop();
[email protected]2e3f4572011-03-25 19:24:47652 FOR_EACH_OBSERVER(TabContentsObserver, observers_, StopNavigation());
[email protected]96d185d2009-04-24 03:28:54653}
654
[email protected]96d185d2009-04-24 03:28:54655TabContents* TabContents::Clone() {
656 // We create a new SiteInstance so that the new tab won't share processes
657 // with the old one. This can be changed in the future if we need it to share
658 // processes for some reason.
[email protected]3d7474ff2011-07-27 17:47:37659 TabContents* tc = new TabContents(
660 browser_context(),
661 SiteInstance::CreateSiteInstance(browser_context()),
662 MSG_ROUTING_NONE, this, NULL);
[email protected]96d185d2009-04-24 03:28:54663 tc->controller().CopyStateFrom(controller_);
664 return tc;
665}
666
[email protected]4d677202009-07-19 07:37:12667void TabContents::ShowPageInfo(const GURL& url,
668 const NavigationEntry::SSLStatus& ssl,
669 bool show_history) {
670 if (!delegate_)
671 return;
672
[email protected]3d7474ff2011-07-27 17:47:37673 delegate_->ShowPageInfo(browser_context(), url, ssl, show_history);
[email protected]4d677202009-07-19 07:37:12674}
675
[email protected]aed59602011-02-28 22:57:33676void TabContents::AddConstrainedDialog(ConstrainedWindow* window) {
initial.commit09911bf2008-07-26 23:55:29677 child_windows_.push_back(window);
[email protected]fa1cf0b82010-01-15 21:49:44678
679 if (child_windows_.size() == 1) {
680 window->ShowConstrainedWindow();
681 BlockTabContent(true);
682 }
initial.commit09911bf2008-07-26 23:55:29683}
684
[email protected]fa1cf0b82010-01-15 21:49:44685void TabContents::BlockTabContent(bool blocked) {
[email protected]8dccd7cb2010-02-25 05:19:55686 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
[email protected]1b072562010-12-13 19:37:45687 // 70% opaque grey.
688 SkColor greyish = SkColorSetARGB(178, 0, 0, 0);
[email protected]8dccd7cb2010-02-25 05:19:55689 if (rwhv)
[email protected]1b072562010-12-13 19:37:45690 rwhv->SetVisuallyDeemphasized(blocked ? &greyish : NULL, false);
[email protected]a7eccac2011-04-15 03:05:19691 // RenderViewHost may be NULL during shutdown.
692 if (render_view_host())
693 render_view_host()->set_ignore_input_events(blocked);
[email protected]fa1cf0b82010-01-15 21:49:44694 if (delegate_)
695 delegate_->SetTabContentBlocked(this, blocked);
696}
697
[email protected]e7cfdbd2011-04-22 14:41:37698void TabContents::AddNewContents(TabContents* new_contents,
699 WindowOpenDisposition disposition,
700 const gfx::Rect& initial_pos,
701 bool user_gesture) {
initial.commit09911bf2008-07-26 23:55:29702 if (!delegate_)
703 return;
704
[email protected]e7cfdbd2011-04-22 14:41:37705 delegate_->AddNewContents(this, new_contents, disposition, initial_pos,
706 user_gesture);
[email protected]96d185d2009-04-24 03:28:54707}
708
[email protected]5c9e97a2009-09-09 23:48:30709gfx::NativeView TabContents::GetContentNativeView() const {
[email protected]96d185d2009-04-24 03:28:54710 return view_->GetContentNativeView();
711}
712
713gfx::NativeView TabContents::GetNativeView() const {
714 return view_->GetNativeView();
715}
716
717void TabContents::GetContainerBounds(gfx::Rect *out) const {
718 view_->GetContainerBounds(out);
719}
720
721void TabContents::Focus() {
722 view_->Focus();
723}
724
[email protected]90daadb42009-06-08 21:27:28725void TabContents::FocusThroughTabTraversal(bool reverse) {
[email protected]7e383692009-06-12 19:14:54726 if (showing_interstitial_page()) {
727 render_manager_.interstitial_page()->FocusThroughTabTraversal(reverse);
728 return;
729 }
[email protected]96d185d2009-04-24 03:28:54730 render_view_host()->SetInitialFocus(reverse);
731}
732
733bool TabContents::FocusLocationBarByDefault() {
[email protected]d0980792011-02-13 19:41:40734 WebUI* web_ui = GetWebUIForCurrentState();
[email protected]7ade2732011-02-10 00:13:58735 if (web_ui)
736 return web_ui->focus_location_bar_by_default();
[email protected]3e3f0eb2009-06-22 18:33:43737 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]e0d481582009-09-15 21:06:25738 if (entry && entry->url() == GURL(chrome::kAboutBlankURL))
[email protected]3e3f0eb2009-06-22 18:33:43739 return true;
[email protected]96d185d2009-04-24 03:28:54740 return false;
741}
742
[email protected]a26dc362010-04-23 01:48:58743void TabContents::SetFocusToLocationBar(bool select_all) {
[email protected]a11aaf22010-03-30 00:03:38744 if (delegate())
[email protected]a26dc362010-04-23 01:48:58745 delegate()->SetFocusToLocationBar(select_all);
[email protected]a11aaf22010-03-30 00:03:38746}
747
[email protected]686493142011-07-15 21:47:22748bool TabContents::CanDownload(int request_id) {
749 TabContentsDelegate* d = delegate();
750 if (d)
751 return d->CanDownload(this, request_id);
752 return true;
753}
754
755void TabContents::OnStartDownload(DownloadItem* download) {
756 TabContentsDelegate* d = delegate();
757 if (d)
758 d->OnStartDownload(this, download);
759}
760
[email protected]d5f942ba2008-09-26 19:30:34761void TabContents::WillClose(ConstrainedWindow* window) {
[email protected]d6219912010-11-17 19:47:37762 ConstrainedWindowList::iterator i(
763 std::find(child_windows_.begin(), child_windows_.end(), window));
764 bool removed_topmost_window = i == child_windows_.begin();
765 if (i != child_windows_.end())
766 child_windows_.erase(i);
767 if (child_windows_.empty()) {
[email protected]fa1cf0b82010-01-15 21:49:44768 BlockTabContent(false);
[email protected]d6219912010-11-17 19:47:37769 } else {
770 if (removed_topmost_window)
771 child_windows_[0]->ShowConstrainedWindow();
772 BlockTabContent(true);
[email protected]fa1cf0b82010-01-15 21:49:44773 }
[email protected]66ba4932009-06-04 19:22:13774}
[email protected]d5f942ba2008-09-26 19:30:34775
[email protected]c7dd2f62011-07-18 15:57:59776void TabContents::OnSavePage() {
777 // If we can not save the page, try to download it.
778 if (!SavePackage::IsSavableContents(contents_mime_type())) {
[email protected]3d7474ff2011-07-27 17:47:37779 DownloadManager* dlm = browser_context()->GetDownloadManager();
[email protected]c7dd2f62011-07-18 15:57:59780 const GURL& current_page_url = GetURL();
781 if (dlm && current_page_url.is_valid()) {
782 dlm->DownloadUrl(current_page_url, GURL(), "", this);
[email protected]8bd9e562011-08-16 23:55:46783 download_stats::RecordDownloadCount(
784 download_stats::INITIATED_BY_SAVE_PACKAGE_FAILURE_COUNT);
[email protected]c7dd2f62011-07-18 15:57:59785 return;
786 }
787 }
788
789 Stop();
790
791 // Create the save package and possibly prompt the user for the name to save
792 // the page as. The user prompt is an asynchronous operation that runs on
793 // another thread.
794 save_package_ = new SavePackage(this);
795 save_package_->GetSaveInfo();
796}
797
798// Used in automated testing to bypass prompting the user for file names.
799// Instead, the names and paths are hard coded rather than running them through
800// file name sanitation and extension / mime checking.
801bool TabContents::SavePage(const FilePath& main_file, const FilePath& dir_path,
802 SavePackage::SavePackageType save_type) {
803 // Stop the page from navigating.
804 Stop();
805
806 save_package_ = new SavePackage(this, save_type, main_file, dir_path);
807 return save_package_->Init();
808}
809
810void TabContents::OnSaveURL(const GURL& url) {
[email protected]3d7474ff2011-07-27 17:47:37811 DownloadManager* dlm = browser_context()->GetDownloadManager();
[email protected]c7dd2f62011-07-18 15:57:59812 dlm->DownloadUrl(url, GetURL(), "", this);
813}
814
[email protected]420ae012009-04-24 05:16:32815bool TabContents::IsActiveEntry(int32 page_id) {
816 NavigationEntry* active_entry = controller_.GetActiveEntry();
817 return (active_entry != NULL &&
818 active_entry->site_instance() == GetSiteInstance() &&
819 active_entry->page_id() == page_id);
820}
821
[email protected]b2fe07d12010-02-09 14:38:08822void TabContents::SetOverrideEncoding(const std::string& encoding) {
[email protected]8cb5d5b2010-02-09 11:36:16823 set_encoding(encoding);
[email protected]216813952011-05-19 22:21:26824 render_view_host()->Send(new ViewMsg_SetPageEncoding(
825 render_view_host()->routing_id(), encoding));
[email protected]8cb5d5b2010-02-09 11:36:16826}
827
[email protected]b2fe07d12010-02-09 14:38:08828void TabContents::ResetOverrideEncoding() {
[email protected]8cb5d5b2010-02-09 11:36:16829 reset_encoding();
[email protected]216813952011-05-19 22:21:26830 render_view_host()->Send(new ViewMsg_ResetPageEncodingToDefault(
831 render_view_host()->routing_id()));
[email protected]8cb5d5b2010-02-09 11:36:16832}
833
[email protected]7ab1e7d62009-10-14 23:32:01834void TabContents::OnCloseStarted() {
835 if (tab_close_start_time_.is_null())
836 tab_close_start_time_ = base::TimeTicks::Now();
837}
838
[email protected]46624bf2010-06-09 16:04:19839bool TabContents::ShouldAcceptDragAndDrop() const {
840#if defined(OS_CHROMEOS)
841 // ChromeOS panels (pop-ups) do not take drag-n-drop.
842 // See http://crosbug.com/2413
[email protected]b35b26b32011-05-05 20:35:14843 if (delegate() && delegate()->IsPopupOrPanel(this))
[email protected]b83af492010-10-09 03:21:20844 return false;
845 return true;
[email protected]46624bf2010-06-09 16:04:19846#else
847 return true;
848#endif
849}
850
[email protected]7813bd72011-02-05 02:19:34851void TabContents::SystemDragEnded() {
852 if (render_view_host())
853 render_view_host()->DragSourceSystemDragEnded();
854 if (delegate())
855 delegate()->DragEnded();
856}
857
[email protected]d0b8d092010-10-25 04:05:17858double TabContents::GetZoomLevel() const {
[email protected]3d7474ff2011-07-27 17:47:37859 HostZoomMap* zoom_map = browser_context()->GetHostZoomMap();
[email protected]b75b8292010-10-01 07:28:25860 if (!zoom_map)
[email protected]d0b8d092010-10-25 04:05:17861 return 0;
[email protected]b75b8292010-10-01 07:28:25862
863 double zoom_level;
864 if (temporary_zoom_settings_) {
865 zoom_level = zoom_map->GetTemporaryZoomLevel(
866 GetRenderProcessHost()->id(), render_view_host()->routing_id());
867 } else {
[email protected]13ffa32e2011-05-27 16:37:01868 zoom_level = zoom_map->GetZoomLevel(net::GetHostOrSpecFromURL(GetURL()));
[email protected]b75b8292010-10-01 07:28:25869 }
[email protected]d0b8d092010-10-25 04:05:17870 return zoom_level;
871}
[email protected]b75b8292010-10-01 07:28:25872
[email protected]d0b8d092010-10-25 04:05:17873int TabContents::GetZoomPercent(bool* enable_increment,
874 bool* enable_decrement) {
875 *enable_decrement = *enable_increment = false;
[email protected]b75b8292010-10-01 07:28:25876 int percent = static_cast<int>(
[email protected]d0b8d092010-10-25 04:05:17877 WebKit::WebView::zoomLevelToZoomFactor(GetZoomLevel()) * 100);
[email protected]b75b8292010-10-01 07:28:25878 *enable_decrement = percent > minimum_zoom_percent_;
879 *enable_increment = percent < maximum_zoom_percent_;
880 return percent;
881}
882
[email protected]3c733bde2010-12-21 19:56:31883void TabContents::ViewSource() {
[email protected]1788e772010-12-15 16:40:50884 if (!delegate_)
885 return;
886
887 NavigationEntry* active_entry = controller().GetActiveEntry();
888 if (!active_entry)
889 return;
890
891 delegate_->ViewSourceForTab(this, active_entry->url());
[email protected]77d8d622010-12-15 10:30:12892}
893
[email protected]932b7a12011-03-09 12:50:27894void TabContents::ViewFrameSource(const GURL& url,
895 const std::string& content_state) {
896 if (!delegate_)
897 return;
898
899 delegate_->ViewSourceForFrame(this, url, content_state);
900}
901
[email protected]c40d6232011-03-25 00:16:21902void TabContents::SetContentRestrictions(int restrictions) {
903 content_restrictions_ = restrictions;
904 delegate()->ContentRestrictionsChanged(this);
905}
906
[email protected]724159a2010-12-30 01:11:18907void TabContents::OnDidStartProvisionalLoadForFrame(int64 frame_id,
908 bool is_main_frame,
[email protected]eacb080b2011-05-22 19:40:26909 bool has_opener_set,
[email protected]724159a2010-12-30 01:11:18910 const GURL& url) {
911 bool is_error_page = (url.spec() == chrome::kUnreachableWebDataURL);
912 GURL validated_url(url);
913 render_view_host()->FilterURL(ChildProcessSecurityPolicy::GetInstance(),
914 GetRenderProcessHost()->id(), &validated_url);
915
[email protected]8093a542011-05-13 07:29:32916 RenderViewHost* rvh =
917 render_manager_.pending_render_view_host() ?
918 render_manager_.pending_render_view_host() : render_view_host();
[email protected]0d60f0192011-04-14 12:40:10919 // Notify observers about the start of the provisional load.
920 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
921 DidStartProvisionalLoadForFrame(frame_id, is_main_frame,
[email protected]8093a542011-05-13 07:29:32922 validated_url, is_error_page, rvh));
[email protected]0d60f0192011-04-14 12:40:10923
[email protected]724159a2010-12-30 01:11:18924 if (is_main_frame) {
[email protected]4850a7f2011-03-08 23:36:59925 // Notify observers about the provisional change in the main frame URL.
926 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
[email protected]eacb080b2011-05-22 19:40:26927 ProvisionalChangeToMainFrameUrl(url, has_opener_set));
[email protected]724159a2010-12-30 01:11:18928 }
929}
930
931void TabContents::OnDidRedirectProvisionalLoad(int32 page_id,
[email protected]eacb080b2011-05-22 19:40:26932 bool has_opener_set,
[email protected]724159a2010-12-30 01:11:18933 const GURL& source_url,
934 const GURL& target_url) {
[email protected]18ba2b12011-04-06 16:35:49935 // TODO(creis): Remove this method and have the pre-rendering code listen to
936 // the ResourceDispatcherHost's RESOURCE_RECEIVED_REDIRECT notification
937 // instead. See http://crbug.com/78512.
[email protected]724159a2010-12-30 01:11:18938 NavigationEntry* entry;
939 if (page_id == -1)
940 entry = controller_.pending_entry();
941 else
942 entry = controller_.GetEntryWithPageID(GetSiteInstance(), page_id);
943 if (!entry || entry->url() != source_url)
944 return;
[email protected]e7d50892011-01-19 21:47:38945
[email protected]4850a7f2011-03-08 23:36:59946 // Notify observers about the provisional change in the main frame URL.
947 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
[email protected]eacb080b2011-05-22 19:40:26948 ProvisionalChangeToMainFrameUrl(target_url,
949 has_opener_set));
[email protected]724159a2010-12-30 01:11:18950}
951
952void TabContents::OnDidFailProvisionalLoadWithError(
953 int64 frame_id,
954 bool is_main_frame,
955 int error_code,
956 const GURL& url,
957 bool showing_repost_interstitial) {
958 VLOG(1) << "Failed Provisional Load: " << url.possibly_invalid_spec()
959 << ", error_code: " << error_code
960 << " is_main_frame: " << is_main_frame
961 << " showing_repost_interstitial: " << showing_repost_interstitial
962 << " frame_id: " << frame_id;
963 GURL validated_url(url);
964 render_view_host()->FilterURL(ChildProcessSecurityPolicy::GetInstance(),
965 GetRenderProcessHost()->id(), &validated_url);
966
967 if (net::ERR_ABORTED == error_code) {
968 // EVIL HACK ALERT! Ignore failed loads when we're showing interstitials.
969 // This means that the interstitial won't be torn down properly, which is
970 // bad. But if we have an interstitial, go back to another tab type, and
971 // then load the same interstitial again, we could end up getting the first
972 // interstitial's "failed" message (as a result of the cancel) when we're on
973 // the second one.
974 //
975 // We can't tell this apart, so we think we're tearing down the current page
976 // which will cause a crash later one. There is also some code in
977 // RenderViewHostManager::RendererAbortedProvisionalLoad that is commented
978 // out because of this problem.
979 //
980 // http://code.google.com/p/chromium/issues/detail?id=2855
981 // Because this will not tear down the interstitial properly, if "back" is
982 // back to another tab type, the interstitial will still be somewhat alive
983 // in the previous tab type. If you navigate somewhere that activates the
984 // tab with the interstitial again, you'll see a flash before the new load
985 // commits of the interstitial page.
986 if (showing_interstitial_page()) {
987 LOG(WARNING) << "Discarding message during interstitial.";
988 return;
989 }
990
[email protected]02102f82011-06-13 20:37:02991 // Discard our pending entry if the load canceled (e.g. if we decided to
992 // download the file instead of load it). We do not verify that the URL
993 // being canceled matches the pending entry's URL because they will not
994 // match if a redirect occurred (in which case we do not want to leave a
995 // stale redirect URL showing). This means that we also cancel the pending
996 // entry if the user started a new navigation. As a result, the navigation
997 // controller may not remember that a load is in progress, but the
998 // navigation will still commit even if there is no pending entry.
[email protected]724159a2010-12-30 01:11:18999 NavigationEntry* pending_entry = controller_.pending_entry();
[email protected]02102f82011-06-13 20:37:021000 if (pending_entry)
[email protected]c95fa8b2011-04-28 20:26:161001 DidCancelLoading();
[email protected]724159a2010-12-30 01:11:181002
1003 render_manager_.RendererAbortedProvisionalLoad(render_view_host());
1004 }
1005
1006 // Send out a notification that we failed a provisional load with an error.
1007 ProvisionalLoadDetails details(
1008 is_main_frame, controller_.IsURLInPageNavigation(validated_url),
[email protected]b9e8ea62011-03-04 06:29:091009 validated_url, std::string(), false, frame_id);
[email protected]724159a2010-12-30 01:11:181010 details.set_error_code(error_code);
1011
1012 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271013 content::NOTIFICATION_FAIL_PROVISIONAL_LOAD_WITH_ERROR,
[email protected]724159a2010-12-30 01:11:181014 Source<NavigationController>(&controller_),
1015 Details<ProvisionalLoadDetails>(&details));
[email protected]0d60f0192011-04-14 12:40:101016
1017 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
1018 DidFailProvisionalLoad(frame_id, is_main_frame,
1019 validated_url, error_code));
[email protected]724159a2010-12-30 01:11:181020}
1021
1022void TabContents::OnDidLoadResourceFromMemoryCache(
1023 const GURL& url,
[email protected]70435962011-08-02 20:13:281024 const std::string& security_info,
1025 const std::string& http_method,
1026 ResourceType::Type resource_type) {
[email protected]28620862011-03-22 23:07:191027 base::StatsCounter cache("WebKit.CacheHit");
[email protected]724159a2010-12-30 01:11:181028 cache.Increment();
1029
1030 // Send out a notification that we loaded a resource from our memory cache.
1031 int cert_id = 0, cert_status = 0, security_bits = -1, connection_status = 0;
1032 SSLManager::DeserializeSecurityInfo(security_info,
1033 &cert_id, &cert_status,
1034 &security_bits,
1035 &connection_status);
[email protected]7a8c55e2011-02-15 08:21:161036 LoadFromMemoryCacheDetails details(url, GetRenderProcessHost()->id(),
1037 cert_id, cert_status);
[email protected]724159a2010-12-30 01:11:181038
1039 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271040 content::NOTIFICATION_LOAD_FROM_MEMORY_CACHE,
[email protected]724159a2010-12-30 01:11:181041 Source<NavigationController>(&controller_),
1042 Details<LoadFromMemoryCacheDetails>(&details));
1043}
1044
1045void TabContents::OnDidDisplayInsecureContent() {
[email protected]221a5d92011-04-28 18:23:161046 UserMetrics::RecordAction(UserMetricsAction("SSL.DisplayedInsecureContent"));
[email protected]724159a2010-12-30 01:11:181047 displayed_insecure_content_ = true;
[email protected]9678d8f2011-08-05 04:05:071048 SSLManager::NotifySSLInternalStateChanged(&controller());
[email protected]724159a2010-12-30 01:11:181049}
1050
1051void TabContents::OnDidRunInsecureContent(
[email protected]92771112011-01-20 00:13:021052 const std::string& security_origin, const GURL& target_url) {
[email protected]9941cf52011-02-08 16:17:231053 LOG(INFO) << security_origin << " ran insecure content from "
1054 << target_url.possibly_invalid_spec();
[email protected]221a5d92011-04-28 18:23:161055 UserMetrics::RecordAction(UserMetricsAction("SSL.RanInsecureContent"));
[email protected]ca406032011-07-19 21:53:051056 if (EndsWith(security_origin, kDotGoogleDotCom, false)) {
1057 UserMetrics::RecordAction(
1058 UserMetricsAction("SSL.RanInsecureContentGoogle"));
1059 }
[email protected]724159a2010-12-30 01:11:181060 controller_.ssl_manager()->DidRunInsecureContent(security_origin);
[email protected]f2b58c4d2011-06-01 23:29:411061 displayed_insecure_content_ = true;
[email protected]9678d8f2011-08-05 04:05:071062 SSLManager::NotifySSLInternalStateChanged(&controller());
[email protected]724159a2010-12-30 01:11:181063}
1064
1065void TabContents::OnDocumentLoadedInFrame(int64 frame_id) {
1066 controller_.DocumentLoadedInFrame();
[email protected]0d60f0192011-04-14 12:40:101067 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
1068 DocumentLoadedInFrame(frame_id));
[email protected]724159a2010-12-30 01:11:181069}
1070
1071void TabContents::OnDidFinishLoad(int64 frame_id) {
[email protected]0d60f0192011-04-14 12:40:101072 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
1073 DidFinishLoad(frame_id));
[email protected]724159a2010-12-30 01:11:181074}
1075
[email protected]c8f73ab2011-01-22 00:05:171076void TabContents::OnUpdateContentRestrictions(int restrictions) {
[email protected]c40d6232011-03-25 00:16:211077 SetContentRestrictions(restrictions);
[email protected]c8f73ab2011-01-22 00:05:171078}
1079
[email protected]216813952011-05-19 22:21:261080void TabContents::OnGoToEntryAtOffset(int offset) {
1081 if (!delegate_ || delegate_->OnGoToEntryOffset(offset)) {
1082 NavigationEntry* entry = controller_.GetEntryAtOffset(offset);
1083 if (!entry)
1084 return;
1085 // Note that we don't call NavigationController::GotToOffset() as we don't
1086 // want to create a pending navigation entry (it might end up lingering
1087 // http://crbug.com/51680).
1088 entry->set_transition_type(entry->transition_type() |
1089 PageTransition::FORWARD_BACK);
1090 NavigateToEntry(*entry, NavigationController::NO_RELOAD);
[email protected]a13cc362011-07-28 21:29:571091
1092 // If the entry is being restored and doesn't have a SiteInstance yet, fill
1093 // it in now that we know. This allows us to find the entry when it commits.
1094 if (!entry->site_instance() &&
1095 entry->restore_type() != NavigationEntry::RESTORE_NONE) {
[email protected]77362eb2011-08-01 17:18:381096 entry->set_site_instance(GetPendingSiteInstance());
[email protected]a13cc362011-07-28 21:29:571097 }
[email protected]216813952011-05-19 22:21:261098 }
1099}
1100
1101void TabContents::OnUpdateZoomLimits(int minimum_percent,
1102 int maximum_percent,
1103 bool remember) {
1104 minimum_zoom_percent_ = minimum_percent;
1105 maximum_zoom_percent_ = maximum_percent;
1106 temporary_zoom_settings_ = !remember;
1107}
1108
1109void TabContents::OnFocusedNodeChanged(bool is_editable_node) {
1110 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271111 content::NOTIFICATION_FOCUS_CHANGED_IN_PAGE,
[email protected]216813952011-05-19 22:21:261112 Source<TabContents>(this),
1113 Details<const bool>(&is_editable_node));
1114}
1115
[email protected]3a29a6e2011-08-24 18:26:211116void TabContents::OnEnumerateDirectory(int request_id,
1117 const FilePath& path) {
1118 delegate()->EnumerateDirectory(this, request_id, path);
1119}
1120
[email protected]7d189022011-08-25 22:54:201121void TabContents::OnJSOutOfMemory() {
1122 delegate()->JSOutOfMemory(this);
1123}
1124
1125void TabContents::OnRegisterProtocolHandler(const std::string& protocol,
1126 const GURL& url,
1127 const string16& title) {
1128 delegate()->RegisterProtocolHandler(this, protocol, url, title);
1129}
1130
1131void TabContents::OnRegisterIntentHandler(const string16& action,
1132 const string16& type,
1133 const string16& href,
1134 const string16& title) {
1135 delegate()->RegisterIntentHandler(this, action, type, href, title);
1136}
1137
1138void TabContents::OnWebIntentDispatch(const IPC::Message& message,
1139 const string16& action,
1140 const string16& type,
1141 const string16& data,
1142 int intent_id) {
1143 delegate()->WebIntentDispatch(this, message.routing_id(), action, type,
1144 data, intent_id);
1145}
1146
[email protected]b888919c2011-09-02 00:32:161147void TabContents::OnFindReply(int request_id,
1148 int number_of_matches,
1149 const gfx::Rect& selection_rect,
1150 int active_match_ordinal,
1151 bool final_update) {
1152 delegate()->FindReply(this, request_id, number_of_matches, selection_rect,
1153 active_match_ordinal, final_update);
1154}
1155
[email protected]d952a052011-09-06 18:42:451156void TabContents::OnCrashedPlugin(const FilePath& plugin_path) {
1157 delegate()->CrashedPlugin(this, plugin_path);
1158}
1159
[email protected]7fc4bbb2011-09-08 21:23:101160void TabContents::OnAppCacheAccessed(const GURL& manifest_url,
1161 bool blocked_by_policy) {
1162 // Notify observers about navigation.
1163 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
1164 AppCacheAccessed(manifest_url, blocked_by_policy));
1165}
1166
[email protected]96d185d2009-04-24 03:28:541167// Notifies the RenderWidgetHost instance about the fact that the page is
1168// loading, or done loading and calls the base implementation.
1169void TabContents::SetIsLoading(bool is_loading,
1170 LoadNotificationDetails* details) {
1171 if (is_loading == is_loading_)
1172 return;
1173
1174 if (!is_loading) {
[email protected]9c235f042011-08-10 22:28:211175 load_state_ = net::LoadStateWithParam(net::LOAD_STATE_IDLE, string16());
[email protected]96d185d2009-04-24 03:28:541176 load_state_host_.clear();
[email protected]094e5b22009-09-25 04:23:561177 upload_size_ = 0;
1178 upload_position_ = 0;
[email protected]96d185d2009-04-24 03:28:541179 }
1180
1181 render_manager_.SetIsLoading(is_loading);
1182
1183 is_loading_ = is_loading;
1184 waiting_for_response_ = is_loading;
1185
[email protected]6ebdc9b2010-09-27 16:55:571186 if (delegate_)
1187 delegate_->LoadingStateChanged(this);
[email protected]329581b2009-04-28 06:52:351188 NotifyNavigationStateChanged(INVALIDATE_LOAD);
[email protected]96d185d2009-04-24 03:28:541189
[email protected]432115822011-07-10 15:52:271190 int type = is_loading ? content::NOTIFICATION_LOAD_START :
1191 content::NOTIFICATION_LOAD_STOP;
[email protected]ce5c4502009-05-06 16:46:111192 NotificationDetails det = NotificationService::NoDetails();
[email protected]96d185d2009-04-24 03:28:541193 if (details)
1194 det = Details<LoadNotificationDetails>(details);
1195 NotificationService::current()->Notify(type,
1196 Source<NavigationController>(&controller_),
1197 det);
1198}
1199
[email protected]d0980792011-02-13 19:41:401200WebUI* TabContents::GetWebUIForCurrentState() {
[email protected]7ade2732011-02-10 00:13:581201 // When there is a pending navigation entry, we want to use the pending WebUI
[email protected]96d185d2009-04-24 03:28:541202 // that goes along with it to control the basic flags. For example, we want to
1203 // show the pending URL in the URL bar, so we want the display_url flag to
1204 // be from the pending entry.
1205 //
1206 // The confusion comes because there are multiple possibilities for the
1207 // initial load in a tab as a side effect of the way the RenderViewHostManager
1208 // works.
1209 //
[email protected]e0112912011-02-02 22:54:351210 // - For the very first tab the load looks "normal". The new tab Web UI is
[email protected]96d185d2009-04-24 03:28:541211 // the pending one, and we want it to apply here.
1212 //
1213 // - For subsequent new tabs, they'll get a new SiteInstance which will then
1214 // get switched to the one previously associated with the new tab pages.
[email protected]7ade2732011-02-10 00:13:581215 // This switching will cause the manager to commit the RVH/WebUI. So we'll
[email protected]e0112912011-02-02 22:54:351216 // have a committed Web UI in this case.
[email protected]96d185d2009-04-24 03:28:541217 //
1218 // This condition handles all of these cases:
1219 //
1220 // - First load in first tab: no committed nav entry + pending nav entry +
1221 // pending dom ui:
[email protected]e0112912011-02-02 22:54:351222 // -> Use pending Web UI if any.
[email protected]96d185d2009-04-24 03:28:541223 //
1224 // - First load in second tab: no committed nav entry + pending nav entry +
[email protected]e0112912011-02-02 22:54:351225 // no pending Web UI:
1226 // -> Use the committed Web UI if any.
[email protected]96d185d2009-04-24 03:28:541227 //
1228 // - Second navigation in any tab: committed nav entry + pending nav entry:
[email protected]e0112912011-02-02 22:54:351229 // -> Use pending Web UI if any.
[email protected]96d185d2009-04-24 03:28:541230 //
1231 // - Normal state with no load: committed nav entry + no pending nav entry:
[email protected]e0112912011-02-02 22:54:351232 // -> Use committed Web UI.
[email protected]96d185d2009-04-24 03:28:541233 if (controller_.pending_entry() &&
1234 (controller_.GetLastCommittedEntry() ||
[email protected]7ade2732011-02-10 00:13:581235 render_manager_.pending_web_ui()))
1236 return render_manager_.pending_web_ui();
1237 return render_manager_.web_ui();
[email protected]96d185d2009-04-24 03:28:541238}
[email protected]420ae012009-04-24 05:16:321239
[email protected]1fd1a502011-03-30 16:55:561240WebUI::TypeID TabContents::GetWebUITypeForCurrentState() {
[email protected]3d7474ff2011-07-27 17:47:371241 return content::WebUIFactory::Get()->GetWebUIType(browser_context(),
1242 GetURL());
[email protected]1fd1a502011-03-30 16:55:561243}
1244
[email protected]420ae012009-04-24 05:16:321245void TabContents::DidNavigateMainFramePostCommit(
[email protected]8286f51a2011-05-31 17:39:131246 const content::LoadCommittedDetails& details,
[email protected]420ae012009-04-24 05:16:321247 const ViewHostMsg_FrameNavigate_Params& params) {
[email protected]1fd1a502011-03-30 16:55:561248 if (opener_web_ui_type_ != WebUI::kNoWebUI) {
[email protected]80a8fad2011-01-29 04:02:381249 // If this is a window.open navigation, use the same WebUI as the renderer
[email protected]c2e74fe82009-09-03 17:57:441250 // that opened the window, as long as both renderers have the same
1251 // privileges.
[email protected]1fd1a502011-03-30 16:55:561252 if (delegate_ && opener_web_ui_type_ == GetWebUITypeForCurrentState()) {
1253 WebUI* web_ui = content::GetContentClient()->browser()->
1254 GetWebUIFactory()->CreateWebUIForURL(this, GetURL());
[email protected]7ade2732011-02-10 00:13:581255 // web_ui might be NULL if the URL refers to a non-existent extension.
1256 if (web_ui) {
[email protected]d0980792011-02-13 19:41:401257 render_manager_.SetWebUIPostCommit(web_ui);
[email protected]7ade2732011-02-10 00:13:581258 web_ui->RenderViewCreated(render_view_host());
[email protected]c2e74fe82009-09-03 17:57:441259 }
1260 }
[email protected]1fd1a502011-03-30 16:55:561261 opener_web_ui_type_ = WebUI::kNoWebUI;
[email protected]c2e74fe82009-09-03 17:57:441262 }
1263
[email protected]4e697b042011-07-08 06:44:561264 if (details.is_navigation_to_different_page()) {
[email protected]420ae012009-04-24 05:16:321265 // Clear the status bubble. This is a workaround for a bug where WebKit
1266 // doesn't let us know that the cursor left an element during a
1267 // transition (this is also why the mouse cursor remains as a hand after
1268 // clicking on a link); see bugs 1184641 and 980803. We don't want to
1269 // clear the bubble when a user navigates to a named anchor in the same
1270 // page.
1271 UpdateTargetURL(details.entry->page_id(), GURL());
[email protected]420ae012009-04-24 05:16:321272 }
1273
[email protected]a6e82fc2010-02-24 22:28:141274 if (!details.is_in_page) {
[email protected]f17a0ee2010-05-17 17:38:471275 // Once the main frame is navigated, we're no longer considered to have
1276 // displayed insecure content.
1277 displayed_insecure_content_ = false;
[email protected]aece2c7f2009-08-27 20:43:171278 }
[email protected]ce5c4502009-05-06 16:46:111279
[email protected]8cc8d492010-02-02 10:40:491280 // Close constrained windows if necessary.
1281 if (!net::RegistryControlledDomainService::SameDomainOrHost(
1282 details.previous_url, details.entry->url()))
1283 CloseConstrainedWindows();
1284
[email protected]3c9e1872010-11-18 16:17:491285 // Notify observers about navigation.
[email protected]585b30362011-01-28 02:30:171286 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
[email protected]3c9e1872010-11-18 16:17:491287 DidNavigateMainFramePostCommit(details, params));
[email protected]420ae012009-04-24 05:16:321288}
1289
1290void TabContents::DidNavigateAnyFramePostCommit(
1291 RenderViewHost* render_view_host,
[email protected]8286f51a2011-05-31 17:39:131292 const content::LoadCommittedDetails& details,
[email protected]420ae012009-04-24 05:16:321293 const ViewHostMsg_FrameNavigate_Params& params) {
[email protected]3ab9cb82011-06-03 18:02:071294 // If we navigate, reset JavaScript state. This does nothing to prevent
[email protected]420ae012009-04-24 05:16:321295 // a malicious script from spamming messages, since the script could just
1296 // reload the page to stop blocking.
[email protected]2e5b90c2011-08-16 21:11:551297 if (dialog_creator_) {
1298 dialog_creator_->ResetJavaScriptState(this);
1299 dialog_creator_ = NULL;
1300 }
[email protected]420ae012009-04-24 05:16:321301
[email protected]3c9e1872010-11-18 16:17:491302 // Notify observers about navigation.
[email protected]585b30362011-01-28 02:30:171303 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
[email protected]3c9e1872010-11-18 16:17:491304 DidNavigateAnyFramePostCommit(details, params));
[email protected]420ae012009-04-24 05:16:321305}
1306
[email protected]8cc8d492010-02-02 10:40:491307void TabContents::CloseConstrainedWindows() {
1308 // Clear out any constrained windows since we are leaving this page entirely.
[email protected]aed59602011-02-28 22:57:331309 // To ensure that we iterate over every element in child_windows_ we
1310 // need to use a copy of child_windows_. Otherwise if
1311 // window->CloseConstrainedWindow() modifies child_windows_ we could end up
1312 // skipping some elements.
1313 ConstrainedWindowList child_windows_copy(child_windows_);
1314 for (ConstrainedWindowList::iterator it = child_windows_copy.begin();
1315 it != child_windows_copy.end(); ++it) {
1316 ConstrainedWindow* window = *it;
[email protected]fa1cf0b82010-01-15 21:49:441317 if (window) {
[email protected]420ae012009-04-24 05:16:321318 window->CloseConstrainedWindow();
[email protected]fa1cf0b82010-01-15 21:49:441319 BlockTabContent(false);
1320 }
[email protected]420ae012009-04-24 05:16:321321 }
1322}
1323
[email protected]420ae012009-04-24 05:16:321324void TabContents::UpdateMaxPageIDIfNecessary(SiteInstance* site_instance,
1325 RenderViewHost* rvh) {
1326 // If we are creating a RVH for a restored controller, then we might
1327 // have more page IDs than the SiteInstance's current max page ID. We must
1328 // make sure that the max page ID is larger than any restored page ID.
1329 // Note that it is ok for conflicting page IDs to exist in another tab
1330 // (i.e., NavigationController), but if any page ID is larger than the max,
1331 // the back/forward list will get confused.
1332 int max_restored_page_id = controller_.max_restored_page_id();
1333 if (max_restored_page_id > 0) {
1334 int curr_max_page_id = site_instance->max_page_id();
1335 if (max_restored_page_id > curr_max_page_id) {
1336 // Need to update the site instance immediately.
1337 site_instance->UpdateMaxPageID(max_restored_page_id);
1338
1339 // Also tell the renderer to update its internal representation. We
1340 // need to reserve enough IDs to make all restored page IDs less than
1341 // the max.
1342 if (curr_max_page_id < 0)
1343 curr_max_page_id = 0;
[email protected]216813952011-05-19 22:21:261344 rvh->Send(new ViewMsg_ReservePageIDRange(
1345 rvh->routing_id(), max_restored_page_id - curr_max_page_id));
[email protected]420ae012009-04-24 05:16:321346 }
1347 }
1348}
1349
[email protected]6b2f7a82011-04-25 19:30:511350bool TabContents::UpdateTitleForEntry(NavigationEntry* entry,
[email protected]acafd272011-07-26 17:35:571351 const string16& title) {
[email protected]420ae012009-04-24 05:16:321352 // For file URLs without a title, use the pathname instead. In the case of a
1353 // synthesized title, we don't want the update to count toward the "one set
1354 // per page of the title to history."
[email protected]6b2f7a82011-04-25 19:30:511355 string16 final_title;
[email protected]420ae012009-04-24 05:16:321356 bool explicit_set;
[email protected]987fc3a2011-05-26 14:18:091357 if (entry && entry->url().SchemeIsFile() && title.empty()) {
[email protected]6b2f7a82011-04-25 19:30:511358 final_title = UTF8ToUTF16(entry->url().ExtractFileName());
[email protected]420ae012009-04-24 05:16:321359 explicit_set = false; // Don't count synthetic titles toward the set limit.
1360 } else {
[email protected]acafd272011-07-26 17:35:571361 TrimWhitespace(title, TRIM_ALL, &final_title);
[email protected]420ae012009-04-24 05:16:321362 explicit_set = true;
1363 }
1364
[email protected]987fc3a2011-05-26 14:18:091365 // If a page is created via window.open and never navigated,
1366 // there will be no navigation entry. In this situation,
1367 // |page_title_when_no_navigaiton_entry_| will be used for page title.
1368 if (entry) {
1369 if (final_title == entry->title())
1370 return false; // Nothing changed, don't bother.
[email protected]420ae012009-04-24 05:16:321371
[email protected]987fc3a2011-05-26 14:18:091372 entry->set_title(final_title);
1373 } else {
1374 if (page_title_when_no_navigation_entry_ == final_title)
1375 return false; // Nothing changed, don't bother.
1376
1377 page_title_when_no_navigation_entry_ = final_title;
1378 }
[email protected]420ae012009-04-24 05:16:321379
[email protected]420ae012009-04-24 05:16:321380 // Lastly, set the title for the view.
[email protected]6b2f7a82011-04-25 19:30:511381 view_->SetPageTitle(UTF16ToWideHack(final_title));
[email protected]420ae012009-04-24 05:16:321382
[email protected]105bb0f2011-05-24 17:12:141383 TitleUpdatedDetails details(entry, explicit_set);
1384
[email protected]cbc0e1b2010-04-12 18:33:041385 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271386 content::NOTIFICATION_TAB_CONTENTS_TITLE_UPDATED,
[email protected]cbc0e1b2010-04-12 18:33:041387 Source<TabContents>(this),
[email protected]105bb0f2011-05-24 17:12:141388 Details<TitleUpdatedDetails>(&details));
[email protected]cbc0e1b2010-04-12 18:33:041389
[email protected]420ae012009-04-24 05:16:321390 return true;
1391}
1392
1393void TabContents::NotifySwapped() {
1394 // After sending out a swap notification, we need to send a disconnect
1395 // notification so that clients that pick up a pointer to |this| can NULL the
1396 // pointer. See Bug 1230284.
1397 notify_disconnection_ = true;
1398 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271399 content::NOTIFICATION_TAB_CONTENTS_SWAPPED,
[email protected]d82ed61e2009-06-16 02:46:221400 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:321401 NotificationService::NoDetails());
1402}
1403
1404void TabContents::NotifyConnected() {
1405 notify_disconnection_ = true;
1406 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271407 content::NOTIFICATION_TAB_CONTENTS_CONNECTED,
[email protected]d82ed61e2009-06-16 02:46:221408 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:321409 NotificationService::NoDetails());
1410}
1411
1412void TabContents::NotifyDisconnected() {
1413 if (!notify_disconnection_)
1414 return;
1415
1416 notify_disconnection_ = false;
1417 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271418 content::NOTIFICATION_TAB_CONTENTS_DISCONNECTED,
[email protected]d82ed61e2009-06-16 02:46:221419 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:321420 NotificationService::NoDetails());
1421}
1422
[email protected]8d3347f2009-07-09 22:00:211423RenderViewHostDelegate::View* TabContents::GetViewDelegate() {
[email protected]420ae012009-04-24 05:16:321424 return view_.get();
1425}
1426
[email protected]8d3347f2009-07-09 22:00:211427RenderViewHostDelegate::RendererManagement*
1428TabContents::GetRendererManagementDelegate() {
1429 return &render_manager_;
1430}
1431
[email protected]3d7474ff2011-07-27 17:47:371432RendererPreferences TabContents::GetRendererPrefs(
1433 content::BrowserContext* browser_context) const {
[email protected]840b1512009-07-21 16:53:461434 return renderer_preferences_;
[email protected]80d96fa2009-06-10 22:34:511435}
1436
[email protected]57c6a652009-05-04 07:58:341437TabContents* TabContents::GetAsTabContents() {
1438 return this;
1439}
1440
[email protected]7b291f92009-08-14 05:43:531441ViewType::Type TabContents::GetRenderViewType() const {
1442 return ViewType::TAB_CONTENTS;
1443}
1444
[email protected]420ae012009-04-24 05:16:321445void TabContents::RenderViewCreated(RenderViewHost* render_view_host) {
[email protected]2e4633c2009-07-09 16:58:061446 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271447 content::NOTIFICATION_RENDER_VIEW_HOST_CREATED_FOR_TAB,
[email protected]2e4633c2009-07-09 16:58:061448 Source<TabContents>(this),
1449 Details<RenderViewHost>(render_view_host));
[email protected]420ae012009-04-24 05:16:321450 NavigationEntry* entry = controller_.GetActiveEntry();
1451 if (!entry)
1452 return;
1453
1454 // When we're creating views, we're still doing initial setup, so we always
[email protected]e0112912011-02-02 22:54:351455 // use the pending Web UI rather than any possibly existing committed one.
[email protected]1fd1a502011-03-30 16:55:561456 if (render_manager_.pending_web_ui())
[email protected]7ade2732011-02-10 00:13:581457 render_manager_.pending_web_ui()->RenderViewCreated(render_view_host);
[email protected]420ae012009-04-24 05:16:321458
1459 if (entry->IsViewSourceMode()) {
1460 // Put the renderer in view source mode.
1461 render_view_host->Send(
1462 new ViewMsg_EnableViewSourceMode(render_view_host->routing_id()));
1463 }
[email protected]0666aef2009-05-13 19:48:081464
1465 view()->RenderViewCreated(render_view_host);
[email protected]11f764bb2011-05-25 19:45:261466
1467 FOR_EACH_OBSERVER(
1468 TabContentsObserver, observers_, RenderViewCreated(render_view_host));
[email protected]420ae012009-04-24 05:16:321469}
1470
1471void TabContents::RenderViewReady(RenderViewHost* rvh) {
1472 if (rvh != render_view_host()) {
1473 // Don't notify the world, since this came from a renderer in the
1474 // background.
1475 return;
1476 }
1477
1478 NotifyConnected();
[email protected]9d8a4642009-07-29 17:25:301479 bool was_crashed = is_crashed();
[email protected]443b80e2010-12-14 00:42:231480 SetIsCrashed(base::TERMINATION_STATUS_STILL_RUNNING, 0);
[email protected]9d8a4642009-07-29 17:25:301481
1482 // Restore the focus to the tab (otherwise the focus will be on the top
1483 // window).
[email protected]484ae5912010-09-29 19:16:141484 if (was_crashed && !FocusLocationBarByDefault() &&
1485 (!delegate_ || delegate_->ShouldFocusPageAfterCrash())) {
[email protected]9d8a4642009-07-29 17:25:301486 Focus();
[email protected]484ae5912010-09-29 19:16:141487 }
[email protected]420ae012009-04-24 05:16:321488}
1489
[email protected]443b80e2010-12-14 00:42:231490void TabContents::RenderViewGone(RenderViewHost* rvh,
1491 base::TerminationStatus status,
1492 int error_code) {
[email protected]420ae012009-04-24 05:16:321493 if (rvh != render_view_host()) {
1494 // The pending page's RenderViewHost is gone.
1495 return;
1496 }
1497
1498 SetIsLoading(false, NULL);
1499 NotifyDisconnected();
[email protected]443b80e2010-12-14 00:42:231500 SetIsCrashed(status, error_code);
[email protected]420ae012009-04-24 05:16:321501
[email protected]55452902011-06-01 21:57:471502 FOR_EACH_OBSERVER(TabContentsObserver, observers_, RenderViewGone());
[email protected]420ae012009-04-24 05:16:321503}
1504
[email protected]2e4633c2009-07-09 16:58:061505void TabContents::RenderViewDeleted(RenderViewHost* rvh) {
[email protected]fb7b79f72009-11-06 18:00:481506 render_manager_.RenderViewDeleted(rvh);
[email protected]2e4633c2009-07-09 16:58:061507}
1508
[email protected]420ae012009-04-24 05:16:321509void TabContents::DidNavigate(RenderViewHost* rvh,
1510 const ViewHostMsg_FrameNavigate_Params& params) {
[email protected]09b29342011-06-24 19:18:481511 if (PageTransition::IsMainFrame(params.transition))
[email protected]420ae012009-04-24 05:16:321512 render_manager_.DidNavigateMainFrame(rvh);
[email protected]8030f012009-09-25 18:09:371513
[email protected]420ae012009-04-24 05:16:321514 // Update the site of the SiteInstance if it doesn't have one yet.
1515 if (!GetSiteInstance()->has_site())
1516 GetSiteInstance()->SetSite(params.url);
1517
1518 // Need to update MIME type here because it's referred to in
1519 // UpdateNavigationCommands() called by RendererDidNavigate() to
1520 // determine whether or not to enable the encoding menu.
1521 // It's updated only for the main frame. For a subframe,
1522 // RenderView::UpdateURL does not set params.contents_mime_type.
1523 // (see http://code.google.com/p/chromium/issues/detail?id=2929 )
1524 // TODO(jungshik): Add a test for the encoding menu to avoid
1525 // regressing it again.
1526 if (PageTransition::IsMainFrame(params.transition))
1527 contents_mime_type_ = params.contents_mime_type;
1528
[email protected]8286f51a2011-05-31 17:39:131529 content::LoadCommittedDetails details;
[email protected]93f230e02011-06-01 14:40:001530 bool did_navigate = controller_.RendererDidNavigate(params, &details);
[email protected]3e90d4a2009-07-03 17:38:391531
[email protected]a9c0bfe2010-09-17 08:35:221532 // Send notification about committed provisional loads. This notification is
1533 // different from the NAV_ENTRY_COMMITTED notification which doesn't include
1534 // the actual URL navigated to and isn't sent for AUTO_SUBFRAME navigations.
1535 if (details.type != NavigationType::NAV_IGNORE) {
[email protected]dabb0d12010-10-05 12:50:071536 // For AUTO_SUBFRAME navigations, an event for the main frame is generated
1537 // that is not recorded in the navigation history. For the purpose of
1538 // tracking navigation events, we treat this event as a sub frame navigation
1539 // event.
1540 bool is_main_frame = did_navigate ? details.is_main_frame : false;
[email protected]0d60f0192011-04-14 12:40:101541 PageTransition::Type transition_type = params.transition;
[email protected]a9c0bfe2010-09-17 08:35:221542 // Whether or not a page transition was triggered by going backward or
1543 // forward in the history is only stored in the navigation controller's
1544 // entry list.
1545 if (did_navigate &&
1546 (controller_.GetActiveEntry()->transition_type() &
1547 PageTransition::FORWARD_BACK)) {
[email protected]0d60f0192011-04-14 12:40:101548 transition_type = params.transition | PageTransition::FORWARD_BACK;
[email protected]a9c0bfe2010-09-17 08:35:221549 }
[email protected]0d60f0192011-04-14 12:40:101550 // Notify observers about the commit of the provisional load.
1551 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
1552 DidCommitProvisionalLoadForFrame(params.frame_id,
1553 is_main_frame, params.url, transition_type));
[email protected]a9c0bfe2010-09-17 08:35:221554 }
1555
[email protected]76543b92009-08-31 17:27:451556 if (!did_navigate)
1557 return; // No navigation happened.
[email protected]420ae012009-04-24 05:16:321558
1559 // DO NOT ADD MORE STUFF TO THIS FUNCTION! Your component should either listen
1560 // for the appropriate notification (best) or you can add it to
1561 // DidNavigateMainFramePostCommit / DidNavigateAnyFramePostCommit (only if
1562 // necessary, please).
1563
1564 // Run post-commit tasks.
[email protected]93f230e02011-06-01 14:40:001565 if (details.is_main_frame) {
[email protected]420ae012009-04-24 05:16:321566 DidNavigateMainFramePostCommit(details, params);
[email protected]09b29342011-06-24 19:18:481567 if (delegate())
1568 delegate()->DidNavigateMainFramePostCommit(this);
[email protected]93f230e02011-06-01 14:40:001569 }
[email protected]420ae012009-04-24 05:16:321570 DidNavigateAnyFramePostCommit(rvh, details, params);
1571}
1572
1573void TabContents::UpdateState(RenderViewHost* rvh,
1574 int32 page_id,
1575 const std::string& state) {
[email protected]992db4c2011-05-12 15:37:151576 // Ensure that this state update comes from either the active RVH or one of
1577 // the swapped out RVHs. We don't expect to hear from any other RVHs.
1578 DCHECK(rvh == render_view_host() || render_manager_.IsSwappedOut(rvh));
[email protected]420ae012009-04-24 05:16:321579
1580 // We must be prepared to handle state updates for any page, these occur
1581 // when the user is scrolling and entering form data, as well as when we're
1582 // leaving a page, in which case our state may have already been moved to
1583 // the next page. The navigation controller will look up the appropriate
1584 // NavigationEntry and update it when it is notified via the delegate.
1585
1586 int entry_index = controller_.GetEntryIndexWithPageID(
[email protected]992db4c2011-05-12 15:37:151587 rvh->site_instance(), page_id);
[email protected]420ae012009-04-24 05:16:321588 if (entry_index < 0)
1589 return;
1590 NavigationEntry* entry = controller_.GetEntryAtIndex(entry_index);
1591
1592 if (state == entry->content_state())
1593 return; // Nothing to update.
1594 entry->set_content_state(state);
1595 controller_.NotifyEntryChanged(entry, entry_index);
1596}
1597
1598void TabContents::UpdateTitle(RenderViewHost* rvh,
[email protected]acafd272011-07-26 17:35:571599 int32 page_id,
[email protected]a49e10b2011-08-01 23:57:461600 const string16& title,
1601 base::i18n::TextDirection title_direction) {
[email protected]420ae012009-04-24 05:16:321602 // If we have a title, that's a pretty good indication that we've started
1603 // getting useful data.
1604 SetNotWaitingForResponse();
1605
1606 DCHECK(rvh == render_view_host());
[email protected]371cc8b2010-09-01 16:03:481607 NavigationEntry* entry = controller_.GetEntryWithPageID(rvh->site_instance(),
[email protected]ec0b6c42010-08-26 03:16:581608 page_id);
[email protected]987fc3a2011-05-26 14:18:091609
[email protected]a49e10b2011-08-01 23:57:461610 // TODO(evan): make use of title_direction.
1611 // http://code.google.com/p/chromium/issues/detail?id=27094
[email protected]987fc3a2011-05-26 14:18:091612 if (!UpdateTitleForEntry(entry, title))
[email protected]420ae012009-04-24 05:16:321613 return;
1614
1615 // Broadcast notifications when the UI should be updated.
1616 if (entry == controller_.GetEntryAtOffset(0))
[email protected]f1cd5e82009-10-23 17:19:031617 NotifyNavigationStateChanged(INVALIDATE_TITLE);
[email protected]420ae012009-04-24 05:16:321618}
1619
[email protected]420ae012009-04-24 05:16:321620void TabContents::UpdateEncoding(RenderViewHost* render_view_host,
[email protected]41fc0322009-09-04 22:23:401621 const std::string& encoding) {
[email protected]420ae012009-04-24 05:16:321622 set_encoding(encoding);
1623}
1624
1625void TabContents::UpdateTargetURL(int32 page_id, const GURL& url) {
1626 if (delegate())
[email protected]d952a052011-09-06 18:42:451627 delegate()->UpdateTargetURL(this, page_id, url);
[email protected]420ae012009-04-24 05:16:321628}
1629
[email protected]420ae012009-04-24 05:16:321630void TabContents::Close(RenderViewHost* rvh) {
[email protected]07707302009-11-06 00:50:291631 // The UI may be in an event-tracking loop, such as between the
1632 // mouse-down and mouse-up in text selection or a button click.
1633 // Defer the close until after tracking is complete, so that we
1634 // don't free objects out from under the UI.
1635 // TODO(shess): This could probably be integrated with the
1636 // IsDoingDrag() test below. Punting for now because I need more
1637 // research to understand how this impacts platforms other than Mac.
1638 // TODO(shess): This could get more fine-grained. For instance,
1639 // closing a tab in another window while selecting text in the
1640 // current window's Omnibox should be just fine.
1641 if (view()->IsEventTracking()) {
1642 view()->CloseTabAfterEventTracking();
1643 return;
1644 }
1645
[email protected]24a4d1062009-07-10 23:10:421646 // If we close the tab while we're in the middle of a drag, we'll crash.
1647 // Instead, cancel the drag and close it as soon as the drag ends.
1648 if (view()->IsDoingDrag()) {
1649 view()->CancelDragAndCloseTab();
1650 return;
1651 }
1652
[email protected]420ae012009-04-24 05:16:321653 // Ignore this if it comes from a RenderViewHost that we aren't showing.
1654 if (delegate() && rvh == render_view_host())
1655 delegate()->CloseContents(this);
1656}
1657
1658void TabContents::RequestMove(const gfx::Rect& new_bounds) {
[email protected]b35b26b32011-05-05 20:35:141659 if (delegate() && delegate()->IsPopupOrPanel(this))
[email protected]420ae012009-04-24 05:16:321660 delegate()->MoveContents(this, new_bounds);
1661}
1662
[email protected]7ab1e7d62009-10-14 23:32:011663void TabContents::DidStartLoading() {
[email protected]420ae012009-04-24 05:16:321664 SetIsLoading(true, NULL);
[email protected]3c9e1872010-11-18 16:17:491665
[email protected]c40d6232011-03-25 00:16:211666 if (delegate() && content_restrictions_) {
1667 content_restrictions_ = 0;
[email protected]de4761b2010-12-07 21:05:211668 delegate()->ContentRestrictionsChanged(this);
[email protected]9e823662010-10-13 23:36:001669 }
[email protected]3c9e1872010-11-18 16:17:491670
1671 // Notify observers about navigation.
[email protected]585b30362011-01-28 02:30:171672 FOR_EACH_OBSERVER(TabContentsObserver, observers_, DidStartLoading());
[email protected]420ae012009-04-24 05:16:321673}
1674
[email protected]7ab1e7d62009-10-14 23:32:011675void TabContents::DidStopLoading() {
[email protected]420ae012009-04-24 05:16:321676 scoped_ptr<LoadNotificationDetails> details;
1677
1678 NavigationEntry* entry = controller_.GetActiveEntry();
1679 // An entry may not exist for a stop when loading an initial blank page or
1680 // if an iframe injected by script into a blank page finishes loading.
1681 if (entry) {
[email protected]420ae012009-04-24 05:16:321682 base::TimeDelta elapsed = base::TimeTicks::Now() - current_load_start_;
1683
1684 details.reset(new LoadNotificationDetails(
[email protected]ebe89e062009-08-13 23:16:541685 entry->virtual_url(),
[email protected]420ae012009-04-24 05:16:321686 entry->transition_type(),
1687 elapsed,
1688 &controller_,
1689 controller_.GetCurrentEntryIndex()));
1690 }
1691
[email protected]420ae012009-04-24 05:16:321692 SetIsLoading(false, details.get());
[email protected]3c9e1872010-11-18 16:17:491693
1694 // Notify observers about navigation.
[email protected]585b30362011-01-28 02:30:171695 FOR_EACH_OBSERVER(TabContentsObserver, observers_, DidStopLoading());
[email protected]420ae012009-04-24 05:16:321696}
1697
[email protected]c95fa8b2011-04-28 20:26:161698void TabContents::DidCancelLoading() {
1699 controller_.DiscardNonCommittedEntries();
1700
1701 // Update the URL display.
1702 NotifyNavigationStateChanged(TabContents::INVALIDATE_URL);
1703}
1704
[email protected]1a3c3cb2010-12-16 21:03:401705void TabContents::DidChangeLoadProgress(double progress) {
1706 if (delegate())
1707 delegate()->LoadProgressChanged(progress);
1708}
1709
[email protected]25497492010-09-11 15:15:081710void TabContents::DocumentOnLoadCompletedInMainFrame(
1711 RenderViewHost* render_view_host,
1712 int32 page_id) {
1713 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271714 content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME,
[email protected]25497492010-09-11 15:15:081715 Source<TabContents>(this),
1716 Details<int>(&page_id));
1717}
1718
[email protected]420ae012009-04-24 05:16:321719void TabContents::RequestOpenURL(const GURL& url, const GURL& referrer,
1720 WindowOpenDisposition disposition) {
[email protected]7ade2732011-02-10 00:13:581721 if (render_manager_.web_ui()) {
[email protected]e0112912011-02-02 22:54:351722 // When we're a Web UI, it will provide a page transition type for us (this
[email protected]420ae012009-04-24 05:16:321723 // is so the new tab page can specify AUTO_BOOKMARK for automatically
1724 // generated suggestions).
1725 //
[email protected]e0112912011-02-02 22:54:351726 // Note also that we hide the referrer for Web UI pages. We don't really
[email protected]60e448982009-05-06 04:21:161727 // want web sites to see a referrer of "chrome://blah" (and some
1728 // chrome: URLs might have search terms or other stuff we don't want to
[email protected]420ae012009-04-24 05:16:321729 // send to the site), so we send no referrer.
1730 OpenURL(url, GURL(), disposition,
[email protected]7ade2732011-02-10 00:13:581731 render_manager_.web_ui()->link_transition_type());
[email protected]420ae012009-04-24 05:16:321732 } else {
1733 OpenURL(url, referrer, disposition, PageTransition::LINK);
1734 }
1735}
1736
[email protected]420ae012009-04-24 05:16:321737void TabContents::RunJavaScriptMessage(
[email protected]992db4c2011-05-12 15:37:151738 const RenderViewHost* rvh,
[email protected]4f5ce842011-05-27 19:34:411739 const string16& message,
1740 const string16& default_prompt,
[email protected]420ae012009-04-24 05:16:321741 const GURL& frame_url,
1742 const int flags,
1743 IPC::Message* reply_msg,
1744 bool* did_suppress_message) {
[email protected]3ab9cb82011-06-03 18:02:071745 // Suppress JavaScript dialogs when requested. Also suppress messages when
1746 // showing an interstitial as it's shown over the previous page and we don't
1747 // want the hidden page's dialogs to interfere with the interstitial.
[email protected]8f55e802010-12-06 18:11:501748 bool suppress_this_message =
[email protected]992db4c2011-05-12 15:37:151749 rvh->is_swapped_out() ||
[email protected]8f55e802010-12-06 18:11:501750 showing_interstitial_page() ||
[email protected]3ab9cb82011-06-03 18:02:071751 !delegate_ ||
1752 delegate_->ShouldSuppressDialogs();
[email protected]420ae012009-04-24 05:16:321753
1754 if (!suppress_this_message) {
[email protected]b627d9a2011-06-28 14:06:341755 content::JavaScriptDialogCreator::TitleType title_type;
1756 string16 title;
1757
1758 if (!frame_url.has_host()) {
1759 title_type = content::JavaScriptDialogCreator::DIALOG_TITLE_NONE;
1760 } else {
1761 title_type = content::JavaScriptDialogCreator::DIALOG_TITLE_FORMATTED_URL;
1762 title = net::FormatUrl(
1763 frame_url.GetOrigin(),
1764 content::GetContentClient()->browser()->GetAcceptLangs(this));
1765 }
1766
[email protected]2e5b90c2011-08-16 21:11:551767 dialog_creator_ = delegate_->GetJavaScriptDialogCreator();
1768 dialog_creator_->RunJavaScriptDialog(this,
1769 title_type,
1770 title,
1771 flags,
1772 message,
1773 default_prompt,
1774 reply_msg,
1775 &suppress_this_message);
[email protected]420ae012009-04-24 05:16:321776 }
[email protected]3ab9cb82011-06-03 18:02:071777
1778 if (suppress_this_message) {
1779 // If we are suppressing messages, just reply as if the user immediately
1780 // pressed "Cancel".
1781 OnDialogClosed(reply_msg, false, string16());
1782 }
1783
1784 *did_suppress_message = suppress_this_message;
[email protected]420ae012009-04-24 05:16:321785}
1786
[email protected]992db4c2011-05-12 15:37:151787void TabContents::RunBeforeUnloadConfirm(const RenderViewHost* rvh,
[email protected]4f5ce842011-05-27 19:34:411788 const string16& message,
[email protected]420ae012009-04-24 05:16:321789 IPC::Message* reply_msg) {
[email protected]5aab5e22010-12-08 22:13:291790 if (delegate())
1791 delegate()->WillRunBeforeUnloadConfirm();
[email protected]3ab9cb82011-06-03 18:02:071792
1793 bool suppress_this_message =
1794 rvh->is_swapped_out() ||
1795 !delegate_ ||
1796 delegate_->ShouldSuppressDialogs();
[email protected]992db4c2011-05-12 15:37:151797 if (suppress_this_message) {
[email protected]4f5ce842011-05-27 19:34:411798 render_view_host()->JavaScriptDialogClosed(reply_msg, true, string16());
[email protected]8f55e802010-12-06 18:11:501799 return;
1800 }
[email protected]3ab9cb82011-06-03 18:02:071801
[email protected]7ab1e7d62009-10-14 23:32:011802 is_showing_before_unload_dialog_ = true;
[email protected]2e5b90c2011-08-16 21:11:551803 dialog_creator_ = delegate_->GetJavaScriptDialogCreator();
1804 dialog_creator_->RunBeforeUnloadDialog(this,
1805 message,
1806 reply_msg);
[email protected]420ae012009-04-24 05:16:321807}
1808
[email protected]420ae012009-04-24 05:16:321809WebPreferences TabContents::GetWebkitPrefs() {
[email protected]447021c2010-09-08 21:29:081810 WebPreferences web_prefs =
[email protected]181a95ee2011-07-12 19:26:361811 content::GetContentClient()->browser()->GetWebkitPrefs(
[email protected]3d7474ff2011-07-27 17:47:371812 render_view_host()->process()->browser_context(), false);
[email protected]447021c2010-09-08 21:29:081813
[email protected]b8299c12011-06-03 19:52:281814 // Force accelerated compositing and 2d canvas off for chrome:, about: and
[email protected]2ef216b2011-08-01 19:25:211815 // chrome-devtools: pages (unless it's specifically allowed).
1816 if ((GetURL().SchemeIs(chrome::kChromeDevToolsScheme) ||
[email protected]30b8e0ee2011-09-12 14:45:571817#if !defined(TOUCH_UI)
1818 // Allow accelerated compositing for keyboard and log in screen.
[email protected]b8299c12011-06-03 19:52:281819 GetURL().SchemeIs(chrome::kChromeUIScheme) ||
[email protected]30b8e0ee2011-09-12 14:45:571820#endif
[email protected]cd4a5bd22011-09-13 06:46:301821 (GetURL().SchemeIs(chrome::kAboutScheme) &&
1822 GetURL().spec() != chrome::kAboutBlankURL)) &&
[email protected]2ef216b2011-08-01 19:25:211823 !web_prefs.allow_webui_compositing) {
[email protected]447021c2010-09-08 21:29:081824 web_prefs.accelerated_compositing_enabled = false;
[email protected]9beff752010-09-22 19:35:431825 web_prefs.accelerated_2d_canvas_enabled = false;
[email protected]447021c2010-09-08 21:29:081826 }
1827
[email protected]39553442010-09-09 00:01:551828#if defined(OS_MACOSX)
1829 // Disable accelerated compositing if IOSurface's are not supported,
1830 // as is the case in 10.5.
1831 if (!IOSurfaceSupport::Initialize())
1832 web_prefs.accelerated_compositing_enabled = false;
1833#endif
1834
[email protected]2f7830b32011-08-26 18:22:251835#if defined(TOUCH_UI)
1836 web_prefs.force_compositing_mode = true;
1837#endif
1838
[email protected]447021c2010-09-08 21:29:081839 return web_prefs;
[email protected]420ae012009-04-24 05:16:321840}
1841
[email protected]7d472472011-01-22 01:30:251842void TabContents::OnUserGesture() {
[email protected]ddb85052011-05-18 14:40:271843 // Notify observers.
1844 FOR_EACH_OBSERVER(TabContentsObserver, observers_, DidGetUserGesture());
[email protected]686493142011-07-15 21:47:221845
1846 ResourceDispatcherHost* rdh =
1847 content::GetContentClient()->browser()->GetResourceDispatcherHost();
1848 if (rdh) // NULL in unittests.
[email protected]8ec98cd22011-08-09 22:31:301849 rdh->OnUserGesture(this);
[email protected]7d472472011-01-22 01:30:251850}
1851
[email protected]fa1cf0b82010-01-15 21:49:441852void TabContents::OnIgnoredUIEvent() {
1853 if (constrained_window_count()) {
1854 ConstrainedWindow* window = *constrained_window_begin();
1855 window->FocusConstrainedWindow();
1856 }
1857}
1858
[email protected]420ae012009-04-24 05:16:321859void TabContents::RendererUnresponsive(RenderViewHost* rvh,
1860 bool is_during_unload) {
[email protected]992db4c2011-05-12 15:37:151861 // Don't show hung renderer dialog for a swapped out RVH.
1862 if (rvh != render_view_host())
1863 return;
1864
[email protected]e5fc1632011-08-08 07:51:531865 // Ignore renderer unresponsive event if debugger is attached to the tab
1866 // since the event may be a result of the renderer sitting on a breakpoint.
1867 // See http://crbug.com/65458
1868 DevToolsManager* devtools_manager = DevToolsManager::GetInstance();
1869 if (devtools_manager &&
1870 devtools_manager->GetDevToolsClientHostFor(rvh) != NULL)
1871 return;
1872
[email protected]420ae012009-04-24 05:16:321873 if (is_during_unload) {
1874 // Hang occurred while firing the beforeunload/unload handler.
1875 // Pretend the handler fired so tab closing continues as if it had.
[email protected]829e7612009-04-25 01:15:111876 rvh->set_sudden_termination_allowed(true);
[email protected]420ae012009-04-24 05:16:321877
1878 if (!render_manager_.ShouldCloseTabOnUnresponsiveRenderer())
1879 return;
1880
1881 // If the tab hangs in the beforeunload/unload handler there's really
1882 // nothing we can do to recover. Pretend the unload listeners have
1883 // all fired and close the tab. If the hang is in the beforeunload handler
1884 // then the user will not have the option of cancelling the close.
1885 Close(rvh);
1886 return;
1887 }
1888
[email protected]55452902011-06-01 21:57:471889 if (!render_view_host() || !render_view_host()->IsRenderViewLive())
1890 return;
1891
1892 if (delegate())
1893 delegate()->RendererUnresponsive(this);
[email protected]420ae012009-04-24 05:16:321894}
1895
1896void TabContents::RendererResponsive(RenderViewHost* render_view_host) {
[email protected]55452902011-06-01 21:57:471897 if (delegate())
1898 delegate()->RendererResponsive(this);
[email protected]420ae012009-04-24 05:16:321899}
1900
1901void TabContents::LoadStateChanged(const GURL& url,
[email protected]9c235f042011-08-10 22:28:211902 const net::LoadStateWithParam& load_state,
[email protected]094e5b22009-09-25 04:23:561903 uint64 upload_position,
1904 uint64 upload_size) {
[email protected]420ae012009-04-24 05:16:321905 load_state_ = load_state;
[email protected]094e5b22009-09-25 04:23:561906 upload_position_ = upload_position;
1907 upload_size_ = upload_size;
[email protected]04866c42011-05-03 20:03:501908 load_state_host_ = net::IDNToUnicode(url.host(),
[email protected]b5cca982011-05-26 04:42:081909 content::GetContentClient()->browser()->GetAcceptLangs(this));
[email protected]9c235f042011-08-10 22:28:211910 if (load_state_.state == net::LOAD_STATE_READING_RESPONSE)
[email protected]420ae012009-04-24 05:16:321911 SetNotWaitingForResponse();
[email protected]f5d978c2011-07-21 14:43:511912 if (IsLoading())
[email protected]c9cd2222009-05-06 05:16:501913 NotifyNavigationStateChanged(INVALIDATE_LOAD | INVALIDATE_TAB);
[email protected]420ae012009-04-24 05:16:321914}
1915
[email protected]7d472472011-01-22 01:30:251916void TabContents::WorkerCrashed() {
[email protected]0dd3a0ab2011-02-18 08:17:441917 if (delegate())
[email protected]55452902011-06-01 21:57:471918 delegate()->WorkerCrashed(this);
[email protected]7d472472011-01-22 01:30:251919}
1920
[email protected]420ae012009-04-24 05:16:321921void TabContents::BeforeUnloadFiredFromRenderManager(
1922 bool proceed,
1923 bool* proceed_to_fire_unload) {
1924 if (delegate())
1925 delegate()->BeforeUnloadFired(this, proceed, proceed_to_fire_unload);
1926}
1927
[email protected]3a3d47472010-07-15 21:03:541928void TabContents::DidStartLoadingFromRenderManager(
1929 RenderViewHost* render_view_host) {
1930 DidStartLoading();
1931}
1932
1933void TabContents::RenderViewGoneFromRenderManager(
1934 RenderViewHost* render_view_host) {
[email protected]443b80e2010-12-14 00:42:231935 DCHECK(crashed_status_ != base::TERMINATION_STATUS_STILL_RUNNING);
1936 RenderViewGone(render_view_host, crashed_status_, crashed_error_code_);
[email protected]3a3d47472010-07-15 21:03:541937}
1938
[email protected]420ae012009-04-24 05:16:321939void TabContents::UpdateRenderViewSizeForRenderManager() {
1940 // TODO(brettw) this is a hack. See TabContentsView::SizeContents.
[email protected]0323ee42010-02-17 22:03:261941 gfx::Size size = view_->GetContainerSize();
1942 // 0x0 isn't a valid window size (minimal window size is 1x1) but it may be
1943 // here during container initialization and normal window size will be set
1944 // later. In case of tab duplication this resizing to 0x0 prevents setting
1945 // normal size later so just ignore it.
1946 if (!size.IsEmpty())
1947 view_->SizeContents(size);
[email protected]420ae012009-04-24 05:16:321948}
1949
[email protected]3a3d47472010-07-15 21:03:541950void TabContents::NotifySwappedFromRenderManager() {
1951 NotifySwapped();
1952}
1953
1954NavigationController& TabContents::GetControllerForRenderManager() {
1955 return controller();
1956}
1957
[email protected]d0980792011-02-13 19:41:401958WebUI* TabContents::CreateWebUIForRenderManager(const GURL& url) {
[email protected]1fd1a502011-03-30 16:55:561959 return content::WebUIFactory::Get()->CreateWebUIForURL(this, url);
[email protected]420ae012009-04-24 05:16:321960}
1961
1962NavigationEntry*
1963TabContents::GetLastCommittedNavigationEntryForRenderManager() {
1964 return controller_.GetLastCommittedEntry();
1965}
1966
1967bool TabContents::CreateRenderViewForRenderManager(
1968 RenderViewHost* render_view_host) {
[email protected]420ae012009-04-24 05:16:321969 RenderWidgetHostView* rwh_view = view_->CreateViewForWidget(render_view_host);
[email protected]1a98a932009-11-17 00:12:521970
[email protected]420ae012009-04-24 05:16:321971 // Now that the RenderView has been created, we need to tell it its size.
1972 rwh_view->SetSize(view_->GetContainerSize());
1973
[email protected]a4127722011-04-27 23:13:521974 if (!render_view_host->CreateRenderView(string16()))
1975 return false;
1976
[email protected]f8233cc2011-05-31 20:24:501977#if defined(OS_LINUX)
1978 // Force a ViewMsg_Resize to be sent, needed to make plugins show up on
1979 // linux. See crbug.com/83941.
1980 if (RenderWidgetHost* render_widget_host = rwh_view->GetRenderWidgetHost())
1981 render_widget_host->WasResized();
1982#endif
1983
[email protected]420ae012009-04-24 05:16:321984 UpdateMaxPageIDIfNecessary(render_view_host->site_instance(),
1985 render_view_host);
1986 return true;
1987}
1988
[email protected]3ab9cb82011-06-03 18:02:071989void TabContents::OnDialogClosed(IPC::Message* reply_msg,
1990 bool success,
1991 const string16& user_input) {
[email protected]beb440c2009-11-06 04:08:541992 if (is_showing_before_unload_dialog_ && !success) {
1993 // If a beforeunload dialog is canceled, we need to stop the throbber from
1994 // spinning, since we forced it to start spinning in Navigate.
1995 DidStopLoading();
1996
1997 tab_close_start_time_ = base::TimeTicks();
1998 }
1999 is_showing_before_unload_dialog_ = false;
[email protected]4f5ce842011-05-27 19:34:412000 render_view_host()->JavaScriptDialogClosed(reply_msg,
2001 success,
[email protected]3ab9cb82011-06-03 18:02:072002 user_input);
[email protected]beb440c2009-11-06 04:08:542003}
2004
[email protected]3ab9cb82011-06-03 18:02:072005gfx::NativeWindow TabContents::GetDialogRootWindow() {
2006 return view_->GetTopLevelNativeWindow();
[email protected]beb440c2009-11-06 04:08:542007}
2008
[email protected]a1e97f02011-06-30 14:04:342009void TabContents::OnDialogShown() {
2010 Activate();
[email protected]3a3d47472010-07-15 21:03:542011}
2012
[email protected]41fc0322009-09-04 22:23:402013void TabContents::set_encoding(const std::string& encoding) {
[email protected]763ec4ca2011-04-29 15:48:122014 encoding_ = content::GetContentClient()->browser()->
2015 GetCanonicalEncodingNameByAliasName(encoding);
[email protected]aacd7cc2009-09-02 14:51:102016}
[email protected]f45d2a72010-03-08 23:28:352017
[email protected]33f74972010-12-08 16:40:362018void TabContents::SwapInRenderViewHost(RenderViewHost* rvh) {
2019 render_manager_.SwapInRenderViewHost(rvh);
2020}
2021
2022void TabContents::CreateViewAndSetSizeForRVH(RenderViewHost* rvh) {
2023 RenderWidgetHostView* rwh_view = view()->CreateViewForWidget(rvh);
2024 rwh_view->SetSize(view()->GetContainerSize());
2025}