blob: 4119f0f22b2fbe09950f2b64a65705a2eda15241 [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]96d185d2009-04-24 03:28:5416#include "chrome/browser/browser_process.h"
[email protected]cf68b092010-01-15 20:05:5517#include "chrome/browser/browser_shutdown.h"
[email protected]5f24f212010-12-02 14:47:4518#include "chrome/browser/content_settings/content_settings_details.h"
[email protected]9d01a6a2010-11-30 12:03:3319#include "chrome/browser/content_settings/host_content_settings_map.h"
[email protected]96d185d2009-04-24 03:28:5420#include "chrome/browser/debugger/devtools_manager.h"
[email protected]c8f74942010-09-03 10:04:1421#include "chrome/browser/defaults.h"
[email protected]bcef0dc02009-02-28 00:35:0222#include "chrome/browser/download/download_item_model.h"
[email protected]420ae012009-04-24 05:16:3223#include "chrome/browser/download/download_manager.h"
[email protected]41291322010-07-15 17:09:0124#include "chrome/browser/download/download_request_limiter.h"
[email protected]3c733bde2010-12-21 19:56:3125#include "chrome/browser/external_protocol_handler.h"
[email protected]ef0d04c2010-12-15 23:34:2126#include "chrome/browser/history/history.h"
[email protected]ec0b6c42010-08-26 03:16:5827#include "chrome/browser/history/history_types.h"
[email protected]420ae012009-04-24 05:16:3228#include "chrome/browser/load_from_memory_cache_details.h"
29#include "chrome/browser/load_notification_details.h"
[email protected]f364d1392011-04-08 21:03:1030#include "chrome/browser/notifications/desktop_notification_service.h"
[email protected]dcb72d52011-04-13 12:36:5331#include "chrome/browser/notifications/desktop_notification_service_factory.h"
[email protected]14a000d2010-04-29 21:44:2432#include "chrome/browser/platform_util.h"
[email protected]67c052e2011-01-31 16:33:5833#include "chrome/browser/plugin_observer.h"
[email protected]36fb2c7c2011-04-04 15:49:0834#include "chrome/browser/prefs/pref_service.h"
[email protected]8ecad5e2010-12-02 21:18:3335#include "chrome/browser/profiles/profile.h"
[email protected]96d185d2009-04-24 03:28:5436#include "chrome/browser/renderer_host/web_cache_manager.h"
[email protected]93623c5d2009-12-10 21:40:3237#include "chrome/browser/renderer_preferences_util.h"
[email protected]c37e3b62010-01-22 00:15:1738#include "chrome/browser/sessions/session_types.h"
[email protected]a40f6e92011-03-04 23:30:2939#include "chrome/browser/tab_contents/infobar_delegate.h"
[email protected]1f18184a2010-07-21 19:34:4940#include "chrome/browser/tab_contents/tab_contents_ssl_helper.h"
[email protected]1caa1522010-12-01 20:23:3541#include "chrome/browser/ui/app_modal_dialogs/message_box_handler.h"
[email protected]7ec6bef2011-02-26 02:22:2942#include "chrome/browser/ui/browser_dialogs.h"
[email protected]1698f612011-03-11 11:28:1543#include "chrome/common/chrome_constants.h"
[email protected]35f7d212009-04-29 21:19:2744#include "chrome/common/chrome_switches.h"
initial.commit09911bf2008-07-26 23:55:2945#include "chrome/common/pref_names.h"
[email protected]420ae012009-04-24 05:16:3246#include "chrome/common/render_messages.h"
[email protected]96d185d2009-04-24 03:28:5447#include "chrome/common/url_constants.h"
[email protected]567812d2011-02-24 17:40:5048#include "content/browser/child_process_security_policy.h"
[email protected]1fd1a502011-03-30 16:55:5649#include "content/browser/content_browser_client.h"
[email protected]567812d2011-02-24 17:40:5050#include "content/browser/host_zoom_map.h"
51#include "content/browser/in_process_webkit/session_storage_namespace.h"
52#include "content/browser/renderer_host/render_process_host.h"
53#include "content/browser/renderer_host/render_view_host.h"
54#include "content/browser/renderer_host/render_widget_host_view.h"
55#include "content/browser/renderer_host/resource_request_details.h"
56#include "content/browser/site_instance.h"
[email protected]0dd3a0ab2011-02-18 08:17:4457#include "content/browser/tab_contents/interstitial_page.h"
58#include "content/browser/tab_contents/navigation_entry.h"
59#include "content/browser/tab_contents/provisional_load_details.h"
60#include "content/browser/tab_contents/tab_contents_delegate.h"
61#include "content/browser/tab_contents/tab_contents_observer.h"
62#include "content/browser/tab_contents/tab_contents_view.h"
[email protected]afd1e522011-04-27 23:29:5963#include "content/browser/user_metrics.h"
[email protected]1fd1a502011-03-30 16:55:5664#include "content/browser/webui/web_ui_factory.h"
[email protected]9966325b2011-04-18 05:00:1065#include "content/common/bindings_policy.h"
[email protected]1fd1a502011-03-30 16:55:5666#include "content/common/content_client.h"
[email protected]763ec4ca2011-04-29 15:48:1267#include "content/common/content_restriction.h"
[email protected]4dd57932011-03-17 06:06:1268#include "content/common/navigation_types.h"
[email protected]7f070d42011-03-09 20:25:3269#include "content/common/notification_service.h"
[email protected]2c5569662011-03-22 20:45:0270#include "content/common/view_messages.h"
[email protected]d686e812009-06-03 19:10:2971#include "net/base/net_util.h"
[email protected]420ae012009-04-24 05:16:3272#include "net/base/registry_controlled_domain.h"
[email protected]abe2c032011-03-31 18:49:3473#include "net/url_request/url_request_context_getter.h"
[email protected]8bd0fe62011-01-17 06:44:3774#include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h"
[email protected]42ce29d2011-01-20 23:19:4675#include "ui/base/resource/resource_bundle.h"
[email protected]08397d52011-02-05 01:53:3876#include "ui/gfx/codec/png_codec.h"
[email protected]d5e311b2010-07-20 17:15:0377#include "webkit/glue/password_form.h"
[email protected]3c733bde2010-12-21 19:56:3178#include "webkit/glue/webpreferences.h"
79
80#if defined(OS_MACOSX)
[email protected]b9b751f22011-03-25 14:04:1281#include "ui/gfx/surface/io_surface_support_mac.h"
[email protected]3c733bde2010-12-21 19:56:3182#endif // defined(OS_MACOSX)
[email protected]3e45ba92009-02-20 21:09:0083
[email protected]420ae012009-04-24 05:16:3284// Cross-Site Navigations
85//
86// If a TabContents is told to navigate to a different web site (as determined
87// by SiteInstance), it will replace its current RenderViewHost with a new
88// RenderViewHost dedicated to the new SiteInstance. This works as follows:
89//
90// - Navigate determines whether the destination is cross-site, and if so,
91// it creates a pending_render_view_host_ and moves into the PENDING
92// RendererState.
93// - The pending RVH is "suspended," so that no navigation messages are sent to
94// its renderer until the onbeforeunload JavaScript handler has a chance to
95// run in the current RVH.
96// - The pending RVH tells CrossSiteRequestManager (a thread-safe singleton)
97// that it has a pending cross-site request. ResourceDispatcherHost will
98// check for this when the response arrives.
99// - The current RVH runs its onbeforeunload handler. If it returns false, we
100// cancel all the pending logic and go back to NORMAL. Otherwise we allow
101// the pending RVH to send the navigation request to its renderer.
102// - ResourceDispatcherHost receives a ResourceRequest on the IO thread. It
103// checks CrossSiteRequestManager to see that the RVH responsible has a
104// pending cross-site request, and then installs a CrossSiteEventHandler.
105// - When RDH receives a response, the BufferedEventHandler determines whether
106// it is a download. If so, it sends a message to the new renderer causing
107// it to cancel the request, and the download proceeds in the download
108// thread. For now, we stay in a PENDING state (with a pending RVH) until
[email protected]57c6a652009-05-04 07:58:34109// the next DidNavigate event for this TabContents. This isn't ideal, but it
[email protected]420ae012009-04-24 05:16:32110// doesn't affect any functionality.
111// - After RDH receives a response and determines that it is safe and not a
112// download, it pauses the response to first run the old page's onunload
113// handler. It does this by asynchronously calling the OnCrossSiteResponse
[email protected]57c6a652009-05-04 07:58:34114// method of TabContents on the UI thread, which sends a ClosePage message
[email protected]420ae012009-04-24 05:16:32115// to the current RVH.
116// - Once the onunload handler is finished, a ClosePage_ACK message is sent to
117// the ResourceDispatcherHost, who unpauses the response. Data is then sent
118// to the pending RVH.
119// - The pending renderer sends a FrameNavigate message that invokes the
120// DidNavigate method. This replaces the current RVH with the
121// pending RVH and goes back to the NORMAL RendererState.
122
123namespace {
124
125// Amount of time we wait between when a key event is received and the renderer
126// is queried for its state and pushed to the NavigationEntry.
127const int kQueryStateDelay = 5000;
128
[email protected]6ebdc9b2010-09-27 16:55:57129const int kSyncWaitDelay = 40;
130
[email protected]420ae012009-04-24 05:16:32131// The list of prefs we want to observe.
[email protected]57ecc4b2010-08-11 03:02:51132const char* kPrefsToObserve[] = {
[email protected]420ae012009-04-24 05:16:32133 prefs::kAlternateErrorPagesEnabled,
[email protected]d0b8d092010-10-25 04:05:17134 prefs::kDefaultZoomLevel,
[email protected]420ae012009-04-24 05:16:32135 prefs::kWebKitJavaEnabled,
136 prefs::kWebKitJavascriptEnabled,
137 prefs::kWebKitLoadsImagesAutomatically,
138 prefs::kWebKitPluginsEnabled,
139 prefs::kWebKitUsesUniversalDetector,
[email protected]16480f62011-03-01 23:39:10140 prefs::kWebKitStandardFontFamily,
[email protected]420ae012009-04-24 05:16:32141 prefs::kWebKitSerifFontFamily,
142 prefs::kWebKitSansSerifFontFamily,
143 prefs::kWebKitFixedFontFamily,
144 prefs::kWebKitDefaultFontSize,
145 prefs::kWebKitDefaultFixedFontSize,
[email protected]6bdf05b2010-10-13 20:34:13146 prefs::kWebKitMinimumFontSize,
147 prefs::kWebKitMinimumLogicalFontSize,
[email protected]1661a3c02010-05-20 16:32:36148 prefs::kWebkitTabsToLinks,
[email protected]0a8db0d2011-04-13 15:15:40149 prefs::kDefaultCharset,
150 prefs::kEnableReferrers
[email protected]420ae012009-04-24 05:16:32151};
152
153const int kPrefsToObserveLength = arraysize(kPrefsToObserve);
154
[email protected]420ae012009-04-24 05:16:32155#if defined(OS_WIN)
156
157BOOL CALLBACK InvalidateWindow(HWND hwnd, LPARAM lparam) {
158 // Note: erase is required to properly paint some widgets borders. This can
159 // be seen with textfields.
160 InvalidateRect(hwnd, NULL, TRUE);
161 return TRUE;
162}
163#endif
164
[email protected]2c5569662011-03-22 20:45:02165ViewMsg_Navigate_Type::Value GetNavigationType(
[email protected]1ccb3568d2010-02-19 10:51:16166 Profile* profile, const NavigationEntry& entry,
167 NavigationController::ReloadType reload_type) {
168 switch (reload_type) {
169 case NavigationController::RELOAD:
[email protected]2c5569662011-03-22 20:45:02170 return ViewMsg_Navigate_Type::RELOAD;
[email protected]1ccb3568d2010-02-19 10:51:16171 case NavigationController::RELOAD_IGNORING_CACHE:
[email protected]2c5569662011-03-22 20:45:02172 return ViewMsg_Navigate_Type::RELOAD_IGNORING_CACHE;
[email protected]1ccb3568d2010-02-19 10:51:16173 case NavigationController::NO_RELOAD:
174 break; // Fall through to rest of function.
175 }
[email protected]5e369672009-11-03 23:48:30176
177 if (entry.restore_type() == NavigationEntry::RESTORE_LAST_SESSION &&
178 profile->DidLastSessionExitCleanly())
[email protected]2c5569662011-03-22 20:45:02179 return ViewMsg_Navigate_Type::RESTORE;
[email protected]5e369672009-11-03 23:48:30180
[email protected]2c5569662011-03-22 20:45:02181 return ViewMsg_Navigate_Type::NORMAL;
[email protected]5e369672009-11-03 23:48:30182}
183
[email protected]876bc832010-09-07 16:29:54184void MakeNavigateParams(const NavigationEntry& entry,
185 const NavigationController& controller,
[email protected]1ccb3568d2010-02-19 10:51:16186 NavigationController::ReloadType reload_type,
187 ViewMsg_Navigate_Params* params) {
[email protected]056de2d2009-06-26 16:41:34188 params->page_id = entry.page_id();
[email protected]876bc832010-09-07 16:29:54189 params->pending_history_list_offset = controller.GetIndexOfEntry(&entry);
[email protected]3cc72b12010-03-18 23:03:00190 params->current_history_list_offset = controller.last_committed_entry_index();
191 params->current_history_list_length = controller.entry_count();
[email protected]056de2d2009-06-26 16:41:34192 params->url = entry.url();
193 params->referrer = entry.referrer();
194 params->transition = entry.transition_type();
195 params->state = entry.content_state();
[email protected]3cc72b12010-03-18 23:03:00196 params->navigation_type =
197 GetNavigationType(controller.profile(), entry, reload_type);
[email protected]056de2d2009-06-26 16:41:34198 params->request_time = base::Time::Now();
199}
200
[email protected]420ae012009-04-24 05:16:32201} // namespace
202
[email protected]f4f50ef2011-01-21 19:01:19203
204// TabContents ----------------------------------------------------------------
[email protected]420ae012009-04-24 05:16:32205
[email protected]420ae012009-04-24 05:16:32206TabContents::TabContents(Profile* profile,
207 SiteInstance* site_instance,
208 int routing_id,
[email protected]6ee12c42010-09-14 09:36:07209 const TabContents* base_tab_contents,
210 SessionStorageNamespace* session_storage_namespace)
[email protected]b680ad22009-04-15 23:19:42211 : delegate_(NULL),
[email protected]6ee12c42010-09-14 09:36:07212 ALLOW_THIS_IN_INITIALIZER_LIST(controller_(
213 this, profile, session_storage_namespace)),
[email protected]66ba4932009-06-04 19:22:13214 ALLOW_THIS_IN_INITIALIZER_LIST(view_(
[email protected]d82ed61e2009-06-16 02:46:22215 TabContentsView::Create(this))),
216 ALLOW_THIS_IN_INITIALIZER_LIST(render_manager_(this, this)),
[email protected]6f821dd2010-02-23 00:54:06217 bookmark_drag_(NULL),
[email protected]d5f942ba2008-09-26 19:30:34218 is_loading_(false),
[email protected]443b80e2010-12-14 00:42:23219 crashed_status_(base::TERMINATION_STATUS_STILL_RUNNING),
220 crashed_error_code_(0),
[email protected]d5f942ba2008-09-26 19:30:34221 waiting_for_response_(false),
[email protected]d5f942ba2008-09-26 19:30:34222 max_page_id_(-1),
[email protected]fdd61c62009-04-22 19:22:57223 load_state_(net::LOAD_STATE_IDLE),
[email protected]094e5b22009-09-25 04:23:56224 upload_size_(0),
225 upload_position_(0),
[email protected]fdd61c62009-04-22 19:22:57226 received_page_title_(false),
[email protected]f17a0ee2010-05-17 17:38:47227 displayed_insecure_content_(false),
[email protected]fdd61c62009-04-22 19:22:57228 capturing_contents_(false),
229 is_being_destroyed_(false),
230 notify_disconnection_(false),
[email protected]fdd61c62009-04-22 19:22:57231#if defined(OS_WIN)
232 message_box_active_(CreateEvent(NULL, TRUE, FALSE, NULL)),
233#endif
[email protected]c2e74fe82009-09-03 17:57:44234 suppress_javascript_messages_(false),
[email protected]7ab1e7d62009-10-14 23:32:01235 is_showing_before_unload_dialog_(false),
[email protected]1fd1a502011-03-30 16:55:56236 opener_web_ui_type_(WebUI::kNoWebUI),
[email protected]ebf40a72010-07-22 01:46:38237 closed_by_user_gesture_(false),
[email protected]b75b8292010-10-01 07:28:25238 minimum_zoom_percent_(
239 static_cast<int>(WebKit::WebView::minTextSizeMultiplier * 100)),
240 maximum_zoom_percent_(
241 static_cast<int>(WebKit::WebView::maxTextSizeMultiplier * 100)),
[email protected]9e823662010-10-13 23:36:00242 temporary_zoom_settings_(false),
[email protected]4850a7f2011-03-08 23:36:59243 content_restrictions_(0) {
[email protected]7ff431e2010-01-07 18:18:54244 renderer_preferences_util::UpdateFromSystemSettings(
245 &renderer_preferences_, profile);
246
[email protected]12636df2009-09-28 22:32:21247 render_manager_.Init(profile, site_instance, routing_id);
[email protected]420ae012009-04-24 05:16:32248
[email protected]34ac70502009-09-25 17:07:23249 // We have the initial size of the view be based on the size of the passed in
250 // tab contents (normally a tab from the same window).
251 view_->CreateView(base_tab_contents ?
252 base_tab_contents->view()->GetContainerSize() : gfx::Size());
[email protected]420ae012009-04-24 05:16:32253
254 // Register for notifications about all interested prefs change.
255 PrefService* prefs = profile->GetPrefs();
[email protected]2fb7dc982010-09-29 12:24:28256 pref_change_registrar_.Init(prefs);
[email protected]420ae012009-04-24 05:16:32257 if (prefs) {
258 for (int i = 0; i < kPrefsToObserveLength; ++i)
[email protected]2fb7dc982010-09-29 12:24:28259 pref_change_registrar_.Add(kPrefsToObserve[i], this);
[email protected]420ae012009-04-24 05:16:32260 }
261
[email protected]420ae012009-04-24 05:16:32262 registrar_.Add(this, NotificationType::RENDER_WIDGET_HOST_DESTROYED,
263 NotificationService::AllSources());
[email protected]d3ba77272009-09-03 00:06:09264#if defined(OS_LINUX)
265 registrar_.Add(this, NotificationType::BROWSER_THEME_CHANGED,
266 NotificationService::AllSources());
267#endif
[email protected]420ae012009-04-24 05:16:32268
[email protected]6c32ce72010-03-08 05:18:06269 registrar_.Add(this, NotificationType::USER_STYLE_SHEET_UPDATED,
270 NotificationService::AllSources());
271
[email protected]79580c62010-02-02 02:36:25272 // Register for notifications about content setting changes.
273 registrar_.Add(this, NotificationType::CONTENT_SETTINGS_CHANGED,
274 NotificationService::AllSources());
275
[email protected]969182a2011-03-03 14:53:23276 // Listen for Google URL changes.
[email protected]88152052010-10-01 04:05:18277 registrar_.Add(this, NotificationType::GOOGLE_URL_UPDATED,
278 NotificationService::AllSources());
279
[email protected]b5a1d11c2011-02-17 03:09:42280 // Can only add observers after render_manager_.Init() is called, since that's
281 // what sets up the render_view_host which TabContentObserver's constructor
282 // uses to get the routing_id.
283 AddObservers();
[email protected]332af7732009-03-11 13:21:35284}
initial.commit09911bf2008-07-26 23:55:29285
286TabContents::~TabContents() {
[email protected]420ae012009-04-24 05:16:32287 is_being_destroyed_ = true;
288
[email protected]bfe4c1582009-10-02 18:11:09289 // We don't want any notifications while we're running our destructor.
[email protected]420ae012009-04-24 05:16:32290 registrar_.RemoveAll();
[email protected]2fb7dc982010-09-29 12:24:28291 pref_change_registrar_.RemoveAll();
[email protected]420ae012009-04-24 05:16:32292
[email protected]420ae012009-04-24 05:16:32293 NotifyDisconnected();
[email protected]7ec6bef2011-02-26 02:22:29294 browser::HideHungRendererDialog(this);
[email protected]420ae012009-04-24 05:16:32295
[email protected]420ae012009-04-24 05:16:32296 // First cleanly close all child windows.
297 // TODO(mpcomplete): handle case if MaybeCloseChildWindows() already asked
298 // some of these to close. CloseWindows is async, so it might get called
299 // twice before it runs.
[email protected]8cc8d492010-02-02 10:40:49300 CloseConstrainedWindows();
[email protected]420ae012009-04-24 05:16:32301
[email protected]420ae012009-04-24 05:16:32302 // Notify any observer that have a reference on this tab contents.
303 NotificationService::current()->Notify(
304 NotificationType::TAB_CONTENTS_DESTROYED,
305 Source<TabContents>(this),
306 NotificationService::NoDetails());
307
[email protected]1e5e74f2009-05-27 20:55:12308 // Notify any lasting InfobarDelegates that have not yet been removed that
309 // whatever infobar they were handling in this TabContents has closed,
310 // because the TabContents is going away entirely.
311 // This must happen after the TAB_CONTENTS_DESTROYED notification as the
312 // notification may trigger infobars calls that access their delegate. (and
313 // some implementations of InfoBarDelegate do delete themselves on
314 // InfoBarClosed()).
[email protected]ec8f51162011-02-08 04:20:40315 for (size_t i = 0; i < infobar_count(); ++i) {
[email protected]1e5e74f2009-05-27 20:55:12316 InfoBarDelegate* delegate = GetInfoBarDelegateAt(i);
317 delegate->InfoBarClosed();
318 }
319 infobar_delegates_.clear();
320
[email protected]420ae012009-04-24 05:16:32321 // TODO(brettw) this should be moved to the view.
322#if defined(OS_WIN)
323 // If we still have a window handle, destroy it. GetNativeView can return
324 // NULL if this contents was part of a window that closed.
[email protected]50bd6452010-11-27 19:39:42325 if (GetNativeView()) {
326 RenderViewHost* host = render_view_host();
[email protected]b9a1fb42011-04-12 23:16:44327 if (host && host->view())
[email protected]50bd6452010-11-27 19:39:42328 host->view()->WillWmDestroy();
[email protected]50bd6452010-11-27 19:39:42329 }
[email protected]420ae012009-04-24 05:16:32330#endif
[email protected]7ab1e7d62009-10-14 23:32:01331
332 // OnCloseStarted isn't called in unit tests.
333 if (!tab_close_start_time_.is_null()) {
334 UMA_HISTOGRAM_TIMES("Tab.Close",
335 base::TimeTicks::Now() - tab_close_start_time_);
336 }
[email protected]b5a1d11c2011-02-17 03:09:42337
[email protected]07d490bc2011-03-07 17:05:26338 FOR_EACH_OBSERVER(TabContentsObserver, observers_, TabContentsDestroyed());
[email protected]232a5812011-03-04 22:42:08339
340 net::NetworkChangeNotifier::RemoveOnlineStateObserver(this);
[email protected]b5a1d11c2011-02-17 03:09:42341}
342
343void TabContents::AddObservers() {
[email protected]5327dfb2011-05-03 17:50:36344 content_settings_delegate_.reset(new TabSpecificContentSettings(this));
[email protected]b5a1d11c2011-02-17 03:09:42345 plugin_observer_.reset(new PluginObserver(this));
[email protected]232a5812011-03-04 22:42:08346 net::NetworkChangeNotifier::AddOnlineStateObserver(this);
initial.commit09911bf2008-07-26 23:55:29347}
348
[email protected]724159a2010-12-30 01:11:18349bool TabContents::OnMessageReceived(const IPC::Message& message) {
[email protected]f82d57b52011-04-27 19:13:17350 if (web_ui() && web_ui()->OnMessageReceived(message))
351 return true;
352
[email protected]585b30362011-01-28 02:30:17353 ObserverListBase<TabContentsObserver>::Iterator it(observers_);
354 TabContentsObserver* observer;
[email protected]0f180ee2011-01-21 18:23:10355 while ((observer = it.GetNext()) != NULL)
356 if (observer->OnMessageReceived(message))
[email protected]403415a2011-01-10 18:57:53357 return true;
[email protected]403415a2011-01-10 18:57:53358
[email protected]724159a2010-12-30 01:11:18359 bool handled = true;
360 bool message_is_ok = true;
361 IPC_BEGIN_MESSAGE_MAP_EX(TabContents, message, message_is_ok)
362 IPC_MESSAGE_HANDLER(ViewHostMsg_DidStartProvisionalLoadForFrame,
363 OnDidStartProvisionalLoadForFrame)
364 IPC_MESSAGE_HANDLER(ViewHostMsg_DidRedirectProvisionalLoad,
365 OnDidRedirectProvisionalLoad)
366 IPC_MESSAGE_HANDLER(ViewHostMsg_DidFailProvisionalLoadWithError,
367 OnDidFailProvisionalLoadWithError)
368 IPC_MESSAGE_HANDLER(ViewHostMsg_DidLoadResourceFromMemoryCache,
369 OnDidLoadResourceFromMemoryCache)
370 IPC_MESSAGE_HANDLER(ViewHostMsg_DidDisplayInsecureContent,
371 OnDidDisplayInsecureContent)
372 IPC_MESSAGE_HANDLER(ViewHostMsg_DidRunInsecureContent,
373 OnDidRunInsecureContent)
374 IPC_MESSAGE_HANDLER(ViewHostMsg_DocumentLoadedInFrame,
375 OnDocumentLoadedInFrame)
376 IPC_MESSAGE_HANDLER(ViewHostMsg_DidFinishLoad, OnDidFinishLoad)
[email protected]c8f73ab2011-01-22 00:05:17377 IPC_MESSAGE_HANDLER(ViewHostMsg_UpdateContentRestrictions,
378 OnUpdateContentRestrictions)
[email protected]7d472472011-01-22 01:30:25379 IPC_MESSAGE_HANDLER(ViewHostMsg_GoToEntryAtOffset, OnGoToEntryAtOffset)
[email protected]724159a2010-12-30 01:11:18380 IPC_MESSAGE_UNHANDLED(handled = false)
381 IPC_END_MESSAGE_MAP_EX()
382
383 if (!message_is_ok) {
384 UserMetrics::RecordAction(UserMetricsAction("BadMessageTerminate_RVD"));
385 GetRenderProcessHost()->ReceivedBadMessage();
386 }
387
388 return handled;
389}
390
[email protected]13367f72009-07-08 16:39:02391// Returns true if contains content rendered by an extension.
392bool TabContents::HostsExtension() const {
393 return GetURL().SchemeIs(chrome::kExtensionScheme);
[email protected]d5f942ba2008-09-26 19:30:34394}
395
[email protected]1f18184a2010-07-21 19:34:49396TabContentsSSLHelper* TabContents::GetSSLHelper() {
397 if (ssl_helper_.get() == NULL)
398 ssl_helper_.reset(new TabContentsSSLHelper(this));
399 return ssl_helper_.get();
400}
401
[email protected]8cb5d5b2010-02-09 11:36:16402RenderProcessHost* TabContents::GetRenderProcessHost() const {
403 return render_manager_.current_host()->process();
404}
405
[email protected]d5f942ba2008-09-26 19:30:34406const GURL& TabContents::GetURL() const {
407 // We may not have a navigation entry yet
[email protected]ce3fa3c2009-04-20 19:55:57408 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]ebe89e062009-08-13 23:16:54409 return entry ? entry->virtual_url() : GURL::EmptyGURL();
[email protected]d5f942ba2008-09-26 19:30:34410}
411
[email protected]96d185d2009-04-24 03:28:54412const string16& TabContents::GetTitle() const {
[email protected]4c6092c5b2009-06-06 00:23:55413 // Transient entries take precedence. They are used for interstitial pages
414 // that are shown on top of existing pages.
415 NavigationEntry* entry = controller_.GetTransientEntry();
[email protected]45d0ef7f2011-01-05 13:46:23416 if (entry) {
417 return entry->GetTitleForDisplay(profile()->GetPrefs()->
[email protected]6b2f7a82011-04-25 19:30:51418 GetString(prefs::kAcceptLanguages));
[email protected]45d0ef7f2011-01-05 13:46:23419 }
[email protected]7ade2732011-02-10 00:13:58420 WebUI* our_web_ui = render_manager_.pending_web_ui() ?
421 render_manager_.pending_web_ui() : render_manager_.web_ui();
422 if (our_web_ui) {
[email protected]96d185d2009-04-24 03:28:54423 // Don't override the title in view source mode.
[email protected]4c6092c5b2009-06-06 00:23:55424 entry = controller_.GetActiveEntry();
[email protected]96d185d2009-04-24 03:28:54425 if (!(entry && entry->IsViewSourceMode())) {
[email protected]e0112912011-02-02 22:54:35426 // Give the Web UI the chance to override our title.
[email protected]7ade2732011-02-10 00:13:58427 const string16& title = our_web_ui->overridden_title();
[email protected]96d185d2009-04-24 03:28:54428 if (!title.empty())
429 return title;
430 }
431 }
432
433 // We use the title for the last committed entry rather than a pending
434 // navigation entry. For example, when the user types in a URL, we want to
435 // keep the old page's title until the new load has committed and we get a new
436 // title.
[email protected]96d185d2009-04-24 03:28:54437 entry = controller_.GetLastCommittedEntry();
[email protected]45d0ef7f2011-01-05 13:46:23438 if (entry) {
439 return entry->GetTitleForDisplay(profile()->GetPrefs()->
[email protected]6b2f7a82011-04-25 19:30:51440 GetString(prefs::kAcceptLanguages));
[email protected]45d0ef7f2011-01-05 13:46:23441 }
[email protected]96d185d2009-04-24 03:28:54442 return EmptyString16();
443}
444
[email protected]d5f942ba2008-09-26 19:30:34445int32 TabContents::GetMaxPageID() {
446 if (GetSiteInstance())
447 return GetSiteInstance()->max_page_id();
448 else
449 return max_page_id_;
450}
451
452void TabContents::UpdateMaxPageID(int32 page_id) {
453 // Ensure both the SiteInstance and RenderProcessHost update their max page
[email protected]57c6a652009-05-04 07:58:34454 // IDs in sync. Only TabContents will also have site instances, except during
[email protected]d5f942ba2008-09-26 19:30:34455 // testing.
456 if (GetSiteInstance())
457 GetSiteInstance()->UpdateMaxPageID(page_id);
[email protected]8cb5d5b2010-02-09 11:36:16458 GetRenderProcessHost()->UpdateMaxPageID(page_id);
[email protected]d5f942ba2008-09-26 19:30:34459}
460
[email protected]96d185d2009-04-24 03:28:54461SiteInstance* TabContents::GetSiteInstance() const {
462 return render_manager_.current_host()->site_instance();
463}
464
[email protected]96d185d2009-04-24 03:28:54465bool TabContents::ShouldDisplayURL() {
[email protected]8ab24cc2009-11-13 20:56:15466 // Don't hide the url in view source mode and with interstitials.
[email protected]96d185d2009-04-24 03:28:54467 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]8ab24cc2009-11-13 20:56:15468 if (entry && (entry->IsViewSourceMode() ||
[email protected]cccd3762010-11-12 18:40:01469 entry->page_type() == INTERSTITIAL_PAGE)) {
[email protected]96d185d2009-04-24 03:28:54470 return true;
[email protected]8ab24cc2009-11-13 20:56:15471 }
[email protected]83e3895b2009-06-11 00:07:16472
[email protected]2b9a06402011-02-16 04:57:10473 // We always display the URL for non-WebUI URLs to prevent spoofing.
[email protected]1fd1a502011-03-30 16:55:56474 if (entry && !content::WebUIFactory::Get()->HasWebUIScheme(entry->url()))
[email protected]2b9a06402011-02-16 04:57:10475 return true;
476
[email protected]d0980792011-02-13 19:41:40477 WebUI* web_ui = GetWebUIForCurrentState();
[email protected]7ade2732011-02-10 00:13:58478 if (web_ui)
479 return !web_ui->should_hide_url();
[email protected]96d185d2009-04-24 03:28:54480 return true;
481}
482
[email protected]585b30362011-01-28 02:30:17483void TabContents::AddObserver(TabContentsObserver* observer) {
484 observers_.AddObserver(observer);
[email protected]3c9e1872010-11-18 16:17:49485}
486
[email protected]585b30362011-01-28 02:30:17487void TabContents::RemoveObserver(TabContentsObserver* observer) {
488 observers_.RemoveObserver(observer);
[email protected]3c9e1872010-11-18 16:17:49489}
490
[email protected]443b80e2010-12-14 00:42:23491void TabContents::SetIsCrashed(base::TerminationStatus status, int error_code) {
492 if (status == crashed_status_)
[email protected]d5f942ba2008-09-26 19:30:34493 return;
494
[email protected]443b80e2010-12-14 00:42:23495 crashed_status_ = status;
496 crashed_error_code_ = error_code;
[email protected]c9cd2222009-05-06 05:16:50497 NotifyNavigationStateChanged(INVALIDATE_TAB);
[email protected]d5f942ba2008-09-26 19:30:34498}
499
500void TabContents::NotifyNavigationStateChanged(unsigned changed_flags) {
501 if (delegate_)
502 delegate_->NavigationStateChanged(this, changed_flags);
503}
504
[email protected]96d185d2009-04-24 03:28:54505void TabContents::DidBecomeSelected() {
506 controller_.SetActive(true);
[email protected]8cb5d5b2010-02-09 11:36:16507 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
508 if (rwhv) {
509 rwhv->DidBecomeSelected();
[email protected]789e9152009-08-04 21:59:43510#if defined(OS_MACOSX)
[email protected]8cb5d5b2010-02-09 11:36:16511 rwhv->SetActive(true);
[email protected]789e9152009-08-04 21:59:43512#endif
513 }
[email protected]96d185d2009-04-24 03:28:54514
[email protected]8cb5d5b2010-02-09 11:36:16515 WebCacheManager::GetInstance()->ObserveActivity(GetRenderProcessHost()->id());
[email protected]5ac20162010-11-24 23:33:11516 last_selected_time_ = base::TimeTicks::Now();
[email protected]96d185d2009-04-24 03:28:54517}
518
519void TabContents::WasHidden() {
520 if (!capturing_contents()) {
521 // |render_view_host()| can be NULL if the user middle clicks a link to open
522 // a tab in then background, then closes the tab before selecting it. This
523 // is because closing the tab calls TabContents::Destroy(), which removes
524 // the |render_view_host()|; then when we actually destroy the window,
525 // OnWindowPosChanged() notices and calls HideContents() (which calls us).
[email protected]8cb5d5b2010-02-09 11:36:16526 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
527 if (rwhv)
528 rwhv->WasHidden();
[email protected]96d185d2009-04-24 03:28:54529 }
530
531 NotificationService::current()->Notify(
532 NotificationType::TAB_CONTENTS_HIDDEN,
533 Source<TabContents>(this),
534 NotificationService::NoDetails());
535}
536
[email protected]d5f942ba2008-09-26 19:30:34537void TabContents::Activate() {
538 if (delegate_)
539 delegate_->ActivateContents(this);
540}
541
[email protected]ea42e7782010-08-23 23:58:12542void TabContents::Deactivate() {
543 if (delegate_)
544 delegate_->DeactivateContents(this);
545}
546
[email protected]96d185d2009-04-24 03:28:54547void TabContents::ShowContents() {
[email protected]8cb5d5b2010-02-09 11:36:16548 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
549 if (rwhv)
550 rwhv->DidBecomeSelected();
[email protected]96d185d2009-04-24 03:28:54551}
552
553void TabContents::HideContents() {
554 // TODO(pkasting): http://b/1239839 Right now we purposefully don't call
555 // our superclass HideContents(), because some callers want to be very picky
556 // about the order in which these get called. In addition to making the code
557 // here practically impossible to understand, this also means we end up
558 // calling TabContents::WasHidden() twice if callers call both versions of
[email protected]57c6a652009-05-04 07:58:34559 // HideContents() on a TabContents.
[email protected]96d185d2009-04-24 03:28:54560 WasHidden();
561}
562
[email protected]5aab5e22010-12-08 22:13:29563bool TabContents::NeedToFireBeforeUnload() {
564 // TODO(creis): Should we fire even for interstitial pages?
565 return notify_disconnection() &&
566 !showing_interstitial_page() &&
567 !render_view_host()->SuddenTerminationAllowed();
568}
569
[email protected]c0588052008-10-27 23:01:50570void TabContents::OpenURL(const GURL& url, const GURL& referrer,
[email protected]d5f942ba2008-09-26 19:30:34571 WindowOpenDisposition disposition,
572 PageTransition::Type transition) {
[email protected]1c642b52011-04-15 09:05:49573 if (delegate_) {
[email protected]c0588052008-10-27 23:01:50574 delegate_->OpenURLFromTab(this, url, referrer, disposition, transition);
[email protected]1c642b52011-04-15 09:05:49575 // Notify observers.
576 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
577 DidOpenURL(url, referrer, disposition, transition));
578 }
[email protected]d5f942ba2008-09-26 19:30:34579}
580
[email protected]1ccb3568d2010-02-19 10:51:16581bool TabContents::NavigateToPendingEntry(
582 NavigationController::ReloadType reload_type) {
[email protected]876bc832010-09-07 16:29:54583 return NavigateToEntry(*controller_.pending_entry(), reload_type);
584}
[email protected]96d185d2009-04-24 03:28:54585
[email protected]876bc832010-09-07 16:29:54586bool TabContents::NavigateToEntry(
587 const NavigationEntry& entry,
588 NavigationController::ReloadType reload_type) {
[email protected]96d185d2009-04-24 03:28:54589 RenderViewHost* dest_render_view_host = render_manager_.Navigate(entry);
590 if (!dest_render_view_host)
591 return false; // Unable to create the desired render view host.
592
[email protected]770dd8b2010-05-24 18:11:39593 if (delegate_ && delegate_->ShouldEnablePreferredSizeNotifications()) {
594 dest_render_view_host->EnablePreferredSizeChangedMode(
595 kPreferredSizeWidth | kPreferredSizeHeightThisIsSlow);
596 }
[email protected]9fb325e2010-05-06 18:23:24597
[email protected]80a8fad2011-01-29 04:02:38598 // For security, we should never send non-Web-UI URLs to a Web UI renderer.
[email protected]05fdd492010-11-15 17:52:07599 // Double check that here.
[email protected]54ec6472010-04-09 19:39:58600 int enabled_bindings = dest_render_view_host->enabled_bindings();
[email protected]1fd1a502011-03-30 16:55:56601 bool is_allowed_in_web_ui_renderer = content::GetContentClient()->
602 browser()->GetWebUIFactory()->IsURLAcceptableForWebUI(profile(),
603 entry.url());
[email protected]c09163a2011-02-15 00:05:55604 CHECK(!BindingsPolicy::is_web_ui_enabled(enabled_bindings) ||
605 is_allowed_in_web_ui_renderer);
[email protected]54ec6472010-04-09 19:39:58606
[email protected]96d185d2009-04-24 03:28:54607 // Tell DevTools agent that it is attached prior to the navigation.
[email protected]d9f9b792009-06-24 13:17:12608 DevToolsManager* devtools_manager = DevToolsManager::GetInstance();
[email protected]7e8e6b62009-05-08 11:28:32609 if (devtools_manager) { // NULL in unit tests.
[email protected]3cc72b12010-03-18 23:03:00610 devtools_manager->OnNavigatingToPendingEntry(render_view_host(),
611 dest_render_view_host,
612 entry.url());
[email protected]7e8e6b62009-05-08 11:28:32613 }
[email protected]96d185d2009-04-24 03:28:54614
615 // Used for page load time metrics.
616 current_load_start_ = base::TimeTicks::Now();
617
618 // Navigate in the desired RenderViewHost.
[email protected]056de2d2009-06-26 16:41:34619 ViewMsg_Navigate_Params navigate_params;
[email protected]876bc832010-09-07 16:29:54620 MakeNavigateParams(entry, controller_, reload_type, &navigate_params);
[email protected]52c68652010-12-07 17:47:04621 if (delegate_) {
622 navigate_params.extra_headers =
623 delegate_->GetNavigationHeaders(navigate_params.url);
624 }
[email protected]056de2d2009-06-26 16:41:34625 dest_render_view_host->Navigate(navigate_params);
[email protected]96d185d2009-04-24 03:28:54626
627 if (entry.page_id() == -1) {
628 // HACK!! This code suppresses javascript: URLs from being added to
629 // session history, which is what we want to do for javascript: URLs that
630 // do not generate content. What we really need is a message from the
631 // renderer telling us that a new page was not created. The same message
632 // could be used for mailto: URLs and the like.
633 if (entry.url().SchemeIs(chrome::kJavaScriptScheme))
634 return false;
635 }
636
[email protected]3c9e1872010-11-18 16:17:49637 // Notify observers about navigation.
[email protected]b375c5d2011-05-03 21:15:04638 FOR_EACH_OBSERVER(TabContentsObserver,
639 observers_,
640 NavigateToPendingEntry(entry.url(), reload_type));
[email protected]96d185d2009-04-24 03:28:54641
[email protected]d5f942ba2008-09-26 19:30:34642 return true;
643}
644
[email protected]96d185d2009-04-24 03:28:54645void TabContents::Stop() {
646 render_manager_.Stop();
[email protected]2e3f4572011-03-25 19:24:47647 FOR_EACH_OBSERVER(TabContentsObserver, observers_, StopNavigation());
[email protected]96d185d2009-04-24 03:28:54648}
649
[email protected]96d185d2009-04-24 03:28:54650void TabContents::DisassociateFromPopupCount() {
651 render_view_host()->DisassociateFromPopupCount();
652}
653
654TabContents* TabContents::Clone() {
655 // We create a new SiteInstance so that the new tab won't share processes
656 // with the old one. This can be changed in the future if we need it to share
657 // processes for some reason.
[email protected]420ae012009-04-24 05:16:32658 TabContents* tc = new TabContents(profile(),
[email protected]96d185d2009-04-24 03:28:54659 SiteInstance::CreateSiteInstance(profile()),
[email protected]6ee12c42010-09-14 09:36:07660 MSG_ROUTING_NONE, this, NULL);
[email protected]96d185d2009-04-24 03:28:54661 tc->controller().CopyStateFrom(controller_);
662 return tc;
663}
664
[email protected]4d677202009-07-19 07:37:12665void TabContents::ShowPageInfo(const GURL& url,
666 const NavigationEntry::SSLStatus& ssl,
667 bool show_history) {
668 if (!delegate_)
669 return;
670
[email protected]bb678332009-07-21 00:15:50671 delegate_->ShowPageInfo(profile(), url, ssl, show_history);
[email protected]4d677202009-07-19 07:37:12672}
673
initial.commit09911bf2008-07-26 23:55:29674ConstrainedWindow* TabContents::CreateConstrainedDialog(
[email protected]e8382172009-06-19 22:16:28675 ConstrainedWindowDelegate* delegate) {
initial.commit09911bf2008-07-26 23:55:29676 ConstrainedWindow* window =
[email protected]e8382172009-06-19 22:16:28677 ConstrainedWindow::CreateConstrainedDialog(this, delegate);
[email protected]aed59602011-02-28 22:57:33678 AddConstrainedDialog(window);
679 return window;
680}
681
682void TabContents::AddConstrainedDialog(ConstrainedWindow* window) {
initial.commit09911bf2008-07-26 23:55:29683 child_windows_.push_back(window);
[email protected]fa1cf0b82010-01-15 21:49:44684
685 if (child_windows_.size() == 1) {
686 window->ShowConstrainedWindow();
687 BlockTabContent(true);
688 }
initial.commit09911bf2008-07-26 23:55:29689}
690
[email protected]fa1cf0b82010-01-15 21:49:44691void TabContents::BlockTabContent(bool blocked) {
[email protected]8dccd7cb2010-02-25 05:19:55692 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
[email protected]1b072562010-12-13 19:37:45693 // 70% opaque grey.
694 SkColor greyish = SkColorSetARGB(178, 0, 0, 0);
[email protected]8dccd7cb2010-02-25 05:19:55695 if (rwhv)
[email protected]1b072562010-12-13 19:37:45696 rwhv->SetVisuallyDeemphasized(blocked ? &greyish : NULL, false);
[email protected]a7eccac2011-04-15 03:05:19697 // RenderViewHost may be NULL during shutdown.
698 if (render_view_host())
699 render_view_host()->set_ignore_input_events(blocked);
[email protected]fa1cf0b82010-01-15 21:49:44700 if (delegate_)
701 delegate_->SetTabContentBlocked(this, blocked);
702}
703
[email protected]e7cfdbd2011-04-22 14:41:37704void TabContents::AddNewContents(TabContents* new_contents,
705 WindowOpenDisposition disposition,
706 const gfx::Rect& initial_pos,
707 bool user_gesture) {
initial.commit09911bf2008-07-26 23:55:29708 if (!delegate_)
709 return;
710
[email protected]e7cfdbd2011-04-22 14:41:37711 delegate_->AddNewContents(this, new_contents, disposition, initial_pos,
712 user_gesture);
[email protected]96d185d2009-04-24 03:28:54713}
714
[email protected]5c9e97a2009-09-09 23:48:30715gfx::NativeView TabContents::GetContentNativeView() const {
[email protected]96d185d2009-04-24 03:28:54716 return view_->GetContentNativeView();
717}
718
719gfx::NativeView TabContents::GetNativeView() const {
720 return view_->GetNativeView();
721}
722
723void TabContents::GetContainerBounds(gfx::Rect *out) const {
724 view_->GetContainerBounds(out);
725}
726
727void TabContents::Focus() {
728 view_->Focus();
729}
730
[email protected]90daadb42009-06-08 21:27:28731void TabContents::FocusThroughTabTraversal(bool reverse) {
[email protected]7e383692009-06-12 19:14:54732 if (showing_interstitial_page()) {
733 render_manager_.interstitial_page()->FocusThroughTabTraversal(reverse);
734 return;
735 }
[email protected]96d185d2009-04-24 03:28:54736 render_view_host()->SetInitialFocus(reverse);
737}
738
739bool TabContents::FocusLocationBarByDefault() {
[email protected]d0980792011-02-13 19:41:40740 WebUI* web_ui = GetWebUIForCurrentState();
[email protected]7ade2732011-02-10 00:13:58741 if (web_ui)
742 return web_ui->focus_location_bar_by_default();
[email protected]3e3f0eb2009-06-22 18:33:43743 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]e0d481582009-09-15 21:06:25744 if (entry && entry->url() == GURL(chrome::kAboutBlankURL))
[email protected]3e3f0eb2009-06-22 18:33:43745 return true;
[email protected]96d185d2009-04-24 03:28:54746 return false;
747}
748
[email protected]a26dc362010-04-23 01:48:58749void TabContents::SetFocusToLocationBar(bool select_all) {
[email protected]a11aaf22010-03-30 00:03:38750 if (delegate())
[email protected]a26dc362010-04-23 01:48:58751 delegate()->SetFocusToLocationBar(select_all);
[email protected]a11aaf22010-03-30 00:03:38752}
753
[email protected]616ed5a2008-11-21 22:27:24754void TabContents::AddInfoBar(InfoBarDelegate* delegate) {
[email protected]f7019302010-03-26 19:58:32755 if (delegate_ && !delegate_->infobars_enabled()) {
756 delegate->InfoBarClosed();
757 return;
758 }
759
[email protected]616ed5a2008-11-21 22:27:24760 // Look through the existing InfoBarDelegates we have for a match. If we've
761 // already got one that matches, then we don't add the new one.
[email protected]ec8f51162011-02-08 04:20:40762 for (size_t i = 0; i < infobar_count(); ++i) {
[email protected]6d27a7b2009-12-18 23:25:45763 if (GetInfoBarDelegateAt(i)->EqualsDelegate(delegate)) {
764 // Tell the new infobar to close so that it can clean itself up.
765 delegate->InfoBarClosed();
[email protected]616ed5a2008-11-21 22:27:24766 return;
[email protected]6d27a7b2009-12-18 23:25:45767 }
[email protected]616ed5a2008-11-21 22:27:24768 }
769
770 infobar_delegates_.push_back(delegate);
[email protected]bfd04a62009-02-01 18:16:56771 NotificationService::current()->Notify(
[email protected]f4f50ef2011-01-21 19:01:19772 NotificationType::TAB_CONTENTS_INFOBAR_ADDED, Source<TabContents>(this),
[email protected]bfd04a62009-02-01 18:16:56773 Details<InfoBarDelegate>(delegate));
[email protected]616ed5a2008-11-21 22:27:24774
775 // Add ourselves as an observer for navigations the first time a delegate is
776 // added. We use this notification to expire InfoBars that need to expire on
777 // page transitions.
778 if (infobar_delegates_.size() == 1) {
[email protected]bfd04a62009-02-01 18:16:56779 registrar_.Add(this, NotificationType::NAV_ENTRY_COMMITTED,
[email protected]ce3fa3c2009-04-20 19:55:57780 Source<NavigationController>(&controller_));
[email protected]616ed5a2008-11-21 22:27:24781 }
782}
783
784void TabContents::RemoveInfoBar(InfoBarDelegate* delegate) {
[email protected]f7019302010-03-26 19:58:32785 if (delegate_ && !delegate_->infobars_enabled()) {
786 return;
787 }
788
[email protected]616ed5a2008-11-21 22:27:24789 std::vector<InfoBarDelegate*>::iterator it =
790 find(infobar_delegates_.begin(), infobar_delegates_.end(), delegate);
791 if (it != infobar_delegates_.end()) {
792 InfoBarDelegate* delegate = *it;
[email protected]bfd04a62009-02-01 18:16:56793 NotificationService::current()->Notify(
794 NotificationType::TAB_CONTENTS_INFOBAR_REMOVED,
795 Source<TabContents>(this),
796 Details<InfoBarDelegate>(delegate));
[email protected]616ed5a2008-11-21 22:27:24797
[email protected]f4f50ef2011-01-21 19:01:19798 infobar_delegates_.erase(it);
799 // Remove ourselves as an observer if we are tracking no more InfoBars.
800 if (infobar_delegates_.empty()) {
801 registrar_.Remove(this, NotificationType::NAV_ENTRY_COMMITTED,
802 Source<NavigationController>(&controller_));
[email protected]6a02963e2009-01-06 16:58:03803 }
[email protected]616ed5a2008-11-21 22:27:24804 }
805}
[email protected]3edd9522009-03-04 22:19:41806
[email protected]f81821c2009-08-07 01:29:47807void TabContents::ReplaceInfoBar(InfoBarDelegate* old_delegate,
808 InfoBarDelegate* new_delegate) {
[email protected]f7019302010-03-26 19:58:32809 if (delegate_ && !delegate_->infobars_enabled()) {
810 new_delegate->InfoBarClosed();
811 return;
812 }
813
[email protected]f81821c2009-08-07 01:29:47814 std::vector<InfoBarDelegate*>::iterator it =
815 find(infobar_delegates_.begin(), infobar_delegates_.end(), old_delegate);
816 DCHECK(it != infobar_delegates_.end());
817
818 // Notify the container about the change of plans.
819 scoped_ptr<std::pair<InfoBarDelegate*, InfoBarDelegate*> > details(
820 new std::pair<InfoBarDelegate*, InfoBarDelegate*>(
821 old_delegate, new_delegate));
822 NotificationService::current()->Notify(
823 NotificationType::TAB_CONTENTS_INFOBAR_REPLACED,
824 Source<TabContents>(this),
825 Details<std::pair<InfoBarDelegate*, InfoBarDelegate*> >(details.get()));
826
[email protected]f4f50ef2011-01-21 19:01:19827 // Remove the old one.
828 infobar_delegates_.erase(it);
[email protected]f81821c2009-08-07 01:29:47829
830 // Add the new one.
[email protected]cdadf6f2010-08-20 16:36:08831 DCHECK(find(infobar_delegates_.begin(),
832 infobar_delegates_.end(), new_delegate) ==
833 infobar_delegates_.end());
[email protected]f81821c2009-08-07 01:29:47834 infobar_delegates_.push_back(new_delegate);
835}
836
[email protected]20ede072009-10-06 00:28:35837bool TabContents::ShouldShowBookmarkBar() {
[email protected]aa9196f2009-10-22 16:20:27838 if (showing_interstitial_page())
839 return false;
840
[email protected]c8f74942010-09-03 10:04:14841 // Do not show bookmarks bar if bookmarks aren't enabled.
842 if (!browser_defaults::bookmarks_enabled)
843 return false;
844
[email protected]b3be10fa2011-04-11 15:43:15845 if (!profile()->GetPrefs()->GetBoolean(prefs::kEnableBookmarkBar))
846 return false;
847
[email protected]d0980792011-02-13 19:41:40848 // See GetWebUIForCurrentState() comment for more info. This case is very
[email protected]96d185d2009-04-24 03:28:54849 // similar, but for non-first loads, we want to use the committed entry. This
850 // is so the bookmarks bar disappears at the same time the page does.
851 if (controller_.GetLastCommittedEntry()) {
[email protected]e0112912011-02-02 22:54:35852 // Not the first load, always use the committed Web UI.
[email protected]7ade2732011-02-10 00:13:58853 return (render_manager_.web_ui() == NULL) ?
854 false : render_manager_.web_ui()->force_bookmark_bar_visible();
[email protected]96d185d2009-04-24 03:28:54855 }
856
857 // When it's the first load, we know either the pending one or the committed
[email protected]d0980792011-02-13 19:41:40858 // one will have the Web UI in it (see GetWebUIForCurrentState), and only one
[email protected]96d185d2009-04-24 03:28:54859 // of them will be valid, so we can just check both.
[email protected]7ade2732011-02-10 00:13:58860 if (render_manager_.pending_web_ui())
861 return render_manager_.pending_web_ui()->force_bookmark_bar_visible();
862 return (render_manager_.web_ui() == NULL) ?
863 false : render_manager_.web_ui()->force_bookmark_bar_visible();
[email protected]96d185d2009-04-24 03:28:54864}
865
[email protected]fc6fb7fb2009-11-07 02:35:04866bool TabContents::CanDownload(int request_id) {
867 TabContentsDelegate* d = delegate();
868 if (d)
869 return d->CanDownload(request_id);
870 return true;
871}
872
[email protected]d5f942ba2008-09-26 19:30:34873void TabContents::OnStartDownload(DownloadItem* download) {
874 DCHECK(download);
[email protected]d5f942ba2008-09-26 19:30:34875
876 // Download in a constrained popup is shown in the tab that opened it.
[email protected]b6c874582009-05-08 19:38:31877 TabContents* tab_contents = delegate()->GetConstrainingContents(this);
[email protected]d5f942ba2008-09-26 19:30:34878
[email protected]5b928b62010-08-03 11:46:51879 if (tab_contents && tab_contents->delegate())
880 tab_contents->delegate()->OnStartDownload(download, this);
[email protected]3edd9522009-03-04 22:19:41881}
[email protected]3edd9522009-03-04 22:19:41882
[email protected]d5f942ba2008-09-26 19:30:34883void TabContents::WillClose(ConstrainedWindow* window) {
[email protected]d6219912010-11-17 19:47:37884 ConstrainedWindowList::iterator i(
885 std::find(child_windows_.begin(), child_windows_.end(), window));
886 bool removed_topmost_window = i == child_windows_.begin();
887 if (i != child_windows_.end())
888 child_windows_.erase(i);
889 if (child_windows_.empty()) {
[email protected]fa1cf0b82010-01-15 21:49:44890 BlockTabContent(false);
[email protected]d6219912010-11-17 19:47:37891 } else {
892 if (removed_topmost_window)
893 child_windows_[0]->ShowConstrainedWindow();
894 BlockTabContent(true);
[email protected]fa1cf0b82010-01-15 21:49:44895 }
[email protected]66ba4932009-06-04 19:22:13896}
[email protected]d5f942ba2008-09-26 19:30:34897
[email protected]89d6e6e2009-12-03 23:06:51898void TabContents::EmailPageLocation() {
899 std::string title = EscapeQueryParamValue(UTF16ToUTF8(GetTitle()), false);
900 std::string page_url = EscapeQueryParamValue(GetURL().spec(), false);
901 std::string mailto = std::string("mailto:?subject=Fwd:%20") +
902 title + "&body=%0A%0A" + page_url;
903 platform_util::OpenExternal(GURL(mailto));
904}
905
[email protected]420ae012009-04-24 05:16:32906bool TabContents::IsActiveEntry(int32 page_id) {
907 NavigationEntry* active_entry = controller_.GetActiveEntry();
908 return (active_entry != NULL &&
909 active_entry->site_instance() == GetSiteInstance() &&
910 active_entry->page_id() == page_id);
911}
912
[email protected]b2fe07d12010-02-09 14:38:08913void TabContents::SetOverrideEncoding(const std::string& encoding) {
[email protected]8cb5d5b2010-02-09 11:36:16914 set_encoding(encoding);
915 render_view_host()->SetPageEncoding(encoding);
916}
917
[email protected]b2fe07d12010-02-09 14:38:08918void TabContents::ResetOverrideEncoding() {
[email protected]8cb5d5b2010-02-09 11:36:16919 reset_encoding();
920 render_view_host()->ResetPageEncodingToDefault();
921}
922
923void TabContents::WindowMoveOrResizeStarted() {
924 render_view_host()->WindowMoveOrResizeStarted();
925}
926
[email protected]7ab1e7d62009-10-14 23:32:01927void TabContents::OnCloseStarted() {
928 if (tab_close_start_time_.is_null())
929 tab_close_start_time_ = base::TimeTicks::Now();
930}
931
[email protected]46624bf2010-06-09 16:04:19932bool TabContents::ShouldAcceptDragAndDrop() const {
933#if defined(OS_CHROMEOS)
934 // ChromeOS panels (pop-ups) do not take drag-n-drop.
935 // See http://crosbug.com/2413
[email protected]b83af492010-10-09 03:21:20936 if (delegate() && delegate()->IsPopup(this))
937 return false;
938 return true;
[email protected]46624bf2010-06-09 16:04:19939#else
940 return true;
941#endif
942}
943
[email protected]7813bd72011-02-05 02:19:34944void TabContents::SystemDragEnded() {
945 if (render_view_host())
946 render_view_host()->DragSourceSystemDragEnded();
947 if (delegate())
948 delegate()->DragEnded();
949}
950
[email protected]ec0b6c42010-08-26 03:16:58951void TabContents::UpdateHistoryForNavigation(
952 scoped_refptr<history::HistoryAddPageArgs> add_page_args) {
953 if (profile()->IsOffTheRecord())
954 return;
955
956 // Add to history service.
957 HistoryService* hs = profile()->GetHistoryService(Profile::IMPLICIT_ACCESS);
958 if (hs)
959 hs->AddPage(*add_page_args);
960}
961
962void TabContents::UpdateHistoryPageTitle(const NavigationEntry& entry) {
963 if (profile()->IsOffTheRecord())
964 return;
965
966 HistoryService* hs = profile()->GetHistoryService(Profile::IMPLICIT_ACCESS);
[email protected]6b2f7a82011-04-25 19:30:51967 if (hs)
968 hs->SetPageTitle(entry.virtual_url(), entry.title());
[email protected]ec0b6c42010-08-26 03:16:58969}
970
[email protected]d0b8d092010-10-25 04:05:17971double TabContents::GetZoomLevel() const {
[email protected]b75b8292010-10-01 07:28:25972 HostZoomMap* zoom_map = profile()->GetHostZoomMap();
973 if (!zoom_map)
[email protected]d0b8d092010-10-25 04:05:17974 return 0;
[email protected]b75b8292010-10-01 07:28:25975
976 double zoom_level;
977 if (temporary_zoom_settings_) {
978 zoom_level = zoom_map->GetTemporaryZoomLevel(
979 GetRenderProcessHost()->id(), render_view_host()->routing_id());
980 } else {
981 zoom_level = zoom_map->GetZoomLevel(GetURL());
982 }
[email protected]d0b8d092010-10-25 04:05:17983 return zoom_level;
984}
[email protected]b75b8292010-10-01 07:28:25985
[email protected]d0b8d092010-10-25 04:05:17986int TabContents::GetZoomPercent(bool* enable_increment,
987 bool* enable_decrement) {
988 *enable_decrement = *enable_increment = false;
[email protected]b75b8292010-10-01 07:28:25989 int percent = static_cast<int>(
[email protected]d0b8d092010-10-25 04:05:17990 WebKit::WebView::zoomLevelToZoomFactor(GetZoomLevel()) * 100);
[email protected]b75b8292010-10-01 07:28:25991 *enable_decrement = percent > minimum_zoom_percent_;
992 *enable_increment = percent < maximum_zoom_percent_;
993 return percent;
994}
995
[email protected]3c733bde2010-12-21 19:56:31996void TabContents::ViewSource() {
[email protected]1788e772010-12-15 16:40:50997 if (!delegate_)
998 return;
999
1000 NavigationEntry* active_entry = controller().GetActiveEntry();
1001 if (!active_entry)
1002 return;
1003
1004 delegate_->ViewSourceForTab(this, active_entry->url());
[email protected]77d8d622010-12-15 10:30:121005}
1006
[email protected]932b7a12011-03-09 12:50:271007void TabContents::ViewFrameSource(const GURL& url,
1008 const std::string& content_state) {
1009 if (!delegate_)
1010 return;
1011
1012 delegate_->ViewSourceForFrame(this, url, content_state);
1013}
1014
[email protected]c40d6232011-03-25 00:16:211015void TabContents::SetContentRestrictions(int restrictions) {
1016 content_restrictions_ = restrictions;
1017 delegate()->ContentRestrictionsChanged(this);
1018}
1019
[email protected]724159a2010-12-30 01:11:181020void TabContents::OnDidStartProvisionalLoadForFrame(int64 frame_id,
1021 bool is_main_frame,
1022 const GURL& url) {
1023 bool is_error_page = (url.spec() == chrome::kUnreachableWebDataURL);
1024 GURL validated_url(url);
1025 render_view_host()->FilterURL(ChildProcessSecurityPolicy::GetInstance(),
1026 GetRenderProcessHost()->id(), &validated_url);
1027
[email protected]0d60f0192011-04-14 12:40:101028 // Notify observers about the start of the provisional load.
1029 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
1030 DidStartProvisionalLoadForFrame(frame_id, is_main_frame,
1031 validated_url, is_error_page));
1032
[email protected]724159a2010-12-30 01:11:181033 if (is_main_frame) {
[email protected]4850a7f2011-03-08 23:36:591034 // Notify observers about the provisional change in the main frame URL.
1035 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
[email protected]0d60f0192011-04-14 12:40:101036 ProvisionalChangeToMainFrameUrl(url));
[email protected]724159a2010-12-30 01:11:181037 }
1038}
1039
1040void TabContents::OnDidRedirectProvisionalLoad(int32 page_id,
1041 const GURL& source_url,
1042 const GURL& target_url) {
[email protected]18ba2b12011-04-06 16:35:491043 // TODO(creis): Remove this method and have the pre-rendering code listen to
1044 // the ResourceDispatcherHost's RESOURCE_RECEIVED_REDIRECT notification
1045 // instead. See http://crbug.com/78512.
[email protected]724159a2010-12-30 01:11:181046 NavigationEntry* entry;
1047 if (page_id == -1)
1048 entry = controller_.pending_entry();
1049 else
1050 entry = controller_.GetEntryWithPageID(GetSiteInstance(), page_id);
1051 if (!entry || entry->url() != source_url)
1052 return;
[email protected]e7d50892011-01-19 21:47:381053
[email protected]4850a7f2011-03-08 23:36:591054 // Notify observers about the provisional change in the main frame URL.
1055 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
[email protected]0d60f0192011-04-14 12:40:101056 ProvisionalChangeToMainFrameUrl(target_url));
[email protected]724159a2010-12-30 01:11:181057}
1058
1059void TabContents::OnDidFailProvisionalLoadWithError(
1060 int64 frame_id,
1061 bool is_main_frame,
1062 int error_code,
1063 const GURL& url,
1064 bool showing_repost_interstitial) {
1065 VLOG(1) << "Failed Provisional Load: " << url.possibly_invalid_spec()
1066 << ", error_code: " << error_code
1067 << " is_main_frame: " << is_main_frame
1068 << " showing_repost_interstitial: " << showing_repost_interstitial
1069 << " frame_id: " << frame_id;
1070 GURL validated_url(url);
1071 render_view_host()->FilterURL(ChildProcessSecurityPolicy::GetInstance(),
1072 GetRenderProcessHost()->id(), &validated_url);
1073
1074 if (net::ERR_ABORTED == error_code) {
1075 // EVIL HACK ALERT! Ignore failed loads when we're showing interstitials.
1076 // This means that the interstitial won't be torn down properly, which is
1077 // bad. But if we have an interstitial, go back to another tab type, and
1078 // then load the same interstitial again, we could end up getting the first
1079 // interstitial's "failed" message (as a result of the cancel) when we're on
1080 // the second one.
1081 //
1082 // We can't tell this apart, so we think we're tearing down the current page
1083 // which will cause a crash later one. There is also some code in
1084 // RenderViewHostManager::RendererAbortedProvisionalLoad that is commented
1085 // out because of this problem.
1086 //
1087 // http://code.google.com/p/chromium/issues/detail?id=2855
1088 // Because this will not tear down the interstitial properly, if "back" is
1089 // back to another tab type, the interstitial will still be somewhat alive
1090 // in the previous tab type. If you navigate somewhere that activates the
1091 // tab with the interstitial again, you'll see a flash before the new load
1092 // commits of the interstitial page.
1093 if (showing_interstitial_page()) {
1094 LOG(WARNING) << "Discarding message during interstitial.";
1095 return;
1096 }
1097
1098 // This will discard our pending entry if we cancelled the load (e.g., if we
1099 // decided to download the file instead of load it). Only discard the
1100 // pending entry if the URLs match, otherwise the user initiated a navigate
1101 // before the page loaded so that the discard would discard the wrong entry.
1102 NavigationEntry* pending_entry = controller_.pending_entry();
1103 if (pending_entry && pending_entry->url() == validated_url) {
[email protected]c95fa8b2011-04-28 20:26:161104 DidCancelLoading();
[email protected]724159a2010-12-30 01:11:181105 }
1106
1107 render_manager_.RendererAbortedProvisionalLoad(render_view_host());
1108 }
1109
1110 // Send out a notification that we failed a provisional load with an error.
1111 ProvisionalLoadDetails details(
1112 is_main_frame, controller_.IsURLInPageNavigation(validated_url),
[email protected]b9e8ea62011-03-04 06:29:091113 validated_url, std::string(), false, frame_id);
[email protected]724159a2010-12-30 01:11:181114 details.set_error_code(error_code);
1115
1116 NotificationService::current()->Notify(
1117 NotificationType::FAIL_PROVISIONAL_LOAD_WITH_ERROR,
1118 Source<NavigationController>(&controller_),
1119 Details<ProvisionalLoadDetails>(&details));
[email protected]0d60f0192011-04-14 12:40:101120
1121 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
1122 DidFailProvisionalLoad(frame_id, is_main_frame,
1123 validated_url, error_code));
[email protected]724159a2010-12-30 01:11:181124}
1125
1126void TabContents::OnDidLoadResourceFromMemoryCache(
1127 const GURL& url,
[email protected]724159a2010-12-30 01:11:181128 const std::string& security_info) {
[email protected]28620862011-03-22 23:07:191129 base::StatsCounter cache("WebKit.CacheHit");
[email protected]724159a2010-12-30 01:11:181130 cache.Increment();
1131
1132 // Send out a notification that we loaded a resource from our memory cache.
1133 int cert_id = 0, cert_status = 0, security_bits = -1, connection_status = 0;
1134 SSLManager::DeserializeSecurityInfo(security_info,
1135 &cert_id, &cert_status,
1136 &security_bits,
1137 &connection_status);
[email protected]7a8c55e2011-02-15 08:21:161138 LoadFromMemoryCacheDetails details(url, GetRenderProcessHost()->id(),
1139 cert_id, cert_status);
[email protected]724159a2010-12-30 01:11:181140
1141 NotificationService::current()->Notify(
1142 NotificationType::LOAD_FROM_MEMORY_CACHE,
1143 Source<NavigationController>(&controller_),
1144 Details<LoadFromMemoryCacheDetails>(&details));
1145}
1146
1147void TabContents::OnDidDisplayInsecureContent() {
[email protected]221a5d92011-04-28 18:23:161148 UserMetrics::RecordAction(UserMetricsAction("SSL.DisplayedInsecureContent"));
[email protected]724159a2010-12-30 01:11:181149 displayed_insecure_content_ = true;
1150 SSLManager::NotifySSLInternalStateChanged();
1151}
1152
1153void TabContents::OnDidRunInsecureContent(
[email protected]92771112011-01-20 00:13:021154 const std::string& security_origin, const GURL& target_url) {
[email protected]9941cf52011-02-08 16:17:231155 LOG(INFO) << security_origin << " ran insecure content from "
1156 << target_url.possibly_invalid_spec();
[email protected]221a5d92011-04-28 18:23:161157 UserMetrics::RecordAction(UserMetricsAction("SSL.RanInsecureContent"));
[email protected]724159a2010-12-30 01:11:181158 controller_.ssl_manager()->DidRunInsecureContent(security_origin);
1159}
1160
1161void TabContents::OnDocumentLoadedInFrame(int64 frame_id) {
1162 controller_.DocumentLoadedInFrame();
[email protected]0d60f0192011-04-14 12:40:101163 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
1164 DocumentLoadedInFrame(frame_id));
[email protected]724159a2010-12-30 01:11:181165}
1166
1167void TabContents::OnDidFinishLoad(int64 frame_id) {
[email protected]0d60f0192011-04-14 12:40:101168 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
1169 DidFinishLoad(frame_id));
[email protected]724159a2010-12-30 01:11:181170}
1171
[email protected]c8f73ab2011-01-22 00:05:171172void TabContents::OnUpdateContentRestrictions(int restrictions) {
[email protected]c40d6232011-03-25 00:16:211173 SetContentRestrictions(restrictions);
[email protected]c8f73ab2011-01-22 00:05:171174}
1175
[email protected]96d185d2009-04-24 03:28:541176// Notifies the RenderWidgetHost instance about the fact that the page is
1177// loading, or done loading and calls the base implementation.
1178void TabContents::SetIsLoading(bool is_loading,
1179 LoadNotificationDetails* details) {
1180 if (is_loading == is_loading_)
1181 return;
1182
1183 if (!is_loading) {
1184 load_state_ = net::LOAD_STATE_IDLE;
1185 load_state_host_.clear();
[email protected]094e5b22009-09-25 04:23:561186 upload_size_ = 0;
1187 upload_position_ = 0;
[email protected]96d185d2009-04-24 03:28:541188 }
1189
1190 render_manager_.SetIsLoading(is_loading);
1191
1192 is_loading_ = is_loading;
1193 waiting_for_response_ = is_loading;
1194
[email protected]6ebdc9b2010-09-27 16:55:571195 if (delegate_)
1196 delegate_->LoadingStateChanged(this);
[email protected]329581b2009-04-28 06:52:351197 NotifyNavigationStateChanged(INVALIDATE_LOAD);
[email protected]96d185d2009-04-24 03:28:541198
1199 NotificationType type = is_loading ? NotificationType::LOAD_START :
1200 NotificationType::LOAD_STOP;
[email protected]ce5c4502009-05-06 16:46:111201 NotificationDetails det = NotificationService::NoDetails();
[email protected]96d185d2009-04-24 03:28:541202 if (details)
1203 det = Details<LoadNotificationDetails>(details);
1204 NotificationService::current()->Notify(type,
1205 Source<NavigationController>(&controller_),
1206 det);
1207}
1208
[email protected]616ed5a2008-11-21 22:27:241209void TabContents::ExpireInfoBars(
1210 const NavigationController::LoadCommittedDetails& details) {
1211 // Only hide InfoBars when the user has done something that makes the main
1212 // frame load. We don't want various automatic or subframe navigations making
1213 // it disappear.
1214 if (!details.is_user_initiated_main_frame_load())
1215 return;
1216
[email protected]ec8f51162011-02-08 04:20:401217 // NOTE: It is not safe to change the following code to count upwards or use
1218 // iterators, as the RemoveInfoBar() call synchronously modifies our delegate
1219 // list.
1220 for (size_t i = infobar_count(); i > 0; --i) {
1221 InfoBarDelegate* delegate = GetInfoBarDelegateAt(i - 1);
[email protected]f86a07022008-11-25 01:06:051222 if (delegate->ShouldExpire(details))
[email protected]616ed5a2008-11-21 22:27:241223 RemoveInfoBar(delegate);
[email protected]616ed5a2008-11-21 22:27:241224 }
1225}
[email protected]fdd61c62009-04-22 19:22:571226
[email protected]d0980792011-02-13 19:41:401227WebUI* TabContents::GetWebUIForCurrentState() {
[email protected]7ade2732011-02-10 00:13:581228 // When there is a pending navigation entry, we want to use the pending WebUI
[email protected]96d185d2009-04-24 03:28:541229 // that goes along with it to control the basic flags. For example, we want to
1230 // show the pending URL in the URL bar, so we want the display_url flag to
1231 // be from the pending entry.
1232 //
1233 // The confusion comes because there are multiple possibilities for the
1234 // initial load in a tab as a side effect of the way the RenderViewHostManager
1235 // works.
1236 //
[email protected]e0112912011-02-02 22:54:351237 // - For the very first tab the load looks "normal". The new tab Web UI is
[email protected]96d185d2009-04-24 03:28:541238 // the pending one, and we want it to apply here.
1239 //
1240 // - For subsequent new tabs, they'll get a new SiteInstance which will then
1241 // get switched to the one previously associated with the new tab pages.
[email protected]7ade2732011-02-10 00:13:581242 // This switching will cause the manager to commit the RVH/WebUI. So we'll
[email protected]e0112912011-02-02 22:54:351243 // have a committed Web UI in this case.
[email protected]96d185d2009-04-24 03:28:541244 //
1245 // This condition handles all of these cases:
1246 //
1247 // - First load in first tab: no committed nav entry + pending nav entry +
1248 // pending dom ui:
[email protected]e0112912011-02-02 22:54:351249 // -> Use pending Web UI if any.
[email protected]96d185d2009-04-24 03:28:541250 //
1251 // - First load in second tab: no committed nav entry + pending nav entry +
[email protected]e0112912011-02-02 22:54:351252 // no pending Web UI:
1253 // -> Use the committed Web UI if any.
[email protected]96d185d2009-04-24 03:28:541254 //
1255 // - Second navigation in any tab: committed nav entry + pending nav entry:
[email protected]e0112912011-02-02 22:54:351256 // -> Use pending Web UI if any.
[email protected]96d185d2009-04-24 03:28:541257 //
1258 // - Normal state with no load: committed nav entry + no pending nav entry:
[email protected]e0112912011-02-02 22:54:351259 // -> Use committed Web UI.
[email protected]96d185d2009-04-24 03:28:541260 if (controller_.pending_entry() &&
1261 (controller_.GetLastCommittedEntry() ||
[email protected]7ade2732011-02-10 00:13:581262 render_manager_.pending_web_ui()))
1263 return render_manager_.pending_web_ui();
1264 return render_manager_.web_ui();
[email protected]96d185d2009-04-24 03:28:541265}
[email protected]420ae012009-04-24 05:16:321266
[email protected]1fd1a502011-03-30 16:55:561267WebUI::TypeID TabContents::GetWebUITypeForCurrentState() {
1268 return content::WebUIFactory::Get()->GetWebUIType(profile(), GetURL());
1269}
1270
[email protected]420ae012009-04-24 05:16:321271void TabContents::DidNavigateMainFramePostCommit(
1272 const NavigationController::LoadCommittedDetails& details,
1273 const ViewHostMsg_FrameNavigate_Params& params) {
[email protected]1fd1a502011-03-30 16:55:561274 if (opener_web_ui_type_ != WebUI::kNoWebUI) {
[email protected]80a8fad2011-01-29 04:02:381275 // If this is a window.open navigation, use the same WebUI as the renderer
[email protected]c2e74fe82009-09-03 17:57:441276 // that opened the window, as long as both renderers have the same
1277 // privileges.
[email protected]1fd1a502011-03-30 16:55:561278 if (delegate_ && opener_web_ui_type_ == GetWebUITypeForCurrentState()) {
1279 WebUI* web_ui = content::GetContentClient()->browser()->
1280 GetWebUIFactory()->CreateWebUIForURL(this, GetURL());
[email protected]7ade2732011-02-10 00:13:581281 // web_ui might be NULL if the URL refers to a non-existent extension.
1282 if (web_ui) {
[email protected]d0980792011-02-13 19:41:401283 render_manager_.SetWebUIPostCommit(web_ui);
[email protected]7ade2732011-02-10 00:13:581284 web_ui->RenderViewCreated(render_view_host());
[email protected]c2e74fe82009-09-03 17:57:441285 }
1286 }
[email protected]1fd1a502011-03-30 16:55:561287 opener_web_ui_type_ = WebUI::kNoWebUI;
[email protected]c2e74fe82009-09-03 17:57:441288 }
1289
[email protected]420ae012009-04-24 05:16:321290 if (details.is_user_initiated_main_frame_load()) {
1291 // Clear the status bubble. This is a workaround for a bug where WebKit
1292 // doesn't let us know that the cursor left an element during a
1293 // transition (this is also why the mouse cursor remains as a hand after
1294 // clicking on a link); see bugs 1184641 and 980803. We don't want to
1295 // clear the bubble when a user navigates to a named anchor in the same
1296 // page.
1297 UpdateTargetURL(details.entry->page_id(), GURL());
[email protected]420ae012009-04-24 05:16:321298 }
1299
[email protected]420ae012009-04-24 05:16:321300 // Allow the new page to set the title again.
1301 received_page_title_ = false;
1302
[email protected]a6e82fc2010-02-24 22:28:141303 if (!details.is_in_page) {
[email protected]f17a0ee2010-05-17 17:38:471304 // Once the main frame is navigated, we're no longer considered to have
1305 // displayed insecure content.
1306 displayed_insecure_content_ = false;
[email protected]aece2c7f2009-08-27 20:43:171307 }
[email protected]ce5c4502009-05-06 16:46:111308
[email protected]8cc8d492010-02-02 10:40:491309 // Close constrained windows if necessary.
1310 if (!net::RegistryControlledDomainService::SameDomainOrHost(
1311 details.previous_url, details.entry->url()))
1312 CloseConstrainedWindows();
1313
[email protected]3c9e1872010-11-18 16:17:491314 // Notify observers about navigation.
[email protected]585b30362011-01-28 02:30:171315 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
[email protected]3c9e1872010-11-18 16:17:491316 DidNavigateMainFramePostCommit(details, params));
[email protected]420ae012009-04-24 05:16:321317}
1318
1319void TabContents::DidNavigateAnyFramePostCommit(
1320 RenderViewHost* render_view_host,
1321 const NavigationController::LoadCommittedDetails& details,
1322 const ViewHostMsg_FrameNavigate_Params& params) {
1323 // If we navigate, start showing messages again. This does nothing to prevent
1324 // a malicious script from spamming messages, since the script could just
1325 // reload the page to stop blocking.
1326 suppress_javascript_messages_ = false;
1327
[email protected]3c9e1872010-11-18 16:17:491328 // Notify observers about navigation.
[email protected]585b30362011-01-28 02:30:171329 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
[email protected]3c9e1872010-11-18 16:17:491330 DidNavigateAnyFramePostCommit(details, params));
[email protected]420ae012009-04-24 05:16:321331}
1332
[email protected]8cc8d492010-02-02 10:40:491333void TabContents::CloseConstrainedWindows() {
1334 // Clear out any constrained windows since we are leaving this page entirely.
[email protected]aed59602011-02-28 22:57:331335 // To ensure that we iterate over every element in child_windows_ we
1336 // need to use a copy of child_windows_. Otherwise if
1337 // window->CloseConstrainedWindow() modifies child_windows_ we could end up
1338 // skipping some elements.
1339 ConstrainedWindowList child_windows_copy(child_windows_);
1340 for (ConstrainedWindowList::iterator it = child_windows_copy.begin();
1341 it != child_windows_copy.end(); ++it) {
1342 ConstrainedWindow* window = *it;
[email protected]fa1cf0b82010-01-15 21:49:441343 if (window) {
[email protected]420ae012009-04-24 05:16:321344 window->CloseConstrainedWindow();
[email protected]fa1cf0b82010-01-15 21:49:441345 BlockTabContent(false);
1346 }
[email protected]420ae012009-04-24 05:16:321347 }
1348}
1349
[email protected]420ae012009-04-24 05:16:321350void TabContents::UpdateAlternateErrorPageURL() {
1351 GURL url = GetAlternateErrorPageURL();
1352 render_view_host()->SetAlternateErrorPageURL(url);
1353}
1354
1355void TabContents::UpdateWebPreferences() {
1356 render_view_host()->UpdateWebPreferences(GetWebkitPrefs());
1357}
1358
[email protected]d0b8d092010-10-25 04:05:171359void TabContents::UpdateZoomLevel() {
1360 render_view_host()->SetZoomLevel(GetZoomLevel());
1361}
1362
[email protected]420ae012009-04-24 05:16:321363void TabContents::UpdateMaxPageIDIfNecessary(SiteInstance* site_instance,
1364 RenderViewHost* rvh) {
1365 // If we are creating a RVH for a restored controller, then we might
1366 // have more page IDs than the SiteInstance's current max page ID. We must
1367 // make sure that the max page ID is larger than any restored page ID.
1368 // Note that it is ok for conflicting page IDs to exist in another tab
1369 // (i.e., NavigationController), but if any page ID is larger than the max,
1370 // the back/forward list will get confused.
1371 int max_restored_page_id = controller_.max_restored_page_id();
1372 if (max_restored_page_id > 0) {
1373 int curr_max_page_id = site_instance->max_page_id();
1374 if (max_restored_page_id > curr_max_page_id) {
1375 // Need to update the site instance immediately.
1376 site_instance->UpdateMaxPageID(max_restored_page_id);
1377
1378 // Also tell the renderer to update its internal representation. We
1379 // need to reserve enough IDs to make all restored page IDs less than
1380 // the max.
1381 if (curr_max_page_id < 0)
1382 curr_max_page_id = 0;
1383 rvh->ReservePageIDRange(max_restored_page_id - curr_max_page_id);
1384 }
1385 }
1386}
1387
[email protected]ec0b6c42010-08-26 03:16:581388scoped_refptr<history::HistoryAddPageArgs>
1389TabContents::CreateHistoryAddPageArgs(
[email protected]ebe89e062009-08-13 23:16:541390 const GURL& virtual_url,
[email protected]befd8d822009-07-01 04:51:471391 const NavigationController::LoadCommittedDetails& details,
[email protected]420ae012009-04-24 05:16:321392 const ViewHostMsg_FrameNavigate_Params& params) {
[email protected]ec0b6c42010-08-26 03:16:581393 scoped_refptr<history::HistoryAddPageArgs> add_page_args(
1394 new history::HistoryAddPageArgs(
1395 params.url, base::Time::Now(), this, params.page_id, params.referrer,
1396 params.redirects, params.transition, history::SOURCE_BROWSED,
1397 details.did_replace_entry));
1398 if (PageTransition::IsMainFrame(params.transition) &&
1399 virtual_url != params.url) {
1400 // Hack on the "virtual" URL so that it will appear in history. For some
1401 // types of URLs, we will display a magic URL that is different from where
1402 // the page is actually navigated. We want the user to see in history what
1403 // they saw in the URL bar, so we add the virtual URL as a redirect. This
1404 // only applies to the main frame, as the virtual URL doesn't apply to
1405 // sub-frames.
1406 add_page_args->url = virtual_url;
1407 if (!add_page_args->redirects.empty())
1408 add_page_args->redirects.back() = virtual_url;
[email protected]420ae012009-04-24 05:16:321409 }
[email protected]ec0b6c42010-08-26 03:16:581410 return add_page_args;
[email protected]420ae012009-04-24 05:16:321411}
1412
[email protected]6b2f7a82011-04-25 19:30:511413bool TabContents::UpdateTitleForEntry(NavigationEntry* entry,
1414 const std::wstring& title) {
[email protected]420ae012009-04-24 05:16:321415 // For file URLs without a title, use the pathname instead. In the case of a
1416 // synthesized title, we don't want the update to count toward the "one set
1417 // per page of the title to history."
[email protected]6b2f7a82011-04-25 19:30:511418 string16 final_title;
[email protected]420ae012009-04-24 05:16:321419 bool explicit_set;
[email protected]6b2f7a82011-04-25 19:30:511420 if (entry->url().SchemeIsFile() && title.empty()) {
1421 final_title = UTF8ToUTF16(entry->url().ExtractFileName());
[email protected]420ae012009-04-24 05:16:321422 explicit_set = false; // Don't count synthetic titles toward the set limit.
1423 } else {
[email protected]6b2f7a82011-04-25 19:30:511424 TrimWhitespace(WideToUTF16Hack(title), TRIM_ALL, &final_title);
[email protected]420ae012009-04-24 05:16:321425 explicit_set = true;
1426 }
1427
[email protected]e53668962010-06-23 15:35:251428 if (final_title == entry->title())
[email protected]420ae012009-04-24 05:16:321429 return false; // Nothing changed, don't bother.
1430
[email protected]e53668962010-06-23 15:35:251431 entry->set_title(final_title);
[email protected]420ae012009-04-24 05:16:321432
[email protected]ec0b6c42010-08-26 03:16:581433 if (!received_page_title_) {
1434 UpdateHistoryPageTitle(*entry);
[email protected]420ae012009-04-24 05:16:321435 received_page_title_ = explicit_set;
1436 }
1437
1438 // Lastly, set the title for the view.
[email protected]6b2f7a82011-04-25 19:30:511439 view_->SetPageTitle(UTF16ToWideHack(final_title));
[email protected]420ae012009-04-24 05:16:321440
[email protected]cbc0e1b2010-04-12 18:33:041441 NotificationService::current()->Notify(
1442 NotificationType::TAB_CONTENTS_TITLE_UPDATED,
1443 Source<TabContents>(this),
1444 NotificationService::NoDetails());
1445
[email protected]420ae012009-04-24 05:16:321446 return true;
1447}
1448
1449void TabContents::NotifySwapped() {
1450 // After sending out a swap notification, we need to send a disconnect
1451 // notification so that clients that pick up a pointer to |this| can NULL the
1452 // pointer. See Bug 1230284.
1453 notify_disconnection_ = true;
1454 NotificationService::current()->Notify(
[email protected]57c6a652009-05-04 07:58:341455 NotificationType::TAB_CONTENTS_SWAPPED,
[email protected]d82ed61e2009-06-16 02:46:221456 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:321457 NotificationService::NoDetails());
1458}
1459
1460void TabContents::NotifyConnected() {
1461 notify_disconnection_ = true;
1462 NotificationService::current()->Notify(
[email protected]57c6a652009-05-04 07:58:341463 NotificationType::TAB_CONTENTS_CONNECTED,
[email protected]d82ed61e2009-06-16 02:46:221464 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:321465 NotificationService::NoDetails());
1466}
1467
1468void TabContents::NotifyDisconnected() {
1469 if (!notify_disconnection_)
1470 return;
1471
1472 notify_disconnection_ = false;
1473 NotificationService::current()->Notify(
[email protected]57c6a652009-05-04 07:58:341474 NotificationType::TAB_CONTENTS_DISCONNECTED,
[email protected]d82ed61e2009-06-16 02:46:221475 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:321476 NotificationService::NoDetails());
1477}
1478
[email protected]7d472472011-01-22 01:30:251479void TabContents::OnGoToEntryAtOffset(int offset) {
[email protected]876bc832010-09-07 16:29:541480 if (!delegate_ || delegate_->OnGoToEntryOffset(offset)) {
1481 NavigationEntry* entry = controller_.GetEntryAtOffset(offset);
1482 if (!entry)
1483 return;
1484 // Note that we don't call NavigationController::GotToOffset() as we don't
1485 // want to create a pending navigation entry (it might end up lingering
1486 // http://crbug.com/51680).
[email protected]784688a62010-09-13 07:06:521487 entry->set_transition_type(entry->transition_type() |
1488 PageTransition::FORWARD_BACK);
[email protected]876bc832010-09-07 16:29:541489 NavigateToEntry(*entry, NavigationController::NO_RELOAD);
1490 }
[email protected]bf5c2ff392009-07-08 16:24:331491}
1492
[email protected]8d3347f2009-07-09 22:00:211493RenderViewHostDelegate::View* TabContents::GetViewDelegate() {
[email protected]420ae012009-04-24 05:16:321494 return view_.get();
1495}
1496
[email protected]8d3347f2009-07-09 22:00:211497RenderViewHostDelegate::RendererManagement*
1498TabContents::GetRendererManagementDelegate() {
1499 return &render_manager_;
1500}
1501
[email protected]46ba9082010-06-22 10:00:581502RenderViewHostDelegate::ContentSettings*
1503TabContents::GetContentSettingsDelegate() {
[email protected]9fb83e82010-07-02 18:24:551504 return content_settings_delegate_.get();
[email protected]46ba9082010-06-22 10:00:581505}
1506
[email protected]1f18184a2010-07-21 19:34:491507RenderViewHostDelegate::SSL* TabContents::GetSSLDelegate() {
1508 return GetSSLHelper();
1509}
1510
[email protected]ced90ae12010-02-20 02:06:161511RenderViewHostDelegate::BookmarkDrag* TabContents::GetBookmarkDragDelegate() {
1512 return bookmark_drag_;
1513}
1514
1515void TabContents::SetBookmarkDragDelegate(
1516 RenderViewHostDelegate::BookmarkDrag* bookmark_drag) {
1517 bookmark_drag_ = bookmark_drag;
1518}
1519
[email protected]9fb83e82010-07-02 18:24:551520TabSpecificContentSettings* TabContents::GetTabSpecificContentSettings() const {
1521 return content_settings_delegate_.get();
1522}
1523
[email protected]93623c5d2009-12-10 21:40:321524RendererPreferences TabContents::GetRendererPrefs(Profile* profile) const {
[email protected]840b1512009-07-21 16:53:461525 return renderer_preferences_;
[email protected]80d96fa2009-06-10 22:34:511526}
1527
[email protected]57c6a652009-05-04 07:58:341528TabContents* TabContents::GetAsTabContents() {
1529 return this;
1530}
1531
[email protected]7b291f92009-08-14 05:43:531532ViewType::Type TabContents::GetRenderViewType() const {
1533 return ViewType::TAB_CONTENTS;
1534}
1535
1536int TabContents::GetBrowserWindowID() const {
1537 return controller().window_id().id();
1538}
1539
[email protected]420ae012009-04-24 05:16:321540void TabContents::RenderViewCreated(RenderViewHost* render_view_host) {
[email protected]2e4633c2009-07-09 16:58:061541 NotificationService::current()->Notify(
1542 NotificationType::RENDER_VIEW_HOST_CREATED_FOR_TAB,
1543 Source<TabContents>(this),
1544 Details<RenderViewHost>(render_view_host));
[email protected]420ae012009-04-24 05:16:321545 NavigationEntry* entry = controller_.GetActiveEntry();
1546 if (!entry)
1547 return;
1548
1549 // When we're creating views, we're still doing initial setup, so we always
[email protected]e0112912011-02-02 22:54:351550 // use the pending Web UI rather than any possibly existing committed one.
[email protected]1fd1a502011-03-30 16:55:561551 if (render_manager_.pending_web_ui())
[email protected]7ade2732011-02-10 00:13:581552 render_manager_.pending_web_ui()->RenderViewCreated(render_view_host);
[email protected]420ae012009-04-24 05:16:321553
1554 if (entry->IsViewSourceMode()) {
1555 // Put the renderer in view source mode.
1556 render_view_host->Send(
1557 new ViewMsg_EnableViewSourceMode(render_view_host->routing_id()));
1558 }
[email protected]0666aef2009-05-13 19:48:081559
1560 view()->RenderViewCreated(render_view_host);
[email protected]420ae012009-04-24 05:16:321561}
1562
1563void TabContents::RenderViewReady(RenderViewHost* rvh) {
1564 if (rvh != render_view_host()) {
1565 // Don't notify the world, since this came from a renderer in the
1566 // background.
1567 return;
1568 }
1569
1570 NotifyConnected();
[email protected]9d8a4642009-07-29 17:25:301571 bool was_crashed = is_crashed();
[email protected]443b80e2010-12-14 00:42:231572 SetIsCrashed(base::TERMINATION_STATUS_STILL_RUNNING, 0);
[email protected]9d8a4642009-07-29 17:25:301573
1574 // Restore the focus to the tab (otherwise the focus will be on the top
1575 // window).
[email protected]484ae5912010-09-29 19:16:141576 if (was_crashed && !FocusLocationBarByDefault() &&
1577 (!delegate_ || delegate_->ShouldFocusPageAfterCrash())) {
[email protected]9d8a4642009-07-29 17:25:301578 Focus();
[email protected]484ae5912010-09-29 19:16:141579 }
[email protected]420ae012009-04-24 05:16:321580}
1581
[email protected]443b80e2010-12-14 00:42:231582void TabContents::RenderViewGone(RenderViewHost* rvh,
1583 base::TerminationStatus status,
1584 int error_code) {
[email protected]420ae012009-04-24 05:16:321585 if (rvh != render_view_host()) {
1586 // The pending page's RenderViewHost is gone.
1587 return;
1588 }
1589
[email protected]2e3f4572011-03-25 19:24:471590 // Let observers know first and give them a chance to act.
1591 FOR_EACH_OBSERVER(TabContentsObserver, observers_, RenderViewGone());
1592
[email protected]420ae012009-04-24 05:16:321593 SetIsLoading(false, NULL);
1594 NotifyDisconnected();
[email protected]443b80e2010-12-14 00:42:231595 SetIsCrashed(status, error_code);
[email protected]420ae012009-04-24 05:16:321596
[email protected]ff9efc62010-02-18 22:10:541597 // Remove all infobars.
[email protected]ec8f51162011-02-08 04:20:401598 while (!infobar_delegates_.empty())
1599 RemoveInfoBar(GetInfoBarDelegateAt(infobar_count() - 1));
[email protected]ff9efc62010-02-18 22:10:541600
[email protected]8e537302009-06-10 21:19:581601 // Tell the view that we've crashed so it can prepare the sad tab page.
[email protected]cf68b092010-01-15 20:05:551602 // Only do this if we're not in browser shutdown, so that TabContents
1603 // objects that are not in a browser (e.g., HTML dialogs) and thus are
1604 // visible do not flash a sad tab page.
1605 if (browser_shutdown::GetShutdownType() == browser_shutdown::NOT_VALID)
[email protected]a8f024392011-01-13 21:50:161606 view_->OnTabCrashed(status, error_code);
[email protected]420ae012009-04-24 05:16:321607
1608 // Hide any visible hung renderer warning for this web contents' process.
[email protected]7ec6bef2011-02-26 02:22:291609 browser::HideHungRendererDialog(this);
[email protected]420ae012009-04-24 05:16:321610}
1611
[email protected]2e4633c2009-07-09 16:58:061612void TabContents::RenderViewDeleted(RenderViewHost* rvh) {
[email protected]fb7b79f72009-11-06 18:00:481613 render_manager_.RenderViewDeleted(rvh);
[email protected]2e4633c2009-07-09 16:58:061614}
1615
[email protected]420ae012009-04-24 05:16:321616void TabContents::DidNavigate(RenderViewHost* rvh,
1617 const ViewHostMsg_FrameNavigate_Params& params) {
[email protected]8030f012009-09-25 18:09:371618 int extra_invalidate_flags = 0;
1619
1620 if (PageTransition::IsMainFrame(params.transition)) {
[email protected]20ede072009-10-06 00:28:351621 bool was_bookmark_bar_visible = ShouldShowBookmarkBar();
[email protected]8030f012009-09-25 18:09:371622
[email protected]420ae012009-04-24 05:16:321623 render_manager_.DidNavigateMainFrame(rvh);
1624
[email protected]20ede072009-10-06 00:28:351625 if (was_bookmark_bar_visible != ShouldShowBookmarkBar())
[email protected]8030f012009-09-25 18:09:371626 extra_invalidate_flags |= INVALIDATE_BOOKMARK_BAR;
[email protected]8030f012009-09-25 18:09:371627 }
1628
[email protected]420ae012009-04-24 05:16:321629 // Update the site of the SiteInstance if it doesn't have one yet.
1630 if (!GetSiteInstance()->has_site())
1631 GetSiteInstance()->SetSite(params.url);
1632
1633 // Need to update MIME type here because it's referred to in
1634 // UpdateNavigationCommands() called by RendererDidNavigate() to
1635 // determine whether or not to enable the encoding menu.
1636 // It's updated only for the main frame. For a subframe,
1637 // RenderView::UpdateURL does not set params.contents_mime_type.
1638 // (see http://code.google.com/p/chromium/issues/detail?id=2929 )
1639 // TODO(jungshik): Add a test for the encoding menu to avoid
1640 // regressing it again.
1641 if (PageTransition::IsMainFrame(params.transition))
1642 contents_mime_type_ = params.contents_mime_type;
1643
1644 NavigationController::LoadCommittedDetails details;
[email protected]8030f012009-09-25 18:09:371645 bool did_navigate = controller_.RendererDidNavigate(
1646 params, extra_invalidate_flags, &details);
[email protected]3e90d4a2009-07-03 17:38:391647
[email protected]a9c0bfe2010-09-17 08:35:221648 // Send notification about committed provisional loads. This notification is
1649 // different from the NAV_ENTRY_COMMITTED notification which doesn't include
1650 // the actual URL navigated to and isn't sent for AUTO_SUBFRAME navigations.
1651 if (details.type != NavigationType::NAV_IGNORE) {
[email protected]dabb0d12010-10-05 12:50:071652 // For AUTO_SUBFRAME navigations, an event for the main frame is generated
1653 // that is not recorded in the navigation history. For the purpose of
1654 // tracking navigation events, we treat this event as a sub frame navigation
1655 // event.
1656 bool is_main_frame = did_navigate ? details.is_main_frame : false;
[email protected]0d60f0192011-04-14 12:40:101657 PageTransition::Type transition_type = params.transition;
[email protected]a9c0bfe2010-09-17 08:35:221658 // Whether or not a page transition was triggered by going backward or
1659 // forward in the history is only stored in the navigation controller's
1660 // entry list.
1661 if (did_navigate &&
1662 (controller_.GetActiveEntry()->transition_type() &
1663 PageTransition::FORWARD_BACK)) {
[email protected]0d60f0192011-04-14 12:40:101664 transition_type = params.transition | PageTransition::FORWARD_BACK;
[email protected]a9c0bfe2010-09-17 08:35:221665 }
[email protected]0d60f0192011-04-14 12:40:101666 // Notify observers about the commit of the provisional load.
1667 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
1668 DidCommitProvisionalLoadForFrame(params.frame_id,
1669 is_main_frame, params.url, transition_type));
[email protected]a9c0bfe2010-09-17 08:35:221670 }
1671
[email protected]3e90d4a2009-07-03 17:38:391672 // Update history. Note that this needs to happen after the entry is complete,
1673 // which WillNavigate[Main,Sub]Frame will do before this function is called.
[email protected]ec0b6c42010-08-26 03:16:581674 if (params.should_update_history) {
[email protected]3e90d4a2009-07-03 17:38:391675 // Most of the time, the displayURL matches the loaded URL, but for about:
[email protected]ec0b6c42010-08-26 03:16:581676 // URLs, we use a data: URL as the real value. We actually want to save the
1677 // about: URL to the history db and keep the data: URL hidden. This is what
1678 // the TabContents' URL getter does.
1679 scoped_refptr<history::HistoryAddPageArgs> add_page_args(
1680 CreateHistoryAddPageArgs(GetURL(), details, params));
1681 if (!delegate() ||
1682 delegate()->ShouldAddNavigationToHistory(*add_page_args,
1683 details.type)) {
1684 UpdateHistoryForNavigation(add_page_args);
1685 }
[email protected]3e90d4a2009-07-03 17:38:391686 }
1687
[email protected]76543b92009-08-31 17:27:451688 if (!did_navigate)
1689 return; // No navigation happened.
[email protected]420ae012009-04-24 05:16:321690
1691 // DO NOT ADD MORE STUFF TO THIS FUNCTION! Your component should either listen
1692 // for the appropriate notification (best) or you can add it to
1693 // DidNavigateMainFramePostCommit / DidNavigateAnyFramePostCommit (only if
1694 // necessary, please).
1695
1696 // Run post-commit tasks.
1697 if (details.is_main_frame)
1698 DidNavigateMainFramePostCommit(details, params);
1699 DidNavigateAnyFramePostCommit(rvh, details, params);
1700}
1701
1702void TabContents::UpdateState(RenderViewHost* rvh,
1703 int32 page_id,
1704 const std::string& state) {
1705 DCHECK(rvh == render_view_host());
1706
1707 // We must be prepared to handle state updates for any page, these occur
1708 // when the user is scrolling and entering form data, as well as when we're
1709 // leaving a page, in which case our state may have already been moved to
1710 // the next page. The navigation controller will look up the appropriate
1711 // NavigationEntry and update it when it is notified via the delegate.
1712
1713 int entry_index = controller_.GetEntryIndexWithPageID(
1714 GetSiteInstance(), page_id);
1715 if (entry_index < 0)
1716 return;
1717 NavigationEntry* entry = controller_.GetEntryAtIndex(entry_index);
1718
1719 if (state == entry->content_state())
1720 return; // Nothing to update.
1721 entry->set_content_state(state);
1722 controller_.NotifyEntryChanged(entry, entry_index);
1723}
1724
1725void TabContents::UpdateTitle(RenderViewHost* rvh,
[email protected]6b2f7a82011-04-25 19:30:511726 int32 page_id, const std::wstring& title) {
[email protected]420ae012009-04-24 05:16:321727 // If we have a title, that's a pretty good indication that we've started
1728 // getting useful data.
1729 SetNotWaitingForResponse();
1730
1731 DCHECK(rvh == render_view_host());
[email protected]371cc8b2010-09-01 16:03:481732 NavigationEntry* entry = controller_.GetEntryWithPageID(rvh->site_instance(),
[email protected]ec0b6c42010-08-26 03:16:581733 page_id);
[email protected]40ead7b62011-04-23 03:17:281734 if (!entry || !UpdateTitleForEntry(entry, title))
[email protected]420ae012009-04-24 05:16:321735 return;
1736
1737 // Broadcast notifications when the UI should be updated.
1738 if (entry == controller_.GetEntryAtOffset(0))
[email protected]f1cd5e82009-10-23 17:19:031739 NotifyNavigationStateChanged(INVALIDATE_TITLE);
[email protected]420ae012009-04-24 05:16:321740}
1741
[email protected]420ae012009-04-24 05:16:321742void TabContents::UpdateEncoding(RenderViewHost* render_view_host,
[email protected]41fc0322009-09-04 22:23:401743 const std::string& encoding) {
[email protected]420ae012009-04-24 05:16:321744 set_encoding(encoding);
1745}
1746
1747void TabContents::UpdateTargetURL(int32 page_id, const GURL& url) {
1748 if (delegate())
1749 delegate()->UpdateTargetURL(this, url);
1750}
1751
[email protected]c4e98902010-06-01 10:20:141752void TabContents::UpdateInspectorSetting(const std::string& key,
1753 const std::string& value) {
[email protected]24d27d72010-10-12 16:30:051754 RenderViewHostDelegateHelper::UpdateInspectorSetting(profile(), key, value);
[email protected]0df30122009-06-03 12:13:081755}
1756
[email protected]45aafc92010-06-07 14:18:531757void TabContents::ClearInspectorSettings() {
[email protected]24d27d72010-10-12 16:30:051758 RenderViewHostDelegateHelper::ClearInspectorSettings(profile());
[email protected]45aafc92010-06-07 14:18:531759}
1760
[email protected]420ae012009-04-24 05:16:321761void TabContents::Close(RenderViewHost* rvh) {
[email protected]07707302009-11-06 00:50:291762 // The UI may be in an event-tracking loop, such as between the
1763 // mouse-down and mouse-up in text selection or a button click.
1764 // Defer the close until after tracking is complete, so that we
1765 // don't free objects out from under the UI.
1766 // TODO(shess): This could probably be integrated with the
1767 // IsDoingDrag() test below. Punting for now because I need more
1768 // research to understand how this impacts platforms other than Mac.
1769 // TODO(shess): This could get more fine-grained. For instance,
1770 // closing a tab in another window while selecting text in the
1771 // current window's Omnibox should be just fine.
1772 if (view()->IsEventTracking()) {
1773 view()->CloseTabAfterEventTracking();
1774 return;
1775 }
1776
[email protected]24a4d1062009-07-10 23:10:421777 // If we close the tab while we're in the middle of a drag, we'll crash.
1778 // Instead, cancel the drag and close it as soon as the drag ends.
1779 if (view()->IsDoingDrag()) {
1780 view()->CancelDragAndCloseTab();
1781 return;
1782 }
1783
[email protected]420ae012009-04-24 05:16:321784 // Ignore this if it comes from a RenderViewHost that we aren't showing.
1785 if (delegate() && rvh == render_view_host())
1786 delegate()->CloseContents(this);
1787}
1788
1789void TabContents::RequestMove(const gfx::Rect& new_bounds) {
1790 if (delegate() && delegate()->IsPopup(this))
1791 delegate()->MoveContents(this, new_bounds);
1792}
1793
[email protected]7ab1e7d62009-10-14 23:32:011794void TabContents::DidStartLoading() {
[email protected]420ae012009-04-24 05:16:321795 SetIsLoading(true, NULL);
[email protected]3c9e1872010-11-18 16:17:491796
[email protected]c40d6232011-03-25 00:16:211797 if (delegate() && content_restrictions_) {
1798 content_restrictions_ = 0;
[email protected]de4761b2010-12-07 21:05:211799 delegate()->ContentRestrictionsChanged(this);
[email protected]9e823662010-10-13 23:36:001800 }
[email protected]3c9e1872010-11-18 16:17:491801
1802 // Notify observers about navigation.
[email protected]585b30362011-01-28 02:30:171803 FOR_EACH_OBSERVER(TabContentsObserver, observers_, DidStartLoading());
[email protected]420ae012009-04-24 05:16:321804}
1805
[email protected]7ab1e7d62009-10-14 23:32:011806void TabContents::DidStopLoading() {
[email protected]420ae012009-04-24 05:16:321807 scoped_ptr<LoadNotificationDetails> details;
1808
1809 NavigationEntry* entry = controller_.GetActiveEntry();
1810 // An entry may not exist for a stop when loading an initial blank page or
1811 // if an iframe injected by script into a blank page finishes loading.
1812 if (entry) {
[email protected]420ae012009-04-24 05:16:321813 base::TimeDelta elapsed = base::TimeTicks::Now() - current_load_start_;
1814
1815 details.reset(new LoadNotificationDetails(
[email protected]ebe89e062009-08-13 23:16:541816 entry->virtual_url(),
[email protected]420ae012009-04-24 05:16:321817 entry->transition_type(),
1818 elapsed,
1819 &controller_,
1820 controller_.GetCurrentEntryIndex()));
1821 }
1822
[email protected]420ae012009-04-24 05:16:321823 SetIsLoading(false, details.get());
[email protected]3c9e1872010-11-18 16:17:491824
1825 // Notify observers about navigation.
[email protected]585b30362011-01-28 02:30:171826 FOR_EACH_OBSERVER(TabContentsObserver, observers_, DidStopLoading());
[email protected]420ae012009-04-24 05:16:321827}
1828
[email protected]c95fa8b2011-04-28 20:26:161829void TabContents::DidCancelLoading() {
1830 controller_.DiscardNonCommittedEntries();
1831
1832 // Update the URL display.
1833 NotifyNavigationStateChanged(TabContents::INVALIDATE_URL);
1834}
1835
[email protected]1a3c3cb2010-12-16 21:03:401836void TabContents::DidChangeLoadProgress(double progress) {
1837 if (delegate())
1838 delegate()->LoadProgressChanged(progress);
1839}
1840
[email protected]25497492010-09-11 15:15:081841void TabContents::DocumentOnLoadCompletedInMainFrame(
1842 RenderViewHost* render_view_host,
1843 int32 page_id) {
1844 NotificationService::current()->Notify(
1845 NotificationType::LOAD_COMPLETED_MAIN_FRAME,
1846 Source<TabContents>(this),
1847 Details<int>(&page_id));
1848}
1849
[email protected]420ae012009-04-24 05:16:321850void TabContents::RequestOpenURL(const GURL& url, const GURL& referrer,
1851 WindowOpenDisposition disposition) {
[email protected]7ade2732011-02-10 00:13:581852 if (render_manager_.web_ui()) {
[email protected]e0112912011-02-02 22:54:351853 // When we're a Web UI, it will provide a page transition type for us (this
[email protected]420ae012009-04-24 05:16:321854 // is so the new tab page can specify AUTO_BOOKMARK for automatically
1855 // generated suggestions).
1856 //
[email protected]e0112912011-02-02 22:54:351857 // Note also that we hide the referrer for Web UI pages. We don't really
[email protected]60e448982009-05-06 04:21:161858 // want web sites to see a referrer of "chrome://blah" (and some
1859 // chrome: URLs might have search terms or other stuff we don't want to
[email protected]420ae012009-04-24 05:16:321860 // send to the site), so we send no referrer.
1861 OpenURL(url, GURL(), disposition,
[email protected]7ade2732011-02-10 00:13:581862 render_manager_.web_ui()->link_transition_type());
[email protected]420ae012009-04-24 05:16:321863 } else {
1864 OpenURL(url, referrer, disposition, PageTransition::LINK);
1865 }
1866}
1867
[email protected]420ae012009-04-24 05:16:321868void TabContents::ProcessExternalHostMessage(const std::string& message,
1869 const std::string& origin,
1870 const std::string& target) {
1871 if (delegate())
1872 delegate()->ForwardMessageToExternalHost(message, origin, target);
1873}
1874
[email protected]420ae012009-04-24 05:16:321875void TabContents::RunJavaScriptMessage(
1876 const std::wstring& message,
1877 const std::wstring& default_prompt,
1878 const GURL& frame_url,
1879 const int flags,
1880 IPC::Message* reply_msg,
1881 bool* did_suppress_message) {
1882 // Suppress javascript messages when requested and when inside a constrained
1883 // popup window (because that activates them and breaks them out of the
1884 // constrained window jail).
[email protected]91ed49a2009-07-07 21:29:551885 // Also suppress messages when showing an interstitial. The interstitial is
1886 // shown over the previous page, we don't want the hidden page dialogs to
1887 // interfere with the interstitial.
[email protected]8f55e802010-12-06 18:11:501888 bool suppress_this_message =
1889 suppress_javascript_messages_ ||
1890 showing_interstitial_page() ||
1891 (delegate() && delegate()->ShouldSuppressDialogs());
[email protected]420ae012009-04-24 05:16:321892 if (delegate())
1893 suppress_this_message |=
[email protected]b6c874582009-05-08 19:38:311894 (delegate()->GetConstrainingContents(this) != this);
[email protected]420ae012009-04-24 05:16:321895
1896 *did_suppress_message = suppress_this_message;
1897
1898 if (!suppress_this_message) {
1899 base::TimeDelta time_since_last_message(
1900 base::TimeTicks::Now() - last_javascript_message_dismissal_);
1901 bool show_suppress_checkbox = false;
1902 // Show a checkbox offering to suppress further messages if this message is
1903 // being displayed within kJavascriptMessageExpectedDelay of the last one.
1904 if (time_since_last_message <
[email protected]1698f612011-03-11 11:28:151905 base::TimeDelta::FromMilliseconds(
1906 chrome::kJavascriptMessageExpectedDelay))
[email protected]420ae012009-04-24 05:16:321907 show_suppress_checkbox = true;
1908
[email protected]d6ad7442010-09-05 19:06:521909 RunJavascriptMessageBox(profile(), this, frame_url, flags, message,
1910 default_prompt, show_suppress_checkbox, reply_msg);
[email protected]420ae012009-04-24 05:16:321911 } else {
1912 // If we are suppressing messages, just reply as is if the user immediately
1913 // pressed "Cancel".
[email protected]beb440c2009-11-06 04:08:541914 OnMessageBoxClosed(reply_msg, false, std::wstring());
[email protected]420ae012009-04-24 05:16:321915 }
1916}
1917
1918void TabContents::RunBeforeUnloadConfirm(const std::wstring& message,
1919 IPC::Message* reply_msg) {
[email protected]5aab5e22010-12-08 22:13:291920 if (delegate())
1921 delegate()->WillRunBeforeUnloadConfirm();
[email protected]8f55e802010-12-06 18:11:501922 if (delegate() && delegate()->ShouldSuppressDialogs()) {
1923 render_view_host()->JavaScriptMessageBoxClosed(reply_msg, true,
1924 std::wstring());
1925 return;
1926 }
[email protected]7ab1e7d62009-10-14 23:32:011927 is_showing_before_unload_dialog_ = true;
[email protected]d82ed61e2009-06-16 02:46:221928 RunBeforeUnloadDialog(this, message, reply_msg);
[email protected]420ae012009-04-24 05:16:321929}
1930
[email protected]420ae012009-04-24 05:16:321931GURL TabContents::GetAlternateErrorPageURL() const {
[email protected]763ec4ca2011-04-29 15:48:121932 return content::GetContentClient()->browser()->GetAlternateErrorPageURL(this);
[email protected]420ae012009-04-24 05:16:321933}
1934
1935WebPreferences TabContents::GetWebkitPrefs() {
[email protected]6c32ce72010-03-08 05:18:061936 Profile* profile = render_view_host()->process()->profile();
[email protected]7ade2732011-02-10 00:13:581937 bool is_web_ui = false;
[email protected]447021c2010-09-08 21:29:081938 WebPreferences web_prefs =
[email protected]7ade2732011-02-10 00:13:581939 RenderViewHostDelegateHelper::GetWebkitPrefs(profile, is_web_ui);
[email protected]447021c2010-09-08 21:29:081940
[email protected]9beff752010-09-22 19:35:431941 // Force accelerated compositing and 2d canvas off for chrome: and
1942 // chrome-extension: pages.
[email protected]27c790d42010-10-22 09:28:431943 if (GetURL().SchemeIs(chrome::kChromeDevToolsScheme) ||
1944 GetURL().SchemeIs(chrome::kChromeUIScheme)) {
[email protected]447021c2010-09-08 21:29:081945 web_prefs.accelerated_compositing_enabled = false;
[email protected]9beff752010-09-22 19:35:431946 web_prefs.accelerated_2d_canvas_enabled = false;
[email protected]447021c2010-09-08 21:29:081947 }
1948
[email protected]39553442010-09-09 00:01:551949#if defined(OS_MACOSX)
1950 // Disable accelerated compositing if IOSurface's are not supported,
1951 // as is the case in 10.5.
1952 if (!IOSurfaceSupport::Initialize())
1953 web_prefs.accelerated_compositing_enabled = false;
1954#endif
1955
[email protected]447021c2010-09-08 21:29:081956 return web_prefs;
[email protected]420ae012009-04-24 05:16:321957}
1958
[email protected]7d472472011-01-22 01:30:251959void TabContents::OnUserGesture() {
1960 // See comment in RenderViewHostDelegate::OnUserGesture as to why we do this.
1961 DownloadRequestLimiter* limiter =
1962 g_browser_process->download_request_limiter();
1963 if (limiter)
1964 limiter->OnUserGesture(this);
1965 ExternalProtocolHandler::PermitLaunchUrl();
1966}
1967
[email protected]fa1cf0b82010-01-15 21:49:441968void TabContents::OnIgnoredUIEvent() {
1969 if (constrained_window_count()) {
1970 ConstrainedWindow* window = *constrained_window_begin();
1971 window->FocusConstrainedWindow();
1972 }
1973}
1974
[email protected]420ae012009-04-24 05:16:321975void TabContents::OnCrossSiteResponse(int new_render_process_host_id,
1976 int new_request_id) {
[email protected]57c6a652009-05-04 07:58:341977 // Allows the TabContents to react when a cross-site response is ready to be
[email protected]420ae012009-04-24 05:16:321978 // delivered to a pending RenderViewHost. We must first run the onunload
1979 // handler of the old RenderViewHost before we can allow it to proceed.
1980 render_manager_.OnCrossSiteResponse(new_render_process_host_id,
1981 new_request_id);
1982}
1983
[email protected]420ae012009-04-24 05:16:321984void TabContents::RendererUnresponsive(RenderViewHost* rvh,
1985 bool is_during_unload) {
1986 if (is_during_unload) {
1987 // Hang occurred while firing the beforeunload/unload handler.
1988 // Pretend the handler fired so tab closing continues as if it had.
[email protected]829e7612009-04-25 01:15:111989 rvh->set_sudden_termination_allowed(true);
[email protected]420ae012009-04-24 05:16:321990
1991 if (!render_manager_.ShouldCloseTabOnUnresponsiveRenderer())
1992 return;
1993
1994 // If the tab hangs in the beforeunload/unload handler there's really
1995 // nothing we can do to recover. Pretend the unload listeners have
1996 // all fired and close the tab. If the hang is in the beforeunload handler
1997 // then the user will not have the option of cancelling the close.
1998 Close(rvh);
1999 return;
2000 }
2001
[email protected]61b50092011-02-03 18:02:192002 if (render_view_host() && render_view_host()->IsRenderViewLive() &&
2003 (!delegate() || delegate()->ShouldShowHungRendererDialog())) {
[email protected]7ec6bef2011-02-26 02:22:292004 browser::ShowHungRendererDialog(this);
[email protected]61b50092011-02-03 18:02:192005 }
[email protected]420ae012009-04-24 05:16:322006}
2007
2008void TabContents::RendererResponsive(RenderViewHost* render_view_host) {
[email protected]7ec6bef2011-02-26 02:22:292009 browser::HideHungRendererDialog(this);
[email protected]420ae012009-04-24 05:16:322010}
2011
2012void TabContents::LoadStateChanged(const GURL& url,
[email protected]094e5b22009-09-25 04:23:562013 net::LoadState load_state,
2014 uint64 upload_position,
2015 uint64 upload_size) {
[email protected]420ae012009-04-24 05:16:322016 load_state_ = load_state;
[email protected]094e5b22009-09-25 04:23:562017 upload_position_ = upload_position;
2018 upload_size_ = upload_size;
[email protected]04866c42011-05-03 20:03:502019 load_state_host_ = net::IDNToUnicode(url.host(),
2020 profile()->GetPrefs()->GetString(prefs::kAcceptLanguages));
[email protected]420ae012009-04-24 05:16:322021 if (load_state_ == net::LOAD_STATE_READING_RESPONSE)
2022 SetNotWaitingForResponse();
2023 if (is_loading())
[email protected]c9cd2222009-05-06 05:16:502024 NotifyNavigationStateChanged(INVALIDATE_LOAD | INVALIDATE_TAB);
[email protected]420ae012009-04-24 05:16:322025}
2026
[email protected]1ee614862009-07-06 20:29:182027bool TabContents::IsExternalTabContainer() const {
2028 if (!delegate())
2029 return false;
2030
2031 return delegate()->IsExternalTabContainer();
2032}
2033
[email protected]1ee614862009-07-06 20:29:182034void TabContents::DidInsertCSS() {
2035 // This RVHDelegate function is used for extensions and not us.
[email protected]420ae012009-04-24 05:16:322036}
2037
[email protected]546ae4e02010-12-08 14:57:192038void TabContents::FocusedNodeChanged(bool is_editable_node) {
[email protected]130efb02009-09-18 18:54:352039 NotificationService::current()->Notify(
2040 NotificationType::FOCUS_CHANGED_IN_PAGE,
[email protected]4e85c112011-01-26 22:27:212041 Source<TabContents>(this),
[email protected]546ae4e02010-12-08 14:57:192042 Details<const bool>(&is_editable_node));
[email protected]130efb02009-09-18 18:54:352043}
2044
[email protected]b75b8292010-10-01 07:28:252045void TabContents::UpdateZoomLimits(int minimum_percent,
2046 int maximum_percent,
2047 bool remember) {
2048 minimum_zoom_percent_ = minimum_percent;
2049 maximum_zoom_percent_ = maximum_percent;
2050 temporary_zoom_settings_ = !remember;
[email protected]ebf40a72010-07-22 01:46:382051}
2052
[email protected]7d472472011-01-22 01:30:252053void TabContents::WorkerCrashed() {
[email protected]0dd3a0ab2011-02-18 08:17:442054 if (delegate())
2055 delegate()->WorkerCrashed();
[email protected]7d472472011-01-22 01:30:252056}
2057
[email protected]f364d1392011-04-08 21:03:102058void TabContents::RequestDesktopNotificationPermission(
2059 const GURL& source_origin, int callback_context) {
2060 DesktopNotificationService* service =
[email protected]dcb72d52011-04-13 12:36:532061 DesktopNotificationServiceFactory::GetForProfile(profile());
[email protected]f364d1392011-04-08 21:03:102062 service->RequestPermission(
2063 source_origin, GetRenderProcessHost()->id(),
2064 render_view_host()->routing_id(), callback_context, this);
2065}
2066
[email protected]420ae012009-04-24 05:16:322067void TabContents::BeforeUnloadFiredFromRenderManager(
2068 bool proceed,
2069 bool* proceed_to_fire_unload) {
2070 if (delegate())
2071 delegate()->BeforeUnloadFired(this, proceed, proceed_to_fire_unload);
2072}
2073
[email protected]3a3d47472010-07-15 21:03:542074void TabContents::DidStartLoadingFromRenderManager(
2075 RenderViewHost* render_view_host) {
2076 DidStartLoading();
2077}
2078
2079void TabContents::RenderViewGoneFromRenderManager(
2080 RenderViewHost* render_view_host) {
[email protected]443b80e2010-12-14 00:42:232081 DCHECK(crashed_status_ != base::TERMINATION_STATUS_STILL_RUNNING);
2082 RenderViewGone(render_view_host, crashed_status_, crashed_error_code_);
[email protected]3a3d47472010-07-15 21:03:542083}
2084
[email protected]420ae012009-04-24 05:16:322085void TabContents::UpdateRenderViewSizeForRenderManager() {
2086 // TODO(brettw) this is a hack. See TabContentsView::SizeContents.
[email protected]0323ee42010-02-17 22:03:262087 gfx::Size size = view_->GetContainerSize();
2088 // 0x0 isn't a valid window size (minimal window size is 1x1) but it may be
2089 // here during container initialization and normal window size will be set
2090 // later. In case of tab duplication this resizing to 0x0 prevents setting
2091 // normal size later so just ignore it.
2092 if (!size.IsEmpty())
2093 view_->SizeContents(size);
[email protected]420ae012009-04-24 05:16:322094}
2095
[email protected]3a3d47472010-07-15 21:03:542096void TabContents::NotifySwappedFromRenderManager() {
2097 NotifySwapped();
2098}
2099
2100NavigationController& TabContents::GetControllerForRenderManager() {
2101 return controller();
2102}
2103
[email protected]d0980792011-02-13 19:41:402104WebUI* TabContents::CreateWebUIForRenderManager(const GURL& url) {
[email protected]1fd1a502011-03-30 16:55:562105 return content::WebUIFactory::Get()->CreateWebUIForURL(this, url);
[email protected]420ae012009-04-24 05:16:322106}
2107
2108NavigationEntry*
2109TabContents::GetLastCommittedNavigationEntryForRenderManager() {
2110 return controller_.GetLastCommittedEntry();
2111}
2112
2113bool TabContents::CreateRenderViewForRenderManager(
2114 RenderViewHost* render_view_host) {
[email protected]420ae012009-04-24 05:16:322115 RenderWidgetHostView* rwh_view = view_->CreateViewForWidget(render_view_host);
[email protected]1a98a932009-11-17 00:12:522116
[email protected]420ae012009-04-24 05:16:322117 // Now that the RenderView has been created, we need to tell it its size.
2118 rwh_view->SetSize(view_->GetContainerSize());
2119
[email protected]a4127722011-04-27 23:13:522120 if (!render_view_host->CreateRenderView(string16()))
2121 return false;
2122
[email protected]420ae012009-04-24 05:16:322123 UpdateMaxPageIDIfNecessary(render_view_host->site_instance(),
2124 render_view_host);
2125 return true;
2126}
2127
2128void TabContents::Observe(NotificationType type,
2129 const NotificationSource& source,
2130 const NotificationDetails& details) {
2131 switch (type.value) {
[email protected]420ae012009-04-24 05:16:322132 case NotificationType::PREF_CHANGED: {
[email protected]57ecc4b2010-08-11 03:02:512133 std::string* pref_name_in = Details<std::string>(details).ptr();
[email protected]420ae012009-04-24 05:16:322134 DCHECK(Source<PrefService>(source).ptr() == profile()->GetPrefs());
2135 if (*pref_name_in == prefs::kAlternateErrorPagesEnabled) {
2136 UpdateAlternateErrorPageURL();
[email protected]88e14382010-10-14 21:45:532137 } else if ((*pref_name_in == prefs::kDefaultCharset) ||
2138 StartsWithASCII(*pref_name_in, "webkit.webprefs.", true)) {
[email protected]420ae012009-04-24 05:16:322139 UpdateWebPreferences();
[email protected]d0b8d092010-10-25 04:05:172140 } else if (*pref_name_in == prefs::kDefaultZoomLevel) {
2141 UpdateZoomLevel();
[email protected]0a8db0d2011-04-13 15:15:402142 } else if (*pref_name_in == prefs::kEnableReferrers) {
2143 renderer_preferences_util::UpdateFromSystemSettings(
2144 &renderer_preferences_, profile());
2145 render_view_host()->SyncRendererPrefs();
[email protected]420ae012009-04-24 05:16:322146 } else {
2147 NOTREACHED() << "unexpected pref change notification" << *pref_name_in;
2148 }
2149 break;
2150 }
2151 case NotificationType::RENDER_WIDGET_HOST_DESTROYED:
2152 view_->RenderWidgetHostDestroyed(Source<RenderWidgetHost>(source).ptr());
2153 break;
2154
2155 case NotificationType::NAV_ENTRY_COMMITTED: {
2156 DCHECK(&controller_ == Source<NavigationController>(source).ptr());
2157
2158 NavigationController::LoadCommittedDetails& committed_details =
2159 *(Details<NavigationController::LoadCommittedDetails>(details).ptr());
2160 ExpireInfoBars(committed_details);
2161 break;
2162 }
2163
[email protected]d3ba77272009-09-03 00:06:092164#if defined(OS_LINUX)
2165 case NotificationType::BROWSER_THEME_CHANGED: {
[email protected]7ff431e2010-01-07 18:18:542166 renderer_preferences_util::UpdateFromSystemSettings(
2167 &renderer_preferences_, profile());
[email protected]d3ba77272009-09-03 00:06:092168 render_view_host()->SyncRendererPrefs();
2169 break;
2170 }
2171#endif
2172
[email protected]6c32ce72010-03-08 05:18:062173 case NotificationType::USER_STYLE_SHEET_UPDATED:
2174 UpdateWebPreferences();
2175 break;
2176
[email protected]79580c62010-02-02 02:36:252177 case NotificationType::CONTENT_SETTINGS_CHANGED: {
[email protected]5f24f212010-12-02 14:47:452178 Details<const ContentSettingsDetails> settings_details(details);
[email protected]79580c62010-02-02 02:36:252179 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]da0c8e92010-02-09 23:25:492180 GURL entry_url;
[email protected]0314ae02010-04-08 09:18:292181 if (entry)
[email protected]da0c8e92010-02-09 23:25:492182 entry_url = entry->url();
[email protected]0314ae02010-04-08 09:18:292183 if (settings_details.ptr()->update_all() ||
2184 settings_details.ptr()->pattern().Matches(entry_url)) {
2185 render_view_host()->SendContentSettings(entry_url,
[email protected]b9a4ea7f2010-06-25 07:11:232186 profile()->GetHostContentSettingsMap()->
2187 GetContentSettings(entry_url));
[email protected]79580c62010-02-02 02:36:252188 }
2189 break;
2190 }
2191
[email protected]88152052010-10-01 04:05:182192 case NotificationType::GOOGLE_URL_UPDATED:
2193 UpdateAlternateErrorPageURL();
2194 break;
2195
[email protected]420ae012009-04-24 05:16:322196 default:
2197 NOTREACHED();
2198 }
2199}
[email protected]aacd7cc2009-09-02 14:51:102200
[email protected]beb440c2009-11-06 04:08:542201gfx::NativeWindow TabContents::GetMessageBoxRootWindow() {
2202 return view_->GetTopLevelNativeWindow();
2203}
2204
2205void TabContents::OnMessageBoxClosed(IPC::Message* reply_msg,
2206 bool success,
2207 const std::wstring& prompt) {
2208 last_javascript_message_dismissal_ = base::TimeTicks::Now();
2209 if (is_showing_before_unload_dialog_ && !success) {
2210 // If a beforeunload dialog is canceled, we need to stop the throbber from
2211 // spinning, since we forced it to start spinning in Navigate.
2212 DidStopLoading();
2213
2214 tab_close_start_time_ = base::TimeTicks();
2215 }
2216 is_showing_before_unload_dialog_ = false;
2217 render_view_host()->JavaScriptMessageBoxClosed(reply_msg, success, prompt);
2218}
2219
2220void TabContents::SetSuppressMessageBoxes(bool suppress_message_boxes) {
2221 set_suppress_javascript_messages(suppress_message_boxes);
2222}
2223
[email protected]3a3d47472010-07-15 21:03:542224TabContents* TabContents::AsTabContents() {
2225 return this;
2226}
2227
2228ExtensionHost* TabContents::AsExtensionHost() {
2229 return NULL;
2230}
2231
[email protected]41fc0322009-09-04 22:23:402232void TabContents::set_encoding(const std::string& encoding) {
[email protected]763ec4ca2011-04-29 15:48:122233 encoding_ = content::GetContentClient()->browser()->
2234 GetCanonicalEncodingNameByAliasName(encoding);
[email protected]aacd7cc2009-09-02 14:51:102235}
[email protected]f45d2a72010-03-08 23:28:352236
[email protected]33f74972010-12-08 16:40:362237void TabContents::SwapInRenderViewHost(RenderViewHost* rvh) {
2238 render_manager_.SwapInRenderViewHost(rvh);
2239}
2240
2241void TabContents::CreateViewAndSetSizeForRVH(RenderViewHost* rvh) {
2242 RenderWidgetHostView* rwh_view = view()->CreateViewForWidget(rvh);
2243 rwh_view->SetSize(view()->GetContainerSize());
2244}
[email protected]e7d50892011-01-19 21:47:382245
[email protected]232a5812011-03-04 22:42:082246void TabContents::OnOnlineStateChanged(bool online) {
2247 render_view_host()->Send(new ViewMsg_NetworkStateChanged(
2248 render_view_host()->routing_id(), online));
2249}