blob: 73bbe5b56735dbfbc9cb38c51a76d118a904d56b [file] [log] [blame]
[email protected]61d68ef12011-01-13 14:02:561// Copyright (c) 2011 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
[email protected]0dd3a0ab2011-02-18 08:17:445#include "content/browser/tab_contents/tab_contents.h"
[email protected]39526562011-02-05 03:41:516
[email protected]b75b8292010-10-01 07:28:257#include <cmath>
8
[email protected]36fb2c7c2011-04-04 15:49:089#include "base/command_line.h"
[email protected]835d7c82010-10-14 04:38:3810#include "base/metrics/histogram.h"
[email protected]724159a2010-12-30 01:11:1811#include "base/metrics/stats_counters.h"
[email protected]96d185d2009-04-24 03:28:5412#include "base/string16.h"
[email protected]996fd702009-09-04 19:12:3713#include "base/string_util.h"
[email protected]96d185d2009-04-24 03:28:5414#include "base/time.h"
[email protected]3c733bde2010-12-21 19:56:3115#include "base/utf_string_conversions.h"
[email protected]3d7474ff2011-07-27 17:47:3716#include "content/browser/browser_context.h"
[email protected]567812d2011-02-24 17:40:5017#include "content/browser/child_process_security_policy.h"
[email protected]1fd1a502011-03-30 16:55:5618#include "content/browser/content_browser_client.h"
[email protected]b46442d7e2011-06-29 02:16:0619#include "content/browser/debugger/devtools_manager.h"
[email protected]71bf3f5e2011-08-15 21:05:2220#include "content/browser/download/download_manager.h"
[email protected]8bd9e562011-08-16 23:55:4621#include "content/browser/download/download_stats.h"
[email protected]567812d2011-02-24 17:40:5022#include "content/browser/host_zoom_map.h"
23#include "content/browser/in_process_webkit/session_storage_namespace.h"
[email protected]37a72af2011-06-13 05:42:0124#include "content/browser/load_from_memory_cache_details.h"
[email protected]35e251d2011-05-24 21:01:0425#include "content/browser/load_notification_details.h"
[email protected]567812d2011-02-24 17:40:5026#include "content/browser/renderer_host/render_process_host.h"
27#include "content/browser/renderer_host/render_view_host.h"
28#include "content/browser/renderer_host/render_widget_host_view.h"
[email protected]686493142011-07-15 21:47:2229#include "content/browser/renderer_host/resource_dispatcher_host.h"
[email protected]567812d2011-02-24 17:40:5030#include "content/browser/renderer_host/resource_request_details.h"
31#include "content/browser/site_instance.h"
[email protected]0dd3a0ab2011-02-18 08:17:4432#include "content/browser/tab_contents/interstitial_page.h"
[email protected]8286f51a2011-05-31 17:39:1333#include "content/browser/tab_contents/navigation_details.h"
[email protected]0dd3a0ab2011-02-18 08:17:4434#include "content/browser/tab_contents/navigation_entry.h"
35#include "content/browser/tab_contents/provisional_load_details.h"
36#include "content/browser/tab_contents/tab_contents_delegate.h"
37#include "content/browser/tab_contents/tab_contents_observer.h"
38#include "content/browser/tab_contents/tab_contents_view.h"
[email protected]105bb0f2011-05-24 17:12:1439#include "content/browser/tab_contents/title_updated_details.h"
[email protected]afd1e522011-04-27 23:29:5940#include "content/browser/user_metrics.h"
[email protected]1fd1a502011-03-30 16:55:5641#include "content/browser/webui/web_ui_factory.h"
[email protected]9966325b2011-04-18 05:00:1042#include "content/common/bindings_policy.h"
[email protected]1fd1a502011-03-30 16:55:5643#include "content/common/content_client.h"
[email protected]763ec4ca2011-04-29 15:48:1244#include "content/common/content_restriction.h"
[email protected]ce9751942011-09-21 01:57:2445#include "content/common/intents_messages.h"
[email protected]4dd57932011-03-17 06:06:1246#include "content/common/navigation_types.h"
[email protected]7f070d42011-03-09 20:25:3247#include "content/common/notification_service.h"
[email protected]0f012df82011-05-19 14:15:2948#include "content/common/url_constants.h"
[email protected]2c5569662011-03-22 20:45:0249#include "content/common/view_messages.h"
[email protected]d686e812009-06-03 19:10:2950#include "net/base/net_util.h"
[email protected]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]ce9751942011-09-21 01:57:2456#include "webkit/glue/web_intent_data.h"
[email protected]3c733bde2010-12-21 19:56:3157#include "webkit/glue/webpreferences.h"
58
59#if defined(OS_MACOSX)
[email protected]b9b751f22011-03-25 14:04:1260#include "ui/gfx/surface/io_surface_support_mac.h"
[email protected]3c733bde2010-12-21 19:56:3161#endif // defined(OS_MACOSX)
[email protected]3e45ba92009-02-20 21:09:0062
[email protected]420ae012009-04-24 05:16:3263// Cross-Site Navigations
64//
65// If a TabContents is told to navigate to a different web site (as determined
66// by SiteInstance), it will replace its current RenderViewHost with a new
67// RenderViewHost dedicated to the new SiteInstance. This works as follows:
68//
69// - Navigate determines whether the destination is cross-site, and if so,
[email protected]a2750082011-09-01 12:29:4670// it creates a pending_render_view_host_.
[email protected]420ae012009-04-24 05:16:3271// - 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
[email protected]a2750082011-09-01 12:29:4678// cancel all the pending logic. Otherwise we allow the pending RVH to send
79// the navigation request to its renderer.
80// - ResourceDispatcherHost receives a ResourceRequest on the IO thread for the
81// main resource load on the pending RVH. It checks CrossSiteRequestManager
82// to see that it is a cross-site request, and installs a
83// CrossSiteResourceHandler.
84// - When RDH receives a response, the BufferedResourceHandler determines
85// whether it is a download. If so, it sends a message to the new renderer
86// causing it to cancel the request, and the download proceeds. For now, the
87// pending RVH remains until the next DidNavigate event for this TabContents.
88// This isn't ideal, but it doesn't affect any functionality.
[email protected]420ae012009-04-24 05:16:3289// - 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
[email protected]a2750082011-09-01 12:29:4699// pending RVH.
[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]3d7474ff2011-07-27 17:47:37125 content::BrowserContext* browser_context, const NavigationEntry& entry,
[email protected]1ccb3568d2010-02-19 10:51:16126 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 &&
[email protected]3d7474ff2011-07-27 17:47:37137 browser_context->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]6c6b02d2011-09-02 03:36:47145 TabContentsDelegate* delegate,
[email protected]1ccb3568d2010-02-19 10:51:16146 NavigationController::ReloadType reload_type,
147 ViewMsg_Navigate_Params* params) {
[email protected]056de2d2009-06-26 16:41:34148 params->page_id = entry.page_id();
[email protected]876bc832010-09-07 16:29:54149 params->pending_history_list_offset = controller.GetIndexOfEntry(&entry);
[email protected]3cc72b12010-03-18 23:03:00150 params->current_history_list_offset = controller.last_committed_entry_index();
151 params->current_history_list_length = controller.entry_count();
[email protected]056de2d2009-06-26 16:41:34152 params->url = entry.url();
153 params->referrer = entry.referrer();
154 params->transition = entry.transition_type();
155 params->state = entry.content_state();
[email protected]3cc72b12010-03-18 23:03:00156 params->navigation_type =
[email protected]3d7474ff2011-07-27 17:47:37157 GetNavigationType(controller.browser_context(), entry, reload_type);
[email protected]056de2d2009-06-26 16:41:34158 params->request_time = base::Time::Now();
[email protected]6c6b02d2011-09-02 03:36:47159 params->extra_headers = entry.extra_headers();
160
161 if (delegate)
162 delegate->AddNavigationHeaders(params->url, &params->extra_headers);
[email protected]056de2d2009-06-26 16:41:34163}
164
[email protected]420ae012009-04-24 05:16:32165} // namespace
166
[email protected]f4f50ef2011-01-21 19:01:19167
168// TabContents ----------------------------------------------------------------
[email protected]420ae012009-04-24 05:16:32169
[email protected]3d7474ff2011-07-27 17:47:37170TabContents::TabContents(content::BrowserContext* browser_context,
[email protected]420ae012009-04-24 05:16:32171 SiteInstance* site_instance,
172 int routing_id,
[email protected]6ee12c42010-09-14 09:36:07173 const TabContents* base_tab_contents,
174 SessionStorageNamespace* session_storage_namespace)
[email protected]b680ad22009-04-15 23:19:42175 : delegate_(NULL),
[email protected]6ee12c42010-09-14 09:36:07176 ALLOW_THIS_IN_INITIALIZER_LIST(controller_(
[email protected]3d7474ff2011-07-27 17:47:37177 this, browser_context, session_storage_namespace)),
[email protected]66ba4932009-06-04 19:22:13178 ALLOW_THIS_IN_INITIALIZER_LIST(view_(
[email protected]74313b42011-08-24 16:51:32179 content::GetContentClient()->browser()->CreateTabContentsView(this))),
[email protected]d82ed61e2009-06-16 02:46:22180 ALLOW_THIS_IN_INITIALIZER_LIST(render_manager_(this, this)),
[email protected]d5f942ba2008-09-26 19:30:34181 is_loading_(false),
[email protected]443b80e2010-12-14 00:42:23182 crashed_status_(base::TERMINATION_STATUS_STILL_RUNNING),
183 crashed_error_code_(0),
[email protected]d5f942ba2008-09-26 19:30:34184 waiting_for_response_(false),
[email protected]d5f942ba2008-09-26 19:30:34185 max_page_id_(-1),
[email protected]9c235f042011-08-10 22:28:21186 load_state_(net::LOAD_STATE_IDLE, string16()),
[email protected]094e5b22009-09-25 04:23:56187 upload_size_(0),
188 upload_position_(0),
[email protected]f17a0ee2010-05-17 17:38:47189 displayed_insecure_content_(false),
[email protected]fdd61c62009-04-22 19:22:57190 capturing_contents_(false),
191 is_being_destroyed_(false),
192 notify_disconnection_(false),
[email protected]2e5b90c2011-08-16 21:11:55193 dialog_creator_(NULL),
[email protected]fdd61c62009-04-22 19:22:57194#if defined(OS_WIN)
195 message_box_active_(CreateEvent(NULL, TRUE, FALSE, NULL)),
196#endif
[email protected]7ab1e7d62009-10-14 23:32:01197 is_showing_before_unload_dialog_(false),
[email protected]1fd1a502011-03-30 16:55:56198 opener_web_ui_type_(WebUI::kNoWebUI),
[email protected]ebf40a72010-07-22 01:46:38199 closed_by_user_gesture_(false),
[email protected]b75b8292010-10-01 07:28:25200 minimum_zoom_percent_(
201 static_cast<int>(WebKit::WebView::minTextSizeMultiplier * 100)),
202 maximum_zoom_percent_(
203 static_cast<int>(WebKit::WebView::maxTextSizeMultiplier * 100)),
[email protected]9e823662010-10-13 23:36:00204 temporary_zoom_settings_(false),
[email protected]4850a7f2011-03-08 23:36:59205 content_restrictions_(0) {
[email protected]7ff431e2010-01-07 18:18:54206
[email protected]3d7474ff2011-07-27 17:47:37207 render_manager_.Init(browser_context, site_instance, routing_id);
[email protected]420ae012009-04-24 05:16:32208
[email protected]34ac70502009-09-25 17:07:23209 // We have the initial size of the view be based on the size of the passed in
210 // tab contents (normally a tab from the same window).
211 view_->CreateView(base_tab_contents ?
212 base_tab_contents->view()->GetContainerSize() : gfx::Size());
[email protected]332af7732009-03-11 13:21:35213}
initial.commit09911bf2008-07-26 23:55:29214
215TabContents::~TabContents() {
[email protected]420ae012009-04-24 05:16:32216 is_being_destroyed_ = true;
217
[email protected]3ab9cb82011-06-03 18:02:07218 // Clear out any JavaScript state.
[email protected]2e5b90c2011-08-16 21:11:55219 if (dialog_creator_)
220 dialog_creator_->ResetJavaScriptState(this);
[email protected]3ab9cb82011-06-03 18:02:07221
[email protected]420ae012009-04-24 05:16:32222 NotifyDisconnected();
[email protected]420ae012009-04-24 05:16:32223
[email protected]420ae012009-04-24 05:16:32224 // First cleanly close all child windows.
225 // TODO(mpcomplete): handle case if MaybeCloseChildWindows() already asked
226 // some of these to close. CloseWindows is async, so it might get called
227 // twice before it runs.
[email protected]8cc8d492010-02-02 10:40:49228 CloseConstrainedWindows();
[email protected]420ae012009-04-24 05:16:32229
[email protected]420ae012009-04-24 05:16:32230 // Notify any observer that have a reference on this tab contents.
231 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:27232 content::NOTIFICATION_TAB_CONTENTS_DESTROYED,
[email protected]420ae012009-04-24 05:16:32233 Source<TabContents>(this),
234 NotificationService::NoDetails());
235
236 // TODO(brettw) this should be moved to the view.
237#if defined(OS_WIN)
238 // If we still have a window handle, destroy it. GetNativeView can return
239 // NULL if this contents was part of a window that closed.
[email protected]50bd6452010-11-27 19:39:42240 if (GetNativeView()) {
241 RenderViewHost* host = render_view_host();
[email protected]b9a1fb42011-04-12 23:16:44242 if (host && host->view())
[email protected]50bd6452010-11-27 19:39:42243 host->view()->WillWmDestroy();
[email protected]50bd6452010-11-27 19:39:42244 }
[email protected]420ae012009-04-24 05:16:32245#endif
[email protected]7ab1e7d62009-10-14 23:32:01246
247 // OnCloseStarted isn't called in unit tests.
248 if (!tab_close_start_time_.is_null()) {
249 UMA_HISTOGRAM_TIMES("Tab.Close",
250 base::TimeTicks::Now() - tab_close_start_time_);
251 }
[email protected]b5a1d11c2011-02-17 03:09:42252
[email protected]07d490bc2011-03-07 17:05:26253 FOR_EACH_OBSERVER(TabContentsObserver, observers_, TabContentsDestroyed());
[email protected]232a5812011-03-04 22:42:08254
[email protected]1de2b8b2011-06-29 19:38:46255 set_delegate(NULL);
256}
257
258// TODO(cbentzel): Either remove the debugging code, or rename to SetDelegate.
259void TabContents::set_delegate(TabContentsDelegate* delegate) {
260 if (delegate == delegate_)
261 return;
262 if (delegate_)
263 delegate_->Detach(this);
264 delegate_ = delegate;
265 if (delegate_)
266 delegate_->Attach(this);
[email protected]b5a1d11c2011-02-17 03:09:42267}
268
[email protected]724159a2010-12-30 01:11:18269bool TabContents::OnMessageReceived(const IPC::Message& message) {
[email protected]f82d57b52011-04-27 19:13:17270 if (web_ui() && web_ui()->OnMessageReceived(message))
271 return true;
272
[email protected]585b30362011-01-28 02:30:17273 ObserverListBase<TabContentsObserver>::Iterator it(observers_);
274 TabContentsObserver* observer;
[email protected]0f180ee2011-01-21 18:23:10275 while ((observer = it.GetNext()) != NULL)
276 if (observer->OnMessageReceived(message))
[email protected]403415a2011-01-10 18:57:53277 return true;
[email protected]403415a2011-01-10 18:57:53278
[email protected]724159a2010-12-30 01:11:18279 bool handled = true;
280 bool message_is_ok = true;
281 IPC_BEGIN_MESSAGE_MAP_EX(TabContents, message, message_is_ok)
[email protected]ce9751942011-09-21 01:57:24282 IPC_MESSAGE_HANDLER(IntentsHostMsg_WebIntentDispatch,
283 OnWebIntentDispatch)
[email protected]724159a2010-12-30 01:11:18284 IPC_MESSAGE_HANDLER(ViewHostMsg_DidStartProvisionalLoadForFrame,
285 OnDidStartProvisionalLoadForFrame)
286 IPC_MESSAGE_HANDLER(ViewHostMsg_DidRedirectProvisionalLoad,
287 OnDidRedirectProvisionalLoad)
288 IPC_MESSAGE_HANDLER(ViewHostMsg_DidFailProvisionalLoadWithError,
289 OnDidFailProvisionalLoadWithError)
290 IPC_MESSAGE_HANDLER(ViewHostMsg_DidLoadResourceFromMemoryCache,
291 OnDidLoadResourceFromMemoryCache)
292 IPC_MESSAGE_HANDLER(ViewHostMsg_DidDisplayInsecureContent,
293 OnDidDisplayInsecureContent)
294 IPC_MESSAGE_HANDLER(ViewHostMsg_DidRunInsecureContent,
295 OnDidRunInsecureContent)
296 IPC_MESSAGE_HANDLER(ViewHostMsg_DocumentLoadedInFrame,
297 OnDocumentLoadedInFrame)
298 IPC_MESSAGE_HANDLER(ViewHostMsg_DidFinishLoad, OnDidFinishLoad)
[email protected]c8f73ab2011-01-22 00:05:17299 IPC_MESSAGE_HANDLER(ViewHostMsg_UpdateContentRestrictions,
300 OnUpdateContentRestrictions)
[email protected]7d472472011-01-22 01:30:25301 IPC_MESSAGE_HANDLER(ViewHostMsg_GoToEntryAtOffset, OnGoToEntryAtOffset)
[email protected]216813952011-05-19 22:21:26302 IPC_MESSAGE_HANDLER(ViewHostMsg_UpdateZoomLimits, OnUpdateZoomLimits)
303 IPC_MESSAGE_HANDLER(ViewHostMsg_FocusedNodeChanged, OnFocusedNodeChanged)
[email protected]c7dd2f62011-07-18 15:57:59304 IPC_MESSAGE_HANDLER(ViewHostMsg_SaveURLAs, OnSaveURL)
[email protected]3a29a6e2011-08-24 18:26:21305 IPC_MESSAGE_HANDLER(ViewHostMsg_EnumerateDirectory, OnEnumerateDirectory)
[email protected]7d189022011-08-25 22:54:20306 IPC_MESSAGE_HANDLER(ViewHostMsg_JSOutOfMemory, OnJSOutOfMemory)
307 IPC_MESSAGE_HANDLER(ViewHostMsg_RegisterProtocolHandler,
308 OnRegisterProtocolHandler)
309 IPC_MESSAGE_HANDLER(ViewHostMsg_RegisterIntentHandler,
310 OnRegisterIntentHandler)
[email protected]b888919c2011-09-02 00:32:16311 IPC_MESSAGE_HANDLER(ViewHostMsg_Find_Reply, OnFindReply)
[email protected]d952a052011-09-06 18:42:45312 IPC_MESSAGE_HANDLER(ViewHostMsg_CrashedPlugin, OnCrashedPlugin)
[email protected]7fc4bbb2011-09-08 21:23:10313 IPC_MESSAGE_HANDLER(ViewHostMsg_AppCacheAccessed, OnAppCacheAccessed)
[email protected]724159a2010-12-30 01:11:18314 IPC_MESSAGE_UNHANDLED(handled = false)
315 IPC_END_MESSAGE_MAP_EX()
316
317 if (!message_is_ok) {
318 UserMetrics::RecordAction(UserMetricsAction("BadMessageTerminate_RVD"));
319 GetRenderProcessHost()->ReceivedBadMessage();
320 }
321
322 return handled;
323}
324
[email protected]6c2e472f2011-08-24 23:26:18325void TabContents::RunFileChooser(
326 RenderViewHost* render_view_host,
327 const ViewHostMsg_RunFileChooser_Params& params) {
328 delegate()->RunFileChooser(this, params);
329}
330
[email protected]8cb5d5b2010-02-09 11:36:16331RenderProcessHost* TabContents::GetRenderProcessHost() const {
332 return render_manager_.current_host()->process();
333}
334
[email protected]d5f942ba2008-09-26 19:30:34335const GURL& TabContents::GetURL() const {
336 // We may not have a navigation entry yet
[email protected]ce3fa3c2009-04-20 19:55:57337 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]ebe89e062009-08-13 23:16:54338 return entry ? entry->virtual_url() : GURL::EmptyGURL();
[email protected]d5f942ba2008-09-26 19:30:34339}
340
[email protected]96d185d2009-04-24 03:28:54341const string16& TabContents::GetTitle() const {
[email protected]4c6092c5b2009-06-06 00:23:55342 // Transient entries take precedence. They are used for interstitial pages
343 // that are shown on top of existing pages.
344 NavigationEntry* entry = controller_.GetTransientEntry();
[email protected]b5cca982011-05-26 04:42:08345 std::string accept_languages =
346 content::GetContentClient()->browser()->GetAcceptLangs(this);
[email protected]45d0ef7f2011-01-05 13:46:23347 if (entry) {
[email protected]b5cca982011-05-26 04:42:08348 return entry->GetTitleForDisplay(accept_languages);
[email protected]45d0ef7f2011-01-05 13:46:23349 }
[email protected]7ade2732011-02-10 00:13:58350 WebUI* our_web_ui = render_manager_.pending_web_ui() ?
351 render_manager_.pending_web_ui() : render_manager_.web_ui();
352 if (our_web_ui) {
[email protected]96d185d2009-04-24 03:28:54353 // Don't override the title in view source mode.
[email protected]4c6092c5b2009-06-06 00:23:55354 entry = controller_.GetActiveEntry();
[email protected]96d185d2009-04-24 03:28:54355 if (!(entry && entry->IsViewSourceMode())) {
[email protected]e0112912011-02-02 22:54:35356 // Give the Web UI the chance to override our title.
[email protected]7ade2732011-02-10 00:13:58357 const string16& title = our_web_ui->overridden_title();
[email protected]96d185d2009-04-24 03:28:54358 if (!title.empty())
359 return title;
360 }
361 }
362
363 // We use the title for the last committed entry rather than a pending
364 // navigation entry. For example, when the user types in a URL, we want to
365 // keep the old page's title until the new load has committed and we get a new
366 // title.
[email protected]96d185d2009-04-24 03:28:54367 entry = controller_.GetLastCommittedEntry();
[email protected]45d0ef7f2011-01-05 13:46:23368 if (entry) {
[email protected]b5cca982011-05-26 04:42:08369 return entry->GetTitleForDisplay(accept_languages);
[email protected]45d0ef7f2011-01-05 13:46:23370 }
[email protected]987fc3a2011-05-26 14:18:09371
372 // |page_title_when_no_navigation_entry_| is finally used
373 // if no title cannot be retrieved.
374 return page_title_when_no_navigation_entry_;
[email protected]96d185d2009-04-24 03:28:54375}
376
[email protected]d5f942ba2008-09-26 19:30:34377int32 TabContents::GetMaxPageID() {
378 if (GetSiteInstance())
379 return GetSiteInstance()->max_page_id();
380 else
381 return max_page_id_;
382}
383
384void TabContents::UpdateMaxPageID(int32 page_id) {
385 // Ensure both the SiteInstance and RenderProcessHost update their max page
[email protected]57c6a652009-05-04 07:58:34386 // IDs in sync. Only TabContents will also have site instances, except during
[email protected]d5f942ba2008-09-26 19:30:34387 // testing.
388 if (GetSiteInstance())
389 GetSiteInstance()->UpdateMaxPageID(page_id);
[email protected]8cb5d5b2010-02-09 11:36:16390 GetRenderProcessHost()->UpdateMaxPageID(page_id);
[email protected]d5f942ba2008-09-26 19:30:34391}
392
[email protected]96d185d2009-04-24 03:28:54393SiteInstance* TabContents::GetSiteInstance() const {
394 return render_manager_.current_host()->site_instance();
395}
396
[email protected]77362eb2011-08-01 17:18:38397SiteInstance* TabContents::GetPendingSiteInstance() const {
398 RenderViewHost* dest_rvh = render_manager_.pending_render_view_host() ?
399 render_manager_.pending_render_view_host() :
400 render_manager_.current_host();
401 return dest_rvh->site_instance();
402}
403
[email protected]96d185d2009-04-24 03:28:54404bool TabContents::ShouldDisplayURL() {
[email protected]8ab24cc2009-11-13 20:56:15405 // Don't hide the url in view source mode and with interstitials.
[email protected]96d185d2009-04-24 03:28:54406 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]8ab24cc2009-11-13 20:56:15407 if (entry && (entry->IsViewSourceMode() ||
[email protected]cccd3762010-11-12 18:40:01408 entry->page_type() == INTERSTITIAL_PAGE)) {
[email protected]96d185d2009-04-24 03:28:54409 return true;
[email protected]8ab24cc2009-11-13 20:56:15410 }
[email protected]83e3895b2009-06-11 00:07:16411
[email protected]2b9a06402011-02-16 04:57:10412 // We always display the URL for non-WebUI URLs to prevent spoofing.
[email protected]1fd1a502011-03-30 16:55:56413 if (entry && !content::WebUIFactory::Get()->HasWebUIScheme(entry->url()))
[email protected]2b9a06402011-02-16 04:57:10414 return true;
415
[email protected]d0980792011-02-13 19:41:40416 WebUI* web_ui = GetWebUIForCurrentState();
[email protected]7ade2732011-02-10 00:13:58417 if (web_ui)
418 return !web_ui->should_hide_url();
[email protected]96d185d2009-04-24 03:28:54419 return true;
420}
421
[email protected]f5d978c2011-07-21 14:43:51422bool TabContents::IsLoading() const {
423 return is_loading_ || (web_ui() && web_ui()->IsLoading());
424}
425
[email protected]585b30362011-01-28 02:30:17426void TabContents::AddObserver(TabContentsObserver* observer) {
427 observers_.AddObserver(observer);
[email protected]3c9e1872010-11-18 16:17:49428}
429
[email protected]585b30362011-01-28 02:30:17430void TabContents::RemoveObserver(TabContentsObserver* observer) {
431 observers_.RemoveObserver(observer);
[email protected]3c9e1872010-11-18 16:17:49432}
433
[email protected]443b80e2010-12-14 00:42:23434void TabContents::SetIsCrashed(base::TerminationStatus status, int error_code) {
435 if (status == crashed_status_)
[email protected]d5f942ba2008-09-26 19:30:34436 return;
437
[email protected]443b80e2010-12-14 00:42:23438 crashed_status_ = status;
439 crashed_error_code_ = error_code;
[email protected]c9cd2222009-05-06 05:16:50440 NotifyNavigationStateChanged(INVALIDATE_TAB);
[email protected]d5f942ba2008-09-26 19:30:34441}
442
443void TabContents::NotifyNavigationStateChanged(unsigned changed_flags) {
444 if (delegate_)
445 delegate_->NavigationStateChanged(this, changed_flags);
446}
447
[email protected]96d185d2009-04-24 03:28:54448void TabContents::DidBecomeSelected() {
449 controller_.SetActive(true);
[email protected]8cb5d5b2010-02-09 11:36:16450 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
451 if (rwhv) {
452 rwhv->DidBecomeSelected();
[email protected]789e9152009-08-04 21:59:43453#if defined(OS_MACOSX)
[email protected]8cb5d5b2010-02-09 11:36:16454 rwhv->SetActive(true);
[email protected]789e9152009-08-04 21:59:43455#endif
456 }
[email protected]96d185d2009-04-24 03:28:54457
[email protected]5ac20162010-11-24 23:33:11458 last_selected_time_ = base::TimeTicks::Now();
[email protected]3e69bc82011-05-26 23:22:38459
460 FOR_EACH_OBSERVER(TabContentsObserver, observers_, DidBecomeSelected());
[email protected]96d185d2009-04-24 03:28:54461}
462
463void TabContents::WasHidden() {
464 if (!capturing_contents()) {
465 // |render_view_host()| can be NULL if the user middle clicks a link to open
466 // a tab in then background, then closes the tab before selecting it. This
467 // is because closing the tab calls TabContents::Destroy(), which removes
468 // the |render_view_host()|; then when we actually destroy the window,
469 // OnWindowPosChanged() notices and calls HideContents() (which calls us).
[email protected]8cb5d5b2010-02-09 11:36:16470 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
471 if (rwhv)
472 rwhv->WasHidden();
[email protected]96d185d2009-04-24 03:28:54473 }
474
475 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:27476 content::NOTIFICATION_TAB_CONTENTS_HIDDEN,
[email protected]96d185d2009-04-24 03:28:54477 Source<TabContents>(this),
478 NotificationService::NoDetails());
479}
480
[email protected]d5f942ba2008-09-26 19:30:34481void TabContents::Activate() {
482 if (delegate_)
483 delegate_->ActivateContents(this);
484}
485
[email protected]ea42e7782010-08-23 23:58:12486void TabContents::Deactivate() {
487 if (delegate_)
488 delegate_->DeactivateContents(this);
489}
490
[email protected]63954792011-07-11 04:17:48491void TabContents::LostCapture() {
492 if (delegate_)
493 delegate_->LostCapture();
494}
495
496bool TabContents::PreHandleKeyboardEvent(const NativeWebKeyboardEvent& event,
497 bool* is_keyboard_shortcut) {
498 return delegate_ &&
499 delegate_->PreHandleKeyboardEvent(event, is_keyboard_shortcut);
500}
501
502void TabContents::HandleKeyboardEvent(const NativeWebKeyboardEvent& event) {
503 if (delegate_)
504 delegate_->HandleKeyboardEvent(event);
505}
506
507void TabContents::HandleMouseUp() {
508 if (delegate_)
509 delegate_->HandleMouseUp();
510}
511
512void TabContents::HandleMouseActivate() {
513 if (delegate_)
514 delegate_->HandleMouseActivate();
515}
516
[email protected]8a5e0ca2011-08-25 06:30:47517void TabContents::ToggleFullscreenMode(bool enter_fullscreen) {
518 if (delegate_)
519 delegate_->ToggleFullscreenModeForTab(this, enter_fullscreen);
520}
521
[email protected]0548c5352011-09-07 00:33:33522void TabContents::UpdatePreferredSize(const gfx::Size& pref_size) {
523 if (delegate_)
524 delegate_->UpdatePreferredSize(this, pref_size);
525}
526
[email protected]96d185d2009-04-24 03:28:54527void TabContents::ShowContents() {
[email protected]8cb5d5b2010-02-09 11:36:16528 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
529 if (rwhv)
530 rwhv->DidBecomeSelected();
[email protected]96d185d2009-04-24 03:28:54531}
532
533void TabContents::HideContents() {
534 // TODO(pkasting): http://b/1239839 Right now we purposefully don't call
535 // our superclass HideContents(), because some callers want to be very picky
536 // about the order in which these get called. In addition to making the code
537 // here practically impossible to understand, this also means we end up
538 // calling TabContents::WasHidden() twice if callers call both versions of
[email protected]57c6a652009-05-04 07:58:34539 // HideContents() on a TabContents.
[email protected]96d185d2009-04-24 03:28:54540 WasHidden();
541}
542
[email protected]5aab5e22010-12-08 22:13:29543bool TabContents::NeedToFireBeforeUnload() {
544 // TODO(creis): Should we fire even for interstitial pages?
545 return notify_disconnection() &&
546 !showing_interstitial_page() &&
547 !render_view_host()->SuddenTerminationAllowed();
548}
549
[email protected]00c37fc2011-08-02 00:22:50550// TODO(adriansc): Remove this method once refactoring changed all call sites.
[email protected]992e4542011-07-20 23:09:25551TabContents* TabContents::OpenURL(const GURL& url,
552 const GURL& referrer,
553 WindowOpenDisposition disposition,
554 PageTransition::Type transition) {
[email protected]00c37fc2011-08-02 00:22:50555 return OpenURL(OpenURLParams(url, referrer, disposition, transition));
556}
557
558TabContents* TabContents::OpenURL(const OpenURLParams& params) {
[email protected]1c642b52011-04-15 09:05:49559 if (delegate_) {
[email protected]00c37fc2011-08-02 00:22:50560 TabContents* new_contents = delegate_->OpenURLFromTab(this, params);
[email protected]1c642b52011-04-15 09:05:49561 // Notify observers.
562 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
[email protected]00c37fc2011-08-02 00:22:50563 DidOpenURL(params.url, params.referrer,
564 params.disposition, params.transition));
[email protected]992e4542011-07-20 23:09:25565 return new_contents;
[email protected]1c642b52011-04-15 09:05:49566 }
[email protected]992e4542011-07-20 23:09:25567 return NULL;
[email protected]d5f942ba2008-09-26 19:30:34568}
569
[email protected]1ccb3568d2010-02-19 10:51:16570bool TabContents::NavigateToPendingEntry(
571 NavigationController::ReloadType reload_type) {
[email protected]876bc832010-09-07 16:29:54572 return NavigateToEntry(*controller_.pending_entry(), reload_type);
573}
[email protected]96d185d2009-04-24 03:28:54574
[email protected]876bc832010-09-07 16:29:54575bool TabContents::NavigateToEntry(
576 const NavigationEntry& entry,
577 NavigationController::ReloadType reload_type) {
[email protected]96d185d2009-04-24 03:28:54578 RenderViewHost* dest_render_view_host = render_manager_.Navigate(entry);
579 if (!dest_render_view_host)
580 return false; // Unable to create the desired render view host.
581
[email protected]80a8fad2011-01-29 04:02:38582 // For security, we should never send non-Web-UI URLs to a Web UI renderer.
[email protected]05fdd492010-11-15 17:52:07583 // Double check that here.
[email protected]54ec6472010-04-09 19:39:58584 int enabled_bindings = dest_render_view_host->enabled_bindings();
[email protected]1fd1a502011-03-30 16:55:56585 bool is_allowed_in_web_ui_renderer = content::GetContentClient()->
[email protected]3d7474ff2011-07-27 17:47:37586 browser()->GetWebUIFactory()->IsURLAcceptableForWebUI(browser_context(),
[email protected]1fd1a502011-03-30 16:55:56587 entry.url());
[email protected]c09163a2011-02-15 00:05:55588 CHECK(!BindingsPolicy::is_web_ui_enabled(enabled_bindings) ||
589 is_allowed_in_web_ui_renderer);
[email protected]54ec6472010-04-09 19:39:58590
[email protected]96d185d2009-04-24 03:28:54591 // Tell DevTools agent that it is attached prior to the navigation.
[email protected]d9f9b792009-06-24 13:17:12592 DevToolsManager* devtools_manager = DevToolsManager::GetInstance();
[email protected]7e8e6b62009-05-08 11:28:32593 if (devtools_manager) { // NULL in unit tests.
[email protected]3cc72b12010-03-18 23:03:00594 devtools_manager->OnNavigatingToPendingEntry(render_view_host(),
595 dest_render_view_host,
596 entry.url());
[email protected]7e8e6b62009-05-08 11:28:32597 }
[email protected]96d185d2009-04-24 03:28:54598
599 // Used for page load time metrics.
600 current_load_start_ = base::TimeTicks::Now();
601
602 // Navigate in the desired RenderViewHost.
[email protected]056de2d2009-06-26 16:41:34603 ViewMsg_Navigate_Params navigate_params;
[email protected]6c6b02d2011-09-02 03:36:47604 MakeNavigateParams(entry, controller_, delegate_, reload_type,
605 &navigate_params);
[email protected]056de2d2009-06-26 16:41:34606 dest_render_view_host->Navigate(navigate_params);
[email protected]96d185d2009-04-24 03:28:54607
608 if (entry.page_id() == -1) {
609 // HACK!! This code suppresses javascript: URLs from being added to
610 // session history, which is what we want to do for javascript: URLs that
611 // do not generate content. What we really need is a message from the
612 // renderer telling us that a new page was not created. The same message
613 // could be used for mailto: URLs and the like.
614 if (entry.url().SchemeIs(chrome::kJavaScriptScheme))
615 return false;
616 }
617
[email protected]3c9e1872010-11-18 16:17:49618 // Notify observers about navigation.
[email protected]b375c5d2011-05-03 21:15:04619 FOR_EACH_OBSERVER(TabContentsObserver,
620 observers_,
621 NavigateToPendingEntry(entry.url(), reload_type));
[email protected]96d185d2009-04-24 03:28:54622
[email protected]09b29342011-06-24 19:18:48623 if (delegate_)
624 delegate_->DidNavigateToPendingEntry(this);
625
[email protected]d5f942ba2008-09-26 19:30:34626 return true;
627}
628
[email protected]9e1ad4b2011-08-14 16:49:19629void TabContents::SetHistoryLengthAndPrune(const SiteInstance* site_instance,
630 int history_length,
631 int32 minimum_page_id) {
[email protected]5229253b2011-08-16 14:36:41632 // SetHistoryLengthAndPrune doesn't work when there are pending cross-site
633 // navigations. Callers should ensure that this is the case.
[email protected]9e1ad4b2011-08-14 16:49:19634 if (render_manager_.pending_render_view_host()) {
635 NOTREACHED();
[email protected]796931a92011-08-10 01:32:14636 return;
[email protected]9e1ad4b2011-08-14 16:49:19637 }
638 RenderViewHost* rvh = render_view_host();
639 if (!rvh) {
640 NOTREACHED();
641 return;
642 }
643 if (site_instance && rvh->site_instance() != site_instance) {
644 NOTREACHED();
645 return;
646 }
647 rvh->Send(new ViewMsg_SetHistoryLengthAndPrune(rvh->routing_id(),
648 history_length,
649 minimum_page_id));
[email protected]796931a92011-08-10 01:32:14650}
651
[email protected]96d185d2009-04-24 03:28:54652void TabContents::Stop() {
653 render_manager_.Stop();
[email protected]2e3f4572011-03-25 19:24:47654 FOR_EACH_OBSERVER(TabContentsObserver, observers_, StopNavigation());
[email protected]96d185d2009-04-24 03:28:54655}
656
[email protected]96d185d2009-04-24 03:28:54657TabContents* TabContents::Clone() {
658 // We create a new SiteInstance so that the new tab won't share processes
659 // with the old one. This can be changed in the future if we need it to share
660 // processes for some reason.
[email protected]3d7474ff2011-07-27 17:47:37661 TabContents* tc = new TabContents(
662 browser_context(),
663 SiteInstance::CreateSiteInstance(browser_context()),
664 MSG_ROUTING_NONE, this, NULL);
[email protected]96d185d2009-04-24 03:28:54665 tc->controller().CopyStateFrom(controller_);
666 return tc;
667}
668
[email protected]4d677202009-07-19 07:37:12669void TabContents::ShowPageInfo(const GURL& url,
670 const NavigationEntry::SSLStatus& ssl,
671 bool show_history) {
672 if (!delegate_)
673 return;
674
[email protected]3d7474ff2011-07-27 17:47:37675 delegate_->ShowPageInfo(browser_context(), url, ssl, show_history);
[email protected]4d677202009-07-19 07:37:12676}
677
[email protected]aed59602011-02-28 22:57:33678void TabContents::AddConstrainedDialog(ConstrainedWindow* window) {
initial.commit09911bf2008-07-26 23:55:29679 child_windows_.push_back(window);
[email protected]fa1cf0b82010-01-15 21:49:44680
681 if (child_windows_.size() == 1) {
682 window->ShowConstrainedWindow();
683 BlockTabContent(true);
684 }
initial.commit09911bf2008-07-26 23:55:29685}
686
[email protected]fa1cf0b82010-01-15 21:49:44687void TabContents::BlockTabContent(bool blocked) {
[email protected]8dccd7cb2010-02-25 05:19:55688 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
[email protected]1b072562010-12-13 19:37:45689 // 70% opaque grey.
690 SkColor greyish = SkColorSetARGB(178, 0, 0, 0);
[email protected]8dccd7cb2010-02-25 05:19:55691 if (rwhv)
[email protected]1b072562010-12-13 19:37:45692 rwhv->SetVisuallyDeemphasized(blocked ? &greyish : NULL, false);
[email protected]a7eccac2011-04-15 03:05:19693 // RenderViewHost may be NULL during shutdown.
694 if (render_view_host())
695 render_view_host()->set_ignore_input_events(blocked);
[email protected]fa1cf0b82010-01-15 21:49:44696 if (delegate_)
697 delegate_->SetTabContentBlocked(this, blocked);
698}
699
[email protected]e7cfdbd2011-04-22 14:41:37700void TabContents::AddNewContents(TabContents* new_contents,
701 WindowOpenDisposition disposition,
702 const gfx::Rect& initial_pos,
703 bool user_gesture) {
initial.commit09911bf2008-07-26 23:55:29704 if (!delegate_)
705 return;
706
[email protected]e7cfdbd2011-04-22 14:41:37707 delegate_->AddNewContents(this, new_contents, disposition, initial_pos,
708 user_gesture);
[email protected]96d185d2009-04-24 03:28:54709}
710
[email protected]5c9e97a2009-09-09 23:48:30711gfx::NativeView TabContents::GetContentNativeView() const {
[email protected]96d185d2009-04-24 03:28:54712 return view_->GetContentNativeView();
713}
714
715gfx::NativeView TabContents::GetNativeView() const {
716 return view_->GetNativeView();
717}
718
719void TabContents::GetContainerBounds(gfx::Rect *out) const {
720 view_->GetContainerBounds(out);
721}
722
723void TabContents::Focus() {
724 view_->Focus();
725}
726
[email protected]90daadb42009-06-08 21:27:28727void TabContents::FocusThroughTabTraversal(bool reverse) {
[email protected]7e383692009-06-12 19:14:54728 if (showing_interstitial_page()) {
729 render_manager_.interstitial_page()->FocusThroughTabTraversal(reverse);
730 return;
731 }
[email protected]96d185d2009-04-24 03:28:54732 render_view_host()->SetInitialFocus(reverse);
733}
734
735bool TabContents::FocusLocationBarByDefault() {
[email protected]d0980792011-02-13 19:41:40736 WebUI* web_ui = GetWebUIForCurrentState();
[email protected]7ade2732011-02-10 00:13:58737 if (web_ui)
738 return web_ui->focus_location_bar_by_default();
[email protected]3e3f0eb2009-06-22 18:33:43739 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]e0d481582009-09-15 21:06:25740 if (entry && entry->url() == GURL(chrome::kAboutBlankURL))
[email protected]3e3f0eb2009-06-22 18:33:43741 return true;
[email protected]96d185d2009-04-24 03:28:54742 return false;
743}
744
[email protected]a26dc362010-04-23 01:48:58745void TabContents::SetFocusToLocationBar(bool select_all) {
[email protected]a11aaf22010-03-30 00:03:38746 if (delegate())
[email protected]a26dc362010-04-23 01:48:58747 delegate()->SetFocusToLocationBar(select_all);
[email protected]a11aaf22010-03-30 00:03:38748}
749
[email protected]686493142011-07-15 21:47:22750bool TabContents::CanDownload(int request_id) {
751 TabContentsDelegate* d = delegate();
752 if (d)
753 return d->CanDownload(this, request_id);
754 return true;
755}
756
757void TabContents::OnStartDownload(DownloadItem* download) {
758 TabContentsDelegate* d = delegate();
759 if (d)
760 d->OnStartDownload(this, download);
761}
762
[email protected]d5f942ba2008-09-26 19:30:34763void TabContents::WillClose(ConstrainedWindow* window) {
[email protected]d6219912010-11-17 19:47:37764 ConstrainedWindowList::iterator i(
765 std::find(child_windows_.begin(), child_windows_.end(), window));
766 bool removed_topmost_window = i == child_windows_.begin();
767 if (i != child_windows_.end())
768 child_windows_.erase(i);
769 if (child_windows_.empty()) {
[email protected]fa1cf0b82010-01-15 21:49:44770 BlockTabContent(false);
[email protected]d6219912010-11-17 19:47:37771 } else {
772 if (removed_topmost_window)
773 child_windows_[0]->ShowConstrainedWindow();
774 BlockTabContent(true);
[email protected]fa1cf0b82010-01-15 21:49:44775 }
[email protected]66ba4932009-06-04 19:22:13776}
[email protected]d5f942ba2008-09-26 19:30:34777
[email protected]c7dd2f62011-07-18 15:57:59778void TabContents::OnSavePage() {
779 // If we can not save the page, try to download it.
780 if (!SavePackage::IsSavableContents(contents_mime_type())) {
[email protected]3d7474ff2011-07-27 17:47:37781 DownloadManager* dlm = browser_context()->GetDownloadManager();
[email protected]c7dd2f62011-07-18 15:57:59782 const GURL& current_page_url = GetURL();
783 if (dlm && current_page_url.is_valid()) {
784 dlm->DownloadUrl(current_page_url, GURL(), "", this);
[email protected]8bd9e562011-08-16 23:55:46785 download_stats::RecordDownloadCount(
786 download_stats::INITIATED_BY_SAVE_PACKAGE_FAILURE_COUNT);
[email protected]c7dd2f62011-07-18 15:57:59787 return;
788 }
789 }
790
791 Stop();
792
793 // Create the save package and possibly prompt the user for the name to save
794 // the page as. The user prompt is an asynchronous operation that runs on
795 // another thread.
796 save_package_ = new SavePackage(this);
797 save_package_->GetSaveInfo();
798}
799
800// Used in automated testing to bypass prompting the user for file names.
801// Instead, the names and paths are hard coded rather than running them through
802// file name sanitation and extension / mime checking.
803bool TabContents::SavePage(const FilePath& main_file, const FilePath& dir_path,
804 SavePackage::SavePackageType save_type) {
805 // Stop the page from navigating.
806 Stop();
807
808 save_package_ = new SavePackage(this, save_type, main_file, dir_path);
809 return save_package_->Init();
810}
811
812void TabContents::OnSaveURL(const GURL& url) {
[email protected]3d7474ff2011-07-27 17:47:37813 DownloadManager* dlm = browser_context()->GetDownloadManager();
[email protected]c7dd2f62011-07-18 15:57:59814 dlm->DownloadUrl(url, GetURL(), "", this);
815}
816
[email protected]420ae012009-04-24 05:16:32817bool TabContents::IsActiveEntry(int32 page_id) {
818 NavigationEntry* active_entry = controller_.GetActiveEntry();
819 return (active_entry != NULL &&
820 active_entry->site_instance() == GetSiteInstance() &&
821 active_entry->page_id() == page_id);
822}
823
[email protected]b2fe07d12010-02-09 14:38:08824void TabContents::SetOverrideEncoding(const std::string& encoding) {
[email protected]8cb5d5b2010-02-09 11:36:16825 set_encoding(encoding);
[email protected]216813952011-05-19 22:21:26826 render_view_host()->Send(new ViewMsg_SetPageEncoding(
827 render_view_host()->routing_id(), encoding));
[email protected]8cb5d5b2010-02-09 11:36:16828}
829
[email protected]b2fe07d12010-02-09 14:38:08830void TabContents::ResetOverrideEncoding() {
[email protected]8cb5d5b2010-02-09 11:36:16831 reset_encoding();
[email protected]216813952011-05-19 22:21:26832 render_view_host()->Send(new ViewMsg_ResetPageEncodingToDefault(
833 render_view_host()->routing_id()));
[email protected]8cb5d5b2010-02-09 11:36:16834}
835
[email protected]7ab1e7d62009-10-14 23:32:01836void TabContents::OnCloseStarted() {
837 if (tab_close_start_time_.is_null())
838 tab_close_start_time_ = base::TimeTicks::Now();
839}
840
[email protected]46624bf2010-06-09 16:04:19841bool TabContents::ShouldAcceptDragAndDrop() const {
842#if defined(OS_CHROMEOS)
843 // ChromeOS panels (pop-ups) do not take drag-n-drop.
844 // See http://crosbug.com/2413
[email protected]b35b26b32011-05-05 20:35:14845 if (delegate() && delegate()->IsPopupOrPanel(this))
[email protected]b83af492010-10-09 03:21:20846 return false;
847 return true;
[email protected]46624bf2010-06-09 16:04:19848#else
849 return true;
850#endif
851}
852
[email protected]7813bd72011-02-05 02:19:34853void TabContents::SystemDragEnded() {
854 if (render_view_host())
855 render_view_host()->DragSourceSystemDragEnded();
856 if (delegate())
857 delegate()->DragEnded();
858}
859
[email protected]d0b8d092010-10-25 04:05:17860double TabContents::GetZoomLevel() const {
[email protected]3d7474ff2011-07-27 17:47:37861 HostZoomMap* zoom_map = browser_context()->GetHostZoomMap();
[email protected]b75b8292010-10-01 07:28:25862 if (!zoom_map)
[email protected]d0b8d092010-10-25 04:05:17863 return 0;
[email protected]b75b8292010-10-01 07:28:25864
865 double zoom_level;
866 if (temporary_zoom_settings_) {
867 zoom_level = zoom_map->GetTemporaryZoomLevel(
868 GetRenderProcessHost()->id(), render_view_host()->routing_id());
869 } else {
[email protected]13ffa32e2011-05-27 16:37:01870 zoom_level = zoom_map->GetZoomLevel(net::GetHostOrSpecFromURL(GetURL()));
[email protected]b75b8292010-10-01 07:28:25871 }
[email protected]d0b8d092010-10-25 04:05:17872 return zoom_level;
873}
[email protected]b75b8292010-10-01 07:28:25874
[email protected]d0b8d092010-10-25 04:05:17875int TabContents::GetZoomPercent(bool* enable_increment,
876 bool* enable_decrement) {
877 *enable_decrement = *enable_increment = false;
[email protected]b75b8292010-10-01 07:28:25878 int percent = static_cast<int>(
[email protected]d0b8d092010-10-25 04:05:17879 WebKit::WebView::zoomLevelToZoomFactor(GetZoomLevel()) * 100);
[email protected]b75b8292010-10-01 07:28:25880 *enable_decrement = percent > minimum_zoom_percent_;
881 *enable_increment = percent < maximum_zoom_percent_;
882 return percent;
883}
884
[email protected]3c733bde2010-12-21 19:56:31885void TabContents::ViewSource() {
[email protected]1788e772010-12-15 16:40:50886 if (!delegate_)
887 return;
888
889 NavigationEntry* active_entry = controller().GetActiveEntry();
890 if (!active_entry)
891 return;
892
893 delegate_->ViewSourceForTab(this, active_entry->url());
[email protected]77d8d622010-12-15 10:30:12894}
895
[email protected]932b7a12011-03-09 12:50:27896void TabContents::ViewFrameSource(const GURL& url,
897 const std::string& content_state) {
898 if (!delegate_)
899 return;
900
901 delegate_->ViewSourceForFrame(this, url, content_state);
902}
903
[email protected]c40d6232011-03-25 00:16:21904void TabContents::SetContentRestrictions(int restrictions) {
905 content_restrictions_ = restrictions;
906 delegate()->ContentRestrictionsChanged(this);
907}
908
[email protected]724159a2010-12-30 01:11:18909void TabContents::OnDidStartProvisionalLoadForFrame(int64 frame_id,
910 bool is_main_frame,
[email protected]eacb080b2011-05-22 19:40:26911 bool has_opener_set,
[email protected]724159a2010-12-30 01:11:18912 const GURL& url) {
913 bool is_error_page = (url.spec() == chrome::kUnreachableWebDataURL);
914 GURL validated_url(url);
915 render_view_host()->FilterURL(ChildProcessSecurityPolicy::GetInstance(),
916 GetRenderProcessHost()->id(), &validated_url);
917
[email protected]8093a542011-05-13 07:29:32918 RenderViewHost* rvh =
919 render_manager_.pending_render_view_host() ?
920 render_manager_.pending_render_view_host() : render_view_host();
[email protected]0d60f0192011-04-14 12:40:10921 // Notify observers about the start of the provisional load.
922 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
923 DidStartProvisionalLoadForFrame(frame_id, is_main_frame,
[email protected]8093a542011-05-13 07:29:32924 validated_url, is_error_page, rvh));
[email protected]0d60f0192011-04-14 12:40:10925
[email protected]724159a2010-12-30 01:11:18926 if (is_main_frame) {
[email protected]4850a7f2011-03-08 23:36:59927 // Notify observers about the provisional change in the main frame URL.
928 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
[email protected]eacb080b2011-05-22 19:40:26929 ProvisionalChangeToMainFrameUrl(url, has_opener_set));
[email protected]724159a2010-12-30 01:11:18930 }
931}
932
933void TabContents::OnDidRedirectProvisionalLoad(int32 page_id,
[email protected]eacb080b2011-05-22 19:40:26934 bool has_opener_set,
[email protected]724159a2010-12-30 01:11:18935 const GURL& source_url,
936 const GURL& target_url) {
[email protected]18ba2b12011-04-06 16:35:49937 // TODO(creis): Remove this method and have the pre-rendering code listen to
938 // the ResourceDispatcherHost's RESOURCE_RECEIVED_REDIRECT notification
939 // instead. See http://crbug.com/78512.
[email protected]724159a2010-12-30 01:11:18940 NavigationEntry* entry;
941 if (page_id == -1)
942 entry = controller_.pending_entry();
943 else
944 entry = controller_.GetEntryWithPageID(GetSiteInstance(), page_id);
945 if (!entry || entry->url() != source_url)
946 return;
[email protected]e7d50892011-01-19 21:47:38947
[email protected]4850a7f2011-03-08 23:36:59948 // Notify observers about the provisional change in the main frame URL.
949 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
[email protected]eacb080b2011-05-22 19:40:26950 ProvisionalChangeToMainFrameUrl(target_url,
951 has_opener_set));
[email protected]724159a2010-12-30 01:11:18952}
953
954void TabContents::OnDidFailProvisionalLoadWithError(
955 int64 frame_id,
956 bool is_main_frame,
957 int error_code,
958 const GURL& url,
959 bool showing_repost_interstitial) {
960 VLOG(1) << "Failed Provisional Load: " << url.possibly_invalid_spec()
961 << ", error_code: " << error_code
962 << " is_main_frame: " << is_main_frame
963 << " showing_repost_interstitial: " << showing_repost_interstitial
964 << " frame_id: " << frame_id;
965 GURL validated_url(url);
966 render_view_host()->FilterURL(ChildProcessSecurityPolicy::GetInstance(),
967 GetRenderProcessHost()->id(), &validated_url);
968
969 if (net::ERR_ABORTED == error_code) {
970 // EVIL HACK ALERT! Ignore failed loads when we're showing interstitials.
971 // This means that the interstitial won't be torn down properly, which is
972 // bad. But if we have an interstitial, go back to another tab type, and
973 // then load the same interstitial again, we could end up getting the first
974 // interstitial's "failed" message (as a result of the cancel) when we're on
975 // the second one.
976 //
977 // We can't tell this apart, so we think we're tearing down the current page
978 // which will cause a crash later one. There is also some code in
979 // RenderViewHostManager::RendererAbortedProvisionalLoad that is commented
980 // out because of this problem.
981 //
982 // http://code.google.com/p/chromium/issues/detail?id=2855
983 // Because this will not tear down the interstitial properly, if "back" is
984 // back to another tab type, the interstitial will still be somewhat alive
985 // in the previous tab type. If you navigate somewhere that activates the
986 // tab with the interstitial again, you'll see a flash before the new load
987 // commits of the interstitial page.
988 if (showing_interstitial_page()) {
989 LOG(WARNING) << "Discarding message during interstitial.";
990 return;
991 }
992
[email protected]02102f82011-06-13 20:37:02993 // Discard our pending entry if the load canceled (e.g. if we decided to
994 // download the file instead of load it). We do not verify that the URL
995 // being canceled matches the pending entry's URL because they will not
996 // match if a redirect occurred (in which case we do not want to leave a
997 // stale redirect URL showing). This means that we also cancel the pending
998 // entry if the user started a new navigation. As a result, the navigation
999 // controller may not remember that a load is in progress, but the
1000 // navigation will still commit even if there is no pending entry.
[email protected]724159a2010-12-30 01:11:181001 NavigationEntry* pending_entry = controller_.pending_entry();
[email protected]02102f82011-06-13 20:37:021002 if (pending_entry)
[email protected]c95fa8b2011-04-28 20:26:161003 DidCancelLoading();
[email protected]724159a2010-12-30 01:11:181004
1005 render_manager_.RendererAbortedProvisionalLoad(render_view_host());
1006 }
1007
1008 // Send out a notification that we failed a provisional load with an error.
1009 ProvisionalLoadDetails details(
1010 is_main_frame, controller_.IsURLInPageNavigation(validated_url),
[email protected]b9e8ea62011-03-04 06:29:091011 validated_url, std::string(), false, frame_id);
[email protected]724159a2010-12-30 01:11:181012 details.set_error_code(error_code);
1013
1014 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271015 content::NOTIFICATION_FAIL_PROVISIONAL_LOAD_WITH_ERROR,
[email protected]724159a2010-12-30 01:11:181016 Source<NavigationController>(&controller_),
1017 Details<ProvisionalLoadDetails>(&details));
[email protected]0d60f0192011-04-14 12:40:101018
1019 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
1020 DidFailProvisionalLoad(frame_id, is_main_frame,
1021 validated_url, error_code));
[email protected]724159a2010-12-30 01:11:181022}
1023
1024void TabContents::OnDidLoadResourceFromMemoryCache(
1025 const GURL& url,
[email protected]70435962011-08-02 20:13:281026 const std::string& security_info,
1027 const std::string& http_method,
1028 ResourceType::Type resource_type) {
[email protected]28620862011-03-22 23:07:191029 base::StatsCounter cache("WebKit.CacheHit");
[email protected]724159a2010-12-30 01:11:181030 cache.Increment();
1031
1032 // Send out a notification that we loaded a resource from our memory cache.
[email protected]619864a2011-09-22 19:03:121033 int cert_id = 0, cert_status = 0, security_bits = -1, connection_status = 0;
[email protected]724159a2010-12-30 01:11:181034 SSLManager::DeserializeSecurityInfo(security_info,
1035 &cert_id, &cert_status,
1036 &security_bits,
1037 &connection_status);
[email protected]7a8c55e2011-02-15 08:21:161038 LoadFromMemoryCacheDetails details(url, GetRenderProcessHost()->id(),
1039 cert_id, cert_status);
[email protected]724159a2010-12-30 01:11:181040
1041 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271042 content::NOTIFICATION_LOAD_FROM_MEMORY_CACHE,
[email protected]724159a2010-12-30 01:11:181043 Source<NavigationController>(&controller_),
1044 Details<LoadFromMemoryCacheDetails>(&details));
1045}
1046
1047void TabContents::OnDidDisplayInsecureContent() {
[email protected]221a5d92011-04-28 18:23:161048 UserMetrics::RecordAction(UserMetricsAction("SSL.DisplayedInsecureContent"));
[email protected]724159a2010-12-30 01:11:181049 displayed_insecure_content_ = true;
[email protected]9678d8f2011-08-05 04:05:071050 SSLManager::NotifySSLInternalStateChanged(&controller());
[email protected]724159a2010-12-30 01:11:181051}
1052
1053void TabContents::OnDidRunInsecureContent(
[email protected]92771112011-01-20 00:13:021054 const std::string& security_origin, const GURL& target_url) {
[email protected]9941cf52011-02-08 16:17:231055 LOG(INFO) << security_origin << " ran insecure content from "
1056 << target_url.possibly_invalid_spec();
[email protected]221a5d92011-04-28 18:23:161057 UserMetrics::RecordAction(UserMetricsAction("SSL.RanInsecureContent"));
[email protected]ca406032011-07-19 21:53:051058 if (EndsWith(security_origin, kDotGoogleDotCom, false)) {
1059 UserMetrics::RecordAction(
1060 UserMetricsAction("SSL.RanInsecureContentGoogle"));
1061 }
[email protected]724159a2010-12-30 01:11:181062 controller_.ssl_manager()->DidRunInsecureContent(security_origin);
[email protected]f2b58c4d2011-06-01 23:29:411063 displayed_insecure_content_ = true;
[email protected]9678d8f2011-08-05 04:05:071064 SSLManager::NotifySSLInternalStateChanged(&controller());
[email protected]724159a2010-12-30 01:11:181065}
1066
1067void TabContents::OnDocumentLoadedInFrame(int64 frame_id) {
1068 controller_.DocumentLoadedInFrame();
[email protected]0d60f0192011-04-14 12:40:101069 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
1070 DocumentLoadedInFrame(frame_id));
[email protected]724159a2010-12-30 01:11:181071}
1072
1073void TabContents::OnDidFinishLoad(int64 frame_id) {
[email protected]0d60f0192011-04-14 12:40:101074 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
1075 DidFinishLoad(frame_id));
[email protected]724159a2010-12-30 01:11:181076}
1077
[email protected]c8f73ab2011-01-22 00:05:171078void TabContents::OnUpdateContentRestrictions(int restrictions) {
[email protected]c40d6232011-03-25 00:16:211079 SetContentRestrictions(restrictions);
[email protected]c8f73ab2011-01-22 00:05:171080}
1081
[email protected]216813952011-05-19 22:21:261082void TabContents::OnGoToEntryAtOffset(int offset) {
1083 if (!delegate_ || delegate_->OnGoToEntryOffset(offset)) {
1084 NavigationEntry* entry = controller_.GetEntryAtOffset(offset);
1085 if (!entry)
1086 return;
1087 // Note that we don't call NavigationController::GotToOffset() as we don't
1088 // want to create a pending navigation entry (it might end up lingering
1089 // http://crbug.com/51680).
1090 entry->set_transition_type(entry->transition_type() |
1091 PageTransition::FORWARD_BACK);
1092 NavigateToEntry(*entry, NavigationController::NO_RELOAD);
[email protected]a13cc362011-07-28 21:29:571093
1094 // If the entry is being restored and doesn't have a SiteInstance yet, fill
1095 // it in now that we know. This allows us to find the entry when it commits.
1096 if (!entry->site_instance() &&
1097 entry->restore_type() != NavigationEntry::RESTORE_NONE) {
[email protected]77362eb2011-08-01 17:18:381098 entry->set_site_instance(GetPendingSiteInstance());
[email protected]a13cc362011-07-28 21:29:571099 }
[email protected]216813952011-05-19 22:21:261100 }
1101}
1102
1103void TabContents::OnUpdateZoomLimits(int minimum_percent,
1104 int maximum_percent,
1105 bool remember) {
1106 minimum_zoom_percent_ = minimum_percent;
1107 maximum_zoom_percent_ = maximum_percent;
1108 temporary_zoom_settings_ = !remember;
1109}
1110
1111void TabContents::OnFocusedNodeChanged(bool is_editable_node) {
1112 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271113 content::NOTIFICATION_FOCUS_CHANGED_IN_PAGE,
[email protected]216813952011-05-19 22:21:261114 Source<TabContents>(this),
1115 Details<const bool>(&is_editable_node));
1116}
1117
[email protected]3a29a6e2011-08-24 18:26:211118void TabContents::OnEnumerateDirectory(int request_id,
1119 const FilePath& path) {
1120 delegate()->EnumerateDirectory(this, request_id, path);
1121}
1122
[email protected]7d189022011-08-25 22:54:201123void TabContents::OnJSOutOfMemory() {
1124 delegate()->JSOutOfMemory(this);
1125}
1126
1127void TabContents::OnRegisterProtocolHandler(const std::string& protocol,
1128 const GURL& url,
1129 const string16& title) {
1130 delegate()->RegisterProtocolHandler(this, protocol, url, title);
1131}
1132
1133void TabContents::OnRegisterIntentHandler(const string16& action,
1134 const string16& type,
1135 const string16& href,
1136 const string16& title) {
1137 delegate()->RegisterIntentHandler(this, action, type, href, title);
1138}
1139
1140void TabContents::OnWebIntentDispatch(const IPC::Message& message,
[email protected]ce9751942011-09-21 01:57:241141 const webkit_glue::WebIntentData& intent,
[email protected]7d189022011-08-25 22:54:201142 int intent_id) {
[email protected]ce9751942011-09-21 01:57:241143 delegate()->WebIntentDispatch(this, message.routing_id(), intent, intent_id);
[email protected]7d189022011-08-25 22:54:201144}
1145
[email protected]b888919c2011-09-02 00:32:161146void TabContents::OnFindReply(int request_id,
1147 int number_of_matches,
1148 const gfx::Rect& selection_rect,
1149 int active_match_ordinal,
1150 bool final_update) {
1151 delegate()->FindReply(this, request_id, number_of_matches, selection_rect,
1152 active_match_ordinal, final_update);
[email protected]4e6239c2011-09-23 00:11:001153 // Send a notification to the renderer that we are ready to receive more
1154 // results from the scoping effort of the Find operation. The FindInPage
1155 // scoping is asynchronous and periodically sends results back up to the
1156 // browser using IPC. In an effort to not spam the browser we have the
1157 // browser send an ACK for each FindReply message and have the renderer
1158 // queue up the latest status message while waiting for this ACK.
1159 render_view_host()->Send(
1160 new ViewMsg_FindReplyACK(render_view_host()->routing_id()));
[email protected]b888919c2011-09-02 00:32:161161}
1162
[email protected]d952a052011-09-06 18:42:451163void TabContents::OnCrashedPlugin(const FilePath& plugin_path) {
1164 delegate()->CrashedPlugin(this, plugin_path);
1165}
1166
[email protected]7fc4bbb2011-09-08 21:23:101167void TabContents::OnAppCacheAccessed(const GURL& manifest_url,
1168 bool blocked_by_policy) {
1169 // Notify observers about navigation.
1170 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
1171 AppCacheAccessed(manifest_url, blocked_by_policy));
1172}
1173
[email protected]96d185d2009-04-24 03:28:541174// Notifies the RenderWidgetHost instance about the fact that the page is
1175// loading, or done loading and calls the base implementation.
1176void TabContents::SetIsLoading(bool is_loading,
1177 LoadNotificationDetails* details) {
1178 if (is_loading == is_loading_)
1179 return;
1180
1181 if (!is_loading) {
[email protected]9c235f042011-08-10 22:28:211182 load_state_ = net::LoadStateWithParam(net::LOAD_STATE_IDLE, string16());
[email protected]96d185d2009-04-24 03:28:541183 load_state_host_.clear();
[email protected]094e5b22009-09-25 04:23:561184 upload_size_ = 0;
1185 upload_position_ = 0;
[email protected]96d185d2009-04-24 03:28:541186 }
1187
1188 render_manager_.SetIsLoading(is_loading);
1189
1190 is_loading_ = is_loading;
1191 waiting_for_response_ = is_loading;
1192
[email protected]6ebdc9b2010-09-27 16:55:571193 if (delegate_)
1194 delegate_->LoadingStateChanged(this);
[email protected]329581b2009-04-28 06:52:351195 NotifyNavigationStateChanged(INVALIDATE_LOAD);
[email protected]96d185d2009-04-24 03:28:541196
[email protected]432115822011-07-10 15:52:271197 int type = is_loading ? content::NOTIFICATION_LOAD_START :
1198 content::NOTIFICATION_LOAD_STOP;
[email protected]ce5c4502009-05-06 16:46:111199 NotificationDetails det = NotificationService::NoDetails();
[email protected]96d185d2009-04-24 03:28:541200 if (details)
1201 det = Details<LoadNotificationDetails>(details);
1202 NotificationService::current()->Notify(type,
1203 Source<NavigationController>(&controller_),
1204 det);
1205}
1206
[email protected]d0980792011-02-13 19:41:401207WebUI* TabContents::GetWebUIForCurrentState() {
[email protected]7ade2732011-02-10 00:13:581208 // When there is a pending navigation entry, we want to use the pending WebUI
[email protected]96d185d2009-04-24 03:28:541209 // that goes along with it to control the basic flags. For example, we want to
1210 // show the pending URL in the URL bar, so we want the display_url flag to
1211 // be from the pending entry.
1212 //
1213 // The confusion comes because there are multiple possibilities for the
1214 // initial load in a tab as a side effect of the way the RenderViewHostManager
1215 // works.
1216 //
[email protected]e0112912011-02-02 22:54:351217 // - For the very first tab the load looks "normal". The new tab Web UI is
[email protected]96d185d2009-04-24 03:28:541218 // the pending one, and we want it to apply here.
1219 //
1220 // - For subsequent new tabs, they'll get a new SiteInstance which will then
1221 // get switched to the one previously associated with the new tab pages.
[email protected]7ade2732011-02-10 00:13:581222 // This switching will cause the manager to commit the RVH/WebUI. So we'll
[email protected]e0112912011-02-02 22:54:351223 // have a committed Web UI in this case.
[email protected]96d185d2009-04-24 03:28:541224 //
1225 // This condition handles all of these cases:
1226 //
1227 // - First load in first tab: no committed nav entry + pending nav entry +
1228 // pending dom ui:
[email protected]e0112912011-02-02 22:54:351229 // -> Use pending Web UI if any.
[email protected]96d185d2009-04-24 03:28:541230 //
1231 // - First load in second tab: no committed nav entry + pending nav entry +
[email protected]e0112912011-02-02 22:54:351232 // no pending Web UI:
1233 // -> Use the committed Web UI if any.
[email protected]96d185d2009-04-24 03:28:541234 //
1235 // - Second navigation in any tab: committed nav entry + pending nav entry:
[email protected]e0112912011-02-02 22:54:351236 // -> Use pending Web UI if any.
[email protected]96d185d2009-04-24 03:28:541237 //
1238 // - Normal state with no load: committed nav entry + no pending nav entry:
[email protected]e0112912011-02-02 22:54:351239 // -> Use committed Web UI.
[email protected]96d185d2009-04-24 03:28:541240 if (controller_.pending_entry() &&
1241 (controller_.GetLastCommittedEntry() ||
[email protected]7ade2732011-02-10 00:13:581242 render_manager_.pending_web_ui()))
1243 return render_manager_.pending_web_ui();
1244 return render_manager_.web_ui();
[email protected]96d185d2009-04-24 03:28:541245}
[email protected]420ae012009-04-24 05:16:321246
[email protected]1fd1a502011-03-30 16:55:561247WebUI::TypeID TabContents::GetWebUITypeForCurrentState() {
[email protected]3d7474ff2011-07-27 17:47:371248 return content::WebUIFactory::Get()->GetWebUIType(browser_context(),
1249 GetURL());
[email protected]1fd1a502011-03-30 16:55:561250}
1251
[email protected]420ae012009-04-24 05:16:321252void TabContents::DidNavigateMainFramePostCommit(
[email protected]8286f51a2011-05-31 17:39:131253 const content::LoadCommittedDetails& details,
[email protected]420ae012009-04-24 05:16:321254 const ViewHostMsg_FrameNavigate_Params& params) {
[email protected]1fd1a502011-03-30 16:55:561255 if (opener_web_ui_type_ != WebUI::kNoWebUI) {
[email protected]80a8fad2011-01-29 04:02:381256 // If this is a window.open navigation, use the same WebUI as the renderer
[email protected]c2e74fe82009-09-03 17:57:441257 // that opened the window, as long as both renderers have the same
1258 // privileges.
[email protected]1fd1a502011-03-30 16:55:561259 if (delegate_ && opener_web_ui_type_ == GetWebUITypeForCurrentState()) {
1260 WebUI* web_ui = content::GetContentClient()->browser()->
1261 GetWebUIFactory()->CreateWebUIForURL(this, GetURL());
[email protected]7ade2732011-02-10 00:13:581262 // web_ui might be NULL if the URL refers to a non-existent extension.
1263 if (web_ui) {
[email protected]d0980792011-02-13 19:41:401264 render_manager_.SetWebUIPostCommit(web_ui);
[email protected]7ade2732011-02-10 00:13:581265 web_ui->RenderViewCreated(render_view_host());
[email protected]c2e74fe82009-09-03 17:57:441266 }
1267 }
[email protected]1fd1a502011-03-30 16:55:561268 opener_web_ui_type_ = WebUI::kNoWebUI;
[email protected]c2e74fe82009-09-03 17:57:441269 }
1270
[email protected]4e697b042011-07-08 06:44:561271 if (details.is_navigation_to_different_page()) {
[email protected]420ae012009-04-24 05:16:321272 // Clear the status bubble. This is a workaround for a bug where WebKit
1273 // doesn't let us know that the cursor left an element during a
1274 // transition (this is also why the mouse cursor remains as a hand after
1275 // clicking on a link); see bugs 1184641 and 980803. We don't want to
1276 // clear the bubble when a user navigates to a named anchor in the same
1277 // page.
1278 UpdateTargetURL(details.entry->page_id(), GURL());
[email protected]420ae012009-04-24 05:16:321279 }
1280
[email protected]a6e82fc2010-02-24 22:28:141281 if (!details.is_in_page) {
[email protected]f17a0ee2010-05-17 17:38:471282 // Once the main frame is navigated, we're no longer considered to have
1283 // displayed insecure content.
1284 displayed_insecure_content_ = false;
[email protected]aece2c7f2009-08-27 20:43:171285 }
[email protected]ce5c4502009-05-06 16:46:111286
[email protected]8cc8d492010-02-02 10:40:491287 // Close constrained windows if necessary.
1288 if (!net::RegistryControlledDomainService::SameDomainOrHost(
1289 details.previous_url, details.entry->url()))
1290 CloseConstrainedWindows();
1291
[email protected]3c9e1872010-11-18 16:17:491292 // Notify observers about navigation.
[email protected]585b30362011-01-28 02:30:171293 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
[email protected]3c9e1872010-11-18 16:17:491294 DidNavigateMainFramePostCommit(details, params));
[email protected]420ae012009-04-24 05:16:321295}
1296
1297void TabContents::DidNavigateAnyFramePostCommit(
1298 RenderViewHost* render_view_host,
[email protected]8286f51a2011-05-31 17:39:131299 const content::LoadCommittedDetails& details,
[email protected]420ae012009-04-24 05:16:321300 const ViewHostMsg_FrameNavigate_Params& params) {
[email protected]3ab9cb82011-06-03 18:02:071301 // If we navigate, reset JavaScript state. This does nothing to prevent
[email protected]420ae012009-04-24 05:16:321302 // a malicious script from spamming messages, since the script could just
1303 // reload the page to stop blocking.
[email protected]2e5b90c2011-08-16 21:11:551304 if (dialog_creator_) {
1305 dialog_creator_->ResetJavaScriptState(this);
1306 dialog_creator_ = NULL;
1307 }
[email protected]420ae012009-04-24 05:16:321308
[email protected]3c9e1872010-11-18 16:17:491309 // Notify observers about navigation.
[email protected]585b30362011-01-28 02:30:171310 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
[email protected]3c9e1872010-11-18 16:17:491311 DidNavigateAnyFramePostCommit(details, params));
[email protected]420ae012009-04-24 05:16:321312}
1313
[email protected]8cc8d492010-02-02 10:40:491314void TabContents::CloseConstrainedWindows() {
1315 // Clear out any constrained windows since we are leaving this page entirely.
[email protected]aed59602011-02-28 22:57:331316 // To ensure that we iterate over every element in child_windows_ we
1317 // need to use a copy of child_windows_. Otherwise if
1318 // window->CloseConstrainedWindow() modifies child_windows_ we could end up
1319 // skipping some elements.
1320 ConstrainedWindowList child_windows_copy(child_windows_);
1321 for (ConstrainedWindowList::iterator it = child_windows_copy.begin();
1322 it != child_windows_copy.end(); ++it) {
1323 ConstrainedWindow* window = *it;
[email protected]fa1cf0b82010-01-15 21:49:441324 if (window) {
[email protected]420ae012009-04-24 05:16:321325 window->CloseConstrainedWindow();
[email protected]fa1cf0b82010-01-15 21:49:441326 BlockTabContent(false);
1327 }
[email protected]420ae012009-04-24 05:16:321328 }
1329}
1330
[email protected]420ae012009-04-24 05:16:321331void TabContents::UpdateMaxPageIDIfNecessary(SiteInstance* site_instance,
1332 RenderViewHost* rvh) {
1333 // If we are creating a RVH for a restored controller, then we might
1334 // have more page IDs than the SiteInstance's current max page ID. We must
1335 // make sure that the max page ID is larger than any restored page ID.
1336 // Note that it is ok for conflicting page IDs to exist in another tab
1337 // (i.e., NavigationController), but if any page ID is larger than the max,
1338 // the back/forward list will get confused.
1339 int max_restored_page_id = controller_.max_restored_page_id();
1340 if (max_restored_page_id > 0) {
1341 int curr_max_page_id = site_instance->max_page_id();
1342 if (max_restored_page_id > curr_max_page_id) {
1343 // Need to update the site instance immediately.
1344 site_instance->UpdateMaxPageID(max_restored_page_id);
1345
1346 // Also tell the renderer to update its internal representation. We
1347 // need to reserve enough IDs to make all restored page IDs less than
1348 // the max.
1349 if (curr_max_page_id < 0)
1350 curr_max_page_id = 0;
[email protected]216813952011-05-19 22:21:261351 rvh->Send(new ViewMsg_ReservePageIDRange(
1352 rvh->routing_id(), max_restored_page_id - curr_max_page_id));
[email protected]420ae012009-04-24 05:16:321353 }
1354 }
1355}
1356
[email protected]6b2f7a82011-04-25 19:30:511357bool TabContents::UpdateTitleForEntry(NavigationEntry* entry,
[email protected]acafd272011-07-26 17:35:571358 const string16& title) {
[email protected]420ae012009-04-24 05:16:321359 // For file URLs without a title, use the pathname instead. In the case of a
1360 // synthesized title, we don't want the update to count toward the "one set
1361 // per page of the title to history."
[email protected]6b2f7a82011-04-25 19:30:511362 string16 final_title;
[email protected]420ae012009-04-24 05:16:321363 bool explicit_set;
[email protected]987fc3a2011-05-26 14:18:091364 if (entry && entry->url().SchemeIsFile() && title.empty()) {
[email protected]6b2f7a82011-04-25 19:30:511365 final_title = UTF8ToUTF16(entry->url().ExtractFileName());
[email protected]420ae012009-04-24 05:16:321366 explicit_set = false; // Don't count synthetic titles toward the set limit.
1367 } else {
[email protected]acafd272011-07-26 17:35:571368 TrimWhitespace(title, TRIM_ALL, &final_title);
[email protected]420ae012009-04-24 05:16:321369 explicit_set = true;
1370 }
1371
[email protected]987fc3a2011-05-26 14:18:091372 // If a page is created via window.open and never navigated,
1373 // there will be no navigation entry. In this situation,
1374 // |page_title_when_no_navigaiton_entry_| will be used for page title.
1375 if (entry) {
1376 if (final_title == entry->title())
1377 return false; // Nothing changed, don't bother.
[email protected]420ae012009-04-24 05:16:321378
[email protected]987fc3a2011-05-26 14:18:091379 entry->set_title(final_title);
1380 } else {
1381 if (page_title_when_no_navigation_entry_ == final_title)
1382 return false; // Nothing changed, don't bother.
1383
1384 page_title_when_no_navigation_entry_ = final_title;
1385 }
[email protected]420ae012009-04-24 05:16:321386
[email protected]420ae012009-04-24 05:16:321387 // Lastly, set the title for the view.
[email protected]790e2fd2011-09-21 20:28:231388 view_->SetPageTitle(final_title);
[email protected]420ae012009-04-24 05:16:321389
[email protected]105bb0f2011-05-24 17:12:141390 TitleUpdatedDetails details(entry, explicit_set);
1391
[email protected]cbc0e1b2010-04-12 18:33:041392 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271393 content::NOTIFICATION_TAB_CONTENTS_TITLE_UPDATED,
[email protected]cbc0e1b2010-04-12 18:33:041394 Source<TabContents>(this),
[email protected]105bb0f2011-05-24 17:12:141395 Details<TitleUpdatedDetails>(&details));
[email protected]cbc0e1b2010-04-12 18:33:041396
[email protected]420ae012009-04-24 05:16:321397 return true;
1398}
1399
1400void TabContents::NotifySwapped() {
1401 // After sending out a swap notification, we need to send a disconnect
1402 // notification so that clients that pick up a pointer to |this| can NULL the
1403 // pointer. See Bug 1230284.
1404 notify_disconnection_ = true;
1405 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271406 content::NOTIFICATION_TAB_CONTENTS_SWAPPED,
[email protected]d82ed61e2009-06-16 02:46:221407 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:321408 NotificationService::NoDetails());
1409}
1410
1411void TabContents::NotifyConnected() {
1412 notify_disconnection_ = true;
1413 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271414 content::NOTIFICATION_TAB_CONTENTS_CONNECTED,
[email protected]d82ed61e2009-06-16 02:46:221415 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:321416 NotificationService::NoDetails());
1417}
1418
1419void TabContents::NotifyDisconnected() {
1420 if (!notify_disconnection_)
1421 return;
1422
1423 notify_disconnection_ = false;
1424 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271425 content::NOTIFICATION_TAB_CONTENTS_DISCONNECTED,
[email protected]d82ed61e2009-06-16 02:46:221426 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:321427 NotificationService::NoDetails());
1428}
1429
[email protected]8d3347f2009-07-09 22:00:211430RenderViewHostDelegate::View* TabContents::GetViewDelegate() {
[email protected]420ae012009-04-24 05:16:321431 return view_.get();
1432}
1433
[email protected]8d3347f2009-07-09 22:00:211434RenderViewHostDelegate::RendererManagement*
1435TabContents::GetRendererManagementDelegate() {
1436 return &render_manager_;
1437}
1438
[email protected]3d7474ff2011-07-27 17:47:371439RendererPreferences TabContents::GetRendererPrefs(
1440 content::BrowserContext* browser_context) const {
[email protected]840b1512009-07-21 16:53:461441 return renderer_preferences_;
[email protected]80d96fa2009-06-10 22:34:511442}
1443
[email protected]57c6a652009-05-04 07:58:341444TabContents* TabContents::GetAsTabContents() {
1445 return this;
1446}
1447
[email protected]7b291f92009-08-14 05:43:531448ViewType::Type TabContents::GetRenderViewType() const {
1449 return ViewType::TAB_CONTENTS;
1450}
1451
[email protected]420ae012009-04-24 05:16:321452void TabContents::RenderViewCreated(RenderViewHost* render_view_host) {
[email protected]2e4633c2009-07-09 16:58:061453 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271454 content::NOTIFICATION_RENDER_VIEW_HOST_CREATED_FOR_TAB,
[email protected]2e4633c2009-07-09 16:58:061455 Source<TabContents>(this),
1456 Details<RenderViewHost>(render_view_host));
[email protected]420ae012009-04-24 05:16:321457 NavigationEntry* entry = controller_.GetActiveEntry();
1458 if (!entry)
1459 return;
1460
1461 // When we're creating views, we're still doing initial setup, so we always
[email protected]e0112912011-02-02 22:54:351462 // use the pending Web UI rather than any possibly existing committed one.
[email protected]1fd1a502011-03-30 16:55:561463 if (render_manager_.pending_web_ui())
[email protected]7ade2732011-02-10 00:13:581464 render_manager_.pending_web_ui()->RenderViewCreated(render_view_host);
[email protected]420ae012009-04-24 05:16:321465
1466 if (entry->IsViewSourceMode()) {
1467 // Put the renderer in view source mode.
1468 render_view_host->Send(
1469 new ViewMsg_EnableViewSourceMode(render_view_host->routing_id()));
1470 }
[email protected]0666aef2009-05-13 19:48:081471
1472 view()->RenderViewCreated(render_view_host);
[email protected]11f764bb2011-05-25 19:45:261473
1474 FOR_EACH_OBSERVER(
1475 TabContentsObserver, observers_, RenderViewCreated(render_view_host));
[email protected]420ae012009-04-24 05:16:321476}
1477
1478void TabContents::RenderViewReady(RenderViewHost* rvh) {
1479 if (rvh != render_view_host()) {
1480 // Don't notify the world, since this came from a renderer in the
1481 // background.
1482 return;
1483 }
1484
1485 NotifyConnected();
[email protected]9d8a4642009-07-29 17:25:301486 bool was_crashed = is_crashed();
[email protected]443b80e2010-12-14 00:42:231487 SetIsCrashed(base::TERMINATION_STATUS_STILL_RUNNING, 0);
[email protected]9d8a4642009-07-29 17:25:301488
1489 // Restore the focus to the tab (otherwise the focus will be on the top
1490 // window).
[email protected]484ae5912010-09-29 19:16:141491 if (was_crashed && !FocusLocationBarByDefault() &&
1492 (!delegate_ || delegate_->ShouldFocusPageAfterCrash())) {
[email protected]9d8a4642009-07-29 17:25:301493 Focus();
[email protected]484ae5912010-09-29 19:16:141494 }
[email protected]420ae012009-04-24 05:16:321495}
1496
[email protected]443b80e2010-12-14 00:42:231497void TabContents::RenderViewGone(RenderViewHost* rvh,
1498 base::TerminationStatus status,
1499 int error_code) {
[email protected]420ae012009-04-24 05:16:321500 if (rvh != render_view_host()) {
1501 // The pending page's RenderViewHost is gone.
1502 return;
1503 }
1504
1505 SetIsLoading(false, NULL);
1506 NotifyDisconnected();
[email protected]443b80e2010-12-14 00:42:231507 SetIsCrashed(status, error_code);
[email protected]420ae012009-04-24 05:16:321508
[email protected]55452902011-06-01 21:57:471509 FOR_EACH_OBSERVER(TabContentsObserver, observers_, RenderViewGone());
[email protected]420ae012009-04-24 05:16:321510}
1511
[email protected]2e4633c2009-07-09 16:58:061512void TabContents::RenderViewDeleted(RenderViewHost* rvh) {
[email protected]fb7b79f72009-11-06 18:00:481513 render_manager_.RenderViewDeleted(rvh);
[email protected]2e4633c2009-07-09 16:58:061514}
1515
[email protected]420ae012009-04-24 05:16:321516void TabContents::DidNavigate(RenderViewHost* rvh,
1517 const ViewHostMsg_FrameNavigate_Params& params) {
[email protected]09b29342011-06-24 19:18:481518 if (PageTransition::IsMainFrame(params.transition))
[email protected]420ae012009-04-24 05:16:321519 render_manager_.DidNavigateMainFrame(rvh);
[email protected]8030f012009-09-25 18:09:371520
[email protected]420ae012009-04-24 05:16:321521 // Update the site of the SiteInstance if it doesn't have one yet.
1522 if (!GetSiteInstance()->has_site())
1523 GetSiteInstance()->SetSite(params.url);
1524
1525 // Need to update MIME type here because it's referred to in
1526 // UpdateNavigationCommands() called by RendererDidNavigate() to
1527 // determine whether or not to enable the encoding menu.
1528 // It's updated only for the main frame. For a subframe,
1529 // RenderView::UpdateURL does not set params.contents_mime_type.
1530 // (see http://code.google.com/p/chromium/issues/detail?id=2929 )
1531 // TODO(jungshik): Add a test for the encoding menu to avoid
1532 // regressing it again.
1533 if (PageTransition::IsMainFrame(params.transition))
1534 contents_mime_type_ = params.contents_mime_type;
1535
[email protected]8286f51a2011-05-31 17:39:131536 content::LoadCommittedDetails details;
[email protected]93f230e02011-06-01 14:40:001537 bool did_navigate = controller_.RendererDidNavigate(params, &details);
[email protected]3e90d4a2009-07-03 17:38:391538
[email protected]a9c0bfe2010-09-17 08:35:221539 // Send notification about committed provisional loads. This notification is
1540 // different from the NAV_ENTRY_COMMITTED notification which doesn't include
1541 // the actual URL navigated to and isn't sent for AUTO_SUBFRAME navigations.
1542 if (details.type != NavigationType::NAV_IGNORE) {
[email protected]dabb0d12010-10-05 12:50:071543 // For AUTO_SUBFRAME navigations, an event for the main frame is generated
1544 // that is not recorded in the navigation history. For the purpose of
1545 // tracking navigation events, we treat this event as a sub frame navigation
1546 // event.
1547 bool is_main_frame = did_navigate ? details.is_main_frame : false;
[email protected]0d60f0192011-04-14 12:40:101548 PageTransition::Type transition_type = params.transition;
[email protected]a9c0bfe2010-09-17 08:35:221549 // Whether or not a page transition was triggered by going backward or
1550 // forward in the history is only stored in the navigation controller's
1551 // entry list.
1552 if (did_navigate &&
1553 (controller_.GetActiveEntry()->transition_type() &
1554 PageTransition::FORWARD_BACK)) {
[email protected]0d60f0192011-04-14 12:40:101555 transition_type = params.transition | PageTransition::FORWARD_BACK;
[email protected]a9c0bfe2010-09-17 08:35:221556 }
[email protected]0d60f0192011-04-14 12:40:101557 // Notify observers about the commit of the provisional load.
1558 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
1559 DidCommitProvisionalLoadForFrame(params.frame_id,
1560 is_main_frame, params.url, transition_type));
[email protected]a9c0bfe2010-09-17 08:35:221561 }
1562
[email protected]76543b92009-08-31 17:27:451563 if (!did_navigate)
1564 return; // No navigation happened.
[email protected]420ae012009-04-24 05:16:321565
1566 // DO NOT ADD MORE STUFF TO THIS FUNCTION! Your component should either listen
1567 // for the appropriate notification (best) or you can add it to
1568 // DidNavigateMainFramePostCommit / DidNavigateAnyFramePostCommit (only if
1569 // necessary, please).
1570
1571 // Run post-commit tasks.
[email protected]93f230e02011-06-01 14:40:001572 if (details.is_main_frame) {
[email protected]420ae012009-04-24 05:16:321573 DidNavigateMainFramePostCommit(details, params);
[email protected]09b29342011-06-24 19:18:481574 if (delegate())
1575 delegate()->DidNavigateMainFramePostCommit(this);
[email protected]93f230e02011-06-01 14:40:001576 }
[email protected]420ae012009-04-24 05:16:321577 DidNavigateAnyFramePostCommit(rvh, details, params);
1578}
1579
1580void TabContents::UpdateState(RenderViewHost* rvh,
1581 int32 page_id,
1582 const std::string& state) {
[email protected]992db4c2011-05-12 15:37:151583 // Ensure that this state update comes from either the active RVH or one of
1584 // the swapped out RVHs. We don't expect to hear from any other RVHs.
1585 DCHECK(rvh == render_view_host() || render_manager_.IsSwappedOut(rvh));
[email protected]420ae012009-04-24 05:16:321586
1587 // We must be prepared to handle state updates for any page, these occur
1588 // when the user is scrolling and entering form data, as well as when we're
1589 // leaving a page, in which case our state may have already been moved to
1590 // the next page. The navigation controller will look up the appropriate
1591 // NavigationEntry and update it when it is notified via the delegate.
1592
1593 int entry_index = controller_.GetEntryIndexWithPageID(
[email protected]992db4c2011-05-12 15:37:151594 rvh->site_instance(), page_id);
[email protected]420ae012009-04-24 05:16:321595 if (entry_index < 0)
1596 return;
1597 NavigationEntry* entry = controller_.GetEntryAtIndex(entry_index);
1598
1599 if (state == entry->content_state())
1600 return; // Nothing to update.
1601 entry->set_content_state(state);
1602 controller_.NotifyEntryChanged(entry, entry_index);
1603}
1604
1605void TabContents::UpdateTitle(RenderViewHost* rvh,
[email protected]acafd272011-07-26 17:35:571606 int32 page_id,
[email protected]a49e10b2011-08-01 23:57:461607 const string16& title,
1608 base::i18n::TextDirection title_direction) {
[email protected]420ae012009-04-24 05:16:321609 // If we have a title, that's a pretty good indication that we've started
1610 // getting useful data.
1611 SetNotWaitingForResponse();
1612
1613 DCHECK(rvh == render_view_host());
[email protected]371cc8b2010-09-01 16:03:481614 NavigationEntry* entry = controller_.GetEntryWithPageID(rvh->site_instance(),
[email protected]ec0b6c42010-08-26 03:16:581615 page_id);
[email protected]987fc3a2011-05-26 14:18:091616
[email protected]a49e10b2011-08-01 23:57:461617 // TODO(evan): make use of title_direction.
1618 // http://code.google.com/p/chromium/issues/detail?id=27094
[email protected]987fc3a2011-05-26 14:18:091619 if (!UpdateTitleForEntry(entry, title))
[email protected]420ae012009-04-24 05:16:321620 return;
1621
1622 // Broadcast notifications when the UI should be updated.
1623 if (entry == controller_.GetEntryAtOffset(0))
[email protected]f1cd5e82009-10-23 17:19:031624 NotifyNavigationStateChanged(INVALIDATE_TITLE);
[email protected]420ae012009-04-24 05:16:321625}
1626
[email protected]420ae012009-04-24 05:16:321627void TabContents::UpdateEncoding(RenderViewHost* render_view_host,
[email protected]41fc0322009-09-04 22:23:401628 const std::string& encoding) {
[email protected]420ae012009-04-24 05:16:321629 set_encoding(encoding);
1630}
1631
1632void TabContents::UpdateTargetURL(int32 page_id, const GURL& url) {
1633 if (delegate())
[email protected]d952a052011-09-06 18:42:451634 delegate()->UpdateTargetURL(this, page_id, url);
[email protected]420ae012009-04-24 05:16:321635}
1636
[email protected]420ae012009-04-24 05:16:321637void TabContents::Close(RenderViewHost* rvh) {
[email protected]07707302009-11-06 00:50:291638 // The UI may be in an event-tracking loop, such as between the
1639 // mouse-down and mouse-up in text selection or a button click.
1640 // Defer the close until after tracking is complete, so that we
1641 // don't free objects out from under the UI.
1642 // TODO(shess): This could probably be integrated with the
1643 // IsDoingDrag() test below. Punting for now because I need more
1644 // research to understand how this impacts platforms other than Mac.
1645 // TODO(shess): This could get more fine-grained. For instance,
1646 // closing a tab in another window while selecting text in the
1647 // current window's Omnibox should be just fine.
1648 if (view()->IsEventTracking()) {
1649 view()->CloseTabAfterEventTracking();
1650 return;
1651 }
1652
[email protected]24a4d1062009-07-10 23:10:421653 // If we close the tab while we're in the middle of a drag, we'll crash.
1654 // Instead, cancel the drag and close it as soon as the drag ends.
1655 if (view()->IsDoingDrag()) {
1656 view()->CancelDragAndCloseTab();
1657 return;
1658 }
1659
[email protected]420ae012009-04-24 05:16:321660 // Ignore this if it comes from a RenderViewHost that we aren't showing.
1661 if (delegate() && rvh == render_view_host())
1662 delegate()->CloseContents(this);
1663}
1664
1665void TabContents::RequestMove(const gfx::Rect& new_bounds) {
[email protected]b35b26b32011-05-05 20:35:141666 if (delegate() && delegate()->IsPopupOrPanel(this))
[email protected]420ae012009-04-24 05:16:321667 delegate()->MoveContents(this, new_bounds);
1668}
1669
[email protected]7ab1e7d62009-10-14 23:32:011670void TabContents::DidStartLoading() {
[email protected]420ae012009-04-24 05:16:321671 SetIsLoading(true, NULL);
[email protected]3c9e1872010-11-18 16:17:491672
[email protected]c40d6232011-03-25 00:16:211673 if (delegate() && content_restrictions_) {
1674 content_restrictions_ = 0;
[email protected]de4761b2010-12-07 21:05:211675 delegate()->ContentRestrictionsChanged(this);
[email protected]9e823662010-10-13 23:36:001676 }
[email protected]3c9e1872010-11-18 16:17:491677
1678 // Notify observers about navigation.
[email protected]585b30362011-01-28 02:30:171679 FOR_EACH_OBSERVER(TabContentsObserver, observers_, DidStartLoading());
[email protected]420ae012009-04-24 05:16:321680}
1681
[email protected]7ab1e7d62009-10-14 23:32:011682void TabContents::DidStopLoading() {
[email protected]420ae012009-04-24 05:16:321683 scoped_ptr<LoadNotificationDetails> details;
1684
1685 NavigationEntry* entry = controller_.GetActiveEntry();
1686 // An entry may not exist for a stop when loading an initial blank page or
1687 // if an iframe injected by script into a blank page finishes loading.
1688 if (entry) {
[email protected]420ae012009-04-24 05:16:321689 base::TimeDelta elapsed = base::TimeTicks::Now() - current_load_start_;
1690
1691 details.reset(new LoadNotificationDetails(
[email protected]ebe89e062009-08-13 23:16:541692 entry->virtual_url(),
[email protected]420ae012009-04-24 05:16:321693 entry->transition_type(),
1694 elapsed,
1695 &controller_,
1696 controller_.GetCurrentEntryIndex()));
1697 }
1698
[email protected]420ae012009-04-24 05:16:321699 SetIsLoading(false, details.get());
[email protected]3c9e1872010-11-18 16:17:491700
1701 // Notify observers about navigation.
[email protected]585b30362011-01-28 02:30:171702 FOR_EACH_OBSERVER(TabContentsObserver, observers_, DidStopLoading());
[email protected]420ae012009-04-24 05:16:321703}
1704
[email protected]c95fa8b2011-04-28 20:26:161705void TabContents::DidCancelLoading() {
1706 controller_.DiscardNonCommittedEntries();
1707
1708 // Update the URL display.
1709 NotifyNavigationStateChanged(TabContents::INVALIDATE_URL);
1710}
1711
[email protected]1a3c3cb2010-12-16 21:03:401712void TabContents::DidChangeLoadProgress(double progress) {
1713 if (delegate())
1714 delegate()->LoadProgressChanged(progress);
1715}
1716
[email protected]25497492010-09-11 15:15:081717void TabContents::DocumentOnLoadCompletedInMainFrame(
1718 RenderViewHost* render_view_host,
1719 int32 page_id) {
1720 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271721 content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME,
[email protected]25497492010-09-11 15:15:081722 Source<TabContents>(this),
1723 Details<int>(&page_id));
1724}
1725
[email protected]420ae012009-04-24 05:16:321726void TabContents::RequestOpenURL(const GURL& url, const GURL& referrer,
1727 WindowOpenDisposition disposition) {
[email protected]7ade2732011-02-10 00:13:581728 if (render_manager_.web_ui()) {
[email protected]e0112912011-02-02 22:54:351729 // When we're a Web UI, it will provide a page transition type for us (this
[email protected]420ae012009-04-24 05:16:321730 // is so the new tab page can specify AUTO_BOOKMARK for automatically
1731 // generated suggestions).
1732 //
[email protected]e0112912011-02-02 22:54:351733 // Note also that we hide the referrer for Web UI pages. We don't really
[email protected]60e448982009-05-06 04:21:161734 // want web sites to see a referrer of "chrome://blah" (and some
1735 // chrome: URLs might have search terms or other stuff we don't want to
[email protected]420ae012009-04-24 05:16:321736 // send to the site), so we send no referrer.
1737 OpenURL(url, GURL(), disposition,
[email protected]7ade2732011-02-10 00:13:581738 render_manager_.web_ui()->link_transition_type());
[email protected]420ae012009-04-24 05:16:321739 } else {
1740 OpenURL(url, referrer, disposition, PageTransition::LINK);
1741 }
1742}
1743
[email protected]420ae012009-04-24 05:16:321744void TabContents::RunJavaScriptMessage(
[email protected]992db4c2011-05-12 15:37:151745 const RenderViewHost* rvh,
[email protected]4f5ce842011-05-27 19:34:411746 const string16& message,
1747 const string16& default_prompt,
[email protected]420ae012009-04-24 05:16:321748 const GURL& frame_url,
1749 const int flags,
1750 IPC::Message* reply_msg,
1751 bool* did_suppress_message) {
[email protected]3ab9cb82011-06-03 18:02:071752 // Suppress JavaScript dialogs when requested. Also suppress messages when
1753 // showing an interstitial as it's shown over the previous page and we don't
1754 // want the hidden page's dialogs to interfere with the interstitial.
[email protected]8f55e802010-12-06 18:11:501755 bool suppress_this_message =
[email protected]992db4c2011-05-12 15:37:151756 rvh->is_swapped_out() ||
[email protected]8f55e802010-12-06 18:11:501757 showing_interstitial_page() ||
[email protected]3ab9cb82011-06-03 18:02:071758 !delegate_ ||
1759 delegate_->ShouldSuppressDialogs();
[email protected]420ae012009-04-24 05:16:321760
1761 if (!suppress_this_message) {
[email protected]b627d9a2011-06-28 14:06:341762 content::JavaScriptDialogCreator::TitleType title_type;
1763 string16 title;
1764
1765 if (!frame_url.has_host()) {
1766 title_type = content::JavaScriptDialogCreator::DIALOG_TITLE_NONE;
1767 } else {
1768 title_type = content::JavaScriptDialogCreator::DIALOG_TITLE_FORMATTED_URL;
1769 title = net::FormatUrl(
1770 frame_url.GetOrigin(),
1771 content::GetContentClient()->browser()->GetAcceptLangs(this));
1772 }
1773
[email protected]2e5b90c2011-08-16 21:11:551774 dialog_creator_ = delegate_->GetJavaScriptDialogCreator();
1775 dialog_creator_->RunJavaScriptDialog(this,
1776 title_type,
1777 title,
1778 flags,
1779 message,
1780 default_prompt,
1781 reply_msg,
1782 &suppress_this_message);
[email protected]420ae012009-04-24 05:16:321783 }
[email protected]3ab9cb82011-06-03 18:02:071784
1785 if (suppress_this_message) {
1786 // If we are suppressing messages, just reply as if the user immediately
1787 // pressed "Cancel".
1788 OnDialogClosed(reply_msg, false, string16());
1789 }
1790
1791 *did_suppress_message = suppress_this_message;
[email protected]420ae012009-04-24 05:16:321792}
1793
[email protected]992db4c2011-05-12 15:37:151794void TabContents::RunBeforeUnloadConfirm(const RenderViewHost* rvh,
[email protected]4f5ce842011-05-27 19:34:411795 const string16& message,
[email protected]420ae012009-04-24 05:16:321796 IPC::Message* reply_msg) {
[email protected]5aab5e22010-12-08 22:13:291797 if (delegate())
1798 delegate()->WillRunBeforeUnloadConfirm();
[email protected]3ab9cb82011-06-03 18:02:071799
1800 bool suppress_this_message =
1801 rvh->is_swapped_out() ||
1802 !delegate_ ||
1803 delegate_->ShouldSuppressDialogs();
[email protected]992db4c2011-05-12 15:37:151804 if (suppress_this_message) {
[email protected]4f5ce842011-05-27 19:34:411805 render_view_host()->JavaScriptDialogClosed(reply_msg, true, string16());
[email protected]8f55e802010-12-06 18:11:501806 return;
1807 }
[email protected]3ab9cb82011-06-03 18:02:071808
[email protected]7ab1e7d62009-10-14 23:32:011809 is_showing_before_unload_dialog_ = true;
[email protected]2e5b90c2011-08-16 21:11:551810 dialog_creator_ = delegate_->GetJavaScriptDialogCreator();
1811 dialog_creator_->RunBeforeUnloadDialog(this,
1812 message,
1813 reply_msg);
[email protected]420ae012009-04-24 05:16:321814}
1815
[email protected]420ae012009-04-24 05:16:321816WebPreferences TabContents::GetWebkitPrefs() {
[email protected]447021c2010-09-08 21:29:081817 WebPreferences web_prefs =
[email protected]181a95ee2011-07-12 19:26:361818 content::GetContentClient()->browser()->GetWebkitPrefs(
[email protected]3d7474ff2011-07-27 17:47:371819 render_view_host()->process()->browser_context(), false);
[email protected]447021c2010-09-08 21:29:081820
[email protected]b8299c12011-06-03 19:52:281821 // Force accelerated compositing and 2d canvas off for chrome:, about: and
[email protected]2ef216b2011-08-01 19:25:211822 // chrome-devtools: pages (unless it's specifically allowed).
1823 if ((GetURL().SchemeIs(chrome::kChromeDevToolsScheme) ||
[email protected]30b8e0ee2011-09-12 14:45:571824#if !defined(TOUCH_UI)
1825 // Allow accelerated compositing for keyboard and log in screen.
[email protected]b8299c12011-06-03 19:52:281826 GetURL().SchemeIs(chrome::kChromeUIScheme) ||
[email protected]30b8e0ee2011-09-12 14:45:571827#endif
[email protected]cd4a5bd22011-09-13 06:46:301828 (GetURL().SchemeIs(chrome::kAboutScheme) &&
1829 GetURL().spec() != chrome::kAboutBlankURL)) &&
[email protected]2ef216b2011-08-01 19:25:211830 !web_prefs.allow_webui_compositing) {
[email protected]447021c2010-09-08 21:29:081831 web_prefs.accelerated_compositing_enabled = false;
[email protected]9beff752010-09-22 19:35:431832 web_prefs.accelerated_2d_canvas_enabled = false;
[email protected]447021c2010-09-08 21:29:081833 }
1834
[email protected]39553442010-09-09 00:01:551835#if defined(OS_MACOSX)
1836 // Disable accelerated compositing if IOSurface's are not supported,
1837 // as is the case in 10.5.
1838 if (!IOSurfaceSupport::Initialize())
1839 web_prefs.accelerated_compositing_enabled = false;
1840#endif
1841
[email protected]2f7830b32011-08-26 18:22:251842#if defined(TOUCH_UI)
1843 web_prefs.force_compositing_mode = true;
1844#endif
1845
[email protected]447021c2010-09-08 21:29:081846 return web_prefs;
[email protected]420ae012009-04-24 05:16:321847}
1848
[email protected]7d472472011-01-22 01:30:251849void TabContents::OnUserGesture() {
[email protected]ddb85052011-05-18 14:40:271850 // Notify observers.
1851 FOR_EACH_OBSERVER(TabContentsObserver, observers_, DidGetUserGesture());
[email protected]686493142011-07-15 21:47:221852
1853 ResourceDispatcherHost* rdh =
1854 content::GetContentClient()->browser()->GetResourceDispatcherHost();
1855 if (rdh) // NULL in unittests.
[email protected]8ec98cd22011-08-09 22:31:301856 rdh->OnUserGesture(this);
[email protected]7d472472011-01-22 01:30:251857}
1858
[email protected]fa1cf0b82010-01-15 21:49:441859void TabContents::OnIgnoredUIEvent() {
1860 if (constrained_window_count()) {
1861 ConstrainedWindow* window = *constrained_window_begin();
1862 window->FocusConstrainedWindow();
1863 }
1864}
1865
[email protected]420ae012009-04-24 05:16:321866void TabContents::RendererUnresponsive(RenderViewHost* rvh,
1867 bool is_during_unload) {
[email protected]992db4c2011-05-12 15:37:151868 // Don't show hung renderer dialog for a swapped out RVH.
1869 if (rvh != render_view_host())
1870 return;
1871
[email protected]e5fc1632011-08-08 07:51:531872 // Ignore renderer unresponsive event if debugger is attached to the tab
1873 // since the event may be a result of the renderer sitting on a breakpoint.
1874 // See http://crbug.com/65458
1875 DevToolsManager* devtools_manager = DevToolsManager::GetInstance();
1876 if (devtools_manager &&
1877 devtools_manager->GetDevToolsClientHostFor(rvh) != NULL)
1878 return;
1879
[email protected]420ae012009-04-24 05:16:321880 if (is_during_unload) {
1881 // Hang occurred while firing the beforeunload/unload handler.
1882 // Pretend the handler fired so tab closing continues as if it had.
[email protected]829e7612009-04-25 01:15:111883 rvh->set_sudden_termination_allowed(true);
[email protected]420ae012009-04-24 05:16:321884
1885 if (!render_manager_.ShouldCloseTabOnUnresponsiveRenderer())
1886 return;
1887
1888 // If the tab hangs in the beforeunload/unload handler there's really
1889 // nothing we can do to recover. Pretend the unload listeners have
1890 // all fired and close the tab. If the hang is in the beforeunload handler
1891 // then the user will not have the option of cancelling the close.
1892 Close(rvh);
1893 return;
1894 }
1895
[email protected]55452902011-06-01 21:57:471896 if (!render_view_host() || !render_view_host()->IsRenderViewLive())
1897 return;
1898
1899 if (delegate())
1900 delegate()->RendererUnresponsive(this);
[email protected]420ae012009-04-24 05:16:321901}
1902
1903void TabContents::RendererResponsive(RenderViewHost* render_view_host) {
[email protected]55452902011-06-01 21:57:471904 if (delegate())
1905 delegate()->RendererResponsive(this);
[email protected]420ae012009-04-24 05:16:321906}
1907
1908void TabContents::LoadStateChanged(const GURL& url,
[email protected]9c235f042011-08-10 22:28:211909 const net::LoadStateWithParam& load_state,
[email protected]094e5b22009-09-25 04:23:561910 uint64 upload_position,
1911 uint64 upload_size) {
[email protected]420ae012009-04-24 05:16:321912 load_state_ = load_state;
[email protected]094e5b22009-09-25 04:23:561913 upload_position_ = upload_position;
1914 upload_size_ = upload_size;
[email protected]04866c42011-05-03 20:03:501915 load_state_host_ = net::IDNToUnicode(url.host(),
[email protected]b5cca982011-05-26 04:42:081916 content::GetContentClient()->browser()->GetAcceptLangs(this));
[email protected]9c235f042011-08-10 22:28:211917 if (load_state_.state == net::LOAD_STATE_READING_RESPONSE)
[email protected]420ae012009-04-24 05:16:321918 SetNotWaitingForResponse();
[email protected]f5d978c2011-07-21 14:43:511919 if (IsLoading())
[email protected]c9cd2222009-05-06 05:16:501920 NotifyNavigationStateChanged(INVALIDATE_LOAD | INVALIDATE_TAB);
[email protected]420ae012009-04-24 05:16:321921}
1922
[email protected]7d472472011-01-22 01:30:251923void TabContents::WorkerCrashed() {
[email protected]0dd3a0ab2011-02-18 08:17:441924 if (delegate())
[email protected]55452902011-06-01 21:57:471925 delegate()->WorkerCrashed(this);
[email protected]7d472472011-01-22 01:30:251926}
1927
[email protected]420ae012009-04-24 05:16:321928void TabContents::BeforeUnloadFiredFromRenderManager(
1929 bool proceed,
1930 bool* proceed_to_fire_unload) {
1931 if (delegate())
1932 delegate()->BeforeUnloadFired(this, proceed, proceed_to_fire_unload);
1933}
1934
[email protected]3a3d47472010-07-15 21:03:541935void TabContents::DidStartLoadingFromRenderManager(
1936 RenderViewHost* render_view_host) {
1937 DidStartLoading();
1938}
1939
1940void TabContents::RenderViewGoneFromRenderManager(
1941 RenderViewHost* render_view_host) {
[email protected]443b80e2010-12-14 00:42:231942 DCHECK(crashed_status_ != base::TERMINATION_STATUS_STILL_RUNNING);
1943 RenderViewGone(render_view_host, crashed_status_, crashed_error_code_);
[email protected]3a3d47472010-07-15 21:03:541944}
1945
[email protected]420ae012009-04-24 05:16:321946void TabContents::UpdateRenderViewSizeForRenderManager() {
1947 // TODO(brettw) this is a hack. See TabContentsView::SizeContents.
[email protected]0323ee42010-02-17 22:03:261948 gfx::Size size = view_->GetContainerSize();
1949 // 0x0 isn't a valid window size (minimal window size is 1x1) but it may be
1950 // here during container initialization and normal window size will be set
1951 // later. In case of tab duplication this resizing to 0x0 prevents setting
1952 // normal size later so just ignore it.
1953 if (!size.IsEmpty())
1954 view_->SizeContents(size);
[email protected]420ae012009-04-24 05:16:321955}
1956
[email protected]3a3d47472010-07-15 21:03:541957void TabContents::NotifySwappedFromRenderManager() {
1958 NotifySwapped();
1959}
1960
1961NavigationController& TabContents::GetControllerForRenderManager() {
1962 return controller();
1963}
1964
[email protected]d0980792011-02-13 19:41:401965WebUI* TabContents::CreateWebUIForRenderManager(const GURL& url) {
[email protected]1fd1a502011-03-30 16:55:561966 return content::WebUIFactory::Get()->CreateWebUIForURL(this, url);
[email protected]420ae012009-04-24 05:16:321967}
1968
1969NavigationEntry*
1970TabContents::GetLastCommittedNavigationEntryForRenderManager() {
1971 return controller_.GetLastCommittedEntry();
1972}
1973
1974bool TabContents::CreateRenderViewForRenderManager(
1975 RenderViewHost* render_view_host) {
[email protected]420ae012009-04-24 05:16:321976 RenderWidgetHostView* rwh_view = view_->CreateViewForWidget(render_view_host);
[email protected]1a98a932009-11-17 00:12:521977
[email protected]420ae012009-04-24 05:16:321978 // Now that the RenderView has been created, we need to tell it its size.
1979 rwh_view->SetSize(view_->GetContainerSize());
1980
[email protected]a4127722011-04-27 23:13:521981 if (!render_view_host->CreateRenderView(string16()))
1982 return false;
1983
[email protected]f8233cc2011-05-31 20:24:501984#if defined(OS_LINUX)
1985 // Force a ViewMsg_Resize to be sent, needed to make plugins show up on
1986 // linux. See crbug.com/83941.
1987 if (RenderWidgetHost* render_widget_host = rwh_view->GetRenderWidgetHost())
1988 render_widget_host->WasResized();
1989#endif
1990
[email protected]420ae012009-04-24 05:16:321991 UpdateMaxPageIDIfNecessary(render_view_host->site_instance(),
1992 render_view_host);
1993 return true;
1994}
1995
[email protected]3ab9cb82011-06-03 18:02:071996void TabContents::OnDialogClosed(IPC::Message* reply_msg,
1997 bool success,
1998 const string16& user_input) {
[email protected]beb440c2009-11-06 04:08:541999 if (is_showing_before_unload_dialog_ && !success) {
2000 // If a beforeunload dialog is canceled, we need to stop the throbber from
2001 // spinning, since we forced it to start spinning in Navigate.
2002 DidStopLoading();
2003
2004 tab_close_start_time_ = base::TimeTicks();
2005 }
2006 is_showing_before_unload_dialog_ = false;
[email protected]4f5ce842011-05-27 19:34:412007 render_view_host()->JavaScriptDialogClosed(reply_msg,
2008 success,
[email protected]3ab9cb82011-06-03 18:02:072009 user_input);
[email protected]beb440c2009-11-06 04:08:542010}
2011
[email protected]3ab9cb82011-06-03 18:02:072012gfx::NativeWindow TabContents::GetDialogRootWindow() {
2013 return view_->GetTopLevelNativeWindow();
[email protected]beb440c2009-11-06 04:08:542014}
2015
[email protected]a1e97f02011-06-30 14:04:342016void TabContents::OnDialogShown() {
2017 Activate();
[email protected]3a3d47472010-07-15 21:03:542018}
2019
[email protected]41fc0322009-09-04 22:23:402020void TabContents::set_encoding(const std::string& encoding) {
[email protected]763ec4ca2011-04-29 15:48:122021 encoding_ = content::GetContentClient()->browser()->
2022 GetCanonicalEncodingNameByAliasName(encoding);
[email protected]aacd7cc2009-09-02 14:51:102023}
[email protected]f45d2a72010-03-08 23:28:352024
[email protected]33f74972010-12-08 16:40:362025void TabContents::SwapInRenderViewHost(RenderViewHost* rvh) {
2026 render_manager_.SwapInRenderViewHost(rvh);
2027}
2028
2029void TabContents::CreateViewAndSetSizeForRVH(RenderViewHost* rvh) {
2030 RenderWidgetHostView* rwh_view = view()->CreateViewForWidget(rvh);
2031 rwh_view->SetSize(view()->GetContainerSize());
2032}