blob: 2c4a4a302cd142f7c9e51664e7102691978719ea [file] [log] [blame]
[email protected]61d68ef12011-01-13 14:02:561// Copyright (c) 2011 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
[email protected]0dd3a0ab2011-02-18 08:17:445#include "content/browser/tab_contents/tab_contents.h"
[email protected]39526562011-02-05 03:41:516
[email protected]b75b8292010-10-01 07:28:257#include <cmath>
8
[email protected]36fb2c7c2011-04-04 15:49:089#include "base/command_line.h"
[email protected]835d7c82010-10-14 04:38:3810#include "base/metrics/histogram.h"
[email protected]724159a2010-12-30 01:11:1811#include "base/metrics/stats_counters.h"
[email protected]96d185d2009-04-24 03:28:5412#include "base/string16.h"
[email protected]996fd702009-09-04 19:12:3713#include "base/string_util.h"
[email protected]96d185d2009-04-24 03:28:5414#include "base/time.h"
[email protected]3c733bde2010-12-21 19:56:3115#include "base/utf_string_conversions.h"
[email protected]3d7474ff2011-07-27 17:47:3716#include "content/browser/browser_context.h"
[email protected]567812d2011-02-24 17:40:5017#include "content/browser/child_process_security_policy.h"
[email protected]1fd1a502011-03-30 16:55:5618#include "content/browser/content_browser_client.h"
[email protected]b46442d7e2011-06-29 02:16:0619#include "content/browser/debugger/devtools_manager.h"
[email protected]71bf3f5e2011-08-15 21:05:2220#include "content/browser/download/download_manager.h"
[email protected]8bd9e562011-08-16 23:55:4621#include "content/browser/download/download_stats.h"
[email protected]567812d2011-02-24 17:40:5022#include "content/browser/host_zoom_map.h"
23#include "content/browser/in_process_webkit/session_storage_namespace.h"
[email protected]37a72af2011-06-13 05:42:0124#include "content/browser/load_from_memory_cache_details.h"
[email protected]35e251d2011-05-24 21:01:0425#include "content/browser/load_notification_details.h"
[email protected]567812d2011-02-24 17:40:5026#include "content/browser/renderer_host/render_process_host.h"
27#include "content/browser/renderer_host/render_view_host.h"
28#include "content/browser/renderer_host/render_widget_host_view.h"
[email protected]686493142011-07-15 21:47:2229#include "content/browser/renderer_host/resource_dispatcher_host.h"
[email protected]567812d2011-02-24 17:40:5030#include "content/browser/renderer_host/resource_request_details.h"
31#include "content/browser/site_instance.h"
[email protected]0dd3a0ab2011-02-18 08:17:4432#include "content/browser/tab_contents/interstitial_page.h"
[email protected]8286f51a2011-05-31 17:39:1333#include "content/browser/tab_contents/navigation_details.h"
[email protected]0dd3a0ab2011-02-18 08:17:4434#include "content/browser/tab_contents/navigation_entry.h"
35#include "content/browser/tab_contents/provisional_load_details.h"
36#include "content/browser/tab_contents/tab_contents_delegate.h"
37#include "content/browser/tab_contents/tab_contents_observer.h"
38#include "content/browser/tab_contents/tab_contents_view.h"
[email protected]105bb0f2011-05-24 17:12:1439#include "content/browser/tab_contents/title_updated_details.h"
[email protected]afd1e522011-04-27 23:29:5940#include "content/browser/user_metrics.h"
[email protected]1fd1a502011-03-30 16:55:5641#include "content/browser/webui/web_ui_factory.h"
[email protected]9966325b2011-04-18 05:00:1042#include "content/common/bindings_policy.h"
[email protected]1fd1a502011-03-30 16:55:5643#include "content/common/content_client.h"
[email protected]763ec4ca2011-04-29 15:48:1244#include "content/common/content_restriction.h"
[email protected]4dd57932011-03-17 06:06:1245#include "content/common/navigation_types.h"
[email protected]7f070d42011-03-09 20:25:3246#include "content/common/notification_service.h"
[email protected]0f012df82011-05-19 14:15:2947#include "content/common/url_constants.h"
[email protected]2c5569662011-03-22 20:45:0248#include "content/common/view_messages.h"
[email protected]d686e812009-06-03 19:10:2949#include "net/base/net_util.h"
[email protected]420ae012009-04-24 05:16:3250#include "net/base/registry_controlled_domain.h"
[email protected]abe2c032011-03-31 18:49:3451#include "net/url_request/url_request_context_getter.h"
[email protected]8bd0fe62011-01-17 06:44:3752#include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h"
[email protected]08397d52011-02-05 01:53:3853#include "ui/gfx/codec/png_codec.h"
[email protected]d5e311b2010-07-20 17:15:0354#include "webkit/glue/password_form.h"
[email protected]3c733bde2010-12-21 19:56:3155#include "webkit/glue/webpreferences.h"
56
57#if defined(OS_MACOSX)
[email protected]b9b751f22011-03-25 14:04:1258#include "ui/gfx/surface/io_surface_support_mac.h"
[email protected]3c733bde2010-12-21 19:56:3159#endif // defined(OS_MACOSX)
[email protected]3e45ba92009-02-20 21:09:0060
[email protected]420ae012009-04-24 05:16:3261// Cross-Site Navigations
62//
63// If a TabContents is told to navigate to a different web site (as determined
64// by SiteInstance), it will replace its current RenderViewHost with a new
65// RenderViewHost dedicated to the new SiteInstance. This works as follows:
66//
67// - Navigate determines whether the destination is cross-site, and if so,
68// it creates a pending_render_view_host_ and moves into the PENDING
69// RendererState.
70// - 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
77// cancel all the pending logic and go back to NORMAL. Otherwise we allow
78// the pending RVH to send the navigation request to its renderer.
79// - ResourceDispatcherHost receives a ResourceRequest on the IO thread. It
80// checks CrossSiteRequestManager to see that the RVH responsible has a
81// pending cross-site request, and then installs a CrossSiteEventHandler.
82// - When RDH receives a response, the BufferedEventHandler determines whether
83// it is a download. If so, it sends a message to the new renderer causing
84// it to cancel the request, and the download proceeds in the download
85// thread. For now, we stay in a PENDING state (with a pending RVH) until
[email protected]57c6a652009-05-04 07:58:3486// the next DidNavigate event for this TabContents. This isn't ideal, but it
[email protected]420ae012009-04-24 05:16:3287// doesn't affect any functionality.
88// - 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
98// pending RVH and goes back to the NORMAL RendererState.
[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]1ccb3568d2010-02-19 10:51:16144 NavigationController::ReloadType reload_type,
145 ViewMsg_Navigate_Params* params) {
[email protected]056de2d2009-06-26 16:41:34146 params->page_id = entry.page_id();
[email protected]876bc832010-09-07 16:29:54147 params->pending_history_list_offset = controller.GetIndexOfEntry(&entry);
[email protected]3cc72b12010-03-18 23:03:00148 params->current_history_list_offset = controller.last_committed_entry_index();
149 params->current_history_list_length = controller.entry_count();
[email protected]056de2d2009-06-26 16:41:34150 params->url = entry.url();
151 params->referrer = entry.referrer();
152 params->transition = entry.transition_type();
153 params->state = entry.content_state();
[email protected]3cc72b12010-03-18 23:03:00154 params->navigation_type =
[email protected]3d7474ff2011-07-27 17:47:37155 GetNavigationType(controller.browser_context(), entry, reload_type);
[email protected]056de2d2009-06-26 16:41:34156 params->request_time = base::Time::Now();
157}
158
[email protected]420ae012009-04-24 05:16:32159} // namespace
160
[email protected]f4f50ef2011-01-21 19:01:19161
162// TabContents ----------------------------------------------------------------
[email protected]420ae012009-04-24 05:16:32163
[email protected]3d7474ff2011-07-27 17:47:37164TabContents::TabContents(content::BrowserContext* browser_context,
[email protected]420ae012009-04-24 05:16:32165 SiteInstance* site_instance,
166 int routing_id,
[email protected]6ee12c42010-09-14 09:36:07167 const TabContents* base_tab_contents,
168 SessionStorageNamespace* session_storage_namespace)
[email protected]b680ad22009-04-15 23:19:42169 : delegate_(NULL),
[email protected]6ee12c42010-09-14 09:36:07170 ALLOW_THIS_IN_INITIALIZER_LIST(controller_(
[email protected]3d7474ff2011-07-27 17:47:37171 this, browser_context, session_storage_namespace)),
[email protected]66ba4932009-06-04 19:22:13172 ALLOW_THIS_IN_INITIALIZER_LIST(view_(
[email protected]74313b42011-08-24 16:51:32173 content::GetContentClient()->browser()->CreateTabContentsView(this))),
[email protected]d82ed61e2009-06-16 02:46:22174 ALLOW_THIS_IN_INITIALIZER_LIST(render_manager_(this, this)),
[email protected]d5f942ba2008-09-26 19:30:34175 is_loading_(false),
[email protected]443b80e2010-12-14 00:42:23176 crashed_status_(base::TERMINATION_STATUS_STILL_RUNNING),
177 crashed_error_code_(0),
[email protected]d5f942ba2008-09-26 19:30:34178 waiting_for_response_(false),
[email protected]d5f942ba2008-09-26 19:30:34179 max_page_id_(-1),
[email protected]9c235f042011-08-10 22:28:21180 load_state_(net::LOAD_STATE_IDLE, string16()),
[email protected]094e5b22009-09-25 04:23:56181 upload_size_(0),
182 upload_position_(0),
[email protected]f17a0ee2010-05-17 17:38:47183 displayed_insecure_content_(false),
[email protected]fdd61c62009-04-22 19:22:57184 capturing_contents_(false),
185 is_being_destroyed_(false),
186 notify_disconnection_(false),
[email protected]2e5b90c2011-08-16 21:11:55187 dialog_creator_(NULL),
[email protected]fdd61c62009-04-22 19:22:57188#if defined(OS_WIN)
189 message_box_active_(CreateEvent(NULL, TRUE, FALSE, NULL)),
190#endif
[email protected]7ab1e7d62009-10-14 23:32:01191 is_showing_before_unload_dialog_(false),
[email protected]1fd1a502011-03-30 16:55:56192 opener_web_ui_type_(WebUI::kNoWebUI),
[email protected]ebf40a72010-07-22 01:46:38193 closed_by_user_gesture_(false),
[email protected]b75b8292010-10-01 07:28:25194 minimum_zoom_percent_(
195 static_cast<int>(WebKit::WebView::minTextSizeMultiplier * 100)),
196 maximum_zoom_percent_(
197 static_cast<int>(WebKit::WebView::maxTextSizeMultiplier * 100)),
[email protected]9e823662010-10-13 23:36:00198 temporary_zoom_settings_(false),
[email protected]4850a7f2011-03-08 23:36:59199 content_restrictions_(0) {
[email protected]7ff431e2010-01-07 18:18:54200
[email protected]3d7474ff2011-07-27 17:47:37201 render_manager_.Init(browser_context, site_instance, routing_id);
[email protected]420ae012009-04-24 05:16:32202
[email protected]34ac70502009-09-25 17:07:23203 // We have the initial size of the view be based on the size of the passed in
204 // tab contents (normally a tab from the same window).
205 view_->CreateView(base_tab_contents ?
206 base_tab_contents->view()->GetContainerSize() : gfx::Size());
[email protected]332af7732009-03-11 13:21:35207}
initial.commit09911bf2008-07-26 23:55:29208
209TabContents::~TabContents() {
[email protected]420ae012009-04-24 05:16:32210 is_being_destroyed_ = true;
211
[email protected]3ab9cb82011-06-03 18:02:07212 // Clear out any JavaScript state.
[email protected]2e5b90c2011-08-16 21:11:55213 if (dialog_creator_)
214 dialog_creator_->ResetJavaScriptState(this);
[email protected]3ab9cb82011-06-03 18:02:07215
[email protected]420ae012009-04-24 05:16:32216 NotifyDisconnected();
[email protected]420ae012009-04-24 05:16:32217
[email protected]420ae012009-04-24 05:16:32218 // First cleanly close all child windows.
219 // TODO(mpcomplete): handle case if MaybeCloseChildWindows() already asked
220 // some of these to close. CloseWindows is async, so it might get called
221 // twice before it runs.
[email protected]8cc8d492010-02-02 10:40:49222 CloseConstrainedWindows();
[email protected]420ae012009-04-24 05:16:32223
[email protected]420ae012009-04-24 05:16:32224 // Notify any observer that have a reference on this tab contents.
225 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:27226 content::NOTIFICATION_TAB_CONTENTS_DESTROYED,
[email protected]420ae012009-04-24 05:16:32227 Source<TabContents>(this),
228 NotificationService::NoDetails());
229
230 // TODO(brettw) this should be moved to the view.
231#if defined(OS_WIN)
232 // If we still have a window handle, destroy it. GetNativeView can return
233 // NULL if this contents was part of a window that closed.
[email protected]50bd6452010-11-27 19:39:42234 if (GetNativeView()) {
235 RenderViewHost* host = render_view_host();
[email protected]b9a1fb42011-04-12 23:16:44236 if (host && host->view())
[email protected]50bd6452010-11-27 19:39:42237 host->view()->WillWmDestroy();
[email protected]50bd6452010-11-27 19:39:42238 }
[email protected]420ae012009-04-24 05:16:32239#endif
[email protected]7ab1e7d62009-10-14 23:32:01240
241 // OnCloseStarted isn't called in unit tests.
242 if (!tab_close_start_time_.is_null()) {
243 UMA_HISTOGRAM_TIMES("Tab.Close",
244 base::TimeTicks::Now() - tab_close_start_time_);
245 }
[email protected]b5a1d11c2011-02-17 03:09:42246
[email protected]07d490bc2011-03-07 17:05:26247 FOR_EACH_OBSERVER(TabContentsObserver, observers_, TabContentsDestroyed());
[email protected]232a5812011-03-04 22:42:08248
[email protected]1de2b8b2011-06-29 19:38:46249 set_delegate(NULL);
250}
251
252// TODO(cbentzel): Either remove the debugging code, or rename to SetDelegate.
253void TabContents::set_delegate(TabContentsDelegate* delegate) {
254 if (delegate == delegate_)
255 return;
256 if (delegate_)
257 delegate_->Detach(this);
258 delegate_ = delegate;
259 if (delegate_)
260 delegate_->Attach(this);
[email protected]b5a1d11c2011-02-17 03:09:42261}
262
[email protected]724159a2010-12-30 01:11:18263bool TabContents::OnMessageReceived(const IPC::Message& message) {
[email protected]f82d57b52011-04-27 19:13:17264 if (web_ui() && web_ui()->OnMessageReceived(message))
265 return true;
266
[email protected]585b30362011-01-28 02:30:17267 ObserverListBase<TabContentsObserver>::Iterator it(observers_);
268 TabContentsObserver* observer;
[email protected]0f180ee2011-01-21 18:23:10269 while ((observer = it.GetNext()) != NULL)
270 if (observer->OnMessageReceived(message))
[email protected]403415a2011-01-10 18:57:53271 return true;
[email protected]403415a2011-01-10 18:57:53272
[email protected]724159a2010-12-30 01:11:18273 bool handled = true;
274 bool message_is_ok = true;
275 IPC_BEGIN_MESSAGE_MAP_EX(TabContents, message, message_is_ok)
276 IPC_MESSAGE_HANDLER(ViewHostMsg_DidStartProvisionalLoadForFrame,
277 OnDidStartProvisionalLoadForFrame)
278 IPC_MESSAGE_HANDLER(ViewHostMsg_DidRedirectProvisionalLoad,
279 OnDidRedirectProvisionalLoad)
280 IPC_MESSAGE_HANDLER(ViewHostMsg_DidFailProvisionalLoadWithError,
281 OnDidFailProvisionalLoadWithError)
282 IPC_MESSAGE_HANDLER(ViewHostMsg_DidLoadResourceFromMemoryCache,
283 OnDidLoadResourceFromMemoryCache)
284 IPC_MESSAGE_HANDLER(ViewHostMsg_DidDisplayInsecureContent,
285 OnDidDisplayInsecureContent)
286 IPC_MESSAGE_HANDLER(ViewHostMsg_DidRunInsecureContent,
287 OnDidRunInsecureContent)
288 IPC_MESSAGE_HANDLER(ViewHostMsg_DocumentLoadedInFrame,
289 OnDocumentLoadedInFrame)
290 IPC_MESSAGE_HANDLER(ViewHostMsg_DidFinishLoad, OnDidFinishLoad)
[email protected]c8f73ab2011-01-22 00:05:17291 IPC_MESSAGE_HANDLER(ViewHostMsg_UpdateContentRestrictions,
292 OnUpdateContentRestrictions)
[email protected]7d472472011-01-22 01:30:25293 IPC_MESSAGE_HANDLER(ViewHostMsg_GoToEntryAtOffset, OnGoToEntryAtOffset)
[email protected]216813952011-05-19 22:21:26294 IPC_MESSAGE_HANDLER(ViewHostMsg_UpdateZoomLimits, OnUpdateZoomLimits)
295 IPC_MESSAGE_HANDLER(ViewHostMsg_FocusedNodeChanged, OnFocusedNodeChanged)
[email protected]c7dd2f62011-07-18 15:57:59296 IPC_MESSAGE_HANDLER(ViewHostMsg_SaveURLAs, OnSaveURL)
[email protected]724159a2010-12-30 01:11:18297 IPC_MESSAGE_UNHANDLED(handled = false)
298 IPC_END_MESSAGE_MAP_EX()
299
300 if (!message_is_ok) {
301 UserMetrics::RecordAction(UserMetricsAction("BadMessageTerminate_RVD"));
302 GetRenderProcessHost()->ReceivedBadMessage();
303 }
304
305 return handled;
306}
307
[email protected]8cb5d5b2010-02-09 11:36:16308RenderProcessHost* TabContents::GetRenderProcessHost() const {
309 return render_manager_.current_host()->process();
310}
311
[email protected]d5f942ba2008-09-26 19:30:34312const GURL& TabContents::GetURL() const {
313 // We may not have a navigation entry yet
[email protected]ce3fa3c2009-04-20 19:55:57314 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]ebe89e062009-08-13 23:16:54315 return entry ? entry->virtual_url() : GURL::EmptyGURL();
[email protected]d5f942ba2008-09-26 19:30:34316}
317
[email protected]96d185d2009-04-24 03:28:54318const string16& TabContents::GetTitle() const {
[email protected]4c6092c5b2009-06-06 00:23:55319 // Transient entries take precedence. They are used for interstitial pages
320 // that are shown on top of existing pages.
321 NavigationEntry* entry = controller_.GetTransientEntry();
[email protected]b5cca982011-05-26 04:42:08322 std::string accept_languages =
323 content::GetContentClient()->browser()->GetAcceptLangs(this);
[email protected]45d0ef7f2011-01-05 13:46:23324 if (entry) {
[email protected]b5cca982011-05-26 04:42:08325 return entry->GetTitleForDisplay(accept_languages);
[email protected]45d0ef7f2011-01-05 13:46:23326 }
[email protected]7ade2732011-02-10 00:13:58327 WebUI* our_web_ui = render_manager_.pending_web_ui() ?
328 render_manager_.pending_web_ui() : render_manager_.web_ui();
329 if (our_web_ui) {
[email protected]96d185d2009-04-24 03:28:54330 // Don't override the title in view source mode.
[email protected]4c6092c5b2009-06-06 00:23:55331 entry = controller_.GetActiveEntry();
[email protected]96d185d2009-04-24 03:28:54332 if (!(entry && entry->IsViewSourceMode())) {
[email protected]e0112912011-02-02 22:54:35333 // Give the Web UI the chance to override our title.
[email protected]7ade2732011-02-10 00:13:58334 const string16& title = our_web_ui->overridden_title();
[email protected]96d185d2009-04-24 03:28:54335 if (!title.empty())
336 return title;
337 }
338 }
339
340 // We use the title for the last committed entry rather than a pending
341 // navigation entry. For example, when the user types in a URL, we want to
342 // keep the old page's title until the new load has committed and we get a new
343 // title.
[email protected]96d185d2009-04-24 03:28:54344 entry = controller_.GetLastCommittedEntry();
[email protected]45d0ef7f2011-01-05 13:46:23345 if (entry) {
[email protected]b5cca982011-05-26 04:42:08346 return entry->GetTitleForDisplay(accept_languages);
[email protected]45d0ef7f2011-01-05 13:46:23347 }
[email protected]987fc3a2011-05-26 14:18:09348
349 // |page_title_when_no_navigation_entry_| is finally used
350 // if no title cannot be retrieved.
351 return page_title_when_no_navigation_entry_;
[email protected]96d185d2009-04-24 03:28:54352}
353
[email protected]d5f942ba2008-09-26 19:30:34354int32 TabContents::GetMaxPageID() {
355 if (GetSiteInstance())
356 return GetSiteInstance()->max_page_id();
357 else
358 return max_page_id_;
359}
360
361void TabContents::UpdateMaxPageID(int32 page_id) {
362 // Ensure both the SiteInstance and RenderProcessHost update their max page
[email protected]57c6a652009-05-04 07:58:34363 // IDs in sync. Only TabContents will also have site instances, except during
[email protected]d5f942ba2008-09-26 19:30:34364 // testing.
365 if (GetSiteInstance())
366 GetSiteInstance()->UpdateMaxPageID(page_id);
[email protected]8cb5d5b2010-02-09 11:36:16367 GetRenderProcessHost()->UpdateMaxPageID(page_id);
[email protected]d5f942ba2008-09-26 19:30:34368}
369
[email protected]96d185d2009-04-24 03:28:54370SiteInstance* TabContents::GetSiteInstance() const {
371 return render_manager_.current_host()->site_instance();
372}
373
[email protected]77362eb2011-08-01 17:18:38374SiteInstance* TabContents::GetPendingSiteInstance() const {
375 RenderViewHost* dest_rvh = render_manager_.pending_render_view_host() ?
376 render_manager_.pending_render_view_host() :
377 render_manager_.current_host();
378 return dest_rvh->site_instance();
379}
380
[email protected]96d185d2009-04-24 03:28:54381bool TabContents::ShouldDisplayURL() {
[email protected]8ab24cc2009-11-13 20:56:15382 // Don't hide the url in view source mode and with interstitials.
[email protected]96d185d2009-04-24 03:28:54383 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]8ab24cc2009-11-13 20:56:15384 if (entry && (entry->IsViewSourceMode() ||
[email protected]cccd3762010-11-12 18:40:01385 entry->page_type() == INTERSTITIAL_PAGE)) {
[email protected]96d185d2009-04-24 03:28:54386 return true;
[email protected]8ab24cc2009-11-13 20:56:15387 }
[email protected]83e3895b2009-06-11 00:07:16388
[email protected]2b9a06402011-02-16 04:57:10389 // We always display the URL for non-WebUI URLs to prevent spoofing.
[email protected]1fd1a502011-03-30 16:55:56390 if (entry && !content::WebUIFactory::Get()->HasWebUIScheme(entry->url()))
[email protected]2b9a06402011-02-16 04:57:10391 return true;
392
[email protected]d0980792011-02-13 19:41:40393 WebUI* web_ui = GetWebUIForCurrentState();
[email protected]7ade2732011-02-10 00:13:58394 if (web_ui)
395 return !web_ui->should_hide_url();
[email protected]96d185d2009-04-24 03:28:54396 return true;
397}
398
[email protected]f5d978c2011-07-21 14:43:51399bool TabContents::IsLoading() const {
400 return is_loading_ || (web_ui() && web_ui()->IsLoading());
401}
402
[email protected]585b30362011-01-28 02:30:17403void TabContents::AddObserver(TabContentsObserver* observer) {
404 observers_.AddObserver(observer);
[email protected]3c9e1872010-11-18 16:17:49405}
406
[email protected]585b30362011-01-28 02:30:17407void TabContents::RemoveObserver(TabContentsObserver* observer) {
408 observers_.RemoveObserver(observer);
[email protected]3c9e1872010-11-18 16:17:49409}
410
[email protected]443b80e2010-12-14 00:42:23411void TabContents::SetIsCrashed(base::TerminationStatus status, int error_code) {
412 if (status == crashed_status_)
[email protected]d5f942ba2008-09-26 19:30:34413 return;
414
[email protected]443b80e2010-12-14 00:42:23415 crashed_status_ = status;
416 crashed_error_code_ = error_code;
[email protected]c9cd2222009-05-06 05:16:50417 NotifyNavigationStateChanged(INVALIDATE_TAB);
[email protected]d5f942ba2008-09-26 19:30:34418}
419
420void TabContents::NotifyNavigationStateChanged(unsigned changed_flags) {
421 if (delegate_)
422 delegate_->NavigationStateChanged(this, changed_flags);
423}
424
[email protected]96d185d2009-04-24 03:28:54425void TabContents::DidBecomeSelected() {
426 controller_.SetActive(true);
[email protected]8cb5d5b2010-02-09 11:36:16427 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
428 if (rwhv) {
429 rwhv->DidBecomeSelected();
[email protected]789e9152009-08-04 21:59:43430#if defined(OS_MACOSX)
[email protected]8cb5d5b2010-02-09 11:36:16431 rwhv->SetActive(true);
[email protected]789e9152009-08-04 21:59:43432#endif
433 }
[email protected]96d185d2009-04-24 03:28:54434
[email protected]5ac20162010-11-24 23:33:11435 last_selected_time_ = base::TimeTicks::Now();
[email protected]3e69bc82011-05-26 23:22:38436
437 FOR_EACH_OBSERVER(TabContentsObserver, observers_, DidBecomeSelected());
[email protected]96d185d2009-04-24 03:28:54438}
439
440void TabContents::WasHidden() {
441 if (!capturing_contents()) {
442 // |render_view_host()| can be NULL if the user middle clicks a link to open
443 // a tab in then background, then closes the tab before selecting it. This
444 // is because closing the tab calls TabContents::Destroy(), which removes
445 // the |render_view_host()|; then when we actually destroy the window,
446 // OnWindowPosChanged() notices and calls HideContents() (which calls us).
[email protected]8cb5d5b2010-02-09 11:36:16447 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
448 if (rwhv)
449 rwhv->WasHidden();
[email protected]96d185d2009-04-24 03:28:54450 }
451
452 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:27453 content::NOTIFICATION_TAB_CONTENTS_HIDDEN,
[email protected]96d185d2009-04-24 03:28:54454 Source<TabContents>(this),
455 NotificationService::NoDetails());
456}
457
[email protected]d5f942ba2008-09-26 19:30:34458void TabContents::Activate() {
459 if (delegate_)
460 delegate_->ActivateContents(this);
461}
462
[email protected]ea42e7782010-08-23 23:58:12463void TabContents::Deactivate() {
464 if (delegate_)
465 delegate_->DeactivateContents(this);
466}
467
[email protected]63954792011-07-11 04:17:48468void TabContents::LostCapture() {
469 if (delegate_)
470 delegate_->LostCapture();
471}
472
473bool TabContents::PreHandleKeyboardEvent(const NativeWebKeyboardEvent& event,
474 bool* is_keyboard_shortcut) {
475 return delegate_ &&
476 delegate_->PreHandleKeyboardEvent(event, is_keyboard_shortcut);
477}
478
479void TabContents::HandleKeyboardEvent(const NativeWebKeyboardEvent& event) {
480 if (delegate_)
481 delegate_->HandleKeyboardEvent(event);
482}
483
484void TabContents::HandleMouseUp() {
485 if (delegate_)
486 delegate_->HandleMouseUp();
487}
488
489void TabContents::HandleMouseActivate() {
490 if (delegate_)
491 delegate_->HandleMouseActivate();
492}
493
[email protected]96d185d2009-04-24 03:28:54494void TabContents::ShowContents() {
[email protected]8cb5d5b2010-02-09 11:36:16495 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
496 if (rwhv)
497 rwhv->DidBecomeSelected();
[email protected]96d185d2009-04-24 03:28:54498}
499
500void TabContents::HideContents() {
501 // TODO(pkasting): http://b/1239839 Right now we purposefully don't call
502 // our superclass HideContents(), because some callers want to be very picky
503 // about the order in which these get called. In addition to making the code
504 // here practically impossible to understand, this also means we end up
505 // calling TabContents::WasHidden() twice if callers call both versions of
[email protected]57c6a652009-05-04 07:58:34506 // HideContents() on a TabContents.
[email protected]96d185d2009-04-24 03:28:54507 WasHidden();
508}
509
[email protected]5aab5e22010-12-08 22:13:29510bool TabContents::NeedToFireBeforeUnload() {
511 // TODO(creis): Should we fire even for interstitial pages?
512 return notify_disconnection() &&
513 !showing_interstitial_page() &&
514 !render_view_host()->SuddenTerminationAllowed();
515}
516
[email protected]00c37fc2011-08-02 00:22:50517// TODO(adriansc): Remove this method once refactoring changed all call sites.
[email protected]992e4542011-07-20 23:09:25518TabContents* TabContents::OpenURL(const GURL& url,
519 const GURL& referrer,
520 WindowOpenDisposition disposition,
521 PageTransition::Type transition) {
[email protected]00c37fc2011-08-02 00:22:50522 return OpenURL(OpenURLParams(url, referrer, disposition, transition));
523}
524
525TabContents* TabContents::OpenURL(const OpenURLParams& params) {
[email protected]1c642b52011-04-15 09:05:49526 if (delegate_) {
[email protected]00c37fc2011-08-02 00:22:50527 TabContents* new_contents = delegate_->OpenURLFromTab(this, params);
[email protected]1c642b52011-04-15 09:05:49528 // Notify observers.
529 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
[email protected]00c37fc2011-08-02 00:22:50530 DidOpenURL(params.url, params.referrer,
531 params.disposition, params.transition));
[email protected]992e4542011-07-20 23:09:25532 return new_contents;
[email protected]1c642b52011-04-15 09:05:49533 }
[email protected]992e4542011-07-20 23:09:25534 return NULL;
[email protected]d5f942ba2008-09-26 19:30:34535}
536
[email protected]1ccb3568d2010-02-19 10:51:16537bool TabContents::NavigateToPendingEntry(
538 NavigationController::ReloadType reload_type) {
[email protected]876bc832010-09-07 16:29:54539 return NavigateToEntry(*controller_.pending_entry(), reload_type);
540}
[email protected]96d185d2009-04-24 03:28:54541
[email protected]876bc832010-09-07 16:29:54542bool TabContents::NavigateToEntry(
543 const NavigationEntry& entry,
544 NavigationController::ReloadType reload_type) {
[email protected]96d185d2009-04-24 03:28:54545 RenderViewHost* dest_render_view_host = render_manager_.Navigate(entry);
546 if (!dest_render_view_host)
547 return false; // Unable to create the desired render view host.
548
[email protected]80a8fad2011-01-29 04:02:38549 // For security, we should never send non-Web-UI URLs to a Web UI renderer.
[email protected]05fdd492010-11-15 17:52:07550 // Double check that here.
[email protected]54ec6472010-04-09 19:39:58551 int enabled_bindings = dest_render_view_host->enabled_bindings();
[email protected]1fd1a502011-03-30 16:55:56552 bool is_allowed_in_web_ui_renderer = content::GetContentClient()->
[email protected]3d7474ff2011-07-27 17:47:37553 browser()->GetWebUIFactory()->IsURLAcceptableForWebUI(browser_context(),
[email protected]1fd1a502011-03-30 16:55:56554 entry.url());
[email protected]c09163a2011-02-15 00:05:55555 CHECK(!BindingsPolicy::is_web_ui_enabled(enabled_bindings) ||
556 is_allowed_in_web_ui_renderer);
[email protected]54ec6472010-04-09 19:39:58557
[email protected]96d185d2009-04-24 03:28:54558 // Tell DevTools agent that it is attached prior to the navigation.
[email protected]d9f9b792009-06-24 13:17:12559 DevToolsManager* devtools_manager = DevToolsManager::GetInstance();
[email protected]7e8e6b62009-05-08 11:28:32560 if (devtools_manager) { // NULL in unit tests.
[email protected]3cc72b12010-03-18 23:03:00561 devtools_manager->OnNavigatingToPendingEntry(render_view_host(),
562 dest_render_view_host,
563 entry.url());
[email protected]7e8e6b62009-05-08 11:28:32564 }
[email protected]96d185d2009-04-24 03:28:54565
566 // Used for page load time metrics.
567 current_load_start_ = base::TimeTicks::Now();
568
569 // Navigate in the desired RenderViewHost.
[email protected]056de2d2009-06-26 16:41:34570 ViewMsg_Navigate_Params navigate_params;
[email protected]876bc832010-09-07 16:29:54571 MakeNavigateParams(entry, controller_, reload_type, &navigate_params);
[email protected]52c68652010-12-07 17:47:04572 if (delegate_) {
573 navigate_params.extra_headers =
574 delegate_->GetNavigationHeaders(navigate_params.url);
575 }
[email protected]056de2d2009-06-26 16:41:34576 dest_render_view_host->Navigate(navigate_params);
[email protected]96d185d2009-04-24 03:28:54577
578 if (entry.page_id() == -1) {
579 // HACK!! This code suppresses javascript: URLs from being added to
580 // session history, which is what we want to do for javascript: URLs that
581 // do not generate content. What we really need is a message from the
582 // renderer telling us that a new page was not created. The same message
583 // could be used for mailto: URLs and the like.
584 if (entry.url().SchemeIs(chrome::kJavaScriptScheme))
585 return false;
586 }
587
[email protected]3c9e1872010-11-18 16:17:49588 // Notify observers about navigation.
[email protected]b375c5d2011-05-03 21:15:04589 FOR_EACH_OBSERVER(TabContentsObserver,
590 observers_,
591 NavigateToPendingEntry(entry.url(), reload_type));
[email protected]96d185d2009-04-24 03:28:54592
[email protected]09b29342011-06-24 19:18:48593 if (delegate_)
594 delegate_->DidNavigateToPendingEntry(this);
595
[email protected]d5f942ba2008-09-26 19:30:34596 return true;
597}
598
[email protected]9e1ad4b2011-08-14 16:49:19599void TabContents::SetHistoryLengthAndPrune(const SiteInstance* site_instance,
600 int history_length,
601 int32 minimum_page_id) {
[email protected]5229253b2011-08-16 14:36:41602 // SetHistoryLengthAndPrune doesn't work when there are pending cross-site
603 // navigations. Callers should ensure that this is the case.
[email protected]9e1ad4b2011-08-14 16:49:19604 if (render_manager_.pending_render_view_host()) {
605 NOTREACHED();
[email protected]796931a92011-08-10 01:32:14606 return;
[email protected]9e1ad4b2011-08-14 16:49:19607 }
608 RenderViewHost* rvh = render_view_host();
609 if (!rvh) {
610 NOTREACHED();
611 return;
612 }
613 if (site_instance && rvh->site_instance() != site_instance) {
614 NOTREACHED();
615 return;
616 }
617 rvh->Send(new ViewMsg_SetHistoryLengthAndPrune(rvh->routing_id(),
618 history_length,
619 minimum_page_id));
[email protected]796931a92011-08-10 01:32:14620}
621
[email protected]96d185d2009-04-24 03:28:54622void TabContents::Stop() {
623 render_manager_.Stop();
[email protected]2e3f4572011-03-25 19:24:47624 FOR_EACH_OBSERVER(TabContentsObserver, observers_, StopNavigation());
[email protected]96d185d2009-04-24 03:28:54625}
626
[email protected]96d185d2009-04-24 03:28:54627TabContents* TabContents::Clone() {
628 // We create a new SiteInstance so that the new tab won't share processes
629 // with the old one. This can be changed in the future if we need it to share
630 // processes for some reason.
[email protected]3d7474ff2011-07-27 17:47:37631 TabContents* tc = new TabContents(
632 browser_context(),
633 SiteInstance::CreateSiteInstance(browser_context()),
634 MSG_ROUTING_NONE, this, NULL);
[email protected]96d185d2009-04-24 03:28:54635 tc->controller().CopyStateFrom(controller_);
636 return tc;
637}
638
[email protected]4d677202009-07-19 07:37:12639void TabContents::ShowPageInfo(const GURL& url,
640 const NavigationEntry::SSLStatus& ssl,
641 bool show_history) {
642 if (!delegate_)
643 return;
644
[email protected]3d7474ff2011-07-27 17:47:37645 delegate_->ShowPageInfo(browser_context(), url, ssl, show_history);
[email protected]4d677202009-07-19 07:37:12646}
647
initial.commit09911bf2008-07-26 23:55:29648ConstrainedWindow* TabContents::CreateConstrainedDialog(
[email protected]e8382172009-06-19 22:16:28649 ConstrainedWindowDelegate* delegate) {
initial.commit09911bf2008-07-26 23:55:29650 ConstrainedWindow* window =
[email protected]e8382172009-06-19 22:16:28651 ConstrainedWindow::CreateConstrainedDialog(this, delegate);
[email protected]aed59602011-02-28 22:57:33652 AddConstrainedDialog(window);
653 return window;
654}
655
656void TabContents::AddConstrainedDialog(ConstrainedWindow* window) {
initial.commit09911bf2008-07-26 23:55:29657 child_windows_.push_back(window);
[email protected]fa1cf0b82010-01-15 21:49:44658
659 if (child_windows_.size() == 1) {
660 window->ShowConstrainedWindow();
661 BlockTabContent(true);
662 }
initial.commit09911bf2008-07-26 23:55:29663}
664
[email protected]fa1cf0b82010-01-15 21:49:44665void TabContents::BlockTabContent(bool blocked) {
[email protected]8dccd7cb2010-02-25 05:19:55666 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
[email protected]1b072562010-12-13 19:37:45667 // 70% opaque grey.
668 SkColor greyish = SkColorSetARGB(178, 0, 0, 0);
[email protected]8dccd7cb2010-02-25 05:19:55669 if (rwhv)
[email protected]1b072562010-12-13 19:37:45670 rwhv->SetVisuallyDeemphasized(blocked ? &greyish : NULL, false);
[email protected]a7eccac2011-04-15 03:05:19671 // RenderViewHost may be NULL during shutdown.
672 if (render_view_host())
673 render_view_host()->set_ignore_input_events(blocked);
[email protected]fa1cf0b82010-01-15 21:49:44674 if (delegate_)
675 delegate_->SetTabContentBlocked(this, blocked);
676}
677
[email protected]e7cfdbd2011-04-22 14:41:37678void TabContents::AddNewContents(TabContents* new_contents,
679 WindowOpenDisposition disposition,
680 const gfx::Rect& initial_pos,
681 bool user_gesture) {
initial.commit09911bf2008-07-26 23:55:29682 if (!delegate_)
683 return;
684
[email protected]e7cfdbd2011-04-22 14:41:37685 delegate_->AddNewContents(this, new_contents, disposition, initial_pos,
686 user_gesture);
[email protected]96d185d2009-04-24 03:28:54687}
688
[email protected]5c9e97a2009-09-09 23:48:30689gfx::NativeView TabContents::GetContentNativeView() const {
[email protected]96d185d2009-04-24 03:28:54690 return view_->GetContentNativeView();
691}
692
693gfx::NativeView TabContents::GetNativeView() const {
694 return view_->GetNativeView();
695}
696
697void TabContents::GetContainerBounds(gfx::Rect *out) const {
698 view_->GetContainerBounds(out);
699}
700
701void TabContents::Focus() {
702 view_->Focus();
703}
704
[email protected]90daadb42009-06-08 21:27:28705void TabContents::FocusThroughTabTraversal(bool reverse) {
[email protected]7e383692009-06-12 19:14:54706 if (showing_interstitial_page()) {
707 render_manager_.interstitial_page()->FocusThroughTabTraversal(reverse);
708 return;
709 }
[email protected]96d185d2009-04-24 03:28:54710 render_view_host()->SetInitialFocus(reverse);
711}
712
713bool TabContents::FocusLocationBarByDefault() {
[email protected]d0980792011-02-13 19:41:40714 WebUI* web_ui = GetWebUIForCurrentState();
[email protected]7ade2732011-02-10 00:13:58715 if (web_ui)
716 return web_ui->focus_location_bar_by_default();
[email protected]3e3f0eb2009-06-22 18:33:43717 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]e0d481582009-09-15 21:06:25718 if (entry && entry->url() == GURL(chrome::kAboutBlankURL))
[email protected]3e3f0eb2009-06-22 18:33:43719 return true;
[email protected]96d185d2009-04-24 03:28:54720 return false;
721}
722
[email protected]a26dc362010-04-23 01:48:58723void TabContents::SetFocusToLocationBar(bool select_all) {
[email protected]a11aaf22010-03-30 00:03:38724 if (delegate())
[email protected]a26dc362010-04-23 01:48:58725 delegate()->SetFocusToLocationBar(select_all);
[email protected]a11aaf22010-03-30 00:03:38726}
727
[email protected]686493142011-07-15 21:47:22728bool TabContents::CanDownload(int request_id) {
729 TabContentsDelegate* d = delegate();
730 if (d)
731 return d->CanDownload(this, request_id);
732 return true;
733}
734
735void TabContents::OnStartDownload(DownloadItem* download) {
736 TabContentsDelegate* d = delegate();
737 if (d)
738 d->OnStartDownload(this, download);
739}
740
[email protected]d5f942ba2008-09-26 19:30:34741void TabContents::WillClose(ConstrainedWindow* window) {
[email protected]d6219912010-11-17 19:47:37742 ConstrainedWindowList::iterator i(
743 std::find(child_windows_.begin(), child_windows_.end(), window));
744 bool removed_topmost_window = i == child_windows_.begin();
745 if (i != child_windows_.end())
746 child_windows_.erase(i);
747 if (child_windows_.empty()) {
[email protected]fa1cf0b82010-01-15 21:49:44748 BlockTabContent(false);
[email protected]d6219912010-11-17 19:47:37749 } else {
750 if (removed_topmost_window)
751 child_windows_[0]->ShowConstrainedWindow();
752 BlockTabContent(true);
[email protected]fa1cf0b82010-01-15 21:49:44753 }
[email protected]66ba4932009-06-04 19:22:13754}
[email protected]d5f942ba2008-09-26 19:30:34755
[email protected]c7dd2f62011-07-18 15:57:59756void TabContents::OnSavePage() {
757 // If we can not save the page, try to download it.
758 if (!SavePackage::IsSavableContents(contents_mime_type())) {
[email protected]3d7474ff2011-07-27 17:47:37759 DownloadManager* dlm = browser_context()->GetDownloadManager();
[email protected]c7dd2f62011-07-18 15:57:59760 const GURL& current_page_url = GetURL();
761 if (dlm && current_page_url.is_valid()) {
762 dlm->DownloadUrl(current_page_url, GURL(), "", this);
[email protected]8bd9e562011-08-16 23:55:46763 download_stats::RecordDownloadCount(
764 download_stats::INITIATED_BY_SAVE_PACKAGE_FAILURE_COUNT);
[email protected]c7dd2f62011-07-18 15:57:59765 return;
766 }
767 }
768
769 Stop();
770
771 // Create the save package and possibly prompt the user for the name to save
772 // the page as. The user prompt is an asynchronous operation that runs on
773 // another thread.
774 save_package_ = new SavePackage(this);
775 save_package_->GetSaveInfo();
776}
777
778// Used in automated testing to bypass prompting the user for file names.
779// Instead, the names and paths are hard coded rather than running them through
780// file name sanitation and extension / mime checking.
781bool TabContents::SavePage(const FilePath& main_file, const FilePath& dir_path,
782 SavePackage::SavePackageType save_type) {
783 // Stop the page from navigating.
784 Stop();
785
786 save_package_ = new SavePackage(this, save_type, main_file, dir_path);
787 return save_package_->Init();
788}
789
790void TabContents::OnSaveURL(const GURL& url) {
[email protected]3d7474ff2011-07-27 17:47:37791 DownloadManager* dlm = browser_context()->GetDownloadManager();
[email protected]c7dd2f62011-07-18 15:57:59792 dlm->DownloadUrl(url, GetURL(), "", this);
793}
794
[email protected]420ae012009-04-24 05:16:32795bool TabContents::IsActiveEntry(int32 page_id) {
796 NavigationEntry* active_entry = controller_.GetActiveEntry();
797 return (active_entry != NULL &&
798 active_entry->site_instance() == GetSiteInstance() &&
799 active_entry->page_id() == page_id);
800}
801
[email protected]b2fe07d12010-02-09 14:38:08802void TabContents::SetOverrideEncoding(const std::string& encoding) {
[email protected]8cb5d5b2010-02-09 11:36:16803 set_encoding(encoding);
[email protected]216813952011-05-19 22:21:26804 render_view_host()->Send(new ViewMsg_SetPageEncoding(
805 render_view_host()->routing_id(), encoding));
[email protected]8cb5d5b2010-02-09 11:36:16806}
807
[email protected]b2fe07d12010-02-09 14:38:08808void TabContents::ResetOverrideEncoding() {
[email protected]8cb5d5b2010-02-09 11:36:16809 reset_encoding();
[email protected]216813952011-05-19 22:21:26810 render_view_host()->Send(new ViewMsg_ResetPageEncodingToDefault(
811 render_view_host()->routing_id()));
[email protected]8cb5d5b2010-02-09 11:36:16812}
813
[email protected]7ab1e7d62009-10-14 23:32:01814void TabContents::OnCloseStarted() {
815 if (tab_close_start_time_.is_null())
816 tab_close_start_time_ = base::TimeTicks::Now();
817}
818
[email protected]46624bf2010-06-09 16:04:19819bool TabContents::ShouldAcceptDragAndDrop() const {
820#if defined(OS_CHROMEOS)
821 // ChromeOS panels (pop-ups) do not take drag-n-drop.
822 // See http://crosbug.com/2413
[email protected]b35b26b32011-05-05 20:35:14823 if (delegate() && delegate()->IsPopupOrPanel(this))
[email protected]b83af492010-10-09 03:21:20824 return false;
825 return true;
[email protected]46624bf2010-06-09 16:04:19826#else
827 return true;
828#endif
829}
830
[email protected]7813bd72011-02-05 02:19:34831void TabContents::SystemDragEnded() {
832 if (render_view_host())
833 render_view_host()->DragSourceSystemDragEnded();
834 if (delegate())
835 delegate()->DragEnded();
836}
837
[email protected]d0b8d092010-10-25 04:05:17838double TabContents::GetZoomLevel() const {
[email protected]3d7474ff2011-07-27 17:47:37839 HostZoomMap* zoom_map = browser_context()->GetHostZoomMap();
[email protected]b75b8292010-10-01 07:28:25840 if (!zoom_map)
[email protected]d0b8d092010-10-25 04:05:17841 return 0;
[email protected]b75b8292010-10-01 07:28:25842
843 double zoom_level;
844 if (temporary_zoom_settings_) {
845 zoom_level = zoom_map->GetTemporaryZoomLevel(
846 GetRenderProcessHost()->id(), render_view_host()->routing_id());
847 } else {
[email protected]13ffa32e2011-05-27 16:37:01848 zoom_level = zoom_map->GetZoomLevel(net::GetHostOrSpecFromURL(GetURL()));
[email protected]b75b8292010-10-01 07:28:25849 }
[email protected]d0b8d092010-10-25 04:05:17850 return zoom_level;
851}
[email protected]b75b8292010-10-01 07:28:25852
[email protected]d0b8d092010-10-25 04:05:17853int TabContents::GetZoomPercent(bool* enable_increment,
854 bool* enable_decrement) {
855 *enable_decrement = *enable_increment = false;
[email protected]b75b8292010-10-01 07:28:25856 int percent = static_cast<int>(
[email protected]d0b8d092010-10-25 04:05:17857 WebKit::WebView::zoomLevelToZoomFactor(GetZoomLevel()) * 100);
[email protected]b75b8292010-10-01 07:28:25858 *enable_decrement = percent > minimum_zoom_percent_;
859 *enable_increment = percent < maximum_zoom_percent_;
860 return percent;
861}
862
[email protected]3c733bde2010-12-21 19:56:31863void TabContents::ViewSource() {
[email protected]1788e772010-12-15 16:40:50864 if (!delegate_)
865 return;
866
867 NavigationEntry* active_entry = controller().GetActiveEntry();
868 if (!active_entry)
869 return;
870
871 delegate_->ViewSourceForTab(this, active_entry->url());
[email protected]77d8d622010-12-15 10:30:12872}
873
[email protected]932b7a12011-03-09 12:50:27874void TabContents::ViewFrameSource(const GURL& url,
875 const std::string& content_state) {
876 if (!delegate_)
877 return;
878
879 delegate_->ViewSourceForFrame(this, url, content_state);
880}
881
[email protected]c40d6232011-03-25 00:16:21882void TabContents::SetContentRestrictions(int restrictions) {
883 content_restrictions_ = restrictions;
884 delegate()->ContentRestrictionsChanged(this);
885}
886
[email protected]724159a2010-12-30 01:11:18887void TabContents::OnDidStartProvisionalLoadForFrame(int64 frame_id,
888 bool is_main_frame,
[email protected]eacb080b2011-05-22 19:40:26889 bool has_opener_set,
[email protected]724159a2010-12-30 01:11:18890 const GURL& url) {
891 bool is_error_page = (url.spec() == chrome::kUnreachableWebDataURL);
892 GURL validated_url(url);
893 render_view_host()->FilterURL(ChildProcessSecurityPolicy::GetInstance(),
894 GetRenderProcessHost()->id(), &validated_url);
895
[email protected]8093a542011-05-13 07:29:32896 RenderViewHost* rvh =
897 render_manager_.pending_render_view_host() ?
898 render_manager_.pending_render_view_host() : render_view_host();
[email protected]0d60f0192011-04-14 12:40:10899 // Notify observers about the start of the provisional load.
900 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
901 DidStartProvisionalLoadForFrame(frame_id, is_main_frame,
[email protected]8093a542011-05-13 07:29:32902 validated_url, is_error_page, rvh));
[email protected]0d60f0192011-04-14 12:40:10903
[email protected]724159a2010-12-30 01:11:18904 if (is_main_frame) {
[email protected]4850a7f2011-03-08 23:36:59905 // Notify observers about the provisional change in the main frame URL.
906 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
[email protected]eacb080b2011-05-22 19:40:26907 ProvisionalChangeToMainFrameUrl(url, has_opener_set));
[email protected]724159a2010-12-30 01:11:18908 }
909}
910
911void TabContents::OnDidRedirectProvisionalLoad(int32 page_id,
[email protected]eacb080b2011-05-22 19:40:26912 bool has_opener_set,
[email protected]724159a2010-12-30 01:11:18913 const GURL& source_url,
914 const GURL& target_url) {
[email protected]18ba2b12011-04-06 16:35:49915 // TODO(creis): Remove this method and have the pre-rendering code listen to
916 // the ResourceDispatcherHost's RESOURCE_RECEIVED_REDIRECT notification
917 // instead. See http://crbug.com/78512.
[email protected]724159a2010-12-30 01:11:18918 NavigationEntry* entry;
919 if (page_id == -1)
920 entry = controller_.pending_entry();
921 else
922 entry = controller_.GetEntryWithPageID(GetSiteInstance(), page_id);
923 if (!entry || entry->url() != source_url)
924 return;
[email protected]e7d50892011-01-19 21:47:38925
[email protected]4850a7f2011-03-08 23:36:59926 // Notify observers about the provisional change in the main frame URL.
927 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
[email protected]eacb080b2011-05-22 19:40:26928 ProvisionalChangeToMainFrameUrl(target_url,
929 has_opener_set));
[email protected]724159a2010-12-30 01:11:18930}
931
932void TabContents::OnDidFailProvisionalLoadWithError(
933 int64 frame_id,
934 bool is_main_frame,
935 int error_code,
936 const GURL& url,
937 bool showing_repost_interstitial) {
938 VLOG(1) << "Failed Provisional Load: " << url.possibly_invalid_spec()
939 << ", error_code: " << error_code
940 << " is_main_frame: " << is_main_frame
941 << " showing_repost_interstitial: " << showing_repost_interstitial
942 << " frame_id: " << frame_id;
943 GURL validated_url(url);
944 render_view_host()->FilterURL(ChildProcessSecurityPolicy::GetInstance(),
945 GetRenderProcessHost()->id(), &validated_url);
946
947 if (net::ERR_ABORTED == error_code) {
948 // EVIL HACK ALERT! Ignore failed loads when we're showing interstitials.
949 // This means that the interstitial won't be torn down properly, which is
950 // bad. But if we have an interstitial, go back to another tab type, and
951 // then load the same interstitial again, we could end up getting the first
952 // interstitial's "failed" message (as a result of the cancel) when we're on
953 // the second one.
954 //
955 // We can't tell this apart, so we think we're tearing down the current page
956 // which will cause a crash later one. There is also some code in
957 // RenderViewHostManager::RendererAbortedProvisionalLoad that is commented
958 // out because of this problem.
959 //
960 // http://code.google.com/p/chromium/issues/detail?id=2855
961 // Because this will not tear down the interstitial properly, if "back" is
962 // back to another tab type, the interstitial will still be somewhat alive
963 // in the previous tab type. If you navigate somewhere that activates the
964 // tab with the interstitial again, you'll see a flash before the new load
965 // commits of the interstitial page.
966 if (showing_interstitial_page()) {
967 LOG(WARNING) << "Discarding message during interstitial.";
968 return;
969 }
970
[email protected]02102f82011-06-13 20:37:02971 // Discard our pending entry if the load canceled (e.g. if we decided to
972 // download the file instead of load it). We do not verify that the URL
973 // being canceled matches the pending entry's URL because they will not
974 // match if a redirect occurred (in which case we do not want to leave a
975 // stale redirect URL showing). This means that we also cancel the pending
976 // entry if the user started a new navigation. As a result, the navigation
977 // controller may not remember that a load is in progress, but the
978 // navigation will still commit even if there is no pending entry.
[email protected]724159a2010-12-30 01:11:18979 NavigationEntry* pending_entry = controller_.pending_entry();
[email protected]02102f82011-06-13 20:37:02980 if (pending_entry)
[email protected]c95fa8b2011-04-28 20:26:16981 DidCancelLoading();
[email protected]724159a2010-12-30 01:11:18982
983 render_manager_.RendererAbortedProvisionalLoad(render_view_host());
984 }
985
986 // Send out a notification that we failed a provisional load with an error.
987 ProvisionalLoadDetails details(
988 is_main_frame, controller_.IsURLInPageNavigation(validated_url),
[email protected]b9e8ea62011-03-04 06:29:09989 validated_url, std::string(), false, frame_id);
[email protected]724159a2010-12-30 01:11:18990 details.set_error_code(error_code);
991
992 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:27993 content::NOTIFICATION_FAIL_PROVISIONAL_LOAD_WITH_ERROR,
[email protected]724159a2010-12-30 01:11:18994 Source<NavigationController>(&controller_),
995 Details<ProvisionalLoadDetails>(&details));
[email protected]0d60f0192011-04-14 12:40:10996
997 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
998 DidFailProvisionalLoad(frame_id, is_main_frame,
999 validated_url, error_code));
[email protected]724159a2010-12-30 01:11:181000}
1001
1002void TabContents::OnDidLoadResourceFromMemoryCache(
1003 const GURL& url,
[email protected]70435962011-08-02 20:13:281004 const std::string& security_info,
1005 const std::string& http_method,
1006 ResourceType::Type resource_type) {
[email protected]28620862011-03-22 23:07:191007 base::StatsCounter cache("WebKit.CacheHit");
[email protected]724159a2010-12-30 01:11:181008 cache.Increment();
1009
1010 // Send out a notification that we loaded a resource from our memory cache.
1011 int cert_id = 0, cert_status = 0, security_bits = -1, connection_status = 0;
1012 SSLManager::DeserializeSecurityInfo(security_info,
1013 &cert_id, &cert_status,
1014 &security_bits,
1015 &connection_status);
[email protected]7a8c55e2011-02-15 08:21:161016 LoadFromMemoryCacheDetails details(url, GetRenderProcessHost()->id(),
1017 cert_id, cert_status);
[email protected]724159a2010-12-30 01:11:181018
1019 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271020 content::NOTIFICATION_LOAD_FROM_MEMORY_CACHE,
[email protected]724159a2010-12-30 01:11:181021 Source<NavigationController>(&controller_),
1022 Details<LoadFromMemoryCacheDetails>(&details));
1023}
1024
1025void TabContents::OnDidDisplayInsecureContent() {
[email protected]221a5d92011-04-28 18:23:161026 UserMetrics::RecordAction(UserMetricsAction("SSL.DisplayedInsecureContent"));
[email protected]724159a2010-12-30 01:11:181027 displayed_insecure_content_ = true;
[email protected]9678d8f2011-08-05 04:05:071028 SSLManager::NotifySSLInternalStateChanged(&controller());
[email protected]724159a2010-12-30 01:11:181029}
1030
1031void TabContents::OnDidRunInsecureContent(
[email protected]92771112011-01-20 00:13:021032 const std::string& security_origin, const GURL& target_url) {
[email protected]9941cf52011-02-08 16:17:231033 LOG(INFO) << security_origin << " ran insecure content from "
1034 << target_url.possibly_invalid_spec();
[email protected]221a5d92011-04-28 18:23:161035 UserMetrics::RecordAction(UserMetricsAction("SSL.RanInsecureContent"));
[email protected]ca406032011-07-19 21:53:051036 if (EndsWith(security_origin, kDotGoogleDotCom, false)) {
1037 UserMetrics::RecordAction(
1038 UserMetricsAction("SSL.RanInsecureContentGoogle"));
1039 }
[email protected]724159a2010-12-30 01:11:181040 controller_.ssl_manager()->DidRunInsecureContent(security_origin);
[email protected]f2b58c4d2011-06-01 23:29:411041 displayed_insecure_content_ = true;
[email protected]9678d8f2011-08-05 04:05:071042 SSLManager::NotifySSLInternalStateChanged(&controller());
[email protected]724159a2010-12-30 01:11:181043}
1044
1045void TabContents::OnDocumentLoadedInFrame(int64 frame_id) {
1046 controller_.DocumentLoadedInFrame();
[email protected]0d60f0192011-04-14 12:40:101047 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
1048 DocumentLoadedInFrame(frame_id));
[email protected]724159a2010-12-30 01:11:181049}
1050
1051void TabContents::OnDidFinishLoad(int64 frame_id) {
[email protected]0d60f0192011-04-14 12:40:101052 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
1053 DidFinishLoad(frame_id));
[email protected]724159a2010-12-30 01:11:181054}
1055
[email protected]c8f73ab2011-01-22 00:05:171056void TabContents::OnUpdateContentRestrictions(int restrictions) {
[email protected]c40d6232011-03-25 00:16:211057 SetContentRestrictions(restrictions);
[email protected]c8f73ab2011-01-22 00:05:171058}
1059
[email protected]216813952011-05-19 22:21:261060void TabContents::OnGoToEntryAtOffset(int offset) {
1061 if (!delegate_ || delegate_->OnGoToEntryOffset(offset)) {
1062 NavigationEntry* entry = controller_.GetEntryAtOffset(offset);
1063 if (!entry)
1064 return;
1065 // Note that we don't call NavigationController::GotToOffset() as we don't
1066 // want to create a pending navigation entry (it might end up lingering
1067 // http://crbug.com/51680).
1068 entry->set_transition_type(entry->transition_type() |
1069 PageTransition::FORWARD_BACK);
1070 NavigateToEntry(*entry, NavigationController::NO_RELOAD);
[email protected]a13cc362011-07-28 21:29:571071
1072 // If the entry is being restored and doesn't have a SiteInstance yet, fill
1073 // it in now that we know. This allows us to find the entry when it commits.
1074 if (!entry->site_instance() &&
1075 entry->restore_type() != NavigationEntry::RESTORE_NONE) {
[email protected]77362eb2011-08-01 17:18:381076 entry->set_site_instance(GetPendingSiteInstance());
[email protected]a13cc362011-07-28 21:29:571077 }
[email protected]216813952011-05-19 22:21:261078 }
1079}
1080
1081void TabContents::OnUpdateZoomLimits(int minimum_percent,
1082 int maximum_percent,
1083 bool remember) {
1084 minimum_zoom_percent_ = minimum_percent;
1085 maximum_zoom_percent_ = maximum_percent;
1086 temporary_zoom_settings_ = !remember;
1087}
1088
1089void TabContents::OnFocusedNodeChanged(bool is_editable_node) {
1090 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271091 content::NOTIFICATION_FOCUS_CHANGED_IN_PAGE,
[email protected]216813952011-05-19 22:21:261092 Source<TabContents>(this),
1093 Details<const bool>(&is_editable_node));
1094}
1095
[email protected]96d185d2009-04-24 03:28:541096// Notifies the RenderWidgetHost instance about the fact that the page is
1097// loading, or done loading and calls the base implementation.
1098void TabContents::SetIsLoading(bool is_loading,
1099 LoadNotificationDetails* details) {
1100 if (is_loading == is_loading_)
1101 return;
1102
1103 if (!is_loading) {
[email protected]9c235f042011-08-10 22:28:211104 load_state_ = net::LoadStateWithParam(net::LOAD_STATE_IDLE, string16());
[email protected]96d185d2009-04-24 03:28:541105 load_state_host_.clear();
[email protected]094e5b22009-09-25 04:23:561106 upload_size_ = 0;
1107 upload_position_ = 0;
[email protected]96d185d2009-04-24 03:28:541108 }
1109
1110 render_manager_.SetIsLoading(is_loading);
1111
1112 is_loading_ = is_loading;
1113 waiting_for_response_ = is_loading;
1114
[email protected]6ebdc9b2010-09-27 16:55:571115 if (delegate_)
1116 delegate_->LoadingStateChanged(this);
[email protected]329581b2009-04-28 06:52:351117 NotifyNavigationStateChanged(INVALIDATE_LOAD);
[email protected]96d185d2009-04-24 03:28:541118
[email protected]432115822011-07-10 15:52:271119 int type = is_loading ? content::NOTIFICATION_LOAD_START :
1120 content::NOTIFICATION_LOAD_STOP;
[email protected]ce5c4502009-05-06 16:46:111121 NotificationDetails det = NotificationService::NoDetails();
[email protected]96d185d2009-04-24 03:28:541122 if (details)
1123 det = Details<LoadNotificationDetails>(details);
1124 NotificationService::current()->Notify(type,
1125 Source<NavigationController>(&controller_),
1126 det);
1127}
1128
[email protected]d0980792011-02-13 19:41:401129WebUI* TabContents::GetWebUIForCurrentState() {
[email protected]7ade2732011-02-10 00:13:581130 // When there is a pending navigation entry, we want to use the pending WebUI
[email protected]96d185d2009-04-24 03:28:541131 // that goes along with it to control the basic flags. For example, we want to
1132 // show the pending URL in the URL bar, so we want the display_url flag to
1133 // be from the pending entry.
1134 //
1135 // The confusion comes because there are multiple possibilities for the
1136 // initial load in a tab as a side effect of the way the RenderViewHostManager
1137 // works.
1138 //
[email protected]e0112912011-02-02 22:54:351139 // - For the very first tab the load looks "normal". The new tab Web UI is
[email protected]96d185d2009-04-24 03:28:541140 // the pending one, and we want it to apply here.
1141 //
1142 // - For subsequent new tabs, they'll get a new SiteInstance which will then
1143 // get switched to the one previously associated with the new tab pages.
[email protected]7ade2732011-02-10 00:13:581144 // This switching will cause the manager to commit the RVH/WebUI. So we'll
[email protected]e0112912011-02-02 22:54:351145 // have a committed Web UI in this case.
[email protected]96d185d2009-04-24 03:28:541146 //
1147 // This condition handles all of these cases:
1148 //
1149 // - First load in first tab: no committed nav entry + pending nav entry +
1150 // pending dom ui:
[email protected]e0112912011-02-02 22:54:351151 // -> Use pending Web UI if any.
[email protected]96d185d2009-04-24 03:28:541152 //
1153 // - First load in second tab: no committed nav entry + pending nav entry +
[email protected]e0112912011-02-02 22:54:351154 // no pending Web UI:
1155 // -> Use the committed Web UI if any.
[email protected]96d185d2009-04-24 03:28:541156 //
1157 // - Second navigation in any tab: committed nav entry + pending nav entry:
[email protected]e0112912011-02-02 22:54:351158 // -> Use pending Web UI if any.
[email protected]96d185d2009-04-24 03:28:541159 //
1160 // - Normal state with no load: committed nav entry + no pending nav entry:
[email protected]e0112912011-02-02 22:54:351161 // -> Use committed Web UI.
[email protected]96d185d2009-04-24 03:28:541162 if (controller_.pending_entry() &&
1163 (controller_.GetLastCommittedEntry() ||
[email protected]7ade2732011-02-10 00:13:581164 render_manager_.pending_web_ui()))
1165 return render_manager_.pending_web_ui();
1166 return render_manager_.web_ui();
[email protected]96d185d2009-04-24 03:28:541167}
[email protected]420ae012009-04-24 05:16:321168
[email protected]1fd1a502011-03-30 16:55:561169WebUI::TypeID TabContents::GetWebUITypeForCurrentState() {
[email protected]3d7474ff2011-07-27 17:47:371170 return content::WebUIFactory::Get()->GetWebUIType(browser_context(),
1171 GetURL());
[email protected]1fd1a502011-03-30 16:55:561172}
1173
[email protected]420ae012009-04-24 05:16:321174void TabContents::DidNavigateMainFramePostCommit(
[email protected]8286f51a2011-05-31 17:39:131175 const content::LoadCommittedDetails& details,
[email protected]420ae012009-04-24 05:16:321176 const ViewHostMsg_FrameNavigate_Params& params) {
[email protected]1fd1a502011-03-30 16:55:561177 if (opener_web_ui_type_ != WebUI::kNoWebUI) {
[email protected]80a8fad2011-01-29 04:02:381178 // If this is a window.open navigation, use the same WebUI as the renderer
[email protected]c2e74fe82009-09-03 17:57:441179 // that opened the window, as long as both renderers have the same
1180 // privileges.
[email protected]1fd1a502011-03-30 16:55:561181 if (delegate_ && opener_web_ui_type_ == GetWebUITypeForCurrentState()) {
1182 WebUI* web_ui = content::GetContentClient()->browser()->
1183 GetWebUIFactory()->CreateWebUIForURL(this, GetURL());
[email protected]7ade2732011-02-10 00:13:581184 // web_ui might be NULL if the URL refers to a non-existent extension.
1185 if (web_ui) {
[email protected]d0980792011-02-13 19:41:401186 render_manager_.SetWebUIPostCommit(web_ui);
[email protected]7ade2732011-02-10 00:13:581187 web_ui->RenderViewCreated(render_view_host());
[email protected]c2e74fe82009-09-03 17:57:441188 }
1189 }
[email protected]1fd1a502011-03-30 16:55:561190 opener_web_ui_type_ = WebUI::kNoWebUI;
[email protected]c2e74fe82009-09-03 17:57:441191 }
1192
[email protected]4e697b042011-07-08 06:44:561193 if (details.is_navigation_to_different_page()) {
[email protected]420ae012009-04-24 05:16:321194 // Clear the status bubble. This is a workaround for a bug where WebKit
1195 // doesn't let us know that the cursor left an element during a
1196 // transition (this is also why the mouse cursor remains as a hand after
1197 // clicking on a link); see bugs 1184641 and 980803. We don't want to
1198 // clear the bubble when a user navigates to a named anchor in the same
1199 // page.
1200 UpdateTargetURL(details.entry->page_id(), GURL());
[email protected]420ae012009-04-24 05:16:321201 }
1202
[email protected]a6e82fc2010-02-24 22:28:141203 if (!details.is_in_page) {
[email protected]f17a0ee2010-05-17 17:38:471204 // Once the main frame is navigated, we're no longer considered to have
1205 // displayed insecure content.
1206 displayed_insecure_content_ = false;
[email protected]aece2c7f2009-08-27 20:43:171207 }
[email protected]ce5c4502009-05-06 16:46:111208
[email protected]8cc8d492010-02-02 10:40:491209 // Close constrained windows if necessary.
1210 if (!net::RegistryControlledDomainService::SameDomainOrHost(
1211 details.previous_url, details.entry->url()))
1212 CloseConstrainedWindows();
1213
[email protected]3c9e1872010-11-18 16:17:491214 // Notify observers about navigation.
[email protected]585b30362011-01-28 02:30:171215 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
[email protected]3c9e1872010-11-18 16:17:491216 DidNavigateMainFramePostCommit(details, params));
[email protected]420ae012009-04-24 05:16:321217}
1218
1219void TabContents::DidNavigateAnyFramePostCommit(
1220 RenderViewHost* render_view_host,
[email protected]8286f51a2011-05-31 17:39:131221 const content::LoadCommittedDetails& details,
[email protected]420ae012009-04-24 05:16:321222 const ViewHostMsg_FrameNavigate_Params& params) {
[email protected]3ab9cb82011-06-03 18:02:071223 // If we navigate, reset JavaScript state. This does nothing to prevent
[email protected]420ae012009-04-24 05:16:321224 // a malicious script from spamming messages, since the script could just
1225 // reload the page to stop blocking.
[email protected]2e5b90c2011-08-16 21:11:551226 if (dialog_creator_) {
1227 dialog_creator_->ResetJavaScriptState(this);
1228 dialog_creator_ = NULL;
1229 }
[email protected]420ae012009-04-24 05:16:321230
[email protected]3c9e1872010-11-18 16:17:491231 // Notify observers about navigation.
[email protected]585b30362011-01-28 02:30:171232 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
[email protected]3c9e1872010-11-18 16:17:491233 DidNavigateAnyFramePostCommit(details, params));
[email protected]420ae012009-04-24 05:16:321234}
1235
[email protected]8cc8d492010-02-02 10:40:491236void TabContents::CloseConstrainedWindows() {
1237 // Clear out any constrained windows since we are leaving this page entirely.
[email protected]aed59602011-02-28 22:57:331238 // To ensure that we iterate over every element in child_windows_ we
1239 // need to use a copy of child_windows_. Otherwise if
1240 // window->CloseConstrainedWindow() modifies child_windows_ we could end up
1241 // skipping some elements.
1242 ConstrainedWindowList child_windows_copy(child_windows_);
1243 for (ConstrainedWindowList::iterator it = child_windows_copy.begin();
1244 it != child_windows_copy.end(); ++it) {
1245 ConstrainedWindow* window = *it;
[email protected]fa1cf0b82010-01-15 21:49:441246 if (window) {
[email protected]420ae012009-04-24 05:16:321247 window->CloseConstrainedWindow();
[email protected]fa1cf0b82010-01-15 21:49:441248 BlockTabContent(false);
1249 }
[email protected]420ae012009-04-24 05:16:321250 }
1251}
1252
[email protected]420ae012009-04-24 05:16:321253void TabContents::UpdateMaxPageIDIfNecessary(SiteInstance* site_instance,
1254 RenderViewHost* rvh) {
1255 // If we are creating a RVH for a restored controller, then we might
1256 // have more page IDs than the SiteInstance's current max page ID. We must
1257 // make sure that the max page ID is larger than any restored page ID.
1258 // Note that it is ok for conflicting page IDs to exist in another tab
1259 // (i.e., NavigationController), but if any page ID is larger than the max,
1260 // the back/forward list will get confused.
1261 int max_restored_page_id = controller_.max_restored_page_id();
1262 if (max_restored_page_id > 0) {
1263 int curr_max_page_id = site_instance->max_page_id();
1264 if (max_restored_page_id > curr_max_page_id) {
1265 // Need to update the site instance immediately.
1266 site_instance->UpdateMaxPageID(max_restored_page_id);
1267
1268 // Also tell the renderer to update its internal representation. We
1269 // need to reserve enough IDs to make all restored page IDs less than
1270 // the max.
1271 if (curr_max_page_id < 0)
1272 curr_max_page_id = 0;
[email protected]216813952011-05-19 22:21:261273 rvh->Send(new ViewMsg_ReservePageIDRange(
1274 rvh->routing_id(), max_restored_page_id - curr_max_page_id));
[email protected]420ae012009-04-24 05:16:321275 }
1276 }
1277}
1278
[email protected]6b2f7a82011-04-25 19:30:511279bool TabContents::UpdateTitleForEntry(NavigationEntry* entry,
[email protected]acafd272011-07-26 17:35:571280 const string16& title) {
[email protected]420ae012009-04-24 05:16:321281 // For file URLs without a title, use the pathname instead. In the case of a
1282 // synthesized title, we don't want the update to count toward the "one set
1283 // per page of the title to history."
[email protected]6b2f7a82011-04-25 19:30:511284 string16 final_title;
[email protected]420ae012009-04-24 05:16:321285 bool explicit_set;
[email protected]987fc3a2011-05-26 14:18:091286 if (entry && entry->url().SchemeIsFile() && title.empty()) {
[email protected]6b2f7a82011-04-25 19:30:511287 final_title = UTF8ToUTF16(entry->url().ExtractFileName());
[email protected]420ae012009-04-24 05:16:321288 explicit_set = false; // Don't count synthetic titles toward the set limit.
1289 } else {
[email protected]acafd272011-07-26 17:35:571290 TrimWhitespace(title, TRIM_ALL, &final_title);
[email protected]420ae012009-04-24 05:16:321291 explicit_set = true;
1292 }
1293
[email protected]987fc3a2011-05-26 14:18:091294 // If a page is created via window.open and never navigated,
1295 // there will be no navigation entry. In this situation,
1296 // |page_title_when_no_navigaiton_entry_| will be used for page title.
1297 if (entry) {
1298 if (final_title == entry->title())
1299 return false; // Nothing changed, don't bother.
[email protected]420ae012009-04-24 05:16:321300
[email protected]987fc3a2011-05-26 14:18:091301 entry->set_title(final_title);
1302 } else {
1303 if (page_title_when_no_navigation_entry_ == final_title)
1304 return false; // Nothing changed, don't bother.
1305
1306 page_title_when_no_navigation_entry_ = final_title;
1307 }
[email protected]420ae012009-04-24 05:16:321308
[email protected]420ae012009-04-24 05:16:321309 // Lastly, set the title for the view.
[email protected]6b2f7a82011-04-25 19:30:511310 view_->SetPageTitle(UTF16ToWideHack(final_title));
[email protected]420ae012009-04-24 05:16:321311
[email protected]105bb0f2011-05-24 17:12:141312 TitleUpdatedDetails details(entry, explicit_set);
1313
[email protected]cbc0e1b2010-04-12 18:33:041314 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271315 content::NOTIFICATION_TAB_CONTENTS_TITLE_UPDATED,
[email protected]cbc0e1b2010-04-12 18:33:041316 Source<TabContents>(this),
[email protected]105bb0f2011-05-24 17:12:141317 Details<TitleUpdatedDetails>(&details));
[email protected]cbc0e1b2010-04-12 18:33:041318
[email protected]420ae012009-04-24 05:16:321319 return true;
1320}
1321
1322void TabContents::NotifySwapped() {
1323 // After sending out a swap notification, we need to send a disconnect
1324 // notification so that clients that pick up a pointer to |this| can NULL the
1325 // pointer. See Bug 1230284.
1326 notify_disconnection_ = true;
1327 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271328 content::NOTIFICATION_TAB_CONTENTS_SWAPPED,
[email protected]d82ed61e2009-06-16 02:46:221329 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:321330 NotificationService::NoDetails());
1331}
1332
1333void TabContents::NotifyConnected() {
1334 notify_disconnection_ = true;
1335 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271336 content::NOTIFICATION_TAB_CONTENTS_CONNECTED,
[email protected]d82ed61e2009-06-16 02:46:221337 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:321338 NotificationService::NoDetails());
1339}
1340
1341void TabContents::NotifyDisconnected() {
1342 if (!notify_disconnection_)
1343 return;
1344
1345 notify_disconnection_ = false;
1346 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271347 content::NOTIFICATION_TAB_CONTENTS_DISCONNECTED,
[email protected]d82ed61e2009-06-16 02:46:221348 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:321349 NotificationService::NoDetails());
1350}
1351
[email protected]8d3347f2009-07-09 22:00:211352RenderViewHostDelegate::View* TabContents::GetViewDelegate() {
[email protected]420ae012009-04-24 05:16:321353 return view_.get();
1354}
1355
[email protected]8d3347f2009-07-09 22:00:211356RenderViewHostDelegate::RendererManagement*
1357TabContents::GetRendererManagementDelegate() {
1358 return &render_manager_;
1359}
1360
[email protected]3d7474ff2011-07-27 17:47:371361RendererPreferences TabContents::GetRendererPrefs(
1362 content::BrowserContext* browser_context) const {
[email protected]840b1512009-07-21 16:53:461363 return renderer_preferences_;
[email protected]80d96fa2009-06-10 22:34:511364}
1365
[email protected]57c6a652009-05-04 07:58:341366TabContents* TabContents::GetAsTabContents() {
1367 return this;
1368}
1369
[email protected]7b291f92009-08-14 05:43:531370ViewType::Type TabContents::GetRenderViewType() const {
1371 return ViewType::TAB_CONTENTS;
1372}
1373
[email protected]420ae012009-04-24 05:16:321374void TabContents::RenderViewCreated(RenderViewHost* render_view_host) {
[email protected]2e4633c2009-07-09 16:58:061375 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271376 content::NOTIFICATION_RENDER_VIEW_HOST_CREATED_FOR_TAB,
[email protected]2e4633c2009-07-09 16:58:061377 Source<TabContents>(this),
1378 Details<RenderViewHost>(render_view_host));
[email protected]420ae012009-04-24 05:16:321379 NavigationEntry* entry = controller_.GetActiveEntry();
1380 if (!entry)
1381 return;
1382
1383 // When we're creating views, we're still doing initial setup, so we always
[email protected]e0112912011-02-02 22:54:351384 // use the pending Web UI rather than any possibly existing committed one.
[email protected]1fd1a502011-03-30 16:55:561385 if (render_manager_.pending_web_ui())
[email protected]7ade2732011-02-10 00:13:581386 render_manager_.pending_web_ui()->RenderViewCreated(render_view_host);
[email protected]420ae012009-04-24 05:16:321387
1388 if (entry->IsViewSourceMode()) {
1389 // Put the renderer in view source mode.
1390 render_view_host->Send(
1391 new ViewMsg_EnableViewSourceMode(render_view_host->routing_id()));
1392 }
[email protected]0666aef2009-05-13 19:48:081393
1394 view()->RenderViewCreated(render_view_host);
[email protected]11f764bb2011-05-25 19:45:261395
1396 FOR_EACH_OBSERVER(
1397 TabContentsObserver, observers_, RenderViewCreated(render_view_host));
[email protected]420ae012009-04-24 05:16:321398}
1399
1400void TabContents::RenderViewReady(RenderViewHost* rvh) {
1401 if (rvh != render_view_host()) {
1402 // Don't notify the world, since this came from a renderer in the
1403 // background.
1404 return;
1405 }
1406
1407 NotifyConnected();
[email protected]9d8a4642009-07-29 17:25:301408 bool was_crashed = is_crashed();
[email protected]443b80e2010-12-14 00:42:231409 SetIsCrashed(base::TERMINATION_STATUS_STILL_RUNNING, 0);
[email protected]9d8a4642009-07-29 17:25:301410
1411 // Restore the focus to the tab (otherwise the focus will be on the top
1412 // window).
[email protected]484ae5912010-09-29 19:16:141413 if (was_crashed && !FocusLocationBarByDefault() &&
1414 (!delegate_ || delegate_->ShouldFocusPageAfterCrash())) {
[email protected]9d8a4642009-07-29 17:25:301415 Focus();
[email protected]484ae5912010-09-29 19:16:141416 }
[email protected]420ae012009-04-24 05:16:321417}
1418
[email protected]443b80e2010-12-14 00:42:231419void TabContents::RenderViewGone(RenderViewHost* rvh,
1420 base::TerminationStatus status,
1421 int error_code) {
[email protected]420ae012009-04-24 05:16:321422 if (rvh != render_view_host()) {
1423 // The pending page's RenderViewHost is gone.
1424 return;
1425 }
1426
1427 SetIsLoading(false, NULL);
1428 NotifyDisconnected();
[email protected]443b80e2010-12-14 00:42:231429 SetIsCrashed(status, error_code);
[email protected]420ae012009-04-24 05:16:321430
[email protected]55452902011-06-01 21:57:471431 FOR_EACH_OBSERVER(TabContentsObserver, observers_, RenderViewGone());
[email protected]420ae012009-04-24 05:16:321432}
1433
[email protected]2e4633c2009-07-09 16:58:061434void TabContents::RenderViewDeleted(RenderViewHost* rvh) {
[email protected]fb7b79f72009-11-06 18:00:481435 render_manager_.RenderViewDeleted(rvh);
[email protected]2e4633c2009-07-09 16:58:061436}
1437
[email protected]420ae012009-04-24 05:16:321438void TabContents::DidNavigate(RenderViewHost* rvh,
1439 const ViewHostMsg_FrameNavigate_Params& params) {
[email protected]09b29342011-06-24 19:18:481440 if (PageTransition::IsMainFrame(params.transition))
[email protected]420ae012009-04-24 05:16:321441 render_manager_.DidNavigateMainFrame(rvh);
[email protected]8030f012009-09-25 18:09:371442
[email protected]420ae012009-04-24 05:16:321443 // Update the site of the SiteInstance if it doesn't have one yet.
1444 if (!GetSiteInstance()->has_site())
1445 GetSiteInstance()->SetSite(params.url);
1446
1447 // Need to update MIME type here because it's referred to in
1448 // UpdateNavigationCommands() called by RendererDidNavigate() to
1449 // determine whether or not to enable the encoding menu.
1450 // It's updated only for the main frame. For a subframe,
1451 // RenderView::UpdateURL does not set params.contents_mime_type.
1452 // (see http://code.google.com/p/chromium/issues/detail?id=2929 )
1453 // TODO(jungshik): Add a test for the encoding menu to avoid
1454 // regressing it again.
1455 if (PageTransition::IsMainFrame(params.transition))
1456 contents_mime_type_ = params.contents_mime_type;
1457
[email protected]8286f51a2011-05-31 17:39:131458 content::LoadCommittedDetails details;
[email protected]93f230e02011-06-01 14:40:001459 bool did_navigate = controller_.RendererDidNavigate(params, &details);
[email protected]3e90d4a2009-07-03 17:38:391460
[email protected]a9c0bfe2010-09-17 08:35:221461 // Send notification about committed provisional loads. This notification is
1462 // different from the NAV_ENTRY_COMMITTED notification which doesn't include
1463 // the actual URL navigated to and isn't sent for AUTO_SUBFRAME navigations.
1464 if (details.type != NavigationType::NAV_IGNORE) {
[email protected]dabb0d12010-10-05 12:50:071465 // For AUTO_SUBFRAME navigations, an event for the main frame is generated
1466 // that is not recorded in the navigation history. For the purpose of
1467 // tracking navigation events, we treat this event as a sub frame navigation
1468 // event.
1469 bool is_main_frame = did_navigate ? details.is_main_frame : false;
[email protected]0d60f0192011-04-14 12:40:101470 PageTransition::Type transition_type = params.transition;
[email protected]a9c0bfe2010-09-17 08:35:221471 // Whether or not a page transition was triggered by going backward or
1472 // forward in the history is only stored in the navigation controller's
1473 // entry list.
1474 if (did_navigate &&
1475 (controller_.GetActiveEntry()->transition_type() &
1476 PageTransition::FORWARD_BACK)) {
[email protected]0d60f0192011-04-14 12:40:101477 transition_type = params.transition | PageTransition::FORWARD_BACK;
[email protected]a9c0bfe2010-09-17 08:35:221478 }
[email protected]0d60f0192011-04-14 12:40:101479 // Notify observers about the commit of the provisional load.
1480 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
1481 DidCommitProvisionalLoadForFrame(params.frame_id,
1482 is_main_frame, params.url, transition_type));
[email protected]a9c0bfe2010-09-17 08:35:221483 }
1484
[email protected]76543b92009-08-31 17:27:451485 if (!did_navigate)
1486 return; // No navigation happened.
[email protected]420ae012009-04-24 05:16:321487
1488 // DO NOT ADD MORE STUFF TO THIS FUNCTION! Your component should either listen
1489 // for the appropriate notification (best) or you can add it to
1490 // DidNavigateMainFramePostCommit / DidNavigateAnyFramePostCommit (only if
1491 // necessary, please).
1492
1493 // Run post-commit tasks.
[email protected]93f230e02011-06-01 14:40:001494 if (details.is_main_frame) {
[email protected]420ae012009-04-24 05:16:321495 DidNavigateMainFramePostCommit(details, params);
[email protected]09b29342011-06-24 19:18:481496 if (delegate())
1497 delegate()->DidNavigateMainFramePostCommit(this);
[email protected]93f230e02011-06-01 14:40:001498 }
[email protected]420ae012009-04-24 05:16:321499 DidNavigateAnyFramePostCommit(rvh, details, params);
1500}
1501
1502void TabContents::UpdateState(RenderViewHost* rvh,
1503 int32 page_id,
1504 const std::string& state) {
[email protected]992db4c2011-05-12 15:37:151505 // Ensure that this state update comes from either the active RVH or one of
1506 // the swapped out RVHs. We don't expect to hear from any other RVHs.
1507 DCHECK(rvh == render_view_host() || render_manager_.IsSwappedOut(rvh));
[email protected]420ae012009-04-24 05:16:321508
1509 // We must be prepared to handle state updates for any page, these occur
1510 // when the user is scrolling and entering form data, as well as when we're
1511 // leaving a page, in which case our state may have already been moved to
1512 // the next page. The navigation controller will look up the appropriate
1513 // NavigationEntry and update it when it is notified via the delegate.
1514
1515 int entry_index = controller_.GetEntryIndexWithPageID(
[email protected]992db4c2011-05-12 15:37:151516 rvh->site_instance(), page_id);
[email protected]420ae012009-04-24 05:16:321517 if (entry_index < 0)
1518 return;
1519 NavigationEntry* entry = controller_.GetEntryAtIndex(entry_index);
1520
1521 if (state == entry->content_state())
1522 return; // Nothing to update.
1523 entry->set_content_state(state);
1524 controller_.NotifyEntryChanged(entry, entry_index);
1525}
1526
1527void TabContents::UpdateTitle(RenderViewHost* rvh,
[email protected]acafd272011-07-26 17:35:571528 int32 page_id,
[email protected]a49e10b2011-08-01 23:57:461529 const string16& title,
1530 base::i18n::TextDirection title_direction) {
[email protected]420ae012009-04-24 05:16:321531 // If we have a title, that's a pretty good indication that we've started
1532 // getting useful data.
1533 SetNotWaitingForResponse();
1534
1535 DCHECK(rvh == render_view_host());
[email protected]371cc8b2010-09-01 16:03:481536 NavigationEntry* entry = controller_.GetEntryWithPageID(rvh->site_instance(),
[email protected]ec0b6c42010-08-26 03:16:581537 page_id);
[email protected]987fc3a2011-05-26 14:18:091538
[email protected]a49e10b2011-08-01 23:57:461539 // TODO(evan): make use of title_direction.
1540 // http://code.google.com/p/chromium/issues/detail?id=27094
[email protected]987fc3a2011-05-26 14:18:091541 if (!UpdateTitleForEntry(entry, title))
[email protected]420ae012009-04-24 05:16:321542 return;
1543
1544 // Broadcast notifications when the UI should be updated.
1545 if (entry == controller_.GetEntryAtOffset(0))
[email protected]f1cd5e82009-10-23 17:19:031546 NotifyNavigationStateChanged(INVALIDATE_TITLE);
[email protected]420ae012009-04-24 05:16:321547}
1548
[email protected]420ae012009-04-24 05:16:321549void TabContents::UpdateEncoding(RenderViewHost* render_view_host,
[email protected]41fc0322009-09-04 22:23:401550 const std::string& encoding) {
[email protected]420ae012009-04-24 05:16:321551 set_encoding(encoding);
1552}
1553
1554void TabContents::UpdateTargetURL(int32 page_id, const GURL& url) {
1555 if (delegate())
1556 delegate()->UpdateTargetURL(this, url);
1557}
1558
[email protected]420ae012009-04-24 05:16:321559void TabContents::Close(RenderViewHost* rvh) {
[email protected]07707302009-11-06 00:50:291560 // The UI may be in an event-tracking loop, such as between the
1561 // mouse-down and mouse-up in text selection or a button click.
1562 // Defer the close until after tracking is complete, so that we
1563 // don't free objects out from under the UI.
1564 // TODO(shess): This could probably be integrated with the
1565 // IsDoingDrag() test below. Punting for now because I need more
1566 // research to understand how this impacts platforms other than Mac.
1567 // TODO(shess): This could get more fine-grained. For instance,
1568 // closing a tab in another window while selecting text in the
1569 // current window's Omnibox should be just fine.
1570 if (view()->IsEventTracking()) {
1571 view()->CloseTabAfterEventTracking();
1572 return;
1573 }
1574
[email protected]24a4d1062009-07-10 23:10:421575 // If we close the tab while we're in the middle of a drag, we'll crash.
1576 // Instead, cancel the drag and close it as soon as the drag ends.
1577 if (view()->IsDoingDrag()) {
1578 view()->CancelDragAndCloseTab();
1579 return;
1580 }
1581
[email protected]420ae012009-04-24 05:16:321582 // Ignore this if it comes from a RenderViewHost that we aren't showing.
1583 if (delegate() && rvh == render_view_host())
1584 delegate()->CloseContents(this);
1585}
1586
1587void TabContents::RequestMove(const gfx::Rect& new_bounds) {
[email protected]b35b26b32011-05-05 20:35:141588 if (delegate() && delegate()->IsPopupOrPanel(this))
[email protected]420ae012009-04-24 05:16:321589 delegate()->MoveContents(this, new_bounds);
1590}
1591
[email protected]7ab1e7d62009-10-14 23:32:011592void TabContents::DidStartLoading() {
[email protected]420ae012009-04-24 05:16:321593 SetIsLoading(true, NULL);
[email protected]3c9e1872010-11-18 16:17:491594
[email protected]c40d6232011-03-25 00:16:211595 if (delegate() && content_restrictions_) {
1596 content_restrictions_ = 0;
[email protected]de4761b2010-12-07 21:05:211597 delegate()->ContentRestrictionsChanged(this);
[email protected]9e823662010-10-13 23:36:001598 }
[email protected]3c9e1872010-11-18 16:17:491599
1600 // Notify observers about navigation.
[email protected]585b30362011-01-28 02:30:171601 FOR_EACH_OBSERVER(TabContentsObserver, observers_, DidStartLoading());
[email protected]420ae012009-04-24 05:16:321602}
1603
[email protected]7ab1e7d62009-10-14 23:32:011604void TabContents::DidStopLoading() {
[email protected]420ae012009-04-24 05:16:321605 scoped_ptr<LoadNotificationDetails> details;
1606
1607 NavigationEntry* entry = controller_.GetActiveEntry();
1608 // An entry may not exist for a stop when loading an initial blank page or
1609 // if an iframe injected by script into a blank page finishes loading.
1610 if (entry) {
[email protected]420ae012009-04-24 05:16:321611 base::TimeDelta elapsed = base::TimeTicks::Now() - current_load_start_;
1612
1613 details.reset(new LoadNotificationDetails(
[email protected]ebe89e062009-08-13 23:16:541614 entry->virtual_url(),
[email protected]420ae012009-04-24 05:16:321615 entry->transition_type(),
1616 elapsed,
1617 &controller_,
1618 controller_.GetCurrentEntryIndex()));
1619 }
1620
[email protected]420ae012009-04-24 05:16:321621 SetIsLoading(false, details.get());
[email protected]3c9e1872010-11-18 16:17:491622
1623 // Notify observers about navigation.
[email protected]585b30362011-01-28 02:30:171624 FOR_EACH_OBSERVER(TabContentsObserver, observers_, DidStopLoading());
[email protected]420ae012009-04-24 05:16:321625}
1626
[email protected]c95fa8b2011-04-28 20:26:161627void TabContents::DidCancelLoading() {
1628 controller_.DiscardNonCommittedEntries();
1629
1630 // Update the URL display.
1631 NotifyNavigationStateChanged(TabContents::INVALIDATE_URL);
1632}
1633
[email protected]1a3c3cb2010-12-16 21:03:401634void TabContents::DidChangeLoadProgress(double progress) {
1635 if (delegate())
1636 delegate()->LoadProgressChanged(progress);
1637}
1638
[email protected]25497492010-09-11 15:15:081639void TabContents::DocumentOnLoadCompletedInMainFrame(
1640 RenderViewHost* render_view_host,
1641 int32 page_id) {
1642 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271643 content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME,
[email protected]25497492010-09-11 15:15:081644 Source<TabContents>(this),
1645 Details<int>(&page_id));
1646}
1647
[email protected]420ae012009-04-24 05:16:321648void TabContents::RequestOpenURL(const GURL& url, const GURL& referrer,
1649 WindowOpenDisposition disposition) {
[email protected]7ade2732011-02-10 00:13:581650 if (render_manager_.web_ui()) {
[email protected]e0112912011-02-02 22:54:351651 // When we're a Web UI, it will provide a page transition type for us (this
[email protected]420ae012009-04-24 05:16:321652 // is so the new tab page can specify AUTO_BOOKMARK for automatically
1653 // generated suggestions).
1654 //
[email protected]e0112912011-02-02 22:54:351655 // Note also that we hide the referrer for Web UI pages. We don't really
[email protected]60e448982009-05-06 04:21:161656 // want web sites to see a referrer of "chrome://blah" (and some
1657 // chrome: URLs might have search terms or other stuff we don't want to
[email protected]420ae012009-04-24 05:16:321658 // send to the site), so we send no referrer.
1659 OpenURL(url, GURL(), disposition,
[email protected]7ade2732011-02-10 00:13:581660 render_manager_.web_ui()->link_transition_type());
[email protected]420ae012009-04-24 05:16:321661 } else {
1662 OpenURL(url, referrer, disposition, PageTransition::LINK);
1663 }
1664}
1665
[email protected]420ae012009-04-24 05:16:321666void TabContents::RunJavaScriptMessage(
[email protected]992db4c2011-05-12 15:37:151667 const RenderViewHost* rvh,
[email protected]4f5ce842011-05-27 19:34:411668 const string16& message,
1669 const string16& default_prompt,
[email protected]420ae012009-04-24 05:16:321670 const GURL& frame_url,
1671 const int flags,
1672 IPC::Message* reply_msg,
1673 bool* did_suppress_message) {
[email protected]3ab9cb82011-06-03 18:02:071674 // Suppress JavaScript dialogs when requested. Also suppress messages when
1675 // showing an interstitial as it's shown over the previous page and we don't
1676 // want the hidden page's dialogs to interfere with the interstitial.
[email protected]8f55e802010-12-06 18:11:501677 bool suppress_this_message =
[email protected]992db4c2011-05-12 15:37:151678 rvh->is_swapped_out() ||
[email protected]8f55e802010-12-06 18:11:501679 showing_interstitial_page() ||
[email protected]3ab9cb82011-06-03 18:02:071680 !delegate_ ||
1681 delegate_->ShouldSuppressDialogs();
[email protected]420ae012009-04-24 05:16:321682
1683 if (!suppress_this_message) {
[email protected]b627d9a2011-06-28 14:06:341684 content::JavaScriptDialogCreator::TitleType title_type;
1685 string16 title;
1686
1687 if (!frame_url.has_host()) {
1688 title_type = content::JavaScriptDialogCreator::DIALOG_TITLE_NONE;
1689 } else {
1690 title_type = content::JavaScriptDialogCreator::DIALOG_TITLE_FORMATTED_URL;
1691 title = net::FormatUrl(
1692 frame_url.GetOrigin(),
1693 content::GetContentClient()->browser()->GetAcceptLangs(this));
1694 }
1695
[email protected]2e5b90c2011-08-16 21:11:551696 dialog_creator_ = delegate_->GetJavaScriptDialogCreator();
1697 dialog_creator_->RunJavaScriptDialog(this,
1698 title_type,
1699 title,
1700 flags,
1701 message,
1702 default_prompt,
1703 reply_msg,
1704 &suppress_this_message);
[email protected]420ae012009-04-24 05:16:321705 }
[email protected]3ab9cb82011-06-03 18:02:071706
1707 if (suppress_this_message) {
1708 // If we are suppressing messages, just reply as if the user immediately
1709 // pressed "Cancel".
1710 OnDialogClosed(reply_msg, false, string16());
1711 }
1712
1713 *did_suppress_message = suppress_this_message;
[email protected]420ae012009-04-24 05:16:321714}
1715
[email protected]992db4c2011-05-12 15:37:151716void TabContents::RunBeforeUnloadConfirm(const RenderViewHost* rvh,
[email protected]4f5ce842011-05-27 19:34:411717 const string16& message,
[email protected]420ae012009-04-24 05:16:321718 IPC::Message* reply_msg) {
[email protected]5aab5e22010-12-08 22:13:291719 if (delegate())
1720 delegate()->WillRunBeforeUnloadConfirm();
[email protected]3ab9cb82011-06-03 18:02:071721
1722 bool suppress_this_message =
1723 rvh->is_swapped_out() ||
1724 !delegate_ ||
1725 delegate_->ShouldSuppressDialogs();
[email protected]992db4c2011-05-12 15:37:151726 if (suppress_this_message) {
[email protected]4f5ce842011-05-27 19:34:411727 render_view_host()->JavaScriptDialogClosed(reply_msg, true, string16());
[email protected]8f55e802010-12-06 18:11:501728 return;
1729 }
[email protected]3ab9cb82011-06-03 18:02:071730
[email protected]7ab1e7d62009-10-14 23:32:011731 is_showing_before_unload_dialog_ = true;
[email protected]2e5b90c2011-08-16 21:11:551732 dialog_creator_ = delegate_->GetJavaScriptDialogCreator();
1733 dialog_creator_->RunBeforeUnloadDialog(this,
1734 message,
1735 reply_msg);
[email protected]420ae012009-04-24 05:16:321736}
1737
[email protected]420ae012009-04-24 05:16:321738WebPreferences TabContents::GetWebkitPrefs() {
[email protected]447021c2010-09-08 21:29:081739 WebPreferences web_prefs =
[email protected]181a95ee2011-07-12 19:26:361740 content::GetContentClient()->browser()->GetWebkitPrefs(
[email protected]3d7474ff2011-07-27 17:47:371741 render_view_host()->process()->browser_context(), false);
[email protected]447021c2010-09-08 21:29:081742
[email protected]b8299c12011-06-03 19:52:281743 // Force accelerated compositing and 2d canvas off for chrome:, about: and
[email protected]2ef216b2011-08-01 19:25:211744 // chrome-devtools: pages (unless it's specifically allowed).
1745 if ((GetURL().SchemeIs(chrome::kChromeDevToolsScheme) ||
[email protected]b8299c12011-06-03 19:52:281746 GetURL().SchemeIs(chrome::kChromeUIScheme) ||
[email protected]2ef216b2011-08-01 19:25:211747 GetURL().SchemeIs(chrome::kAboutScheme)) &&
1748 !web_prefs.allow_webui_compositing) {
[email protected]447021c2010-09-08 21:29:081749 web_prefs.accelerated_compositing_enabled = false;
[email protected]9beff752010-09-22 19:35:431750 web_prefs.accelerated_2d_canvas_enabled = false;
[email protected]447021c2010-09-08 21:29:081751 }
1752
[email protected]39553442010-09-09 00:01:551753#if defined(OS_MACOSX)
1754 // Disable accelerated compositing if IOSurface's are not supported,
1755 // as is the case in 10.5.
1756 if (!IOSurfaceSupport::Initialize())
1757 web_prefs.accelerated_compositing_enabled = false;
1758#endif
1759
[email protected]447021c2010-09-08 21:29:081760 return web_prefs;
[email protected]420ae012009-04-24 05:16:321761}
1762
[email protected]7d472472011-01-22 01:30:251763void TabContents::OnUserGesture() {
[email protected]ddb85052011-05-18 14:40:271764 // Notify observers.
1765 FOR_EACH_OBSERVER(TabContentsObserver, observers_, DidGetUserGesture());
[email protected]686493142011-07-15 21:47:221766
1767 ResourceDispatcherHost* rdh =
1768 content::GetContentClient()->browser()->GetResourceDispatcherHost();
1769 if (rdh) // NULL in unittests.
[email protected]8ec98cd22011-08-09 22:31:301770 rdh->OnUserGesture(this);
[email protected]7d472472011-01-22 01:30:251771}
1772
[email protected]fa1cf0b82010-01-15 21:49:441773void TabContents::OnIgnoredUIEvent() {
1774 if (constrained_window_count()) {
1775 ConstrainedWindow* window = *constrained_window_begin();
1776 window->FocusConstrainedWindow();
1777 }
1778}
1779
[email protected]420ae012009-04-24 05:16:321780void TabContents::RendererUnresponsive(RenderViewHost* rvh,
1781 bool is_during_unload) {
[email protected]992db4c2011-05-12 15:37:151782 // Don't show hung renderer dialog for a swapped out RVH.
1783 if (rvh != render_view_host())
1784 return;
1785
[email protected]e5fc1632011-08-08 07:51:531786 // Ignore renderer unresponsive event if debugger is attached to the tab
1787 // since the event may be a result of the renderer sitting on a breakpoint.
1788 // See http://crbug.com/65458
1789 DevToolsManager* devtools_manager = DevToolsManager::GetInstance();
1790 if (devtools_manager &&
1791 devtools_manager->GetDevToolsClientHostFor(rvh) != NULL)
1792 return;
1793
[email protected]420ae012009-04-24 05:16:321794 if (is_during_unload) {
1795 // Hang occurred while firing the beforeunload/unload handler.
1796 // Pretend the handler fired so tab closing continues as if it had.
[email protected]829e7612009-04-25 01:15:111797 rvh->set_sudden_termination_allowed(true);
[email protected]420ae012009-04-24 05:16:321798
1799 if (!render_manager_.ShouldCloseTabOnUnresponsiveRenderer())
1800 return;
1801
1802 // If the tab hangs in the beforeunload/unload handler there's really
1803 // nothing we can do to recover. Pretend the unload listeners have
1804 // all fired and close the tab. If the hang is in the beforeunload handler
1805 // then the user will not have the option of cancelling the close.
1806 Close(rvh);
1807 return;
1808 }
1809
[email protected]55452902011-06-01 21:57:471810 if (!render_view_host() || !render_view_host()->IsRenderViewLive())
1811 return;
1812
1813 if (delegate())
1814 delegate()->RendererUnresponsive(this);
[email protected]420ae012009-04-24 05:16:321815}
1816
1817void TabContents::RendererResponsive(RenderViewHost* render_view_host) {
[email protected]55452902011-06-01 21:57:471818 if (delegate())
1819 delegate()->RendererResponsive(this);
[email protected]420ae012009-04-24 05:16:321820}
1821
1822void TabContents::LoadStateChanged(const GURL& url,
[email protected]9c235f042011-08-10 22:28:211823 const net::LoadStateWithParam& load_state,
[email protected]094e5b22009-09-25 04:23:561824 uint64 upload_position,
1825 uint64 upload_size) {
[email protected]420ae012009-04-24 05:16:321826 load_state_ = load_state;
[email protected]094e5b22009-09-25 04:23:561827 upload_position_ = upload_position;
1828 upload_size_ = upload_size;
[email protected]04866c42011-05-03 20:03:501829 load_state_host_ = net::IDNToUnicode(url.host(),
[email protected]b5cca982011-05-26 04:42:081830 content::GetContentClient()->browser()->GetAcceptLangs(this));
[email protected]9c235f042011-08-10 22:28:211831 if (load_state_.state == net::LOAD_STATE_READING_RESPONSE)
[email protected]420ae012009-04-24 05:16:321832 SetNotWaitingForResponse();
[email protected]f5d978c2011-07-21 14:43:511833 if (IsLoading())
[email protected]c9cd2222009-05-06 05:16:501834 NotifyNavigationStateChanged(INVALIDATE_LOAD | INVALIDATE_TAB);
[email protected]420ae012009-04-24 05:16:321835}
1836
[email protected]7d472472011-01-22 01:30:251837void TabContents::WorkerCrashed() {
[email protected]0dd3a0ab2011-02-18 08:17:441838 if (delegate())
[email protected]55452902011-06-01 21:57:471839 delegate()->WorkerCrashed(this);
[email protected]7d472472011-01-22 01:30:251840}
1841
[email protected]420ae012009-04-24 05:16:321842void TabContents::BeforeUnloadFiredFromRenderManager(
1843 bool proceed,
1844 bool* proceed_to_fire_unload) {
1845 if (delegate())
1846 delegate()->BeforeUnloadFired(this, proceed, proceed_to_fire_unload);
1847}
1848
[email protected]3a3d47472010-07-15 21:03:541849void TabContents::DidStartLoadingFromRenderManager(
1850 RenderViewHost* render_view_host) {
1851 DidStartLoading();
1852}
1853
1854void TabContents::RenderViewGoneFromRenderManager(
1855 RenderViewHost* render_view_host) {
[email protected]443b80e2010-12-14 00:42:231856 DCHECK(crashed_status_ != base::TERMINATION_STATUS_STILL_RUNNING);
1857 RenderViewGone(render_view_host, crashed_status_, crashed_error_code_);
[email protected]3a3d47472010-07-15 21:03:541858}
1859
[email protected]420ae012009-04-24 05:16:321860void TabContents::UpdateRenderViewSizeForRenderManager() {
1861 // TODO(brettw) this is a hack. See TabContentsView::SizeContents.
[email protected]0323ee42010-02-17 22:03:261862 gfx::Size size = view_->GetContainerSize();
1863 // 0x0 isn't a valid window size (minimal window size is 1x1) but it may be
1864 // here during container initialization and normal window size will be set
1865 // later. In case of tab duplication this resizing to 0x0 prevents setting
1866 // normal size later so just ignore it.
1867 if (!size.IsEmpty())
1868 view_->SizeContents(size);
[email protected]420ae012009-04-24 05:16:321869}
1870
[email protected]3a3d47472010-07-15 21:03:541871void TabContents::NotifySwappedFromRenderManager() {
1872 NotifySwapped();
1873}
1874
1875NavigationController& TabContents::GetControllerForRenderManager() {
1876 return controller();
1877}
1878
[email protected]d0980792011-02-13 19:41:401879WebUI* TabContents::CreateWebUIForRenderManager(const GURL& url) {
[email protected]1fd1a502011-03-30 16:55:561880 return content::WebUIFactory::Get()->CreateWebUIForURL(this, url);
[email protected]420ae012009-04-24 05:16:321881}
1882
1883NavigationEntry*
1884TabContents::GetLastCommittedNavigationEntryForRenderManager() {
1885 return controller_.GetLastCommittedEntry();
1886}
1887
1888bool TabContents::CreateRenderViewForRenderManager(
1889 RenderViewHost* render_view_host) {
[email protected]420ae012009-04-24 05:16:321890 RenderWidgetHostView* rwh_view = view_->CreateViewForWidget(render_view_host);
[email protected]1a98a932009-11-17 00:12:521891
[email protected]420ae012009-04-24 05:16:321892 // Now that the RenderView has been created, we need to tell it its size.
1893 rwh_view->SetSize(view_->GetContainerSize());
1894
[email protected]a4127722011-04-27 23:13:521895 if (!render_view_host->CreateRenderView(string16()))
1896 return false;
1897
[email protected]f8233cc2011-05-31 20:24:501898#if defined(OS_LINUX)
1899 // Force a ViewMsg_Resize to be sent, needed to make plugins show up on
1900 // linux. See crbug.com/83941.
1901 if (RenderWidgetHost* render_widget_host = rwh_view->GetRenderWidgetHost())
1902 render_widget_host->WasResized();
1903#endif
1904
[email protected]420ae012009-04-24 05:16:321905 UpdateMaxPageIDIfNecessary(render_view_host->site_instance(),
1906 render_view_host);
1907 return true;
1908}
1909
[email protected]3ab9cb82011-06-03 18:02:071910void TabContents::OnDialogClosed(IPC::Message* reply_msg,
1911 bool success,
1912 const string16& user_input) {
[email protected]beb440c2009-11-06 04:08:541913 if (is_showing_before_unload_dialog_ && !success) {
1914 // If a beforeunload dialog is canceled, we need to stop the throbber from
1915 // spinning, since we forced it to start spinning in Navigate.
1916 DidStopLoading();
1917
1918 tab_close_start_time_ = base::TimeTicks();
1919 }
1920 is_showing_before_unload_dialog_ = false;
[email protected]4f5ce842011-05-27 19:34:411921 render_view_host()->JavaScriptDialogClosed(reply_msg,
1922 success,
[email protected]3ab9cb82011-06-03 18:02:071923 user_input);
[email protected]beb440c2009-11-06 04:08:541924}
1925
[email protected]3ab9cb82011-06-03 18:02:071926gfx::NativeWindow TabContents::GetDialogRootWindow() {
1927 return view_->GetTopLevelNativeWindow();
[email protected]beb440c2009-11-06 04:08:541928}
1929
[email protected]a1e97f02011-06-30 14:04:341930void TabContents::OnDialogShown() {
1931 Activate();
[email protected]3a3d47472010-07-15 21:03:541932}
1933
[email protected]41fc0322009-09-04 22:23:401934void TabContents::set_encoding(const std::string& encoding) {
[email protected]763ec4ca2011-04-29 15:48:121935 encoding_ = content::GetContentClient()->browser()->
1936 GetCanonicalEncodingNameByAliasName(encoding);
[email protected]aacd7cc2009-09-02 14:51:101937}
[email protected]f45d2a72010-03-08 23:28:351938
[email protected]33f74972010-12-08 16:40:361939void TabContents::SwapInRenderViewHost(RenderViewHost* rvh) {
1940 render_manager_.SwapInRenderViewHost(rvh);
1941}
1942
1943void TabContents::CreateViewAndSetSizeForRVH(RenderViewHost* rvh) {
1944 RenderWidgetHostView* rwh_view = view()->CreateViewForWidget(rvh);
1945 rwh_view->SetSize(view()->GetContainerSize());
1946}