blob: d4d1ccbf1655dbbcc5c47eb314ecd33d05ba2648 [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]19e81142011-10-03 16:19:4044#include "content/common/content_constants.h"
[email protected]763ec4ca2011-04-29 15:48:1245#include "content/common/content_restriction.h"
[email protected]ce9751942011-09-21 01:57:2446#include "content/common/intents_messages.h"
[email protected]4dd57932011-03-17 06:06:1247#include "content/common/navigation_types.h"
[email protected]7f070d42011-03-09 20:25:3248#include "content/common/notification_service.h"
[email protected]0f012df82011-05-19 14:15:2949#include "content/common/url_constants.h"
[email protected]2c5569662011-03-22 20:45:0250#include "content/common/view_messages.h"
[email protected]d686e812009-06-03 19:10:2951#include "net/base/net_util.h"
[email protected]abe2c032011-03-31 18:49:3452#include "net/url_request/url_request_context_getter.h"
[email protected]8bd0fe62011-01-17 06:44:3753#include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h"
[email protected]08397d52011-02-05 01:53:3854#include "ui/gfx/codec/png_codec.h"
[email protected]d5e311b2010-07-20 17:15:0355#include "webkit/glue/password_form.h"
[email protected]ce9751942011-09-21 01:57:2456#include "webkit/glue/web_intent_data.h"
[email protected]3c733bde2010-12-21 19:56:3157#include "webkit/glue/webpreferences.h"
58
59#if defined(OS_MACOSX)
[email protected]b9b751f22011-03-25 14:04:1260#include "ui/gfx/surface/io_surface_support_mac.h"
[email protected]3c733bde2010-12-21 19:56:3161#endif // defined(OS_MACOSX)
[email protected]3e45ba92009-02-20 21:09:0062
[email protected]420ae012009-04-24 05:16:3263// Cross-Site Navigations
64//
65// If a TabContents is told to navigate to a different web site (as determined
66// by SiteInstance), it will replace its current RenderViewHost with a new
67// RenderViewHost dedicated to the new SiteInstance. This works as follows:
68//
69// - Navigate determines whether the destination is cross-site, and if so,
[email protected]a2750082011-09-01 12:29:4670// it creates a pending_render_view_host_.
[email protected]420ae012009-04-24 05:16:3271// - The pending RVH is "suspended," so that no navigation messages are sent to
72// its renderer until the onbeforeunload JavaScript handler has a chance to
73// run in the current RVH.
74// - The pending RVH tells CrossSiteRequestManager (a thread-safe singleton)
75// that it has a pending cross-site request. ResourceDispatcherHost will
76// check for this when the response arrives.
77// - The current RVH runs its onbeforeunload handler. If it returns false, we
[email protected]a2750082011-09-01 12:29:4678// cancel all the pending logic. Otherwise we allow the pending RVH to send
79// the navigation request to its renderer.
80// - ResourceDispatcherHost receives a ResourceRequest on the IO thread for the
81// main resource load on the pending RVH. It checks CrossSiteRequestManager
82// to see that it is a cross-site request, and installs a
83// CrossSiteResourceHandler.
84// - When RDH receives a response, the BufferedResourceHandler determines
85// whether it is a download. If so, it sends a message to the new renderer
86// causing it to cancel the request, and the download proceeds. For now, the
87// pending RVH remains until the next DidNavigate event for this TabContents.
88// This isn't ideal, but it doesn't affect any functionality.
[email protected]420ae012009-04-24 05:16:3289// - After RDH receives a response and determines that it is safe and not a
90// download, it pauses the response to first run the old page's onunload
91// handler. It does this by asynchronously calling the OnCrossSiteResponse
[email protected]992db4c2011-05-12 15:37:1592// method of TabContents on the UI thread, which sends a SwapOut message
[email protected]420ae012009-04-24 05:16:3293// to the current RVH.
[email protected]992db4c2011-05-12 15:37:1594// - Once the onunload handler is finished, a SwapOut_ACK message is sent to
[email protected]420ae012009-04-24 05:16:3295// the ResourceDispatcherHost, who unpauses the response. Data is then sent
96// to the pending RVH.
97// - The pending renderer sends a FrameNavigate message that invokes the
98// DidNavigate method. This replaces the current RVH with the
[email protected]a2750082011-09-01 12:29:4699// pending RVH.
[email protected]992db4c2011-05-12 15:37:15100// - The previous renderer is kept swapped out in RenderViewHostManager in case
101// the user goes back. The process only stays live if another tab is using
102// it, but if so, the existing frame relationships will be maintained.
[email protected]420ae012009-04-24 05:16:32103
104namespace {
105
106// Amount of time we wait between when a key event is received and the renderer
107// is queried for its state and pushed to the NavigationEntry.
108const int kQueryStateDelay = 5000;
109
[email protected]6ebdc9b2010-09-27 16:55:57110const int kSyncWaitDelay = 40;
111
[email protected]ca406032011-07-19 21:53:05112static const char kDotGoogleDotCom[] = ".google.com";
113
[email protected]420ae012009-04-24 05:16:32114#if defined(OS_WIN)
115
116BOOL CALLBACK InvalidateWindow(HWND hwnd, LPARAM lparam) {
117 // Note: erase is required to properly paint some widgets borders. This can
118 // be seen with textfields.
119 InvalidateRect(hwnd, NULL, TRUE);
120 return TRUE;
121}
122#endif
123
[email protected]2c5569662011-03-22 20:45:02124ViewMsg_Navigate_Type::Value GetNavigationType(
[email protected]3d7474ff2011-07-27 17:47:37125 content::BrowserContext* browser_context, const NavigationEntry& entry,
[email protected]1ccb3568d2010-02-19 10:51:16126 NavigationController::ReloadType reload_type) {
127 switch (reload_type) {
128 case NavigationController::RELOAD:
[email protected]2c5569662011-03-22 20:45:02129 return ViewMsg_Navigate_Type::RELOAD;
[email protected]1ccb3568d2010-02-19 10:51:16130 case NavigationController::RELOAD_IGNORING_CACHE:
[email protected]2c5569662011-03-22 20:45:02131 return ViewMsg_Navigate_Type::RELOAD_IGNORING_CACHE;
[email protected]1ccb3568d2010-02-19 10:51:16132 case NavigationController::NO_RELOAD:
133 break; // Fall through to rest of function.
134 }
[email protected]5e369672009-11-03 23:48:30135
136 if (entry.restore_type() == NavigationEntry::RESTORE_LAST_SESSION &&
[email protected]3d7474ff2011-07-27 17:47:37137 browser_context->DidLastSessionExitCleanly())
[email protected]2c5569662011-03-22 20:45:02138 return ViewMsg_Navigate_Type::RESTORE;
[email protected]5e369672009-11-03 23:48:30139
[email protected]2c5569662011-03-22 20:45:02140 return ViewMsg_Navigate_Type::NORMAL;
[email protected]5e369672009-11-03 23:48:30141}
142
[email protected]876bc832010-09-07 16:29:54143void MakeNavigateParams(const NavigationEntry& entry,
144 const NavigationController& controller,
[email protected]6c6b02d2011-09-02 03:36:47145 TabContentsDelegate* delegate,
[email protected]1ccb3568d2010-02-19 10:51:16146 NavigationController::ReloadType reload_type,
147 ViewMsg_Navigate_Params* params) {
[email protected]056de2d2009-06-26 16:41:34148 params->page_id = entry.page_id();
[email protected]876bc832010-09-07 16:29:54149 params->pending_history_list_offset = controller.GetIndexOfEntry(&entry);
[email protected]3cc72b12010-03-18 23:03:00150 params->current_history_list_offset = controller.last_committed_entry_index();
151 params->current_history_list_length = controller.entry_count();
[email protected]056de2d2009-06-26 16:41:34152 params->url = entry.url();
153 params->referrer = entry.referrer();
154 params->transition = entry.transition_type();
155 params->state = entry.content_state();
[email protected]3cc72b12010-03-18 23:03:00156 params->navigation_type =
[email protected]3d7474ff2011-07-27 17:47:37157 GetNavigationType(controller.browser_context(), entry, reload_type);
[email protected]056de2d2009-06-26 16:41:34158 params->request_time = base::Time::Now();
[email protected]6c6b02d2011-09-02 03:36:47159 params->extra_headers = entry.extra_headers();
160
161 if (delegate)
162 delegate->AddNavigationHeaders(params->url, &params->extra_headers);
[email protected]056de2d2009-06-26 16:41:34163}
164
[email protected]420ae012009-04-24 05:16:32165} // namespace
166
[email protected]f4f50ef2011-01-21 19:01:19167
168// TabContents ----------------------------------------------------------------
[email protected]420ae012009-04-24 05:16:32169
[email protected]3d7474ff2011-07-27 17:47:37170TabContents::TabContents(content::BrowserContext* browser_context,
[email protected]420ae012009-04-24 05:16:32171 SiteInstance* site_instance,
172 int routing_id,
[email protected]6ee12c42010-09-14 09:36:07173 const TabContents* base_tab_contents,
174 SessionStorageNamespace* session_storage_namespace)
[email protected]b680ad22009-04-15 23:19:42175 : delegate_(NULL),
[email protected]6ee12c42010-09-14 09:36:07176 ALLOW_THIS_IN_INITIALIZER_LIST(controller_(
[email protected]3d7474ff2011-07-27 17:47:37177 this, browser_context, session_storage_namespace)),
[email protected]66ba4932009-06-04 19:22:13178 ALLOW_THIS_IN_INITIALIZER_LIST(view_(
[email protected]74313b42011-08-24 16:51:32179 content::GetContentClient()->browser()->CreateTabContentsView(this))),
[email protected]d82ed61e2009-06-16 02:46:22180 ALLOW_THIS_IN_INITIALIZER_LIST(render_manager_(this, this)),
[email protected]d5f942ba2008-09-26 19:30:34181 is_loading_(false),
[email protected]443b80e2010-12-14 00:42:23182 crashed_status_(base::TERMINATION_STATUS_STILL_RUNNING),
183 crashed_error_code_(0),
[email protected]d5f942ba2008-09-26 19:30:34184 waiting_for_response_(false),
[email protected]d5f942ba2008-09-26 19:30:34185 max_page_id_(-1),
[email protected]9c235f042011-08-10 22:28:21186 load_state_(net::LOAD_STATE_IDLE, string16()),
[email protected]094e5b22009-09-25 04:23:56187 upload_size_(0),
188 upload_position_(0),
[email protected]f17a0ee2010-05-17 17:38:47189 displayed_insecure_content_(false),
[email protected]fdd61c62009-04-22 19:22:57190 capturing_contents_(false),
191 is_being_destroyed_(false),
192 notify_disconnection_(false),
[email protected]2e5b90c2011-08-16 21:11:55193 dialog_creator_(NULL),
[email protected]fdd61c62009-04-22 19:22:57194#if defined(OS_WIN)
195 message_box_active_(CreateEvent(NULL, TRUE, FALSE, NULL)),
196#endif
[email protected]7ab1e7d62009-10-14 23:32:01197 is_showing_before_unload_dialog_(false),
[email protected]1fd1a502011-03-30 16:55:56198 opener_web_ui_type_(WebUI::kNoWebUI),
[email protected]ebf40a72010-07-22 01:46:38199 closed_by_user_gesture_(false),
[email protected]b75b8292010-10-01 07:28:25200 minimum_zoom_percent_(
201 static_cast<int>(WebKit::WebView::minTextSizeMultiplier * 100)),
202 maximum_zoom_percent_(
203 static_cast<int>(WebKit::WebView::maxTextSizeMultiplier * 100)),
[email protected]9e823662010-10-13 23:36:00204 temporary_zoom_settings_(false),
[email protected]4850a7f2011-03-08 23:36:59205 content_restrictions_(0) {
[email protected]7ff431e2010-01-07 18:18:54206
[email protected]3d7474ff2011-07-27 17:47:37207 render_manager_.Init(browser_context, site_instance, routing_id);
[email protected]420ae012009-04-24 05:16:32208
[email protected]34ac70502009-09-25 17:07:23209 // We have the initial size of the view be based on the size of the passed in
210 // tab contents (normally a tab from the same window).
211 view_->CreateView(base_tab_contents ?
212 base_tab_contents->view()->GetContainerSize() : gfx::Size());
[email protected]332af7732009-03-11 13:21:35213}
initial.commit09911bf2008-07-26 23:55:29214
215TabContents::~TabContents() {
[email protected]420ae012009-04-24 05:16:32216 is_being_destroyed_ = true;
217
[email protected]3ab9cb82011-06-03 18:02:07218 // Clear out any JavaScript state.
[email protected]2e5b90c2011-08-16 21:11:55219 if (dialog_creator_)
220 dialog_creator_->ResetJavaScriptState(this);
[email protected]3ab9cb82011-06-03 18:02:07221
[email protected]420ae012009-04-24 05:16:32222 NotifyDisconnected();
[email protected]420ae012009-04-24 05:16:32223
[email protected]420ae012009-04-24 05:16:32224 // Notify any observer that have a reference on this tab contents.
225 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:27226 content::NOTIFICATION_TAB_CONTENTS_DESTROYED,
[email protected]420ae012009-04-24 05:16:32227 Source<TabContents>(this),
228 NotificationService::NoDetails());
229
230 // TODO(brettw) this should be moved to the view.
231#if defined(OS_WIN)
232 // If we still have a window handle, destroy it. GetNativeView can return
233 // NULL if this contents was part of a window that closed.
[email protected]50bd6452010-11-27 19:39:42234 if (GetNativeView()) {
235 RenderViewHost* host = render_view_host();
[email protected]b9a1fb42011-04-12 23:16:44236 if (host && host->view())
[email protected]50bd6452010-11-27 19:39:42237 host->view()->WillWmDestroy();
[email protected]50bd6452010-11-27 19:39:42238 }
[email protected]420ae012009-04-24 05:16:32239#endif
[email protected]7ab1e7d62009-10-14 23:32:01240
241 // OnCloseStarted isn't called in unit tests.
242 if (!tab_close_start_time_.is_null()) {
243 UMA_HISTOGRAM_TIMES("Tab.Close",
244 base::TimeTicks::Now() - tab_close_start_time_);
245 }
[email protected]b5a1d11c2011-02-17 03:09:42246
[email protected]07d490bc2011-03-07 17:05:26247 FOR_EACH_OBSERVER(TabContentsObserver, observers_, TabContentsDestroyed());
[email protected]232a5812011-03-04 22:42:08248
[email protected]1de2b8b2011-06-29 19:38:46249 set_delegate(NULL);
250}
251
252// TODO(cbentzel): Either remove the debugging code, or rename to SetDelegate.
253void TabContents::set_delegate(TabContentsDelegate* delegate) {
254 if (delegate == delegate_)
255 return;
256 if (delegate_)
257 delegate_->Detach(this);
258 delegate_ = delegate;
259 if (delegate_)
260 delegate_->Attach(this);
[email protected]b5a1d11c2011-02-17 03:09:42261}
262
[email protected]724159a2010-12-30 01:11:18263bool TabContents::OnMessageReceived(const IPC::Message& message) {
[email protected]f82d57b52011-04-27 19:13:17264 if (web_ui() && web_ui()->OnMessageReceived(message))
265 return true;
266
[email protected]585b30362011-01-28 02:30:17267 ObserverListBase<TabContentsObserver>::Iterator it(observers_);
268 TabContentsObserver* observer;
[email protected]0f180ee2011-01-21 18:23:10269 while ((observer = it.GetNext()) != NULL)
270 if (observer->OnMessageReceived(message))
[email protected]403415a2011-01-10 18:57:53271 return true;
[email protected]403415a2011-01-10 18:57:53272
[email protected]724159a2010-12-30 01:11:18273 bool handled = true;
274 bool message_is_ok = true;
275 IPC_BEGIN_MESSAGE_MAP_EX(TabContents, message, message_is_ok)
[email protected]ce9751942011-09-21 01:57:24276 IPC_MESSAGE_HANDLER(IntentsHostMsg_WebIntentDispatch,
277 OnWebIntentDispatch)
[email protected]724159a2010-12-30 01:11:18278 IPC_MESSAGE_HANDLER(ViewHostMsg_DidStartProvisionalLoadForFrame,
279 OnDidStartProvisionalLoadForFrame)
280 IPC_MESSAGE_HANDLER(ViewHostMsg_DidRedirectProvisionalLoad,
281 OnDidRedirectProvisionalLoad)
282 IPC_MESSAGE_HANDLER(ViewHostMsg_DidFailProvisionalLoadWithError,
283 OnDidFailProvisionalLoadWithError)
284 IPC_MESSAGE_HANDLER(ViewHostMsg_DidLoadResourceFromMemoryCache,
285 OnDidLoadResourceFromMemoryCache)
286 IPC_MESSAGE_HANDLER(ViewHostMsg_DidDisplayInsecureContent,
287 OnDidDisplayInsecureContent)
288 IPC_MESSAGE_HANDLER(ViewHostMsg_DidRunInsecureContent,
289 OnDidRunInsecureContent)
290 IPC_MESSAGE_HANDLER(ViewHostMsg_DocumentLoadedInFrame,
291 OnDocumentLoadedInFrame)
292 IPC_MESSAGE_HANDLER(ViewHostMsg_DidFinishLoad, OnDidFinishLoad)
[email protected]c8f73ab2011-01-22 00:05:17293 IPC_MESSAGE_HANDLER(ViewHostMsg_UpdateContentRestrictions,
294 OnUpdateContentRestrictions)
[email protected]7d472472011-01-22 01:30:25295 IPC_MESSAGE_HANDLER(ViewHostMsg_GoToEntryAtOffset, OnGoToEntryAtOffset)
[email protected]216813952011-05-19 22:21:26296 IPC_MESSAGE_HANDLER(ViewHostMsg_UpdateZoomLimits, OnUpdateZoomLimits)
297 IPC_MESSAGE_HANDLER(ViewHostMsg_FocusedNodeChanged, OnFocusedNodeChanged)
[email protected]c7dd2f62011-07-18 15:57:59298 IPC_MESSAGE_HANDLER(ViewHostMsg_SaveURLAs, OnSaveURL)
[email protected]3a29a6e2011-08-24 18:26:21299 IPC_MESSAGE_HANDLER(ViewHostMsg_EnumerateDirectory, OnEnumerateDirectory)
[email protected]7d189022011-08-25 22:54:20300 IPC_MESSAGE_HANDLER(ViewHostMsg_JSOutOfMemory, OnJSOutOfMemory)
301 IPC_MESSAGE_HANDLER(ViewHostMsg_RegisterProtocolHandler,
302 OnRegisterProtocolHandler)
303 IPC_MESSAGE_HANDLER(ViewHostMsg_RegisterIntentHandler,
304 OnRegisterIntentHandler)
[email protected]b888919c2011-09-02 00:32:16305 IPC_MESSAGE_HANDLER(ViewHostMsg_Find_Reply, OnFindReply)
[email protected]d952a052011-09-06 18:42:45306 IPC_MESSAGE_HANDLER(ViewHostMsg_CrashedPlugin, OnCrashedPlugin)
[email protected]7fc4bbb2011-09-08 21:23:10307 IPC_MESSAGE_HANDLER(ViewHostMsg_AppCacheAccessed, OnAppCacheAccessed)
[email protected]724159a2010-12-30 01:11:18308 IPC_MESSAGE_UNHANDLED(handled = false)
309 IPC_END_MESSAGE_MAP_EX()
310
311 if (!message_is_ok) {
312 UserMetrics::RecordAction(UserMetricsAction("BadMessageTerminate_RVD"));
313 GetRenderProcessHost()->ReceivedBadMessage();
314 }
315
316 return handled;
317}
318
[email protected]6c2e472f2011-08-24 23:26:18319void TabContents::RunFileChooser(
320 RenderViewHost* render_view_host,
321 const ViewHostMsg_RunFileChooser_Params& params) {
322 delegate()->RunFileChooser(this, params);
323}
324
[email protected]8cb5d5b2010-02-09 11:36:16325RenderProcessHost* TabContents::GetRenderProcessHost() const {
326 return render_manager_.current_host()->process();
327}
328
[email protected]d5f942ba2008-09-26 19:30:34329const GURL& TabContents::GetURL() const {
330 // We may not have a navigation entry yet
[email protected]ce3fa3c2009-04-20 19:55:57331 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]ebe89e062009-08-13 23:16:54332 return entry ? entry->virtual_url() : GURL::EmptyGURL();
[email protected]d5f942ba2008-09-26 19:30:34333}
334
[email protected]96d185d2009-04-24 03:28:54335const string16& TabContents::GetTitle() const {
[email protected]4c6092c5b2009-06-06 00:23:55336 // Transient entries take precedence. They are used for interstitial pages
337 // that are shown on top of existing pages.
338 NavigationEntry* entry = controller_.GetTransientEntry();
[email protected]b5cca982011-05-26 04:42:08339 std::string accept_languages =
340 content::GetContentClient()->browser()->GetAcceptLangs(this);
[email protected]45d0ef7f2011-01-05 13:46:23341 if (entry) {
[email protected]b5cca982011-05-26 04:42:08342 return entry->GetTitleForDisplay(accept_languages);
[email protected]45d0ef7f2011-01-05 13:46:23343 }
[email protected]7ade2732011-02-10 00:13:58344 WebUI* our_web_ui = render_manager_.pending_web_ui() ?
345 render_manager_.pending_web_ui() : render_manager_.web_ui();
346 if (our_web_ui) {
[email protected]96d185d2009-04-24 03:28:54347 // Don't override the title in view source mode.
[email protected]4c6092c5b2009-06-06 00:23:55348 entry = controller_.GetActiveEntry();
[email protected]96d185d2009-04-24 03:28:54349 if (!(entry && entry->IsViewSourceMode())) {
[email protected]e0112912011-02-02 22:54:35350 // Give the Web UI the chance to override our title.
[email protected]7ade2732011-02-10 00:13:58351 const string16& title = our_web_ui->overridden_title();
[email protected]96d185d2009-04-24 03:28:54352 if (!title.empty())
353 return title;
354 }
355 }
356
357 // We use the title for the last committed entry rather than a pending
358 // navigation entry. For example, when the user types in a URL, we want to
359 // keep the old page's title until the new load has committed and we get a new
360 // title.
[email protected]96d185d2009-04-24 03:28:54361 entry = controller_.GetLastCommittedEntry();
[email protected]45d0ef7f2011-01-05 13:46:23362 if (entry) {
[email protected]b5cca982011-05-26 04:42:08363 return entry->GetTitleForDisplay(accept_languages);
[email protected]45d0ef7f2011-01-05 13:46:23364 }
[email protected]987fc3a2011-05-26 14:18:09365
366 // |page_title_when_no_navigation_entry_| is finally used
367 // if no title cannot be retrieved.
368 return page_title_when_no_navigation_entry_;
[email protected]96d185d2009-04-24 03:28:54369}
370
[email protected]d5f942ba2008-09-26 19:30:34371int32 TabContents::GetMaxPageID() {
372 if (GetSiteInstance())
373 return GetSiteInstance()->max_page_id();
374 else
375 return max_page_id_;
376}
377
378void TabContents::UpdateMaxPageID(int32 page_id) {
379 // Ensure both the SiteInstance and RenderProcessHost update their max page
[email protected]57c6a652009-05-04 07:58:34380 // IDs in sync. Only TabContents will also have site instances, except during
[email protected]d5f942ba2008-09-26 19:30:34381 // testing.
382 if (GetSiteInstance())
383 GetSiteInstance()->UpdateMaxPageID(page_id);
[email protected]8cb5d5b2010-02-09 11:36:16384 GetRenderProcessHost()->UpdateMaxPageID(page_id);
[email protected]d5f942ba2008-09-26 19:30:34385}
386
[email protected]96d185d2009-04-24 03:28:54387SiteInstance* TabContents::GetSiteInstance() const {
388 return render_manager_.current_host()->site_instance();
389}
390
[email protected]77362eb2011-08-01 17:18:38391SiteInstance* TabContents::GetPendingSiteInstance() const {
392 RenderViewHost* dest_rvh = render_manager_.pending_render_view_host() ?
393 render_manager_.pending_render_view_host() :
394 render_manager_.current_host();
395 return dest_rvh->site_instance();
396}
397
[email protected]96d185d2009-04-24 03:28:54398bool TabContents::ShouldDisplayURL() {
[email protected]8ab24cc2009-11-13 20:56:15399 // Don't hide the url in view source mode and with interstitials.
[email protected]96d185d2009-04-24 03:28:54400 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]8ab24cc2009-11-13 20:56:15401 if (entry && (entry->IsViewSourceMode() ||
[email protected]cccd3762010-11-12 18:40:01402 entry->page_type() == INTERSTITIAL_PAGE)) {
[email protected]96d185d2009-04-24 03:28:54403 return true;
[email protected]8ab24cc2009-11-13 20:56:15404 }
[email protected]83e3895b2009-06-11 00:07:16405
[email protected]2b9a06402011-02-16 04:57:10406 // We always display the URL for non-WebUI URLs to prevent spoofing.
[email protected]1fd1a502011-03-30 16:55:56407 if (entry && !content::WebUIFactory::Get()->HasWebUIScheme(entry->url()))
[email protected]2b9a06402011-02-16 04:57:10408 return true;
409
[email protected]d0980792011-02-13 19:41:40410 WebUI* web_ui = GetWebUIForCurrentState();
[email protected]7ade2732011-02-10 00:13:58411 if (web_ui)
412 return !web_ui->should_hide_url();
[email protected]96d185d2009-04-24 03:28:54413 return true;
414}
415
[email protected]f5d978c2011-07-21 14:43:51416bool TabContents::IsLoading() const {
417 return is_loading_ || (web_ui() && web_ui()->IsLoading());
418}
419
[email protected]585b30362011-01-28 02:30:17420void TabContents::AddObserver(TabContentsObserver* observer) {
421 observers_.AddObserver(observer);
[email protected]3c9e1872010-11-18 16:17:49422}
423
[email protected]585b30362011-01-28 02:30:17424void TabContents::RemoveObserver(TabContentsObserver* observer) {
425 observers_.RemoveObserver(observer);
[email protected]3c9e1872010-11-18 16:17:49426}
427
[email protected]443b80e2010-12-14 00:42:23428void TabContents::SetIsCrashed(base::TerminationStatus status, int error_code) {
429 if (status == crashed_status_)
[email protected]d5f942ba2008-09-26 19:30:34430 return;
431
[email protected]443b80e2010-12-14 00:42:23432 crashed_status_ = status;
433 crashed_error_code_ = error_code;
[email protected]c9cd2222009-05-06 05:16:50434 NotifyNavigationStateChanged(INVALIDATE_TAB);
[email protected]d5f942ba2008-09-26 19:30:34435}
436
437void TabContents::NotifyNavigationStateChanged(unsigned changed_flags) {
438 if (delegate_)
439 delegate_->NavigationStateChanged(this, changed_flags);
440}
441
[email protected]96d185d2009-04-24 03:28:54442void TabContents::DidBecomeSelected() {
443 controller_.SetActive(true);
[email protected]8cb5d5b2010-02-09 11:36:16444 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
445 if (rwhv) {
446 rwhv->DidBecomeSelected();
[email protected]789e9152009-08-04 21:59:43447#if defined(OS_MACOSX)
[email protected]8cb5d5b2010-02-09 11:36:16448 rwhv->SetActive(true);
[email protected]789e9152009-08-04 21:59:43449#endif
450 }
[email protected]96d185d2009-04-24 03:28:54451
[email protected]5ac20162010-11-24 23:33:11452 last_selected_time_ = base::TimeTicks::Now();
[email protected]3e69bc82011-05-26 23:22:38453
454 FOR_EACH_OBSERVER(TabContentsObserver, observers_, DidBecomeSelected());
[email protected]96d185d2009-04-24 03:28:54455}
456
457void TabContents::WasHidden() {
458 if (!capturing_contents()) {
459 // |render_view_host()| can be NULL if the user middle clicks a link to open
460 // a tab in then background, then closes the tab before selecting it. This
461 // is because closing the tab calls TabContents::Destroy(), which removes
462 // the |render_view_host()|; then when we actually destroy the window,
463 // OnWindowPosChanged() notices and calls HideContents() (which calls us).
[email protected]8cb5d5b2010-02-09 11:36:16464 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
465 if (rwhv)
466 rwhv->WasHidden();
[email protected]96d185d2009-04-24 03:28:54467 }
468
469 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:27470 content::NOTIFICATION_TAB_CONTENTS_HIDDEN,
[email protected]96d185d2009-04-24 03:28:54471 Source<TabContents>(this),
472 NotificationService::NoDetails());
473}
474
[email protected]d5f942ba2008-09-26 19:30:34475void TabContents::Activate() {
476 if (delegate_)
477 delegate_->ActivateContents(this);
478}
479
[email protected]ea42e7782010-08-23 23:58:12480void TabContents::Deactivate() {
481 if (delegate_)
482 delegate_->DeactivateContents(this);
483}
484
[email protected]63954792011-07-11 04:17:48485void TabContents::LostCapture() {
486 if (delegate_)
487 delegate_->LostCapture();
488}
489
490bool TabContents::PreHandleKeyboardEvent(const NativeWebKeyboardEvent& event,
491 bool* is_keyboard_shortcut) {
492 return delegate_ &&
493 delegate_->PreHandleKeyboardEvent(event, is_keyboard_shortcut);
494}
495
496void TabContents::HandleKeyboardEvent(const NativeWebKeyboardEvent& event) {
497 if (delegate_)
498 delegate_->HandleKeyboardEvent(event);
499}
500
501void TabContents::HandleMouseUp() {
502 if (delegate_)
503 delegate_->HandleMouseUp();
504}
505
506void TabContents::HandleMouseActivate() {
507 if (delegate_)
508 delegate_->HandleMouseActivate();
509}
510
[email protected]8a5e0ca2011-08-25 06:30:47511void TabContents::ToggleFullscreenMode(bool enter_fullscreen) {
512 if (delegate_)
513 delegate_->ToggleFullscreenModeForTab(this, enter_fullscreen);
514}
515
[email protected]5d5f7af2011-10-01 01:38:12516bool TabContents::IsFullscreenForCurrentTab() const {
517 return delegate_ ? delegate_->IsFullscreenForTab(this) : false;
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]19e81142011-10-03 16:19:40580 // The renderer will reject IPC messages with URLs longer than
581 // this limit, so don't attempt to navigate with a longer URL.
582 if (entry.url().spec().size() > content::kMaxURLChars)
583 return false;
584
[email protected]80a8fad2011-01-29 04:02:38585 // For security, we should never send non-Web-UI URLs to a Web UI renderer.
[email protected]05fdd492010-11-15 17:52:07586 // Double check that here.
[email protected]54ec6472010-04-09 19:39:58587 int enabled_bindings = dest_render_view_host->enabled_bindings();
[email protected]1fd1a502011-03-30 16:55:56588 bool is_allowed_in_web_ui_renderer = content::GetContentClient()->
[email protected]3d7474ff2011-07-27 17:47:37589 browser()->GetWebUIFactory()->IsURLAcceptableForWebUI(browser_context(),
[email protected]1fd1a502011-03-30 16:55:56590 entry.url());
[email protected]c09163a2011-02-15 00:05:55591 CHECK(!BindingsPolicy::is_web_ui_enabled(enabled_bindings) ||
592 is_allowed_in_web_ui_renderer);
[email protected]54ec6472010-04-09 19:39:58593
[email protected]96d185d2009-04-24 03:28:54594 // Tell DevTools agent that it is attached prior to the navigation.
[email protected]d9f9b792009-06-24 13:17:12595 DevToolsManager* devtools_manager = DevToolsManager::GetInstance();
[email protected]7e8e6b62009-05-08 11:28:32596 if (devtools_manager) { // NULL in unit tests.
[email protected]3cc72b12010-03-18 23:03:00597 devtools_manager->OnNavigatingToPendingEntry(render_view_host(),
598 dest_render_view_host,
599 entry.url());
[email protected]7e8e6b62009-05-08 11:28:32600 }
[email protected]96d185d2009-04-24 03:28:54601
602 // Used for page load time metrics.
603 current_load_start_ = base::TimeTicks::Now();
604
605 // Navigate in the desired RenderViewHost.
[email protected]056de2d2009-06-26 16:41:34606 ViewMsg_Navigate_Params navigate_params;
[email protected]6c6b02d2011-09-02 03:36:47607 MakeNavigateParams(entry, controller_, delegate_, reload_type,
608 &navigate_params);
[email protected]056de2d2009-06-26 16:41:34609 dest_render_view_host->Navigate(navigate_params);
[email protected]96d185d2009-04-24 03:28:54610
611 if (entry.page_id() == -1) {
612 // HACK!! This code suppresses javascript: URLs from being added to
613 // session history, which is what we want to do for javascript: URLs that
614 // do not generate content. What we really need is a message from the
615 // renderer telling us that a new page was not created. The same message
616 // could be used for mailto: URLs and the like.
617 if (entry.url().SchemeIs(chrome::kJavaScriptScheme))
618 return false;
619 }
620
[email protected]3c9e1872010-11-18 16:17:49621 // Notify observers about navigation.
[email protected]b375c5d2011-05-03 21:15:04622 FOR_EACH_OBSERVER(TabContentsObserver,
623 observers_,
624 NavigateToPendingEntry(entry.url(), reload_type));
[email protected]96d185d2009-04-24 03:28:54625
[email protected]09b29342011-06-24 19:18:48626 if (delegate_)
627 delegate_->DidNavigateToPendingEntry(this);
628
[email protected]d5f942ba2008-09-26 19:30:34629 return true;
630}
631
[email protected]9e1ad4b2011-08-14 16:49:19632void TabContents::SetHistoryLengthAndPrune(const SiteInstance* site_instance,
633 int history_length,
634 int32 minimum_page_id) {
[email protected]5229253b2011-08-16 14:36:41635 // SetHistoryLengthAndPrune doesn't work when there are pending cross-site
636 // navigations. Callers should ensure that this is the case.
[email protected]9e1ad4b2011-08-14 16:49:19637 if (render_manager_.pending_render_view_host()) {
638 NOTREACHED();
[email protected]796931a92011-08-10 01:32:14639 return;
[email protected]9e1ad4b2011-08-14 16:49:19640 }
641 RenderViewHost* rvh = render_view_host();
642 if (!rvh) {
643 NOTREACHED();
644 return;
645 }
646 if (site_instance && rvh->site_instance() != site_instance) {
647 NOTREACHED();
648 return;
649 }
650 rvh->Send(new ViewMsg_SetHistoryLengthAndPrune(rvh->routing_id(),
651 history_length,
652 minimum_page_id));
[email protected]796931a92011-08-10 01:32:14653}
654
[email protected]96d185d2009-04-24 03:28:54655void TabContents::Stop() {
656 render_manager_.Stop();
[email protected]2e3f4572011-03-25 19:24:47657 FOR_EACH_OBSERVER(TabContentsObserver, observers_, StopNavigation());
[email protected]96d185d2009-04-24 03:28:54658}
659
[email protected]96d185d2009-04-24 03:28:54660TabContents* TabContents::Clone() {
661 // We create a new SiteInstance so that the new tab won't share processes
662 // with the old one. This can be changed in the future if we need it to share
663 // processes for some reason.
[email protected]3d7474ff2011-07-27 17:47:37664 TabContents* tc = new TabContents(
665 browser_context(),
666 SiteInstance::CreateSiteInstance(browser_context()),
667 MSG_ROUTING_NONE, this, NULL);
[email protected]96d185d2009-04-24 03:28:54668 tc->controller().CopyStateFrom(controller_);
669 return tc;
670}
671
[email protected]4d677202009-07-19 07:37:12672void TabContents::ShowPageInfo(const GURL& url,
673 const NavigationEntry::SSLStatus& ssl,
674 bool show_history) {
675 if (!delegate_)
676 return;
677
[email protected]3d7474ff2011-07-27 17:47:37678 delegate_->ShowPageInfo(browser_context(), url, ssl, show_history);
[email protected]4d677202009-07-19 07:37:12679}
680
[email protected]e7cfdbd2011-04-22 14:41:37681void TabContents::AddNewContents(TabContents* new_contents,
682 WindowOpenDisposition disposition,
683 const gfx::Rect& initial_pos,
684 bool user_gesture) {
initial.commit09911bf2008-07-26 23:55:29685 if (!delegate_)
686 return;
687
[email protected]e7cfdbd2011-04-22 14:41:37688 delegate_->AddNewContents(this, new_contents, disposition, initial_pos,
689 user_gesture);
[email protected]96d185d2009-04-24 03:28:54690}
691
[email protected]5c9e97a2009-09-09 23:48:30692gfx::NativeView TabContents::GetContentNativeView() const {
[email protected]96d185d2009-04-24 03:28:54693 return view_->GetContentNativeView();
694}
695
696gfx::NativeView TabContents::GetNativeView() const {
697 return view_->GetNativeView();
698}
699
700void TabContents::GetContainerBounds(gfx::Rect *out) const {
701 view_->GetContainerBounds(out);
702}
703
704void TabContents::Focus() {
705 view_->Focus();
706}
707
[email protected]90daadb42009-06-08 21:27:28708void TabContents::FocusThroughTabTraversal(bool reverse) {
[email protected]7e383692009-06-12 19:14:54709 if (showing_interstitial_page()) {
710 render_manager_.interstitial_page()->FocusThroughTabTraversal(reverse);
711 return;
712 }
[email protected]96d185d2009-04-24 03:28:54713 render_view_host()->SetInitialFocus(reverse);
714}
715
716bool TabContents::FocusLocationBarByDefault() {
[email protected]d0980792011-02-13 19:41:40717 WebUI* web_ui = GetWebUIForCurrentState();
[email protected]7ade2732011-02-10 00:13:58718 if (web_ui)
719 return web_ui->focus_location_bar_by_default();
[email protected]3e3f0eb2009-06-22 18:33:43720 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]e0d481582009-09-15 21:06:25721 if (entry && entry->url() == GURL(chrome::kAboutBlankURL))
[email protected]3e3f0eb2009-06-22 18:33:43722 return true;
[email protected]96d185d2009-04-24 03:28:54723 return false;
724}
725
[email protected]a26dc362010-04-23 01:48:58726void TabContents::SetFocusToLocationBar(bool select_all) {
[email protected]a11aaf22010-03-30 00:03:38727 if (delegate())
[email protected]a26dc362010-04-23 01:48:58728 delegate()->SetFocusToLocationBar(select_all);
[email protected]a11aaf22010-03-30 00:03:38729}
730
[email protected]686493142011-07-15 21:47:22731bool TabContents::CanDownload(int request_id) {
732 TabContentsDelegate* d = delegate();
733 if (d)
734 return d->CanDownload(this, request_id);
735 return true;
736}
737
738void TabContents::OnStartDownload(DownloadItem* download) {
739 TabContentsDelegate* d = delegate();
740 if (d)
741 d->OnStartDownload(this, download);
742}
743
[email protected]c7dd2f62011-07-18 15:57:59744void TabContents::OnSavePage() {
745 // If we can not save the page, try to download it.
746 if (!SavePackage::IsSavableContents(contents_mime_type())) {
[email protected]3d7474ff2011-07-27 17:47:37747 DownloadManager* dlm = browser_context()->GetDownloadManager();
[email protected]c7dd2f62011-07-18 15:57:59748 const GURL& current_page_url = GetURL();
749 if (dlm && current_page_url.is_valid()) {
750 dlm->DownloadUrl(current_page_url, GURL(), "", this);
[email protected]8bd9e562011-08-16 23:55:46751 download_stats::RecordDownloadCount(
752 download_stats::INITIATED_BY_SAVE_PACKAGE_FAILURE_COUNT);
[email protected]c7dd2f62011-07-18 15:57:59753 return;
754 }
755 }
756
757 Stop();
758
759 // Create the save package and possibly prompt the user for the name to save
760 // the page as. The user prompt is an asynchronous operation that runs on
761 // another thread.
762 save_package_ = new SavePackage(this);
763 save_package_->GetSaveInfo();
764}
765
766// Used in automated testing to bypass prompting the user for file names.
767// Instead, the names and paths are hard coded rather than running them through
768// file name sanitation and extension / mime checking.
769bool TabContents::SavePage(const FilePath& main_file, const FilePath& dir_path,
770 SavePackage::SavePackageType save_type) {
771 // Stop the page from navigating.
772 Stop();
773
774 save_package_ = new SavePackage(this, save_type, main_file, dir_path);
775 return save_package_->Init();
776}
777
778void TabContents::OnSaveURL(const GURL& url) {
[email protected]3d7474ff2011-07-27 17:47:37779 DownloadManager* dlm = browser_context()->GetDownloadManager();
[email protected]c7dd2f62011-07-18 15:57:59780 dlm->DownloadUrl(url, GetURL(), "", this);
781}
782
[email protected]420ae012009-04-24 05:16:32783bool TabContents::IsActiveEntry(int32 page_id) {
784 NavigationEntry* active_entry = controller_.GetActiveEntry();
785 return (active_entry != NULL &&
786 active_entry->site_instance() == GetSiteInstance() &&
787 active_entry->page_id() == page_id);
788}
789
[email protected]b2fe07d12010-02-09 14:38:08790void TabContents::SetOverrideEncoding(const std::string& encoding) {
[email protected]8cb5d5b2010-02-09 11:36:16791 set_encoding(encoding);
[email protected]216813952011-05-19 22:21:26792 render_view_host()->Send(new ViewMsg_SetPageEncoding(
793 render_view_host()->routing_id(), encoding));
[email protected]8cb5d5b2010-02-09 11:36:16794}
795
[email protected]b2fe07d12010-02-09 14:38:08796void TabContents::ResetOverrideEncoding() {
[email protected]8cb5d5b2010-02-09 11:36:16797 reset_encoding();
[email protected]216813952011-05-19 22:21:26798 render_view_host()->Send(new ViewMsg_ResetPageEncodingToDefault(
799 render_view_host()->routing_id()));
[email protected]8cb5d5b2010-02-09 11:36:16800}
801
[email protected]7ab1e7d62009-10-14 23:32:01802void TabContents::OnCloseStarted() {
803 if (tab_close_start_time_.is_null())
804 tab_close_start_time_ = base::TimeTicks::Now();
805}
806
[email protected]46624bf2010-06-09 16:04:19807bool TabContents::ShouldAcceptDragAndDrop() const {
808#if defined(OS_CHROMEOS)
809 // ChromeOS panels (pop-ups) do not take drag-n-drop.
810 // See http://crosbug.com/2413
[email protected]b35b26b32011-05-05 20:35:14811 if (delegate() && delegate()->IsPopupOrPanel(this))
[email protected]b83af492010-10-09 03:21:20812 return false;
813 return true;
[email protected]46624bf2010-06-09 16:04:19814#else
815 return true;
816#endif
817}
818
[email protected]7813bd72011-02-05 02:19:34819void TabContents::SystemDragEnded() {
820 if (render_view_host())
821 render_view_host()->DragSourceSystemDragEnded();
822 if (delegate())
823 delegate()->DragEnded();
824}
825
[email protected]d0b8d092010-10-25 04:05:17826double TabContents::GetZoomLevel() const {
[email protected]3d7474ff2011-07-27 17:47:37827 HostZoomMap* zoom_map = browser_context()->GetHostZoomMap();
[email protected]b75b8292010-10-01 07:28:25828 if (!zoom_map)
[email protected]d0b8d092010-10-25 04:05:17829 return 0;
[email protected]b75b8292010-10-01 07:28:25830
831 double zoom_level;
832 if (temporary_zoom_settings_) {
833 zoom_level = zoom_map->GetTemporaryZoomLevel(
834 GetRenderProcessHost()->id(), render_view_host()->routing_id());
835 } else {
[email protected]13ffa32e2011-05-27 16:37:01836 zoom_level = zoom_map->GetZoomLevel(net::GetHostOrSpecFromURL(GetURL()));
[email protected]b75b8292010-10-01 07:28:25837 }
[email protected]d0b8d092010-10-25 04:05:17838 return zoom_level;
839}
[email protected]b75b8292010-10-01 07:28:25840
[email protected]d0b8d092010-10-25 04:05:17841int TabContents::GetZoomPercent(bool* enable_increment,
842 bool* enable_decrement) {
843 *enable_decrement = *enable_increment = false;
[email protected]b75b8292010-10-01 07:28:25844 int percent = static_cast<int>(
[email protected]d0b8d092010-10-25 04:05:17845 WebKit::WebView::zoomLevelToZoomFactor(GetZoomLevel()) * 100);
[email protected]b75b8292010-10-01 07:28:25846 *enable_decrement = percent > minimum_zoom_percent_;
847 *enable_increment = percent < maximum_zoom_percent_;
848 return percent;
849}
850
[email protected]3c733bde2010-12-21 19:56:31851void TabContents::ViewSource() {
[email protected]1788e772010-12-15 16:40:50852 if (!delegate_)
853 return;
854
855 NavigationEntry* active_entry = controller().GetActiveEntry();
856 if (!active_entry)
857 return;
858
859 delegate_->ViewSourceForTab(this, active_entry->url());
[email protected]77d8d622010-12-15 10:30:12860}
861
[email protected]932b7a12011-03-09 12:50:27862void TabContents::ViewFrameSource(const GURL& url,
863 const std::string& content_state) {
864 if (!delegate_)
865 return;
866
867 delegate_->ViewSourceForFrame(this, url, content_state);
868}
869
[email protected]c40d6232011-03-25 00:16:21870void TabContents::SetContentRestrictions(int restrictions) {
871 content_restrictions_ = restrictions;
872 delegate()->ContentRestrictionsChanged(this);
873}
874
[email protected]724159a2010-12-30 01:11:18875void TabContents::OnDidStartProvisionalLoadForFrame(int64 frame_id,
876 bool is_main_frame,
[email protected]eacb080b2011-05-22 19:40:26877 bool has_opener_set,
[email protected]724159a2010-12-30 01:11:18878 const GURL& url) {
879 bool is_error_page = (url.spec() == chrome::kUnreachableWebDataURL);
880 GURL validated_url(url);
881 render_view_host()->FilterURL(ChildProcessSecurityPolicy::GetInstance(),
882 GetRenderProcessHost()->id(), &validated_url);
883
[email protected]8093a542011-05-13 07:29:32884 RenderViewHost* rvh =
885 render_manager_.pending_render_view_host() ?
886 render_manager_.pending_render_view_host() : render_view_host();
[email protected]0d60f0192011-04-14 12:40:10887 // Notify observers about the start of the provisional load.
888 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
889 DidStartProvisionalLoadForFrame(frame_id, is_main_frame,
[email protected]8093a542011-05-13 07:29:32890 validated_url, is_error_page, rvh));
[email protected]0d60f0192011-04-14 12:40:10891
[email protected]724159a2010-12-30 01:11:18892 if (is_main_frame) {
[email protected]4850a7f2011-03-08 23:36:59893 // Notify observers about the provisional change in the main frame URL.
894 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
[email protected]eacb080b2011-05-22 19:40:26895 ProvisionalChangeToMainFrameUrl(url, has_opener_set));
[email protected]724159a2010-12-30 01:11:18896 }
897}
898
899void TabContents::OnDidRedirectProvisionalLoad(int32 page_id,
[email protected]eacb080b2011-05-22 19:40:26900 bool has_opener_set,
[email protected]724159a2010-12-30 01:11:18901 const GURL& source_url,
902 const GURL& target_url) {
[email protected]18ba2b12011-04-06 16:35:49903 // TODO(creis): Remove this method and have the pre-rendering code listen to
904 // the ResourceDispatcherHost's RESOURCE_RECEIVED_REDIRECT notification
905 // instead. See http://crbug.com/78512.
[email protected]724159a2010-12-30 01:11:18906 NavigationEntry* entry;
907 if (page_id == -1)
908 entry = controller_.pending_entry();
909 else
910 entry = controller_.GetEntryWithPageID(GetSiteInstance(), page_id);
911 if (!entry || entry->url() != source_url)
912 return;
[email protected]e7d50892011-01-19 21:47:38913
[email protected]4850a7f2011-03-08 23:36:59914 // Notify observers about the provisional change in the main frame URL.
915 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
[email protected]eacb080b2011-05-22 19:40:26916 ProvisionalChangeToMainFrameUrl(target_url,
917 has_opener_set));
[email protected]724159a2010-12-30 01:11:18918}
919
920void TabContents::OnDidFailProvisionalLoadWithError(
921 int64 frame_id,
922 bool is_main_frame,
923 int error_code,
924 const GURL& url,
925 bool showing_repost_interstitial) {
926 VLOG(1) << "Failed Provisional Load: " << url.possibly_invalid_spec()
927 << ", error_code: " << error_code
928 << " is_main_frame: " << is_main_frame
929 << " showing_repost_interstitial: " << showing_repost_interstitial
930 << " frame_id: " << frame_id;
931 GURL validated_url(url);
932 render_view_host()->FilterURL(ChildProcessSecurityPolicy::GetInstance(),
933 GetRenderProcessHost()->id(), &validated_url);
934
935 if (net::ERR_ABORTED == error_code) {
936 // EVIL HACK ALERT! Ignore failed loads when we're showing interstitials.
937 // This means that the interstitial won't be torn down properly, which is
938 // bad. But if we have an interstitial, go back to another tab type, and
939 // then load the same interstitial again, we could end up getting the first
940 // interstitial's "failed" message (as a result of the cancel) when we're on
941 // the second one.
942 //
943 // We can't tell this apart, so we think we're tearing down the current page
944 // which will cause a crash later one. There is also some code in
945 // RenderViewHostManager::RendererAbortedProvisionalLoad that is commented
946 // out because of this problem.
947 //
948 // http://code.google.com/p/chromium/issues/detail?id=2855
949 // Because this will not tear down the interstitial properly, if "back" is
950 // back to another tab type, the interstitial will still be somewhat alive
951 // in the previous tab type. If you navigate somewhere that activates the
952 // tab with the interstitial again, you'll see a flash before the new load
953 // commits of the interstitial page.
954 if (showing_interstitial_page()) {
955 LOG(WARNING) << "Discarding message during interstitial.";
956 return;
957 }
958
[email protected]02102f82011-06-13 20:37:02959 // Discard our pending entry if the load canceled (e.g. if we decided to
960 // download the file instead of load it). We do not verify that the URL
961 // being canceled matches the pending entry's URL because they will not
962 // match if a redirect occurred (in which case we do not want to leave a
963 // stale redirect URL showing). This means that we also cancel the pending
964 // entry if the user started a new navigation. As a result, the navigation
965 // controller may not remember that a load is in progress, but the
966 // navigation will still commit even if there is no pending entry.
[email protected]724159a2010-12-30 01:11:18967 NavigationEntry* pending_entry = controller_.pending_entry();
[email protected]02102f82011-06-13 20:37:02968 if (pending_entry)
[email protected]c95fa8b2011-04-28 20:26:16969 DidCancelLoading();
[email protected]724159a2010-12-30 01:11:18970
971 render_manager_.RendererAbortedProvisionalLoad(render_view_host());
972 }
973
974 // Send out a notification that we failed a provisional load with an error.
975 ProvisionalLoadDetails details(
976 is_main_frame, controller_.IsURLInPageNavigation(validated_url),
[email protected]b9e8ea62011-03-04 06:29:09977 validated_url, std::string(), false, frame_id);
[email protected]724159a2010-12-30 01:11:18978 details.set_error_code(error_code);
979
980 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:27981 content::NOTIFICATION_FAIL_PROVISIONAL_LOAD_WITH_ERROR,
[email protected]724159a2010-12-30 01:11:18982 Source<NavigationController>(&controller_),
983 Details<ProvisionalLoadDetails>(&details));
[email protected]0d60f0192011-04-14 12:40:10984
985 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
986 DidFailProvisionalLoad(frame_id, is_main_frame,
987 validated_url, error_code));
[email protected]724159a2010-12-30 01:11:18988}
989
990void TabContents::OnDidLoadResourceFromMemoryCache(
991 const GURL& url,
[email protected]70435962011-08-02 20:13:28992 const std::string& security_info,
993 const std::string& http_method,
994 ResourceType::Type resource_type) {
[email protected]28620862011-03-22 23:07:19995 base::StatsCounter cache("WebKit.CacheHit");
[email protected]724159a2010-12-30 01:11:18996 cache.Increment();
997
998 // Send out a notification that we loaded a resource from our memory cache.
[email protected]70d66502011-09-23 00:55:08999 int cert_id = 0;
1000 net::CertStatus cert_status = 0;
1001 int security_bits = -1;
1002 int connection_status = 0;
[email protected]724159a2010-12-30 01:11:181003 SSLManager::DeserializeSecurityInfo(security_info,
1004 &cert_id, &cert_status,
1005 &security_bits,
1006 &connection_status);
[email protected]7a8c55e2011-02-15 08:21:161007 LoadFromMemoryCacheDetails details(url, GetRenderProcessHost()->id(),
1008 cert_id, cert_status);
[email protected]724159a2010-12-30 01:11:181009
1010 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271011 content::NOTIFICATION_LOAD_FROM_MEMORY_CACHE,
[email protected]724159a2010-12-30 01:11:181012 Source<NavigationController>(&controller_),
1013 Details<LoadFromMemoryCacheDetails>(&details));
1014}
1015
1016void TabContents::OnDidDisplayInsecureContent() {
[email protected]221a5d92011-04-28 18:23:161017 UserMetrics::RecordAction(UserMetricsAction("SSL.DisplayedInsecureContent"));
[email protected]724159a2010-12-30 01:11:181018 displayed_insecure_content_ = true;
[email protected]9678d8f2011-08-05 04:05:071019 SSLManager::NotifySSLInternalStateChanged(&controller());
[email protected]724159a2010-12-30 01:11:181020}
1021
1022void TabContents::OnDidRunInsecureContent(
[email protected]92771112011-01-20 00:13:021023 const std::string& security_origin, const GURL& target_url) {
[email protected]9941cf52011-02-08 16:17:231024 LOG(INFO) << security_origin << " ran insecure content from "
1025 << target_url.possibly_invalid_spec();
[email protected]221a5d92011-04-28 18:23:161026 UserMetrics::RecordAction(UserMetricsAction("SSL.RanInsecureContent"));
[email protected]ca406032011-07-19 21:53:051027 if (EndsWith(security_origin, kDotGoogleDotCom, false)) {
1028 UserMetrics::RecordAction(
1029 UserMetricsAction("SSL.RanInsecureContentGoogle"));
1030 }
[email protected]724159a2010-12-30 01:11:181031 controller_.ssl_manager()->DidRunInsecureContent(security_origin);
[email protected]f2b58c4d2011-06-01 23:29:411032 displayed_insecure_content_ = true;
[email protected]9678d8f2011-08-05 04:05:071033 SSLManager::NotifySSLInternalStateChanged(&controller());
[email protected]724159a2010-12-30 01:11:181034}
1035
1036void TabContents::OnDocumentLoadedInFrame(int64 frame_id) {
1037 controller_.DocumentLoadedInFrame();
[email protected]0d60f0192011-04-14 12:40:101038 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
1039 DocumentLoadedInFrame(frame_id));
[email protected]724159a2010-12-30 01:11:181040}
1041
1042void TabContents::OnDidFinishLoad(int64 frame_id) {
[email protected]0d60f0192011-04-14 12:40:101043 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
1044 DidFinishLoad(frame_id));
[email protected]724159a2010-12-30 01:11:181045}
1046
[email protected]c8f73ab2011-01-22 00:05:171047void TabContents::OnUpdateContentRestrictions(int restrictions) {
[email protected]c40d6232011-03-25 00:16:211048 SetContentRestrictions(restrictions);
[email protected]c8f73ab2011-01-22 00:05:171049}
1050
[email protected]216813952011-05-19 22:21:261051void TabContents::OnGoToEntryAtOffset(int offset) {
1052 if (!delegate_ || delegate_->OnGoToEntryOffset(offset)) {
1053 NavigationEntry* entry = controller_.GetEntryAtOffset(offset);
1054 if (!entry)
1055 return;
1056 // Note that we don't call NavigationController::GotToOffset() as we don't
1057 // want to create a pending navigation entry (it might end up lingering
1058 // http://crbug.com/51680).
1059 entry->set_transition_type(entry->transition_type() |
1060 PageTransition::FORWARD_BACK);
1061 NavigateToEntry(*entry, NavigationController::NO_RELOAD);
[email protected]a13cc362011-07-28 21:29:571062
1063 // If the entry is being restored and doesn't have a SiteInstance yet, fill
1064 // it in now that we know. This allows us to find the entry when it commits.
1065 if (!entry->site_instance() &&
1066 entry->restore_type() != NavigationEntry::RESTORE_NONE) {
[email protected]77362eb2011-08-01 17:18:381067 entry->set_site_instance(GetPendingSiteInstance());
[email protected]a13cc362011-07-28 21:29:571068 }
[email protected]216813952011-05-19 22:21:261069 }
1070}
1071
1072void TabContents::OnUpdateZoomLimits(int minimum_percent,
1073 int maximum_percent,
1074 bool remember) {
1075 minimum_zoom_percent_ = minimum_percent;
1076 maximum_zoom_percent_ = maximum_percent;
1077 temporary_zoom_settings_ = !remember;
1078}
1079
1080void TabContents::OnFocusedNodeChanged(bool is_editable_node) {
1081 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271082 content::NOTIFICATION_FOCUS_CHANGED_IN_PAGE,
[email protected]216813952011-05-19 22:21:261083 Source<TabContents>(this),
1084 Details<const bool>(&is_editable_node));
1085}
1086
[email protected]3a29a6e2011-08-24 18:26:211087void TabContents::OnEnumerateDirectory(int request_id,
1088 const FilePath& path) {
1089 delegate()->EnumerateDirectory(this, request_id, path);
1090}
1091
[email protected]7d189022011-08-25 22:54:201092void TabContents::OnJSOutOfMemory() {
1093 delegate()->JSOutOfMemory(this);
1094}
1095
1096void TabContents::OnRegisterProtocolHandler(const std::string& protocol,
1097 const GURL& url,
1098 const string16& title) {
1099 delegate()->RegisterProtocolHandler(this, protocol, url, title);
1100}
1101
1102void TabContents::OnRegisterIntentHandler(const string16& action,
1103 const string16& type,
1104 const string16& href,
1105 const string16& title) {
1106 delegate()->RegisterIntentHandler(this, action, type, href, title);
1107}
1108
1109void TabContents::OnWebIntentDispatch(const IPC::Message& message,
[email protected]ce9751942011-09-21 01:57:241110 const webkit_glue::WebIntentData& intent,
[email protected]7d189022011-08-25 22:54:201111 int intent_id) {
[email protected]ce9751942011-09-21 01:57:241112 delegate()->WebIntentDispatch(this, message.routing_id(), intent, intent_id);
[email protected]7d189022011-08-25 22:54:201113}
1114
[email protected]b888919c2011-09-02 00:32:161115void TabContents::OnFindReply(int request_id,
1116 int number_of_matches,
1117 const gfx::Rect& selection_rect,
1118 int active_match_ordinal,
1119 bool final_update) {
1120 delegate()->FindReply(this, request_id, number_of_matches, selection_rect,
1121 active_match_ordinal, final_update);
[email protected]4e6239c2011-09-23 00:11:001122 // Send a notification to the renderer that we are ready to receive more
1123 // results from the scoping effort of the Find operation. The FindInPage
1124 // scoping is asynchronous and periodically sends results back up to the
1125 // browser using IPC. In an effort to not spam the browser we have the
1126 // browser send an ACK for each FindReply message and have the renderer
1127 // queue up the latest status message while waiting for this ACK.
1128 render_view_host()->Send(
1129 new ViewMsg_FindReplyACK(render_view_host()->routing_id()));
[email protected]b888919c2011-09-02 00:32:161130}
1131
[email protected]d952a052011-09-06 18:42:451132void TabContents::OnCrashedPlugin(const FilePath& plugin_path) {
1133 delegate()->CrashedPlugin(this, plugin_path);
1134}
1135
[email protected]7fc4bbb2011-09-08 21:23:101136void TabContents::OnAppCacheAccessed(const GURL& manifest_url,
1137 bool blocked_by_policy) {
1138 // Notify observers about navigation.
1139 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
1140 AppCacheAccessed(manifest_url, blocked_by_policy));
1141}
1142
[email protected]96d185d2009-04-24 03:28:541143// Notifies the RenderWidgetHost instance about the fact that the page is
1144// loading, or done loading and calls the base implementation.
1145void TabContents::SetIsLoading(bool is_loading,
1146 LoadNotificationDetails* details) {
1147 if (is_loading == is_loading_)
1148 return;
1149
1150 if (!is_loading) {
[email protected]9c235f042011-08-10 22:28:211151 load_state_ = net::LoadStateWithParam(net::LOAD_STATE_IDLE, string16());
[email protected]96d185d2009-04-24 03:28:541152 load_state_host_.clear();
[email protected]094e5b22009-09-25 04:23:561153 upload_size_ = 0;
1154 upload_position_ = 0;
[email protected]96d185d2009-04-24 03:28:541155 }
1156
1157 render_manager_.SetIsLoading(is_loading);
1158
1159 is_loading_ = is_loading;
1160 waiting_for_response_ = is_loading;
1161
[email protected]6ebdc9b2010-09-27 16:55:571162 if (delegate_)
1163 delegate_->LoadingStateChanged(this);
[email protected]329581b2009-04-28 06:52:351164 NotifyNavigationStateChanged(INVALIDATE_LOAD);
[email protected]96d185d2009-04-24 03:28:541165
[email protected]432115822011-07-10 15:52:271166 int type = is_loading ? content::NOTIFICATION_LOAD_START :
1167 content::NOTIFICATION_LOAD_STOP;
[email protected]ce5c4502009-05-06 16:46:111168 NotificationDetails det = NotificationService::NoDetails();
[email protected]96d185d2009-04-24 03:28:541169 if (details)
1170 det = Details<LoadNotificationDetails>(details);
1171 NotificationService::current()->Notify(type,
1172 Source<NavigationController>(&controller_),
1173 det);
1174}
1175
[email protected]d0980792011-02-13 19:41:401176WebUI* TabContents::GetWebUIForCurrentState() {
[email protected]7ade2732011-02-10 00:13:581177 // When there is a pending navigation entry, we want to use the pending WebUI
[email protected]96d185d2009-04-24 03:28:541178 // that goes along with it to control the basic flags. For example, we want to
1179 // show the pending URL in the URL bar, so we want the display_url flag to
1180 // be from the pending entry.
1181 //
1182 // The confusion comes because there are multiple possibilities for the
1183 // initial load in a tab as a side effect of the way the RenderViewHostManager
1184 // works.
1185 //
[email protected]e0112912011-02-02 22:54:351186 // - For the very first tab the load looks "normal". The new tab Web UI is
[email protected]96d185d2009-04-24 03:28:541187 // the pending one, and we want it to apply here.
1188 //
1189 // - For subsequent new tabs, they'll get a new SiteInstance which will then
1190 // get switched to the one previously associated with the new tab pages.
[email protected]7ade2732011-02-10 00:13:581191 // This switching will cause the manager to commit the RVH/WebUI. So we'll
[email protected]e0112912011-02-02 22:54:351192 // have a committed Web UI in this case.
[email protected]96d185d2009-04-24 03:28:541193 //
1194 // This condition handles all of these cases:
1195 //
1196 // - First load in first tab: no committed nav entry + pending nav entry +
1197 // pending dom ui:
[email protected]e0112912011-02-02 22:54:351198 // -> Use pending Web UI if any.
[email protected]96d185d2009-04-24 03:28:541199 //
1200 // - First load in second tab: no committed nav entry + pending nav entry +
[email protected]e0112912011-02-02 22:54:351201 // no pending Web UI:
1202 // -> Use the committed Web UI if any.
[email protected]96d185d2009-04-24 03:28:541203 //
1204 // - Second navigation in any tab: committed nav entry + pending nav entry:
[email protected]e0112912011-02-02 22:54:351205 // -> Use pending Web UI if any.
[email protected]96d185d2009-04-24 03:28:541206 //
1207 // - Normal state with no load: committed nav entry + no pending nav entry:
[email protected]e0112912011-02-02 22:54:351208 // -> Use committed Web UI.
[email protected]96d185d2009-04-24 03:28:541209 if (controller_.pending_entry() &&
1210 (controller_.GetLastCommittedEntry() ||
[email protected]7ade2732011-02-10 00:13:581211 render_manager_.pending_web_ui()))
1212 return render_manager_.pending_web_ui();
1213 return render_manager_.web_ui();
[email protected]96d185d2009-04-24 03:28:541214}
[email protected]420ae012009-04-24 05:16:321215
[email protected]1fd1a502011-03-30 16:55:561216WebUI::TypeID TabContents::GetWebUITypeForCurrentState() {
[email protected]3d7474ff2011-07-27 17:47:371217 return content::WebUIFactory::Get()->GetWebUIType(browser_context(),
1218 GetURL());
[email protected]1fd1a502011-03-30 16:55:561219}
1220
[email protected]420ae012009-04-24 05:16:321221void TabContents::DidNavigateMainFramePostCommit(
[email protected]8286f51a2011-05-31 17:39:131222 const content::LoadCommittedDetails& details,
[email protected]420ae012009-04-24 05:16:321223 const ViewHostMsg_FrameNavigate_Params& params) {
[email protected]1fd1a502011-03-30 16:55:561224 if (opener_web_ui_type_ != WebUI::kNoWebUI) {
[email protected]80a8fad2011-01-29 04:02:381225 // If this is a window.open navigation, use the same WebUI as the renderer
[email protected]c2e74fe82009-09-03 17:57:441226 // that opened the window, as long as both renderers have the same
1227 // privileges.
[email protected]1fd1a502011-03-30 16:55:561228 if (delegate_ && opener_web_ui_type_ == GetWebUITypeForCurrentState()) {
1229 WebUI* web_ui = content::GetContentClient()->browser()->
1230 GetWebUIFactory()->CreateWebUIForURL(this, GetURL());
[email protected]7ade2732011-02-10 00:13:581231 // web_ui might be NULL if the URL refers to a non-existent extension.
1232 if (web_ui) {
[email protected]d0980792011-02-13 19:41:401233 render_manager_.SetWebUIPostCommit(web_ui);
[email protected]7ade2732011-02-10 00:13:581234 web_ui->RenderViewCreated(render_view_host());
[email protected]c2e74fe82009-09-03 17:57:441235 }
1236 }
[email protected]1fd1a502011-03-30 16:55:561237 opener_web_ui_type_ = WebUI::kNoWebUI;
[email protected]c2e74fe82009-09-03 17:57:441238 }
1239
[email protected]4e697b042011-07-08 06:44:561240 if (details.is_navigation_to_different_page()) {
[email protected]420ae012009-04-24 05:16:321241 // Clear the status bubble. This is a workaround for a bug where WebKit
1242 // doesn't let us know that the cursor left an element during a
1243 // transition (this is also why the mouse cursor remains as a hand after
1244 // clicking on a link); see bugs 1184641 and 980803. We don't want to
1245 // clear the bubble when a user navigates to a named anchor in the same
1246 // page.
1247 UpdateTargetURL(details.entry->page_id(), GURL());
[email protected]420ae012009-04-24 05:16:321248 }
1249
[email protected]a6e82fc2010-02-24 22:28:141250 if (!details.is_in_page) {
[email protected]f17a0ee2010-05-17 17:38:471251 // Once the main frame is navigated, we're no longer considered to have
1252 // displayed insecure content.
1253 displayed_insecure_content_ = false;
[email protected]aece2c7f2009-08-27 20:43:171254 }
[email protected]ce5c4502009-05-06 16:46:111255
[email protected]3c9e1872010-11-18 16:17:491256 // Notify observers about navigation.
[email protected]585b30362011-01-28 02:30:171257 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
[email protected]3c9e1872010-11-18 16:17:491258 DidNavigateMainFramePostCommit(details, params));
[email protected]420ae012009-04-24 05:16:321259}
1260
1261void TabContents::DidNavigateAnyFramePostCommit(
1262 RenderViewHost* render_view_host,
[email protected]8286f51a2011-05-31 17:39:131263 const content::LoadCommittedDetails& details,
[email protected]420ae012009-04-24 05:16:321264 const ViewHostMsg_FrameNavigate_Params& params) {
[email protected]3ab9cb82011-06-03 18:02:071265 // If we navigate, reset JavaScript state. This does nothing to prevent
[email protected]420ae012009-04-24 05:16:321266 // a malicious script from spamming messages, since the script could just
1267 // reload the page to stop blocking.
[email protected]2e5b90c2011-08-16 21:11:551268 if (dialog_creator_) {
1269 dialog_creator_->ResetJavaScriptState(this);
1270 dialog_creator_ = NULL;
1271 }
[email protected]420ae012009-04-24 05:16:321272
[email protected]3c9e1872010-11-18 16:17:491273 // Notify observers about navigation.
[email protected]585b30362011-01-28 02:30:171274 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
[email protected]3c9e1872010-11-18 16:17:491275 DidNavigateAnyFramePostCommit(details, params));
[email protected]420ae012009-04-24 05:16:321276}
1277
[email protected]420ae012009-04-24 05:16:321278void TabContents::UpdateMaxPageIDIfNecessary(SiteInstance* site_instance,
1279 RenderViewHost* rvh) {
1280 // If we are creating a RVH for a restored controller, then we might
1281 // have more page IDs than the SiteInstance's current max page ID. We must
1282 // make sure that the max page ID is larger than any restored page ID.
1283 // Note that it is ok for conflicting page IDs to exist in another tab
1284 // (i.e., NavigationController), but if any page ID is larger than the max,
1285 // the back/forward list will get confused.
1286 int max_restored_page_id = controller_.max_restored_page_id();
1287 if (max_restored_page_id > 0) {
1288 int curr_max_page_id = site_instance->max_page_id();
1289 if (max_restored_page_id > curr_max_page_id) {
1290 // Need to update the site instance immediately.
1291 site_instance->UpdateMaxPageID(max_restored_page_id);
1292
1293 // Also tell the renderer to update its internal representation. We
1294 // need to reserve enough IDs to make all restored page IDs less than
1295 // the max.
1296 if (curr_max_page_id < 0)
1297 curr_max_page_id = 0;
[email protected]216813952011-05-19 22:21:261298 rvh->Send(new ViewMsg_ReservePageIDRange(
1299 rvh->routing_id(), max_restored_page_id - curr_max_page_id));
[email protected]420ae012009-04-24 05:16:321300 }
1301 }
1302}
1303
[email protected]6b2f7a82011-04-25 19:30:511304bool TabContents::UpdateTitleForEntry(NavigationEntry* entry,
[email protected]acafd272011-07-26 17:35:571305 const string16& title) {
[email protected]420ae012009-04-24 05:16:321306 // For file URLs without a title, use the pathname instead. In the case of a
1307 // synthesized title, we don't want the update to count toward the "one set
1308 // per page of the title to history."
[email protected]6b2f7a82011-04-25 19:30:511309 string16 final_title;
[email protected]420ae012009-04-24 05:16:321310 bool explicit_set;
[email protected]987fc3a2011-05-26 14:18:091311 if (entry && entry->url().SchemeIsFile() && title.empty()) {
[email protected]6b2f7a82011-04-25 19:30:511312 final_title = UTF8ToUTF16(entry->url().ExtractFileName());
[email protected]420ae012009-04-24 05:16:321313 explicit_set = false; // Don't count synthetic titles toward the set limit.
1314 } else {
[email protected]acafd272011-07-26 17:35:571315 TrimWhitespace(title, TRIM_ALL, &final_title);
[email protected]420ae012009-04-24 05:16:321316 explicit_set = true;
1317 }
1318
[email protected]987fc3a2011-05-26 14:18:091319 // If a page is created via window.open and never navigated,
1320 // there will be no navigation entry. In this situation,
1321 // |page_title_when_no_navigaiton_entry_| will be used for page title.
1322 if (entry) {
1323 if (final_title == entry->title())
1324 return false; // Nothing changed, don't bother.
[email protected]420ae012009-04-24 05:16:321325
[email protected]987fc3a2011-05-26 14:18:091326 entry->set_title(final_title);
1327 } else {
1328 if (page_title_when_no_navigation_entry_ == final_title)
1329 return false; // Nothing changed, don't bother.
1330
1331 page_title_when_no_navigation_entry_ = final_title;
1332 }
[email protected]420ae012009-04-24 05:16:321333
[email protected]420ae012009-04-24 05:16:321334 // Lastly, set the title for the view.
[email protected]790e2fd2011-09-21 20:28:231335 view_->SetPageTitle(final_title);
[email protected]420ae012009-04-24 05:16:321336
[email protected]105bb0f2011-05-24 17:12:141337 TitleUpdatedDetails details(entry, explicit_set);
1338
[email protected]cbc0e1b2010-04-12 18:33:041339 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271340 content::NOTIFICATION_TAB_CONTENTS_TITLE_UPDATED,
[email protected]cbc0e1b2010-04-12 18:33:041341 Source<TabContents>(this),
[email protected]105bb0f2011-05-24 17:12:141342 Details<TitleUpdatedDetails>(&details));
[email protected]cbc0e1b2010-04-12 18:33:041343
[email protected]420ae012009-04-24 05:16:321344 return true;
1345}
1346
1347void TabContents::NotifySwapped() {
1348 // After sending out a swap notification, we need to send a disconnect
1349 // notification so that clients that pick up a pointer to |this| can NULL the
1350 // pointer. See Bug 1230284.
1351 notify_disconnection_ = true;
1352 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271353 content::NOTIFICATION_TAB_CONTENTS_SWAPPED,
[email protected]d82ed61e2009-06-16 02:46:221354 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:321355 NotificationService::NoDetails());
1356}
1357
1358void TabContents::NotifyConnected() {
1359 notify_disconnection_ = true;
1360 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271361 content::NOTIFICATION_TAB_CONTENTS_CONNECTED,
[email protected]d82ed61e2009-06-16 02:46:221362 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:321363 NotificationService::NoDetails());
1364}
1365
1366void TabContents::NotifyDisconnected() {
1367 if (!notify_disconnection_)
1368 return;
1369
1370 notify_disconnection_ = false;
1371 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271372 content::NOTIFICATION_TAB_CONTENTS_DISCONNECTED,
[email protected]d82ed61e2009-06-16 02:46:221373 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:321374 NotificationService::NoDetails());
1375}
1376
[email protected]8d3347f2009-07-09 22:00:211377RenderViewHostDelegate::View* TabContents::GetViewDelegate() {
[email protected]420ae012009-04-24 05:16:321378 return view_.get();
1379}
1380
[email protected]8d3347f2009-07-09 22:00:211381RenderViewHostDelegate::RendererManagement*
1382TabContents::GetRendererManagementDelegate() {
1383 return &render_manager_;
1384}
1385
[email protected]3d7474ff2011-07-27 17:47:371386RendererPreferences TabContents::GetRendererPrefs(
1387 content::BrowserContext* browser_context) const {
[email protected]840b1512009-07-21 16:53:461388 return renderer_preferences_;
[email protected]80d96fa2009-06-10 22:34:511389}
1390
[email protected]57c6a652009-05-04 07:58:341391TabContents* TabContents::GetAsTabContents() {
1392 return this;
1393}
1394
[email protected]7b291f92009-08-14 05:43:531395ViewType::Type TabContents::GetRenderViewType() const {
1396 return ViewType::TAB_CONTENTS;
1397}
1398
[email protected]420ae012009-04-24 05:16:321399void TabContents::RenderViewCreated(RenderViewHost* render_view_host) {
[email protected]2e4633c2009-07-09 16:58:061400 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271401 content::NOTIFICATION_RENDER_VIEW_HOST_CREATED_FOR_TAB,
[email protected]2e4633c2009-07-09 16:58:061402 Source<TabContents>(this),
1403 Details<RenderViewHost>(render_view_host));
[email protected]420ae012009-04-24 05:16:321404 NavigationEntry* entry = controller_.GetActiveEntry();
1405 if (!entry)
1406 return;
1407
1408 // When we're creating views, we're still doing initial setup, so we always
[email protected]e0112912011-02-02 22:54:351409 // use the pending Web UI rather than any possibly existing committed one.
[email protected]1fd1a502011-03-30 16:55:561410 if (render_manager_.pending_web_ui())
[email protected]7ade2732011-02-10 00:13:581411 render_manager_.pending_web_ui()->RenderViewCreated(render_view_host);
[email protected]420ae012009-04-24 05:16:321412
1413 if (entry->IsViewSourceMode()) {
1414 // Put the renderer in view source mode.
1415 render_view_host->Send(
1416 new ViewMsg_EnableViewSourceMode(render_view_host->routing_id()));
1417 }
[email protected]0666aef2009-05-13 19:48:081418
1419 view()->RenderViewCreated(render_view_host);
[email protected]11f764bb2011-05-25 19:45:261420
1421 FOR_EACH_OBSERVER(
1422 TabContentsObserver, observers_, RenderViewCreated(render_view_host));
[email protected]420ae012009-04-24 05:16:321423}
1424
1425void TabContents::RenderViewReady(RenderViewHost* rvh) {
1426 if (rvh != render_view_host()) {
1427 // Don't notify the world, since this came from a renderer in the
1428 // background.
1429 return;
1430 }
1431
1432 NotifyConnected();
[email protected]9d8a4642009-07-29 17:25:301433 bool was_crashed = is_crashed();
[email protected]443b80e2010-12-14 00:42:231434 SetIsCrashed(base::TERMINATION_STATUS_STILL_RUNNING, 0);
[email protected]9d8a4642009-07-29 17:25:301435
1436 // Restore the focus to the tab (otherwise the focus will be on the top
1437 // window).
[email protected]484ae5912010-09-29 19:16:141438 if (was_crashed && !FocusLocationBarByDefault() &&
1439 (!delegate_ || delegate_->ShouldFocusPageAfterCrash())) {
[email protected]9d8a4642009-07-29 17:25:301440 Focus();
[email protected]484ae5912010-09-29 19:16:141441 }
[email protected]420ae012009-04-24 05:16:321442}
1443
[email protected]443b80e2010-12-14 00:42:231444void TabContents::RenderViewGone(RenderViewHost* rvh,
1445 base::TerminationStatus status,
1446 int error_code) {
[email protected]420ae012009-04-24 05:16:321447 if (rvh != render_view_host()) {
1448 // The pending page's RenderViewHost is gone.
1449 return;
1450 }
1451
1452 SetIsLoading(false, NULL);
1453 NotifyDisconnected();
[email protected]443b80e2010-12-14 00:42:231454 SetIsCrashed(status, error_code);
[email protected]b3e3f87a2011-10-01 23:37:541455 view()->OnTabCrashed(crashed_status(), crashed_error_code());
[email protected]420ae012009-04-24 05:16:321456
[email protected]55452902011-06-01 21:57:471457 FOR_EACH_OBSERVER(TabContentsObserver, observers_, RenderViewGone());
[email protected]420ae012009-04-24 05:16:321458}
1459
[email protected]2e4633c2009-07-09 16:58:061460void TabContents::RenderViewDeleted(RenderViewHost* rvh) {
[email protected]fb7b79f72009-11-06 18:00:481461 render_manager_.RenderViewDeleted(rvh);
[email protected]2e4633c2009-07-09 16:58:061462}
1463
[email protected]420ae012009-04-24 05:16:321464void TabContents::DidNavigate(RenderViewHost* rvh,
1465 const ViewHostMsg_FrameNavigate_Params& params) {
[email protected]09b29342011-06-24 19:18:481466 if (PageTransition::IsMainFrame(params.transition))
[email protected]420ae012009-04-24 05:16:321467 render_manager_.DidNavigateMainFrame(rvh);
[email protected]8030f012009-09-25 18:09:371468
[email protected]420ae012009-04-24 05:16:321469 // Update the site of the SiteInstance if it doesn't have one yet.
1470 if (!GetSiteInstance()->has_site())
1471 GetSiteInstance()->SetSite(params.url);
1472
1473 // Need to update MIME type here because it's referred to in
1474 // UpdateNavigationCommands() called by RendererDidNavigate() to
1475 // determine whether or not to enable the encoding menu.
1476 // It's updated only for the main frame. For a subframe,
1477 // RenderView::UpdateURL does not set params.contents_mime_type.
1478 // (see http://code.google.com/p/chromium/issues/detail?id=2929 )
1479 // TODO(jungshik): Add a test for the encoding menu to avoid
1480 // regressing it again.
1481 if (PageTransition::IsMainFrame(params.transition))
1482 contents_mime_type_ = params.contents_mime_type;
1483
[email protected]8286f51a2011-05-31 17:39:131484 content::LoadCommittedDetails details;
[email protected]93f230e02011-06-01 14:40:001485 bool did_navigate = controller_.RendererDidNavigate(params, &details);
[email protected]3e90d4a2009-07-03 17:38:391486
[email protected]a9c0bfe2010-09-17 08:35:221487 // Send notification about committed provisional loads. This notification is
1488 // different from the NAV_ENTRY_COMMITTED notification which doesn't include
1489 // the actual URL navigated to and isn't sent for AUTO_SUBFRAME navigations.
1490 if (details.type != NavigationType::NAV_IGNORE) {
[email protected]dabb0d12010-10-05 12:50:071491 // For AUTO_SUBFRAME navigations, an event for the main frame is generated
1492 // that is not recorded in the navigation history. For the purpose of
1493 // tracking navigation events, we treat this event as a sub frame navigation
1494 // event.
1495 bool is_main_frame = did_navigate ? details.is_main_frame : false;
[email protected]0d60f0192011-04-14 12:40:101496 PageTransition::Type transition_type = params.transition;
[email protected]a9c0bfe2010-09-17 08:35:221497 // Whether or not a page transition was triggered by going backward or
1498 // forward in the history is only stored in the navigation controller's
1499 // entry list.
1500 if (did_navigate &&
1501 (controller_.GetActiveEntry()->transition_type() &
1502 PageTransition::FORWARD_BACK)) {
[email protected]0d60f0192011-04-14 12:40:101503 transition_type = params.transition | PageTransition::FORWARD_BACK;
[email protected]a9c0bfe2010-09-17 08:35:221504 }
[email protected]0d60f0192011-04-14 12:40:101505 // Notify observers about the commit of the provisional load.
1506 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
1507 DidCommitProvisionalLoadForFrame(params.frame_id,
1508 is_main_frame, params.url, transition_type));
[email protected]a9c0bfe2010-09-17 08:35:221509 }
1510
[email protected]76543b92009-08-31 17:27:451511 if (!did_navigate)
1512 return; // No navigation happened.
[email protected]420ae012009-04-24 05:16:321513
1514 // DO NOT ADD MORE STUFF TO THIS FUNCTION! Your component should either listen
1515 // for the appropriate notification (best) or you can add it to
1516 // DidNavigateMainFramePostCommit / DidNavigateAnyFramePostCommit (only if
1517 // necessary, please).
1518
1519 // Run post-commit tasks.
[email protected]93f230e02011-06-01 14:40:001520 if (details.is_main_frame) {
[email protected]420ae012009-04-24 05:16:321521 DidNavigateMainFramePostCommit(details, params);
[email protected]09b29342011-06-24 19:18:481522 if (delegate())
1523 delegate()->DidNavigateMainFramePostCommit(this);
[email protected]93f230e02011-06-01 14:40:001524 }
[email protected]420ae012009-04-24 05:16:321525 DidNavigateAnyFramePostCommit(rvh, details, params);
1526}
1527
1528void TabContents::UpdateState(RenderViewHost* rvh,
1529 int32 page_id,
1530 const std::string& state) {
[email protected]992db4c2011-05-12 15:37:151531 // Ensure that this state update comes from either the active RVH or one of
1532 // the swapped out RVHs. We don't expect to hear from any other RVHs.
1533 DCHECK(rvh == render_view_host() || render_manager_.IsSwappedOut(rvh));
[email protected]420ae012009-04-24 05:16:321534
1535 // We must be prepared to handle state updates for any page, these occur
1536 // when the user is scrolling and entering form data, as well as when we're
1537 // leaving a page, in which case our state may have already been moved to
1538 // the next page. The navigation controller will look up the appropriate
1539 // NavigationEntry and update it when it is notified via the delegate.
1540
1541 int entry_index = controller_.GetEntryIndexWithPageID(
[email protected]992db4c2011-05-12 15:37:151542 rvh->site_instance(), page_id);
[email protected]420ae012009-04-24 05:16:321543 if (entry_index < 0)
1544 return;
1545 NavigationEntry* entry = controller_.GetEntryAtIndex(entry_index);
1546
1547 if (state == entry->content_state())
1548 return; // Nothing to update.
1549 entry->set_content_state(state);
1550 controller_.NotifyEntryChanged(entry, entry_index);
1551}
1552
1553void TabContents::UpdateTitle(RenderViewHost* rvh,
[email protected]acafd272011-07-26 17:35:571554 int32 page_id,
[email protected]a49e10b2011-08-01 23:57:461555 const string16& title,
1556 base::i18n::TextDirection title_direction) {
[email protected]420ae012009-04-24 05:16:321557 // If we have a title, that's a pretty good indication that we've started
1558 // getting useful data.
1559 SetNotWaitingForResponse();
1560
1561 DCHECK(rvh == render_view_host());
[email protected]371cc8b2010-09-01 16:03:481562 NavigationEntry* entry = controller_.GetEntryWithPageID(rvh->site_instance(),
[email protected]ec0b6c42010-08-26 03:16:581563 page_id);
[email protected]987fc3a2011-05-26 14:18:091564
[email protected]a49e10b2011-08-01 23:57:461565 // TODO(evan): make use of title_direction.
1566 // http://code.google.com/p/chromium/issues/detail?id=27094
[email protected]987fc3a2011-05-26 14:18:091567 if (!UpdateTitleForEntry(entry, title))
[email protected]420ae012009-04-24 05:16:321568 return;
1569
1570 // Broadcast notifications when the UI should be updated.
1571 if (entry == controller_.GetEntryAtOffset(0))
[email protected]f1cd5e82009-10-23 17:19:031572 NotifyNavigationStateChanged(INVALIDATE_TITLE);
[email protected]420ae012009-04-24 05:16:321573}
1574
[email protected]420ae012009-04-24 05:16:321575void TabContents::UpdateEncoding(RenderViewHost* render_view_host,
[email protected]41fc0322009-09-04 22:23:401576 const std::string& encoding) {
[email protected]420ae012009-04-24 05:16:321577 set_encoding(encoding);
1578}
1579
1580void TabContents::UpdateTargetURL(int32 page_id, const GURL& url) {
1581 if (delegate())
[email protected]d952a052011-09-06 18:42:451582 delegate()->UpdateTargetURL(this, page_id, url);
[email protected]420ae012009-04-24 05:16:321583}
1584
[email protected]420ae012009-04-24 05:16:321585void TabContents::Close(RenderViewHost* rvh) {
[email protected]07707302009-11-06 00:50:291586 // The UI may be in an event-tracking loop, such as between the
1587 // mouse-down and mouse-up in text selection or a button click.
1588 // Defer the close until after tracking is complete, so that we
1589 // don't free objects out from under the UI.
1590 // TODO(shess): This could probably be integrated with the
1591 // IsDoingDrag() test below. Punting for now because I need more
1592 // research to understand how this impacts platforms other than Mac.
1593 // TODO(shess): This could get more fine-grained. For instance,
1594 // closing a tab in another window while selecting text in the
1595 // current window's Omnibox should be just fine.
1596 if (view()->IsEventTracking()) {
1597 view()->CloseTabAfterEventTracking();
1598 return;
1599 }
1600
[email protected]24a4d1062009-07-10 23:10:421601 // If we close the tab while we're in the middle of a drag, we'll crash.
1602 // Instead, cancel the drag and close it as soon as the drag ends.
1603 if (view()->IsDoingDrag()) {
1604 view()->CancelDragAndCloseTab();
1605 return;
1606 }
1607
[email protected]420ae012009-04-24 05:16:321608 // Ignore this if it comes from a RenderViewHost that we aren't showing.
1609 if (delegate() && rvh == render_view_host())
1610 delegate()->CloseContents(this);
1611}
1612
1613void TabContents::RequestMove(const gfx::Rect& new_bounds) {
[email protected]b35b26b32011-05-05 20:35:141614 if (delegate() && delegate()->IsPopupOrPanel(this))
[email protected]420ae012009-04-24 05:16:321615 delegate()->MoveContents(this, new_bounds);
1616}
1617
[email protected]7ab1e7d62009-10-14 23:32:011618void TabContents::DidStartLoading() {
[email protected]420ae012009-04-24 05:16:321619 SetIsLoading(true, NULL);
[email protected]3c9e1872010-11-18 16:17:491620
[email protected]c40d6232011-03-25 00:16:211621 if (delegate() && content_restrictions_) {
1622 content_restrictions_ = 0;
[email protected]de4761b2010-12-07 21:05:211623 delegate()->ContentRestrictionsChanged(this);
[email protected]9e823662010-10-13 23:36:001624 }
[email protected]3c9e1872010-11-18 16:17:491625
1626 // Notify observers about navigation.
[email protected]585b30362011-01-28 02:30:171627 FOR_EACH_OBSERVER(TabContentsObserver, observers_, DidStartLoading());
[email protected]420ae012009-04-24 05:16:321628}
1629
[email protected]7ab1e7d62009-10-14 23:32:011630void TabContents::DidStopLoading() {
[email protected]420ae012009-04-24 05:16:321631 scoped_ptr<LoadNotificationDetails> details;
1632
1633 NavigationEntry* entry = controller_.GetActiveEntry();
1634 // An entry may not exist for a stop when loading an initial blank page or
1635 // if an iframe injected by script into a blank page finishes loading.
1636 if (entry) {
[email protected]420ae012009-04-24 05:16:321637 base::TimeDelta elapsed = base::TimeTicks::Now() - current_load_start_;
1638
1639 details.reset(new LoadNotificationDetails(
[email protected]ebe89e062009-08-13 23:16:541640 entry->virtual_url(),
[email protected]420ae012009-04-24 05:16:321641 entry->transition_type(),
1642 elapsed,
1643 &controller_,
1644 controller_.GetCurrentEntryIndex()));
1645 }
1646
[email protected]420ae012009-04-24 05:16:321647 SetIsLoading(false, details.get());
[email protected]3c9e1872010-11-18 16:17:491648
1649 // Notify observers about navigation.
[email protected]585b30362011-01-28 02:30:171650 FOR_EACH_OBSERVER(TabContentsObserver, observers_, DidStopLoading());
[email protected]420ae012009-04-24 05:16:321651}
1652
[email protected]c95fa8b2011-04-28 20:26:161653void TabContents::DidCancelLoading() {
1654 controller_.DiscardNonCommittedEntries();
1655
1656 // Update the URL display.
1657 NotifyNavigationStateChanged(TabContents::INVALIDATE_URL);
1658}
1659
[email protected]1a3c3cb2010-12-16 21:03:401660void TabContents::DidChangeLoadProgress(double progress) {
1661 if (delegate())
1662 delegate()->LoadProgressChanged(progress);
1663}
1664
[email protected]25497492010-09-11 15:15:081665void TabContents::DocumentOnLoadCompletedInMainFrame(
1666 RenderViewHost* render_view_host,
1667 int32 page_id) {
1668 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271669 content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME,
[email protected]25497492010-09-11 15:15:081670 Source<TabContents>(this),
1671 Details<int>(&page_id));
1672}
1673
[email protected]420ae012009-04-24 05:16:321674void TabContents::RequestOpenURL(const GURL& url, const GURL& referrer,
1675 WindowOpenDisposition disposition) {
[email protected]7ade2732011-02-10 00:13:581676 if (render_manager_.web_ui()) {
[email protected]e0112912011-02-02 22:54:351677 // When we're a Web UI, it will provide a page transition type for us (this
[email protected]420ae012009-04-24 05:16:321678 // is so the new tab page can specify AUTO_BOOKMARK for automatically
1679 // generated suggestions).
1680 //
[email protected]e0112912011-02-02 22:54:351681 // Note also that we hide the referrer for Web UI pages. We don't really
[email protected]60e448982009-05-06 04:21:161682 // want web sites to see a referrer of "chrome://blah" (and some
1683 // chrome: URLs might have search terms or other stuff we don't want to
[email protected]420ae012009-04-24 05:16:321684 // send to the site), so we send no referrer.
1685 OpenURL(url, GURL(), disposition,
[email protected]7ade2732011-02-10 00:13:581686 render_manager_.web_ui()->link_transition_type());
[email protected]420ae012009-04-24 05:16:321687 } else {
1688 OpenURL(url, referrer, disposition, PageTransition::LINK);
1689 }
1690}
1691
[email protected]420ae012009-04-24 05:16:321692void TabContents::RunJavaScriptMessage(
[email protected]992db4c2011-05-12 15:37:151693 const RenderViewHost* rvh,
[email protected]4f5ce842011-05-27 19:34:411694 const string16& message,
1695 const string16& default_prompt,
[email protected]420ae012009-04-24 05:16:321696 const GURL& frame_url,
1697 const int flags,
1698 IPC::Message* reply_msg,
1699 bool* did_suppress_message) {
[email protected]3ab9cb82011-06-03 18:02:071700 // Suppress JavaScript dialogs when requested. Also suppress messages when
1701 // showing an interstitial as it's shown over the previous page and we don't
1702 // want the hidden page's dialogs to interfere with the interstitial.
[email protected]8f55e802010-12-06 18:11:501703 bool suppress_this_message =
[email protected]992db4c2011-05-12 15:37:151704 rvh->is_swapped_out() ||
[email protected]8f55e802010-12-06 18:11:501705 showing_interstitial_page() ||
[email protected]3ab9cb82011-06-03 18:02:071706 !delegate_ ||
1707 delegate_->ShouldSuppressDialogs();
[email protected]420ae012009-04-24 05:16:321708
1709 if (!suppress_this_message) {
[email protected]b627d9a2011-06-28 14:06:341710 content::JavaScriptDialogCreator::TitleType title_type;
1711 string16 title;
1712
1713 if (!frame_url.has_host()) {
1714 title_type = content::JavaScriptDialogCreator::DIALOG_TITLE_NONE;
1715 } else {
1716 title_type = content::JavaScriptDialogCreator::DIALOG_TITLE_FORMATTED_URL;
1717 title = net::FormatUrl(
1718 frame_url.GetOrigin(),
1719 content::GetContentClient()->browser()->GetAcceptLangs(this));
1720 }
1721
[email protected]2e5b90c2011-08-16 21:11:551722 dialog_creator_ = delegate_->GetJavaScriptDialogCreator();
1723 dialog_creator_->RunJavaScriptDialog(this,
1724 title_type,
1725 title,
1726 flags,
1727 message,
1728 default_prompt,
1729 reply_msg,
1730 &suppress_this_message);
[email protected]420ae012009-04-24 05:16:321731 }
[email protected]3ab9cb82011-06-03 18:02:071732
1733 if (suppress_this_message) {
1734 // If we are suppressing messages, just reply as if the user immediately
1735 // pressed "Cancel".
1736 OnDialogClosed(reply_msg, false, string16());
1737 }
1738
1739 *did_suppress_message = suppress_this_message;
[email protected]420ae012009-04-24 05:16:321740}
1741
[email protected]992db4c2011-05-12 15:37:151742void TabContents::RunBeforeUnloadConfirm(const RenderViewHost* rvh,
[email protected]4f5ce842011-05-27 19:34:411743 const string16& message,
[email protected]420ae012009-04-24 05:16:321744 IPC::Message* reply_msg) {
[email protected]5aab5e22010-12-08 22:13:291745 if (delegate())
1746 delegate()->WillRunBeforeUnloadConfirm();
[email protected]3ab9cb82011-06-03 18:02:071747
1748 bool suppress_this_message =
1749 rvh->is_swapped_out() ||
1750 !delegate_ ||
1751 delegate_->ShouldSuppressDialogs();
[email protected]992db4c2011-05-12 15:37:151752 if (suppress_this_message) {
[email protected]4f5ce842011-05-27 19:34:411753 render_view_host()->JavaScriptDialogClosed(reply_msg, true, string16());
[email protected]8f55e802010-12-06 18:11:501754 return;
1755 }
[email protected]3ab9cb82011-06-03 18:02:071756
[email protected]7ab1e7d62009-10-14 23:32:011757 is_showing_before_unload_dialog_ = true;
[email protected]2e5b90c2011-08-16 21:11:551758 dialog_creator_ = delegate_->GetJavaScriptDialogCreator();
1759 dialog_creator_->RunBeforeUnloadDialog(this,
1760 message,
1761 reply_msg);
[email protected]420ae012009-04-24 05:16:321762}
1763
[email protected]420ae012009-04-24 05:16:321764WebPreferences TabContents::GetWebkitPrefs() {
[email protected]447021c2010-09-08 21:29:081765 WebPreferences web_prefs =
[email protected]181a95ee2011-07-12 19:26:361766 content::GetContentClient()->browser()->GetWebkitPrefs(
[email protected]3d7474ff2011-07-27 17:47:371767 render_view_host()->process()->browser_context(), false);
[email protected]447021c2010-09-08 21:29:081768
[email protected]b8299c12011-06-03 19:52:281769 // Force accelerated compositing and 2d canvas off for chrome:, about: and
[email protected]2ef216b2011-08-01 19:25:211770 // chrome-devtools: pages (unless it's specifically allowed).
1771 if ((GetURL().SchemeIs(chrome::kChromeDevToolsScheme) ||
[email protected]30b8e0ee2011-09-12 14:45:571772#if !defined(TOUCH_UI)
1773 // Allow accelerated compositing for keyboard and log in screen.
[email protected]b8299c12011-06-03 19:52:281774 GetURL().SchemeIs(chrome::kChromeUIScheme) ||
[email protected]30b8e0ee2011-09-12 14:45:571775#endif
[email protected]cd4a5bd22011-09-13 06:46:301776 (GetURL().SchemeIs(chrome::kAboutScheme) &&
1777 GetURL().spec() != chrome::kAboutBlankURL)) &&
[email protected]2ef216b2011-08-01 19:25:211778 !web_prefs.allow_webui_compositing) {
[email protected]447021c2010-09-08 21:29:081779 web_prefs.accelerated_compositing_enabled = false;
[email protected]9beff752010-09-22 19:35:431780 web_prefs.accelerated_2d_canvas_enabled = false;
[email protected]447021c2010-09-08 21:29:081781 }
1782
[email protected]2f7830b32011-08-26 18:22:251783#if defined(TOUCH_UI)
1784 web_prefs.force_compositing_mode = true;
1785#endif
1786
[email protected]447021c2010-09-08 21:29:081787 return web_prefs;
[email protected]420ae012009-04-24 05:16:321788}
1789
[email protected]7d472472011-01-22 01:30:251790void TabContents::OnUserGesture() {
[email protected]ddb85052011-05-18 14:40:271791 // Notify observers.
1792 FOR_EACH_OBSERVER(TabContentsObserver, observers_, DidGetUserGesture());
[email protected]686493142011-07-15 21:47:221793
1794 ResourceDispatcherHost* rdh =
1795 content::GetContentClient()->browser()->GetResourceDispatcherHost();
1796 if (rdh) // NULL in unittests.
[email protected]8ec98cd22011-08-09 22:31:301797 rdh->OnUserGesture(this);
[email protected]7d472472011-01-22 01:30:251798}
1799
[email protected]fa1cf0b82010-01-15 21:49:441800void TabContents::OnIgnoredUIEvent() {
[email protected]31145092011-09-30 01:49:441801 // Notify observers.
1802 FOR_EACH_OBSERVER(TabContentsObserver, observers_, DidGetIgnoredUIEvent());
[email protected]fa1cf0b82010-01-15 21:49:441803}
1804
[email protected]420ae012009-04-24 05:16:321805void TabContents::RendererUnresponsive(RenderViewHost* rvh,
1806 bool is_during_unload) {
[email protected]992db4c2011-05-12 15:37:151807 // Don't show hung renderer dialog for a swapped out RVH.
1808 if (rvh != render_view_host())
1809 return;
1810
[email protected]e5fc1632011-08-08 07:51:531811 // Ignore renderer unresponsive event if debugger is attached to the tab
1812 // since the event may be a result of the renderer sitting on a breakpoint.
1813 // See http://crbug.com/65458
1814 DevToolsManager* devtools_manager = DevToolsManager::GetInstance();
1815 if (devtools_manager &&
1816 devtools_manager->GetDevToolsClientHostFor(rvh) != NULL)
1817 return;
1818
[email protected]420ae012009-04-24 05:16:321819 if (is_during_unload) {
1820 // Hang occurred while firing the beforeunload/unload handler.
1821 // Pretend the handler fired so tab closing continues as if it had.
[email protected]829e7612009-04-25 01:15:111822 rvh->set_sudden_termination_allowed(true);
[email protected]420ae012009-04-24 05:16:321823
1824 if (!render_manager_.ShouldCloseTabOnUnresponsiveRenderer())
1825 return;
1826
1827 // If the tab hangs in the beforeunload/unload handler there's really
1828 // nothing we can do to recover. Pretend the unload listeners have
1829 // all fired and close the tab. If the hang is in the beforeunload handler
1830 // then the user will not have the option of cancelling the close.
1831 Close(rvh);
1832 return;
1833 }
1834
[email protected]55452902011-06-01 21:57:471835 if (!render_view_host() || !render_view_host()->IsRenderViewLive())
1836 return;
1837
1838 if (delegate())
1839 delegate()->RendererUnresponsive(this);
[email protected]420ae012009-04-24 05:16:321840}
1841
1842void TabContents::RendererResponsive(RenderViewHost* render_view_host) {
[email protected]55452902011-06-01 21:57:471843 if (delegate())
1844 delegate()->RendererResponsive(this);
[email protected]420ae012009-04-24 05:16:321845}
1846
1847void TabContents::LoadStateChanged(const GURL& url,
[email protected]9c235f042011-08-10 22:28:211848 const net::LoadStateWithParam& load_state,
[email protected]094e5b22009-09-25 04:23:561849 uint64 upload_position,
1850 uint64 upload_size) {
[email protected]420ae012009-04-24 05:16:321851 load_state_ = load_state;
[email protected]094e5b22009-09-25 04:23:561852 upload_position_ = upload_position;
1853 upload_size_ = upload_size;
[email protected]04866c42011-05-03 20:03:501854 load_state_host_ = net::IDNToUnicode(url.host(),
[email protected]b5cca982011-05-26 04:42:081855 content::GetContentClient()->browser()->GetAcceptLangs(this));
[email protected]9c235f042011-08-10 22:28:211856 if (load_state_.state == net::LOAD_STATE_READING_RESPONSE)
[email protected]420ae012009-04-24 05:16:321857 SetNotWaitingForResponse();
[email protected]f5d978c2011-07-21 14:43:511858 if (IsLoading())
[email protected]c9cd2222009-05-06 05:16:501859 NotifyNavigationStateChanged(INVALIDATE_LOAD | INVALIDATE_TAB);
[email protected]420ae012009-04-24 05:16:321860}
1861
[email protected]7d472472011-01-22 01:30:251862void TabContents::WorkerCrashed() {
[email protected]0dd3a0ab2011-02-18 08:17:441863 if (delegate())
[email protected]55452902011-06-01 21:57:471864 delegate()->WorkerCrashed(this);
[email protected]7d472472011-01-22 01:30:251865}
1866
[email protected]420ae012009-04-24 05:16:321867void TabContents::BeforeUnloadFiredFromRenderManager(
1868 bool proceed,
1869 bool* proceed_to_fire_unload) {
1870 if (delegate())
1871 delegate()->BeforeUnloadFired(this, proceed, proceed_to_fire_unload);
1872}
1873
[email protected]3a3d47472010-07-15 21:03:541874void TabContents::DidStartLoadingFromRenderManager(
1875 RenderViewHost* render_view_host) {
1876 DidStartLoading();
1877}
1878
1879void TabContents::RenderViewGoneFromRenderManager(
1880 RenderViewHost* render_view_host) {
[email protected]443b80e2010-12-14 00:42:231881 DCHECK(crashed_status_ != base::TERMINATION_STATUS_STILL_RUNNING);
1882 RenderViewGone(render_view_host, crashed_status_, crashed_error_code_);
[email protected]3a3d47472010-07-15 21:03:541883}
1884
[email protected]420ae012009-04-24 05:16:321885void TabContents::UpdateRenderViewSizeForRenderManager() {
1886 // TODO(brettw) this is a hack. See TabContentsView::SizeContents.
[email protected]0323ee42010-02-17 22:03:261887 gfx::Size size = view_->GetContainerSize();
1888 // 0x0 isn't a valid window size (minimal window size is 1x1) but it may be
1889 // here during container initialization and normal window size will be set
1890 // later. In case of tab duplication this resizing to 0x0 prevents setting
1891 // normal size later so just ignore it.
1892 if (!size.IsEmpty())
1893 view_->SizeContents(size);
[email protected]420ae012009-04-24 05:16:321894}
1895
[email protected]3a3d47472010-07-15 21:03:541896void TabContents::NotifySwappedFromRenderManager() {
1897 NotifySwapped();
1898}
1899
1900NavigationController& TabContents::GetControllerForRenderManager() {
1901 return controller();
1902}
1903
[email protected]d0980792011-02-13 19:41:401904WebUI* TabContents::CreateWebUIForRenderManager(const GURL& url) {
[email protected]1fd1a502011-03-30 16:55:561905 return content::WebUIFactory::Get()->CreateWebUIForURL(this, url);
[email protected]420ae012009-04-24 05:16:321906}
1907
1908NavigationEntry*
1909TabContents::GetLastCommittedNavigationEntryForRenderManager() {
1910 return controller_.GetLastCommittedEntry();
1911}
1912
1913bool TabContents::CreateRenderViewForRenderManager(
1914 RenderViewHost* render_view_host) {
[email protected]420ae012009-04-24 05:16:321915 RenderWidgetHostView* rwh_view = view_->CreateViewForWidget(render_view_host);
[email protected]1a98a932009-11-17 00:12:521916
[email protected]420ae012009-04-24 05:16:321917 // Now that the RenderView has been created, we need to tell it its size.
1918 rwh_view->SetSize(view_->GetContainerSize());
1919
[email protected]a4127722011-04-27 23:13:521920 if (!render_view_host->CreateRenderView(string16()))
1921 return false;
1922
[email protected]f8233cc2011-05-31 20:24:501923#if defined(OS_LINUX)
1924 // Force a ViewMsg_Resize to be sent, needed to make plugins show up on
1925 // linux. See crbug.com/83941.
1926 if (RenderWidgetHost* render_widget_host = rwh_view->GetRenderWidgetHost())
1927 render_widget_host->WasResized();
1928#endif
1929
[email protected]420ae012009-04-24 05:16:321930 UpdateMaxPageIDIfNecessary(render_view_host->site_instance(),
1931 render_view_host);
1932 return true;
1933}
1934
[email protected]3ab9cb82011-06-03 18:02:071935void TabContents::OnDialogClosed(IPC::Message* reply_msg,
1936 bool success,
1937 const string16& user_input) {
[email protected]beb440c2009-11-06 04:08:541938 if (is_showing_before_unload_dialog_ && !success) {
1939 // If a beforeunload dialog is canceled, we need to stop the throbber from
1940 // spinning, since we forced it to start spinning in Navigate.
1941 DidStopLoading();
1942
1943 tab_close_start_time_ = base::TimeTicks();
1944 }
1945 is_showing_before_unload_dialog_ = false;
[email protected]4f5ce842011-05-27 19:34:411946 render_view_host()->JavaScriptDialogClosed(reply_msg,
1947 success,
[email protected]3ab9cb82011-06-03 18:02:071948 user_input);
[email protected]beb440c2009-11-06 04:08:541949}
1950
[email protected]3ab9cb82011-06-03 18:02:071951gfx::NativeWindow TabContents::GetDialogRootWindow() {
1952 return view_->GetTopLevelNativeWindow();
[email protected]beb440c2009-11-06 04:08:541953}
1954
[email protected]a1e97f02011-06-30 14:04:341955void TabContents::OnDialogShown() {
1956 Activate();
[email protected]3a3d47472010-07-15 21:03:541957}
1958
[email protected]41fc0322009-09-04 22:23:401959void TabContents::set_encoding(const std::string& encoding) {
[email protected]763ec4ca2011-04-29 15:48:121960 encoding_ = content::GetContentClient()->browser()->
1961 GetCanonicalEncodingNameByAliasName(encoding);
[email protected]aacd7cc2009-09-02 14:51:101962}
[email protected]f45d2a72010-03-08 23:28:351963
[email protected]33f74972010-12-08 16:40:361964void TabContents::CreateViewAndSetSizeForRVH(RenderViewHost* rvh) {
1965 RenderWidgetHostView* rwh_view = view()->CreateViewForWidget(rvh);
1966 rwh_view->SetSize(view()->GetContainerSize());
1967}