blob: 1f66529d4fd2c3100ce38aef32ff413386b7ff58 [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"
42#include "content/common/content_client.h"
[email protected]19e81142011-10-03 16:19:4043#include "content/common/content_constants.h"
[email protected]763ec4ca2011-04-29 15:48:1244#include "content/common/content_restriction.h"
[email protected]ce9751942011-09-21 01:57:2445#include "content/common/intents_messages.h"
[email protected]4dd57932011-03-17 06:06:1246#include "content/common/navigation_types.h"
[email protected]7f070d42011-03-09 20:25:3247#include "content/common/notification_service.h"
[email protected]0f012df82011-05-19 14:15:2948#include "content/common/url_constants.h"
[email protected]2c5569662011-03-22 20:45:0249#include "content/common/view_messages.h"
[email protected]e091df82011-10-11 18:13:2150#include "content/public/common/bindings_policy.h"
[email protected]da4dfc42011-10-12 15:53:5651#include "content/public/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]f5d978c2011-07-21 14:43:51402bool TabContents::IsLoading() const {
403 return is_loading_ || (web_ui() && web_ui()->IsLoading());
404}
405
[email protected]585b30362011-01-28 02:30:17406void TabContents::AddObserver(TabContentsObserver* observer) {
407 observers_.AddObserver(observer);
[email protected]3c9e1872010-11-18 16:17:49408}
409
[email protected]585b30362011-01-28 02:30:17410void TabContents::RemoveObserver(TabContentsObserver* observer) {
411 observers_.RemoveObserver(observer);
[email protected]3c9e1872010-11-18 16:17:49412}
413
[email protected]443b80e2010-12-14 00:42:23414void TabContents::SetIsCrashed(base::TerminationStatus status, int error_code) {
415 if (status == crashed_status_)
[email protected]d5f942ba2008-09-26 19:30:34416 return;
417
[email protected]443b80e2010-12-14 00:42:23418 crashed_status_ = status;
419 crashed_error_code_ = error_code;
[email protected]c9cd2222009-05-06 05:16:50420 NotifyNavigationStateChanged(INVALIDATE_TAB);
[email protected]d5f942ba2008-09-26 19:30:34421}
422
423void TabContents::NotifyNavigationStateChanged(unsigned changed_flags) {
424 if (delegate_)
425 delegate_->NavigationStateChanged(this, changed_flags);
426}
427
[email protected]96d185d2009-04-24 03:28:54428void TabContents::DidBecomeSelected() {
429 controller_.SetActive(true);
[email protected]8cb5d5b2010-02-09 11:36:16430 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
431 if (rwhv) {
432 rwhv->DidBecomeSelected();
[email protected]789e9152009-08-04 21:59:43433#if defined(OS_MACOSX)
[email protected]8cb5d5b2010-02-09 11:36:16434 rwhv->SetActive(true);
[email protected]789e9152009-08-04 21:59:43435#endif
436 }
[email protected]96d185d2009-04-24 03:28:54437
[email protected]5ac20162010-11-24 23:33:11438 last_selected_time_ = base::TimeTicks::Now();
[email protected]3e69bc82011-05-26 23:22:38439
440 FOR_EACH_OBSERVER(TabContentsObserver, observers_, DidBecomeSelected());
[email protected]96d185d2009-04-24 03:28:54441}
442
443void TabContents::WasHidden() {
444 if (!capturing_contents()) {
445 // |render_view_host()| can be NULL if the user middle clicks a link to open
446 // a tab in then background, then closes the tab before selecting it. This
447 // is because closing the tab calls TabContents::Destroy(), which removes
448 // the |render_view_host()|; then when we actually destroy the window,
449 // OnWindowPosChanged() notices and calls HideContents() (which calls us).
[email protected]8cb5d5b2010-02-09 11:36:16450 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
451 if (rwhv)
452 rwhv->WasHidden();
[email protected]96d185d2009-04-24 03:28:54453 }
454
455 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:27456 content::NOTIFICATION_TAB_CONTENTS_HIDDEN,
[email protected]96d185d2009-04-24 03:28:54457 Source<TabContents>(this),
458 NotificationService::NoDetails());
459}
460
[email protected]d5f942ba2008-09-26 19:30:34461void TabContents::Activate() {
462 if (delegate_)
463 delegate_->ActivateContents(this);
464}
465
[email protected]ea42e7782010-08-23 23:58:12466void TabContents::Deactivate() {
467 if (delegate_)
468 delegate_->DeactivateContents(this);
469}
470
[email protected]63954792011-07-11 04:17:48471void TabContents::LostCapture() {
472 if (delegate_)
473 delegate_->LostCapture();
474}
475
476bool TabContents::PreHandleKeyboardEvent(const NativeWebKeyboardEvent& event,
477 bool* is_keyboard_shortcut) {
478 return delegate_ &&
479 delegate_->PreHandleKeyboardEvent(event, is_keyboard_shortcut);
480}
481
482void TabContents::HandleKeyboardEvent(const NativeWebKeyboardEvent& event) {
483 if (delegate_)
484 delegate_->HandleKeyboardEvent(event);
485}
486
487void TabContents::HandleMouseUp() {
488 if (delegate_)
489 delegate_->HandleMouseUp();
490}
491
492void TabContents::HandleMouseActivate() {
493 if (delegate_)
494 delegate_->HandleMouseActivate();
495}
496
[email protected]8a5e0ca2011-08-25 06:30:47497void TabContents::ToggleFullscreenMode(bool enter_fullscreen) {
498 if (delegate_)
499 delegate_->ToggleFullscreenModeForTab(this, enter_fullscreen);
500}
501
[email protected]5d5f7af2011-10-01 01:38:12502bool TabContents::IsFullscreenForCurrentTab() const {
503 return delegate_ ? delegate_->IsFullscreenForTab(this) : false;
504}
505
[email protected]0548c5352011-09-07 00:33:33506void TabContents::UpdatePreferredSize(const gfx::Size& pref_size) {
507 if (delegate_)
508 delegate_->UpdatePreferredSize(this, pref_size);
509}
510
[email protected]96d185d2009-04-24 03:28:54511void TabContents::ShowContents() {
[email protected]8cb5d5b2010-02-09 11:36:16512 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
513 if (rwhv)
514 rwhv->DidBecomeSelected();
[email protected]96d185d2009-04-24 03:28:54515}
516
517void TabContents::HideContents() {
518 // TODO(pkasting): http://b/1239839 Right now we purposefully don't call
519 // our superclass HideContents(), because some callers want to be very picky
520 // about the order in which these get called. In addition to making the code
521 // here practically impossible to understand, this also means we end up
522 // calling TabContents::WasHidden() twice if callers call both versions of
[email protected]57c6a652009-05-04 07:58:34523 // HideContents() on a TabContents.
[email protected]96d185d2009-04-24 03:28:54524 WasHidden();
525}
526
[email protected]5aab5e22010-12-08 22:13:29527bool TabContents::NeedToFireBeforeUnload() {
528 // TODO(creis): Should we fire even for interstitial pages?
529 return notify_disconnection() &&
530 !showing_interstitial_page() &&
531 !render_view_host()->SuddenTerminationAllowed();
532}
533
[email protected]00c37fc2011-08-02 00:22:50534// TODO(adriansc): Remove this method once refactoring changed all call sites.
[email protected]992e4542011-07-20 23:09:25535TabContents* TabContents::OpenURL(const GURL& url,
536 const GURL& referrer,
537 WindowOpenDisposition disposition,
538 PageTransition::Type transition) {
[email protected]00c37fc2011-08-02 00:22:50539 return OpenURL(OpenURLParams(url, referrer, disposition, transition));
540}
541
542TabContents* TabContents::OpenURL(const OpenURLParams& params) {
[email protected]1c642b52011-04-15 09:05:49543 if (delegate_) {
[email protected]00c37fc2011-08-02 00:22:50544 TabContents* new_contents = delegate_->OpenURLFromTab(this, params);
[email protected]1c642b52011-04-15 09:05:49545 // Notify observers.
546 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
[email protected]00c37fc2011-08-02 00:22:50547 DidOpenURL(params.url, params.referrer,
548 params.disposition, params.transition));
[email protected]992e4542011-07-20 23:09:25549 return new_contents;
[email protected]1c642b52011-04-15 09:05:49550 }
[email protected]992e4542011-07-20 23:09:25551 return NULL;
[email protected]d5f942ba2008-09-26 19:30:34552}
553
[email protected]1ccb3568d2010-02-19 10:51:16554bool TabContents::NavigateToPendingEntry(
555 NavigationController::ReloadType reload_type) {
[email protected]876bc832010-09-07 16:29:54556 return NavigateToEntry(*controller_.pending_entry(), reload_type);
557}
[email protected]96d185d2009-04-24 03:28:54558
[email protected]876bc832010-09-07 16:29:54559bool TabContents::NavigateToEntry(
560 const NavigationEntry& entry,
561 NavigationController::ReloadType reload_type) {
[email protected]19e81142011-10-03 16:19:40562 // The renderer will reject IPC messages with URLs longer than
563 // this limit, so don't attempt to navigate with a longer URL.
564 if (entry.url().spec().size() > content::kMaxURLChars)
565 return false;
566
[email protected]ffc906f2011-10-04 22:55:40567 RenderViewHost* dest_render_view_host = render_manager_.Navigate(entry);
568 if (!dest_render_view_host)
569 return false; // Unable to create the desired render view host.
570
[email protected]80a8fad2011-01-29 04:02:38571 // For security, we should never send non-Web-UI URLs to a Web UI renderer.
[email protected]05fdd492010-11-15 17:52:07572 // Double check that here.
[email protected]54ec6472010-04-09 19:39:58573 int enabled_bindings = dest_render_view_host->enabled_bindings();
[email protected]1fd1a502011-03-30 16:55:56574 bool is_allowed_in_web_ui_renderer = content::GetContentClient()->
[email protected]3d7474ff2011-07-27 17:47:37575 browser()->GetWebUIFactory()->IsURLAcceptableForWebUI(browser_context(),
[email protected]1fd1a502011-03-30 16:55:56576 entry.url());
[email protected]e091df82011-10-11 18:13:21577 CHECK(!(enabled_bindings & content::BINDINGS_POLICY_WEB_UI) ||
[email protected]c09163a2011-02-15 00:05:55578 is_allowed_in_web_ui_renderer);
[email protected]54ec6472010-04-09 19:39:58579
[email protected]96d185d2009-04-24 03:28:54580 // Tell DevTools agent that it is attached prior to the navigation.
[email protected]d9f9b792009-06-24 13:17:12581 DevToolsManager* devtools_manager = DevToolsManager::GetInstance();
[email protected]7e8e6b62009-05-08 11:28:32582 if (devtools_manager) { // NULL in unit tests.
[email protected]3cc72b12010-03-18 23:03:00583 devtools_manager->OnNavigatingToPendingEntry(render_view_host(),
584 dest_render_view_host,
585 entry.url());
[email protected]7e8e6b62009-05-08 11:28:32586 }
[email protected]96d185d2009-04-24 03:28:54587
588 // Used for page load time metrics.
589 current_load_start_ = base::TimeTicks::Now();
590
591 // Navigate in the desired RenderViewHost.
[email protected]056de2d2009-06-26 16:41:34592 ViewMsg_Navigate_Params navigate_params;
[email protected]6c6b02d2011-09-02 03:36:47593 MakeNavigateParams(entry, controller_, delegate_, reload_type,
594 &navigate_params);
[email protected]056de2d2009-06-26 16:41:34595 dest_render_view_host->Navigate(navigate_params);
[email protected]96d185d2009-04-24 03:28:54596
597 if (entry.page_id() == -1) {
598 // HACK!! This code suppresses javascript: URLs from being added to
599 // session history, which is what we want to do for javascript: URLs that
600 // do not generate content. What we really need is a message from the
601 // renderer telling us that a new page was not created. The same message
602 // could be used for mailto: URLs and the like.
603 if (entry.url().SchemeIs(chrome::kJavaScriptScheme))
604 return false;
605 }
606
[email protected]3c9e1872010-11-18 16:17:49607 // Notify observers about navigation.
[email protected]b375c5d2011-05-03 21:15:04608 FOR_EACH_OBSERVER(TabContentsObserver,
609 observers_,
610 NavigateToPendingEntry(entry.url(), reload_type));
[email protected]96d185d2009-04-24 03:28:54611
[email protected]09b29342011-06-24 19:18:48612 if (delegate_)
613 delegate_->DidNavigateToPendingEntry(this);
614
[email protected]d5f942ba2008-09-26 19:30:34615 return true;
616}
617
[email protected]9e1ad4b2011-08-14 16:49:19618void TabContents::SetHistoryLengthAndPrune(const SiteInstance* site_instance,
619 int history_length,
620 int32 minimum_page_id) {
[email protected]5229253b2011-08-16 14:36:41621 // SetHistoryLengthAndPrune doesn't work when there are pending cross-site
622 // navigations. Callers should ensure that this is the case.
[email protected]9e1ad4b2011-08-14 16:49:19623 if (render_manager_.pending_render_view_host()) {
624 NOTREACHED();
[email protected]796931a92011-08-10 01:32:14625 return;
[email protected]9e1ad4b2011-08-14 16:49:19626 }
627 RenderViewHost* rvh = render_view_host();
628 if (!rvh) {
629 NOTREACHED();
630 return;
631 }
632 if (site_instance && rvh->site_instance() != site_instance) {
633 NOTREACHED();
634 return;
635 }
636 rvh->Send(new ViewMsg_SetHistoryLengthAndPrune(rvh->routing_id(),
637 history_length,
638 minimum_page_id));
[email protected]796931a92011-08-10 01:32:14639}
640
[email protected]96d185d2009-04-24 03:28:54641void TabContents::Stop() {
642 render_manager_.Stop();
[email protected]2e3f4572011-03-25 19:24:47643 FOR_EACH_OBSERVER(TabContentsObserver, observers_, StopNavigation());
[email protected]96d185d2009-04-24 03:28:54644}
645
[email protected]96d185d2009-04-24 03:28:54646TabContents* TabContents::Clone() {
647 // We create a new SiteInstance so that the new tab won't share processes
648 // with the old one. This can be changed in the future if we need it to share
649 // processes for some reason.
[email protected]3d7474ff2011-07-27 17:47:37650 TabContents* tc = new TabContents(
651 browser_context(),
652 SiteInstance::CreateSiteInstance(browser_context()),
653 MSG_ROUTING_NONE, this, NULL);
[email protected]96d185d2009-04-24 03:28:54654 tc->controller().CopyStateFrom(controller_);
655 return tc;
656}
657
[email protected]4d677202009-07-19 07:37:12658void TabContents::ShowPageInfo(const GURL& url,
659 const NavigationEntry::SSLStatus& ssl,
660 bool show_history) {
661 if (!delegate_)
662 return;
663
[email protected]3d7474ff2011-07-27 17:47:37664 delegate_->ShowPageInfo(browser_context(), url, ssl, show_history);
[email protected]4d677202009-07-19 07:37:12665}
666
[email protected]e7cfdbd2011-04-22 14:41:37667void TabContents::AddNewContents(TabContents* new_contents,
668 WindowOpenDisposition disposition,
669 const gfx::Rect& initial_pos,
670 bool user_gesture) {
initial.commit09911bf2008-07-26 23:55:29671 if (!delegate_)
672 return;
673
[email protected]e7cfdbd2011-04-22 14:41:37674 delegate_->AddNewContents(this, new_contents, disposition, initial_pos,
675 user_gesture);
[email protected]96d185d2009-04-24 03:28:54676}
677
[email protected]5c9e97a2009-09-09 23:48:30678gfx::NativeView TabContents::GetContentNativeView() const {
[email protected]96d185d2009-04-24 03:28:54679 return view_->GetContentNativeView();
680}
681
682gfx::NativeView TabContents::GetNativeView() const {
683 return view_->GetNativeView();
684}
685
686void TabContents::GetContainerBounds(gfx::Rect *out) const {
687 view_->GetContainerBounds(out);
688}
689
690void TabContents::Focus() {
691 view_->Focus();
692}
693
[email protected]90daadb42009-06-08 21:27:28694void TabContents::FocusThroughTabTraversal(bool reverse) {
[email protected]7e383692009-06-12 19:14:54695 if (showing_interstitial_page()) {
696 render_manager_.interstitial_page()->FocusThroughTabTraversal(reverse);
697 return;
698 }
[email protected]96d185d2009-04-24 03:28:54699 render_view_host()->SetInitialFocus(reverse);
700}
701
702bool TabContents::FocusLocationBarByDefault() {
[email protected]d0980792011-02-13 19:41:40703 WebUI* web_ui = GetWebUIForCurrentState();
[email protected]7ade2732011-02-10 00:13:58704 if (web_ui)
705 return web_ui->focus_location_bar_by_default();
[email protected]3e3f0eb2009-06-22 18:33:43706 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]e0d481582009-09-15 21:06:25707 if (entry && entry->url() == GURL(chrome::kAboutBlankURL))
[email protected]3e3f0eb2009-06-22 18:33:43708 return true;
[email protected]96d185d2009-04-24 03:28:54709 return false;
710}
711
[email protected]a26dc362010-04-23 01:48:58712void TabContents::SetFocusToLocationBar(bool select_all) {
[email protected]a11aaf22010-03-30 00:03:38713 if (delegate())
[email protected]a26dc362010-04-23 01:48:58714 delegate()->SetFocusToLocationBar(select_all);
[email protected]a11aaf22010-03-30 00:03:38715}
716
[email protected]686493142011-07-15 21:47:22717bool TabContents::CanDownload(int request_id) {
718 TabContentsDelegate* d = delegate();
719 if (d)
720 return d->CanDownload(this, request_id);
721 return true;
722}
723
724void TabContents::OnStartDownload(DownloadItem* download) {
725 TabContentsDelegate* d = delegate();
726 if (d)
727 d->OnStartDownload(this, download);
728}
729
[email protected]c7dd2f62011-07-18 15:57:59730void TabContents::OnSavePage() {
731 // If we can not save the page, try to download it.
732 if (!SavePackage::IsSavableContents(contents_mime_type())) {
[email protected]3d7474ff2011-07-27 17:47:37733 DownloadManager* dlm = browser_context()->GetDownloadManager();
[email protected]c7dd2f62011-07-18 15:57:59734 const GURL& current_page_url = GetURL();
735 if (dlm && current_page_url.is_valid()) {
736 dlm->DownloadUrl(current_page_url, GURL(), "", this);
[email protected]8bd9e562011-08-16 23:55:46737 download_stats::RecordDownloadCount(
738 download_stats::INITIATED_BY_SAVE_PACKAGE_FAILURE_COUNT);
[email protected]c7dd2f62011-07-18 15:57:59739 return;
740 }
741 }
742
743 Stop();
744
745 // Create the save package and possibly prompt the user for the name to save
746 // the page as. The user prompt is an asynchronous operation that runs on
747 // another thread.
748 save_package_ = new SavePackage(this);
749 save_package_->GetSaveInfo();
750}
751
752// Used in automated testing to bypass prompting the user for file names.
753// Instead, the names and paths are hard coded rather than running them through
754// file name sanitation and extension / mime checking.
755bool TabContents::SavePage(const FilePath& main_file, const FilePath& dir_path,
756 SavePackage::SavePackageType save_type) {
757 // Stop the page from navigating.
758 Stop();
759
760 save_package_ = new SavePackage(this, save_type, main_file, dir_path);
761 return save_package_->Init();
762}
763
764void TabContents::OnSaveURL(const GURL& url) {
[email protected]3d7474ff2011-07-27 17:47:37765 DownloadManager* dlm = browser_context()->GetDownloadManager();
[email protected]c7dd2f62011-07-18 15:57:59766 dlm->DownloadUrl(url, GetURL(), "", this);
767}
768
[email protected]420ae012009-04-24 05:16:32769bool TabContents::IsActiveEntry(int32 page_id) {
770 NavigationEntry* active_entry = controller_.GetActiveEntry();
771 return (active_entry != NULL &&
772 active_entry->site_instance() == GetSiteInstance() &&
773 active_entry->page_id() == page_id);
774}
775
[email protected]b2fe07d12010-02-09 14:38:08776void TabContents::SetOverrideEncoding(const std::string& encoding) {
[email protected]8cb5d5b2010-02-09 11:36:16777 set_encoding(encoding);
[email protected]216813952011-05-19 22:21:26778 render_view_host()->Send(new ViewMsg_SetPageEncoding(
779 render_view_host()->routing_id(), encoding));
[email protected]8cb5d5b2010-02-09 11:36:16780}
781
[email protected]b2fe07d12010-02-09 14:38:08782void TabContents::ResetOverrideEncoding() {
[email protected]8cb5d5b2010-02-09 11:36:16783 reset_encoding();
[email protected]216813952011-05-19 22:21:26784 render_view_host()->Send(new ViewMsg_ResetPageEncodingToDefault(
785 render_view_host()->routing_id()));
[email protected]8cb5d5b2010-02-09 11:36:16786}
787
[email protected]7ab1e7d62009-10-14 23:32:01788void TabContents::OnCloseStarted() {
789 if (tab_close_start_time_.is_null())
790 tab_close_start_time_ = base::TimeTicks::Now();
791}
792
[email protected]46624bf2010-06-09 16:04:19793bool TabContents::ShouldAcceptDragAndDrop() const {
794#if defined(OS_CHROMEOS)
795 // ChromeOS panels (pop-ups) do not take drag-n-drop.
796 // See http://crosbug.com/2413
[email protected]b35b26b32011-05-05 20:35:14797 if (delegate() && delegate()->IsPopupOrPanel(this))
[email protected]b83af492010-10-09 03:21:20798 return false;
799 return true;
[email protected]46624bf2010-06-09 16:04:19800#else
801 return true;
802#endif
803}
804
[email protected]7813bd72011-02-05 02:19:34805void TabContents::SystemDragEnded() {
806 if (render_view_host())
807 render_view_host()->DragSourceSystemDragEnded();
808 if (delegate())
809 delegate()->DragEnded();
810}
811
[email protected]d0b8d092010-10-25 04:05:17812double TabContents::GetZoomLevel() const {
[email protected]3d7474ff2011-07-27 17:47:37813 HostZoomMap* zoom_map = browser_context()->GetHostZoomMap();
[email protected]b75b8292010-10-01 07:28:25814 if (!zoom_map)
[email protected]d0b8d092010-10-25 04:05:17815 return 0;
[email protected]b75b8292010-10-01 07:28:25816
817 double zoom_level;
818 if (temporary_zoom_settings_) {
819 zoom_level = zoom_map->GetTemporaryZoomLevel(
820 GetRenderProcessHost()->id(), render_view_host()->routing_id());
821 } else {
[email protected]13ffa32e2011-05-27 16:37:01822 zoom_level = zoom_map->GetZoomLevel(net::GetHostOrSpecFromURL(GetURL()));
[email protected]b75b8292010-10-01 07:28:25823 }
[email protected]d0b8d092010-10-25 04:05:17824 return zoom_level;
825}
[email protected]b75b8292010-10-01 07:28:25826
[email protected]d0b8d092010-10-25 04:05:17827int TabContents::GetZoomPercent(bool* enable_increment,
828 bool* enable_decrement) {
829 *enable_decrement = *enable_increment = false;
[email protected]b75b8292010-10-01 07:28:25830 int percent = static_cast<int>(
[email protected]d0b8d092010-10-25 04:05:17831 WebKit::WebView::zoomLevelToZoomFactor(GetZoomLevel()) * 100);
[email protected]b75b8292010-10-01 07:28:25832 *enable_decrement = percent > minimum_zoom_percent_;
833 *enable_increment = percent < maximum_zoom_percent_;
834 return percent;
835}
836
[email protected]3c733bde2010-12-21 19:56:31837void TabContents::ViewSource() {
[email protected]1788e772010-12-15 16:40:50838 if (!delegate_)
839 return;
840
841 NavigationEntry* active_entry = controller().GetActiveEntry();
842 if (!active_entry)
843 return;
844
845 delegate_->ViewSourceForTab(this, active_entry->url());
[email protected]77d8d622010-12-15 10:30:12846}
847
[email protected]932b7a12011-03-09 12:50:27848void TabContents::ViewFrameSource(const GURL& url,
849 const std::string& content_state) {
850 if (!delegate_)
851 return;
852
853 delegate_->ViewSourceForFrame(this, url, content_state);
854}
855
[email protected]c40d6232011-03-25 00:16:21856void TabContents::SetContentRestrictions(int restrictions) {
857 content_restrictions_ = restrictions;
858 delegate()->ContentRestrictionsChanged(this);
859}
860
[email protected]724159a2010-12-30 01:11:18861void TabContents::OnDidStartProvisionalLoadForFrame(int64 frame_id,
862 bool is_main_frame,
[email protected]57b9396c2011-10-07 19:11:59863 const GURL& opener_url,
[email protected]724159a2010-12-30 01:11:18864 const GURL& url) {
865 bool is_error_page = (url.spec() == chrome::kUnreachableWebDataURL);
866 GURL validated_url(url);
867 render_view_host()->FilterURL(ChildProcessSecurityPolicy::GetInstance(),
868 GetRenderProcessHost()->id(), &validated_url);
869
[email protected]8093a542011-05-13 07:29:32870 RenderViewHost* rvh =
871 render_manager_.pending_render_view_host() ?
872 render_manager_.pending_render_view_host() : render_view_host();
[email protected]0d60f0192011-04-14 12:40:10873 // Notify observers about the start of the provisional load.
874 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
875 DidStartProvisionalLoadForFrame(frame_id, is_main_frame,
[email protected]8093a542011-05-13 07:29:32876 validated_url, is_error_page, rvh));
[email protected]0d60f0192011-04-14 12:40:10877
[email protected]724159a2010-12-30 01:11:18878 if (is_main_frame) {
[email protected]4850a7f2011-03-08 23:36:59879 // Notify observers about the provisional change in the main frame URL.
880 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
[email protected]57b9396c2011-10-07 19:11:59881 ProvisionalChangeToMainFrameUrl(url, opener_url));
[email protected]724159a2010-12-30 01:11:18882 }
883}
884
885void TabContents::OnDidRedirectProvisionalLoad(int32 page_id,
[email protected]57b9396c2011-10-07 19:11:59886 const GURL& opener_url,
[email protected]724159a2010-12-30 01:11:18887 const GURL& source_url,
888 const GURL& target_url) {
[email protected]18ba2b12011-04-06 16:35:49889 // TODO(creis): Remove this method and have the pre-rendering code listen to
890 // the ResourceDispatcherHost's RESOURCE_RECEIVED_REDIRECT notification
891 // instead. See http://crbug.com/78512.
[email protected]724159a2010-12-30 01:11:18892 NavigationEntry* entry;
893 if (page_id == -1)
894 entry = controller_.pending_entry();
895 else
896 entry = controller_.GetEntryWithPageID(GetSiteInstance(), page_id);
897 if (!entry || entry->url() != source_url)
898 return;
[email protected]e7d50892011-01-19 21:47:38899
[email protected]4850a7f2011-03-08 23:36:59900 // Notify observers about the provisional change in the main frame URL.
901 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
[email protected]eacb080b2011-05-22 19:40:26902 ProvisionalChangeToMainFrameUrl(target_url,
[email protected]57b9396c2011-10-07 19:11:59903 opener_url));
[email protected]724159a2010-12-30 01:11:18904}
905
906void TabContents::OnDidFailProvisionalLoadWithError(
[email protected]d7b175e2011-10-11 15:31:58907 const ViewHostMsg_DidFailProvisionalLoadWithError_Params& params) {
908 VLOG(1) << "Failed Provisional Load: " << params.url.possibly_invalid_spec()
909 << ", error_code: " << params.error_code
910 << ", error_description: " << params.error_description
911 << ", is_main_frame: " << params.is_main_frame
912 << ", showing_repost_interstitial: " <<
913 params.showing_repost_interstitial
914 << ", frame_id: " << params.frame_id;
915 GURL validated_url(params.url);
[email protected]724159a2010-12-30 01:11:18916 render_view_host()->FilterURL(ChildProcessSecurityPolicy::GetInstance(),
917 GetRenderProcessHost()->id(), &validated_url);
918
[email protected]d7b175e2011-10-11 15:31:58919 if (net::ERR_ABORTED == params.error_code) {
[email protected]724159a2010-12-30 01:11:18920 // EVIL HACK ALERT! Ignore failed loads when we're showing interstitials.
921 // This means that the interstitial won't be torn down properly, which is
922 // bad. But if we have an interstitial, go back to another tab type, and
923 // then load the same interstitial again, we could end up getting the first
924 // interstitial's "failed" message (as a result of the cancel) when we're on
925 // the second one.
926 //
927 // We can't tell this apart, so we think we're tearing down the current page
928 // which will cause a crash later one. There is also some code in
929 // RenderViewHostManager::RendererAbortedProvisionalLoad that is commented
930 // out because of this problem.
931 //
932 // http://code.google.com/p/chromium/issues/detail?id=2855
933 // Because this will not tear down the interstitial properly, if "back" is
934 // back to another tab type, the interstitial will still be somewhat alive
935 // in the previous tab type. If you navigate somewhere that activates the
936 // tab with the interstitial again, you'll see a flash before the new load
937 // commits of the interstitial page.
938 if (showing_interstitial_page()) {
939 LOG(WARNING) << "Discarding message during interstitial.";
940 return;
941 }
942
[email protected]02102f82011-06-13 20:37:02943 // Discard our pending entry if the load canceled (e.g. if we decided to
944 // download the file instead of load it). We do not verify that the URL
945 // being canceled matches the pending entry's URL because they will not
946 // match if a redirect occurred (in which case we do not want to leave a
947 // stale redirect URL showing). This means that we also cancel the pending
948 // entry if the user started a new navigation. As a result, the navigation
949 // controller may not remember that a load is in progress, but the
950 // navigation will still commit even if there is no pending entry.
[email protected]724159a2010-12-30 01:11:18951 NavigationEntry* pending_entry = controller_.pending_entry();
[email protected]02102f82011-06-13 20:37:02952 if (pending_entry)
[email protected]c95fa8b2011-04-28 20:26:16953 DidCancelLoading();
[email protected]724159a2010-12-30 01:11:18954
955 render_manager_.RendererAbortedProvisionalLoad(render_view_host());
956 }
957
958 // Send out a notification that we failed a provisional load with an error.
959 ProvisionalLoadDetails details(
[email protected]d7b175e2011-10-11 15:31:58960 params.is_main_frame,
961 controller_.IsURLInPageNavigation(validated_url),
962 validated_url,
963 std::string(),
964 false,
965 params.frame_id);
966 details.set_error_code(params.error_code);
[email protected]724159a2010-12-30 01:11:18967
968 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:27969 content::NOTIFICATION_FAIL_PROVISIONAL_LOAD_WITH_ERROR,
[email protected]724159a2010-12-30 01:11:18970 Source<NavigationController>(&controller_),
971 Details<ProvisionalLoadDetails>(&details));
[email protected]0d60f0192011-04-14 12:40:10972
[email protected]d7b175e2011-10-11 15:31:58973 FOR_EACH_OBSERVER(TabContentsObserver,
974 observers_,
975 DidFailProvisionalLoad(params.frame_id,
976 params.is_main_frame,
977 validated_url,
978 params.error_code,
979 params.error_description));
[email protected]724159a2010-12-30 01:11:18980}
981
982void TabContents::OnDidLoadResourceFromMemoryCache(
983 const GURL& url,
[email protected]70435962011-08-02 20:13:28984 const std::string& security_info,
985 const std::string& http_method,
986 ResourceType::Type resource_type) {
[email protected]28620862011-03-22 23:07:19987 base::StatsCounter cache("WebKit.CacheHit");
[email protected]724159a2010-12-30 01:11:18988 cache.Increment();
989
990 // Send out a notification that we loaded a resource from our memory cache.
[email protected]70d66502011-09-23 00:55:08991 int cert_id = 0;
992 net::CertStatus cert_status = 0;
993 int security_bits = -1;
994 int connection_status = 0;
[email protected]724159a2010-12-30 01:11:18995 SSLManager::DeserializeSecurityInfo(security_info,
996 &cert_id, &cert_status,
997 &security_bits,
998 &connection_status);
[email protected]7a8c55e2011-02-15 08:21:16999 LoadFromMemoryCacheDetails details(url, GetRenderProcessHost()->id(),
1000 cert_id, cert_status);
[email protected]724159a2010-12-30 01:11:181001
1002 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271003 content::NOTIFICATION_LOAD_FROM_MEMORY_CACHE,
[email protected]724159a2010-12-30 01:11:181004 Source<NavigationController>(&controller_),
1005 Details<LoadFromMemoryCacheDetails>(&details));
1006}
1007
1008void TabContents::OnDidDisplayInsecureContent() {
[email protected]221a5d92011-04-28 18:23:161009 UserMetrics::RecordAction(UserMetricsAction("SSL.DisplayedInsecureContent"));
[email protected]724159a2010-12-30 01:11:181010 displayed_insecure_content_ = true;
[email protected]9678d8f2011-08-05 04:05:071011 SSLManager::NotifySSLInternalStateChanged(&controller());
[email protected]724159a2010-12-30 01:11:181012}
1013
1014void TabContents::OnDidRunInsecureContent(
[email protected]92771112011-01-20 00:13:021015 const std::string& security_origin, const GURL& target_url) {
[email protected]9941cf52011-02-08 16:17:231016 LOG(INFO) << security_origin << " ran insecure content from "
1017 << target_url.possibly_invalid_spec();
[email protected]221a5d92011-04-28 18:23:161018 UserMetrics::RecordAction(UserMetricsAction("SSL.RanInsecureContent"));
[email protected]ca406032011-07-19 21:53:051019 if (EndsWith(security_origin, kDotGoogleDotCom, false)) {
1020 UserMetrics::RecordAction(
1021 UserMetricsAction("SSL.RanInsecureContentGoogle"));
1022 }
[email protected]724159a2010-12-30 01:11:181023 controller_.ssl_manager()->DidRunInsecureContent(security_origin);
[email protected]f2b58c4d2011-06-01 23:29:411024 displayed_insecure_content_ = true;
[email protected]9678d8f2011-08-05 04:05:071025 SSLManager::NotifySSLInternalStateChanged(&controller());
[email protected]724159a2010-12-30 01:11:181026}
1027
1028void TabContents::OnDocumentLoadedInFrame(int64 frame_id) {
1029 controller_.DocumentLoadedInFrame();
[email protected]0d60f0192011-04-14 12:40:101030 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
1031 DocumentLoadedInFrame(frame_id));
[email protected]724159a2010-12-30 01:11:181032}
1033
1034void TabContents::OnDidFinishLoad(int64 frame_id) {
[email protected]0d60f0192011-04-14 12:40:101035 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
1036 DidFinishLoad(frame_id));
[email protected]724159a2010-12-30 01:11:181037}
1038
[email protected]c8f73ab2011-01-22 00:05:171039void TabContents::OnUpdateContentRestrictions(int restrictions) {
[email protected]c40d6232011-03-25 00:16:211040 SetContentRestrictions(restrictions);
[email protected]c8f73ab2011-01-22 00:05:171041}
1042
[email protected]216813952011-05-19 22:21:261043void TabContents::OnGoToEntryAtOffset(int offset) {
1044 if (!delegate_ || delegate_->OnGoToEntryOffset(offset)) {
1045 NavigationEntry* entry = controller_.GetEntryAtOffset(offset);
1046 if (!entry)
1047 return;
1048 // Note that we don't call NavigationController::GotToOffset() as we don't
1049 // want to create a pending navigation entry (it might end up lingering
1050 // http://crbug.com/51680).
1051 entry->set_transition_type(entry->transition_type() |
1052 PageTransition::FORWARD_BACK);
1053 NavigateToEntry(*entry, NavigationController::NO_RELOAD);
[email protected]a13cc362011-07-28 21:29:571054
1055 // If the entry is being restored and doesn't have a SiteInstance yet, fill
1056 // it in now that we know. This allows us to find the entry when it commits.
1057 if (!entry->site_instance() &&
1058 entry->restore_type() != NavigationEntry::RESTORE_NONE) {
[email protected]77362eb2011-08-01 17:18:381059 entry->set_site_instance(GetPendingSiteInstance());
[email protected]a13cc362011-07-28 21:29:571060 }
[email protected]216813952011-05-19 22:21:261061 }
1062}
1063
1064void TabContents::OnUpdateZoomLimits(int minimum_percent,
1065 int maximum_percent,
1066 bool remember) {
1067 minimum_zoom_percent_ = minimum_percent;
1068 maximum_zoom_percent_ = maximum_percent;
1069 temporary_zoom_settings_ = !remember;
1070}
1071
1072void TabContents::OnFocusedNodeChanged(bool is_editable_node) {
1073 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271074 content::NOTIFICATION_FOCUS_CHANGED_IN_PAGE,
[email protected]216813952011-05-19 22:21:261075 Source<TabContents>(this),
1076 Details<const bool>(&is_editable_node));
1077}
1078
[email protected]3a29a6e2011-08-24 18:26:211079void TabContents::OnEnumerateDirectory(int request_id,
1080 const FilePath& path) {
1081 delegate()->EnumerateDirectory(this, request_id, path);
1082}
1083
[email protected]7d189022011-08-25 22:54:201084void TabContents::OnJSOutOfMemory() {
1085 delegate()->JSOutOfMemory(this);
1086}
1087
1088void TabContents::OnRegisterProtocolHandler(const std::string& protocol,
1089 const GURL& url,
1090 const string16& title) {
1091 delegate()->RegisterProtocolHandler(this, protocol, url, title);
1092}
1093
1094void TabContents::OnRegisterIntentHandler(const string16& action,
1095 const string16& type,
1096 const string16& href,
1097 const string16& title) {
1098 delegate()->RegisterIntentHandler(this, action, type, href, title);
1099}
1100
1101void TabContents::OnWebIntentDispatch(const IPC::Message& message,
[email protected]ce9751942011-09-21 01:57:241102 const webkit_glue::WebIntentData& intent,
[email protected]7d189022011-08-25 22:54:201103 int intent_id) {
[email protected]ce9751942011-09-21 01:57:241104 delegate()->WebIntentDispatch(this, message.routing_id(), intent, intent_id);
[email protected]7d189022011-08-25 22:54:201105}
1106
[email protected]b888919c2011-09-02 00:32:161107void TabContents::OnFindReply(int request_id,
1108 int number_of_matches,
1109 const gfx::Rect& selection_rect,
1110 int active_match_ordinal,
1111 bool final_update) {
1112 delegate()->FindReply(this, request_id, number_of_matches, selection_rect,
1113 active_match_ordinal, final_update);
[email protected]4e6239c2011-09-23 00:11:001114 // Send a notification to the renderer that we are ready to receive more
1115 // results from the scoping effort of the Find operation. The FindInPage
1116 // scoping is asynchronous and periodically sends results back up to the
1117 // browser using IPC. In an effort to not spam the browser we have the
1118 // browser send an ACK for each FindReply message and have the renderer
1119 // queue up the latest status message while waiting for this ACK.
1120 render_view_host()->Send(
1121 new ViewMsg_FindReplyACK(render_view_host()->routing_id()));
[email protected]b888919c2011-09-02 00:32:161122}
1123
[email protected]d952a052011-09-06 18:42:451124void TabContents::OnCrashedPlugin(const FilePath& plugin_path) {
1125 delegate()->CrashedPlugin(this, plugin_path);
1126}
1127
[email protected]7fc4bbb2011-09-08 21:23:101128void TabContents::OnAppCacheAccessed(const GURL& manifest_url,
1129 bool blocked_by_policy) {
1130 // Notify observers about navigation.
1131 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
1132 AppCacheAccessed(manifest_url, blocked_by_policy));
1133}
1134
[email protected]96d185d2009-04-24 03:28:541135// Notifies the RenderWidgetHost instance about the fact that the page is
1136// loading, or done loading and calls the base implementation.
1137void TabContents::SetIsLoading(bool is_loading,
1138 LoadNotificationDetails* details) {
1139 if (is_loading == is_loading_)
1140 return;
1141
1142 if (!is_loading) {
[email protected]9c235f042011-08-10 22:28:211143 load_state_ = net::LoadStateWithParam(net::LOAD_STATE_IDLE, string16());
[email protected]96d185d2009-04-24 03:28:541144 load_state_host_.clear();
[email protected]094e5b22009-09-25 04:23:561145 upload_size_ = 0;
1146 upload_position_ = 0;
[email protected]96d185d2009-04-24 03:28:541147 }
1148
1149 render_manager_.SetIsLoading(is_loading);
1150
1151 is_loading_ = is_loading;
1152 waiting_for_response_ = is_loading;
1153
[email protected]6ebdc9b2010-09-27 16:55:571154 if (delegate_)
1155 delegate_->LoadingStateChanged(this);
[email protected]329581b2009-04-28 06:52:351156 NotifyNavigationStateChanged(INVALIDATE_LOAD);
[email protected]96d185d2009-04-24 03:28:541157
[email protected]432115822011-07-10 15:52:271158 int type = is_loading ? content::NOTIFICATION_LOAD_START :
1159 content::NOTIFICATION_LOAD_STOP;
[email protected]ce5c4502009-05-06 16:46:111160 NotificationDetails det = NotificationService::NoDetails();
[email protected]96d185d2009-04-24 03:28:541161 if (details)
1162 det = Details<LoadNotificationDetails>(details);
1163 NotificationService::current()->Notify(type,
1164 Source<NavigationController>(&controller_),
1165 det);
1166}
1167
[email protected]d0980792011-02-13 19:41:401168WebUI* TabContents::GetWebUIForCurrentState() {
[email protected]7ade2732011-02-10 00:13:581169 // When there is a pending navigation entry, we want to use the pending WebUI
[email protected]96d185d2009-04-24 03:28:541170 // that goes along with it to control the basic flags. For example, we want to
1171 // show the pending URL in the URL bar, so we want the display_url flag to
1172 // be from the pending entry.
1173 //
1174 // The confusion comes because there are multiple possibilities for the
1175 // initial load in a tab as a side effect of the way the RenderViewHostManager
1176 // works.
1177 //
[email protected]e0112912011-02-02 22:54:351178 // - For the very first tab the load looks "normal". The new tab Web UI is
[email protected]96d185d2009-04-24 03:28:541179 // the pending one, and we want it to apply here.
1180 //
1181 // - For subsequent new tabs, they'll get a new SiteInstance which will then
1182 // get switched to the one previously associated with the new tab pages.
[email protected]7ade2732011-02-10 00:13:581183 // This switching will cause the manager to commit the RVH/WebUI. So we'll
[email protected]e0112912011-02-02 22:54:351184 // have a committed Web UI in this case.
[email protected]96d185d2009-04-24 03:28:541185 //
1186 // This condition handles all of these cases:
1187 //
1188 // - First load in first tab: no committed nav entry + pending nav entry +
1189 // pending dom ui:
[email protected]e0112912011-02-02 22:54:351190 // -> Use pending Web UI if any.
[email protected]96d185d2009-04-24 03:28:541191 //
1192 // - First load in second tab: no committed nav entry + pending nav entry +
[email protected]e0112912011-02-02 22:54:351193 // no pending Web UI:
1194 // -> Use the committed Web UI if any.
[email protected]96d185d2009-04-24 03:28:541195 //
1196 // - Second navigation in any tab: committed nav entry + pending nav entry:
[email protected]e0112912011-02-02 22:54:351197 // -> Use pending Web UI if any.
[email protected]96d185d2009-04-24 03:28:541198 //
1199 // - Normal state with no load: committed nav entry + no pending nav entry:
[email protected]e0112912011-02-02 22:54:351200 // -> Use committed Web UI.
[email protected]96d185d2009-04-24 03:28:541201 if (controller_.pending_entry() &&
1202 (controller_.GetLastCommittedEntry() ||
[email protected]7ade2732011-02-10 00:13:581203 render_manager_.pending_web_ui()))
1204 return render_manager_.pending_web_ui();
1205 return render_manager_.web_ui();
[email protected]96d185d2009-04-24 03:28:541206}
[email protected]420ae012009-04-24 05:16:321207
[email protected]1fd1a502011-03-30 16:55:561208WebUI::TypeID TabContents::GetWebUITypeForCurrentState() {
[email protected]3d7474ff2011-07-27 17:47:371209 return content::WebUIFactory::Get()->GetWebUIType(browser_context(),
1210 GetURL());
[email protected]1fd1a502011-03-30 16:55:561211}
1212
[email protected]420ae012009-04-24 05:16:321213void TabContents::DidNavigateMainFramePostCommit(
[email protected]8286f51a2011-05-31 17:39:131214 const content::LoadCommittedDetails& details,
[email protected]420ae012009-04-24 05:16:321215 const ViewHostMsg_FrameNavigate_Params& params) {
[email protected]1fd1a502011-03-30 16:55:561216 if (opener_web_ui_type_ != WebUI::kNoWebUI) {
[email protected]80a8fad2011-01-29 04:02:381217 // If this is a window.open navigation, use the same WebUI as the renderer
[email protected]c2e74fe82009-09-03 17:57:441218 // that opened the window, as long as both renderers have the same
1219 // privileges.
[email protected]1fd1a502011-03-30 16:55:561220 if (delegate_ && opener_web_ui_type_ == GetWebUITypeForCurrentState()) {
1221 WebUI* web_ui = content::GetContentClient()->browser()->
1222 GetWebUIFactory()->CreateWebUIForURL(this, GetURL());
[email protected]7ade2732011-02-10 00:13:581223 // web_ui might be NULL if the URL refers to a non-existent extension.
1224 if (web_ui) {
[email protected]d0980792011-02-13 19:41:401225 render_manager_.SetWebUIPostCommit(web_ui);
[email protected]7ade2732011-02-10 00:13:581226 web_ui->RenderViewCreated(render_view_host());
[email protected]c2e74fe82009-09-03 17:57:441227 }
1228 }
[email protected]1fd1a502011-03-30 16:55:561229 opener_web_ui_type_ = WebUI::kNoWebUI;
[email protected]c2e74fe82009-09-03 17:57:441230 }
1231
[email protected]4e697b042011-07-08 06:44:561232 if (details.is_navigation_to_different_page()) {
[email protected]420ae012009-04-24 05:16:321233 // Clear the status bubble. This is a workaround for a bug where WebKit
1234 // doesn't let us know that the cursor left an element during a
1235 // transition (this is also why the mouse cursor remains as a hand after
1236 // clicking on a link); see bugs 1184641 and 980803. We don't want to
1237 // clear the bubble when a user navigates to a named anchor in the same
1238 // page.
1239 UpdateTargetURL(details.entry->page_id(), GURL());
[email protected]420ae012009-04-24 05:16:321240 }
1241
[email protected]a6e82fc2010-02-24 22:28:141242 if (!details.is_in_page) {
[email protected]f17a0ee2010-05-17 17:38:471243 // Once the main frame is navigated, we're no longer considered to have
1244 // displayed insecure content.
1245 displayed_insecure_content_ = false;
[email protected]aece2c7f2009-08-27 20:43:171246 }
[email protected]ce5c4502009-05-06 16:46:111247
[email protected]3c9e1872010-11-18 16:17:491248 // Notify observers about navigation.
[email protected]585b30362011-01-28 02:30:171249 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
[email protected]3c9e1872010-11-18 16:17:491250 DidNavigateMainFramePostCommit(details, params));
[email protected]420ae012009-04-24 05:16:321251}
1252
1253void TabContents::DidNavigateAnyFramePostCommit(
1254 RenderViewHost* render_view_host,
[email protected]8286f51a2011-05-31 17:39:131255 const content::LoadCommittedDetails& details,
[email protected]420ae012009-04-24 05:16:321256 const ViewHostMsg_FrameNavigate_Params& params) {
[email protected]3ab9cb82011-06-03 18:02:071257 // If we navigate, reset JavaScript state. This does nothing to prevent
[email protected]420ae012009-04-24 05:16:321258 // a malicious script from spamming messages, since the script could just
1259 // reload the page to stop blocking.
[email protected]2e5b90c2011-08-16 21:11:551260 if (dialog_creator_) {
1261 dialog_creator_->ResetJavaScriptState(this);
1262 dialog_creator_ = NULL;
1263 }
[email protected]420ae012009-04-24 05:16:321264
[email protected]3c9e1872010-11-18 16:17:491265 // Notify observers about navigation.
[email protected]585b30362011-01-28 02:30:171266 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
[email protected]3c9e1872010-11-18 16:17:491267 DidNavigateAnyFramePostCommit(details, params));
[email protected]420ae012009-04-24 05:16:321268}
1269
[email protected]420ae012009-04-24 05:16:321270void TabContents::UpdateMaxPageIDIfNecessary(SiteInstance* site_instance,
1271 RenderViewHost* rvh) {
1272 // If we are creating a RVH for a restored controller, then we might
1273 // have more page IDs than the SiteInstance's current max page ID. We must
1274 // make sure that the max page ID is larger than any restored page ID.
1275 // Note that it is ok for conflicting page IDs to exist in another tab
1276 // (i.e., NavigationController), but if any page ID is larger than the max,
1277 // the back/forward list will get confused.
1278 int max_restored_page_id = controller_.max_restored_page_id();
1279 if (max_restored_page_id > 0) {
1280 int curr_max_page_id = site_instance->max_page_id();
1281 if (max_restored_page_id > curr_max_page_id) {
1282 // Need to update the site instance immediately.
1283 site_instance->UpdateMaxPageID(max_restored_page_id);
1284
1285 // Also tell the renderer to update its internal representation. We
1286 // need to reserve enough IDs to make all restored page IDs less than
1287 // the max.
1288 if (curr_max_page_id < 0)
1289 curr_max_page_id = 0;
[email protected]216813952011-05-19 22:21:261290 rvh->Send(new ViewMsg_ReservePageIDRange(
1291 rvh->routing_id(), max_restored_page_id - curr_max_page_id));
[email protected]420ae012009-04-24 05:16:321292 }
1293 }
1294}
1295
[email protected]6b2f7a82011-04-25 19:30:511296bool TabContents::UpdateTitleForEntry(NavigationEntry* entry,
[email protected]acafd272011-07-26 17:35:571297 const string16& title) {
[email protected]420ae012009-04-24 05:16:321298 // For file URLs without a title, use the pathname instead. In the case of a
1299 // synthesized title, we don't want the update to count toward the "one set
1300 // per page of the title to history."
[email protected]6b2f7a82011-04-25 19:30:511301 string16 final_title;
[email protected]420ae012009-04-24 05:16:321302 bool explicit_set;
[email protected]987fc3a2011-05-26 14:18:091303 if (entry && entry->url().SchemeIsFile() && title.empty()) {
[email protected]6b2f7a82011-04-25 19:30:511304 final_title = UTF8ToUTF16(entry->url().ExtractFileName());
[email protected]420ae012009-04-24 05:16:321305 explicit_set = false; // Don't count synthetic titles toward the set limit.
1306 } else {
[email protected]acafd272011-07-26 17:35:571307 TrimWhitespace(title, TRIM_ALL, &final_title);
[email protected]420ae012009-04-24 05:16:321308 explicit_set = true;
1309 }
1310
[email protected]987fc3a2011-05-26 14:18:091311 // If a page is created via window.open and never navigated,
1312 // there will be no navigation entry. In this situation,
1313 // |page_title_when_no_navigaiton_entry_| will be used for page title.
1314 if (entry) {
1315 if (final_title == entry->title())
1316 return false; // Nothing changed, don't bother.
[email protected]420ae012009-04-24 05:16:321317
[email protected]987fc3a2011-05-26 14:18:091318 entry->set_title(final_title);
1319 } else {
1320 if (page_title_when_no_navigation_entry_ == final_title)
1321 return false; // Nothing changed, don't bother.
1322
1323 page_title_when_no_navigation_entry_ = final_title;
1324 }
[email protected]420ae012009-04-24 05:16:321325
[email protected]420ae012009-04-24 05:16:321326 // Lastly, set the title for the view.
[email protected]790e2fd2011-09-21 20:28:231327 view_->SetPageTitle(final_title);
[email protected]420ae012009-04-24 05:16:321328
[email protected]105bb0f2011-05-24 17:12:141329 TitleUpdatedDetails details(entry, explicit_set);
1330
[email protected]cbc0e1b2010-04-12 18:33:041331 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271332 content::NOTIFICATION_TAB_CONTENTS_TITLE_UPDATED,
[email protected]cbc0e1b2010-04-12 18:33:041333 Source<TabContents>(this),
[email protected]105bb0f2011-05-24 17:12:141334 Details<TitleUpdatedDetails>(&details));
[email protected]cbc0e1b2010-04-12 18:33:041335
[email protected]420ae012009-04-24 05:16:321336 return true;
1337}
1338
1339void TabContents::NotifySwapped() {
1340 // After sending out a swap notification, we need to send a disconnect
1341 // notification so that clients that pick up a pointer to |this| can NULL the
1342 // pointer. See Bug 1230284.
1343 notify_disconnection_ = true;
1344 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271345 content::NOTIFICATION_TAB_CONTENTS_SWAPPED,
[email protected]d82ed61e2009-06-16 02:46:221346 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:321347 NotificationService::NoDetails());
1348}
1349
1350void TabContents::NotifyConnected() {
1351 notify_disconnection_ = true;
1352 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271353 content::NOTIFICATION_TAB_CONTENTS_CONNECTED,
[email protected]d82ed61e2009-06-16 02:46:221354 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:321355 NotificationService::NoDetails());
1356}
1357
1358void TabContents::NotifyDisconnected() {
1359 if (!notify_disconnection_)
1360 return;
1361
1362 notify_disconnection_ = false;
1363 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271364 content::NOTIFICATION_TAB_CONTENTS_DISCONNECTED,
[email protected]d82ed61e2009-06-16 02:46:221365 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:321366 NotificationService::NoDetails());
1367}
1368
[email protected]8d3347f2009-07-09 22:00:211369RenderViewHostDelegate::View* TabContents::GetViewDelegate() {
[email protected]420ae012009-04-24 05:16:321370 return view_.get();
1371}
1372
[email protected]8d3347f2009-07-09 22:00:211373RenderViewHostDelegate::RendererManagement*
1374TabContents::GetRendererManagementDelegate() {
1375 return &render_manager_;
1376}
1377
[email protected]3d7474ff2011-07-27 17:47:371378RendererPreferences TabContents::GetRendererPrefs(
1379 content::BrowserContext* browser_context) const {
[email protected]840b1512009-07-21 16:53:461380 return renderer_preferences_;
[email protected]80d96fa2009-06-10 22:34:511381}
1382
[email protected]57c6a652009-05-04 07:58:341383TabContents* TabContents::GetAsTabContents() {
1384 return this;
1385}
1386
[email protected]da4dfc42011-10-12 15:53:561387content::ViewType TabContents::GetRenderViewType() const {
1388 return content::VIEW_TYPE_TAB_CONTENTS;
[email protected]7b291f92009-08-14 05:43:531389}
1390
[email protected]420ae012009-04-24 05:16:321391void TabContents::RenderViewCreated(RenderViewHost* render_view_host) {
[email protected]2e4633c2009-07-09 16:58:061392 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271393 content::NOTIFICATION_RENDER_VIEW_HOST_CREATED_FOR_TAB,
[email protected]2e4633c2009-07-09 16:58:061394 Source<TabContents>(this),
1395 Details<RenderViewHost>(render_view_host));
[email protected]420ae012009-04-24 05:16:321396 NavigationEntry* entry = controller_.GetActiveEntry();
1397 if (!entry)
1398 return;
1399
1400 // When we're creating views, we're still doing initial setup, so we always
[email protected]e0112912011-02-02 22:54:351401 // use the pending Web UI rather than any possibly existing committed one.
[email protected]1fd1a502011-03-30 16:55:561402 if (render_manager_.pending_web_ui())
[email protected]7ade2732011-02-10 00:13:581403 render_manager_.pending_web_ui()->RenderViewCreated(render_view_host);
[email protected]420ae012009-04-24 05:16:321404
1405 if (entry->IsViewSourceMode()) {
1406 // Put the renderer in view source mode.
1407 render_view_host->Send(
1408 new ViewMsg_EnableViewSourceMode(render_view_host->routing_id()));
1409 }
[email protected]0666aef2009-05-13 19:48:081410
1411 view()->RenderViewCreated(render_view_host);
[email protected]11f764bb2011-05-25 19:45:261412
1413 FOR_EACH_OBSERVER(
1414 TabContentsObserver, observers_, RenderViewCreated(render_view_host));
[email protected]420ae012009-04-24 05:16:321415}
1416
1417void TabContents::RenderViewReady(RenderViewHost* rvh) {
1418 if (rvh != render_view_host()) {
1419 // Don't notify the world, since this came from a renderer in the
1420 // background.
1421 return;
1422 }
1423
1424 NotifyConnected();
[email protected]9d8a4642009-07-29 17:25:301425 bool was_crashed = is_crashed();
[email protected]443b80e2010-12-14 00:42:231426 SetIsCrashed(base::TERMINATION_STATUS_STILL_RUNNING, 0);
[email protected]9d8a4642009-07-29 17:25:301427
1428 // Restore the focus to the tab (otherwise the focus will be on the top
1429 // window).
[email protected]484ae5912010-09-29 19:16:141430 if (was_crashed && !FocusLocationBarByDefault() &&
1431 (!delegate_ || delegate_->ShouldFocusPageAfterCrash())) {
[email protected]9d8a4642009-07-29 17:25:301432 Focus();
[email protected]484ae5912010-09-29 19:16:141433 }
[email protected]420ae012009-04-24 05:16:321434}
1435
[email protected]443b80e2010-12-14 00:42:231436void TabContents::RenderViewGone(RenderViewHost* rvh,
1437 base::TerminationStatus status,
1438 int error_code) {
[email protected]420ae012009-04-24 05:16:321439 if (rvh != render_view_host()) {
1440 // The pending page's RenderViewHost is gone.
1441 return;
1442 }
1443
1444 SetIsLoading(false, NULL);
1445 NotifyDisconnected();
[email protected]443b80e2010-12-14 00:42:231446 SetIsCrashed(status, error_code);
[email protected]b3e3f87a2011-10-01 23:37:541447 view()->OnTabCrashed(crashed_status(), crashed_error_code());
[email protected]420ae012009-04-24 05:16:321448
[email protected]55452902011-06-01 21:57:471449 FOR_EACH_OBSERVER(TabContentsObserver, observers_, RenderViewGone());
[email protected]420ae012009-04-24 05:16:321450}
1451
[email protected]2e4633c2009-07-09 16:58:061452void TabContents::RenderViewDeleted(RenderViewHost* rvh) {
[email protected]fb7b79f72009-11-06 18:00:481453 render_manager_.RenderViewDeleted(rvh);
[email protected]2e4633c2009-07-09 16:58:061454}
1455
[email protected]420ae012009-04-24 05:16:321456void TabContents::DidNavigate(RenderViewHost* rvh,
1457 const ViewHostMsg_FrameNavigate_Params& params) {
[email protected]09b29342011-06-24 19:18:481458 if (PageTransition::IsMainFrame(params.transition))
[email protected]420ae012009-04-24 05:16:321459 render_manager_.DidNavigateMainFrame(rvh);
[email protected]8030f012009-09-25 18:09:371460
[email protected]420ae012009-04-24 05:16:321461 // Update the site of the SiteInstance if it doesn't have one yet.
1462 if (!GetSiteInstance()->has_site())
1463 GetSiteInstance()->SetSite(params.url);
1464
1465 // Need to update MIME type here because it's referred to in
1466 // UpdateNavigationCommands() called by RendererDidNavigate() to
1467 // determine whether or not to enable the encoding menu.
1468 // It's updated only for the main frame. For a subframe,
1469 // RenderView::UpdateURL does not set params.contents_mime_type.
1470 // (see http://code.google.com/p/chromium/issues/detail?id=2929 )
1471 // TODO(jungshik): Add a test for the encoding menu to avoid
1472 // regressing it again.
1473 if (PageTransition::IsMainFrame(params.transition))
1474 contents_mime_type_ = params.contents_mime_type;
1475
[email protected]8286f51a2011-05-31 17:39:131476 content::LoadCommittedDetails details;
[email protected]93f230e02011-06-01 14:40:001477 bool did_navigate = controller_.RendererDidNavigate(params, &details);
[email protected]3e90d4a2009-07-03 17:38:391478
[email protected]a9c0bfe2010-09-17 08:35:221479 // Send notification about committed provisional loads. This notification is
1480 // different from the NAV_ENTRY_COMMITTED notification which doesn't include
1481 // the actual URL navigated to and isn't sent for AUTO_SUBFRAME navigations.
1482 if (details.type != NavigationType::NAV_IGNORE) {
[email protected]dabb0d12010-10-05 12:50:071483 // For AUTO_SUBFRAME navigations, an event for the main frame is generated
1484 // that is not recorded in the navigation history. For the purpose of
1485 // tracking navigation events, we treat this event as a sub frame navigation
1486 // event.
1487 bool is_main_frame = did_navigate ? details.is_main_frame : false;
[email protected]0d60f0192011-04-14 12:40:101488 PageTransition::Type transition_type = params.transition;
[email protected]a9c0bfe2010-09-17 08:35:221489 // Whether or not a page transition was triggered by going backward or
1490 // forward in the history is only stored in the navigation controller's
1491 // entry list.
1492 if (did_navigate &&
1493 (controller_.GetActiveEntry()->transition_type() &
1494 PageTransition::FORWARD_BACK)) {
[email protected]0d60f0192011-04-14 12:40:101495 transition_type = params.transition | PageTransition::FORWARD_BACK;
[email protected]a9c0bfe2010-09-17 08:35:221496 }
[email protected]0d60f0192011-04-14 12:40:101497 // Notify observers about the commit of the provisional load.
1498 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
1499 DidCommitProvisionalLoadForFrame(params.frame_id,
1500 is_main_frame, params.url, transition_type));
[email protected]a9c0bfe2010-09-17 08:35:221501 }
1502
[email protected]76543b92009-08-31 17:27:451503 if (!did_navigate)
1504 return; // No navigation happened.
[email protected]420ae012009-04-24 05:16:321505
1506 // DO NOT ADD MORE STUFF TO THIS FUNCTION! Your component should either listen
1507 // for the appropriate notification (best) or you can add it to
1508 // DidNavigateMainFramePostCommit / DidNavigateAnyFramePostCommit (only if
1509 // necessary, please).
1510
1511 // Run post-commit tasks.
[email protected]93f230e02011-06-01 14:40:001512 if (details.is_main_frame) {
[email protected]420ae012009-04-24 05:16:321513 DidNavigateMainFramePostCommit(details, params);
[email protected]09b29342011-06-24 19:18:481514 if (delegate())
1515 delegate()->DidNavigateMainFramePostCommit(this);
[email protected]93f230e02011-06-01 14:40:001516 }
[email protected]420ae012009-04-24 05:16:321517 DidNavigateAnyFramePostCommit(rvh, details, params);
1518}
1519
1520void TabContents::UpdateState(RenderViewHost* rvh,
1521 int32 page_id,
1522 const std::string& state) {
[email protected]992db4c2011-05-12 15:37:151523 // Ensure that this state update comes from either the active RVH or one of
1524 // the swapped out RVHs. We don't expect to hear from any other RVHs.
1525 DCHECK(rvh == render_view_host() || render_manager_.IsSwappedOut(rvh));
[email protected]420ae012009-04-24 05:16:321526
1527 // We must be prepared to handle state updates for any page, these occur
1528 // when the user is scrolling and entering form data, as well as when we're
1529 // leaving a page, in which case our state may have already been moved to
1530 // the next page. The navigation controller will look up the appropriate
1531 // NavigationEntry and update it when it is notified via the delegate.
1532
1533 int entry_index = controller_.GetEntryIndexWithPageID(
[email protected]992db4c2011-05-12 15:37:151534 rvh->site_instance(), page_id);
[email protected]420ae012009-04-24 05:16:321535 if (entry_index < 0)
1536 return;
1537 NavigationEntry* entry = controller_.GetEntryAtIndex(entry_index);
1538
1539 if (state == entry->content_state())
1540 return; // Nothing to update.
1541 entry->set_content_state(state);
1542 controller_.NotifyEntryChanged(entry, entry_index);
1543}
1544
1545void TabContents::UpdateTitle(RenderViewHost* rvh,
[email protected]acafd272011-07-26 17:35:571546 int32 page_id,
[email protected]a49e10b2011-08-01 23:57:461547 const string16& title,
1548 base::i18n::TextDirection title_direction) {
[email protected]420ae012009-04-24 05:16:321549 // If we have a title, that's a pretty good indication that we've started
1550 // getting useful data.
1551 SetNotWaitingForResponse();
1552
1553 DCHECK(rvh == render_view_host());
[email protected]371cc8b2010-09-01 16:03:481554 NavigationEntry* entry = controller_.GetEntryWithPageID(rvh->site_instance(),
[email protected]ec0b6c42010-08-26 03:16:581555 page_id);
[email protected]987fc3a2011-05-26 14:18:091556
[email protected]a49e10b2011-08-01 23:57:461557 // TODO(evan): make use of title_direction.
1558 // http://code.google.com/p/chromium/issues/detail?id=27094
[email protected]987fc3a2011-05-26 14:18:091559 if (!UpdateTitleForEntry(entry, title))
[email protected]420ae012009-04-24 05:16:321560 return;
1561
1562 // Broadcast notifications when the UI should be updated.
1563 if (entry == controller_.GetEntryAtOffset(0))
[email protected]f1cd5e82009-10-23 17:19:031564 NotifyNavigationStateChanged(INVALIDATE_TITLE);
[email protected]420ae012009-04-24 05:16:321565}
1566
[email protected]420ae012009-04-24 05:16:321567void TabContents::UpdateEncoding(RenderViewHost* render_view_host,
[email protected]41fc0322009-09-04 22:23:401568 const std::string& encoding) {
[email protected]420ae012009-04-24 05:16:321569 set_encoding(encoding);
1570}
1571
1572void TabContents::UpdateTargetURL(int32 page_id, const GURL& url) {
1573 if (delegate())
[email protected]d952a052011-09-06 18:42:451574 delegate()->UpdateTargetURL(this, page_id, url);
[email protected]420ae012009-04-24 05:16:321575}
1576
[email protected]420ae012009-04-24 05:16:321577void TabContents::Close(RenderViewHost* rvh) {
[email protected]07707302009-11-06 00:50:291578 // The UI may be in an event-tracking loop, such as between the
1579 // mouse-down and mouse-up in text selection or a button click.
1580 // Defer the close until after tracking is complete, so that we
1581 // don't free objects out from under the UI.
1582 // TODO(shess): This could probably be integrated with the
1583 // IsDoingDrag() test below. Punting for now because I need more
1584 // research to understand how this impacts platforms other than Mac.
1585 // TODO(shess): This could get more fine-grained. For instance,
1586 // closing a tab in another window while selecting text in the
1587 // current window's Omnibox should be just fine.
1588 if (view()->IsEventTracking()) {
1589 view()->CloseTabAfterEventTracking();
1590 return;
1591 }
1592
[email protected]24a4d1062009-07-10 23:10:421593 // If we close the tab while we're in the middle of a drag, we'll crash.
1594 // Instead, cancel the drag and close it as soon as the drag ends.
1595 if (view()->IsDoingDrag()) {
1596 view()->CancelDragAndCloseTab();
1597 return;
1598 }
1599
[email protected]420ae012009-04-24 05:16:321600 // Ignore this if it comes from a RenderViewHost that we aren't showing.
1601 if (delegate() && rvh == render_view_host())
1602 delegate()->CloseContents(this);
1603}
1604
1605void TabContents::RequestMove(const gfx::Rect& new_bounds) {
[email protected]b35b26b32011-05-05 20:35:141606 if (delegate() && delegate()->IsPopupOrPanel(this))
[email protected]420ae012009-04-24 05:16:321607 delegate()->MoveContents(this, new_bounds);
1608}
1609
[email protected]7ab1e7d62009-10-14 23:32:011610void TabContents::DidStartLoading() {
[email protected]420ae012009-04-24 05:16:321611 SetIsLoading(true, NULL);
[email protected]3c9e1872010-11-18 16:17:491612
[email protected]c40d6232011-03-25 00:16:211613 if (delegate() && content_restrictions_) {
1614 content_restrictions_ = 0;
[email protected]de4761b2010-12-07 21:05:211615 delegate()->ContentRestrictionsChanged(this);
[email protected]9e823662010-10-13 23:36:001616 }
[email protected]3c9e1872010-11-18 16:17:491617
1618 // Notify observers about navigation.
[email protected]585b30362011-01-28 02:30:171619 FOR_EACH_OBSERVER(TabContentsObserver, observers_, DidStartLoading());
[email protected]420ae012009-04-24 05:16:321620}
1621
[email protected]7ab1e7d62009-10-14 23:32:011622void TabContents::DidStopLoading() {
[email protected]420ae012009-04-24 05:16:321623 scoped_ptr<LoadNotificationDetails> details;
1624
1625 NavigationEntry* entry = controller_.GetActiveEntry();
1626 // An entry may not exist for a stop when loading an initial blank page or
1627 // if an iframe injected by script into a blank page finishes loading.
1628 if (entry) {
[email protected]420ae012009-04-24 05:16:321629 base::TimeDelta elapsed = base::TimeTicks::Now() - current_load_start_;
1630
1631 details.reset(new LoadNotificationDetails(
[email protected]ebe89e062009-08-13 23:16:541632 entry->virtual_url(),
[email protected]420ae012009-04-24 05:16:321633 entry->transition_type(),
1634 elapsed,
1635 &controller_,
1636 controller_.GetCurrentEntryIndex()));
1637 }
1638
[email protected]420ae012009-04-24 05:16:321639 SetIsLoading(false, details.get());
[email protected]3c9e1872010-11-18 16:17:491640
1641 // Notify observers about navigation.
[email protected]585b30362011-01-28 02:30:171642 FOR_EACH_OBSERVER(TabContentsObserver, observers_, DidStopLoading());
[email protected]420ae012009-04-24 05:16:321643}
1644
[email protected]c95fa8b2011-04-28 20:26:161645void TabContents::DidCancelLoading() {
1646 controller_.DiscardNonCommittedEntries();
1647
1648 // Update the URL display.
1649 NotifyNavigationStateChanged(TabContents::INVALIDATE_URL);
1650}
1651
[email protected]1a3c3cb2010-12-16 21:03:401652void TabContents::DidChangeLoadProgress(double progress) {
1653 if (delegate())
1654 delegate()->LoadProgressChanged(progress);
1655}
1656
[email protected]25497492010-09-11 15:15:081657void TabContents::DocumentOnLoadCompletedInMainFrame(
1658 RenderViewHost* render_view_host,
1659 int32 page_id) {
1660 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271661 content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME,
[email protected]25497492010-09-11 15:15:081662 Source<TabContents>(this),
1663 Details<int>(&page_id));
1664}
1665
[email protected]ae5184d62011-10-06 19:25:581666void TabContents::RequestOpenURL(const GURL& url,
1667 const GURL& referrer,
1668 WindowOpenDisposition disposition,
1669 int64 source_frame_id) {
1670 TabContents* new_contents = NULL;
1671 PageTransition::Type transition_type = PageTransition::LINK;
[email protected]7ade2732011-02-10 00:13:581672 if (render_manager_.web_ui()) {
[email protected]e0112912011-02-02 22:54:351673 // When we're a Web UI, it will provide a page transition type for us (this
[email protected]420ae012009-04-24 05:16:321674 // is so the new tab page can specify AUTO_BOOKMARK for automatically
1675 // generated suggestions).
1676 //
[email protected]e0112912011-02-02 22:54:351677 // Note also that we hide the referrer for Web UI pages. We don't really
[email protected]60e448982009-05-06 04:21:161678 // want web sites to see a referrer of "chrome://blah" (and some
1679 // chrome: URLs might have search terms or other stuff we don't want to
[email protected]420ae012009-04-24 05:16:321680 // send to the site), so we send no referrer.
[email protected]ae5184d62011-10-06 19:25:581681 new_contents = OpenURL(url, GURL(), disposition,
[email protected]7ade2732011-02-10 00:13:581682 render_manager_.web_ui()->link_transition_type());
[email protected]ae5184d62011-10-06 19:25:581683 transition_type = render_manager_.web_ui()->link_transition_type();
[email protected]420ae012009-04-24 05:16:321684 } else {
[email protected]ae5184d62011-10-06 19:25:581685 new_contents = OpenURL(url, referrer, disposition, PageTransition::LINK);
1686 }
1687 if (new_contents) {
1688 // Notify observers.
1689 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
1690 DidOpenRequestedURL(new_contents,
1691 url,
1692 referrer,
1693 disposition,
1694 transition_type,
1695 source_frame_id));
[email protected]420ae012009-04-24 05:16:321696 }
1697}
1698
[email protected]420ae012009-04-24 05:16:321699void TabContents::RunJavaScriptMessage(
[email protected]992db4c2011-05-12 15:37:151700 const RenderViewHost* rvh,
[email protected]4f5ce842011-05-27 19:34:411701 const string16& message,
1702 const string16& default_prompt,
[email protected]420ae012009-04-24 05:16:321703 const GURL& frame_url,
1704 const int flags,
1705 IPC::Message* reply_msg,
1706 bool* did_suppress_message) {
[email protected]3ab9cb82011-06-03 18:02:071707 // Suppress JavaScript dialogs when requested. Also suppress messages when
1708 // showing an interstitial as it's shown over the previous page and we don't
1709 // want the hidden page's dialogs to interfere with the interstitial.
[email protected]8f55e802010-12-06 18:11:501710 bool suppress_this_message =
[email protected]992db4c2011-05-12 15:37:151711 rvh->is_swapped_out() ||
[email protected]8f55e802010-12-06 18:11:501712 showing_interstitial_page() ||
[email protected]3ab9cb82011-06-03 18:02:071713 !delegate_ ||
1714 delegate_->ShouldSuppressDialogs();
[email protected]420ae012009-04-24 05:16:321715
1716 if (!suppress_this_message) {
[email protected]b627d9a2011-06-28 14:06:341717 content::JavaScriptDialogCreator::TitleType title_type;
1718 string16 title;
1719
1720 if (!frame_url.has_host()) {
1721 title_type = content::JavaScriptDialogCreator::DIALOG_TITLE_NONE;
1722 } else {
1723 title_type = content::JavaScriptDialogCreator::DIALOG_TITLE_FORMATTED_URL;
1724 title = net::FormatUrl(
1725 frame_url.GetOrigin(),
1726 content::GetContentClient()->browser()->GetAcceptLangs(this));
1727 }
1728
[email protected]2e5b90c2011-08-16 21:11:551729 dialog_creator_ = delegate_->GetJavaScriptDialogCreator();
1730 dialog_creator_->RunJavaScriptDialog(this,
1731 title_type,
1732 title,
1733 flags,
1734 message,
1735 default_prompt,
1736 reply_msg,
1737 &suppress_this_message);
[email protected]420ae012009-04-24 05:16:321738 }
[email protected]3ab9cb82011-06-03 18:02:071739
1740 if (suppress_this_message) {
1741 // If we are suppressing messages, just reply as if the user immediately
1742 // pressed "Cancel".
1743 OnDialogClosed(reply_msg, false, string16());
1744 }
1745
1746 *did_suppress_message = suppress_this_message;
[email protected]420ae012009-04-24 05:16:321747}
1748
[email protected]992db4c2011-05-12 15:37:151749void TabContents::RunBeforeUnloadConfirm(const RenderViewHost* rvh,
[email protected]4f5ce842011-05-27 19:34:411750 const string16& message,
[email protected]420ae012009-04-24 05:16:321751 IPC::Message* reply_msg) {
[email protected]5aab5e22010-12-08 22:13:291752 if (delegate())
1753 delegate()->WillRunBeforeUnloadConfirm();
[email protected]3ab9cb82011-06-03 18:02:071754
1755 bool suppress_this_message =
1756 rvh->is_swapped_out() ||
1757 !delegate_ ||
1758 delegate_->ShouldSuppressDialogs();
[email protected]992db4c2011-05-12 15:37:151759 if (suppress_this_message) {
[email protected]4f5ce842011-05-27 19:34:411760 render_view_host()->JavaScriptDialogClosed(reply_msg, true, string16());
[email protected]8f55e802010-12-06 18:11:501761 return;
1762 }
[email protected]3ab9cb82011-06-03 18:02:071763
[email protected]7ab1e7d62009-10-14 23:32:011764 is_showing_before_unload_dialog_ = true;
[email protected]2e5b90c2011-08-16 21:11:551765 dialog_creator_ = delegate_->GetJavaScriptDialogCreator();
1766 dialog_creator_->RunBeforeUnloadDialog(this,
1767 message,
1768 reply_msg);
[email protected]420ae012009-04-24 05:16:321769}
1770
[email protected]420ae012009-04-24 05:16:321771WebPreferences TabContents::GetWebkitPrefs() {
[email protected]447021c2010-09-08 21:29:081772 WebPreferences web_prefs =
[email protected]181a95ee2011-07-12 19:26:361773 content::GetContentClient()->browser()->GetWebkitPrefs(
[email protected]3d7474ff2011-07-27 17:47:371774 render_view_host()->process()->browser_context(), false);
[email protected]447021c2010-09-08 21:29:081775
[email protected]b8299c12011-06-03 19:52:281776 // Force accelerated compositing and 2d canvas off for chrome:, about: and
[email protected]2ef216b2011-08-01 19:25:211777 // chrome-devtools: pages (unless it's specifically allowed).
1778 if ((GetURL().SchemeIs(chrome::kChromeDevToolsScheme) ||
[email protected]30b8e0ee2011-09-12 14:45:571779#if !defined(TOUCH_UI)
1780 // Allow accelerated compositing for keyboard and log in screen.
[email protected]b8299c12011-06-03 19:52:281781 GetURL().SchemeIs(chrome::kChromeUIScheme) ||
[email protected]30b8e0ee2011-09-12 14:45:571782#endif
[email protected]cd4a5bd22011-09-13 06:46:301783 (GetURL().SchemeIs(chrome::kAboutScheme) &&
1784 GetURL().spec() != chrome::kAboutBlankURL)) &&
[email protected]2ef216b2011-08-01 19:25:211785 !web_prefs.allow_webui_compositing) {
[email protected]447021c2010-09-08 21:29:081786 web_prefs.accelerated_compositing_enabled = false;
[email protected]9beff752010-09-22 19:35:431787 web_prefs.accelerated_2d_canvas_enabled = false;
[email protected]447021c2010-09-08 21:29:081788 }
1789
[email protected]2f7830b32011-08-26 18:22:251790#if defined(TOUCH_UI)
1791 web_prefs.force_compositing_mode = true;
1792#endif
1793
[email protected]447021c2010-09-08 21:29:081794 return web_prefs;
[email protected]420ae012009-04-24 05:16:321795}
1796
[email protected]7d472472011-01-22 01:30:251797void TabContents::OnUserGesture() {
[email protected]ddb85052011-05-18 14:40:271798 // Notify observers.
1799 FOR_EACH_OBSERVER(TabContentsObserver, observers_, DidGetUserGesture());
[email protected]686493142011-07-15 21:47:221800
1801 ResourceDispatcherHost* rdh =
1802 content::GetContentClient()->browser()->GetResourceDispatcherHost();
1803 if (rdh) // NULL in unittests.
[email protected]8ec98cd22011-08-09 22:31:301804 rdh->OnUserGesture(this);
[email protected]7d472472011-01-22 01:30:251805}
1806
[email protected]fa1cf0b82010-01-15 21:49:441807void TabContents::OnIgnoredUIEvent() {
[email protected]31145092011-09-30 01:49:441808 // Notify observers.
1809 FOR_EACH_OBSERVER(TabContentsObserver, observers_, DidGetIgnoredUIEvent());
[email protected]fa1cf0b82010-01-15 21:49:441810}
1811
[email protected]420ae012009-04-24 05:16:321812void TabContents::RendererUnresponsive(RenderViewHost* rvh,
1813 bool is_during_unload) {
[email protected]992db4c2011-05-12 15:37:151814 // Don't show hung renderer dialog for a swapped out RVH.
1815 if (rvh != render_view_host())
1816 return;
1817
[email protected]e5fc1632011-08-08 07:51:531818 // Ignore renderer unresponsive event if debugger is attached to the tab
1819 // since the event may be a result of the renderer sitting on a breakpoint.
1820 // See http://crbug.com/65458
1821 DevToolsManager* devtools_manager = DevToolsManager::GetInstance();
1822 if (devtools_manager &&
1823 devtools_manager->GetDevToolsClientHostFor(rvh) != NULL)
1824 return;
1825
[email protected]420ae012009-04-24 05:16:321826 if (is_during_unload) {
1827 // Hang occurred while firing the beforeunload/unload handler.
1828 // Pretend the handler fired so tab closing continues as if it had.
[email protected]829e7612009-04-25 01:15:111829 rvh->set_sudden_termination_allowed(true);
[email protected]420ae012009-04-24 05:16:321830
1831 if (!render_manager_.ShouldCloseTabOnUnresponsiveRenderer())
1832 return;
1833
1834 // If the tab hangs in the beforeunload/unload handler there's really
1835 // nothing we can do to recover. Pretend the unload listeners have
1836 // all fired and close the tab. If the hang is in the beforeunload handler
1837 // then the user will not have the option of cancelling the close.
1838 Close(rvh);
1839 return;
1840 }
1841
[email protected]55452902011-06-01 21:57:471842 if (!render_view_host() || !render_view_host()->IsRenderViewLive())
1843 return;
1844
1845 if (delegate())
1846 delegate()->RendererUnresponsive(this);
[email protected]420ae012009-04-24 05:16:321847}
1848
1849void TabContents::RendererResponsive(RenderViewHost* render_view_host) {
[email protected]55452902011-06-01 21:57:471850 if (delegate())
1851 delegate()->RendererResponsive(this);
[email protected]420ae012009-04-24 05:16:321852}
1853
1854void TabContents::LoadStateChanged(const GURL& url,
[email protected]9c235f042011-08-10 22:28:211855 const net::LoadStateWithParam& load_state,
[email protected]094e5b22009-09-25 04:23:561856 uint64 upload_position,
1857 uint64 upload_size) {
[email protected]420ae012009-04-24 05:16:321858 load_state_ = load_state;
[email protected]094e5b22009-09-25 04:23:561859 upload_position_ = upload_position;
1860 upload_size_ = upload_size;
[email protected]04866c42011-05-03 20:03:501861 load_state_host_ = net::IDNToUnicode(url.host(),
[email protected]b5cca982011-05-26 04:42:081862 content::GetContentClient()->browser()->GetAcceptLangs(this));
[email protected]9c235f042011-08-10 22:28:211863 if (load_state_.state == net::LOAD_STATE_READING_RESPONSE)
[email protected]420ae012009-04-24 05:16:321864 SetNotWaitingForResponse();
[email protected]f5d978c2011-07-21 14:43:511865 if (IsLoading())
[email protected]c9cd2222009-05-06 05:16:501866 NotifyNavigationStateChanged(INVALIDATE_LOAD | INVALIDATE_TAB);
[email protected]420ae012009-04-24 05:16:321867}
1868
[email protected]7d472472011-01-22 01:30:251869void TabContents::WorkerCrashed() {
[email protected]0dd3a0ab2011-02-18 08:17:441870 if (delegate())
[email protected]55452902011-06-01 21:57:471871 delegate()->WorkerCrashed(this);
[email protected]7d472472011-01-22 01:30:251872}
1873
[email protected]420ae012009-04-24 05:16:321874void TabContents::BeforeUnloadFiredFromRenderManager(
1875 bool proceed,
1876 bool* proceed_to_fire_unload) {
1877 if (delegate())
1878 delegate()->BeforeUnloadFired(this, proceed, proceed_to_fire_unload);
1879}
1880
[email protected]3a3d47472010-07-15 21:03:541881void TabContents::DidStartLoadingFromRenderManager(
1882 RenderViewHost* render_view_host) {
1883 DidStartLoading();
1884}
1885
1886void TabContents::RenderViewGoneFromRenderManager(
1887 RenderViewHost* render_view_host) {
[email protected]443b80e2010-12-14 00:42:231888 DCHECK(crashed_status_ != base::TERMINATION_STATUS_STILL_RUNNING);
1889 RenderViewGone(render_view_host, crashed_status_, crashed_error_code_);
[email protected]3a3d47472010-07-15 21:03:541890}
1891
[email protected]420ae012009-04-24 05:16:321892void TabContents::UpdateRenderViewSizeForRenderManager() {
1893 // TODO(brettw) this is a hack. See TabContentsView::SizeContents.
[email protected]0323ee42010-02-17 22:03:261894 gfx::Size size = view_->GetContainerSize();
1895 // 0x0 isn't a valid window size (minimal window size is 1x1) but it may be
1896 // here during container initialization and normal window size will be set
1897 // later. In case of tab duplication this resizing to 0x0 prevents setting
1898 // normal size later so just ignore it.
1899 if (!size.IsEmpty())
1900 view_->SizeContents(size);
[email protected]420ae012009-04-24 05:16:321901}
1902
[email protected]3a3d47472010-07-15 21:03:541903void TabContents::NotifySwappedFromRenderManager() {
1904 NotifySwapped();
1905}
1906
1907NavigationController& TabContents::GetControllerForRenderManager() {
1908 return controller();
1909}
1910
[email protected]d0980792011-02-13 19:41:401911WebUI* TabContents::CreateWebUIForRenderManager(const GURL& url) {
[email protected]1fd1a502011-03-30 16:55:561912 return content::WebUIFactory::Get()->CreateWebUIForURL(this, url);
[email protected]420ae012009-04-24 05:16:321913}
1914
1915NavigationEntry*
1916TabContents::GetLastCommittedNavigationEntryForRenderManager() {
1917 return controller_.GetLastCommittedEntry();
1918}
1919
1920bool TabContents::CreateRenderViewForRenderManager(
1921 RenderViewHost* render_view_host) {
[email protected]420ae012009-04-24 05:16:321922 RenderWidgetHostView* rwh_view = view_->CreateViewForWidget(render_view_host);
[email protected]1a98a932009-11-17 00:12:521923
[email protected]420ae012009-04-24 05:16:321924 // Now that the RenderView has been created, we need to tell it its size.
1925 rwh_view->SetSize(view_->GetContainerSize());
1926
[email protected]a4127722011-04-27 23:13:521927 if (!render_view_host->CreateRenderView(string16()))
1928 return false;
1929
[email protected]f8233cc2011-05-31 20:24:501930#if defined(OS_LINUX)
1931 // Force a ViewMsg_Resize to be sent, needed to make plugins show up on
1932 // linux. See crbug.com/83941.
1933 if (RenderWidgetHost* render_widget_host = rwh_view->GetRenderWidgetHost())
1934 render_widget_host->WasResized();
1935#endif
1936
[email protected]420ae012009-04-24 05:16:321937 UpdateMaxPageIDIfNecessary(render_view_host->site_instance(),
1938 render_view_host);
1939 return true;
1940}
1941
[email protected]3ab9cb82011-06-03 18:02:071942void TabContents::OnDialogClosed(IPC::Message* reply_msg,
1943 bool success,
1944 const string16& user_input) {
[email protected]beb440c2009-11-06 04:08:541945 if (is_showing_before_unload_dialog_ && !success) {
1946 // If a beforeunload dialog is canceled, we need to stop the throbber from
1947 // spinning, since we forced it to start spinning in Navigate.
1948 DidStopLoading();
1949
1950 tab_close_start_time_ = base::TimeTicks();
1951 }
1952 is_showing_before_unload_dialog_ = false;
[email protected]4f5ce842011-05-27 19:34:411953 render_view_host()->JavaScriptDialogClosed(reply_msg,
1954 success,
[email protected]3ab9cb82011-06-03 18:02:071955 user_input);
[email protected]beb440c2009-11-06 04:08:541956}
1957
[email protected]3ab9cb82011-06-03 18:02:071958gfx::NativeWindow TabContents::GetDialogRootWindow() {
1959 return view_->GetTopLevelNativeWindow();
[email protected]beb440c2009-11-06 04:08:541960}
1961
[email protected]a1e97f02011-06-30 14:04:341962void TabContents::OnDialogShown() {
1963 Activate();
[email protected]3a3d47472010-07-15 21:03:541964}
1965
[email protected]41fc0322009-09-04 22:23:401966void TabContents::set_encoding(const std::string& encoding) {
[email protected]763ec4ca2011-04-29 15:48:121967 encoding_ = content::GetContentClient()->browser()->
1968 GetCanonicalEncodingNameByAliasName(encoding);
[email protected]aacd7cc2009-09-02 14:51:101969}
[email protected]f45d2a72010-03-08 23:28:351970
[email protected]33f74972010-12-08 16:40:361971void TabContents::CreateViewAndSetSizeForRVH(RenderViewHost* rvh) {
1972 RenderWidgetHostView* rwh_view = view()->CreateViewForWidget(rvh);
1973 rwh_view->SetSize(view()->GetContainerSize());
1974}