blob: fb62732f1a764a9c3e29d0345eb34c372542c4c1 [file] [log] [blame]
[email protected]7e7414ae2010-01-26 20:19:291// Copyright (c) 2010 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]f3ec7742009-01-15 00:59:165#include "chrome/browser/tab_contents/tab_contents.h"
initial.commit09911bf2008-07-26 23:55:296
[email protected]06821f42d2010-05-03 22:34:357#if defined(OS_CHROMEOS)
8// For GdkScreen
9#include <gdk/gdk.h>
10#endif // defined(OS_CHROMEOS)
11
[email protected]a92b8642009-05-05 23:38:5612#include "app/l10n_util.h"
[email protected]9929da92009-05-05 02:05:1113#include "app/resource_bundle.h"
[email protected]614519812010-03-19 08:20:5614#include "app/text_elider.h"
[email protected]8cc8d492010-02-02 10:40:4915#include "base/auto_reset.h"
[email protected]420ae012009-04-24 05:16:3216#include "base/file_version_info.h"
[email protected]7cf1b6ce2010-03-20 06:37:0117#include "base/i18n/rtl.h"
[email protected]420ae012009-04-24 05:16:3218#include "base/process_util.h"
[email protected]96d185d2009-04-24 03:28:5419#include "base/string16.h"
[email protected]996fd702009-09-04 19:12:3720#include "base/string_util.h"
[email protected]96d185d2009-04-24 03:28:5421#include "base/time.h"
[email protected]6db0def92010-03-02 19:55:4522#include "chrome/browser/autocomplete_history_manager.h"
[email protected]21674df2009-10-28 19:51:2123#include "chrome/browser/autofill/autofill_manager.h"
[email protected]66ba4932009-06-04 19:22:1324#include "chrome/browser/blocked_popup_container.h"
[email protected]420ae012009-04-24 05:16:3225#include "chrome/browser/bookmarks/bookmark_model.h"
[email protected]281918912010-05-27 22:05:1326#include "chrome/browser/browser.h"
[email protected]96d185d2009-04-24 03:28:5427#include "chrome/browser/browser_process.h"
[email protected]cf68b092010-01-15 20:05:5528#include "chrome/browser/browser_shutdown.h"
initial.commit09911bf2008-07-26 23:55:2929#include "chrome/browser/cert_store.h"
[email protected]aacd7cc2009-09-02 14:51:1030#include "chrome/browser/character_encoding.h"
[email protected]96d185d2009-04-24 03:28:5431#include "chrome/browser/debugger/devtools_manager.h"
[email protected]420ae012009-04-24 05:16:3232#include "chrome/browser/dom_operation_notification_details.h"
[email protected]96d185d2009-04-24 03:28:5433#include "chrome/browser/dom_ui/dom_ui.h"
34#include "chrome/browser/dom_ui/dom_ui_factory.h"
[email protected]bcef0dc02009-02-28 00:35:0235#include "chrome/browser/download/download_item_model.h"
[email protected]420ae012009-04-24 05:16:3236#include "chrome/browser/download/download_manager.h"
[email protected]41291322010-07-15 17:09:0137#include "chrome/browser/download/download_request_limiter.h"
[email protected]e7eaedde2009-09-25 20:09:4938#include "chrome/browser/external_protocol_handler.h"
[email protected]92c6f9b92009-10-24 04:35:0839#include "chrome/browser/extensions/extensions_service.h"
[email protected]523623c2010-05-19 16:03:4240#include "chrome/browser/history/top_sites.h"
[email protected]0189bc722009-08-28 21:56:4841#include "chrome/browser/favicon_service.h"
[email protected]e5d2f7132010-04-09 18:13:5342#include "chrome/browser/find_bar_state.h"
[email protected]420ae012009-04-24 05:16:3243#include "chrome/browser/google_util.h"
[email protected]79580c62010-02-02 02:36:2544#include "chrome/browser/host_content_settings_map.h"
[email protected]8897c382009-05-06 17:53:2645#include "chrome/browser/hung_renderer_dialog.h"
[email protected]12f74a92010-02-05 22:32:1446#include "chrome/browser/message_box_handler.h"
[email protected]420ae012009-04-24 05:16:3247#include "chrome/browser/load_from_memory_cache_details.h"
48#include "chrome/browser/load_notification_details.h"
[email protected]84abba62009-10-07 17:01:4449#include "chrome/browser/metrics/metric_event_duration_details.h"
[email protected]ea0e80892009-08-22 00:36:0550#include "chrome/browser/modal_html_dialog_delegate.h"
[email protected]c8865482009-07-23 20:40:1051#include "chrome/browser/omnibox_search_hint.h"
[email protected]fdd61c62009-04-22 19:22:5752#include "chrome/browser/password_manager/password_manager.h"
[email protected]14a000d2010-04-29 21:44:2453#include "chrome/browser/platform_util.h"
[email protected]fdd61c62009-04-22 19:22:5754#include "chrome/browser/plugin_installer.h"
[email protected]052313b2010-02-19 09:43:0855#include "chrome/browser/pref_service.h"
[email protected]12802702010-07-09 19:43:0956#include "chrome/browser/printing/print_view_manager.h"
[email protected]ce560f82009-06-03 09:39:4457#include "chrome/browser/profile.h"
[email protected]1db6ff152009-10-12 15:32:0758#include "chrome/browser/renderer_host/render_process_host.h"
[email protected]8cb5d5b2010-02-09 11:36:1659#include "chrome/browser/renderer_host/render_view_host.h"
[email protected]96d185d2009-04-24 03:28:5460#include "chrome/browser/renderer_host/render_widget_host_view.h"
[email protected]fd694982009-07-03 17:32:3761#include "chrome/browser/renderer_host/resource_request_details.h"
[email protected]1db6ff152009-10-12 15:32:0762#include "chrome/browser/renderer_host/site_instance.h"
[email protected]96d185d2009-04-24 03:28:5463#include "chrome/browser/renderer_host/web_cache_manager.h"
[email protected]93623c5d2009-12-10 21:40:3264#include "chrome/browser/renderer_preferences_util.h"
[email protected]152b8ba32010-03-29 19:40:1665#include "chrome/browser/search_engines/template_url_fetcher.h"
66#include "chrome/browser/search_engines/template_url_model.h"
[email protected]c37e3b62010-01-22 00:15:1767#include "chrome/browser/sessions/session_types.h"
[email protected]bd1ad682009-05-15 22:19:1768#include "chrome/browser/tab_contents/infobar_delegate.h"
[email protected]7e383692009-06-12 19:14:5469#include "chrome/browser/tab_contents/interstitial_page.h"
[email protected]f3ec7742009-01-15 00:59:1670#include "chrome/browser/tab_contents/navigation_entry.h"
[email protected]de34a962009-06-26 23:22:4371#include "chrome/browser/tab_contents/provisional_load_details.h"
[email protected]f3ec7742009-01-15 00:59:1672#include "chrome/browser/tab_contents/tab_contents_delegate.h"
[email protected]fdd61c62009-04-22 19:22:5773#include "chrome/browser/tab_contents/tab_contents_view.h"
[email protected]58dca552009-06-17 00:35:0274#include "chrome/browser/tab_contents/thumbnail_generator.h"
[email protected]152b8ba32010-03-29 19:40:1675#include "chrome/browser/translate/page_translated_details.h"
[email protected]54ec6472010-04-09 19:39:5876#include "chrome/common/bindings_policy.h"
[email protected]35f7d212009-04-29 21:19:2777#include "chrome/common/chrome_switches.h"
[email protected]3b355642010-02-05 16:01:4978#include "chrome/common/extensions/extension.h"
[email protected]5d246db22009-10-27 06:17:5779#include "chrome/common/extensions/extension_action.h"
[email protected]942690b132010-05-11 06:42:1480#include "chrome/common/extensions/extension_resource.h"
[email protected]12802702010-07-09 19:43:0981#include "chrome/common/extensions/url_pattern.h"
82#include "chrome/common/navigation_types.h"
83#include "chrome/common/net/url_request_context_getter.h"
[email protected]bfd04a62009-02-01 18:16:5684#include "chrome/common/notification_service.h"
initial.commit09911bf2008-07-26 23:55:2985#include "chrome/common/pref_names.h"
[email protected]420ae012009-04-24 05:16:3286#include "chrome/common/render_messages.h"
[email protected]1ee614862009-07-06 20:29:1887#include "chrome/common/renderer_preferences.h"
[email protected]96d185d2009-04-24 03:28:5488#include "chrome/common/url_constants.h"
[email protected]ea68c06a2010-04-23 17:25:3789#include "grit/chromium_strings.h"
[email protected]8897c382009-05-06 17:53:2690#include "grit/generated_resources.h"
[email protected]420ae012009-04-24 05:16:3291#include "grit/locale_settings.h"
[email protected]fd626782010-01-22 05:26:0692#include "grit/platform_locale_settings.h"
[email protected]ea68c06a2010-04-23 17:25:3793#include "grit/theme_resources.h"
[email protected]420ae012009-04-24 05:16:3294#include "net/base/mime_util.h"
95#include "net/base/net_errors.h"
[email protected]d686e812009-06-03 19:10:2996#include "net/base/net_util.h"
[email protected]420ae012009-04-24 05:16:3297#include "net/base/registry_controlled_domain.h"
[email protected]1ee614862009-07-06 20:29:1898#include "webkit/glue/password_form.h"
99#include "webkit/glue/webpreferences.h"
[email protected]3e45ba92009-02-20 21:09:00100
[email protected]420ae012009-04-24 05:16:32101// Cross-Site Navigations
102//
103// If a TabContents is told to navigate to a different web site (as determined
104// by SiteInstance), it will replace its current RenderViewHost with a new
105// RenderViewHost dedicated to the new SiteInstance. This works as follows:
106//
107// - Navigate determines whether the destination is cross-site, and if so,
108// it creates a pending_render_view_host_ and moves into the PENDING
109// RendererState.
110// - The pending RVH is "suspended," so that no navigation messages are sent to
111// its renderer until the onbeforeunload JavaScript handler has a chance to
112// run in the current RVH.
113// - The pending RVH tells CrossSiteRequestManager (a thread-safe singleton)
114// that it has a pending cross-site request. ResourceDispatcherHost will
115// check for this when the response arrives.
116// - The current RVH runs its onbeforeunload handler. If it returns false, we
117// cancel all the pending logic and go back to NORMAL. Otherwise we allow
118// the pending RVH to send the navigation request to its renderer.
119// - ResourceDispatcherHost receives a ResourceRequest on the IO thread. It
120// checks CrossSiteRequestManager to see that the RVH responsible has a
121// pending cross-site request, and then installs a CrossSiteEventHandler.
122// - When RDH receives a response, the BufferedEventHandler determines whether
123// it is a download. If so, it sends a message to the new renderer causing
124// it to cancel the request, and the download proceeds in the download
125// thread. For now, we stay in a PENDING state (with a pending RVH) until
[email protected]57c6a652009-05-04 07:58:34126// the next DidNavigate event for this TabContents. This isn't ideal, but it
[email protected]420ae012009-04-24 05:16:32127// doesn't affect any functionality.
128// - After RDH receives a response and determines that it is safe and not a
129// download, it pauses the response to first run the old page's onunload
130// handler. It does this by asynchronously calling the OnCrossSiteResponse
[email protected]57c6a652009-05-04 07:58:34131// method of TabContents on the UI thread, which sends a ClosePage message
[email protected]420ae012009-04-24 05:16:32132// to the current RVH.
133// - Once the onunload handler is finished, a ClosePage_ACK message is sent to
134// the ResourceDispatcherHost, who unpauses the response. Data is then sent
135// to the pending RVH.
136// - The pending renderer sends a FrameNavigate message that invokes the
137// DidNavigate method. This replaces the current RVH with the
138// pending RVH and goes back to the NORMAL RendererState.
139
140namespace {
141
142// Amount of time we wait between when a key event is received and the renderer
143// is queried for its state and pushed to the NavigationEntry.
144const int kQueryStateDelay = 5000;
145
146const int kSyncWaitDelay = 40;
147
[email protected]304f8792009-08-19 21:28:09148#if defined(OS_CHROMEOS)
149// If a popup window is bigger than this fraction of the screen on chrome os,
150// turn it into a tab
151const float kMaxWidthFactor = 0.5;
152const float kMaxHeightFactor = 0.6;
153#endif
154
[email protected]420ae012009-04-24 05:16:32155// If another javascript message box is displayed within
156// kJavascriptMessageExpectedDelay of a previous javascript message box being
157// dismissed, display an option to suppress future message boxes from this
158// contents.
159const int kJavascriptMessageExpectedDelay = 1000;
160
[email protected]420ae012009-04-24 05:16:32161// The list of prefs we want to observe.
162const wchar_t* kPrefsToObserve[] = {
163 prefs::kAlternateErrorPagesEnabled,
164 prefs::kWebKitJavaEnabled,
165 prefs::kWebKitJavascriptEnabled,
166 prefs::kWebKitLoadsImagesAutomatically,
167 prefs::kWebKitPluginsEnabled,
168 prefs::kWebKitUsesUniversalDetector,
169 prefs::kWebKitSerifFontFamily,
170 prefs::kWebKitSansSerifFontFamily,
171 prefs::kWebKitFixedFontFamily,
172 prefs::kWebKitDefaultFontSize,
173 prefs::kWebKitDefaultFixedFontSize,
[email protected]1661a3c02010-05-20 16:32:36174 prefs::kWebkitTabsToLinks,
[email protected]420ae012009-04-24 05:16:32175 prefs::kDefaultCharset
176 // kWebKitStandardFontIsSerif needs to be added
177 // if we let users pick which font to use, serif or sans-serif when
178 // no font is specified or a CSS generic family (serif or sans-serif)
179 // is not specified.
180};
181
182const int kPrefsToObserveLength = arraysize(kPrefsToObserve);
183
[email protected]420ae012009-04-24 05:16:32184// Returns true if the entry's transition type is FORM_SUBMIT.
185bool IsFormSubmit(const NavigationEntry* entry) {
186 return (PageTransition::StripQualifier(entry->transition_type()) ==
187 PageTransition::FORM_SUBMIT);
188}
189
190#if defined(OS_WIN)
191
192BOOL CALLBACK InvalidateWindow(HWND hwnd, LPARAM lparam) {
193 // Note: erase is required to properly paint some widgets borders. This can
194 // be seen with textfields.
195 InvalidateRect(hwnd, NULL, TRUE);
196 return TRUE;
197}
198#endif
199
[email protected]5e369672009-11-03 23:48:30200ViewMsg_Navigate_Params::NavigationType GetNavigationType(
[email protected]1ccb3568d2010-02-19 10:51:16201 Profile* profile, const NavigationEntry& entry,
202 NavigationController::ReloadType reload_type) {
203 switch (reload_type) {
204 case NavigationController::RELOAD:
205 return ViewMsg_Navigate_Params::RELOAD;
206 case NavigationController::RELOAD_IGNORING_CACHE:
207 return ViewMsg_Navigate_Params::RELOAD_IGNORING_CACHE;
208 case NavigationController::NO_RELOAD:
209 break; // Fall through to rest of function.
210 }
[email protected]5e369672009-11-03 23:48:30211
212 if (entry.restore_type() == NavigationEntry::RESTORE_LAST_SESSION &&
213 profile->DidLastSessionExitCleanly())
214 return ViewMsg_Navigate_Params::RESTORE;
215
216 return ViewMsg_Navigate_Params::NORMAL;
217}
218
[email protected]3cc72b12010-03-18 23:03:00219void MakeNavigateParams(const NavigationController& controller,
[email protected]1ccb3568d2010-02-19 10:51:16220 NavigationController::ReloadType reload_type,
221 ViewMsg_Navigate_Params* params) {
[email protected]3cc72b12010-03-18 23:03:00222 const NavigationEntry& entry = *controller.pending_entry();
[email protected]056de2d2009-06-26 16:41:34223 params->page_id = entry.page_id();
[email protected]3cc72b12010-03-18 23:03:00224 params->pending_history_list_offset = controller.pending_entry_index();
225 params->current_history_list_offset = controller.last_committed_entry_index();
226 params->current_history_list_length = controller.entry_count();
[email protected]056de2d2009-06-26 16:41:34227 params->url = entry.url();
228 params->referrer = entry.referrer();
229 params->transition = entry.transition_type();
230 params->state = entry.content_state();
[email protected]3cc72b12010-03-18 23:03:00231 params->navigation_type =
232 GetNavigationType(controller.profile(), entry, reload_type);
[email protected]056de2d2009-06-26 16:41:34233 params->request_time = base::Time::Now();
234}
235
[email protected]420ae012009-04-24 05:16:32236} // namespace
237
238// -----------------------------------------------------------------------------
239
[email protected]fdd61c62009-04-22 19:22:57240// static
241int TabContents::find_request_id_counter_ = -1;
242
[email protected]420ae012009-04-24 05:16:32243
[email protected]420ae012009-04-24 05:16:32244TabContents::TabContents(Profile* profile,
245 SiteInstance* site_instance,
246 int routing_id,
[email protected]34ac70502009-09-25 17:07:23247 const TabContents* base_tab_contents)
[email protected]b680ad22009-04-15 23:19:42248 : delegate_(NULL),
[email protected]66ba4932009-06-04 19:22:13249 ALLOW_THIS_IN_INITIALIZER_LIST(controller_(this, profile)),
250 ALLOW_THIS_IN_INITIALIZER_LIST(view_(
[email protected]d82ed61e2009-06-16 02:46:22251 TabContentsView::Create(this))),
252 ALLOW_THIS_IN_INITIALIZER_LIST(render_manager_(this, this)),
[email protected]fdd61c62009-04-22 19:22:57253 property_bag_(),
254 registrar_(),
[email protected]12802702010-07-09 19:43:09255 ALLOW_THIS_IN_INITIALIZER_LIST(printing_(
256 new printing::PrintViewManager(*this))),
[email protected]fdd61c62009-04-22 19:22:57257 save_package_(),
[email protected]6db0def92010-03-02 19:55:45258 autocomplete_history_manager_(),
[email protected]21674df2009-10-28 19:51:21259 autofill_manager_(),
[email protected]fdd61c62009-04-22 19:22:57260 password_manager_(),
261 plugin_installer_(),
[email protected]6f821dd2010-02-23 00:54:06262 bookmark_drag_(NULL),
[email protected]d82ed61e2009-06-16 02:46:22263 ALLOW_THIS_IN_INITIALIZER_LIST(fav_icon_helper_(this)),
[email protected]fdd61c62009-04-22 19:22:57264 select_file_dialog_(),
[email protected]d5f942ba2008-09-26 19:30:34265 is_loading_(false),
initial.commit09911bf2008-07-26 23:55:29266 is_crashed_(false),
[email protected]d5f942ba2008-09-26 19:30:34267 waiting_for_response_(false),
[email protected]d5f942ba2008-09-26 19:30:34268 max_page_id_(-1),
[email protected]fdd61c62009-04-22 19:22:57269 current_load_start_(),
270 load_state_(net::LOAD_STATE_IDLE),
271 load_state_host_(),
[email protected]094e5b22009-09-25 04:23:56272 upload_size_(0),
273 upload_position_(0),
[email protected]fdd61c62009-04-22 19:22:57274 received_page_title_(false),
275 is_starred_(false),
276 contents_mime_type_(),
277 encoding_(),
[email protected]332af7732009-03-11 13:21:35278 blocked_popups_(NULL),
[email protected]8cc8d492010-02-02 10:40:49279 dont_notify_render_view_(false),
[email protected]f17a0ee2010-05-17 17:38:47280 displayed_insecure_content_(false),
[email protected]fdd61c62009-04-22 19:22:57281 infobar_delegates_(),
[email protected]fdd61c62009-04-22 19:22:57282 find_ui_active_(false),
283 find_op_aborted_(false),
284 current_find_request_id_(find_request_id_counter_++),
[email protected]e491f1c2009-05-22 20:28:12285 last_search_case_sensitive_(false),
[email protected]e491f1c2009-05-22 20:28:12286 last_search_result_(),
[email protected]98aa0b52010-05-06 17:03:08287 extension_app_(NULL),
[email protected]fdd61c62009-04-22 19:22:57288 capturing_contents_(false),
289 is_being_destroyed_(false),
290 notify_disconnection_(false),
291 history_requests_(),
292#if defined(OS_WIN)
293 message_box_active_(CreateEvent(NULL, TRUE, FALSE, NULL)),
294#endif
295 last_javascript_message_dismissal_(),
[email protected]c2e74fe82009-09-03 17:57:44296 suppress_javascript_messages_(false),
[email protected]7ab1e7d62009-10-14 23:32:01297 is_showing_before_unload_dialog_(false),
[email protected]7ff431e2010-01-07 18:18:54298 renderer_preferences_(),
[email protected]1a242c32010-02-02 21:20:54299 opener_dom_ui_type_(DOMUIFactory::kNoDOMUI),
[email protected]777a7b62010-05-11 13:28:16300 language_state_(&controller_),
[email protected]0cce15f2010-05-21 16:58:39301 closed_by_user_gesture_(false) {
[email protected]7ff431e2010-01-07 18:18:54302 renderer_preferences_util::UpdateFromSystemSettings(
303 &renderer_preferences_, profile);
304
[email protected]9fb83e82010-07-02 18:24:55305 content_settings_delegate_.reset(
306 new TabSpecificContentSettings(this, profile));
307
[email protected]16779842009-07-08 23:45:29308#if defined(OS_CHROMEOS)
[email protected]58dca552009-06-17 00:35:02309 // Make sure the thumbnailer is started before starting the render manager.
310 // The thumbnailer will want to listen for RVH creations, one of which will
311 // happen in RVHManager::Init.
312 ThumbnailGenerator* generator = g_browser_process->GetThumbnailGenerator();
313 if (generator)
314 generator->StartThumbnailing();
315#endif
316
[email protected]12636df2009-09-28 22:32:21317 render_manager_.Init(profile, site_instance, routing_id);
[email protected]420ae012009-04-24 05:16:32318
[email protected]34ac70502009-09-25 17:07:23319 // We have the initial size of the view be based on the size of the passed in
320 // tab contents (normally a tab from the same window).
321 view_->CreateView(base_tab_contents ?
322 base_tab_contents->view()->GetContainerSize() : gfx::Size());
[email protected]420ae012009-04-24 05:16:32323
324 // Register for notifications about all interested prefs change.
325 PrefService* prefs = profile->GetPrefs();
326 if (prefs) {
327 for (int i = 0; i < kPrefsToObserveLength; ++i)
328 prefs->AddPrefObserver(kPrefsToObserve[i], this);
329 }
330
331 // Register for notifications about URL starredness changing on any profile.
332 registrar_.Add(this, NotificationType::URLS_STARRED,
333 NotificationService::AllSources());
334 registrar_.Add(this, NotificationType::BOOKMARK_MODEL_LOADED,
335 NotificationService::AllSources());
336 registrar_.Add(this, NotificationType::RENDER_WIDGET_HOST_DESTROYED,
337 NotificationService::AllSources());
[email protected]d3ba77272009-09-03 00:06:09338#if defined(OS_LINUX)
339 registrar_.Add(this, NotificationType::BROWSER_THEME_CHANGED,
340 NotificationService::AllSources());
341#endif
[email protected]420ae012009-04-24 05:16:32342
[email protected]6c32ce72010-03-08 05:18:06343 registrar_.Add(this, NotificationType::USER_STYLE_SHEET_UPDATED,
344 NotificationService::AllSources());
345
[email protected]79580c62010-02-02 02:36:25346 // Register for notifications about content setting changes.
347 registrar_.Add(this, NotificationType::CONTENT_SETTINGS_CHANGED,
348 NotificationService::AllSources());
349
[email protected]a888b29e62010-04-01 13:38:57350 // Listen for extension changes so we can update extension_for_current_page_.
351 registrar_.Add(this, NotificationType::EXTENSION_LOADED,
352 NotificationService::AllSources());
353 registrar_.Add(this, NotificationType::EXTENSION_UNLOADED,
354 NotificationService::AllSources());
355 registrar_.Add(this, NotificationType::EXTENSION_UNLOADED_DISABLED,
356 NotificationService::AllSources());
357
[email protected]c8865482009-07-23 20:40:10358 // Set-up the showing of the omnibox search infobar if applicable.
359 if (OmniboxSearchHint::IsEnabled(profile))
360 omnibox_search_hint_.reset(new OmniboxSearchHint(this));
[email protected]332af7732009-03-11 13:21:35361}
initial.commit09911bf2008-07-26 23:55:29362
363TabContents::~TabContents() {
[email protected]420ae012009-04-24 05:16:32364 is_being_destroyed_ = true;
365
[email protected]bfe4c1582009-10-02 18:11:09366 // We don't want any notifications while we're running our destructor.
[email protected]420ae012009-04-24 05:16:32367 registrar_.RemoveAll();
368
369 // Unregister the notifications of all observed prefs change.
370 PrefService* prefs = profile()->GetPrefs();
371 if (prefs) {
372 for (int i = 0; i < kPrefsToObserveLength; ++i)
373 prefs->RemovePrefObserver(kPrefsToObserve[i], this);
374 }
375
[email protected]420ae012009-04-24 05:16:32376 NotifyDisconnected();
[email protected]e7b8d832009-11-20 22:41:00377 hung_renderer_dialog::HideForTabContents(this);
[email protected]420ae012009-04-24 05:16:32378
[email protected]420ae012009-04-24 05:16:32379 // First cleanly close all child windows.
380 // TODO(mpcomplete): handle case if MaybeCloseChildWindows() already asked
381 // some of these to close. CloseWindows is async, so it might get called
382 // twice before it runs.
[email protected]8cc8d492010-02-02 10:40:49383 CloseConstrainedWindows();
[email protected]420ae012009-04-24 05:16:32384
[email protected]8cc8d492010-02-02 10:40:49385 // Close all blocked popups.
[email protected]66ba4932009-06-04 19:22:13386 if (blocked_popups_)
387 blocked_popups_->Destroy();
388
[email protected]420ae012009-04-24 05:16:32389 // Notify any observer that have a reference on this tab contents.
390 NotificationService::current()->Notify(
391 NotificationType::TAB_CONTENTS_DESTROYED,
392 Source<TabContents>(this),
393 NotificationService::NoDetails());
394
[email protected]1e5e74f2009-05-27 20:55:12395 // Notify any lasting InfobarDelegates that have not yet been removed that
396 // whatever infobar they were handling in this TabContents has closed,
397 // because the TabContents is going away entirely.
398 // This must happen after the TAB_CONTENTS_DESTROYED notification as the
399 // notification may trigger infobars calls that access their delegate. (and
400 // some implementations of InfoBarDelegate do delete themselves on
401 // InfoBarClosed()).
402 for (int i = 0; i < infobar_delegate_count(); ++i) {
403 InfoBarDelegate* delegate = GetInfoBarDelegateAt(i);
404 delegate->InfoBarClosed();
405 }
406 infobar_delegates_.clear();
407
[email protected]420ae012009-04-24 05:16:32408 // TODO(brettw) this should be moved to the view.
409#if defined(OS_WIN)
410 // If we still have a window handle, destroy it. GetNativeView can return
411 // NULL if this contents was part of a window that closed.
412 if (GetNativeView())
413 ::DestroyWindow(GetNativeView());
414#endif
[email protected]7ab1e7d62009-10-14 23:32:01415
416 // OnCloseStarted isn't called in unit tests.
417 if (!tab_close_start_time_.is_null()) {
418 UMA_HISTOGRAM_TIMES("Tab.Close",
419 base::TimeTicks::Now() - tab_close_start_time_);
420 }
initial.commit09911bf2008-07-26 23:55:29421}
422
[email protected]d5f942ba2008-09-26 19:30:34423// static
424void TabContents::RegisterUserPrefs(PrefService* prefs) {
[email protected]420ae012009-04-24 05:16:32425 prefs->RegisterBooleanPref(prefs::kAlternateErrorPagesEnabled, true);
426
427 WebPreferences pref_defaults;
428 prefs->RegisterBooleanPref(prefs::kWebKitJavascriptEnabled,
429 pref_defaults.javascript_enabled);
430 prefs->RegisterBooleanPref(prefs::kWebKitWebSecurityEnabled,
431 pref_defaults.web_security_enabled);
432 prefs->RegisterBooleanPref(
433 prefs::kWebKitJavascriptCanOpenWindowsAutomatically, true);
434 prefs->RegisterBooleanPref(prefs::kWebKitLoadsImagesAutomatically,
435 pref_defaults.loads_images_automatically);
436 prefs->RegisterBooleanPref(prefs::kWebKitPluginsEnabled,
437 pref_defaults.plugins_enabled);
438 prefs->RegisterBooleanPref(prefs::kWebKitDomPasteEnabled,
439 pref_defaults.dom_paste_enabled);
440 prefs->RegisterBooleanPref(prefs::kWebKitShrinksStandaloneImagesToFit,
441 pref_defaults.shrinks_standalone_images_to_fit);
[email protected]c4e98902010-06-01 10:20:14442 prefs->RegisterDictionaryPref(prefs::kWebKitInspectorSettings);
[email protected]420ae012009-04-24 05:16:32443 prefs->RegisterBooleanPref(prefs::kWebKitTextAreasAreResizable,
444 pref_defaults.text_areas_are_resizable);
445 prefs->RegisterBooleanPref(prefs::kWebKitJavaEnabled,
446 pref_defaults.java_enabled);
[email protected]1661a3c02010-05-20 16:32:36447 prefs->RegisterBooleanPref(prefs::kWebkitTabsToLinks,
448 pref_defaults.tabs_to_links);
[email protected]420ae012009-04-24 05:16:32449
450 prefs->RegisterLocalizedStringPref(prefs::kAcceptLanguages,
451 IDS_ACCEPT_LANGUAGES);
452 prefs->RegisterLocalizedStringPref(prefs::kDefaultCharset,
453 IDS_DEFAULT_ENCODING);
454 prefs->RegisterLocalizedBooleanPref(prefs::kWebKitStandardFontIsSerif,
455 IDS_STANDARD_FONT_IS_SERIF);
456 prefs->RegisterLocalizedStringPref(prefs::kWebKitFixedFontFamily,
457 IDS_FIXED_FONT_FAMILY);
458 prefs->RegisterLocalizedStringPref(prefs::kWebKitSerifFontFamily,
459 IDS_SERIF_FONT_FAMILY);
460 prefs->RegisterLocalizedStringPref(prefs::kWebKitSansSerifFontFamily,
461 IDS_SANS_SERIF_FONT_FAMILY);
462 prefs->RegisterLocalizedStringPref(prefs::kWebKitCursiveFontFamily,
463 IDS_CURSIVE_FONT_FAMILY);
464 prefs->RegisterLocalizedStringPref(prefs::kWebKitFantasyFontFamily,
465 IDS_FANTASY_FONT_FAMILY);
466 prefs->RegisterLocalizedIntegerPref(prefs::kWebKitDefaultFontSize,
467 IDS_DEFAULT_FONT_SIZE);
468 prefs->RegisterLocalizedIntegerPref(prefs::kWebKitDefaultFixedFontSize,
469 IDS_DEFAULT_FIXED_FONT_SIZE);
470 prefs->RegisterLocalizedIntegerPref(prefs::kWebKitMinimumFontSize,
471 IDS_MINIMUM_FONT_SIZE);
472 prefs->RegisterLocalizedIntegerPref(prefs::kWebKitMinimumLogicalFontSize,
473 IDS_MINIMUM_LOGICAL_FONT_SIZE);
474 prefs->RegisterLocalizedBooleanPref(prefs::kWebKitUsesUniversalDetector,
475 IDS_USES_UNIVERSAL_DETECTOR);
476 prefs->RegisterLocalizedStringPref(prefs::kStaticEncodings,
477 IDS_STATIC_ENCODING_LIST);
initial.commit09911bf2008-07-26 23:55:29478}
479
[email protected]13367f72009-07-08 16:39:02480// Returns true if contains content rendered by an extension.
481bool TabContents::HostsExtension() const {
482 return GetURL().SchemeIs(chrome::kExtensionScheme);
[email protected]d5f942ba2008-09-26 19:30:34483}
484
[email protected]cf28ec52010-04-09 22:56:34485AutoFillManager* TabContents::GetAutoFillManager() {
486 if (autofill_manager_.get() == NULL)
487 autofill_manager_.reset(new AutoFillManager(this));
488 return autofill_manager_.get();
489}
490
[email protected]6e95706d2009-04-23 22:55:01491PasswordManager* TabContents::GetPasswordManager() {
492 if (password_manager_.get() == NULL)
493 password_manager_.reset(new PasswordManager(this));
494 return password_manager_.get();
495}
496
497PluginInstaller* TabContents::GetPluginInstaller() {
498 if (plugin_installer_.get() == NULL)
499 plugin_installer_.reset(new PluginInstaller(this));
500 return plugin_installer_.get();
501}
502
[email protected]8cb5d5b2010-02-09 11:36:16503RenderProcessHost* TabContents::GetRenderProcessHost() const {
504 return render_manager_.current_host()->process();
505}
506
[email protected]98aa0b52010-05-06 17:03:08507void TabContents::SetExtensionApp(Extension* extension) {
[email protected]867a73e12010-03-19 20:45:46508 DCHECK(!extension || extension->GetFullLaunchURL().is_valid());
[email protected]98aa0b52010-05-06 17:03:08509 extension_app_ = extension;
[email protected]fca656c2010-02-10 20:30:10510
[email protected]98aa0b52010-05-06 17:03:08511 UpdateExtensionAppIcon(extension_app_);
[email protected]a888b29e62010-04-01 13:38:57512
[email protected]fca656c2010-02-10 20:30:10513 NotificationService::current()->Notify(
514 NotificationType::TAB_CONTENTS_APPLICATION_EXTENSION_CHANGED,
515 Source<TabContents>(this),
516 NotificationService::NoDetails());
[email protected]3b355642010-02-05 16:01:49517}
518
[email protected]98aa0b52010-05-06 17:03:08519void TabContents::SetExtensionAppById(const std::string& extension_app_id) {
520 if (extension_app_id.empty())
[email protected]911f8152010-03-18 16:46:40521 return;
522
523 ExtensionsService* extension_service = profile()->GetExtensionsService();
524 if (extension_service && extension_service->is_ready()) {
525 Extension* extension =
[email protected]98aa0b52010-05-06 17:03:08526 extension_service->GetExtensionById(extension_app_id, false);
[email protected]911f8152010-03-18 16:46:40527 if (extension)
[email protected]98aa0b52010-05-06 17:03:08528 SetExtensionApp(extension);
[email protected]911f8152010-03-18 16:46:40529 }
530}
531
[email protected]98aa0b52010-05-06 17:03:08532SkBitmap* TabContents::GetExtensionAppIcon() {
[email protected]8eb043b2010-04-30 07:19:51533 // We don't show the big icons in tabs for TYPE_EXTENSION_APP windows because
534 // for those windows, we already have a big icon in the top-left outside any
535 // tab. Having big tab icons too looks kinda redonk.
[email protected]06821f42d2010-05-03 22:34:35536 Browser* browser = delegate_ ? delegate_->GetBrowser() : NULL;
537 if (browser && browser->type() == Browser::TYPE_EXTENSION_APP)
[email protected]8eb043b2010-04-30 07:19:51538 return NULL;
[email protected]8eb043b2010-04-30 07:19:51539
[email protected]98aa0b52010-05-06 17:03:08540 if (extension_app_icon_.empty())
[email protected]8eb043b2010-04-30 07:19:51541 return NULL;
542
[email protected]98aa0b52010-05-06 17:03:08543 return &extension_app_icon_;
[email protected]8eb043b2010-04-30 07:19:51544}
545
[email protected]d5f942ba2008-09-26 19:30:34546const GURL& TabContents::GetURL() const {
547 // We may not have a navigation entry yet
[email protected]ce3fa3c2009-04-20 19:55:57548 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]ebe89e062009-08-13 23:16:54549 return entry ? entry->virtual_url() : GURL::EmptyGURL();
[email protected]d5f942ba2008-09-26 19:30:34550}
551
[email protected]96d185d2009-04-24 03:28:54552const string16& TabContents::GetTitle() const {
[email protected]4c6092c5b2009-06-06 00:23:55553 // Transient entries take precedence. They are used for interstitial pages
554 // that are shown on top of existing pages.
555 NavigationEntry* entry = controller_.GetTransientEntry();
556 if (entry)
557 return entry->GetTitleForDisplay(&controller_);
558
[email protected]96d185d2009-04-24 03:28:54559 DOMUI* our_dom_ui = render_manager_.pending_dom_ui() ?
560 render_manager_.pending_dom_ui() : render_manager_.dom_ui();
561 if (our_dom_ui) {
562 // Don't override the title in view source mode.
[email protected]4c6092c5b2009-06-06 00:23:55563 entry = controller_.GetActiveEntry();
[email protected]96d185d2009-04-24 03:28:54564 if (!(entry && entry->IsViewSourceMode())) {
565 // Give the DOM UI the chance to override our title.
566 const string16& title = our_dom_ui->overridden_title();
567 if (!title.empty())
568 return title;
569 }
570 }
571
572 // We use the title for the last committed entry rather than a pending
573 // navigation entry. For example, when the user types in a URL, we want to
574 // keep the old page's title until the new load has committed and we get a new
575 // title.
[email protected]96d185d2009-04-24 03:28:54576 entry = controller_.GetLastCommittedEntry();
577 if (entry)
578 return entry->GetTitleForDisplay(&controller_);
[email protected]96d185d2009-04-24 03:28:54579 return EmptyString16();
580}
581
[email protected]bbbc1ef2010-02-12 18:03:41582// static
583string16 TabContents::GetDefaultTitle() {
584 return l10n_util::GetStringUTF16(IDS_DEFAULT_TAB_TITLE);
585}
586
[email protected]d5f942ba2008-09-26 19:30:34587int32 TabContents::GetMaxPageID() {
588 if (GetSiteInstance())
589 return GetSiteInstance()->max_page_id();
590 else
591 return max_page_id_;
592}
593
594void TabContents::UpdateMaxPageID(int32 page_id) {
595 // Ensure both the SiteInstance and RenderProcessHost update their max page
[email protected]57c6a652009-05-04 07:58:34596 // IDs in sync. Only TabContents will also have site instances, except during
[email protected]d5f942ba2008-09-26 19:30:34597 // testing.
598 if (GetSiteInstance())
599 GetSiteInstance()->UpdateMaxPageID(page_id);
[email protected]8cb5d5b2010-02-09 11:36:16600 GetRenderProcessHost()->UpdateMaxPageID(page_id);
[email protected]d5f942ba2008-09-26 19:30:34601}
602
[email protected]96d185d2009-04-24 03:28:54603SiteInstance* TabContents::GetSiteInstance() const {
604 return render_manager_.current_host()->site_instance();
605}
606
[email protected]96d185d2009-04-24 03:28:54607bool TabContents::ShouldDisplayURL() {
[email protected]8ab24cc2009-11-13 20:56:15608 // Don't hide the url in view source mode and with interstitials.
[email protected]96d185d2009-04-24 03:28:54609 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]8ab24cc2009-11-13 20:56:15610 if (entry && (entry->IsViewSourceMode() ||
611 entry->page_type() == NavigationEntry::INTERSTITIAL_PAGE)) {
[email protected]96d185d2009-04-24 03:28:54612 return true;
[email protected]8ab24cc2009-11-13 20:56:15613 }
[email protected]83e3895b2009-06-11 00:07:16614
[email protected]96d185d2009-04-24 03:28:54615 DOMUI* dom_ui = GetDOMUIForCurrentState();
616 if (dom_ui)
617 return !dom_ui->should_hide_url();
618 return true;
619}
620
[email protected]d5f942ba2008-09-26 19:30:34621SkBitmap TabContents::GetFavIcon() const {
622 // Like GetTitle(), we also want to use the favicon for the last committed
623 // entry rather than a pending navigation entry.
[email protected]ce3fa3c2009-04-20 19:55:57624 NavigationEntry* entry = controller_.GetTransientEntry();
[email protected]cbab76d2008-10-13 22:42:47625 if (entry)
626 return entry->favicon().bitmap();
627
[email protected]ce3fa3c2009-04-20 19:55:57628 entry = controller_.GetLastCommittedEntry();
[email protected]d5f942ba2008-09-26 19:30:34629 if (entry)
630 return entry->favicon().bitmap();
[email protected]d5f942ba2008-09-26 19:30:34631 return SkBitmap();
632}
633
[email protected]0b303cc2009-09-28 22:35:15634bool TabContents::FavIconIsValid() const {
635 NavigationEntry* entry = controller_.GetTransientEntry();
636 if (entry)
637 return entry->favicon().is_valid();
638
639 entry = controller_.GetLastCommittedEntry();
640 if (entry)
641 return entry->favicon().is_valid();
642
643 return false;
644}
645
[email protected]96d185d2009-04-24 03:28:54646bool TabContents::ShouldDisplayFavIcon() {
647 // Always display a throbber during pending loads.
648 if (controller_.GetLastCommittedEntry() && controller_.pending_entry())
649 return true;
650
651 DOMUI* dom_ui = GetDOMUIForCurrentState();
652 if (dom_ui)
653 return !dom_ui->hide_favicon();
654 return true;
655}
656
[email protected]96d185d2009-04-24 03:28:54657std::wstring TabContents::GetStatusText() const {
658 if (!is_loading() || load_state_ == net::LOAD_STATE_IDLE)
659 return std::wstring();
660
661 switch (load_state_) {
662 case net::LOAD_STATE_WAITING_FOR_CACHE:
663 return l10n_util::GetString(IDS_LOAD_STATE_WAITING_FOR_CACHE);
[email protected]8a1f3312010-05-25 19:25:04664 case net::LOAD_STATE_ESTABLISHING_PROXY_TUNNEL:
665 return l10n_util::GetString(IDS_LOAD_STATE_ESTABLISHING_PROXY_TUNNEL);
[email protected]96d185d2009-04-24 03:28:54666 case net::LOAD_STATE_RESOLVING_PROXY_FOR_URL:
667 return l10n_util::GetString(IDS_LOAD_STATE_RESOLVING_PROXY_FOR_URL);
668 case net::LOAD_STATE_RESOLVING_HOST:
669 return l10n_util::GetString(IDS_LOAD_STATE_RESOLVING_HOST);
670 case net::LOAD_STATE_CONNECTING:
671 return l10n_util::GetString(IDS_LOAD_STATE_CONNECTING);
[email protected]375e3112010-05-27 20:32:06672 case net::LOAD_STATE_SSL_HANDSHAKE:
673 return l10n_util::GetString(IDS_LOAD_STATE_SSL_HANDSHAKE);
[email protected]96d185d2009-04-24 03:28:54674 case net::LOAD_STATE_SENDING_REQUEST:
[email protected]094e5b22009-09-25 04:23:56675 if (upload_size_)
676 return l10n_util::GetStringF(
677 IDS_LOAD_STATE_SENDING_REQUEST_WITH_PROGRESS,
678 static_cast<int>((100 * upload_position_) / upload_size_));
679 else
680 return l10n_util::GetString(IDS_LOAD_STATE_SENDING_REQUEST);
[email protected]96d185d2009-04-24 03:28:54681 case net::LOAD_STATE_WAITING_FOR_RESPONSE:
682 return l10n_util::GetStringF(IDS_LOAD_STATE_WAITING_FOR_RESPONSE,
683 load_state_host_);
684 // Ignore net::LOAD_STATE_READING_RESPONSE and net::LOAD_STATE_IDLE
685 case net::LOAD_STATE_IDLE:
686 case net::LOAD_STATE_READING_RESPONSE:
687 break;
688 }
689
690 return std::wstring();
691}
692
[email protected]d5f942ba2008-09-26 19:30:34693void TabContents::SetIsCrashed(bool state) {
694 if (state == is_crashed_)
695 return;
696
697 is_crashed_ = state;
[email protected]c9cd2222009-05-06 05:16:50698 NotifyNavigationStateChanged(INVALIDATE_TAB);
[email protected]d5f942ba2008-09-26 19:30:34699}
700
[email protected]744ef172009-10-16 21:53:46701void TabContents::PageActionStateChanged() {
702 NotifyNavigationStateChanged(TabContents::INVALIDATE_PAGE_ACTIONS);
[email protected]d7eaf572009-07-01 21:57:00703}
[email protected]ce5c4502009-05-06 16:46:11704
[email protected]d5f942ba2008-09-26 19:30:34705void TabContents::NotifyNavigationStateChanged(unsigned changed_flags) {
706 if (delegate_)
707 delegate_->NavigationStateChanged(this, changed_flags);
708}
709
[email protected]96d185d2009-04-24 03:28:54710void TabContents::DidBecomeSelected() {
711 controller_.SetActive(true);
[email protected]8cb5d5b2010-02-09 11:36:16712 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
713 if (rwhv) {
714 rwhv->DidBecomeSelected();
[email protected]789e9152009-08-04 21:59:43715#if defined(OS_MACOSX)
[email protected]8cb5d5b2010-02-09 11:36:16716 rwhv->SetActive(true);
[email protected]789e9152009-08-04 21:59:43717#endif
718 }
[email protected]96d185d2009-04-24 03:28:54719
[email protected]8cb5d5b2010-02-09 11:36:16720 WebCacheManager::GetInstance()->ObserveActivity(GetRenderProcessHost()->id());
[email protected]96d185d2009-04-24 03:28:54721}
722
723void TabContents::WasHidden() {
724 if (!capturing_contents()) {
725 // |render_view_host()| can be NULL if the user middle clicks a link to open
726 // a tab in then background, then closes the tab before selecting it. This
727 // is because closing the tab calls TabContents::Destroy(), which removes
728 // the |render_view_host()|; then when we actually destroy the window,
729 // OnWindowPosChanged() notices and calls HideContents() (which calls us).
[email protected]8cb5d5b2010-02-09 11:36:16730 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
731 if (rwhv)
732 rwhv->WasHidden();
[email protected]96d185d2009-04-24 03:28:54733 }
734
735 NotificationService::current()->Notify(
736 NotificationType::TAB_CONTENTS_HIDDEN,
737 Source<TabContents>(this),
738 NotificationService::NoDetails());
739}
740
[email protected]d5f942ba2008-09-26 19:30:34741void TabContents::Activate() {
742 if (delegate_)
743 delegate_->ActivateContents(this);
744}
745
[email protected]96d185d2009-04-24 03:28:54746void TabContents::ShowContents() {
[email protected]8cb5d5b2010-02-09 11:36:16747 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
748 if (rwhv)
749 rwhv->DidBecomeSelected();
[email protected]96d185d2009-04-24 03:28:54750}
751
752void TabContents::HideContents() {
753 // TODO(pkasting): http://b/1239839 Right now we purposefully don't call
754 // our superclass HideContents(), because some callers want to be very picky
755 // about the order in which these get called. In addition to making the code
756 // here practically impossible to understand, this also means we end up
757 // calling TabContents::WasHidden() twice if callers call both versions of
[email protected]57c6a652009-05-04 07:58:34758 // HideContents() on a TabContents.
[email protected]96d185d2009-04-24 03:28:54759 WasHidden();
760}
761
[email protected]c0588052008-10-27 23:01:50762void TabContents::OpenURL(const GURL& url, const GURL& referrer,
[email protected]d5f942ba2008-09-26 19:30:34763 WindowOpenDisposition disposition,
764 PageTransition::Type transition) {
765 if (delegate_)
[email protected]c0588052008-10-27 23:01:50766 delegate_->OpenURLFromTab(this, url, referrer, disposition, transition);
[email protected]d5f942ba2008-09-26 19:30:34767}
768
[email protected]1ccb3568d2010-02-19 10:51:16769bool TabContents::NavigateToPendingEntry(
770 NavigationController::ReloadType reload_type) {
[email protected]96d185d2009-04-24 03:28:54771 const NavigationEntry& entry = *controller_.pending_entry();
772
773 RenderViewHost* dest_render_view_host = render_manager_.Navigate(entry);
774 if (!dest_render_view_host)
775 return false; // Unable to create the desired render view host.
776
[email protected]770dd8b2010-05-24 18:11:39777 if (delegate_ && delegate_->ShouldEnablePreferredSizeNotifications()) {
778 dest_render_view_host->EnablePreferredSizeChangedMode(
779 kPreferredSizeWidth | kPreferredSizeHeightThisIsSlow);
780 }
[email protected]9fb325e2010-05-06 18:23:24781
[email protected]0352e5472010-04-29 00:11:40782 // For security, we should never send non-DOM-UI URLs (other than about:blank)
783 // to a DOM UI renderer. Double check that here.
[email protected]54ec6472010-04-09 19:39:58784 int enabled_bindings = dest_render_view_host->enabled_bindings();
[email protected]0352e5472010-04-29 00:11:40785 bool is_allowed_in_dom_ui_renderer =
786 DOMUIFactory::UseDOMUIForURL(entry.url()) ||
787 entry.url() == GURL(chrome::kAboutBlankURL);
[email protected]54ec6472010-04-09 19:39:58788 CHECK(!BindingsPolicy::is_dom_ui_enabled(enabled_bindings) ||
[email protected]0352e5472010-04-29 00:11:40789 is_allowed_in_dom_ui_renderer);
[email protected]54ec6472010-04-09 19:39:58790
[email protected]96d185d2009-04-24 03:28:54791 // Tell DevTools agent that it is attached prior to the navigation.
[email protected]d9f9b792009-06-24 13:17:12792 DevToolsManager* devtools_manager = DevToolsManager::GetInstance();
[email protected]7e8e6b62009-05-08 11:28:32793 if (devtools_manager) { // NULL in unit tests.
[email protected]3cc72b12010-03-18 23:03:00794 devtools_manager->OnNavigatingToPendingEntry(render_view_host(),
795 dest_render_view_host,
796 entry.url());
[email protected]7e8e6b62009-05-08 11:28:32797 }
[email protected]96d185d2009-04-24 03:28:54798
799 // Used for page load time metrics.
800 current_load_start_ = base::TimeTicks::Now();
801
802 // Navigate in the desired RenderViewHost.
[email protected]056de2d2009-06-26 16:41:34803 ViewMsg_Navigate_Params navigate_params;
[email protected]3cc72b12010-03-18 23:03:00804 MakeNavigateParams(controller_, reload_type, &navigate_params);
[email protected]056de2d2009-06-26 16:41:34805 dest_render_view_host->Navigate(navigate_params);
[email protected]96d185d2009-04-24 03:28:54806
807 if (entry.page_id() == -1) {
808 // HACK!! This code suppresses javascript: URLs from being added to
809 // session history, which is what we want to do for javascript: URLs that
810 // do not generate content. What we really need is a message from the
811 // renderer telling us that a new page was not created. The same message
812 // could be used for mailto: URLs and the like.
813 if (entry.url().SchemeIs(chrome::kJavaScriptScheme))
814 return false;
815 }
816
817 // Clear any provisional password saves - this stops password infobars
818 // showing up on pages the user navigates to while the right page is
819 // loading.
820 GetPasswordManager()->ClearProvisionalSave();
821
[email protected]1ccb3568d2010-02-19 10:51:16822 if (reload_type != NavigationController::NO_RELOAD &&
823 !profile()->IsOffTheRecord()) {
[email protected]0189bc722009-08-28 21:56:48824 FaviconService* favicon_service =
825 profile()->GetFaviconService(Profile::IMPLICIT_ACCESS);
826 if (favicon_service)
827 favicon_service->SetFaviconOutOfDateForPage(entry.url());
[email protected]96d185d2009-04-24 03:28:54828 }
829
[email protected]d5f942ba2008-09-26 19:30:34830 return true;
831}
832
[email protected]96d185d2009-04-24 03:28:54833void TabContents::Stop() {
834 render_manager_.Stop();
[email protected]12802702010-07-09 19:43:09835 printing_->Stop();
[email protected]96d185d2009-04-24 03:28:54836}
837
[email protected]96d185d2009-04-24 03:28:54838void TabContents::DisassociateFromPopupCount() {
839 render_view_host()->DisassociateFromPopupCount();
840}
841
842TabContents* TabContents::Clone() {
843 // We create a new SiteInstance so that the new tab won't share processes
844 // with the old one. This can be changed in the future if we need it to share
845 // processes for some reason.
[email protected]420ae012009-04-24 05:16:32846 TabContents* tc = new TabContents(profile(),
[email protected]96d185d2009-04-24 03:28:54847 SiteInstance::CreateSiteInstance(profile()),
[email protected]12636df2009-09-28 22:32:21848 MSG_ROUTING_NONE, this);
[email protected]96d185d2009-04-24 03:28:54849 tc->controller().CopyStateFrom(controller_);
[email protected]98aa0b52010-05-06 17:03:08850 tc->extension_app_ = extension_app_;
[email protected]98aa0b52010-05-06 17:03:08851 tc->extension_app_icon_ = extension_app_icon_;
[email protected]96d185d2009-04-24 03:28:54852 return tc;
853}
854
[email protected]4d677202009-07-19 07:37:12855void TabContents::ShowPageInfo(const GURL& url,
856 const NavigationEntry::SSLStatus& ssl,
857 bool show_history) {
858 if (!delegate_)
859 return;
860
[email protected]bb678332009-07-21 00:15:50861 delegate_->ShowPageInfo(profile(), url, ssl, show_history);
[email protected]4d677202009-07-19 07:37:12862}
863
initial.commit09911bf2008-07-26 23:55:29864ConstrainedWindow* TabContents::CreateConstrainedDialog(
[email protected]e8382172009-06-19 22:16:28865 ConstrainedWindowDelegate* delegate) {
initial.commit09911bf2008-07-26 23:55:29866 ConstrainedWindow* window =
[email protected]e8382172009-06-19 22:16:28867 ConstrainedWindow::CreateConstrainedDialog(this, delegate);
initial.commit09911bf2008-07-26 23:55:29868 child_windows_.push_back(window);
[email protected]fa1cf0b82010-01-15 21:49:44869
870 if (child_windows_.size() == 1) {
871 window->ShowConstrainedWindow();
872 BlockTabContent(true);
873 }
874
initial.commit09911bf2008-07-26 23:55:29875 return window;
876}
877
[email protected]fa1cf0b82010-01-15 21:49:44878void TabContents::BlockTabContent(bool blocked) {
[email protected]8dccd7cb2010-02-25 05:19:55879 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
880 if (rwhv)
881 rwhv->SetVisuallyDeemphasized(blocked);
[email protected]fa1cf0b82010-01-15 21:49:44882 render_view_host()->set_ignore_input_events(blocked);
883 if (delegate_)
884 delegate_->SetTabContentBlocked(this, blocked);
885}
886
initial.commit09911bf2008-07-26 23:55:29887void TabContents::AddNewContents(TabContents* new_contents,
888 WindowOpenDisposition disposition,
889 const gfx::Rect& initial_pos,
[email protected]7e7414ae2010-01-26 20:19:29890 bool user_gesture) {
initial.commit09911bf2008-07-26 23:55:29891 if (!delegate_)
892 return;
893
[email protected]35f7d212009-04-29 21:19:27894 if ((disposition == NEW_POPUP) && !user_gesture &&
895 !CommandLine::ForCurrentProcess()->HasSwitch(
896 switches::kDisablePopupBlocking)) {
[email protected]2c4410d2009-05-06 23:46:22897 // Unrequested popups from normal pages are constrained unless they're in
[email protected]b6c874582009-05-08 19:38:31898 // the whitelist. The popup owner will handle checking this.
[email protected]7e7414ae2010-01-26 20:19:29899 delegate_->GetConstrainingContents(this)->AddPopup(
900 new_contents, initial_pos);
[email protected]b6c874582009-05-08 19:38:31901 } else {
[email protected]304f8792009-08-19 21:28:09902#if defined(OS_CHROMEOS)
903 if (disposition == NEW_POPUP) {
904 // If the popup is bigger than a given factor of the screen, then
905 // turn it into a foreground tab (on chrome os only)
[email protected]8323b082010-07-16 16:56:11906 // Also check for width or height == 0, which would otherwise indicate
907 // a tab sized popup window.
[email protected]304f8792009-08-19 21:28:09908 GdkScreen* screen = gdk_screen_get_default();
909 int max_width = gdk_screen_get_width(screen) * kMaxWidthFactor;
910 int max_height = gdk_screen_get_height(screen) * kMaxHeightFactor;
[email protected]8323b082010-07-16 16:56:11911 if (initial_pos.width() > max_width || initial_pos.width() == 0 ||
912 initial_pos.height() > max_height || initial_pos.height() == 0) {
[email protected]304f8792009-08-19 21:28:09913 disposition = NEW_FOREGROUND_TAB;
914 }
915 }
916#endif
917
[email protected]0aa55312008-10-17 21:53:08918 new_contents->DisassociateFromPopupCount();
initial.commit09911bf2008-07-26 23:55:29919 delegate_->AddNewContents(this, new_contents, disposition, initial_pos,
920 user_gesture);
[email protected]7f37dd92009-09-15 16:35:49921 NotificationService::current()->Notify(
922 NotificationType::TAB_ADDED,
923 Source<TabContentsDelegate>(delegate_),
924 Details<TabContents>(this));
initial.commit09911bf2008-07-26 23:55:29925 }
initial.commit09911bf2008-07-26 23:55:29926
[email protected]8cc8d492010-02-02 10:40:49927 // TODO(pkasting): Why is this necessary?
928 PopupNotificationVisibilityChanged(blocked_popups_ != NULL);
initial.commit09911bf2008-07-26 23:55:29929}
930
[email protected]bcd9dc42010-01-23 16:38:18931bool TabContents::ExecuteCode(int request_id, const std::string& extension_id,
[email protected]61f5a7b2009-12-22 22:21:20932 const std::vector<URLPattern>& host_permissions,
[email protected]20ad2692009-11-20 18:27:20933 bool is_js_code, const std::string& code_string,
934 bool all_frames) {
[email protected]ebd0eae72010-02-16 23:50:51935 RenderViewHost* host = render_view_host();
936 if (!host)
937 return false;
938
939 return host->Send(new ViewMsg_ExecuteCode(host->routing_id(),
940 ViewMsg_ExecuteCode_Params(request_id, extension_id, host_permissions,
941 is_js_code, code_string, all_frames)));
[email protected]912256b32009-09-18 09:47:35942}
943
[email protected]96d185d2009-04-24 03:28:54944void TabContents::PopupNotificationVisibilityChanged(bool visible) {
[email protected]8cc8d492010-02-02 10:40:49945 if (is_being_destroyed_)
946 return;
[email protected]9fb83e82010-07-02 18:24:55947 content_settings_delegate_->SetPopupsBlocked(visible);
[email protected]8cc8d492010-02-02 10:40:49948 if (!dont_notify_render_view_)
[email protected]e8345242010-05-06 03:00:40949 render_view_host()->AllowScriptToClose(!visible);
[email protected]96d185d2009-04-24 03:28:54950}
951
[email protected]5c9e97a2009-09-09 23:48:30952gfx::NativeView TabContents::GetContentNativeView() const {
[email protected]96d185d2009-04-24 03:28:54953 return view_->GetContentNativeView();
954}
955
956gfx::NativeView TabContents::GetNativeView() const {
957 return view_->GetNativeView();
958}
959
960void TabContents::GetContainerBounds(gfx::Rect *out) const {
961 view_->GetContainerBounds(out);
962}
963
964void TabContents::Focus() {
965 view_->Focus();
966}
967
[email protected]90daadb42009-06-08 21:27:28968void TabContents::FocusThroughTabTraversal(bool reverse) {
[email protected]7e383692009-06-12 19:14:54969 if (showing_interstitial_page()) {
970 render_manager_.interstitial_page()->FocusThroughTabTraversal(reverse);
971 return;
972 }
[email protected]96d185d2009-04-24 03:28:54973 render_view_host()->SetInitialFocus(reverse);
974}
975
976bool TabContents::FocusLocationBarByDefault() {
977 DOMUI* dom_ui = GetDOMUIForCurrentState();
978 if (dom_ui)
979 return dom_ui->focus_location_bar_by_default();
[email protected]3e3f0eb2009-06-22 18:33:43980 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]e0d481582009-09-15 21:06:25981 if (entry && entry->url() == GURL(chrome::kAboutBlankURL))
[email protected]3e3f0eb2009-06-22 18:33:43982 return true;
[email protected]96d185d2009-04-24 03:28:54983 return false;
984}
985
[email protected]a26dc362010-04-23 01:48:58986void TabContents::SetFocusToLocationBar(bool select_all) {
[email protected]a11aaf22010-03-30 00:03:38987 if (delegate())
[email protected]a26dc362010-04-23 01:48:58988 delegate()->SetFocusToLocationBar(select_all);
[email protected]a11aaf22010-03-30 00:03:38989}
990
[email protected]616ed5a2008-11-21 22:27:24991void TabContents::AddInfoBar(InfoBarDelegate* delegate) {
[email protected]f7019302010-03-26 19:58:32992 if (delegate_ && !delegate_->infobars_enabled()) {
993 delegate->InfoBarClosed();
994 return;
995 }
996
[email protected]616ed5a2008-11-21 22:27:24997 // Look through the existing InfoBarDelegates we have for a match. If we've
998 // already got one that matches, then we don't add the new one.
[email protected]f86a07022008-11-25 01:06:05999 for (int i = 0; i < infobar_delegate_count(); ++i) {
[email protected]6d27a7b2009-12-18 23:25:451000 if (GetInfoBarDelegateAt(i)->EqualsDelegate(delegate)) {
1001 // Tell the new infobar to close so that it can clean itself up.
1002 delegate->InfoBarClosed();
[email protected]616ed5a2008-11-21 22:27:241003 return;
[email protected]6d27a7b2009-12-18 23:25:451004 }
[email protected]616ed5a2008-11-21 22:27:241005 }
1006
1007 infobar_delegates_.push_back(delegate);
[email protected]bfd04a62009-02-01 18:16:561008 NotificationService::current()->Notify(
1009 NotificationType::TAB_CONTENTS_INFOBAR_ADDED,
1010 Source<TabContents>(this),
1011 Details<InfoBarDelegate>(delegate));
[email protected]616ed5a2008-11-21 22:27:241012
1013 // Add ourselves as an observer for navigations the first time a delegate is
1014 // added. We use this notification to expire InfoBars that need to expire on
1015 // page transitions.
1016 if (infobar_delegates_.size() == 1) {
[email protected]bfd04a62009-02-01 18:16:561017 registrar_.Add(this, NotificationType::NAV_ENTRY_COMMITTED,
[email protected]ce3fa3c2009-04-20 19:55:571018 Source<NavigationController>(&controller_));
[email protected]616ed5a2008-11-21 22:27:241019 }
1020}
1021
1022void TabContents::RemoveInfoBar(InfoBarDelegate* delegate) {
[email protected]f7019302010-03-26 19:58:321023 if (delegate_ && !delegate_->infobars_enabled()) {
1024 return;
1025 }
1026
[email protected]616ed5a2008-11-21 22:27:241027 std::vector<InfoBarDelegate*>::iterator it =
1028 find(infobar_delegates_.begin(), infobar_delegates_.end(), delegate);
1029 if (it != infobar_delegates_.end()) {
1030 InfoBarDelegate* delegate = *it;
[email protected]bfd04a62009-02-01 18:16:561031 NotificationService::current()->Notify(
1032 NotificationType::TAB_CONTENTS_INFOBAR_REMOVED,
1033 Source<TabContents>(this),
1034 Details<InfoBarDelegate>(delegate));
[email protected]f86a07022008-11-25 01:06:051035 infobar_delegates_.erase(it);
[email protected]616ed5a2008-11-21 22:27:241036
[email protected]6a02963e2009-01-06 16:58:031037 // Remove ourselves as an observer if we are tracking no more InfoBars.
1038 if (infobar_delegates_.empty()) {
[email protected]bfd04a62009-02-01 18:16:561039 registrar_.Remove(this, NotificationType::NAV_ENTRY_COMMITTED,
[email protected]ce3fa3c2009-04-20 19:55:571040 Source<NavigationController>(&controller_));
[email protected]6a02963e2009-01-06 16:58:031041 }
[email protected]616ed5a2008-11-21 22:27:241042 }
1043}
[email protected]3edd9522009-03-04 22:19:411044
[email protected]f81821c2009-08-07 01:29:471045void TabContents::ReplaceInfoBar(InfoBarDelegate* old_delegate,
1046 InfoBarDelegate* new_delegate) {
[email protected]f7019302010-03-26 19:58:321047 if (delegate_ && !delegate_->infobars_enabled()) {
1048 new_delegate->InfoBarClosed();
1049 return;
1050 }
1051
[email protected]f81821c2009-08-07 01:29:471052 std::vector<InfoBarDelegate*>::iterator it =
1053 find(infobar_delegates_.begin(), infobar_delegates_.end(), old_delegate);
1054 DCHECK(it != infobar_delegates_.end());
1055
1056 // Notify the container about the change of plans.
1057 scoped_ptr<std::pair<InfoBarDelegate*, InfoBarDelegate*> > details(
1058 new std::pair<InfoBarDelegate*, InfoBarDelegate*>(
1059 old_delegate, new_delegate));
1060 NotificationService::current()->Notify(
1061 NotificationType::TAB_CONTENTS_INFOBAR_REPLACED,
1062 Source<TabContents>(this),
1063 Details<std::pair<InfoBarDelegate*, InfoBarDelegate*> >(details.get()));
1064
1065 // Remove the old one.
1066 infobar_delegates_.erase(it);
1067
1068 // Add the new one.
1069 infobar_delegates_.push_back(new_delegate);
1070}
1071
[email protected]20ede072009-10-06 00:28:351072bool TabContents::ShouldShowBookmarkBar() {
[email protected]aa9196f2009-10-22 16:20:271073 if (showing_interstitial_page())
1074 return false;
1075
[email protected]96d185d2009-04-24 03:28:541076 // See GetDOMUIForCurrentState() comment for more info. This case is very
1077 // similar, but for non-first loads, we want to use the committed entry. This
1078 // is so the bookmarks bar disappears at the same time the page does.
1079 if (controller_.GetLastCommittedEntry()) {
1080 // Not the first load, always use the committed DOM UI.
1081 if (render_manager_.dom_ui())
1082 return render_manager_.dom_ui()->force_bookmark_bar_visible();
1083 return false; // Default.
1084 }
1085
1086 // When it's the first load, we know either the pending one or the committed
1087 // one will have the DOM UI in it (see GetDOMUIForCurrentState), and only one
1088 // of them will be valid, so we can just check both.
1089 if (render_manager_.pending_dom_ui())
1090 return render_manager_.pending_dom_ui()->force_bookmark_bar_visible();
1091 if (render_manager_.dom_ui())
1092 return render_manager_.dom_ui()->force_bookmark_bar_visible();
1093 return false; // Default.
1094}
1095
[email protected]d8f16ae2009-08-30 03:47:261096bool TabContents::IsExtensionShelfAlwaysVisible() {
1097 // See GetDOMUIForCurrentState() comment for more info. This case is very
1098 // similar, but for non-first loads, we want to use the committed entry. This
1099 // is so the bookmarks bar disappears at the same time the page does.
1100 if (controller_.GetLastCommittedEntry()) {
1101 // Not the first load, always use the committed DOM UI.
1102 if (render_manager_.dom_ui())
1103 return render_manager_.dom_ui()->force_extension_shelf_visible();
1104 return false; // Default.
1105 }
1106
1107 // When it's the first load, we know either the pending one or the committed
1108 // one will have the DOM UI in it (see GetDOMUIForCurrentState), and only one
1109 // of them will be valid, so we can just check both.
1110 if (render_manager_.pending_dom_ui())
1111 return render_manager_.pending_dom_ui()->force_extension_shelf_visible();
1112 if (render_manager_.dom_ui())
1113 return render_manager_.dom_ui()->force_extension_shelf_visible();
1114 return false; // Default.
1115}
1116
[email protected]3edd9522009-03-04 22:19:411117void TabContents::ToolbarSizeChanged(bool is_animating) {
1118 TabContentsDelegate* d = delegate();
1119 if (d)
1120 d->ToolbarSizeChanged(this, is_animating);
1121}
[email protected]616ed5a2008-11-21 22:27:241122
[email protected]fc6fb7fb2009-11-07 02:35:041123bool TabContents::CanDownload(int request_id) {
1124 TabContentsDelegate* d = delegate();
1125 if (d)
1126 return d->CanDownload(request_id);
1127 return true;
1128}
1129
[email protected]d5f942ba2008-09-26 19:30:341130void TabContents::OnStartDownload(DownloadItem* download) {
1131 DCHECK(download);
[email protected]d5f942ba2008-09-26 19:30:341132
1133 // Download in a constrained popup is shown in the tab that opened it.
[email protected]b6c874582009-05-08 19:38:311134 TabContents* tab_contents = delegate()->GetConstrainingContents(this);
[email protected]d5f942ba2008-09-26 19:30:341135
[email protected]c70f9b82010-04-21 07:31:111136 if (tab_contents && tab_contents->delegate()) {
[email protected]59560e0b2009-06-04 03:30:221137 tab_contents->delegate()->OnStartDownload(download);
[email protected]c70f9b82010-04-21 07:31:111138 // If the download occurs in a new tab, close it
1139 if (controller_.IsInitialNavigation() && (tab_contents == this))
1140 delegate()->CloseContents(this);
1141 }
[email protected]3edd9522009-03-04 22:19:411142}
[email protected]3edd9522009-03-04 22:19:411143
[email protected]d5f942ba2008-09-26 19:30:341144void TabContents::WillClose(ConstrainedWindow* window) {
1145 ConstrainedWindowList::iterator it =
1146 find(child_windows_.begin(), child_windows_.end(), window);
[email protected]fa1cf0b82010-01-15 21:49:441147 bool removed_topmost_window = it == child_windows_.begin();
[email protected]d5f942ba2008-09-26 19:30:341148 if (it != child_windows_.end())
1149 child_windows_.erase(it);
[email protected]55098792010-03-24 18:20:371150 if (child_windows_.size() > 0) {
1151 if (removed_topmost_window) {
1152 child_windows_[0]->ShowConstrainedWindow();
1153 }
[email protected]fa1cf0b82010-01-15 21:49:441154 BlockTabContent(true);
1155 } else {
1156 BlockTabContent(false);
1157 }
[email protected]66ba4932009-06-04 19:22:131158}
[email protected]d5f942ba2008-09-26 19:30:341159
[email protected]66ba4932009-06-04 19:22:131160void TabContents::WillCloseBlockedPopupContainer(
1161 BlockedPopupContainer* container) {
1162 DCHECK(blocked_popups_ == container);
1163 blocked_popups_ = NULL;
[email protected]8cc8d492010-02-02 10:40:491164 PopupNotificationVisibilityChanged(false);
[email protected]d5f942ba2008-09-26 19:30:341165}
1166
[email protected]d5f942ba2008-09-26 19:30:341167void TabContents::DidMoveOrResize(ConstrainedWindow* window) {
[email protected]332af7732009-03-11 13:21:351168#if defined(OS_WIN)
[email protected]92edc472009-02-10 20:32:061169 UpdateWindow(GetNativeView());
[email protected]332af7732009-03-11 13:21:351170#endif
[email protected]d5f942ba2008-09-26 19:30:341171}
1172
[email protected]c1dc9f132009-11-20 23:13:211173void TabContents::StartFinding(string16 search_string,
[email protected]e491f1c2009-05-22 20:28:121174 bool forward_direction,
1175 bool case_sensitive) {
[email protected]c1dc9f132009-11-20 23:13:211176 // If search_string is empty, it means FindNext was pressed with a keyboard
[email protected]6e95706d2009-04-23 22:55:011177 // shortcut so unless we have something to search for we return early.
[email protected]c1dc9f132009-11-20 23:13:211178 if (search_string.empty() && find_text_.empty()) {
[email protected]e5d2f7132010-04-09 18:13:531179 string16 last_search_prepopulate_text =
1180 FindBarState::GetLastPrepopulateText(profile());
1181
[email protected]87fae512009-12-11 02:20:211182 // Try the last thing we searched for on this tab, then the last thing
1183 // searched for on any tab.
1184 if (!previous_find_text_.empty())
1185 search_string = previous_find_text_;
[email protected]e5d2f7132010-04-09 18:13:531186 else if (!last_search_prepopulate_text.empty())
1187 search_string = last_search_prepopulate_text;
[email protected]87fae512009-12-11 02:20:211188 else
[email protected]e491f1c2009-05-22 20:28:121189 return;
[email protected]e491f1c2009-05-22 20:28:121190 }
[email protected]6e95706d2009-04-23 22:55:011191
[email protected]101c90e2009-10-26 21:14:441192 // Keep track of the previous search.
1193 previous_find_text_ = find_text_;
1194
[email protected]6e95706d2009-04-23 22:55:011195 // This is a FindNext operation if we are searching for the same text again,
1196 // or if the passed in search text is empty (FindNext keyboard shortcut). The
1197 // exception to this is if the Find was aborted (then we don't want FindNext
1198 // because the highlighting has been cleared and we need it to reappear). We
1199 // therefore treat FindNext after an aborted Find operation as a full fledged
1200 // Find.
[email protected]c1dc9f132009-11-20 23:13:211201 bool find_next = (find_text_ == search_string || search_string.empty()) &&
[email protected]e491f1c2009-05-22 20:28:121202 (last_search_case_sensitive_ == case_sensitive) &&
[email protected]6e95706d2009-04-23 22:55:011203 !find_op_aborted_;
1204 if (!find_next)
1205 current_find_request_id_ = find_request_id_counter_++;
1206
[email protected]c1dc9f132009-11-20 23:13:211207 if (!search_string.empty())
1208 find_text_ = search_string;
[email protected]e491f1c2009-05-22 20:28:121209 last_search_case_sensitive_ = case_sensitive;
[email protected]6e95706d2009-04-23 22:55:011210
1211 find_op_aborted_ = false;
1212
1213 // Keep track of what the last search was across the tabs.
[email protected]e5d2f7132010-04-09 18:13:531214 FindBarState* find_bar_state = profile()->GetFindBarState();
1215 find_bar_state->set_last_prepopulate_text(find_text_);
[email protected]6e95706d2009-04-23 22:55:011216 render_view_host()->StartFinding(current_find_request_id_,
1217 find_text_,
1218 forward_direction,
[email protected]e491f1c2009-05-22 20:28:121219 case_sensitive,
[email protected]6e95706d2009-04-23 22:55:011220 find_next);
1221}
1222
[email protected]5bc8fe92010-03-11 18:19:001223void TabContents::StopFinding(
1224 FindBarController::SelectionAction selection_action) {
[email protected]d6e8d332010-06-04 22:26:011225 if (selection_action == FindBarController::kClearSelection) {
1226 // kClearSelection means the find string has been cleared by the user, but
1227 // the UI has not been dismissed. In that case we want to clear the
1228 // previously remembered search (http://crbug.com/42639).
1229 previous_find_text_ = string16();
1230 } else {
[email protected]2d2dffc2009-06-09 21:41:001231 find_ui_active_ = false;
[email protected]d6e8d332010-06-04 22:26:011232 if (!find_text_.empty())
1233 previous_find_text_ = find_text_;
1234 }
[email protected]101c90e2009-10-26 21:14:441235 find_text_.clear();
[email protected]6e95706d2009-04-23 22:55:011236 find_op_aborted_ = true;
[email protected]e491f1c2009-05-22 20:28:121237 last_search_result_ = FindNotificationDetails();
[email protected]5bc8fe92010-03-11 18:19:001238 render_view_host()->StopFinding(selection_action);
[email protected]6e95706d2009-04-23 22:55:011239}
1240
[email protected]420ae012009-04-24 05:16:321241void TabContents::OnSavePage() {
1242 // If we can not save the page, try to download it.
1243 if (!SavePackage::IsSavableContents(contents_mime_type())) {
1244 DownloadManager* dlm = profile()->GetDownloadManager();
1245 const GURL& current_page_url = GetURL();
1246 if (dlm && current_page_url.is_valid())
[email protected]d82ed61e2009-06-16 02:46:221247 dlm->DownloadUrl(current_page_url, GURL(), "", this);
[email protected]420ae012009-04-24 05:16:321248 return;
1249 }
1250
1251 Stop();
1252
1253 // Create the save package and possibly prompt the user for the name to save
1254 // the page as. The user prompt is an asynchronous operation that runs on
1255 // another thread.
[email protected]d82ed61e2009-06-16 02:46:221256 save_package_ = new SavePackage(this);
[email protected]420ae012009-04-24 05:16:321257 save_package_->GetSaveInfo();
1258}
1259
1260// Used in automated testing to bypass prompting the user for file names.
1261// Instead, the names and paths are hard coded rather than running them through
1262// file name sanitation and extension / mime checking.
[email protected]828cabe2009-09-26 22:47:111263bool TabContents::SavePage(const FilePath& main_file, const FilePath& dir_path,
[email protected]420ae012009-04-24 05:16:321264 SavePackage::SavePackageType save_type) {
1265 // Stop the page from navigating.
1266 Stop();
1267
[email protected]828cabe2009-09-26 22:47:111268 save_package_ = new SavePackage(this, save_type, main_file, dir_path);
[email protected]cdf6cc82009-08-04 21:48:591269 return save_package_->Init();
[email protected]420ae012009-04-24 05:16:321270}
1271
[email protected]89d6e6e2009-12-03 23:06:511272void TabContents::EmailPageLocation() {
1273 std::string title = EscapeQueryParamValue(UTF16ToUTF8(GetTitle()), false);
1274 std::string page_url = EscapeQueryParamValue(GetURL().spec(), false);
1275 std::string mailto = std::string("mailto:?subject=Fwd:%20") +
1276 title + "&body=%0A%0A" + page_url;
1277 platform_util::OpenExternal(GURL(mailto));
1278}
1279
[email protected]420ae012009-04-24 05:16:321280void TabContents::PrintPreview() {
1281 // We don't show the print preview yet, only the print dialog.
1282 PrintNow();
1283}
1284
1285bool TabContents::PrintNow() {
1286 // We can't print interstitial page for now.
1287 if (showing_interstitial_page())
1288 return false;
1289
1290 return render_view_host()->PrintPages();
1291}
1292
[email protected]82270452009-06-19 15:58:011293void TabContents::PrintingDone(int document_cookie, bool success) {
1294 render_view_host()->PrintingDone(document_cookie, success);
1295}
1296
[email protected]420ae012009-04-24 05:16:321297bool TabContents::IsActiveEntry(int32 page_id) {
1298 NavigationEntry* active_entry = controller_.GetActiveEntry();
1299 return (active_entry != NULL &&
1300 active_entry->site_instance() == GetSiteInstance() &&
1301 active_entry->page_id() == page_id);
1302}
1303
[email protected]b2fe07d12010-02-09 14:38:081304void TabContents::SetOverrideEncoding(const std::string& encoding) {
[email protected]8cb5d5b2010-02-09 11:36:161305 set_encoding(encoding);
1306 render_view_host()->SetPageEncoding(encoding);
1307}
1308
[email protected]b2fe07d12010-02-09 14:38:081309void TabContents::ResetOverrideEncoding() {
[email protected]8cb5d5b2010-02-09 11:36:161310 reset_encoding();
1311 render_view_host()->ResetPageEncodingToDefault();
1312}
1313
1314void TabContents::WindowMoveOrResizeStarted() {
1315 render_view_host()->WindowMoveOrResizeStarted();
1316}
1317
[email protected]3f8eb7f2009-10-02 23:58:051318void TabContents::LogNewTabTime(const std::string& event_name) {
1319 // Not all new tab pages get timed. In those cases, we don't have a
1320 // new_tab_start_time_.
1321 if (new_tab_start_time_.is_null())
1322 return;
1323
1324 base::TimeDelta duration = base::TimeTicks::Now() - new_tab_start_time_;
[email protected]84abba62009-10-07 17:01:441325 MetricEventDurationDetails details(event_name,
1326 static_cast<int>(duration.InMilliseconds()));
1327
[email protected]7ab1e7d62009-10-14 23:32:011328 if (event_name == "Tab.NewTabScriptStart") {
1329 UMA_HISTOGRAM_TIMES("Tab.NewTabScriptStart", duration);
1330 } else if (event_name == "Tab.NewTabDOMContentLoaded") {
1331 UMA_HISTOGRAM_TIMES("Tab.NewTabDOMContentLoaded", duration);
1332 } else if (event_name == "Tab.NewTabOnload") {
1333 UMA_HISTOGRAM_TIMES("Tab.NewTabOnload", duration);
[email protected]3f8eb7f2009-10-02 23:58:051334 // The new tab page has finished loading; reset it.
1335 new_tab_start_time_ = base::TimeTicks();
[email protected]84abba62009-10-07 17:01:441336 } else {
1337 NOTREACHED();
[email protected]3f8eb7f2009-10-02 23:58:051338 }
[email protected]84abba62009-10-07 17:01:441339 NotificationService::current()->Notify(
1340 NotificationType::METRIC_EVENT_DURATION,
1341 Source<TabContents>(this),
1342 Details<MetricEventDurationDetails>(&details));
[email protected]3f8eb7f2009-10-02 23:58:051343}
1344
[email protected]7ab1e7d62009-10-14 23:32:011345void TabContents::OnCloseStarted() {
1346 if (tab_close_start_time_.is_null())
1347 tab_close_start_time_ = base::TimeTicks::Now();
1348}
1349
[email protected]b5558cf22010-07-12 17:30:061350void TabContents::set_request_context(URLRequestContextGetter* context) {
1351 request_context_ = context;
1352}
1353
[email protected]46624bf2010-06-09 16:04:191354bool TabContents::ShouldAcceptDragAndDrop() const {
1355#if defined(OS_CHROMEOS)
1356 // ChromeOS panels (pop-ups) do not take drag-n-drop.
1357 // See http://crosbug.com/2413
1358 return delegate() && !delegate()->IsPopup(const_cast<TabContents*>(this));
1359#else
1360 return true;
1361#endif
1362}
1363
[email protected]c37e3b62010-01-22 00:15:171364TabContents* TabContents::CloneAndMakePhantom() {
[email protected]c37e3b62010-01-22 00:15:171365 TabNavigation tab_nav;
[email protected]df72ac72010-02-06 00:43:161366
1367 NavigationEntry* entry = controller().GetActiveEntry();
[email protected]98aa0b52010-05-06 17:03:081368 if (extension_app_)
1369 tab_nav.set_virtual_url(extension_app_->GetFullLaunchURL());
[email protected]df72ac72010-02-06 00:43:161370 else if (entry)
1371 tab_nav.SetFromNavigationEntry(*entry);
1372
[email protected]c37e3b62010-01-22 00:15:171373 std::vector<TabNavigation> navigations;
1374 navigations.push_back(tab_nav);
1375
1376 TabContents* new_contents =
1377 new TabContents(profile(), NULL, MSG_ROUTING_NONE, NULL);
[email protected]98aa0b52010-05-06 17:03:081378 new_contents->SetExtensionApp(extension_app_);
[email protected]c37e3b62010-01-22 00:15:171379 new_contents->controller().RestoreFromState(navigations, 0, false);
[email protected]c37e3b62010-01-22 00:15:171380
[email protected]98aa0b52010-05-06 17:03:081381 if (!extension_app_ && entry)
[email protected]df72ac72010-02-06 00:43:161382 new_contents->controller().GetActiveEntry()->favicon() = entry->favicon();
1383
[email protected]c37e3b62010-01-22 00:15:171384 return new_contents;
1385}
1386
[email protected]96d185d2009-04-24 03:28:541387// Notifies the RenderWidgetHost instance about the fact that the page is
1388// loading, or done loading and calls the base implementation.
1389void TabContents::SetIsLoading(bool is_loading,
1390 LoadNotificationDetails* details) {
1391 if (is_loading == is_loading_)
1392 return;
1393
1394 if (!is_loading) {
1395 load_state_ = net::LOAD_STATE_IDLE;
1396 load_state_host_.clear();
[email protected]094e5b22009-09-25 04:23:561397 upload_size_ = 0;
1398 upload_position_ = 0;
[email protected]96d185d2009-04-24 03:28:541399 }
1400
1401 render_manager_.SetIsLoading(is_loading);
1402
1403 is_loading_ = is_loading;
1404 waiting_for_response_ = is_loading;
1405
1406 if (delegate_)
1407 delegate_->LoadingStateChanged(this);
[email protected]329581b2009-04-28 06:52:351408 NotifyNavigationStateChanged(INVALIDATE_LOAD);
[email protected]96d185d2009-04-24 03:28:541409
1410 NotificationType type = is_loading ? NotificationType::LOAD_START :
1411 NotificationType::LOAD_STOP;
[email protected]ce5c4502009-05-06 16:46:111412 NotificationDetails det = NotificationService::NoDetails();
[email protected]96d185d2009-04-24 03:28:541413 if (details)
1414 det = Details<LoadNotificationDetails>(details);
1415 NotificationService::current()->Notify(type,
1416 Source<NavigationController>(&controller_),
1417 det);
1418}
1419
[email protected]b6c874582009-05-08 19:38:311420void TabContents::AddPopup(TabContents* new_contents,
[email protected]7e7414ae2010-01-26 20:19:291421 const gfx::Rect& initial_pos) {
[email protected]57eda82a2010-05-05 21:28:061422 // A page can't spawn popups (or do anything else, either) until its load
1423 // commits, so when we reach here, the popup was spawned by the
1424 // NavigationController's last committed entry, not the active entry. For
1425 // example, if a page opens a popup in an onunload() handler, then the active
1426 // entry is the page to be loaded as we navigate away from the unloading
1427 // page. For this reason, we can't use GetURL() to get the opener URL,
1428 // because it returns the active entry.
1429 NavigationEntry* entry = controller_.GetLastCommittedEntry();
1430 GURL creator = entry ? entry->virtual_url() : GURL::EmptyGURL();
1431
1432 if (creator.is_valid() &&
[email protected]8cc8d492010-02-02 10:40:491433 profile()->GetHostContentSettingsMap()->GetContentSetting(
[email protected]57eda82a2010-05-05 21:28:061434 creator, CONTENT_SETTINGS_TYPE_POPUPS) == CONTENT_SETTING_ALLOW) {
[email protected]8cc8d492010-02-02 10:40:491435 AddNewContents(new_contents, NEW_POPUP, initial_pos, true);
1436 } else {
1437 if (!blocked_popups_)
1438 blocked_popups_ = new BlockedPopupContainer(this);
1439 blocked_popups_->AddTabContents(new_contents, initial_pos);
[email protected]9fb83e82010-07-02 18:24:551440 content_settings_delegate_->OnContentBlocked(CONTENT_SETTINGS_TYPE_POPUPS);
[email protected]8cc8d492010-02-02 10:40:491441 }
[email protected]b9681312008-11-07 00:08:261442}
[email protected]616ed5a2008-11-21 22:27:241443
1444namespace {
1445bool TransitionIsReload(PageTransition::Type transition) {
1446 return PageTransition::StripQualifier(transition) == PageTransition::RELOAD;
1447}
1448}
1449
1450void TabContents::ExpireInfoBars(
1451 const NavigationController::LoadCommittedDetails& details) {
1452 // Only hide InfoBars when the user has done something that makes the main
1453 // frame load. We don't want various automatic or subframe navigations making
1454 // it disappear.
1455 if (!details.is_user_initiated_main_frame_load())
1456 return;
1457
[email protected]f86a07022008-11-25 01:06:051458 for (int i = infobar_delegate_count() - 1; i >= 0; --i) {
[email protected]616ed5a2008-11-21 22:27:241459 InfoBarDelegate* delegate = GetInfoBarDelegateAt(i);
[email protected]f86a07022008-11-25 01:06:051460 if (delegate->ShouldExpire(details))
[email protected]616ed5a2008-11-21 22:27:241461 RemoveInfoBar(delegate);
[email protected]616ed5a2008-11-21 22:27:241462 }
1463}
[email protected]fdd61c62009-04-22 19:22:571464
[email protected]96d185d2009-04-24 03:28:541465DOMUI* TabContents::GetDOMUIForCurrentState() {
1466 // When there is a pending navigation entry, we want to use the pending DOMUI
1467 // that goes along with it to control the basic flags. For example, we want to
1468 // show the pending URL in the URL bar, so we want the display_url flag to
1469 // be from the pending entry.
1470 //
1471 // The confusion comes because there are multiple possibilities for the
1472 // initial load in a tab as a side effect of the way the RenderViewHostManager
1473 // works.
1474 //
1475 // - For the very first tab the load looks "normal". The new tab DOM UI is
1476 // the pending one, and we want it to apply here.
1477 //
1478 // - For subsequent new tabs, they'll get a new SiteInstance which will then
1479 // get switched to the one previously associated with the new tab pages.
1480 // This switching will cause the manager to commit the RVH/DOMUI. So we'll
1481 // have a committed DOM UI in this case.
1482 //
1483 // This condition handles all of these cases:
1484 //
1485 // - First load in first tab: no committed nav entry + pending nav entry +
1486 // pending dom ui:
1487 // -> Use pending DOM UI if any.
1488 //
1489 // - First load in second tab: no committed nav entry + pending nav entry +
1490 // no pending DOM UI:
1491 // -> Use the committed DOM UI if any.
1492 //
1493 // - Second navigation in any tab: committed nav entry + pending nav entry:
1494 // -> Use pending DOM UI if any.
1495 //
1496 // - Normal state with no load: committed nav entry + no pending nav entry:
1497 // -> Use committed DOM UI.
1498 if (controller_.pending_entry() &&
1499 (controller_.GetLastCommittedEntry() ||
1500 render_manager_.pending_dom_ui()))
1501 return render_manager_.pending_dom_ui();
1502 return render_manager_.dom_ui();
1503}
[email protected]420ae012009-04-24 05:16:321504
1505void TabContents::DidNavigateMainFramePostCommit(
1506 const NavigationController::LoadCommittedDetails& details,
1507 const ViewHostMsg_FrameNavigate_Params& params) {
[email protected]c2e74fe82009-09-03 17:57:441508 if (opener_dom_ui_type_ != DOMUIFactory::kNoDOMUI) {
1509 // If this is a window.open navigation, use the same DOMUI as the renderer
1510 // that opened the window, as long as both renderers have the same
1511 // privileges.
1512 if (opener_dom_ui_type_ == DOMUIFactory::GetDOMUIType(GetURL())) {
1513 DOMUI* dom_ui = DOMUIFactory::CreateDOMUIForURL(this, GetURL());
1514 // dom_ui might be NULL if the URL refers to a non-existent extension.
1515 if (dom_ui) {
1516 render_manager_.SetDOMUIPostCommit(dom_ui);
1517 dom_ui->RenderViewCreated(render_view_host());
1518 }
1519 }
1520 opener_dom_ui_type_ = DOMUIFactory::kNoDOMUI;
1521 }
1522
[email protected]420ae012009-04-24 05:16:321523 if (details.is_user_initiated_main_frame_load()) {
1524 // Clear the status bubble. This is a workaround for a bug where WebKit
1525 // doesn't let us know that the cursor left an element during a
1526 // transition (this is also why the mouse cursor remains as a hand after
1527 // clicking on a link); see bugs 1184641 and 980803. We don't want to
1528 // clear the bubble when a user navigates to a named anchor in the same
1529 // page.
1530 UpdateTargetURL(details.entry->page_id(), GURL());
1531
1532 // UpdateHelpersForDidNavigate will handle the case where the password_form
1533 // origin is valid.
1534 // TODO(brettw) bug 1343111: Password manager stuff in here needs to be
1535 // cleaned up and covered by tests.
1536 if (!params.password_form.origin.is_valid())
1537 GetPasswordManager()->DidNavigate();
1538 }
1539
1540 // The keyword generator uses the navigation entries, so must be called after
1541 // the commit.
1542 GenerateKeywordIfNecessary(params);
1543
1544 // Allow the new page to set the title again.
1545 received_page_title_ = false;
1546
1547 // Get the favicon, either from history or request it from the net.
1548 fav_icon_helper_.FetchFavIcon(details.entry->url());
1549
[email protected]0de80162010-02-03 04:52:351550 // Clear all page actions, blocked content notifications and browser actions
1551 // for this tab, unless this is an in-page navigation.
[email protected]a6e82fc2010-02-24 22:28:141552 if (!details.is_in_page) {
[email protected]92c6f9b92009-10-24 04:35:081553 ExtensionsService* service = profile()->GetExtensionsService();
1554 if (service) {
1555 for (size_t i = 0; i < service->extensions()->size(); ++i) {
[email protected]5d246db22009-10-27 06:17:571556 ExtensionAction* browser_action =
[email protected]92c6f9b92009-10-24 04:35:081557 service->extensions()->at(i)->browser_action();
[email protected]56ce6e52009-10-27 00:10:521558 if (browser_action) {
1559 browser_action->ClearAllValuesForTab(controller().session_id().id());
1560 NotificationService::current()->Notify(
1561 NotificationType::EXTENSION_BROWSER_ACTION_UPDATED,
[email protected]5d246db22009-10-27 06:17:571562 Source<ExtensionAction>(browser_action),
[email protected]56ce6e52009-10-27 00:10:521563 NotificationService::NoDetails());
1564 }
[email protected]92c6f9b92009-10-24 04:35:081565
[email protected]5d246db22009-10-27 06:17:571566 ExtensionAction* page_action =
[email protected]56ce6e52009-10-27 00:10:521567 service->extensions()->at(i)->page_action();
1568 if (page_action) {
1569 page_action->ClearAllValuesForTab(controller().session_id().id());
1570 PageActionStateChanged();
1571 }
[email protected]92c6f9b92009-10-24 04:35:081572 }
[email protected]aece2c7f2009-08-27 20:43:171573 }
[email protected]0de80162010-02-03 04:52:351574
1575 // Close blocked popups.
1576 if (blocked_popups_) {
[email protected]0fbd70332010-06-01 19:28:341577 AutoReset<bool> auto_reset(&dont_notify_render_view_, true);
[email protected]0de80162010-02-03 04:52:351578 blocked_popups_->Destroy();
[email protected]da7b5262010-02-25 21:12:461579 blocked_popups_ = NULL;
[email protected]0de80162010-02-03 04:52:351580 }
1581
1582 // Clear "blocked" flags.
[email protected]9fb83e82010-07-02 18:24:551583 content_settings_delegate_->ClearBlockedContentSettings();
1584 content_settings_delegate_->GeolocationDidNavigate(details);
[email protected]f17a0ee2010-05-17 17:38:471585
1586 // Once the main frame is navigated, we're no longer considered to have
1587 // displayed insecure content.
1588 displayed_insecure_content_ = false;
[email protected]aece2c7f2009-08-27 20:43:171589 }
[email protected]ce5c4502009-05-06 16:46:111590
[email protected]8cc8d492010-02-02 10:40:491591 // Close constrained windows if necessary.
1592 if (!net::RegistryControlledDomainService::SameDomainOrHost(
1593 details.previous_url, details.entry->url()))
1594 CloseConstrainedWindows();
1595
[email protected]420ae012009-04-24 05:16:321596 // Update the starred state.
1597 UpdateStarredStateForCurrentURL();
[email protected]c07c9752010-03-12 05:57:531598
1599 // Clear the cache of forms in AutoFill.
1600 if (autofill_manager_.get())
1601 autofill_manager_->Reset();
[email protected]420ae012009-04-24 05:16:321602}
1603
1604void TabContents::DidNavigateAnyFramePostCommit(
1605 RenderViewHost* render_view_host,
1606 const NavigationController::LoadCommittedDetails& details,
1607 const ViewHostMsg_FrameNavigate_Params& params) {
1608 // If we navigate, start showing messages again. This does nothing to prevent
1609 // a malicious script from spamming messages, since the script could just
1610 // reload the page to stop blocking.
1611 suppress_javascript_messages_ = false;
1612
[email protected]420ae012009-04-24 05:16:321613 // Notify the password manager of the navigation or form submit.
1614 // TODO(brettw) bug 1343111: Password manager stuff in here needs to be
1615 // cleaned up and covered by tests.
1616 if (params.password_form.origin.is_valid())
1617 GetPasswordManager()->ProvisionallySavePassword(params.password_form);
[email protected]3b1c1812010-02-12 21:53:551618
1619 // Let the LanguageState clear its state.
[email protected]34cec8d82010-03-05 18:27:011620 bool reload = details.entry->transition_type() == PageTransition::RELOAD ||
1621 details.type == NavigationType::SAME_PAGE;
1622 language_state_.DidNavigate(reload, details.is_in_page);
[email protected]420ae012009-04-24 05:16:321623}
1624
[email protected]8cc8d492010-02-02 10:40:491625void TabContents::CloseConstrainedWindows() {
1626 // Clear out any constrained windows since we are leaving this page entirely.
[email protected]420ae012009-04-24 05:16:321627 // We use indices instead of iterators in case CloseWindow does something
1628 // that may invalidate an iterator.
1629 int size = static_cast<int>(child_windows_.size());
1630 for (int i = size - 1; i >= 0; --i) {
1631 ConstrainedWindow* window = child_windows_[i];
[email protected]fa1cf0b82010-01-15 21:49:441632 if (window) {
[email protected]420ae012009-04-24 05:16:321633 window->CloseConstrainedWindow();
[email protected]fa1cf0b82010-01-15 21:49:441634 BlockTabContent(false);
1635 }
[email protected]420ae012009-04-24 05:16:321636 }
1637}
1638
1639void TabContents::UpdateStarredStateForCurrentURL() {
1640 BookmarkModel* model = profile()->GetBookmarkModel();
1641 const bool old_state = is_starred_;
1642 is_starred_ = (model && model->IsBookmarked(GetURL()));
1643
1644 if (is_starred_ != old_state && delegate())
1645 delegate()->URLStarredChanged(this, is_starred_);
1646}
1647
1648void TabContents::UpdateAlternateErrorPageURL() {
1649 GURL url = GetAlternateErrorPageURL();
1650 render_view_host()->SetAlternateErrorPageURL(url);
1651}
1652
1653void TabContents::UpdateWebPreferences() {
1654 render_view_host()->UpdateWebPreferences(GetWebkitPrefs());
1655}
1656
1657void TabContents::UpdateMaxPageIDIfNecessary(SiteInstance* site_instance,
1658 RenderViewHost* rvh) {
1659 // If we are creating a RVH for a restored controller, then we might
1660 // have more page IDs than the SiteInstance's current max page ID. We must
1661 // make sure that the max page ID is larger than any restored page ID.
1662 // Note that it is ok for conflicting page IDs to exist in another tab
1663 // (i.e., NavigationController), but if any page ID is larger than the max,
1664 // the back/forward list will get confused.
1665 int max_restored_page_id = controller_.max_restored_page_id();
1666 if (max_restored_page_id > 0) {
1667 int curr_max_page_id = site_instance->max_page_id();
1668 if (max_restored_page_id > curr_max_page_id) {
1669 // Need to update the site instance immediately.
1670 site_instance->UpdateMaxPageID(max_restored_page_id);
1671
1672 // Also tell the renderer to update its internal representation. We
1673 // need to reserve enough IDs to make all restored page IDs less than
1674 // the max.
1675 if (curr_max_page_id < 0)
1676 curr_max_page_id = 0;
1677 rvh->ReservePageIDRange(max_restored_page_id - curr_max_page_id);
1678 }
1679 }
1680}
1681
1682void TabContents::UpdateHistoryForNavigation(
[email protected]ebe89e062009-08-13 23:16:541683 const GURL& virtual_url,
[email protected]befd8d822009-07-01 04:51:471684 const NavigationController::LoadCommittedDetails& details,
[email protected]420ae012009-04-24 05:16:321685 const ViewHostMsg_FrameNavigate_Params& params) {
1686 if (profile()->IsOffTheRecord())
1687 return;
1688
1689 // Add to history service.
1690 HistoryService* hs = profile()->GetHistoryService(Profile::IMPLICIT_ACCESS);
1691 if (hs) {
1692 if (PageTransition::IsMainFrame(params.transition) &&
[email protected]ebe89e062009-08-13 23:16:541693 virtual_url != params.url) {
1694 // Hack on the "virtual" URL so that it will appear in history. For some
[email protected]420ae012009-04-24 05:16:321695 // types of URLs, we will display a magic URL that is different from where
1696 // the page is actually navigated. We want the user to see in history
[email protected]ebe89e062009-08-13 23:16:541697 // what they saw in the URL bar, so we add the virtual URL as a redirect.
1698 // This only applies to the main frame, as the virtual URL doesn't apply
[email protected]420ae012009-04-24 05:16:321699 // to sub-frames.
1700 std::vector<GURL> redirects = params.redirects;
1701 if (!redirects.empty())
[email protected]ebe89e062009-08-13 23:16:541702 redirects.back() = virtual_url;
1703 hs->AddPage(virtual_url, this, params.page_id, params.referrer,
[email protected]befd8d822009-07-01 04:51:471704 params.transition, redirects, details.did_replace_entry);
[email protected]420ae012009-04-24 05:16:321705 } else {
1706 hs->AddPage(params.url, this, params.page_id, params.referrer,
[email protected]befd8d822009-07-01 04:51:471707 params.transition, params.redirects,
1708 details.did_replace_entry);
[email protected]420ae012009-04-24 05:16:321709 }
1710 }
1711}
1712
1713bool TabContents::UpdateTitleForEntry(NavigationEntry* entry,
1714 const std::wstring& title) {
1715 // For file URLs without a title, use the pathname instead. In the case of a
1716 // synthesized title, we don't want the update to count toward the "one set
1717 // per page of the title to history."
[email protected]e53668962010-06-23 15:35:251718 string16 final_title;
[email protected]420ae012009-04-24 05:16:321719 bool explicit_set;
1720 if (entry->url().SchemeIsFile() && title.empty()) {
[email protected]e53668962010-06-23 15:35:251721 final_title = UTF8ToUTF16(entry->url().ExtractFileName());
[email protected]420ae012009-04-24 05:16:321722 explicit_set = false; // Don't count synthetic titles toward the set limit.
1723 } else {
[email protected]e53668962010-06-23 15:35:251724 TrimWhitespace(WideToUTF16Hack(title), TRIM_ALL, &final_title);
[email protected]420ae012009-04-24 05:16:321725 explicit_set = true;
1726 }
1727
[email protected]e53668962010-06-23 15:35:251728 if (final_title == entry->title())
[email protected]420ae012009-04-24 05:16:321729 return false; // Nothing changed, don't bother.
1730
[email protected]e53668962010-06-23 15:35:251731 entry->set_title(final_title);
[email protected]420ae012009-04-24 05:16:321732
1733 // Update the history system for this page.
1734 if (!profile()->IsOffTheRecord() && !received_page_title_) {
1735 HistoryService* hs =
1736 profile()->GetHistoryService(Profile::IMPLICIT_ACCESS);
1737 if (hs)
[email protected]ebe89e062009-08-13 23:16:541738 hs->SetPageTitle(entry->virtual_url(), final_title);
[email protected]420ae012009-04-24 05:16:321739
1740 // Don't allow the title to be saved again for explicitly set ones.
1741 received_page_title_ = explicit_set;
1742 }
1743
1744 // Lastly, set the title for the view.
[email protected]e53668962010-06-23 15:35:251745 view_->SetPageTitle(UTF16ToWideHack(final_title));
[email protected]420ae012009-04-24 05:16:321746
[email protected]cbc0e1b2010-04-12 18:33:041747 NotificationService::current()->Notify(
1748 NotificationType::TAB_CONTENTS_TITLE_UPDATED,
1749 Source<TabContents>(this),
1750 NotificationService::NoDetails());
1751
[email protected]420ae012009-04-24 05:16:321752 return true;
1753}
1754
1755void TabContents::NotifySwapped() {
1756 // After sending out a swap notification, we need to send a disconnect
1757 // notification so that clients that pick up a pointer to |this| can NULL the
1758 // pointer. See Bug 1230284.
1759 notify_disconnection_ = true;
1760 NotificationService::current()->Notify(
[email protected]57c6a652009-05-04 07:58:341761 NotificationType::TAB_CONTENTS_SWAPPED,
[email protected]d82ed61e2009-06-16 02:46:221762 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:321763 NotificationService::NoDetails());
1764}
1765
1766void TabContents::NotifyConnected() {
1767 notify_disconnection_ = true;
1768 NotificationService::current()->Notify(
[email protected]57c6a652009-05-04 07:58:341769 NotificationType::TAB_CONTENTS_CONNECTED,
[email protected]d82ed61e2009-06-16 02:46:221770 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:321771 NotificationService::NoDetails());
1772}
1773
1774void TabContents::NotifyDisconnected() {
1775 if (!notify_disconnection_)
1776 return;
1777
1778 notify_disconnection_ = false;
1779 NotificationService::current()->Notify(
[email protected]57c6a652009-05-04 07:58:341780 NotificationType::TAB_CONTENTS_DISCONNECTED,
[email protected]d82ed61e2009-06-16 02:46:221781 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:321782 NotificationService::NoDetails());
1783}
1784
1785void TabContents::GenerateKeywordIfNecessary(
1786 const ViewHostMsg_FrameNavigate_Params& params) {
1787 if (!params.searchable_form_url.is_valid())
1788 return;
1789
1790 if (profile()->IsOffTheRecord())
1791 return;
1792
1793 int last_index = controller_.last_committed_entry_index();
1794 // When there was no previous page, the last index will be 0. This is
1795 // normally due to a form submit that opened in a new tab.
1796 // TODO(brettw) bug 916126: we should support keywords when form submits
1797 // happen in new tabs.
1798 if (last_index <= 0)
1799 return;
1800 const NavigationEntry* previous_entry =
1801 controller_.GetEntryAtIndex(last_index - 1);
1802 if (IsFormSubmit(previous_entry)) {
1803 // Only generate a keyword if the previous page wasn't itself a form
1804 // submit.
1805 return;
1806 }
1807
1808 GURL keyword_url = previous_entry->user_typed_url().is_valid() ?
1809 previous_entry->user_typed_url() : previous_entry->url();
1810 std::wstring keyword =
1811 TemplateURLModel::GenerateKeyword(keyword_url, true); // autodetected
1812 if (keyword.empty())
1813 return;
1814
1815 TemplateURLModel* url_model = profile()->GetTemplateURLModel();
1816 if (!url_model)
1817 return;
1818
1819 if (!url_model->loaded()) {
1820 url_model->Load();
1821 return;
1822 }
1823
1824 const TemplateURL* current_url;
[email protected]ddd231e2010-06-29 20:35:191825 GURL url = params.searchable_form_url;
[email protected]420ae012009-04-24 05:16:321826 if (!url_model->CanReplaceKeyword(keyword, url, &current_url))
1827 return;
1828
1829 if (current_url) {
1830 if (current_url->originating_url().is_valid()) {
1831 // The existing keyword was generated from an OpenSearch description
1832 // document, don't regenerate.
1833 return;
1834 }
1835 url_model->Remove(current_url);
1836 }
1837 TemplateURL* new_url = new TemplateURL();
1838 new_url->set_keyword(keyword);
1839 new_url->set_short_name(keyword);
[email protected]ddd231e2010-06-29 20:35:191840 new_url->SetURL(url.spec(), 0, 0);
[email protected]420ae012009-04-24 05:16:321841 new_url->add_input_encoding(params.searchable_form_encoding);
1842 DCHECK(controller_.GetLastCommittedEntry());
1843 const GURL& favicon_url =
1844 controller_.GetLastCommittedEntry()->favicon().url();
1845 if (favicon_url.is_valid()) {
1846 new_url->SetFavIconURL(favicon_url);
1847 } else {
1848 // The favicon url isn't valid. This means there really isn't a favicon,
1849 // or the favicon url wasn't obtained before the load started. This assumes
1850 // the later.
1851 // TODO(sky): Need a way to set the favicon that doesn't involve generating
1852 // its url.
1853 new_url->SetFavIconURL(TemplateURL::GenerateFaviconURL(params.referrer));
1854 }
1855 new_url->set_safe_for_autoreplace(true);
1856 url_model->Add(new_url);
1857}
1858
[email protected]bf5c2ff392009-07-08 16:24:331859void TabContents::OnUserGesture() {
1860 // See comment in RenderViewHostDelegate::OnUserGesture as to why we do this.
[email protected]41291322010-07-15 17:09:011861 DownloadRequestLimiter* limiter =
1862 g_browser_process->download_request_limiter();
1863 if (limiter)
1864 limiter->OnUserGesture(this);
[email protected]e7eaedde2009-09-25 20:09:491865 ExternalProtocolHandler::OnUserGesture();
[email protected]bf5c2ff392009-07-08 16:24:331866 controller_.OnUserGesture();
1867}
1868
1869void TabContents::OnFindReply(int request_id,
1870 int number_of_matches,
1871 const gfx::Rect& selection_rect,
1872 int active_match_ordinal,
1873 bool final_update) {
[email protected]3ce7ced2009-07-24 18:52:421874 // Ignore responses for requests that have been aborted.
1875 if (find_op_aborted_)
1876 return;
1877
[email protected]bf5c2ff392009-07-08 16:24:331878 // Ignore responses for requests other than the one we have most recently
1879 // issued. That way we won't act on stale results when the user has
1880 // already typed in another query.
1881 if (request_id != current_find_request_id_)
1882 return;
1883
1884 if (number_of_matches == -1)
1885 number_of_matches = last_search_result_.number_of_matches();
1886 if (active_match_ordinal == -1)
1887 active_match_ordinal = last_search_result_.active_match_ordinal();
1888
1889 gfx::Rect selection = selection_rect;
1890 if (selection.IsEmpty())
1891 selection = last_search_result_.selection_rect();
1892
1893 // Notify the UI, automation and any other observers that a find result was
1894 // found.
1895 last_search_result_ = FindNotificationDetails(request_id, number_of_matches,
1896 selection, active_match_ordinal,
1897 final_update);
1898 NotificationService::current()->Notify(
1899 NotificationType::FIND_RESULT_AVAILABLE,
1900 Source<TabContents>(this),
1901 Details<FindNotificationDetails>(&last_search_result_));
1902}
1903
1904void TabContents::GoToEntryAtOffset(int offset) {
[email protected]f9cc4c452009-10-13 14:56:381905 if (!delegate_ || delegate_->OnGoToEntryOffset(offset))
1906 controller_.GoToOffset(offset);
[email protected]bf5c2ff392009-07-08 16:24:331907}
1908
[email protected]bf5c2ff392009-07-08 16:24:331909void TabContents::OnMissingPluginStatus(int status) {
1910#if defined(OS_WIN)
1911// TODO(PORT): pull in when plug-ins work
1912 GetPluginInstaller()->OnMissingPluginStatus(status);
1913#endif
1914}
1915
1916void TabContents::OnCrashedPlugin(const FilePath& plugin_path) {
[email protected]bf5c2ff392009-07-08 16:24:331917 DCHECK(!plugin_path.value().empty());
1918
1919 std::wstring plugin_name = plugin_path.ToWStringHack();
[email protected]d32d46182009-07-23 00:09:351920#if defined(OS_WIN) || defined(OS_MACOSX)
[email protected]bf5c2ff392009-07-08 16:24:331921 scoped_ptr<FileVersionInfo> version_info(
1922 FileVersionInfo::CreateFileVersionInfo(plugin_path));
1923 if (version_info.get()) {
1924 const std::wstring& product_name = version_info->product_name();
[email protected]996fd702009-09-04 19:12:371925 if (!product_name.empty()) {
[email protected]bf5c2ff392009-07-08 16:24:331926 plugin_name = product_name;
[email protected]996fd702009-09-04 19:12:371927#if defined(OS_MACOSX)
1928 // Many plugins on the Mac have .plugin in the actual name, which looks
1929 // terrible, so look for that and strip it off if present.
1930 const std::wstring plugin_extension(L".plugin");
1931 if (EndsWith(plugin_name, plugin_extension, true))
1932 plugin_name.erase(plugin_name.length() - plugin_extension.length());
1933#endif // OS_MACOSX
1934 }
[email protected]bf5c2ff392009-07-08 16:24:331935 }
[email protected]4a560ed2009-07-13 23:35:261936#else
1937 NOTIMPLEMENTED() << " convert plugin path to plugin name";
1938#endif
[email protected]2ae8a922010-06-28 20:27:531939 SkBitmap* crash_icon = ResourceBundle::GetSharedInstance().GetBitmapNamed(
1940 IDR_INFOBAR_PLUGIN_CRASHED);
[email protected]bf5c2ff392009-07-08 16:24:331941 AddInfoBar(new SimpleAlertInfoBarDelegate(
1942 this, l10n_util::GetStringF(IDS_PLUGIN_CRASHED_PROMPT, plugin_name),
[email protected]2ae8a922010-06-28 20:27:531943 crash_icon, true));
[email protected]bf5c2ff392009-07-08 16:24:331944}
1945
1946void TabContents::OnCrashedWorker() {
1947 AddInfoBar(new SimpleAlertInfoBarDelegate(
1948 this, l10n_util::GetString(IDS_WEBWORKER_CRASHED_PROMPT),
[email protected]938e1f92010-04-01 18:09:421949 NULL, true));
[email protected]bf5c2ff392009-07-08 16:24:331950}
1951
1952void TabContents::OnDidGetApplicationInfo(
1953 int32 page_id,
1954 const webkit_glue::WebApplicationInfo& info) {
[email protected]ed5431872009-11-17 08:39:511955 web_app_info_ = info;
[email protected]bf5c2ff392009-07-08 16:24:331956
[email protected]ed5431872009-11-17 08:39:511957 if (delegate())
1958 delegate()->OnDidGetApplicationInfo(this, page_id);
[email protected]bf5c2ff392009-07-08 16:24:331959}
1960
[email protected]a2cf65eb2010-01-18 08:20:381961void TabContents::OnPageContents(const GURL& url,
1962 int renderer_process_id,
1963 int32 page_id,
[email protected]e5106202010-06-11 21:12:361964 const string16& contents,
[email protected]1c57b7b2010-07-12 17:38:101965 const std::string& language,
1966 bool page_translatable) {
[email protected]a2cf65eb2010-01-18 08:20:381967 // Don't index any https pages. People generally don't want their bank
1968 // accounts, etc. indexed on their computer, especially since some of these
1969 // things are not marked cachable.
1970 // TODO(brettw) we may want to consider more elaborate heuristics such as
1971 // the cachability of the page. We may also want to consider subframes (this
1972 // test will still index subframes if the subframe is SSL).
[email protected]ccd40a52010-05-11 16:15:271973 // TODO(zelidrag) bug chromium-os:2808 - figure out if we want to reenable
1974 // content indexing for chromeos in some future releases.
1975#if !defined(OS_CHROMEOS)
[email protected]a2cf65eb2010-01-18 08:20:381976 if (!url.SchemeIsSecure()) {
1977 Profile* p = profile();
1978 if (p && !p->IsOffTheRecord()) {
1979 HistoryService* hs = p->GetHistoryService(Profile::IMPLICIT_ACCESS);
1980 if (hs)
1981 hs->SetPageContents(url, contents);
1982 }
1983 }
[email protected]ccd40a52010-05-11 16:15:271984#endif
[email protected]a8a81292010-01-21 00:32:451985
[email protected]1c57b7b2010-07-12 17:38:101986 language_state_.LanguageDetermined(language, page_translatable);
[email protected]a8a81292010-01-21 00:32:451987
1988 std::string lang = language;
1989 NotificationService::current()->Notify(
1990 NotificationType::TAB_LANGUAGE_DETERMINED,
[email protected]7e0831b2010-02-01 08:45:151991 Source<TabContents>(this),
[email protected]a8a81292010-01-21 00:32:451992 Details<std::string>(&lang));
[email protected]a2cf65eb2010-01-18 08:20:381993}
1994
[email protected]7e0831b2010-02-01 08:45:151995void TabContents::OnPageTranslated(int32 page_id,
1996 const std::string& original_lang,
[email protected]152b8ba32010-03-29 19:40:161997 const std::string& translated_lang,
1998 TranslateErrors::Type error_type) {
[email protected]3b1c1812010-02-12 21:53:551999 language_state_.set_current_language(translated_lang);
2000 language_state_.set_translation_pending(false);
[email protected]152b8ba32010-03-29 19:40:162001 PageTranslatedDetails details(original_lang, translated_lang, error_type);
[email protected]7e0831b2010-02-01 08:45:152002 NotificationService::current()->Notify(
2003 NotificationType::PAGE_TRANSLATED,
2004 Source<TabContents>(this),
[email protected]152b8ba32010-03-29 19:40:162005 Details<PageTranslatedDetails>(&details));
[email protected]7e0831b2010-02-01 08:45:152006}
2007
[email protected]bf5c2ff392009-07-08 16:24:332008void TabContents::DidStartProvisionalLoadForFrame(
2009 RenderViewHost* render_view_host,
2010 bool is_main_frame,
2011 const GURL& url) {
2012 ProvisionalLoadDetails details(is_main_frame,
2013 controller_.IsURLInPageNavigation(url),
2014 url, std::string(), false);
2015 NotificationService::current()->Notify(
2016 NotificationType::FRAME_PROVISIONAL_LOAD_START,
2017 Source<NavigationController>(&controller_),
2018 Details<ProvisionalLoadDetails>(&details));
2019}
2020
2021void TabContents::DidStartReceivingResourceResponse(
[email protected]3dbb80b2010-02-09 22:41:202022 const ResourceRequestDetails& details) {
[email protected]bf5c2ff392009-07-08 16:24:332023 NotificationService::current()->Notify(
2024 NotificationType::RESOURCE_RESPONSE_STARTED,
2025 Source<NavigationController>(&controller()),
[email protected]3dbb80b2010-02-09 22:41:202026 Details<const ResourceRequestDetails>(&details));
[email protected]bf5c2ff392009-07-08 16:24:332027}
2028
[email protected]3dbb80b2010-02-09 22:41:202029void TabContents::DidRedirectResource(
2030 const ResourceRedirectDetails& details) {
[email protected]bf5c2ff392009-07-08 16:24:332031 NotificationService::current()->Notify(
2032 NotificationType::RESOURCE_RECEIVED_REDIRECT,
2033 Source<NavigationController>(&controller()),
[email protected]3dbb80b2010-02-09 22:41:202034 Details<const ResourceRequestDetails>(&details));
[email protected]bf5c2ff392009-07-08 16:24:332035}
2036
2037void TabContents::DidLoadResourceFromMemoryCache(
2038 const GURL& url,
2039 const std::string& frame_origin,
2040 const std::string& main_frame_origin,
2041 const std::string& security_info) {
2042 // Send out a notification that we loaded a resource from our memory cache.
[email protected]fc7de492010-07-12 14:49:042043 int cert_id = 0, cert_status = 0, security_bits = 0, connection_status = 0;
[email protected]bf5c2ff392009-07-08 16:24:332044 SSLManager::DeserializeSecurityInfo(security_info,
2045 &cert_id, &cert_status,
[email protected]fc7de492010-07-12 14:49:042046 &security_bits,
2047 &connection_status);
[email protected]bf5c2ff392009-07-08 16:24:332048 LoadFromMemoryCacheDetails details(url, frame_origin, main_frame_origin,
[email protected]8cb5d5b2010-02-09 11:36:162049 GetRenderProcessHost()->id(), cert_id,
2050 cert_status);
[email protected]bf5c2ff392009-07-08 16:24:332051
2052 NotificationService::current()->Notify(
2053 NotificationType::LOAD_FROM_MEMORY_CACHE,
2054 Source<NavigationController>(&controller_),
2055 Details<LoadFromMemoryCacheDetails>(&details));
2056}
2057
[email protected]e3d60e5d2009-09-25 21:08:292058void TabContents::DidDisplayInsecureContent() {
[email protected]f17a0ee2010-05-17 17:38:472059 displayed_insecure_content_ = true;
2060 SSLManager::NotifySSLInternalStateChanged();
[email protected]e3d60e5d2009-09-25 21:08:292061}
2062
2063void TabContents::DidRunInsecureContent(const std::string& security_origin) {
[email protected]09843b5f2009-09-26 08:30:592064 controller_.ssl_manager()->DidRunInsecureContent(security_origin);
[email protected]e3d60e5d2009-09-25 21:08:292065}
2066
[email protected]bf5c2ff392009-07-08 16:24:332067void TabContents::DidFailProvisionalLoadWithError(
2068 RenderViewHost* render_view_host,
2069 bool is_main_frame,
2070 int error_code,
2071 const GURL& url,
2072 bool showing_repost_interstitial) {
2073 if (net::ERR_ABORTED == error_code) {
2074 // EVIL HACK ALERT! Ignore failed loads when we're showing interstitials.
2075 // This means that the interstitial won't be torn down properly, which is
2076 // bad. But if we have an interstitial, go back to another tab type, and
2077 // then load the same interstitial again, we could end up getting the first
2078 // interstitial's "failed" message (as a result of the cancel) when we're on
2079 // the second one.
2080 //
2081 // We can't tell this apart, so we think we're tearing down the current page
2082 // which will cause a crash later one. There is also some code in
2083 // RenderViewHostManager::RendererAbortedProvisionalLoad that is commented
2084 // out because of this problem.
2085 //
2086 // http://code.google.com/p/chromium/issues/detail?id=2855
2087 // Because this will not tear down the interstitial properly, if "back" is
2088 // back to another tab type, the interstitial will still be somewhat alive
2089 // in the previous tab type. If you navigate somewhere that activates the
2090 // tab with the interstitial again, you'll see a flash before the new load
2091 // commits of the interstitial page.
2092 if (showing_interstitial_page()) {
2093 LOG(WARNING) << "Discarding message during interstitial.";
2094 return;
2095 }
2096
2097 // This will discard our pending entry if we cancelled the load (e.g., if we
2098 // decided to download the file instead of load it). Only discard the
2099 // pending entry if the URLs match, otherwise the user initiated a navigate
2100 // before the page loaded so that the discard would discard the wrong entry.
2101 NavigationEntry* pending_entry = controller_.pending_entry();
2102 if (pending_entry && pending_entry->url() == url) {
2103 controller_.DiscardNonCommittedEntries();
2104 // Update the URL display.
2105 NotifyNavigationStateChanged(TabContents::INVALIDATE_URL);
2106 }
2107
2108 render_manager_.RendererAbortedProvisionalLoad(render_view_host);
2109 }
2110
2111 // Send out a notification that we failed a provisional load with an error.
2112 ProvisionalLoadDetails details(is_main_frame,
2113 controller_.IsURLInPageNavigation(url),
2114 url, std::string(), false);
2115 details.set_error_code(error_code);
2116
2117 NotificationService::current()->Notify(
2118 NotificationType::FAIL_PROVISIONAL_LOAD_WITH_ERROR,
2119 Source<NavigationController>(&controller_),
2120 Details<ProvisionalLoadDetails>(&details));
2121}
2122
2123void TabContents::DocumentLoadedInFrame() {
2124 controller_.DocumentLoadedInFrame();
2125}
2126
[email protected]9fb83e82010-07-02 18:24:552127void TabContents::OnContentSettingsChange() {
[email protected]614c0e12010-03-25 10:12:562128 if (delegate_)
[email protected]20ad6a92010-04-21 14:07:242129 delegate_->OnContentSettingsChange(this);
[email protected]614c0e12010-03-25 10:12:562130}
2131
[email protected]8d3347f2009-07-09 22:00:212132RenderViewHostDelegate::View* TabContents::GetViewDelegate() {
[email protected]420ae012009-04-24 05:16:322133 return view_.get();
2134}
2135
[email protected]8d3347f2009-07-09 22:00:212136RenderViewHostDelegate::RendererManagement*
2137TabContents::GetRendererManagementDelegate() {
2138 return &render_manager_;
2139}
2140
[email protected]bf5c2ff392009-07-08 16:24:332141RenderViewHostDelegate::BrowserIntegration*
[email protected]8d3347f2009-07-09 22:00:212142 TabContents::GetBrowserIntegrationDelegate() {
2143 return this;
[email protected]bf5c2ff392009-07-08 16:24:332144}
2145
[email protected]8d3347f2009-07-09 22:00:212146RenderViewHostDelegate::Resource* TabContents::GetResourceDelegate() {
2147 return this;
[email protected]bf5c2ff392009-07-08 16:24:332148}
2149
[email protected]46ba9082010-06-22 10:00:582150RenderViewHostDelegate::ContentSettings*
2151TabContents::GetContentSettingsDelegate() {
[email protected]9fb83e82010-07-02 18:24:552152 return content_settings_delegate_.get();
[email protected]46ba9082010-06-22 10:00:582153}
2154
[email protected]8d3347f2009-07-09 22:00:212155RenderViewHostDelegate::Save* TabContents::GetSaveDelegate() {
[email protected]420ae012009-04-24 05:16:322156 return save_package_.get(); // May be NULL, but we can return NULL.
2157}
2158
[email protected]8d3347f2009-07-09 22:00:212159RenderViewHostDelegate::Printing* TabContents::GetPrintingDelegate() {
[email protected]12802702010-07-09 19:43:092160 return printing_.get();
[email protected]8d3347f2009-07-09 22:00:212161}
2162
2163RenderViewHostDelegate::FavIcon* TabContents::GetFavIconDelegate() {
2164 return &fav_icon_helper_;
2165}
2166
[email protected]e8dbd674b2010-03-11 18:34:402167RenderViewHostDelegate::Autocomplete* TabContents::GetAutocompleteDelegate() {
[email protected]6db0def92010-03-02 19:55:452168 if (autocomplete_history_manager_.get() == NULL)
2169 autocomplete_history_manager_.reset(new AutocompleteHistoryManager(this));
2170 return autocomplete_history_manager_.get();
[email protected]bf5c2ff392009-07-08 16:24:332171}
2172
[email protected]21674df2009-10-28 19:51:212173RenderViewHostDelegate::AutoFill* TabContents::GetAutoFillDelegate() {
[email protected]cf28ec52010-04-09 22:56:342174 return GetAutoFillManager();
[email protected]21674df2009-10-28 19:51:212175}
2176
[email protected]fd571bb2010-03-19 01:23:542177AutomationResourceRoutingDelegate*
2178TabContents::GetAutomationResourceRoutingDelegate() {
2179 return delegate();
2180}
2181
[email protected]ced90ae12010-02-20 02:06:162182RenderViewHostDelegate::BookmarkDrag* TabContents::GetBookmarkDragDelegate() {
2183 return bookmark_drag_;
2184}
2185
2186void TabContents::SetBookmarkDragDelegate(
2187 RenderViewHostDelegate::BookmarkDrag* bookmark_drag) {
2188 bookmark_drag_ = bookmark_drag;
2189}
2190
[email protected]9fb83e82010-07-02 18:24:552191TabSpecificContentSettings* TabContents::GetTabSpecificContentSettings() const {
2192 return content_settings_delegate_.get();
2193}
2194
[email protected]93623c5d2009-12-10 21:40:322195RendererPreferences TabContents::GetRendererPrefs(Profile* profile) const {
[email protected]840b1512009-07-21 16:53:462196 return renderer_preferences_;
[email protected]80d96fa2009-06-10 22:34:512197}
2198
[email protected]57c6a652009-05-04 07:58:342199TabContents* TabContents::GetAsTabContents() {
2200 return this;
2201}
2202
[email protected]7b291f92009-08-14 05:43:532203ViewType::Type TabContents::GetRenderViewType() const {
2204 return ViewType::TAB_CONTENTS;
2205}
2206
2207int TabContents::GetBrowserWindowID() const {
2208 return controller().window_id().id();
2209}
2210
[email protected]420ae012009-04-24 05:16:322211void TabContents::RenderViewCreated(RenderViewHost* render_view_host) {
[email protected]2e4633c2009-07-09 16:58:062212 NotificationService::current()->Notify(
2213 NotificationType::RENDER_VIEW_HOST_CREATED_FOR_TAB,
2214 Source<TabContents>(this),
2215 Details<RenderViewHost>(render_view_host));
[email protected]420ae012009-04-24 05:16:322216 NavigationEntry* entry = controller_.GetActiveEntry();
2217 if (!entry)
2218 return;
2219
2220 // When we're creating views, we're still doing initial setup, so we always
2221 // use the pending DOM UI rather than any possibly existing committed one.
2222 if (render_manager_.pending_dom_ui()) {
2223 render_manager_.pending_dom_ui()->RenderViewCreated(render_view_host);
2224 }
2225
2226 if (entry->IsViewSourceMode()) {
2227 // Put the renderer in view source mode.
2228 render_view_host->Send(
2229 new ViewMsg_EnableViewSourceMode(render_view_host->routing_id()));
2230 }
[email protected]0666aef2009-05-13 19:48:082231
2232 view()->RenderViewCreated(render_view_host);
[email protected]420ae012009-04-24 05:16:322233}
2234
2235void TabContents::RenderViewReady(RenderViewHost* rvh) {
2236 if (rvh != render_view_host()) {
2237 // Don't notify the world, since this came from a renderer in the
2238 // background.
2239 return;
2240 }
2241
2242 NotifyConnected();
[email protected]9d8a4642009-07-29 17:25:302243 bool was_crashed = is_crashed();
[email protected]420ae012009-04-24 05:16:322244 SetIsCrashed(false);
[email protected]9d8a4642009-07-29 17:25:302245
2246 // Restore the focus to the tab (otherwise the focus will be on the top
2247 // window).
2248 if (was_crashed && !FocusLocationBarByDefault())
2249 Focus();
[email protected]420ae012009-04-24 05:16:322250}
2251
2252void TabContents::RenderViewGone(RenderViewHost* rvh) {
2253 // Ask the print preview if this renderer was valuable.
[email protected]12802702010-07-09 19:43:092254 if (!printing_->OnRenderViewGone(rvh))
[email protected]420ae012009-04-24 05:16:322255 return;
2256 if (rvh != render_view_host()) {
2257 // The pending page's RenderViewHost is gone.
2258 return;
2259 }
2260
2261 SetIsLoading(false, NULL);
2262 NotifyDisconnected();
2263 SetIsCrashed(true);
2264
[email protected]ff9efc62010-02-18 22:10:542265 // Remove all infobars.
[email protected]e5430c92010-02-18 22:39:212266 for (int i = infobar_delegate_count() - 1; i >=0 ; --i)
[email protected]ff9efc62010-02-18 22:10:542267 RemoveInfoBar(GetInfoBarDelegateAt(i));
2268
[email protected]8e537302009-06-10 21:19:582269 // Tell the view that we've crashed so it can prepare the sad tab page.
[email protected]cf68b092010-01-15 20:05:552270 // Only do this if we're not in browser shutdown, so that TabContents
2271 // objects that are not in a browser (e.g., HTML dialogs) and thus are
2272 // visible do not flash a sad tab page.
2273 if (browser_shutdown::GetShutdownType() == browser_shutdown::NOT_VALID)
2274 view_->OnTabCrashed();
[email protected]420ae012009-04-24 05:16:322275
2276 // Hide any visible hung renderer warning for this web contents' process.
[email protected]e7b8d832009-11-20 22:41:002277 hung_renderer_dialog::HideForTabContents(this);
[email protected]420ae012009-04-24 05:16:322278}
2279
[email protected]2e4633c2009-07-09 16:58:062280void TabContents::RenderViewDeleted(RenderViewHost* rvh) {
2281 NotificationService::current()->Notify(
2282 NotificationType::RENDER_VIEW_HOST_DELETED,
2283 Source<TabContents>(this),
2284 Details<RenderViewHost>(rvh));
[email protected]fb7b79f72009-11-06 18:00:482285 render_manager_.RenderViewDeleted(rvh);
[email protected]2e4633c2009-07-09 16:58:062286}
2287
[email protected]420ae012009-04-24 05:16:322288void TabContents::DidNavigate(RenderViewHost* rvh,
2289 const ViewHostMsg_FrameNavigate_Params& params) {
[email protected]8030f012009-09-25 18:09:372290 int extra_invalidate_flags = 0;
2291
2292 if (PageTransition::IsMainFrame(params.transition)) {
[email protected]20ede072009-10-06 00:28:352293 bool was_bookmark_bar_visible = ShouldShowBookmarkBar();
[email protected]8030f012009-09-25 18:09:372294 bool was_extension_shelf_visible = IsExtensionShelfAlwaysVisible();
2295
[email protected]420ae012009-04-24 05:16:322296 render_manager_.DidNavigateMainFrame(rvh);
2297
[email protected]20ede072009-10-06 00:28:352298 if (was_bookmark_bar_visible != ShouldShowBookmarkBar())
[email protected]8030f012009-09-25 18:09:372299 extra_invalidate_flags |= INVALIDATE_BOOKMARK_BAR;
2300 if (was_extension_shelf_visible != IsExtensionShelfAlwaysVisible())
2301 extra_invalidate_flags |= INVALIDATE_EXTENSION_SHELF;
2302 }
2303
[email protected]420ae012009-04-24 05:16:322304 // Update the site of the SiteInstance if it doesn't have one yet.
2305 if (!GetSiteInstance()->has_site())
2306 GetSiteInstance()->SetSite(params.url);
2307
2308 // Need to update MIME type here because it's referred to in
2309 // UpdateNavigationCommands() called by RendererDidNavigate() to
2310 // determine whether or not to enable the encoding menu.
2311 // It's updated only for the main frame. For a subframe,
2312 // RenderView::UpdateURL does not set params.contents_mime_type.
2313 // (see http://code.google.com/p/chromium/issues/detail?id=2929 )
2314 // TODO(jungshik): Add a test for the encoding menu to avoid
2315 // regressing it again.
2316 if (PageTransition::IsMainFrame(params.transition))
2317 contents_mime_type_ = params.contents_mime_type;
2318
2319 NavigationController::LoadCommittedDetails details;
[email protected]8030f012009-09-25 18:09:372320 bool did_navigate = controller_.RendererDidNavigate(
2321 params, extra_invalidate_flags, &details);
[email protected]3e90d4a2009-07-03 17:38:392322
2323 // Update history. Note that this needs to happen after the entry is complete,
2324 // which WillNavigate[Main,Sub]Frame will do before this function is called.
[email protected]2e29d922009-10-22 22:13:352325 if (params.should_update_history &&
2326 (!delegate() || delegate()->ShouldAddNavigationsToHistory())) {
[email protected]3e90d4a2009-07-03 17:38:392327 // Most of the time, the displayURL matches the loaded URL, but for about:
2328 // URLs, we use a data: URL as the real value. We actually want to save
2329 // the about: URL to the history db and keep the data: URL hidden. This is
2330 // what the TabContents' URL getter does.
2331 UpdateHistoryForNavigation(GetURL(), details, params);
2332 }
2333
[email protected]76543b92009-08-31 17:27:452334 if (!did_navigate)
2335 return; // No navigation happened.
[email protected]420ae012009-04-24 05:16:322336
2337 // DO NOT ADD MORE STUFF TO THIS FUNCTION! Your component should either listen
2338 // for the appropriate notification (best) or you can add it to
2339 // DidNavigateMainFramePostCommit / DidNavigateAnyFramePostCommit (only if
2340 // necessary, please).
2341
2342 // Run post-commit tasks.
2343 if (details.is_main_frame)
2344 DidNavigateMainFramePostCommit(details, params);
2345 DidNavigateAnyFramePostCommit(rvh, details, params);
2346}
2347
2348void TabContents::UpdateState(RenderViewHost* rvh,
2349 int32 page_id,
2350 const std::string& state) {
2351 DCHECK(rvh == render_view_host());
2352
2353 // We must be prepared to handle state updates for any page, these occur
2354 // when the user is scrolling and entering form data, as well as when we're
2355 // leaving a page, in which case our state may have already been moved to
2356 // the next page. The navigation controller will look up the appropriate
2357 // NavigationEntry and update it when it is notified via the delegate.
2358
2359 int entry_index = controller_.GetEntryIndexWithPageID(
2360 GetSiteInstance(), page_id);
2361 if (entry_index < 0)
2362 return;
2363 NavigationEntry* entry = controller_.GetEntryAtIndex(entry_index);
2364
2365 if (state == entry->content_state())
2366 return; // Nothing to update.
2367 entry->set_content_state(state);
2368 controller_.NotifyEntryChanged(entry, entry_index);
2369}
2370
2371void TabContents::UpdateTitle(RenderViewHost* rvh,
2372 int32 page_id, const std::wstring& title) {
2373 // If we have a title, that's a pretty good indication that we've started
2374 // getting useful data.
2375 SetNotWaitingForResponse();
2376
2377 DCHECK(rvh == render_view_host());
2378 NavigationEntry* entry = controller_.GetEntryWithPageID(GetSiteInstance(),
2379 page_id);
2380 if (!entry || !UpdateTitleForEntry(entry, title))
2381 return;
2382
2383 // Broadcast notifications when the UI should be updated.
2384 if (entry == controller_.GetEntryAtOffset(0))
[email protected]f1cd5e82009-10-23 17:19:032385 NotifyNavigationStateChanged(INVALIDATE_TITLE);
[email protected]420ae012009-04-24 05:16:322386}
2387
[email protected]420ae012009-04-24 05:16:322388void TabContents::UpdateEncoding(RenderViewHost* render_view_host,
[email protected]41fc0322009-09-04 22:23:402389 const std::string& encoding) {
[email protected]420ae012009-04-24 05:16:322390 set_encoding(encoding);
2391}
2392
2393void TabContents::UpdateTargetURL(int32 page_id, const GURL& url) {
2394 if (delegate())
2395 delegate()->UpdateTargetURL(this, url);
2396}
2397
2398void TabContents::UpdateThumbnail(const GURL& url,
2399 const SkBitmap& bitmap,
2400 const ThumbnailScore& score) {
2401 // Tell History about this thumbnail
[email protected]523623c2010-05-19 16:03:422402 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kTopSites)) {
[email protected]72d0fc72009-07-09 17:30:492403 if (!profile()->IsOffTheRecord())
[email protected]523623c2010-05-19 16:03:422404 profile()->GetTopSites()->SetPageThumbnail(url, bitmap, score);
[email protected]449478302009-06-09 20:04:282405 } else {
2406 HistoryService* hs;
2407 if (!profile()->IsOffTheRecord() &&
2408 (hs = profile()->GetHistoryService(Profile::IMPLICIT_ACCESS))) {
2409 hs->SetPageThumbnail(url, bitmap, score);
2410 }
[email protected]420ae012009-04-24 05:16:322411 }
2412}
2413
[email protected]c4e98902010-06-01 10:20:142414void TabContents::UpdateInspectorSetting(const std::string& key,
2415 const std::string& value) {
2416 DictionaryValue* inspector_settings =
2417 profile()->GetPrefs()->GetMutableDictionary(
2418 prefs::kWebKitInspectorSettings);
[email protected]628cf5a52010-06-07 14:25:332419 inspector_settings->SetWithoutPathExpansion(UTF8ToWide(key),
2420 Value::CreateStringValue(value));
[email protected]0df30122009-06-03 12:13:082421}
2422
[email protected]45aafc92010-06-07 14:18:532423void TabContents::ClearInspectorSettings() {
2424 DictionaryValue* inspector_settings =
2425 profile()->GetPrefs()->GetMutableDictionary(
2426 prefs::kWebKitInspectorSettings);
2427 inspector_settings->Clear();
2428}
2429
[email protected]420ae012009-04-24 05:16:322430void TabContents::Close(RenderViewHost* rvh) {
[email protected]07707302009-11-06 00:50:292431 // The UI may be in an event-tracking loop, such as between the
2432 // mouse-down and mouse-up in text selection or a button click.
2433 // Defer the close until after tracking is complete, so that we
2434 // don't free objects out from under the UI.
2435 // TODO(shess): This could probably be integrated with the
2436 // IsDoingDrag() test below. Punting for now because I need more
2437 // research to understand how this impacts platforms other than Mac.
2438 // TODO(shess): This could get more fine-grained. For instance,
2439 // closing a tab in another window while selecting text in the
2440 // current window's Omnibox should be just fine.
2441 if (view()->IsEventTracking()) {
2442 view()->CloseTabAfterEventTracking();
2443 return;
2444 }
2445
[email protected]24a4d1062009-07-10 23:10:422446 // If we close the tab while we're in the middle of a drag, we'll crash.
2447 // Instead, cancel the drag and close it as soon as the drag ends.
2448 if (view()->IsDoingDrag()) {
2449 view()->CancelDragAndCloseTab();
2450 return;
2451 }
2452
[email protected]420ae012009-04-24 05:16:322453 // Ignore this if it comes from a RenderViewHost that we aren't showing.
2454 if (delegate() && rvh == render_view_host())
2455 delegate()->CloseContents(this);
2456}
2457
2458void TabContents::RequestMove(const gfx::Rect& new_bounds) {
2459 if (delegate() && delegate()->IsPopup(this))
2460 delegate()->MoveContents(this, new_bounds);
2461}
2462
[email protected]7ab1e7d62009-10-14 23:32:012463void TabContents::DidStartLoading() {
[email protected]420ae012009-04-24 05:16:322464 SetIsLoading(true, NULL);
2465}
2466
[email protected]7ab1e7d62009-10-14 23:32:012467void TabContents::DidStopLoading() {
[email protected]420ae012009-04-24 05:16:322468 scoped_ptr<LoadNotificationDetails> details;
2469
2470 NavigationEntry* entry = controller_.GetActiveEntry();
2471 // An entry may not exist for a stop when loading an initial blank page or
2472 // if an iframe injected by script into a blank page finishes loading.
2473 if (entry) {
[email protected]420ae012009-04-24 05:16:322474 base::TimeDelta elapsed = base::TimeTicks::Now() - current_load_start_;
2475
2476 details.reset(new LoadNotificationDetails(
[email protected]ebe89e062009-08-13 23:16:542477 entry->virtual_url(),
[email protected]420ae012009-04-24 05:16:322478 entry->transition_type(),
2479 elapsed,
2480 &controller_,
2481 controller_.GetCurrentEntryIndex()));
2482 }
2483
2484 // Tell PasswordManager we've finished a page load, which serves as a
2485 // green light to save pending passwords and reset itself.
2486 GetPasswordManager()->DidStopLoading();
2487
2488 SetIsLoading(false, details.get());
2489}
2490
[email protected]420ae012009-04-24 05:16:322491void TabContents::DidRedirectProvisionalLoad(int32 page_id,
2492 const GURL& source_url,
2493 const GURL& target_url) {
2494 NavigationEntry* entry;
2495 if (page_id == -1)
2496 entry = controller_.pending_entry();
2497 else
2498 entry = controller_.GetEntryWithPageID(GetSiteInstance(), page_id);
2499 if (!entry || entry->url() != source_url)
2500 return;
2501 entry->set_url(target_url);
2502}
2503
[email protected]420ae012009-04-24 05:16:322504void TabContents::RequestOpenURL(const GURL& url, const GURL& referrer,
2505 WindowOpenDisposition disposition) {
2506 if (render_manager_.dom_ui()) {
2507 // When we're a DOM UI, it will provide a page transition type for us (this
2508 // is so the new tab page can specify AUTO_BOOKMARK for automatically
2509 // generated suggestions).
2510 //
2511 // Note also that we hide the referrer for DOM UI pages. We don't really
[email protected]60e448982009-05-06 04:21:162512 // want web sites to see a referrer of "chrome://blah" (and some
2513 // chrome: URLs might have search terms or other stuff we don't want to
[email protected]420ae012009-04-24 05:16:322514 // send to the site), so we send no referrer.
2515 OpenURL(url, GURL(), disposition,
2516 render_manager_.dom_ui()->link_transition_type());
2517 } else {
2518 OpenURL(url, referrer, disposition, PageTransition::LINK);
2519 }
2520}
2521
[email protected]1ee614862009-07-06 20:29:182522void TabContents::DomOperationResponse(const std::string& json_string,
2523 int automation_id) {
2524}
2525
[email protected]420ae012009-04-24 05:16:322526void TabContents::ProcessDOMUIMessage(const std::string& message,
[email protected]438c97d2010-05-21 23:30:152527 const ListValue* content,
[email protected]bb64b512010-04-02 21:01:392528 const GURL& source_url,
[email protected]811bfe32009-07-01 08:46:252529 int request_id,
2530 bool has_callback) {
[email protected]420ae012009-04-24 05:16:322531 if (!render_manager_.dom_ui()) {
[email protected]c2e74fe82009-09-03 17:57:442532 // This can happen if someone uses window.open() to open an extension URL
2533 // from a non-extension context.
2534 render_view_host()->BlockExtensionRequest(request_id);
[email protected]420ae012009-04-24 05:16:322535 return;
2536 }
[email protected]bb64b512010-04-02 21:01:392537 render_manager_.dom_ui()->ProcessDOMUIMessage(message, content, source_url,
2538 request_id,
[email protected]811bfe32009-07-01 08:46:252539 has_callback);
[email protected]420ae012009-04-24 05:16:322540}
2541
2542void TabContents::ProcessExternalHostMessage(const std::string& message,
2543 const std::string& origin,
2544 const std::string& target) {
2545 if (delegate())
2546 delegate()->ForwardMessageToExternalHost(message, origin, target);
2547}
2548
[email protected]cdaf8d02010-03-30 19:52:472549void TabContents::RunFileChooser(
2550 const ViewHostMsg_RunFileChooser_Params &params) {
[email protected]420ae012009-04-24 05:16:322551 if (!select_file_dialog_.get())
2552 select_file_dialog_ = SelectFileDialog::Create(this);
[email protected]cdaf8d02010-03-30 19:52:472553
2554 SelectFileDialog::Type dialog_type;
2555 switch (params.mode) {
2556 case ViewHostMsg_RunFileChooser_Params::Open:
2557 dialog_type = SelectFileDialog::SELECT_OPEN_FILE;
2558 break;
2559 case ViewHostMsg_RunFileChooser_Params::OpenMultiple:
2560 dialog_type = SelectFileDialog::SELECT_OPEN_MULTI_FILE;
2561 break;
2562 case ViewHostMsg_RunFileChooser_Params::Save:
2563 dialog_type = SelectFileDialog::SELECT_SAVEAS_FILE;
2564 break;
2565 default:
[email protected]bed31952010-03-30 20:07:592566 dialog_type = SelectFileDialog::SELECT_OPEN_FILE; // Prevent warning.
[email protected]cdaf8d02010-03-30 19:52:472567 NOTREACHED();
2568 }
[email protected]35896a32010-06-09 08:42:512569 FilePath default_file_name = params.default_file_name;
2570 if (default_file_name.empty())
2571 default_file_name = profile()->last_selected_directory();
[email protected]cdaf8d02010-03-30 19:52:472572 select_file_dialog_->SelectFile(dialog_type, params.title,
[email protected]35896a32010-06-09 08:42:512573 default_file_name,
[email protected]420ae012009-04-24 05:16:322574 NULL, 0, FILE_PATH_LITERAL(""),
2575 view_->GetTopLevelNativeWindow(), NULL);
2576}
2577
2578void TabContents::RunJavaScriptMessage(
2579 const std::wstring& message,
2580 const std::wstring& default_prompt,
2581 const GURL& frame_url,
2582 const int flags,
2583 IPC::Message* reply_msg,
2584 bool* did_suppress_message) {
2585 // Suppress javascript messages when requested and when inside a constrained
2586 // popup window (because that activates them and breaks them out of the
2587 // constrained window jail).
[email protected]91ed49a2009-07-07 21:29:552588 // Also suppress messages when showing an interstitial. The interstitial is
2589 // shown over the previous page, we don't want the hidden page dialogs to
2590 // interfere with the interstitial.
2591 bool suppress_this_message = suppress_javascript_messages_ ||
2592 showing_interstitial_page();
[email protected]420ae012009-04-24 05:16:322593 if (delegate())
2594 suppress_this_message |=
[email protected]b6c874582009-05-08 19:38:312595 (delegate()->GetConstrainingContents(this) != this);
[email protected]420ae012009-04-24 05:16:322596
2597 *did_suppress_message = suppress_this_message;
2598
2599 if (!suppress_this_message) {
2600 base::TimeDelta time_since_last_message(
2601 base::TimeTicks::Now() - last_javascript_message_dismissal_);
2602 bool show_suppress_checkbox = false;
2603 // Show a checkbox offering to suppress further messages if this message is
2604 // being displayed within kJavascriptMessageExpectedDelay of the last one.
2605 if (time_since_last_message <
2606 base::TimeDelta::FromMilliseconds(kJavascriptMessageExpectedDelay))
2607 show_suppress_checkbox = true;
2608
[email protected]d82ed61e2009-06-16 02:46:222609 RunJavascriptMessageBox(this, frame_url, flags, message, default_prompt,
[email protected]420ae012009-04-24 05:16:322610 show_suppress_checkbox, reply_msg);
2611 } else {
2612 // If we are suppressing messages, just reply as is if the user immediately
2613 // pressed "Cancel".
[email protected]beb440c2009-11-06 04:08:542614 OnMessageBoxClosed(reply_msg, false, std::wstring());
[email protected]420ae012009-04-24 05:16:322615 }
2616}
2617
2618void TabContents::RunBeforeUnloadConfirm(const std::wstring& message,
2619 IPC::Message* reply_msg) {
[email protected]7ab1e7d62009-10-14 23:32:012620 is_showing_before_unload_dialog_ = true;
[email protected]d82ed61e2009-06-16 02:46:222621 RunBeforeUnloadDialog(this, message, reply_msg);
[email protected]420ae012009-04-24 05:16:322622}
2623
2624void TabContents::ShowModalHTMLDialog(const GURL& url, int width, int height,
2625 const std::string& json_arguments,
2626 IPC::Message* reply_msg) {
2627 if (delegate()) {
2628 HtmlDialogUIDelegate* dialog_delegate =
2629 new ModalHtmlDialogDelegate(url, width, height, json_arguments,
[email protected]d82ed61e2009-06-16 02:46:222630 reply_msg, this);
[email protected]420ae012009-04-24 05:16:322631 delegate()->ShowHtmlDialog(dialog_delegate, NULL);
2632 }
2633}
2634
[email protected]4d44a1c2010-04-28 19:20:412635void TabContents::PasswordFormsFound(
[email protected]ed3fb032009-06-16 19:50:562636 const std::vector<webkit_glue::PasswordForm>& forms) {
[email protected]4d44a1c2010-04-28 19:20:412637 GetPasswordManager()->PasswordFormsFound(forms);
2638}
2639
2640void TabContents::PasswordFormsVisible(
2641 const std::vector<webkit_glue::PasswordForm>& visible_forms) {
2642 GetPasswordManager()->PasswordFormsVisible(visible_forms);
[email protected]420ae012009-04-24 05:16:322643}
2644
[email protected]420ae012009-04-24 05:16:322645// Checks to see if we should generate a keyword based on the OSDD, and if
2646// necessary uses TemplateURLFetcher to download the OSDD and create a keyword.
2647void TabContents::PageHasOSDD(RenderViewHost* render_view_host,
2648 int32 page_id, const GURL& url,
2649 bool autodetected) {
2650 // Make sure page_id is the current page, and the TemplateURLModel is loaded.
2651 DCHECK(url.is_valid());
2652 if (!IsActiveEntry(page_id))
2653 return;
2654 TemplateURLModel* url_model = profile()->GetTemplateURLModel();
2655 if (!url_model)
2656 return;
2657 if (!url_model->loaded()) {
2658 url_model->Load();
2659 return;
2660 }
2661 if (!profile()->GetTemplateURLFetcher())
2662 return;
2663
2664 if (profile()->IsOffTheRecord())
2665 return;
2666
2667 const NavigationEntry* entry = controller_.GetLastCommittedEntry();
2668 DCHECK(entry);
2669
2670 const NavigationEntry* base_entry = entry;
2671 if (IsFormSubmit(base_entry)) {
2672 // If the current page is a form submit, find the last page that was not
2673 // a form submit and use its url to generate the keyword from.
2674 int index = controller_.last_committed_entry_index() - 1;
2675 while (index >= 0 && IsFormSubmit(controller_.GetEntryAtIndex(index)))
2676 index--;
2677 if (index >= 0)
2678 base_entry = controller_.GetEntryAtIndex(index);
2679 else
2680 base_entry = NULL;
2681 }
2682
2683 // We want to use the user typed URL if available since that represents what
2684 // the user typed to get here, and fall back on the regular URL if not.
2685 if (!base_entry)
2686 return;
2687 GURL keyword_url = base_entry->user_typed_url().is_valid() ?
2688 base_entry->user_typed_url() : base_entry->url();
2689 if (!keyword_url.is_valid())
2690 return;
2691 std::wstring keyword = TemplateURLModel::GenerateKeyword(keyword_url,
2692 autodetected);
[email protected]a88dbdd52010-01-15 19:25:532693
2694 // For JS added OSDD empty keyword is OK because we will generate keyword
2695 // later from OSDD content.
2696 if (autodetected && keyword.empty())
[email protected]420ae012009-04-24 05:16:322697 return;
2698 const TemplateURL* template_url =
2699 url_model->GetTemplateURLForKeyword(keyword);
2700 if (template_url && (!template_url->safe_for_autoreplace() ||
2701 template_url->originating_url() == url)) {
2702 // Either there is a user created TemplateURL for this keyword, or the
2703 // keyword has the same OSDD url and we've parsed it.
2704 return;
2705 }
2706
2707 // Download the OpenSearch description document. If this is successful a
2708 // new keyword will be created when done.
[email protected]420ae012009-04-24 05:16:322709 profile()->GetTemplateURLFetcher()->ScheduleDownload(
2710 keyword,
2711 url,
2712 base_entry->favicon().url(),
[email protected]d0cc9fb52009-06-23 02:26:162713 this,
[email protected]420ae012009-04-24 05:16:322714 autodetected);
2715}
2716
[email protected]420ae012009-04-24 05:16:322717GURL TabContents::GetAlternateErrorPageURL() const {
2718 GURL url;
2719 // Disable alternate error pages when in OffTheRecord/Incognito mode.
2720 if (profile()->IsOffTheRecord())
2721 return url;
2722
2723 PrefService* prefs = profile()->GetPrefs();
2724 DCHECK(prefs);
2725 if (prefs->GetBoolean(prefs::kAlternateErrorPagesEnabled)) {
[email protected]a33457b82009-09-21 23:07:152726 url = google_util::AppendGoogleLocaleParam(
2727 GURL(google_util::kLinkDoctorBaseURL));
[email protected]420ae012009-04-24 05:16:322728 url = google_util::AppendGoogleTLDParam(url);
2729 }
2730 return url;
2731}
2732
2733WebPreferences TabContents::GetWebkitPrefs() {
[email protected]6c32ce72010-03-08 05:18:062734 Profile* profile = render_view_host()->process()->profile();
[email protected]7ccddb8c2009-08-04 17:36:552735 bool is_dom_ui = false;
[email protected]6c32ce72010-03-08 05:18:062736 return RenderViewHostDelegateHelper::GetWebkitPrefs(profile, is_dom_ui);
[email protected]420ae012009-04-24 05:16:322737}
2738
[email protected]fa1cf0b82010-01-15 21:49:442739void TabContents::OnIgnoredUIEvent() {
2740 if (constrained_window_count()) {
2741 ConstrainedWindow* window = *constrained_window_begin();
2742 window->FocusConstrainedWindow();
2743 }
2744}
2745
[email protected]420ae012009-04-24 05:16:322746void TabContents::OnJSOutOfMemory() {
2747 AddInfoBar(new SimpleAlertInfoBarDelegate(
[email protected]938e1f92010-04-01 18:09:422748 this, l10n_util::GetString(IDS_JS_OUT_OF_MEMORY_PROMPT), NULL, true));
[email protected]420ae012009-04-24 05:16:322749}
2750
[email protected]420ae012009-04-24 05:16:322751void TabContents::OnCrossSiteResponse(int new_render_process_host_id,
2752 int new_request_id) {
[email protected]57c6a652009-05-04 07:58:342753 // Allows the TabContents to react when a cross-site response is ready to be
[email protected]420ae012009-04-24 05:16:322754 // delivered to a pending RenderViewHost. We must first run the onunload
2755 // handler of the old RenderViewHost before we can allow it to proceed.
2756 render_manager_.OnCrossSiteResponse(new_render_process_host_id,
2757 new_request_id);
2758}
2759
[email protected]3e108342010-07-16 03:09:122760bool TabContents::CanBlur() const {
2761 return delegate() ? delegate()->CanBlur() : true;
2762}
2763
[email protected]420ae012009-04-24 05:16:322764gfx::Rect TabContents::GetRootWindowResizerRect() const {
2765 if (delegate())
2766 return delegate()->GetRootWindowResizerRect();
2767 return gfx::Rect();
2768}
2769
2770void TabContents::RendererUnresponsive(RenderViewHost* rvh,
2771 bool is_during_unload) {
2772 if (is_during_unload) {
2773 // Hang occurred while firing the beforeunload/unload handler.
2774 // Pretend the handler fired so tab closing continues as if it had.
[email protected]829e7612009-04-25 01:15:112775 rvh->set_sudden_termination_allowed(true);
[email protected]420ae012009-04-24 05:16:322776
2777 if (!render_manager_.ShouldCloseTabOnUnresponsiveRenderer())
2778 return;
2779
2780 // If the tab hangs in the beforeunload/unload handler there's really
2781 // nothing we can do to recover. Pretend the unload listeners have
2782 // all fired and close the tab. If the hang is in the beforeunload handler
2783 // then the user will not have the option of cancelling the close.
2784 Close(rvh);
2785 return;
2786 }
2787
2788 if (render_view_host() && render_view_host()->IsRenderViewLive())
[email protected]e7b8d832009-11-20 22:41:002789 hung_renderer_dialog::ShowForTabContents(this);
[email protected]420ae012009-04-24 05:16:322790}
2791
2792void TabContents::RendererResponsive(RenderViewHost* render_view_host) {
[email protected]e7b8d832009-11-20 22:41:002793 hung_renderer_dialog::HideForTabContents(this);
[email protected]420ae012009-04-24 05:16:322794}
2795
2796void TabContents::LoadStateChanged(const GURL& url,
[email protected]094e5b22009-09-25 04:23:562797 net::LoadState load_state,
2798 uint64 upload_position,
2799 uint64 upload_size) {
[email protected]420ae012009-04-24 05:16:322800 load_state_ = load_state;
[email protected]094e5b22009-09-25 04:23:562801 upload_position_ = upload_position;
2802 upload_size_ = upload_size;
[email protected]d686e812009-06-03 19:10:292803 std::wstring languages =
[email protected]ddd231e2010-06-29 20:35:192804 UTF8ToWide(profile()->GetPrefs()->GetString(prefs::kAcceptLanguages));
[email protected]d686e812009-06-03 19:10:292805 std::string host = url.host();
[email protected]ce85f602009-11-07 01:34:532806 load_state_host_ =
2807 net::IDNToUnicode(host.c_str(), host.size(), languages, NULL);
[email protected]420ae012009-04-24 05:16:322808 if (load_state_ == net::LOAD_STATE_READING_RESPONSE)
2809 SetNotWaitingForResponse();
2810 if (is_loading())
[email protected]c9cd2222009-05-06 05:16:502811 NotifyNavigationStateChanged(INVALIDATE_LOAD | INVALIDATE_TAB);
[email protected]420ae012009-04-24 05:16:322812}
2813
[email protected]1ee614862009-07-06 20:29:182814bool TabContents::IsExternalTabContainer() const {
2815 if (!delegate())
2816 return false;
2817
2818 return delegate()->IsExternalTabContainer();
2819}
2820
[email protected]1ee614862009-07-06 20:29:182821void TabContents::DidInsertCSS() {
2822 // This RVHDelegate function is used for extensions and not us.
[email protected]420ae012009-04-24 05:16:322823}
2824
[email protected]130efb02009-09-18 18:54:352825void TabContents::FocusedNodeChanged() {
2826 NotificationService::current()->Notify(
2827 NotificationType::FOCUS_CHANGED_IN_PAGE,
2828 Source<RenderViewHost>(render_view_host()),
2829 NotificationService::NoDetails());
2830}
2831
[email protected]420ae012009-04-24 05:16:322832void TabContents::FileSelected(const FilePath& path,
2833 int index, void* params) {
[email protected]35896a32010-06-09 08:42:512834 profile()->set_last_selected_directory(path.DirName());
[email protected]cdaf8d02010-03-30 19:52:472835 std::vector<FilePath> files;
2836 files.push_back(path);
2837 render_view_host()->FilesSelectedInChooser(files);
[email protected]420ae012009-04-24 05:16:322838}
2839
2840void TabContents::MultiFilesSelected(const std::vector<FilePath>& files,
2841 void* params) {
[email protected]35896a32010-06-09 08:42:512842 if (!files.empty())
2843 profile()->set_last_selected_directory(files[0].DirName());
[email protected]cdaf8d02010-03-30 19:52:472844 render_view_host()->FilesSelectedInChooser(files);
[email protected]420ae012009-04-24 05:16:322845}
2846
2847void TabContents::FileSelectionCanceled(void* params) {
2848 // If the user cancels choosing a file to upload we pass back an
2849 // empty vector.
[email protected]cdaf8d02010-03-30 19:52:472850 render_view_host()->FilesSelectedInChooser(std::vector<FilePath>());
[email protected]420ae012009-04-24 05:16:322851}
2852
2853void TabContents::BeforeUnloadFiredFromRenderManager(
2854 bool proceed,
2855 bool* proceed_to_fire_unload) {
2856 if (delegate())
2857 delegate()->BeforeUnloadFired(this, proceed, proceed_to_fire_unload);
2858}
2859
[email protected]3a3d47472010-07-15 21:03:542860void TabContents::DidStartLoadingFromRenderManager(
2861 RenderViewHost* render_view_host) {
2862 DidStartLoading();
2863}
2864
2865void TabContents::RenderViewGoneFromRenderManager(
2866 RenderViewHost* render_view_host) {
2867 RenderViewGone(render_view_host);
2868}
2869
[email protected]420ae012009-04-24 05:16:322870void TabContents::UpdateRenderViewSizeForRenderManager() {
2871 // TODO(brettw) this is a hack. See TabContentsView::SizeContents.
[email protected]0323ee42010-02-17 22:03:262872 gfx::Size size = view_->GetContainerSize();
2873 // 0x0 isn't a valid window size (minimal window size is 1x1) but it may be
2874 // here during container initialization and normal window size will be set
2875 // later. In case of tab duplication this resizing to 0x0 prevents setting
2876 // normal size later so just ignore it.
2877 if (!size.IsEmpty())
2878 view_->SizeContents(size);
[email protected]420ae012009-04-24 05:16:322879}
2880
[email protected]3a3d47472010-07-15 21:03:542881void TabContents::NotifySwappedFromRenderManager() {
2882 NotifySwapped();
2883}
2884
2885NavigationController& TabContents::GetControllerForRenderManager() {
2886 return controller();
2887}
2888
[email protected]420ae012009-04-24 05:16:322889DOMUI* TabContents::CreateDOMUIForRenderManager(const GURL& url) {
[email protected]d82ed61e2009-06-16 02:46:222890 return DOMUIFactory::CreateDOMUIForURL(this, url);
[email protected]420ae012009-04-24 05:16:322891}
2892
2893NavigationEntry*
2894TabContents::GetLastCommittedNavigationEntryForRenderManager() {
2895 return controller_.GetLastCommittedEntry();
2896}
2897
2898bool TabContents::CreateRenderViewForRenderManager(
2899 RenderViewHost* render_view_host) {
[email protected]420ae012009-04-24 05:16:322900 RenderWidgetHostView* rwh_view = view_->CreateViewForWidget(render_view_host);
[email protected]1a98a932009-11-17 00:12:522901
2902 scoped_refptr<URLRequestContextGetter> request_context = request_context_;
2903 if (!request_context.get())
2904 request_context = profile()->GetRequestContext();
2905
[email protected]8ab04652010-06-12 02:47:262906 if (!render_view_host->CreateRenderView(request_context, string16()))
[email protected]420ae012009-04-24 05:16:322907 return false;
2908
2909 // Now that the RenderView has been created, we need to tell it its size.
2910 rwh_view->SetSize(view_->GetContainerSize());
2911
2912 UpdateMaxPageIDIfNecessary(render_view_host->site_instance(),
2913 render_view_host);
2914 return true;
2915}
2916
2917void TabContents::Observe(NotificationType type,
2918 const NotificationSource& source,
2919 const NotificationDetails& details) {
2920 switch (type.value) {
2921 case NotificationType::BOOKMARK_MODEL_LOADED:
2922 // BookmarkModel finished loading, fall through to update starred state.
2923 case NotificationType::URLS_STARRED: {
2924 // Somewhere, a URL has been starred.
2925 // Ignore notifications for profiles other than our current one.
2926 Profile* source_profile = Source<Profile>(source).ptr();
2927 if (!source_profile || !source_profile->IsSameProfile(profile()))
2928 return;
2929
2930 UpdateStarredStateForCurrentURL();
2931 break;
2932 }
2933 case NotificationType::PREF_CHANGED: {
2934 std::wstring* pref_name_in = Details<std::wstring>(details).ptr();
2935 DCHECK(Source<PrefService>(source).ptr() == profile()->GetPrefs());
2936 if (*pref_name_in == prefs::kAlternateErrorPagesEnabled) {
2937 UpdateAlternateErrorPageURL();
2938 } else if (*pref_name_in == prefs::kDefaultCharset ||
2939 StartsWithASCII(WideToUTF8(*pref_name_in), "webkit.webprefs.", true)
2940 ) {
2941 UpdateWebPreferences();
2942 } else {
2943 NOTREACHED() << "unexpected pref change notification" << *pref_name_in;
2944 }
2945 break;
2946 }
2947 case NotificationType::RENDER_WIDGET_HOST_DESTROYED:
2948 view_->RenderWidgetHostDestroyed(Source<RenderWidgetHost>(source).ptr());
2949 break;
2950
2951 case NotificationType::NAV_ENTRY_COMMITTED: {
2952 DCHECK(&controller_ == Source<NavigationController>(source).ptr());
2953
2954 NavigationController::LoadCommittedDetails& committed_details =
2955 *(Details<NavigationController::LoadCommittedDetails>(details).ptr());
2956 ExpireInfoBars(committed_details);
2957 break;
2958 }
2959
[email protected]d3ba77272009-09-03 00:06:092960#if defined(OS_LINUX)
2961 case NotificationType::BROWSER_THEME_CHANGED: {
[email protected]7ff431e2010-01-07 18:18:542962 renderer_preferences_util::UpdateFromSystemSettings(
2963 &renderer_preferences_, profile());
[email protected]d3ba77272009-09-03 00:06:092964 render_view_host()->SyncRendererPrefs();
2965 break;
2966 }
2967#endif
2968
[email protected]6c32ce72010-03-08 05:18:062969 case NotificationType::USER_STYLE_SHEET_UPDATED:
2970 UpdateWebPreferences();
2971 break;
2972
[email protected]79580c62010-02-02 02:36:252973 case NotificationType::CONTENT_SETTINGS_CHANGED: {
[email protected]0314ae02010-04-08 09:18:292974 Details<const HostContentSettingsMap::ContentSettingsDetails>
[email protected]79580c62010-02-02 02:36:252975 settings_details(details);
[email protected]79580c62010-02-02 02:36:252976 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]da0c8e92010-02-09 23:25:492977 GURL entry_url;
[email protected]0314ae02010-04-08 09:18:292978 if (entry)
[email protected]da0c8e92010-02-09 23:25:492979 entry_url = entry->url();
[email protected]0314ae02010-04-08 09:18:292980 if (settings_details.ptr()->update_all() ||
2981 settings_details.ptr()->pattern().Matches(entry_url)) {
2982 render_view_host()->SendContentSettings(entry_url,
[email protected]b9a4ea7f2010-06-25 07:11:232983 profile()->GetHostContentSettingsMap()->
2984 GetContentSettings(entry_url));
[email protected]79580c62010-02-02 02:36:252985 }
2986 break;
2987 }
2988
[email protected]e5adff72010-06-14 18:13:062989 case NotificationType::EXTENSION_LOADED:
[email protected]a888b29e62010-04-01 13:38:572990 break;
[email protected]a888b29e62010-04-01 13:38:572991
2992 case NotificationType::EXTENSION_UNLOADED:
[email protected]e5adff72010-06-14 18:13:062993 case NotificationType::EXTENSION_UNLOADED_DISABLED:
[email protected]a888b29e62010-04-01 13:38:572994 break;
[email protected]a888b29e62010-04-01 13:38:572995
[email protected]420ae012009-04-24 05:16:322996 default:
2997 NOTREACHED();
2998 }
2999}
[email protected]aacd7cc2009-09-02 14:51:103000
[email protected]98aa0b52010-05-06 17:03:083001void TabContents::UpdateExtensionAppIcon(Extension* extension) {
3002 extension_app_icon_.reset();
[email protected]f075fe0a22010-04-26 04:06:433003
[email protected]a888b29e62010-04-01 13:38:573004 if (extension) {
[email protected]98aa0b52010-05-06 17:03:083005 extension_app_image_loader_.reset(new ImageLoadingTracker(this));
3006 extension_app_image_loader_->LoadImage(
[email protected]a888b29e62010-04-01 13:38:573007 extension,
3008 extension->GetIconPath(Extension::EXTENSION_ICON_SMALLISH),
3009 gfx::Size(Extension::EXTENSION_ICON_SMALLISH,
3010 Extension::EXTENSION_ICON_SMALLISH),
3011 ImageLoadingTracker::CACHE);
3012 } else {
[email protected]98aa0b52010-05-06 17:03:083013 extension_app_image_loader_.reset(NULL);
[email protected]a888b29e62010-04-01 13:38:573014 }
3015}
3016
3017Extension* TabContents::GetExtensionContaining(const GURL& url) {
3018 ExtensionsService* extensions_service = profile()->GetExtensionsService();
3019 if (!extensions_service)
3020 return NULL;
3021
3022 Extension* extension = extensions_service->GetExtensionByURL(url);
3023 return extension ?
3024 extension : extensions_service->GetExtensionByWebExtent(url);
3025}
3026
[email protected]a888b29e62010-04-01 13:38:573027void TabContents::OnImageLoaded(SkBitmap* image, ExtensionResource resource,
3028 int index) {
3029 if (image) {
[email protected]98aa0b52010-05-06 17:03:083030 extension_app_icon_ = *image;
[email protected]a888b29e62010-04-01 13:38:573031 NotifyNavigationStateChanged(INVALIDATE_TAB);
3032 }
3033}
3034
[email protected]beb440c2009-11-06 04:08:543035std::wstring TabContents::GetMessageBoxTitle(const GURL& frame_url,
3036 bool is_alert) {
3037 if (!frame_url.has_host())
3038 return l10n_util::GetString(
3039 is_alert ? IDS_JAVASCRIPT_ALERT_DEFAULT_TITLE
3040 : IDS_JAVASCRIPT_MESSAGEBOX_DEFAULT_TITLE);
3041
3042 // We really only want the scheme, hostname, and port.
3043 GURL::Replacements replacements;
3044 replacements.ClearUsername();
3045 replacements.ClearPassword();
3046 replacements.ClearPath();
3047 replacements.ClearQuery();
3048 replacements.ClearRef();
3049 GURL clean_url = frame_url.ReplaceComponents(replacements);
3050
3051 // TODO(brettw) it should be easier than this to do the correct language
3052 // handling without getting the accept language from the profile.
3053 std::wstring base_address = gfx::ElideUrl(clean_url, gfx::Font(), 0,
[email protected]ddd231e2010-06-29 20:35:193054 UTF8ToWide(profile()->GetPrefs()->GetString(prefs::kAcceptLanguages)));
[email protected]beb440c2009-11-06 04:08:543055 // Force URL to have LTR directionality.
[email protected]4a4afe32010-05-05 01:39:563056 base::i18n::GetDisplayStringInLTRDirectionality(&base_address);
[email protected]beb440c2009-11-06 04:08:543057
3058 return l10n_util::GetStringF(
3059 is_alert ? IDS_JAVASCRIPT_ALERT_TITLE : IDS_JAVASCRIPT_MESSAGEBOX_TITLE,
3060 base_address);
3061}
3062
3063gfx::NativeWindow TabContents::GetMessageBoxRootWindow() {
3064 return view_->GetTopLevelNativeWindow();
3065}
3066
3067void TabContents::OnMessageBoxClosed(IPC::Message* reply_msg,
3068 bool success,
3069 const std::wstring& prompt) {
3070 last_javascript_message_dismissal_ = base::TimeTicks::Now();
3071 if (is_showing_before_unload_dialog_ && !success) {
3072 // If a beforeunload dialog is canceled, we need to stop the throbber from
3073 // spinning, since we forced it to start spinning in Navigate.
3074 DidStopLoading();
3075
3076 tab_close_start_time_ = base::TimeTicks();
3077 }
3078 is_showing_before_unload_dialog_ = false;
3079 render_view_host()->JavaScriptMessageBoxClosed(reply_msg, success, prompt);
3080}
3081
3082void TabContents::SetSuppressMessageBoxes(bool suppress_message_boxes) {
3083 set_suppress_javascript_messages(suppress_message_boxes);
3084}
3085
[email protected]3a3d47472010-07-15 21:03:543086TabContents* TabContents::AsTabContents() {
3087 return this;
3088}
3089
3090ExtensionHost* TabContents::AsExtensionHost() {
3091 return NULL;
3092}
3093
[email protected]41fc0322009-09-04 22:23:403094void TabContents::set_encoding(const std::string& encoding) {
[email protected]aacd7cc2009-09-02 14:51:103095 encoding_ = CharacterEncoding::GetCanonicalEncodingNameByAliasName(encoding);
3096}
[email protected]f45d2a72010-03-08 23:28:353097
3098void TabContents::SetAppIcon(const SkBitmap& app_icon) {
3099 app_icon_ = app_icon;
3100 NotifyNavigationStateChanged(INVALIDATE_TITLE);
3101}
[email protected]ea68c06a2010-04-23 17:25:373102
3103// After a successful *new* login attempt, we take the PasswordFormManager in
3104// provisional_save_manager_ and move it to a SavePasswordInfoBarDelegate while
3105// the user makes up their mind with the "save password" infobar. Note if the
3106// login is one we already know about, the end of the line is
3107// provisional_save_manager_ because we just update it on success and so such
3108// forms never end up in an infobar.
3109class SavePasswordInfoBarDelegate : public ConfirmInfoBarDelegate {
3110 public:
3111 SavePasswordInfoBarDelegate(TabContents* tab_contents,
[email protected]12bb6822010-06-16 02:00:463112 PasswordFormManager* form_to_save)
3113 : ConfirmInfoBarDelegate(tab_contents),
3114 form_to_save_(form_to_save),
3115 infobar_response_(kNoResponse) {
[email protected]ea68c06a2010-04-23 17:25:373116 }
3117
3118 virtual ~SavePasswordInfoBarDelegate() { }
3119
3120 // Overridden from ConfirmInfoBarDelegate:
3121 virtual void InfoBarClosed() {
[email protected]12bb6822010-06-16 02:00:463122 UMA_HISTOGRAM_ENUMERATION("PasswordManager.InfoBarResponse",
3123 infobar_response_, kNumResponseTypes);
[email protected]ea68c06a2010-04-23 17:25:373124 delete this;
3125 }
3126
3127 virtual std::wstring GetMessageText() const {
3128 return l10n_util::GetString(IDS_PASSWORD_MANAGER_SAVE_PASSWORD_PROMPT);
3129 }
3130
3131 virtual SkBitmap* GetIcon() const {
3132 return ResourceBundle::GetSharedInstance().GetBitmapNamed(
3133 IDR_INFOBAR_SAVE_PASSWORD);
3134 }
3135
3136 virtual int GetButtons() const {
3137 return BUTTON_OK | BUTTON_CANCEL;
3138 }
3139
3140 virtual std::wstring GetButtonLabel(InfoBarButton button) const {
3141 if (button == BUTTON_OK)
3142 return l10n_util::GetString(IDS_PASSWORD_MANAGER_SAVE_BUTTON);
3143 if (button == BUTTON_CANCEL)
3144 return l10n_util::GetString(IDS_PASSWORD_MANAGER_BLACKLIST_BUTTON);
3145 NOTREACHED();
3146 return std::wstring();
3147 }
3148
3149 virtual bool Accept() {
3150 DCHECK(form_to_save_.get());
3151 form_to_save_->Save();
[email protected]12bb6822010-06-16 02:00:463152 infobar_response_ = kRememberPassword;
[email protected]ea68c06a2010-04-23 17:25:373153 return true;
3154 }
3155
3156 virtual bool Cancel() {
3157 DCHECK(form_to_save_.get());
3158 form_to_save_->PermanentlyBlacklist();
[email protected]12bb6822010-06-16 02:00:463159 infobar_response_ = kDontRememberPassword;
[email protected]ea68c06a2010-04-23 17:25:373160 return true;
3161 }
3162
3163 private:
3164 // The PasswordFormManager managing the form we're asking the user about,
3165 // and should update as per her decision.
3166 scoped_ptr<PasswordFormManager> form_to_save_;
3167
[email protected]12bb6822010-06-16 02:00:463168 // Used to track the results we get from the info bar.
3169 enum ResponseType {
3170 kNoResponse = 0,
3171 kRememberPassword,
3172 kDontRememberPassword,
3173 kNumResponseTypes,
3174 };
3175 ResponseType infobar_response_;
3176
[email protected]ea68c06a2010-04-23 17:25:373177 DISALLOW_COPY_AND_ASSIGN(SavePasswordInfoBarDelegate);
3178};
3179
3180void TabContents::FillPasswordForm(
3181 const webkit_glue::PasswordFormDomManager::FillData& form_data) {
3182 render_view_host()->FillPasswordForm(form_data);
3183}
3184
3185void TabContents::AddSavePasswordInfoBar(PasswordFormManager* form_to_save) {
3186 AddInfoBar(new SavePasswordInfoBarDelegate(this, form_to_save));
3187}
3188
3189Profile* TabContents::GetProfileForPasswordManager() {
3190 return profile();
3191}
3192
3193bool TabContents::DidLastPageLoadEncounterSSLErrors() {
3194 return controller().ssl_manager()->ProcessedSSLErrorFromRequest();
3195}