blob: 5b987343ed6bc6bb120aa01379ae1375adee956c [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]b46442d7e2011-06-29 02:16:0618#include "content/browser/debugger/devtools_manager.h"
[email protected]71bf3f5e2011-08-15 21:05:2219#include "content/browser/download/download_manager.h"
[email protected]8bd9e562011-08-16 23:55:4620#include "content/browser/download/download_stats.h"
[email protected]567812d2011-02-24 17:40:5021#include "content/browser/host_zoom_map.h"
22#include "content/browser/in_process_webkit/session_storage_namespace.h"
[email protected]37a72af2011-06-13 05:42:0123#include "content/browser/load_from_memory_cache_details.h"
[email protected]35e251d2011-05-24 21:01:0424#include "content/browser/load_notification_details.h"
[email protected]567812d2011-02-24 17:40:5025#include "content/browser/renderer_host/render_process_host.h"
26#include "content/browser/renderer_host/render_view_host.h"
27#include "content/browser/renderer_host/render_widget_host_view.h"
[email protected]686493142011-07-15 21:47:2228#include "content/browser/renderer_host/resource_dispatcher_host.h"
[email protected]567812d2011-02-24 17:40:5029#include "content/browser/renderer_host/resource_request_details.h"
30#include "content/browser/site_instance.h"
[email protected]0dd3a0ab2011-02-18 08:17:4431#include "content/browser/tab_contents/interstitial_page.h"
[email protected]8286f51a2011-05-31 17:39:1332#include "content/browser/tab_contents/navigation_details.h"
[email protected]0dd3a0ab2011-02-18 08:17:4433#include "content/browser/tab_contents/navigation_entry.h"
34#include "content/browser/tab_contents/provisional_load_details.h"
35#include "content/browser/tab_contents/tab_contents_delegate.h"
36#include "content/browser/tab_contents/tab_contents_observer.h"
37#include "content/browser/tab_contents/tab_contents_view.h"
[email protected]105bb0f2011-05-24 17:12:1438#include "content/browser/tab_contents/title_updated_details.h"
[email protected]afd1e522011-04-27 23:29:5939#include "content/browser/user_metrics.h"
[email protected]1fd1a502011-03-30 16:55:5640#include "content/browser/webui/web_ui_factory.h"
[email protected]19e81142011-10-03 16:19:4041#include "content/common/content_constants.h"
[email protected]763ec4ca2011-04-29 15:48:1242#include "content/common/content_restriction.h"
[email protected]ce9751942011-09-21 01:57:2443#include "content/common/intents_messages.h"
[email protected]7f070d42011-03-09 20:25:3244#include "content/common/notification_service.h"
[email protected]2c5569662011-03-22 20:45:0245#include "content/common/view_messages.h"
[email protected]87f3c082011-10-19 18:07:4446#include "content/public/browser/content_browser_client.h"
[email protected]610cbb82011-10-18 16:35:0847#include "content/public/browser/navigation_types.h"
[email protected]e091df82011-10-11 18:13:2148#include "content/public/common/bindings_policy.h"
[email protected]a1d29162011-10-14 17:14:0349#include "content/public/common/url_constants.h"
[email protected]da4dfc42011-10-12 15:53:5650#include "content/public/common/view_types.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]6c2381d2011-10-19 02:52:53227 content::Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:32228 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]f5d978c2011-07-21 14:43:51401bool TabContents::IsLoading() const {
402 return is_loading_ || (web_ui() && web_ui()->IsLoading());
403}
404
[email protected]585b30362011-01-28 02:30:17405void TabContents::AddObserver(TabContentsObserver* observer) {
406 observers_.AddObserver(observer);
[email protected]3c9e1872010-11-18 16:17:49407}
408
[email protected]585b30362011-01-28 02:30:17409void TabContents::RemoveObserver(TabContentsObserver* observer) {
410 observers_.RemoveObserver(observer);
[email protected]3c9e1872010-11-18 16:17:49411}
412
[email protected]443b80e2010-12-14 00:42:23413void TabContents::SetIsCrashed(base::TerminationStatus status, int error_code) {
414 if (status == crashed_status_)
[email protected]d5f942ba2008-09-26 19:30:34415 return;
416
[email protected]443b80e2010-12-14 00:42:23417 crashed_status_ = status;
418 crashed_error_code_ = error_code;
[email protected]c9cd2222009-05-06 05:16:50419 NotifyNavigationStateChanged(INVALIDATE_TAB);
[email protected]d5f942ba2008-09-26 19:30:34420}
421
422void TabContents::NotifyNavigationStateChanged(unsigned changed_flags) {
423 if (delegate_)
424 delegate_->NavigationStateChanged(this, changed_flags);
425}
426
[email protected]96d185d2009-04-24 03:28:54427void TabContents::DidBecomeSelected() {
428 controller_.SetActive(true);
[email protected]8cb5d5b2010-02-09 11:36:16429 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
430 if (rwhv) {
431 rwhv->DidBecomeSelected();
[email protected]789e9152009-08-04 21:59:43432#if defined(OS_MACOSX)
[email protected]8cb5d5b2010-02-09 11:36:16433 rwhv->SetActive(true);
[email protected]789e9152009-08-04 21:59:43434#endif
435 }
[email protected]96d185d2009-04-24 03:28:54436
[email protected]5ac20162010-11-24 23:33:11437 last_selected_time_ = base::TimeTicks::Now();
[email protected]3e69bc82011-05-26 23:22:38438
439 FOR_EACH_OBSERVER(TabContentsObserver, observers_, DidBecomeSelected());
[email protected]96d185d2009-04-24 03:28:54440}
441
442void TabContents::WasHidden() {
443 if (!capturing_contents()) {
444 // |render_view_host()| can be NULL if the user middle clicks a link to open
445 // a tab in then background, then closes the tab before selecting it. This
446 // is because closing the tab calls TabContents::Destroy(), which removes
447 // the |render_view_host()|; then when we actually destroy the window,
448 // OnWindowPosChanged() notices and calls HideContents() (which calls us).
[email protected]8cb5d5b2010-02-09 11:36:16449 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
450 if (rwhv)
451 rwhv->WasHidden();
[email protected]96d185d2009-04-24 03:28:54452 }
453
454 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:27455 content::NOTIFICATION_TAB_CONTENTS_HIDDEN,
[email protected]6c2381d2011-10-19 02:52:53456 content::Source<TabContents>(this),
[email protected]96d185d2009-04-24 03:28:54457 NotificationService::NoDetails());
458}
459
[email protected]d5f942ba2008-09-26 19:30:34460void TabContents::Activate() {
461 if (delegate_)
462 delegate_->ActivateContents(this);
463}
464
[email protected]ea42e7782010-08-23 23:58:12465void TabContents::Deactivate() {
466 if (delegate_)
467 delegate_->DeactivateContents(this);
468}
469
[email protected]63954792011-07-11 04:17:48470void TabContents::LostCapture() {
471 if (delegate_)
472 delegate_->LostCapture();
473}
474
475bool TabContents::PreHandleKeyboardEvent(const NativeWebKeyboardEvent& event,
476 bool* is_keyboard_shortcut) {
477 return delegate_ &&
478 delegate_->PreHandleKeyboardEvent(event, is_keyboard_shortcut);
479}
480
481void TabContents::HandleKeyboardEvent(const NativeWebKeyboardEvent& event) {
482 if (delegate_)
483 delegate_->HandleKeyboardEvent(event);
484}
485
486void TabContents::HandleMouseUp() {
487 if (delegate_)
488 delegate_->HandleMouseUp();
489}
490
491void TabContents::HandleMouseActivate() {
492 if (delegate_)
493 delegate_->HandleMouseActivate();
494}
495
[email protected]8a5e0ca2011-08-25 06:30:47496void TabContents::ToggleFullscreenMode(bool enter_fullscreen) {
497 if (delegate_)
498 delegate_->ToggleFullscreenModeForTab(this, enter_fullscreen);
499}
500
[email protected]5d5f7af2011-10-01 01:38:12501bool TabContents::IsFullscreenForCurrentTab() const {
502 return delegate_ ? delegate_->IsFullscreenForTab(this) : false;
503}
504
[email protected]e9621112011-10-17 05:38:37505void TabContents::RequestToLockMouse() {
506 if (delegate_) {
507 delegate_->RequestToLockMouse(this);
508 } else {
509 GotResponseToLockMouseRequest(false);
510 }
511}
512
513void TabContents::LostMouseLock() {
514 if (delegate_)
515 delegate_->LostMouseLock();
516}
517
[email protected]0548c5352011-09-07 00:33:33518void TabContents::UpdatePreferredSize(const gfx::Size& pref_size) {
519 if (delegate_)
520 delegate_->UpdatePreferredSize(this, pref_size);
521}
522
[email protected]96d185d2009-04-24 03:28:54523void TabContents::ShowContents() {
[email protected]8cb5d5b2010-02-09 11:36:16524 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
525 if (rwhv)
526 rwhv->DidBecomeSelected();
[email protected]96d185d2009-04-24 03:28:54527}
528
529void TabContents::HideContents() {
530 // TODO(pkasting): http://b/1239839 Right now we purposefully don't call
531 // our superclass HideContents(), because some callers want to be very picky
532 // about the order in which these get called. In addition to making the code
533 // here practically impossible to understand, this also means we end up
534 // calling TabContents::WasHidden() twice if callers call both versions of
[email protected]57c6a652009-05-04 07:58:34535 // HideContents() on a TabContents.
[email protected]96d185d2009-04-24 03:28:54536 WasHidden();
537}
538
[email protected]5aab5e22010-12-08 22:13:29539bool TabContents::NeedToFireBeforeUnload() {
540 // TODO(creis): Should we fire even for interstitial pages?
541 return notify_disconnection() &&
542 !showing_interstitial_page() &&
543 !render_view_host()->SuddenTerminationAllowed();
544}
545
[email protected]00c37fc2011-08-02 00:22:50546// TODO(adriansc): Remove this method once refactoring changed all call sites.
[email protected]992e4542011-07-20 23:09:25547TabContents* TabContents::OpenURL(const GURL& url,
548 const GURL& referrer,
549 WindowOpenDisposition disposition,
[email protected]2905f742011-10-13 03:51:58550 content::PageTransition transition) {
[email protected]e47ae9472011-10-13 19:48:34551 return OpenURL(OpenURLParams(url, referrer, disposition, transition,
552 false));
[email protected]00c37fc2011-08-02 00:22:50553}
554
555TabContents* TabContents::OpenURL(const OpenURLParams& params) {
[email protected]1c642b52011-04-15 09:05:49556 if (delegate_) {
[email protected]00c37fc2011-08-02 00:22:50557 TabContents* new_contents = delegate_->OpenURLFromTab(this, params);
[email protected]1c642b52011-04-15 09:05:49558 // Notify observers.
559 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
[email protected]00c37fc2011-08-02 00:22:50560 DidOpenURL(params.url, params.referrer,
561 params.disposition, params.transition));
[email protected]992e4542011-07-20 23:09:25562 return new_contents;
[email protected]1c642b52011-04-15 09:05:49563 }
[email protected]992e4542011-07-20 23:09:25564 return NULL;
[email protected]d5f942ba2008-09-26 19:30:34565}
566
[email protected]1ccb3568d2010-02-19 10:51:16567bool TabContents::NavigateToPendingEntry(
568 NavigationController::ReloadType reload_type) {
[email protected]876bc832010-09-07 16:29:54569 return NavigateToEntry(*controller_.pending_entry(), reload_type);
570}
[email protected]96d185d2009-04-24 03:28:54571
[email protected]876bc832010-09-07 16:29:54572bool TabContents::NavigateToEntry(
573 const NavigationEntry& entry,
574 NavigationController::ReloadType reload_type) {
[email protected]19e81142011-10-03 16:19:40575 // The renderer will reject IPC messages with URLs longer than
576 // this limit, so don't attempt to navigate with a longer URL.
577 if (entry.url().spec().size() > content::kMaxURLChars)
578 return false;
579
[email protected]ffc906f2011-10-04 22:55:40580 RenderViewHost* dest_render_view_host = render_manager_.Navigate(entry);
581 if (!dest_render_view_host)
582 return false; // Unable to create the desired render view host.
583
[email protected]80a8fad2011-01-29 04:02:38584 // For security, we should never send non-Web-UI URLs to a Web UI renderer.
[email protected]05fdd492010-11-15 17:52:07585 // Double check that here.
[email protected]54ec6472010-04-09 19:39:58586 int enabled_bindings = dest_render_view_host->enabled_bindings();
[email protected]1fd1a502011-03-30 16:55:56587 bool is_allowed_in_web_ui_renderer = content::GetContentClient()->
[email protected]3d7474ff2011-07-27 17:47:37588 browser()->GetWebUIFactory()->IsURLAcceptableForWebUI(browser_context(),
[email protected]1fd1a502011-03-30 16:55:56589 entry.url());
[email protected]e091df82011-10-11 18:13:21590 CHECK(!(enabled_bindings & content::BINDINGS_POLICY_WEB_UI) ||
[email protected]c09163a2011-02-15 00:05:55591 is_allowed_in_web_ui_renderer);
[email protected]54ec6472010-04-09 19:39:58592
[email protected]96d185d2009-04-24 03:28:54593 // Tell DevTools agent that it is attached prior to the navigation.
[email protected]d9f9b792009-06-24 13:17:12594 DevToolsManager* devtools_manager = DevToolsManager::GetInstance();
[email protected]7e8e6b62009-05-08 11:28:32595 if (devtools_manager) { // NULL in unit tests.
[email protected]3cc72b12010-03-18 23:03:00596 devtools_manager->OnNavigatingToPendingEntry(render_view_host(),
597 dest_render_view_host,
598 entry.url());
[email protected]7e8e6b62009-05-08 11:28:32599 }
[email protected]96d185d2009-04-24 03:28:54600
601 // Used for page load time metrics.
602 current_load_start_ = base::TimeTicks::Now();
603
604 // Navigate in the desired RenderViewHost.
[email protected]056de2d2009-06-26 16:41:34605 ViewMsg_Navigate_Params navigate_params;
[email protected]6c6b02d2011-09-02 03:36:47606 MakeNavigateParams(entry, controller_, delegate_, reload_type,
607 &navigate_params);
[email protected]056de2d2009-06-26 16:41:34608 dest_render_view_host->Navigate(navigate_params);
[email protected]96d185d2009-04-24 03:28:54609
610 if (entry.page_id() == -1) {
611 // HACK!! This code suppresses javascript: URLs from being added to
612 // session history, which is what we want to do for javascript: URLs that
613 // do not generate content. What we really need is a message from the
614 // renderer telling us that a new page was not created. The same message
615 // could be used for mailto: URLs and the like.
616 if (entry.url().SchemeIs(chrome::kJavaScriptScheme))
617 return false;
618 }
619
[email protected]3c9e1872010-11-18 16:17:49620 // Notify observers about navigation.
[email protected]b375c5d2011-05-03 21:15:04621 FOR_EACH_OBSERVER(TabContentsObserver,
622 observers_,
623 NavigateToPendingEntry(entry.url(), reload_type));
[email protected]96d185d2009-04-24 03:28:54624
[email protected]09b29342011-06-24 19:18:48625 if (delegate_)
626 delegate_->DidNavigateToPendingEntry(this);
627
[email protected]d5f942ba2008-09-26 19:30:34628 return true;
629}
630
[email protected]9e1ad4b2011-08-14 16:49:19631void TabContents::SetHistoryLengthAndPrune(const SiteInstance* site_instance,
632 int history_length,
633 int32 minimum_page_id) {
[email protected]5229253b2011-08-16 14:36:41634 // SetHistoryLengthAndPrune doesn't work when there are pending cross-site
635 // navigations. Callers should ensure that this is the case.
[email protected]9e1ad4b2011-08-14 16:49:19636 if (render_manager_.pending_render_view_host()) {
637 NOTREACHED();
[email protected]796931a92011-08-10 01:32:14638 return;
[email protected]9e1ad4b2011-08-14 16:49:19639 }
640 RenderViewHost* rvh = render_view_host();
641 if (!rvh) {
642 NOTREACHED();
643 return;
644 }
645 if (site_instance && rvh->site_instance() != site_instance) {
646 NOTREACHED();
647 return;
648 }
649 rvh->Send(new ViewMsg_SetHistoryLengthAndPrune(rvh->routing_id(),
650 history_length,
651 minimum_page_id));
[email protected]796931a92011-08-10 01:32:14652}
653
[email protected]96d185d2009-04-24 03:28:54654void TabContents::Stop() {
655 render_manager_.Stop();
[email protected]2e3f4572011-03-25 19:24:47656 FOR_EACH_OBSERVER(TabContentsObserver, observers_, StopNavigation());
[email protected]96d185d2009-04-24 03:28:54657}
658
[email protected]96d185d2009-04-24 03:28:54659TabContents* TabContents::Clone() {
660 // We create a new SiteInstance so that the new tab won't share processes
661 // with the old one. This can be changed in the future if we need it to share
662 // processes for some reason.
[email protected]3d7474ff2011-07-27 17:47:37663 TabContents* tc = new TabContents(
664 browser_context(),
665 SiteInstance::CreateSiteInstance(browser_context()),
666 MSG_ROUTING_NONE, this, NULL);
[email protected]96d185d2009-04-24 03:28:54667 tc->controller().CopyStateFrom(controller_);
668 return tc;
669}
670
[email protected]4d677202009-07-19 07:37:12671void TabContents::ShowPageInfo(const GURL& url,
672 const NavigationEntry::SSLStatus& ssl,
673 bool show_history) {
674 if (!delegate_)
675 return;
676
[email protected]3d7474ff2011-07-27 17:47:37677 delegate_->ShowPageInfo(browser_context(), url, ssl, show_history);
[email protected]4d677202009-07-19 07:37:12678}
679
[email protected]e7cfdbd2011-04-22 14:41:37680void TabContents::AddNewContents(TabContents* new_contents,
681 WindowOpenDisposition disposition,
682 const gfx::Rect& initial_pos,
683 bool user_gesture) {
initial.commit09911bf2008-07-26 23:55:29684 if (!delegate_)
685 return;
686
[email protected]e7cfdbd2011-04-22 14:41:37687 delegate_->AddNewContents(this, new_contents, disposition, initial_pos,
688 user_gesture);
[email protected]96d185d2009-04-24 03:28:54689}
690
[email protected]5c9e97a2009-09-09 23:48:30691gfx::NativeView TabContents::GetContentNativeView() const {
[email protected]96d185d2009-04-24 03:28:54692 return view_->GetContentNativeView();
693}
694
695gfx::NativeView TabContents::GetNativeView() const {
696 return view_->GetNativeView();
697}
698
699void TabContents::GetContainerBounds(gfx::Rect *out) const {
700 view_->GetContainerBounds(out);
701}
702
703void TabContents::Focus() {
704 view_->Focus();
705}
706
[email protected]90daadb42009-06-08 21:27:28707void TabContents::FocusThroughTabTraversal(bool reverse) {
[email protected]7e383692009-06-12 19:14:54708 if (showing_interstitial_page()) {
709 render_manager_.interstitial_page()->FocusThroughTabTraversal(reverse);
710 return;
711 }
[email protected]96d185d2009-04-24 03:28:54712 render_view_host()->SetInitialFocus(reverse);
713}
714
715bool TabContents::FocusLocationBarByDefault() {
[email protected]d0980792011-02-13 19:41:40716 WebUI* web_ui = GetWebUIForCurrentState();
[email protected]7ade2732011-02-10 00:13:58717 if (web_ui)
718 return web_ui->focus_location_bar_by_default();
[email protected]3e3f0eb2009-06-22 18:33:43719 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]e0d481582009-09-15 21:06:25720 if (entry && entry->url() == GURL(chrome::kAboutBlankURL))
[email protected]3e3f0eb2009-06-22 18:33:43721 return true;
[email protected]96d185d2009-04-24 03:28:54722 return false;
723}
724
[email protected]a26dc362010-04-23 01:48:58725void TabContents::SetFocusToLocationBar(bool select_all) {
[email protected]a11aaf22010-03-30 00:03:38726 if (delegate())
[email protected]a26dc362010-04-23 01:48:58727 delegate()->SetFocusToLocationBar(select_all);
[email protected]a11aaf22010-03-30 00:03:38728}
729
[email protected]686493142011-07-15 21:47:22730bool TabContents::CanDownload(int request_id) {
731 TabContentsDelegate* d = delegate();
732 if (d)
733 return d->CanDownload(this, request_id);
734 return true;
735}
736
737void TabContents::OnStartDownload(DownloadItem* download) {
738 TabContentsDelegate* d = delegate();
739 if (d)
740 d->OnStartDownload(this, download);
741}
742
[email protected]c7dd2f62011-07-18 15:57:59743void TabContents::OnSavePage() {
744 // If we can not save the page, try to download it.
745 if (!SavePackage::IsSavableContents(contents_mime_type())) {
[email protected]3d7474ff2011-07-27 17:47:37746 DownloadManager* dlm = browser_context()->GetDownloadManager();
[email protected]c7dd2f62011-07-18 15:57:59747 const GURL& current_page_url = GetURL();
748 if (dlm && current_page_url.is_valid()) {
749 dlm->DownloadUrl(current_page_url, GURL(), "", this);
[email protected]8bd9e562011-08-16 23:55:46750 download_stats::RecordDownloadCount(
751 download_stats::INITIATED_BY_SAVE_PACKAGE_FAILURE_COUNT);
[email protected]c7dd2f62011-07-18 15:57:59752 return;
753 }
754 }
755
756 Stop();
757
758 // Create the save package and possibly prompt the user for the name to save
759 // the page as. The user prompt is an asynchronous operation that runs on
760 // another thread.
761 save_package_ = new SavePackage(this);
762 save_package_->GetSaveInfo();
763}
764
765// Used in automated testing to bypass prompting the user for file names.
766// Instead, the names and paths are hard coded rather than running them through
767// file name sanitation and extension / mime checking.
768bool TabContents::SavePage(const FilePath& main_file, const FilePath& dir_path,
769 SavePackage::SavePackageType save_type) {
770 // Stop the page from navigating.
771 Stop();
772
773 save_package_ = new SavePackage(this, save_type, main_file, dir_path);
774 return save_package_->Init();
775}
776
777void TabContents::OnSaveURL(const GURL& url) {
[email protected]3d7474ff2011-07-27 17:47:37778 DownloadManager* dlm = browser_context()->GetDownloadManager();
[email protected]c7dd2f62011-07-18 15:57:59779 dlm->DownloadUrl(url, GetURL(), "", this);
780}
781
[email protected]420ae012009-04-24 05:16:32782bool TabContents::IsActiveEntry(int32 page_id) {
783 NavigationEntry* active_entry = controller_.GetActiveEntry();
784 return (active_entry != NULL &&
785 active_entry->site_instance() == GetSiteInstance() &&
786 active_entry->page_id() == page_id);
787}
788
[email protected]b2fe07d12010-02-09 14:38:08789void TabContents::SetOverrideEncoding(const std::string& encoding) {
[email protected]8cb5d5b2010-02-09 11:36:16790 set_encoding(encoding);
[email protected]216813952011-05-19 22:21:26791 render_view_host()->Send(new ViewMsg_SetPageEncoding(
792 render_view_host()->routing_id(), encoding));
[email protected]8cb5d5b2010-02-09 11:36:16793}
794
[email protected]b2fe07d12010-02-09 14:38:08795void TabContents::ResetOverrideEncoding() {
[email protected]8cb5d5b2010-02-09 11:36:16796 reset_encoding();
[email protected]216813952011-05-19 22:21:26797 render_view_host()->Send(new ViewMsg_ResetPageEncodingToDefault(
798 render_view_host()->routing_id()));
[email protected]8cb5d5b2010-02-09 11:36:16799}
800
[email protected]7ab1e7d62009-10-14 23:32:01801void TabContents::OnCloseStarted() {
802 if (tab_close_start_time_.is_null())
803 tab_close_start_time_ = base::TimeTicks::Now();
804}
805
[email protected]46624bf2010-06-09 16:04:19806bool TabContents::ShouldAcceptDragAndDrop() const {
807#if defined(OS_CHROMEOS)
808 // ChromeOS panels (pop-ups) do not take drag-n-drop.
809 // See http://crosbug.com/2413
[email protected]b35b26b32011-05-05 20:35:14810 if (delegate() && delegate()->IsPopupOrPanel(this))
[email protected]b83af492010-10-09 03:21:20811 return false;
812 return true;
[email protected]46624bf2010-06-09 16:04:19813#else
814 return true;
815#endif
816}
817
[email protected]7813bd72011-02-05 02:19:34818void TabContents::SystemDragEnded() {
819 if (render_view_host())
820 render_view_host()->DragSourceSystemDragEnded();
821 if (delegate())
822 delegate()->DragEnded();
823}
824
[email protected]d0b8d092010-10-25 04:05:17825double TabContents::GetZoomLevel() const {
[email protected]3d7474ff2011-07-27 17:47:37826 HostZoomMap* zoom_map = browser_context()->GetHostZoomMap();
[email protected]b75b8292010-10-01 07:28:25827 if (!zoom_map)
[email protected]d0b8d092010-10-25 04:05:17828 return 0;
[email protected]b75b8292010-10-01 07:28:25829
830 double zoom_level;
831 if (temporary_zoom_settings_) {
832 zoom_level = zoom_map->GetTemporaryZoomLevel(
833 GetRenderProcessHost()->id(), render_view_host()->routing_id());
834 } else {
[email protected]2ae88d12011-10-14 09:11:19835 GURL url;
836 NavigationEntry* active_entry = controller().GetActiveEntry();
837 // Since zoom map is updated using rewritten URL, use rewritten URL
838 // to get the zoom level.
839 url = active_entry ? active_entry->url() : GURL::EmptyGURL();
840 zoom_level = zoom_map->GetZoomLevel(net::GetHostOrSpecFromURL(url));
[email protected]b75b8292010-10-01 07:28:25841 }
[email protected]d0b8d092010-10-25 04:05:17842 return zoom_level;
843}
[email protected]b75b8292010-10-01 07:28:25844
[email protected]d0b8d092010-10-25 04:05:17845int TabContents::GetZoomPercent(bool* enable_increment,
846 bool* enable_decrement) {
847 *enable_decrement = *enable_increment = false;
[email protected]b75b8292010-10-01 07:28:25848 int percent = static_cast<int>(
[email protected]d0b8d092010-10-25 04:05:17849 WebKit::WebView::zoomLevelToZoomFactor(GetZoomLevel()) * 100);
[email protected]b75b8292010-10-01 07:28:25850 *enable_decrement = percent > minimum_zoom_percent_;
851 *enable_increment = percent < maximum_zoom_percent_;
852 return percent;
853}
854
[email protected]3c733bde2010-12-21 19:56:31855void TabContents::ViewSource() {
[email protected]1788e772010-12-15 16:40:50856 if (!delegate_)
857 return;
858
859 NavigationEntry* active_entry = controller().GetActiveEntry();
860 if (!active_entry)
861 return;
862
863 delegate_->ViewSourceForTab(this, active_entry->url());
[email protected]77d8d622010-12-15 10:30:12864}
865
[email protected]932b7a12011-03-09 12:50:27866void TabContents::ViewFrameSource(const GURL& url,
867 const std::string& content_state) {
868 if (!delegate_)
869 return;
870
871 delegate_->ViewSourceForFrame(this, url, content_state);
872}
873
[email protected]c40d6232011-03-25 00:16:21874void TabContents::SetContentRestrictions(int restrictions) {
875 content_restrictions_ = restrictions;
876 delegate()->ContentRestrictionsChanged(this);
877}
878
[email protected]724159a2010-12-30 01:11:18879void TabContents::OnDidStartProvisionalLoadForFrame(int64 frame_id,
880 bool is_main_frame,
[email protected]57b9396c2011-10-07 19:11:59881 const GURL& opener_url,
[email protected]724159a2010-12-30 01:11:18882 const GURL& url) {
883 bool is_error_page = (url.spec() == chrome::kUnreachableWebDataURL);
884 GURL validated_url(url);
885 render_view_host()->FilterURL(ChildProcessSecurityPolicy::GetInstance(),
886 GetRenderProcessHost()->id(), &validated_url);
887
[email protected]8093a542011-05-13 07:29:32888 RenderViewHost* rvh =
889 render_manager_.pending_render_view_host() ?
890 render_manager_.pending_render_view_host() : render_view_host();
[email protected]0d60f0192011-04-14 12:40:10891 // Notify observers about the start of the provisional load.
892 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
893 DidStartProvisionalLoadForFrame(frame_id, is_main_frame,
[email protected]8093a542011-05-13 07:29:32894 validated_url, is_error_page, rvh));
[email protected]0d60f0192011-04-14 12:40:10895
[email protected]724159a2010-12-30 01:11:18896 if (is_main_frame) {
[email protected]4850a7f2011-03-08 23:36:59897 // Notify observers about the provisional change in the main frame URL.
898 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
[email protected]57b9396c2011-10-07 19:11:59899 ProvisionalChangeToMainFrameUrl(url, opener_url));
[email protected]724159a2010-12-30 01:11:18900 }
901}
902
903void TabContents::OnDidRedirectProvisionalLoad(int32 page_id,
[email protected]57b9396c2011-10-07 19:11:59904 const GURL& opener_url,
[email protected]724159a2010-12-30 01:11:18905 const GURL& source_url,
906 const GURL& target_url) {
[email protected]18ba2b12011-04-06 16:35:49907 // TODO(creis): Remove this method and have the pre-rendering code listen to
908 // the ResourceDispatcherHost's RESOURCE_RECEIVED_REDIRECT notification
909 // instead. See http://crbug.com/78512.
[email protected]724159a2010-12-30 01:11:18910 NavigationEntry* entry;
911 if (page_id == -1)
912 entry = controller_.pending_entry();
913 else
914 entry = controller_.GetEntryWithPageID(GetSiteInstance(), page_id);
915 if (!entry || entry->url() != source_url)
916 return;
[email protected]e7d50892011-01-19 21:47:38917
[email protected]4850a7f2011-03-08 23:36:59918 // Notify observers about the provisional change in the main frame URL.
919 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
[email protected]eacb080b2011-05-22 19:40:26920 ProvisionalChangeToMainFrameUrl(target_url,
[email protected]57b9396c2011-10-07 19:11:59921 opener_url));
[email protected]724159a2010-12-30 01:11:18922}
923
924void TabContents::OnDidFailProvisionalLoadWithError(
[email protected]d7b175e2011-10-11 15:31:58925 const ViewHostMsg_DidFailProvisionalLoadWithError_Params& params) {
926 VLOG(1) << "Failed Provisional Load: " << params.url.possibly_invalid_spec()
927 << ", error_code: " << params.error_code
928 << ", error_description: " << params.error_description
929 << ", is_main_frame: " << params.is_main_frame
930 << ", showing_repost_interstitial: " <<
931 params.showing_repost_interstitial
932 << ", frame_id: " << params.frame_id;
933 GURL validated_url(params.url);
[email protected]724159a2010-12-30 01:11:18934 render_view_host()->FilterURL(ChildProcessSecurityPolicy::GetInstance(),
935 GetRenderProcessHost()->id(), &validated_url);
936
[email protected]d7b175e2011-10-11 15:31:58937 if (net::ERR_ABORTED == params.error_code) {
[email protected]724159a2010-12-30 01:11:18938 // EVIL HACK ALERT! Ignore failed loads when we're showing interstitials.
939 // This means that the interstitial won't be torn down properly, which is
940 // bad. But if we have an interstitial, go back to another tab type, and
941 // then load the same interstitial again, we could end up getting the first
942 // interstitial's "failed" message (as a result of the cancel) when we're on
943 // the second one.
944 //
945 // We can't tell this apart, so we think we're tearing down the current page
946 // which will cause a crash later one. There is also some code in
947 // RenderViewHostManager::RendererAbortedProvisionalLoad that is commented
948 // out because of this problem.
949 //
950 // http://code.google.com/p/chromium/issues/detail?id=2855
951 // Because this will not tear down the interstitial properly, if "back" is
952 // back to another tab type, the interstitial will still be somewhat alive
953 // in the previous tab type. If you navigate somewhere that activates the
954 // tab with the interstitial again, you'll see a flash before the new load
955 // commits of the interstitial page.
956 if (showing_interstitial_page()) {
957 LOG(WARNING) << "Discarding message during interstitial.";
958 return;
959 }
960
[email protected]02102f82011-06-13 20:37:02961 // Discard our pending entry if the load canceled (e.g. if we decided to
962 // download the file instead of load it). We do not verify that the URL
963 // being canceled matches the pending entry's URL because they will not
964 // match if a redirect occurred (in which case we do not want to leave a
965 // stale redirect URL showing). This means that we also cancel the pending
966 // entry if the user started a new navigation. As a result, the navigation
967 // controller may not remember that a load is in progress, but the
968 // navigation will still commit even if there is no pending entry.
[email protected]724159a2010-12-30 01:11:18969 NavigationEntry* pending_entry = controller_.pending_entry();
[email protected]02102f82011-06-13 20:37:02970 if (pending_entry)
[email protected]c95fa8b2011-04-28 20:26:16971 DidCancelLoading();
[email protected]724159a2010-12-30 01:11:18972
973 render_manager_.RendererAbortedProvisionalLoad(render_view_host());
974 }
975
976 // Send out a notification that we failed a provisional load with an error.
977 ProvisionalLoadDetails details(
[email protected]d7b175e2011-10-11 15:31:58978 params.is_main_frame,
979 controller_.IsURLInPageNavigation(validated_url),
980 validated_url,
981 std::string(),
982 false,
983 params.frame_id);
984 details.set_error_code(params.error_code);
[email protected]724159a2010-12-30 01:11:18985
986 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:27987 content::NOTIFICATION_FAIL_PROVISIONAL_LOAD_WITH_ERROR,
[email protected]6c2381d2011-10-19 02:52:53988 content::Source<NavigationController>(&controller_),
989 content::Details<ProvisionalLoadDetails>(&details));
[email protected]0d60f0192011-04-14 12:40:10990
[email protected]d7b175e2011-10-11 15:31:58991 FOR_EACH_OBSERVER(TabContentsObserver,
992 observers_,
993 DidFailProvisionalLoad(params.frame_id,
994 params.is_main_frame,
995 validated_url,
996 params.error_code,
997 params.error_description));
[email protected]724159a2010-12-30 01:11:18998}
999
1000void TabContents::OnDidLoadResourceFromMemoryCache(
1001 const GURL& url,
[email protected]70435962011-08-02 20:13:281002 const std::string& security_info,
1003 const std::string& http_method,
1004 ResourceType::Type resource_type) {
[email protected]28620862011-03-22 23:07:191005 base::StatsCounter cache("WebKit.CacheHit");
[email protected]724159a2010-12-30 01:11:181006 cache.Increment();
1007
1008 // Send out a notification that we loaded a resource from our memory cache.
[email protected]70d66502011-09-23 00:55:081009 int cert_id = 0;
1010 net::CertStatus cert_status = 0;
1011 int security_bits = -1;
1012 int connection_status = 0;
[email protected]724159a2010-12-30 01:11:181013 SSLManager::DeserializeSecurityInfo(security_info,
1014 &cert_id, &cert_status,
1015 &security_bits,
1016 &connection_status);
[email protected]7a8c55e2011-02-15 08:21:161017 LoadFromMemoryCacheDetails details(url, GetRenderProcessHost()->id(),
1018 cert_id, cert_status);
[email protected]724159a2010-12-30 01:11:181019
1020 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271021 content::NOTIFICATION_LOAD_FROM_MEMORY_CACHE,
[email protected]6c2381d2011-10-19 02:52:531022 content::Source<NavigationController>(&controller_),
1023 content::Details<LoadFromMemoryCacheDetails>(&details));
[email protected]724159a2010-12-30 01:11:181024}
1025
1026void TabContents::OnDidDisplayInsecureContent() {
[email protected]221a5d92011-04-28 18:23:161027 UserMetrics::RecordAction(UserMetricsAction("SSL.DisplayedInsecureContent"));
[email protected]724159a2010-12-30 01:11:181028 displayed_insecure_content_ = true;
[email protected]9678d8f2011-08-05 04:05:071029 SSLManager::NotifySSLInternalStateChanged(&controller());
[email protected]724159a2010-12-30 01:11:181030}
1031
1032void TabContents::OnDidRunInsecureContent(
[email protected]92771112011-01-20 00:13:021033 const std::string& security_origin, const GURL& target_url) {
[email protected]9941cf52011-02-08 16:17:231034 LOG(INFO) << security_origin << " ran insecure content from "
1035 << target_url.possibly_invalid_spec();
[email protected]221a5d92011-04-28 18:23:161036 UserMetrics::RecordAction(UserMetricsAction("SSL.RanInsecureContent"));
[email protected]ca406032011-07-19 21:53:051037 if (EndsWith(security_origin, kDotGoogleDotCom, false)) {
1038 UserMetrics::RecordAction(
1039 UserMetricsAction("SSL.RanInsecureContentGoogle"));
1040 }
[email protected]724159a2010-12-30 01:11:181041 controller_.ssl_manager()->DidRunInsecureContent(security_origin);
[email protected]f2b58c4d2011-06-01 23:29:411042 displayed_insecure_content_ = true;
[email protected]9678d8f2011-08-05 04:05:071043 SSLManager::NotifySSLInternalStateChanged(&controller());
[email protected]724159a2010-12-30 01:11:181044}
1045
1046void TabContents::OnDocumentLoadedInFrame(int64 frame_id) {
1047 controller_.DocumentLoadedInFrame();
[email protected]0d60f0192011-04-14 12:40:101048 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
1049 DocumentLoadedInFrame(frame_id));
[email protected]724159a2010-12-30 01:11:181050}
1051
1052void TabContents::OnDidFinishLoad(int64 frame_id) {
[email protected]0d60f0192011-04-14 12:40:101053 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
1054 DidFinishLoad(frame_id));
[email protected]724159a2010-12-30 01:11:181055}
1056
[email protected]c8f73ab2011-01-22 00:05:171057void TabContents::OnUpdateContentRestrictions(int restrictions) {
[email protected]c40d6232011-03-25 00:16:211058 SetContentRestrictions(restrictions);
[email protected]c8f73ab2011-01-22 00:05:171059}
1060
[email protected]216813952011-05-19 22:21:261061void TabContents::OnGoToEntryAtOffset(int offset) {
1062 if (!delegate_ || delegate_->OnGoToEntryOffset(offset)) {
1063 NavigationEntry* entry = controller_.GetEntryAtOffset(offset);
1064 if (!entry)
1065 return;
1066 // Note that we don't call NavigationController::GotToOffset() as we don't
1067 // want to create a pending navigation entry (it might end up lingering
1068 // http://crbug.com/51680).
[email protected]2905f742011-10-13 03:51:581069 entry->set_transition_type(
1070 content::PageTransitionFromInt(
1071 entry->transition_type() |
1072 content::PAGE_TRANSITION_FORWARD_BACK));
[email protected]216813952011-05-19 22:21:261073 NavigateToEntry(*entry, NavigationController::NO_RELOAD);
[email protected]a13cc362011-07-28 21:29:571074
1075 // If the entry is being restored and doesn't have a SiteInstance yet, fill
1076 // it in now that we know. This allows us to find the entry when it commits.
1077 if (!entry->site_instance() &&
1078 entry->restore_type() != NavigationEntry::RESTORE_NONE) {
[email protected]77362eb2011-08-01 17:18:381079 entry->set_site_instance(GetPendingSiteInstance());
[email protected]a13cc362011-07-28 21:29:571080 }
[email protected]216813952011-05-19 22:21:261081 }
1082}
1083
1084void TabContents::OnUpdateZoomLimits(int minimum_percent,
1085 int maximum_percent,
1086 bool remember) {
1087 minimum_zoom_percent_ = minimum_percent;
1088 maximum_zoom_percent_ = maximum_percent;
1089 temporary_zoom_settings_ = !remember;
1090}
1091
1092void TabContents::OnFocusedNodeChanged(bool is_editable_node) {
1093 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271094 content::NOTIFICATION_FOCUS_CHANGED_IN_PAGE,
[email protected]6c2381d2011-10-19 02:52:531095 content::Source<TabContents>(this),
1096 content::Details<const bool>(&is_editable_node));
[email protected]216813952011-05-19 22:21:261097}
1098
[email protected]3a29a6e2011-08-24 18:26:211099void TabContents::OnEnumerateDirectory(int request_id,
1100 const FilePath& path) {
1101 delegate()->EnumerateDirectory(this, request_id, path);
1102}
1103
[email protected]7d189022011-08-25 22:54:201104void TabContents::OnJSOutOfMemory() {
1105 delegate()->JSOutOfMemory(this);
1106}
1107
1108void TabContents::OnRegisterProtocolHandler(const std::string& protocol,
1109 const GURL& url,
1110 const string16& title) {
1111 delegate()->RegisterProtocolHandler(this, protocol, url, title);
1112}
1113
1114void TabContents::OnRegisterIntentHandler(const string16& action,
1115 const string16& type,
1116 const string16& href,
1117 const string16& title) {
1118 delegate()->RegisterIntentHandler(this, action, type, href, title);
1119}
1120
1121void TabContents::OnWebIntentDispatch(const IPC::Message& message,
[email protected]ce9751942011-09-21 01:57:241122 const webkit_glue::WebIntentData& intent,
[email protected]7d189022011-08-25 22:54:201123 int intent_id) {
[email protected]ce9751942011-09-21 01:57:241124 delegate()->WebIntentDispatch(this, message.routing_id(), intent, intent_id);
[email protected]7d189022011-08-25 22:54:201125}
1126
[email protected]b888919c2011-09-02 00:32:161127void TabContents::OnFindReply(int request_id,
1128 int number_of_matches,
1129 const gfx::Rect& selection_rect,
1130 int active_match_ordinal,
1131 bool final_update) {
1132 delegate()->FindReply(this, request_id, number_of_matches, selection_rect,
1133 active_match_ordinal, final_update);
[email protected]4e6239c2011-09-23 00:11:001134 // Send a notification to the renderer that we are ready to receive more
1135 // results from the scoping effort of the Find operation. The FindInPage
1136 // scoping is asynchronous and periodically sends results back up to the
1137 // browser using IPC. In an effort to not spam the browser we have the
1138 // browser send an ACK for each FindReply message and have the renderer
1139 // queue up the latest status message while waiting for this ACK.
1140 render_view_host()->Send(
1141 new ViewMsg_FindReplyACK(render_view_host()->routing_id()));
[email protected]b888919c2011-09-02 00:32:161142}
1143
[email protected]d952a052011-09-06 18:42:451144void TabContents::OnCrashedPlugin(const FilePath& plugin_path) {
1145 delegate()->CrashedPlugin(this, plugin_path);
1146}
1147
[email protected]7fc4bbb2011-09-08 21:23:101148void TabContents::OnAppCacheAccessed(const GURL& manifest_url,
1149 bool blocked_by_policy) {
1150 // Notify observers about navigation.
1151 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
1152 AppCacheAccessed(manifest_url, blocked_by_policy));
1153}
1154
[email protected]96d185d2009-04-24 03:28:541155// Notifies the RenderWidgetHost instance about the fact that the page is
1156// loading, or done loading and calls the base implementation.
1157void TabContents::SetIsLoading(bool is_loading,
1158 LoadNotificationDetails* details) {
1159 if (is_loading == is_loading_)
1160 return;
1161
1162 if (!is_loading) {
[email protected]9c235f042011-08-10 22:28:211163 load_state_ = net::LoadStateWithParam(net::LOAD_STATE_IDLE, string16());
[email protected]96d185d2009-04-24 03:28:541164 load_state_host_.clear();
[email protected]094e5b22009-09-25 04:23:561165 upload_size_ = 0;
1166 upload_position_ = 0;
[email protected]96d185d2009-04-24 03:28:541167 }
1168
1169 render_manager_.SetIsLoading(is_loading);
1170
1171 is_loading_ = is_loading;
1172 waiting_for_response_ = is_loading;
1173
[email protected]6ebdc9b2010-09-27 16:55:571174 if (delegate_)
1175 delegate_->LoadingStateChanged(this);
[email protected]329581b2009-04-28 06:52:351176 NotifyNavigationStateChanged(INVALIDATE_LOAD);
[email protected]96d185d2009-04-24 03:28:541177
[email protected]432115822011-07-10 15:52:271178 int type = is_loading ? content::NOTIFICATION_LOAD_START :
1179 content::NOTIFICATION_LOAD_STOP;
[email protected]6c2381d2011-10-19 02:52:531180 content::NotificationDetails det = NotificationService::NoDetails();
[email protected]96d185d2009-04-24 03:28:541181 if (details)
[email protected]6c2381d2011-10-19 02:52:531182 det = content::Details<LoadNotificationDetails>(details);
[email protected]96d185d2009-04-24 03:28:541183 NotificationService::current()->Notify(type,
[email protected]6c2381d2011-10-19 02:52:531184 content::Source<NavigationController>(&controller_),
[email protected]96d185d2009-04-24 03:28:541185 det);
1186}
1187
[email protected]d0980792011-02-13 19:41:401188WebUI* TabContents::GetWebUIForCurrentState() {
[email protected]7ade2732011-02-10 00:13:581189 // When there is a pending navigation entry, we want to use the pending WebUI
[email protected]96d185d2009-04-24 03:28:541190 // that goes along with it to control the basic flags. For example, we want to
1191 // show the pending URL in the URL bar, so we want the display_url flag to
1192 // be from the pending entry.
1193 //
1194 // The confusion comes because there are multiple possibilities for the
1195 // initial load in a tab as a side effect of the way the RenderViewHostManager
1196 // works.
1197 //
[email protected]e0112912011-02-02 22:54:351198 // - For the very first tab the load looks "normal". The new tab Web UI is
[email protected]96d185d2009-04-24 03:28:541199 // the pending one, and we want it to apply here.
1200 //
1201 // - For subsequent new tabs, they'll get a new SiteInstance which will then
1202 // get switched to the one previously associated with the new tab pages.
[email protected]7ade2732011-02-10 00:13:581203 // This switching will cause the manager to commit the RVH/WebUI. So we'll
[email protected]e0112912011-02-02 22:54:351204 // have a committed Web UI in this case.
[email protected]96d185d2009-04-24 03:28:541205 //
1206 // This condition handles all of these cases:
1207 //
1208 // - First load in first tab: no committed nav entry + pending nav entry +
1209 // pending dom ui:
[email protected]e0112912011-02-02 22:54:351210 // -> Use pending Web UI if any.
[email protected]96d185d2009-04-24 03:28:541211 //
1212 // - First load in second tab: no committed nav entry + pending nav entry +
[email protected]e0112912011-02-02 22:54:351213 // no pending Web UI:
1214 // -> Use the committed Web UI if any.
[email protected]96d185d2009-04-24 03:28:541215 //
1216 // - Second navigation in any tab: committed nav entry + pending nav entry:
[email protected]e0112912011-02-02 22:54:351217 // -> Use pending Web UI if any.
[email protected]96d185d2009-04-24 03:28:541218 //
1219 // - Normal state with no load: committed nav entry + no pending nav entry:
[email protected]e0112912011-02-02 22:54:351220 // -> Use committed Web UI.
[email protected]96d185d2009-04-24 03:28:541221 if (controller_.pending_entry() &&
1222 (controller_.GetLastCommittedEntry() ||
[email protected]7ade2732011-02-10 00:13:581223 render_manager_.pending_web_ui()))
1224 return render_manager_.pending_web_ui();
1225 return render_manager_.web_ui();
[email protected]96d185d2009-04-24 03:28:541226}
[email protected]420ae012009-04-24 05:16:321227
[email protected]1fd1a502011-03-30 16:55:561228WebUI::TypeID TabContents::GetWebUITypeForCurrentState() {
[email protected]3d7474ff2011-07-27 17:47:371229 return content::WebUIFactory::Get()->GetWebUIType(browser_context(),
1230 GetURL());
[email protected]1fd1a502011-03-30 16:55:561231}
1232
[email protected]420ae012009-04-24 05:16:321233void TabContents::DidNavigateMainFramePostCommit(
[email protected]8286f51a2011-05-31 17:39:131234 const content::LoadCommittedDetails& details,
[email protected]420ae012009-04-24 05:16:321235 const ViewHostMsg_FrameNavigate_Params& params) {
[email protected]1fd1a502011-03-30 16:55:561236 if (opener_web_ui_type_ != WebUI::kNoWebUI) {
[email protected]80a8fad2011-01-29 04:02:381237 // If this is a window.open navigation, use the same WebUI as the renderer
[email protected]c2e74fe82009-09-03 17:57:441238 // that opened the window, as long as both renderers have the same
1239 // privileges.
[email protected]1fd1a502011-03-30 16:55:561240 if (delegate_ && opener_web_ui_type_ == GetWebUITypeForCurrentState()) {
1241 WebUI* web_ui = content::GetContentClient()->browser()->
1242 GetWebUIFactory()->CreateWebUIForURL(this, GetURL());
[email protected]7ade2732011-02-10 00:13:581243 // web_ui might be NULL if the URL refers to a non-existent extension.
1244 if (web_ui) {
[email protected]d0980792011-02-13 19:41:401245 render_manager_.SetWebUIPostCommit(web_ui);
[email protected]7ade2732011-02-10 00:13:581246 web_ui->RenderViewCreated(render_view_host());
[email protected]c2e74fe82009-09-03 17:57:441247 }
1248 }
[email protected]1fd1a502011-03-30 16:55:561249 opener_web_ui_type_ = WebUI::kNoWebUI;
[email protected]c2e74fe82009-09-03 17:57:441250 }
1251
[email protected]4e697b042011-07-08 06:44:561252 if (details.is_navigation_to_different_page()) {
[email protected]420ae012009-04-24 05:16:321253 // Clear the status bubble. This is a workaround for a bug where WebKit
1254 // doesn't let us know that the cursor left an element during a
1255 // transition (this is also why the mouse cursor remains as a hand after
1256 // clicking on a link); see bugs 1184641 and 980803. We don't want to
1257 // clear the bubble when a user navigates to a named anchor in the same
1258 // page.
1259 UpdateTargetURL(details.entry->page_id(), GURL());
[email protected]420ae012009-04-24 05:16:321260 }
1261
[email protected]a6e82fc2010-02-24 22:28:141262 if (!details.is_in_page) {
[email protected]f17a0ee2010-05-17 17:38:471263 // Once the main frame is navigated, we're no longer considered to have
1264 // displayed insecure content.
1265 displayed_insecure_content_ = false;
[email protected]aece2c7f2009-08-27 20:43:171266 }
[email protected]ce5c4502009-05-06 16:46:111267
[email protected]3c9e1872010-11-18 16:17:491268 // Notify observers about navigation.
[email protected]585b30362011-01-28 02:30:171269 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
[email protected]3c9e1872010-11-18 16:17:491270 DidNavigateMainFramePostCommit(details, params));
[email protected]420ae012009-04-24 05:16:321271}
1272
1273void TabContents::DidNavigateAnyFramePostCommit(
1274 RenderViewHost* render_view_host,
[email protected]8286f51a2011-05-31 17:39:131275 const content::LoadCommittedDetails& details,
[email protected]420ae012009-04-24 05:16:321276 const ViewHostMsg_FrameNavigate_Params& params) {
[email protected]3ab9cb82011-06-03 18:02:071277 // If we navigate, reset JavaScript state. This does nothing to prevent
[email protected]420ae012009-04-24 05:16:321278 // a malicious script from spamming messages, since the script could just
1279 // reload the page to stop blocking.
[email protected]2e5b90c2011-08-16 21:11:551280 if (dialog_creator_) {
1281 dialog_creator_->ResetJavaScriptState(this);
1282 dialog_creator_ = NULL;
1283 }
[email protected]420ae012009-04-24 05:16:321284
[email protected]3c9e1872010-11-18 16:17:491285 // Notify observers about navigation.
[email protected]585b30362011-01-28 02:30:171286 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
[email protected]3c9e1872010-11-18 16:17:491287 DidNavigateAnyFramePostCommit(details, params));
[email protected]420ae012009-04-24 05:16:321288}
1289
[email protected]420ae012009-04-24 05:16:321290void TabContents::UpdateMaxPageIDIfNecessary(SiteInstance* site_instance,
1291 RenderViewHost* rvh) {
1292 // If we are creating a RVH for a restored controller, then we might
1293 // have more page IDs than the SiteInstance's current max page ID. We must
1294 // make sure that the max page ID is larger than any restored page ID.
1295 // Note that it is ok for conflicting page IDs to exist in another tab
1296 // (i.e., NavigationController), but if any page ID is larger than the max,
1297 // the back/forward list will get confused.
1298 int max_restored_page_id = controller_.max_restored_page_id();
1299 if (max_restored_page_id > 0) {
1300 int curr_max_page_id = site_instance->max_page_id();
1301 if (max_restored_page_id > curr_max_page_id) {
1302 // Need to update the site instance immediately.
1303 site_instance->UpdateMaxPageID(max_restored_page_id);
1304
1305 // Also tell the renderer to update its internal representation. We
1306 // need to reserve enough IDs to make all restored page IDs less than
1307 // the max.
1308 if (curr_max_page_id < 0)
1309 curr_max_page_id = 0;
[email protected]216813952011-05-19 22:21:261310 rvh->Send(new ViewMsg_ReservePageIDRange(
1311 rvh->routing_id(), max_restored_page_id - curr_max_page_id));
[email protected]420ae012009-04-24 05:16:321312 }
1313 }
1314}
1315
[email protected]6b2f7a82011-04-25 19:30:511316bool TabContents::UpdateTitleForEntry(NavigationEntry* entry,
[email protected]acafd272011-07-26 17:35:571317 const string16& title) {
[email protected]420ae012009-04-24 05:16:321318 // For file URLs without a title, use the pathname instead. In the case of a
1319 // synthesized title, we don't want the update to count toward the "one set
1320 // per page of the title to history."
[email protected]6b2f7a82011-04-25 19:30:511321 string16 final_title;
[email protected]420ae012009-04-24 05:16:321322 bool explicit_set;
[email protected]987fc3a2011-05-26 14:18:091323 if (entry && entry->url().SchemeIsFile() && title.empty()) {
[email protected]6b2f7a82011-04-25 19:30:511324 final_title = UTF8ToUTF16(entry->url().ExtractFileName());
[email protected]420ae012009-04-24 05:16:321325 explicit_set = false; // Don't count synthetic titles toward the set limit.
1326 } else {
[email protected]acafd272011-07-26 17:35:571327 TrimWhitespace(title, TRIM_ALL, &final_title);
[email protected]420ae012009-04-24 05:16:321328 explicit_set = true;
1329 }
1330
[email protected]987fc3a2011-05-26 14:18:091331 // If a page is created via window.open and never navigated,
1332 // there will be no navigation entry. In this situation,
1333 // |page_title_when_no_navigaiton_entry_| will be used for page title.
1334 if (entry) {
1335 if (final_title == entry->title())
1336 return false; // Nothing changed, don't bother.
[email protected]420ae012009-04-24 05:16:321337
[email protected]987fc3a2011-05-26 14:18:091338 entry->set_title(final_title);
1339 } else {
1340 if (page_title_when_no_navigation_entry_ == final_title)
1341 return false; // Nothing changed, don't bother.
1342
1343 page_title_when_no_navigation_entry_ = final_title;
1344 }
[email protected]420ae012009-04-24 05:16:321345
[email protected]420ae012009-04-24 05:16:321346 // Lastly, set the title for the view.
[email protected]790e2fd2011-09-21 20:28:231347 view_->SetPageTitle(final_title);
[email protected]420ae012009-04-24 05:16:321348
[email protected]105bb0f2011-05-24 17:12:141349 TitleUpdatedDetails details(entry, explicit_set);
1350
[email protected]cbc0e1b2010-04-12 18:33:041351 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271352 content::NOTIFICATION_TAB_CONTENTS_TITLE_UPDATED,
[email protected]6c2381d2011-10-19 02:52:531353 content::Source<TabContents>(this),
1354 content::Details<TitleUpdatedDetails>(&details));
[email protected]cbc0e1b2010-04-12 18:33:041355
[email protected]420ae012009-04-24 05:16:321356 return true;
1357}
1358
1359void TabContents::NotifySwapped() {
1360 // After sending out a swap notification, we need to send a disconnect
1361 // notification so that clients that pick up a pointer to |this| can NULL the
1362 // pointer. See Bug 1230284.
1363 notify_disconnection_ = true;
1364 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271365 content::NOTIFICATION_TAB_CONTENTS_SWAPPED,
[email protected]6c2381d2011-10-19 02:52:531366 content::Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:321367 NotificationService::NoDetails());
1368}
1369
1370void TabContents::NotifyConnected() {
1371 notify_disconnection_ = true;
1372 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271373 content::NOTIFICATION_TAB_CONTENTS_CONNECTED,
[email protected]6c2381d2011-10-19 02:52:531374 content::Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:321375 NotificationService::NoDetails());
1376}
1377
1378void TabContents::NotifyDisconnected() {
1379 if (!notify_disconnection_)
1380 return;
1381
1382 notify_disconnection_ = false;
1383 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271384 content::NOTIFICATION_TAB_CONTENTS_DISCONNECTED,
[email protected]6c2381d2011-10-19 02:52:531385 content::Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:321386 NotificationService::NoDetails());
1387}
1388
[email protected]8d3347f2009-07-09 22:00:211389RenderViewHostDelegate::View* TabContents::GetViewDelegate() {
[email protected]420ae012009-04-24 05:16:321390 return view_.get();
1391}
1392
[email protected]8d3347f2009-07-09 22:00:211393RenderViewHostDelegate::RendererManagement*
1394TabContents::GetRendererManagementDelegate() {
1395 return &render_manager_;
1396}
1397
[email protected]3d7474ff2011-07-27 17:47:371398RendererPreferences TabContents::GetRendererPrefs(
1399 content::BrowserContext* browser_context) const {
[email protected]840b1512009-07-21 16:53:461400 return renderer_preferences_;
[email protected]80d96fa2009-06-10 22:34:511401}
1402
[email protected]57c6a652009-05-04 07:58:341403TabContents* TabContents::GetAsTabContents() {
1404 return this;
1405}
1406
[email protected]da4dfc42011-10-12 15:53:561407content::ViewType TabContents::GetRenderViewType() const {
1408 return content::VIEW_TYPE_TAB_CONTENTS;
[email protected]7b291f92009-08-14 05:43:531409}
1410
[email protected]420ae012009-04-24 05:16:321411void TabContents::RenderViewCreated(RenderViewHost* render_view_host) {
[email protected]2e4633c2009-07-09 16:58:061412 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271413 content::NOTIFICATION_RENDER_VIEW_HOST_CREATED_FOR_TAB,
[email protected]6c2381d2011-10-19 02:52:531414 content::Source<TabContents>(this),
1415 content::Details<RenderViewHost>(render_view_host));
[email protected]420ae012009-04-24 05:16:321416 NavigationEntry* entry = controller_.GetActiveEntry();
1417 if (!entry)
1418 return;
1419
1420 // When we're creating views, we're still doing initial setup, so we always
[email protected]e0112912011-02-02 22:54:351421 // use the pending Web UI rather than any possibly existing committed one.
[email protected]1fd1a502011-03-30 16:55:561422 if (render_manager_.pending_web_ui())
[email protected]7ade2732011-02-10 00:13:581423 render_manager_.pending_web_ui()->RenderViewCreated(render_view_host);
[email protected]420ae012009-04-24 05:16:321424
1425 if (entry->IsViewSourceMode()) {
1426 // Put the renderer in view source mode.
1427 render_view_host->Send(
1428 new ViewMsg_EnableViewSourceMode(render_view_host->routing_id()));
1429 }
[email protected]0666aef2009-05-13 19:48:081430
1431 view()->RenderViewCreated(render_view_host);
[email protected]11f764bb2011-05-25 19:45:261432
1433 FOR_EACH_OBSERVER(
1434 TabContentsObserver, observers_, RenderViewCreated(render_view_host));
[email protected]420ae012009-04-24 05:16:321435}
1436
1437void TabContents::RenderViewReady(RenderViewHost* rvh) {
1438 if (rvh != render_view_host()) {
1439 // Don't notify the world, since this came from a renderer in the
1440 // background.
1441 return;
1442 }
1443
1444 NotifyConnected();
[email protected]9d8a4642009-07-29 17:25:301445 bool was_crashed = is_crashed();
[email protected]443b80e2010-12-14 00:42:231446 SetIsCrashed(base::TERMINATION_STATUS_STILL_RUNNING, 0);
[email protected]9d8a4642009-07-29 17:25:301447
1448 // Restore the focus to the tab (otherwise the focus will be on the top
1449 // window).
[email protected]484ae5912010-09-29 19:16:141450 if (was_crashed && !FocusLocationBarByDefault() &&
1451 (!delegate_ || delegate_->ShouldFocusPageAfterCrash())) {
[email protected]9d8a4642009-07-29 17:25:301452 Focus();
[email protected]484ae5912010-09-29 19:16:141453 }
[email protected]420ae012009-04-24 05:16:321454}
1455
[email protected]443b80e2010-12-14 00:42:231456void TabContents::RenderViewGone(RenderViewHost* rvh,
1457 base::TerminationStatus status,
1458 int error_code) {
[email protected]420ae012009-04-24 05:16:321459 if (rvh != render_view_host()) {
1460 // The pending page's RenderViewHost is gone.
1461 return;
1462 }
1463
1464 SetIsLoading(false, NULL);
1465 NotifyDisconnected();
[email protected]443b80e2010-12-14 00:42:231466 SetIsCrashed(status, error_code);
[email protected]b3e3f87a2011-10-01 23:37:541467 view()->OnTabCrashed(crashed_status(), crashed_error_code());
[email protected]420ae012009-04-24 05:16:321468
[email protected]55452902011-06-01 21:57:471469 FOR_EACH_OBSERVER(TabContentsObserver, observers_, RenderViewGone());
[email protected]420ae012009-04-24 05:16:321470}
1471
[email protected]2e4633c2009-07-09 16:58:061472void TabContents::RenderViewDeleted(RenderViewHost* rvh) {
[email protected]fb7b79f72009-11-06 18:00:481473 render_manager_.RenderViewDeleted(rvh);
[email protected]2e4633c2009-07-09 16:58:061474}
1475
[email protected]420ae012009-04-24 05:16:321476void TabContents::DidNavigate(RenderViewHost* rvh,
1477 const ViewHostMsg_FrameNavigate_Params& params) {
[email protected]2905f742011-10-13 03:51:581478 if (content::PageTransitionIsMainFrame(params.transition))
[email protected]420ae012009-04-24 05:16:321479 render_manager_.DidNavigateMainFrame(rvh);
[email protected]8030f012009-09-25 18:09:371480
[email protected]420ae012009-04-24 05:16:321481 // Update the site of the SiteInstance if it doesn't have one yet.
1482 if (!GetSiteInstance()->has_site())
1483 GetSiteInstance()->SetSite(params.url);
1484
1485 // Need to update MIME type here because it's referred to in
1486 // UpdateNavigationCommands() called by RendererDidNavigate() to
1487 // determine whether or not to enable the encoding menu.
1488 // It's updated only for the main frame. For a subframe,
1489 // RenderView::UpdateURL does not set params.contents_mime_type.
1490 // (see http://code.google.com/p/chromium/issues/detail?id=2929 )
1491 // TODO(jungshik): Add a test for the encoding menu to avoid
1492 // regressing it again.
[email protected]2905f742011-10-13 03:51:581493 if (content::PageTransitionIsMainFrame(params.transition))
[email protected]420ae012009-04-24 05:16:321494 contents_mime_type_ = params.contents_mime_type;
1495
[email protected]8286f51a2011-05-31 17:39:131496 content::LoadCommittedDetails details;
[email protected]93f230e02011-06-01 14:40:001497 bool did_navigate = controller_.RendererDidNavigate(params, &details);
[email protected]3e90d4a2009-07-03 17:38:391498
[email protected]a9c0bfe2010-09-17 08:35:221499 // Send notification about committed provisional loads. This notification is
1500 // different from the NAV_ENTRY_COMMITTED notification which doesn't include
1501 // the actual URL navigated to and isn't sent for AUTO_SUBFRAME navigations.
[email protected]610cbb82011-10-18 16:35:081502 if (details.type != content::NAVIGATION_TYPE_NAV_IGNORE) {
[email protected]dabb0d12010-10-05 12:50:071503 // For AUTO_SUBFRAME navigations, an event for the main frame is generated
1504 // that is not recorded in the navigation history. For the purpose of
1505 // tracking navigation events, we treat this event as a sub frame navigation
1506 // event.
1507 bool is_main_frame = did_navigate ? details.is_main_frame : false;
[email protected]2905f742011-10-13 03:51:581508 content::PageTransition transition_type = params.transition;
[email protected]a9c0bfe2010-09-17 08:35:221509 // Whether or not a page transition was triggered by going backward or
1510 // forward in the history is only stored in the navigation controller's
1511 // entry list.
1512 if (did_navigate &&
1513 (controller_.GetActiveEntry()->transition_type() &
[email protected]2905f742011-10-13 03:51:581514 content::PAGE_TRANSITION_FORWARD_BACK)) {
1515 transition_type = content::PageTransitionFromInt(
1516 params.transition | content::PAGE_TRANSITION_FORWARD_BACK);
[email protected]a9c0bfe2010-09-17 08:35:221517 }
[email protected]0d60f0192011-04-14 12:40:101518 // Notify observers about the commit of the provisional load.
1519 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
1520 DidCommitProvisionalLoadForFrame(params.frame_id,
1521 is_main_frame, params.url, transition_type));
[email protected]a9c0bfe2010-09-17 08:35:221522 }
1523
[email protected]76543b92009-08-31 17:27:451524 if (!did_navigate)
1525 return; // No navigation happened.
[email protected]420ae012009-04-24 05:16:321526
1527 // DO NOT ADD MORE STUFF TO THIS FUNCTION! Your component should either listen
1528 // for the appropriate notification (best) or you can add it to
1529 // DidNavigateMainFramePostCommit / DidNavigateAnyFramePostCommit (only if
1530 // necessary, please).
1531
1532 // Run post-commit tasks.
[email protected]93f230e02011-06-01 14:40:001533 if (details.is_main_frame) {
[email protected]420ae012009-04-24 05:16:321534 DidNavigateMainFramePostCommit(details, params);
[email protected]09b29342011-06-24 19:18:481535 if (delegate())
1536 delegate()->DidNavigateMainFramePostCommit(this);
[email protected]93f230e02011-06-01 14:40:001537 }
[email protected]420ae012009-04-24 05:16:321538 DidNavigateAnyFramePostCommit(rvh, details, params);
1539}
1540
1541void TabContents::UpdateState(RenderViewHost* rvh,
1542 int32 page_id,
1543 const std::string& state) {
[email protected]992db4c2011-05-12 15:37:151544 // Ensure that this state update comes from either the active RVH or one of
1545 // the swapped out RVHs. We don't expect to hear from any other RVHs.
1546 DCHECK(rvh == render_view_host() || render_manager_.IsSwappedOut(rvh));
[email protected]420ae012009-04-24 05:16:321547
1548 // We must be prepared to handle state updates for any page, these occur
1549 // when the user is scrolling and entering form data, as well as when we're
1550 // leaving a page, in which case our state may have already been moved to
1551 // the next page. The navigation controller will look up the appropriate
1552 // NavigationEntry and update it when it is notified via the delegate.
1553
1554 int entry_index = controller_.GetEntryIndexWithPageID(
[email protected]992db4c2011-05-12 15:37:151555 rvh->site_instance(), page_id);
[email protected]420ae012009-04-24 05:16:321556 if (entry_index < 0)
1557 return;
1558 NavigationEntry* entry = controller_.GetEntryAtIndex(entry_index);
1559
1560 if (state == entry->content_state())
1561 return; // Nothing to update.
1562 entry->set_content_state(state);
1563 controller_.NotifyEntryChanged(entry, entry_index);
1564}
1565
1566void TabContents::UpdateTitle(RenderViewHost* rvh,
[email protected]acafd272011-07-26 17:35:571567 int32 page_id,
[email protected]a49e10b2011-08-01 23:57:461568 const string16& title,
1569 base::i18n::TextDirection title_direction) {
[email protected]420ae012009-04-24 05:16:321570 // If we have a title, that's a pretty good indication that we've started
1571 // getting useful data.
1572 SetNotWaitingForResponse();
1573
1574 DCHECK(rvh == render_view_host());
[email protected]371cc8b2010-09-01 16:03:481575 NavigationEntry* entry = controller_.GetEntryWithPageID(rvh->site_instance(),
[email protected]ec0b6c42010-08-26 03:16:581576 page_id);
[email protected]987fc3a2011-05-26 14:18:091577
[email protected]a49e10b2011-08-01 23:57:461578 // TODO(evan): make use of title_direction.
1579 // http://code.google.com/p/chromium/issues/detail?id=27094
[email protected]987fc3a2011-05-26 14:18:091580 if (!UpdateTitleForEntry(entry, title))
[email protected]420ae012009-04-24 05:16:321581 return;
1582
1583 // Broadcast notifications when the UI should be updated.
1584 if (entry == controller_.GetEntryAtOffset(0))
[email protected]f1cd5e82009-10-23 17:19:031585 NotifyNavigationStateChanged(INVALIDATE_TITLE);
[email protected]420ae012009-04-24 05:16:321586}
1587
[email protected]420ae012009-04-24 05:16:321588void TabContents::UpdateEncoding(RenderViewHost* render_view_host,
[email protected]41fc0322009-09-04 22:23:401589 const std::string& encoding) {
[email protected]420ae012009-04-24 05:16:321590 set_encoding(encoding);
1591}
1592
1593void TabContents::UpdateTargetURL(int32 page_id, const GURL& url) {
1594 if (delegate())
[email protected]d952a052011-09-06 18:42:451595 delegate()->UpdateTargetURL(this, page_id, url);
[email protected]420ae012009-04-24 05:16:321596}
1597
[email protected]420ae012009-04-24 05:16:321598void TabContents::Close(RenderViewHost* rvh) {
[email protected]07707302009-11-06 00:50:291599 // The UI may be in an event-tracking loop, such as between the
1600 // mouse-down and mouse-up in text selection or a button click.
1601 // Defer the close until after tracking is complete, so that we
1602 // don't free objects out from under the UI.
1603 // TODO(shess): This could probably be integrated with the
1604 // IsDoingDrag() test below. Punting for now because I need more
1605 // research to understand how this impacts platforms other than Mac.
1606 // TODO(shess): This could get more fine-grained. For instance,
1607 // closing a tab in another window while selecting text in the
1608 // current window's Omnibox should be just fine.
1609 if (view()->IsEventTracking()) {
1610 view()->CloseTabAfterEventTracking();
1611 return;
1612 }
1613
[email protected]24a4d1062009-07-10 23:10:421614 // If we close the tab while we're in the middle of a drag, we'll crash.
1615 // Instead, cancel the drag and close it as soon as the drag ends.
1616 if (view()->IsDoingDrag()) {
1617 view()->CancelDragAndCloseTab();
1618 return;
1619 }
1620
[email protected]420ae012009-04-24 05:16:321621 // Ignore this if it comes from a RenderViewHost that we aren't showing.
1622 if (delegate() && rvh == render_view_host())
1623 delegate()->CloseContents(this);
1624}
1625
1626void TabContents::RequestMove(const gfx::Rect& new_bounds) {
[email protected]b35b26b32011-05-05 20:35:141627 if (delegate() && delegate()->IsPopupOrPanel(this))
[email protected]420ae012009-04-24 05:16:321628 delegate()->MoveContents(this, new_bounds);
1629}
1630
[email protected]7ab1e7d62009-10-14 23:32:011631void TabContents::DidStartLoading() {
[email protected]420ae012009-04-24 05:16:321632 SetIsLoading(true, NULL);
[email protected]3c9e1872010-11-18 16:17:491633
[email protected]c40d6232011-03-25 00:16:211634 if (delegate() && content_restrictions_) {
1635 content_restrictions_ = 0;
[email protected]de4761b2010-12-07 21:05:211636 delegate()->ContentRestrictionsChanged(this);
[email protected]9e823662010-10-13 23:36:001637 }
[email protected]3c9e1872010-11-18 16:17:491638
1639 // Notify observers about navigation.
[email protected]585b30362011-01-28 02:30:171640 FOR_EACH_OBSERVER(TabContentsObserver, observers_, DidStartLoading());
[email protected]420ae012009-04-24 05:16:321641}
1642
[email protected]7ab1e7d62009-10-14 23:32:011643void TabContents::DidStopLoading() {
[email protected]420ae012009-04-24 05:16:321644 scoped_ptr<LoadNotificationDetails> details;
1645
1646 NavigationEntry* entry = controller_.GetActiveEntry();
1647 // An entry may not exist for a stop when loading an initial blank page or
1648 // if an iframe injected by script into a blank page finishes loading.
1649 if (entry) {
[email protected]420ae012009-04-24 05:16:321650 base::TimeDelta elapsed = base::TimeTicks::Now() - current_load_start_;
1651
1652 details.reset(new LoadNotificationDetails(
[email protected]ebe89e062009-08-13 23:16:541653 entry->virtual_url(),
[email protected]420ae012009-04-24 05:16:321654 entry->transition_type(),
1655 elapsed,
1656 &controller_,
1657 controller_.GetCurrentEntryIndex()));
1658 }
1659
[email protected]420ae012009-04-24 05:16:321660 SetIsLoading(false, details.get());
[email protected]3c9e1872010-11-18 16:17:491661
1662 // Notify observers about navigation.
[email protected]585b30362011-01-28 02:30:171663 FOR_EACH_OBSERVER(TabContentsObserver, observers_, DidStopLoading());
[email protected]420ae012009-04-24 05:16:321664}
1665
[email protected]c95fa8b2011-04-28 20:26:161666void TabContents::DidCancelLoading() {
1667 controller_.DiscardNonCommittedEntries();
1668
1669 // Update the URL display.
1670 NotifyNavigationStateChanged(TabContents::INVALIDATE_URL);
1671}
1672
[email protected]1a3c3cb2010-12-16 21:03:401673void TabContents::DidChangeLoadProgress(double progress) {
1674 if (delegate())
1675 delegate()->LoadProgressChanged(progress);
1676}
1677
[email protected]25497492010-09-11 15:15:081678void TabContents::DocumentOnLoadCompletedInMainFrame(
1679 RenderViewHost* render_view_host,
1680 int32 page_id) {
1681 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271682 content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME,
[email protected]6c2381d2011-10-19 02:52:531683 content::Source<TabContents>(this),
1684 content::Details<int>(&page_id));
[email protected]25497492010-09-11 15:15:081685}
1686
[email protected]ae5184d62011-10-06 19:25:581687void TabContents::RequestOpenURL(const GURL& url,
1688 const GURL& referrer,
1689 WindowOpenDisposition disposition,
1690 int64 source_frame_id) {
1691 TabContents* new_contents = NULL;
[email protected]2905f742011-10-13 03:51:581692 content::PageTransition transition_type = content::PAGE_TRANSITION_LINK;
[email protected]7ade2732011-02-10 00:13:581693 if (render_manager_.web_ui()) {
[email protected]e0112912011-02-02 22:54:351694 // When we're a Web UI, it will provide a page transition type for us (this
[email protected]420ae012009-04-24 05:16:321695 // is so the new tab page can specify AUTO_BOOKMARK for automatically
1696 // generated suggestions).
1697 //
[email protected]e0112912011-02-02 22:54:351698 // Note also that we hide the referrer for Web UI pages. We don't really
[email protected]60e448982009-05-06 04:21:161699 // want web sites to see a referrer of "chrome://blah" (and some
1700 // chrome: URLs might have search terms or other stuff we don't want to
[email protected]420ae012009-04-24 05:16:321701 // send to the site), so we send no referrer.
[email protected]ae5184d62011-10-06 19:25:581702 new_contents = OpenURL(url, GURL(), disposition,
[email protected]7ade2732011-02-10 00:13:581703 render_manager_.web_ui()->link_transition_type());
[email protected]ae5184d62011-10-06 19:25:581704 transition_type = render_manager_.web_ui()->link_transition_type();
[email protected]420ae012009-04-24 05:16:321705 } else {
[email protected]e47ae9472011-10-13 19:48:341706 new_contents = OpenURL(OpenURLParams(
1707 url, referrer, disposition, content::PAGE_TRANSITION_LINK,
1708 true /* is_renderer_initiated */));
[email protected]ae5184d62011-10-06 19:25:581709 }
1710 if (new_contents) {
1711 // Notify observers.
1712 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
1713 DidOpenRequestedURL(new_contents,
1714 url,
1715 referrer,
1716 disposition,
1717 transition_type,
1718 source_frame_id));
[email protected]420ae012009-04-24 05:16:321719 }
1720}
1721
[email protected]420ae012009-04-24 05:16:321722void TabContents::RunJavaScriptMessage(
[email protected]992db4c2011-05-12 15:37:151723 const RenderViewHost* rvh,
[email protected]4f5ce842011-05-27 19:34:411724 const string16& message,
1725 const string16& default_prompt,
[email protected]420ae012009-04-24 05:16:321726 const GURL& frame_url,
1727 const int flags,
1728 IPC::Message* reply_msg,
1729 bool* did_suppress_message) {
[email protected]3ab9cb82011-06-03 18:02:071730 // Suppress JavaScript dialogs when requested. Also suppress messages when
1731 // showing an interstitial as it's shown over the previous page and we don't
1732 // want the hidden page's dialogs to interfere with the interstitial.
[email protected]8f55e802010-12-06 18:11:501733 bool suppress_this_message =
[email protected]992db4c2011-05-12 15:37:151734 rvh->is_swapped_out() ||
[email protected]8f55e802010-12-06 18:11:501735 showing_interstitial_page() ||
[email protected]3ab9cb82011-06-03 18:02:071736 !delegate_ ||
1737 delegate_->ShouldSuppressDialogs();
[email protected]420ae012009-04-24 05:16:321738
1739 if (!suppress_this_message) {
[email protected]b627d9a2011-06-28 14:06:341740 content::JavaScriptDialogCreator::TitleType title_type;
1741 string16 title;
1742
1743 if (!frame_url.has_host()) {
1744 title_type = content::JavaScriptDialogCreator::DIALOG_TITLE_NONE;
1745 } else {
1746 title_type = content::JavaScriptDialogCreator::DIALOG_TITLE_FORMATTED_URL;
1747 title = net::FormatUrl(
1748 frame_url.GetOrigin(),
1749 content::GetContentClient()->browser()->GetAcceptLangs(this));
1750 }
1751
[email protected]2e5b90c2011-08-16 21:11:551752 dialog_creator_ = delegate_->GetJavaScriptDialogCreator();
1753 dialog_creator_->RunJavaScriptDialog(this,
1754 title_type,
1755 title,
1756 flags,
1757 message,
1758 default_prompt,
1759 reply_msg,
1760 &suppress_this_message);
[email protected]420ae012009-04-24 05:16:321761 }
[email protected]3ab9cb82011-06-03 18:02:071762
1763 if (suppress_this_message) {
1764 // If we are suppressing messages, just reply as if the user immediately
1765 // pressed "Cancel".
1766 OnDialogClosed(reply_msg, false, string16());
1767 }
1768
1769 *did_suppress_message = suppress_this_message;
[email protected]420ae012009-04-24 05:16:321770}
1771
[email protected]992db4c2011-05-12 15:37:151772void TabContents::RunBeforeUnloadConfirm(const RenderViewHost* rvh,
[email protected]4f5ce842011-05-27 19:34:411773 const string16& message,
[email protected]420ae012009-04-24 05:16:321774 IPC::Message* reply_msg) {
[email protected]5aab5e22010-12-08 22:13:291775 if (delegate())
1776 delegate()->WillRunBeforeUnloadConfirm();
[email protected]3ab9cb82011-06-03 18:02:071777
1778 bool suppress_this_message =
1779 rvh->is_swapped_out() ||
1780 !delegate_ ||
1781 delegate_->ShouldSuppressDialogs();
[email protected]992db4c2011-05-12 15:37:151782 if (suppress_this_message) {
[email protected]4f5ce842011-05-27 19:34:411783 render_view_host()->JavaScriptDialogClosed(reply_msg, true, string16());
[email protected]8f55e802010-12-06 18:11:501784 return;
1785 }
[email protected]3ab9cb82011-06-03 18:02:071786
[email protected]7ab1e7d62009-10-14 23:32:011787 is_showing_before_unload_dialog_ = true;
[email protected]2e5b90c2011-08-16 21:11:551788 dialog_creator_ = delegate_->GetJavaScriptDialogCreator();
1789 dialog_creator_->RunBeforeUnloadDialog(this,
1790 message,
1791 reply_msg);
[email protected]420ae012009-04-24 05:16:321792}
1793
[email protected]420ae012009-04-24 05:16:321794WebPreferences TabContents::GetWebkitPrefs() {
[email protected]447021c2010-09-08 21:29:081795 WebPreferences web_prefs =
[email protected]181a95ee2011-07-12 19:26:361796 content::GetContentClient()->browser()->GetWebkitPrefs(
[email protected]3d7474ff2011-07-27 17:47:371797 render_view_host()->process()->browser_context(), false);
[email protected]447021c2010-09-08 21:29:081798
[email protected]b8299c12011-06-03 19:52:281799 // Force accelerated compositing and 2d canvas off for chrome:, about: and
[email protected]2ef216b2011-08-01 19:25:211800 // chrome-devtools: pages (unless it's specifically allowed).
1801 if ((GetURL().SchemeIs(chrome::kChromeDevToolsScheme) ||
[email protected]30b8e0ee2011-09-12 14:45:571802#if !defined(TOUCH_UI)
1803 // Allow accelerated compositing for keyboard and log in screen.
[email protected]b8299c12011-06-03 19:52:281804 GetURL().SchemeIs(chrome::kChromeUIScheme) ||
[email protected]30b8e0ee2011-09-12 14:45:571805#endif
[email protected]cd4a5bd22011-09-13 06:46:301806 (GetURL().SchemeIs(chrome::kAboutScheme) &&
1807 GetURL().spec() != chrome::kAboutBlankURL)) &&
[email protected]2ef216b2011-08-01 19:25:211808 !web_prefs.allow_webui_compositing) {
[email protected]447021c2010-09-08 21:29:081809 web_prefs.accelerated_compositing_enabled = false;
[email protected]9beff752010-09-22 19:35:431810 web_prefs.accelerated_2d_canvas_enabled = false;
[email protected]447021c2010-09-08 21:29:081811 }
1812
[email protected]2f7830b32011-08-26 18:22:251813#if defined(TOUCH_UI)
1814 web_prefs.force_compositing_mode = true;
1815#endif
1816
[email protected]447021c2010-09-08 21:29:081817 return web_prefs;
[email protected]420ae012009-04-24 05:16:321818}
1819
[email protected]7d472472011-01-22 01:30:251820void TabContents::OnUserGesture() {
[email protected]ddb85052011-05-18 14:40:271821 // Notify observers.
1822 FOR_EACH_OBSERVER(TabContentsObserver, observers_, DidGetUserGesture());
[email protected]686493142011-07-15 21:47:221823
1824 ResourceDispatcherHost* rdh =
1825 content::GetContentClient()->browser()->GetResourceDispatcherHost();
1826 if (rdh) // NULL in unittests.
[email protected]8ec98cd22011-08-09 22:31:301827 rdh->OnUserGesture(this);
[email protected]7d472472011-01-22 01:30:251828}
1829
[email protected]fa1cf0b82010-01-15 21:49:441830void TabContents::OnIgnoredUIEvent() {
[email protected]31145092011-09-30 01:49:441831 // Notify observers.
1832 FOR_EACH_OBSERVER(TabContentsObserver, observers_, DidGetIgnoredUIEvent());
[email protected]fa1cf0b82010-01-15 21:49:441833}
1834
[email protected]420ae012009-04-24 05:16:321835void TabContents::RendererUnresponsive(RenderViewHost* rvh,
1836 bool is_during_unload) {
[email protected]992db4c2011-05-12 15:37:151837 // Don't show hung renderer dialog for a swapped out RVH.
1838 if (rvh != render_view_host())
1839 return;
1840
[email protected]e5fc1632011-08-08 07:51:531841 // Ignore renderer unresponsive event if debugger is attached to the tab
1842 // since the event may be a result of the renderer sitting on a breakpoint.
1843 // See http://crbug.com/65458
1844 DevToolsManager* devtools_manager = DevToolsManager::GetInstance();
1845 if (devtools_manager &&
1846 devtools_manager->GetDevToolsClientHostFor(rvh) != NULL)
1847 return;
1848
[email protected]420ae012009-04-24 05:16:321849 if (is_during_unload) {
1850 // Hang occurred while firing the beforeunload/unload handler.
1851 // Pretend the handler fired so tab closing continues as if it had.
[email protected]829e7612009-04-25 01:15:111852 rvh->set_sudden_termination_allowed(true);
[email protected]420ae012009-04-24 05:16:321853
1854 if (!render_manager_.ShouldCloseTabOnUnresponsiveRenderer())
1855 return;
1856
1857 // If the tab hangs in the beforeunload/unload handler there's really
1858 // nothing we can do to recover. Pretend the unload listeners have
1859 // all fired and close the tab. If the hang is in the beforeunload handler
1860 // then the user will not have the option of cancelling the close.
1861 Close(rvh);
1862 return;
1863 }
1864
[email protected]55452902011-06-01 21:57:471865 if (!render_view_host() || !render_view_host()->IsRenderViewLive())
1866 return;
1867
1868 if (delegate())
1869 delegate()->RendererUnresponsive(this);
[email protected]420ae012009-04-24 05:16:321870}
1871
1872void TabContents::RendererResponsive(RenderViewHost* render_view_host) {
[email protected]55452902011-06-01 21:57:471873 if (delegate())
1874 delegate()->RendererResponsive(this);
[email protected]420ae012009-04-24 05:16:321875}
1876
1877void TabContents::LoadStateChanged(const GURL& url,
[email protected]9c235f042011-08-10 22:28:211878 const net::LoadStateWithParam& load_state,
[email protected]094e5b22009-09-25 04:23:561879 uint64 upload_position,
1880 uint64 upload_size) {
[email protected]420ae012009-04-24 05:16:321881 load_state_ = load_state;
[email protected]094e5b22009-09-25 04:23:561882 upload_position_ = upload_position;
1883 upload_size_ = upload_size;
[email protected]04866c42011-05-03 20:03:501884 load_state_host_ = net::IDNToUnicode(url.host(),
[email protected]b5cca982011-05-26 04:42:081885 content::GetContentClient()->browser()->GetAcceptLangs(this));
[email protected]9c235f042011-08-10 22:28:211886 if (load_state_.state == net::LOAD_STATE_READING_RESPONSE)
[email protected]420ae012009-04-24 05:16:321887 SetNotWaitingForResponse();
[email protected]f5d978c2011-07-21 14:43:511888 if (IsLoading())
[email protected]c9cd2222009-05-06 05:16:501889 NotifyNavigationStateChanged(INVALIDATE_LOAD | INVALIDATE_TAB);
[email protected]420ae012009-04-24 05:16:321890}
1891
[email protected]7d472472011-01-22 01:30:251892void TabContents::WorkerCrashed() {
[email protected]0dd3a0ab2011-02-18 08:17:441893 if (delegate())
[email protected]55452902011-06-01 21:57:471894 delegate()->WorkerCrashed(this);
[email protected]7d472472011-01-22 01:30:251895}
1896
[email protected]420ae012009-04-24 05:16:321897void TabContents::BeforeUnloadFiredFromRenderManager(
1898 bool proceed,
1899 bool* proceed_to_fire_unload) {
1900 if (delegate())
1901 delegate()->BeforeUnloadFired(this, proceed, proceed_to_fire_unload);
1902}
1903
[email protected]3a3d47472010-07-15 21:03:541904void TabContents::DidStartLoadingFromRenderManager(
1905 RenderViewHost* render_view_host) {
1906 DidStartLoading();
1907}
1908
1909void TabContents::RenderViewGoneFromRenderManager(
1910 RenderViewHost* render_view_host) {
[email protected]443b80e2010-12-14 00:42:231911 DCHECK(crashed_status_ != base::TERMINATION_STATUS_STILL_RUNNING);
1912 RenderViewGone(render_view_host, crashed_status_, crashed_error_code_);
[email protected]3a3d47472010-07-15 21:03:541913}
1914
[email protected]420ae012009-04-24 05:16:321915void TabContents::UpdateRenderViewSizeForRenderManager() {
1916 // TODO(brettw) this is a hack. See TabContentsView::SizeContents.
[email protected]0323ee42010-02-17 22:03:261917 gfx::Size size = view_->GetContainerSize();
1918 // 0x0 isn't a valid window size (minimal window size is 1x1) but it may be
1919 // here during container initialization and normal window size will be set
1920 // later. In case of tab duplication this resizing to 0x0 prevents setting
1921 // normal size later so just ignore it.
1922 if (!size.IsEmpty())
1923 view_->SizeContents(size);
[email protected]420ae012009-04-24 05:16:321924}
1925
[email protected]3a3d47472010-07-15 21:03:541926void TabContents::NotifySwappedFromRenderManager() {
1927 NotifySwapped();
1928}
1929
1930NavigationController& TabContents::GetControllerForRenderManager() {
1931 return controller();
1932}
1933
[email protected]d0980792011-02-13 19:41:401934WebUI* TabContents::CreateWebUIForRenderManager(const GURL& url) {
[email protected]1fd1a502011-03-30 16:55:561935 return content::WebUIFactory::Get()->CreateWebUIForURL(this, url);
[email protected]420ae012009-04-24 05:16:321936}
1937
1938NavigationEntry*
1939TabContents::GetLastCommittedNavigationEntryForRenderManager() {
1940 return controller_.GetLastCommittedEntry();
1941}
1942
1943bool TabContents::CreateRenderViewForRenderManager(
1944 RenderViewHost* render_view_host) {
[email protected]420ae012009-04-24 05:16:321945 RenderWidgetHostView* rwh_view = view_->CreateViewForWidget(render_view_host);
[email protected]1a98a932009-11-17 00:12:521946
[email protected]420ae012009-04-24 05:16:321947 // Now that the RenderView has been created, we need to tell it its size.
1948 rwh_view->SetSize(view_->GetContainerSize());
1949
[email protected]a4127722011-04-27 23:13:521950 if (!render_view_host->CreateRenderView(string16()))
1951 return false;
1952
[email protected]f8233cc2011-05-31 20:24:501953#if defined(OS_LINUX)
1954 // Force a ViewMsg_Resize to be sent, needed to make plugins show up on
1955 // linux. See crbug.com/83941.
1956 if (RenderWidgetHost* render_widget_host = rwh_view->GetRenderWidgetHost())
1957 render_widget_host->WasResized();
1958#endif
1959
[email protected]420ae012009-04-24 05:16:321960 UpdateMaxPageIDIfNecessary(render_view_host->site_instance(),
1961 render_view_host);
1962 return true;
1963}
1964
[email protected]3ab9cb82011-06-03 18:02:071965void TabContents::OnDialogClosed(IPC::Message* reply_msg,
1966 bool success,
1967 const string16& user_input) {
[email protected]beb440c2009-11-06 04:08:541968 if (is_showing_before_unload_dialog_ && !success) {
1969 // If a beforeunload dialog is canceled, we need to stop the throbber from
1970 // spinning, since we forced it to start spinning in Navigate.
1971 DidStopLoading();
1972
1973 tab_close_start_time_ = base::TimeTicks();
1974 }
1975 is_showing_before_unload_dialog_ = false;
[email protected]4f5ce842011-05-27 19:34:411976 render_view_host()->JavaScriptDialogClosed(reply_msg,
1977 success,
[email protected]3ab9cb82011-06-03 18:02:071978 user_input);
[email protected]beb440c2009-11-06 04:08:541979}
1980
[email protected]3ab9cb82011-06-03 18:02:071981gfx::NativeWindow TabContents::GetDialogRootWindow() {
1982 return view_->GetTopLevelNativeWindow();
[email protected]beb440c2009-11-06 04:08:541983}
1984
[email protected]a1e97f02011-06-30 14:04:341985void TabContents::OnDialogShown() {
1986 Activate();
[email protected]3a3d47472010-07-15 21:03:541987}
1988
[email protected]41fc0322009-09-04 22:23:401989void TabContents::set_encoding(const std::string& encoding) {
[email protected]763ec4ca2011-04-29 15:48:121990 encoding_ = content::GetContentClient()->browser()->
1991 GetCanonicalEncodingNameByAliasName(encoding);
[email protected]aacd7cc2009-09-02 14:51:101992}
[email protected]f45d2a72010-03-08 23:28:351993
[email protected]33f74972010-12-08 16:40:361994void TabContents::CreateViewAndSetSizeForRVH(RenderViewHost* rvh) {
1995 RenderWidgetHostView* rwh_view = view()->CreateViewForWidget(rvh);
1996 rwh_view->SetSize(view()->GetContainerSize());
1997}
[email protected]e9621112011-10-17 05:38:371998
1999bool TabContents::GotResponseToLockMouseRequest(bool allowed) {
2000 return render_view_host() ?
2001 render_view_host()->GotResponseToLockMouseRequest(allowed) : false;
2002}
2003