blob: 8e3cfa841b6909a7dc48204397ef9e8668b0ed31 [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]c7dd2f62011-07-18 15:57:5916#include "chrome/browser/download/download_manager.h"
[email protected]686493142011-07-15 21:47:2217#include "chrome/browser/download/download_request_limiter.h"
[email protected]c7dd2f62011-07-18 15:57:5918#include "chrome/browser/download/download_util.h"
[email protected]8ecad5e2010-12-02 21:18:3319#include "chrome/browser/profiles/profile.h"
[email protected]567812d2011-02-24 17:40:5020#include "content/browser/child_process_security_policy.h"
[email protected]1fd1a502011-03-30 16:55:5621#include "content/browser/content_browser_client.h"
[email protected]b46442d7e2011-06-29 02:16:0622#include "content/browser/debugger/devtools_manager.h"
[email protected]567812d2011-02-24 17:40:5023#include "content/browser/host_zoom_map.h"
24#include "content/browser/in_process_webkit/session_storage_namespace.h"
[email protected]37a72af2011-06-13 05:42:0125#include "content/browser/load_from_memory_cache_details.h"
[email protected]35e251d2011-05-24 21:01:0426#include "content/browser/load_notification_details.h"
[email protected]567812d2011-02-24 17:40:5027#include "content/browser/renderer_host/render_process_host.h"
28#include "content/browser/renderer_host/render_view_host.h"
29#include "content/browser/renderer_host/render_widget_host_view.h"
[email protected]686493142011-07-15 21:47:2230#include "content/browser/renderer_host/resource_dispatcher_host.h"
[email protected]567812d2011-02-24 17:40:5031#include "content/browser/renderer_host/resource_request_details.h"
32#include "content/browser/site_instance.h"
[email protected]0dd3a0ab2011-02-18 08:17:4433#include "content/browser/tab_contents/interstitial_page.h"
[email protected]8286f51a2011-05-31 17:39:1334#include "content/browser/tab_contents/navigation_details.h"
[email protected]0dd3a0ab2011-02-18 08:17:4435#include "content/browser/tab_contents/navigation_entry.h"
36#include "content/browser/tab_contents/provisional_load_details.h"
37#include "content/browser/tab_contents/tab_contents_delegate.h"
38#include "content/browser/tab_contents/tab_contents_observer.h"
39#include "content/browser/tab_contents/tab_contents_view.h"
[email protected]105bb0f2011-05-24 17:12:1440#include "content/browser/tab_contents/title_updated_details.h"
[email protected]afd1e522011-04-27 23:29:5941#include "content/browser/user_metrics.h"
[email protected]1fd1a502011-03-30 16:55:5642#include "content/browser/webui/web_ui_factory.h"
[email protected]9966325b2011-04-18 05:00:1043#include "content/common/bindings_policy.h"
[email protected]1fd1a502011-03-30 16:55:5644#include "content/common/content_client.h"
[email protected]763ec4ca2011-04-29 15:48:1245#include "content/common/content_restriction.h"
[email protected]4dd57932011-03-17 06:06:1246#include "content/common/navigation_types.h"
[email protected]7f070d42011-03-09 20:25:3247#include "content/common/notification_service.h"
[email protected]0f012df82011-05-19 14:15:2948#include "content/common/url_constants.h"
[email protected]2c5569662011-03-22 20:45:0249#include "content/common/view_messages.h"
[email protected]d686e812009-06-03 19:10:2950#include "net/base/net_util.h"
[email protected]420ae012009-04-24 05:16:3251#include "net/base/registry_controlled_domain.h"
[email protected]abe2c032011-03-31 18:49:3452#include "net/url_request/url_request_context_getter.h"
[email protected]8bd0fe62011-01-17 06:44:3753#include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h"
[email protected]08397d52011-02-05 01:53:3854#include "ui/gfx/codec/png_codec.h"
[email protected]d5e311b2010-07-20 17:15:0355#include "webkit/glue/password_form.h"
[email protected]3c733bde2010-12-21 19:56:3156#include "webkit/glue/webpreferences.h"
57
58#if defined(OS_MACOSX)
[email protected]b9b751f22011-03-25 14:04:1259#include "ui/gfx/surface/io_surface_support_mac.h"
[email protected]3c733bde2010-12-21 19:56:3160#endif // defined(OS_MACOSX)
[email protected]3e45ba92009-02-20 21:09:0061
[email protected]420ae012009-04-24 05:16:3262// Cross-Site Navigations
63//
64// If a TabContents is told to navigate to a different web site (as determined
65// by SiteInstance), it will replace its current RenderViewHost with a new
66// RenderViewHost dedicated to the new SiteInstance. This works as follows:
67//
68// - Navigate determines whether the destination is cross-site, and if so,
69// it creates a pending_render_view_host_ and moves into the PENDING
70// RendererState.
71// - The pending RVH is "suspended," so that no navigation messages are sent to
72// its renderer until the onbeforeunload JavaScript handler has a chance to
73// run in the current RVH.
74// - The pending RVH tells CrossSiteRequestManager (a thread-safe singleton)
75// that it has a pending cross-site request. ResourceDispatcherHost will
76// check for this when the response arrives.
77// - The current RVH runs its onbeforeunload handler. If it returns false, we
78// cancel all the pending logic and go back to NORMAL. Otherwise we allow
79// the pending RVH to send the navigation request to its renderer.
80// - ResourceDispatcherHost receives a ResourceRequest on the IO thread. It
81// checks CrossSiteRequestManager to see that the RVH responsible has a
82// pending cross-site request, and then installs a CrossSiteEventHandler.
83// - When RDH receives a response, the BufferedEventHandler determines whether
84// it is a download. If so, it sends a message to the new renderer causing
85// it to cancel the request, and the download proceeds in the download
86// thread. For now, we stay in a PENDING state (with a pending RVH) until
[email protected]57c6a652009-05-04 07:58:3487// the next DidNavigate event for this TabContents. This isn't ideal, but it
[email protected]420ae012009-04-24 05:16:3288// doesn't affect any functionality.
89// - After RDH receives a response and determines that it is safe and not a
90// download, it pauses the response to first run the old page's onunload
91// handler. It does this by asynchronously calling the OnCrossSiteResponse
[email protected]992db4c2011-05-12 15:37:1592// method of TabContents on the UI thread, which sends a SwapOut message
[email protected]420ae012009-04-24 05:16:3293// to the current RVH.
[email protected]992db4c2011-05-12 15:37:1594// - Once the onunload handler is finished, a SwapOut_ACK message is sent to
[email protected]420ae012009-04-24 05:16:3295// the ResourceDispatcherHost, who unpauses the response. Data is then sent
96// to the pending RVH.
97// - The pending renderer sends a FrameNavigate message that invokes the
98// DidNavigate method. This replaces the current RVH with the
99// pending RVH and goes back to the NORMAL RendererState.
[email protected]992db4c2011-05-12 15:37:15100// - The previous renderer is kept swapped out in RenderViewHostManager in case
101// the user goes back. The process only stays live if another tab is using
102// it, but if so, the existing frame relationships will be maintained.
[email protected]420ae012009-04-24 05:16:32103
104namespace {
105
106// Amount of time we wait between when a key event is received and the renderer
107// is queried for its state and pushed to the NavigationEntry.
108const int kQueryStateDelay = 5000;
109
[email protected]6ebdc9b2010-09-27 16:55:57110const int kSyncWaitDelay = 40;
111
[email protected]420ae012009-04-24 05:16:32112#if defined(OS_WIN)
113
114BOOL CALLBACK InvalidateWindow(HWND hwnd, LPARAM lparam) {
115 // Note: erase is required to properly paint some widgets borders. This can
116 // be seen with textfields.
117 InvalidateRect(hwnd, NULL, TRUE);
118 return TRUE;
119}
120#endif
121
[email protected]2c5569662011-03-22 20:45:02122ViewMsg_Navigate_Type::Value GetNavigationType(
[email protected]1ccb3568d2010-02-19 10:51:16123 Profile* profile, const NavigationEntry& entry,
124 NavigationController::ReloadType reload_type) {
125 switch (reload_type) {
126 case NavigationController::RELOAD:
[email protected]2c5569662011-03-22 20:45:02127 return ViewMsg_Navigate_Type::RELOAD;
[email protected]1ccb3568d2010-02-19 10:51:16128 case NavigationController::RELOAD_IGNORING_CACHE:
[email protected]2c5569662011-03-22 20:45:02129 return ViewMsg_Navigate_Type::RELOAD_IGNORING_CACHE;
[email protected]1ccb3568d2010-02-19 10:51:16130 case NavigationController::NO_RELOAD:
131 break; // Fall through to rest of function.
132 }
[email protected]5e369672009-11-03 23:48:30133
134 if (entry.restore_type() == NavigationEntry::RESTORE_LAST_SESSION &&
135 profile->DidLastSessionExitCleanly())
[email protected]2c5569662011-03-22 20:45:02136 return ViewMsg_Navigate_Type::RESTORE;
[email protected]5e369672009-11-03 23:48:30137
[email protected]2c5569662011-03-22 20:45:02138 return ViewMsg_Navigate_Type::NORMAL;
[email protected]5e369672009-11-03 23:48:30139}
140
[email protected]876bc832010-09-07 16:29:54141void MakeNavigateParams(const NavigationEntry& entry,
142 const NavigationController& controller,
[email protected]1ccb3568d2010-02-19 10:51:16143 NavigationController::ReloadType reload_type,
144 ViewMsg_Navigate_Params* params) {
[email protected]056de2d2009-06-26 16:41:34145 params->page_id = entry.page_id();
[email protected]876bc832010-09-07 16:29:54146 params->pending_history_list_offset = controller.GetIndexOfEntry(&entry);
[email protected]3cc72b12010-03-18 23:03:00147 params->current_history_list_offset = controller.last_committed_entry_index();
148 params->current_history_list_length = controller.entry_count();
[email protected]056de2d2009-06-26 16:41:34149 params->url = entry.url();
150 params->referrer = entry.referrer();
151 params->transition = entry.transition_type();
152 params->state = entry.content_state();
[email protected]3cc72b12010-03-18 23:03:00153 params->navigation_type =
154 GetNavigationType(controller.profile(), entry, reload_type);
[email protected]056de2d2009-06-26 16:41:34155 params->request_time = base::Time::Now();
156}
157
[email protected]420ae012009-04-24 05:16:32158} // namespace
159
[email protected]f4f50ef2011-01-21 19:01:19160
161// TabContents ----------------------------------------------------------------
[email protected]420ae012009-04-24 05:16:32162
[email protected]420ae012009-04-24 05:16:32163TabContents::TabContents(Profile* profile,
164 SiteInstance* site_instance,
165 int routing_id,
[email protected]6ee12c42010-09-14 09:36:07166 const TabContents* base_tab_contents,
167 SessionStorageNamespace* session_storage_namespace)
[email protected]b680ad22009-04-15 23:19:42168 : delegate_(NULL),
[email protected]6ee12c42010-09-14 09:36:07169 ALLOW_THIS_IN_INITIALIZER_LIST(controller_(
170 this, profile, session_storage_namespace)),
[email protected]66ba4932009-06-04 19:22:13171 ALLOW_THIS_IN_INITIALIZER_LIST(view_(
[email protected]d82ed61e2009-06-16 02:46:22172 TabContentsView::Create(this))),
173 ALLOW_THIS_IN_INITIALIZER_LIST(render_manager_(this, this)),
[email protected]d5f942ba2008-09-26 19:30:34174 is_loading_(false),
[email protected]443b80e2010-12-14 00:42:23175 crashed_status_(base::TERMINATION_STATUS_STILL_RUNNING),
176 crashed_error_code_(0),
[email protected]d5f942ba2008-09-26 19:30:34177 waiting_for_response_(false),
[email protected]d5f942ba2008-09-26 19:30:34178 max_page_id_(-1),
[email protected]fdd61c62009-04-22 19:22:57179 load_state_(net::LOAD_STATE_IDLE),
[email protected]094e5b22009-09-25 04:23:56180 upload_size_(0),
181 upload_position_(0),
[email protected]f17a0ee2010-05-17 17:38:47182 displayed_insecure_content_(false),
[email protected]fdd61c62009-04-22 19:22:57183 capturing_contents_(false),
184 is_being_destroyed_(false),
185 notify_disconnection_(false),
[email protected]fdd61c62009-04-22 19:22:57186#if defined(OS_WIN)
187 message_box_active_(CreateEvent(NULL, TRUE, FALSE, NULL)),
188#endif
[email protected]7ab1e7d62009-10-14 23:32:01189 is_showing_before_unload_dialog_(false),
[email protected]1fd1a502011-03-30 16:55:56190 opener_web_ui_type_(WebUI::kNoWebUI),
[email protected]ebf40a72010-07-22 01:46:38191 closed_by_user_gesture_(false),
[email protected]b75b8292010-10-01 07:28:25192 minimum_zoom_percent_(
193 static_cast<int>(WebKit::WebView::minTextSizeMultiplier * 100)),
194 maximum_zoom_percent_(
195 static_cast<int>(WebKit::WebView::maxTextSizeMultiplier * 100)),
[email protected]9e823662010-10-13 23:36:00196 temporary_zoom_settings_(false),
[email protected]4850a7f2011-03-08 23:36:59197 content_restrictions_(0) {
[email protected]7ff431e2010-01-07 18:18:54198
[email protected]12636df2009-09-28 22:32:21199 render_manager_.Init(profile, site_instance, routing_id);
[email protected]420ae012009-04-24 05:16:32200
[email protected]34ac70502009-09-25 17:07:23201 // We have the initial size of the view be based on the size of the passed in
202 // tab contents (normally a tab from the same window).
203 view_->CreateView(base_tab_contents ?
204 base_tab_contents->view()->GetContainerSize() : gfx::Size());
[email protected]332af7732009-03-11 13:21:35205}
initial.commit09911bf2008-07-26 23:55:29206
207TabContents::~TabContents() {
[email protected]420ae012009-04-24 05:16:32208 is_being_destroyed_ = true;
209
[email protected]3ab9cb82011-06-03 18:02:07210 // Clear out any JavaScript state.
211 if (delegate_)
212 delegate_->GetJavaScriptDialogCreator()->ResetJavaScriptState(this);
213
[email protected]420ae012009-04-24 05:16:32214 NotifyDisconnected();
[email protected]420ae012009-04-24 05:16:32215
[email protected]420ae012009-04-24 05:16:32216 // First cleanly close all child windows.
217 // TODO(mpcomplete): handle case if MaybeCloseChildWindows() already asked
218 // some of these to close. CloseWindows is async, so it might get called
219 // twice before it runs.
[email protected]8cc8d492010-02-02 10:40:49220 CloseConstrainedWindows();
[email protected]420ae012009-04-24 05:16:32221
[email protected]420ae012009-04-24 05:16:32222 // Notify any observer that have a reference on this tab contents.
223 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:27224 content::NOTIFICATION_TAB_CONTENTS_DESTROYED,
[email protected]420ae012009-04-24 05:16:32225 Source<TabContents>(this),
226 NotificationService::NoDetails());
227
228 // TODO(brettw) this should be moved to the view.
229#if defined(OS_WIN)
230 // If we still have a window handle, destroy it. GetNativeView can return
231 // NULL if this contents was part of a window that closed.
[email protected]50bd6452010-11-27 19:39:42232 if (GetNativeView()) {
233 RenderViewHost* host = render_view_host();
[email protected]b9a1fb42011-04-12 23:16:44234 if (host && host->view())
[email protected]50bd6452010-11-27 19:39:42235 host->view()->WillWmDestroy();
[email protected]50bd6452010-11-27 19:39:42236 }
[email protected]420ae012009-04-24 05:16:32237#endif
[email protected]7ab1e7d62009-10-14 23:32:01238
239 // OnCloseStarted isn't called in unit tests.
240 if (!tab_close_start_time_.is_null()) {
241 UMA_HISTOGRAM_TIMES("Tab.Close",
242 base::TimeTicks::Now() - tab_close_start_time_);
243 }
[email protected]b5a1d11c2011-02-17 03:09:42244
[email protected]07d490bc2011-03-07 17:05:26245 FOR_EACH_OBSERVER(TabContentsObserver, observers_, TabContentsDestroyed());
[email protected]232a5812011-03-04 22:42:08246
[email protected]1de2b8b2011-06-29 19:38:46247 set_delegate(NULL);
248}
249
250// TODO(cbentzel): Either remove the debugging code, or rename to SetDelegate.
251void TabContents::set_delegate(TabContentsDelegate* delegate) {
252 if (delegate == delegate_)
253 return;
254 if (delegate_)
255 delegate_->Detach(this);
256 delegate_ = delegate;
257 if (delegate_)
258 delegate_->Attach(this);
[email protected]b5a1d11c2011-02-17 03:09:42259}
260
[email protected]724159a2010-12-30 01:11:18261bool TabContents::OnMessageReceived(const IPC::Message& message) {
[email protected]f82d57b52011-04-27 19:13:17262 if (web_ui() && web_ui()->OnMessageReceived(message))
263 return true;
264
[email protected]585b30362011-01-28 02:30:17265 ObserverListBase<TabContentsObserver>::Iterator it(observers_);
266 TabContentsObserver* observer;
[email protected]0f180ee2011-01-21 18:23:10267 while ((observer = it.GetNext()) != NULL)
268 if (observer->OnMessageReceived(message))
[email protected]403415a2011-01-10 18:57:53269 return true;
[email protected]403415a2011-01-10 18:57:53270
[email protected]724159a2010-12-30 01:11:18271 bool handled = true;
272 bool message_is_ok = true;
273 IPC_BEGIN_MESSAGE_MAP_EX(TabContents, message, message_is_ok)
274 IPC_MESSAGE_HANDLER(ViewHostMsg_DidStartProvisionalLoadForFrame,
275 OnDidStartProvisionalLoadForFrame)
276 IPC_MESSAGE_HANDLER(ViewHostMsg_DidRedirectProvisionalLoad,
277 OnDidRedirectProvisionalLoad)
278 IPC_MESSAGE_HANDLER(ViewHostMsg_DidFailProvisionalLoadWithError,
279 OnDidFailProvisionalLoadWithError)
280 IPC_MESSAGE_HANDLER(ViewHostMsg_DidLoadResourceFromMemoryCache,
281 OnDidLoadResourceFromMemoryCache)
282 IPC_MESSAGE_HANDLER(ViewHostMsg_DidDisplayInsecureContent,
283 OnDidDisplayInsecureContent)
284 IPC_MESSAGE_HANDLER(ViewHostMsg_DidRunInsecureContent,
285 OnDidRunInsecureContent)
286 IPC_MESSAGE_HANDLER(ViewHostMsg_DocumentLoadedInFrame,
287 OnDocumentLoadedInFrame)
288 IPC_MESSAGE_HANDLER(ViewHostMsg_DidFinishLoad, OnDidFinishLoad)
[email protected]c8f73ab2011-01-22 00:05:17289 IPC_MESSAGE_HANDLER(ViewHostMsg_UpdateContentRestrictions,
290 OnUpdateContentRestrictions)
[email protected]7d472472011-01-22 01:30:25291 IPC_MESSAGE_HANDLER(ViewHostMsg_GoToEntryAtOffset, OnGoToEntryAtOffset)
[email protected]216813952011-05-19 22:21:26292 IPC_MESSAGE_HANDLER(ViewHostMsg_UpdateZoomLimits, OnUpdateZoomLimits)
293 IPC_MESSAGE_HANDLER(ViewHostMsg_FocusedNodeChanged, OnFocusedNodeChanged)
[email protected]c7dd2f62011-07-18 15:57:59294 IPC_MESSAGE_HANDLER(ViewHostMsg_SaveURLAs, OnSaveURL)
[email protected]724159a2010-12-30 01:11:18295 IPC_MESSAGE_UNHANDLED(handled = false)
296 IPC_END_MESSAGE_MAP_EX()
297
298 if (!message_is_ok) {
299 UserMetrics::RecordAction(UserMetricsAction("BadMessageTerminate_RVD"));
300 GetRenderProcessHost()->ReceivedBadMessage();
301 }
302
303 return handled;
304}
305
[email protected]13367f72009-07-08 16:39:02306// Returns true if contains content rendered by an extension.
307bool TabContents::HostsExtension() const {
308 return GetURL().SchemeIs(chrome::kExtensionScheme);
[email protected]d5f942ba2008-09-26 19:30:34309}
310
[email protected]8cb5d5b2010-02-09 11:36:16311RenderProcessHost* TabContents::GetRenderProcessHost() const {
312 return render_manager_.current_host()->process();
313}
314
[email protected]d5f942ba2008-09-26 19:30:34315const GURL& TabContents::GetURL() const {
316 // We may not have a navigation entry yet
[email protected]ce3fa3c2009-04-20 19:55:57317 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]ebe89e062009-08-13 23:16:54318 return entry ? entry->virtual_url() : GURL::EmptyGURL();
[email protected]d5f942ba2008-09-26 19:30:34319}
320
[email protected]96d185d2009-04-24 03:28:54321const string16& TabContents::GetTitle() const {
[email protected]4c6092c5b2009-06-06 00:23:55322 // Transient entries take precedence. They are used for interstitial pages
323 // that are shown on top of existing pages.
324 NavigationEntry* entry = controller_.GetTransientEntry();
[email protected]b5cca982011-05-26 04:42:08325 std::string accept_languages =
326 content::GetContentClient()->browser()->GetAcceptLangs(this);
[email protected]45d0ef7f2011-01-05 13:46:23327 if (entry) {
[email protected]b5cca982011-05-26 04:42:08328 return entry->GetTitleForDisplay(accept_languages);
[email protected]45d0ef7f2011-01-05 13:46:23329 }
[email protected]7ade2732011-02-10 00:13:58330 WebUI* our_web_ui = render_manager_.pending_web_ui() ?
331 render_manager_.pending_web_ui() : render_manager_.web_ui();
332 if (our_web_ui) {
[email protected]96d185d2009-04-24 03:28:54333 // Don't override the title in view source mode.
[email protected]4c6092c5b2009-06-06 00:23:55334 entry = controller_.GetActiveEntry();
[email protected]96d185d2009-04-24 03:28:54335 if (!(entry && entry->IsViewSourceMode())) {
[email protected]e0112912011-02-02 22:54:35336 // Give the Web UI the chance to override our title.
[email protected]7ade2732011-02-10 00:13:58337 const string16& title = our_web_ui->overridden_title();
[email protected]96d185d2009-04-24 03:28:54338 if (!title.empty())
339 return title;
340 }
341 }
342
343 // We use the title for the last committed entry rather than a pending
344 // navigation entry. For example, when the user types in a URL, we want to
345 // keep the old page's title until the new load has committed and we get a new
346 // title.
[email protected]96d185d2009-04-24 03:28:54347 entry = controller_.GetLastCommittedEntry();
[email protected]45d0ef7f2011-01-05 13:46:23348 if (entry) {
[email protected]b5cca982011-05-26 04:42:08349 return entry->GetTitleForDisplay(accept_languages);
[email protected]45d0ef7f2011-01-05 13:46:23350 }
[email protected]987fc3a2011-05-26 14:18:09351
352 // |page_title_when_no_navigation_entry_| is finally used
353 // if no title cannot be retrieved.
354 return page_title_when_no_navigation_entry_;
[email protected]96d185d2009-04-24 03:28:54355}
356
[email protected]d5f942ba2008-09-26 19:30:34357int32 TabContents::GetMaxPageID() {
358 if (GetSiteInstance())
359 return GetSiteInstance()->max_page_id();
360 else
361 return max_page_id_;
362}
363
364void TabContents::UpdateMaxPageID(int32 page_id) {
365 // Ensure both the SiteInstance and RenderProcessHost update their max page
[email protected]57c6a652009-05-04 07:58:34366 // IDs in sync. Only TabContents will also have site instances, except during
[email protected]d5f942ba2008-09-26 19:30:34367 // testing.
368 if (GetSiteInstance())
369 GetSiteInstance()->UpdateMaxPageID(page_id);
[email protected]8cb5d5b2010-02-09 11:36:16370 GetRenderProcessHost()->UpdateMaxPageID(page_id);
[email protected]d5f942ba2008-09-26 19:30:34371}
372
[email protected]96d185d2009-04-24 03:28:54373SiteInstance* TabContents::GetSiteInstance() const {
374 return render_manager_.current_host()->site_instance();
375}
376
[email protected]96d185d2009-04-24 03:28:54377bool TabContents::ShouldDisplayURL() {
[email protected]8ab24cc2009-11-13 20:56:15378 // Don't hide the url in view source mode and with interstitials.
[email protected]96d185d2009-04-24 03:28:54379 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]8ab24cc2009-11-13 20:56:15380 if (entry && (entry->IsViewSourceMode() ||
[email protected]cccd3762010-11-12 18:40:01381 entry->page_type() == INTERSTITIAL_PAGE)) {
[email protected]96d185d2009-04-24 03:28:54382 return true;
[email protected]8ab24cc2009-11-13 20:56:15383 }
[email protected]83e3895b2009-06-11 00:07:16384
[email protected]2b9a06402011-02-16 04:57:10385 // We always display the URL for non-WebUI URLs to prevent spoofing.
[email protected]1fd1a502011-03-30 16:55:56386 if (entry && !content::WebUIFactory::Get()->HasWebUIScheme(entry->url()))
[email protected]2b9a06402011-02-16 04:57:10387 return true;
388
[email protected]d0980792011-02-13 19:41:40389 WebUI* web_ui = GetWebUIForCurrentState();
[email protected]7ade2732011-02-10 00:13:58390 if (web_ui)
391 return !web_ui->should_hide_url();
[email protected]96d185d2009-04-24 03:28:54392 return true;
393}
394
[email protected]585b30362011-01-28 02:30:17395void TabContents::AddObserver(TabContentsObserver* observer) {
396 observers_.AddObserver(observer);
[email protected]3c9e1872010-11-18 16:17:49397}
398
[email protected]585b30362011-01-28 02:30:17399void TabContents::RemoveObserver(TabContentsObserver* observer) {
400 observers_.RemoveObserver(observer);
[email protected]3c9e1872010-11-18 16:17:49401}
402
[email protected]443b80e2010-12-14 00:42:23403void TabContents::SetIsCrashed(base::TerminationStatus status, int error_code) {
404 if (status == crashed_status_)
[email protected]d5f942ba2008-09-26 19:30:34405 return;
406
[email protected]443b80e2010-12-14 00:42:23407 crashed_status_ = status;
408 crashed_error_code_ = error_code;
[email protected]c9cd2222009-05-06 05:16:50409 NotifyNavigationStateChanged(INVALIDATE_TAB);
[email protected]d5f942ba2008-09-26 19:30:34410}
411
412void TabContents::NotifyNavigationStateChanged(unsigned changed_flags) {
413 if (delegate_)
414 delegate_->NavigationStateChanged(this, changed_flags);
415}
416
[email protected]96d185d2009-04-24 03:28:54417void TabContents::DidBecomeSelected() {
418 controller_.SetActive(true);
[email protected]8cb5d5b2010-02-09 11:36:16419 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
420 if (rwhv) {
421 rwhv->DidBecomeSelected();
[email protected]789e9152009-08-04 21:59:43422#if defined(OS_MACOSX)
[email protected]8cb5d5b2010-02-09 11:36:16423 rwhv->SetActive(true);
[email protected]789e9152009-08-04 21:59:43424#endif
425 }
[email protected]96d185d2009-04-24 03:28:54426
[email protected]5ac20162010-11-24 23:33:11427 last_selected_time_ = base::TimeTicks::Now();
[email protected]3e69bc82011-05-26 23:22:38428
429 FOR_EACH_OBSERVER(TabContentsObserver, observers_, DidBecomeSelected());
[email protected]96d185d2009-04-24 03:28:54430}
431
432void TabContents::WasHidden() {
433 if (!capturing_contents()) {
434 // |render_view_host()| can be NULL if the user middle clicks a link to open
435 // a tab in then background, then closes the tab before selecting it. This
436 // is because closing the tab calls TabContents::Destroy(), which removes
437 // the |render_view_host()|; then when we actually destroy the window,
438 // OnWindowPosChanged() notices and calls HideContents() (which calls us).
[email protected]8cb5d5b2010-02-09 11:36:16439 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
440 if (rwhv)
441 rwhv->WasHidden();
[email protected]96d185d2009-04-24 03:28:54442 }
443
444 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:27445 content::NOTIFICATION_TAB_CONTENTS_HIDDEN,
[email protected]96d185d2009-04-24 03:28:54446 Source<TabContents>(this),
447 NotificationService::NoDetails());
448}
449
[email protected]d5f942ba2008-09-26 19:30:34450void TabContents::Activate() {
451 if (delegate_)
452 delegate_->ActivateContents(this);
453}
454
[email protected]ea42e7782010-08-23 23:58:12455void TabContents::Deactivate() {
456 if (delegate_)
457 delegate_->DeactivateContents(this);
458}
459
[email protected]63954792011-07-11 04:17:48460void TabContents::LostCapture() {
461 if (delegate_)
462 delegate_->LostCapture();
463}
464
465bool TabContents::PreHandleKeyboardEvent(const NativeWebKeyboardEvent& event,
466 bool* is_keyboard_shortcut) {
467 return delegate_ &&
468 delegate_->PreHandleKeyboardEvent(event, is_keyboard_shortcut);
469}
470
471void TabContents::HandleKeyboardEvent(const NativeWebKeyboardEvent& event) {
472 if (delegate_)
473 delegate_->HandleKeyboardEvent(event);
474}
475
476void TabContents::HandleMouseUp() {
477 if (delegate_)
478 delegate_->HandleMouseUp();
479}
480
481void TabContents::HandleMouseActivate() {
482 if (delegate_)
483 delegate_->HandleMouseActivate();
484}
485
[email protected]96d185d2009-04-24 03:28:54486void TabContents::ShowContents() {
[email protected]8cb5d5b2010-02-09 11:36:16487 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
488 if (rwhv)
489 rwhv->DidBecomeSelected();
[email protected]96d185d2009-04-24 03:28:54490}
491
492void TabContents::HideContents() {
493 // TODO(pkasting): http://b/1239839 Right now we purposefully don't call
494 // our superclass HideContents(), because some callers want to be very picky
495 // about the order in which these get called. In addition to making the code
496 // here practically impossible to understand, this also means we end up
497 // calling TabContents::WasHidden() twice if callers call both versions of
[email protected]57c6a652009-05-04 07:58:34498 // HideContents() on a TabContents.
[email protected]96d185d2009-04-24 03:28:54499 WasHidden();
500}
501
[email protected]5aab5e22010-12-08 22:13:29502bool TabContents::NeedToFireBeforeUnload() {
503 // TODO(creis): Should we fire even for interstitial pages?
504 return notify_disconnection() &&
505 !showing_interstitial_page() &&
506 !render_view_host()->SuddenTerminationAllowed();
507}
508
[email protected]c0588052008-10-27 23:01:50509void TabContents::OpenURL(const GURL& url, const GURL& referrer,
[email protected]d5f942ba2008-09-26 19:30:34510 WindowOpenDisposition disposition,
511 PageTransition::Type transition) {
[email protected]1c642b52011-04-15 09:05:49512 if (delegate_) {
[email protected]c0588052008-10-27 23:01:50513 delegate_->OpenURLFromTab(this, url, referrer, disposition, transition);
[email protected]1c642b52011-04-15 09:05:49514 // Notify observers.
515 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
516 DidOpenURL(url, referrer, disposition, transition));
517 }
[email protected]d5f942ba2008-09-26 19:30:34518}
519
[email protected]1ccb3568d2010-02-19 10:51:16520bool TabContents::NavigateToPendingEntry(
521 NavigationController::ReloadType reload_type) {
[email protected]876bc832010-09-07 16:29:54522 return NavigateToEntry(*controller_.pending_entry(), reload_type);
523}
[email protected]96d185d2009-04-24 03:28:54524
[email protected]876bc832010-09-07 16:29:54525bool TabContents::NavigateToEntry(
526 const NavigationEntry& entry,
527 NavigationController::ReloadType reload_type) {
[email protected]96d185d2009-04-24 03:28:54528 RenderViewHost* dest_render_view_host = render_manager_.Navigate(entry);
529 if (!dest_render_view_host)
530 return false; // Unable to create the desired render view host.
531
[email protected]80a8fad2011-01-29 04:02:38532 // For security, we should never send non-Web-UI URLs to a Web UI renderer.
[email protected]05fdd492010-11-15 17:52:07533 // Double check that here.
[email protected]54ec6472010-04-09 19:39:58534 int enabled_bindings = dest_render_view_host->enabled_bindings();
[email protected]1fd1a502011-03-30 16:55:56535 bool is_allowed_in_web_ui_renderer = content::GetContentClient()->
536 browser()->GetWebUIFactory()->IsURLAcceptableForWebUI(profile(),
537 entry.url());
[email protected]c09163a2011-02-15 00:05:55538 CHECK(!BindingsPolicy::is_web_ui_enabled(enabled_bindings) ||
539 is_allowed_in_web_ui_renderer);
[email protected]54ec6472010-04-09 19:39:58540
[email protected]96d185d2009-04-24 03:28:54541 // Tell DevTools agent that it is attached prior to the navigation.
[email protected]d9f9b792009-06-24 13:17:12542 DevToolsManager* devtools_manager = DevToolsManager::GetInstance();
[email protected]7e8e6b62009-05-08 11:28:32543 if (devtools_manager) { // NULL in unit tests.
[email protected]3cc72b12010-03-18 23:03:00544 devtools_manager->OnNavigatingToPendingEntry(render_view_host(),
545 dest_render_view_host,
546 entry.url());
[email protected]7e8e6b62009-05-08 11:28:32547 }
[email protected]96d185d2009-04-24 03:28:54548
549 // Used for page load time metrics.
550 current_load_start_ = base::TimeTicks::Now();
551
552 // Navigate in the desired RenderViewHost.
[email protected]056de2d2009-06-26 16:41:34553 ViewMsg_Navigate_Params navigate_params;
[email protected]876bc832010-09-07 16:29:54554 MakeNavigateParams(entry, controller_, reload_type, &navigate_params);
[email protected]52c68652010-12-07 17:47:04555 if (delegate_) {
556 navigate_params.extra_headers =
557 delegate_->GetNavigationHeaders(navigate_params.url);
558 }
[email protected]056de2d2009-06-26 16:41:34559 dest_render_view_host->Navigate(navigate_params);
[email protected]96d185d2009-04-24 03:28:54560
561 if (entry.page_id() == -1) {
562 // HACK!! This code suppresses javascript: URLs from being added to
563 // session history, which is what we want to do for javascript: URLs that
564 // do not generate content. What we really need is a message from the
565 // renderer telling us that a new page was not created. The same message
566 // could be used for mailto: URLs and the like.
567 if (entry.url().SchemeIs(chrome::kJavaScriptScheme))
568 return false;
569 }
570
[email protected]3c9e1872010-11-18 16:17:49571 // Notify observers about navigation.
[email protected]b375c5d2011-05-03 21:15:04572 FOR_EACH_OBSERVER(TabContentsObserver,
573 observers_,
574 NavigateToPendingEntry(entry.url(), reload_type));
[email protected]96d185d2009-04-24 03:28:54575
[email protected]09b29342011-06-24 19:18:48576 if (delegate_)
577 delegate_->DidNavigateToPendingEntry(this);
578
[email protected]d5f942ba2008-09-26 19:30:34579 return true;
580}
581
[email protected]96d185d2009-04-24 03:28:54582void TabContents::Stop() {
583 render_manager_.Stop();
[email protected]2e3f4572011-03-25 19:24:47584 FOR_EACH_OBSERVER(TabContentsObserver, observers_, StopNavigation());
[email protected]96d185d2009-04-24 03:28:54585}
586
[email protected]96d185d2009-04-24 03:28:54587TabContents* TabContents::Clone() {
588 // We create a new SiteInstance so that the new tab won't share processes
589 // with the old one. This can be changed in the future if we need it to share
590 // processes for some reason.
[email protected]420ae012009-04-24 05:16:32591 TabContents* tc = new TabContents(profile(),
[email protected]96d185d2009-04-24 03:28:54592 SiteInstance::CreateSiteInstance(profile()),
[email protected]6ee12c42010-09-14 09:36:07593 MSG_ROUTING_NONE, this, NULL);
[email protected]96d185d2009-04-24 03:28:54594 tc->controller().CopyStateFrom(controller_);
595 return tc;
596}
597
[email protected]4d677202009-07-19 07:37:12598void TabContents::ShowPageInfo(const GURL& url,
599 const NavigationEntry::SSLStatus& ssl,
600 bool show_history) {
601 if (!delegate_)
602 return;
603
[email protected]bb678332009-07-21 00:15:50604 delegate_->ShowPageInfo(profile(), url, ssl, show_history);
[email protected]4d677202009-07-19 07:37:12605}
606
initial.commit09911bf2008-07-26 23:55:29607ConstrainedWindow* TabContents::CreateConstrainedDialog(
[email protected]e8382172009-06-19 22:16:28608 ConstrainedWindowDelegate* delegate) {
initial.commit09911bf2008-07-26 23:55:29609 ConstrainedWindow* window =
[email protected]e8382172009-06-19 22:16:28610 ConstrainedWindow::CreateConstrainedDialog(this, delegate);
[email protected]aed59602011-02-28 22:57:33611 AddConstrainedDialog(window);
612 return window;
613}
614
615void TabContents::AddConstrainedDialog(ConstrainedWindow* window) {
initial.commit09911bf2008-07-26 23:55:29616 child_windows_.push_back(window);
[email protected]fa1cf0b82010-01-15 21:49:44617
618 if (child_windows_.size() == 1) {
619 window->ShowConstrainedWindow();
620 BlockTabContent(true);
621 }
initial.commit09911bf2008-07-26 23:55:29622}
623
[email protected]fa1cf0b82010-01-15 21:49:44624void TabContents::BlockTabContent(bool blocked) {
[email protected]8dccd7cb2010-02-25 05:19:55625 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
[email protected]1b072562010-12-13 19:37:45626 // 70% opaque grey.
627 SkColor greyish = SkColorSetARGB(178, 0, 0, 0);
[email protected]8dccd7cb2010-02-25 05:19:55628 if (rwhv)
[email protected]1b072562010-12-13 19:37:45629 rwhv->SetVisuallyDeemphasized(blocked ? &greyish : NULL, false);
[email protected]a7eccac2011-04-15 03:05:19630 // RenderViewHost may be NULL during shutdown.
631 if (render_view_host())
632 render_view_host()->set_ignore_input_events(blocked);
[email protected]fa1cf0b82010-01-15 21:49:44633 if (delegate_)
634 delegate_->SetTabContentBlocked(this, blocked);
635}
636
[email protected]e7cfdbd2011-04-22 14:41:37637void TabContents::AddNewContents(TabContents* new_contents,
638 WindowOpenDisposition disposition,
639 const gfx::Rect& initial_pos,
640 bool user_gesture) {
initial.commit09911bf2008-07-26 23:55:29641 if (!delegate_)
642 return;
643
[email protected]e7cfdbd2011-04-22 14:41:37644 delegate_->AddNewContents(this, new_contents, disposition, initial_pos,
645 user_gesture);
[email protected]96d185d2009-04-24 03:28:54646}
647
[email protected]5c9e97a2009-09-09 23:48:30648gfx::NativeView TabContents::GetContentNativeView() const {
[email protected]96d185d2009-04-24 03:28:54649 return view_->GetContentNativeView();
650}
651
652gfx::NativeView TabContents::GetNativeView() const {
653 return view_->GetNativeView();
654}
655
656void TabContents::GetContainerBounds(gfx::Rect *out) const {
657 view_->GetContainerBounds(out);
658}
659
660void TabContents::Focus() {
661 view_->Focus();
662}
663
[email protected]90daadb42009-06-08 21:27:28664void TabContents::FocusThroughTabTraversal(bool reverse) {
[email protected]7e383692009-06-12 19:14:54665 if (showing_interstitial_page()) {
666 render_manager_.interstitial_page()->FocusThroughTabTraversal(reverse);
667 return;
668 }
[email protected]96d185d2009-04-24 03:28:54669 render_view_host()->SetInitialFocus(reverse);
670}
671
672bool TabContents::FocusLocationBarByDefault() {
[email protected]d0980792011-02-13 19:41:40673 WebUI* web_ui = GetWebUIForCurrentState();
[email protected]7ade2732011-02-10 00:13:58674 if (web_ui)
675 return web_ui->focus_location_bar_by_default();
[email protected]3e3f0eb2009-06-22 18:33:43676 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]e0d481582009-09-15 21:06:25677 if (entry && entry->url() == GURL(chrome::kAboutBlankURL))
[email protected]3e3f0eb2009-06-22 18:33:43678 return true;
[email protected]96d185d2009-04-24 03:28:54679 return false;
680}
681
[email protected]a26dc362010-04-23 01:48:58682void TabContents::SetFocusToLocationBar(bool select_all) {
[email protected]a11aaf22010-03-30 00:03:38683 if (delegate())
[email protected]a26dc362010-04-23 01:48:58684 delegate()->SetFocusToLocationBar(select_all);
[email protected]a11aaf22010-03-30 00:03:38685}
686
[email protected]686493142011-07-15 21:47:22687bool TabContents::CanDownload(int request_id) {
688 TabContentsDelegate* d = delegate();
689 if (d)
690 return d->CanDownload(this, request_id);
691 return true;
692}
693
694void TabContents::OnStartDownload(DownloadItem* download) {
695 TabContentsDelegate* d = delegate();
696 if (d)
697 d->OnStartDownload(this, download);
698}
699
[email protected]d5f942ba2008-09-26 19:30:34700void TabContents::WillClose(ConstrainedWindow* window) {
[email protected]d6219912010-11-17 19:47:37701 ConstrainedWindowList::iterator i(
702 std::find(child_windows_.begin(), child_windows_.end(), window));
703 bool removed_topmost_window = i == child_windows_.begin();
704 if (i != child_windows_.end())
705 child_windows_.erase(i);
706 if (child_windows_.empty()) {
[email protected]fa1cf0b82010-01-15 21:49:44707 BlockTabContent(false);
[email protected]d6219912010-11-17 19:47:37708 } else {
709 if (removed_topmost_window)
710 child_windows_[0]->ShowConstrainedWindow();
711 BlockTabContent(true);
[email protected]fa1cf0b82010-01-15 21:49:44712 }
[email protected]66ba4932009-06-04 19:22:13713}
[email protected]d5f942ba2008-09-26 19:30:34714
[email protected]c7dd2f62011-07-18 15:57:59715void TabContents::OnSavePage() {
716 // If we can not save the page, try to download it.
717 if (!SavePackage::IsSavableContents(contents_mime_type())) {
718 DownloadManager* dlm = profile()->GetDownloadManager();
719 const GURL& current_page_url = GetURL();
720 if (dlm && current_page_url.is_valid()) {
721 dlm->DownloadUrl(current_page_url, GURL(), "", this);
722 download_util::RecordDownloadCount(
723 download_util::INITIATED_BY_SAVE_PACKAGE_FAILURE_COUNT);
724 return;
725 }
726 }
727
728 Stop();
729
730 // Create the save package and possibly prompt the user for the name to save
731 // the page as. The user prompt is an asynchronous operation that runs on
732 // another thread.
733 save_package_ = new SavePackage(this);
734 save_package_->GetSaveInfo();
735}
736
737// Used in automated testing to bypass prompting the user for file names.
738// Instead, the names and paths are hard coded rather than running them through
739// file name sanitation and extension / mime checking.
740bool TabContents::SavePage(const FilePath& main_file, const FilePath& dir_path,
741 SavePackage::SavePackageType save_type) {
742 // Stop the page from navigating.
743 Stop();
744
745 save_package_ = new SavePackage(this, save_type, main_file, dir_path);
746 return save_package_->Init();
747}
748
749void TabContents::OnSaveURL(const GURL& url) {
750 DownloadManager* dlm = profile()->GetDownloadManager();
751 dlm->DownloadUrl(url, GetURL(), "", this);
752}
753
[email protected]420ae012009-04-24 05:16:32754bool TabContents::IsActiveEntry(int32 page_id) {
755 NavigationEntry* active_entry = controller_.GetActiveEntry();
756 return (active_entry != NULL &&
757 active_entry->site_instance() == GetSiteInstance() &&
758 active_entry->page_id() == page_id);
759}
760
[email protected]b2fe07d12010-02-09 14:38:08761void TabContents::SetOverrideEncoding(const std::string& encoding) {
[email protected]8cb5d5b2010-02-09 11:36:16762 set_encoding(encoding);
[email protected]216813952011-05-19 22:21:26763 render_view_host()->Send(new ViewMsg_SetPageEncoding(
764 render_view_host()->routing_id(), encoding));
[email protected]8cb5d5b2010-02-09 11:36:16765}
766
[email protected]b2fe07d12010-02-09 14:38:08767void TabContents::ResetOverrideEncoding() {
[email protected]8cb5d5b2010-02-09 11:36:16768 reset_encoding();
[email protected]216813952011-05-19 22:21:26769 render_view_host()->Send(new ViewMsg_ResetPageEncodingToDefault(
770 render_view_host()->routing_id()));
[email protected]8cb5d5b2010-02-09 11:36:16771}
772
[email protected]7ab1e7d62009-10-14 23:32:01773void TabContents::OnCloseStarted() {
774 if (tab_close_start_time_.is_null())
775 tab_close_start_time_ = base::TimeTicks::Now();
776}
777
[email protected]46624bf2010-06-09 16:04:19778bool TabContents::ShouldAcceptDragAndDrop() const {
779#if defined(OS_CHROMEOS)
780 // ChromeOS panels (pop-ups) do not take drag-n-drop.
781 // See http://crosbug.com/2413
[email protected]b35b26b32011-05-05 20:35:14782 if (delegate() && delegate()->IsPopupOrPanel(this))
[email protected]b83af492010-10-09 03:21:20783 return false;
784 return true;
[email protected]46624bf2010-06-09 16:04:19785#else
786 return true;
787#endif
788}
789
[email protected]7813bd72011-02-05 02:19:34790void TabContents::SystemDragEnded() {
791 if (render_view_host())
792 render_view_host()->DragSourceSystemDragEnded();
793 if (delegate())
794 delegate()->DragEnded();
795}
796
[email protected]d0b8d092010-10-25 04:05:17797double TabContents::GetZoomLevel() const {
[email protected]b75b8292010-10-01 07:28:25798 HostZoomMap* zoom_map = profile()->GetHostZoomMap();
799 if (!zoom_map)
[email protected]d0b8d092010-10-25 04:05:17800 return 0;
[email protected]b75b8292010-10-01 07:28:25801
802 double zoom_level;
803 if (temporary_zoom_settings_) {
804 zoom_level = zoom_map->GetTemporaryZoomLevel(
805 GetRenderProcessHost()->id(), render_view_host()->routing_id());
806 } else {
[email protected]13ffa32e2011-05-27 16:37:01807 zoom_level = zoom_map->GetZoomLevel(net::GetHostOrSpecFromURL(GetURL()));
[email protected]b75b8292010-10-01 07:28:25808 }
[email protected]d0b8d092010-10-25 04:05:17809 return zoom_level;
810}
[email protected]b75b8292010-10-01 07:28:25811
[email protected]d0b8d092010-10-25 04:05:17812int TabContents::GetZoomPercent(bool* enable_increment,
813 bool* enable_decrement) {
814 *enable_decrement = *enable_increment = false;
[email protected]b75b8292010-10-01 07:28:25815 int percent = static_cast<int>(
[email protected]d0b8d092010-10-25 04:05:17816 WebKit::WebView::zoomLevelToZoomFactor(GetZoomLevel()) * 100);
[email protected]b75b8292010-10-01 07:28:25817 *enable_decrement = percent > minimum_zoom_percent_;
818 *enable_increment = percent < maximum_zoom_percent_;
819 return percent;
820}
821
[email protected]3c733bde2010-12-21 19:56:31822void TabContents::ViewSource() {
[email protected]1788e772010-12-15 16:40:50823 if (!delegate_)
824 return;
825
826 NavigationEntry* active_entry = controller().GetActiveEntry();
827 if (!active_entry)
828 return;
829
830 delegate_->ViewSourceForTab(this, active_entry->url());
[email protected]77d8d622010-12-15 10:30:12831}
832
[email protected]932b7a12011-03-09 12:50:27833void TabContents::ViewFrameSource(const GURL& url,
834 const std::string& content_state) {
835 if (!delegate_)
836 return;
837
838 delegate_->ViewSourceForFrame(this, url, content_state);
839}
840
[email protected]c40d6232011-03-25 00:16:21841void TabContents::SetContentRestrictions(int restrictions) {
842 content_restrictions_ = restrictions;
843 delegate()->ContentRestrictionsChanged(this);
844}
845
[email protected]724159a2010-12-30 01:11:18846void TabContents::OnDidStartProvisionalLoadForFrame(int64 frame_id,
847 bool is_main_frame,
[email protected]eacb080b2011-05-22 19:40:26848 bool has_opener_set,
[email protected]724159a2010-12-30 01:11:18849 const GURL& url) {
850 bool is_error_page = (url.spec() == chrome::kUnreachableWebDataURL);
851 GURL validated_url(url);
852 render_view_host()->FilterURL(ChildProcessSecurityPolicy::GetInstance(),
853 GetRenderProcessHost()->id(), &validated_url);
854
[email protected]8093a542011-05-13 07:29:32855 RenderViewHost* rvh =
856 render_manager_.pending_render_view_host() ?
857 render_manager_.pending_render_view_host() : render_view_host();
[email protected]0d60f0192011-04-14 12:40:10858 // Notify observers about the start of the provisional load.
859 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
860 DidStartProvisionalLoadForFrame(frame_id, is_main_frame,
[email protected]8093a542011-05-13 07:29:32861 validated_url, is_error_page, rvh));
[email protected]0d60f0192011-04-14 12:40:10862
[email protected]724159a2010-12-30 01:11:18863 if (is_main_frame) {
[email protected]4850a7f2011-03-08 23:36:59864 // Notify observers about the provisional change in the main frame URL.
865 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
[email protected]eacb080b2011-05-22 19:40:26866 ProvisionalChangeToMainFrameUrl(url, has_opener_set));
[email protected]724159a2010-12-30 01:11:18867 }
868}
869
870void TabContents::OnDidRedirectProvisionalLoad(int32 page_id,
[email protected]eacb080b2011-05-22 19:40:26871 bool has_opener_set,
[email protected]724159a2010-12-30 01:11:18872 const GURL& source_url,
873 const GURL& target_url) {
[email protected]18ba2b12011-04-06 16:35:49874 // TODO(creis): Remove this method and have the pre-rendering code listen to
875 // the ResourceDispatcherHost's RESOURCE_RECEIVED_REDIRECT notification
876 // instead. See http://crbug.com/78512.
[email protected]724159a2010-12-30 01:11:18877 NavigationEntry* entry;
878 if (page_id == -1)
879 entry = controller_.pending_entry();
880 else
881 entry = controller_.GetEntryWithPageID(GetSiteInstance(), page_id);
882 if (!entry || entry->url() != source_url)
883 return;
[email protected]e7d50892011-01-19 21:47:38884
[email protected]4850a7f2011-03-08 23:36:59885 // Notify observers about the provisional change in the main frame URL.
886 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
[email protected]eacb080b2011-05-22 19:40:26887 ProvisionalChangeToMainFrameUrl(target_url,
888 has_opener_set));
[email protected]724159a2010-12-30 01:11:18889}
890
891void TabContents::OnDidFailProvisionalLoadWithError(
892 int64 frame_id,
893 bool is_main_frame,
894 int error_code,
895 const GURL& url,
896 bool showing_repost_interstitial) {
897 VLOG(1) << "Failed Provisional Load: " << url.possibly_invalid_spec()
898 << ", error_code: " << error_code
899 << " is_main_frame: " << is_main_frame
900 << " showing_repost_interstitial: " << showing_repost_interstitial
901 << " frame_id: " << frame_id;
902 GURL validated_url(url);
903 render_view_host()->FilterURL(ChildProcessSecurityPolicy::GetInstance(),
904 GetRenderProcessHost()->id(), &validated_url);
905
906 if (net::ERR_ABORTED == error_code) {
907 // EVIL HACK ALERT! Ignore failed loads when we're showing interstitials.
908 // This means that the interstitial won't be torn down properly, which is
909 // bad. But if we have an interstitial, go back to another tab type, and
910 // then load the same interstitial again, we could end up getting the first
911 // interstitial's "failed" message (as a result of the cancel) when we're on
912 // the second one.
913 //
914 // We can't tell this apart, so we think we're tearing down the current page
915 // which will cause a crash later one. There is also some code in
916 // RenderViewHostManager::RendererAbortedProvisionalLoad that is commented
917 // out because of this problem.
918 //
919 // http://code.google.com/p/chromium/issues/detail?id=2855
920 // Because this will not tear down the interstitial properly, if "back" is
921 // back to another tab type, the interstitial will still be somewhat alive
922 // in the previous tab type. If you navigate somewhere that activates the
923 // tab with the interstitial again, you'll see a flash before the new load
924 // commits of the interstitial page.
925 if (showing_interstitial_page()) {
926 LOG(WARNING) << "Discarding message during interstitial.";
927 return;
928 }
929
[email protected]02102f82011-06-13 20:37:02930 // Discard our pending entry if the load canceled (e.g. if we decided to
931 // download the file instead of load it). We do not verify that the URL
932 // being canceled matches the pending entry's URL because they will not
933 // match if a redirect occurred (in which case we do not want to leave a
934 // stale redirect URL showing). This means that we also cancel the pending
935 // entry if the user started a new navigation. As a result, the navigation
936 // controller may not remember that a load is in progress, but the
937 // navigation will still commit even if there is no pending entry.
[email protected]724159a2010-12-30 01:11:18938 NavigationEntry* pending_entry = controller_.pending_entry();
[email protected]02102f82011-06-13 20:37:02939 if (pending_entry)
[email protected]c95fa8b2011-04-28 20:26:16940 DidCancelLoading();
[email protected]724159a2010-12-30 01:11:18941
942 render_manager_.RendererAbortedProvisionalLoad(render_view_host());
943 }
944
945 // Send out a notification that we failed a provisional load with an error.
946 ProvisionalLoadDetails details(
947 is_main_frame, controller_.IsURLInPageNavigation(validated_url),
[email protected]b9e8ea62011-03-04 06:29:09948 validated_url, std::string(), false, frame_id);
[email protected]724159a2010-12-30 01:11:18949 details.set_error_code(error_code);
950
951 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:27952 content::NOTIFICATION_FAIL_PROVISIONAL_LOAD_WITH_ERROR,
[email protected]724159a2010-12-30 01:11:18953 Source<NavigationController>(&controller_),
954 Details<ProvisionalLoadDetails>(&details));
[email protected]0d60f0192011-04-14 12:40:10955
956 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
957 DidFailProvisionalLoad(frame_id, is_main_frame,
958 validated_url, error_code));
[email protected]724159a2010-12-30 01:11:18959}
960
961void TabContents::OnDidLoadResourceFromMemoryCache(
962 const GURL& url,
[email protected]724159a2010-12-30 01:11:18963 const std::string& security_info) {
[email protected]28620862011-03-22 23:07:19964 base::StatsCounter cache("WebKit.CacheHit");
[email protected]724159a2010-12-30 01:11:18965 cache.Increment();
966
967 // Send out a notification that we loaded a resource from our memory cache.
968 int cert_id = 0, cert_status = 0, security_bits = -1, connection_status = 0;
969 SSLManager::DeserializeSecurityInfo(security_info,
970 &cert_id, &cert_status,
971 &security_bits,
972 &connection_status);
[email protected]7a8c55e2011-02-15 08:21:16973 LoadFromMemoryCacheDetails details(url, GetRenderProcessHost()->id(),
974 cert_id, cert_status);
[email protected]724159a2010-12-30 01:11:18975
976 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:27977 content::NOTIFICATION_LOAD_FROM_MEMORY_CACHE,
[email protected]724159a2010-12-30 01:11:18978 Source<NavigationController>(&controller_),
979 Details<LoadFromMemoryCacheDetails>(&details));
980}
981
982void TabContents::OnDidDisplayInsecureContent() {
[email protected]221a5d92011-04-28 18:23:16983 UserMetrics::RecordAction(UserMetricsAction("SSL.DisplayedInsecureContent"));
[email protected]724159a2010-12-30 01:11:18984 displayed_insecure_content_ = true;
985 SSLManager::NotifySSLInternalStateChanged();
986}
987
988void TabContents::OnDidRunInsecureContent(
[email protected]92771112011-01-20 00:13:02989 const std::string& security_origin, const GURL& target_url) {
[email protected]9941cf52011-02-08 16:17:23990 LOG(INFO) << security_origin << " ran insecure content from "
991 << target_url.possibly_invalid_spec();
[email protected]221a5d92011-04-28 18:23:16992 UserMetrics::RecordAction(UserMetricsAction("SSL.RanInsecureContent"));
[email protected]724159a2010-12-30 01:11:18993 controller_.ssl_manager()->DidRunInsecureContent(security_origin);
[email protected]f2b58c4d2011-06-01 23:29:41994 displayed_insecure_content_ = true;
995 SSLManager::NotifySSLInternalStateChanged();
[email protected]724159a2010-12-30 01:11:18996}
997
998void TabContents::OnDocumentLoadedInFrame(int64 frame_id) {
999 controller_.DocumentLoadedInFrame();
[email protected]0d60f0192011-04-14 12:40:101000 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
1001 DocumentLoadedInFrame(frame_id));
[email protected]724159a2010-12-30 01:11:181002}
1003
1004void TabContents::OnDidFinishLoad(int64 frame_id) {
[email protected]0d60f0192011-04-14 12:40:101005 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
1006 DidFinishLoad(frame_id));
[email protected]724159a2010-12-30 01:11:181007}
1008
[email protected]c8f73ab2011-01-22 00:05:171009void TabContents::OnUpdateContentRestrictions(int restrictions) {
[email protected]c40d6232011-03-25 00:16:211010 SetContentRestrictions(restrictions);
[email protected]c8f73ab2011-01-22 00:05:171011}
1012
[email protected]216813952011-05-19 22:21:261013void TabContents::OnGoToEntryAtOffset(int offset) {
1014 if (!delegate_ || delegate_->OnGoToEntryOffset(offset)) {
1015 NavigationEntry* entry = controller_.GetEntryAtOffset(offset);
1016 if (!entry)
1017 return;
1018 // Note that we don't call NavigationController::GotToOffset() as we don't
1019 // want to create a pending navigation entry (it might end up lingering
1020 // http://crbug.com/51680).
1021 entry->set_transition_type(entry->transition_type() |
1022 PageTransition::FORWARD_BACK);
1023 NavigateToEntry(*entry, NavigationController::NO_RELOAD);
1024 }
1025}
1026
1027void TabContents::OnUpdateZoomLimits(int minimum_percent,
1028 int maximum_percent,
1029 bool remember) {
1030 minimum_zoom_percent_ = minimum_percent;
1031 maximum_zoom_percent_ = maximum_percent;
1032 temporary_zoom_settings_ = !remember;
1033}
1034
1035void TabContents::OnFocusedNodeChanged(bool is_editable_node) {
1036 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271037 content::NOTIFICATION_FOCUS_CHANGED_IN_PAGE,
[email protected]216813952011-05-19 22:21:261038 Source<TabContents>(this),
1039 Details<const bool>(&is_editable_node));
1040}
1041
[email protected]96d185d2009-04-24 03:28:541042// Notifies the RenderWidgetHost instance about the fact that the page is
1043// loading, or done loading and calls the base implementation.
1044void TabContents::SetIsLoading(bool is_loading,
1045 LoadNotificationDetails* details) {
1046 if (is_loading == is_loading_)
1047 return;
1048
1049 if (!is_loading) {
1050 load_state_ = net::LOAD_STATE_IDLE;
1051 load_state_host_.clear();
[email protected]094e5b22009-09-25 04:23:561052 upload_size_ = 0;
1053 upload_position_ = 0;
[email protected]96d185d2009-04-24 03:28:541054 }
1055
1056 render_manager_.SetIsLoading(is_loading);
1057
1058 is_loading_ = is_loading;
1059 waiting_for_response_ = is_loading;
1060
[email protected]6ebdc9b2010-09-27 16:55:571061 if (delegate_)
1062 delegate_->LoadingStateChanged(this);
[email protected]329581b2009-04-28 06:52:351063 NotifyNavigationStateChanged(INVALIDATE_LOAD);
[email protected]96d185d2009-04-24 03:28:541064
[email protected]432115822011-07-10 15:52:271065 int type = is_loading ? content::NOTIFICATION_LOAD_START :
1066 content::NOTIFICATION_LOAD_STOP;
[email protected]ce5c4502009-05-06 16:46:111067 NotificationDetails det = NotificationService::NoDetails();
[email protected]96d185d2009-04-24 03:28:541068 if (details)
1069 det = Details<LoadNotificationDetails>(details);
1070 NotificationService::current()->Notify(type,
1071 Source<NavigationController>(&controller_),
1072 det);
1073}
1074
[email protected]d0980792011-02-13 19:41:401075WebUI* TabContents::GetWebUIForCurrentState() {
[email protected]7ade2732011-02-10 00:13:581076 // When there is a pending navigation entry, we want to use the pending WebUI
[email protected]96d185d2009-04-24 03:28:541077 // that goes along with it to control the basic flags. For example, we want to
1078 // show the pending URL in the URL bar, so we want the display_url flag to
1079 // be from the pending entry.
1080 //
1081 // The confusion comes because there are multiple possibilities for the
1082 // initial load in a tab as a side effect of the way the RenderViewHostManager
1083 // works.
1084 //
[email protected]e0112912011-02-02 22:54:351085 // - For the very first tab the load looks "normal". The new tab Web UI is
[email protected]96d185d2009-04-24 03:28:541086 // the pending one, and we want it to apply here.
1087 //
1088 // - For subsequent new tabs, they'll get a new SiteInstance which will then
1089 // get switched to the one previously associated with the new tab pages.
[email protected]7ade2732011-02-10 00:13:581090 // This switching will cause the manager to commit the RVH/WebUI. So we'll
[email protected]e0112912011-02-02 22:54:351091 // have a committed Web UI in this case.
[email protected]96d185d2009-04-24 03:28:541092 //
1093 // This condition handles all of these cases:
1094 //
1095 // - First load in first tab: no committed nav entry + pending nav entry +
1096 // pending dom ui:
[email protected]e0112912011-02-02 22:54:351097 // -> Use pending Web UI if any.
[email protected]96d185d2009-04-24 03:28:541098 //
1099 // - First load in second tab: no committed nav entry + pending nav entry +
[email protected]e0112912011-02-02 22:54:351100 // no pending Web UI:
1101 // -> Use the committed Web UI if any.
[email protected]96d185d2009-04-24 03:28:541102 //
1103 // - Second navigation in any tab: committed nav entry + pending nav entry:
[email protected]e0112912011-02-02 22:54:351104 // -> Use pending Web UI if any.
[email protected]96d185d2009-04-24 03:28:541105 //
1106 // - Normal state with no load: committed nav entry + no pending nav entry:
[email protected]e0112912011-02-02 22:54:351107 // -> Use committed Web UI.
[email protected]96d185d2009-04-24 03:28:541108 if (controller_.pending_entry() &&
1109 (controller_.GetLastCommittedEntry() ||
[email protected]7ade2732011-02-10 00:13:581110 render_manager_.pending_web_ui()))
1111 return render_manager_.pending_web_ui();
1112 return render_manager_.web_ui();
[email protected]96d185d2009-04-24 03:28:541113}
[email protected]420ae012009-04-24 05:16:321114
[email protected]1fd1a502011-03-30 16:55:561115WebUI::TypeID TabContents::GetWebUITypeForCurrentState() {
1116 return content::WebUIFactory::Get()->GetWebUIType(profile(), GetURL());
1117}
1118
[email protected]420ae012009-04-24 05:16:321119void TabContents::DidNavigateMainFramePostCommit(
[email protected]8286f51a2011-05-31 17:39:131120 const content::LoadCommittedDetails& details,
[email protected]420ae012009-04-24 05:16:321121 const ViewHostMsg_FrameNavigate_Params& params) {
[email protected]1fd1a502011-03-30 16:55:561122 if (opener_web_ui_type_ != WebUI::kNoWebUI) {
[email protected]80a8fad2011-01-29 04:02:381123 // If this is a window.open navigation, use the same WebUI as the renderer
[email protected]c2e74fe82009-09-03 17:57:441124 // that opened the window, as long as both renderers have the same
1125 // privileges.
[email protected]1fd1a502011-03-30 16:55:561126 if (delegate_ && opener_web_ui_type_ == GetWebUITypeForCurrentState()) {
1127 WebUI* web_ui = content::GetContentClient()->browser()->
1128 GetWebUIFactory()->CreateWebUIForURL(this, GetURL());
[email protected]7ade2732011-02-10 00:13:581129 // web_ui might be NULL if the URL refers to a non-existent extension.
1130 if (web_ui) {
[email protected]d0980792011-02-13 19:41:401131 render_manager_.SetWebUIPostCommit(web_ui);
[email protected]7ade2732011-02-10 00:13:581132 web_ui->RenderViewCreated(render_view_host());
[email protected]c2e74fe82009-09-03 17:57:441133 }
1134 }
[email protected]1fd1a502011-03-30 16:55:561135 opener_web_ui_type_ = WebUI::kNoWebUI;
[email protected]c2e74fe82009-09-03 17:57:441136 }
1137
[email protected]4e697b042011-07-08 06:44:561138 if (details.is_navigation_to_different_page()) {
[email protected]420ae012009-04-24 05:16:321139 // Clear the status bubble. This is a workaround for a bug where WebKit
1140 // doesn't let us know that the cursor left an element during a
1141 // transition (this is also why the mouse cursor remains as a hand after
1142 // clicking on a link); see bugs 1184641 and 980803. We don't want to
1143 // clear the bubble when a user navigates to a named anchor in the same
1144 // page.
1145 UpdateTargetURL(details.entry->page_id(), GURL());
[email protected]420ae012009-04-24 05:16:321146 }
1147
[email protected]a6e82fc2010-02-24 22:28:141148 if (!details.is_in_page) {
[email protected]f17a0ee2010-05-17 17:38:471149 // Once the main frame is navigated, we're no longer considered to have
1150 // displayed insecure content.
1151 displayed_insecure_content_ = false;
[email protected]aece2c7f2009-08-27 20:43:171152 }
[email protected]ce5c4502009-05-06 16:46:111153
[email protected]8cc8d492010-02-02 10:40:491154 // Close constrained windows if necessary.
1155 if (!net::RegistryControlledDomainService::SameDomainOrHost(
1156 details.previous_url, details.entry->url()))
1157 CloseConstrainedWindows();
1158
[email protected]3c9e1872010-11-18 16:17:491159 // Notify observers about navigation.
[email protected]585b30362011-01-28 02:30:171160 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
[email protected]3c9e1872010-11-18 16:17:491161 DidNavigateMainFramePostCommit(details, params));
[email protected]420ae012009-04-24 05:16:321162}
1163
1164void TabContents::DidNavigateAnyFramePostCommit(
1165 RenderViewHost* render_view_host,
[email protected]8286f51a2011-05-31 17:39:131166 const content::LoadCommittedDetails& details,
[email protected]420ae012009-04-24 05:16:321167 const ViewHostMsg_FrameNavigate_Params& params) {
[email protected]3ab9cb82011-06-03 18:02:071168 // If we navigate, reset JavaScript state. This does nothing to prevent
[email protected]420ae012009-04-24 05:16:321169 // a malicious script from spamming messages, since the script could just
1170 // reload the page to stop blocking.
[email protected]3ab9cb82011-06-03 18:02:071171 if (delegate_)
1172 delegate_->GetJavaScriptDialogCreator()->ResetJavaScriptState(this);
[email protected]420ae012009-04-24 05:16:321173
[email protected]3c9e1872010-11-18 16:17:491174 // Notify observers about navigation.
[email protected]585b30362011-01-28 02:30:171175 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
[email protected]3c9e1872010-11-18 16:17:491176 DidNavigateAnyFramePostCommit(details, params));
[email protected]420ae012009-04-24 05:16:321177}
1178
[email protected]8cc8d492010-02-02 10:40:491179void TabContents::CloseConstrainedWindows() {
1180 // Clear out any constrained windows since we are leaving this page entirely.
[email protected]aed59602011-02-28 22:57:331181 // To ensure that we iterate over every element in child_windows_ we
1182 // need to use a copy of child_windows_. Otherwise if
1183 // window->CloseConstrainedWindow() modifies child_windows_ we could end up
1184 // skipping some elements.
1185 ConstrainedWindowList child_windows_copy(child_windows_);
1186 for (ConstrainedWindowList::iterator it = child_windows_copy.begin();
1187 it != child_windows_copy.end(); ++it) {
1188 ConstrainedWindow* window = *it;
[email protected]fa1cf0b82010-01-15 21:49:441189 if (window) {
[email protected]420ae012009-04-24 05:16:321190 window->CloseConstrainedWindow();
[email protected]fa1cf0b82010-01-15 21:49:441191 BlockTabContent(false);
1192 }
[email protected]420ae012009-04-24 05:16:321193 }
1194}
1195
[email protected]420ae012009-04-24 05:16:321196void TabContents::UpdateMaxPageIDIfNecessary(SiteInstance* site_instance,
1197 RenderViewHost* rvh) {
1198 // If we are creating a RVH for a restored controller, then we might
1199 // have more page IDs than the SiteInstance's current max page ID. We must
1200 // make sure that the max page ID is larger than any restored page ID.
1201 // Note that it is ok for conflicting page IDs to exist in another tab
1202 // (i.e., NavigationController), but if any page ID is larger than the max,
1203 // the back/forward list will get confused.
1204 int max_restored_page_id = controller_.max_restored_page_id();
1205 if (max_restored_page_id > 0) {
1206 int curr_max_page_id = site_instance->max_page_id();
1207 if (max_restored_page_id > curr_max_page_id) {
1208 // Need to update the site instance immediately.
1209 site_instance->UpdateMaxPageID(max_restored_page_id);
1210
1211 // Also tell the renderer to update its internal representation. We
1212 // need to reserve enough IDs to make all restored page IDs less than
1213 // the max.
1214 if (curr_max_page_id < 0)
1215 curr_max_page_id = 0;
[email protected]216813952011-05-19 22:21:261216 rvh->Send(new ViewMsg_ReservePageIDRange(
1217 rvh->routing_id(), max_restored_page_id - curr_max_page_id));
[email protected]420ae012009-04-24 05:16:321218 }
1219 }
1220}
1221
[email protected]6b2f7a82011-04-25 19:30:511222bool TabContents::UpdateTitleForEntry(NavigationEntry* entry,
1223 const std::wstring& title) {
[email protected]420ae012009-04-24 05:16:321224 // For file URLs without a title, use the pathname instead. In the case of a
1225 // synthesized title, we don't want the update to count toward the "one set
1226 // per page of the title to history."
[email protected]6b2f7a82011-04-25 19:30:511227 string16 final_title;
[email protected]420ae012009-04-24 05:16:321228 bool explicit_set;
[email protected]987fc3a2011-05-26 14:18:091229 if (entry && entry->url().SchemeIsFile() && title.empty()) {
[email protected]6b2f7a82011-04-25 19:30:511230 final_title = UTF8ToUTF16(entry->url().ExtractFileName());
[email protected]420ae012009-04-24 05:16:321231 explicit_set = false; // Don't count synthetic titles toward the set limit.
1232 } else {
[email protected]6b2f7a82011-04-25 19:30:511233 TrimWhitespace(WideToUTF16Hack(title), TRIM_ALL, &final_title);
[email protected]420ae012009-04-24 05:16:321234 explicit_set = true;
1235 }
1236
[email protected]987fc3a2011-05-26 14:18:091237 // If a page is created via window.open and never navigated,
1238 // there will be no navigation entry. In this situation,
1239 // |page_title_when_no_navigaiton_entry_| will be used for page title.
1240 if (entry) {
1241 if (final_title == entry->title())
1242 return false; // Nothing changed, don't bother.
[email protected]420ae012009-04-24 05:16:321243
[email protected]987fc3a2011-05-26 14:18:091244 entry->set_title(final_title);
1245 } else {
1246 if (page_title_when_no_navigation_entry_ == final_title)
1247 return false; // Nothing changed, don't bother.
1248
1249 page_title_when_no_navigation_entry_ = final_title;
1250 }
[email protected]420ae012009-04-24 05:16:321251
[email protected]420ae012009-04-24 05:16:321252 // Lastly, set the title for the view.
[email protected]6b2f7a82011-04-25 19:30:511253 view_->SetPageTitle(UTF16ToWideHack(final_title));
[email protected]420ae012009-04-24 05:16:321254
[email protected]105bb0f2011-05-24 17:12:141255 TitleUpdatedDetails details(entry, explicit_set);
1256
[email protected]cbc0e1b2010-04-12 18:33:041257 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271258 content::NOTIFICATION_TAB_CONTENTS_TITLE_UPDATED,
[email protected]cbc0e1b2010-04-12 18:33:041259 Source<TabContents>(this),
[email protected]105bb0f2011-05-24 17:12:141260 Details<TitleUpdatedDetails>(&details));
[email protected]cbc0e1b2010-04-12 18:33:041261
[email protected]420ae012009-04-24 05:16:321262 return true;
1263}
1264
1265void TabContents::NotifySwapped() {
1266 // After sending out a swap notification, we need to send a disconnect
1267 // notification so that clients that pick up a pointer to |this| can NULL the
1268 // pointer. See Bug 1230284.
1269 notify_disconnection_ = true;
1270 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271271 content::NOTIFICATION_TAB_CONTENTS_SWAPPED,
[email protected]d82ed61e2009-06-16 02:46:221272 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:321273 NotificationService::NoDetails());
1274}
1275
1276void TabContents::NotifyConnected() {
1277 notify_disconnection_ = true;
1278 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271279 content::NOTIFICATION_TAB_CONTENTS_CONNECTED,
[email protected]d82ed61e2009-06-16 02:46:221280 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:321281 NotificationService::NoDetails());
1282}
1283
1284void TabContents::NotifyDisconnected() {
1285 if (!notify_disconnection_)
1286 return;
1287
1288 notify_disconnection_ = false;
1289 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271290 content::NOTIFICATION_TAB_CONTENTS_DISCONNECTED,
[email protected]d82ed61e2009-06-16 02:46:221291 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:321292 NotificationService::NoDetails());
1293}
1294
[email protected]8d3347f2009-07-09 22:00:211295RenderViewHostDelegate::View* TabContents::GetViewDelegate() {
[email protected]420ae012009-04-24 05:16:321296 return view_.get();
1297}
1298
[email protected]8d3347f2009-07-09 22:00:211299RenderViewHostDelegate::RendererManagement*
1300TabContents::GetRendererManagementDelegate() {
1301 return &render_manager_;
1302}
1303
[email protected]93623c5d2009-12-10 21:40:321304RendererPreferences TabContents::GetRendererPrefs(Profile* profile) const {
[email protected]840b1512009-07-21 16:53:461305 return renderer_preferences_;
[email protected]80d96fa2009-06-10 22:34:511306}
1307
[email protected]57c6a652009-05-04 07:58:341308TabContents* TabContents::GetAsTabContents() {
1309 return this;
1310}
1311
[email protected]7b291f92009-08-14 05:43:531312ViewType::Type TabContents::GetRenderViewType() const {
1313 return ViewType::TAB_CONTENTS;
1314}
1315
[email protected]420ae012009-04-24 05:16:321316void TabContents::RenderViewCreated(RenderViewHost* render_view_host) {
[email protected]2e4633c2009-07-09 16:58:061317 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271318 content::NOTIFICATION_RENDER_VIEW_HOST_CREATED_FOR_TAB,
[email protected]2e4633c2009-07-09 16:58:061319 Source<TabContents>(this),
1320 Details<RenderViewHost>(render_view_host));
[email protected]420ae012009-04-24 05:16:321321 NavigationEntry* entry = controller_.GetActiveEntry();
1322 if (!entry)
1323 return;
1324
1325 // When we're creating views, we're still doing initial setup, so we always
[email protected]e0112912011-02-02 22:54:351326 // use the pending Web UI rather than any possibly existing committed one.
[email protected]1fd1a502011-03-30 16:55:561327 if (render_manager_.pending_web_ui())
[email protected]7ade2732011-02-10 00:13:581328 render_manager_.pending_web_ui()->RenderViewCreated(render_view_host);
[email protected]420ae012009-04-24 05:16:321329
1330 if (entry->IsViewSourceMode()) {
1331 // Put the renderer in view source mode.
1332 render_view_host->Send(
1333 new ViewMsg_EnableViewSourceMode(render_view_host->routing_id()));
1334 }
[email protected]0666aef2009-05-13 19:48:081335
1336 view()->RenderViewCreated(render_view_host);
[email protected]11f764bb2011-05-25 19:45:261337
1338 FOR_EACH_OBSERVER(
1339 TabContentsObserver, observers_, RenderViewCreated(render_view_host));
[email protected]420ae012009-04-24 05:16:321340}
1341
1342void TabContents::RenderViewReady(RenderViewHost* rvh) {
1343 if (rvh != render_view_host()) {
1344 // Don't notify the world, since this came from a renderer in the
1345 // background.
1346 return;
1347 }
1348
1349 NotifyConnected();
[email protected]9d8a4642009-07-29 17:25:301350 bool was_crashed = is_crashed();
[email protected]443b80e2010-12-14 00:42:231351 SetIsCrashed(base::TERMINATION_STATUS_STILL_RUNNING, 0);
[email protected]9d8a4642009-07-29 17:25:301352
1353 // Restore the focus to the tab (otherwise the focus will be on the top
1354 // window).
[email protected]484ae5912010-09-29 19:16:141355 if (was_crashed && !FocusLocationBarByDefault() &&
1356 (!delegate_ || delegate_->ShouldFocusPageAfterCrash())) {
[email protected]9d8a4642009-07-29 17:25:301357 Focus();
[email protected]484ae5912010-09-29 19:16:141358 }
[email protected]420ae012009-04-24 05:16:321359}
1360
[email protected]443b80e2010-12-14 00:42:231361void TabContents::RenderViewGone(RenderViewHost* rvh,
1362 base::TerminationStatus status,
1363 int error_code) {
[email protected]420ae012009-04-24 05:16:321364 if (rvh != render_view_host()) {
1365 // The pending page's RenderViewHost is gone.
1366 return;
1367 }
1368
1369 SetIsLoading(false, NULL);
1370 NotifyDisconnected();
[email protected]443b80e2010-12-14 00:42:231371 SetIsCrashed(status, error_code);
[email protected]420ae012009-04-24 05:16:321372
[email protected]55452902011-06-01 21:57:471373 FOR_EACH_OBSERVER(TabContentsObserver, observers_, RenderViewGone());
[email protected]420ae012009-04-24 05:16:321374}
1375
[email protected]2e4633c2009-07-09 16:58:061376void TabContents::RenderViewDeleted(RenderViewHost* rvh) {
[email protected]fb7b79f72009-11-06 18:00:481377 render_manager_.RenderViewDeleted(rvh);
[email protected]2e4633c2009-07-09 16:58:061378}
1379
[email protected]420ae012009-04-24 05:16:321380void TabContents::DidNavigate(RenderViewHost* rvh,
1381 const ViewHostMsg_FrameNavigate_Params& params) {
[email protected]09b29342011-06-24 19:18:481382 if (PageTransition::IsMainFrame(params.transition))
[email protected]420ae012009-04-24 05:16:321383 render_manager_.DidNavigateMainFrame(rvh);
[email protected]8030f012009-09-25 18:09:371384
[email protected]420ae012009-04-24 05:16:321385 // Update the site of the SiteInstance if it doesn't have one yet.
1386 if (!GetSiteInstance()->has_site())
1387 GetSiteInstance()->SetSite(params.url);
1388
1389 // Need to update MIME type here because it's referred to in
1390 // UpdateNavigationCommands() called by RendererDidNavigate() to
1391 // determine whether or not to enable the encoding menu.
1392 // It's updated only for the main frame. For a subframe,
1393 // RenderView::UpdateURL does not set params.contents_mime_type.
1394 // (see http://code.google.com/p/chromium/issues/detail?id=2929 )
1395 // TODO(jungshik): Add a test for the encoding menu to avoid
1396 // regressing it again.
1397 if (PageTransition::IsMainFrame(params.transition))
1398 contents_mime_type_ = params.contents_mime_type;
1399
[email protected]8286f51a2011-05-31 17:39:131400 content::LoadCommittedDetails details;
[email protected]93f230e02011-06-01 14:40:001401 bool did_navigate = controller_.RendererDidNavigate(params, &details);
[email protected]3e90d4a2009-07-03 17:38:391402
[email protected]a9c0bfe2010-09-17 08:35:221403 // Send notification about committed provisional loads. This notification is
1404 // different from the NAV_ENTRY_COMMITTED notification which doesn't include
1405 // the actual URL navigated to and isn't sent for AUTO_SUBFRAME navigations.
1406 if (details.type != NavigationType::NAV_IGNORE) {
[email protected]dabb0d12010-10-05 12:50:071407 // For AUTO_SUBFRAME navigations, an event for the main frame is generated
1408 // that is not recorded in the navigation history. For the purpose of
1409 // tracking navigation events, we treat this event as a sub frame navigation
1410 // event.
1411 bool is_main_frame = did_navigate ? details.is_main_frame : false;
[email protected]0d60f0192011-04-14 12:40:101412 PageTransition::Type transition_type = params.transition;
[email protected]a9c0bfe2010-09-17 08:35:221413 // Whether or not a page transition was triggered by going backward or
1414 // forward in the history is only stored in the navigation controller's
1415 // entry list.
1416 if (did_navigate &&
1417 (controller_.GetActiveEntry()->transition_type() &
1418 PageTransition::FORWARD_BACK)) {
[email protected]0d60f0192011-04-14 12:40:101419 transition_type = params.transition | PageTransition::FORWARD_BACK;
[email protected]a9c0bfe2010-09-17 08:35:221420 }
[email protected]0d60f0192011-04-14 12:40:101421 // Notify observers about the commit of the provisional load.
1422 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
1423 DidCommitProvisionalLoadForFrame(params.frame_id,
1424 is_main_frame, params.url, transition_type));
[email protected]a9c0bfe2010-09-17 08:35:221425 }
1426
[email protected]76543b92009-08-31 17:27:451427 if (!did_navigate)
1428 return; // No navigation happened.
[email protected]420ae012009-04-24 05:16:321429
1430 // DO NOT ADD MORE STUFF TO THIS FUNCTION! Your component should either listen
1431 // for the appropriate notification (best) or you can add it to
1432 // DidNavigateMainFramePostCommit / DidNavigateAnyFramePostCommit (only if
1433 // necessary, please).
1434
1435 // Run post-commit tasks.
[email protected]93f230e02011-06-01 14:40:001436 if (details.is_main_frame) {
[email protected]420ae012009-04-24 05:16:321437 DidNavigateMainFramePostCommit(details, params);
[email protected]09b29342011-06-24 19:18:481438 if (delegate())
1439 delegate()->DidNavigateMainFramePostCommit(this);
[email protected]93f230e02011-06-01 14:40:001440 }
[email protected]420ae012009-04-24 05:16:321441 DidNavigateAnyFramePostCommit(rvh, details, params);
1442}
1443
1444void TabContents::UpdateState(RenderViewHost* rvh,
1445 int32 page_id,
1446 const std::string& state) {
[email protected]992db4c2011-05-12 15:37:151447 // Ensure that this state update comes from either the active RVH or one of
1448 // the swapped out RVHs. We don't expect to hear from any other RVHs.
1449 DCHECK(rvh == render_view_host() || render_manager_.IsSwappedOut(rvh));
[email protected]420ae012009-04-24 05:16:321450
1451 // We must be prepared to handle state updates for any page, these occur
1452 // when the user is scrolling and entering form data, as well as when we're
1453 // leaving a page, in which case our state may have already been moved to
1454 // the next page. The navigation controller will look up the appropriate
1455 // NavigationEntry and update it when it is notified via the delegate.
1456
1457 int entry_index = controller_.GetEntryIndexWithPageID(
[email protected]992db4c2011-05-12 15:37:151458 rvh->site_instance(), page_id);
[email protected]420ae012009-04-24 05:16:321459 if (entry_index < 0)
1460 return;
1461 NavigationEntry* entry = controller_.GetEntryAtIndex(entry_index);
1462
1463 if (state == entry->content_state())
1464 return; // Nothing to update.
1465 entry->set_content_state(state);
1466 controller_.NotifyEntryChanged(entry, entry_index);
1467}
1468
1469void TabContents::UpdateTitle(RenderViewHost* rvh,
[email protected]6b2f7a82011-04-25 19:30:511470 int32 page_id, const std::wstring& title) {
[email protected]420ae012009-04-24 05:16:321471 // If we have a title, that's a pretty good indication that we've started
1472 // getting useful data.
1473 SetNotWaitingForResponse();
1474
1475 DCHECK(rvh == render_view_host());
[email protected]371cc8b2010-09-01 16:03:481476 NavigationEntry* entry = controller_.GetEntryWithPageID(rvh->site_instance(),
[email protected]ec0b6c42010-08-26 03:16:581477 page_id);
[email protected]987fc3a2011-05-26 14:18:091478
1479 if (!UpdateTitleForEntry(entry, title))
[email protected]420ae012009-04-24 05:16:321480 return;
1481
1482 // Broadcast notifications when the UI should be updated.
1483 if (entry == controller_.GetEntryAtOffset(0))
[email protected]f1cd5e82009-10-23 17:19:031484 NotifyNavigationStateChanged(INVALIDATE_TITLE);
[email protected]420ae012009-04-24 05:16:321485}
1486
[email protected]420ae012009-04-24 05:16:321487void TabContents::UpdateEncoding(RenderViewHost* render_view_host,
[email protected]41fc0322009-09-04 22:23:401488 const std::string& encoding) {
[email protected]420ae012009-04-24 05:16:321489 set_encoding(encoding);
1490}
1491
1492void TabContents::UpdateTargetURL(int32 page_id, const GURL& url) {
1493 if (delegate())
1494 delegate()->UpdateTargetURL(this, url);
1495}
1496
[email protected]420ae012009-04-24 05:16:321497void TabContents::Close(RenderViewHost* rvh) {
[email protected]07707302009-11-06 00:50:291498 // The UI may be in an event-tracking loop, such as between the
1499 // mouse-down and mouse-up in text selection or a button click.
1500 // Defer the close until after tracking is complete, so that we
1501 // don't free objects out from under the UI.
1502 // TODO(shess): This could probably be integrated with the
1503 // IsDoingDrag() test below. Punting for now because I need more
1504 // research to understand how this impacts platforms other than Mac.
1505 // TODO(shess): This could get more fine-grained. For instance,
1506 // closing a tab in another window while selecting text in the
1507 // current window's Omnibox should be just fine.
1508 if (view()->IsEventTracking()) {
1509 view()->CloseTabAfterEventTracking();
1510 return;
1511 }
1512
[email protected]24a4d1062009-07-10 23:10:421513 // If we close the tab while we're in the middle of a drag, we'll crash.
1514 // Instead, cancel the drag and close it as soon as the drag ends.
1515 if (view()->IsDoingDrag()) {
1516 view()->CancelDragAndCloseTab();
1517 return;
1518 }
1519
[email protected]420ae012009-04-24 05:16:321520 // Ignore this if it comes from a RenderViewHost that we aren't showing.
1521 if (delegate() && rvh == render_view_host())
1522 delegate()->CloseContents(this);
1523}
1524
1525void TabContents::RequestMove(const gfx::Rect& new_bounds) {
[email protected]b35b26b32011-05-05 20:35:141526 if (delegate() && delegate()->IsPopupOrPanel(this))
[email protected]420ae012009-04-24 05:16:321527 delegate()->MoveContents(this, new_bounds);
1528}
1529
[email protected]7ab1e7d62009-10-14 23:32:011530void TabContents::DidStartLoading() {
[email protected]420ae012009-04-24 05:16:321531 SetIsLoading(true, NULL);
[email protected]3c9e1872010-11-18 16:17:491532
[email protected]c40d6232011-03-25 00:16:211533 if (delegate() && content_restrictions_) {
1534 content_restrictions_ = 0;
[email protected]de4761b2010-12-07 21:05:211535 delegate()->ContentRestrictionsChanged(this);
[email protected]9e823662010-10-13 23:36:001536 }
[email protected]3c9e1872010-11-18 16:17:491537
1538 // Notify observers about navigation.
[email protected]585b30362011-01-28 02:30:171539 FOR_EACH_OBSERVER(TabContentsObserver, observers_, DidStartLoading());
[email protected]420ae012009-04-24 05:16:321540}
1541
[email protected]7ab1e7d62009-10-14 23:32:011542void TabContents::DidStopLoading() {
[email protected]420ae012009-04-24 05:16:321543 scoped_ptr<LoadNotificationDetails> details;
1544
1545 NavigationEntry* entry = controller_.GetActiveEntry();
1546 // An entry may not exist for a stop when loading an initial blank page or
1547 // if an iframe injected by script into a blank page finishes loading.
1548 if (entry) {
[email protected]420ae012009-04-24 05:16:321549 base::TimeDelta elapsed = base::TimeTicks::Now() - current_load_start_;
1550
1551 details.reset(new LoadNotificationDetails(
[email protected]ebe89e062009-08-13 23:16:541552 entry->virtual_url(),
[email protected]420ae012009-04-24 05:16:321553 entry->transition_type(),
1554 elapsed,
1555 &controller_,
1556 controller_.GetCurrentEntryIndex()));
1557 }
1558
[email protected]420ae012009-04-24 05:16:321559 SetIsLoading(false, details.get());
[email protected]3c9e1872010-11-18 16:17:491560
1561 // Notify observers about navigation.
[email protected]585b30362011-01-28 02:30:171562 FOR_EACH_OBSERVER(TabContentsObserver, observers_, DidStopLoading());
[email protected]420ae012009-04-24 05:16:321563}
1564
[email protected]c95fa8b2011-04-28 20:26:161565void TabContents::DidCancelLoading() {
1566 controller_.DiscardNonCommittedEntries();
1567
1568 // Update the URL display.
1569 NotifyNavigationStateChanged(TabContents::INVALIDATE_URL);
1570}
1571
[email protected]1a3c3cb2010-12-16 21:03:401572void TabContents::DidChangeLoadProgress(double progress) {
1573 if (delegate())
1574 delegate()->LoadProgressChanged(progress);
1575}
1576
[email protected]25497492010-09-11 15:15:081577void TabContents::DocumentOnLoadCompletedInMainFrame(
1578 RenderViewHost* render_view_host,
1579 int32 page_id) {
1580 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271581 content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME,
[email protected]25497492010-09-11 15:15:081582 Source<TabContents>(this),
1583 Details<int>(&page_id));
1584}
1585
[email protected]420ae012009-04-24 05:16:321586void TabContents::RequestOpenURL(const GURL& url, const GURL& referrer,
1587 WindowOpenDisposition disposition) {
[email protected]7ade2732011-02-10 00:13:581588 if (render_manager_.web_ui()) {
[email protected]e0112912011-02-02 22:54:351589 // When we're a Web UI, it will provide a page transition type for us (this
[email protected]420ae012009-04-24 05:16:321590 // is so the new tab page can specify AUTO_BOOKMARK for automatically
1591 // generated suggestions).
1592 //
[email protected]e0112912011-02-02 22:54:351593 // Note also that we hide the referrer for Web UI pages. We don't really
[email protected]60e448982009-05-06 04:21:161594 // want web sites to see a referrer of "chrome://blah" (and some
1595 // chrome: URLs might have search terms or other stuff we don't want to
[email protected]420ae012009-04-24 05:16:321596 // send to the site), so we send no referrer.
1597 OpenURL(url, GURL(), disposition,
[email protected]7ade2732011-02-10 00:13:581598 render_manager_.web_ui()->link_transition_type());
[email protected]420ae012009-04-24 05:16:321599 } else {
1600 OpenURL(url, referrer, disposition, PageTransition::LINK);
1601 }
1602}
1603
[email protected]420ae012009-04-24 05:16:321604void TabContents::RunJavaScriptMessage(
[email protected]992db4c2011-05-12 15:37:151605 const RenderViewHost* rvh,
[email protected]4f5ce842011-05-27 19:34:411606 const string16& message,
1607 const string16& default_prompt,
[email protected]420ae012009-04-24 05:16:321608 const GURL& frame_url,
1609 const int flags,
1610 IPC::Message* reply_msg,
1611 bool* did_suppress_message) {
[email protected]3ab9cb82011-06-03 18:02:071612 // Suppress JavaScript dialogs when requested. Also suppress messages when
1613 // showing an interstitial as it's shown over the previous page and we don't
1614 // want the hidden page's dialogs to interfere with the interstitial.
[email protected]8f55e802010-12-06 18:11:501615 bool suppress_this_message =
[email protected]992db4c2011-05-12 15:37:151616 rvh->is_swapped_out() ||
[email protected]8f55e802010-12-06 18:11:501617 showing_interstitial_page() ||
[email protected]3ab9cb82011-06-03 18:02:071618 !delegate_ ||
1619 delegate_->ShouldSuppressDialogs();
[email protected]420ae012009-04-24 05:16:321620
1621 if (!suppress_this_message) {
[email protected]b627d9a2011-06-28 14:06:341622 content::JavaScriptDialogCreator::TitleType title_type;
1623 string16 title;
1624
1625 if (!frame_url.has_host()) {
1626 title_type = content::JavaScriptDialogCreator::DIALOG_TITLE_NONE;
1627 } else {
1628 title_type = content::JavaScriptDialogCreator::DIALOG_TITLE_FORMATTED_URL;
1629 title = net::FormatUrl(
1630 frame_url.GetOrigin(),
1631 content::GetContentClient()->browser()->GetAcceptLangs(this));
1632 }
1633
[email protected]3ab9cb82011-06-03 18:02:071634 delegate_->GetJavaScriptDialogCreator()->RunJavaScriptDialog(
1635 this,
[email protected]b627d9a2011-06-28 14:06:341636 title_type,
1637 title,
[email protected]3ab9cb82011-06-03 18:02:071638 flags,
1639 message,
1640 default_prompt,
1641 reply_msg,
[email protected]b627d9a2011-06-28 14:06:341642 &suppress_this_message);
[email protected]420ae012009-04-24 05:16:321643 }
[email protected]3ab9cb82011-06-03 18:02:071644
1645 if (suppress_this_message) {
1646 // If we are suppressing messages, just reply as if the user immediately
1647 // pressed "Cancel".
1648 OnDialogClosed(reply_msg, false, string16());
1649 }
1650
1651 *did_suppress_message = suppress_this_message;
[email protected]420ae012009-04-24 05:16:321652}
1653
[email protected]992db4c2011-05-12 15:37:151654void TabContents::RunBeforeUnloadConfirm(const RenderViewHost* rvh,
[email protected]4f5ce842011-05-27 19:34:411655 const string16& message,
[email protected]420ae012009-04-24 05:16:321656 IPC::Message* reply_msg) {
[email protected]5aab5e22010-12-08 22:13:291657 if (delegate())
1658 delegate()->WillRunBeforeUnloadConfirm();
[email protected]3ab9cb82011-06-03 18:02:071659
1660 bool suppress_this_message =
1661 rvh->is_swapped_out() ||
1662 !delegate_ ||
1663 delegate_->ShouldSuppressDialogs();
[email protected]992db4c2011-05-12 15:37:151664 if (suppress_this_message) {
[email protected]4f5ce842011-05-27 19:34:411665 render_view_host()->JavaScriptDialogClosed(reply_msg, true, string16());
[email protected]8f55e802010-12-06 18:11:501666 return;
1667 }
[email protected]3ab9cb82011-06-03 18:02:071668
[email protected]7ab1e7d62009-10-14 23:32:011669 is_showing_before_unload_dialog_ = true;
[email protected]3ab9cb82011-06-03 18:02:071670 delegate_->GetJavaScriptDialogCreator()->RunBeforeUnloadDialog(
1671 this,
1672 message,
1673 reply_msg);
[email protected]420ae012009-04-24 05:16:321674}
1675
[email protected]420ae012009-04-24 05:16:321676WebPreferences TabContents::GetWebkitPrefs() {
[email protected]447021c2010-09-08 21:29:081677 WebPreferences web_prefs =
[email protected]181a95ee2011-07-12 19:26:361678 content::GetContentClient()->browser()->GetWebkitPrefs(
1679 render_view_host()->process()->profile(), false);
[email protected]447021c2010-09-08 21:29:081680
[email protected]b8299c12011-06-03 19:52:281681 // Force accelerated compositing and 2d canvas off for chrome:, about: and
1682 // chrome-devtools: pages.
[email protected]27c790d42010-10-22 09:28:431683 if (GetURL().SchemeIs(chrome::kChromeDevToolsScheme) ||
[email protected]b8299c12011-06-03 19:52:281684 GetURL().SchemeIs(chrome::kChromeUIScheme) ||
1685 GetURL().SchemeIs(chrome::kAboutScheme)) {
[email protected]447021c2010-09-08 21:29:081686 web_prefs.accelerated_compositing_enabled = false;
[email protected]9beff752010-09-22 19:35:431687 web_prefs.accelerated_2d_canvas_enabled = false;
[email protected]447021c2010-09-08 21:29:081688 }
1689
[email protected]39553442010-09-09 00:01:551690#if defined(OS_MACOSX)
1691 // Disable accelerated compositing if IOSurface's are not supported,
1692 // as is the case in 10.5.
1693 if (!IOSurfaceSupport::Initialize())
1694 web_prefs.accelerated_compositing_enabled = false;
1695#endif
1696
[email protected]447021c2010-09-08 21:29:081697 return web_prefs;
[email protected]420ae012009-04-24 05:16:321698}
1699
[email protected]7d472472011-01-22 01:30:251700void TabContents::OnUserGesture() {
[email protected]ddb85052011-05-18 14:40:271701 // Notify observers.
1702 FOR_EACH_OBSERVER(TabContentsObserver, observers_, DidGetUserGesture());
[email protected]686493142011-07-15 21:47:221703
1704 ResourceDispatcherHost* rdh =
1705 content::GetContentClient()->browser()->GetResourceDispatcherHost();
1706 if (rdh) // NULL in unittests.
1707 rdh->download_request_limiter()->OnUserGesture(this);
[email protected]7d472472011-01-22 01:30:251708}
1709
[email protected]fa1cf0b82010-01-15 21:49:441710void TabContents::OnIgnoredUIEvent() {
1711 if (constrained_window_count()) {
1712 ConstrainedWindow* window = *constrained_window_begin();
1713 window->FocusConstrainedWindow();
1714 }
1715}
1716
[email protected]420ae012009-04-24 05:16:321717void TabContents::OnCrossSiteResponse(int new_render_process_host_id,
1718 int new_request_id) {
[email protected]57c6a652009-05-04 07:58:341719 // Allows the TabContents to react when a cross-site response is ready to be
[email protected]420ae012009-04-24 05:16:321720 // delivered to a pending RenderViewHost. We must first run the onunload
1721 // handler of the old RenderViewHost before we can allow it to proceed.
1722 render_manager_.OnCrossSiteResponse(new_render_process_host_id,
1723 new_request_id);
1724}
1725
[email protected]420ae012009-04-24 05:16:321726void TabContents::RendererUnresponsive(RenderViewHost* rvh,
1727 bool is_during_unload) {
[email protected]992db4c2011-05-12 15:37:151728 // Don't show hung renderer dialog for a swapped out RVH.
1729 if (rvh != render_view_host())
1730 return;
1731
[email protected]420ae012009-04-24 05:16:321732 if (is_during_unload) {
1733 // Hang occurred while firing the beforeunload/unload handler.
1734 // Pretend the handler fired so tab closing continues as if it had.
[email protected]829e7612009-04-25 01:15:111735 rvh->set_sudden_termination_allowed(true);
[email protected]420ae012009-04-24 05:16:321736
1737 if (!render_manager_.ShouldCloseTabOnUnresponsiveRenderer())
1738 return;
1739
1740 // If the tab hangs in the beforeunload/unload handler there's really
1741 // nothing we can do to recover. Pretend the unload listeners have
1742 // all fired and close the tab. If the hang is in the beforeunload handler
1743 // then the user will not have the option of cancelling the close.
1744 Close(rvh);
1745 return;
1746 }
1747
[email protected]55452902011-06-01 21:57:471748 if (!render_view_host() || !render_view_host()->IsRenderViewLive())
1749 return;
1750
1751 if (delegate())
1752 delegate()->RendererUnresponsive(this);
[email protected]420ae012009-04-24 05:16:321753}
1754
1755void TabContents::RendererResponsive(RenderViewHost* render_view_host) {
[email protected]55452902011-06-01 21:57:471756 if (delegate())
1757 delegate()->RendererResponsive(this);
[email protected]420ae012009-04-24 05:16:321758}
1759
1760void TabContents::LoadStateChanged(const GURL& url,
[email protected]094e5b22009-09-25 04:23:561761 net::LoadState load_state,
1762 uint64 upload_position,
1763 uint64 upload_size) {
[email protected]420ae012009-04-24 05:16:321764 load_state_ = load_state;
[email protected]094e5b22009-09-25 04:23:561765 upload_position_ = upload_position;
1766 upload_size_ = upload_size;
[email protected]04866c42011-05-03 20:03:501767 load_state_host_ = net::IDNToUnicode(url.host(),
[email protected]b5cca982011-05-26 04:42:081768 content::GetContentClient()->browser()->GetAcceptLangs(this));
[email protected]420ae012009-04-24 05:16:321769 if (load_state_ == net::LOAD_STATE_READING_RESPONSE)
1770 SetNotWaitingForResponse();
1771 if (is_loading())
[email protected]c9cd2222009-05-06 05:16:501772 NotifyNavigationStateChanged(INVALIDATE_LOAD | INVALIDATE_TAB);
[email protected]420ae012009-04-24 05:16:321773}
1774
[email protected]7d472472011-01-22 01:30:251775void TabContents::WorkerCrashed() {
[email protected]0dd3a0ab2011-02-18 08:17:441776 if (delegate())
[email protected]55452902011-06-01 21:57:471777 delegate()->WorkerCrashed(this);
[email protected]7d472472011-01-22 01:30:251778}
1779
[email protected]420ae012009-04-24 05:16:321780void TabContents::BeforeUnloadFiredFromRenderManager(
1781 bool proceed,
1782 bool* proceed_to_fire_unload) {
1783 if (delegate())
1784 delegate()->BeforeUnloadFired(this, proceed, proceed_to_fire_unload);
1785}
1786
[email protected]3a3d47472010-07-15 21:03:541787void TabContents::DidStartLoadingFromRenderManager(
1788 RenderViewHost* render_view_host) {
1789 DidStartLoading();
1790}
1791
1792void TabContents::RenderViewGoneFromRenderManager(
1793 RenderViewHost* render_view_host) {
[email protected]443b80e2010-12-14 00:42:231794 DCHECK(crashed_status_ != base::TERMINATION_STATUS_STILL_RUNNING);
1795 RenderViewGone(render_view_host, crashed_status_, crashed_error_code_);
[email protected]3a3d47472010-07-15 21:03:541796}
1797
[email protected]420ae012009-04-24 05:16:321798void TabContents::UpdateRenderViewSizeForRenderManager() {
1799 // TODO(brettw) this is a hack. See TabContentsView::SizeContents.
[email protected]0323ee42010-02-17 22:03:261800 gfx::Size size = view_->GetContainerSize();
1801 // 0x0 isn't a valid window size (minimal window size is 1x1) but it may be
1802 // here during container initialization and normal window size will be set
1803 // later. In case of tab duplication this resizing to 0x0 prevents setting
1804 // normal size later so just ignore it.
1805 if (!size.IsEmpty())
1806 view_->SizeContents(size);
[email protected]420ae012009-04-24 05:16:321807}
1808
[email protected]3a3d47472010-07-15 21:03:541809void TabContents::NotifySwappedFromRenderManager() {
1810 NotifySwapped();
1811}
1812
1813NavigationController& TabContents::GetControllerForRenderManager() {
1814 return controller();
1815}
1816
[email protected]d0980792011-02-13 19:41:401817WebUI* TabContents::CreateWebUIForRenderManager(const GURL& url) {
[email protected]1fd1a502011-03-30 16:55:561818 return content::WebUIFactory::Get()->CreateWebUIForURL(this, url);
[email protected]420ae012009-04-24 05:16:321819}
1820
1821NavigationEntry*
1822TabContents::GetLastCommittedNavigationEntryForRenderManager() {
1823 return controller_.GetLastCommittedEntry();
1824}
1825
1826bool TabContents::CreateRenderViewForRenderManager(
1827 RenderViewHost* render_view_host) {
[email protected]420ae012009-04-24 05:16:321828 RenderWidgetHostView* rwh_view = view_->CreateViewForWidget(render_view_host);
[email protected]1a98a932009-11-17 00:12:521829
[email protected]420ae012009-04-24 05:16:321830 // Now that the RenderView has been created, we need to tell it its size.
1831 rwh_view->SetSize(view_->GetContainerSize());
1832
[email protected]a4127722011-04-27 23:13:521833 if (!render_view_host->CreateRenderView(string16()))
1834 return false;
1835
[email protected]f8233cc2011-05-31 20:24:501836#if defined(OS_LINUX)
1837 // Force a ViewMsg_Resize to be sent, needed to make plugins show up on
1838 // linux. See crbug.com/83941.
1839 if (RenderWidgetHost* render_widget_host = rwh_view->GetRenderWidgetHost())
1840 render_widget_host->WasResized();
1841#endif
1842
[email protected]420ae012009-04-24 05:16:321843 UpdateMaxPageIDIfNecessary(render_view_host->site_instance(),
1844 render_view_host);
1845 return true;
1846}
1847
[email protected]3ab9cb82011-06-03 18:02:071848void TabContents::OnDialogClosed(IPC::Message* reply_msg,
1849 bool success,
1850 const string16& user_input) {
[email protected]beb440c2009-11-06 04:08:541851 if (is_showing_before_unload_dialog_ && !success) {
1852 // If a beforeunload dialog is canceled, we need to stop the throbber from
1853 // spinning, since we forced it to start spinning in Navigate.
1854 DidStopLoading();
1855
1856 tab_close_start_time_ = base::TimeTicks();
1857 }
1858 is_showing_before_unload_dialog_ = false;
[email protected]4f5ce842011-05-27 19:34:411859 render_view_host()->JavaScriptDialogClosed(reply_msg,
1860 success,
[email protected]3ab9cb82011-06-03 18:02:071861 user_input);
[email protected]beb440c2009-11-06 04:08:541862}
1863
[email protected]3ab9cb82011-06-03 18:02:071864gfx::NativeWindow TabContents::GetDialogRootWindow() {
1865 return view_->GetTopLevelNativeWindow();
[email protected]beb440c2009-11-06 04:08:541866}
1867
[email protected]a1e97f02011-06-30 14:04:341868void TabContents::OnDialogShown() {
1869 Activate();
[email protected]3a3d47472010-07-15 21:03:541870}
1871
[email protected]41fc0322009-09-04 22:23:401872void TabContents::set_encoding(const std::string& encoding) {
[email protected]763ec4ca2011-04-29 15:48:121873 encoding_ = content::GetContentClient()->browser()->
1874 GetCanonicalEncodingNameByAliasName(encoding);
[email protected]aacd7cc2009-09-02 14:51:101875}
[email protected]f45d2a72010-03-08 23:28:351876
[email protected]33f74972010-12-08 16:40:361877void TabContents::SwapInRenderViewHost(RenderViewHost* rvh) {
1878 render_manager_.SwapInRenderViewHost(rvh);
1879}
1880
1881void TabContents::CreateViewAndSetSizeForRVH(RenderViewHost* rvh) {
1882 RenderWidgetHostView* rwh_view = view()->CreateViewForWidget(rvh);
1883 rwh_view->SetSize(view()->GetContainerSize());
1884}