blob: 21350104467a07555f48576a1848825f3b59ddfe [file] [log] [blame]
[email protected]61d68ef12011-01-13 14:02:561// Copyright (c) 2011 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
[email protected]0dd3a0ab2011-02-18 08:17:445#include "content/browser/tab_contents/tab_contents.h"
[email protected]39526562011-02-05 03:41:516
[email protected]b75b8292010-10-01 07:28:257#include <cmath>
8
[email protected]36fb2c7c2011-04-04 15:49:089#include "base/command_line.h"
[email protected]835d7c82010-10-14 04:38:3810#include "base/metrics/histogram.h"
[email protected]724159a2010-12-30 01:11:1811#include "base/metrics/stats_counters.h"
[email protected]96d185d2009-04-24 03:28:5412#include "base/string16.h"
[email protected]996fd702009-09-04 19:12:3713#include "base/string_util.h"
[email protected]96d185d2009-04-24 03:28:5414#include "base/time.h"
[email protected]3c733bde2010-12-21 19:56:3115#include "base/utf_string_conversions.h"
[email protected]3d7474ff2011-07-27 17:47:3716#include "content/browser/browser_context.h"
[email protected]567812d2011-02-24 17:40:5017#include "content/browser/child_process_security_policy.h"
[email protected]1fd1a502011-03-30 16:55:5618#include "content/browser/content_browser_client.h"
[email protected]b46442d7e2011-06-29 02:16:0619#include "content/browser/debugger/devtools_manager.h"
[email protected]71bf3f5e2011-08-15 21:05:2220#include "content/browser/download/download_manager.h"
[email protected]8bd9e562011-08-16 23:55:4621#include "content/browser/download/download_stats.h"
[email protected]567812d2011-02-24 17:40:5022#include "content/browser/host_zoom_map.h"
23#include "content/browser/in_process_webkit/session_storage_namespace.h"
[email protected]37a72af2011-06-13 05:42:0124#include "content/browser/load_from_memory_cache_details.h"
[email protected]35e251d2011-05-24 21:01:0425#include "content/browser/load_notification_details.h"
[email protected]567812d2011-02-24 17:40:5026#include "content/browser/renderer_host/render_process_host.h"
27#include "content/browser/renderer_host/render_view_host.h"
28#include "content/browser/renderer_host/render_widget_host_view.h"
[email protected]686493142011-07-15 21:47:2229#include "content/browser/renderer_host/resource_dispatcher_host.h"
[email protected]567812d2011-02-24 17:40:5030#include "content/browser/renderer_host/resource_request_details.h"
31#include "content/browser/site_instance.h"
[email protected]0dd3a0ab2011-02-18 08:17:4432#include "content/browser/tab_contents/interstitial_page.h"
[email protected]8286f51a2011-05-31 17:39:1333#include "content/browser/tab_contents/navigation_details.h"
[email protected]0dd3a0ab2011-02-18 08:17:4434#include "content/browser/tab_contents/navigation_entry.h"
35#include "content/browser/tab_contents/provisional_load_details.h"
36#include "content/browser/tab_contents/tab_contents_delegate.h"
37#include "content/browser/tab_contents/tab_contents_observer.h"
38#include "content/browser/tab_contents/tab_contents_view.h"
[email protected]105bb0f2011-05-24 17:12:1439#include "content/browser/tab_contents/title_updated_details.h"
[email protected]afd1e522011-04-27 23:29:5940#include "content/browser/user_metrics.h"
[email protected]1fd1a502011-03-30 16:55:5641#include "content/browser/webui/web_ui_factory.h"
[email protected]9966325b2011-04-18 05:00:1042#include "content/common/bindings_policy.h"
[email protected]1fd1a502011-03-30 16:55:5643#include "content/common/content_client.h"
[email protected]763ec4ca2011-04-29 15:48:1244#include "content/common/content_restriction.h"
[email protected]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]d686e812009-06-03 19:10:2950#include "net/base/net_util.h"
[email protected]abe2c032011-03-31 18:49:3451#include "net/url_request/url_request_context_getter.h"
[email protected]8bd0fe62011-01-17 06:44:3752#include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h"
[email protected]08397d52011-02-05 01:53:3853#include "ui/gfx/codec/png_codec.h"
[email protected]d5e311b2010-07-20 17:15:0354#include "webkit/glue/password_form.h"
[email protected]ce9751942011-09-21 01:57:2455#include "webkit/glue/web_intent_data.h"
[email protected]3c733bde2010-12-21 19:56:3156#include "webkit/glue/webpreferences.h"
57
58#if defined(OS_MACOSX)
[email protected]b9b751f22011-03-25 14:04:1259#include "ui/gfx/surface/io_surface_support_mac.h"
[email protected]3c733bde2010-12-21 19:56:3160#endif // defined(OS_MACOSX)
[email protected]3e45ba92009-02-20 21:09:0061
[email protected]420ae012009-04-24 05:16:3262// Cross-Site Navigations
63//
64// If a TabContents is told to navigate to a different web site (as determined
65// by SiteInstance), it will replace its current RenderViewHost with a new
66// RenderViewHost dedicated to the new SiteInstance. This works as follows:
67//
68// - Navigate determines whether the destination is cross-site, and if so,
[email protected]a2750082011-09-01 12:29:4669// it creates a pending_render_view_host_.
[email protected]420ae012009-04-24 05:16:3270// - The pending RVH is "suspended," so that no navigation messages are sent to
71// its renderer until the onbeforeunload JavaScript handler has a chance to
72// run in the current RVH.
73// - The pending RVH tells CrossSiteRequestManager (a thread-safe singleton)
74// that it has a pending cross-site request. ResourceDispatcherHost will
75// check for this when the response arrives.
76// - The current RVH runs its onbeforeunload handler. If it returns false, we
[email protected]a2750082011-09-01 12:29:4677// cancel all the pending logic. Otherwise we allow the pending RVH to send
78// the navigation request to its renderer.
79// - ResourceDispatcherHost receives a ResourceRequest on the IO thread for the
80// main resource load on the pending RVH. It checks CrossSiteRequestManager
81// to see that it is a cross-site request, and installs a
82// CrossSiteResourceHandler.
83// - When RDH receives a response, the BufferedResourceHandler determines
84// whether it is a download. If so, it sends a message to the new renderer
85// causing it to cancel the request, and the download proceeds. For now, the
86// pending RVH remains until the next DidNavigate event for this TabContents.
87// This isn't ideal, but it doesn't affect any functionality.
[email protected]420ae012009-04-24 05:16:3288// - After RDH receives a response and determines that it is safe and not a
89// download, it pauses the response to first run the old page's onunload
90// handler. It does this by asynchronously calling the OnCrossSiteResponse
[email protected]992db4c2011-05-12 15:37:1591// method of TabContents on the UI thread, which sends a SwapOut message
[email protected]420ae012009-04-24 05:16:3292// to the current RVH.
[email protected]992db4c2011-05-12 15:37:1593// - Once the onunload handler is finished, a SwapOut_ACK message is sent to
[email protected]420ae012009-04-24 05:16:3294// the ResourceDispatcherHost, who unpauses the response. Data is then sent
95// to the pending RVH.
96// - The pending renderer sends a FrameNavigate message that invokes the
97// DidNavigate method. This replaces the current RVH with the
[email protected]a2750082011-09-01 12:29:4698// pending RVH.
[email protected]992db4c2011-05-12 15:37:1599// - The previous renderer is kept swapped out in RenderViewHostManager in case
100// the user goes back. The process only stays live if another tab is using
101// it, but if so, the existing frame relationships will be maintained.
[email protected]420ae012009-04-24 05:16:32102
103namespace {
104
105// Amount of time we wait between when a key event is received and the renderer
106// is queried for its state and pushed to the NavigationEntry.
107const int kQueryStateDelay = 5000;
108
[email protected]6ebdc9b2010-09-27 16:55:57109const int kSyncWaitDelay = 40;
110
[email protected]ca406032011-07-19 21:53:05111static const char kDotGoogleDotCom[] = ".google.com";
112
[email protected]420ae012009-04-24 05:16:32113#if defined(OS_WIN)
114
115BOOL CALLBACK InvalidateWindow(HWND hwnd, LPARAM lparam) {
116 // Note: erase is required to properly paint some widgets borders. This can
117 // be seen with textfields.
118 InvalidateRect(hwnd, NULL, TRUE);
119 return TRUE;
120}
121#endif
122
[email protected]2c5569662011-03-22 20:45:02123ViewMsg_Navigate_Type::Value GetNavigationType(
[email protected]3d7474ff2011-07-27 17:47:37124 content::BrowserContext* browser_context, const NavigationEntry& entry,
[email protected]1ccb3568d2010-02-19 10:51:16125 NavigationController::ReloadType reload_type) {
126 switch (reload_type) {
127 case NavigationController::RELOAD:
[email protected]2c5569662011-03-22 20:45:02128 return ViewMsg_Navigate_Type::RELOAD;
[email protected]1ccb3568d2010-02-19 10:51:16129 case NavigationController::RELOAD_IGNORING_CACHE:
[email protected]2c5569662011-03-22 20:45:02130 return ViewMsg_Navigate_Type::RELOAD_IGNORING_CACHE;
[email protected]1ccb3568d2010-02-19 10:51:16131 case NavigationController::NO_RELOAD:
132 break; // Fall through to rest of function.
133 }
[email protected]5e369672009-11-03 23:48:30134
135 if (entry.restore_type() == NavigationEntry::RESTORE_LAST_SESSION &&
[email protected]3d7474ff2011-07-27 17:47:37136 browser_context->DidLastSessionExitCleanly())
[email protected]2c5569662011-03-22 20:45:02137 return ViewMsg_Navigate_Type::RESTORE;
[email protected]5e369672009-11-03 23:48:30138
[email protected]2c5569662011-03-22 20:45:02139 return ViewMsg_Navigate_Type::NORMAL;
[email protected]5e369672009-11-03 23:48:30140}
141
[email protected]876bc832010-09-07 16:29:54142void MakeNavigateParams(const NavigationEntry& entry,
143 const NavigationController& controller,
[email protected]6c6b02d2011-09-02 03:36:47144 TabContentsDelegate* delegate,
[email protected]1ccb3568d2010-02-19 10:51:16145 NavigationController::ReloadType reload_type,
146 ViewMsg_Navigate_Params* params) {
[email protected]056de2d2009-06-26 16:41:34147 params->page_id = entry.page_id();
[email protected]876bc832010-09-07 16:29:54148 params->pending_history_list_offset = controller.GetIndexOfEntry(&entry);
[email protected]3cc72b12010-03-18 23:03:00149 params->current_history_list_offset = controller.last_committed_entry_index();
150 params->current_history_list_length = controller.entry_count();
[email protected]056de2d2009-06-26 16:41:34151 params->url = entry.url();
152 params->referrer = entry.referrer();
153 params->transition = entry.transition_type();
154 params->state = entry.content_state();
[email protected]3cc72b12010-03-18 23:03:00155 params->navigation_type =
[email protected]3d7474ff2011-07-27 17:47:37156 GetNavigationType(controller.browser_context(), entry, reload_type);
[email protected]056de2d2009-06-26 16:41:34157 params->request_time = base::Time::Now();
[email protected]6c6b02d2011-09-02 03:36:47158 params->extra_headers = entry.extra_headers();
159
160 if (delegate)
161 delegate->AddNavigationHeaders(params->url, &params->extra_headers);
[email protected]056de2d2009-06-26 16:41:34162}
163
[email protected]420ae012009-04-24 05:16:32164} // namespace
165
[email protected]f4f50ef2011-01-21 19:01:19166
167// TabContents ----------------------------------------------------------------
[email protected]420ae012009-04-24 05:16:32168
[email protected]3d7474ff2011-07-27 17:47:37169TabContents::TabContents(content::BrowserContext* browser_context,
[email protected]420ae012009-04-24 05:16:32170 SiteInstance* site_instance,
171 int routing_id,
[email protected]6ee12c42010-09-14 09:36:07172 const TabContents* base_tab_contents,
173 SessionStorageNamespace* session_storage_namespace)
[email protected]b680ad22009-04-15 23:19:42174 : delegate_(NULL),
[email protected]6ee12c42010-09-14 09:36:07175 ALLOW_THIS_IN_INITIALIZER_LIST(controller_(
[email protected]3d7474ff2011-07-27 17:47:37176 this, browser_context, session_storage_namespace)),
[email protected]66ba4932009-06-04 19:22:13177 ALLOW_THIS_IN_INITIALIZER_LIST(view_(
[email protected]74313b42011-08-24 16:51:32178 content::GetContentClient()->browser()->CreateTabContentsView(this))),
[email protected]d82ed61e2009-06-16 02:46:22179 ALLOW_THIS_IN_INITIALIZER_LIST(render_manager_(this, this)),
[email protected]d5f942ba2008-09-26 19:30:34180 is_loading_(false),
[email protected]443b80e2010-12-14 00:42:23181 crashed_status_(base::TERMINATION_STATUS_STILL_RUNNING),
182 crashed_error_code_(0),
[email protected]d5f942ba2008-09-26 19:30:34183 waiting_for_response_(false),
[email protected]d5f942ba2008-09-26 19:30:34184 max_page_id_(-1),
[email protected]9c235f042011-08-10 22:28:21185 load_state_(net::LOAD_STATE_IDLE, string16()),
[email protected]094e5b22009-09-25 04:23:56186 upload_size_(0),
187 upload_position_(0),
[email protected]f17a0ee2010-05-17 17:38:47188 displayed_insecure_content_(false),
[email protected]fdd61c62009-04-22 19:22:57189 capturing_contents_(false),
190 is_being_destroyed_(false),
191 notify_disconnection_(false),
[email protected]2e5b90c2011-08-16 21:11:55192 dialog_creator_(NULL),
[email protected]fdd61c62009-04-22 19:22:57193#if defined(OS_WIN)
194 message_box_active_(CreateEvent(NULL, TRUE, FALSE, NULL)),
195#endif
[email protected]7ab1e7d62009-10-14 23:32:01196 is_showing_before_unload_dialog_(false),
[email protected]1fd1a502011-03-30 16:55:56197 opener_web_ui_type_(WebUI::kNoWebUI),
[email protected]ebf40a72010-07-22 01:46:38198 closed_by_user_gesture_(false),
[email protected]b75b8292010-10-01 07:28:25199 minimum_zoom_percent_(
200 static_cast<int>(WebKit::WebView::minTextSizeMultiplier * 100)),
201 maximum_zoom_percent_(
202 static_cast<int>(WebKit::WebView::maxTextSizeMultiplier * 100)),
[email protected]9e823662010-10-13 23:36:00203 temporary_zoom_settings_(false),
[email protected]4850a7f2011-03-08 23:36:59204 content_restrictions_(0) {
[email protected]7ff431e2010-01-07 18:18:54205
[email protected]3d7474ff2011-07-27 17:47:37206 render_manager_.Init(browser_context, site_instance, routing_id);
[email protected]420ae012009-04-24 05:16:32207
[email protected]34ac70502009-09-25 17:07:23208 // We have the initial size of the view be based on the size of the passed in
209 // tab contents (normally a tab from the same window).
210 view_->CreateView(base_tab_contents ?
211 base_tab_contents->view()->GetContainerSize() : gfx::Size());
[email protected]332af7732009-03-11 13:21:35212}
initial.commit09911bf2008-07-26 23:55:29213
214TabContents::~TabContents() {
[email protected]420ae012009-04-24 05:16:32215 is_being_destroyed_ = true;
216
[email protected]3ab9cb82011-06-03 18:02:07217 // Clear out any JavaScript state.
[email protected]2e5b90c2011-08-16 21:11:55218 if (dialog_creator_)
219 dialog_creator_->ResetJavaScriptState(this);
[email protected]3ab9cb82011-06-03 18:02:07220
[email protected]420ae012009-04-24 05:16:32221 NotifyDisconnected();
[email protected]420ae012009-04-24 05:16:32222
[email protected]420ae012009-04-24 05:16:32223 // Notify any observer that have a reference on this tab contents.
224 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:27225 content::NOTIFICATION_TAB_CONTENTS_DESTROYED,
[email protected]420ae012009-04-24 05:16:32226 Source<TabContents>(this),
227 NotificationService::NoDetails());
228
229 // TODO(brettw) this should be moved to the view.
230#if defined(OS_WIN)
231 // If we still have a window handle, destroy it. GetNativeView can return
232 // NULL if this contents was part of a window that closed.
[email protected]50bd6452010-11-27 19:39:42233 if (GetNativeView()) {
234 RenderViewHost* host = render_view_host();
[email protected]b9a1fb42011-04-12 23:16:44235 if (host && host->view())
[email protected]50bd6452010-11-27 19:39:42236 host->view()->WillWmDestroy();
[email protected]50bd6452010-11-27 19:39:42237 }
[email protected]420ae012009-04-24 05:16:32238#endif
[email protected]7ab1e7d62009-10-14 23:32:01239
240 // OnCloseStarted isn't called in unit tests.
241 if (!tab_close_start_time_.is_null()) {
242 UMA_HISTOGRAM_TIMES("Tab.Close",
243 base::TimeTicks::Now() - tab_close_start_time_);
244 }
[email protected]b5a1d11c2011-02-17 03:09:42245
[email protected]07d490bc2011-03-07 17:05:26246 FOR_EACH_OBSERVER(TabContentsObserver, observers_, TabContentsDestroyed());
[email protected]232a5812011-03-04 22:42:08247
[email protected]1de2b8b2011-06-29 19:38:46248 set_delegate(NULL);
249}
250
251// TODO(cbentzel): Either remove the debugging code, or rename to SetDelegate.
252void TabContents::set_delegate(TabContentsDelegate* delegate) {
253 if (delegate == delegate_)
254 return;
255 if (delegate_)
256 delegate_->Detach(this);
257 delegate_ = delegate;
258 if (delegate_)
259 delegate_->Attach(this);
[email protected]b5a1d11c2011-02-17 03:09:42260}
261
[email protected]724159a2010-12-30 01:11:18262bool TabContents::OnMessageReceived(const IPC::Message& message) {
[email protected]f82d57b52011-04-27 19:13:17263 if (web_ui() && web_ui()->OnMessageReceived(message))
264 return true;
265
[email protected]585b30362011-01-28 02:30:17266 ObserverListBase<TabContentsObserver>::Iterator it(observers_);
267 TabContentsObserver* observer;
[email protected]0f180ee2011-01-21 18:23:10268 while ((observer = it.GetNext()) != NULL)
269 if (observer->OnMessageReceived(message))
[email protected]403415a2011-01-10 18:57:53270 return true;
[email protected]403415a2011-01-10 18:57:53271
[email protected]724159a2010-12-30 01:11:18272 bool handled = true;
273 bool message_is_ok = true;
274 IPC_BEGIN_MESSAGE_MAP_EX(TabContents, message, message_is_ok)
[email protected]ce9751942011-09-21 01:57:24275 IPC_MESSAGE_HANDLER(IntentsHostMsg_WebIntentDispatch,
276 OnWebIntentDispatch)
[email protected]724159a2010-12-30 01:11:18277 IPC_MESSAGE_HANDLER(ViewHostMsg_DidStartProvisionalLoadForFrame,
278 OnDidStartProvisionalLoadForFrame)
279 IPC_MESSAGE_HANDLER(ViewHostMsg_DidRedirectProvisionalLoad,
280 OnDidRedirectProvisionalLoad)
281 IPC_MESSAGE_HANDLER(ViewHostMsg_DidFailProvisionalLoadWithError,
282 OnDidFailProvisionalLoadWithError)
283 IPC_MESSAGE_HANDLER(ViewHostMsg_DidLoadResourceFromMemoryCache,
284 OnDidLoadResourceFromMemoryCache)
285 IPC_MESSAGE_HANDLER(ViewHostMsg_DidDisplayInsecureContent,
286 OnDidDisplayInsecureContent)
287 IPC_MESSAGE_HANDLER(ViewHostMsg_DidRunInsecureContent,
288 OnDidRunInsecureContent)
289 IPC_MESSAGE_HANDLER(ViewHostMsg_DocumentLoadedInFrame,
290 OnDocumentLoadedInFrame)
291 IPC_MESSAGE_HANDLER(ViewHostMsg_DidFinishLoad, OnDidFinishLoad)
[email protected]c8f73ab2011-01-22 00:05:17292 IPC_MESSAGE_HANDLER(ViewHostMsg_UpdateContentRestrictions,
293 OnUpdateContentRestrictions)
[email protected]7d472472011-01-22 01:30:25294 IPC_MESSAGE_HANDLER(ViewHostMsg_GoToEntryAtOffset, OnGoToEntryAtOffset)
[email protected]216813952011-05-19 22:21:26295 IPC_MESSAGE_HANDLER(ViewHostMsg_UpdateZoomLimits, OnUpdateZoomLimits)
296 IPC_MESSAGE_HANDLER(ViewHostMsg_FocusedNodeChanged, OnFocusedNodeChanged)
[email protected]c7dd2f62011-07-18 15:57:59297 IPC_MESSAGE_HANDLER(ViewHostMsg_SaveURLAs, OnSaveURL)
[email protected]3a29a6e2011-08-24 18:26:21298 IPC_MESSAGE_HANDLER(ViewHostMsg_EnumerateDirectory, OnEnumerateDirectory)
[email protected]7d189022011-08-25 22:54:20299 IPC_MESSAGE_HANDLER(ViewHostMsg_JSOutOfMemory, OnJSOutOfMemory)
300 IPC_MESSAGE_HANDLER(ViewHostMsg_RegisterProtocolHandler,
301 OnRegisterProtocolHandler)
302 IPC_MESSAGE_HANDLER(ViewHostMsg_RegisterIntentHandler,
303 OnRegisterIntentHandler)
[email protected]b888919c2011-09-02 00:32:16304 IPC_MESSAGE_HANDLER(ViewHostMsg_Find_Reply, OnFindReply)
[email protected]d952a052011-09-06 18:42:45305 IPC_MESSAGE_HANDLER(ViewHostMsg_CrashedPlugin, OnCrashedPlugin)
[email protected]7fc4bbb2011-09-08 21:23:10306 IPC_MESSAGE_HANDLER(ViewHostMsg_AppCacheAccessed, OnAppCacheAccessed)
[email protected]724159a2010-12-30 01:11:18307 IPC_MESSAGE_UNHANDLED(handled = false)
308 IPC_END_MESSAGE_MAP_EX()
309
310 if (!message_is_ok) {
311 UserMetrics::RecordAction(UserMetricsAction("BadMessageTerminate_RVD"));
312 GetRenderProcessHost()->ReceivedBadMessage();
313 }
314
315 return handled;
316}
317
[email protected]6c2e472f2011-08-24 23:26:18318void TabContents::RunFileChooser(
319 RenderViewHost* render_view_host,
320 const ViewHostMsg_RunFileChooser_Params& params) {
321 delegate()->RunFileChooser(this, params);
322}
323
[email protected]8cb5d5b2010-02-09 11:36:16324RenderProcessHost* TabContents::GetRenderProcessHost() const {
325 return render_manager_.current_host()->process();
326}
327
[email protected]d5f942ba2008-09-26 19:30:34328const GURL& TabContents::GetURL() const {
329 // We may not have a navigation entry yet
[email protected]ce3fa3c2009-04-20 19:55:57330 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]ebe89e062009-08-13 23:16:54331 return entry ? entry->virtual_url() : GURL::EmptyGURL();
[email protected]d5f942ba2008-09-26 19:30:34332}
333
[email protected]96d185d2009-04-24 03:28:54334const string16& TabContents::GetTitle() const {
[email protected]4c6092c5b2009-06-06 00:23:55335 // Transient entries take precedence. They are used for interstitial pages
336 // that are shown on top of existing pages.
337 NavigationEntry* entry = controller_.GetTransientEntry();
[email protected]b5cca982011-05-26 04:42:08338 std::string accept_languages =
339 content::GetContentClient()->browser()->GetAcceptLangs(this);
[email protected]45d0ef7f2011-01-05 13:46:23340 if (entry) {
[email protected]b5cca982011-05-26 04:42:08341 return entry->GetTitleForDisplay(accept_languages);
[email protected]45d0ef7f2011-01-05 13:46:23342 }
[email protected]7ade2732011-02-10 00:13:58343 WebUI* our_web_ui = render_manager_.pending_web_ui() ?
344 render_manager_.pending_web_ui() : render_manager_.web_ui();
345 if (our_web_ui) {
[email protected]96d185d2009-04-24 03:28:54346 // Don't override the title in view source mode.
[email protected]4c6092c5b2009-06-06 00:23:55347 entry = controller_.GetActiveEntry();
[email protected]96d185d2009-04-24 03:28:54348 if (!(entry && entry->IsViewSourceMode())) {
[email protected]e0112912011-02-02 22:54:35349 // Give the Web UI the chance to override our title.
[email protected]7ade2732011-02-10 00:13:58350 const string16& title = our_web_ui->overridden_title();
[email protected]96d185d2009-04-24 03:28:54351 if (!title.empty())
352 return title;
353 }
354 }
355
356 // We use the title for the last committed entry rather than a pending
357 // navigation entry. For example, when the user types in a URL, we want to
358 // keep the old page's title until the new load has committed and we get a new
359 // title.
[email protected]96d185d2009-04-24 03:28:54360 entry = controller_.GetLastCommittedEntry();
[email protected]45d0ef7f2011-01-05 13:46:23361 if (entry) {
[email protected]b5cca982011-05-26 04:42:08362 return entry->GetTitleForDisplay(accept_languages);
[email protected]45d0ef7f2011-01-05 13:46:23363 }
[email protected]987fc3a2011-05-26 14:18:09364
365 // |page_title_when_no_navigation_entry_| is finally used
366 // if no title cannot be retrieved.
367 return page_title_when_no_navigation_entry_;
[email protected]96d185d2009-04-24 03:28:54368}
369
[email protected]d5f942ba2008-09-26 19:30:34370int32 TabContents::GetMaxPageID() {
371 if (GetSiteInstance())
372 return GetSiteInstance()->max_page_id();
373 else
374 return max_page_id_;
375}
376
377void TabContents::UpdateMaxPageID(int32 page_id) {
378 // Ensure both the SiteInstance and RenderProcessHost update their max page
[email protected]57c6a652009-05-04 07:58:34379 // IDs in sync. Only TabContents will also have site instances, except during
[email protected]d5f942ba2008-09-26 19:30:34380 // testing.
381 if (GetSiteInstance())
382 GetSiteInstance()->UpdateMaxPageID(page_id);
[email protected]8cb5d5b2010-02-09 11:36:16383 GetRenderProcessHost()->UpdateMaxPageID(page_id);
[email protected]d5f942ba2008-09-26 19:30:34384}
385
[email protected]96d185d2009-04-24 03:28:54386SiteInstance* TabContents::GetSiteInstance() const {
387 return render_manager_.current_host()->site_instance();
388}
389
[email protected]77362eb2011-08-01 17:18:38390SiteInstance* TabContents::GetPendingSiteInstance() const {
391 RenderViewHost* dest_rvh = render_manager_.pending_render_view_host() ?
392 render_manager_.pending_render_view_host() :
393 render_manager_.current_host();
394 return dest_rvh->site_instance();
395}
396
[email protected]96d185d2009-04-24 03:28:54397bool TabContents::ShouldDisplayURL() {
[email protected]8ab24cc2009-11-13 20:56:15398 // Don't hide the url in view source mode and with interstitials.
[email protected]96d185d2009-04-24 03:28:54399 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]8ab24cc2009-11-13 20:56:15400 if (entry && (entry->IsViewSourceMode() ||
[email protected]cccd3762010-11-12 18:40:01401 entry->page_type() == INTERSTITIAL_PAGE)) {
[email protected]96d185d2009-04-24 03:28:54402 return true;
[email protected]8ab24cc2009-11-13 20:56:15403 }
[email protected]83e3895b2009-06-11 00:07:16404
[email protected]2b9a06402011-02-16 04:57:10405 // We always display the URL for non-WebUI URLs to prevent spoofing.
[email protected]1fd1a502011-03-30 16:55:56406 if (entry && !content::WebUIFactory::Get()->HasWebUIScheme(entry->url()))
[email protected]2b9a06402011-02-16 04:57:10407 return true;
408
[email protected]d0980792011-02-13 19:41:40409 WebUI* web_ui = GetWebUIForCurrentState();
[email protected]7ade2732011-02-10 00:13:58410 if (web_ui)
411 return !web_ui->should_hide_url();
[email protected]96d185d2009-04-24 03:28:54412 return true;
413}
414
[email protected]f5d978c2011-07-21 14:43:51415bool TabContents::IsLoading() const {
416 return is_loading_ || (web_ui() && web_ui()->IsLoading());
417}
418
[email protected]585b30362011-01-28 02:30:17419void TabContents::AddObserver(TabContentsObserver* observer) {
420 observers_.AddObserver(observer);
[email protected]3c9e1872010-11-18 16:17:49421}
422
[email protected]585b30362011-01-28 02:30:17423void TabContents::RemoveObserver(TabContentsObserver* observer) {
424 observers_.RemoveObserver(observer);
[email protected]3c9e1872010-11-18 16:17:49425}
426
[email protected]443b80e2010-12-14 00:42:23427void TabContents::SetIsCrashed(base::TerminationStatus status, int error_code) {
428 if (status == crashed_status_)
[email protected]d5f942ba2008-09-26 19:30:34429 return;
430
[email protected]443b80e2010-12-14 00:42:23431 crashed_status_ = status;
432 crashed_error_code_ = error_code;
[email protected]c9cd2222009-05-06 05:16:50433 NotifyNavigationStateChanged(INVALIDATE_TAB);
[email protected]d5f942ba2008-09-26 19:30:34434}
435
436void TabContents::NotifyNavigationStateChanged(unsigned changed_flags) {
437 if (delegate_)
438 delegate_->NavigationStateChanged(this, changed_flags);
439}
440
[email protected]96d185d2009-04-24 03:28:54441void TabContents::DidBecomeSelected() {
442 controller_.SetActive(true);
[email protected]8cb5d5b2010-02-09 11:36:16443 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
444 if (rwhv) {
445 rwhv->DidBecomeSelected();
[email protected]789e9152009-08-04 21:59:43446#if defined(OS_MACOSX)
[email protected]8cb5d5b2010-02-09 11:36:16447 rwhv->SetActive(true);
[email protected]789e9152009-08-04 21:59:43448#endif
449 }
[email protected]96d185d2009-04-24 03:28:54450
[email protected]5ac20162010-11-24 23:33:11451 last_selected_time_ = base::TimeTicks::Now();
[email protected]3e69bc82011-05-26 23:22:38452
453 FOR_EACH_OBSERVER(TabContentsObserver, observers_, DidBecomeSelected());
[email protected]96d185d2009-04-24 03:28:54454}
455
456void TabContents::WasHidden() {
457 if (!capturing_contents()) {
458 // |render_view_host()| can be NULL if the user middle clicks a link to open
459 // a tab in then background, then closes the tab before selecting it. This
460 // is because closing the tab calls TabContents::Destroy(), which removes
461 // the |render_view_host()|; then when we actually destroy the window,
462 // OnWindowPosChanged() notices and calls HideContents() (which calls us).
[email protected]8cb5d5b2010-02-09 11:36:16463 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
464 if (rwhv)
465 rwhv->WasHidden();
[email protected]96d185d2009-04-24 03:28:54466 }
467
468 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:27469 content::NOTIFICATION_TAB_CONTENTS_HIDDEN,
[email protected]96d185d2009-04-24 03:28:54470 Source<TabContents>(this),
471 NotificationService::NoDetails());
472}
473
[email protected]d5f942ba2008-09-26 19:30:34474void TabContents::Activate() {
475 if (delegate_)
476 delegate_->ActivateContents(this);
477}
478
[email protected]ea42e7782010-08-23 23:58:12479void TabContents::Deactivate() {
480 if (delegate_)
481 delegate_->DeactivateContents(this);
482}
483
[email protected]63954792011-07-11 04:17:48484void TabContents::LostCapture() {
485 if (delegate_)
486 delegate_->LostCapture();
487}
488
489bool TabContents::PreHandleKeyboardEvent(const NativeWebKeyboardEvent& event,
490 bool* is_keyboard_shortcut) {
491 return delegate_ &&
492 delegate_->PreHandleKeyboardEvent(event, is_keyboard_shortcut);
493}
494
495void TabContents::HandleKeyboardEvent(const NativeWebKeyboardEvent& event) {
496 if (delegate_)
497 delegate_->HandleKeyboardEvent(event);
498}
499
500void TabContents::HandleMouseUp() {
501 if (delegate_)
502 delegate_->HandleMouseUp();
503}
504
505void TabContents::HandleMouseActivate() {
506 if (delegate_)
507 delegate_->HandleMouseActivate();
508}
509
[email protected]8a5e0ca2011-08-25 06:30:47510void TabContents::ToggleFullscreenMode(bool enter_fullscreen) {
511 if (delegate_)
512 delegate_->ToggleFullscreenModeForTab(this, enter_fullscreen);
513}
514
[email protected]5d5f7af2011-10-01 01:38:12515bool TabContents::IsFullscreenForCurrentTab() const {
516 return delegate_ ? delegate_->IsFullscreenForTab(this) : false;
517}
518
[email protected]0548c5352011-09-07 00:33:33519void TabContents::UpdatePreferredSize(const gfx::Size& pref_size) {
520 if (delegate_)
521 delegate_->UpdatePreferredSize(this, pref_size);
522}
523
[email protected]96d185d2009-04-24 03:28:54524void TabContents::ShowContents() {
[email protected]8cb5d5b2010-02-09 11:36:16525 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
526 if (rwhv)
527 rwhv->DidBecomeSelected();
[email protected]96d185d2009-04-24 03:28:54528}
529
530void TabContents::HideContents() {
531 // TODO(pkasting): http://b/1239839 Right now we purposefully don't call
532 // our superclass HideContents(), because some callers want to be very picky
533 // about the order in which these get called. In addition to making the code
534 // here practically impossible to understand, this also means we end up
535 // calling TabContents::WasHidden() twice if callers call both versions of
[email protected]57c6a652009-05-04 07:58:34536 // HideContents() on a TabContents.
[email protected]96d185d2009-04-24 03:28:54537 WasHidden();
538}
539
[email protected]5aab5e22010-12-08 22:13:29540bool TabContents::NeedToFireBeforeUnload() {
541 // TODO(creis): Should we fire even for interstitial pages?
542 return notify_disconnection() &&
543 !showing_interstitial_page() &&
544 !render_view_host()->SuddenTerminationAllowed();
545}
546
[email protected]00c37fc2011-08-02 00:22:50547// TODO(adriansc): Remove this method once refactoring changed all call sites.
[email protected]992e4542011-07-20 23:09:25548TabContents* TabContents::OpenURL(const GURL& url,
549 const GURL& referrer,
550 WindowOpenDisposition disposition,
551 PageTransition::Type transition) {
[email protected]00c37fc2011-08-02 00:22:50552 return OpenURL(OpenURLParams(url, referrer, disposition, transition));
553}
554
555TabContents* TabContents::OpenURL(const OpenURLParams& params) {
[email protected]1c642b52011-04-15 09:05:49556 if (delegate_) {
[email protected]00c37fc2011-08-02 00:22:50557 TabContents* new_contents = delegate_->OpenURLFromTab(this, params);
[email protected]1c642b52011-04-15 09:05:49558 // Notify observers.
559 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
[email protected]00c37fc2011-08-02 00:22:50560 DidOpenURL(params.url, params.referrer,
561 params.disposition, params.transition));
[email protected]992e4542011-07-20 23:09:25562 return new_contents;
[email protected]1c642b52011-04-15 09:05:49563 }
[email protected]992e4542011-07-20 23:09:25564 return NULL;
[email protected]d5f942ba2008-09-26 19:30:34565}
566
[email protected]1ccb3568d2010-02-19 10:51:16567bool TabContents::NavigateToPendingEntry(
568 NavigationController::ReloadType reload_type) {
[email protected]876bc832010-09-07 16:29:54569 return NavigateToEntry(*controller_.pending_entry(), reload_type);
570}
[email protected]96d185d2009-04-24 03:28:54571
[email protected]876bc832010-09-07 16:29:54572bool TabContents::NavigateToEntry(
573 const NavigationEntry& entry,
574 NavigationController::ReloadType reload_type) {
[email protected]96d185d2009-04-24 03:28:54575 RenderViewHost* dest_render_view_host = render_manager_.Navigate(entry);
576 if (!dest_render_view_host)
577 return false; // Unable to create the desired render view host.
578
[email protected]80a8fad2011-01-29 04:02:38579 // For security, we should never send non-Web-UI URLs to a Web UI renderer.
[email protected]05fdd492010-11-15 17:52:07580 // Double check that here.
[email protected]54ec6472010-04-09 19:39:58581 int enabled_bindings = dest_render_view_host->enabled_bindings();
[email protected]1fd1a502011-03-30 16:55:56582 bool is_allowed_in_web_ui_renderer = content::GetContentClient()->
[email protected]3d7474ff2011-07-27 17:47:37583 browser()->GetWebUIFactory()->IsURLAcceptableForWebUI(browser_context(),
[email protected]1fd1a502011-03-30 16:55:56584 entry.url());
[email protected]c09163a2011-02-15 00:05:55585 CHECK(!BindingsPolicy::is_web_ui_enabled(enabled_bindings) ||
586 is_allowed_in_web_ui_renderer);
[email protected]54ec6472010-04-09 19:39:58587
[email protected]96d185d2009-04-24 03:28:54588 // Tell DevTools agent that it is attached prior to the navigation.
[email protected]d9f9b792009-06-24 13:17:12589 DevToolsManager* devtools_manager = DevToolsManager::GetInstance();
[email protected]7e8e6b62009-05-08 11:28:32590 if (devtools_manager) { // NULL in unit tests.
[email protected]3cc72b12010-03-18 23:03:00591 devtools_manager->OnNavigatingToPendingEntry(render_view_host(),
592 dest_render_view_host,
593 entry.url());
[email protected]7e8e6b62009-05-08 11:28:32594 }
[email protected]96d185d2009-04-24 03:28:54595
596 // Used for page load time metrics.
597 current_load_start_ = base::TimeTicks::Now();
598
599 // Navigate in the desired RenderViewHost.
[email protected]056de2d2009-06-26 16:41:34600 ViewMsg_Navigate_Params navigate_params;
[email protected]6c6b02d2011-09-02 03:36:47601 MakeNavigateParams(entry, controller_, delegate_, reload_type,
602 &navigate_params);
[email protected]056de2d2009-06-26 16:41:34603 dest_render_view_host->Navigate(navigate_params);
[email protected]96d185d2009-04-24 03:28:54604
605 if (entry.page_id() == -1) {
606 // HACK!! This code suppresses javascript: URLs from being added to
607 // session history, which is what we want to do for javascript: URLs that
608 // do not generate content. What we really need is a message from the
609 // renderer telling us that a new page was not created. The same message
610 // could be used for mailto: URLs and the like.
611 if (entry.url().SchemeIs(chrome::kJavaScriptScheme))
612 return false;
613 }
614
[email protected]3c9e1872010-11-18 16:17:49615 // Notify observers about navigation.
[email protected]b375c5d2011-05-03 21:15:04616 FOR_EACH_OBSERVER(TabContentsObserver,
617 observers_,
618 NavigateToPendingEntry(entry.url(), reload_type));
[email protected]96d185d2009-04-24 03:28:54619
[email protected]09b29342011-06-24 19:18:48620 if (delegate_)
621 delegate_->DidNavigateToPendingEntry(this);
622
[email protected]d5f942ba2008-09-26 19:30:34623 return true;
624}
625
[email protected]9e1ad4b2011-08-14 16:49:19626void TabContents::SetHistoryLengthAndPrune(const SiteInstance* site_instance,
627 int history_length,
628 int32 minimum_page_id) {
[email protected]5229253b2011-08-16 14:36:41629 // SetHistoryLengthAndPrune doesn't work when there are pending cross-site
630 // navigations. Callers should ensure that this is the case.
[email protected]9e1ad4b2011-08-14 16:49:19631 if (render_manager_.pending_render_view_host()) {
632 NOTREACHED();
[email protected]796931a92011-08-10 01:32:14633 return;
[email protected]9e1ad4b2011-08-14 16:49:19634 }
635 RenderViewHost* rvh = render_view_host();
636 if (!rvh) {
637 NOTREACHED();
638 return;
639 }
640 if (site_instance && rvh->site_instance() != site_instance) {
641 NOTREACHED();
642 return;
643 }
644 rvh->Send(new ViewMsg_SetHistoryLengthAndPrune(rvh->routing_id(),
645 history_length,
646 minimum_page_id));
[email protected]796931a92011-08-10 01:32:14647}
648
[email protected]96d185d2009-04-24 03:28:54649void TabContents::Stop() {
650 render_manager_.Stop();
[email protected]2e3f4572011-03-25 19:24:47651 FOR_EACH_OBSERVER(TabContentsObserver, observers_, StopNavigation());
[email protected]96d185d2009-04-24 03:28:54652}
653
[email protected]96d185d2009-04-24 03:28:54654TabContents* TabContents::Clone() {
655 // We create a new SiteInstance so that the new tab won't share processes
656 // with the old one. This can be changed in the future if we need it to share
657 // processes for some reason.
[email protected]3d7474ff2011-07-27 17:47:37658 TabContents* tc = new TabContents(
659 browser_context(),
660 SiteInstance::CreateSiteInstance(browser_context()),
661 MSG_ROUTING_NONE, this, NULL);
[email protected]96d185d2009-04-24 03:28:54662 tc->controller().CopyStateFrom(controller_);
663 return tc;
664}
665
[email protected]4d677202009-07-19 07:37:12666void TabContents::ShowPageInfo(const GURL& url,
667 const NavigationEntry::SSLStatus& ssl,
668 bool show_history) {
669 if (!delegate_)
670 return;
671
[email protected]3d7474ff2011-07-27 17:47:37672 delegate_->ShowPageInfo(browser_context(), url, ssl, show_history);
[email protected]4d677202009-07-19 07:37:12673}
674
[email protected]e7cfdbd2011-04-22 14:41:37675void TabContents::AddNewContents(TabContents* new_contents,
676 WindowOpenDisposition disposition,
677 const gfx::Rect& initial_pos,
678 bool user_gesture) {
initial.commit09911bf2008-07-26 23:55:29679 if (!delegate_)
680 return;
681
[email protected]e7cfdbd2011-04-22 14:41:37682 delegate_->AddNewContents(this, new_contents, disposition, initial_pos,
683 user_gesture);
[email protected]96d185d2009-04-24 03:28:54684}
685
[email protected]5c9e97a2009-09-09 23:48:30686gfx::NativeView TabContents::GetContentNativeView() const {
[email protected]96d185d2009-04-24 03:28:54687 return view_->GetContentNativeView();
688}
689
690gfx::NativeView TabContents::GetNativeView() const {
691 return view_->GetNativeView();
692}
693
694void TabContents::GetContainerBounds(gfx::Rect *out) const {
695 view_->GetContainerBounds(out);
696}
697
698void TabContents::Focus() {
699 view_->Focus();
700}
701
[email protected]90daadb42009-06-08 21:27:28702void TabContents::FocusThroughTabTraversal(bool reverse) {
[email protected]7e383692009-06-12 19:14:54703 if (showing_interstitial_page()) {
704 render_manager_.interstitial_page()->FocusThroughTabTraversal(reverse);
705 return;
706 }
[email protected]96d185d2009-04-24 03:28:54707 render_view_host()->SetInitialFocus(reverse);
708}
709
710bool TabContents::FocusLocationBarByDefault() {
[email protected]d0980792011-02-13 19:41:40711 WebUI* web_ui = GetWebUIForCurrentState();
[email protected]7ade2732011-02-10 00:13:58712 if (web_ui)
713 return web_ui->focus_location_bar_by_default();
[email protected]3e3f0eb2009-06-22 18:33:43714 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]e0d481582009-09-15 21:06:25715 if (entry && entry->url() == GURL(chrome::kAboutBlankURL))
[email protected]3e3f0eb2009-06-22 18:33:43716 return true;
[email protected]96d185d2009-04-24 03:28:54717 return false;
718}
719
[email protected]a26dc362010-04-23 01:48:58720void TabContents::SetFocusToLocationBar(bool select_all) {
[email protected]a11aaf22010-03-30 00:03:38721 if (delegate())
[email protected]a26dc362010-04-23 01:48:58722 delegate()->SetFocusToLocationBar(select_all);
[email protected]a11aaf22010-03-30 00:03:38723}
724
[email protected]686493142011-07-15 21:47:22725bool TabContents::CanDownload(int request_id) {
726 TabContentsDelegate* d = delegate();
727 if (d)
728 return d->CanDownload(this, request_id);
729 return true;
730}
731
732void TabContents::OnStartDownload(DownloadItem* download) {
733 TabContentsDelegate* d = delegate();
734 if (d)
735 d->OnStartDownload(this, download);
736}
737
[email protected]c7dd2f62011-07-18 15:57:59738void TabContents::OnSavePage() {
739 // If we can not save the page, try to download it.
740 if (!SavePackage::IsSavableContents(contents_mime_type())) {
[email protected]3d7474ff2011-07-27 17:47:37741 DownloadManager* dlm = browser_context()->GetDownloadManager();
[email protected]c7dd2f62011-07-18 15:57:59742 const GURL& current_page_url = GetURL();
743 if (dlm && current_page_url.is_valid()) {
744 dlm->DownloadUrl(current_page_url, GURL(), "", this);
[email protected]8bd9e562011-08-16 23:55:46745 download_stats::RecordDownloadCount(
746 download_stats::INITIATED_BY_SAVE_PACKAGE_FAILURE_COUNT);
[email protected]c7dd2f62011-07-18 15:57:59747 return;
748 }
749 }
750
751 Stop();
752
753 // Create the save package and possibly prompt the user for the name to save
754 // the page as. The user prompt is an asynchronous operation that runs on
755 // another thread.
756 save_package_ = new SavePackage(this);
757 save_package_->GetSaveInfo();
758}
759
760// Used in automated testing to bypass prompting the user for file names.
761// Instead, the names and paths are hard coded rather than running them through
762// file name sanitation and extension / mime checking.
763bool TabContents::SavePage(const FilePath& main_file, const FilePath& dir_path,
764 SavePackage::SavePackageType save_type) {
765 // Stop the page from navigating.
766 Stop();
767
768 save_package_ = new SavePackage(this, save_type, main_file, dir_path);
769 return save_package_->Init();
770}
771
772void TabContents::OnSaveURL(const GURL& url) {
[email protected]3d7474ff2011-07-27 17:47:37773 DownloadManager* dlm = browser_context()->GetDownloadManager();
[email protected]c7dd2f62011-07-18 15:57:59774 dlm->DownloadUrl(url, GetURL(), "", this);
775}
776
[email protected]420ae012009-04-24 05:16:32777bool TabContents::IsActiveEntry(int32 page_id) {
778 NavigationEntry* active_entry = controller_.GetActiveEntry();
779 return (active_entry != NULL &&
780 active_entry->site_instance() == GetSiteInstance() &&
781 active_entry->page_id() == page_id);
782}
783
[email protected]b2fe07d12010-02-09 14:38:08784void TabContents::SetOverrideEncoding(const std::string& encoding) {
[email protected]8cb5d5b2010-02-09 11:36:16785 set_encoding(encoding);
[email protected]216813952011-05-19 22:21:26786 render_view_host()->Send(new ViewMsg_SetPageEncoding(
787 render_view_host()->routing_id(), encoding));
[email protected]8cb5d5b2010-02-09 11:36:16788}
789
[email protected]b2fe07d12010-02-09 14:38:08790void TabContents::ResetOverrideEncoding() {
[email protected]8cb5d5b2010-02-09 11:36:16791 reset_encoding();
[email protected]216813952011-05-19 22:21:26792 render_view_host()->Send(new ViewMsg_ResetPageEncodingToDefault(
793 render_view_host()->routing_id()));
[email protected]8cb5d5b2010-02-09 11:36:16794}
795
[email protected]7ab1e7d62009-10-14 23:32:01796void TabContents::OnCloseStarted() {
797 if (tab_close_start_time_.is_null())
798 tab_close_start_time_ = base::TimeTicks::Now();
799}
800
[email protected]46624bf2010-06-09 16:04:19801bool TabContents::ShouldAcceptDragAndDrop() const {
802#if defined(OS_CHROMEOS)
803 // ChromeOS panels (pop-ups) do not take drag-n-drop.
804 // See http://crosbug.com/2413
[email protected]b35b26b32011-05-05 20:35:14805 if (delegate() && delegate()->IsPopupOrPanel(this))
[email protected]b83af492010-10-09 03:21:20806 return false;
807 return true;
[email protected]46624bf2010-06-09 16:04:19808#else
809 return true;
810#endif
811}
812
[email protected]7813bd72011-02-05 02:19:34813void TabContents::SystemDragEnded() {
814 if (render_view_host())
815 render_view_host()->DragSourceSystemDragEnded();
816 if (delegate())
817 delegate()->DragEnded();
818}
819
[email protected]d0b8d092010-10-25 04:05:17820double TabContents::GetZoomLevel() const {
[email protected]3d7474ff2011-07-27 17:47:37821 HostZoomMap* zoom_map = browser_context()->GetHostZoomMap();
[email protected]b75b8292010-10-01 07:28:25822 if (!zoom_map)
[email protected]d0b8d092010-10-25 04:05:17823 return 0;
[email protected]b75b8292010-10-01 07:28:25824
825 double zoom_level;
826 if (temporary_zoom_settings_) {
827 zoom_level = zoom_map->GetTemporaryZoomLevel(
828 GetRenderProcessHost()->id(), render_view_host()->routing_id());
829 } else {
[email protected]13ffa32e2011-05-27 16:37:01830 zoom_level = zoom_map->GetZoomLevel(net::GetHostOrSpecFromURL(GetURL()));
[email protected]b75b8292010-10-01 07:28:25831 }
[email protected]d0b8d092010-10-25 04:05:17832 return zoom_level;
833}
[email protected]b75b8292010-10-01 07:28:25834
[email protected]d0b8d092010-10-25 04:05:17835int TabContents::GetZoomPercent(bool* enable_increment,
836 bool* enable_decrement) {
837 *enable_decrement = *enable_increment = false;
[email protected]b75b8292010-10-01 07:28:25838 int percent = static_cast<int>(
[email protected]d0b8d092010-10-25 04:05:17839 WebKit::WebView::zoomLevelToZoomFactor(GetZoomLevel()) * 100);
[email protected]b75b8292010-10-01 07:28:25840 *enable_decrement = percent > minimum_zoom_percent_;
841 *enable_increment = percent < maximum_zoom_percent_;
842 return percent;
843}
844
[email protected]3c733bde2010-12-21 19:56:31845void TabContents::ViewSource() {
[email protected]1788e772010-12-15 16:40:50846 if (!delegate_)
847 return;
848
849 NavigationEntry* active_entry = controller().GetActiveEntry();
850 if (!active_entry)
851 return;
852
853 delegate_->ViewSourceForTab(this, active_entry->url());
[email protected]77d8d622010-12-15 10:30:12854}
855
[email protected]932b7a12011-03-09 12:50:27856void TabContents::ViewFrameSource(const GURL& url,
857 const std::string& content_state) {
858 if (!delegate_)
859 return;
860
861 delegate_->ViewSourceForFrame(this, url, content_state);
862}
863
[email protected]c40d6232011-03-25 00:16:21864void TabContents::SetContentRestrictions(int restrictions) {
865 content_restrictions_ = restrictions;
866 delegate()->ContentRestrictionsChanged(this);
867}
868
[email protected]724159a2010-12-30 01:11:18869void TabContents::OnDidStartProvisionalLoadForFrame(int64 frame_id,
870 bool is_main_frame,
[email protected]eacb080b2011-05-22 19:40:26871 bool has_opener_set,
[email protected]724159a2010-12-30 01:11:18872 const GURL& url) {
873 bool is_error_page = (url.spec() == chrome::kUnreachableWebDataURL);
874 GURL validated_url(url);
875 render_view_host()->FilterURL(ChildProcessSecurityPolicy::GetInstance(),
876 GetRenderProcessHost()->id(), &validated_url);
877
[email protected]8093a542011-05-13 07:29:32878 RenderViewHost* rvh =
879 render_manager_.pending_render_view_host() ?
880 render_manager_.pending_render_view_host() : render_view_host();
[email protected]0d60f0192011-04-14 12:40:10881 // Notify observers about the start of the provisional load.
882 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
883 DidStartProvisionalLoadForFrame(frame_id, is_main_frame,
[email protected]8093a542011-05-13 07:29:32884 validated_url, is_error_page, rvh));
[email protected]0d60f0192011-04-14 12:40:10885
[email protected]724159a2010-12-30 01:11:18886 if (is_main_frame) {
[email protected]4850a7f2011-03-08 23:36:59887 // Notify observers about the provisional change in the main frame URL.
888 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
[email protected]eacb080b2011-05-22 19:40:26889 ProvisionalChangeToMainFrameUrl(url, has_opener_set));
[email protected]724159a2010-12-30 01:11:18890 }
891}
892
893void TabContents::OnDidRedirectProvisionalLoad(int32 page_id,
[email protected]eacb080b2011-05-22 19:40:26894 bool has_opener_set,
[email protected]724159a2010-12-30 01:11:18895 const GURL& source_url,
896 const GURL& target_url) {
[email protected]18ba2b12011-04-06 16:35:49897 // TODO(creis): Remove this method and have the pre-rendering code listen to
898 // the ResourceDispatcherHost's RESOURCE_RECEIVED_REDIRECT notification
899 // instead. See http://crbug.com/78512.
[email protected]724159a2010-12-30 01:11:18900 NavigationEntry* entry;
901 if (page_id == -1)
902 entry = controller_.pending_entry();
903 else
904 entry = controller_.GetEntryWithPageID(GetSiteInstance(), page_id);
905 if (!entry || entry->url() != source_url)
906 return;
[email protected]e7d50892011-01-19 21:47:38907
[email protected]4850a7f2011-03-08 23:36:59908 // Notify observers about the provisional change in the main frame URL.
909 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
[email protected]eacb080b2011-05-22 19:40:26910 ProvisionalChangeToMainFrameUrl(target_url,
911 has_opener_set));
[email protected]724159a2010-12-30 01:11:18912}
913
914void TabContents::OnDidFailProvisionalLoadWithError(
915 int64 frame_id,
916 bool is_main_frame,
917 int error_code,
918 const GURL& url,
919 bool showing_repost_interstitial) {
920 VLOG(1) << "Failed Provisional Load: " << url.possibly_invalid_spec()
921 << ", error_code: " << error_code
922 << " is_main_frame: " << is_main_frame
923 << " showing_repost_interstitial: " << showing_repost_interstitial
924 << " frame_id: " << frame_id;
925 GURL validated_url(url);
926 render_view_host()->FilterURL(ChildProcessSecurityPolicy::GetInstance(),
927 GetRenderProcessHost()->id(), &validated_url);
928
929 if (net::ERR_ABORTED == error_code) {
930 // EVIL HACK ALERT! Ignore failed loads when we're showing interstitials.
931 // This means that the interstitial won't be torn down properly, which is
932 // bad. But if we have an interstitial, go back to another tab type, and
933 // then load the same interstitial again, we could end up getting the first
934 // interstitial's "failed" message (as a result of the cancel) when we're on
935 // the second one.
936 //
937 // We can't tell this apart, so we think we're tearing down the current page
938 // which will cause a crash later one. There is also some code in
939 // RenderViewHostManager::RendererAbortedProvisionalLoad that is commented
940 // out because of this problem.
941 //
942 // http://code.google.com/p/chromium/issues/detail?id=2855
943 // Because this will not tear down the interstitial properly, if "back" is
944 // back to another tab type, the interstitial will still be somewhat alive
945 // in the previous tab type. If you navigate somewhere that activates the
946 // tab with the interstitial again, you'll see a flash before the new load
947 // commits of the interstitial page.
948 if (showing_interstitial_page()) {
949 LOG(WARNING) << "Discarding message during interstitial.";
950 return;
951 }
952
[email protected]02102f82011-06-13 20:37:02953 // Discard our pending entry if the load canceled (e.g. if we decided to
954 // download the file instead of load it). We do not verify that the URL
955 // being canceled matches the pending entry's URL because they will not
956 // match if a redirect occurred (in which case we do not want to leave a
957 // stale redirect URL showing). This means that we also cancel the pending
958 // entry if the user started a new navigation. As a result, the navigation
959 // controller may not remember that a load is in progress, but the
960 // navigation will still commit even if there is no pending entry.
[email protected]724159a2010-12-30 01:11:18961 NavigationEntry* pending_entry = controller_.pending_entry();
[email protected]02102f82011-06-13 20:37:02962 if (pending_entry)
[email protected]c95fa8b2011-04-28 20:26:16963 DidCancelLoading();
[email protected]724159a2010-12-30 01:11:18964
965 render_manager_.RendererAbortedProvisionalLoad(render_view_host());
966 }
967
968 // Send out a notification that we failed a provisional load with an error.
969 ProvisionalLoadDetails details(
970 is_main_frame, controller_.IsURLInPageNavigation(validated_url),
[email protected]b9e8ea62011-03-04 06:29:09971 validated_url, std::string(), false, frame_id);
[email protected]724159a2010-12-30 01:11:18972 details.set_error_code(error_code);
973
974 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:27975 content::NOTIFICATION_FAIL_PROVISIONAL_LOAD_WITH_ERROR,
[email protected]724159a2010-12-30 01:11:18976 Source<NavigationController>(&controller_),
977 Details<ProvisionalLoadDetails>(&details));
[email protected]0d60f0192011-04-14 12:40:10978
979 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
980 DidFailProvisionalLoad(frame_id, is_main_frame,
981 validated_url, error_code));
[email protected]724159a2010-12-30 01:11:18982}
983
984void TabContents::OnDidLoadResourceFromMemoryCache(
985 const GURL& url,
[email protected]70435962011-08-02 20:13:28986 const std::string& security_info,
987 const std::string& http_method,
988 ResourceType::Type resource_type) {
[email protected]28620862011-03-22 23:07:19989 base::StatsCounter cache("WebKit.CacheHit");
[email protected]724159a2010-12-30 01:11:18990 cache.Increment();
991
992 // Send out a notification that we loaded a resource from our memory cache.
[email protected]70d66502011-09-23 00:55:08993 int cert_id = 0;
994 net::CertStatus cert_status = 0;
995 int security_bits = -1;
996 int connection_status = 0;
[email protected]724159a2010-12-30 01:11:18997 SSLManager::DeserializeSecurityInfo(security_info,
998 &cert_id, &cert_status,
999 &security_bits,
1000 &connection_status);
[email protected]7a8c55e2011-02-15 08:21:161001 LoadFromMemoryCacheDetails details(url, GetRenderProcessHost()->id(),
1002 cert_id, cert_status);
[email protected]724159a2010-12-30 01:11:181003
1004 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271005 content::NOTIFICATION_LOAD_FROM_MEMORY_CACHE,
[email protected]724159a2010-12-30 01:11:181006 Source<NavigationController>(&controller_),
1007 Details<LoadFromMemoryCacheDetails>(&details));
1008}
1009
1010void TabContents::OnDidDisplayInsecureContent() {
[email protected]221a5d92011-04-28 18:23:161011 UserMetrics::RecordAction(UserMetricsAction("SSL.DisplayedInsecureContent"));
[email protected]724159a2010-12-30 01:11:181012 displayed_insecure_content_ = true;
[email protected]9678d8f2011-08-05 04:05:071013 SSLManager::NotifySSLInternalStateChanged(&controller());
[email protected]724159a2010-12-30 01:11:181014}
1015
1016void TabContents::OnDidRunInsecureContent(
[email protected]92771112011-01-20 00:13:021017 const std::string& security_origin, const GURL& target_url) {
[email protected]9941cf52011-02-08 16:17:231018 LOG(INFO) << security_origin << " ran insecure content from "
1019 << target_url.possibly_invalid_spec();
[email protected]221a5d92011-04-28 18:23:161020 UserMetrics::RecordAction(UserMetricsAction("SSL.RanInsecureContent"));
[email protected]ca406032011-07-19 21:53:051021 if (EndsWith(security_origin, kDotGoogleDotCom, false)) {
1022 UserMetrics::RecordAction(
1023 UserMetricsAction("SSL.RanInsecureContentGoogle"));
1024 }
[email protected]724159a2010-12-30 01:11:181025 controller_.ssl_manager()->DidRunInsecureContent(security_origin);
[email protected]f2b58c4d2011-06-01 23:29:411026 displayed_insecure_content_ = true;
[email protected]9678d8f2011-08-05 04:05:071027 SSLManager::NotifySSLInternalStateChanged(&controller());
[email protected]724159a2010-12-30 01:11:181028}
1029
1030void TabContents::OnDocumentLoadedInFrame(int64 frame_id) {
1031 controller_.DocumentLoadedInFrame();
[email protected]0d60f0192011-04-14 12:40:101032 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
1033 DocumentLoadedInFrame(frame_id));
[email protected]724159a2010-12-30 01:11:181034}
1035
1036void TabContents::OnDidFinishLoad(int64 frame_id) {
[email protected]0d60f0192011-04-14 12:40:101037 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
1038 DidFinishLoad(frame_id));
[email protected]724159a2010-12-30 01:11:181039}
1040
[email protected]c8f73ab2011-01-22 00:05:171041void TabContents::OnUpdateContentRestrictions(int restrictions) {
[email protected]c40d6232011-03-25 00:16:211042 SetContentRestrictions(restrictions);
[email protected]c8f73ab2011-01-22 00:05:171043}
1044
[email protected]216813952011-05-19 22:21:261045void TabContents::OnGoToEntryAtOffset(int offset) {
1046 if (!delegate_ || delegate_->OnGoToEntryOffset(offset)) {
1047 NavigationEntry* entry = controller_.GetEntryAtOffset(offset);
1048 if (!entry)
1049 return;
1050 // Note that we don't call NavigationController::GotToOffset() as we don't
1051 // want to create a pending navigation entry (it might end up lingering
1052 // http://crbug.com/51680).
1053 entry->set_transition_type(entry->transition_type() |
1054 PageTransition::FORWARD_BACK);
1055 NavigateToEntry(*entry, NavigationController::NO_RELOAD);
[email protected]a13cc362011-07-28 21:29:571056
1057 // If the entry is being restored and doesn't have a SiteInstance yet, fill
1058 // it in now that we know. This allows us to find the entry when it commits.
1059 if (!entry->site_instance() &&
1060 entry->restore_type() != NavigationEntry::RESTORE_NONE) {
[email protected]77362eb2011-08-01 17:18:381061 entry->set_site_instance(GetPendingSiteInstance());
[email protected]a13cc362011-07-28 21:29:571062 }
[email protected]216813952011-05-19 22:21:261063 }
1064}
1065
1066void TabContents::OnUpdateZoomLimits(int minimum_percent,
1067 int maximum_percent,
1068 bool remember) {
1069 minimum_zoom_percent_ = minimum_percent;
1070 maximum_zoom_percent_ = maximum_percent;
1071 temporary_zoom_settings_ = !remember;
1072}
1073
1074void TabContents::OnFocusedNodeChanged(bool is_editable_node) {
1075 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271076 content::NOTIFICATION_FOCUS_CHANGED_IN_PAGE,
[email protected]216813952011-05-19 22:21:261077 Source<TabContents>(this),
1078 Details<const bool>(&is_editable_node));
1079}
1080
[email protected]3a29a6e2011-08-24 18:26:211081void TabContents::OnEnumerateDirectory(int request_id,
1082 const FilePath& path) {
1083 delegate()->EnumerateDirectory(this, request_id, path);
1084}
1085
[email protected]7d189022011-08-25 22:54:201086void TabContents::OnJSOutOfMemory() {
1087 delegate()->JSOutOfMemory(this);
1088}
1089
1090void TabContents::OnRegisterProtocolHandler(const std::string& protocol,
1091 const GURL& url,
1092 const string16& title) {
1093 delegate()->RegisterProtocolHandler(this, protocol, url, title);
1094}
1095
1096void TabContents::OnRegisterIntentHandler(const string16& action,
1097 const string16& type,
1098 const string16& href,
1099 const string16& title) {
1100 delegate()->RegisterIntentHandler(this, action, type, href, title);
1101}
1102
1103void TabContents::OnWebIntentDispatch(const IPC::Message& message,
[email protected]ce9751942011-09-21 01:57:241104 const webkit_glue::WebIntentData& intent,
[email protected]7d189022011-08-25 22:54:201105 int intent_id) {
[email protected]ce9751942011-09-21 01:57:241106 delegate()->WebIntentDispatch(this, message.routing_id(), intent, intent_id);
[email protected]7d189022011-08-25 22:54:201107}
1108
[email protected]b888919c2011-09-02 00:32:161109void TabContents::OnFindReply(int request_id,
1110 int number_of_matches,
1111 const gfx::Rect& selection_rect,
1112 int active_match_ordinal,
1113 bool final_update) {
1114 delegate()->FindReply(this, request_id, number_of_matches, selection_rect,
1115 active_match_ordinal, final_update);
[email protected]4e6239c2011-09-23 00:11:001116 // Send a notification to the renderer that we are ready to receive more
1117 // results from the scoping effort of the Find operation. The FindInPage
1118 // scoping is asynchronous and periodically sends results back up to the
1119 // browser using IPC. In an effort to not spam the browser we have the
1120 // browser send an ACK for each FindReply message and have the renderer
1121 // queue up the latest status message while waiting for this ACK.
1122 render_view_host()->Send(
1123 new ViewMsg_FindReplyACK(render_view_host()->routing_id()));
[email protected]b888919c2011-09-02 00:32:161124}
1125
[email protected]d952a052011-09-06 18:42:451126void TabContents::OnCrashedPlugin(const FilePath& plugin_path) {
1127 delegate()->CrashedPlugin(this, plugin_path);
1128}
1129
[email protected]7fc4bbb2011-09-08 21:23:101130void TabContents::OnAppCacheAccessed(const GURL& manifest_url,
1131 bool blocked_by_policy) {
1132 // Notify observers about navigation.
1133 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
1134 AppCacheAccessed(manifest_url, blocked_by_policy));
1135}
1136
[email protected]96d185d2009-04-24 03:28:541137// Notifies the RenderWidgetHost instance about the fact that the page is
1138// loading, or done loading and calls the base implementation.
1139void TabContents::SetIsLoading(bool is_loading,
1140 LoadNotificationDetails* details) {
1141 if (is_loading == is_loading_)
1142 return;
1143
1144 if (!is_loading) {
[email protected]9c235f042011-08-10 22:28:211145 load_state_ = net::LoadStateWithParam(net::LOAD_STATE_IDLE, string16());
[email protected]96d185d2009-04-24 03:28:541146 load_state_host_.clear();
[email protected]094e5b22009-09-25 04:23:561147 upload_size_ = 0;
1148 upload_position_ = 0;
[email protected]96d185d2009-04-24 03:28:541149 }
1150
1151 render_manager_.SetIsLoading(is_loading);
1152
1153 is_loading_ = is_loading;
1154 waiting_for_response_ = is_loading;
1155
[email protected]6ebdc9b2010-09-27 16:55:571156 if (delegate_)
1157 delegate_->LoadingStateChanged(this);
[email protected]329581b2009-04-28 06:52:351158 NotifyNavigationStateChanged(INVALIDATE_LOAD);
[email protected]96d185d2009-04-24 03:28:541159
[email protected]432115822011-07-10 15:52:271160 int type = is_loading ? content::NOTIFICATION_LOAD_START :
1161 content::NOTIFICATION_LOAD_STOP;
[email protected]ce5c4502009-05-06 16:46:111162 NotificationDetails det = NotificationService::NoDetails();
[email protected]96d185d2009-04-24 03:28:541163 if (details)
1164 det = Details<LoadNotificationDetails>(details);
1165 NotificationService::current()->Notify(type,
1166 Source<NavigationController>(&controller_),
1167 det);
1168}
1169
[email protected]d0980792011-02-13 19:41:401170WebUI* TabContents::GetWebUIForCurrentState() {
[email protected]7ade2732011-02-10 00:13:581171 // When there is a pending navigation entry, we want to use the pending WebUI
[email protected]96d185d2009-04-24 03:28:541172 // that goes along with it to control the basic flags. For example, we want to
1173 // show the pending URL in the URL bar, so we want the display_url flag to
1174 // be from the pending entry.
1175 //
1176 // The confusion comes because there are multiple possibilities for the
1177 // initial load in a tab as a side effect of the way the RenderViewHostManager
1178 // works.
1179 //
[email protected]e0112912011-02-02 22:54:351180 // - For the very first tab the load looks "normal". The new tab Web UI is
[email protected]96d185d2009-04-24 03:28:541181 // the pending one, and we want it to apply here.
1182 //
1183 // - For subsequent new tabs, they'll get a new SiteInstance which will then
1184 // get switched to the one previously associated with the new tab pages.
[email protected]7ade2732011-02-10 00:13:581185 // This switching will cause the manager to commit the RVH/WebUI. So we'll
[email protected]e0112912011-02-02 22:54:351186 // have a committed Web UI in this case.
[email protected]96d185d2009-04-24 03:28:541187 //
1188 // This condition handles all of these cases:
1189 //
1190 // - First load in first tab: no committed nav entry + pending nav entry +
1191 // pending dom ui:
[email protected]e0112912011-02-02 22:54:351192 // -> Use pending Web UI if any.
[email protected]96d185d2009-04-24 03:28:541193 //
1194 // - First load in second tab: no committed nav entry + pending nav entry +
[email protected]e0112912011-02-02 22:54:351195 // no pending Web UI:
1196 // -> Use the committed Web UI if any.
[email protected]96d185d2009-04-24 03:28:541197 //
1198 // - Second navigation in any tab: committed nav entry + pending nav entry:
[email protected]e0112912011-02-02 22:54:351199 // -> Use pending Web UI if any.
[email protected]96d185d2009-04-24 03:28:541200 //
1201 // - Normal state with no load: committed nav entry + no pending nav entry:
[email protected]e0112912011-02-02 22:54:351202 // -> Use committed Web UI.
[email protected]96d185d2009-04-24 03:28:541203 if (controller_.pending_entry() &&
1204 (controller_.GetLastCommittedEntry() ||
[email protected]7ade2732011-02-10 00:13:581205 render_manager_.pending_web_ui()))
1206 return render_manager_.pending_web_ui();
1207 return render_manager_.web_ui();
[email protected]96d185d2009-04-24 03:28:541208}
[email protected]420ae012009-04-24 05:16:321209
[email protected]1fd1a502011-03-30 16:55:561210WebUI::TypeID TabContents::GetWebUITypeForCurrentState() {
[email protected]3d7474ff2011-07-27 17:47:371211 return content::WebUIFactory::Get()->GetWebUIType(browser_context(),
1212 GetURL());
[email protected]1fd1a502011-03-30 16:55:561213}
1214
[email protected]420ae012009-04-24 05:16:321215void TabContents::DidNavigateMainFramePostCommit(
[email protected]8286f51a2011-05-31 17:39:131216 const content::LoadCommittedDetails& details,
[email protected]420ae012009-04-24 05:16:321217 const ViewHostMsg_FrameNavigate_Params& params) {
[email protected]1fd1a502011-03-30 16:55:561218 if (opener_web_ui_type_ != WebUI::kNoWebUI) {
[email protected]80a8fad2011-01-29 04:02:381219 // If this is a window.open navigation, use the same WebUI as the renderer
[email protected]c2e74fe82009-09-03 17:57:441220 // that opened the window, as long as both renderers have the same
1221 // privileges.
[email protected]1fd1a502011-03-30 16:55:561222 if (delegate_ && opener_web_ui_type_ == GetWebUITypeForCurrentState()) {
1223 WebUI* web_ui = content::GetContentClient()->browser()->
1224 GetWebUIFactory()->CreateWebUIForURL(this, GetURL());
[email protected]7ade2732011-02-10 00:13:581225 // web_ui might be NULL if the URL refers to a non-existent extension.
1226 if (web_ui) {
[email protected]d0980792011-02-13 19:41:401227 render_manager_.SetWebUIPostCommit(web_ui);
[email protected]7ade2732011-02-10 00:13:581228 web_ui->RenderViewCreated(render_view_host());
[email protected]c2e74fe82009-09-03 17:57:441229 }
1230 }
[email protected]1fd1a502011-03-30 16:55:561231 opener_web_ui_type_ = WebUI::kNoWebUI;
[email protected]c2e74fe82009-09-03 17:57:441232 }
1233
[email protected]4e697b042011-07-08 06:44:561234 if (details.is_navigation_to_different_page()) {
[email protected]420ae012009-04-24 05:16:321235 // Clear the status bubble. This is a workaround for a bug where WebKit
1236 // doesn't let us know that the cursor left an element during a
1237 // transition (this is also why the mouse cursor remains as a hand after
1238 // clicking on a link); see bugs 1184641 and 980803. We don't want to
1239 // clear the bubble when a user navigates to a named anchor in the same
1240 // page.
1241 UpdateTargetURL(details.entry->page_id(), GURL());
[email protected]420ae012009-04-24 05:16:321242 }
1243
[email protected]a6e82fc2010-02-24 22:28:141244 if (!details.is_in_page) {
[email protected]f17a0ee2010-05-17 17:38:471245 // Once the main frame is navigated, we're no longer considered to have
1246 // displayed insecure content.
1247 displayed_insecure_content_ = false;
[email protected]aece2c7f2009-08-27 20:43:171248 }
[email protected]ce5c4502009-05-06 16:46:111249
[email protected]3c9e1872010-11-18 16:17:491250 // Notify observers about navigation.
[email protected]585b30362011-01-28 02:30:171251 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
[email protected]3c9e1872010-11-18 16:17:491252 DidNavigateMainFramePostCommit(details, params));
[email protected]420ae012009-04-24 05:16:321253}
1254
1255void TabContents::DidNavigateAnyFramePostCommit(
1256 RenderViewHost* render_view_host,
[email protected]8286f51a2011-05-31 17:39:131257 const content::LoadCommittedDetails& details,
[email protected]420ae012009-04-24 05:16:321258 const ViewHostMsg_FrameNavigate_Params& params) {
[email protected]3ab9cb82011-06-03 18:02:071259 // If we navigate, reset JavaScript state. This does nothing to prevent
[email protected]420ae012009-04-24 05:16:321260 // a malicious script from spamming messages, since the script could just
1261 // reload the page to stop blocking.
[email protected]2e5b90c2011-08-16 21:11:551262 if (dialog_creator_) {
1263 dialog_creator_->ResetJavaScriptState(this);
1264 dialog_creator_ = NULL;
1265 }
[email protected]420ae012009-04-24 05:16:321266
[email protected]3c9e1872010-11-18 16:17:491267 // Notify observers about navigation.
[email protected]585b30362011-01-28 02:30:171268 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
[email protected]3c9e1872010-11-18 16:17:491269 DidNavigateAnyFramePostCommit(details, params));
[email protected]420ae012009-04-24 05:16:321270}
1271
[email protected]420ae012009-04-24 05:16:321272void TabContents::UpdateMaxPageIDIfNecessary(SiteInstance* site_instance,
1273 RenderViewHost* rvh) {
1274 // If we are creating a RVH for a restored controller, then we might
1275 // have more page IDs than the SiteInstance's current max page ID. We must
1276 // make sure that the max page ID is larger than any restored page ID.
1277 // Note that it is ok for conflicting page IDs to exist in another tab
1278 // (i.e., NavigationController), but if any page ID is larger than the max,
1279 // the back/forward list will get confused.
1280 int max_restored_page_id = controller_.max_restored_page_id();
1281 if (max_restored_page_id > 0) {
1282 int curr_max_page_id = site_instance->max_page_id();
1283 if (max_restored_page_id > curr_max_page_id) {
1284 // Need to update the site instance immediately.
1285 site_instance->UpdateMaxPageID(max_restored_page_id);
1286
1287 // Also tell the renderer to update its internal representation. We
1288 // need to reserve enough IDs to make all restored page IDs less than
1289 // the max.
1290 if (curr_max_page_id < 0)
1291 curr_max_page_id = 0;
[email protected]216813952011-05-19 22:21:261292 rvh->Send(new ViewMsg_ReservePageIDRange(
1293 rvh->routing_id(), max_restored_page_id - curr_max_page_id));
[email protected]420ae012009-04-24 05:16:321294 }
1295 }
1296}
1297
[email protected]6b2f7a82011-04-25 19:30:511298bool TabContents::UpdateTitleForEntry(NavigationEntry* entry,
[email protected]acafd272011-07-26 17:35:571299 const string16& title) {
[email protected]420ae012009-04-24 05:16:321300 // For file URLs without a title, use the pathname instead. In the case of a
1301 // synthesized title, we don't want the update to count toward the "one set
1302 // per page of the title to history."
[email protected]6b2f7a82011-04-25 19:30:511303 string16 final_title;
[email protected]420ae012009-04-24 05:16:321304 bool explicit_set;
[email protected]987fc3a2011-05-26 14:18:091305 if (entry && entry->url().SchemeIsFile() && title.empty()) {
[email protected]6b2f7a82011-04-25 19:30:511306 final_title = UTF8ToUTF16(entry->url().ExtractFileName());
[email protected]420ae012009-04-24 05:16:321307 explicit_set = false; // Don't count synthetic titles toward the set limit.
1308 } else {
[email protected]acafd272011-07-26 17:35:571309 TrimWhitespace(title, TRIM_ALL, &final_title);
[email protected]420ae012009-04-24 05:16:321310 explicit_set = true;
1311 }
1312
[email protected]987fc3a2011-05-26 14:18:091313 // If a page is created via window.open and never navigated,
1314 // there will be no navigation entry. In this situation,
1315 // |page_title_when_no_navigaiton_entry_| will be used for page title.
1316 if (entry) {
1317 if (final_title == entry->title())
1318 return false; // Nothing changed, don't bother.
[email protected]420ae012009-04-24 05:16:321319
[email protected]987fc3a2011-05-26 14:18:091320 entry->set_title(final_title);
1321 } else {
1322 if (page_title_when_no_navigation_entry_ == final_title)
1323 return false; // Nothing changed, don't bother.
1324
1325 page_title_when_no_navigation_entry_ = final_title;
1326 }
[email protected]420ae012009-04-24 05:16:321327
[email protected]420ae012009-04-24 05:16:321328 // Lastly, set the title for the view.
[email protected]790e2fd2011-09-21 20:28:231329 view_->SetPageTitle(final_title);
[email protected]420ae012009-04-24 05:16:321330
[email protected]105bb0f2011-05-24 17:12:141331 TitleUpdatedDetails details(entry, explicit_set);
1332
[email protected]cbc0e1b2010-04-12 18:33:041333 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271334 content::NOTIFICATION_TAB_CONTENTS_TITLE_UPDATED,
[email protected]cbc0e1b2010-04-12 18:33:041335 Source<TabContents>(this),
[email protected]105bb0f2011-05-24 17:12:141336 Details<TitleUpdatedDetails>(&details));
[email protected]cbc0e1b2010-04-12 18:33:041337
[email protected]420ae012009-04-24 05:16:321338 return true;
1339}
1340
1341void TabContents::NotifySwapped() {
1342 // After sending out a swap notification, we need to send a disconnect
1343 // notification so that clients that pick up a pointer to |this| can NULL the
1344 // pointer. See Bug 1230284.
1345 notify_disconnection_ = true;
1346 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271347 content::NOTIFICATION_TAB_CONTENTS_SWAPPED,
[email protected]d82ed61e2009-06-16 02:46:221348 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:321349 NotificationService::NoDetails());
1350}
1351
1352void TabContents::NotifyConnected() {
1353 notify_disconnection_ = true;
1354 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271355 content::NOTIFICATION_TAB_CONTENTS_CONNECTED,
[email protected]d82ed61e2009-06-16 02:46:221356 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:321357 NotificationService::NoDetails());
1358}
1359
1360void TabContents::NotifyDisconnected() {
1361 if (!notify_disconnection_)
1362 return;
1363
1364 notify_disconnection_ = false;
1365 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271366 content::NOTIFICATION_TAB_CONTENTS_DISCONNECTED,
[email protected]d82ed61e2009-06-16 02:46:221367 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:321368 NotificationService::NoDetails());
1369}
1370
[email protected]8d3347f2009-07-09 22:00:211371RenderViewHostDelegate::View* TabContents::GetViewDelegate() {
[email protected]420ae012009-04-24 05:16:321372 return view_.get();
1373}
1374
[email protected]8d3347f2009-07-09 22:00:211375RenderViewHostDelegate::RendererManagement*
1376TabContents::GetRendererManagementDelegate() {
1377 return &render_manager_;
1378}
1379
[email protected]3d7474ff2011-07-27 17:47:371380RendererPreferences TabContents::GetRendererPrefs(
1381 content::BrowserContext* browser_context) const {
[email protected]840b1512009-07-21 16:53:461382 return renderer_preferences_;
[email protected]80d96fa2009-06-10 22:34:511383}
1384
[email protected]57c6a652009-05-04 07:58:341385TabContents* TabContents::GetAsTabContents() {
1386 return this;
1387}
1388
[email protected]7b291f92009-08-14 05:43:531389ViewType::Type TabContents::GetRenderViewType() const {
1390 return ViewType::TAB_CONTENTS;
1391}
1392
[email protected]420ae012009-04-24 05:16:321393void TabContents::RenderViewCreated(RenderViewHost* render_view_host) {
[email protected]2e4633c2009-07-09 16:58:061394 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271395 content::NOTIFICATION_RENDER_VIEW_HOST_CREATED_FOR_TAB,
[email protected]2e4633c2009-07-09 16:58:061396 Source<TabContents>(this),
1397 Details<RenderViewHost>(render_view_host));
[email protected]420ae012009-04-24 05:16:321398 NavigationEntry* entry = controller_.GetActiveEntry();
1399 if (!entry)
1400 return;
1401
1402 // When we're creating views, we're still doing initial setup, so we always
[email protected]e0112912011-02-02 22:54:351403 // use the pending Web UI rather than any possibly existing committed one.
[email protected]1fd1a502011-03-30 16:55:561404 if (render_manager_.pending_web_ui())
[email protected]7ade2732011-02-10 00:13:581405 render_manager_.pending_web_ui()->RenderViewCreated(render_view_host);
[email protected]420ae012009-04-24 05:16:321406
1407 if (entry->IsViewSourceMode()) {
1408 // Put the renderer in view source mode.
1409 render_view_host->Send(
1410 new ViewMsg_EnableViewSourceMode(render_view_host->routing_id()));
1411 }
[email protected]0666aef2009-05-13 19:48:081412
1413 view()->RenderViewCreated(render_view_host);
[email protected]11f764bb2011-05-25 19:45:261414
1415 FOR_EACH_OBSERVER(
1416 TabContentsObserver, observers_, RenderViewCreated(render_view_host));
[email protected]420ae012009-04-24 05:16:321417}
1418
1419void TabContents::RenderViewReady(RenderViewHost* rvh) {
1420 if (rvh != render_view_host()) {
1421 // Don't notify the world, since this came from a renderer in the
1422 // background.
1423 return;
1424 }
1425
1426 NotifyConnected();
[email protected]9d8a4642009-07-29 17:25:301427 bool was_crashed = is_crashed();
[email protected]443b80e2010-12-14 00:42:231428 SetIsCrashed(base::TERMINATION_STATUS_STILL_RUNNING, 0);
[email protected]9d8a4642009-07-29 17:25:301429
1430 // Restore the focus to the tab (otherwise the focus will be on the top
1431 // window).
[email protected]484ae5912010-09-29 19:16:141432 if (was_crashed && !FocusLocationBarByDefault() &&
1433 (!delegate_ || delegate_->ShouldFocusPageAfterCrash())) {
[email protected]9d8a4642009-07-29 17:25:301434 Focus();
[email protected]484ae5912010-09-29 19:16:141435 }
[email protected]420ae012009-04-24 05:16:321436}
1437
[email protected]443b80e2010-12-14 00:42:231438void TabContents::RenderViewGone(RenderViewHost* rvh,
1439 base::TerminationStatus status,
1440 int error_code) {
[email protected]420ae012009-04-24 05:16:321441 if (rvh != render_view_host()) {
1442 // The pending page's RenderViewHost is gone.
1443 return;
1444 }
1445
1446 SetIsLoading(false, NULL);
1447 NotifyDisconnected();
[email protected]443b80e2010-12-14 00:42:231448 SetIsCrashed(status, error_code);
[email protected]b3e3f87a2011-10-01 23:37:541449 view()->OnTabCrashed(crashed_status(), crashed_error_code());
[email protected]420ae012009-04-24 05:16:321450
[email protected]55452902011-06-01 21:57:471451 FOR_EACH_OBSERVER(TabContentsObserver, observers_, RenderViewGone());
[email protected]420ae012009-04-24 05:16:321452}
1453
[email protected]2e4633c2009-07-09 16:58:061454void TabContents::RenderViewDeleted(RenderViewHost* rvh) {
[email protected]fb7b79f72009-11-06 18:00:481455 render_manager_.RenderViewDeleted(rvh);
[email protected]2e4633c2009-07-09 16:58:061456}
1457
[email protected]420ae012009-04-24 05:16:321458void TabContents::DidNavigate(RenderViewHost* rvh,
1459 const ViewHostMsg_FrameNavigate_Params& params) {
[email protected]09b29342011-06-24 19:18:481460 if (PageTransition::IsMainFrame(params.transition))
[email protected]420ae012009-04-24 05:16:321461 render_manager_.DidNavigateMainFrame(rvh);
[email protected]8030f012009-09-25 18:09:371462
[email protected]420ae012009-04-24 05:16:321463 // Update the site of the SiteInstance if it doesn't have one yet.
1464 if (!GetSiteInstance()->has_site())
1465 GetSiteInstance()->SetSite(params.url);
1466
1467 // Need to update MIME type here because it's referred to in
1468 // UpdateNavigationCommands() called by RendererDidNavigate() to
1469 // determine whether or not to enable the encoding menu.
1470 // It's updated only for the main frame. For a subframe,
1471 // RenderView::UpdateURL does not set params.contents_mime_type.
1472 // (see http://code.google.com/p/chromium/issues/detail?id=2929 )
1473 // TODO(jungshik): Add a test for the encoding menu to avoid
1474 // regressing it again.
1475 if (PageTransition::IsMainFrame(params.transition))
1476 contents_mime_type_ = params.contents_mime_type;
1477
[email protected]8286f51a2011-05-31 17:39:131478 content::LoadCommittedDetails details;
[email protected]93f230e02011-06-01 14:40:001479 bool did_navigate = controller_.RendererDidNavigate(params, &details);
[email protected]3e90d4a2009-07-03 17:38:391480
[email protected]a9c0bfe2010-09-17 08:35:221481 // Send notification about committed provisional loads. This notification is
1482 // different from the NAV_ENTRY_COMMITTED notification which doesn't include
1483 // the actual URL navigated to and isn't sent for AUTO_SUBFRAME navigations.
1484 if (details.type != NavigationType::NAV_IGNORE) {
[email protected]dabb0d12010-10-05 12:50:071485 // For AUTO_SUBFRAME navigations, an event for the main frame is generated
1486 // that is not recorded in the navigation history. For the purpose of
1487 // tracking navigation events, we treat this event as a sub frame navigation
1488 // event.
1489 bool is_main_frame = did_navigate ? details.is_main_frame : false;
[email protected]0d60f0192011-04-14 12:40:101490 PageTransition::Type transition_type = params.transition;
[email protected]a9c0bfe2010-09-17 08:35:221491 // Whether or not a page transition was triggered by going backward or
1492 // forward in the history is only stored in the navigation controller's
1493 // entry list.
1494 if (did_navigate &&
1495 (controller_.GetActiveEntry()->transition_type() &
1496 PageTransition::FORWARD_BACK)) {
[email protected]0d60f0192011-04-14 12:40:101497 transition_type = params.transition | PageTransition::FORWARD_BACK;
[email protected]a9c0bfe2010-09-17 08:35:221498 }
[email protected]0d60f0192011-04-14 12:40:101499 // Notify observers about the commit of the provisional load.
1500 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
1501 DidCommitProvisionalLoadForFrame(params.frame_id,
1502 is_main_frame, params.url, transition_type));
[email protected]a9c0bfe2010-09-17 08:35:221503 }
1504
[email protected]76543b92009-08-31 17:27:451505 if (!did_navigate)
1506 return; // No navigation happened.
[email protected]420ae012009-04-24 05:16:321507
1508 // DO NOT ADD MORE STUFF TO THIS FUNCTION! Your component should either listen
1509 // for the appropriate notification (best) or you can add it to
1510 // DidNavigateMainFramePostCommit / DidNavigateAnyFramePostCommit (only if
1511 // necessary, please).
1512
1513 // Run post-commit tasks.
[email protected]93f230e02011-06-01 14:40:001514 if (details.is_main_frame) {
[email protected]420ae012009-04-24 05:16:321515 DidNavigateMainFramePostCommit(details, params);
[email protected]09b29342011-06-24 19:18:481516 if (delegate())
1517 delegate()->DidNavigateMainFramePostCommit(this);
[email protected]93f230e02011-06-01 14:40:001518 }
[email protected]420ae012009-04-24 05:16:321519 DidNavigateAnyFramePostCommit(rvh, details, params);
1520}
1521
1522void TabContents::UpdateState(RenderViewHost* rvh,
1523 int32 page_id,
1524 const std::string& state) {
[email protected]992db4c2011-05-12 15:37:151525 // Ensure that this state update comes from either the active RVH or one of
1526 // the swapped out RVHs. We don't expect to hear from any other RVHs.
1527 DCHECK(rvh == render_view_host() || render_manager_.IsSwappedOut(rvh));
[email protected]420ae012009-04-24 05:16:321528
1529 // We must be prepared to handle state updates for any page, these occur
1530 // when the user is scrolling and entering form data, as well as when we're
1531 // leaving a page, in which case our state may have already been moved to
1532 // the next page. The navigation controller will look up the appropriate
1533 // NavigationEntry and update it when it is notified via the delegate.
1534
1535 int entry_index = controller_.GetEntryIndexWithPageID(
[email protected]992db4c2011-05-12 15:37:151536 rvh->site_instance(), page_id);
[email protected]420ae012009-04-24 05:16:321537 if (entry_index < 0)
1538 return;
1539 NavigationEntry* entry = controller_.GetEntryAtIndex(entry_index);
1540
1541 if (state == entry->content_state())
1542 return; // Nothing to update.
1543 entry->set_content_state(state);
1544 controller_.NotifyEntryChanged(entry, entry_index);
1545}
1546
1547void TabContents::UpdateTitle(RenderViewHost* rvh,
[email protected]acafd272011-07-26 17:35:571548 int32 page_id,
[email protected]a49e10b2011-08-01 23:57:461549 const string16& title,
1550 base::i18n::TextDirection title_direction) {
[email protected]420ae012009-04-24 05:16:321551 // If we have a title, that's a pretty good indication that we've started
1552 // getting useful data.
1553 SetNotWaitingForResponse();
1554
1555 DCHECK(rvh == render_view_host());
[email protected]371cc8b2010-09-01 16:03:481556 NavigationEntry* entry = controller_.GetEntryWithPageID(rvh->site_instance(),
[email protected]ec0b6c42010-08-26 03:16:581557 page_id);
[email protected]987fc3a2011-05-26 14:18:091558
[email protected]a49e10b2011-08-01 23:57:461559 // TODO(evan): make use of title_direction.
1560 // http://code.google.com/p/chromium/issues/detail?id=27094
[email protected]987fc3a2011-05-26 14:18:091561 if (!UpdateTitleForEntry(entry, title))
[email protected]420ae012009-04-24 05:16:321562 return;
1563
1564 // Broadcast notifications when the UI should be updated.
1565 if (entry == controller_.GetEntryAtOffset(0))
[email protected]f1cd5e82009-10-23 17:19:031566 NotifyNavigationStateChanged(INVALIDATE_TITLE);
[email protected]420ae012009-04-24 05:16:321567}
1568
[email protected]420ae012009-04-24 05:16:321569void TabContents::UpdateEncoding(RenderViewHost* render_view_host,
[email protected]41fc0322009-09-04 22:23:401570 const std::string& encoding) {
[email protected]420ae012009-04-24 05:16:321571 set_encoding(encoding);
1572}
1573
1574void TabContents::UpdateTargetURL(int32 page_id, const GURL& url) {
1575 if (delegate())
[email protected]d952a052011-09-06 18:42:451576 delegate()->UpdateTargetURL(this, page_id, url);
[email protected]420ae012009-04-24 05:16:321577}
1578
[email protected]420ae012009-04-24 05:16:321579void TabContents::Close(RenderViewHost* rvh) {
[email protected]07707302009-11-06 00:50:291580 // The UI may be in an event-tracking loop, such as between the
1581 // mouse-down and mouse-up in text selection or a button click.
1582 // Defer the close until after tracking is complete, so that we
1583 // don't free objects out from under the UI.
1584 // TODO(shess): This could probably be integrated with the
1585 // IsDoingDrag() test below. Punting for now because I need more
1586 // research to understand how this impacts platforms other than Mac.
1587 // TODO(shess): This could get more fine-grained. For instance,
1588 // closing a tab in another window while selecting text in the
1589 // current window's Omnibox should be just fine.
1590 if (view()->IsEventTracking()) {
1591 view()->CloseTabAfterEventTracking();
1592 return;
1593 }
1594
[email protected]24a4d1062009-07-10 23:10:421595 // If we close the tab while we're in the middle of a drag, we'll crash.
1596 // Instead, cancel the drag and close it as soon as the drag ends.
1597 if (view()->IsDoingDrag()) {
1598 view()->CancelDragAndCloseTab();
1599 return;
1600 }
1601
[email protected]420ae012009-04-24 05:16:321602 // Ignore this if it comes from a RenderViewHost that we aren't showing.
1603 if (delegate() && rvh == render_view_host())
1604 delegate()->CloseContents(this);
1605}
1606
1607void TabContents::RequestMove(const gfx::Rect& new_bounds) {
[email protected]b35b26b32011-05-05 20:35:141608 if (delegate() && delegate()->IsPopupOrPanel(this))
[email protected]420ae012009-04-24 05:16:321609 delegate()->MoveContents(this, new_bounds);
1610}
1611
[email protected]7ab1e7d62009-10-14 23:32:011612void TabContents::DidStartLoading() {
[email protected]420ae012009-04-24 05:16:321613 SetIsLoading(true, NULL);
[email protected]3c9e1872010-11-18 16:17:491614
[email protected]c40d6232011-03-25 00:16:211615 if (delegate() && content_restrictions_) {
1616 content_restrictions_ = 0;
[email protected]de4761b2010-12-07 21:05:211617 delegate()->ContentRestrictionsChanged(this);
[email protected]9e823662010-10-13 23:36:001618 }
[email protected]3c9e1872010-11-18 16:17:491619
1620 // Notify observers about navigation.
[email protected]585b30362011-01-28 02:30:171621 FOR_EACH_OBSERVER(TabContentsObserver, observers_, DidStartLoading());
[email protected]420ae012009-04-24 05:16:321622}
1623
[email protected]7ab1e7d62009-10-14 23:32:011624void TabContents::DidStopLoading() {
[email protected]420ae012009-04-24 05:16:321625 scoped_ptr<LoadNotificationDetails> details;
1626
1627 NavigationEntry* entry = controller_.GetActiveEntry();
1628 // An entry may not exist for a stop when loading an initial blank page or
1629 // if an iframe injected by script into a blank page finishes loading.
1630 if (entry) {
[email protected]420ae012009-04-24 05:16:321631 base::TimeDelta elapsed = base::TimeTicks::Now() - current_load_start_;
1632
1633 details.reset(new LoadNotificationDetails(
[email protected]ebe89e062009-08-13 23:16:541634 entry->virtual_url(),
[email protected]420ae012009-04-24 05:16:321635 entry->transition_type(),
1636 elapsed,
1637 &controller_,
1638 controller_.GetCurrentEntryIndex()));
1639 }
1640
[email protected]420ae012009-04-24 05:16:321641 SetIsLoading(false, details.get());
[email protected]3c9e1872010-11-18 16:17:491642
1643 // Notify observers about navigation.
[email protected]585b30362011-01-28 02:30:171644 FOR_EACH_OBSERVER(TabContentsObserver, observers_, DidStopLoading());
[email protected]420ae012009-04-24 05:16:321645}
1646
[email protected]c95fa8b2011-04-28 20:26:161647void TabContents::DidCancelLoading() {
1648 controller_.DiscardNonCommittedEntries();
1649
1650 // Update the URL display.
1651 NotifyNavigationStateChanged(TabContents::INVALIDATE_URL);
1652}
1653
[email protected]1a3c3cb2010-12-16 21:03:401654void TabContents::DidChangeLoadProgress(double progress) {
1655 if (delegate())
1656 delegate()->LoadProgressChanged(progress);
1657}
1658
[email protected]25497492010-09-11 15:15:081659void TabContents::DocumentOnLoadCompletedInMainFrame(
1660 RenderViewHost* render_view_host,
1661 int32 page_id) {
1662 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271663 content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME,
[email protected]25497492010-09-11 15:15:081664 Source<TabContents>(this),
1665 Details<int>(&page_id));
1666}
1667
[email protected]420ae012009-04-24 05:16:321668void TabContents::RequestOpenURL(const GURL& url, const GURL& referrer,
1669 WindowOpenDisposition disposition) {
[email protected]7ade2732011-02-10 00:13:581670 if (render_manager_.web_ui()) {
[email protected]e0112912011-02-02 22:54:351671 // When we're a Web UI, it will provide a page transition type for us (this
[email protected]420ae012009-04-24 05:16:321672 // is so the new tab page can specify AUTO_BOOKMARK for automatically
1673 // generated suggestions).
1674 //
[email protected]e0112912011-02-02 22:54:351675 // Note also that we hide the referrer for Web UI pages. We don't really
[email protected]60e448982009-05-06 04:21:161676 // want web sites to see a referrer of "chrome://blah" (and some
1677 // chrome: URLs might have search terms or other stuff we don't want to
[email protected]420ae012009-04-24 05:16:321678 // send to the site), so we send no referrer.
1679 OpenURL(url, GURL(), disposition,
[email protected]7ade2732011-02-10 00:13:581680 render_manager_.web_ui()->link_transition_type());
[email protected]420ae012009-04-24 05:16:321681 } else {
1682 OpenURL(url, referrer, disposition, PageTransition::LINK);
1683 }
1684}
1685
[email protected]420ae012009-04-24 05:16:321686void TabContents::RunJavaScriptMessage(
[email protected]992db4c2011-05-12 15:37:151687 const RenderViewHost* rvh,
[email protected]4f5ce842011-05-27 19:34:411688 const string16& message,
1689 const string16& default_prompt,
[email protected]420ae012009-04-24 05:16:321690 const GURL& frame_url,
1691 const int flags,
1692 IPC::Message* reply_msg,
1693 bool* did_suppress_message) {
[email protected]3ab9cb82011-06-03 18:02:071694 // Suppress JavaScript dialogs when requested. Also suppress messages when
1695 // showing an interstitial as it's shown over the previous page and we don't
1696 // want the hidden page's dialogs to interfere with the interstitial.
[email protected]8f55e802010-12-06 18:11:501697 bool suppress_this_message =
[email protected]992db4c2011-05-12 15:37:151698 rvh->is_swapped_out() ||
[email protected]8f55e802010-12-06 18:11:501699 showing_interstitial_page() ||
[email protected]3ab9cb82011-06-03 18:02:071700 !delegate_ ||
1701 delegate_->ShouldSuppressDialogs();
[email protected]420ae012009-04-24 05:16:321702
1703 if (!suppress_this_message) {
[email protected]b627d9a2011-06-28 14:06:341704 content::JavaScriptDialogCreator::TitleType title_type;
1705 string16 title;
1706
1707 if (!frame_url.has_host()) {
1708 title_type = content::JavaScriptDialogCreator::DIALOG_TITLE_NONE;
1709 } else {
1710 title_type = content::JavaScriptDialogCreator::DIALOG_TITLE_FORMATTED_URL;
1711 title = net::FormatUrl(
1712 frame_url.GetOrigin(),
1713 content::GetContentClient()->browser()->GetAcceptLangs(this));
1714 }
1715
[email protected]2e5b90c2011-08-16 21:11:551716 dialog_creator_ = delegate_->GetJavaScriptDialogCreator();
1717 dialog_creator_->RunJavaScriptDialog(this,
1718 title_type,
1719 title,
1720 flags,
1721 message,
1722 default_prompt,
1723 reply_msg,
1724 &suppress_this_message);
[email protected]420ae012009-04-24 05:16:321725 }
[email protected]3ab9cb82011-06-03 18:02:071726
1727 if (suppress_this_message) {
1728 // If we are suppressing messages, just reply as if the user immediately
1729 // pressed "Cancel".
1730 OnDialogClosed(reply_msg, false, string16());
1731 }
1732
1733 *did_suppress_message = suppress_this_message;
[email protected]420ae012009-04-24 05:16:321734}
1735
[email protected]992db4c2011-05-12 15:37:151736void TabContents::RunBeforeUnloadConfirm(const RenderViewHost* rvh,
[email protected]4f5ce842011-05-27 19:34:411737 const string16& message,
[email protected]420ae012009-04-24 05:16:321738 IPC::Message* reply_msg) {
[email protected]5aab5e22010-12-08 22:13:291739 if (delegate())
1740 delegate()->WillRunBeforeUnloadConfirm();
[email protected]3ab9cb82011-06-03 18:02:071741
1742 bool suppress_this_message =
1743 rvh->is_swapped_out() ||
1744 !delegate_ ||
1745 delegate_->ShouldSuppressDialogs();
[email protected]992db4c2011-05-12 15:37:151746 if (suppress_this_message) {
[email protected]4f5ce842011-05-27 19:34:411747 render_view_host()->JavaScriptDialogClosed(reply_msg, true, string16());
[email protected]8f55e802010-12-06 18:11:501748 return;
1749 }
[email protected]3ab9cb82011-06-03 18:02:071750
[email protected]7ab1e7d62009-10-14 23:32:011751 is_showing_before_unload_dialog_ = true;
[email protected]2e5b90c2011-08-16 21:11:551752 dialog_creator_ = delegate_->GetJavaScriptDialogCreator();
1753 dialog_creator_->RunBeforeUnloadDialog(this,
1754 message,
1755 reply_msg);
[email protected]420ae012009-04-24 05:16:321756}
1757
[email protected]420ae012009-04-24 05:16:321758WebPreferences TabContents::GetWebkitPrefs() {
[email protected]447021c2010-09-08 21:29:081759 WebPreferences web_prefs =
[email protected]181a95ee2011-07-12 19:26:361760 content::GetContentClient()->browser()->GetWebkitPrefs(
[email protected]3d7474ff2011-07-27 17:47:371761 render_view_host()->process()->browser_context(), false);
[email protected]447021c2010-09-08 21:29:081762
[email protected]b8299c12011-06-03 19:52:281763 // Force accelerated compositing and 2d canvas off for chrome:, about: and
[email protected]2ef216b2011-08-01 19:25:211764 // chrome-devtools: pages (unless it's specifically allowed).
1765 if ((GetURL().SchemeIs(chrome::kChromeDevToolsScheme) ||
[email protected]30b8e0ee2011-09-12 14:45:571766#if !defined(TOUCH_UI)
1767 // Allow accelerated compositing for keyboard and log in screen.
[email protected]b8299c12011-06-03 19:52:281768 GetURL().SchemeIs(chrome::kChromeUIScheme) ||
[email protected]30b8e0ee2011-09-12 14:45:571769#endif
[email protected]cd4a5bd22011-09-13 06:46:301770 (GetURL().SchemeIs(chrome::kAboutScheme) &&
1771 GetURL().spec() != chrome::kAboutBlankURL)) &&
[email protected]2ef216b2011-08-01 19:25:211772 !web_prefs.allow_webui_compositing) {
[email protected]447021c2010-09-08 21:29:081773 web_prefs.accelerated_compositing_enabled = false;
[email protected]9beff752010-09-22 19:35:431774 web_prefs.accelerated_2d_canvas_enabled = false;
[email protected]447021c2010-09-08 21:29:081775 }
1776
[email protected]2f7830b32011-08-26 18:22:251777#if defined(TOUCH_UI)
1778 web_prefs.force_compositing_mode = true;
1779#endif
1780
[email protected]447021c2010-09-08 21:29:081781 return web_prefs;
[email protected]420ae012009-04-24 05:16:321782}
1783
[email protected]7d472472011-01-22 01:30:251784void TabContents::OnUserGesture() {
[email protected]ddb85052011-05-18 14:40:271785 // Notify observers.
1786 FOR_EACH_OBSERVER(TabContentsObserver, observers_, DidGetUserGesture());
[email protected]686493142011-07-15 21:47:221787
1788 ResourceDispatcherHost* rdh =
1789 content::GetContentClient()->browser()->GetResourceDispatcherHost();
1790 if (rdh) // NULL in unittests.
[email protected]8ec98cd22011-08-09 22:31:301791 rdh->OnUserGesture(this);
[email protected]7d472472011-01-22 01:30:251792}
1793
[email protected]fa1cf0b82010-01-15 21:49:441794void TabContents::OnIgnoredUIEvent() {
[email protected]31145092011-09-30 01:49:441795 // Notify observers.
1796 FOR_EACH_OBSERVER(TabContentsObserver, observers_, DidGetIgnoredUIEvent());
[email protected]fa1cf0b82010-01-15 21:49:441797}
1798
[email protected]420ae012009-04-24 05:16:321799void TabContents::RendererUnresponsive(RenderViewHost* rvh,
1800 bool is_during_unload) {
[email protected]992db4c2011-05-12 15:37:151801 // Don't show hung renderer dialog for a swapped out RVH.
1802 if (rvh != render_view_host())
1803 return;
1804
[email protected]e5fc1632011-08-08 07:51:531805 // Ignore renderer unresponsive event if debugger is attached to the tab
1806 // since the event may be a result of the renderer sitting on a breakpoint.
1807 // See http://crbug.com/65458
1808 DevToolsManager* devtools_manager = DevToolsManager::GetInstance();
1809 if (devtools_manager &&
1810 devtools_manager->GetDevToolsClientHostFor(rvh) != NULL)
1811 return;
1812
[email protected]420ae012009-04-24 05:16:321813 if (is_during_unload) {
1814 // Hang occurred while firing the beforeunload/unload handler.
1815 // Pretend the handler fired so tab closing continues as if it had.
[email protected]829e7612009-04-25 01:15:111816 rvh->set_sudden_termination_allowed(true);
[email protected]420ae012009-04-24 05:16:321817
1818 if (!render_manager_.ShouldCloseTabOnUnresponsiveRenderer())
1819 return;
1820
1821 // If the tab hangs in the beforeunload/unload handler there's really
1822 // nothing we can do to recover. Pretend the unload listeners have
1823 // all fired and close the tab. If the hang is in the beforeunload handler
1824 // then the user will not have the option of cancelling the close.
1825 Close(rvh);
1826 return;
1827 }
1828
[email protected]55452902011-06-01 21:57:471829 if (!render_view_host() || !render_view_host()->IsRenderViewLive())
1830 return;
1831
1832 if (delegate())
1833 delegate()->RendererUnresponsive(this);
[email protected]420ae012009-04-24 05:16:321834}
1835
1836void TabContents::RendererResponsive(RenderViewHost* render_view_host) {
[email protected]55452902011-06-01 21:57:471837 if (delegate())
1838 delegate()->RendererResponsive(this);
[email protected]420ae012009-04-24 05:16:321839}
1840
1841void TabContents::LoadStateChanged(const GURL& url,
[email protected]9c235f042011-08-10 22:28:211842 const net::LoadStateWithParam& load_state,
[email protected]094e5b22009-09-25 04:23:561843 uint64 upload_position,
1844 uint64 upload_size) {
[email protected]420ae012009-04-24 05:16:321845 load_state_ = load_state;
[email protected]094e5b22009-09-25 04:23:561846 upload_position_ = upload_position;
1847 upload_size_ = upload_size;
[email protected]04866c42011-05-03 20:03:501848 load_state_host_ = net::IDNToUnicode(url.host(),
[email protected]b5cca982011-05-26 04:42:081849 content::GetContentClient()->browser()->GetAcceptLangs(this));
[email protected]9c235f042011-08-10 22:28:211850 if (load_state_.state == net::LOAD_STATE_READING_RESPONSE)
[email protected]420ae012009-04-24 05:16:321851 SetNotWaitingForResponse();
[email protected]f5d978c2011-07-21 14:43:511852 if (IsLoading())
[email protected]c9cd2222009-05-06 05:16:501853 NotifyNavigationStateChanged(INVALIDATE_LOAD | INVALIDATE_TAB);
[email protected]420ae012009-04-24 05:16:321854}
1855
[email protected]7d472472011-01-22 01:30:251856void TabContents::WorkerCrashed() {
[email protected]0dd3a0ab2011-02-18 08:17:441857 if (delegate())
[email protected]55452902011-06-01 21:57:471858 delegate()->WorkerCrashed(this);
[email protected]7d472472011-01-22 01:30:251859}
1860
[email protected]420ae012009-04-24 05:16:321861void TabContents::BeforeUnloadFiredFromRenderManager(
1862 bool proceed,
1863 bool* proceed_to_fire_unload) {
1864 if (delegate())
1865 delegate()->BeforeUnloadFired(this, proceed, proceed_to_fire_unload);
1866}
1867
[email protected]3a3d47472010-07-15 21:03:541868void TabContents::DidStartLoadingFromRenderManager(
1869 RenderViewHost* render_view_host) {
1870 DidStartLoading();
1871}
1872
1873void TabContents::RenderViewGoneFromRenderManager(
1874 RenderViewHost* render_view_host) {
[email protected]443b80e2010-12-14 00:42:231875 DCHECK(crashed_status_ != base::TERMINATION_STATUS_STILL_RUNNING);
1876 RenderViewGone(render_view_host, crashed_status_, crashed_error_code_);
[email protected]3a3d47472010-07-15 21:03:541877}
1878
[email protected]420ae012009-04-24 05:16:321879void TabContents::UpdateRenderViewSizeForRenderManager() {
1880 // TODO(brettw) this is a hack. See TabContentsView::SizeContents.
[email protected]0323ee42010-02-17 22:03:261881 gfx::Size size = view_->GetContainerSize();
1882 // 0x0 isn't a valid window size (minimal window size is 1x1) but it may be
1883 // here during container initialization and normal window size will be set
1884 // later. In case of tab duplication this resizing to 0x0 prevents setting
1885 // normal size later so just ignore it.
1886 if (!size.IsEmpty())
1887 view_->SizeContents(size);
[email protected]420ae012009-04-24 05:16:321888}
1889
[email protected]3a3d47472010-07-15 21:03:541890void TabContents::NotifySwappedFromRenderManager() {
1891 NotifySwapped();
1892}
1893
1894NavigationController& TabContents::GetControllerForRenderManager() {
1895 return controller();
1896}
1897
[email protected]d0980792011-02-13 19:41:401898WebUI* TabContents::CreateWebUIForRenderManager(const GURL& url) {
[email protected]1fd1a502011-03-30 16:55:561899 return content::WebUIFactory::Get()->CreateWebUIForURL(this, url);
[email protected]420ae012009-04-24 05:16:321900}
1901
1902NavigationEntry*
1903TabContents::GetLastCommittedNavigationEntryForRenderManager() {
1904 return controller_.GetLastCommittedEntry();
1905}
1906
1907bool TabContents::CreateRenderViewForRenderManager(
1908 RenderViewHost* render_view_host) {
[email protected]420ae012009-04-24 05:16:321909 RenderWidgetHostView* rwh_view = view_->CreateViewForWidget(render_view_host);
[email protected]1a98a932009-11-17 00:12:521910
[email protected]420ae012009-04-24 05:16:321911 // Now that the RenderView has been created, we need to tell it its size.
1912 rwh_view->SetSize(view_->GetContainerSize());
1913
[email protected]a4127722011-04-27 23:13:521914 if (!render_view_host->CreateRenderView(string16()))
1915 return false;
1916
[email protected]f8233cc2011-05-31 20:24:501917#if defined(OS_LINUX)
1918 // Force a ViewMsg_Resize to be sent, needed to make plugins show up on
1919 // linux. See crbug.com/83941.
1920 if (RenderWidgetHost* render_widget_host = rwh_view->GetRenderWidgetHost())
1921 render_widget_host->WasResized();
1922#endif
1923
[email protected]420ae012009-04-24 05:16:321924 UpdateMaxPageIDIfNecessary(render_view_host->site_instance(),
1925 render_view_host);
1926 return true;
1927}
1928
[email protected]3ab9cb82011-06-03 18:02:071929void TabContents::OnDialogClosed(IPC::Message* reply_msg,
1930 bool success,
1931 const string16& user_input) {
[email protected]beb440c2009-11-06 04:08:541932 if (is_showing_before_unload_dialog_ && !success) {
1933 // If a beforeunload dialog is canceled, we need to stop the throbber from
1934 // spinning, since we forced it to start spinning in Navigate.
1935 DidStopLoading();
1936
1937 tab_close_start_time_ = base::TimeTicks();
1938 }
1939 is_showing_before_unload_dialog_ = false;
[email protected]4f5ce842011-05-27 19:34:411940 render_view_host()->JavaScriptDialogClosed(reply_msg,
1941 success,
[email protected]3ab9cb82011-06-03 18:02:071942 user_input);
[email protected]beb440c2009-11-06 04:08:541943}
1944
[email protected]3ab9cb82011-06-03 18:02:071945gfx::NativeWindow TabContents::GetDialogRootWindow() {
1946 return view_->GetTopLevelNativeWindow();
[email protected]beb440c2009-11-06 04:08:541947}
1948
[email protected]a1e97f02011-06-30 14:04:341949void TabContents::OnDialogShown() {
1950 Activate();
[email protected]3a3d47472010-07-15 21:03:541951}
1952
[email protected]41fc0322009-09-04 22:23:401953void TabContents::set_encoding(const std::string& encoding) {
[email protected]763ec4ca2011-04-29 15:48:121954 encoding_ = content::GetContentClient()->browser()->
1955 GetCanonicalEncodingNameByAliasName(encoding);
[email protected]aacd7cc2009-09-02 14:51:101956}
[email protected]f45d2a72010-03-08 23:28:351957
[email protected]33f74972010-12-08 16:40:361958void TabContents::SwapInRenderViewHost(RenderViewHost* rvh) {
1959 render_manager_.SwapInRenderViewHost(rvh);
1960}
1961
1962void TabContents::CreateViewAndSetSizeForRVH(RenderViewHost* rvh) {
1963 RenderWidgetHostView* rwh_view = view()->CreateViewForWidget(rvh);
1964 rwh_view->SetSize(view()->GetContainerSize());
1965}