blob: 090dd087ffd486761c972136f374c0a2c228e2a4 [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]ca406032011-07-19 21:53:05112static const char kDotGoogleDotCom[] = ".google.com";
113
[email protected]420ae012009-04-24 05:16:32114#if defined(OS_WIN)
115
116BOOL CALLBACK InvalidateWindow(HWND hwnd, LPARAM lparam) {
117 // Note: erase is required to properly paint some widgets borders. This can
118 // be seen with textfields.
119 InvalidateRect(hwnd, NULL, TRUE);
120 return TRUE;
121}
122#endif
123
[email protected]2c5569662011-03-22 20:45:02124ViewMsg_Navigate_Type::Value GetNavigationType(
[email protected]1ccb3568d2010-02-19 10:51:16125 Profile* profile, const NavigationEntry& entry,
126 NavigationController::ReloadType reload_type) {
127 switch (reload_type) {
128 case NavigationController::RELOAD:
[email protected]2c5569662011-03-22 20:45:02129 return ViewMsg_Navigate_Type::RELOAD;
[email protected]1ccb3568d2010-02-19 10:51:16130 case NavigationController::RELOAD_IGNORING_CACHE:
[email protected]2c5569662011-03-22 20:45:02131 return ViewMsg_Navigate_Type::RELOAD_IGNORING_CACHE;
[email protected]1ccb3568d2010-02-19 10:51:16132 case NavigationController::NO_RELOAD:
133 break; // Fall through to rest of function.
134 }
[email protected]5e369672009-11-03 23:48:30135
136 if (entry.restore_type() == NavigationEntry::RESTORE_LAST_SESSION &&
137 profile->DidLastSessionExitCleanly())
[email protected]2c5569662011-03-22 20:45:02138 return ViewMsg_Navigate_Type::RESTORE;
[email protected]5e369672009-11-03 23:48:30139
[email protected]2c5569662011-03-22 20:45:02140 return ViewMsg_Navigate_Type::NORMAL;
[email protected]5e369672009-11-03 23:48:30141}
142
[email protected]876bc832010-09-07 16:29:54143void MakeNavigateParams(const NavigationEntry& entry,
144 const NavigationController& controller,
[email protected]1ccb3568d2010-02-19 10:51:16145 NavigationController::ReloadType reload_type,
146 ViewMsg_Navigate_Params* params) {
[email protected]056de2d2009-06-26 16:41:34147 params->page_id = entry.page_id();
[email protected]876bc832010-09-07 16:29:54148 params->pending_history_list_offset = controller.GetIndexOfEntry(&entry);
[email protected]3cc72b12010-03-18 23:03:00149 params->current_history_list_offset = controller.last_committed_entry_index();
150 params->current_history_list_length = controller.entry_count();
[email protected]056de2d2009-06-26 16:41:34151 params->url = entry.url();
152 params->referrer = entry.referrer();
153 params->transition = entry.transition_type();
154 params->state = entry.content_state();
[email protected]3cc72b12010-03-18 23:03:00155 params->navigation_type =
156 GetNavigationType(controller.profile(), entry, reload_type);
[email protected]056de2d2009-06-26 16:41:34157 params->request_time = base::Time::Now();
158}
159
[email protected]420ae012009-04-24 05:16:32160} // namespace
161
[email protected]f4f50ef2011-01-21 19:01:19162
163// TabContents ----------------------------------------------------------------
[email protected]420ae012009-04-24 05:16:32164
[email protected]420ae012009-04-24 05:16:32165TabContents::TabContents(Profile* profile,
166 SiteInstance* site_instance,
167 int routing_id,
[email protected]6ee12c42010-09-14 09:36:07168 const TabContents* base_tab_contents,
169 SessionStorageNamespace* session_storage_namespace)
[email protected]b680ad22009-04-15 23:19:42170 : delegate_(NULL),
[email protected]6ee12c42010-09-14 09:36:07171 ALLOW_THIS_IN_INITIALIZER_LIST(controller_(
172 this, profile, session_storage_namespace)),
[email protected]66ba4932009-06-04 19:22:13173 ALLOW_THIS_IN_INITIALIZER_LIST(view_(
[email protected]d82ed61e2009-06-16 02:46:22174 TabContentsView::Create(this))),
175 ALLOW_THIS_IN_INITIALIZER_LIST(render_manager_(this, this)),
[email protected]d5f942ba2008-09-26 19:30:34176 is_loading_(false),
[email protected]443b80e2010-12-14 00:42:23177 crashed_status_(base::TERMINATION_STATUS_STILL_RUNNING),
178 crashed_error_code_(0),
[email protected]d5f942ba2008-09-26 19:30:34179 waiting_for_response_(false),
[email protected]d5f942ba2008-09-26 19:30:34180 max_page_id_(-1),
[email protected]fdd61c62009-04-22 19:22:57181 load_state_(net::LOAD_STATE_IDLE),
[email protected]094e5b22009-09-25 04:23:56182 upload_size_(0),
183 upload_position_(0),
[email protected]f17a0ee2010-05-17 17:38:47184 displayed_insecure_content_(false),
[email protected]fdd61c62009-04-22 19:22:57185 capturing_contents_(false),
186 is_being_destroyed_(false),
187 notify_disconnection_(false),
[email protected]fdd61c62009-04-22 19:22:57188#if defined(OS_WIN)
189 message_box_active_(CreateEvent(NULL, TRUE, FALSE, NULL)),
190#endif
[email protected]7ab1e7d62009-10-14 23:32:01191 is_showing_before_unload_dialog_(false),
[email protected]1fd1a502011-03-30 16:55:56192 opener_web_ui_type_(WebUI::kNoWebUI),
[email protected]ebf40a72010-07-22 01:46:38193 closed_by_user_gesture_(false),
[email protected]b75b8292010-10-01 07:28:25194 minimum_zoom_percent_(
195 static_cast<int>(WebKit::WebView::minTextSizeMultiplier * 100)),
196 maximum_zoom_percent_(
197 static_cast<int>(WebKit::WebView::maxTextSizeMultiplier * 100)),
[email protected]9e823662010-10-13 23:36:00198 temporary_zoom_settings_(false),
[email protected]4850a7f2011-03-08 23:36:59199 content_restrictions_(0) {
[email protected]7ff431e2010-01-07 18:18:54200
[email protected]12636df2009-09-28 22:32:21201 render_manager_.Init(profile, site_instance, routing_id);
[email protected]420ae012009-04-24 05:16:32202
[email protected]34ac70502009-09-25 17:07:23203 // We have the initial size of the view be based on the size of the passed in
204 // tab contents (normally a tab from the same window).
205 view_->CreateView(base_tab_contents ?
206 base_tab_contents->view()->GetContainerSize() : gfx::Size());
[email protected]332af7732009-03-11 13:21:35207}
initial.commit09911bf2008-07-26 23:55:29208
209TabContents::~TabContents() {
[email protected]420ae012009-04-24 05:16:32210 is_being_destroyed_ = true;
211
[email protected]3ab9cb82011-06-03 18:02:07212 // Clear out any JavaScript state.
213 if (delegate_)
214 delegate_->GetJavaScriptDialogCreator()->ResetJavaScriptState(this);
215
[email protected]420ae012009-04-24 05:16:32216 NotifyDisconnected();
[email protected]420ae012009-04-24 05:16:32217
[email protected]420ae012009-04-24 05:16:32218 // First cleanly close all child windows.
219 // TODO(mpcomplete): handle case if MaybeCloseChildWindows() already asked
220 // some of these to close. CloseWindows is async, so it might get called
221 // twice before it runs.
[email protected]8cc8d492010-02-02 10:40:49222 CloseConstrainedWindows();
[email protected]420ae012009-04-24 05:16:32223
[email protected]420ae012009-04-24 05:16:32224 // Notify any observer that have a reference on this tab contents.
225 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:27226 content::NOTIFICATION_TAB_CONTENTS_DESTROYED,
[email protected]420ae012009-04-24 05:16:32227 Source<TabContents>(this),
228 NotificationService::NoDetails());
229
230 // TODO(brettw) this should be moved to the view.
231#if defined(OS_WIN)
232 // If we still have a window handle, destroy it. GetNativeView can return
233 // NULL if this contents was part of a window that closed.
[email protected]50bd6452010-11-27 19:39:42234 if (GetNativeView()) {
235 RenderViewHost* host = render_view_host();
[email protected]b9a1fb42011-04-12 23:16:44236 if (host && host->view())
[email protected]50bd6452010-11-27 19:39:42237 host->view()->WillWmDestroy();
[email protected]50bd6452010-11-27 19:39:42238 }
[email protected]420ae012009-04-24 05:16:32239#endif
[email protected]7ab1e7d62009-10-14 23:32:01240
241 // OnCloseStarted isn't called in unit tests.
242 if (!tab_close_start_time_.is_null()) {
243 UMA_HISTOGRAM_TIMES("Tab.Close",
244 base::TimeTicks::Now() - tab_close_start_time_);
245 }
[email protected]b5a1d11c2011-02-17 03:09:42246
[email protected]07d490bc2011-03-07 17:05:26247 FOR_EACH_OBSERVER(TabContentsObserver, observers_, TabContentsDestroyed());
[email protected]232a5812011-03-04 22:42:08248
[email protected]1de2b8b2011-06-29 19:38:46249 set_delegate(NULL);
250}
251
252// TODO(cbentzel): Either remove the debugging code, or rename to SetDelegate.
253void TabContents::set_delegate(TabContentsDelegate* delegate) {
254 if (delegate == delegate_)
255 return;
256 if (delegate_)
257 delegate_->Detach(this);
258 delegate_ = delegate;
259 if (delegate_)
260 delegate_->Attach(this);
[email protected]b5a1d11c2011-02-17 03:09:42261}
262
[email protected]724159a2010-12-30 01:11:18263bool TabContents::OnMessageReceived(const IPC::Message& message) {
[email protected]f82d57b52011-04-27 19:13:17264 if (web_ui() && web_ui()->OnMessageReceived(message))
265 return true;
266
[email protected]585b30362011-01-28 02:30:17267 ObserverListBase<TabContentsObserver>::Iterator it(observers_);
268 TabContentsObserver* observer;
[email protected]0f180ee2011-01-21 18:23:10269 while ((observer = it.GetNext()) != NULL)
270 if (observer->OnMessageReceived(message))
[email protected]403415a2011-01-10 18:57:53271 return true;
[email protected]403415a2011-01-10 18:57:53272
[email protected]724159a2010-12-30 01:11:18273 bool handled = true;
274 bool message_is_ok = true;
275 IPC_BEGIN_MESSAGE_MAP_EX(TabContents, message, message_is_ok)
276 IPC_MESSAGE_HANDLER(ViewHostMsg_DidStartProvisionalLoadForFrame,
277 OnDidStartProvisionalLoadForFrame)
278 IPC_MESSAGE_HANDLER(ViewHostMsg_DidRedirectProvisionalLoad,
279 OnDidRedirectProvisionalLoad)
280 IPC_MESSAGE_HANDLER(ViewHostMsg_DidFailProvisionalLoadWithError,
281 OnDidFailProvisionalLoadWithError)
282 IPC_MESSAGE_HANDLER(ViewHostMsg_DidLoadResourceFromMemoryCache,
283 OnDidLoadResourceFromMemoryCache)
284 IPC_MESSAGE_HANDLER(ViewHostMsg_DidDisplayInsecureContent,
285 OnDidDisplayInsecureContent)
286 IPC_MESSAGE_HANDLER(ViewHostMsg_DidRunInsecureContent,
287 OnDidRunInsecureContent)
288 IPC_MESSAGE_HANDLER(ViewHostMsg_DocumentLoadedInFrame,
289 OnDocumentLoadedInFrame)
290 IPC_MESSAGE_HANDLER(ViewHostMsg_DidFinishLoad, OnDidFinishLoad)
[email protected]c8f73ab2011-01-22 00:05:17291 IPC_MESSAGE_HANDLER(ViewHostMsg_UpdateContentRestrictions,
292 OnUpdateContentRestrictions)
[email protected]7d472472011-01-22 01:30:25293 IPC_MESSAGE_HANDLER(ViewHostMsg_GoToEntryAtOffset, OnGoToEntryAtOffset)
[email protected]216813952011-05-19 22:21:26294 IPC_MESSAGE_HANDLER(ViewHostMsg_UpdateZoomLimits, OnUpdateZoomLimits)
295 IPC_MESSAGE_HANDLER(ViewHostMsg_FocusedNodeChanged, OnFocusedNodeChanged)
[email protected]c7dd2f62011-07-18 15:57:59296 IPC_MESSAGE_HANDLER(ViewHostMsg_SaveURLAs, OnSaveURL)
[email protected]724159a2010-12-30 01:11:18297 IPC_MESSAGE_UNHANDLED(handled = false)
298 IPC_END_MESSAGE_MAP_EX()
299
300 if (!message_is_ok) {
301 UserMetrics::RecordAction(UserMetricsAction("BadMessageTerminate_RVD"));
302 GetRenderProcessHost()->ReceivedBadMessage();
303 }
304
305 return handled;
306}
307
[email protected]8cb5d5b2010-02-09 11:36:16308RenderProcessHost* TabContents::GetRenderProcessHost() const {
309 return render_manager_.current_host()->process();
310}
311
[email protected]d5f942ba2008-09-26 19:30:34312const GURL& TabContents::GetURL() const {
313 // We may not have a navigation entry yet
[email protected]ce3fa3c2009-04-20 19:55:57314 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]ebe89e062009-08-13 23:16:54315 return entry ? entry->virtual_url() : GURL::EmptyGURL();
[email protected]d5f942ba2008-09-26 19:30:34316}
317
[email protected]96d185d2009-04-24 03:28:54318const string16& TabContents::GetTitle() const {
[email protected]4c6092c5b2009-06-06 00:23:55319 // Transient entries take precedence. They are used for interstitial pages
320 // that are shown on top of existing pages.
321 NavigationEntry* entry = controller_.GetTransientEntry();
[email protected]b5cca982011-05-26 04:42:08322 std::string accept_languages =
323 content::GetContentClient()->browser()->GetAcceptLangs(this);
[email protected]45d0ef7f2011-01-05 13:46:23324 if (entry) {
[email protected]b5cca982011-05-26 04:42:08325 return entry->GetTitleForDisplay(accept_languages);
[email protected]45d0ef7f2011-01-05 13:46:23326 }
[email protected]7ade2732011-02-10 00:13:58327 WebUI* our_web_ui = render_manager_.pending_web_ui() ?
328 render_manager_.pending_web_ui() : render_manager_.web_ui();
329 if (our_web_ui) {
[email protected]96d185d2009-04-24 03:28:54330 // Don't override the title in view source mode.
[email protected]4c6092c5b2009-06-06 00:23:55331 entry = controller_.GetActiveEntry();
[email protected]96d185d2009-04-24 03:28:54332 if (!(entry && entry->IsViewSourceMode())) {
[email protected]e0112912011-02-02 22:54:35333 // Give the Web UI the chance to override our title.
[email protected]7ade2732011-02-10 00:13:58334 const string16& title = our_web_ui->overridden_title();
[email protected]96d185d2009-04-24 03:28:54335 if (!title.empty())
336 return title;
337 }
338 }
339
340 // We use the title for the last committed entry rather than a pending
341 // navigation entry. For example, when the user types in a URL, we want to
342 // keep the old page's title until the new load has committed and we get a new
343 // title.
[email protected]96d185d2009-04-24 03:28:54344 entry = controller_.GetLastCommittedEntry();
[email protected]45d0ef7f2011-01-05 13:46:23345 if (entry) {
[email protected]b5cca982011-05-26 04:42:08346 return entry->GetTitleForDisplay(accept_languages);
[email protected]45d0ef7f2011-01-05 13:46:23347 }
[email protected]987fc3a2011-05-26 14:18:09348
349 // |page_title_when_no_navigation_entry_| is finally used
350 // if no title cannot be retrieved.
351 return page_title_when_no_navigation_entry_;
[email protected]96d185d2009-04-24 03:28:54352}
353
[email protected]d5f942ba2008-09-26 19:30:34354int32 TabContents::GetMaxPageID() {
355 if (GetSiteInstance())
356 return GetSiteInstance()->max_page_id();
357 else
358 return max_page_id_;
359}
360
361void TabContents::UpdateMaxPageID(int32 page_id) {
362 // Ensure both the SiteInstance and RenderProcessHost update their max page
[email protected]57c6a652009-05-04 07:58:34363 // IDs in sync. Only TabContents will also have site instances, except during
[email protected]d5f942ba2008-09-26 19:30:34364 // testing.
365 if (GetSiteInstance())
366 GetSiteInstance()->UpdateMaxPageID(page_id);
[email protected]8cb5d5b2010-02-09 11:36:16367 GetRenderProcessHost()->UpdateMaxPageID(page_id);
[email protected]d5f942ba2008-09-26 19:30:34368}
369
[email protected]96d185d2009-04-24 03:28:54370SiteInstance* TabContents::GetSiteInstance() const {
371 return render_manager_.current_host()->site_instance();
372}
373
[email protected]96d185d2009-04-24 03:28:54374bool TabContents::ShouldDisplayURL() {
[email protected]8ab24cc2009-11-13 20:56:15375 // Don't hide the url in view source mode and with interstitials.
[email protected]96d185d2009-04-24 03:28:54376 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]8ab24cc2009-11-13 20:56:15377 if (entry && (entry->IsViewSourceMode() ||
[email protected]cccd3762010-11-12 18:40:01378 entry->page_type() == INTERSTITIAL_PAGE)) {
[email protected]96d185d2009-04-24 03:28:54379 return true;
[email protected]8ab24cc2009-11-13 20:56:15380 }
[email protected]83e3895b2009-06-11 00:07:16381
[email protected]2b9a06402011-02-16 04:57:10382 // We always display the URL for non-WebUI URLs to prevent spoofing.
[email protected]1fd1a502011-03-30 16:55:56383 if (entry && !content::WebUIFactory::Get()->HasWebUIScheme(entry->url()))
[email protected]2b9a06402011-02-16 04:57:10384 return true;
385
[email protected]d0980792011-02-13 19:41:40386 WebUI* web_ui = GetWebUIForCurrentState();
[email protected]7ade2732011-02-10 00:13:58387 if (web_ui)
388 return !web_ui->should_hide_url();
[email protected]96d185d2009-04-24 03:28:54389 return true;
390}
391
[email protected]f5d978c2011-07-21 14:43:51392bool TabContents::IsLoading() const {
393 return is_loading_ || (web_ui() && web_ui()->IsLoading());
394}
395
[email protected]585b30362011-01-28 02:30:17396void TabContents::AddObserver(TabContentsObserver* observer) {
397 observers_.AddObserver(observer);
[email protected]3c9e1872010-11-18 16:17:49398}
399
[email protected]585b30362011-01-28 02:30:17400void TabContents::RemoveObserver(TabContentsObserver* observer) {
401 observers_.RemoveObserver(observer);
[email protected]3c9e1872010-11-18 16:17:49402}
403
[email protected]443b80e2010-12-14 00:42:23404void TabContents::SetIsCrashed(base::TerminationStatus status, int error_code) {
405 if (status == crashed_status_)
[email protected]d5f942ba2008-09-26 19:30:34406 return;
407
[email protected]443b80e2010-12-14 00:42:23408 crashed_status_ = status;
409 crashed_error_code_ = error_code;
[email protected]c9cd2222009-05-06 05:16:50410 NotifyNavigationStateChanged(INVALIDATE_TAB);
[email protected]d5f942ba2008-09-26 19:30:34411}
412
413void TabContents::NotifyNavigationStateChanged(unsigned changed_flags) {
414 if (delegate_)
415 delegate_->NavigationStateChanged(this, changed_flags);
416}
417
[email protected]96d185d2009-04-24 03:28:54418void TabContents::DidBecomeSelected() {
419 controller_.SetActive(true);
[email protected]8cb5d5b2010-02-09 11:36:16420 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
421 if (rwhv) {
422 rwhv->DidBecomeSelected();
[email protected]789e9152009-08-04 21:59:43423#if defined(OS_MACOSX)
[email protected]8cb5d5b2010-02-09 11:36:16424 rwhv->SetActive(true);
[email protected]789e9152009-08-04 21:59:43425#endif
426 }
[email protected]96d185d2009-04-24 03:28:54427
[email protected]5ac20162010-11-24 23:33:11428 last_selected_time_ = base::TimeTicks::Now();
[email protected]3e69bc82011-05-26 23:22:38429
430 FOR_EACH_OBSERVER(TabContentsObserver, observers_, DidBecomeSelected());
[email protected]96d185d2009-04-24 03:28:54431}
432
433void TabContents::WasHidden() {
434 if (!capturing_contents()) {
435 // |render_view_host()| can be NULL if the user middle clicks a link to open
436 // a tab in then background, then closes the tab before selecting it. This
437 // is because closing the tab calls TabContents::Destroy(), which removes
438 // the |render_view_host()|; then when we actually destroy the window,
439 // OnWindowPosChanged() notices and calls HideContents() (which calls us).
[email protected]8cb5d5b2010-02-09 11:36:16440 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
441 if (rwhv)
442 rwhv->WasHidden();
[email protected]96d185d2009-04-24 03:28:54443 }
444
445 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:27446 content::NOTIFICATION_TAB_CONTENTS_HIDDEN,
[email protected]96d185d2009-04-24 03:28:54447 Source<TabContents>(this),
448 NotificationService::NoDetails());
449}
450
[email protected]d5f942ba2008-09-26 19:30:34451void TabContents::Activate() {
452 if (delegate_)
453 delegate_->ActivateContents(this);
454}
455
[email protected]ea42e7782010-08-23 23:58:12456void TabContents::Deactivate() {
457 if (delegate_)
458 delegate_->DeactivateContents(this);
459}
460
[email protected]63954792011-07-11 04:17:48461void TabContents::LostCapture() {
462 if (delegate_)
463 delegate_->LostCapture();
464}
465
466bool TabContents::PreHandleKeyboardEvent(const NativeWebKeyboardEvent& event,
467 bool* is_keyboard_shortcut) {
468 return delegate_ &&
469 delegate_->PreHandleKeyboardEvent(event, is_keyboard_shortcut);
470}
471
472void TabContents::HandleKeyboardEvent(const NativeWebKeyboardEvent& event) {
473 if (delegate_)
474 delegate_->HandleKeyboardEvent(event);
475}
476
477void TabContents::HandleMouseUp() {
478 if (delegate_)
479 delegate_->HandleMouseUp();
480}
481
482void TabContents::HandleMouseActivate() {
483 if (delegate_)
484 delegate_->HandleMouseActivate();
485}
486
[email protected]96d185d2009-04-24 03:28:54487void TabContents::ShowContents() {
[email protected]8cb5d5b2010-02-09 11:36:16488 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
489 if (rwhv)
490 rwhv->DidBecomeSelected();
[email protected]96d185d2009-04-24 03:28:54491}
492
493void TabContents::HideContents() {
494 // TODO(pkasting): http://b/1239839 Right now we purposefully don't call
495 // our superclass HideContents(), because some callers want to be very picky
496 // about the order in which these get called. In addition to making the code
497 // here practically impossible to understand, this also means we end up
498 // calling TabContents::WasHidden() twice if callers call both versions of
[email protected]57c6a652009-05-04 07:58:34499 // HideContents() on a TabContents.
[email protected]96d185d2009-04-24 03:28:54500 WasHidden();
501}
502
[email protected]5aab5e22010-12-08 22:13:29503bool TabContents::NeedToFireBeforeUnload() {
504 // TODO(creis): Should we fire even for interstitial pages?
505 return notify_disconnection() &&
506 !showing_interstitial_page() &&
507 !render_view_host()->SuddenTerminationAllowed();
508}
509
[email protected]992e4542011-07-20 23:09:25510TabContents* TabContents::OpenURL(const GURL& url,
511 const GURL& referrer,
512 WindowOpenDisposition disposition,
513 PageTransition::Type transition) {
[email protected]1c642b52011-04-15 09:05:49514 if (delegate_) {
[email protected]992e4542011-07-20 23:09:25515 TabContents* new_contents =
516 delegate_->OpenURLFromTab(this, url, referrer, disposition, transition);
[email protected]1c642b52011-04-15 09:05:49517 // Notify observers.
518 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
519 DidOpenURL(url, referrer, disposition, transition));
[email protected]992e4542011-07-20 23:09:25520 return new_contents;
[email protected]1c642b52011-04-15 09:05:49521 }
[email protected]992e4542011-07-20 23:09:25522 return NULL;
[email protected]d5f942ba2008-09-26 19:30:34523}
524
[email protected]1ccb3568d2010-02-19 10:51:16525bool TabContents::NavigateToPendingEntry(
526 NavigationController::ReloadType reload_type) {
[email protected]876bc832010-09-07 16:29:54527 return NavigateToEntry(*controller_.pending_entry(), reload_type);
528}
[email protected]96d185d2009-04-24 03:28:54529
[email protected]876bc832010-09-07 16:29:54530bool TabContents::NavigateToEntry(
531 const NavigationEntry& entry,
532 NavigationController::ReloadType reload_type) {
[email protected]96d185d2009-04-24 03:28:54533 RenderViewHost* dest_render_view_host = render_manager_.Navigate(entry);
534 if (!dest_render_view_host)
535 return false; // Unable to create the desired render view host.
536
[email protected]80a8fad2011-01-29 04:02:38537 // For security, we should never send non-Web-UI URLs to a Web UI renderer.
[email protected]05fdd492010-11-15 17:52:07538 // Double check that here.
[email protected]54ec6472010-04-09 19:39:58539 int enabled_bindings = dest_render_view_host->enabled_bindings();
[email protected]1fd1a502011-03-30 16:55:56540 bool is_allowed_in_web_ui_renderer = content::GetContentClient()->
541 browser()->GetWebUIFactory()->IsURLAcceptableForWebUI(profile(),
542 entry.url());
[email protected]c09163a2011-02-15 00:05:55543 CHECK(!BindingsPolicy::is_web_ui_enabled(enabled_bindings) ||
544 is_allowed_in_web_ui_renderer);
[email protected]54ec6472010-04-09 19:39:58545
[email protected]96d185d2009-04-24 03:28:54546 // Tell DevTools agent that it is attached prior to the navigation.
[email protected]d9f9b792009-06-24 13:17:12547 DevToolsManager* devtools_manager = DevToolsManager::GetInstance();
[email protected]7e8e6b62009-05-08 11:28:32548 if (devtools_manager) { // NULL in unit tests.
[email protected]3cc72b12010-03-18 23:03:00549 devtools_manager->OnNavigatingToPendingEntry(render_view_host(),
550 dest_render_view_host,
551 entry.url());
[email protected]7e8e6b62009-05-08 11:28:32552 }
[email protected]96d185d2009-04-24 03:28:54553
554 // Used for page load time metrics.
555 current_load_start_ = base::TimeTicks::Now();
556
557 // Navigate in the desired RenderViewHost.
[email protected]056de2d2009-06-26 16:41:34558 ViewMsg_Navigate_Params navigate_params;
[email protected]876bc832010-09-07 16:29:54559 MakeNavigateParams(entry, controller_, reload_type, &navigate_params);
[email protected]52c68652010-12-07 17:47:04560 if (delegate_) {
561 navigate_params.extra_headers =
562 delegate_->GetNavigationHeaders(navigate_params.url);
563 }
[email protected]056de2d2009-06-26 16:41:34564 dest_render_view_host->Navigate(navigate_params);
[email protected]96d185d2009-04-24 03:28:54565
566 if (entry.page_id() == -1) {
567 // HACK!! This code suppresses javascript: URLs from being added to
568 // session history, which is what we want to do for javascript: URLs that
569 // do not generate content. What we really need is a message from the
570 // renderer telling us that a new page was not created. The same message
571 // could be used for mailto: URLs and the like.
572 if (entry.url().SchemeIs(chrome::kJavaScriptScheme))
573 return false;
574 }
575
[email protected]3c9e1872010-11-18 16:17:49576 // Notify observers about navigation.
[email protected]b375c5d2011-05-03 21:15:04577 FOR_EACH_OBSERVER(TabContentsObserver,
578 observers_,
579 NavigateToPendingEntry(entry.url(), reload_type));
[email protected]96d185d2009-04-24 03:28:54580
[email protected]09b29342011-06-24 19:18:48581 if (delegate_)
582 delegate_->DidNavigateToPendingEntry(this);
583
[email protected]d5f942ba2008-09-26 19:30:34584 return true;
585}
586
[email protected]96d185d2009-04-24 03:28:54587void TabContents::Stop() {
588 render_manager_.Stop();
[email protected]2e3f4572011-03-25 19:24:47589 FOR_EACH_OBSERVER(TabContentsObserver, observers_, StopNavigation());
[email protected]96d185d2009-04-24 03:28:54590}
591
[email protected]96d185d2009-04-24 03:28:54592TabContents* TabContents::Clone() {
593 // We create a new SiteInstance so that the new tab won't share processes
594 // with the old one. This can be changed in the future if we need it to share
595 // processes for some reason.
[email protected]420ae012009-04-24 05:16:32596 TabContents* tc = new TabContents(profile(),
[email protected]96d185d2009-04-24 03:28:54597 SiteInstance::CreateSiteInstance(profile()),
[email protected]6ee12c42010-09-14 09:36:07598 MSG_ROUTING_NONE, this, NULL);
[email protected]96d185d2009-04-24 03:28:54599 tc->controller().CopyStateFrom(controller_);
600 return tc;
601}
602
[email protected]4d677202009-07-19 07:37:12603void TabContents::ShowPageInfo(const GURL& url,
604 const NavigationEntry::SSLStatus& ssl,
605 bool show_history) {
606 if (!delegate_)
607 return;
608
[email protected]bb678332009-07-21 00:15:50609 delegate_->ShowPageInfo(profile(), url, ssl, show_history);
[email protected]4d677202009-07-19 07:37:12610}
611
initial.commit09911bf2008-07-26 23:55:29612ConstrainedWindow* TabContents::CreateConstrainedDialog(
[email protected]e8382172009-06-19 22:16:28613 ConstrainedWindowDelegate* delegate) {
initial.commit09911bf2008-07-26 23:55:29614 ConstrainedWindow* window =
[email protected]e8382172009-06-19 22:16:28615 ConstrainedWindow::CreateConstrainedDialog(this, delegate);
[email protected]aed59602011-02-28 22:57:33616 AddConstrainedDialog(window);
617 return window;
618}
619
620void TabContents::AddConstrainedDialog(ConstrainedWindow* window) {
initial.commit09911bf2008-07-26 23:55:29621 child_windows_.push_back(window);
[email protected]fa1cf0b82010-01-15 21:49:44622
623 if (child_windows_.size() == 1) {
624 window->ShowConstrainedWindow();
625 BlockTabContent(true);
626 }
initial.commit09911bf2008-07-26 23:55:29627}
628
[email protected]fa1cf0b82010-01-15 21:49:44629void TabContents::BlockTabContent(bool blocked) {
[email protected]8dccd7cb2010-02-25 05:19:55630 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
[email protected]1b072562010-12-13 19:37:45631 // 70% opaque grey.
632 SkColor greyish = SkColorSetARGB(178, 0, 0, 0);
[email protected]8dccd7cb2010-02-25 05:19:55633 if (rwhv)
[email protected]1b072562010-12-13 19:37:45634 rwhv->SetVisuallyDeemphasized(blocked ? &greyish : NULL, false);
[email protected]a7eccac2011-04-15 03:05:19635 // RenderViewHost may be NULL during shutdown.
636 if (render_view_host())
637 render_view_host()->set_ignore_input_events(blocked);
[email protected]fa1cf0b82010-01-15 21:49:44638 if (delegate_)
639 delegate_->SetTabContentBlocked(this, blocked);
640}
641
[email protected]e7cfdbd2011-04-22 14:41:37642void TabContents::AddNewContents(TabContents* new_contents,
643 WindowOpenDisposition disposition,
644 const gfx::Rect& initial_pos,
645 bool user_gesture) {
initial.commit09911bf2008-07-26 23:55:29646 if (!delegate_)
647 return;
648
[email protected]e7cfdbd2011-04-22 14:41:37649 delegate_->AddNewContents(this, new_contents, disposition, initial_pos,
650 user_gesture);
[email protected]96d185d2009-04-24 03:28:54651}
652
[email protected]5c9e97a2009-09-09 23:48:30653gfx::NativeView TabContents::GetContentNativeView() const {
[email protected]96d185d2009-04-24 03:28:54654 return view_->GetContentNativeView();
655}
656
657gfx::NativeView TabContents::GetNativeView() const {
658 return view_->GetNativeView();
659}
660
661void TabContents::GetContainerBounds(gfx::Rect *out) const {
662 view_->GetContainerBounds(out);
663}
664
665void TabContents::Focus() {
666 view_->Focus();
667}
668
[email protected]90daadb42009-06-08 21:27:28669void TabContents::FocusThroughTabTraversal(bool reverse) {
[email protected]7e383692009-06-12 19:14:54670 if (showing_interstitial_page()) {
671 render_manager_.interstitial_page()->FocusThroughTabTraversal(reverse);
672 return;
673 }
[email protected]96d185d2009-04-24 03:28:54674 render_view_host()->SetInitialFocus(reverse);
675}
676
677bool TabContents::FocusLocationBarByDefault() {
[email protected]d0980792011-02-13 19:41:40678 WebUI* web_ui = GetWebUIForCurrentState();
[email protected]7ade2732011-02-10 00:13:58679 if (web_ui)
680 return web_ui->focus_location_bar_by_default();
[email protected]3e3f0eb2009-06-22 18:33:43681 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]e0d481582009-09-15 21:06:25682 if (entry && entry->url() == GURL(chrome::kAboutBlankURL))
[email protected]3e3f0eb2009-06-22 18:33:43683 return true;
[email protected]96d185d2009-04-24 03:28:54684 return false;
685}
686
[email protected]a26dc362010-04-23 01:48:58687void TabContents::SetFocusToLocationBar(bool select_all) {
[email protected]a11aaf22010-03-30 00:03:38688 if (delegate())
[email protected]a26dc362010-04-23 01:48:58689 delegate()->SetFocusToLocationBar(select_all);
[email protected]a11aaf22010-03-30 00:03:38690}
691
[email protected]686493142011-07-15 21:47:22692bool TabContents::CanDownload(int request_id) {
693 TabContentsDelegate* d = delegate();
694 if (d)
695 return d->CanDownload(this, request_id);
696 return true;
697}
698
699void TabContents::OnStartDownload(DownloadItem* download) {
700 TabContentsDelegate* d = delegate();
701 if (d)
702 d->OnStartDownload(this, download);
703}
704
[email protected]d5f942ba2008-09-26 19:30:34705void TabContents::WillClose(ConstrainedWindow* window) {
[email protected]d6219912010-11-17 19:47:37706 ConstrainedWindowList::iterator i(
707 std::find(child_windows_.begin(), child_windows_.end(), window));
708 bool removed_topmost_window = i == child_windows_.begin();
709 if (i != child_windows_.end())
710 child_windows_.erase(i);
711 if (child_windows_.empty()) {
[email protected]fa1cf0b82010-01-15 21:49:44712 BlockTabContent(false);
[email protected]d6219912010-11-17 19:47:37713 } else {
714 if (removed_topmost_window)
715 child_windows_[0]->ShowConstrainedWindow();
716 BlockTabContent(true);
[email protected]fa1cf0b82010-01-15 21:49:44717 }
[email protected]66ba4932009-06-04 19:22:13718}
[email protected]d5f942ba2008-09-26 19:30:34719
[email protected]c7dd2f62011-07-18 15:57:59720void TabContents::OnSavePage() {
721 // If we can not save the page, try to download it.
722 if (!SavePackage::IsSavableContents(contents_mime_type())) {
723 DownloadManager* dlm = profile()->GetDownloadManager();
724 const GURL& current_page_url = GetURL();
725 if (dlm && current_page_url.is_valid()) {
726 dlm->DownloadUrl(current_page_url, GURL(), "", this);
727 download_util::RecordDownloadCount(
728 download_util::INITIATED_BY_SAVE_PACKAGE_FAILURE_COUNT);
729 return;
730 }
731 }
732
733 Stop();
734
735 // Create the save package and possibly prompt the user for the name to save
736 // the page as. The user prompt is an asynchronous operation that runs on
737 // another thread.
738 save_package_ = new SavePackage(this);
739 save_package_->GetSaveInfo();
740}
741
742// Used in automated testing to bypass prompting the user for file names.
743// Instead, the names and paths are hard coded rather than running them through
744// file name sanitation and extension / mime checking.
745bool TabContents::SavePage(const FilePath& main_file, const FilePath& dir_path,
746 SavePackage::SavePackageType save_type) {
747 // Stop the page from navigating.
748 Stop();
749
750 save_package_ = new SavePackage(this, save_type, main_file, dir_path);
751 return save_package_->Init();
752}
753
754void TabContents::OnSaveURL(const GURL& url) {
755 DownloadManager* dlm = profile()->GetDownloadManager();
756 dlm->DownloadUrl(url, GetURL(), "", this);
757}
758
[email protected]420ae012009-04-24 05:16:32759bool TabContents::IsActiveEntry(int32 page_id) {
760 NavigationEntry* active_entry = controller_.GetActiveEntry();
761 return (active_entry != NULL &&
762 active_entry->site_instance() == GetSiteInstance() &&
763 active_entry->page_id() == page_id);
764}
765
[email protected]b2fe07d12010-02-09 14:38:08766void TabContents::SetOverrideEncoding(const std::string& encoding) {
[email protected]8cb5d5b2010-02-09 11:36:16767 set_encoding(encoding);
[email protected]216813952011-05-19 22:21:26768 render_view_host()->Send(new ViewMsg_SetPageEncoding(
769 render_view_host()->routing_id(), encoding));
[email protected]8cb5d5b2010-02-09 11:36:16770}
771
[email protected]b2fe07d12010-02-09 14:38:08772void TabContents::ResetOverrideEncoding() {
[email protected]8cb5d5b2010-02-09 11:36:16773 reset_encoding();
[email protected]216813952011-05-19 22:21:26774 render_view_host()->Send(new ViewMsg_ResetPageEncodingToDefault(
775 render_view_host()->routing_id()));
[email protected]8cb5d5b2010-02-09 11:36:16776}
777
[email protected]7ab1e7d62009-10-14 23:32:01778void TabContents::OnCloseStarted() {
779 if (tab_close_start_time_.is_null())
780 tab_close_start_time_ = base::TimeTicks::Now();
781}
782
[email protected]46624bf2010-06-09 16:04:19783bool TabContents::ShouldAcceptDragAndDrop() const {
784#if defined(OS_CHROMEOS)
785 // ChromeOS panels (pop-ups) do not take drag-n-drop.
786 // See http://crosbug.com/2413
[email protected]b35b26b32011-05-05 20:35:14787 if (delegate() && delegate()->IsPopupOrPanel(this))
[email protected]b83af492010-10-09 03:21:20788 return false;
789 return true;
[email protected]46624bf2010-06-09 16:04:19790#else
791 return true;
792#endif
793}
794
[email protected]7813bd72011-02-05 02:19:34795void TabContents::SystemDragEnded() {
796 if (render_view_host())
797 render_view_host()->DragSourceSystemDragEnded();
798 if (delegate())
799 delegate()->DragEnded();
800}
801
[email protected]d0b8d092010-10-25 04:05:17802double TabContents::GetZoomLevel() const {
[email protected]b75b8292010-10-01 07:28:25803 HostZoomMap* zoom_map = profile()->GetHostZoomMap();
804 if (!zoom_map)
[email protected]d0b8d092010-10-25 04:05:17805 return 0;
[email protected]b75b8292010-10-01 07:28:25806
807 double zoom_level;
808 if (temporary_zoom_settings_) {
809 zoom_level = zoom_map->GetTemporaryZoomLevel(
810 GetRenderProcessHost()->id(), render_view_host()->routing_id());
811 } else {
[email protected]13ffa32e2011-05-27 16:37:01812 zoom_level = zoom_map->GetZoomLevel(net::GetHostOrSpecFromURL(GetURL()));
[email protected]b75b8292010-10-01 07:28:25813 }
[email protected]d0b8d092010-10-25 04:05:17814 return zoom_level;
815}
[email protected]b75b8292010-10-01 07:28:25816
[email protected]d0b8d092010-10-25 04:05:17817int TabContents::GetZoomPercent(bool* enable_increment,
818 bool* enable_decrement) {
819 *enable_decrement = *enable_increment = false;
[email protected]b75b8292010-10-01 07:28:25820 int percent = static_cast<int>(
[email protected]d0b8d092010-10-25 04:05:17821 WebKit::WebView::zoomLevelToZoomFactor(GetZoomLevel()) * 100);
[email protected]b75b8292010-10-01 07:28:25822 *enable_decrement = percent > minimum_zoom_percent_;
823 *enable_increment = percent < maximum_zoom_percent_;
824 return percent;
825}
826
[email protected]3c733bde2010-12-21 19:56:31827void TabContents::ViewSource() {
[email protected]1788e772010-12-15 16:40:50828 if (!delegate_)
829 return;
830
831 NavigationEntry* active_entry = controller().GetActiveEntry();
832 if (!active_entry)
833 return;
834
835 delegate_->ViewSourceForTab(this, active_entry->url());
[email protected]77d8d622010-12-15 10:30:12836}
837
[email protected]932b7a12011-03-09 12:50:27838void TabContents::ViewFrameSource(const GURL& url,
839 const std::string& content_state) {
840 if (!delegate_)
841 return;
842
843 delegate_->ViewSourceForFrame(this, url, content_state);
844}
845
[email protected]c40d6232011-03-25 00:16:21846void TabContents::SetContentRestrictions(int restrictions) {
847 content_restrictions_ = restrictions;
848 delegate()->ContentRestrictionsChanged(this);
849}
850
[email protected]724159a2010-12-30 01:11:18851void TabContents::OnDidStartProvisionalLoadForFrame(int64 frame_id,
852 bool is_main_frame,
[email protected]eacb080b2011-05-22 19:40:26853 bool has_opener_set,
[email protected]724159a2010-12-30 01:11:18854 const GURL& url) {
855 bool is_error_page = (url.spec() == chrome::kUnreachableWebDataURL);
856 GURL validated_url(url);
857 render_view_host()->FilterURL(ChildProcessSecurityPolicy::GetInstance(),
858 GetRenderProcessHost()->id(), &validated_url);
859
[email protected]8093a542011-05-13 07:29:32860 RenderViewHost* rvh =
861 render_manager_.pending_render_view_host() ?
862 render_manager_.pending_render_view_host() : render_view_host();
[email protected]0d60f0192011-04-14 12:40:10863 // Notify observers about the start of the provisional load.
864 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
865 DidStartProvisionalLoadForFrame(frame_id, is_main_frame,
[email protected]8093a542011-05-13 07:29:32866 validated_url, is_error_page, rvh));
[email protected]0d60f0192011-04-14 12:40:10867
[email protected]724159a2010-12-30 01:11:18868 if (is_main_frame) {
[email protected]4850a7f2011-03-08 23:36:59869 // Notify observers about the provisional change in the main frame URL.
870 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
[email protected]eacb080b2011-05-22 19:40:26871 ProvisionalChangeToMainFrameUrl(url, has_opener_set));
[email protected]724159a2010-12-30 01:11:18872 }
873}
874
875void TabContents::OnDidRedirectProvisionalLoad(int32 page_id,
[email protected]eacb080b2011-05-22 19:40:26876 bool has_opener_set,
[email protected]724159a2010-12-30 01:11:18877 const GURL& source_url,
878 const GURL& target_url) {
[email protected]18ba2b12011-04-06 16:35:49879 // TODO(creis): Remove this method and have the pre-rendering code listen to
880 // the ResourceDispatcherHost's RESOURCE_RECEIVED_REDIRECT notification
881 // instead. See http://crbug.com/78512.
[email protected]724159a2010-12-30 01:11:18882 NavigationEntry* entry;
883 if (page_id == -1)
884 entry = controller_.pending_entry();
885 else
886 entry = controller_.GetEntryWithPageID(GetSiteInstance(), page_id);
887 if (!entry || entry->url() != source_url)
888 return;
[email protected]e7d50892011-01-19 21:47:38889
[email protected]4850a7f2011-03-08 23:36:59890 // Notify observers about the provisional change in the main frame URL.
891 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
[email protected]eacb080b2011-05-22 19:40:26892 ProvisionalChangeToMainFrameUrl(target_url,
893 has_opener_set));
[email protected]724159a2010-12-30 01:11:18894}
895
896void TabContents::OnDidFailProvisionalLoadWithError(
897 int64 frame_id,
898 bool is_main_frame,
899 int error_code,
900 const GURL& url,
901 bool showing_repost_interstitial) {
902 VLOG(1) << "Failed Provisional Load: " << url.possibly_invalid_spec()
903 << ", error_code: " << error_code
904 << " is_main_frame: " << is_main_frame
905 << " showing_repost_interstitial: " << showing_repost_interstitial
906 << " frame_id: " << frame_id;
907 GURL validated_url(url);
908 render_view_host()->FilterURL(ChildProcessSecurityPolicy::GetInstance(),
909 GetRenderProcessHost()->id(), &validated_url);
910
911 if (net::ERR_ABORTED == error_code) {
912 // EVIL HACK ALERT! Ignore failed loads when we're showing interstitials.
913 // This means that the interstitial won't be torn down properly, which is
914 // bad. But if we have an interstitial, go back to another tab type, and
915 // then load the same interstitial again, we could end up getting the first
916 // interstitial's "failed" message (as a result of the cancel) when we're on
917 // the second one.
918 //
919 // We can't tell this apart, so we think we're tearing down the current page
920 // which will cause a crash later one. There is also some code in
921 // RenderViewHostManager::RendererAbortedProvisionalLoad that is commented
922 // out because of this problem.
923 //
924 // http://code.google.com/p/chromium/issues/detail?id=2855
925 // Because this will not tear down the interstitial properly, if "back" is
926 // back to another tab type, the interstitial will still be somewhat alive
927 // in the previous tab type. If you navigate somewhere that activates the
928 // tab with the interstitial again, you'll see a flash before the new load
929 // commits of the interstitial page.
930 if (showing_interstitial_page()) {
931 LOG(WARNING) << "Discarding message during interstitial.";
932 return;
933 }
934
[email protected]02102f82011-06-13 20:37:02935 // Discard our pending entry if the load canceled (e.g. if we decided to
936 // download the file instead of load it). We do not verify that the URL
937 // being canceled matches the pending entry's URL because they will not
938 // match if a redirect occurred (in which case we do not want to leave a
939 // stale redirect URL showing). This means that we also cancel the pending
940 // entry if the user started a new navigation. As a result, the navigation
941 // controller may not remember that a load is in progress, but the
942 // navigation will still commit even if there is no pending entry.
[email protected]724159a2010-12-30 01:11:18943 NavigationEntry* pending_entry = controller_.pending_entry();
[email protected]02102f82011-06-13 20:37:02944 if (pending_entry)
[email protected]c95fa8b2011-04-28 20:26:16945 DidCancelLoading();
[email protected]724159a2010-12-30 01:11:18946
947 render_manager_.RendererAbortedProvisionalLoad(render_view_host());
948 }
949
950 // Send out a notification that we failed a provisional load with an error.
951 ProvisionalLoadDetails details(
952 is_main_frame, controller_.IsURLInPageNavigation(validated_url),
[email protected]b9e8ea62011-03-04 06:29:09953 validated_url, std::string(), false, frame_id);
[email protected]724159a2010-12-30 01:11:18954 details.set_error_code(error_code);
955
956 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:27957 content::NOTIFICATION_FAIL_PROVISIONAL_LOAD_WITH_ERROR,
[email protected]724159a2010-12-30 01:11:18958 Source<NavigationController>(&controller_),
959 Details<ProvisionalLoadDetails>(&details));
[email protected]0d60f0192011-04-14 12:40:10960
961 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
962 DidFailProvisionalLoad(frame_id, is_main_frame,
963 validated_url, error_code));
[email protected]724159a2010-12-30 01:11:18964}
965
966void TabContents::OnDidLoadResourceFromMemoryCache(
967 const GURL& url,
[email protected]724159a2010-12-30 01:11:18968 const std::string& security_info) {
[email protected]28620862011-03-22 23:07:19969 base::StatsCounter cache("WebKit.CacheHit");
[email protected]724159a2010-12-30 01:11:18970 cache.Increment();
971
972 // Send out a notification that we loaded a resource from our memory cache.
973 int cert_id = 0, cert_status = 0, security_bits = -1, connection_status = 0;
974 SSLManager::DeserializeSecurityInfo(security_info,
975 &cert_id, &cert_status,
976 &security_bits,
977 &connection_status);
[email protected]7a8c55e2011-02-15 08:21:16978 LoadFromMemoryCacheDetails details(url, GetRenderProcessHost()->id(),
979 cert_id, cert_status);
[email protected]724159a2010-12-30 01:11:18980
981 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:27982 content::NOTIFICATION_LOAD_FROM_MEMORY_CACHE,
[email protected]724159a2010-12-30 01:11:18983 Source<NavigationController>(&controller_),
984 Details<LoadFromMemoryCacheDetails>(&details));
985}
986
987void TabContents::OnDidDisplayInsecureContent() {
[email protected]221a5d92011-04-28 18:23:16988 UserMetrics::RecordAction(UserMetricsAction("SSL.DisplayedInsecureContent"));
[email protected]724159a2010-12-30 01:11:18989 displayed_insecure_content_ = true;
990 SSLManager::NotifySSLInternalStateChanged();
991}
992
993void TabContents::OnDidRunInsecureContent(
[email protected]92771112011-01-20 00:13:02994 const std::string& security_origin, const GURL& target_url) {
[email protected]9941cf52011-02-08 16:17:23995 LOG(INFO) << security_origin << " ran insecure content from "
996 << target_url.possibly_invalid_spec();
[email protected]221a5d92011-04-28 18:23:16997 UserMetrics::RecordAction(UserMetricsAction("SSL.RanInsecureContent"));
[email protected]ca406032011-07-19 21:53:05998 if (EndsWith(security_origin, kDotGoogleDotCom, false)) {
999 UserMetrics::RecordAction(
1000 UserMetricsAction("SSL.RanInsecureContentGoogle"));
1001 }
[email protected]724159a2010-12-30 01:11:181002 controller_.ssl_manager()->DidRunInsecureContent(security_origin);
[email protected]f2b58c4d2011-06-01 23:29:411003 displayed_insecure_content_ = true;
1004 SSLManager::NotifySSLInternalStateChanged();
[email protected]724159a2010-12-30 01:11:181005}
1006
1007void TabContents::OnDocumentLoadedInFrame(int64 frame_id) {
1008 controller_.DocumentLoadedInFrame();
[email protected]0d60f0192011-04-14 12:40:101009 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
1010 DocumentLoadedInFrame(frame_id));
[email protected]724159a2010-12-30 01:11:181011}
1012
1013void TabContents::OnDidFinishLoad(int64 frame_id) {
[email protected]0d60f0192011-04-14 12:40:101014 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
1015 DidFinishLoad(frame_id));
[email protected]724159a2010-12-30 01:11:181016}
1017
[email protected]c8f73ab2011-01-22 00:05:171018void TabContents::OnUpdateContentRestrictions(int restrictions) {
[email protected]c40d6232011-03-25 00:16:211019 SetContentRestrictions(restrictions);
[email protected]c8f73ab2011-01-22 00:05:171020}
1021
[email protected]216813952011-05-19 22:21:261022void TabContents::OnGoToEntryAtOffset(int offset) {
1023 if (!delegate_ || delegate_->OnGoToEntryOffset(offset)) {
1024 NavigationEntry* entry = controller_.GetEntryAtOffset(offset);
1025 if (!entry)
1026 return;
1027 // Note that we don't call NavigationController::GotToOffset() as we don't
1028 // want to create a pending navigation entry (it might end up lingering
1029 // http://crbug.com/51680).
1030 entry->set_transition_type(entry->transition_type() |
1031 PageTransition::FORWARD_BACK);
1032 NavigateToEntry(*entry, NavigationController::NO_RELOAD);
1033 }
1034}
1035
1036void TabContents::OnUpdateZoomLimits(int minimum_percent,
1037 int maximum_percent,
1038 bool remember) {
1039 minimum_zoom_percent_ = minimum_percent;
1040 maximum_zoom_percent_ = maximum_percent;
1041 temporary_zoom_settings_ = !remember;
1042}
1043
1044void TabContents::OnFocusedNodeChanged(bool is_editable_node) {
1045 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271046 content::NOTIFICATION_FOCUS_CHANGED_IN_PAGE,
[email protected]216813952011-05-19 22:21:261047 Source<TabContents>(this),
1048 Details<const bool>(&is_editable_node));
1049}
1050
[email protected]96d185d2009-04-24 03:28:541051// Notifies the RenderWidgetHost instance about the fact that the page is
1052// loading, or done loading and calls the base implementation.
1053void TabContents::SetIsLoading(bool is_loading,
1054 LoadNotificationDetails* details) {
1055 if (is_loading == is_loading_)
1056 return;
1057
1058 if (!is_loading) {
1059 load_state_ = net::LOAD_STATE_IDLE;
1060 load_state_host_.clear();
[email protected]094e5b22009-09-25 04:23:561061 upload_size_ = 0;
1062 upload_position_ = 0;
[email protected]96d185d2009-04-24 03:28:541063 }
1064
1065 render_manager_.SetIsLoading(is_loading);
1066
1067 is_loading_ = is_loading;
1068 waiting_for_response_ = is_loading;
1069
[email protected]6ebdc9b2010-09-27 16:55:571070 if (delegate_)
1071 delegate_->LoadingStateChanged(this);
[email protected]329581b2009-04-28 06:52:351072 NotifyNavigationStateChanged(INVALIDATE_LOAD);
[email protected]96d185d2009-04-24 03:28:541073
[email protected]432115822011-07-10 15:52:271074 int type = is_loading ? content::NOTIFICATION_LOAD_START :
1075 content::NOTIFICATION_LOAD_STOP;
[email protected]ce5c4502009-05-06 16:46:111076 NotificationDetails det = NotificationService::NoDetails();
[email protected]96d185d2009-04-24 03:28:541077 if (details)
1078 det = Details<LoadNotificationDetails>(details);
1079 NotificationService::current()->Notify(type,
1080 Source<NavigationController>(&controller_),
1081 det);
1082}
1083
[email protected]d0980792011-02-13 19:41:401084WebUI* TabContents::GetWebUIForCurrentState() {
[email protected]7ade2732011-02-10 00:13:581085 // When there is a pending navigation entry, we want to use the pending WebUI
[email protected]96d185d2009-04-24 03:28:541086 // that goes along with it to control the basic flags. For example, we want to
1087 // show the pending URL in the URL bar, so we want the display_url flag to
1088 // be from the pending entry.
1089 //
1090 // The confusion comes because there are multiple possibilities for the
1091 // initial load in a tab as a side effect of the way the RenderViewHostManager
1092 // works.
1093 //
[email protected]e0112912011-02-02 22:54:351094 // - For the very first tab the load looks "normal". The new tab Web UI is
[email protected]96d185d2009-04-24 03:28:541095 // the pending one, and we want it to apply here.
1096 //
1097 // - For subsequent new tabs, they'll get a new SiteInstance which will then
1098 // get switched to the one previously associated with the new tab pages.
[email protected]7ade2732011-02-10 00:13:581099 // This switching will cause the manager to commit the RVH/WebUI. So we'll
[email protected]e0112912011-02-02 22:54:351100 // have a committed Web UI in this case.
[email protected]96d185d2009-04-24 03:28:541101 //
1102 // This condition handles all of these cases:
1103 //
1104 // - First load in first tab: no committed nav entry + pending nav entry +
1105 // pending dom ui:
[email protected]e0112912011-02-02 22:54:351106 // -> Use pending Web UI if any.
[email protected]96d185d2009-04-24 03:28:541107 //
1108 // - First load in second tab: no committed nav entry + pending nav entry +
[email protected]e0112912011-02-02 22:54:351109 // no pending Web UI:
1110 // -> Use the committed Web UI if any.
[email protected]96d185d2009-04-24 03:28:541111 //
1112 // - Second navigation in any tab: committed nav entry + pending nav entry:
[email protected]e0112912011-02-02 22:54:351113 // -> Use pending Web UI if any.
[email protected]96d185d2009-04-24 03:28:541114 //
1115 // - Normal state with no load: committed nav entry + no pending nav entry:
[email protected]e0112912011-02-02 22:54:351116 // -> Use committed Web UI.
[email protected]96d185d2009-04-24 03:28:541117 if (controller_.pending_entry() &&
1118 (controller_.GetLastCommittedEntry() ||
[email protected]7ade2732011-02-10 00:13:581119 render_manager_.pending_web_ui()))
1120 return render_manager_.pending_web_ui();
1121 return render_manager_.web_ui();
[email protected]96d185d2009-04-24 03:28:541122}
[email protected]420ae012009-04-24 05:16:321123
[email protected]1fd1a502011-03-30 16:55:561124WebUI::TypeID TabContents::GetWebUITypeForCurrentState() {
1125 return content::WebUIFactory::Get()->GetWebUIType(profile(), GetURL());
1126}
1127
[email protected]420ae012009-04-24 05:16:321128void TabContents::DidNavigateMainFramePostCommit(
[email protected]8286f51a2011-05-31 17:39:131129 const content::LoadCommittedDetails& details,
[email protected]420ae012009-04-24 05:16:321130 const ViewHostMsg_FrameNavigate_Params& params) {
[email protected]1fd1a502011-03-30 16:55:561131 if (opener_web_ui_type_ != WebUI::kNoWebUI) {
[email protected]80a8fad2011-01-29 04:02:381132 // If this is a window.open navigation, use the same WebUI as the renderer
[email protected]c2e74fe82009-09-03 17:57:441133 // that opened the window, as long as both renderers have the same
1134 // privileges.
[email protected]1fd1a502011-03-30 16:55:561135 if (delegate_ && opener_web_ui_type_ == GetWebUITypeForCurrentState()) {
1136 WebUI* web_ui = content::GetContentClient()->browser()->
1137 GetWebUIFactory()->CreateWebUIForURL(this, GetURL());
[email protected]7ade2732011-02-10 00:13:581138 // web_ui might be NULL if the URL refers to a non-existent extension.
1139 if (web_ui) {
[email protected]d0980792011-02-13 19:41:401140 render_manager_.SetWebUIPostCommit(web_ui);
[email protected]7ade2732011-02-10 00:13:581141 web_ui->RenderViewCreated(render_view_host());
[email protected]c2e74fe82009-09-03 17:57:441142 }
1143 }
[email protected]1fd1a502011-03-30 16:55:561144 opener_web_ui_type_ = WebUI::kNoWebUI;
[email protected]c2e74fe82009-09-03 17:57:441145 }
1146
[email protected]4e697b042011-07-08 06:44:561147 if (details.is_navigation_to_different_page()) {
[email protected]420ae012009-04-24 05:16:321148 // Clear the status bubble. This is a workaround for a bug where WebKit
1149 // doesn't let us know that the cursor left an element during a
1150 // transition (this is also why the mouse cursor remains as a hand after
1151 // clicking on a link); see bugs 1184641 and 980803. We don't want to
1152 // clear the bubble when a user navigates to a named anchor in the same
1153 // page.
1154 UpdateTargetURL(details.entry->page_id(), GURL());
[email protected]420ae012009-04-24 05:16:321155 }
1156
[email protected]a6e82fc2010-02-24 22:28:141157 if (!details.is_in_page) {
[email protected]f17a0ee2010-05-17 17:38:471158 // Once the main frame is navigated, we're no longer considered to have
1159 // displayed insecure content.
1160 displayed_insecure_content_ = false;
[email protected]aece2c7f2009-08-27 20:43:171161 }
[email protected]ce5c4502009-05-06 16:46:111162
[email protected]8cc8d492010-02-02 10:40:491163 // Close constrained windows if necessary.
1164 if (!net::RegistryControlledDomainService::SameDomainOrHost(
1165 details.previous_url, details.entry->url()))
1166 CloseConstrainedWindows();
1167
[email protected]3c9e1872010-11-18 16:17:491168 // Notify observers about navigation.
[email protected]585b30362011-01-28 02:30:171169 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
[email protected]3c9e1872010-11-18 16:17:491170 DidNavigateMainFramePostCommit(details, params));
[email protected]420ae012009-04-24 05:16:321171}
1172
1173void TabContents::DidNavigateAnyFramePostCommit(
1174 RenderViewHost* render_view_host,
[email protected]8286f51a2011-05-31 17:39:131175 const content::LoadCommittedDetails& details,
[email protected]420ae012009-04-24 05:16:321176 const ViewHostMsg_FrameNavigate_Params& params) {
[email protected]3ab9cb82011-06-03 18:02:071177 // If we navigate, reset JavaScript state. This does nothing to prevent
[email protected]420ae012009-04-24 05:16:321178 // a malicious script from spamming messages, since the script could just
1179 // reload the page to stop blocking.
[email protected]3ab9cb82011-06-03 18:02:071180 if (delegate_)
1181 delegate_->GetJavaScriptDialogCreator()->ResetJavaScriptState(this);
[email protected]420ae012009-04-24 05:16:321182
[email protected]3c9e1872010-11-18 16:17:491183 // Notify observers about navigation.
[email protected]585b30362011-01-28 02:30:171184 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
[email protected]3c9e1872010-11-18 16:17:491185 DidNavigateAnyFramePostCommit(details, params));
[email protected]420ae012009-04-24 05:16:321186}
1187
[email protected]8cc8d492010-02-02 10:40:491188void TabContents::CloseConstrainedWindows() {
1189 // Clear out any constrained windows since we are leaving this page entirely.
[email protected]aed59602011-02-28 22:57:331190 // To ensure that we iterate over every element in child_windows_ we
1191 // need to use a copy of child_windows_. Otherwise if
1192 // window->CloseConstrainedWindow() modifies child_windows_ we could end up
1193 // skipping some elements.
1194 ConstrainedWindowList child_windows_copy(child_windows_);
1195 for (ConstrainedWindowList::iterator it = child_windows_copy.begin();
1196 it != child_windows_copy.end(); ++it) {
1197 ConstrainedWindow* window = *it;
[email protected]fa1cf0b82010-01-15 21:49:441198 if (window) {
[email protected]420ae012009-04-24 05:16:321199 window->CloseConstrainedWindow();
[email protected]fa1cf0b82010-01-15 21:49:441200 BlockTabContent(false);
1201 }
[email protected]420ae012009-04-24 05:16:321202 }
1203}
1204
[email protected]420ae012009-04-24 05:16:321205void TabContents::UpdateMaxPageIDIfNecessary(SiteInstance* site_instance,
1206 RenderViewHost* rvh) {
1207 // If we are creating a RVH for a restored controller, then we might
1208 // have more page IDs than the SiteInstance's current max page ID. We must
1209 // make sure that the max page ID is larger than any restored page ID.
1210 // Note that it is ok for conflicting page IDs to exist in another tab
1211 // (i.e., NavigationController), but if any page ID is larger than the max,
1212 // the back/forward list will get confused.
1213 int max_restored_page_id = controller_.max_restored_page_id();
1214 if (max_restored_page_id > 0) {
1215 int curr_max_page_id = site_instance->max_page_id();
1216 if (max_restored_page_id > curr_max_page_id) {
1217 // Need to update the site instance immediately.
1218 site_instance->UpdateMaxPageID(max_restored_page_id);
1219
1220 // Also tell the renderer to update its internal representation. We
1221 // need to reserve enough IDs to make all restored page IDs less than
1222 // the max.
1223 if (curr_max_page_id < 0)
1224 curr_max_page_id = 0;
[email protected]216813952011-05-19 22:21:261225 rvh->Send(new ViewMsg_ReservePageIDRange(
1226 rvh->routing_id(), max_restored_page_id - curr_max_page_id));
[email protected]420ae012009-04-24 05:16:321227 }
1228 }
1229}
1230
[email protected]6b2f7a82011-04-25 19:30:511231bool TabContents::UpdateTitleForEntry(NavigationEntry* entry,
[email protected]acafd272011-07-26 17:35:571232 const string16& title) {
[email protected]420ae012009-04-24 05:16:321233 // For file URLs without a title, use the pathname instead. In the case of a
1234 // synthesized title, we don't want the update to count toward the "one set
1235 // per page of the title to history."
[email protected]6b2f7a82011-04-25 19:30:511236 string16 final_title;
[email protected]420ae012009-04-24 05:16:321237 bool explicit_set;
[email protected]987fc3a2011-05-26 14:18:091238 if (entry && entry->url().SchemeIsFile() && title.empty()) {
[email protected]6b2f7a82011-04-25 19:30:511239 final_title = UTF8ToUTF16(entry->url().ExtractFileName());
[email protected]420ae012009-04-24 05:16:321240 explicit_set = false; // Don't count synthetic titles toward the set limit.
1241 } else {
[email protected]acafd272011-07-26 17:35:571242 TrimWhitespace(title, TRIM_ALL, &final_title);
[email protected]420ae012009-04-24 05:16:321243 explicit_set = true;
1244 }
1245
[email protected]987fc3a2011-05-26 14:18:091246 // If a page is created via window.open and never navigated,
1247 // there will be no navigation entry. In this situation,
1248 // |page_title_when_no_navigaiton_entry_| will be used for page title.
1249 if (entry) {
1250 if (final_title == entry->title())
1251 return false; // Nothing changed, don't bother.
[email protected]420ae012009-04-24 05:16:321252
[email protected]987fc3a2011-05-26 14:18:091253 entry->set_title(final_title);
1254 } else {
1255 if (page_title_when_no_navigation_entry_ == final_title)
1256 return false; // Nothing changed, don't bother.
1257
1258 page_title_when_no_navigation_entry_ = final_title;
1259 }
[email protected]420ae012009-04-24 05:16:321260
[email protected]420ae012009-04-24 05:16:321261 // Lastly, set the title for the view.
[email protected]6b2f7a82011-04-25 19:30:511262 view_->SetPageTitle(UTF16ToWideHack(final_title));
[email protected]420ae012009-04-24 05:16:321263
[email protected]105bb0f2011-05-24 17:12:141264 TitleUpdatedDetails details(entry, explicit_set);
1265
[email protected]cbc0e1b2010-04-12 18:33:041266 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271267 content::NOTIFICATION_TAB_CONTENTS_TITLE_UPDATED,
[email protected]cbc0e1b2010-04-12 18:33:041268 Source<TabContents>(this),
[email protected]105bb0f2011-05-24 17:12:141269 Details<TitleUpdatedDetails>(&details));
[email protected]cbc0e1b2010-04-12 18:33:041270
[email protected]420ae012009-04-24 05:16:321271 return true;
1272}
1273
1274void TabContents::NotifySwapped() {
1275 // After sending out a swap notification, we need to send a disconnect
1276 // notification so that clients that pick up a pointer to |this| can NULL the
1277 // pointer. See Bug 1230284.
1278 notify_disconnection_ = true;
1279 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271280 content::NOTIFICATION_TAB_CONTENTS_SWAPPED,
[email protected]d82ed61e2009-06-16 02:46:221281 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:321282 NotificationService::NoDetails());
1283}
1284
1285void TabContents::NotifyConnected() {
1286 notify_disconnection_ = true;
1287 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271288 content::NOTIFICATION_TAB_CONTENTS_CONNECTED,
[email protected]d82ed61e2009-06-16 02:46:221289 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:321290 NotificationService::NoDetails());
1291}
1292
1293void TabContents::NotifyDisconnected() {
1294 if (!notify_disconnection_)
1295 return;
1296
1297 notify_disconnection_ = false;
1298 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271299 content::NOTIFICATION_TAB_CONTENTS_DISCONNECTED,
[email protected]d82ed61e2009-06-16 02:46:221300 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:321301 NotificationService::NoDetails());
1302}
1303
[email protected]8d3347f2009-07-09 22:00:211304RenderViewHostDelegate::View* TabContents::GetViewDelegate() {
[email protected]420ae012009-04-24 05:16:321305 return view_.get();
1306}
1307
[email protected]8d3347f2009-07-09 22:00:211308RenderViewHostDelegate::RendererManagement*
1309TabContents::GetRendererManagementDelegate() {
1310 return &render_manager_;
1311}
1312
[email protected]93623c5d2009-12-10 21:40:321313RendererPreferences TabContents::GetRendererPrefs(Profile* profile) const {
[email protected]840b1512009-07-21 16:53:461314 return renderer_preferences_;
[email protected]80d96fa2009-06-10 22:34:511315}
1316
[email protected]57c6a652009-05-04 07:58:341317TabContents* TabContents::GetAsTabContents() {
1318 return this;
1319}
1320
[email protected]7b291f92009-08-14 05:43:531321ViewType::Type TabContents::GetRenderViewType() const {
1322 return ViewType::TAB_CONTENTS;
1323}
1324
[email protected]420ae012009-04-24 05:16:321325void TabContents::RenderViewCreated(RenderViewHost* render_view_host) {
[email protected]2e4633c2009-07-09 16:58:061326 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271327 content::NOTIFICATION_RENDER_VIEW_HOST_CREATED_FOR_TAB,
[email protected]2e4633c2009-07-09 16:58:061328 Source<TabContents>(this),
1329 Details<RenderViewHost>(render_view_host));
[email protected]420ae012009-04-24 05:16:321330 NavigationEntry* entry = controller_.GetActiveEntry();
1331 if (!entry)
1332 return;
1333
1334 // When we're creating views, we're still doing initial setup, so we always
[email protected]e0112912011-02-02 22:54:351335 // use the pending Web UI rather than any possibly existing committed one.
[email protected]1fd1a502011-03-30 16:55:561336 if (render_manager_.pending_web_ui())
[email protected]7ade2732011-02-10 00:13:581337 render_manager_.pending_web_ui()->RenderViewCreated(render_view_host);
[email protected]420ae012009-04-24 05:16:321338
1339 if (entry->IsViewSourceMode()) {
1340 // Put the renderer in view source mode.
1341 render_view_host->Send(
1342 new ViewMsg_EnableViewSourceMode(render_view_host->routing_id()));
1343 }
[email protected]0666aef2009-05-13 19:48:081344
1345 view()->RenderViewCreated(render_view_host);
[email protected]11f764bb2011-05-25 19:45:261346
1347 FOR_EACH_OBSERVER(
1348 TabContentsObserver, observers_, RenderViewCreated(render_view_host));
[email protected]420ae012009-04-24 05:16:321349}
1350
1351void TabContents::RenderViewReady(RenderViewHost* rvh) {
1352 if (rvh != render_view_host()) {
1353 // Don't notify the world, since this came from a renderer in the
1354 // background.
1355 return;
1356 }
1357
1358 NotifyConnected();
[email protected]9d8a4642009-07-29 17:25:301359 bool was_crashed = is_crashed();
[email protected]443b80e2010-12-14 00:42:231360 SetIsCrashed(base::TERMINATION_STATUS_STILL_RUNNING, 0);
[email protected]9d8a4642009-07-29 17:25:301361
1362 // Restore the focus to the tab (otherwise the focus will be on the top
1363 // window).
[email protected]484ae5912010-09-29 19:16:141364 if (was_crashed && !FocusLocationBarByDefault() &&
1365 (!delegate_ || delegate_->ShouldFocusPageAfterCrash())) {
[email protected]9d8a4642009-07-29 17:25:301366 Focus();
[email protected]484ae5912010-09-29 19:16:141367 }
[email protected]420ae012009-04-24 05:16:321368}
1369
[email protected]443b80e2010-12-14 00:42:231370void TabContents::RenderViewGone(RenderViewHost* rvh,
1371 base::TerminationStatus status,
1372 int error_code) {
[email protected]420ae012009-04-24 05:16:321373 if (rvh != render_view_host()) {
1374 // The pending page's RenderViewHost is gone.
1375 return;
1376 }
1377
1378 SetIsLoading(false, NULL);
1379 NotifyDisconnected();
[email protected]443b80e2010-12-14 00:42:231380 SetIsCrashed(status, error_code);
[email protected]420ae012009-04-24 05:16:321381
[email protected]55452902011-06-01 21:57:471382 FOR_EACH_OBSERVER(TabContentsObserver, observers_, RenderViewGone());
[email protected]420ae012009-04-24 05:16:321383}
1384
[email protected]2e4633c2009-07-09 16:58:061385void TabContents::RenderViewDeleted(RenderViewHost* rvh) {
[email protected]fb7b79f72009-11-06 18:00:481386 render_manager_.RenderViewDeleted(rvh);
[email protected]2e4633c2009-07-09 16:58:061387}
1388
[email protected]420ae012009-04-24 05:16:321389void TabContents::DidNavigate(RenderViewHost* rvh,
1390 const ViewHostMsg_FrameNavigate_Params& params) {
[email protected]09b29342011-06-24 19:18:481391 if (PageTransition::IsMainFrame(params.transition))
[email protected]420ae012009-04-24 05:16:321392 render_manager_.DidNavigateMainFrame(rvh);
[email protected]8030f012009-09-25 18:09:371393
[email protected]420ae012009-04-24 05:16:321394 // Update the site of the SiteInstance if it doesn't have one yet.
1395 if (!GetSiteInstance()->has_site())
1396 GetSiteInstance()->SetSite(params.url);
1397
1398 // Need to update MIME type here because it's referred to in
1399 // UpdateNavigationCommands() called by RendererDidNavigate() to
1400 // determine whether or not to enable the encoding menu.
1401 // It's updated only for the main frame. For a subframe,
1402 // RenderView::UpdateURL does not set params.contents_mime_type.
1403 // (see http://code.google.com/p/chromium/issues/detail?id=2929 )
1404 // TODO(jungshik): Add a test for the encoding menu to avoid
1405 // regressing it again.
1406 if (PageTransition::IsMainFrame(params.transition))
1407 contents_mime_type_ = params.contents_mime_type;
1408
[email protected]8286f51a2011-05-31 17:39:131409 content::LoadCommittedDetails details;
[email protected]93f230e02011-06-01 14:40:001410 bool did_navigate = controller_.RendererDidNavigate(params, &details);
[email protected]3e90d4a2009-07-03 17:38:391411
[email protected]a9c0bfe2010-09-17 08:35:221412 // Send notification about committed provisional loads. This notification is
1413 // different from the NAV_ENTRY_COMMITTED notification which doesn't include
1414 // the actual URL navigated to and isn't sent for AUTO_SUBFRAME navigations.
1415 if (details.type != NavigationType::NAV_IGNORE) {
[email protected]dabb0d12010-10-05 12:50:071416 // For AUTO_SUBFRAME navigations, an event for the main frame is generated
1417 // that is not recorded in the navigation history. For the purpose of
1418 // tracking navigation events, we treat this event as a sub frame navigation
1419 // event.
1420 bool is_main_frame = did_navigate ? details.is_main_frame : false;
[email protected]0d60f0192011-04-14 12:40:101421 PageTransition::Type transition_type = params.transition;
[email protected]a9c0bfe2010-09-17 08:35:221422 // Whether or not a page transition was triggered by going backward or
1423 // forward in the history is only stored in the navigation controller's
1424 // entry list.
1425 if (did_navigate &&
1426 (controller_.GetActiveEntry()->transition_type() &
1427 PageTransition::FORWARD_BACK)) {
[email protected]0d60f0192011-04-14 12:40:101428 transition_type = params.transition | PageTransition::FORWARD_BACK;
[email protected]a9c0bfe2010-09-17 08:35:221429 }
[email protected]0d60f0192011-04-14 12:40:101430 // Notify observers about the commit of the provisional load.
1431 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
1432 DidCommitProvisionalLoadForFrame(params.frame_id,
1433 is_main_frame, params.url, transition_type));
[email protected]a9c0bfe2010-09-17 08:35:221434 }
1435
[email protected]76543b92009-08-31 17:27:451436 if (!did_navigate)
1437 return; // No navigation happened.
[email protected]420ae012009-04-24 05:16:321438
1439 // DO NOT ADD MORE STUFF TO THIS FUNCTION! Your component should either listen
1440 // for the appropriate notification (best) or you can add it to
1441 // DidNavigateMainFramePostCommit / DidNavigateAnyFramePostCommit (only if
1442 // necessary, please).
1443
1444 // Run post-commit tasks.
[email protected]93f230e02011-06-01 14:40:001445 if (details.is_main_frame) {
[email protected]420ae012009-04-24 05:16:321446 DidNavigateMainFramePostCommit(details, params);
[email protected]09b29342011-06-24 19:18:481447 if (delegate())
1448 delegate()->DidNavigateMainFramePostCommit(this);
[email protected]93f230e02011-06-01 14:40:001449 }
[email protected]420ae012009-04-24 05:16:321450 DidNavigateAnyFramePostCommit(rvh, details, params);
1451}
1452
1453void TabContents::UpdateState(RenderViewHost* rvh,
1454 int32 page_id,
1455 const std::string& state) {
[email protected]992db4c2011-05-12 15:37:151456 // Ensure that this state update comes from either the active RVH or one of
1457 // the swapped out RVHs. We don't expect to hear from any other RVHs.
1458 DCHECK(rvh == render_view_host() || render_manager_.IsSwappedOut(rvh));
[email protected]420ae012009-04-24 05:16:321459
1460 // We must be prepared to handle state updates for any page, these occur
1461 // when the user is scrolling and entering form data, as well as when we're
1462 // leaving a page, in which case our state may have already been moved to
1463 // the next page. The navigation controller will look up the appropriate
1464 // NavigationEntry and update it when it is notified via the delegate.
1465
1466 int entry_index = controller_.GetEntryIndexWithPageID(
[email protected]992db4c2011-05-12 15:37:151467 rvh->site_instance(), page_id);
[email protected]420ae012009-04-24 05:16:321468 if (entry_index < 0)
1469 return;
1470 NavigationEntry* entry = controller_.GetEntryAtIndex(entry_index);
1471
1472 if (state == entry->content_state())
1473 return; // Nothing to update.
1474 entry->set_content_state(state);
1475 controller_.NotifyEntryChanged(entry, entry_index);
1476}
1477
1478void TabContents::UpdateTitle(RenderViewHost* rvh,
[email protected]acafd272011-07-26 17:35:571479 int32 page_id,
1480 const string16& title) {
[email protected]420ae012009-04-24 05:16:321481 // If we have a title, that's a pretty good indication that we've started
1482 // getting useful data.
1483 SetNotWaitingForResponse();
1484
1485 DCHECK(rvh == render_view_host());
[email protected]371cc8b2010-09-01 16:03:481486 NavigationEntry* entry = controller_.GetEntryWithPageID(rvh->site_instance(),
[email protected]ec0b6c42010-08-26 03:16:581487 page_id);
[email protected]987fc3a2011-05-26 14:18:091488
1489 if (!UpdateTitleForEntry(entry, title))
[email protected]420ae012009-04-24 05:16:321490 return;
1491
1492 // Broadcast notifications when the UI should be updated.
1493 if (entry == controller_.GetEntryAtOffset(0))
[email protected]f1cd5e82009-10-23 17:19:031494 NotifyNavigationStateChanged(INVALIDATE_TITLE);
[email protected]420ae012009-04-24 05:16:321495}
1496
[email protected]420ae012009-04-24 05:16:321497void TabContents::UpdateEncoding(RenderViewHost* render_view_host,
[email protected]41fc0322009-09-04 22:23:401498 const std::string& encoding) {
[email protected]420ae012009-04-24 05:16:321499 set_encoding(encoding);
1500}
1501
1502void TabContents::UpdateTargetURL(int32 page_id, const GURL& url) {
1503 if (delegate())
1504 delegate()->UpdateTargetURL(this, url);
1505}
1506
[email protected]420ae012009-04-24 05:16:321507void TabContents::Close(RenderViewHost* rvh) {
[email protected]07707302009-11-06 00:50:291508 // The UI may be in an event-tracking loop, such as between the
1509 // mouse-down and mouse-up in text selection or a button click.
1510 // Defer the close until after tracking is complete, so that we
1511 // don't free objects out from under the UI.
1512 // TODO(shess): This could probably be integrated with the
1513 // IsDoingDrag() test below. Punting for now because I need more
1514 // research to understand how this impacts platforms other than Mac.
1515 // TODO(shess): This could get more fine-grained. For instance,
1516 // closing a tab in another window while selecting text in the
1517 // current window's Omnibox should be just fine.
1518 if (view()->IsEventTracking()) {
1519 view()->CloseTabAfterEventTracking();
1520 return;
1521 }
1522
[email protected]24a4d1062009-07-10 23:10:421523 // If we close the tab while we're in the middle of a drag, we'll crash.
1524 // Instead, cancel the drag and close it as soon as the drag ends.
1525 if (view()->IsDoingDrag()) {
1526 view()->CancelDragAndCloseTab();
1527 return;
1528 }
1529
[email protected]420ae012009-04-24 05:16:321530 // Ignore this if it comes from a RenderViewHost that we aren't showing.
1531 if (delegate() && rvh == render_view_host())
1532 delegate()->CloseContents(this);
1533}
1534
1535void TabContents::RequestMove(const gfx::Rect& new_bounds) {
[email protected]b35b26b32011-05-05 20:35:141536 if (delegate() && delegate()->IsPopupOrPanel(this))
[email protected]420ae012009-04-24 05:16:321537 delegate()->MoveContents(this, new_bounds);
1538}
1539
[email protected]7ab1e7d62009-10-14 23:32:011540void TabContents::DidStartLoading() {
[email protected]420ae012009-04-24 05:16:321541 SetIsLoading(true, NULL);
[email protected]3c9e1872010-11-18 16:17:491542
[email protected]c40d6232011-03-25 00:16:211543 if (delegate() && content_restrictions_) {
1544 content_restrictions_ = 0;
[email protected]de4761b2010-12-07 21:05:211545 delegate()->ContentRestrictionsChanged(this);
[email protected]9e823662010-10-13 23:36:001546 }
[email protected]3c9e1872010-11-18 16:17:491547
1548 // Notify observers about navigation.
[email protected]585b30362011-01-28 02:30:171549 FOR_EACH_OBSERVER(TabContentsObserver, observers_, DidStartLoading());
[email protected]420ae012009-04-24 05:16:321550}
1551
[email protected]7ab1e7d62009-10-14 23:32:011552void TabContents::DidStopLoading() {
[email protected]420ae012009-04-24 05:16:321553 scoped_ptr<LoadNotificationDetails> details;
1554
1555 NavigationEntry* entry = controller_.GetActiveEntry();
1556 // An entry may not exist for a stop when loading an initial blank page or
1557 // if an iframe injected by script into a blank page finishes loading.
1558 if (entry) {
[email protected]420ae012009-04-24 05:16:321559 base::TimeDelta elapsed = base::TimeTicks::Now() - current_load_start_;
1560
1561 details.reset(new LoadNotificationDetails(
[email protected]ebe89e062009-08-13 23:16:541562 entry->virtual_url(),
[email protected]420ae012009-04-24 05:16:321563 entry->transition_type(),
1564 elapsed,
1565 &controller_,
1566 controller_.GetCurrentEntryIndex()));
1567 }
1568
[email protected]420ae012009-04-24 05:16:321569 SetIsLoading(false, details.get());
[email protected]3c9e1872010-11-18 16:17:491570
1571 // Notify observers about navigation.
[email protected]585b30362011-01-28 02:30:171572 FOR_EACH_OBSERVER(TabContentsObserver, observers_, DidStopLoading());
[email protected]420ae012009-04-24 05:16:321573}
1574
[email protected]c95fa8b2011-04-28 20:26:161575void TabContents::DidCancelLoading() {
1576 controller_.DiscardNonCommittedEntries();
1577
1578 // Update the URL display.
1579 NotifyNavigationStateChanged(TabContents::INVALIDATE_URL);
1580}
1581
[email protected]1a3c3cb2010-12-16 21:03:401582void TabContents::DidChangeLoadProgress(double progress) {
1583 if (delegate())
1584 delegate()->LoadProgressChanged(progress);
1585}
1586
[email protected]25497492010-09-11 15:15:081587void TabContents::DocumentOnLoadCompletedInMainFrame(
1588 RenderViewHost* render_view_host,
1589 int32 page_id) {
1590 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271591 content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME,
[email protected]25497492010-09-11 15:15:081592 Source<TabContents>(this),
1593 Details<int>(&page_id));
1594}
1595
[email protected]420ae012009-04-24 05:16:321596void TabContents::RequestOpenURL(const GURL& url, const GURL& referrer,
1597 WindowOpenDisposition disposition) {
[email protected]7ade2732011-02-10 00:13:581598 if (render_manager_.web_ui()) {
[email protected]e0112912011-02-02 22:54:351599 // When we're a Web UI, it will provide a page transition type for us (this
[email protected]420ae012009-04-24 05:16:321600 // is so the new tab page can specify AUTO_BOOKMARK for automatically
1601 // generated suggestions).
1602 //
[email protected]e0112912011-02-02 22:54:351603 // Note also that we hide the referrer for Web UI pages. We don't really
[email protected]60e448982009-05-06 04:21:161604 // want web sites to see a referrer of "chrome://blah" (and some
1605 // chrome: URLs might have search terms or other stuff we don't want to
[email protected]420ae012009-04-24 05:16:321606 // send to the site), so we send no referrer.
1607 OpenURL(url, GURL(), disposition,
[email protected]7ade2732011-02-10 00:13:581608 render_manager_.web_ui()->link_transition_type());
[email protected]420ae012009-04-24 05:16:321609 } else {
1610 OpenURL(url, referrer, disposition, PageTransition::LINK);
1611 }
1612}
1613
[email protected]420ae012009-04-24 05:16:321614void TabContents::RunJavaScriptMessage(
[email protected]992db4c2011-05-12 15:37:151615 const RenderViewHost* rvh,
[email protected]4f5ce842011-05-27 19:34:411616 const string16& message,
1617 const string16& default_prompt,
[email protected]420ae012009-04-24 05:16:321618 const GURL& frame_url,
1619 const int flags,
1620 IPC::Message* reply_msg,
1621 bool* did_suppress_message) {
[email protected]3ab9cb82011-06-03 18:02:071622 // Suppress JavaScript dialogs when requested. Also suppress messages when
1623 // showing an interstitial as it's shown over the previous page and we don't
1624 // want the hidden page's dialogs to interfere with the interstitial.
[email protected]8f55e802010-12-06 18:11:501625 bool suppress_this_message =
[email protected]992db4c2011-05-12 15:37:151626 rvh->is_swapped_out() ||
[email protected]8f55e802010-12-06 18:11:501627 showing_interstitial_page() ||
[email protected]3ab9cb82011-06-03 18:02:071628 !delegate_ ||
1629 delegate_->ShouldSuppressDialogs();
[email protected]420ae012009-04-24 05:16:321630
1631 if (!suppress_this_message) {
[email protected]b627d9a2011-06-28 14:06:341632 content::JavaScriptDialogCreator::TitleType title_type;
1633 string16 title;
1634
1635 if (!frame_url.has_host()) {
1636 title_type = content::JavaScriptDialogCreator::DIALOG_TITLE_NONE;
1637 } else {
1638 title_type = content::JavaScriptDialogCreator::DIALOG_TITLE_FORMATTED_URL;
1639 title = net::FormatUrl(
1640 frame_url.GetOrigin(),
1641 content::GetContentClient()->browser()->GetAcceptLangs(this));
1642 }
1643
[email protected]3ab9cb82011-06-03 18:02:071644 delegate_->GetJavaScriptDialogCreator()->RunJavaScriptDialog(
1645 this,
[email protected]b627d9a2011-06-28 14:06:341646 title_type,
1647 title,
[email protected]3ab9cb82011-06-03 18:02:071648 flags,
1649 message,
1650 default_prompt,
1651 reply_msg,
[email protected]b627d9a2011-06-28 14:06:341652 &suppress_this_message);
[email protected]420ae012009-04-24 05:16:321653 }
[email protected]3ab9cb82011-06-03 18:02:071654
1655 if (suppress_this_message) {
1656 // If we are suppressing messages, just reply as if the user immediately
1657 // pressed "Cancel".
1658 OnDialogClosed(reply_msg, false, string16());
1659 }
1660
1661 *did_suppress_message = suppress_this_message;
[email protected]420ae012009-04-24 05:16:321662}
1663
[email protected]992db4c2011-05-12 15:37:151664void TabContents::RunBeforeUnloadConfirm(const RenderViewHost* rvh,
[email protected]4f5ce842011-05-27 19:34:411665 const string16& message,
[email protected]420ae012009-04-24 05:16:321666 IPC::Message* reply_msg) {
[email protected]5aab5e22010-12-08 22:13:291667 if (delegate())
1668 delegate()->WillRunBeforeUnloadConfirm();
[email protected]3ab9cb82011-06-03 18:02:071669
1670 bool suppress_this_message =
1671 rvh->is_swapped_out() ||
1672 !delegate_ ||
1673 delegate_->ShouldSuppressDialogs();
[email protected]992db4c2011-05-12 15:37:151674 if (suppress_this_message) {
[email protected]4f5ce842011-05-27 19:34:411675 render_view_host()->JavaScriptDialogClosed(reply_msg, true, string16());
[email protected]8f55e802010-12-06 18:11:501676 return;
1677 }
[email protected]3ab9cb82011-06-03 18:02:071678
[email protected]7ab1e7d62009-10-14 23:32:011679 is_showing_before_unload_dialog_ = true;
[email protected]3ab9cb82011-06-03 18:02:071680 delegate_->GetJavaScriptDialogCreator()->RunBeforeUnloadDialog(
1681 this,
1682 message,
1683 reply_msg);
[email protected]420ae012009-04-24 05:16:321684}
1685
[email protected]420ae012009-04-24 05:16:321686WebPreferences TabContents::GetWebkitPrefs() {
[email protected]447021c2010-09-08 21:29:081687 WebPreferences web_prefs =
[email protected]181a95ee2011-07-12 19:26:361688 content::GetContentClient()->browser()->GetWebkitPrefs(
1689 render_view_host()->process()->profile(), false);
[email protected]447021c2010-09-08 21:29:081690
[email protected]b8299c12011-06-03 19:52:281691 // Force accelerated compositing and 2d canvas off for chrome:, about: and
1692 // chrome-devtools: pages.
[email protected]27c790d42010-10-22 09:28:431693 if (GetURL().SchemeIs(chrome::kChromeDevToolsScheme) ||
[email protected]b8299c12011-06-03 19:52:281694 GetURL().SchemeIs(chrome::kChromeUIScheme) ||
1695 GetURL().SchemeIs(chrome::kAboutScheme)) {
[email protected]447021c2010-09-08 21:29:081696 web_prefs.accelerated_compositing_enabled = false;
[email protected]9beff752010-09-22 19:35:431697 web_prefs.accelerated_2d_canvas_enabled = false;
[email protected]447021c2010-09-08 21:29:081698 }
1699
[email protected]39553442010-09-09 00:01:551700#if defined(OS_MACOSX)
1701 // Disable accelerated compositing if IOSurface's are not supported,
1702 // as is the case in 10.5.
1703 if (!IOSurfaceSupport::Initialize())
1704 web_prefs.accelerated_compositing_enabled = false;
1705#endif
1706
[email protected]447021c2010-09-08 21:29:081707 return web_prefs;
[email protected]420ae012009-04-24 05:16:321708}
1709
[email protected]7d472472011-01-22 01:30:251710void TabContents::OnUserGesture() {
[email protected]ddb85052011-05-18 14:40:271711 // Notify observers.
1712 FOR_EACH_OBSERVER(TabContentsObserver, observers_, DidGetUserGesture());
[email protected]686493142011-07-15 21:47:221713
1714 ResourceDispatcherHost* rdh =
1715 content::GetContentClient()->browser()->GetResourceDispatcherHost();
1716 if (rdh) // NULL in unittests.
1717 rdh->download_request_limiter()->OnUserGesture(this);
[email protected]7d472472011-01-22 01:30:251718}
1719
[email protected]fa1cf0b82010-01-15 21:49:441720void TabContents::OnIgnoredUIEvent() {
1721 if (constrained_window_count()) {
1722 ConstrainedWindow* window = *constrained_window_begin();
1723 window->FocusConstrainedWindow();
1724 }
1725}
1726
[email protected]420ae012009-04-24 05:16:321727void TabContents::RendererUnresponsive(RenderViewHost* rvh,
1728 bool is_during_unload) {
[email protected]992db4c2011-05-12 15:37:151729 // Don't show hung renderer dialog for a swapped out RVH.
1730 if (rvh != render_view_host())
1731 return;
1732
[email protected]420ae012009-04-24 05:16:321733 if (is_during_unload) {
1734 // Hang occurred while firing the beforeunload/unload handler.
1735 // Pretend the handler fired so tab closing continues as if it had.
[email protected]829e7612009-04-25 01:15:111736 rvh->set_sudden_termination_allowed(true);
[email protected]420ae012009-04-24 05:16:321737
1738 if (!render_manager_.ShouldCloseTabOnUnresponsiveRenderer())
1739 return;
1740
1741 // If the tab hangs in the beforeunload/unload handler there's really
1742 // nothing we can do to recover. Pretend the unload listeners have
1743 // all fired and close the tab. If the hang is in the beforeunload handler
1744 // then the user will not have the option of cancelling the close.
1745 Close(rvh);
1746 return;
1747 }
1748
[email protected]55452902011-06-01 21:57:471749 if (!render_view_host() || !render_view_host()->IsRenderViewLive())
1750 return;
1751
1752 if (delegate())
1753 delegate()->RendererUnresponsive(this);
[email protected]420ae012009-04-24 05:16:321754}
1755
1756void TabContents::RendererResponsive(RenderViewHost* render_view_host) {
[email protected]55452902011-06-01 21:57:471757 if (delegate())
1758 delegate()->RendererResponsive(this);
[email protected]420ae012009-04-24 05:16:321759}
1760
1761void TabContents::LoadStateChanged(const GURL& url,
[email protected]094e5b22009-09-25 04:23:561762 net::LoadState load_state,
1763 uint64 upload_position,
1764 uint64 upload_size) {
[email protected]420ae012009-04-24 05:16:321765 load_state_ = load_state;
[email protected]094e5b22009-09-25 04:23:561766 upload_position_ = upload_position;
1767 upload_size_ = upload_size;
[email protected]04866c42011-05-03 20:03:501768 load_state_host_ = net::IDNToUnicode(url.host(),
[email protected]b5cca982011-05-26 04:42:081769 content::GetContentClient()->browser()->GetAcceptLangs(this));
[email protected]420ae012009-04-24 05:16:321770 if (load_state_ == net::LOAD_STATE_READING_RESPONSE)
1771 SetNotWaitingForResponse();
[email protected]f5d978c2011-07-21 14:43:511772 if (IsLoading())
[email protected]c9cd2222009-05-06 05:16:501773 NotifyNavigationStateChanged(INVALIDATE_LOAD | INVALIDATE_TAB);
[email protected]420ae012009-04-24 05:16:321774}
1775
[email protected]7d472472011-01-22 01:30:251776void TabContents::WorkerCrashed() {
[email protected]0dd3a0ab2011-02-18 08:17:441777 if (delegate())
[email protected]55452902011-06-01 21:57:471778 delegate()->WorkerCrashed(this);
[email protected]7d472472011-01-22 01:30:251779}
1780
[email protected]420ae012009-04-24 05:16:321781void TabContents::BeforeUnloadFiredFromRenderManager(
1782 bool proceed,
1783 bool* proceed_to_fire_unload) {
1784 if (delegate())
1785 delegate()->BeforeUnloadFired(this, proceed, proceed_to_fire_unload);
1786}
1787
[email protected]3a3d47472010-07-15 21:03:541788void TabContents::DidStartLoadingFromRenderManager(
1789 RenderViewHost* render_view_host) {
1790 DidStartLoading();
1791}
1792
1793void TabContents::RenderViewGoneFromRenderManager(
1794 RenderViewHost* render_view_host) {
[email protected]443b80e2010-12-14 00:42:231795 DCHECK(crashed_status_ != base::TERMINATION_STATUS_STILL_RUNNING);
1796 RenderViewGone(render_view_host, crashed_status_, crashed_error_code_);
[email protected]3a3d47472010-07-15 21:03:541797}
1798
[email protected]420ae012009-04-24 05:16:321799void TabContents::UpdateRenderViewSizeForRenderManager() {
1800 // TODO(brettw) this is a hack. See TabContentsView::SizeContents.
[email protected]0323ee42010-02-17 22:03:261801 gfx::Size size = view_->GetContainerSize();
1802 // 0x0 isn't a valid window size (minimal window size is 1x1) but it may be
1803 // here during container initialization and normal window size will be set
1804 // later. In case of tab duplication this resizing to 0x0 prevents setting
1805 // normal size later so just ignore it.
1806 if (!size.IsEmpty())
1807 view_->SizeContents(size);
[email protected]420ae012009-04-24 05:16:321808}
1809
[email protected]3a3d47472010-07-15 21:03:541810void TabContents::NotifySwappedFromRenderManager() {
1811 NotifySwapped();
1812}
1813
1814NavigationController& TabContents::GetControllerForRenderManager() {
1815 return controller();
1816}
1817
[email protected]d0980792011-02-13 19:41:401818WebUI* TabContents::CreateWebUIForRenderManager(const GURL& url) {
[email protected]1fd1a502011-03-30 16:55:561819 return content::WebUIFactory::Get()->CreateWebUIForURL(this, url);
[email protected]420ae012009-04-24 05:16:321820}
1821
1822NavigationEntry*
1823TabContents::GetLastCommittedNavigationEntryForRenderManager() {
1824 return controller_.GetLastCommittedEntry();
1825}
1826
1827bool TabContents::CreateRenderViewForRenderManager(
1828 RenderViewHost* render_view_host) {
[email protected]420ae012009-04-24 05:16:321829 RenderWidgetHostView* rwh_view = view_->CreateViewForWidget(render_view_host);
[email protected]1a98a932009-11-17 00:12:521830
[email protected]420ae012009-04-24 05:16:321831 // Now that the RenderView has been created, we need to tell it its size.
1832 rwh_view->SetSize(view_->GetContainerSize());
1833
[email protected]a4127722011-04-27 23:13:521834 if (!render_view_host->CreateRenderView(string16()))
1835 return false;
1836
[email protected]f8233cc2011-05-31 20:24:501837#if defined(OS_LINUX)
1838 // Force a ViewMsg_Resize to be sent, needed to make plugins show up on
1839 // linux. See crbug.com/83941.
1840 if (RenderWidgetHost* render_widget_host = rwh_view->GetRenderWidgetHost())
1841 render_widget_host->WasResized();
1842#endif
1843
[email protected]420ae012009-04-24 05:16:321844 UpdateMaxPageIDIfNecessary(render_view_host->site_instance(),
1845 render_view_host);
1846 return true;
1847}
1848
[email protected]3ab9cb82011-06-03 18:02:071849void TabContents::OnDialogClosed(IPC::Message* reply_msg,
1850 bool success,
1851 const string16& user_input) {
[email protected]beb440c2009-11-06 04:08:541852 if (is_showing_before_unload_dialog_ && !success) {
1853 // If a beforeunload dialog is canceled, we need to stop the throbber from
1854 // spinning, since we forced it to start spinning in Navigate.
1855 DidStopLoading();
1856
1857 tab_close_start_time_ = base::TimeTicks();
1858 }
1859 is_showing_before_unload_dialog_ = false;
[email protected]4f5ce842011-05-27 19:34:411860 render_view_host()->JavaScriptDialogClosed(reply_msg,
1861 success,
[email protected]3ab9cb82011-06-03 18:02:071862 user_input);
[email protected]beb440c2009-11-06 04:08:541863}
1864
[email protected]3ab9cb82011-06-03 18:02:071865gfx::NativeWindow TabContents::GetDialogRootWindow() {
1866 return view_->GetTopLevelNativeWindow();
[email protected]beb440c2009-11-06 04:08:541867}
1868
[email protected]a1e97f02011-06-30 14:04:341869void TabContents::OnDialogShown() {
1870 Activate();
[email protected]3a3d47472010-07-15 21:03:541871}
1872
[email protected]41fc0322009-09-04 22:23:401873void TabContents::set_encoding(const std::string& encoding) {
[email protected]763ec4ca2011-04-29 15:48:121874 encoding_ = content::GetContentClient()->browser()->
1875 GetCanonicalEncodingNameByAliasName(encoding);
[email protected]aacd7cc2009-09-02 14:51:101876}
[email protected]f45d2a72010-03-08 23:28:351877
[email protected]33f74972010-12-08 16:40:361878void TabContents::SwapInRenderViewHost(RenderViewHost* rvh) {
1879 render_manager_.SwapInRenderViewHost(rvh);
1880}
1881
1882void TabContents::CreateViewAndSetSizeForRVH(RenderViewHost* rvh) {
1883 RenderWidgetHostView* rwh_view = view()->CreateViewForWidget(rvh);
1884 rwh_view->SetSize(view()->GetContainerSize());
1885}