blob: 5d809cc9c29768aaa2f304c91e0567526dec0a18 [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]ee162d82011-10-06 06:35:3751#include "content/common/view_types.h"
[email protected]d686e812009-06-03 19:10:2952#include "net/base/net_util.h"
[email protected]abe2c032011-03-31 18:49:3453#include "net/url_request/url_request_context_getter.h"
[email protected]8bd0fe62011-01-17 06:44:3754#include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h"
[email protected]08397d52011-02-05 01:53:3855#include "ui/gfx/codec/png_codec.h"
[email protected]d5e311b2010-07-20 17:15:0356#include "webkit/glue/password_form.h"
[email protected]ce9751942011-09-21 01:57:2457#include "webkit/glue/web_intent_data.h"
[email protected]3c733bde2010-12-21 19:56:3158#include "webkit/glue/webpreferences.h"
59
60#if defined(OS_MACOSX)
[email protected]b9b751f22011-03-25 14:04:1261#include "ui/gfx/surface/io_surface_support_mac.h"
[email protected]3c733bde2010-12-21 19:56:3162#endif // defined(OS_MACOSX)
[email protected]3e45ba92009-02-20 21:09:0063
[email protected]420ae012009-04-24 05:16:3264// Cross-Site Navigations
65//
66// If a TabContents is told to navigate to a different web site (as determined
67// by SiteInstance), it will replace its current RenderViewHost with a new
68// RenderViewHost dedicated to the new SiteInstance. This works as follows:
69//
70// - Navigate determines whether the destination is cross-site, and if so,
[email protected]a2750082011-09-01 12:29:4671// it creates a pending_render_view_host_.
[email protected]420ae012009-04-24 05:16:3272// - The pending RVH is "suspended," so that no navigation messages are sent to
73// its renderer until the onbeforeunload JavaScript handler has a chance to
74// run in the current RVH.
75// - The pending RVH tells CrossSiteRequestManager (a thread-safe singleton)
76// that it has a pending cross-site request. ResourceDispatcherHost will
77// check for this when the response arrives.
78// - The current RVH runs its onbeforeunload handler. If it returns false, we
[email protected]a2750082011-09-01 12:29:4679// cancel all the pending logic. Otherwise we allow the pending RVH to send
80// the navigation request to its renderer.
81// - ResourceDispatcherHost receives a ResourceRequest on the IO thread for the
82// main resource load on the pending RVH. It checks CrossSiteRequestManager
83// to see that it is a cross-site request, and installs a
84// CrossSiteResourceHandler.
85// - When RDH receives a response, the BufferedResourceHandler determines
86// whether it is a download. If so, it sends a message to the new renderer
87// causing it to cancel the request, and the download proceeds. For now, the
88// pending RVH remains until the next DidNavigate event for this TabContents.
89// This isn't ideal, but it doesn't affect any functionality.
[email protected]420ae012009-04-24 05:16:3290// - After RDH receives a response and determines that it is safe and not a
91// download, it pauses the response to first run the old page's onunload
92// handler. It does this by asynchronously calling the OnCrossSiteResponse
[email protected]992db4c2011-05-12 15:37:1593// method of TabContents on the UI thread, which sends a SwapOut message
[email protected]420ae012009-04-24 05:16:3294// to the current RVH.
[email protected]992db4c2011-05-12 15:37:1595// - Once the onunload handler is finished, a SwapOut_ACK message is sent to
[email protected]420ae012009-04-24 05:16:3296// the ResourceDispatcherHost, who unpauses the response. Data is then sent
97// to the pending RVH.
98// - The pending renderer sends a FrameNavigate message that invokes the
99// DidNavigate method. This replaces the current RVH with the
[email protected]a2750082011-09-01 12:29:46100// pending RVH.
[email protected]992db4c2011-05-12 15:37:15101// - The previous renderer is kept swapped out in RenderViewHostManager in case
102// the user goes back. The process only stays live if another tab is using
103// it, but if so, the existing frame relationships will be maintained.
[email protected]420ae012009-04-24 05:16:32104
105namespace {
106
107// Amount of time we wait between when a key event is received and the renderer
108// is queried for its state and pushed to the NavigationEntry.
109const int kQueryStateDelay = 5000;
110
[email protected]6ebdc9b2010-09-27 16:55:57111const int kSyncWaitDelay = 40;
112
[email protected]ca406032011-07-19 21:53:05113static const char kDotGoogleDotCom[] = ".google.com";
114
[email protected]420ae012009-04-24 05:16:32115#if defined(OS_WIN)
116
117BOOL CALLBACK InvalidateWindow(HWND hwnd, LPARAM lparam) {
118 // Note: erase is required to properly paint some widgets borders. This can
119 // be seen with textfields.
120 InvalidateRect(hwnd, NULL, TRUE);
121 return TRUE;
122}
123#endif
124
[email protected]2c5569662011-03-22 20:45:02125ViewMsg_Navigate_Type::Value GetNavigationType(
[email protected]3d7474ff2011-07-27 17:47:37126 content::BrowserContext* browser_context, const NavigationEntry& entry,
[email protected]1ccb3568d2010-02-19 10:51:16127 NavigationController::ReloadType reload_type) {
128 switch (reload_type) {
129 case NavigationController::RELOAD:
[email protected]2c5569662011-03-22 20:45:02130 return ViewMsg_Navigate_Type::RELOAD;
[email protected]1ccb3568d2010-02-19 10:51:16131 case NavigationController::RELOAD_IGNORING_CACHE:
[email protected]2c5569662011-03-22 20:45:02132 return ViewMsg_Navigate_Type::RELOAD_IGNORING_CACHE;
[email protected]1ccb3568d2010-02-19 10:51:16133 case NavigationController::NO_RELOAD:
134 break; // Fall through to rest of function.
135 }
[email protected]5e369672009-11-03 23:48:30136
137 if (entry.restore_type() == NavigationEntry::RESTORE_LAST_SESSION &&
[email protected]3d7474ff2011-07-27 17:47:37138 browser_context->DidLastSessionExitCleanly())
[email protected]2c5569662011-03-22 20:45:02139 return ViewMsg_Navigate_Type::RESTORE;
[email protected]5e369672009-11-03 23:48:30140
[email protected]2c5569662011-03-22 20:45:02141 return ViewMsg_Navigate_Type::NORMAL;
[email protected]5e369672009-11-03 23:48:30142}
143
[email protected]876bc832010-09-07 16:29:54144void MakeNavigateParams(const NavigationEntry& entry,
145 const NavigationController& controller,
[email protected]6c6b02d2011-09-02 03:36:47146 TabContentsDelegate* delegate,
[email protected]1ccb3568d2010-02-19 10:51:16147 NavigationController::ReloadType reload_type,
148 ViewMsg_Navigate_Params* params) {
[email protected]056de2d2009-06-26 16:41:34149 params->page_id = entry.page_id();
[email protected]876bc832010-09-07 16:29:54150 params->pending_history_list_offset = controller.GetIndexOfEntry(&entry);
[email protected]3cc72b12010-03-18 23:03:00151 params->current_history_list_offset = controller.last_committed_entry_index();
152 params->current_history_list_length = controller.entry_count();
[email protected]056de2d2009-06-26 16:41:34153 params->url = entry.url();
154 params->referrer = entry.referrer();
155 params->transition = entry.transition_type();
156 params->state = entry.content_state();
[email protected]3cc72b12010-03-18 23:03:00157 params->navigation_type =
[email protected]3d7474ff2011-07-27 17:47:37158 GetNavigationType(controller.browser_context(), entry, reload_type);
[email protected]056de2d2009-06-26 16:41:34159 params->request_time = base::Time::Now();
[email protected]6c6b02d2011-09-02 03:36:47160 params->extra_headers = entry.extra_headers();
161
162 if (delegate)
163 delegate->AddNavigationHeaders(params->url, &params->extra_headers);
[email protected]056de2d2009-06-26 16:41:34164}
165
[email protected]420ae012009-04-24 05:16:32166} // namespace
167
[email protected]f4f50ef2011-01-21 19:01:19168
169// TabContents ----------------------------------------------------------------
[email protected]420ae012009-04-24 05:16:32170
[email protected]3d7474ff2011-07-27 17:47:37171TabContents::TabContents(content::BrowserContext* browser_context,
[email protected]420ae012009-04-24 05:16:32172 SiteInstance* site_instance,
173 int routing_id,
[email protected]6ee12c42010-09-14 09:36:07174 const TabContents* base_tab_contents,
175 SessionStorageNamespace* session_storage_namespace)
[email protected]b680ad22009-04-15 23:19:42176 : delegate_(NULL),
[email protected]6ee12c42010-09-14 09:36:07177 ALLOW_THIS_IN_INITIALIZER_LIST(controller_(
[email protected]3d7474ff2011-07-27 17:47:37178 this, browser_context, session_storage_namespace)),
[email protected]66ba4932009-06-04 19:22:13179 ALLOW_THIS_IN_INITIALIZER_LIST(view_(
[email protected]74313b42011-08-24 16:51:32180 content::GetContentClient()->browser()->CreateTabContentsView(this))),
[email protected]d82ed61e2009-06-16 02:46:22181 ALLOW_THIS_IN_INITIALIZER_LIST(render_manager_(this, this)),
[email protected]d5f942ba2008-09-26 19:30:34182 is_loading_(false),
[email protected]443b80e2010-12-14 00:42:23183 crashed_status_(base::TERMINATION_STATUS_STILL_RUNNING),
184 crashed_error_code_(0),
[email protected]d5f942ba2008-09-26 19:30:34185 waiting_for_response_(false),
[email protected]d5f942ba2008-09-26 19:30:34186 max_page_id_(-1),
[email protected]9c235f042011-08-10 22:28:21187 load_state_(net::LOAD_STATE_IDLE, string16()),
[email protected]094e5b22009-09-25 04:23:56188 upload_size_(0),
189 upload_position_(0),
[email protected]f17a0ee2010-05-17 17:38:47190 displayed_insecure_content_(false),
[email protected]fdd61c62009-04-22 19:22:57191 capturing_contents_(false),
192 is_being_destroyed_(false),
193 notify_disconnection_(false),
[email protected]2e5b90c2011-08-16 21:11:55194 dialog_creator_(NULL),
[email protected]fdd61c62009-04-22 19:22:57195#if defined(OS_WIN)
196 message_box_active_(CreateEvent(NULL, TRUE, FALSE, NULL)),
197#endif
[email protected]7ab1e7d62009-10-14 23:32:01198 is_showing_before_unload_dialog_(false),
[email protected]1fd1a502011-03-30 16:55:56199 opener_web_ui_type_(WebUI::kNoWebUI),
[email protected]ebf40a72010-07-22 01:46:38200 closed_by_user_gesture_(false),
[email protected]b75b8292010-10-01 07:28:25201 minimum_zoom_percent_(
202 static_cast<int>(WebKit::WebView::minTextSizeMultiplier * 100)),
203 maximum_zoom_percent_(
204 static_cast<int>(WebKit::WebView::maxTextSizeMultiplier * 100)),
[email protected]9e823662010-10-13 23:36:00205 temporary_zoom_settings_(false),
[email protected]4850a7f2011-03-08 23:36:59206 content_restrictions_(0) {
[email protected]7ff431e2010-01-07 18:18:54207
[email protected]3d7474ff2011-07-27 17:47:37208 render_manager_.Init(browser_context, site_instance, routing_id);
[email protected]420ae012009-04-24 05:16:32209
[email protected]34ac70502009-09-25 17:07:23210 // We have the initial size of the view be based on the size of the passed in
211 // tab contents (normally a tab from the same window).
212 view_->CreateView(base_tab_contents ?
213 base_tab_contents->view()->GetContainerSize() : gfx::Size());
[email protected]332af7732009-03-11 13:21:35214}
initial.commit09911bf2008-07-26 23:55:29215
216TabContents::~TabContents() {
[email protected]420ae012009-04-24 05:16:32217 is_being_destroyed_ = true;
218
[email protected]3ab9cb82011-06-03 18:02:07219 // Clear out any JavaScript state.
[email protected]2e5b90c2011-08-16 21:11:55220 if (dialog_creator_)
221 dialog_creator_->ResetJavaScriptState(this);
[email protected]3ab9cb82011-06-03 18:02:07222
[email protected]420ae012009-04-24 05:16:32223 NotifyDisconnected();
[email protected]420ae012009-04-24 05:16:32224
[email protected]420ae012009-04-24 05:16:32225 // Notify any observer that have a reference on this tab contents.
226 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:27227 content::NOTIFICATION_TAB_CONTENTS_DESTROYED,
[email protected]420ae012009-04-24 05:16:32228 Source<TabContents>(this),
229 NotificationService::NoDetails());
230
231 // TODO(brettw) this should be moved to the view.
232#if defined(OS_WIN)
233 // If we still have a window handle, destroy it. GetNativeView can return
234 // NULL if this contents was part of a window that closed.
[email protected]50bd6452010-11-27 19:39:42235 if (GetNativeView()) {
236 RenderViewHost* host = render_view_host();
[email protected]b9a1fb42011-04-12 23:16:44237 if (host && host->view())
[email protected]50bd6452010-11-27 19:39:42238 host->view()->WillWmDestroy();
[email protected]50bd6452010-11-27 19:39:42239 }
[email protected]420ae012009-04-24 05:16:32240#endif
[email protected]7ab1e7d62009-10-14 23:32:01241
242 // OnCloseStarted isn't called in unit tests.
243 if (!tab_close_start_time_.is_null()) {
244 UMA_HISTOGRAM_TIMES("Tab.Close",
245 base::TimeTicks::Now() - tab_close_start_time_);
246 }
[email protected]b5a1d11c2011-02-17 03:09:42247
[email protected]07d490bc2011-03-07 17:05:26248 FOR_EACH_OBSERVER(TabContentsObserver, observers_, TabContentsDestroyed());
[email protected]232a5812011-03-04 22:42:08249
[email protected]1de2b8b2011-06-29 19:38:46250 set_delegate(NULL);
251}
252
253// TODO(cbentzel): Either remove the debugging code, or rename to SetDelegate.
254void TabContents::set_delegate(TabContentsDelegate* delegate) {
255 if (delegate == delegate_)
256 return;
257 if (delegate_)
258 delegate_->Detach(this);
259 delegate_ = delegate;
260 if (delegate_)
261 delegate_->Attach(this);
[email protected]b5a1d11c2011-02-17 03:09:42262}
263
[email protected]724159a2010-12-30 01:11:18264bool TabContents::OnMessageReceived(const IPC::Message& message) {
[email protected]f82d57b52011-04-27 19:13:17265 if (web_ui() && web_ui()->OnMessageReceived(message))
266 return true;
267
[email protected]585b30362011-01-28 02:30:17268 ObserverListBase<TabContentsObserver>::Iterator it(observers_);
269 TabContentsObserver* observer;
[email protected]0f180ee2011-01-21 18:23:10270 while ((observer = it.GetNext()) != NULL)
271 if (observer->OnMessageReceived(message))
[email protected]403415a2011-01-10 18:57:53272 return true;
[email protected]403415a2011-01-10 18:57:53273
[email protected]724159a2010-12-30 01:11:18274 bool handled = true;
275 bool message_is_ok = true;
276 IPC_BEGIN_MESSAGE_MAP_EX(TabContents, message, message_is_ok)
[email protected]ce9751942011-09-21 01:57:24277 IPC_MESSAGE_HANDLER(IntentsHostMsg_WebIntentDispatch,
278 OnWebIntentDispatch)
[email protected]724159a2010-12-30 01:11:18279 IPC_MESSAGE_HANDLER(ViewHostMsg_DidStartProvisionalLoadForFrame,
280 OnDidStartProvisionalLoadForFrame)
281 IPC_MESSAGE_HANDLER(ViewHostMsg_DidRedirectProvisionalLoad,
282 OnDidRedirectProvisionalLoad)
283 IPC_MESSAGE_HANDLER(ViewHostMsg_DidFailProvisionalLoadWithError,
284 OnDidFailProvisionalLoadWithError)
285 IPC_MESSAGE_HANDLER(ViewHostMsg_DidLoadResourceFromMemoryCache,
286 OnDidLoadResourceFromMemoryCache)
287 IPC_MESSAGE_HANDLER(ViewHostMsg_DidDisplayInsecureContent,
288 OnDidDisplayInsecureContent)
289 IPC_MESSAGE_HANDLER(ViewHostMsg_DidRunInsecureContent,
290 OnDidRunInsecureContent)
291 IPC_MESSAGE_HANDLER(ViewHostMsg_DocumentLoadedInFrame,
292 OnDocumentLoadedInFrame)
293 IPC_MESSAGE_HANDLER(ViewHostMsg_DidFinishLoad, OnDidFinishLoad)
[email protected]c8f73ab2011-01-22 00:05:17294 IPC_MESSAGE_HANDLER(ViewHostMsg_UpdateContentRestrictions,
295 OnUpdateContentRestrictions)
[email protected]7d472472011-01-22 01:30:25296 IPC_MESSAGE_HANDLER(ViewHostMsg_GoToEntryAtOffset, OnGoToEntryAtOffset)
[email protected]216813952011-05-19 22:21:26297 IPC_MESSAGE_HANDLER(ViewHostMsg_UpdateZoomLimits, OnUpdateZoomLimits)
298 IPC_MESSAGE_HANDLER(ViewHostMsg_FocusedNodeChanged, OnFocusedNodeChanged)
[email protected]c7dd2f62011-07-18 15:57:59299 IPC_MESSAGE_HANDLER(ViewHostMsg_SaveURLAs, OnSaveURL)
[email protected]3a29a6e2011-08-24 18:26:21300 IPC_MESSAGE_HANDLER(ViewHostMsg_EnumerateDirectory, OnEnumerateDirectory)
[email protected]7d189022011-08-25 22:54:20301 IPC_MESSAGE_HANDLER(ViewHostMsg_JSOutOfMemory, OnJSOutOfMemory)
302 IPC_MESSAGE_HANDLER(ViewHostMsg_RegisterProtocolHandler,
303 OnRegisterProtocolHandler)
304 IPC_MESSAGE_HANDLER(ViewHostMsg_RegisterIntentHandler,
305 OnRegisterIntentHandler)
[email protected]b888919c2011-09-02 00:32:16306 IPC_MESSAGE_HANDLER(ViewHostMsg_Find_Reply, OnFindReply)
[email protected]d952a052011-09-06 18:42:45307 IPC_MESSAGE_HANDLER(ViewHostMsg_CrashedPlugin, OnCrashedPlugin)
[email protected]7fc4bbb2011-09-08 21:23:10308 IPC_MESSAGE_HANDLER(ViewHostMsg_AppCacheAccessed, OnAppCacheAccessed)
[email protected]724159a2010-12-30 01:11:18309 IPC_MESSAGE_UNHANDLED(handled = false)
310 IPC_END_MESSAGE_MAP_EX()
311
312 if (!message_is_ok) {
313 UserMetrics::RecordAction(UserMetricsAction("BadMessageTerminate_RVD"));
314 GetRenderProcessHost()->ReceivedBadMessage();
315 }
316
317 return handled;
318}
319
[email protected]6c2e472f2011-08-24 23:26:18320void TabContents::RunFileChooser(
321 RenderViewHost* render_view_host,
322 const ViewHostMsg_RunFileChooser_Params& params) {
323 delegate()->RunFileChooser(this, params);
324}
325
[email protected]8cb5d5b2010-02-09 11:36:16326RenderProcessHost* TabContents::GetRenderProcessHost() const {
[email protected]4d0df262011-10-03 20:05:03327 if (render_manager_.current_host())
328 return render_manager_.current_host()->process();
329 else
330 return NULL;
[email protected]8cb5d5b2010-02-09 11:36:16331}
332
[email protected]d5f942ba2008-09-26 19:30:34333const GURL& TabContents::GetURL() const {
334 // We may not have a navigation entry yet
[email protected]ce3fa3c2009-04-20 19:55:57335 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]ebe89e062009-08-13 23:16:54336 return entry ? entry->virtual_url() : GURL::EmptyGURL();
[email protected]d5f942ba2008-09-26 19:30:34337}
338
[email protected]96d185d2009-04-24 03:28:54339const string16& TabContents::GetTitle() const {
[email protected]4c6092c5b2009-06-06 00:23:55340 // Transient entries take precedence. They are used for interstitial pages
341 // that are shown on top of existing pages.
342 NavigationEntry* entry = controller_.GetTransientEntry();
[email protected]b5cca982011-05-26 04:42:08343 std::string accept_languages =
344 content::GetContentClient()->browser()->GetAcceptLangs(this);
[email protected]45d0ef7f2011-01-05 13:46:23345 if (entry) {
[email protected]b5cca982011-05-26 04:42:08346 return entry->GetTitleForDisplay(accept_languages);
[email protected]45d0ef7f2011-01-05 13:46:23347 }
[email protected]7ade2732011-02-10 00:13:58348 WebUI* our_web_ui = render_manager_.pending_web_ui() ?
349 render_manager_.pending_web_ui() : render_manager_.web_ui();
350 if (our_web_ui) {
[email protected]96d185d2009-04-24 03:28:54351 // Don't override the title in view source mode.
[email protected]4c6092c5b2009-06-06 00:23:55352 entry = controller_.GetActiveEntry();
[email protected]96d185d2009-04-24 03:28:54353 if (!(entry && entry->IsViewSourceMode())) {
[email protected]e0112912011-02-02 22:54:35354 // Give the Web UI the chance to override our title.
[email protected]7ade2732011-02-10 00:13:58355 const string16& title = our_web_ui->overridden_title();
[email protected]96d185d2009-04-24 03:28:54356 if (!title.empty())
357 return title;
358 }
359 }
360
361 // We use the title for the last committed entry rather than a pending
362 // navigation entry. For example, when the user types in a URL, we want to
363 // keep the old page's title until the new load has committed and we get a new
364 // title.
[email protected]96d185d2009-04-24 03:28:54365 entry = controller_.GetLastCommittedEntry();
[email protected]45d0ef7f2011-01-05 13:46:23366 if (entry) {
[email protected]b5cca982011-05-26 04:42:08367 return entry->GetTitleForDisplay(accept_languages);
[email protected]45d0ef7f2011-01-05 13:46:23368 }
[email protected]987fc3a2011-05-26 14:18:09369
370 // |page_title_when_no_navigation_entry_| is finally used
371 // if no title cannot be retrieved.
372 return page_title_when_no_navigation_entry_;
[email protected]96d185d2009-04-24 03:28:54373}
374
[email protected]d5f942ba2008-09-26 19:30:34375int32 TabContents::GetMaxPageID() {
376 if (GetSiteInstance())
377 return GetSiteInstance()->max_page_id();
378 else
379 return max_page_id_;
380}
381
382void TabContents::UpdateMaxPageID(int32 page_id) {
383 // Ensure both the SiteInstance and RenderProcessHost update their max page
[email protected]57c6a652009-05-04 07:58:34384 // IDs in sync. Only TabContents will also have site instances, except during
[email protected]d5f942ba2008-09-26 19:30:34385 // testing.
386 if (GetSiteInstance())
387 GetSiteInstance()->UpdateMaxPageID(page_id);
[email protected]8cb5d5b2010-02-09 11:36:16388 GetRenderProcessHost()->UpdateMaxPageID(page_id);
[email protected]d5f942ba2008-09-26 19:30:34389}
390
[email protected]96d185d2009-04-24 03:28:54391SiteInstance* TabContents::GetSiteInstance() const {
392 return render_manager_.current_host()->site_instance();
393}
394
[email protected]77362eb2011-08-01 17:18:38395SiteInstance* TabContents::GetPendingSiteInstance() const {
396 RenderViewHost* dest_rvh = render_manager_.pending_render_view_host() ?
397 render_manager_.pending_render_view_host() :
398 render_manager_.current_host();
399 return dest_rvh->site_instance();
400}
401
[email protected]96d185d2009-04-24 03:28:54402bool TabContents::ShouldDisplayURL() {
[email protected]8ab24cc2009-11-13 20:56:15403 // Don't hide the url in view source mode and with interstitials.
[email protected]96d185d2009-04-24 03:28:54404 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]8ab24cc2009-11-13 20:56:15405 if (entry && (entry->IsViewSourceMode() ||
[email protected]cccd3762010-11-12 18:40:01406 entry->page_type() == INTERSTITIAL_PAGE)) {
[email protected]96d185d2009-04-24 03:28:54407 return true;
[email protected]8ab24cc2009-11-13 20:56:15408 }
[email protected]83e3895b2009-06-11 00:07:16409
[email protected]2b9a06402011-02-16 04:57:10410 // We always display the URL for non-WebUI URLs to prevent spoofing.
[email protected]1fd1a502011-03-30 16:55:56411 if (entry && !content::WebUIFactory::Get()->HasWebUIScheme(entry->url()))
[email protected]2b9a06402011-02-16 04:57:10412 return true;
413
[email protected]d0980792011-02-13 19:41:40414 WebUI* web_ui = GetWebUIForCurrentState();
[email protected]7ade2732011-02-10 00:13:58415 if (web_ui)
416 return !web_ui->should_hide_url();
[email protected]96d185d2009-04-24 03:28:54417 return true;
418}
419
[email protected]f5d978c2011-07-21 14:43:51420bool TabContents::IsLoading() const {
421 return is_loading_ || (web_ui() && web_ui()->IsLoading());
422}
423
[email protected]585b30362011-01-28 02:30:17424void TabContents::AddObserver(TabContentsObserver* observer) {
425 observers_.AddObserver(observer);
[email protected]3c9e1872010-11-18 16:17:49426}
427
[email protected]585b30362011-01-28 02:30:17428void TabContents::RemoveObserver(TabContentsObserver* observer) {
429 observers_.RemoveObserver(observer);
[email protected]3c9e1872010-11-18 16:17:49430}
431
[email protected]443b80e2010-12-14 00:42:23432void TabContents::SetIsCrashed(base::TerminationStatus status, int error_code) {
433 if (status == crashed_status_)
[email protected]d5f942ba2008-09-26 19:30:34434 return;
435
[email protected]443b80e2010-12-14 00:42:23436 crashed_status_ = status;
437 crashed_error_code_ = error_code;
[email protected]c9cd2222009-05-06 05:16:50438 NotifyNavigationStateChanged(INVALIDATE_TAB);
[email protected]d5f942ba2008-09-26 19:30:34439}
440
441void TabContents::NotifyNavigationStateChanged(unsigned changed_flags) {
442 if (delegate_)
443 delegate_->NavigationStateChanged(this, changed_flags);
444}
445
[email protected]96d185d2009-04-24 03:28:54446void TabContents::DidBecomeSelected() {
447 controller_.SetActive(true);
[email protected]8cb5d5b2010-02-09 11:36:16448 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
449 if (rwhv) {
450 rwhv->DidBecomeSelected();
[email protected]789e9152009-08-04 21:59:43451#if defined(OS_MACOSX)
[email protected]8cb5d5b2010-02-09 11:36:16452 rwhv->SetActive(true);
[email protected]789e9152009-08-04 21:59:43453#endif
454 }
[email protected]96d185d2009-04-24 03:28:54455
[email protected]5ac20162010-11-24 23:33:11456 last_selected_time_ = base::TimeTicks::Now();
[email protected]3e69bc82011-05-26 23:22:38457
458 FOR_EACH_OBSERVER(TabContentsObserver, observers_, DidBecomeSelected());
[email protected]96d185d2009-04-24 03:28:54459}
460
461void TabContents::WasHidden() {
462 if (!capturing_contents()) {
463 // |render_view_host()| can be NULL if the user middle clicks a link to open
464 // a tab in then background, then closes the tab before selecting it. This
465 // is because closing the tab calls TabContents::Destroy(), which removes
466 // the |render_view_host()|; then when we actually destroy the window,
467 // OnWindowPosChanged() notices and calls HideContents() (which calls us).
[email protected]8cb5d5b2010-02-09 11:36:16468 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
469 if (rwhv)
470 rwhv->WasHidden();
[email protected]96d185d2009-04-24 03:28:54471 }
472
473 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:27474 content::NOTIFICATION_TAB_CONTENTS_HIDDEN,
[email protected]96d185d2009-04-24 03:28:54475 Source<TabContents>(this),
476 NotificationService::NoDetails());
477}
478
[email protected]d5f942ba2008-09-26 19:30:34479void TabContents::Activate() {
480 if (delegate_)
481 delegate_->ActivateContents(this);
482}
483
[email protected]ea42e7782010-08-23 23:58:12484void TabContents::Deactivate() {
485 if (delegate_)
486 delegate_->DeactivateContents(this);
487}
488
[email protected]63954792011-07-11 04:17:48489void TabContents::LostCapture() {
490 if (delegate_)
491 delegate_->LostCapture();
492}
493
494bool TabContents::PreHandleKeyboardEvent(const NativeWebKeyboardEvent& event,
495 bool* is_keyboard_shortcut) {
496 return delegate_ &&
497 delegate_->PreHandleKeyboardEvent(event, is_keyboard_shortcut);
498}
499
500void TabContents::HandleKeyboardEvent(const NativeWebKeyboardEvent& event) {
501 if (delegate_)
502 delegate_->HandleKeyboardEvent(event);
503}
504
505void TabContents::HandleMouseUp() {
506 if (delegate_)
507 delegate_->HandleMouseUp();
508}
509
510void TabContents::HandleMouseActivate() {
511 if (delegate_)
512 delegate_->HandleMouseActivate();
513}
514
[email protected]8a5e0ca2011-08-25 06:30:47515void TabContents::ToggleFullscreenMode(bool enter_fullscreen) {
516 if (delegate_)
517 delegate_->ToggleFullscreenModeForTab(this, enter_fullscreen);
518}
519
[email protected]5d5f7af2011-10-01 01:38:12520bool TabContents::IsFullscreenForCurrentTab() const {
521 return delegate_ ? delegate_->IsFullscreenForTab(this) : false;
522}
523
[email protected]0548c5352011-09-07 00:33:33524void TabContents::UpdatePreferredSize(const gfx::Size& pref_size) {
525 if (delegate_)
526 delegate_->UpdatePreferredSize(this, pref_size);
527}
528
[email protected]96d185d2009-04-24 03:28:54529void TabContents::ShowContents() {
[email protected]8cb5d5b2010-02-09 11:36:16530 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
531 if (rwhv)
532 rwhv->DidBecomeSelected();
[email protected]96d185d2009-04-24 03:28:54533}
534
535void TabContents::HideContents() {
536 // TODO(pkasting): http://b/1239839 Right now we purposefully don't call
537 // our superclass HideContents(), because some callers want to be very picky
538 // about the order in which these get called. In addition to making the code
539 // here practically impossible to understand, this also means we end up
540 // calling TabContents::WasHidden() twice if callers call both versions of
[email protected]57c6a652009-05-04 07:58:34541 // HideContents() on a TabContents.
[email protected]96d185d2009-04-24 03:28:54542 WasHidden();
543}
544
[email protected]5aab5e22010-12-08 22:13:29545bool TabContents::NeedToFireBeforeUnload() {
546 // TODO(creis): Should we fire even for interstitial pages?
547 return notify_disconnection() &&
548 !showing_interstitial_page() &&
549 !render_view_host()->SuddenTerminationAllowed();
550}
551
[email protected]00c37fc2011-08-02 00:22:50552// TODO(adriansc): Remove this method once refactoring changed all call sites.
[email protected]992e4542011-07-20 23:09:25553TabContents* TabContents::OpenURL(const GURL& url,
554 const GURL& referrer,
555 WindowOpenDisposition disposition,
556 PageTransition::Type transition) {
[email protected]00c37fc2011-08-02 00:22:50557 return OpenURL(OpenURLParams(url, referrer, disposition, transition));
558}
559
560TabContents* TabContents::OpenURL(const OpenURLParams& params) {
[email protected]1c642b52011-04-15 09:05:49561 if (delegate_) {
[email protected]00c37fc2011-08-02 00:22:50562 TabContents* new_contents = delegate_->OpenURLFromTab(this, params);
[email protected]1c642b52011-04-15 09:05:49563 // Notify observers.
564 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
[email protected]00c37fc2011-08-02 00:22:50565 DidOpenURL(params.url, params.referrer,
566 params.disposition, params.transition));
[email protected]992e4542011-07-20 23:09:25567 return new_contents;
[email protected]1c642b52011-04-15 09:05:49568 }
[email protected]992e4542011-07-20 23:09:25569 return NULL;
[email protected]d5f942ba2008-09-26 19:30:34570}
571
[email protected]1ccb3568d2010-02-19 10:51:16572bool TabContents::NavigateToPendingEntry(
573 NavigationController::ReloadType reload_type) {
[email protected]876bc832010-09-07 16:29:54574 return NavigateToEntry(*controller_.pending_entry(), reload_type);
575}
[email protected]96d185d2009-04-24 03:28:54576
[email protected]876bc832010-09-07 16:29:54577bool TabContents::NavigateToEntry(
578 const NavigationEntry& entry,
579 NavigationController::ReloadType reload_type) {
[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]ffc906f2011-10-04 22:55:40585 RenderViewHost* dest_render_view_host = render_manager_.Navigate(entry);
586 if (!dest_render_view_host)
587 return false; // Unable to create the desired render view host.
588
[email protected]80a8fad2011-01-29 04:02:38589 // For security, we should never send non-Web-UI URLs to a Web UI renderer.
[email protected]05fdd492010-11-15 17:52:07590 // Double check that here.
[email protected]54ec6472010-04-09 19:39:58591 int enabled_bindings = dest_render_view_host->enabled_bindings();
[email protected]1fd1a502011-03-30 16:55:56592 bool is_allowed_in_web_ui_renderer = content::GetContentClient()->
[email protected]3d7474ff2011-07-27 17:47:37593 browser()->GetWebUIFactory()->IsURLAcceptableForWebUI(browser_context(),
[email protected]1fd1a502011-03-30 16:55:56594 entry.url());
[email protected]c09163a2011-02-15 00:05:55595 CHECK(!BindingsPolicy::is_web_ui_enabled(enabled_bindings) ||
596 is_allowed_in_web_ui_renderer);
[email protected]54ec6472010-04-09 19:39:58597
[email protected]96d185d2009-04-24 03:28:54598 // Tell DevTools agent that it is attached prior to the navigation.
[email protected]d9f9b792009-06-24 13:17:12599 DevToolsManager* devtools_manager = DevToolsManager::GetInstance();
[email protected]7e8e6b62009-05-08 11:28:32600 if (devtools_manager) { // NULL in unit tests.
[email protected]3cc72b12010-03-18 23:03:00601 devtools_manager->OnNavigatingToPendingEntry(render_view_host(),
602 dest_render_view_host,
603 entry.url());
[email protected]7e8e6b62009-05-08 11:28:32604 }
[email protected]96d185d2009-04-24 03:28:54605
606 // Used for page load time metrics.
607 current_load_start_ = base::TimeTicks::Now();
608
609 // Navigate in the desired RenderViewHost.
[email protected]056de2d2009-06-26 16:41:34610 ViewMsg_Navigate_Params navigate_params;
[email protected]6c6b02d2011-09-02 03:36:47611 MakeNavigateParams(entry, controller_, delegate_, reload_type,
612 &navigate_params);
[email protected]056de2d2009-06-26 16:41:34613 dest_render_view_host->Navigate(navigate_params);
[email protected]96d185d2009-04-24 03:28:54614
615 if (entry.page_id() == -1) {
616 // HACK!! This code suppresses javascript: URLs from being added to
617 // session history, which is what we want to do for javascript: URLs that
618 // do not generate content. What we really need is a message from the
619 // renderer telling us that a new page was not created. The same message
620 // could be used for mailto: URLs and the like.
621 if (entry.url().SchemeIs(chrome::kJavaScriptScheme))
622 return false;
623 }
624
[email protected]3c9e1872010-11-18 16:17:49625 // Notify observers about navigation.
[email protected]b375c5d2011-05-03 21:15:04626 FOR_EACH_OBSERVER(TabContentsObserver,
627 observers_,
628 NavigateToPendingEntry(entry.url(), reload_type));
[email protected]96d185d2009-04-24 03:28:54629
[email protected]09b29342011-06-24 19:18:48630 if (delegate_)
631 delegate_->DidNavigateToPendingEntry(this);
632
[email protected]d5f942ba2008-09-26 19:30:34633 return true;
634}
635
[email protected]9e1ad4b2011-08-14 16:49:19636void TabContents::SetHistoryLengthAndPrune(const SiteInstance* site_instance,
637 int history_length,
638 int32 minimum_page_id) {
[email protected]5229253b2011-08-16 14:36:41639 // SetHistoryLengthAndPrune doesn't work when there are pending cross-site
640 // navigations. Callers should ensure that this is the case.
[email protected]9e1ad4b2011-08-14 16:49:19641 if (render_manager_.pending_render_view_host()) {
642 NOTREACHED();
[email protected]796931a92011-08-10 01:32:14643 return;
[email protected]9e1ad4b2011-08-14 16:49:19644 }
645 RenderViewHost* rvh = render_view_host();
646 if (!rvh) {
647 NOTREACHED();
648 return;
649 }
650 if (site_instance && rvh->site_instance() != site_instance) {
651 NOTREACHED();
652 return;
653 }
654 rvh->Send(new ViewMsg_SetHistoryLengthAndPrune(rvh->routing_id(),
655 history_length,
656 minimum_page_id));
[email protected]796931a92011-08-10 01:32:14657}
658
[email protected]96d185d2009-04-24 03:28:54659void TabContents::Stop() {
660 render_manager_.Stop();
[email protected]2e3f4572011-03-25 19:24:47661 FOR_EACH_OBSERVER(TabContentsObserver, observers_, StopNavigation());
[email protected]96d185d2009-04-24 03:28:54662}
663
[email protected]96d185d2009-04-24 03:28:54664TabContents* TabContents::Clone() {
665 // We create a new SiteInstance so that the new tab won't share processes
666 // with the old one. This can be changed in the future if we need it to share
667 // processes for some reason.
[email protected]3d7474ff2011-07-27 17:47:37668 TabContents* tc = new TabContents(
669 browser_context(),
670 SiteInstance::CreateSiteInstance(browser_context()),
671 MSG_ROUTING_NONE, this, NULL);
[email protected]96d185d2009-04-24 03:28:54672 tc->controller().CopyStateFrom(controller_);
673 return tc;
674}
675
[email protected]4d677202009-07-19 07:37:12676void TabContents::ShowPageInfo(const GURL& url,
677 const NavigationEntry::SSLStatus& ssl,
678 bool show_history) {
679 if (!delegate_)
680 return;
681
[email protected]3d7474ff2011-07-27 17:47:37682 delegate_->ShowPageInfo(browser_context(), url, ssl, show_history);
[email protected]4d677202009-07-19 07:37:12683}
684
[email protected]e7cfdbd2011-04-22 14:41:37685void TabContents::AddNewContents(TabContents* new_contents,
686 WindowOpenDisposition disposition,
687 const gfx::Rect& initial_pos,
688 bool user_gesture) {
initial.commit09911bf2008-07-26 23:55:29689 if (!delegate_)
690 return;
691
[email protected]e7cfdbd2011-04-22 14:41:37692 delegate_->AddNewContents(this, new_contents, disposition, initial_pos,
693 user_gesture);
[email protected]96d185d2009-04-24 03:28:54694}
695
[email protected]5c9e97a2009-09-09 23:48:30696gfx::NativeView TabContents::GetContentNativeView() const {
[email protected]96d185d2009-04-24 03:28:54697 return view_->GetContentNativeView();
698}
699
700gfx::NativeView TabContents::GetNativeView() const {
701 return view_->GetNativeView();
702}
703
704void TabContents::GetContainerBounds(gfx::Rect *out) const {
705 view_->GetContainerBounds(out);
706}
707
708void TabContents::Focus() {
709 view_->Focus();
710}
711
[email protected]90daadb42009-06-08 21:27:28712void TabContents::FocusThroughTabTraversal(bool reverse) {
[email protected]7e383692009-06-12 19:14:54713 if (showing_interstitial_page()) {
714 render_manager_.interstitial_page()->FocusThroughTabTraversal(reverse);
715 return;
716 }
[email protected]96d185d2009-04-24 03:28:54717 render_view_host()->SetInitialFocus(reverse);
718}
719
720bool TabContents::FocusLocationBarByDefault() {
[email protected]d0980792011-02-13 19:41:40721 WebUI* web_ui = GetWebUIForCurrentState();
[email protected]7ade2732011-02-10 00:13:58722 if (web_ui)
723 return web_ui->focus_location_bar_by_default();
[email protected]3e3f0eb2009-06-22 18:33:43724 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]e0d481582009-09-15 21:06:25725 if (entry && entry->url() == GURL(chrome::kAboutBlankURL))
[email protected]3e3f0eb2009-06-22 18:33:43726 return true;
[email protected]96d185d2009-04-24 03:28:54727 return false;
728}
729
[email protected]a26dc362010-04-23 01:48:58730void TabContents::SetFocusToLocationBar(bool select_all) {
[email protected]a11aaf22010-03-30 00:03:38731 if (delegate())
[email protected]a26dc362010-04-23 01:48:58732 delegate()->SetFocusToLocationBar(select_all);
[email protected]a11aaf22010-03-30 00:03:38733}
734
[email protected]686493142011-07-15 21:47:22735bool TabContents::CanDownload(int request_id) {
736 TabContentsDelegate* d = delegate();
737 if (d)
738 return d->CanDownload(this, request_id);
739 return true;
740}
741
742void TabContents::OnStartDownload(DownloadItem* download) {
743 TabContentsDelegate* d = delegate();
744 if (d)
745 d->OnStartDownload(this, download);
746}
747
[email protected]c7dd2f62011-07-18 15:57:59748void TabContents::OnSavePage() {
749 // If we can not save the page, try to download it.
750 if (!SavePackage::IsSavableContents(contents_mime_type())) {
[email protected]3d7474ff2011-07-27 17:47:37751 DownloadManager* dlm = browser_context()->GetDownloadManager();
[email protected]c7dd2f62011-07-18 15:57:59752 const GURL& current_page_url = GetURL();
753 if (dlm && current_page_url.is_valid()) {
754 dlm->DownloadUrl(current_page_url, GURL(), "", this);
[email protected]8bd9e562011-08-16 23:55:46755 download_stats::RecordDownloadCount(
756 download_stats::INITIATED_BY_SAVE_PACKAGE_FAILURE_COUNT);
[email protected]c7dd2f62011-07-18 15:57:59757 return;
758 }
759 }
760
761 Stop();
762
763 // Create the save package and possibly prompt the user for the name to save
764 // the page as. The user prompt is an asynchronous operation that runs on
765 // another thread.
766 save_package_ = new SavePackage(this);
767 save_package_->GetSaveInfo();
768}
769
770// Used in automated testing to bypass prompting the user for file names.
771// Instead, the names and paths are hard coded rather than running them through
772// file name sanitation and extension / mime checking.
773bool TabContents::SavePage(const FilePath& main_file, const FilePath& dir_path,
774 SavePackage::SavePackageType save_type) {
775 // Stop the page from navigating.
776 Stop();
777
778 save_package_ = new SavePackage(this, save_type, main_file, dir_path);
779 return save_package_->Init();
780}
781
782void TabContents::OnSaveURL(const GURL& url) {
[email protected]3d7474ff2011-07-27 17:47:37783 DownloadManager* dlm = browser_context()->GetDownloadManager();
[email protected]c7dd2f62011-07-18 15:57:59784 dlm->DownloadUrl(url, GetURL(), "", this);
785}
786
[email protected]420ae012009-04-24 05:16:32787bool TabContents::IsActiveEntry(int32 page_id) {
788 NavigationEntry* active_entry = controller_.GetActiveEntry();
789 return (active_entry != NULL &&
790 active_entry->site_instance() == GetSiteInstance() &&
791 active_entry->page_id() == page_id);
792}
793
[email protected]b2fe07d12010-02-09 14:38:08794void TabContents::SetOverrideEncoding(const std::string& encoding) {
[email protected]8cb5d5b2010-02-09 11:36:16795 set_encoding(encoding);
[email protected]216813952011-05-19 22:21:26796 render_view_host()->Send(new ViewMsg_SetPageEncoding(
797 render_view_host()->routing_id(), encoding));
[email protected]8cb5d5b2010-02-09 11:36:16798}
799
[email protected]b2fe07d12010-02-09 14:38:08800void TabContents::ResetOverrideEncoding() {
[email protected]8cb5d5b2010-02-09 11:36:16801 reset_encoding();
[email protected]216813952011-05-19 22:21:26802 render_view_host()->Send(new ViewMsg_ResetPageEncodingToDefault(
803 render_view_host()->routing_id()));
[email protected]8cb5d5b2010-02-09 11:36:16804}
805
[email protected]7ab1e7d62009-10-14 23:32:01806void TabContents::OnCloseStarted() {
807 if (tab_close_start_time_.is_null())
808 tab_close_start_time_ = base::TimeTicks::Now();
809}
810
[email protected]46624bf2010-06-09 16:04:19811bool TabContents::ShouldAcceptDragAndDrop() const {
812#if defined(OS_CHROMEOS)
813 // ChromeOS panels (pop-ups) do not take drag-n-drop.
814 // See http://crosbug.com/2413
[email protected]b35b26b32011-05-05 20:35:14815 if (delegate() && delegate()->IsPopupOrPanel(this))
[email protected]b83af492010-10-09 03:21:20816 return false;
817 return true;
[email protected]46624bf2010-06-09 16:04:19818#else
819 return true;
820#endif
821}
822
[email protected]7813bd72011-02-05 02:19:34823void TabContents::SystemDragEnded() {
824 if (render_view_host())
825 render_view_host()->DragSourceSystemDragEnded();
826 if (delegate())
827 delegate()->DragEnded();
828}
829
[email protected]d0b8d092010-10-25 04:05:17830double TabContents::GetZoomLevel() const {
[email protected]3d7474ff2011-07-27 17:47:37831 HostZoomMap* zoom_map = browser_context()->GetHostZoomMap();
[email protected]b75b8292010-10-01 07:28:25832 if (!zoom_map)
[email protected]d0b8d092010-10-25 04:05:17833 return 0;
[email protected]b75b8292010-10-01 07:28:25834
835 double zoom_level;
836 if (temporary_zoom_settings_) {
837 zoom_level = zoom_map->GetTemporaryZoomLevel(
838 GetRenderProcessHost()->id(), render_view_host()->routing_id());
839 } else {
[email protected]13ffa32e2011-05-27 16:37:01840 zoom_level = zoom_map->GetZoomLevel(net::GetHostOrSpecFromURL(GetURL()));
[email protected]b75b8292010-10-01 07:28:25841 }
[email protected]d0b8d092010-10-25 04:05:17842 return zoom_level;
843}
[email protected]b75b8292010-10-01 07:28:25844
[email protected]d0b8d092010-10-25 04:05:17845int TabContents::GetZoomPercent(bool* enable_increment,
846 bool* enable_decrement) {
847 *enable_decrement = *enable_increment = false;
[email protected]b75b8292010-10-01 07:28:25848 int percent = static_cast<int>(
[email protected]d0b8d092010-10-25 04:05:17849 WebKit::WebView::zoomLevelToZoomFactor(GetZoomLevel()) * 100);
[email protected]b75b8292010-10-01 07:28:25850 *enable_decrement = percent > minimum_zoom_percent_;
851 *enable_increment = percent < maximum_zoom_percent_;
852 return percent;
853}
854
[email protected]3c733bde2010-12-21 19:56:31855void TabContents::ViewSource() {
[email protected]1788e772010-12-15 16:40:50856 if (!delegate_)
857 return;
858
859 NavigationEntry* active_entry = controller().GetActiveEntry();
860 if (!active_entry)
861 return;
862
863 delegate_->ViewSourceForTab(this, active_entry->url());
[email protected]77d8d622010-12-15 10:30:12864}
865
[email protected]932b7a12011-03-09 12:50:27866void TabContents::ViewFrameSource(const GURL& url,
867 const std::string& content_state) {
868 if (!delegate_)
869 return;
870
871 delegate_->ViewSourceForFrame(this, url, content_state);
872}
873
[email protected]c40d6232011-03-25 00:16:21874void TabContents::SetContentRestrictions(int restrictions) {
875 content_restrictions_ = restrictions;
876 delegate()->ContentRestrictionsChanged(this);
877}
878
[email protected]724159a2010-12-30 01:11:18879void TabContents::OnDidStartProvisionalLoadForFrame(int64 frame_id,
880 bool is_main_frame,
[email protected]eacb080b2011-05-22 19:40:26881 bool has_opener_set,
[email protected]724159a2010-12-30 01:11:18882 const GURL& url) {
883 bool is_error_page = (url.spec() == chrome::kUnreachableWebDataURL);
884 GURL validated_url(url);
885 render_view_host()->FilterURL(ChildProcessSecurityPolicy::GetInstance(),
886 GetRenderProcessHost()->id(), &validated_url);
887
[email protected]8093a542011-05-13 07:29:32888 RenderViewHost* rvh =
889 render_manager_.pending_render_view_host() ?
890 render_manager_.pending_render_view_host() : render_view_host();
[email protected]0d60f0192011-04-14 12:40:10891 // Notify observers about the start of the provisional load.
892 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
893 DidStartProvisionalLoadForFrame(frame_id, is_main_frame,
[email protected]8093a542011-05-13 07:29:32894 validated_url, is_error_page, rvh));
[email protected]0d60f0192011-04-14 12:40:10895
[email protected]724159a2010-12-30 01:11:18896 if (is_main_frame) {
[email protected]4850a7f2011-03-08 23:36:59897 // Notify observers about the provisional change in the main frame URL.
898 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
[email protected]eacb080b2011-05-22 19:40:26899 ProvisionalChangeToMainFrameUrl(url, has_opener_set));
[email protected]724159a2010-12-30 01:11:18900 }
901}
902
903void TabContents::OnDidRedirectProvisionalLoad(int32 page_id,
[email protected]eacb080b2011-05-22 19:40:26904 bool has_opener_set,
[email protected]724159a2010-12-30 01:11:18905 const GURL& source_url,
906 const GURL& target_url) {
[email protected]18ba2b12011-04-06 16:35:49907 // TODO(creis): Remove this method and have the pre-rendering code listen to
908 // the ResourceDispatcherHost's RESOURCE_RECEIVED_REDIRECT notification
909 // instead. See http://crbug.com/78512.
[email protected]724159a2010-12-30 01:11:18910 NavigationEntry* entry;
911 if (page_id == -1)
912 entry = controller_.pending_entry();
913 else
914 entry = controller_.GetEntryWithPageID(GetSiteInstance(), page_id);
915 if (!entry || entry->url() != source_url)
916 return;
[email protected]e7d50892011-01-19 21:47:38917
[email protected]4850a7f2011-03-08 23:36:59918 // Notify observers about the provisional change in the main frame URL.
919 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
[email protected]eacb080b2011-05-22 19:40:26920 ProvisionalChangeToMainFrameUrl(target_url,
921 has_opener_set));
[email protected]724159a2010-12-30 01:11:18922}
923
924void TabContents::OnDidFailProvisionalLoadWithError(
925 int64 frame_id,
926 bool is_main_frame,
927 int error_code,
928 const GURL& url,
929 bool showing_repost_interstitial) {
930 VLOG(1) << "Failed Provisional Load: " << url.possibly_invalid_spec()
931 << ", error_code: " << error_code
932 << " is_main_frame: " << is_main_frame
933 << " showing_repost_interstitial: " << showing_repost_interstitial
934 << " frame_id: " << frame_id;
935 GURL validated_url(url);
936 render_view_host()->FilterURL(ChildProcessSecurityPolicy::GetInstance(),
937 GetRenderProcessHost()->id(), &validated_url);
938
939 if (net::ERR_ABORTED == error_code) {
940 // EVIL HACK ALERT! Ignore failed loads when we're showing interstitials.
941 // This means that the interstitial won't be torn down properly, which is
942 // bad. But if we have an interstitial, go back to another tab type, and
943 // then load the same interstitial again, we could end up getting the first
944 // interstitial's "failed" message (as a result of the cancel) when we're on
945 // the second one.
946 //
947 // We can't tell this apart, so we think we're tearing down the current page
948 // which will cause a crash later one. There is also some code in
949 // RenderViewHostManager::RendererAbortedProvisionalLoad that is commented
950 // out because of this problem.
951 //
952 // http://code.google.com/p/chromium/issues/detail?id=2855
953 // Because this will not tear down the interstitial properly, if "back" is
954 // back to another tab type, the interstitial will still be somewhat alive
955 // in the previous tab type. If you navigate somewhere that activates the
956 // tab with the interstitial again, you'll see a flash before the new load
957 // commits of the interstitial page.
958 if (showing_interstitial_page()) {
959 LOG(WARNING) << "Discarding message during interstitial.";
960 return;
961 }
962
[email protected]02102f82011-06-13 20:37:02963 // Discard our pending entry if the load canceled (e.g. if we decided to
964 // download the file instead of load it). We do not verify that the URL
965 // being canceled matches the pending entry's URL because they will not
966 // match if a redirect occurred (in which case we do not want to leave a
967 // stale redirect URL showing). This means that we also cancel the pending
968 // entry if the user started a new navigation. As a result, the navigation
969 // controller may not remember that a load is in progress, but the
970 // navigation will still commit even if there is no pending entry.
[email protected]724159a2010-12-30 01:11:18971 NavigationEntry* pending_entry = controller_.pending_entry();
[email protected]02102f82011-06-13 20:37:02972 if (pending_entry)
[email protected]c95fa8b2011-04-28 20:26:16973 DidCancelLoading();
[email protected]724159a2010-12-30 01:11:18974
975 render_manager_.RendererAbortedProvisionalLoad(render_view_host());
976 }
977
978 // Send out a notification that we failed a provisional load with an error.
979 ProvisionalLoadDetails details(
980 is_main_frame, controller_.IsURLInPageNavigation(validated_url),
[email protected]b9e8ea62011-03-04 06:29:09981 validated_url, std::string(), false, frame_id);
[email protected]724159a2010-12-30 01:11:18982 details.set_error_code(error_code);
983
984 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:27985 content::NOTIFICATION_FAIL_PROVISIONAL_LOAD_WITH_ERROR,
[email protected]724159a2010-12-30 01:11:18986 Source<NavigationController>(&controller_),
987 Details<ProvisionalLoadDetails>(&details));
[email protected]0d60f0192011-04-14 12:40:10988
989 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
990 DidFailProvisionalLoad(frame_id, is_main_frame,
991 validated_url, error_code));
[email protected]724159a2010-12-30 01:11:18992}
993
994void TabContents::OnDidLoadResourceFromMemoryCache(
995 const GURL& url,
[email protected]70435962011-08-02 20:13:28996 const std::string& security_info,
997 const std::string& http_method,
998 ResourceType::Type resource_type) {
[email protected]28620862011-03-22 23:07:19999 base::StatsCounter cache("WebKit.CacheHit");
[email protected]724159a2010-12-30 01:11:181000 cache.Increment();
1001
1002 // Send out a notification that we loaded a resource from our memory cache.
[email protected]70d66502011-09-23 00:55:081003 int cert_id = 0;
1004 net::CertStatus cert_status = 0;
1005 int security_bits = -1;
1006 int connection_status = 0;
[email protected]724159a2010-12-30 01:11:181007 SSLManager::DeserializeSecurityInfo(security_info,
1008 &cert_id, &cert_status,
1009 &security_bits,
1010 &connection_status);
[email protected]7a8c55e2011-02-15 08:21:161011 LoadFromMemoryCacheDetails details(url, GetRenderProcessHost()->id(),
1012 cert_id, cert_status);
[email protected]724159a2010-12-30 01:11:181013
1014 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271015 content::NOTIFICATION_LOAD_FROM_MEMORY_CACHE,
[email protected]724159a2010-12-30 01:11:181016 Source<NavigationController>(&controller_),
1017 Details<LoadFromMemoryCacheDetails>(&details));
1018}
1019
1020void TabContents::OnDidDisplayInsecureContent() {
[email protected]221a5d92011-04-28 18:23:161021 UserMetrics::RecordAction(UserMetricsAction("SSL.DisplayedInsecureContent"));
[email protected]724159a2010-12-30 01:11:181022 displayed_insecure_content_ = true;
[email protected]9678d8f2011-08-05 04:05:071023 SSLManager::NotifySSLInternalStateChanged(&controller());
[email protected]724159a2010-12-30 01:11:181024}
1025
1026void TabContents::OnDidRunInsecureContent(
[email protected]92771112011-01-20 00:13:021027 const std::string& security_origin, const GURL& target_url) {
[email protected]9941cf52011-02-08 16:17:231028 LOG(INFO) << security_origin << " ran insecure content from "
1029 << target_url.possibly_invalid_spec();
[email protected]221a5d92011-04-28 18:23:161030 UserMetrics::RecordAction(UserMetricsAction("SSL.RanInsecureContent"));
[email protected]ca406032011-07-19 21:53:051031 if (EndsWith(security_origin, kDotGoogleDotCom, false)) {
1032 UserMetrics::RecordAction(
1033 UserMetricsAction("SSL.RanInsecureContentGoogle"));
1034 }
[email protected]724159a2010-12-30 01:11:181035 controller_.ssl_manager()->DidRunInsecureContent(security_origin);
[email protected]f2b58c4d2011-06-01 23:29:411036 displayed_insecure_content_ = true;
[email protected]9678d8f2011-08-05 04:05:071037 SSLManager::NotifySSLInternalStateChanged(&controller());
[email protected]724159a2010-12-30 01:11:181038}
1039
1040void TabContents::OnDocumentLoadedInFrame(int64 frame_id) {
1041 controller_.DocumentLoadedInFrame();
[email protected]0d60f0192011-04-14 12:40:101042 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
1043 DocumentLoadedInFrame(frame_id));
[email protected]724159a2010-12-30 01:11:181044}
1045
1046void TabContents::OnDidFinishLoad(int64 frame_id) {
[email protected]0d60f0192011-04-14 12:40:101047 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
1048 DidFinishLoad(frame_id));
[email protected]724159a2010-12-30 01:11:181049}
1050
[email protected]c8f73ab2011-01-22 00:05:171051void TabContents::OnUpdateContentRestrictions(int restrictions) {
[email protected]c40d6232011-03-25 00:16:211052 SetContentRestrictions(restrictions);
[email protected]c8f73ab2011-01-22 00:05:171053}
1054
[email protected]216813952011-05-19 22:21:261055void TabContents::OnGoToEntryAtOffset(int offset) {
1056 if (!delegate_ || delegate_->OnGoToEntryOffset(offset)) {
1057 NavigationEntry* entry = controller_.GetEntryAtOffset(offset);
1058 if (!entry)
1059 return;
1060 // Note that we don't call NavigationController::GotToOffset() as we don't
1061 // want to create a pending navigation entry (it might end up lingering
1062 // http://crbug.com/51680).
1063 entry->set_transition_type(entry->transition_type() |
1064 PageTransition::FORWARD_BACK);
1065 NavigateToEntry(*entry, NavigationController::NO_RELOAD);
[email protected]a13cc362011-07-28 21:29:571066
1067 // If the entry is being restored and doesn't have a SiteInstance yet, fill
1068 // it in now that we know. This allows us to find the entry when it commits.
1069 if (!entry->site_instance() &&
1070 entry->restore_type() != NavigationEntry::RESTORE_NONE) {
[email protected]77362eb2011-08-01 17:18:381071 entry->set_site_instance(GetPendingSiteInstance());
[email protected]a13cc362011-07-28 21:29:571072 }
[email protected]216813952011-05-19 22:21:261073 }
1074}
1075
1076void TabContents::OnUpdateZoomLimits(int minimum_percent,
1077 int maximum_percent,
1078 bool remember) {
1079 minimum_zoom_percent_ = minimum_percent;
1080 maximum_zoom_percent_ = maximum_percent;
1081 temporary_zoom_settings_ = !remember;
1082}
1083
1084void TabContents::OnFocusedNodeChanged(bool is_editable_node) {
1085 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271086 content::NOTIFICATION_FOCUS_CHANGED_IN_PAGE,
[email protected]216813952011-05-19 22:21:261087 Source<TabContents>(this),
1088 Details<const bool>(&is_editable_node));
1089}
1090
[email protected]3a29a6e2011-08-24 18:26:211091void TabContents::OnEnumerateDirectory(int request_id,
1092 const FilePath& path) {
1093 delegate()->EnumerateDirectory(this, request_id, path);
1094}
1095
[email protected]7d189022011-08-25 22:54:201096void TabContents::OnJSOutOfMemory() {
1097 delegate()->JSOutOfMemory(this);
1098}
1099
1100void TabContents::OnRegisterProtocolHandler(const std::string& protocol,
1101 const GURL& url,
1102 const string16& title) {
1103 delegate()->RegisterProtocolHandler(this, protocol, url, title);
1104}
1105
1106void TabContents::OnRegisterIntentHandler(const string16& action,
1107 const string16& type,
1108 const string16& href,
1109 const string16& title) {
1110 delegate()->RegisterIntentHandler(this, action, type, href, title);
1111}
1112
1113void TabContents::OnWebIntentDispatch(const IPC::Message& message,
[email protected]ce9751942011-09-21 01:57:241114 const webkit_glue::WebIntentData& intent,
[email protected]7d189022011-08-25 22:54:201115 int intent_id) {
[email protected]ce9751942011-09-21 01:57:241116 delegate()->WebIntentDispatch(this, message.routing_id(), intent, intent_id);
[email protected]7d189022011-08-25 22:54:201117}
1118
[email protected]b888919c2011-09-02 00:32:161119void TabContents::OnFindReply(int request_id,
1120 int number_of_matches,
1121 const gfx::Rect& selection_rect,
1122 int active_match_ordinal,
1123 bool final_update) {
1124 delegate()->FindReply(this, request_id, number_of_matches, selection_rect,
1125 active_match_ordinal, final_update);
[email protected]4e6239c2011-09-23 00:11:001126 // Send a notification to the renderer that we are ready to receive more
1127 // results from the scoping effort of the Find operation. The FindInPage
1128 // scoping is asynchronous and periodically sends results back up to the
1129 // browser using IPC. In an effort to not spam the browser we have the
1130 // browser send an ACK for each FindReply message and have the renderer
1131 // queue up the latest status message while waiting for this ACK.
1132 render_view_host()->Send(
1133 new ViewMsg_FindReplyACK(render_view_host()->routing_id()));
[email protected]b888919c2011-09-02 00:32:161134}
1135
[email protected]d952a052011-09-06 18:42:451136void TabContents::OnCrashedPlugin(const FilePath& plugin_path) {
1137 delegate()->CrashedPlugin(this, plugin_path);
1138}
1139
[email protected]7fc4bbb2011-09-08 21:23:101140void TabContents::OnAppCacheAccessed(const GURL& manifest_url,
1141 bool blocked_by_policy) {
1142 // Notify observers about navigation.
1143 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
1144 AppCacheAccessed(manifest_url, blocked_by_policy));
1145}
1146
[email protected]96d185d2009-04-24 03:28:541147// Notifies the RenderWidgetHost instance about the fact that the page is
1148// loading, or done loading and calls the base implementation.
1149void TabContents::SetIsLoading(bool is_loading,
1150 LoadNotificationDetails* details) {
1151 if (is_loading == is_loading_)
1152 return;
1153
1154 if (!is_loading) {
[email protected]9c235f042011-08-10 22:28:211155 load_state_ = net::LoadStateWithParam(net::LOAD_STATE_IDLE, string16());
[email protected]96d185d2009-04-24 03:28:541156 load_state_host_.clear();
[email protected]094e5b22009-09-25 04:23:561157 upload_size_ = 0;
1158 upload_position_ = 0;
[email protected]96d185d2009-04-24 03:28:541159 }
1160
1161 render_manager_.SetIsLoading(is_loading);
1162
1163 is_loading_ = is_loading;
1164 waiting_for_response_ = is_loading;
1165
[email protected]6ebdc9b2010-09-27 16:55:571166 if (delegate_)
1167 delegate_->LoadingStateChanged(this);
[email protected]329581b2009-04-28 06:52:351168 NotifyNavigationStateChanged(INVALIDATE_LOAD);
[email protected]96d185d2009-04-24 03:28:541169
[email protected]432115822011-07-10 15:52:271170 int type = is_loading ? content::NOTIFICATION_LOAD_START :
1171 content::NOTIFICATION_LOAD_STOP;
[email protected]ce5c4502009-05-06 16:46:111172 NotificationDetails det = NotificationService::NoDetails();
[email protected]96d185d2009-04-24 03:28:541173 if (details)
1174 det = Details<LoadNotificationDetails>(details);
1175 NotificationService::current()->Notify(type,
1176 Source<NavigationController>(&controller_),
1177 det);
1178}
1179
[email protected]d0980792011-02-13 19:41:401180WebUI* TabContents::GetWebUIForCurrentState() {
[email protected]7ade2732011-02-10 00:13:581181 // When there is a pending navigation entry, we want to use the pending WebUI
[email protected]96d185d2009-04-24 03:28:541182 // that goes along with it to control the basic flags. For example, we want to
1183 // show the pending URL in the URL bar, so we want the display_url flag to
1184 // be from the pending entry.
1185 //
1186 // The confusion comes because there are multiple possibilities for the
1187 // initial load in a tab as a side effect of the way the RenderViewHostManager
1188 // works.
1189 //
[email protected]e0112912011-02-02 22:54:351190 // - For the very first tab the load looks "normal". The new tab Web UI is
[email protected]96d185d2009-04-24 03:28:541191 // the pending one, and we want it to apply here.
1192 //
1193 // - For subsequent new tabs, they'll get a new SiteInstance which will then
1194 // get switched to the one previously associated with the new tab pages.
[email protected]7ade2732011-02-10 00:13:581195 // This switching will cause the manager to commit the RVH/WebUI. So we'll
[email protected]e0112912011-02-02 22:54:351196 // have a committed Web UI in this case.
[email protected]96d185d2009-04-24 03:28:541197 //
1198 // This condition handles all of these cases:
1199 //
1200 // - First load in first tab: no committed nav entry + pending nav entry +
1201 // pending dom ui:
[email protected]e0112912011-02-02 22:54:351202 // -> Use pending Web UI if any.
[email protected]96d185d2009-04-24 03:28:541203 //
1204 // - First load in second tab: no committed nav entry + pending nav entry +
[email protected]e0112912011-02-02 22:54:351205 // no pending Web UI:
1206 // -> Use the committed Web UI if any.
[email protected]96d185d2009-04-24 03:28:541207 //
1208 // - Second navigation in any tab: committed nav entry + pending nav entry:
[email protected]e0112912011-02-02 22:54:351209 // -> Use pending Web UI if any.
[email protected]96d185d2009-04-24 03:28:541210 //
1211 // - Normal state with no load: committed nav entry + no pending nav entry:
[email protected]e0112912011-02-02 22:54:351212 // -> Use committed Web UI.
[email protected]96d185d2009-04-24 03:28:541213 if (controller_.pending_entry() &&
1214 (controller_.GetLastCommittedEntry() ||
[email protected]7ade2732011-02-10 00:13:581215 render_manager_.pending_web_ui()))
1216 return render_manager_.pending_web_ui();
1217 return render_manager_.web_ui();
[email protected]96d185d2009-04-24 03:28:541218}
[email protected]420ae012009-04-24 05:16:321219
[email protected]1fd1a502011-03-30 16:55:561220WebUI::TypeID TabContents::GetWebUITypeForCurrentState() {
[email protected]3d7474ff2011-07-27 17:47:371221 return content::WebUIFactory::Get()->GetWebUIType(browser_context(),
1222 GetURL());
[email protected]1fd1a502011-03-30 16:55:561223}
1224
[email protected]420ae012009-04-24 05:16:321225void TabContents::DidNavigateMainFramePostCommit(
[email protected]8286f51a2011-05-31 17:39:131226 const content::LoadCommittedDetails& details,
[email protected]420ae012009-04-24 05:16:321227 const ViewHostMsg_FrameNavigate_Params& params) {
[email protected]1fd1a502011-03-30 16:55:561228 if (opener_web_ui_type_ != WebUI::kNoWebUI) {
[email protected]80a8fad2011-01-29 04:02:381229 // If this is a window.open navigation, use the same WebUI as the renderer
[email protected]c2e74fe82009-09-03 17:57:441230 // that opened the window, as long as both renderers have the same
1231 // privileges.
[email protected]1fd1a502011-03-30 16:55:561232 if (delegate_ && opener_web_ui_type_ == GetWebUITypeForCurrentState()) {
1233 WebUI* web_ui = content::GetContentClient()->browser()->
1234 GetWebUIFactory()->CreateWebUIForURL(this, GetURL());
[email protected]7ade2732011-02-10 00:13:581235 // web_ui might be NULL if the URL refers to a non-existent extension.
1236 if (web_ui) {
[email protected]d0980792011-02-13 19:41:401237 render_manager_.SetWebUIPostCommit(web_ui);
[email protected]7ade2732011-02-10 00:13:581238 web_ui->RenderViewCreated(render_view_host());
[email protected]c2e74fe82009-09-03 17:57:441239 }
1240 }
[email protected]1fd1a502011-03-30 16:55:561241 opener_web_ui_type_ = WebUI::kNoWebUI;
[email protected]c2e74fe82009-09-03 17:57:441242 }
1243
[email protected]4e697b042011-07-08 06:44:561244 if (details.is_navigation_to_different_page()) {
[email protected]420ae012009-04-24 05:16:321245 // Clear the status bubble. This is a workaround for a bug where WebKit
1246 // doesn't let us know that the cursor left an element during a
1247 // transition (this is also why the mouse cursor remains as a hand after
1248 // clicking on a link); see bugs 1184641 and 980803. We don't want to
1249 // clear the bubble when a user navigates to a named anchor in the same
1250 // page.
1251 UpdateTargetURL(details.entry->page_id(), GURL());
[email protected]420ae012009-04-24 05:16:321252 }
1253
[email protected]a6e82fc2010-02-24 22:28:141254 if (!details.is_in_page) {
[email protected]f17a0ee2010-05-17 17:38:471255 // Once the main frame is navigated, we're no longer considered to have
1256 // displayed insecure content.
1257 displayed_insecure_content_ = false;
[email protected]aece2c7f2009-08-27 20:43:171258 }
[email protected]ce5c4502009-05-06 16:46:111259
[email protected]3c9e1872010-11-18 16:17:491260 // Notify observers about navigation.
[email protected]585b30362011-01-28 02:30:171261 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
[email protected]3c9e1872010-11-18 16:17:491262 DidNavigateMainFramePostCommit(details, params));
[email protected]420ae012009-04-24 05:16:321263}
1264
1265void TabContents::DidNavigateAnyFramePostCommit(
1266 RenderViewHost* render_view_host,
[email protected]8286f51a2011-05-31 17:39:131267 const content::LoadCommittedDetails& details,
[email protected]420ae012009-04-24 05:16:321268 const ViewHostMsg_FrameNavigate_Params& params) {
[email protected]3ab9cb82011-06-03 18:02:071269 // If we navigate, reset JavaScript state. This does nothing to prevent
[email protected]420ae012009-04-24 05:16:321270 // a malicious script from spamming messages, since the script could just
1271 // reload the page to stop blocking.
[email protected]2e5b90c2011-08-16 21:11:551272 if (dialog_creator_) {
1273 dialog_creator_->ResetJavaScriptState(this);
1274 dialog_creator_ = NULL;
1275 }
[email protected]420ae012009-04-24 05:16:321276
[email protected]3c9e1872010-11-18 16:17:491277 // Notify observers about navigation.
[email protected]585b30362011-01-28 02:30:171278 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
[email protected]3c9e1872010-11-18 16:17:491279 DidNavigateAnyFramePostCommit(details, params));
[email protected]420ae012009-04-24 05:16:321280}
1281
[email protected]420ae012009-04-24 05:16:321282void TabContents::UpdateMaxPageIDIfNecessary(SiteInstance* site_instance,
1283 RenderViewHost* rvh) {
1284 // If we are creating a RVH for a restored controller, then we might
1285 // have more page IDs than the SiteInstance's current max page ID. We must
1286 // make sure that the max page ID is larger than any restored page ID.
1287 // Note that it is ok for conflicting page IDs to exist in another tab
1288 // (i.e., NavigationController), but if any page ID is larger than the max,
1289 // the back/forward list will get confused.
1290 int max_restored_page_id = controller_.max_restored_page_id();
1291 if (max_restored_page_id > 0) {
1292 int curr_max_page_id = site_instance->max_page_id();
1293 if (max_restored_page_id > curr_max_page_id) {
1294 // Need to update the site instance immediately.
1295 site_instance->UpdateMaxPageID(max_restored_page_id);
1296
1297 // Also tell the renderer to update its internal representation. We
1298 // need to reserve enough IDs to make all restored page IDs less than
1299 // the max.
1300 if (curr_max_page_id < 0)
1301 curr_max_page_id = 0;
[email protected]216813952011-05-19 22:21:261302 rvh->Send(new ViewMsg_ReservePageIDRange(
1303 rvh->routing_id(), max_restored_page_id - curr_max_page_id));
[email protected]420ae012009-04-24 05:16:321304 }
1305 }
1306}
1307
[email protected]6b2f7a82011-04-25 19:30:511308bool TabContents::UpdateTitleForEntry(NavigationEntry* entry,
[email protected]acafd272011-07-26 17:35:571309 const string16& title) {
[email protected]420ae012009-04-24 05:16:321310 // For file URLs without a title, use the pathname instead. In the case of a
1311 // synthesized title, we don't want the update to count toward the "one set
1312 // per page of the title to history."
[email protected]6b2f7a82011-04-25 19:30:511313 string16 final_title;
[email protected]420ae012009-04-24 05:16:321314 bool explicit_set;
[email protected]987fc3a2011-05-26 14:18:091315 if (entry && entry->url().SchemeIsFile() && title.empty()) {
[email protected]6b2f7a82011-04-25 19:30:511316 final_title = UTF8ToUTF16(entry->url().ExtractFileName());
[email protected]420ae012009-04-24 05:16:321317 explicit_set = false; // Don't count synthetic titles toward the set limit.
1318 } else {
[email protected]acafd272011-07-26 17:35:571319 TrimWhitespace(title, TRIM_ALL, &final_title);
[email protected]420ae012009-04-24 05:16:321320 explicit_set = true;
1321 }
1322
[email protected]987fc3a2011-05-26 14:18:091323 // If a page is created via window.open and never navigated,
1324 // there will be no navigation entry. In this situation,
1325 // |page_title_when_no_navigaiton_entry_| will be used for page title.
1326 if (entry) {
1327 if (final_title == entry->title())
1328 return false; // Nothing changed, don't bother.
[email protected]420ae012009-04-24 05:16:321329
[email protected]987fc3a2011-05-26 14:18:091330 entry->set_title(final_title);
1331 } else {
1332 if (page_title_when_no_navigation_entry_ == final_title)
1333 return false; // Nothing changed, don't bother.
1334
1335 page_title_when_no_navigation_entry_ = final_title;
1336 }
[email protected]420ae012009-04-24 05:16:321337
[email protected]420ae012009-04-24 05:16:321338 // Lastly, set the title for the view.
[email protected]790e2fd2011-09-21 20:28:231339 view_->SetPageTitle(final_title);
[email protected]420ae012009-04-24 05:16:321340
[email protected]105bb0f2011-05-24 17:12:141341 TitleUpdatedDetails details(entry, explicit_set);
1342
[email protected]cbc0e1b2010-04-12 18:33:041343 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271344 content::NOTIFICATION_TAB_CONTENTS_TITLE_UPDATED,
[email protected]cbc0e1b2010-04-12 18:33:041345 Source<TabContents>(this),
[email protected]105bb0f2011-05-24 17:12:141346 Details<TitleUpdatedDetails>(&details));
[email protected]cbc0e1b2010-04-12 18:33:041347
[email protected]420ae012009-04-24 05:16:321348 return true;
1349}
1350
1351void TabContents::NotifySwapped() {
1352 // After sending out a swap notification, we need to send a disconnect
1353 // notification so that clients that pick up a pointer to |this| can NULL the
1354 // pointer. See Bug 1230284.
1355 notify_disconnection_ = true;
1356 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271357 content::NOTIFICATION_TAB_CONTENTS_SWAPPED,
[email protected]d82ed61e2009-06-16 02:46:221358 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:321359 NotificationService::NoDetails());
1360}
1361
1362void TabContents::NotifyConnected() {
1363 notify_disconnection_ = true;
1364 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271365 content::NOTIFICATION_TAB_CONTENTS_CONNECTED,
[email protected]d82ed61e2009-06-16 02:46:221366 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:321367 NotificationService::NoDetails());
1368}
1369
1370void TabContents::NotifyDisconnected() {
1371 if (!notify_disconnection_)
1372 return;
1373
1374 notify_disconnection_ = false;
1375 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271376 content::NOTIFICATION_TAB_CONTENTS_DISCONNECTED,
[email protected]d82ed61e2009-06-16 02:46:221377 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:321378 NotificationService::NoDetails());
1379}
1380
[email protected]8d3347f2009-07-09 22:00:211381RenderViewHostDelegate::View* TabContents::GetViewDelegate() {
[email protected]420ae012009-04-24 05:16:321382 return view_.get();
1383}
1384
[email protected]8d3347f2009-07-09 22:00:211385RenderViewHostDelegate::RendererManagement*
1386TabContents::GetRendererManagementDelegate() {
1387 return &render_manager_;
1388}
1389
[email protected]3d7474ff2011-07-27 17:47:371390RendererPreferences TabContents::GetRendererPrefs(
1391 content::BrowserContext* browser_context) const {
[email protected]840b1512009-07-21 16:53:461392 return renderer_preferences_;
[email protected]80d96fa2009-06-10 22:34:511393}
1394
[email protected]57c6a652009-05-04 07:58:341395TabContents* TabContents::GetAsTabContents() {
1396 return this;
1397}
1398
[email protected]ee162d82011-10-06 06:35:371399content::ViewType::Type TabContents::GetRenderViewType() const {
1400 return content::ViewType::TAB_CONTENTS;
[email protected]7b291f92009-08-14 05:43:531401}
1402
[email protected]420ae012009-04-24 05:16:321403void TabContents::RenderViewCreated(RenderViewHost* render_view_host) {
[email protected]2e4633c2009-07-09 16:58:061404 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271405 content::NOTIFICATION_RENDER_VIEW_HOST_CREATED_FOR_TAB,
[email protected]2e4633c2009-07-09 16:58:061406 Source<TabContents>(this),
1407 Details<RenderViewHost>(render_view_host));
[email protected]420ae012009-04-24 05:16:321408 NavigationEntry* entry = controller_.GetActiveEntry();
1409 if (!entry)
1410 return;
1411
1412 // When we're creating views, we're still doing initial setup, so we always
[email protected]e0112912011-02-02 22:54:351413 // use the pending Web UI rather than any possibly existing committed one.
[email protected]1fd1a502011-03-30 16:55:561414 if (render_manager_.pending_web_ui())
[email protected]7ade2732011-02-10 00:13:581415 render_manager_.pending_web_ui()->RenderViewCreated(render_view_host);
[email protected]420ae012009-04-24 05:16:321416
1417 if (entry->IsViewSourceMode()) {
1418 // Put the renderer in view source mode.
1419 render_view_host->Send(
1420 new ViewMsg_EnableViewSourceMode(render_view_host->routing_id()));
1421 }
[email protected]0666aef2009-05-13 19:48:081422
1423 view()->RenderViewCreated(render_view_host);
[email protected]11f764bb2011-05-25 19:45:261424
1425 FOR_EACH_OBSERVER(
1426 TabContentsObserver, observers_, RenderViewCreated(render_view_host));
[email protected]420ae012009-04-24 05:16:321427}
1428
1429void TabContents::RenderViewReady(RenderViewHost* rvh) {
1430 if (rvh != render_view_host()) {
1431 // Don't notify the world, since this came from a renderer in the
1432 // background.
1433 return;
1434 }
1435
1436 NotifyConnected();
[email protected]9d8a4642009-07-29 17:25:301437 bool was_crashed = is_crashed();
[email protected]443b80e2010-12-14 00:42:231438 SetIsCrashed(base::TERMINATION_STATUS_STILL_RUNNING, 0);
[email protected]9d8a4642009-07-29 17:25:301439
1440 // Restore the focus to the tab (otherwise the focus will be on the top
1441 // window).
[email protected]484ae5912010-09-29 19:16:141442 if (was_crashed && !FocusLocationBarByDefault() &&
1443 (!delegate_ || delegate_->ShouldFocusPageAfterCrash())) {
[email protected]9d8a4642009-07-29 17:25:301444 Focus();
[email protected]484ae5912010-09-29 19:16:141445 }
[email protected]420ae012009-04-24 05:16:321446}
1447
[email protected]443b80e2010-12-14 00:42:231448void TabContents::RenderViewGone(RenderViewHost* rvh,
1449 base::TerminationStatus status,
1450 int error_code) {
[email protected]420ae012009-04-24 05:16:321451 if (rvh != render_view_host()) {
1452 // The pending page's RenderViewHost is gone.
1453 return;
1454 }
1455
1456 SetIsLoading(false, NULL);
1457 NotifyDisconnected();
[email protected]443b80e2010-12-14 00:42:231458 SetIsCrashed(status, error_code);
[email protected]b3e3f87a2011-10-01 23:37:541459 view()->OnTabCrashed(crashed_status(), crashed_error_code());
[email protected]420ae012009-04-24 05:16:321460
[email protected]55452902011-06-01 21:57:471461 FOR_EACH_OBSERVER(TabContentsObserver, observers_, RenderViewGone());
[email protected]420ae012009-04-24 05:16:321462}
1463
[email protected]2e4633c2009-07-09 16:58:061464void TabContents::RenderViewDeleted(RenderViewHost* rvh) {
[email protected]fb7b79f72009-11-06 18:00:481465 render_manager_.RenderViewDeleted(rvh);
[email protected]2e4633c2009-07-09 16:58:061466}
1467
[email protected]420ae012009-04-24 05:16:321468void TabContents::DidNavigate(RenderViewHost* rvh,
1469 const ViewHostMsg_FrameNavigate_Params& params) {
[email protected]09b29342011-06-24 19:18:481470 if (PageTransition::IsMainFrame(params.transition))
[email protected]420ae012009-04-24 05:16:321471 render_manager_.DidNavigateMainFrame(rvh);
[email protected]8030f012009-09-25 18:09:371472
[email protected]420ae012009-04-24 05:16:321473 // Update the site of the SiteInstance if it doesn't have one yet.
1474 if (!GetSiteInstance()->has_site())
1475 GetSiteInstance()->SetSite(params.url);
1476
1477 // Need to update MIME type here because it's referred to in
1478 // UpdateNavigationCommands() called by RendererDidNavigate() to
1479 // determine whether or not to enable the encoding menu.
1480 // It's updated only for the main frame. For a subframe,
1481 // RenderView::UpdateURL does not set params.contents_mime_type.
1482 // (see http://code.google.com/p/chromium/issues/detail?id=2929 )
1483 // TODO(jungshik): Add a test for the encoding menu to avoid
1484 // regressing it again.
1485 if (PageTransition::IsMainFrame(params.transition))
1486 contents_mime_type_ = params.contents_mime_type;
1487
[email protected]8286f51a2011-05-31 17:39:131488 content::LoadCommittedDetails details;
[email protected]93f230e02011-06-01 14:40:001489 bool did_navigate = controller_.RendererDidNavigate(params, &details);
[email protected]3e90d4a2009-07-03 17:38:391490
[email protected]a9c0bfe2010-09-17 08:35:221491 // Send notification about committed provisional loads. This notification is
1492 // different from the NAV_ENTRY_COMMITTED notification which doesn't include
1493 // the actual URL navigated to and isn't sent for AUTO_SUBFRAME navigations.
1494 if (details.type != NavigationType::NAV_IGNORE) {
[email protected]dabb0d12010-10-05 12:50:071495 // For AUTO_SUBFRAME navigations, an event for the main frame is generated
1496 // that is not recorded in the navigation history. For the purpose of
1497 // tracking navigation events, we treat this event as a sub frame navigation
1498 // event.
1499 bool is_main_frame = did_navigate ? details.is_main_frame : false;
[email protected]0d60f0192011-04-14 12:40:101500 PageTransition::Type transition_type = params.transition;
[email protected]a9c0bfe2010-09-17 08:35:221501 // Whether or not a page transition was triggered by going backward or
1502 // forward in the history is only stored in the navigation controller's
1503 // entry list.
1504 if (did_navigate &&
1505 (controller_.GetActiveEntry()->transition_type() &
1506 PageTransition::FORWARD_BACK)) {
[email protected]0d60f0192011-04-14 12:40:101507 transition_type = params.transition | PageTransition::FORWARD_BACK;
[email protected]a9c0bfe2010-09-17 08:35:221508 }
[email protected]0d60f0192011-04-14 12:40:101509 // Notify observers about the commit of the provisional load.
1510 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
1511 DidCommitProvisionalLoadForFrame(params.frame_id,
1512 is_main_frame, params.url, transition_type));
[email protected]a9c0bfe2010-09-17 08:35:221513 }
1514
[email protected]76543b92009-08-31 17:27:451515 if (!did_navigate)
1516 return; // No navigation happened.
[email protected]420ae012009-04-24 05:16:321517
1518 // DO NOT ADD MORE STUFF TO THIS FUNCTION! Your component should either listen
1519 // for the appropriate notification (best) or you can add it to
1520 // DidNavigateMainFramePostCommit / DidNavigateAnyFramePostCommit (only if
1521 // necessary, please).
1522
1523 // Run post-commit tasks.
[email protected]93f230e02011-06-01 14:40:001524 if (details.is_main_frame) {
[email protected]420ae012009-04-24 05:16:321525 DidNavigateMainFramePostCommit(details, params);
[email protected]09b29342011-06-24 19:18:481526 if (delegate())
1527 delegate()->DidNavigateMainFramePostCommit(this);
[email protected]93f230e02011-06-01 14:40:001528 }
[email protected]420ae012009-04-24 05:16:321529 DidNavigateAnyFramePostCommit(rvh, details, params);
1530}
1531
1532void TabContents::UpdateState(RenderViewHost* rvh,
1533 int32 page_id,
1534 const std::string& state) {
[email protected]992db4c2011-05-12 15:37:151535 // Ensure that this state update comes from either the active RVH or one of
1536 // the swapped out RVHs. We don't expect to hear from any other RVHs.
1537 DCHECK(rvh == render_view_host() || render_manager_.IsSwappedOut(rvh));
[email protected]420ae012009-04-24 05:16:321538
1539 // We must be prepared to handle state updates for any page, these occur
1540 // when the user is scrolling and entering form data, as well as when we're
1541 // leaving a page, in which case our state may have already been moved to
1542 // the next page. The navigation controller will look up the appropriate
1543 // NavigationEntry and update it when it is notified via the delegate.
1544
1545 int entry_index = controller_.GetEntryIndexWithPageID(
[email protected]992db4c2011-05-12 15:37:151546 rvh->site_instance(), page_id);
[email protected]420ae012009-04-24 05:16:321547 if (entry_index < 0)
1548 return;
1549 NavigationEntry* entry = controller_.GetEntryAtIndex(entry_index);
1550
1551 if (state == entry->content_state())
1552 return; // Nothing to update.
1553 entry->set_content_state(state);
1554 controller_.NotifyEntryChanged(entry, entry_index);
1555}
1556
1557void TabContents::UpdateTitle(RenderViewHost* rvh,
[email protected]acafd272011-07-26 17:35:571558 int32 page_id,
[email protected]a49e10b2011-08-01 23:57:461559 const string16& title,
1560 base::i18n::TextDirection title_direction) {
[email protected]420ae012009-04-24 05:16:321561 // If we have a title, that's a pretty good indication that we've started
1562 // getting useful data.
1563 SetNotWaitingForResponse();
1564
1565 DCHECK(rvh == render_view_host());
[email protected]371cc8b2010-09-01 16:03:481566 NavigationEntry* entry = controller_.GetEntryWithPageID(rvh->site_instance(),
[email protected]ec0b6c42010-08-26 03:16:581567 page_id);
[email protected]987fc3a2011-05-26 14:18:091568
[email protected]a49e10b2011-08-01 23:57:461569 // TODO(evan): make use of title_direction.
1570 // http://code.google.com/p/chromium/issues/detail?id=27094
[email protected]987fc3a2011-05-26 14:18:091571 if (!UpdateTitleForEntry(entry, title))
[email protected]420ae012009-04-24 05:16:321572 return;
1573
1574 // Broadcast notifications when the UI should be updated.
1575 if (entry == controller_.GetEntryAtOffset(0))
[email protected]f1cd5e82009-10-23 17:19:031576 NotifyNavigationStateChanged(INVALIDATE_TITLE);
[email protected]420ae012009-04-24 05:16:321577}
1578
[email protected]420ae012009-04-24 05:16:321579void TabContents::UpdateEncoding(RenderViewHost* render_view_host,
[email protected]41fc0322009-09-04 22:23:401580 const std::string& encoding) {
[email protected]420ae012009-04-24 05:16:321581 set_encoding(encoding);
1582}
1583
1584void TabContents::UpdateTargetURL(int32 page_id, const GURL& url) {
1585 if (delegate())
[email protected]d952a052011-09-06 18:42:451586 delegate()->UpdateTargetURL(this, page_id, url);
[email protected]420ae012009-04-24 05:16:321587}
1588
[email protected]420ae012009-04-24 05:16:321589void TabContents::Close(RenderViewHost* rvh) {
[email protected]07707302009-11-06 00:50:291590 // The UI may be in an event-tracking loop, such as between the
1591 // mouse-down and mouse-up in text selection or a button click.
1592 // Defer the close until after tracking is complete, so that we
1593 // don't free objects out from under the UI.
1594 // TODO(shess): This could probably be integrated with the
1595 // IsDoingDrag() test below. Punting for now because I need more
1596 // research to understand how this impacts platforms other than Mac.
1597 // TODO(shess): This could get more fine-grained. For instance,
1598 // closing a tab in another window while selecting text in the
1599 // current window's Omnibox should be just fine.
1600 if (view()->IsEventTracking()) {
1601 view()->CloseTabAfterEventTracking();
1602 return;
1603 }
1604
[email protected]24a4d1062009-07-10 23:10:421605 // If we close the tab while we're in the middle of a drag, we'll crash.
1606 // Instead, cancel the drag and close it as soon as the drag ends.
1607 if (view()->IsDoingDrag()) {
1608 view()->CancelDragAndCloseTab();
1609 return;
1610 }
1611
[email protected]420ae012009-04-24 05:16:321612 // Ignore this if it comes from a RenderViewHost that we aren't showing.
1613 if (delegate() && rvh == render_view_host())
1614 delegate()->CloseContents(this);
1615}
1616
1617void TabContents::RequestMove(const gfx::Rect& new_bounds) {
[email protected]b35b26b32011-05-05 20:35:141618 if (delegate() && delegate()->IsPopupOrPanel(this))
[email protected]420ae012009-04-24 05:16:321619 delegate()->MoveContents(this, new_bounds);
1620}
1621
[email protected]7ab1e7d62009-10-14 23:32:011622void TabContents::DidStartLoading() {
[email protected]420ae012009-04-24 05:16:321623 SetIsLoading(true, NULL);
[email protected]3c9e1872010-11-18 16:17:491624
[email protected]c40d6232011-03-25 00:16:211625 if (delegate() && content_restrictions_) {
1626 content_restrictions_ = 0;
[email protected]de4761b2010-12-07 21:05:211627 delegate()->ContentRestrictionsChanged(this);
[email protected]9e823662010-10-13 23:36:001628 }
[email protected]3c9e1872010-11-18 16:17:491629
1630 // Notify observers about navigation.
[email protected]585b30362011-01-28 02:30:171631 FOR_EACH_OBSERVER(TabContentsObserver, observers_, DidStartLoading());
[email protected]420ae012009-04-24 05:16:321632}
1633
[email protected]7ab1e7d62009-10-14 23:32:011634void TabContents::DidStopLoading() {
[email protected]420ae012009-04-24 05:16:321635 scoped_ptr<LoadNotificationDetails> details;
1636
1637 NavigationEntry* entry = controller_.GetActiveEntry();
1638 // An entry may not exist for a stop when loading an initial blank page or
1639 // if an iframe injected by script into a blank page finishes loading.
1640 if (entry) {
[email protected]420ae012009-04-24 05:16:321641 base::TimeDelta elapsed = base::TimeTicks::Now() - current_load_start_;
1642
1643 details.reset(new LoadNotificationDetails(
[email protected]ebe89e062009-08-13 23:16:541644 entry->virtual_url(),
[email protected]420ae012009-04-24 05:16:321645 entry->transition_type(),
1646 elapsed,
1647 &controller_,
1648 controller_.GetCurrentEntryIndex()));
1649 }
1650
[email protected]420ae012009-04-24 05:16:321651 SetIsLoading(false, details.get());
[email protected]3c9e1872010-11-18 16:17:491652
1653 // Notify observers about navigation.
[email protected]585b30362011-01-28 02:30:171654 FOR_EACH_OBSERVER(TabContentsObserver, observers_, DidStopLoading());
[email protected]420ae012009-04-24 05:16:321655}
1656
[email protected]c95fa8b2011-04-28 20:26:161657void TabContents::DidCancelLoading() {
1658 controller_.DiscardNonCommittedEntries();
1659
1660 // Update the URL display.
1661 NotifyNavigationStateChanged(TabContents::INVALIDATE_URL);
1662}
1663
[email protected]1a3c3cb2010-12-16 21:03:401664void TabContents::DidChangeLoadProgress(double progress) {
1665 if (delegate())
1666 delegate()->LoadProgressChanged(progress);
1667}
1668
[email protected]25497492010-09-11 15:15:081669void TabContents::DocumentOnLoadCompletedInMainFrame(
1670 RenderViewHost* render_view_host,
1671 int32 page_id) {
1672 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271673 content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME,
[email protected]25497492010-09-11 15:15:081674 Source<TabContents>(this),
1675 Details<int>(&page_id));
1676}
1677
[email protected]ae5184d62011-10-06 19:25:581678void TabContents::RequestOpenURL(const GURL& url,
1679 const GURL& referrer,
1680 WindowOpenDisposition disposition,
1681 int64 source_frame_id) {
1682 TabContents* new_contents = NULL;
1683 PageTransition::Type transition_type = PageTransition::LINK;
[email protected]7ade2732011-02-10 00:13:581684 if (render_manager_.web_ui()) {
[email protected]e0112912011-02-02 22:54:351685 // When we're a Web UI, it will provide a page transition type for us (this
[email protected]420ae012009-04-24 05:16:321686 // is so the new tab page can specify AUTO_BOOKMARK for automatically
1687 // generated suggestions).
1688 //
[email protected]e0112912011-02-02 22:54:351689 // Note also that we hide the referrer for Web UI pages. We don't really
[email protected]60e448982009-05-06 04:21:161690 // want web sites to see a referrer of "chrome://blah" (and some
1691 // chrome: URLs might have search terms or other stuff we don't want to
[email protected]420ae012009-04-24 05:16:321692 // send to the site), so we send no referrer.
[email protected]ae5184d62011-10-06 19:25:581693 new_contents = OpenURL(url, GURL(), disposition,
[email protected]7ade2732011-02-10 00:13:581694 render_manager_.web_ui()->link_transition_type());
[email protected]ae5184d62011-10-06 19:25:581695 transition_type = render_manager_.web_ui()->link_transition_type();
[email protected]420ae012009-04-24 05:16:321696 } else {
[email protected]ae5184d62011-10-06 19:25:581697 new_contents = OpenURL(url, referrer, disposition, PageTransition::LINK);
1698 }
1699 if (new_contents) {
1700 // Notify observers.
1701 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
1702 DidOpenRequestedURL(new_contents,
1703 url,
1704 referrer,
1705 disposition,
1706 transition_type,
1707 source_frame_id));
[email protected]420ae012009-04-24 05:16:321708 }
1709}
1710
[email protected]420ae012009-04-24 05:16:321711void TabContents::RunJavaScriptMessage(
[email protected]992db4c2011-05-12 15:37:151712 const RenderViewHost* rvh,
[email protected]4f5ce842011-05-27 19:34:411713 const string16& message,
1714 const string16& default_prompt,
[email protected]420ae012009-04-24 05:16:321715 const GURL& frame_url,
1716 const int flags,
1717 IPC::Message* reply_msg,
1718 bool* did_suppress_message) {
[email protected]3ab9cb82011-06-03 18:02:071719 // Suppress JavaScript dialogs when requested. Also suppress messages when
1720 // showing an interstitial as it's shown over the previous page and we don't
1721 // want the hidden page's dialogs to interfere with the interstitial.
[email protected]8f55e802010-12-06 18:11:501722 bool suppress_this_message =
[email protected]992db4c2011-05-12 15:37:151723 rvh->is_swapped_out() ||
[email protected]8f55e802010-12-06 18:11:501724 showing_interstitial_page() ||
[email protected]3ab9cb82011-06-03 18:02:071725 !delegate_ ||
1726 delegate_->ShouldSuppressDialogs();
[email protected]420ae012009-04-24 05:16:321727
1728 if (!suppress_this_message) {
[email protected]b627d9a2011-06-28 14:06:341729 content::JavaScriptDialogCreator::TitleType title_type;
1730 string16 title;
1731
1732 if (!frame_url.has_host()) {
1733 title_type = content::JavaScriptDialogCreator::DIALOG_TITLE_NONE;
1734 } else {
1735 title_type = content::JavaScriptDialogCreator::DIALOG_TITLE_FORMATTED_URL;
1736 title = net::FormatUrl(
1737 frame_url.GetOrigin(),
1738 content::GetContentClient()->browser()->GetAcceptLangs(this));
1739 }
1740
[email protected]2e5b90c2011-08-16 21:11:551741 dialog_creator_ = delegate_->GetJavaScriptDialogCreator();
1742 dialog_creator_->RunJavaScriptDialog(this,
1743 title_type,
1744 title,
1745 flags,
1746 message,
1747 default_prompt,
1748 reply_msg,
1749 &suppress_this_message);
[email protected]420ae012009-04-24 05:16:321750 }
[email protected]3ab9cb82011-06-03 18:02:071751
1752 if (suppress_this_message) {
1753 // If we are suppressing messages, just reply as if the user immediately
1754 // pressed "Cancel".
1755 OnDialogClosed(reply_msg, false, string16());
1756 }
1757
1758 *did_suppress_message = suppress_this_message;
[email protected]420ae012009-04-24 05:16:321759}
1760
[email protected]992db4c2011-05-12 15:37:151761void TabContents::RunBeforeUnloadConfirm(const RenderViewHost* rvh,
[email protected]4f5ce842011-05-27 19:34:411762 const string16& message,
[email protected]420ae012009-04-24 05:16:321763 IPC::Message* reply_msg) {
[email protected]5aab5e22010-12-08 22:13:291764 if (delegate())
1765 delegate()->WillRunBeforeUnloadConfirm();
[email protected]3ab9cb82011-06-03 18:02:071766
1767 bool suppress_this_message =
1768 rvh->is_swapped_out() ||
1769 !delegate_ ||
1770 delegate_->ShouldSuppressDialogs();
[email protected]992db4c2011-05-12 15:37:151771 if (suppress_this_message) {
[email protected]4f5ce842011-05-27 19:34:411772 render_view_host()->JavaScriptDialogClosed(reply_msg, true, string16());
[email protected]8f55e802010-12-06 18:11:501773 return;
1774 }
[email protected]3ab9cb82011-06-03 18:02:071775
[email protected]7ab1e7d62009-10-14 23:32:011776 is_showing_before_unload_dialog_ = true;
[email protected]2e5b90c2011-08-16 21:11:551777 dialog_creator_ = delegate_->GetJavaScriptDialogCreator();
1778 dialog_creator_->RunBeforeUnloadDialog(this,
1779 message,
1780 reply_msg);
[email protected]420ae012009-04-24 05:16:321781}
1782
[email protected]420ae012009-04-24 05:16:321783WebPreferences TabContents::GetWebkitPrefs() {
[email protected]447021c2010-09-08 21:29:081784 WebPreferences web_prefs =
[email protected]181a95ee2011-07-12 19:26:361785 content::GetContentClient()->browser()->GetWebkitPrefs(
[email protected]3d7474ff2011-07-27 17:47:371786 render_view_host()->process()->browser_context(), false);
[email protected]447021c2010-09-08 21:29:081787
[email protected]b8299c12011-06-03 19:52:281788 // Force accelerated compositing and 2d canvas off for chrome:, about: and
[email protected]2ef216b2011-08-01 19:25:211789 // chrome-devtools: pages (unless it's specifically allowed).
1790 if ((GetURL().SchemeIs(chrome::kChromeDevToolsScheme) ||
[email protected]30b8e0ee2011-09-12 14:45:571791#if !defined(TOUCH_UI)
1792 // Allow accelerated compositing for keyboard and log in screen.
[email protected]b8299c12011-06-03 19:52:281793 GetURL().SchemeIs(chrome::kChromeUIScheme) ||
[email protected]30b8e0ee2011-09-12 14:45:571794#endif
[email protected]cd4a5bd22011-09-13 06:46:301795 (GetURL().SchemeIs(chrome::kAboutScheme) &&
1796 GetURL().spec() != chrome::kAboutBlankURL)) &&
[email protected]2ef216b2011-08-01 19:25:211797 !web_prefs.allow_webui_compositing) {
[email protected]447021c2010-09-08 21:29:081798 web_prefs.accelerated_compositing_enabled = false;
[email protected]9beff752010-09-22 19:35:431799 web_prefs.accelerated_2d_canvas_enabled = false;
[email protected]447021c2010-09-08 21:29:081800 }
1801
[email protected]2f7830b32011-08-26 18:22:251802#if defined(TOUCH_UI)
1803 web_prefs.force_compositing_mode = true;
1804#endif
1805
[email protected]447021c2010-09-08 21:29:081806 return web_prefs;
[email protected]420ae012009-04-24 05:16:321807}
1808
[email protected]7d472472011-01-22 01:30:251809void TabContents::OnUserGesture() {
[email protected]ddb85052011-05-18 14:40:271810 // Notify observers.
1811 FOR_EACH_OBSERVER(TabContentsObserver, observers_, DidGetUserGesture());
[email protected]686493142011-07-15 21:47:221812
1813 ResourceDispatcherHost* rdh =
1814 content::GetContentClient()->browser()->GetResourceDispatcherHost();
1815 if (rdh) // NULL in unittests.
[email protected]8ec98cd22011-08-09 22:31:301816 rdh->OnUserGesture(this);
[email protected]7d472472011-01-22 01:30:251817}
1818
[email protected]fa1cf0b82010-01-15 21:49:441819void TabContents::OnIgnoredUIEvent() {
[email protected]31145092011-09-30 01:49:441820 // Notify observers.
1821 FOR_EACH_OBSERVER(TabContentsObserver, observers_, DidGetIgnoredUIEvent());
[email protected]fa1cf0b82010-01-15 21:49:441822}
1823
[email protected]420ae012009-04-24 05:16:321824void TabContents::RendererUnresponsive(RenderViewHost* rvh,
1825 bool is_during_unload) {
[email protected]992db4c2011-05-12 15:37:151826 // Don't show hung renderer dialog for a swapped out RVH.
1827 if (rvh != render_view_host())
1828 return;
1829
[email protected]e5fc1632011-08-08 07:51:531830 // Ignore renderer unresponsive event if debugger is attached to the tab
1831 // since the event may be a result of the renderer sitting on a breakpoint.
1832 // See http://crbug.com/65458
1833 DevToolsManager* devtools_manager = DevToolsManager::GetInstance();
1834 if (devtools_manager &&
1835 devtools_manager->GetDevToolsClientHostFor(rvh) != NULL)
1836 return;
1837
[email protected]420ae012009-04-24 05:16:321838 if (is_during_unload) {
1839 // Hang occurred while firing the beforeunload/unload handler.
1840 // Pretend the handler fired so tab closing continues as if it had.
[email protected]829e7612009-04-25 01:15:111841 rvh->set_sudden_termination_allowed(true);
[email protected]420ae012009-04-24 05:16:321842
1843 if (!render_manager_.ShouldCloseTabOnUnresponsiveRenderer())
1844 return;
1845
1846 // If the tab hangs in the beforeunload/unload handler there's really
1847 // nothing we can do to recover. Pretend the unload listeners have
1848 // all fired and close the tab. If the hang is in the beforeunload handler
1849 // then the user will not have the option of cancelling the close.
1850 Close(rvh);
1851 return;
1852 }
1853
[email protected]55452902011-06-01 21:57:471854 if (!render_view_host() || !render_view_host()->IsRenderViewLive())
1855 return;
1856
1857 if (delegate())
1858 delegate()->RendererUnresponsive(this);
[email protected]420ae012009-04-24 05:16:321859}
1860
1861void TabContents::RendererResponsive(RenderViewHost* render_view_host) {
[email protected]55452902011-06-01 21:57:471862 if (delegate())
1863 delegate()->RendererResponsive(this);
[email protected]420ae012009-04-24 05:16:321864}
1865
1866void TabContents::LoadStateChanged(const GURL& url,
[email protected]9c235f042011-08-10 22:28:211867 const net::LoadStateWithParam& load_state,
[email protected]094e5b22009-09-25 04:23:561868 uint64 upload_position,
1869 uint64 upload_size) {
[email protected]420ae012009-04-24 05:16:321870 load_state_ = load_state;
[email protected]094e5b22009-09-25 04:23:561871 upload_position_ = upload_position;
1872 upload_size_ = upload_size;
[email protected]04866c42011-05-03 20:03:501873 load_state_host_ = net::IDNToUnicode(url.host(),
[email protected]b5cca982011-05-26 04:42:081874 content::GetContentClient()->browser()->GetAcceptLangs(this));
[email protected]9c235f042011-08-10 22:28:211875 if (load_state_.state == net::LOAD_STATE_READING_RESPONSE)
[email protected]420ae012009-04-24 05:16:321876 SetNotWaitingForResponse();
[email protected]f5d978c2011-07-21 14:43:511877 if (IsLoading())
[email protected]c9cd2222009-05-06 05:16:501878 NotifyNavigationStateChanged(INVALIDATE_LOAD | INVALIDATE_TAB);
[email protected]420ae012009-04-24 05:16:321879}
1880
[email protected]7d472472011-01-22 01:30:251881void TabContents::WorkerCrashed() {
[email protected]0dd3a0ab2011-02-18 08:17:441882 if (delegate())
[email protected]55452902011-06-01 21:57:471883 delegate()->WorkerCrashed(this);
[email protected]7d472472011-01-22 01:30:251884}
1885
[email protected]420ae012009-04-24 05:16:321886void TabContents::BeforeUnloadFiredFromRenderManager(
1887 bool proceed,
1888 bool* proceed_to_fire_unload) {
1889 if (delegate())
1890 delegate()->BeforeUnloadFired(this, proceed, proceed_to_fire_unload);
1891}
1892
[email protected]3a3d47472010-07-15 21:03:541893void TabContents::DidStartLoadingFromRenderManager(
1894 RenderViewHost* render_view_host) {
1895 DidStartLoading();
1896}
1897
1898void TabContents::RenderViewGoneFromRenderManager(
1899 RenderViewHost* render_view_host) {
[email protected]443b80e2010-12-14 00:42:231900 DCHECK(crashed_status_ != base::TERMINATION_STATUS_STILL_RUNNING);
1901 RenderViewGone(render_view_host, crashed_status_, crashed_error_code_);
[email protected]3a3d47472010-07-15 21:03:541902}
1903
[email protected]420ae012009-04-24 05:16:321904void TabContents::UpdateRenderViewSizeForRenderManager() {
1905 // TODO(brettw) this is a hack. See TabContentsView::SizeContents.
[email protected]0323ee42010-02-17 22:03:261906 gfx::Size size = view_->GetContainerSize();
1907 // 0x0 isn't a valid window size (minimal window size is 1x1) but it may be
1908 // here during container initialization and normal window size will be set
1909 // later. In case of tab duplication this resizing to 0x0 prevents setting
1910 // normal size later so just ignore it.
1911 if (!size.IsEmpty())
1912 view_->SizeContents(size);
[email protected]420ae012009-04-24 05:16:321913}
1914
[email protected]3a3d47472010-07-15 21:03:541915void TabContents::NotifySwappedFromRenderManager() {
1916 NotifySwapped();
1917}
1918
1919NavigationController& TabContents::GetControllerForRenderManager() {
1920 return controller();
1921}
1922
[email protected]d0980792011-02-13 19:41:401923WebUI* TabContents::CreateWebUIForRenderManager(const GURL& url) {
[email protected]1fd1a502011-03-30 16:55:561924 return content::WebUIFactory::Get()->CreateWebUIForURL(this, url);
[email protected]420ae012009-04-24 05:16:321925}
1926
1927NavigationEntry*
1928TabContents::GetLastCommittedNavigationEntryForRenderManager() {
1929 return controller_.GetLastCommittedEntry();
1930}
1931
1932bool TabContents::CreateRenderViewForRenderManager(
1933 RenderViewHost* render_view_host) {
[email protected]420ae012009-04-24 05:16:321934 RenderWidgetHostView* rwh_view = view_->CreateViewForWidget(render_view_host);
[email protected]1a98a932009-11-17 00:12:521935
[email protected]420ae012009-04-24 05:16:321936 // Now that the RenderView has been created, we need to tell it its size.
1937 rwh_view->SetSize(view_->GetContainerSize());
1938
[email protected]a4127722011-04-27 23:13:521939 if (!render_view_host->CreateRenderView(string16()))
1940 return false;
1941
[email protected]f8233cc2011-05-31 20:24:501942#if defined(OS_LINUX)
1943 // Force a ViewMsg_Resize to be sent, needed to make plugins show up on
1944 // linux. See crbug.com/83941.
1945 if (RenderWidgetHost* render_widget_host = rwh_view->GetRenderWidgetHost())
1946 render_widget_host->WasResized();
1947#endif
1948
[email protected]420ae012009-04-24 05:16:321949 UpdateMaxPageIDIfNecessary(render_view_host->site_instance(),
1950 render_view_host);
1951 return true;
1952}
1953
[email protected]3ab9cb82011-06-03 18:02:071954void TabContents::OnDialogClosed(IPC::Message* reply_msg,
1955 bool success,
1956 const string16& user_input) {
[email protected]beb440c2009-11-06 04:08:541957 if (is_showing_before_unload_dialog_ && !success) {
1958 // If a beforeunload dialog is canceled, we need to stop the throbber from
1959 // spinning, since we forced it to start spinning in Navigate.
1960 DidStopLoading();
1961
1962 tab_close_start_time_ = base::TimeTicks();
1963 }
1964 is_showing_before_unload_dialog_ = false;
[email protected]4f5ce842011-05-27 19:34:411965 render_view_host()->JavaScriptDialogClosed(reply_msg,
1966 success,
[email protected]3ab9cb82011-06-03 18:02:071967 user_input);
[email protected]beb440c2009-11-06 04:08:541968}
1969
[email protected]3ab9cb82011-06-03 18:02:071970gfx::NativeWindow TabContents::GetDialogRootWindow() {
1971 return view_->GetTopLevelNativeWindow();
[email protected]beb440c2009-11-06 04:08:541972}
1973
[email protected]a1e97f02011-06-30 14:04:341974void TabContents::OnDialogShown() {
1975 Activate();
[email protected]3a3d47472010-07-15 21:03:541976}
1977
[email protected]41fc0322009-09-04 22:23:401978void TabContents::set_encoding(const std::string& encoding) {
[email protected]763ec4ca2011-04-29 15:48:121979 encoding_ = content::GetContentClient()->browser()->
1980 GetCanonicalEncodingNameByAliasName(encoding);
[email protected]aacd7cc2009-09-02 14:51:101981}
[email protected]f45d2a72010-03-08 23:28:351982
[email protected]33f74972010-12-08 16:40:361983void TabContents::CreateViewAndSetSizeForRVH(RenderViewHost* rvh) {
1984 RenderWidgetHostView* rwh_view = view()->CreateViewForWidget(rvh);
1985 rwh_view->SetSize(view()->GetContainerSize());
1986}