blob: 66e6cbcb01414985c5206b9a2694534ddfee0e95 [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]19e81142011-10-03 16:19:4044#include "content/common/content_constants.h"
[email protected]763ec4ca2011-04-29 15:48:1245#include "content/common/content_restriction.h"
[email protected]ce9751942011-09-21 01:57:2446#include "content/common/intents_messages.h"
[email protected]4dd57932011-03-17 06:06:1247#include "content/common/navigation_types.h"
[email protected]7f070d42011-03-09 20:25:3248#include "content/common/notification_service.h"
[email protected]0f012df82011-05-19 14:15:2949#include "content/common/url_constants.h"
[email protected]2c5569662011-03-22 20:45:0250#include "content/common/view_messages.h"
[email protected]d686e812009-06-03 19:10:2951#include "net/base/net_util.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 // Notify any observer that have a reference on this tab contents.
225 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:27226 content::NOTIFICATION_TAB_CONTENTS_DESTROYED,
[email protected]420ae012009-04-24 05:16:32227 Source<TabContents>(this),
228 NotificationService::NoDetails());
229
230 // TODO(brettw) this should be moved to the view.
231#if defined(OS_WIN)
232 // If we still have a window handle, destroy it. GetNativeView can return
233 // NULL if this contents was part of a window that closed.
[email protected]50bd6452010-11-27 19:39:42234 if (GetNativeView()) {
235 RenderViewHost* host = render_view_host();
[email protected]b9a1fb42011-04-12 23:16:44236 if (host && host->view())
[email protected]50bd6452010-11-27 19:39:42237 host->view()->WillWmDestroy();
[email protected]50bd6452010-11-27 19:39:42238 }
[email protected]420ae012009-04-24 05:16:32239#endif
[email protected]7ab1e7d62009-10-14 23:32:01240
241 // OnCloseStarted isn't called in unit tests.
242 if (!tab_close_start_time_.is_null()) {
243 UMA_HISTOGRAM_TIMES("Tab.Close",
244 base::TimeTicks::Now() - tab_close_start_time_);
245 }
[email protected]b5a1d11c2011-02-17 03:09:42246
[email protected]07d490bc2011-03-07 17:05:26247 FOR_EACH_OBSERVER(TabContentsObserver, observers_, TabContentsDestroyed());
[email protected]232a5812011-03-04 22:42:08248
[email protected]1de2b8b2011-06-29 19:38:46249 set_delegate(NULL);
250}
251
252// TODO(cbentzel): Either remove the debugging code, or rename to SetDelegate.
253void TabContents::set_delegate(TabContentsDelegate* delegate) {
254 if (delegate == delegate_)
255 return;
256 if (delegate_)
257 delegate_->Detach(this);
258 delegate_ = delegate;
259 if (delegate_)
260 delegate_->Attach(this);
[email protected]b5a1d11c2011-02-17 03:09:42261}
262
[email protected]724159a2010-12-30 01:11:18263bool TabContents::OnMessageReceived(const IPC::Message& message) {
[email protected]f82d57b52011-04-27 19:13:17264 if (web_ui() && web_ui()->OnMessageReceived(message))
265 return true;
266
[email protected]585b30362011-01-28 02:30:17267 ObserverListBase<TabContentsObserver>::Iterator it(observers_);
268 TabContentsObserver* observer;
[email protected]0f180ee2011-01-21 18:23:10269 while ((observer = it.GetNext()) != NULL)
270 if (observer->OnMessageReceived(message))
[email protected]403415a2011-01-10 18:57:53271 return true;
[email protected]403415a2011-01-10 18:57:53272
[email protected]724159a2010-12-30 01:11:18273 bool handled = true;
274 bool message_is_ok = true;
275 IPC_BEGIN_MESSAGE_MAP_EX(TabContents, message, message_is_ok)
[email protected]ce9751942011-09-21 01:57:24276 IPC_MESSAGE_HANDLER(IntentsHostMsg_WebIntentDispatch,
277 OnWebIntentDispatch)
[email protected]724159a2010-12-30 01:11:18278 IPC_MESSAGE_HANDLER(ViewHostMsg_DidStartProvisionalLoadForFrame,
279 OnDidStartProvisionalLoadForFrame)
280 IPC_MESSAGE_HANDLER(ViewHostMsg_DidRedirectProvisionalLoad,
281 OnDidRedirectProvisionalLoad)
282 IPC_MESSAGE_HANDLER(ViewHostMsg_DidFailProvisionalLoadWithError,
283 OnDidFailProvisionalLoadWithError)
284 IPC_MESSAGE_HANDLER(ViewHostMsg_DidLoadResourceFromMemoryCache,
285 OnDidLoadResourceFromMemoryCache)
286 IPC_MESSAGE_HANDLER(ViewHostMsg_DidDisplayInsecureContent,
287 OnDidDisplayInsecureContent)
288 IPC_MESSAGE_HANDLER(ViewHostMsg_DidRunInsecureContent,
289 OnDidRunInsecureContent)
290 IPC_MESSAGE_HANDLER(ViewHostMsg_DocumentLoadedInFrame,
291 OnDocumentLoadedInFrame)
292 IPC_MESSAGE_HANDLER(ViewHostMsg_DidFinishLoad, OnDidFinishLoad)
[email protected]c8f73ab2011-01-22 00:05:17293 IPC_MESSAGE_HANDLER(ViewHostMsg_UpdateContentRestrictions,
294 OnUpdateContentRestrictions)
[email protected]7d472472011-01-22 01:30:25295 IPC_MESSAGE_HANDLER(ViewHostMsg_GoToEntryAtOffset, OnGoToEntryAtOffset)
[email protected]216813952011-05-19 22:21:26296 IPC_MESSAGE_HANDLER(ViewHostMsg_UpdateZoomLimits, OnUpdateZoomLimits)
297 IPC_MESSAGE_HANDLER(ViewHostMsg_FocusedNodeChanged, OnFocusedNodeChanged)
[email protected]c7dd2f62011-07-18 15:57:59298 IPC_MESSAGE_HANDLER(ViewHostMsg_SaveURLAs, OnSaveURL)
[email protected]3a29a6e2011-08-24 18:26:21299 IPC_MESSAGE_HANDLER(ViewHostMsg_EnumerateDirectory, OnEnumerateDirectory)
[email protected]7d189022011-08-25 22:54:20300 IPC_MESSAGE_HANDLER(ViewHostMsg_JSOutOfMemory, OnJSOutOfMemory)
301 IPC_MESSAGE_HANDLER(ViewHostMsg_RegisterProtocolHandler,
302 OnRegisterProtocolHandler)
303 IPC_MESSAGE_HANDLER(ViewHostMsg_RegisterIntentHandler,
304 OnRegisterIntentHandler)
[email protected]b888919c2011-09-02 00:32:16305 IPC_MESSAGE_HANDLER(ViewHostMsg_Find_Reply, OnFindReply)
[email protected]d952a052011-09-06 18:42:45306 IPC_MESSAGE_HANDLER(ViewHostMsg_CrashedPlugin, OnCrashedPlugin)
[email protected]7fc4bbb2011-09-08 21:23:10307 IPC_MESSAGE_HANDLER(ViewHostMsg_AppCacheAccessed, OnAppCacheAccessed)
[email protected]724159a2010-12-30 01:11:18308 IPC_MESSAGE_UNHANDLED(handled = false)
309 IPC_END_MESSAGE_MAP_EX()
310
311 if (!message_is_ok) {
312 UserMetrics::RecordAction(UserMetricsAction("BadMessageTerminate_RVD"));
313 GetRenderProcessHost()->ReceivedBadMessage();
314 }
315
316 return handled;
317}
318
[email protected]6c2e472f2011-08-24 23:26:18319void TabContents::RunFileChooser(
320 RenderViewHost* render_view_host,
321 const ViewHostMsg_RunFileChooser_Params& params) {
322 delegate()->RunFileChooser(this, params);
323}
324
[email protected]8cb5d5b2010-02-09 11:36:16325RenderProcessHost* TabContents::GetRenderProcessHost() const {
[email protected]4d0df262011-10-03 20:05:03326 if (render_manager_.current_host())
327 return render_manager_.current_host()->process();
328 else
329 return NULL;
[email protected]8cb5d5b2010-02-09 11:36:16330}
331
[email protected]d5f942ba2008-09-26 19:30:34332const GURL& TabContents::GetURL() const {
333 // We may not have a navigation entry yet
[email protected]ce3fa3c2009-04-20 19:55:57334 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]ebe89e062009-08-13 23:16:54335 return entry ? entry->virtual_url() : GURL::EmptyGURL();
[email protected]d5f942ba2008-09-26 19:30:34336}
337
[email protected]96d185d2009-04-24 03:28:54338const string16& TabContents::GetTitle() const {
[email protected]4c6092c5b2009-06-06 00:23:55339 // Transient entries take precedence. They are used for interstitial pages
340 // that are shown on top of existing pages.
341 NavigationEntry* entry = controller_.GetTransientEntry();
[email protected]b5cca982011-05-26 04:42:08342 std::string accept_languages =
343 content::GetContentClient()->browser()->GetAcceptLangs(this);
[email protected]45d0ef7f2011-01-05 13:46:23344 if (entry) {
[email protected]b5cca982011-05-26 04:42:08345 return entry->GetTitleForDisplay(accept_languages);
[email protected]45d0ef7f2011-01-05 13:46:23346 }
[email protected]7ade2732011-02-10 00:13:58347 WebUI* our_web_ui = render_manager_.pending_web_ui() ?
348 render_manager_.pending_web_ui() : render_manager_.web_ui();
349 if (our_web_ui) {
[email protected]96d185d2009-04-24 03:28:54350 // Don't override the title in view source mode.
[email protected]4c6092c5b2009-06-06 00:23:55351 entry = controller_.GetActiveEntry();
[email protected]96d185d2009-04-24 03:28:54352 if (!(entry && entry->IsViewSourceMode())) {
[email protected]e0112912011-02-02 22:54:35353 // Give the Web UI the chance to override our title.
[email protected]7ade2732011-02-10 00:13:58354 const string16& title = our_web_ui->overridden_title();
[email protected]96d185d2009-04-24 03:28:54355 if (!title.empty())
356 return title;
357 }
358 }
359
360 // We use the title for the last committed entry rather than a pending
361 // navigation entry. For example, when the user types in a URL, we want to
362 // keep the old page's title until the new load has committed and we get a new
363 // title.
[email protected]96d185d2009-04-24 03:28:54364 entry = controller_.GetLastCommittedEntry();
[email protected]45d0ef7f2011-01-05 13:46:23365 if (entry) {
[email protected]b5cca982011-05-26 04:42:08366 return entry->GetTitleForDisplay(accept_languages);
[email protected]45d0ef7f2011-01-05 13:46:23367 }
[email protected]987fc3a2011-05-26 14:18:09368
369 // |page_title_when_no_navigation_entry_| is finally used
370 // if no title cannot be retrieved.
371 return page_title_when_no_navigation_entry_;
[email protected]96d185d2009-04-24 03:28:54372}
373
[email protected]d5f942ba2008-09-26 19:30:34374int32 TabContents::GetMaxPageID() {
375 if (GetSiteInstance())
376 return GetSiteInstance()->max_page_id();
377 else
378 return max_page_id_;
379}
380
381void TabContents::UpdateMaxPageID(int32 page_id) {
382 // Ensure both the SiteInstance and RenderProcessHost update their max page
[email protected]57c6a652009-05-04 07:58:34383 // IDs in sync. Only TabContents will also have site instances, except during
[email protected]d5f942ba2008-09-26 19:30:34384 // testing.
385 if (GetSiteInstance())
386 GetSiteInstance()->UpdateMaxPageID(page_id);
[email protected]8cb5d5b2010-02-09 11:36:16387 GetRenderProcessHost()->UpdateMaxPageID(page_id);
[email protected]d5f942ba2008-09-26 19:30:34388}
389
[email protected]96d185d2009-04-24 03:28:54390SiteInstance* TabContents::GetSiteInstance() const {
391 return render_manager_.current_host()->site_instance();
392}
393
[email protected]77362eb2011-08-01 17:18:38394SiteInstance* TabContents::GetPendingSiteInstance() const {
395 RenderViewHost* dest_rvh = render_manager_.pending_render_view_host() ?
396 render_manager_.pending_render_view_host() :
397 render_manager_.current_host();
398 return dest_rvh->site_instance();
399}
400
[email protected]96d185d2009-04-24 03:28:54401bool TabContents::ShouldDisplayURL() {
[email protected]8ab24cc2009-11-13 20:56:15402 // Don't hide the url in view source mode and with interstitials.
[email protected]96d185d2009-04-24 03:28:54403 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]8ab24cc2009-11-13 20:56:15404 if (entry && (entry->IsViewSourceMode() ||
[email protected]cccd3762010-11-12 18:40:01405 entry->page_type() == INTERSTITIAL_PAGE)) {
[email protected]96d185d2009-04-24 03:28:54406 return true;
[email protected]8ab24cc2009-11-13 20:56:15407 }
[email protected]83e3895b2009-06-11 00:07:16408
[email protected]2b9a06402011-02-16 04:57:10409 // We always display the URL for non-WebUI URLs to prevent spoofing.
[email protected]1fd1a502011-03-30 16:55:56410 if (entry && !content::WebUIFactory::Get()->HasWebUIScheme(entry->url()))
[email protected]2b9a06402011-02-16 04:57:10411 return true;
412
[email protected]d0980792011-02-13 19:41:40413 WebUI* web_ui = GetWebUIForCurrentState();
[email protected]7ade2732011-02-10 00:13:58414 if (web_ui)
415 return !web_ui->should_hide_url();
[email protected]96d185d2009-04-24 03:28:54416 return true;
417}
418
[email protected]f5d978c2011-07-21 14:43:51419bool TabContents::IsLoading() const {
420 return is_loading_ || (web_ui() && web_ui()->IsLoading());
421}
422
[email protected]585b30362011-01-28 02:30:17423void TabContents::AddObserver(TabContentsObserver* observer) {
424 observers_.AddObserver(observer);
[email protected]3c9e1872010-11-18 16:17:49425}
426
[email protected]585b30362011-01-28 02:30:17427void TabContents::RemoveObserver(TabContentsObserver* observer) {
428 observers_.RemoveObserver(observer);
[email protected]3c9e1872010-11-18 16:17:49429}
430
[email protected]443b80e2010-12-14 00:42:23431void TabContents::SetIsCrashed(base::TerminationStatus status, int error_code) {
432 if (status == crashed_status_)
[email protected]d5f942ba2008-09-26 19:30:34433 return;
434
[email protected]443b80e2010-12-14 00:42:23435 crashed_status_ = status;
436 crashed_error_code_ = error_code;
[email protected]c9cd2222009-05-06 05:16:50437 NotifyNavigationStateChanged(INVALIDATE_TAB);
[email protected]d5f942ba2008-09-26 19:30:34438}
439
440void TabContents::NotifyNavigationStateChanged(unsigned changed_flags) {
441 if (delegate_)
442 delegate_->NavigationStateChanged(this, changed_flags);
443}
444
[email protected]96d185d2009-04-24 03:28:54445void TabContents::DidBecomeSelected() {
446 controller_.SetActive(true);
[email protected]8cb5d5b2010-02-09 11:36:16447 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
448 if (rwhv) {
449 rwhv->DidBecomeSelected();
[email protected]789e9152009-08-04 21:59:43450#if defined(OS_MACOSX)
[email protected]8cb5d5b2010-02-09 11:36:16451 rwhv->SetActive(true);
[email protected]789e9152009-08-04 21:59:43452#endif
453 }
[email protected]96d185d2009-04-24 03:28:54454
[email protected]5ac20162010-11-24 23:33:11455 last_selected_time_ = base::TimeTicks::Now();
[email protected]3e69bc82011-05-26 23:22:38456
457 FOR_EACH_OBSERVER(TabContentsObserver, observers_, DidBecomeSelected());
[email protected]96d185d2009-04-24 03:28:54458}
459
460void TabContents::WasHidden() {
461 if (!capturing_contents()) {
462 // |render_view_host()| can be NULL if the user middle clicks a link to open
463 // a tab in then background, then closes the tab before selecting it. This
464 // is because closing the tab calls TabContents::Destroy(), which removes
465 // the |render_view_host()|; then when we actually destroy the window,
466 // OnWindowPosChanged() notices and calls HideContents() (which calls us).
[email protected]8cb5d5b2010-02-09 11:36:16467 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
468 if (rwhv)
469 rwhv->WasHidden();
[email protected]96d185d2009-04-24 03:28:54470 }
471
472 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:27473 content::NOTIFICATION_TAB_CONTENTS_HIDDEN,
[email protected]96d185d2009-04-24 03:28:54474 Source<TabContents>(this),
475 NotificationService::NoDetails());
476}
477
[email protected]d5f942ba2008-09-26 19:30:34478void TabContents::Activate() {
479 if (delegate_)
480 delegate_->ActivateContents(this);
481}
482
[email protected]ea42e7782010-08-23 23:58:12483void TabContents::Deactivate() {
484 if (delegate_)
485 delegate_->DeactivateContents(this);
486}
487
[email protected]63954792011-07-11 04:17:48488void TabContents::LostCapture() {
489 if (delegate_)
490 delegate_->LostCapture();
491}
492
493bool TabContents::PreHandleKeyboardEvent(const NativeWebKeyboardEvent& event,
494 bool* is_keyboard_shortcut) {
495 return delegate_ &&
496 delegate_->PreHandleKeyboardEvent(event, is_keyboard_shortcut);
497}
498
499void TabContents::HandleKeyboardEvent(const NativeWebKeyboardEvent& event) {
500 if (delegate_)
501 delegate_->HandleKeyboardEvent(event);
502}
503
504void TabContents::HandleMouseUp() {
505 if (delegate_)
506 delegate_->HandleMouseUp();
507}
508
509void TabContents::HandleMouseActivate() {
510 if (delegate_)
511 delegate_->HandleMouseActivate();
512}
513
[email protected]8a5e0ca2011-08-25 06:30:47514void TabContents::ToggleFullscreenMode(bool enter_fullscreen) {
515 if (delegate_)
516 delegate_->ToggleFullscreenModeForTab(this, enter_fullscreen);
517}
518
[email protected]5d5f7af2011-10-01 01:38:12519bool TabContents::IsFullscreenForCurrentTab() const {
520 return delegate_ ? delegate_->IsFullscreenForTab(this) : false;
521}
522
[email protected]0548c5352011-09-07 00:33:33523void TabContents::UpdatePreferredSize(const gfx::Size& pref_size) {
524 if (delegate_)
525 delegate_->UpdatePreferredSize(this, pref_size);
526}
527
[email protected]96d185d2009-04-24 03:28:54528void TabContents::ShowContents() {
[email protected]8cb5d5b2010-02-09 11:36:16529 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
530 if (rwhv)
531 rwhv->DidBecomeSelected();
[email protected]96d185d2009-04-24 03:28:54532}
533
534void TabContents::HideContents() {
535 // TODO(pkasting): http://b/1239839 Right now we purposefully don't call
536 // our superclass HideContents(), because some callers want to be very picky
537 // about the order in which these get called. In addition to making the code
538 // here practically impossible to understand, this also means we end up
539 // calling TabContents::WasHidden() twice if callers call both versions of
[email protected]57c6a652009-05-04 07:58:34540 // HideContents() on a TabContents.
[email protected]96d185d2009-04-24 03:28:54541 WasHidden();
542}
543
[email protected]5aab5e22010-12-08 22:13:29544bool TabContents::NeedToFireBeforeUnload() {
545 // TODO(creis): Should we fire even for interstitial pages?
546 return notify_disconnection() &&
547 !showing_interstitial_page() &&
548 !render_view_host()->SuddenTerminationAllowed();
549}
550
[email protected]00c37fc2011-08-02 00:22:50551// TODO(adriansc): Remove this method once refactoring changed all call sites.
[email protected]992e4542011-07-20 23:09:25552TabContents* TabContents::OpenURL(const GURL& url,
553 const GURL& referrer,
554 WindowOpenDisposition disposition,
555 PageTransition::Type transition) {
[email protected]00c37fc2011-08-02 00:22:50556 return OpenURL(OpenURLParams(url, referrer, disposition, transition));
557}
558
559TabContents* TabContents::OpenURL(const OpenURLParams& params) {
[email protected]1c642b52011-04-15 09:05:49560 if (delegate_) {
[email protected]00c37fc2011-08-02 00:22:50561 TabContents* new_contents = delegate_->OpenURLFromTab(this, params);
[email protected]1c642b52011-04-15 09:05:49562 // Notify observers.
563 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
[email protected]00c37fc2011-08-02 00:22:50564 DidOpenURL(params.url, params.referrer,
565 params.disposition, params.transition));
[email protected]992e4542011-07-20 23:09:25566 return new_contents;
[email protected]1c642b52011-04-15 09:05:49567 }
[email protected]992e4542011-07-20 23:09:25568 return NULL;
[email protected]d5f942ba2008-09-26 19:30:34569}
570
[email protected]1ccb3568d2010-02-19 10:51:16571bool TabContents::NavigateToPendingEntry(
572 NavigationController::ReloadType reload_type) {
[email protected]876bc832010-09-07 16:29:54573 return NavigateToEntry(*controller_.pending_entry(), reload_type);
574}
[email protected]96d185d2009-04-24 03:28:54575
[email protected]876bc832010-09-07 16:29:54576bool TabContents::NavigateToEntry(
577 const NavigationEntry& entry,
578 NavigationController::ReloadType reload_type) {
[email protected]96d185d2009-04-24 03:28:54579 RenderViewHost* dest_render_view_host = render_manager_.Navigate(entry);
580 if (!dest_render_view_host)
581 return false; // Unable to create the desired render view host.
582
[email protected]19e81142011-10-03 16:19:40583 // The renderer will reject IPC messages with URLs longer than
584 // this limit, so don't attempt to navigate with a longer URL.
585 if (entry.url().spec().size() > content::kMaxURLChars)
586 return false;
587
[email protected]80a8fad2011-01-29 04:02:38588 // For security, we should never send non-Web-UI URLs to a Web UI renderer.
[email protected]05fdd492010-11-15 17:52:07589 // Double check that here.
[email protected]54ec6472010-04-09 19:39:58590 int enabled_bindings = dest_render_view_host->enabled_bindings();
[email protected]1fd1a502011-03-30 16:55:56591 bool is_allowed_in_web_ui_renderer = content::GetContentClient()->
[email protected]3d7474ff2011-07-27 17:47:37592 browser()->GetWebUIFactory()->IsURLAcceptableForWebUI(browser_context(),
[email protected]1fd1a502011-03-30 16:55:56593 entry.url());
[email protected]c09163a2011-02-15 00:05:55594 CHECK(!BindingsPolicy::is_web_ui_enabled(enabled_bindings) ||
595 is_allowed_in_web_ui_renderer);
[email protected]54ec6472010-04-09 19:39:58596
[email protected]96d185d2009-04-24 03:28:54597 // Tell DevTools agent that it is attached prior to the navigation.
[email protected]d9f9b792009-06-24 13:17:12598 DevToolsManager* devtools_manager = DevToolsManager::GetInstance();
[email protected]7e8e6b62009-05-08 11:28:32599 if (devtools_manager) { // NULL in unit tests.
[email protected]3cc72b12010-03-18 23:03:00600 devtools_manager->OnNavigatingToPendingEntry(render_view_host(),
601 dest_render_view_host,
602 entry.url());
[email protected]7e8e6b62009-05-08 11:28:32603 }
[email protected]96d185d2009-04-24 03:28:54604
605 // Used for page load time metrics.
606 current_load_start_ = base::TimeTicks::Now();
607
608 // Navigate in the desired RenderViewHost.
[email protected]056de2d2009-06-26 16:41:34609 ViewMsg_Navigate_Params navigate_params;
[email protected]6c6b02d2011-09-02 03:36:47610 MakeNavigateParams(entry, controller_, delegate_, reload_type,
611 &navigate_params);
[email protected]056de2d2009-06-26 16:41:34612 dest_render_view_host->Navigate(navigate_params);
[email protected]96d185d2009-04-24 03:28:54613
614 if (entry.page_id() == -1) {
615 // HACK!! This code suppresses javascript: URLs from being added to
616 // session history, which is what we want to do for javascript: URLs that
617 // do not generate content. What we really need is a message from the
618 // renderer telling us that a new page was not created. The same message
619 // could be used for mailto: URLs and the like.
620 if (entry.url().SchemeIs(chrome::kJavaScriptScheme))
621 return false;
622 }
623
[email protected]3c9e1872010-11-18 16:17:49624 // Notify observers about navigation.
[email protected]b375c5d2011-05-03 21:15:04625 FOR_EACH_OBSERVER(TabContentsObserver,
626 observers_,
627 NavigateToPendingEntry(entry.url(), reload_type));
[email protected]96d185d2009-04-24 03:28:54628
[email protected]09b29342011-06-24 19:18:48629 if (delegate_)
630 delegate_->DidNavigateToPendingEntry(this);
631
[email protected]d5f942ba2008-09-26 19:30:34632 return true;
633}
634
[email protected]9e1ad4b2011-08-14 16:49:19635void TabContents::SetHistoryLengthAndPrune(const SiteInstance* site_instance,
636 int history_length,
637 int32 minimum_page_id) {
[email protected]5229253b2011-08-16 14:36:41638 // SetHistoryLengthAndPrune doesn't work when there are pending cross-site
639 // navigations. Callers should ensure that this is the case.
[email protected]9e1ad4b2011-08-14 16:49:19640 if (render_manager_.pending_render_view_host()) {
641 NOTREACHED();
[email protected]796931a92011-08-10 01:32:14642 return;
[email protected]9e1ad4b2011-08-14 16:49:19643 }
644 RenderViewHost* rvh = render_view_host();
645 if (!rvh) {
646 NOTREACHED();
647 return;
648 }
649 if (site_instance && rvh->site_instance() != site_instance) {
650 NOTREACHED();
651 return;
652 }
653 rvh->Send(new ViewMsg_SetHistoryLengthAndPrune(rvh->routing_id(),
654 history_length,
655 minimum_page_id));
[email protected]796931a92011-08-10 01:32:14656}
657
[email protected]96d185d2009-04-24 03:28:54658void TabContents::Stop() {
659 render_manager_.Stop();
[email protected]2e3f4572011-03-25 19:24:47660 FOR_EACH_OBSERVER(TabContentsObserver, observers_, StopNavigation());
[email protected]96d185d2009-04-24 03:28:54661}
662
[email protected]96d185d2009-04-24 03:28:54663TabContents* TabContents::Clone() {
664 // We create a new SiteInstance so that the new tab won't share processes
665 // with the old one. This can be changed in the future if we need it to share
666 // processes for some reason.
[email protected]3d7474ff2011-07-27 17:47:37667 TabContents* tc = new TabContents(
668 browser_context(),
669 SiteInstance::CreateSiteInstance(browser_context()),
670 MSG_ROUTING_NONE, this, NULL);
[email protected]96d185d2009-04-24 03:28:54671 tc->controller().CopyStateFrom(controller_);
672 return tc;
673}
674
[email protected]4d677202009-07-19 07:37:12675void TabContents::ShowPageInfo(const GURL& url,
676 const NavigationEntry::SSLStatus& ssl,
677 bool show_history) {
678 if (!delegate_)
679 return;
680
[email protected]3d7474ff2011-07-27 17:47:37681 delegate_->ShowPageInfo(browser_context(), url, ssl, show_history);
[email protected]4d677202009-07-19 07:37:12682}
683
[email protected]e7cfdbd2011-04-22 14:41:37684void TabContents::AddNewContents(TabContents* new_contents,
685 WindowOpenDisposition disposition,
686 const gfx::Rect& initial_pos,
687 bool user_gesture) {
initial.commit09911bf2008-07-26 23:55:29688 if (!delegate_)
689 return;
690
[email protected]e7cfdbd2011-04-22 14:41:37691 delegate_->AddNewContents(this, new_contents, disposition, initial_pos,
692 user_gesture);
[email protected]96d185d2009-04-24 03:28:54693}
694
[email protected]5c9e97a2009-09-09 23:48:30695gfx::NativeView TabContents::GetContentNativeView() const {
[email protected]96d185d2009-04-24 03:28:54696 return view_->GetContentNativeView();
697}
698
699gfx::NativeView TabContents::GetNativeView() const {
700 return view_->GetNativeView();
701}
702
703void TabContents::GetContainerBounds(gfx::Rect *out) const {
704 view_->GetContainerBounds(out);
705}
706
707void TabContents::Focus() {
708 view_->Focus();
709}
710
[email protected]90daadb42009-06-08 21:27:28711void TabContents::FocusThroughTabTraversal(bool reverse) {
[email protected]7e383692009-06-12 19:14:54712 if (showing_interstitial_page()) {
713 render_manager_.interstitial_page()->FocusThroughTabTraversal(reverse);
714 return;
715 }
[email protected]96d185d2009-04-24 03:28:54716 render_view_host()->SetInitialFocus(reverse);
717}
718
719bool TabContents::FocusLocationBarByDefault() {
[email protected]d0980792011-02-13 19:41:40720 WebUI* web_ui = GetWebUIForCurrentState();
[email protected]7ade2732011-02-10 00:13:58721 if (web_ui)
722 return web_ui->focus_location_bar_by_default();
[email protected]3e3f0eb2009-06-22 18:33:43723 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]e0d481582009-09-15 21:06:25724 if (entry && entry->url() == GURL(chrome::kAboutBlankURL))
[email protected]3e3f0eb2009-06-22 18:33:43725 return true;
[email protected]96d185d2009-04-24 03:28:54726 return false;
727}
728
[email protected]a26dc362010-04-23 01:48:58729void TabContents::SetFocusToLocationBar(bool select_all) {
[email protected]a11aaf22010-03-30 00:03:38730 if (delegate())
[email protected]a26dc362010-04-23 01:48:58731 delegate()->SetFocusToLocationBar(select_all);
[email protected]a11aaf22010-03-30 00:03:38732}
733
[email protected]686493142011-07-15 21:47:22734bool TabContents::CanDownload(int request_id) {
735 TabContentsDelegate* d = delegate();
736 if (d)
737 return d->CanDownload(this, request_id);
738 return true;
739}
740
741void TabContents::OnStartDownload(DownloadItem* download) {
742 TabContentsDelegate* d = delegate();
743 if (d)
744 d->OnStartDownload(this, download);
745}
746
[email protected]c7dd2f62011-07-18 15:57:59747void TabContents::OnSavePage() {
748 // If we can not save the page, try to download it.
749 if (!SavePackage::IsSavableContents(contents_mime_type())) {
[email protected]3d7474ff2011-07-27 17:47:37750 DownloadManager* dlm = browser_context()->GetDownloadManager();
[email protected]c7dd2f62011-07-18 15:57:59751 const GURL& current_page_url = GetURL();
752 if (dlm && current_page_url.is_valid()) {
753 dlm->DownloadUrl(current_page_url, GURL(), "", this);
[email protected]8bd9e562011-08-16 23:55:46754 download_stats::RecordDownloadCount(
755 download_stats::INITIATED_BY_SAVE_PACKAGE_FAILURE_COUNT);
[email protected]c7dd2f62011-07-18 15:57:59756 return;
757 }
758 }
759
760 Stop();
761
762 // Create the save package and possibly prompt the user for the name to save
763 // the page as. The user prompt is an asynchronous operation that runs on
764 // another thread.
765 save_package_ = new SavePackage(this);
766 save_package_->GetSaveInfo();
767}
768
769// Used in automated testing to bypass prompting the user for file names.
770// Instead, the names and paths are hard coded rather than running them through
771// file name sanitation and extension / mime checking.
772bool TabContents::SavePage(const FilePath& main_file, const FilePath& dir_path,
773 SavePackage::SavePackageType save_type) {
774 // Stop the page from navigating.
775 Stop();
776
777 save_package_ = new SavePackage(this, save_type, main_file, dir_path);
778 return save_package_->Init();
779}
780
781void TabContents::OnSaveURL(const GURL& url) {
[email protected]3d7474ff2011-07-27 17:47:37782 DownloadManager* dlm = browser_context()->GetDownloadManager();
[email protected]c7dd2f62011-07-18 15:57:59783 dlm->DownloadUrl(url, GetURL(), "", this);
784}
785
[email protected]420ae012009-04-24 05:16:32786bool TabContents::IsActiveEntry(int32 page_id) {
787 NavigationEntry* active_entry = controller_.GetActiveEntry();
788 return (active_entry != NULL &&
789 active_entry->site_instance() == GetSiteInstance() &&
790 active_entry->page_id() == page_id);
791}
792
[email protected]b2fe07d12010-02-09 14:38:08793void TabContents::SetOverrideEncoding(const std::string& encoding) {
[email protected]8cb5d5b2010-02-09 11:36:16794 set_encoding(encoding);
[email protected]216813952011-05-19 22:21:26795 render_view_host()->Send(new ViewMsg_SetPageEncoding(
796 render_view_host()->routing_id(), encoding));
[email protected]8cb5d5b2010-02-09 11:36:16797}
798
[email protected]b2fe07d12010-02-09 14:38:08799void TabContents::ResetOverrideEncoding() {
[email protected]8cb5d5b2010-02-09 11:36:16800 reset_encoding();
[email protected]216813952011-05-19 22:21:26801 render_view_host()->Send(new ViewMsg_ResetPageEncodingToDefault(
802 render_view_host()->routing_id()));
[email protected]8cb5d5b2010-02-09 11:36:16803}
804
[email protected]7ab1e7d62009-10-14 23:32:01805void TabContents::OnCloseStarted() {
806 if (tab_close_start_time_.is_null())
807 tab_close_start_time_ = base::TimeTicks::Now();
808}
809
[email protected]46624bf2010-06-09 16:04:19810bool TabContents::ShouldAcceptDragAndDrop() const {
811#if defined(OS_CHROMEOS)
812 // ChromeOS panels (pop-ups) do not take drag-n-drop.
813 // See http://crosbug.com/2413
[email protected]b35b26b32011-05-05 20:35:14814 if (delegate() && delegate()->IsPopupOrPanel(this))
[email protected]b83af492010-10-09 03:21:20815 return false;
816 return true;
[email protected]46624bf2010-06-09 16:04:19817#else
818 return true;
819#endif
820}
821
[email protected]7813bd72011-02-05 02:19:34822void TabContents::SystemDragEnded() {
823 if (render_view_host())
824 render_view_host()->DragSourceSystemDragEnded();
825 if (delegate())
826 delegate()->DragEnded();
827}
828
[email protected]d0b8d092010-10-25 04:05:17829double TabContents::GetZoomLevel() const {
[email protected]3d7474ff2011-07-27 17:47:37830 HostZoomMap* zoom_map = browser_context()->GetHostZoomMap();
[email protected]b75b8292010-10-01 07:28:25831 if (!zoom_map)
[email protected]d0b8d092010-10-25 04:05:17832 return 0;
[email protected]b75b8292010-10-01 07:28:25833
834 double zoom_level;
835 if (temporary_zoom_settings_) {
836 zoom_level = zoom_map->GetTemporaryZoomLevel(
837 GetRenderProcessHost()->id(), render_view_host()->routing_id());
838 } else {
[email protected]13ffa32e2011-05-27 16:37:01839 zoom_level = zoom_map->GetZoomLevel(net::GetHostOrSpecFromURL(GetURL()));
[email protected]b75b8292010-10-01 07:28:25840 }
[email protected]d0b8d092010-10-25 04:05:17841 return zoom_level;
842}
[email protected]b75b8292010-10-01 07:28:25843
[email protected]d0b8d092010-10-25 04:05:17844int TabContents::GetZoomPercent(bool* enable_increment,
845 bool* enable_decrement) {
846 *enable_decrement = *enable_increment = false;
[email protected]b75b8292010-10-01 07:28:25847 int percent = static_cast<int>(
[email protected]d0b8d092010-10-25 04:05:17848 WebKit::WebView::zoomLevelToZoomFactor(GetZoomLevel()) * 100);
[email protected]b75b8292010-10-01 07:28:25849 *enable_decrement = percent > minimum_zoom_percent_;
850 *enable_increment = percent < maximum_zoom_percent_;
851 return percent;
852}
853
[email protected]3c733bde2010-12-21 19:56:31854void TabContents::ViewSource() {
[email protected]1788e772010-12-15 16:40:50855 if (!delegate_)
856 return;
857
858 NavigationEntry* active_entry = controller().GetActiveEntry();
859 if (!active_entry)
860 return;
861
862 delegate_->ViewSourceForTab(this, active_entry->url());
[email protected]77d8d622010-12-15 10:30:12863}
864
[email protected]932b7a12011-03-09 12:50:27865void TabContents::ViewFrameSource(const GURL& url,
866 const std::string& content_state) {
867 if (!delegate_)
868 return;
869
870 delegate_->ViewSourceForFrame(this, url, content_state);
871}
872
[email protected]c40d6232011-03-25 00:16:21873void TabContents::SetContentRestrictions(int restrictions) {
874 content_restrictions_ = restrictions;
875 delegate()->ContentRestrictionsChanged(this);
876}
877
[email protected]724159a2010-12-30 01:11:18878void TabContents::OnDidStartProvisionalLoadForFrame(int64 frame_id,
879 bool is_main_frame,
[email protected]eacb080b2011-05-22 19:40:26880 bool has_opener_set,
[email protected]724159a2010-12-30 01:11:18881 const GURL& url) {
882 bool is_error_page = (url.spec() == chrome::kUnreachableWebDataURL);
883 GURL validated_url(url);
884 render_view_host()->FilterURL(ChildProcessSecurityPolicy::GetInstance(),
885 GetRenderProcessHost()->id(), &validated_url);
886
[email protected]8093a542011-05-13 07:29:32887 RenderViewHost* rvh =
888 render_manager_.pending_render_view_host() ?
889 render_manager_.pending_render_view_host() : render_view_host();
[email protected]0d60f0192011-04-14 12:40:10890 // Notify observers about the start of the provisional load.
891 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
892 DidStartProvisionalLoadForFrame(frame_id, is_main_frame,
[email protected]8093a542011-05-13 07:29:32893 validated_url, is_error_page, rvh));
[email protected]0d60f0192011-04-14 12:40:10894
[email protected]724159a2010-12-30 01:11:18895 if (is_main_frame) {
[email protected]4850a7f2011-03-08 23:36:59896 // Notify observers about the provisional change in the main frame URL.
897 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
[email protected]eacb080b2011-05-22 19:40:26898 ProvisionalChangeToMainFrameUrl(url, has_opener_set));
[email protected]724159a2010-12-30 01:11:18899 }
900}
901
902void TabContents::OnDidRedirectProvisionalLoad(int32 page_id,
[email protected]eacb080b2011-05-22 19:40:26903 bool has_opener_set,
[email protected]724159a2010-12-30 01:11:18904 const GURL& source_url,
905 const GURL& target_url) {
[email protected]18ba2b12011-04-06 16:35:49906 // TODO(creis): Remove this method and have the pre-rendering code listen to
907 // the ResourceDispatcherHost's RESOURCE_RECEIVED_REDIRECT notification
908 // instead. See http://crbug.com/78512.
[email protected]724159a2010-12-30 01:11:18909 NavigationEntry* entry;
910 if (page_id == -1)
911 entry = controller_.pending_entry();
912 else
913 entry = controller_.GetEntryWithPageID(GetSiteInstance(), page_id);
914 if (!entry || entry->url() != source_url)
915 return;
[email protected]e7d50892011-01-19 21:47:38916
[email protected]4850a7f2011-03-08 23:36:59917 // Notify observers about the provisional change in the main frame URL.
918 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
[email protected]eacb080b2011-05-22 19:40:26919 ProvisionalChangeToMainFrameUrl(target_url,
920 has_opener_set));
[email protected]724159a2010-12-30 01:11:18921}
922
923void TabContents::OnDidFailProvisionalLoadWithError(
924 int64 frame_id,
925 bool is_main_frame,
926 int error_code,
927 const GURL& url,
928 bool showing_repost_interstitial) {
929 VLOG(1) << "Failed Provisional Load: " << url.possibly_invalid_spec()
930 << ", error_code: " << error_code
931 << " is_main_frame: " << is_main_frame
932 << " showing_repost_interstitial: " << showing_repost_interstitial
933 << " frame_id: " << frame_id;
934 GURL validated_url(url);
935 render_view_host()->FilterURL(ChildProcessSecurityPolicy::GetInstance(),
936 GetRenderProcessHost()->id(), &validated_url);
937
938 if (net::ERR_ABORTED == error_code) {
939 // EVIL HACK ALERT! Ignore failed loads when we're showing interstitials.
940 // This means that the interstitial won't be torn down properly, which is
941 // bad. But if we have an interstitial, go back to another tab type, and
942 // then load the same interstitial again, we could end up getting the first
943 // interstitial's "failed" message (as a result of the cancel) when we're on
944 // the second one.
945 //
946 // We can't tell this apart, so we think we're tearing down the current page
947 // which will cause a crash later one. There is also some code in
948 // RenderViewHostManager::RendererAbortedProvisionalLoad that is commented
949 // out because of this problem.
950 //
951 // http://code.google.com/p/chromium/issues/detail?id=2855
952 // Because this will not tear down the interstitial properly, if "back" is
953 // back to another tab type, the interstitial will still be somewhat alive
954 // in the previous tab type. If you navigate somewhere that activates the
955 // tab with the interstitial again, you'll see a flash before the new load
956 // commits of the interstitial page.
957 if (showing_interstitial_page()) {
958 LOG(WARNING) << "Discarding message during interstitial.";
959 return;
960 }
961
[email protected]02102f82011-06-13 20:37:02962 // Discard our pending entry if the load canceled (e.g. if we decided to
963 // download the file instead of load it). We do not verify that the URL
964 // being canceled matches the pending entry's URL because they will not
965 // match if a redirect occurred (in which case we do not want to leave a
966 // stale redirect URL showing). This means that we also cancel the pending
967 // entry if the user started a new navigation. As a result, the navigation
968 // controller may not remember that a load is in progress, but the
969 // navigation will still commit even if there is no pending entry.
[email protected]724159a2010-12-30 01:11:18970 NavigationEntry* pending_entry = controller_.pending_entry();
[email protected]02102f82011-06-13 20:37:02971 if (pending_entry)
[email protected]c95fa8b2011-04-28 20:26:16972 DidCancelLoading();
[email protected]724159a2010-12-30 01:11:18973
974 render_manager_.RendererAbortedProvisionalLoad(render_view_host());
975 }
976
977 // Send out a notification that we failed a provisional load with an error.
978 ProvisionalLoadDetails details(
979 is_main_frame, controller_.IsURLInPageNavigation(validated_url),
[email protected]b9e8ea62011-03-04 06:29:09980 validated_url, std::string(), false, frame_id);
[email protected]724159a2010-12-30 01:11:18981 details.set_error_code(error_code);
982
983 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:27984 content::NOTIFICATION_FAIL_PROVISIONAL_LOAD_WITH_ERROR,
[email protected]724159a2010-12-30 01:11:18985 Source<NavigationController>(&controller_),
986 Details<ProvisionalLoadDetails>(&details));
[email protected]0d60f0192011-04-14 12:40:10987
988 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
989 DidFailProvisionalLoad(frame_id, is_main_frame,
990 validated_url, error_code));
[email protected]724159a2010-12-30 01:11:18991}
992
993void TabContents::OnDidLoadResourceFromMemoryCache(
994 const GURL& url,
[email protected]70435962011-08-02 20:13:28995 const std::string& security_info,
996 const std::string& http_method,
997 ResourceType::Type resource_type) {
[email protected]28620862011-03-22 23:07:19998 base::StatsCounter cache("WebKit.CacheHit");
[email protected]724159a2010-12-30 01:11:18999 cache.Increment();
1000
1001 // Send out a notification that we loaded a resource from our memory cache.
[email protected]70d66502011-09-23 00:55:081002 int cert_id = 0;
1003 net::CertStatus cert_status = 0;
1004 int security_bits = -1;
1005 int connection_status = 0;
[email protected]724159a2010-12-30 01:11:181006 SSLManager::DeserializeSecurityInfo(security_info,
1007 &cert_id, &cert_status,
1008 &security_bits,
1009 &connection_status);
[email protected]7a8c55e2011-02-15 08:21:161010 LoadFromMemoryCacheDetails details(url, GetRenderProcessHost()->id(),
1011 cert_id, cert_status);
[email protected]724159a2010-12-30 01:11:181012
1013 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271014 content::NOTIFICATION_LOAD_FROM_MEMORY_CACHE,
[email protected]724159a2010-12-30 01:11:181015 Source<NavigationController>(&controller_),
1016 Details<LoadFromMemoryCacheDetails>(&details));
1017}
1018
1019void TabContents::OnDidDisplayInsecureContent() {
[email protected]221a5d92011-04-28 18:23:161020 UserMetrics::RecordAction(UserMetricsAction("SSL.DisplayedInsecureContent"));
[email protected]724159a2010-12-30 01:11:181021 displayed_insecure_content_ = true;
[email protected]9678d8f2011-08-05 04:05:071022 SSLManager::NotifySSLInternalStateChanged(&controller());
[email protected]724159a2010-12-30 01:11:181023}
1024
1025void TabContents::OnDidRunInsecureContent(
[email protected]92771112011-01-20 00:13:021026 const std::string& security_origin, const GURL& target_url) {
[email protected]9941cf52011-02-08 16:17:231027 LOG(INFO) << security_origin << " ran insecure content from "
1028 << target_url.possibly_invalid_spec();
[email protected]221a5d92011-04-28 18:23:161029 UserMetrics::RecordAction(UserMetricsAction("SSL.RanInsecureContent"));
[email protected]ca406032011-07-19 21:53:051030 if (EndsWith(security_origin, kDotGoogleDotCom, false)) {
1031 UserMetrics::RecordAction(
1032 UserMetricsAction("SSL.RanInsecureContentGoogle"));
1033 }
[email protected]724159a2010-12-30 01:11:181034 controller_.ssl_manager()->DidRunInsecureContent(security_origin);
[email protected]f2b58c4d2011-06-01 23:29:411035 displayed_insecure_content_ = true;
[email protected]9678d8f2011-08-05 04:05:071036 SSLManager::NotifySSLInternalStateChanged(&controller());
[email protected]724159a2010-12-30 01:11:181037}
1038
1039void TabContents::OnDocumentLoadedInFrame(int64 frame_id) {
1040 controller_.DocumentLoadedInFrame();
[email protected]0d60f0192011-04-14 12:40:101041 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
1042 DocumentLoadedInFrame(frame_id));
[email protected]724159a2010-12-30 01:11:181043}
1044
1045void TabContents::OnDidFinishLoad(int64 frame_id) {
[email protected]0d60f0192011-04-14 12:40:101046 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
1047 DidFinishLoad(frame_id));
[email protected]724159a2010-12-30 01:11:181048}
1049
[email protected]c8f73ab2011-01-22 00:05:171050void TabContents::OnUpdateContentRestrictions(int restrictions) {
[email protected]c40d6232011-03-25 00:16:211051 SetContentRestrictions(restrictions);
[email protected]c8f73ab2011-01-22 00:05:171052}
1053
[email protected]216813952011-05-19 22:21:261054void TabContents::OnGoToEntryAtOffset(int offset) {
1055 if (!delegate_ || delegate_->OnGoToEntryOffset(offset)) {
1056 NavigationEntry* entry = controller_.GetEntryAtOffset(offset);
1057 if (!entry)
1058 return;
1059 // Note that we don't call NavigationController::GotToOffset() as we don't
1060 // want to create a pending navigation entry (it might end up lingering
1061 // http://crbug.com/51680).
1062 entry->set_transition_type(entry->transition_type() |
1063 PageTransition::FORWARD_BACK);
1064 NavigateToEntry(*entry, NavigationController::NO_RELOAD);
[email protected]a13cc362011-07-28 21:29:571065
1066 // If the entry is being restored and doesn't have a SiteInstance yet, fill
1067 // it in now that we know. This allows us to find the entry when it commits.
1068 if (!entry->site_instance() &&
1069 entry->restore_type() != NavigationEntry::RESTORE_NONE) {
[email protected]77362eb2011-08-01 17:18:381070 entry->set_site_instance(GetPendingSiteInstance());
[email protected]a13cc362011-07-28 21:29:571071 }
[email protected]216813952011-05-19 22:21:261072 }
1073}
1074
1075void TabContents::OnUpdateZoomLimits(int minimum_percent,
1076 int maximum_percent,
1077 bool remember) {
1078 minimum_zoom_percent_ = minimum_percent;
1079 maximum_zoom_percent_ = maximum_percent;
1080 temporary_zoom_settings_ = !remember;
1081}
1082
1083void TabContents::OnFocusedNodeChanged(bool is_editable_node) {
1084 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271085 content::NOTIFICATION_FOCUS_CHANGED_IN_PAGE,
[email protected]216813952011-05-19 22:21:261086 Source<TabContents>(this),
1087 Details<const bool>(&is_editable_node));
1088}
1089
[email protected]3a29a6e2011-08-24 18:26:211090void TabContents::OnEnumerateDirectory(int request_id,
1091 const FilePath& path) {
1092 delegate()->EnumerateDirectory(this, request_id, path);
1093}
1094
[email protected]7d189022011-08-25 22:54:201095void TabContents::OnJSOutOfMemory() {
1096 delegate()->JSOutOfMemory(this);
1097}
1098
1099void TabContents::OnRegisterProtocolHandler(const std::string& protocol,
1100 const GURL& url,
1101 const string16& title) {
1102 delegate()->RegisterProtocolHandler(this, protocol, url, title);
1103}
1104
1105void TabContents::OnRegisterIntentHandler(const string16& action,
1106 const string16& type,
1107 const string16& href,
1108 const string16& title) {
1109 delegate()->RegisterIntentHandler(this, action, type, href, title);
1110}
1111
1112void TabContents::OnWebIntentDispatch(const IPC::Message& message,
[email protected]ce9751942011-09-21 01:57:241113 const webkit_glue::WebIntentData& intent,
[email protected]7d189022011-08-25 22:54:201114 int intent_id) {
[email protected]ce9751942011-09-21 01:57:241115 delegate()->WebIntentDispatch(this, message.routing_id(), intent, intent_id);
[email protected]7d189022011-08-25 22:54:201116}
1117
[email protected]b888919c2011-09-02 00:32:161118void TabContents::OnFindReply(int request_id,
1119 int number_of_matches,
1120 const gfx::Rect& selection_rect,
1121 int active_match_ordinal,
1122 bool final_update) {
1123 delegate()->FindReply(this, request_id, number_of_matches, selection_rect,
1124 active_match_ordinal, final_update);
[email protected]4e6239c2011-09-23 00:11:001125 // Send a notification to the renderer that we are ready to receive more
1126 // results from the scoping effort of the Find operation. The FindInPage
1127 // scoping is asynchronous and periodically sends results back up to the
1128 // browser using IPC. In an effort to not spam the browser we have the
1129 // browser send an ACK for each FindReply message and have the renderer
1130 // queue up the latest status message while waiting for this ACK.
1131 render_view_host()->Send(
1132 new ViewMsg_FindReplyACK(render_view_host()->routing_id()));
[email protected]b888919c2011-09-02 00:32:161133}
1134
[email protected]d952a052011-09-06 18:42:451135void TabContents::OnCrashedPlugin(const FilePath& plugin_path) {
1136 delegate()->CrashedPlugin(this, plugin_path);
1137}
1138
[email protected]7fc4bbb2011-09-08 21:23:101139void TabContents::OnAppCacheAccessed(const GURL& manifest_url,
1140 bool blocked_by_policy) {
1141 // Notify observers about navigation.
1142 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
1143 AppCacheAccessed(manifest_url, blocked_by_policy));
1144}
1145
[email protected]96d185d2009-04-24 03:28:541146// Notifies the RenderWidgetHost instance about the fact that the page is
1147// loading, or done loading and calls the base implementation.
1148void TabContents::SetIsLoading(bool is_loading,
1149 LoadNotificationDetails* details) {
1150 if (is_loading == is_loading_)
1151 return;
1152
1153 if (!is_loading) {
[email protected]9c235f042011-08-10 22:28:211154 load_state_ = net::LoadStateWithParam(net::LOAD_STATE_IDLE, string16());
[email protected]96d185d2009-04-24 03:28:541155 load_state_host_.clear();
[email protected]094e5b22009-09-25 04:23:561156 upload_size_ = 0;
1157 upload_position_ = 0;
[email protected]96d185d2009-04-24 03:28:541158 }
1159
1160 render_manager_.SetIsLoading(is_loading);
1161
1162 is_loading_ = is_loading;
1163 waiting_for_response_ = is_loading;
1164
[email protected]6ebdc9b2010-09-27 16:55:571165 if (delegate_)
1166 delegate_->LoadingStateChanged(this);
[email protected]329581b2009-04-28 06:52:351167 NotifyNavigationStateChanged(INVALIDATE_LOAD);
[email protected]96d185d2009-04-24 03:28:541168
[email protected]432115822011-07-10 15:52:271169 int type = is_loading ? content::NOTIFICATION_LOAD_START :
1170 content::NOTIFICATION_LOAD_STOP;
[email protected]ce5c4502009-05-06 16:46:111171 NotificationDetails det = NotificationService::NoDetails();
[email protected]96d185d2009-04-24 03:28:541172 if (details)
1173 det = Details<LoadNotificationDetails>(details);
1174 NotificationService::current()->Notify(type,
1175 Source<NavigationController>(&controller_),
1176 det);
1177}
1178
[email protected]d0980792011-02-13 19:41:401179WebUI* TabContents::GetWebUIForCurrentState() {
[email protected]7ade2732011-02-10 00:13:581180 // When there is a pending navigation entry, we want to use the pending WebUI
[email protected]96d185d2009-04-24 03:28:541181 // that goes along with it to control the basic flags. For example, we want to
1182 // show the pending URL in the URL bar, so we want the display_url flag to
1183 // be from the pending entry.
1184 //
1185 // The confusion comes because there are multiple possibilities for the
1186 // initial load in a tab as a side effect of the way the RenderViewHostManager
1187 // works.
1188 //
[email protected]e0112912011-02-02 22:54:351189 // - For the very first tab the load looks "normal". The new tab Web UI is
[email protected]96d185d2009-04-24 03:28:541190 // the pending one, and we want it to apply here.
1191 //
1192 // - For subsequent new tabs, they'll get a new SiteInstance which will then
1193 // get switched to the one previously associated with the new tab pages.
[email protected]7ade2732011-02-10 00:13:581194 // This switching will cause the manager to commit the RVH/WebUI. So we'll
[email protected]e0112912011-02-02 22:54:351195 // have a committed Web UI in this case.
[email protected]96d185d2009-04-24 03:28:541196 //
1197 // This condition handles all of these cases:
1198 //
1199 // - First load in first tab: no committed nav entry + pending nav entry +
1200 // pending dom ui:
[email protected]e0112912011-02-02 22:54:351201 // -> Use pending Web UI if any.
[email protected]96d185d2009-04-24 03:28:541202 //
1203 // - First load in second tab: no committed nav entry + pending nav entry +
[email protected]e0112912011-02-02 22:54:351204 // no pending Web UI:
1205 // -> Use the committed Web UI if any.
[email protected]96d185d2009-04-24 03:28:541206 //
1207 // - Second navigation in any tab: committed nav entry + pending nav entry:
[email protected]e0112912011-02-02 22:54:351208 // -> Use pending Web UI if any.
[email protected]96d185d2009-04-24 03:28:541209 //
1210 // - Normal state with no load: committed nav entry + no pending nav entry:
[email protected]e0112912011-02-02 22:54:351211 // -> Use committed Web UI.
[email protected]96d185d2009-04-24 03:28:541212 if (controller_.pending_entry() &&
1213 (controller_.GetLastCommittedEntry() ||
[email protected]7ade2732011-02-10 00:13:581214 render_manager_.pending_web_ui()))
1215 return render_manager_.pending_web_ui();
1216 return render_manager_.web_ui();
[email protected]96d185d2009-04-24 03:28:541217}
[email protected]420ae012009-04-24 05:16:321218
[email protected]1fd1a502011-03-30 16:55:561219WebUI::TypeID TabContents::GetWebUITypeForCurrentState() {
[email protected]3d7474ff2011-07-27 17:47:371220 return content::WebUIFactory::Get()->GetWebUIType(browser_context(),
1221 GetURL());
[email protected]1fd1a502011-03-30 16:55:561222}
1223
[email protected]420ae012009-04-24 05:16:321224void TabContents::DidNavigateMainFramePostCommit(
[email protected]8286f51a2011-05-31 17:39:131225 const content::LoadCommittedDetails& details,
[email protected]420ae012009-04-24 05:16:321226 const ViewHostMsg_FrameNavigate_Params& params) {
[email protected]1fd1a502011-03-30 16:55:561227 if (opener_web_ui_type_ != WebUI::kNoWebUI) {
[email protected]80a8fad2011-01-29 04:02:381228 // If this is a window.open navigation, use the same WebUI as the renderer
[email protected]c2e74fe82009-09-03 17:57:441229 // that opened the window, as long as both renderers have the same
1230 // privileges.
[email protected]1fd1a502011-03-30 16:55:561231 if (delegate_ && opener_web_ui_type_ == GetWebUITypeForCurrentState()) {
1232 WebUI* web_ui = content::GetContentClient()->browser()->
1233 GetWebUIFactory()->CreateWebUIForURL(this, GetURL());
[email protected]7ade2732011-02-10 00:13:581234 // web_ui might be NULL if the URL refers to a non-existent extension.
1235 if (web_ui) {
[email protected]d0980792011-02-13 19:41:401236 render_manager_.SetWebUIPostCommit(web_ui);
[email protected]7ade2732011-02-10 00:13:581237 web_ui->RenderViewCreated(render_view_host());
[email protected]c2e74fe82009-09-03 17:57:441238 }
1239 }
[email protected]1fd1a502011-03-30 16:55:561240 opener_web_ui_type_ = WebUI::kNoWebUI;
[email protected]c2e74fe82009-09-03 17:57:441241 }
1242
[email protected]4e697b042011-07-08 06:44:561243 if (details.is_navigation_to_different_page()) {
[email protected]420ae012009-04-24 05:16:321244 // Clear the status bubble. This is a workaround for a bug where WebKit
1245 // doesn't let us know that the cursor left an element during a
1246 // transition (this is also why the mouse cursor remains as a hand after
1247 // clicking on a link); see bugs 1184641 and 980803. We don't want to
1248 // clear the bubble when a user navigates to a named anchor in the same
1249 // page.
1250 UpdateTargetURL(details.entry->page_id(), GURL());
[email protected]420ae012009-04-24 05:16:321251 }
1252
[email protected]a6e82fc2010-02-24 22:28:141253 if (!details.is_in_page) {
[email protected]f17a0ee2010-05-17 17:38:471254 // Once the main frame is navigated, we're no longer considered to have
1255 // displayed insecure content.
1256 displayed_insecure_content_ = false;
[email protected]aece2c7f2009-08-27 20:43:171257 }
[email protected]ce5c4502009-05-06 16:46:111258
[email protected]3c9e1872010-11-18 16:17:491259 // Notify observers about navigation.
[email protected]585b30362011-01-28 02:30:171260 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
[email protected]3c9e1872010-11-18 16:17:491261 DidNavigateMainFramePostCommit(details, params));
[email protected]420ae012009-04-24 05:16:321262}
1263
1264void TabContents::DidNavigateAnyFramePostCommit(
1265 RenderViewHost* render_view_host,
[email protected]8286f51a2011-05-31 17:39:131266 const content::LoadCommittedDetails& details,
[email protected]420ae012009-04-24 05:16:321267 const ViewHostMsg_FrameNavigate_Params& params) {
[email protected]3ab9cb82011-06-03 18:02:071268 // If we navigate, reset JavaScript state. This does nothing to prevent
[email protected]420ae012009-04-24 05:16:321269 // a malicious script from spamming messages, since the script could just
1270 // reload the page to stop blocking.
[email protected]2e5b90c2011-08-16 21:11:551271 if (dialog_creator_) {
1272 dialog_creator_->ResetJavaScriptState(this);
1273 dialog_creator_ = NULL;
1274 }
[email protected]420ae012009-04-24 05:16:321275
[email protected]3c9e1872010-11-18 16:17:491276 // Notify observers about navigation.
[email protected]585b30362011-01-28 02:30:171277 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
[email protected]3c9e1872010-11-18 16:17:491278 DidNavigateAnyFramePostCommit(details, params));
[email protected]420ae012009-04-24 05:16:321279}
1280
[email protected]420ae012009-04-24 05:16:321281void TabContents::UpdateMaxPageIDIfNecessary(SiteInstance* site_instance,
1282 RenderViewHost* rvh) {
1283 // If we are creating a RVH for a restored controller, then we might
1284 // have more page IDs than the SiteInstance's current max page ID. We must
1285 // make sure that the max page ID is larger than any restored page ID.
1286 // Note that it is ok for conflicting page IDs to exist in another tab
1287 // (i.e., NavigationController), but if any page ID is larger than the max,
1288 // the back/forward list will get confused.
1289 int max_restored_page_id = controller_.max_restored_page_id();
1290 if (max_restored_page_id > 0) {
1291 int curr_max_page_id = site_instance->max_page_id();
1292 if (max_restored_page_id > curr_max_page_id) {
1293 // Need to update the site instance immediately.
1294 site_instance->UpdateMaxPageID(max_restored_page_id);
1295
1296 // Also tell the renderer to update its internal representation. We
1297 // need to reserve enough IDs to make all restored page IDs less than
1298 // the max.
1299 if (curr_max_page_id < 0)
1300 curr_max_page_id = 0;
[email protected]216813952011-05-19 22:21:261301 rvh->Send(new ViewMsg_ReservePageIDRange(
1302 rvh->routing_id(), max_restored_page_id - curr_max_page_id));
[email protected]420ae012009-04-24 05:16:321303 }
1304 }
1305}
1306
[email protected]6b2f7a82011-04-25 19:30:511307bool TabContents::UpdateTitleForEntry(NavigationEntry* entry,
[email protected]acafd272011-07-26 17:35:571308 const string16& title) {
[email protected]420ae012009-04-24 05:16:321309 // For file URLs without a title, use the pathname instead. In the case of a
1310 // synthesized title, we don't want the update to count toward the "one set
1311 // per page of the title to history."
[email protected]6b2f7a82011-04-25 19:30:511312 string16 final_title;
[email protected]420ae012009-04-24 05:16:321313 bool explicit_set;
[email protected]987fc3a2011-05-26 14:18:091314 if (entry && entry->url().SchemeIsFile() && title.empty()) {
[email protected]6b2f7a82011-04-25 19:30:511315 final_title = UTF8ToUTF16(entry->url().ExtractFileName());
[email protected]420ae012009-04-24 05:16:321316 explicit_set = false; // Don't count synthetic titles toward the set limit.
1317 } else {
[email protected]acafd272011-07-26 17:35:571318 TrimWhitespace(title, TRIM_ALL, &final_title);
[email protected]420ae012009-04-24 05:16:321319 explicit_set = true;
1320 }
1321
[email protected]987fc3a2011-05-26 14:18:091322 // If a page is created via window.open and never navigated,
1323 // there will be no navigation entry. In this situation,
1324 // |page_title_when_no_navigaiton_entry_| will be used for page title.
1325 if (entry) {
1326 if (final_title == entry->title())
1327 return false; // Nothing changed, don't bother.
[email protected]420ae012009-04-24 05:16:321328
[email protected]987fc3a2011-05-26 14:18:091329 entry->set_title(final_title);
1330 } else {
1331 if (page_title_when_no_navigation_entry_ == final_title)
1332 return false; // Nothing changed, don't bother.
1333
1334 page_title_when_no_navigation_entry_ = final_title;
1335 }
[email protected]420ae012009-04-24 05:16:321336
[email protected]420ae012009-04-24 05:16:321337 // Lastly, set the title for the view.
[email protected]790e2fd2011-09-21 20:28:231338 view_->SetPageTitle(final_title);
[email protected]420ae012009-04-24 05:16:321339
[email protected]105bb0f2011-05-24 17:12:141340 TitleUpdatedDetails details(entry, explicit_set);
1341
[email protected]cbc0e1b2010-04-12 18:33:041342 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271343 content::NOTIFICATION_TAB_CONTENTS_TITLE_UPDATED,
[email protected]cbc0e1b2010-04-12 18:33:041344 Source<TabContents>(this),
[email protected]105bb0f2011-05-24 17:12:141345 Details<TitleUpdatedDetails>(&details));
[email protected]cbc0e1b2010-04-12 18:33:041346
[email protected]420ae012009-04-24 05:16:321347 return true;
1348}
1349
1350void TabContents::NotifySwapped() {
1351 // After sending out a swap notification, we need to send a disconnect
1352 // notification so that clients that pick up a pointer to |this| can NULL the
1353 // pointer. See Bug 1230284.
1354 notify_disconnection_ = true;
1355 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271356 content::NOTIFICATION_TAB_CONTENTS_SWAPPED,
[email protected]d82ed61e2009-06-16 02:46:221357 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:321358 NotificationService::NoDetails());
1359}
1360
1361void TabContents::NotifyConnected() {
1362 notify_disconnection_ = true;
1363 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271364 content::NOTIFICATION_TAB_CONTENTS_CONNECTED,
[email protected]d82ed61e2009-06-16 02:46:221365 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:321366 NotificationService::NoDetails());
1367}
1368
1369void TabContents::NotifyDisconnected() {
1370 if (!notify_disconnection_)
1371 return;
1372
1373 notify_disconnection_ = false;
1374 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271375 content::NOTIFICATION_TAB_CONTENTS_DISCONNECTED,
[email protected]d82ed61e2009-06-16 02:46:221376 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:321377 NotificationService::NoDetails());
1378}
1379
[email protected]8d3347f2009-07-09 22:00:211380RenderViewHostDelegate::View* TabContents::GetViewDelegate() {
[email protected]420ae012009-04-24 05:16:321381 return view_.get();
1382}
1383
[email protected]8d3347f2009-07-09 22:00:211384RenderViewHostDelegate::RendererManagement*
1385TabContents::GetRendererManagementDelegate() {
1386 return &render_manager_;
1387}
1388
[email protected]3d7474ff2011-07-27 17:47:371389RendererPreferences TabContents::GetRendererPrefs(
1390 content::BrowserContext* browser_context) const {
[email protected]840b1512009-07-21 16:53:461391 return renderer_preferences_;
[email protected]80d96fa2009-06-10 22:34:511392}
1393
[email protected]57c6a652009-05-04 07:58:341394TabContents* TabContents::GetAsTabContents() {
1395 return this;
1396}
1397
[email protected]7b291f92009-08-14 05:43:531398ViewType::Type TabContents::GetRenderViewType() const {
1399 return ViewType::TAB_CONTENTS;
1400}
1401
[email protected]420ae012009-04-24 05:16:321402void TabContents::RenderViewCreated(RenderViewHost* render_view_host) {
[email protected]2e4633c2009-07-09 16:58:061403 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271404 content::NOTIFICATION_RENDER_VIEW_HOST_CREATED_FOR_TAB,
[email protected]2e4633c2009-07-09 16:58:061405 Source<TabContents>(this),
1406 Details<RenderViewHost>(render_view_host));
[email protected]420ae012009-04-24 05:16:321407 NavigationEntry* entry = controller_.GetActiveEntry();
1408 if (!entry)
1409 return;
1410
1411 // When we're creating views, we're still doing initial setup, so we always
[email protected]e0112912011-02-02 22:54:351412 // use the pending Web UI rather than any possibly existing committed one.
[email protected]1fd1a502011-03-30 16:55:561413 if (render_manager_.pending_web_ui())
[email protected]7ade2732011-02-10 00:13:581414 render_manager_.pending_web_ui()->RenderViewCreated(render_view_host);
[email protected]420ae012009-04-24 05:16:321415
1416 if (entry->IsViewSourceMode()) {
1417 // Put the renderer in view source mode.
1418 render_view_host->Send(
1419 new ViewMsg_EnableViewSourceMode(render_view_host->routing_id()));
1420 }
[email protected]0666aef2009-05-13 19:48:081421
1422 view()->RenderViewCreated(render_view_host);
[email protected]11f764bb2011-05-25 19:45:261423
1424 FOR_EACH_OBSERVER(
1425 TabContentsObserver, observers_, RenderViewCreated(render_view_host));
[email protected]420ae012009-04-24 05:16:321426}
1427
1428void TabContents::RenderViewReady(RenderViewHost* rvh) {
1429 if (rvh != render_view_host()) {
1430 // Don't notify the world, since this came from a renderer in the
1431 // background.
1432 return;
1433 }
1434
1435 NotifyConnected();
[email protected]9d8a4642009-07-29 17:25:301436 bool was_crashed = is_crashed();
[email protected]443b80e2010-12-14 00:42:231437 SetIsCrashed(base::TERMINATION_STATUS_STILL_RUNNING, 0);
[email protected]9d8a4642009-07-29 17:25:301438
1439 // Restore the focus to the tab (otherwise the focus will be on the top
1440 // window).
[email protected]484ae5912010-09-29 19:16:141441 if (was_crashed && !FocusLocationBarByDefault() &&
1442 (!delegate_ || delegate_->ShouldFocusPageAfterCrash())) {
[email protected]9d8a4642009-07-29 17:25:301443 Focus();
[email protected]484ae5912010-09-29 19:16:141444 }
[email protected]420ae012009-04-24 05:16:321445}
1446
[email protected]443b80e2010-12-14 00:42:231447void TabContents::RenderViewGone(RenderViewHost* rvh,
1448 base::TerminationStatus status,
1449 int error_code) {
[email protected]420ae012009-04-24 05:16:321450 if (rvh != render_view_host()) {
1451 // The pending page's RenderViewHost is gone.
1452 return;
1453 }
1454
1455 SetIsLoading(false, NULL);
1456 NotifyDisconnected();
[email protected]443b80e2010-12-14 00:42:231457 SetIsCrashed(status, error_code);
[email protected]b3e3f87a2011-10-01 23:37:541458 view()->OnTabCrashed(crashed_status(), crashed_error_code());
[email protected]420ae012009-04-24 05:16:321459
[email protected]55452902011-06-01 21:57:471460 FOR_EACH_OBSERVER(TabContentsObserver, observers_, RenderViewGone());
[email protected]420ae012009-04-24 05:16:321461}
1462
[email protected]2e4633c2009-07-09 16:58:061463void TabContents::RenderViewDeleted(RenderViewHost* rvh) {
[email protected]fb7b79f72009-11-06 18:00:481464 render_manager_.RenderViewDeleted(rvh);
[email protected]2e4633c2009-07-09 16:58:061465}
1466
[email protected]420ae012009-04-24 05:16:321467void TabContents::DidNavigate(RenderViewHost* rvh,
1468 const ViewHostMsg_FrameNavigate_Params& params) {
[email protected]09b29342011-06-24 19:18:481469 if (PageTransition::IsMainFrame(params.transition))
[email protected]420ae012009-04-24 05:16:321470 render_manager_.DidNavigateMainFrame(rvh);
[email protected]8030f012009-09-25 18:09:371471
[email protected]420ae012009-04-24 05:16:321472 // Update the site of the SiteInstance if it doesn't have one yet.
1473 if (!GetSiteInstance()->has_site())
1474 GetSiteInstance()->SetSite(params.url);
1475
1476 // Need to update MIME type here because it's referred to in
1477 // UpdateNavigationCommands() called by RendererDidNavigate() to
1478 // determine whether or not to enable the encoding menu.
1479 // It's updated only for the main frame. For a subframe,
1480 // RenderView::UpdateURL does not set params.contents_mime_type.
1481 // (see http://code.google.com/p/chromium/issues/detail?id=2929 )
1482 // TODO(jungshik): Add a test for the encoding menu to avoid
1483 // regressing it again.
1484 if (PageTransition::IsMainFrame(params.transition))
1485 contents_mime_type_ = params.contents_mime_type;
1486
[email protected]8286f51a2011-05-31 17:39:131487 content::LoadCommittedDetails details;
[email protected]93f230e02011-06-01 14:40:001488 bool did_navigate = controller_.RendererDidNavigate(params, &details);
[email protected]3e90d4a2009-07-03 17:38:391489
[email protected]a9c0bfe2010-09-17 08:35:221490 // Send notification about committed provisional loads. This notification is
1491 // different from the NAV_ENTRY_COMMITTED notification which doesn't include
1492 // the actual URL navigated to and isn't sent for AUTO_SUBFRAME navigations.
1493 if (details.type != NavigationType::NAV_IGNORE) {
[email protected]dabb0d12010-10-05 12:50:071494 // For AUTO_SUBFRAME navigations, an event for the main frame is generated
1495 // that is not recorded in the navigation history. For the purpose of
1496 // tracking navigation events, we treat this event as a sub frame navigation
1497 // event.
1498 bool is_main_frame = did_navigate ? details.is_main_frame : false;
[email protected]0d60f0192011-04-14 12:40:101499 PageTransition::Type transition_type = params.transition;
[email protected]a9c0bfe2010-09-17 08:35:221500 // Whether or not a page transition was triggered by going backward or
1501 // forward in the history is only stored in the navigation controller's
1502 // entry list.
1503 if (did_navigate &&
1504 (controller_.GetActiveEntry()->transition_type() &
1505 PageTransition::FORWARD_BACK)) {
[email protected]0d60f0192011-04-14 12:40:101506 transition_type = params.transition | PageTransition::FORWARD_BACK;
[email protected]a9c0bfe2010-09-17 08:35:221507 }
[email protected]0d60f0192011-04-14 12:40:101508 // Notify observers about the commit of the provisional load.
1509 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
1510 DidCommitProvisionalLoadForFrame(params.frame_id,
1511 is_main_frame, params.url, transition_type));
[email protected]a9c0bfe2010-09-17 08:35:221512 }
1513
[email protected]76543b92009-08-31 17:27:451514 if (!did_navigate)
1515 return; // No navigation happened.
[email protected]420ae012009-04-24 05:16:321516
1517 // DO NOT ADD MORE STUFF TO THIS FUNCTION! Your component should either listen
1518 // for the appropriate notification (best) or you can add it to
1519 // DidNavigateMainFramePostCommit / DidNavigateAnyFramePostCommit (only if
1520 // necessary, please).
1521
1522 // Run post-commit tasks.
[email protected]93f230e02011-06-01 14:40:001523 if (details.is_main_frame) {
[email protected]420ae012009-04-24 05:16:321524 DidNavigateMainFramePostCommit(details, params);
[email protected]09b29342011-06-24 19:18:481525 if (delegate())
1526 delegate()->DidNavigateMainFramePostCommit(this);
[email protected]93f230e02011-06-01 14:40:001527 }
[email protected]420ae012009-04-24 05:16:321528 DidNavigateAnyFramePostCommit(rvh, details, params);
1529}
1530
1531void TabContents::UpdateState(RenderViewHost* rvh,
1532 int32 page_id,
1533 const std::string& state) {
[email protected]992db4c2011-05-12 15:37:151534 // Ensure that this state update comes from either the active RVH or one of
1535 // the swapped out RVHs. We don't expect to hear from any other RVHs.
1536 DCHECK(rvh == render_view_host() || render_manager_.IsSwappedOut(rvh));
[email protected]420ae012009-04-24 05:16:321537
1538 // We must be prepared to handle state updates for any page, these occur
1539 // when the user is scrolling and entering form data, as well as when we're
1540 // leaving a page, in which case our state may have already been moved to
1541 // the next page. The navigation controller will look up the appropriate
1542 // NavigationEntry and update it when it is notified via the delegate.
1543
1544 int entry_index = controller_.GetEntryIndexWithPageID(
[email protected]992db4c2011-05-12 15:37:151545 rvh->site_instance(), page_id);
[email protected]420ae012009-04-24 05:16:321546 if (entry_index < 0)
1547 return;
1548 NavigationEntry* entry = controller_.GetEntryAtIndex(entry_index);
1549
1550 if (state == entry->content_state())
1551 return; // Nothing to update.
1552 entry->set_content_state(state);
1553 controller_.NotifyEntryChanged(entry, entry_index);
1554}
1555
1556void TabContents::UpdateTitle(RenderViewHost* rvh,
[email protected]acafd272011-07-26 17:35:571557 int32 page_id,
[email protected]a49e10b2011-08-01 23:57:461558 const string16& title,
1559 base::i18n::TextDirection title_direction) {
[email protected]420ae012009-04-24 05:16:321560 // If we have a title, that's a pretty good indication that we've started
1561 // getting useful data.
1562 SetNotWaitingForResponse();
1563
1564 DCHECK(rvh == render_view_host());
[email protected]371cc8b2010-09-01 16:03:481565 NavigationEntry* entry = controller_.GetEntryWithPageID(rvh->site_instance(),
[email protected]ec0b6c42010-08-26 03:16:581566 page_id);
[email protected]987fc3a2011-05-26 14:18:091567
[email protected]a49e10b2011-08-01 23:57:461568 // TODO(evan): make use of title_direction.
1569 // http://code.google.com/p/chromium/issues/detail?id=27094
[email protected]987fc3a2011-05-26 14:18:091570 if (!UpdateTitleForEntry(entry, title))
[email protected]420ae012009-04-24 05:16:321571 return;
1572
1573 // Broadcast notifications when the UI should be updated.
1574 if (entry == controller_.GetEntryAtOffset(0))
[email protected]f1cd5e82009-10-23 17:19:031575 NotifyNavigationStateChanged(INVALIDATE_TITLE);
[email protected]420ae012009-04-24 05:16:321576}
1577
[email protected]420ae012009-04-24 05:16:321578void TabContents::UpdateEncoding(RenderViewHost* render_view_host,
[email protected]41fc0322009-09-04 22:23:401579 const std::string& encoding) {
[email protected]420ae012009-04-24 05:16:321580 set_encoding(encoding);
1581}
1582
1583void TabContents::UpdateTargetURL(int32 page_id, const GURL& url) {
1584 if (delegate())
[email protected]d952a052011-09-06 18:42:451585 delegate()->UpdateTargetURL(this, page_id, url);
[email protected]420ae012009-04-24 05:16:321586}
1587
[email protected]420ae012009-04-24 05:16:321588void TabContents::Close(RenderViewHost* rvh) {
[email protected]07707302009-11-06 00:50:291589 // The UI may be in an event-tracking loop, such as between the
1590 // mouse-down and mouse-up in text selection or a button click.
1591 // Defer the close until after tracking is complete, so that we
1592 // don't free objects out from under the UI.
1593 // TODO(shess): This could probably be integrated with the
1594 // IsDoingDrag() test below. Punting for now because I need more
1595 // research to understand how this impacts platforms other than Mac.
1596 // TODO(shess): This could get more fine-grained. For instance,
1597 // closing a tab in another window while selecting text in the
1598 // current window's Omnibox should be just fine.
1599 if (view()->IsEventTracking()) {
1600 view()->CloseTabAfterEventTracking();
1601 return;
1602 }
1603
[email protected]24a4d1062009-07-10 23:10:421604 // If we close the tab while we're in the middle of a drag, we'll crash.
1605 // Instead, cancel the drag and close it as soon as the drag ends.
1606 if (view()->IsDoingDrag()) {
1607 view()->CancelDragAndCloseTab();
1608 return;
1609 }
1610
[email protected]420ae012009-04-24 05:16:321611 // Ignore this if it comes from a RenderViewHost that we aren't showing.
1612 if (delegate() && rvh == render_view_host())
1613 delegate()->CloseContents(this);
1614}
1615
1616void TabContents::RequestMove(const gfx::Rect& new_bounds) {
[email protected]b35b26b32011-05-05 20:35:141617 if (delegate() && delegate()->IsPopupOrPanel(this))
[email protected]420ae012009-04-24 05:16:321618 delegate()->MoveContents(this, new_bounds);
1619}
1620
[email protected]7ab1e7d62009-10-14 23:32:011621void TabContents::DidStartLoading() {
[email protected]420ae012009-04-24 05:16:321622 SetIsLoading(true, NULL);
[email protected]3c9e1872010-11-18 16:17:491623
[email protected]c40d6232011-03-25 00:16:211624 if (delegate() && content_restrictions_) {
1625 content_restrictions_ = 0;
[email protected]de4761b2010-12-07 21:05:211626 delegate()->ContentRestrictionsChanged(this);
[email protected]9e823662010-10-13 23:36:001627 }
[email protected]3c9e1872010-11-18 16:17:491628
1629 // Notify observers about navigation.
[email protected]585b30362011-01-28 02:30:171630 FOR_EACH_OBSERVER(TabContentsObserver, observers_, DidStartLoading());
[email protected]420ae012009-04-24 05:16:321631}
1632
[email protected]7ab1e7d62009-10-14 23:32:011633void TabContents::DidStopLoading() {
[email protected]420ae012009-04-24 05:16:321634 scoped_ptr<LoadNotificationDetails> details;
1635
1636 NavigationEntry* entry = controller_.GetActiveEntry();
1637 // An entry may not exist for a stop when loading an initial blank page or
1638 // if an iframe injected by script into a blank page finishes loading.
1639 if (entry) {
[email protected]420ae012009-04-24 05:16:321640 base::TimeDelta elapsed = base::TimeTicks::Now() - current_load_start_;
1641
1642 details.reset(new LoadNotificationDetails(
[email protected]ebe89e062009-08-13 23:16:541643 entry->virtual_url(),
[email protected]420ae012009-04-24 05:16:321644 entry->transition_type(),
1645 elapsed,
1646 &controller_,
1647 controller_.GetCurrentEntryIndex()));
1648 }
1649
[email protected]420ae012009-04-24 05:16:321650 SetIsLoading(false, details.get());
[email protected]3c9e1872010-11-18 16:17:491651
1652 // Notify observers about navigation.
[email protected]585b30362011-01-28 02:30:171653 FOR_EACH_OBSERVER(TabContentsObserver, observers_, DidStopLoading());
[email protected]420ae012009-04-24 05:16:321654}
1655
[email protected]c95fa8b2011-04-28 20:26:161656void TabContents::DidCancelLoading() {
1657 controller_.DiscardNonCommittedEntries();
1658
1659 // Update the URL display.
1660 NotifyNavigationStateChanged(TabContents::INVALIDATE_URL);
1661}
1662
[email protected]1a3c3cb2010-12-16 21:03:401663void TabContents::DidChangeLoadProgress(double progress) {
1664 if (delegate())
1665 delegate()->LoadProgressChanged(progress);
1666}
1667
[email protected]25497492010-09-11 15:15:081668void TabContents::DocumentOnLoadCompletedInMainFrame(
1669 RenderViewHost* render_view_host,
1670 int32 page_id) {
1671 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271672 content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME,
[email protected]25497492010-09-11 15:15:081673 Source<TabContents>(this),
1674 Details<int>(&page_id));
1675}
1676
[email protected]420ae012009-04-24 05:16:321677void TabContents::RequestOpenURL(const GURL& url, const GURL& referrer,
1678 WindowOpenDisposition disposition) {
[email protected]7ade2732011-02-10 00:13:581679 if (render_manager_.web_ui()) {
[email protected]e0112912011-02-02 22:54:351680 // When we're a Web UI, it will provide a page transition type for us (this
[email protected]420ae012009-04-24 05:16:321681 // is so the new tab page can specify AUTO_BOOKMARK for automatically
1682 // generated suggestions).
1683 //
[email protected]e0112912011-02-02 22:54:351684 // Note also that we hide the referrer for Web UI pages. We don't really
[email protected]60e448982009-05-06 04:21:161685 // want web sites to see a referrer of "chrome://blah" (and some
1686 // chrome: URLs might have search terms or other stuff we don't want to
[email protected]420ae012009-04-24 05:16:321687 // send to the site), so we send no referrer.
1688 OpenURL(url, GURL(), disposition,
[email protected]7ade2732011-02-10 00:13:581689 render_manager_.web_ui()->link_transition_type());
[email protected]420ae012009-04-24 05:16:321690 } else {
1691 OpenURL(url, referrer, disposition, PageTransition::LINK);
1692 }
1693}
1694
[email protected]420ae012009-04-24 05:16:321695void TabContents::RunJavaScriptMessage(
[email protected]992db4c2011-05-12 15:37:151696 const RenderViewHost* rvh,
[email protected]4f5ce842011-05-27 19:34:411697 const string16& message,
1698 const string16& default_prompt,
[email protected]420ae012009-04-24 05:16:321699 const GURL& frame_url,
1700 const int flags,
1701 IPC::Message* reply_msg,
1702 bool* did_suppress_message) {
[email protected]3ab9cb82011-06-03 18:02:071703 // Suppress JavaScript dialogs when requested. Also suppress messages when
1704 // showing an interstitial as it's shown over the previous page and we don't
1705 // want the hidden page's dialogs to interfere with the interstitial.
[email protected]8f55e802010-12-06 18:11:501706 bool suppress_this_message =
[email protected]992db4c2011-05-12 15:37:151707 rvh->is_swapped_out() ||
[email protected]8f55e802010-12-06 18:11:501708 showing_interstitial_page() ||
[email protected]3ab9cb82011-06-03 18:02:071709 !delegate_ ||
1710 delegate_->ShouldSuppressDialogs();
[email protected]420ae012009-04-24 05:16:321711
1712 if (!suppress_this_message) {
[email protected]b627d9a2011-06-28 14:06:341713 content::JavaScriptDialogCreator::TitleType title_type;
1714 string16 title;
1715
1716 if (!frame_url.has_host()) {
1717 title_type = content::JavaScriptDialogCreator::DIALOG_TITLE_NONE;
1718 } else {
1719 title_type = content::JavaScriptDialogCreator::DIALOG_TITLE_FORMATTED_URL;
1720 title = net::FormatUrl(
1721 frame_url.GetOrigin(),
1722 content::GetContentClient()->browser()->GetAcceptLangs(this));
1723 }
1724
[email protected]2e5b90c2011-08-16 21:11:551725 dialog_creator_ = delegate_->GetJavaScriptDialogCreator();
1726 dialog_creator_->RunJavaScriptDialog(this,
1727 title_type,
1728 title,
1729 flags,
1730 message,
1731 default_prompt,
1732 reply_msg,
1733 &suppress_this_message);
[email protected]420ae012009-04-24 05:16:321734 }
[email protected]3ab9cb82011-06-03 18:02:071735
1736 if (suppress_this_message) {
1737 // If we are suppressing messages, just reply as if the user immediately
1738 // pressed "Cancel".
1739 OnDialogClosed(reply_msg, false, string16());
1740 }
1741
1742 *did_suppress_message = suppress_this_message;
[email protected]420ae012009-04-24 05:16:321743}
1744
[email protected]992db4c2011-05-12 15:37:151745void TabContents::RunBeforeUnloadConfirm(const RenderViewHost* rvh,
[email protected]4f5ce842011-05-27 19:34:411746 const string16& message,
[email protected]420ae012009-04-24 05:16:321747 IPC::Message* reply_msg) {
[email protected]5aab5e22010-12-08 22:13:291748 if (delegate())
1749 delegate()->WillRunBeforeUnloadConfirm();
[email protected]3ab9cb82011-06-03 18:02:071750
1751 bool suppress_this_message =
1752 rvh->is_swapped_out() ||
1753 !delegate_ ||
1754 delegate_->ShouldSuppressDialogs();
[email protected]992db4c2011-05-12 15:37:151755 if (suppress_this_message) {
[email protected]4f5ce842011-05-27 19:34:411756 render_view_host()->JavaScriptDialogClosed(reply_msg, true, string16());
[email protected]8f55e802010-12-06 18:11:501757 return;
1758 }
[email protected]3ab9cb82011-06-03 18:02:071759
[email protected]7ab1e7d62009-10-14 23:32:011760 is_showing_before_unload_dialog_ = true;
[email protected]2e5b90c2011-08-16 21:11:551761 dialog_creator_ = delegate_->GetJavaScriptDialogCreator();
1762 dialog_creator_->RunBeforeUnloadDialog(this,
1763 message,
1764 reply_msg);
[email protected]420ae012009-04-24 05:16:321765}
1766
[email protected]420ae012009-04-24 05:16:321767WebPreferences TabContents::GetWebkitPrefs() {
[email protected]447021c2010-09-08 21:29:081768 WebPreferences web_prefs =
[email protected]181a95ee2011-07-12 19:26:361769 content::GetContentClient()->browser()->GetWebkitPrefs(
[email protected]3d7474ff2011-07-27 17:47:371770 render_view_host()->process()->browser_context(), false);
[email protected]447021c2010-09-08 21:29:081771
[email protected]b8299c12011-06-03 19:52:281772 // Force accelerated compositing and 2d canvas off for chrome:, about: and
[email protected]2ef216b2011-08-01 19:25:211773 // chrome-devtools: pages (unless it's specifically allowed).
1774 if ((GetURL().SchemeIs(chrome::kChromeDevToolsScheme) ||
[email protected]30b8e0ee2011-09-12 14:45:571775#if !defined(TOUCH_UI)
1776 // Allow accelerated compositing for keyboard and log in screen.
[email protected]b8299c12011-06-03 19:52:281777 GetURL().SchemeIs(chrome::kChromeUIScheme) ||
[email protected]30b8e0ee2011-09-12 14:45:571778#endif
[email protected]cd4a5bd22011-09-13 06:46:301779 (GetURL().SchemeIs(chrome::kAboutScheme) &&
1780 GetURL().spec() != chrome::kAboutBlankURL)) &&
[email protected]2ef216b2011-08-01 19:25:211781 !web_prefs.allow_webui_compositing) {
[email protected]447021c2010-09-08 21:29:081782 web_prefs.accelerated_compositing_enabled = false;
[email protected]9beff752010-09-22 19:35:431783 web_prefs.accelerated_2d_canvas_enabled = false;
[email protected]447021c2010-09-08 21:29:081784 }
1785
[email protected]2f7830b32011-08-26 18:22:251786#if defined(TOUCH_UI)
1787 web_prefs.force_compositing_mode = true;
1788#endif
1789
[email protected]447021c2010-09-08 21:29:081790 return web_prefs;
[email protected]420ae012009-04-24 05:16:321791}
1792
[email protected]7d472472011-01-22 01:30:251793void TabContents::OnUserGesture() {
[email protected]ddb85052011-05-18 14:40:271794 // Notify observers.
1795 FOR_EACH_OBSERVER(TabContentsObserver, observers_, DidGetUserGesture());
[email protected]686493142011-07-15 21:47:221796
1797 ResourceDispatcherHost* rdh =
1798 content::GetContentClient()->browser()->GetResourceDispatcherHost();
1799 if (rdh) // NULL in unittests.
[email protected]8ec98cd22011-08-09 22:31:301800 rdh->OnUserGesture(this);
[email protected]7d472472011-01-22 01:30:251801}
1802
[email protected]fa1cf0b82010-01-15 21:49:441803void TabContents::OnIgnoredUIEvent() {
[email protected]31145092011-09-30 01:49:441804 // Notify observers.
1805 FOR_EACH_OBSERVER(TabContentsObserver, observers_, DidGetIgnoredUIEvent());
[email protected]fa1cf0b82010-01-15 21:49:441806}
1807
[email protected]420ae012009-04-24 05:16:321808void TabContents::RendererUnresponsive(RenderViewHost* rvh,
1809 bool is_during_unload) {
[email protected]992db4c2011-05-12 15:37:151810 // Don't show hung renderer dialog for a swapped out RVH.
1811 if (rvh != render_view_host())
1812 return;
1813
[email protected]e5fc1632011-08-08 07:51:531814 // Ignore renderer unresponsive event if debugger is attached to the tab
1815 // since the event may be a result of the renderer sitting on a breakpoint.
1816 // See http://crbug.com/65458
1817 DevToolsManager* devtools_manager = DevToolsManager::GetInstance();
1818 if (devtools_manager &&
1819 devtools_manager->GetDevToolsClientHostFor(rvh) != NULL)
1820 return;
1821
[email protected]420ae012009-04-24 05:16:321822 if (is_during_unload) {
1823 // Hang occurred while firing the beforeunload/unload handler.
1824 // Pretend the handler fired so tab closing continues as if it had.
[email protected]829e7612009-04-25 01:15:111825 rvh->set_sudden_termination_allowed(true);
[email protected]420ae012009-04-24 05:16:321826
1827 if (!render_manager_.ShouldCloseTabOnUnresponsiveRenderer())
1828 return;
1829
1830 // If the tab hangs in the beforeunload/unload handler there's really
1831 // nothing we can do to recover. Pretend the unload listeners have
1832 // all fired and close the tab. If the hang is in the beforeunload handler
1833 // then the user will not have the option of cancelling the close.
1834 Close(rvh);
1835 return;
1836 }
1837
[email protected]55452902011-06-01 21:57:471838 if (!render_view_host() || !render_view_host()->IsRenderViewLive())
1839 return;
1840
1841 if (delegate())
1842 delegate()->RendererUnresponsive(this);
[email protected]420ae012009-04-24 05:16:321843}
1844
1845void TabContents::RendererResponsive(RenderViewHost* render_view_host) {
[email protected]55452902011-06-01 21:57:471846 if (delegate())
1847 delegate()->RendererResponsive(this);
[email protected]420ae012009-04-24 05:16:321848}
1849
1850void TabContents::LoadStateChanged(const GURL& url,
[email protected]9c235f042011-08-10 22:28:211851 const net::LoadStateWithParam& load_state,
[email protected]094e5b22009-09-25 04:23:561852 uint64 upload_position,
1853 uint64 upload_size) {
[email protected]420ae012009-04-24 05:16:321854 load_state_ = load_state;
[email protected]094e5b22009-09-25 04:23:561855 upload_position_ = upload_position;
1856 upload_size_ = upload_size;
[email protected]04866c42011-05-03 20:03:501857 load_state_host_ = net::IDNToUnicode(url.host(),
[email protected]b5cca982011-05-26 04:42:081858 content::GetContentClient()->browser()->GetAcceptLangs(this));
[email protected]9c235f042011-08-10 22:28:211859 if (load_state_.state == net::LOAD_STATE_READING_RESPONSE)
[email protected]420ae012009-04-24 05:16:321860 SetNotWaitingForResponse();
[email protected]f5d978c2011-07-21 14:43:511861 if (IsLoading())
[email protected]c9cd2222009-05-06 05:16:501862 NotifyNavigationStateChanged(INVALIDATE_LOAD | INVALIDATE_TAB);
[email protected]420ae012009-04-24 05:16:321863}
1864
[email protected]7d472472011-01-22 01:30:251865void TabContents::WorkerCrashed() {
[email protected]0dd3a0ab2011-02-18 08:17:441866 if (delegate())
[email protected]55452902011-06-01 21:57:471867 delegate()->WorkerCrashed(this);
[email protected]7d472472011-01-22 01:30:251868}
1869
[email protected]420ae012009-04-24 05:16:321870void TabContents::BeforeUnloadFiredFromRenderManager(
1871 bool proceed,
1872 bool* proceed_to_fire_unload) {
1873 if (delegate())
1874 delegate()->BeforeUnloadFired(this, proceed, proceed_to_fire_unload);
1875}
1876
[email protected]3a3d47472010-07-15 21:03:541877void TabContents::DidStartLoadingFromRenderManager(
1878 RenderViewHost* render_view_host) {
1879 DidStartLoading();
1880}
1881
1882void TabContents::RenderViewGoneFromRenderManager(
1883 RenderViewHost* render_view_host) {
[email protected]443b80e2010-12-14 00:42:231884 DCHECK(crashed_status_ != base::TERMINATION_STATUS_STILL_RUNNING);
1885 RenderViewGone(render_view_host, crashed_status_, crashed_error_code_);
[email protected]3a3d47472010-07-15 21:03:541886}
1887
[email protected]420ae012009-04-24 05:16:321888void TabContents::UpdateRenderViewSizeForRenderManager() {
1889 // TODO(brettw) this is a hack. See TabContentsView::SizeContents.
[email protected]0323ee42010-02-17 22:03:261890 gfx::Size size = view_->GetContainerSize();
1891 // 0x0 isn't a valid window size (minimal window size is 1x1) but it may be
1892 // here during container initialization and normal window size will be set
1893 // later. In case of tab duplication this resizing to 0x0 prevents setting
1894 // normal size later so just ignore it.
1895 if (!size.IsEmpty())
1896 view_->SizeContents(size);
[email protected]420ae012009-04-24 05:16:321897}
1898
[email protected]3a3d47472010-07-15 21:03:541899void TabContents::NotifySwappedFromRenderManager() {
1900 NotifySwapped();
1901}
1902
1903NavigationController& TabContents::GetControllerForRenderManager() {
1904 return controller();
1905}
1906
[email protected]d0980792011-02-13 19:41:401907WebUI* TabContents::CreateWebUIForRenderManager(const GURL& url) {
[email protected]1fd1a502011-03-30 16:55:561908 return content::WebUIFactory::Get()->CreateWebUIForURL(this, url);
[email protected]420ae012009-04-24 05:16:321909}
1910
1911NavigationEntry*
1912TabContents::GetLastCommittedNavigationEntryForRenderManager() {
1913 return controller_.GetLastCommittedEntry();
1914}
1915
1916bool TabContents::CreateRenderViewForRenderManager(
1917 RenderViewHost* render_view_host) {
[email protected]420ae012009-04-24 05:16:321918 RenderWidgetHostView* rwh_view = view_->CreateViewForWidget(render_view_host);
[email protected]1a98a932009-11-17 00:12:521919
[email protected]420ae012009-04-24 05:16:321920 // Now that the RenderView has been created, we need to tell it its size.
1921 rwh_view->SetSize(view_->GetContainerSize());
1922
[email protected]a4127722011-04-27 23:13:521923 if (!render_view_host->CreateRenderView(string16()))
1924 return false;
1925
[email protected]f8233cc2011-05-31 20:24:501926#if defined(OS_LINUX)
1927 // Force a ViewMsg_Resize to be sent, needed to make plugins show up on
1928 // linux. See crbug.com/83941.
1929 if (RenderWidgetHost* render_widget_host = rwh_view->GetRenderWidgetHost())
1930 render_widget_host->WasResized();
1931#endif
1932
[email protected]420ae012009-04-24 05:16:321933 UpdateMaxPageIDIfNecessary(render_view_host->site_instance(),
1934 render_view_host);
1935 return true;
1936}
1937
[email protected]3ab9cb82011-06-03 18:02:071938void TabContents::OnDialogClosed(IPC::Message* reply_msg,
1939 bool success,
1940 const string16& user_input) {
[email protected]beb440c2009-11-06 04:08:541941 if (is_showing_before_unload_dialog_ && !success) {
1942 // If a beforeunload dialog is canceled, we need to stop the throbber from
1943 // spinning, since we forced it to start spinning in Navigate.
1944 DidStopLoading();
1945
1946 tab_close_start_time_ = base::TimeTicks();
1947 }
1948 is_showing_before_unload_dialog_ = false;
[email protected]4f5ce842011-05-27 19:34:411949 render_view_host()->JavaScriptDialogClosed(reply_msg,
1950 success,
[email protected]3ab9cb82011-06-03 18:02:071951 user_input);
[email protected]beb440c2009-11-06 04:08:541952}
1953
[email protected]3ab9cb82011-06-03 18:02:071954gfx::NativeWindow TabContents::GetDialogRootWindow() {
1955 return view_->GetTopLevelNativeWindow();
[email protected]beb440c2009-11-06 04:08:541956}
1957
[email protected]a1e97f02011-06-30 14:04:341958void TabContents::OnDialogShown() {
1959 Activate();
[email protected]3a3d47472010-07-15 21:03:541960}
1961
[email protected]41fc0322009-09-04 22:23:401962void TabContents::set_encoding(const std::string& encoding) {
[email protected]763ec4ca2011-04-29 15:48:121963 encoding_ = content::GetContentClient()->browser()->
1964 GetCanonicalEncodingNameByAliasName(encoding);
[email protected]aacd7cc2009-09-02 14:51:101965}
[email protected]f45d2a72010-03-08 23:28:351966
[email protected]33f74972010-12-08 16:40:361967void TabContents::CreateViewAndSetSizeForRVH(RenderViewHost* rvh) {
1968 RenderWidgetHostView* rwh_view = view()->CreateViewForWidget(rvh);
1969 rwh_view->SetSize(view()->GetContainerSize());
1970}