blob: 78b2124cf4232b6fe1fb01b7c9db73956badfd99 [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"
37#include "chrome/browser/download/download_request_manager.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]ce560f82009-06-03 09:39:4456#include "chrome/browser/profile.h"
[email protected]1db6ff152009-10-12 15:32:0757#include "chrome/browser/renderer_host/render_process_host.h"
[email protected]8cb5d5b2010-02-09 11:36:1658#include "chrome/browser/renderer_host/render_view_host.h"
[email protected]96d185d2009-04-24 03:28:5459#include "chrome/browser/renderer_host/render_widget_host_view.h"
[email protected]fd694982009-07-03 17:32:3760#include "chrome/browser/renderer_host/resource_request_details.h"
[email protected]1db6ff152009-10-12 15:32:0761#include "chrome/browser/renderer_host/site_instance.h"
[email protected]96d185d2009-04-24 03:28:5462#include "chrome/browser/renderer_host/web_cache_manager.h"
[email protected]93623c5d2009-12-10 21:40:3263#include "chrome/browser/renderer_preferences_util.h"
[email protected]152b8ba32010-03-29 19:40:1664#include "chrome/browser/search_engines/template_url_fetcher.h"
65#include "chrome/browser/search_engines/template_url_model.h"
[email protected]c37e3b62010-01-22 00:15:1766#include "chrome/browser/sessions/session_types.h"
[email protected]bd1ad682009-05-15 22:19:1767#include "chrome/browser/tab_contents/infobar_delegate.h"
[email protected]7e383692009-06-12 19:14:5468#include "chrome/browser/tab_contents/interstitial_page.h"
[email protected]f3ec7742009-01-15 00:59:1669#include "chrome/browser/tab_contents/navigation_entry.h"
[email protected]de34a962009-06-26 23:22:4370#include "chrome/browser/tab_contents/provisional_load_details.h"
[email protected]f3ec7742009-01-15 00:59:1671#include "chrome/browser/tab_contents/tab_contents_delegate.h"
[email protected]fdd61c62009-04-22 19:22:5772#include "chrome/browser/tab_contents/tab_contents_view.h"
[email protected]58dca552009-06-17 00:35:0273#include "chrome/browser/tab_contents/thumbnail_generator.h"
[email protected]449478302009-06-09 20:04:2874#include "chrome/browser/thumbnail_store.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]bfd04a62009-02-01 18:16:5681#include "chrome/common/notification_service.h"
initial.commit09911bf2008-07-26 23:55:2982#include "chrome/common/pref_names.h"
[email protected]420ae012009-04-24 05:16:3283#include "chrome/common/render_messages.h"
[email protected]1ee614862009-07-06 20:29:1884#include "chrome/common/renderer_preferences.h"
[email protected]96d185d2009-04-24 03:28:5485#include "chrome/common/url_constants.h"
[email protected]ea68c06a2010-04-23 17:25:3786#include "grit/chromium_strings.h"
[email protected]8897c382009-05-06 17:53:2687#include "grit/generated_resources.h"
[email protected]420ae012009-04-24 05:16:3288#include "grit/locale_settings.h"
[email protected]fd626782010-01-22 05:26:0689#include "grit/platform_locale_settings.h"
[email protected]ea68c06a2010-04-23 17:25:3790#include "grit/theme_resources.h"
[email protected]420ae012009-04-24 05:16:3291#include "net/base/mime_util.h"
92#include "net/base/net_errors.h"
[email protected]d686e812009-06-03 19:10:2993#include "net/base/net_util.h"
[email protected]420ae012009-04-24 05:16:3294#include "net/base/registry_controlled_domain.h"
[email protected]1ee614862009-07-06 20:29:1895#include "webkit/glue/password_form.h"
96#include "webkit/glue/webpreferences.h"
[email protected]3e45ba92009-02-20 21:09:0097
[email protected]420ae012009-04-24 05:16:3298// Cross-Site Navigations
99//
100// If a TabContents is told to navigate to a different web site (as determined
101// by SiteInstance), it will replace its current RenderViewHost with a new
102// RenderViewHost dedicated to the new SiteInstance. This works as follows:
103//
104// - Navigate determines whether the destination is cross-site, and if so,
105// it creates a pending_render_view_host_ and moves into the PENDING
106// RendererState.
107// - The pending RVH is "suspended," so that no navigation messages are sent to
108// its renderer until the onbeforeunload JavaScript handler has a chance to
109// run in the current RVH.
110// - The pending RVH tells CrossSiteRequestManager (a thread-safe singleton)
111// that it has a pending cross-site request. ResourceDispatcherHost will
112// check for this when the response arrives.
113// - The current RVH runs its onbeforeunload handler. If it returns false, we
114// cancel all the pending logic and go back to NORMAL. Otherwise we allow
115// the pending RVH to send the navigation request to its renderer.
116// - ResourceDispatcherHost receives a ResourceRequest on the IO thread. It
117// checks CrossSiteRequestManager to see that the RVH responsible has a
118// pending cross-site request, and then installs a CrossSiteEventHandler.
119// - When RDH receives a response, the BufferedEventHandler determines whether
120// it is a download. If so, it sends a message to the new renderer causing
121// it to cancel the request, and the download proceeds in the download
122// thread. For now, we stay in a PENDING state (with a pending RVH) until
[email protected]57c6a652009-05-04 07:58:34123// the next DidNavigate event for this TabContents. This isn't ideal, but it
[email protected]420ae012009-04-24 05:16:32124// doesn't affect any functionality.
125// - After RDH receives a response and determines that it is safe and not a
126// download, it pauses the response to first run the old page's onunload
127// handler. It does this by asynchronously calling the OnCrossSiteResponse
[email protected]57c6a652009-05-04 07:58:34128// method of TabContents on the UI thread, which sends a ClosePage message
[email protected]420ae012009-04-24 05:16:32129// to the current RVH.
130// - Once the onunload handler is finished, a ClosePage_ACK message is sent to
131// the ResourceDispatcherHost, who unpauses the response. Data is then sent
132// to the pending RVH.
133// - The pending renderer sends a FrameNavigate message that invokes the
134// DidNavigate method. This replaces the current RVH with the
135// pending RVH and goes back to the NORMAL RendererState.
136
137namespace {
138
139// Amount of time we wait between when a key event is received and the renderer
140// is queried for its state and pushed to the NavigationEntry.
141const int kQueryStateDelay = 5000;
142
143const int kSyncWaitDelay = 40;
144
[email protected]304f8792009-08-19 21:28:09145#if defined(OS_CHROMEOS)
146// If a popup window is bigger than this fraction of the screen on chrome os,
147// turn it into a tab
148const float kMaxWidthFactor = 0.5;
149const float kMaxHeightFactor = 0.6;
150#endif
151
[email protected]420ae012009-04-24 05:16:32152// If another javascript message box is displayed within
153// kJavascriptMessageExpectedDelay of a previous javascript message box being
154// dismissed, display an option to suppress future message boxes from this
155// contents.
156const int kJavascriptMessageExpectedDelay = 1000;
157
[email protected]420ae012009-04-24 05:16:32158// The list of prefs we want to observe.
159const wchar_t* kPrefsToObserve[] = {
160 prefs::kAlternateErrorPagesEnabled,
161 prefs::kWebKitJavaEnabled,
162 prefs::kWebKitJavascriptEnabled,
163 prefs::kWebKitLoadsImagesAutomatically,
164 prefs::kWebKitPluginsEnabled,
165 prefs::kWebKitUsesUniversalDetector,
166 prefs::kWebKitSerifFontFamily,
167 prefs::kWebKitSansSerifFontFamily,
168 prefs::kWebKitFixedFontFamily,
169 prefs::kWebKitDefaultFontSize,
170 prefs::kWebKitDefaultFixedFontSize,
[email protected]1661a3c02010-05-20 16:32:36171 prefs::kWebkitTabsToLinks,
[email protected]420ae012009-04-24 05:16:32172 prefs::kDefaultCharset
173 // kWebKitStandardFontIsSerif needs to be added
174 // if we let users pick which font to use, serif or sans-serif when
175 // no font is specified or a CSS generic family (serif or sans-serif)
176 // is not specified.
177};
178
179const int kPrefsToObserveLength = arraysize(kPrefsToObserve);
180
[email protected]420ae012009-04-24 05:16:32181// Returns true if the entry's transition type is FORM_SUBMIT.
182bool IsFormSubmit(const NavigationEntry* entry) {
183 return (PageTransition::StripQualifier(entry->transition_type()) ==
184 PageTransition::FORM_SUBMIT);
185}
186
187#if defined(OS_WIN)
188
189BOOL CALLBACK InvalidateWindow(HWND hwnd, LPARAM lparam) {
190 // Note: erase is required to properly paint some widgets borders. This can
191 // be seen with textfields.
192 InvalidateRect(hwnd, NULL, TRUE);
193 return TRUE;
194}
195#endif
196
[email protected]5e369672009-11-03 23:48:30197ViewMsg_Navigate_Params::NavigationType GetNavigationType(
[email protected]1ccb3568d2010-02-19 10:51:16198 Profile* profile, const NavigationEntry& entry,
199 NavigationController::ReloadType reload_type) {
200 switch (reload_type) {
201 case NavigationController::RELOAD:
202 return ViewMsg_Navigate_Params::RELOAD;
203 case NavigationController::RELOAD_IGNORING_CACHE:
204 return ViewMsg_Navigate_Params::RELOAD_IGNORING_CACHE;
205 case NavigationController::NO_RELOAD:
206 break; // Fall through to rest of function.
207 }
[email protected]5e369672009-11-03 23:48:30208
209 if (entry.restore_type() == NavigationEntry::RESTORE_LAST_SESSION &&
210 profile->DidLastSessionExitCleanly())
211 return ViewMsg_Navigate_Params::RESTORE;
212
213 return ViewMsg_Navigate_Params::NORMAL;
214}
215
[email protected]3cc72b12010-03-18 23:03:00216void MakeNavigateParams(const NavigationController& controller,
[email protected]1ccb3568d2010-02-19 10:51:16217 NavigationController::ReloadType reload_type,
218 ViewMsg_Navigate_Params* params) {
[email protected]3cc72b12010-03-18 23:03:00219 const NavigationEntry& entry = *controller.pending_entry();
[email protected]056de2d2009-06-26 16:41:34220 params->page_id = entry.page_id();
[email protected]3cc72b12010-03-18 23:03:00221 params->pending_history_list_offset = controller.pending_entry_index();
222 params->current_history_list_offset = controller.last_committed_entry_index();
223 params->current_history_list_length = controller.entry_count();
[email protected]056de2d2009-06-26 16:41:34224 params->url = entry.url();
225 params->referrer = entry.referrer();
226 params->transition = entry.transition_type();
227 params->state = entry.content_state();
[email protected]3cc72b12010-03-18 23:03:00228 params->navigation_type =
229 GetNavigationType(controller.profile(), entry, reload_type);
[email protected]056de2d2009-06-26 16:41:34230 params->request_time = base::Time::Now();
231}
232
[email protected]420ae012009-04-24 05:16:32233} // namespace
234
235// -----------------------------------------------------------------------------
236
[email protected]fdd61c62009-04-22 19:22:57237// static
238int TabContents::find_request_id_counter_ = -1;
239
[email protected]420ae012009-04-24 05:16:32240
[email protected]420ae012009-04-24 05:16:32241TabContents::TabContents(Profile* profile,
242 SiteInstance* site_instance,
243 int routing_id,
[email protected]34ac70502009-09-25 17:07:23244 const TabContents* base_tab_contents)
[email protected]b680ad22009-04-15 23:19:42245 : delegate_(NULL),
[email protected]66ba4932009-06-04 19:22:13246 ALLOW_THIS_IN_INITIALIZER_LIST(controller_(this, profile)),
247 ALLOW_THIS_IN_INITIALIZER_LIST(view_(
[email protected]d82ed61e2009-06-16 02:46:22248 TabContentsView::Create(this))),
249 ALLOW_THIS_IN_INITIALIZER_LIST(render_manager_(this, this)),
[email protected]fdd61c62009-04-22 19:22:57250 property_bag_(),
251 registrar_(),
[email protected]d82ed61e2009-06-16 02:46:22252 ALLOW_THIS_IN_INITIALIZER_LIST(printing_(*this)),
[email protected]fdd61c62009-04-22 19:22:57253 save_package_(),
[email protected]6db0def92010-03-02 19:55:45254 autocomplete_history_manager_(),
[email protected]21674df2009-10-28 19:51:21255 autofill_manager_(),
[email protected]fdd61c62009-04-22 19:22:57256 password_manager_(),
257 plugin_installer_(),
[email protected]6f821dd2010-02-23 00:54:06258 bookmark_drag_(NULL),
[email protected]d82ed61e2009-06-16 02:46:22259 ALLOW_THIS_IN_INITIALIZER_LIST(fav_icon_helper_(this)),
[email protected]fdd61c62009-04-22 19:22:57260 select_file_dialog_(),
[email protected]d5f942ba2008-09-26 19:30:34261 is_loading_(false),
initial.commit09911bf2008-07-26 23:55:29262 is_crashed_(false),
[email protected]d5f942ba2008-09-26 19:30:34263 waiting_for_response_(false),
[email protected]d5f942ba2008-09-26 19:30:34264 max_page_id_(-1),
[email protected]fdd61c62009-04-22 19:22:57265 current_load_start_(),
266 load_state_(net::LOAD_STATE_IDLE),
267 load_state_host_(),
[email protected]094e5b22009-09-25 04:23:56268 upload_size_(0),
269 upload_position_(0),
[email protected]fdd61c62009-04-22 19:22:57270 received_page_title_(false),
271 is_starred_(false),
272 contents_mime_type_(),
273 encoding_(),
[email protected]332af7732009-03-11 13:21:35274 blocked_popups_(NULL),
[email protected]8cc8d492010-02-02 10:40:49275 dont_notify_render_view_(false),
[email protected]f17a0ee2010-05-17 17:38:47276 displayed_insecure_content_(false),
[email protected]fdd61c62009-04-22 19:22:57277 infobar_delegates_(),
[email protected]fdd61c62009-04-22 19:22:57278 find_ui_active_(false),
279 find_op_aborted_(false),
280 current_find_request_id_(find_request_id_counter_++),
[email protected]e491f1c2009-05-22 20:28:12281 last_search_case_sensitive_(false),
[email protected]e491f1c2009-05-22 20:28:12282 last_search_result_(),
[email protected]98aa0b52010-05-06 17:03:08283 extension_app_(NULL),
[email protected]fdd61c62009-04-22 19:22:57284 capturing_contents_(false),
285 is_being_destroyed_(false),
286 notify_disconnection_(false),
287 history_requests_(),
288#if defined(OS_WIN)
289 message_box_active_(CreateEvent(NULL, TRUE, FALSE, NULL)),
290#endif
291 last_javascript_message_dismissal_(),
[email protected]c2e74fe82009-09-03 17:57:44292 suppress_javascript_messages_(false),
[email protected]7ab1e7d62009-10-14 23:32:01293 is_showing_before_unload_dialog_(false),
[email protected]7ff431e2010-01-07 18:18:54294 renderer_preferences_(),
[email protected]1a242c32010-02-02 21:20:54295 opener_dom_ui_type_(DOMUIFactory::kNoDOMUI),
[email protected]777a7b62010-05-11 13:28:16296 language_state_(&controller_),
[email protected]b3df5a42010-05-11 14:31:09297 geolocation_settings_state_(profile),
[email protected]0cce15f2010-05-21 16:58:39298 closed_by_user_gesture_(false) {
[email protected]0de80162010-02-03 04:52:35299 ClearBlockedContentSettings();
[email protected]7ff431e2010-01-07 18:18:54300 renderer_preferences_util::UpdateFromSystemSettings(
301 &renderer_preferences_, profile);
302
[email protected]16779842009-07-08 23:45:29303#if defined(OS_CHROMEOS)
[email protected]58dca552009-06-17 00:35:02304 // Make sure the thumbnailer is started before starting the render manager.
305 // The thumbnailer will want to listen for RVH creations, one of which will
306 // happen in RVHManager::Init.
307 ThumbnailGenerator* generator = g_browser_process->GetThumbnailGenerator();
308 if (generator)
309 generator->StartThumbnailing();
310#endif
311
[email protected]12636df2009-09-28 22:32:21312 render_manager_.Init(profile, site_instance, routing_id);
[email protected]420ae012009-04-24 05:16:32313
[email protected]34ac70502009-09-25 17:07:23314 // We have the initial size of the view be based on the size of the passed in
315 // tab contents (normally a tab from the same window).
316 view_->CreateView(base_tab_contents ?
317 base_tab_contents->view()->GetContainerSize() : gfx::Size());
[email protected]420ae012009-04-24 05:16:32318
319 // Register for notifications about all interested prefs change.
320 PrefService* prefs = profile->GetPrefs();
321 if (prefs) {
322 for (int i = 0; i < kPrefsToObserveLength; ++i)
323 prefs->AddPrefObserver(kPrefsToObserve[i], this);
324 }
325
326 // Register for notifications about URL starredness changing on any profile.
327 registrar_.Add(this, NotificationType::URLS_STARRED,
328 NotificationService::AllSources());
329 registrar_.Add(this, NotificationType::BOOKMARK_MODEL_LOADED,
330 NotificationService::AllSources());
331 registrar_.Add(this, NotificationType::RENDER_WIDGET_HOST_DESTROYED,
332 NotificationService::AllSources());
[email protected]d3ba77272009-09-03 00:06:09333#if defined(OS_LINUX)
334 registrar_.Add(this, NotificationType::BROWSER_THEME_CHANGED,
335 NotificationService::AllSources());
336#endif
[email protected]420ae012009-04-24 05:16:32337
[email protected]6c32ce72010-03-08 05:18:06338 registrar_.Add(this, NotificationType::USER_STYLE_SHEET_UPDATED,
339 NotificationService::AllSources());
340
[email protected]79580c62010-02-02 02:36:25341 // Register for notifications about content setting changes.
342 registrar_.Add(this, NotificationType::CONTENT_SETTINGS_CHANGED,
343 NotificationService::AllSources());
344
[email protected]a888b29e62010-04-01 13:38:57345 // Listen for extension changes so we can update extension_for_current_page_.
346 registrar_.Add(this, NotificationType::EXTENSION_LOADED,
347 NotificationService::AllSources());
348 registrar_.Add(this, NotificationType::EXTENSION_UNLOADED,
349 NotificationService::AllSources());
350 registrar_.Add(this, NotificationType::EXTENSION_UNLOADED_DISABLED,
351 NotificationService::AllSources());
352
[email protected]c8865482009-07-23 20:40:10353 // Set-up the showing of the omnibox search infobar if applicable.
354 if (OmniboxSearchHint::IsEnabled(profile))
355 omnibox_search_hint_.reset(new OmniboxSearchHint(this));
[email protected]332af7732009-03-11 13:21:35356}
initial.commit09911bf2008-07-26 23:55:29357
358TabContents::~TabContents() {
[email protected]420ae012009-04-24 05:16:32359 is_being_destroyed_ = true;
360
[email protected]bfe4c1582009-10-02 18:11:09361 // We don't want any notifications while we're running our destructor.
[email protected]420ae012009-04-24 05:16:32362 registrar_.RemoveAll();
363
364 // Unregister the notifications of all observed prefs change.
365 PrefService* prefs = profile()->GetPrefs();
366 if (prefs) {
367 for (int i = 0; i < kPrefsToObserveLength; ++i)
368 prefs->RemovePrefObserver(kPrefsToObserve[i], this);
369 }
370
[email protected]420ae012009-04-24 05:16:32371 NotifyDisconnected();
[email protected]e7b8d832009-11-20 22:41:00372 hung_renderer_dialog::HideForTabContents(this);
[email protected]420ae012009-04-24 05:16:32373
[email protected]420ae012009-04-24 05:16:32374 // First cleanly close all child windows.
375 // TODO(mpcomplete): handle case if MaybeCloseChildWindows() already asked
376 // some of these to close. CloseWindows is async, so it might get called
377 // twice before it runs.
[email protected]8cc8d492010-02-02 10:40:49378 CloseConstrainedWindows();
[email protected]420ae012009-04-24 05:16:32379
[email protected]8cc8d492010-02-02 10:40:49380 // Close all blocked popups.
[email protected]66ba4932009-06-04 19:22:13381 if (blocked_popups_)
382 blocked_popups_->Destroy();
383
[email protected]420ae012009-04-24 05:16:32384 // Notify any observer that have a reference on this tab contents.
385 NotificationService::current()->Notify(
386 NotificationType::TAB_CONTENTS_DESTROYED,
387 Source<TabContents>(this),
388 NotificationService::NoDetails());
389
[email protected]1e5e74f2009-05-27 20:55:12390 // Notify any lasting InfobarDelegates that have not yet been removed that
391 // whatever infobar they were handling in this TabContents has closed,
392 // because the TabContents is going away entirely.
393 // This must happen after the TAB_CONTENTS_DESTROYED notification as the
394 // notification may trigger infobars calls that access their delegate. (and
395 // some implementations of InfoBarDelegate do delete themselves on
396 // InfoBarClosed()).
397 for (int i = 0; i < infobar_delegate_count(); ++i) {
398 InfoBarDelegate* delegate = GetInfoBarDelegateAt(i);
399 delegate->InfoBarClosed();
400 }
401 infobar_delegates_.clear();
402
[email protected]420ae012009-04-24 05:16:32403 // TODO(brettw) this should be moved to the view.
404#if defined(OS_WIN)
405 // If we still have a window handle, destroy it. GetNativeView can return
406 // NULL if this contents was part of a window that closed.
407 if (GetNativeView())
408 ::DestroyWindow(GetNativeView());
409#endif
[email protected]7ab1e7d62009-10-14 23:32:01410
411 // OnCloseStarted isn't called in unit tests.
412 if (!tab_close_start_time_.is_null()) {
413 UMA_HISTOGRAM_TIMES("Tab.Close",
414 base::TimeTicks::Now() - tab_close_start_time_);
415 }
initial.commit09911bf2008-07-26 23:55:29416}
417
[email protected]d5f942ba2008-09-26 19:30:34418// static
419void TabContents::RegisterUserPrefs(PrefService* prefs) {
[email protected]420ae012009-04-24 05:16:32420 prefs->RegisterBooleanPref(prefs::kAlternateErrorPagesEnabled, true);
421
422 WebPreferences pref_defaults;
423 prefs->RegisterBooleanPref(prefs::kWebKitJavascriptEnabled,
424 pref_defaults.javascript_enabled);
425 prefs->RegisterBooleanPref(prefs::kWebKitWebSecurityEnabled,
426 pref_defaults.web_security_enabled);
427 prefs->RegisterBooleanPref(
428 prefs::kWebKitJavascriptCanOpenWindowsAutomatically, true);
429 prefs->RegisterBooleanPref(prefs::kWebKitLoadsImagesAutomatically,
430 pref_defaults.loads_images_automatically);
431 prefs->RegisterBooleanPref(prefs::kWebKitPluginsEnabled,
432 pref_defaults.plugins_enabled);
433 prefs->RegisterBooleanPref(prefs::kWebKitDomPasteEnabled,
434 pref_defaults.dom_paste_enabled);
435 prefs->RegisterBooleanPref(prefs::kWebKitShrinksStandaloneImagesToFit,
436 pref_defaults.shrinks_standalone_images_to_fit);
[email protected]c4e98902010-06-01 10:20:14437 prefs->RegisterDictionaryPref(prefs::kWebKitInspectorSettings);
[email protected]420ae012009-04-24 05:16:32438 prefs->RegisterBooleanPref(prefs::kWebKitTextAreasAreResizable,
439 pref_defaults.text_areas_are_resizable);
440 prefs->RegisterBooleanPref(prefs::kWebKitJavaEnabled,
441 pref_defaults.java_enabled);
[email protected]1661a3c02010-05-20 16:32:36442 prefs->RegisterBooleanPref(prefs::kWebkitTabsToLinks,
443 pref_defaults.tabs_to_links);
[email protected]420ae012009-04-24 05:16:32444
445 prefs->RegisterLocalizedStringPref(prefs::kAcceptLanguages,
446 IDS_ACCEPT_LANGUAGES);
447 prefs->RegisterLocalizedStringPref(prefs::kDefaultCharset,
448 IDS_DEFAULT_ENCODING);
449 prefs->RegisterLocalizedBooleanPref(prefs::kWebKitStandardFontIsSerif,
450 IDS_STANDARD_FONT_IS_SERIF);
451 prefs->RegisterLocalizedStringPref(prefs::kWebKitFixedFontFamily,
452 IDS_FIXED_FONT_FAMILY);
453 prefs->RegisterLocalizedStringPref(prefs::kWebKitSerifFontFamily,
454 IDS_SERIF_FONT_FAMILY);
455 prefs->RegisterLocalizedStringPref(prefs::kWebKitSansSerifFontFamily,
456 IDS_SANS_SERIF_FONT_FAMILY);
457 prefs->RegisterLocalizedStringPref(prefs::kWebKitCursiveFontFamily,
458 IDS_CURSIVE_FONT_FAMILY);
459 prefs->RegisterLocalizedStringPref(prefs::kWebKitFantasyFontFamily,
460 IDS_FANTASY_FONT_FAMILY);
461 prefs->RegisterLocalizedIntegerPref(prefs::kWebKitDefaultFontSize,
462 IDS_DEFAULT_FONT_SIZE);
463 prefs->RegisterLocalizedIntegerPref(prefs::kWebKitDefaultFixedFontSize,
464 IDS_DEFAULT_FIXED_FONT_SIZE);
465 prefs->RegisterLocalizedIntegerPref(prefs::kWebKitMinimumFontSize,
466 IDS_MINIMUM_FONT_SIZE);
467 prefs->RegisterLocalizedIntegerPref(prefs::kWebKitMinimumLogicalFontSize,
468 IDS_MINIMUM_LOGICAL_FONT_SIZE);
469 prefs->RegisterLocalizedBooleanPref(prefs::kWebKitUsesUniversalDetector,
470 IDS_USES_UNIVERSAL_DETECTOR);
471 prefs->RegisterLocalizedStringPref(prefs::kStaticEncodings,
472 IDS_STATIC_ENCODING_LIST);
initial.commit09911bf2008-07-26 23:55:29473}
474
[email protected]13367f72009-07-08 16:39:02475// Returns true if contains content rendered by an extension.
476bool TabContents::HostsExtension() const {
477 return GetURL().SchemeIs(chrome::kExtensionScheme);
[email protected]d5f942ba2008-09-26 19:30:34478}
479
[email protected]cf28ec52010-04-09 22:56:34480AutoFillManager* TabContents::GetAutoFillManager() {
481 if (autofill_manager_.get() == NULL)
482 autofill_manager_.reset(new AutoFillManager(this));
483 return autofill_manager_.get();
484}
485
[email protected]6e95706d2009-04-23 22:55:01486PasswordManager* TabContents::GetPasswordManager() {
487 if (password_manager_.get() == NULL)
488 password_manager_.reset(new PasswordManager(this));
489 return password_manager_.get();
490}
491
492PluginInstaller* TabContents::GetPluginInstaller() {
493 if (plugin_installer_.get() == NULL)
494 plugin_installer_.reset(new PluginInstaller(this));
495 return plugin_installer_.get();
496}
497
[email protected]8cb5d5b2010-02-09 11:36:16498RenderProcessHost* TabContents::GetRenderProcessHost() const {
499 return render_manager_.current_host()->process();
500}
501
[email protected]98aa0b52010-05-06 17:03:08502void TabContents::SetExtensionApp(Extension* extension) {
[email protected]867a73e12010-03-19 20:45:46503 DCHECK(!extension || extension->GetFullLaunchURL().is_valid());
[email protected]98aa0b52010-05-06 17:03:08504 extension_app_ = extension;
[email protected]fca656c2010-02-10 20:30:10505
[email protected]98aa0b52010-05-06 17:03:08506 UpdateExtensionAppIcon(extension_app_);
[email protected]a888b29e62010-04-01 13:38:57507
[email protected]fca656c2010-02-10 20:30:10508 NotificationService::current()->Notify(
509 NotificationType::TAB_CONTENTS_APPLICATION_EXTENSION_CHANGED,
510 Source<TabContents>(this),
511 NotificationService::NoDetails());
[email protected]3b355642010-02-05 16:01:49512}
513
[email protected]98aa0b52010-05-06 17:03:08514void TabContents::SetExtensionAppById(const std::string& extension_app_id) {
515 if (extension_app_id.empty())
[email protected]911f8152010-03-18 16:46:40516 return;
517
518 ExtensionsService* extension_service = profile()->GetExtensionsService();
519 if (extension_service && extension_service->is_ready()) {
520 Extension* extension =
[email protected]98aa0b52010-05-06 17:03:08521 extension_service->GetExtensionById(extension_app_id, false);
[email protected]911f8152010-03-18 16:46:40522 if (extension)
[email protected]98aa0b52010-05-06 17:03:08523 SetExtensionApp(extension);
[email protected]911f8152010-03-18 16:46:40524 }
525}
526
[email protected]98aa0b52010-05-06 17:03:08527SkBitmap* TabContents::GetExtensionAppIcon() {
[email protected]8eb043b2010-04-30 07:19:51528 // We don't show the big icons in tabs for TYPE_EXTENSION_APP windows because
529 // for those windows, we already have a big icon in the top-left outside any
530 // tab. Having big tab icons too looks kinda redonk.
[email protected]06821f42d2010-05-03 22:34:35531 Browser* browser = delegate_ ? delegate_->GetBrowser() : NULL;
532 if (browser && browser->type() == Browser::TYPE_EXTENSION_APP)
[email protected]8eb043b2010-04-30 07:19:51533 return NULL;
[email protected]8eb043b2010-04-30 07:19:51534
[email protected]98aa0b52010-05-06 17:03:08535 if (extension_app_icon_.empty())
[email protected]8eb043b2010-04-30 07:19:51536 return NULL;
537
[email protected]98aa0b52010-05-06 17:03:08538 return &extension_app_icon_;
[email protected]8eb043b2010-04-30 07:19:51539}
540
[email protected]d5f942ba2008-09-26 19:30:34541const GURL& TabContents::GetURL() const {
542 // We may not have a navigation entry yet
[email protected]ce3fa3c2009-04-20 19:55:57543 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]ebe89e062009-08-13 23:16:54544 return entry ? entry->virtual_url() : GURL::EmptyGURL();
[email protected]d5f942ba2008-09-26 19:30:34545}
546
[email protected]96d185d2009-04-24 03:28:54547const string16& TabContents::GetTitle() const {
[email protected]4c6092c5b2009-06-06 00:23:55548 // Transient entries take precedence. They are used for interstitial pages
549 // that are shown on top of existing pages.
550 NavigationEntry* entry = controller_.GetTransientEntry();
551 if (entry)
552 return entry->GetTitleForDisplay(&controller_);
553
[email protected]96d185d2009-04-24 03:28:54554 DOMUI* our_dom_ui = render_manager_.pending_dom_ui() ?
555 render_manager_.pending_dom_ui() : render_manager_.dom_ui();
556 if (our_dom_ui) {
557 // Don't override the title in view source mode.
[email protected]4c6092c5b2009-06-06 00:23:55558 entry = controller_.GetActiveEntry();
[email protected]96d185d2009-04-24 03:28:54559 if (!(entry && entry->IsViewSourceMode())) {
560 // Give the DOM UI the chance to override our title.
561 const string16& title = our_dom_ui->overridden_title();
562 if (!title.empty())
563 return title;
564 }
565 }
566
567 // We use the title for the last committed entry rather than a pending
568 // navigation entry. For example, when the user types in a URL, we want to
569 // keep the old page's title until the new load has committed and we get a new
570 // title.
[email protected]96d185d2009-04-24 03:28:54571 entry = controller_.GetLastCommittedEntry();
572 if (entry)
573 return entry->GetTitleForDisplay(&controller_);
[email protected]96d185d2009-04-24 03:28:54574 return EmptyString16();
575}
576
[email protected]bbbc1ef2010-02-12 18:03:41577// static
578string16 TabContents::GetDefaultTitle() {
579 return l10n_util::GetStringUTF16(IDS_DEFAULT_TAB_TITLE);
580}
581
[email protected]d5f942ba2008-09-26 19:30:34582int32 TabContents::GetMaxPageID() {
583 if (GetSiteInstance())
584 return GetSiteInstance()->max_page_id();
585 else
586 return max_page_id_;
587}
588
589void TabContents::UpdateMaxPageID(int32 page_id) {
590 // Ensure both the SiteInstance and RenderProcessHost update their max page
[email protected]57c6a652009-05-04 07:58:34591 // IDs in sync. Only TabContents will also have site instances, except during
[email protected]d5f942ba2008-09-26 19:30:34592 // testing.
593 if (GetSiteInstance())
594 GetSiteInstance()->UpdateMaxPageID(page_id);
[email protected]8cb5d5b2010-02-09 11:36:16595 GetRenderProcessHost()->UpdateMaxPageID(page_id);
[email protected]d5f942ba2008-09-26 19:30:34596}
597
[email protected]96d185d2009-04-24 03:28:54598SiteInstance* TabContents::GetSiteInstance() const {
599 return render_manager_.current_host()->site_instance();
600}
601
[email protected]96d185d2009-04-24 03:28:54602bool TabContents::ShouldDisplayURL() {
[email protected]8ab24cc2009-11-13 20:56:15603 // Don't hide the url in view source mode and with interstitials.
[email protected]96d185d2009-04-24 03:28:54604 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]8ab24cc2009-11-13 20:56:15605 if (entry && (entry->IsViewSourceMode() ||
606 entry->page_type() == NavigationEntry::INTERSTITIAL_PAGE)) {
[email protected]96d185d2009-04-24 03:28:54607 return true;
[email protected]8ab24cc2009-11-13 20:56:15608 }
[email protected]83e3895b2009-06-11 00:07:16609
[email protected]96d185d2009-04-24 03:28:54610 DOMUI* dom_ui = GetDOMUIForCurrentState();
611 if (dom_ui)
612 return !dom_ui->should_hide_url();
613 return true;
614}
615
[email protected]d5f942ba2008-09-26 19:30:34616SkBitmap TabContents::GetFavIcon() const {
617 // Like GetTitle(), we also want to use the favicon for the last committed
618 // entry rather than a pending navigation entry.
[email protected]ce3fa3c2009-04-20 19:55:57619 NavigationEntry* entry = controller_.GetTransientEntry();
[email protected]cbab76d2008-10-13 22:42:47620 if (entry)
621 return entry->favicon().bitmap();
622
[email protected]ce3fa3c2009-04-20 19:55:57623 entry = controller_.GetLastCommittedEntry();
[email protected]d5f942ba2008-09-26 19:30:34624 if (entry)
625 return entry->favicon().bitmap();
[email protected]d5f942ba2008-09-26 19:30:34626 return SkBitmap();
627}
628
[email protected]0b303cc2009-09-28 22:35:15629bool TabContents::FavIconIsValid() const {
630 NavigationEntry* entry = controller_.GetTransientEntry();
631 if (entry)
632 return entry->favicon().is_valid();
633
634 entry = controller_.GetLastCommittedEntry();
635 if (entry)
636 return entry->favicon().is_valid();
637
638 return false;
639}
640
[email protected]96d185d2009-04-24 03:28:54641bool TabContents::ShouldDisplayFavIcon() {
642 // Always display a throbber during pending loads.
643 if (controller_.GetLastCommittedEntry() && controller_.pending_entry())
644 return true;
645
646 DOMUI* dom_ui = GetDOMUIForCurrentState();
647 if (dom_ui)
648 return !dom_ui->hide_favicon();
649 return true;
650}
651
[email protected]2d5a3592010-01-30 04:29:52652bool TabContents::IsContentBlocked(ContentSettingsType content_type) const {
[email protected]8cc8d492010-02-02 10:40:49653 if (content_type == CONTENT_SETTINGS_TYPE_POPUPS)
654 return blocked_popups_ != NULL;
655
[email protected]0de80162010-02-03 04:52:35656 if (content_type == CONTENT_SETTINGS_TYPE_IMAGES ||
657 content_type == CONTENT_SETTINGS_TYPE_JAVASCRIPT ||
[email protected]3dbb80b2010-02-09 22:41:20658 content_type == CONTENT_SETTINGS_TYPE_PLUGINS ||
659 content_type == CONTENT_SETTINGS_TYPE_COOKIES)
[email protected]0de80162010-02-03 04:52:35660 return content_blocked_[content_type];
661
[email protected]b1235062010-03-25 17:31:27662 // TODO(joth): remove once fully implemented.
663 if (content_type == CONTENT_SETTINGS_TYPE_GEOLOCATION)
664 return false;
665
[email protected]3dbb80b2010-02-09 22:41:20666 NOTREACHED();
[email protected]2d5a3592010-01-30 04:29:52667 return false;
668}
669
[email protected]96d185d2009-04-24 03:28:54670std::wstring TabContents::GetStatusText() const {
671 if (!is_loading() || load_state_ == net::LOAD_STATE_IDLE)
672 return std::wstring();
673
674 switch (load_state_) {
675 case net::LOAD_STATE_WAITING_FOR_CACHE:
676 return l10n_util::GetString(IDS_LOAD_STATE_WAITING_FOR_CACHE);
[email protected]8a1f3312010-05-25 19:25:04677 case net::LOAD_STATE_ESTABLISHING_PROXY_TUNNEL:
678 return l10n_util::GetString(IDS_LOAD_STATE_ESTABLISHING_PROXY_TUNNEL);
[email protected]96d185d2009-04-24 03:28:54679 case net::LOAD_STATE_RESOLVING_PROXY_FOR_URL:
680 return l10n_util::GetString(IDS_LOAD_STATE_RESOLVING_PROXY_FOR_URL);
681 case net::LOAD_STATE_RESOLVING_HOST:
682 return l10n_util::GetString(IDS_LOAD_STATE_RESOLVING_HOST);
683 case net::LOAD_STATE_CONNECTING:
684 return l10n_util::GetString(IDS_LOAD_STATE_CONNECTING);
[email protected]375e3112010-05-27 20:32:06685 case net::LOAD_STATE_SSL_HANDSHAKE:
686 return l10n_util::GetString(IDS_LOAD_STATE_SSL_HANDSHAKE);
[email protected]96d185d2009-04-24 03:28:54687 case net::LOAD_STATE_SENDING_REQUEST:
[email protected]094e5b22009-09-25 04:23:56688 if (upload_size_)
689 return l10n_util::GetStringF(
690 IDS_LOAD_STATE_SENDING_REQUEST_WITH_PROGRESS,
691 static_cast<int>((100 * upload_position_) / upload_size_));
692 else
693 return l10n_util::GetString(IDS_LOAD_STATE_SENDING_REQUEST);
[email protected]96d185d2009-04-24 03:28:54694 case net::LOAD_STATE_WAITING_FOR_RESPONSE:
695 return l10n_util::GetStringF(IDS_LOAD_STATE_WAITING_FOR_RESPONSE,
696 load_state_host_);
697 // Ignore net::LOAD_STATE_READING_RESPONSE and net::LOAD_STATE_IDLE
698 case net::LOAD_STATE_IDLE:
699 case net::LOAD_STATE_READING_RESPONSE:
700 break;
701 }
702
703 return std::wstring();
704}
705
[email protected]d5f942ba2008-09-26 19:30:34706void TabContents::SetIsCrashed(bool state) {
707 if (state == is_crashed_)
708 return;
709
710 is_crashed_ = state;
[email protected]c9cd2222009-05-06 05:16:50711 NotifyNavigationStateChanged(INVALIDATE_TAB);
[email protected]d5f942ba2008-09-26 19:30:34712}
713
[email protected]744ef172009-10-16 21:53:46714void TabContents::PageActionStateChanged() {
715 NotifyNavigationStateChanged(TabContents::INVALIDATE_PAGE_ACTIONS);
[email protected]d7eaf572009-07-01 21:57:00716}
[email protected]ce5c4502009-05-06 16:46:11717
[email protected]d5f942ba2008-09-26 19:30:34718void TabContents::NotifyNavigationStateChanged(unsigned changed_flags) {
719 if (delegate_)
720 delegate_->NavigationStateChanged(this, changed_flags);
721}
722
[email protected]96d185d2009-04-24 03:28:54723void TabContents::DidBecomeSelected() {
724 controller_.SetActive(true);
[email protected]8cb5d5b2010-02-09 11:36:16725 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
726 if (rwhv) {
727 rwhv->DidBecomeSelected();
[email protected]789e9152009-08-04 21:59:43728#if defined(OS_MACOSX)
[email protected]8cb5d5b2010-02-09 11:36:16729 rwhv->SetActive(true);
[email protected]789e9152009-08-04 21:59:43730#endif
731 }
[email protected]96d185d2009-04-24 03:28:54732
[email protected]8cb5d5b2010-02-09 11:36:16733 WebCacheManager::GetInstance()->ObserveActivity(GetRenderProcessHost()->id());
[email protected]96d185d2009-04-24 03:28:54734}
735
736void TabContents::WasHidden() {
737 if (!capturing_contents()) {
738 // |render_view_host()| can be NULL if the user middle clicks a link to open
739 // a tab in then background, then closes the tab before selecting it. This
740 // is because closing the tab calls TabContents::Destroy(), which removes
741 // the |render_view_host()|; then when we actually destroy the window,
742 // OnWindowPosChanged() notices and calls HideContents() (which calls us).
[email protected]8cb5d5b2010-02-09 11:36:16743 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
744 if (rwhv)
745 rwhv->WasHidden();
[email protected]96d185d2009-04-24 03:28:54746 }
747
748 NotificationService::current()->Notify(
749 NotificationType::TAB_CONTENTS_HIDDEN,
750 Source<TabContents>(this),
751 NotificationService::NoDetails());
752}
753
[email protected]d5f942ba2008-09-26 19:30:34754void TabContents::Activate() {
755 if (delegate_)
756 delegate_->ActivateContents(this);
757}
758
[email protected]96d185d2009-04-24 03:28:54759void TabContents::ShowContents() {
[email protected]8cb5d5b2010-02-09 11:36:16760 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
761 if (rwhv)
762 rwhv->DidBecomeSelected();
[email protected]96d185d2009-04-24 03:28:54763}
764
765void TabContents::HideContents() {
766 // TODO(pkasting): http://b/1239839 Right now we purposefully don't call
767 // our superclass HideContents(), because some callers want to be very picky
768 // about the order in which these get called. In addition to making the code
769 // here practically impossible to understand, this also means we end up
770 // calling TabContents::WasHidden() twice if callers call both versions of
[email protected]57c6a652009-05-04 07:58:34771 // HideContents() on a TabContents.
[email protected]96d185d2009-04-24 03:28:54772 WasHidden();
773}
774
[email protected]c0588052008-10-27 23:01:50775void TabContents::OpenURL(const GURL& url, const GURL& referrer,
[email protected]d5f942ba2008-09-26 19:30:34776 WindowOpenDisposition disposition,
777 PageTransition::Type transition) {
778 if (delegate_)
[email protected]c0588052008-10-27 23:01:50779 delegate_->OpenURLFromTab(this, url, referrer, disposition, transition);
[email protected]d5f942ba2008-09-26 19:30:34780}
781
[email protected]1ccb3568d2010-02-19 10:51:16782bool TabContents::NavigateToPendingEntry(
783 NavigationController::ReloadType reload_type) {
[email protected]96d185d2009-04-24 03:28:54784 const NavigationEntry& entry = *controller_.pending_entry();
785
786 RenderViewHost* dest_render_view_host = render_manager_.Navigate(entry);
787 if (!dest_render_view_host)
788 return false; // Unable to create the desired render view host.
789
[email protected]770dd8b2010-05-24 18:11:39790 if (delegate_ && delegate_->ShouldEnablePreferredSizeNotifications()) {
791 dest_render_view_host->EnablePreferredSizeChangedMode(
792 kPreferredSizeWidth | kPreferredSizeHeightThisIsSlow);
793 }
[email protected]9fb325e2010-05-06 18:23:24794
[email protected]0352e5472010-04-29 00:11:40795 // For security, we should never send non-DOM-UI URLs (other than about:blank)
796 // to a DOM UI renderer. Double check that here.
[email protected]54ec6472010-04-09 19:39:58797 int enabled_bindings = dest_render_view_host->enabled_bindings();
[email protected]0352e5472010-04-29 00:11:40798 bool is_allowed_in_dom_ui_renderer =
799 DOMUIFactory::UseDOMUIForURL(entry.url()) ||
800 entry.url() == GURL(chrome::kAboutBlankURL);
[email protected]54ec6472010-04-09 19:39:58801 CHECK(!BindingsPolicy::is_dom_ui_enabled(enabled_bindings) ||
[email protected]0352e5472010-04-29 00:11:40802 is_allowed_in_dom_ui_renderer);
[email protected]54ec6472010-04-09 19:39:58803
[email protected]96d185d2009-04-24 03:28:54804 // Tell DevTools agent that it is attached prior to the navigation.
[email protected]d9f9b792009-06-24 13:17:12805 DevToolsManager* devtools_manager = DevToolsManager::GetInstance();
[email protected]7e8e6b62009-05-08 11:28:32806 if (devtools_manager) { // NULL in unit tests.
[email protected]3cc72b12010-03-18 23:03:00807 devtools_manager->OnNavigatingToPendingEntry(render_view_host(),
808 dest_render_view_host,
809 entry.url());
[email protected]7e8e6b62009-05-08 11:28:32810 }
[email protected]96d185d2009-04-24 03:28:54811
812 // Used for page load time metrics.
813 current_load_start_ = base::TimeTicks::Now();
814
815 // Navigate in the desired RenderViewHost.
[email protected]056de2d2009-06-26 16:41:34816 ViewMsg_Navigate_Params navigate_params;
[email protected]3cc72b12010-03-18 23:03:00817 MakeNavigateParams(controller_, reload_type, &navigate_params);
[email protected]056de2d2009-06-26 16:41:34818 dest_render_view_host->Navigate(navigate_params);
[email protected]96d185d2009-04-24 03:28:54819
820 if (entry.page_id() == -1) {
821 // HACK!! This code suppresses javascript: URLs from being added to
822 // session history, which is what we want to do for javascript: URLs that
823 // do not generate content. What we really need is a message from the
824 // renderer telling us that a new page was not created. The same message
825 // could be used for mailto: URLs and the like.
826 if (entry.url().SchemeIs(chrome::kJavaScriptScheme))
827 return false;
828 }
829
830 // Clear any provisional password saves - this stops password infobars
831 // showing up on pages the user navigates to while the right page is
832 // loading.
833 GetPasswordManager()->ClearProvisionalSave();
834
[email protected]1ccb3568d2010-02-19 10:51:16835 if (reload_type != NavigationController::NO_RELOAD &&
836 !profile()->IsOffTheRecord()) {
[email protected]0189bc722009-08-28 21:56:48837 FaviconService* favicon_service =
838 profile()->GetFaviconService(Profile::IMPLICIT_ACCESS);
839 if (favicon_service)
840 favicon_service->SetFaviconOutOfDateForPage(entry.url());
[email protected]96d185d2009-04-24 03:28:54841 }
842
[email protected]d5f942ba2008-09-26 19:30:34843 return true;
844}
845
[email protected]96d185d2009-04-24 03:28:54846void TabContents::Stop() {
847 render_manager_.Stop();
848 printing_.Stop();
849}
850
[email protected]96d185d2009-04-24 03:28:54851void TabContents::DisassociateFromPopupCount() {
852 render_view_host()->DisassociateFromPopupCount();
853}
854
855TabContents* TabContents::Clone() {
856 // We create a new SiteInstance so that the new tab won't share processes
857 // with the old one. This can be changed in the future if we need it to share
858 // processes for some reason.
[email protected]420ae012009-04-24 05:16:32859 TabContents* tc = new TabContents(profile(),
[email protected]96d185d2009-04-24 03:28:54860 SiteInstance::CreateSiteInstance(profile()),
[email protected]12636df2009-09-28 22:32:21861 MSG_ROUTING_NONE, this);
[email protected]96d185d2009-04-24 03:28:54862 tc->controller().CopyStateFrom(controller_);
[email protected]98aa0b52010-05-06 17:03:08863 tc->extension_app_ = extension_app_;
[email protected]98aa0b52010-05-06 17:03:08864 tc->extension_app_icon_ = extension_app_icon_;
[email protected]96d185d2009-04-24 03:28:54865 return tc;
866}
867
[email protected]4d677202009-07-19 07:37:12868void TabContents::ShowPageInfo(const GURL& url,
869 const NavigationEntry::SSLStatus& ssl,
870 bool show_history) {
871 if (!delegate_)
872 return;
873
[email protected]bb678332009-07-21 00:15:50874 delegate_->ShowPageInfo(profile(), url, ssl, show_history);
[email protected]4d677202009-07-19 07:37:12875}
876
initial.commit09911bf2008-07-26 23:55:29877ConstrainedWindow* TabContents::CreateConstrainedDialog(
[email protected]e8382172009-06-19 22:16:28878 ConstrainedWindowDelegate* delegate) {
initial.commit09911bf2008-07-26 23:55:29879 ConstrainedWindow* window =
[email protected]e8382172009-06-19 22:16:28880 ConstrainedWindow::CreateConstrainedDialog(this, delegate);
initial.commit09911bf2008-07-26 23:55:29881 child_windows_.push_back(window);
[email protected]fa1cf0b82010-01-15 21:49:44882
883 if (child_windows_.size() == 1) {
884 window->ShowConstrainedWindow();
885 BlockTabContent(true);
886 }
887
initial.commit09911bf2008-07-26 23:55:29888 return window;
889}
890
[email protected]fa1cf0b82010-01-15 21:49:44891void TabContents::BlockTabContent(bool blocked) {
[email protected]8dccd7cb2010-02-25 05:19:55892 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
893 if (rwhv)
894 rwhv->SetVisuallyDeemphasized(blocked);
[email protected]fa1cf0b82010-01-15 21:49:44895 render_view_host()->set_ignore_input_events(blocked);
896 if (delegate_)
897 delegate_->SetTabContentBlocked(this, blocked);
898}
899
initial.commit09911bf2008-07-26 23:55:29900void TabContents::AddNewContents(TabContents* new_contents,
901 WindowOpenDisposition disposition,
902 const gfx::Rect& initial_pos,
[email protected]7e7414ae2010-01-26 20:19:29903 bool user_gesture) {
initial.commit09911bf2008-07-26 23:55:29904 if (!delegate_)
905 return;
906
[email protected]35f7d212009-04-29 21:19:27907 if ((disposition == NEW_POPUP) && !user_gesture &&
908 !CommandLine::ForCurrentProcess()->HasSwitch(
909 switches::kDisablePopupBlocking)) {
[email protected]2c4410d2009-05-06 23:46:22910 // Unrequested popups from normal pages are constrained unless they're in
[email protected]b6c874582009-05-08 19:38:31911 // the whitelist. The popup owner will handle checking this.
[email protected]7e7414ae2010-01-26 20:19:29912 delegate_->GetConstrainingContents(this)->AddPopup(
913 new_contents, initial_pos);
[email protected]b6c874582009-05-08 19:38:31914 } else {
[email protected]304f8792009-08-19 21:28:09915#if defined(OS_CHROMEOS)
916 if (disposition == NEW_POPUP) {
917 // If the popup is bigger than a given factor of the screen, then
918 // turn it into a foreground tab (on chrome os only)
919 GdkScreen* screen = gdk_screen_get_default();
920 int max_width = gdk_screen_get_width(screen) * kMaxWidthFactor;
921 int max_height = gdk_screen_get_height(screen) * kMaxHeightFactor;
922 if (initial_pos.width() > max_width ||
923 initial_pos.height() > max_height) {
924 disposition = NEW_FOREGROUND_TAB;
925 }
926 }
927#endif
928
[email protected]0aa55312008-10-17 21:53:08929 new_contents->DisassociateFromPopupCount();
initial.commit09911bf2008-07-26 23:55:29930 delegate_->AddNewContents(this, new_contents, disposition, initial_pos,
931 user_gesture);
[email protected]7f37dd92009-09-15 16:35:49932 NotificationService::current()->Notify(
933 NotificationType::TAB_ADDED,
934 Source<TabContentsDelegate>(delegate_),
935 Details<TabContents>(this));
initial.commit09911bf2008-07-26 23:55:29936 }
initial.commit09911bf2008-07-26 23:55:29937
[email protected]8cc8d492010-02-02 10:40:49938 // TODO(pkasting): Why is this necessary?
939 PopupNotificationVisibilityChanged(blocked_popups_ != NULL);
initial.commit09911bf2008-07-26 23:55:29940}
941
[email protected]bcd9dc42010-01-23 16:38:18942bool TabContents::ExecuteCode(int request_id, const std::string& extension_id,
[email protected]61f5a7b2009-12-22 22:21:20943 const std::vector<URLPattern>& host_permissions,
[email protected]20ad2692009-11-20 18:27:20944 bool is_js_code, const std::string& code_string,
945 bool all_frames) {
[email protected]ebd0eae72010-02-16 23:50:51946 RenderViewHost* host = render_view_host();
947 if (!host)
948 return false;
949
950 return host->Send(new ViewMsg_ExecuteCode(host->routing_id(),
951 ViewMsg_ExecuteCode_Params(request_id, extension_id, host_permissions,
952 is_js_code, code_string, all_frames)));
[email protected]912256b32009-09-18 09:47:35953}
954
[email protected]96d185d2009-04-24 03:28:54955void TabContents::PopupNotificationVisibilityChanged(bool visible) {
[email protected]8cc8d492010-02-02 10:40:49956 if (is_being_destroyed_)
957 return;
958 if (!dont_notify_render_view_)
[email protected]e8345242010-05-06 03:00:40959 render_view_host()->AllowScriptToClose(!visible);
[email protected]0de80162010-02-03 04:52:35960 if (delegate_)
[email protected]20ad6a92010-04-21 14:07:24961 delegate_->OnContentSettingsChange(this);
[email protected]96d185d2009-04-24 03:28:54962}
963
[email protected]5c9e97a2009-09-09 23:48:30964gfx::NativeView TabContents::GetContentNativeView() const {
[email protected]96d185d2009-04-24 03:28:54965 return view_->GetContentNativeView();
966}
967
968gfx::NativeView TabContents::GetNativeView() const {
969 return view_->GetNativeView();
970}
971
972void TabContents::GetContainerBounds(gfx::Rect *out) const {
973 view_->GetContainerBounds(out);
974}
975
976void TabContents::Focus() {
977 view_->Focus();
978}
979
[email protected]90daadb42009-06-08 21:27:28980void TabContents::FocusThroughTabTraversal(bool reverse) {
[email protected]7e383692009-06-12 19:14:54981 if (showing_interstitial_page()) {
982 render_manager_.interstitial_page()->FocusThroughTabTraversal(reverse);
983 return;
984 }
[email protected]96d185d2009-04-24 03:28:54985 render_view_host()->SetInitialFocus(reverse);
986}
987
988bool TabContents::FocusLocationBarByDefault() {
989 DOMUI* dom_ui = GetDOMUIForCurrentState();
990 if (dom_ui)
991 return dom_ui->focus_location_bar_by_default();
[email protected]3e3f0eb2009-06-22 18:33:43992 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]e0d481582009-09-15 21:06:25993 if (entry && entry->url() == GURL(chrome::kAboutBlankURL))
[email protected]3e3f0eb2009-06-22 18:33:43994 return true;
[email protected]96d185d2009-04-24 03:28:54995 return false;
996}
997
[email protected]a26dc362010-04-23 01:48:58998void TabContents::SetFocusToLocationBar(bool select_all) {
[email protected]a11aaf22010-03-30 00:03:38999 if (delegate())
[email protected]a26dc362010-04-23 01:48:581000 delegate()->SetFocusToLocationBar(select_all);
[email protected]a11aaf22010-03-30 00:03:381001}
1002
[email protected]616ed5a2008-11-21 22:27:241003void TabContents::AddInfoBar(InfoBarDelegate* delegate) {
[email protected]f7019302010-03-26 19:58:321004 if (delegate_ && !delegate_->infobars_enabled()) {
1005 delegate->InfoBarClosed();
1006 return;
1007 }
1008
[email protected]616ed5a2008-11-21 22:27:241009 // Look through the existing InfoBarDelegates we have for a match. If we've
1010 // already got one that matches, then we don't add the new one.
[email protected]f86a07022008-11-25 01:06:051011 for (int i = 0; i < infobar_delegate_count(); ++i) {
[email protected]6d27a7b2009-12-18 23:25:451012 if (GetInfoBarDelegateAt(i)->EqualsDelegate(delegate)) {
1013 // Tell the new infobar to close so that it can clean itself up.
1014 delegate->InfoBarClosed();
[email protected]616ed5a2008-11-21 22:27:241015 return;
[email protected]6d27a7b2009-12-18 23:25:451016 }
[email protected]616ed5a2008-11-21 22:27:241017 }
1018
1019 infobar_delegates_.push_back(delegate);
[email protected]bfd04a62009-02-01 18:16:561020 NotificationService::current()->Notify(
1021 NotificationType::TAB_CONTENTS_INFOBAR_ADDED,
1022 Source<TabContents>(this),
1023 Details<InfoBarDelegate>(delegate));
[email protected]616ed5a2008-11-21 22:27:241024
1025 // Add ourselves as an observer for navigations the first time a delegate is
1026 // added. We use this notification to expire InfoBars that need to expire on
1027 // page transitions.
1028 if (infobar_delegates_.size() == 1) {
[email protected]bfd04a62009-02-01 18:16:561029 registrar_.Add(this, NotificationType::NAV_ENTRY_COMMITTED,
[email protected]ce3fa3c2009-04-20 19:55:571030 Source<NavigationController>(&controller_));
[email protected]616ed5a2008-11-21 22:27:241031 }
1032}
1033
1034void TabContents::RemoveInfoBar(InfoBarDelegate* delegate) {
[email protected]f7019302010-03-26 19:58:321035 if (delegate_ && !delegate_->infobars_enabled()) {
1036 return;
1037 }
1038
[email protected]616ed5a2008-11-21 22:27:241039 std::vector<InfoBarDelegate*>::iterator it =
1040 find(infobar_delegates_.begin(), infobar_delegates_.end(), delegate);
1041 if (it != infobar_delegates_.end()) {
1042 InfoBarDelegate* delegate = *it;
[email protected]bfd04a62009-02-01 18:16:561043 NotificationService::current()->Notify(
1044 NotificationType::TAB_CONTENTS_INFOBAR_REMOVED,
1045 Source<TabContents>(this),
1046 Details<InfoBarDelegate>(delegate));
[email protected]f86a07022008-11-25 01:06:051047 infobar_delegates_.erase(it);
[email protected]616ed5a2008-11-21 22:27:241048
[email protected]6a02963e2009-01-06 16:58:031049 // Remove ourselves as an observer if we are tracking no more InfoBars.
1050 if (infobar_delegates_.empty()) {
[email protected]bfd04a62009-02-01 18:16:561051 registrar_.Remove(this, NotificationType::NAV_ENTRY_COMMITTED,
[email protected]ce3fa3c2009-04-20 19:55:571052 Source<NavigationController>(&controller_));
[email protected]6a02963e2009-01-06 16:58:031053 }
[email protected]616ed5a2008-11-21 22:27:241054 }
1055}
[email protected]3edd9522009-03-04 22:19:411056
[email protected]f81821c2009-08-07 01:29:471057void TabContents::ReplaceInfoBar(InfoBarDelegate* old_delegate,
1058 InfoBarDelegate* new_delegate) {
[email protected]f7019302010-03-26 19:58:321059 if (delegate_ && !delegate_->infobars_enabled()) {
1060 new_delegate->InfoBarClosed();
1061 return;
1062 }
1063
[email protected]f81821c2009-08-07 01:29:471064 std::vector<InfoBarDelegate*>::iterator it =
1065 find(infobar_delegates_.begin(), infobar_delegates_.end(), old_delegate);
1066 DCHECK(it != infobar_delegates_.end());
1067
1068 // Notify the container about the change of plans.
1069 scoped_ptr<std::pair<InfoBarDelegate*, InfoBarDelegate*> > details(
1070 new std::pair<InfoBarDelegate*, InfoBarDelegate*>(
1071 old_delegate, new_delegate));
1072 NotificationService::current()->Notify(
1073 NotificationType::TAB_CONTENTS_INFOBAR_REPLACED,
1074 Source<TabContents>(this),
1075 Details<std::pair<InfoBarDelegate*, InfoBarDelegate*> >(details.get()));
1076
1077 // Remove the old one.
1078 infobar_delegates_.erase(it);
1079
1080 // Add the new one.
1081 infobar_delegates_.push_back(new_delegate);
1082}
1083
[email protected]20ede072009-10-06 00:28:351084bool TabContents::ShouldShowBookmarkBar() {
[email protected]aa9196f2009-10-22 16:20:271085 if (showing_interstitial_page())
1086 return false;
1087
[email protected]96d185d2009-04-24 03:28:541088 // See GetDOMUIForCurrentState() comment for more info. This case is very
1089 // similar, but for non-first loads, we want to use the committed entry. This
1090 // is so the bookmarks bar disappears at the same time the page does.
1091 if (controller_.GetLastCommittedEntry()) {
1092 // Not the first load, always use the committed DOM UI.
1093 if (render_manager_.dom_ui())
1094 return render_manager_.dom_ui()->force_bookmark_bar_visible();
1095 return false; // Default.
1096 }
1097
1098 // When it's the first load, we know either the pending one or the committed
1099 // one will have the DOM UI in it (see GetDOMUIForCurrentState), and only one
1100 // of them will be valid, so we can just check both.
1101 if (render_manager_.pending_dom_ui())
1102 return render_manager_.pending_dom_ui()->force_bookmark_bar_visible();
1103 if (render_manager_.dom_ui())
1104 return render_manager_.dom_ui()->force_bookmark_bar_visible();
1105 return false; // Default.
1106}
1107
[email protected]d8f16ae2009-08-30 03:47:261108bool TabContents::IsExtensionShelfAlwaysVisible() {
1109 // See GetDOMUIForCurrentState() comment for more info. This case is very
1110 // similar, but for non-first loads, we want to use the committed entry. This
1111 // is so the bookmarks bar disappears at the same time the page does.
1112 if (controller_.GetLastCommittedEntry()) {
1113 // Not the first load, always use the committed DOM UI.
1114 if (render_manager_.dom_ui())
1115 return render_manager_.dom_ui()->force_extension_shelf_visible();
1116 return false; // Default.
1117 }
1118
1119 // When it's the first load, we know either the pending one or the committed
1120 // one will have the DOM UI in it (see GetDOMUIForCurrentState), and only one
1121 // of them will be valid, so we can just check both.
1122 if (render_manager_.pending_dom_ui())
1123 return render_manager_.pending_dom_ui()->force_extension_shelf_visible();
1124 if (render_manager_.dom_ui())
1125 return render_manager_.dom_ui()->force_extension_shelf_visible();
1126 return false; // Default.
1127}
1128
[email protected]3edd9522009-03-04 22:19:411129void TabContents::ToolbarSizeChanged(bool is_animating) {
1130 TabContentsDelegate* d = delegate();
1131 if (d)
1132 d->ToolbarSizeChanged(this, is_animating);
1133}
[email protected]616ed5a2008-11-21 22:27:241134
[email protected]fc6fb7fb2009-11-07 02:35:041135bool TabContents::CanDownload(int request_id) {
1136 TabContentsDelegate* d = delegate();
1137 if (d)
1138 return d->CanDownload(request_id);
1139 return true;
1140}
1141
[email protected]d5f942ba2008-09-26 19:30:341142void TabContents::OnStartDownload(DownloadItem* download) {
1143 DCHECK(download);
[email protected]d5f942ba2008-09-26 19:30:341144
1145 // Download in a constrained popup is shown in the tab that opened it.
[email protected]b6c874582009-05-08 19:38:311146 TabContents* tab_contents = delegate()->GetConstrainingContents(this);
[email protected]d5f942ba2008-09-26 19:30:341147
[email protected]c70f9b82010-04-21 07:31:111148 if (tab_contents && tab_contents->delegate()) {
[email protected]59560e0b2009-06-04 03:30:221149 tab_contents->delegate()->OnStartDownload(download);
[email protected]c70f9b82010-04-21 07:31:111150 // If the download occurs in a new tab, close it
1151 if (controller_.IsInitialNavigation() && (tab_contents == this))
1152 delegate()->CloseContents(this);
1153 }
[email protected]3edd9522009-03-04 22:19:411154}
[email protected]3edd9522009-03-04 22:19:411155
[email protected]d5f942ba2008-09-26 19:30:341156void TabContents::WillClose(ConstrainedWindow* window) {
1157 ConstrainedWindowList::iterator it =
1158 find(child_windows_.begin(), child_windows_.end(), window);
[email protected]fa1cf0b82010-01-15 21:49:441159 bool removed_topmost_window = it == child_windows_.begin();
[email protected]d5f942ba2008-09-26 19:30:341160 if (it != child_windows_.end())
1161 child_windows_.erase(it);
[email protected]55098792010-03-24 18:20:371162 if (child_windows_.size() > 0) {
1163 if (removed_topmost_window) {
1164 child_windows_[0]->ShowConstrainedWindow();
1165 }
[email protected]fa1cf0b82010-01-15 21:49:441166 BlockTabContent(true);
1167 } else {
1168 BlockTabContent(false);
1169 }
[email protected]66ba4932009-06-04 19:22:131170}
[email protected]d5f942ba2008-09-26 19:30:341171
[email protected]66ba4932009-06-04 19:22:131172void TabContents::WillCloseBlockedPopupContainer(
1173 BlockedPopupContainer* container) {
1174 DCHECK(blocked_popups_ == container);
1175 blocked_popups_ = NULL;
[email protected]8cc8d492010-02-02 10:40:491176 PopupNotificationVisibilityChanged(false);
[email protected]d5f942ba2008-09-26 19:30:341177}
1178
[email protected]d5f942ba2008-09-26 19:30:341179void TabContents::DidMoveOrResize(ConstrainedWindow* window) {
[email protected]332af7732009-03-11 13:21:351180#if defined(OS_WIN)
[email protected]92edc472009-02-10 20:32:061181 UpdateWindow(GetNativeView());
[email protected]332af7732009-03-11 13:21:351182#endif
[email protected]d5f942ba2008-09-26 19:30:341183}
1184
[email protected]c1dc9f132009-11-20 23:13:211185void TabContents::StartFinding(string16 search_string,
[email protected]e491f1c2009-05-22 20:28:121186 bool forward_direction,
1187 bool case_sensitive) {
[email protected]c1dc9f132009-11-20 23:13:211188 // If search_string is empty, it means FindNext was pressed with a keyboard
[email protected]6e95706d2009-04-23 22:55:011189 // shortcut so unless we have something to search for we return early.
[email protected]c1dc9f132009-11-20 23:13:211190 if (search_string.empty() && find_text_.empty()) {
[email protected]e5d2f7132010-04-09 18:13:531191 string16 last_search_prepopulate_text =
1192 FindBarState::GetLastPrepopulateText(profile());
1193
[email protected]87fae512009-12-11 02:20:211194 // Try the last thing we searched for on this tab, then the last thing
1195 // searched for on any tab.
1196 if (!previous_find_text_.empty())
1197 search_string = previous_find_text_;
[email protected]e5d2f7132010-04-09 18:13:531198 else if (!last_search_prepopulate_text.empty())
1199 search_string = last_search_prepopulate_text;
[email protected]87fae512009-12-11 02:20:211200 else
[email protected]e491f1c2009-05-22 20:28:121201 return;
[email protected]e491f1c2009-05-22 20:28:121202 }
[email protected]6e95706d2009-04-23 22:55:011203
[email protected]101c90e2009-10-26 21:14:441204 // Keep track of the previous search.
1205 previous_find_text_ = find_text_;
1206
[email protected]6e95706d2009-04-23 22:55:011207 // This is a FindNext operation if we are searching for the same text again,
1208 // or if the passed in search text is empty (FindNext keyboard shortcut). The
1209 // exception to this is if the Find was aborted (then we don't want FindNext
1210 // because the highlighting has been cleared and we need it to reappear). We
1211 // therefore treat FindNext after an aborted Find operation as a full fledged
1212 // Find.
[email protected]c1dc9f132009-11-20 23:13:211213 bool find_next = (find_text_ == search_string || search_string.empty()) &&
[email protected]e491f1c2009-05-22 20:28:121214 (last_search_case_sensitive_ == case_sensitive) &&
[email protected]6e95706d2009-04-23 22:55:011215 !find_op_aborted_;
1216 if (!find_next)
1217 current_find_request_id_ = find_request_id_counter_++;
1218
[email protected]c1dc9f132009-11-20 23:13:211219 if (!search_string.empty())
1220 find_text_ = search_string;
[email protected]e491f1c2009-05-22 20:28:121221 last_search_case_sensitive_ = case_sensitive;
[email protected]6e95706d2009-04-23 22:55:011222
1223 find_op_aborted_ = false;
1224
1225 // Keep track of what the last search was across the tabs.
[email protected]e5d2f7132010-04-09 18:13:531226 FindBarState* find_bar_state = profile()->GetFindBarState();
1227 find_bar_state->set_last_prepopulate_text(find_text_);
[email protected]6e95706d2009-04-23 22:55:011228 render_view_host()->StartFinding(current_find_request_id_,
1229 find_text_,
1230 forward_direction,
[email protected]e491f1c2009-05-22 20:28:121231 case_sensitive,
[email protected]6e95706d2009-04-23 22:55:011232 find_next);
1233}
1234
[email protected]5bc8fe92010-03-11 18:19:001235void TabContents::StopFinding(
1236 FindBarController::SelectionAction selection_action) {
[email protected]d6e8d332010-06-04 22:26:011237 if (selection_action == FindBarController::kClearSelection) {
1238 // kClearSelection means the find string has been cleared by the user, but
1239 // the UI has not been dismissed. In that case we want to clear the
1240 // previously remembered search (http://crbug.com/42639).
1241 previous_find_text_ = string16();
1242 } else {
[email protected]2d2dffc2009-06-09 21:41:001243 find_ui_active_ = false;
[email protected]d6e8d332010-06-04 22:26:011244 if (!find_text_.empty())
1245 previous_find_text_ = find_text_;
1246 }
[email protected]101c90e2009-10-26 21:14:441247 find_text_.clear();
[email protected]6e95706d2009-04-23 22:55:011248 find_op_aborted_ = true;
[email protected]e491f1c2009-05-22 20:28:121249 last_search_result_ = FindNotificationDetails();
[email protected]5bc8fe92010-03-11 18:19:001250 render_view_host()->StopFinding(selection_action);
[email protected]6e95706d2009-04-23 22:55:011251}
1252
[email protected]420ae012009-04-24 05:16:321253void TabContents::OnSavePage() {
1254 // If we can not save the page, try to download it.
1255 if (!SavePackage::IsSavableContents(contents_mime_type())) {
1256 DownloadManager* dlm = profile()->GetDownloadManager();
1257 const GURL& current_page_url = GetURL();
1258 if (dlm && current_page_url.is_valid())
[email protected]d82ed61e2009-06-16 02:46:221259 dlm->DownloadUrl(current_page_url, GURL(), "", this);
[email protected]420ae012009-04-24 05:16:321260 return;
1261 }
1262
1263 Stop();
1264
1265 // Create the save package and possibly prompt the user for the name to save
1266 // the page as. The user prompt is an asynchronous operation that runs on
1267 // another thread.
[email protected]d82ed61e2009-06-16 02:46:221268 save_package_ = new SavePackage(this);
[email protected]420ae012009-04-24 05:16:321269 save_package_->GetSaveInfo();
1270}
1271
1272// Used in automated testing to bypass prompting the user for file names.
1273// Instead, the names and paths are hard coded rather than running them through
1274// file name sanitation and extension / mime checking.
[email protected]828cabe2009-09-26 22:47:111275bool TabContents::SavePage(const FilePath& main_file, const FilePath& dir_path,
[email protected]420ae012009-04-24 05:16:321276 SavePackage::SavePackageType save_type) {
1277 // Stop the page from navigating.
1278 Stop();
1279
[email protected]828cabe2009-09-26 22:47:111280 save_package_ = new SavePackage(this, save_type, main_file, dir_path);
[email protected]cdf6cc82009-08-04 21:48:591281 return save_package_->Init();
[email protected]420ae012009-04-24 05:16:321282}
1283
[email protected]89d6e6e2009-12-03 23:06:511284void TabContents::EmailPageLocation() {
1285 std::string title = EscapeQueryParamValue(UTF16ToUTF8(GetTitle()), false);
1286 std::string page_url = EscapeQueryParamValue(GetURL().spec(), false);
1287 std::string mailto = std::string("mailto:?subject=Fwd:%20") +
1288 title + "&body=%0A%0A" + page_url;
1289 platform_util::OpenExternal(GURL(mailto));
1290}
1291
[email protected]420ae012009-04-24 05:16:321292void TabContents::PrintPreview() {
1293 // We don't show the print preview yet, only the print dialog.
1294 PrintNow();
1295}
1296
1297bool TabContents::PrintNow() {
1298 // We can't print interstitial page for now.
1299 if (showing_interstitial_page())
1300 return false;
1301
1302 return render_view_host()->PrintPages();
1303}
1304
[email protected]82270452009-06-19 15:58:011305void TabContents::PrintingDone(int document_cookie, bool success) {
1306 render_view_host()->PrintingDone(document_cookie, success);
1307}
1308
[email protected]420ae012009-04-24 05:16:321309bool TabContents::IsActiveEntry(int32 page_id) {
1310 NavigationEntry* active_entry = controller_.GetActiveEntry();
1311 return (active_entry != NULL &&
1312 active_entry->site_instance() == GetSiteInstance() &&
1313 active_entry->page_id() == page_id);
1314}
1315
[email protected]b2fe07d12010-02-09 14:38:081316void TabContents::SetOverrideEncoding(const std::string& encoding) {
[email protected]8cb5d5b2010-02-09 11:36:161317 set_encoding(encoding);
1318 render_view_host()->SetPageEncoding(encoding);
1319}
1320
[email protected]b2fe07d12010-02-09 14:38:081321void TabContents::ResetOverrideEncoding() {
[email protected]8cb5d5b2010-02-09 11:36:161322 reset_encoding();
1323 render_view_host()->ResetPageEncodingToDefault();
1324}
1325
1326void TabContents::WindowMoveOrResizeStarted() {
1327 render_view_host()->WindowMoveOrResizeStarted();
1328}
1329
[email protected]3f8eb7f2009-10-02 23:58:051330void TabContents::LogNewTabTime(const std::string& event_name) {
1331 // Not all new tab pages get timed. In those cases, we don't have a
1332 // new_tab_start_time_.
1333 if (new_tab_start_time_.is_null())
1334 return;
1335
1336 base::TimeDelta duration = base::TimeTicks::Now() - new_tab_start_time_;
[email protected]84abba62009-10-07 17:01:441337 MetricEventDurationDetails details(event_name,
1338 static_cast<int>(duration.InMilliseconds()));
1339
[email protected]7ab1e7d62009-10-14 23:32:011340 if (event_name == "Tab.NewTabScriptStart") {
1341 UMA_HISTOGRAM_TIMES("Tab.NewTabScriptStart", duration);
1342 } else if (event_name == "Tab.NewTabDOMContentLoaded") {
1343 UMA_HISTOGRAM_TIMES("Tab.NewTabDOMContentLoaded", duration);
1344 } else if (event_name == "Tab.NewTabOnload") {
1345 UMA_HISTOGRAM_TIMES("Tab.NewTabOnload", duration);
[email protected]3f8eb7f2009-10-02 23:58:051346 // The new tab page has finished loading; reset it.
1347 new_tab_start_time_ = base::TimeTicks();
[email protected]84abba62009-10-07 17:01:441348 } else {
1349 NOTREACHED();
[email protected]3f8eb7f2009-10-02 23:58:051350 }
[email protected]84abba62009-10-07 17:01:441351 NotificationService::current()->Notify(
1352 NotificationType::METRIC_EVENT_DURATION,
1353 Source<TabContents>(this),
1354 Details<MetricEventDurationDetails>(&details));
[email protected]3f8eb7f2009-10-02 23:58:051355}
1356
[email protected]7ab1e7d62009-10-14 23:32:011357void TabContents::OnCloseStarted() {
1358 if (tab_close_start_time_.is_null())
1359 tab_close_start_time_ = base::TimeTicks::Now();
1360}
1361
[email protected]46624bf2010-06-09 16:04:191362bool TabContents::ShouldAcceptDragAndDrop() const {
1363#if defined(OS_CHROMEOS)
1364 // ChromeOS panels (pop-ups) do not take drag-n-drop.
1365 // See http://crosbug.com/2413
1366 return delegate() && !delegate()->IsPopup(const_cast<TabContents*>(this));
1367#else
1368 return true;
1369#endif
1370}
1371
[email protected]c37e3b62010-01-22 00:15:171372TabContents* TabContents::CloneAndMakePhantom() {
[email protected]c37e3b62010-01-22 00:15:171373 TabNavigation tab_nav;
[email protected]df72ac72010-02-06 00:43:161374
1375 NavigationEntry* entry = controller().GetActiveEntry();
[email protected]98aa0b52010-05-06 17:03:081376 if (extension_app_)
1377 tab_nav.set_virtual_url(extension_app_->GetFullLaunchURL());
[email protected]df72ac72010-02-06 00:43:161378 else if (entry)
1379 tab_nav.SetFromNavigationEntry(*entry);
1380
[email protected]c37e3b62010-01-22 00:15:171381 std::vector<TabNavigation> navigations;
1382 navigations.push_back(tab_nav);
1383
1384 TabContents* new_contents =
1385 new TabContents(profile(), NULL, MSG_ROUTING_NONE, NULL);
[email protected]98aa0b52010-05-06 17:03:081386 new_contents->SetExtensionApp(extension_app_);
[email protected]c37e3b62010-01-22 00:15:171387 new_contents->controller().RestoreFromState(navigations, 0, false);
[email protected]c37e3b62010-01-22 00:15:171388
[email protected]98aa0b52010-05-06 17:03:081389 if (!extension_app_ && entry)
[email protected]df72ac72010-02-06 00:43:161390 new_contents->controller().GetActiveEntry()->favicon() = entry->favicon();
1391
[email protected]c37e3b62010-01-22 00:15:171392 return new_contents;
1393}
1394
[email protected]0de80162010-02-03 04:52:351395// Resets the |content_blocked_| array.
1396void TabContents::ClearBlockedContentSettings() {
[email protected]15cf526b2010-02-12 06:33:491397 for (size_t i = 0; i < arraysize(content_blocked_); ++i)
[email protected]0de80162010-02-03 04:52:351398 content_blocked_[i] = false;
1399}
1400
[email protected]96d185d2009-04-24 03:28:541401// Notifies the RenderWidgetHost instance about the fact that the page is
1402// loading, or done loading and calls the base implementation.
1403void TabContents::SetIsLoading(bool is_loading,
1404 LoadNotificationDetails* details) {
1405 if (is_loading == is_loading_)
1406 return;
1407
1408 if (!is_loading) {
1409 load_state_ = net::LOAD_STATE_IDLE;
1410 load_state_host_.clear();
[email protected]094e5b22009-09-25 04:23:561411 upload_size_ = 0;
1412 upload_position_ = 0;
[email protected]96d185d2009-04-24 03:28:541413 }
1414
1415 render_manager_.SetIsLoading(is_loading);
1416
1417 is_loading_ = is_loading;
1418 waiting_for_response_ = is_loading;
1419
1420 if (delegate_)
1421 delegate_->LoadingStateChanged(this);
[email protected]329581b2009-04-28 06:52:351422 NotifyNavigationStateChanged(INVALIDATE_LOAD);
[email protected]96d185d2009-04-24 03:28:541423
1424 NotificationType type = is_loading ? NotificationType::LOAD_START :
1425 NotificationType::LOAD_STOP;
[email protected]ce5c4502009-05-06 16:46:111426 NotificationDetails det = NotificationService::NoDetails();
[email protected]96d185d2009-04-24 03:28:541427 if (details)
1428 det = Details<LoadNotificationDetails>(details);
1429 NotificationService::current()->Notify(type,
1430 Source<NavigationController>(&controller_),
1431 det);
1432}
1433
[email protected]b6c874582009-05-08 19:38:311434void TabContents::AddPopup(TabContents* new_contents,
[email protected]7e7414ae2010-01-26 20:19:291435 const gfx::Rect& initial_pos) {
[email protected]57eda82a2010-05-05 21:28:061436 // A page can't spawn popups (or do anything else, either) until its load
1437 // commits, so when we reach here, the popup was spawned by the
1438 // NavigationController's last committed entry, not the active entry. For
1439 // example, if a page opens a popup in an onunload() handler, then the active
1440 // entry is the page to be loaded as we navigate away from the unloading
1441 // page. For this reason, we can't use GetURL() to get the opener URL,
1442 // because it returns the active entry.
1443 NavigationEntry* entry = controller_.GetLastCommittedEntry();
1444 GURL creator = entry ? entry->virtual_url() : GURL::EmptyGURL();
1445
1446 if (creator.is_valid() &&
[email protected]8cc8d492010-02-02 10:40:491447 profile()->GetHostContentSettingsMap()->GetContentSetting(
[email protected]57eda82a2010-05-05 21:28:061448 creator, CONTENT_SETTINGS_TYPE_POPUPS) == CONTENT_SETTING_ALLOW) {
[email protected]8cc8d492010-02-02 10:40:491449 AddNewContents(new_contents, NEW_POPUP, initial_pos, true);
1450 } else {
1451 if (!blocked_popups_)
1452 blocked_popups_ = new BlockedPopupContainer(this);
1453 blocked_popups_->AddTabContents(new_contents, initial_pos);
1454 }
[email protected]b9681312008-11-07 00:08:261455}
[email protected]616ed5a2008-11-21 22:27:241456
1457namespace {
1458bool TransitionIsReload(PageTransition::Type transition) {
1459 return PageTransition::StripQualifier(transition) == PageTransition::RELOAD;
1460}
1461}
1462
1463void TabContents::ExpireInfoBars(
1464 const NavigationController::LoadCommittedDetails& details) {
1465 // Only hide InfoBars when the user has done something that makes the main
1466 // frame load. We don't want various automatic or subframe navigations making
1467 // it disappear.
1468 if (!details.is_user_initiated_main_frame_load())
1469 return;
1470
[email protected]f86a07022008-11-25 01:06:051471 for (int i = infobar_delegate_count() - 1; i >= 0; --i) {
[email protected]616ed5a2008-11-21 22:27:241472 InfoBarDelegate* delegate = GetInfoBarDelegateAt(i);
[email protected]f86a07022008-11-25 01:06:051473 if (delegate->ShouldExpire(details))
[email protected]616ed5a2008-11-21 22:27:241474 RemoveInfoBar(delegate);
[email protected]616ed5a2008-11-21 22:27:241475 }
1476}
[email protected]fdd61c62009-04-22 19:22:571477
[email protected]96d185d2009-04-24 03:28:541478DOMUI* TabContents::GetDOMUIForCurrentState() {
1479 // When there is a pending navigation entry, we want to use the pending DOMUI
1480 // that goes along with it to control the basic flags. For example, we want to
1481 // show the pending URL in the URL bar, so we want the display_url flag to
1482 // be from the pending entry.
1483 //
1484 // The confusion comes because there are multiple possibilities for the
1485 // initial load in a tab as a side effect of the way the RenderViewHostManager
1486 // works.
1487 //
1488 // - For the very first tab the load looks "normal". The new tab DOM UI is
1489 // the pending one, and we want it to apply here.
1490 //
1491 // - For subsequent new tabs, they'll get a new SiteInstance which will then
1492 // get switched to the one previously associated with the new tab pages.
1493 // This switching will cause the manager to commit the RVH/DOMUI. So we'll
1494 // have a committed DOM UI in this case.
1495 //
1496 // This condition handles all of these cases:
1497 //
1498 // - First load in first tab: no committed nav entry + pending nav entry +
1499 // pending dom ui:
1500 // -> Use pending DOM UI if any.
1501 //
1502 // - First load in second tab: no committed nav entry + pending nav entry +
1503 // no pending DOM UI:
1504 // -> Use the committed DOM UI if any.
1505 //
1506 // - Second navigation in any tab: committed nav entry + pending nav entry:
1507 // -> Use pending DOM UI if any.
1508 //
1509 // - Normal state with no load: committed nav entry + no pending nav entry:
1510 // -> Use committed DOM UI.
1511 if (controller_.pending_entry() &&
1512 (controller_.GetLastCommittedEntry() ||
1513 render_manager_.pending_dom_ui()))
1514 return render_manager_.pending_dom_ui();
1515 return render_manager_.dom_ui();
1516}
[email protected]420ae012009-04-24 05:16:321517
1518void TabContents::DidNavigateMainFramePostCommit(
1519 const NavigationController::LoadCommittedDetails& details,
1520 const ViewHostMsg_FrameNavigate_Params& params) {
[email protected]c2e74fe82009-09-03 17:57:441521 if (opener_dom_ui_type_ != DOMUIFactory::kNoDOMUI) {
1522 // If this is a window.open navigation, use the same DOMUI as the renderer
1523 // that opened the window, as long as both renderers have the same
1524 // privileges.
1525 if (opener_dom_ui_type_ == DOMUIFactory::GetDOMUIType(GetURL())) {
1526 DOMUI* dom_ui = DOMUIFactory::CreateDOMUIForURL(this, GetURL());
1527 // dom_ui might be NULL if the URL refers to a non-existent extension.
1528 if (dom_ui) {
1529 render_manager_.SetDOMUIPostCommit(dom_ui);
1530 dom_ui->RenderViewCreated(render_view_host());
1531 }
1532 }
1533 opener_dom_ui_type_ = DOMUIFactory::kNoDOMUI;
1534 }
1535
[email protected]420ae012009-04-24 05:16:321536 if (details.is_user_initiated_main_frame_load()) {
1537 // Clear the status bubble. This is a workaround for a bug where WebKit
1538 // doesn't let us know that the cursor left an element during a
1539 // transition (this is also why the mouse cursor remains as a hand after
1540 // clicking on a link); see bugs 1184641 and 980803. We don't want to
1541 // clear the bubble when a user navigates to a named anchor in the same
1542 // page.
1543 UpdateTargetURL(details.entry->page_id(), GURL());
1544
1545 // UpdateHelpersForDidNavigate will handle the case where the password_form
1546 // origin is valid.
1547 // TODO(brettw) bug 1343111: Password manager stuff in here needs to be
1548 // cleaned up and covered by tests.
1549 if (!params.password_form.origin.is_valid())
1550 GetPasswordManager()->DidNavigate();
1551 }
1552
1553 // The keyword generator uses the navigation entries, so must be called after
1554 // the commit.
1555 GenerateKeywordIfNecessary(params);
1556
1557 // Allow the new page to set the title again.
1558 received_page_title_ = false;
1559
1560 // Get the favicon, either from history or request it from the net.
1561 fav_icon_helper_.FetchFavIcon(details.entry->url());
1562
[email protected]0de80162010-02-03 04:52:351563 // Clear all page actions, blocked content notifications and browser actions
1564 // for this tab, unless this is an in-page navigation.
[email protected]a6e82fc2010-02-24 22:28:141565 if (!details.is_in_page) {
[email protected]92c6f9b92009-10-24 04:35:081566 ExtensionsService* service = profile()->GetExtensionsService();
1567 if (service) {
1568 for (size_t i = 0; i < service->extensions()->size(); ++i) {
[email protected]5d246db22009-10-27 06:17:571569 ExtensionAction* browser_action =
[email protected]92c6f9b92009-10-24 04:35:081570 service->extensions()->at(i)->browser_action();
[email protected]56ce6e52009-10-27 00:10:521571 if (browser_action) {
1572 browser_action->ClearAllValuesForTab(controller().session_id().id());
1573 NotificationService::current()->Notify(
1574 NotificationType::EXTENSION_BROWSER_ACTION_UPDATED,
[email protected]5d246db22009-10-27 06:17:571575 Source<ExtensionAction>(browser_action),
[email protected]56ce6e52009-10-27 00:10:521576 NotificationService::NoDetails());
1577 }
[email protected]92c6f9b92009-10-24 04:35:081578
[email protected]5d246db22009-10-27 06:17:571579 ExtensionAction* page_action =
[email protected]56ce6e52009-10-27 00:10:521580 service->extensions()->at(i)->page_action();
1581 if (page_action) {
1582 page_action->ClearAllValuesForTab(controller().session_id().id());
1583 PageActionStateChanged();
1584 }
[email protected]92c6f9b92009-10-24 04:35:081585 }
[email protected]aece2c7f2009-08-27 20:43:171586 }
[email protected]0de80162010-02-03 04:52:351587
1588 // Close blocked popups.
1589 if (blocked_popups_) {
[email protected]0fbd70332010-06-01 19:28:341590 AutoReset<bool> auto_reset(&dont_notify_render_view_, true);
[email protected]0de80162010-02-03 04:52:351591 blocked_popups_->Destroy();
[email protected]da7b5262010-02-25 21:12:461592 blocked_popups_ = NULL;
[email protected]0de80162010-02-03 04:52:351593 }
1594
1595 // Clear "blocked" flags.
1596 ClearBlockedContentSettings();
[email protected]777a7b62010-05-11 13:28:161597 geolocation_settings_state_.DidNavigate(details);
[email protected]0de80162010-02-03 04:52:351598 if (delegate_)
[email protected]20ad6a92010-04-21 14:07:241599 delegate_->OnContentSettingsChange(this);
[email protected]f17a0ee2010-05-17 17:38:471600
1601 // Once the main frame is navigated, we're no longer considered to have
1602 // displayed insecure content.
1603 displayed_insecure_content_ = false;
[email protected]aece2c7f2009-08-27 20:43:171604 }
[email protected]ce5c4502009-05-06 16:46:111605
[email protected]8cc8d492010-02-02 10:40:491606 // Close constrained windows if necessary.
1607 if (!net::RegistryControlledDomainService::SameDomainOrHost(
1608 details.previous_url, details.entry->url()))
1609 CloseConstrainedWindows();
1610
[email protected]420ae012009-04-24 05:16:321611 // Update the starred state.
1612 UpdateStarredStateForCurrentURL();
[email protected]c07c9752010-03-12 05:57:531613
1614 // Clear the cache of forms in AutoFill.
1615 if (autofill_manager_.get())
1616 autofill_manager_->Reset();
[email protected]420ae012009-04-24 05:16:321617}
1618
1619void TabContents::DidNavigateAnyFramePostCommit(
1620 RenderViewHost* render_view_host,
1621 const NavigationController::LoadCommittedDetails& details,
1622 const ViewHostMsg_FrameNavigate_Params& params) {
1623 // If we navigate, start showing messages again. This does nothing to prevent
1624 // a malicious script from spamming messages, since the script could just
1625 // reload the page to stop blocking.
1626 suppress_javascript_messages_ = false;
1627
[email protected]420ae012009-04-24 05:16:321628 // Notify the password manager of the navigation or form submit.
1629 // TODO(brettw) bug 1343111: Password manager stuff in here needs to be
1630 // cleaned up and covered by tests.
1631 if (params.password_form.origin.is_valid())
1632 GetPasswordManager()->ProvisionallySavePassword(params.password_form);
[email protected]3b1c1812010-02-12 21:53:551633
1634 // Let the LanguageState clear its state.
[email protected]34cec8d82010-03-05 18:27:011635 bool reload = details.entry->transition_type() == PageTransition::RELOAD ||
1636 details.type == NavigationType::SAME_PAGE;
1637 language_state_.DidNavigate(reload, details.is_in_page);
[email protected]420ae012009-04-24 05:16:321638}
1639
[email protected]8cc8d492010-02-02 10:40:491640void TabContents::CloseConstrainedWindows() {
1641 // Clear out any constrained windows since we are leaving this page entirely.
[email protected]420ae012009-04-24 05:16:321642 // We use indices instead of iterators in case CloseWindow does something
1643 // that may invalidate an iterator.
1644 int size = static_cast<int>(child_windows_.size());
1645 for (int i = size - 1; i >= 0; --i) {
1646 ConstrainedWindow* window = child_windows_[i];
[email protected]fa1cf0b82010-01-15 21:49:441647 if (window) {
[email protected]420ae012009-04-24 05:16:321648 window->CloseConstrainedWindow();
[email protected]fa1cf0b82010-01-15 21:49:441649 BlockTabContent(false);
1650 }
[email protected]420ae012009-04-24 05:16:321651 }
1652}
1653
1654void TabContents::UpdateStarredStateForCurrentURL() {
1655 BookmarkModel* model = profile()->GetBookmarkModel();
1656 const bool old_state = is_starred_;
1657 is_starred_ = (model && model->IsBookmarked(GetURL()));
1658
1659 if (is_starred_ != old_state && delegate())
1660 delegate()->URLStarredChanged(this, is_starred_);
1661}
1662
1663void TabContents::UpdateAlternateErrorPageURL() {
1664 GURL url = GetAlternateErrorPageURL();
1665 render_view_host()->SetAlternateErrorPageURL(url);
1666}
1667
1668void TabContents::UpdateWebPreferences() {
1669 render_view_host()->UpdateWebPreferences(GetWebkitPrefs());
1670}
1671
1672void TabContents::UpdateMaxPageIDIfNecessary(SiteInstance* site_instance,
1673 RenderViewHost* rvh) {
1674 // If we are creating a RVH for a restored controller, then we might
1675 // have more page IDs than the SiteInstance's current max page ID. We must
1676 // make sure that the max page ID is larger than any restored page ID.
1677 // Note that it is ok for conflicting page IDs to exist in another tab
1678 // (i.e., NavigationController), but if any page ID is larger than the max,
1679 // the back/forward list will get confused.
1680 int max_restored_page_id = controller_.max_restored_page_id();
1681 if (max_restored_page_id > 0) {
1682 int curr_max_page_id = site_instance->max_page_id();
1683 if (max_restored_page_id > curr_max_page_id) {
1684 // Need to update the site instance immediately.
1685 site_instance->UpdateMaxPageID(max_restored_page_id);
1686
1687 // Also tell the renderer to update its internal representation. We
1688 // need to reserve enough IDs to make all restored page IDs less than
1689 // the max.
1690 if (curr_max_page_id < 0)
1691 curr_max_page_id = 0;
1692 rvh->ReservePageIDRange(max_restored_page_id - curr_max_page_id);
1693 }
1694 }
1695}
1696
1697void TabContents::UpdateHistoryForNavigation(
[email protected]ebe89e062009-08-13 23:16:541698 const GURL& virtual_url,
[email protected]befd8d822009-07-01 04:51:471699 const NavigationController::LoadCommittedDetails& details,
[email protected]420ae012009-04-24 05:16:321700 const ViewHostMsg_FrameNavigate_Params& params) {
1701 if (profile()->IsOffTheRecord())
1702 return;
1703
1704 // Add to history service.
1705 HistoryService* hs = profile()->GetHistoryService(Profile::IMPLICIT_ACCESS);
1706 if (hs) {
1707 if (PageTransition::IsMainFrame(params.transition) &&
[email protected]ebe89e062009-08-13 23:16:541708 virtual_url != params.url) {
1709 // Hack on the "virtual" URL so that it will appear in history. For some
[email protected]420ae012009-04-24 05:16:321710 // types of URLs, we will display a magic URL that is different from where
1711 // the page is actually navigated. We want the user to see in history
[email protected]ebe89e062009-08-13 23:16:541712 // what they saw in the URL bar, so we add the virtual URL as a redirect.
1713 // This only applies to the main frame, as the virtual URL doesn't apply
[email protected]420ae012009-04-24 05:16:321714 // to sub-frames.
1715 std::vector<GURL> redirects = params.redirects;
1716 if (!redirects.empty())
[email protected]ebe89e062009-08-13 23:16:541717 redirects.back() = virtual_url;
1718 hs->AddPage(virtual_url, this, params.page_id, params.referrer,
[email protected]befd8d822009-07-01 04:51:471719 params.transition, redirects, details.did_replace_entry);
[email protected]420ae012009-04-24 05:16:321720 } else {
1721 hs->AddPage(params.url, this, params.page_id, params.referrer,
[email protected]befd8d822009-07-01 04:51:471722 params.transition, params.redirects,
1723 details.did_replace_entry);
[email protected]420ae012009-04-24 05:16:321724 }
1725 }
1726}
1727
1728bool TabContents::UpdateTitleForEntry(NavigationEntry* entry,
1729 const std::wstring& title) {
1730 // For file URLs without a title, use the pathname instead. In the case of a
1731 // synthesized title, we don't want the update to count toward the "one set
1732 // per page of the title to history."
1733 std::wstring final_title;
1734 bool explicit_set;
1735 if (entry->url().SchemeIsFile() && title.empty()) {
1736 final_title = UTF8ToWide(entry->url().ExtractFileName());
1737 explicit_set = false; // Don't count synthetic titles toward the set limit.
1738 } else {
1739 TrimWhitespace(title, TRIM_ALL, &final_title);
1740 explicit_set = true;
1741 }
1742
1743 if (final_title == UTF16ToWideHack(entry->title()))
1744 return false; // Nothing changed, don't bother.
1745
1746 entry->set_title(WideToUTF16Hack(final_title));
1747
1748 // Update the history system for this page.
1749 if (!profile()->IsOffTheRecord() && !received_page_title_) {
1750 HistoryService* hs =
1751 profile()->GetHistoryService(Profile::IMPLICIT_ACCESS);
1752 if (hs)
[email protected]ebe89e062009-08-13 23:16:541753 hs->SetPageTitle(entry->virtual_url(), final_title);
[email protected]420ae012009-04-24 05:16:321754
1755 // Don't allow the title to be saved again for explicitly set ones.
1756 received_page_title_ = explicit_set;
1757 }
1758
1759 // Lastly, set the title for the view.
1760 view_->SetPageTitle(final_title);
1761
[email protected]cbc0e1b2010-04-12 18:33:041762 NotificationService::current()->Notify(
1763 NotificationType::TAB_CONTENTS_TITLE_UPDATED,
1764 Source<TabContents>(this),
1765 NotificationService::NoDetails());
1766
[email protected]420ae012009-04-24 05:16:321767 return true;
1768}
1769
1770void TabContents::NotifySwapped() {
1771 // After sending out a swap notification, we need to send a disconnect
1772 // notification so that clients that pick up a pointer to |this| can NULL the
1773 // pointer. See Bug 1230284.
1774 notify_disconnection_ = true;
1775 NotificationService::current()->Notify(
[email protected]57c6a652009-05-04 07:58:341776 NotificationType::TAB_CONTENTS_SWAPPED,
[email protected]d82ed61e2009-06-16 02:46:221777 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:321778 NotificationService::NoDetails());
1779}
1780
1781void TabContents::NotifyConnected() {
1782 notify_disconnection_ = true;
1783 NotificationService::current()->Notify(
[email protected]57c6a652009-05-04 07:58:341784 NotificationType::TAB_CONTENTS_CONNECTED,
[email protected]d82ed61e2009-06-16 02:46:221785 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:321786 NotificationService::NoDetails());
1787}
1788
1789void TabContents::NotifyDisconnected() {
1790 if (!notify_disconnection_)
1791 return;
1792
1793 notify_disconnection_ = false;
1794 NotificationService::current()->Notify(
[email protected]57c6a652009-05-04 07:58:341795 NotificationType::TAB_CONTENTS_DISCONNECTED,
[email protected]d82ed61e2009-06-16 02:46:221796 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:321797 NotificationService::NoDetails());
1798}
1799
1800void TabContents::GenerateKeywordIfNecessary(
1801 const ViewHostMsg_FrameNavigate_Params& params) {
1802 if (!params.searchable_form_url.is_valid())
1803 return;
1804
1805 if (profile()->IsOffTheRecord())
1806 return;
1807
1808 int last_index = controller_.last_committed_entry_index();
1809 // When there was no previous page, the last index will be 0. This is
1810 // normally due to a form submit that opened in a new tab.
1811 // TODO(brettw) bug 916126: we should support keywords when form submits
1812 // happen in new tabs.
1813 if (last_index <= 0)
1814 return;
1815 const NavigationEntry* previous_entry =
1816 controller_.GetEntryAtIndex(last_index - 1);
1817 if (IsFormSubmit(previous_entry)) {
1818 // Only generate a keyword if the previous page wasn't itself a form
1819 // submit.
1820 return;
1821 }
1822
1823 GURL keyword_url = previous_entry->user_typed_url().is_valid() ?
1824 previous_entry->user_typed_url() : previous_entry->url();
1825 std::wstring keyword =
1826 TemplateURLModel::GenerateKeyword(keyword_url, true); // autodetected
1827 if (keyword.empty())
1828 return;
1829
1830 TemplateURLModel* url_model = profile()->GetTemplateURLModel();
1831 if (!url_model)
1832 return;
1833
1834 if (!url_model->loaded()) {
1835 url_model->Load();
1836 return;
1837 }
1838
1839 const TemplateURL* current_url;
1840 std::wstring url = UTF8ToWide(params.searchable_form_url.spec());
1841 if (!url_model->CanReplaceKeyword(keyword, url, &current_url))
1842 return;
1843
1844 if (current_url) {
1845 if (current_url->originating_url().is_valid()) {
1846 // The existing keyword was generated from an OpenSearch description
1847 // document, don't regenerate.
1848 return;
1849 }
1850 url_model->Remove(current_url);
1851 }
1852 TemplateURL* new_url = new TemplateURL();
1853 new_url->set_keyword(keyword);
1854 new_url->set_short_name(keyword);
1855 new_url->SetURL(url, 0, 0);
1856 new_url->add_input_encoding(params.searchable_form_encoding);
1857 DCHECK(controller_.GetLastCommittedEntry());
1858 const GURL& favicon_url =
1859 controller_.GetLastCommittedEntry()->favicon().url();
1860 if (favicon_url.is_valid()) {
1861 new_url->SetFavIconURL(favicon_url);
1862 } else {
1863 // The favicon url isn't valid. This means there really isn't a favicon,
1864 // or the favicon url wasn't obtained before the load started. This assumes
1865 // the later.
1866 // TODO(sky): Need a way to set the favicon that doesn't involve generating
1867 // its url.
1868 new_url->SetFavIconURL(TemplateURL::GenerateFaviconURL(params.referrer));
1869 }
1870 new_url->set_safe_for_autoreplace(true);
1871 url_model->Add(new_url);
1872}
1873
[email protected]bf5c2ff392009-07-08 16:24:331874void TabContents::OnUserGesture() {
1875 // See comment in RenderViewHostDelegate::OnUserGesture as to why we do this.
1876 DownloadRequestManager* drm = g_browser_process->download_request_manager();
1877 if (drm)
1878 drm->OnUserGesture(this);
[email protected]e7eaedde2009-09-25 20:09:491879 ExternalProtocolHandler::OnUserGesture();
[email protected]bf5c2ff392009-07-08 16:24:331880 controller_.OnUserGesture();
1881}
1882
1883void TabContents::OnFindReply(int request_id,
1884 int number_of_matches,
1885 const gfx::Rect& selection_rect,
1886 int active_match_ordinal,
1887 bool final_update) {
[email protected]3ce7ced2009-07-24 18:52:421888 // Ignore responses for requests that have been aborted.
1889 if (find_op_aborted_)
1890 return;
1891
[email protected]bf5c2ff392009-07-08 16:24:331892 // Ignore responses for requests other than the one we have most recently
1893 // issued. That way we won't act on stale results when the user has
1894 // already typed in another query.
1895 if (request_id != current_find_request_id_)
1896 return;
1897
1898 if (number_of_matches == -1)
1899 number_of_matches = last_search_result_.number_of_matches();
1900 if (active_match_ordinal == -1)
1901 active_match_ordinal = last_search_result_.active_match_ordinal();
1902
1903 gfx::Rect selection = selection_rect;
1904 if (selection.IsEmpty())
1905 selection = last_search_result_.selection_rect();
1906
1907 // Notify the UI, automation and any other observers that a find result was
1908 // found.
1909 last_search_result_ = FindNotificationDetails(request_id, number_of_matches,
1910 selection, active_match_ordinal,
1911 final_update);
1912 NotificationService::current()->Notify(
1913 NotificationType::FIND_RESULT_AVAILABLE,
1914 Source<TabContents>(this),
1915 Details<FindNotificationDetails>(&last_search_result_));
1916}
1917
1918void TabContents::GoToEntryAtOffset(int offset) {
[email protected]f9cc4c452009-10-13 14:56:381919 if (!delegate_ || delegate_->OnGoToEntryOffset(offset))
1920 controller_.GoToOffset(offset);
[email protected]bf5c2ff392009-07-08 16:24:331921}
1922
[email protected]bf5c2ff392009-07-08 16:24:331923void TabContents::OnMissingPluginStatus(int status) {
1924#if defined(OS_WIN)
1925// TODO(PORT): pull in when plug-ins work
1926 GetPluginInstaller()->OnMissingPluginStatus(status);
1927#endif
1928}
1929
1930void TabContents::OnCrashedPlugin(const FilePath& plugin_path) {
[email protected]bf5c2ff392009-07-08 16:24:331931 DCHECK(!plugin_path.value().empty());
1932
1933 std::wstring plugin_name = plugin_path.ToWStringHack();
[email protected]d32d46182009-07-23 00:09:351934#if defined(OS_WIN) || defined(OS_MACOSX)
[email protected]bf5c2ff392009-07-08 16:24:331935 scoped_ptr<FileVersionInfo> version_info(
1936 FileVersionInfo::CreateFileVersionInfo(plugin_path));
1937 if (version_info.get()) {
1938 const std::wstring& product_name = version_info->product_name();
[email protected]996fd702009-09-04 19:12:371939 if (!product_name.empty()) {
[email protected]bf5c2ff392009-07-08 16:24:331940 plugin_name = product_name;
[email protected]996fd702009-09-04 19:12:371941#if defined(OS_MACOSX)
1942 // Many plugins on the Mac have .plugin in the actual name, which looks
1943 // terrible, so look for that and strip it off if present.
1944 const std::wstring plugin_extension(L".plugin");
1945 if (EndsWith(plugin_name, plugin_extension, true))
1946 plugin_name.erase(plugin_name.length() - plugin_extension.length());
1947#endif // OS_MACOSX
1948 }
[email protected]bf5c2ff392009-07-08 16:24:331949 }
[email protected]4a560ed2009-07-13 23:35:261950#else
1951 NOTIMPLEMENTED() << " convert plugin path to plugin name";
1952#endif
[email protected]bf5c2ff392009-07-08 16:24:331953 AddInfoBar(new SimpleAlertInfoBarDelegate(
1954 this, l10n_util::GetStringF(IDS_PLUGIN_CRASHED_PROMPT, plugin_name),
[email protected]938e1f92010-04-01 18:09:421955 NULL, true));
[email protected]bf5c2ff392009-07-08 16:24:331956}
1957
1958void TabContents::OnCrashedWorker() {
1959 AddInfoBar(new SimpleAlertInfoBarDelegate(
1960 this, l10n_util::GetString(IDS_WEBWORKER_CRASHED_PROMPT),
[email protected]938e1f92010-04-01 18:09:421961 NULL, true));
[email protected]bf5c2ff392009-07-08 16:24:331962}
1963
1964void TabContents::OnDidGetApplicationInfo(
1965 int32 page_id,
1966 const webkit_glue::WebApplicationInfo& info) {
[email protected]ed5431872009-11-17 08:39:511967 web_app_info_ = info;
[email protected]bf5c2ff392009-07-08 16:24:331968
[email protected]ed5431872009-11-17 08:39:511969 if (delegate())
1970 delegate()->OnDidGetApplicationInfo(this, page_id);
[email protected]bf5c2ff392009-07-08 16:24:331971}
1972
[email protected]a2cf65eb2010-01-18 08:20:381973void TabContents::OnPageContents(const GURL& url,
1974 int renderer_process_id,
1975 int32 page_id,
[email protected]e5106202010-06-11 21:12:361976 const string16& contents,
[email protected]a8a81292010-01-21 00:32:451977 const std::string& language) {
[email protected]a2cf65eb2010-01-18 08:20:381978 // Don't index any https pages. People generally don't want their bank
1979 // accounts, etc. indexed on their computer, especially since some of these
1980 // things are not marked cachable.
1981 // TODO(brettw) we may want to consider more elaborate heuristics such as
1982 // the cachability of the page. We may also want to consider subframes (this
1983 // test will still index subframes if the subframe is SSL).
[email protected]ccd40a52010-05-11 16:15:271984 // TODO(zelidrag) bug chromium-os:2808 - figure out if we want to reenable
1985 // content indexing for chromeos in some future releases.
1986#if !defined(OS_CHROMEOS)
[email protected]a2cf65eb2010-01-18 08:20:381987 if (!url.SchemeIsSecure()) {
1988 Profile* p = profile();
1989 if (p && !p->IsOffTheRecord()) {
1990 HistoryService* hs = p->GetHistoryService(Profile::IMPLICIT_ACCESS);
1991 if (hs)
1992 hs->SetPageContents(url, contents);
1993 }
1994 }
[email protected]ccd40a52010-05-11 16:15:271995#endif
[email protected]a8a81292010-01-21 00:32:451996
[email protected]3b1c1812010-02-12 21:53:551997 language_state_.LanguageDetermined(language);
[email protected]a8a81292010-01-21 00:32:451998
1999 std::string lang = language;
2000 NotificationService::current()->Notify(
2001 NotificationType::TAB_LANGUAGE_DETERMINED,
[email protected]7e0831b2010-02-01 08:45:152002 Source<TabContents>(this),
[email protected]a8a81292010-01-21 00:32:452003 Details<std::string>(&lang));
[email protected]a2cf65eb2010-01-18 08:20:382004}
2005
[email protected]7e0831b2010-02-01 08:45:152006void TabContents::OnPageTranslated(int32 page_id,
2007 const std::string& original_lang,
[email protected]152b8ba32010-03-29 19:40:162008 const std::string& translated_lang,
2009 TranslateErrors::Type error_type) {
[email protected]3b1c1812010-02-12 21:53:552010 language_state_.set_current_language(translated_lang);
2011 language_state_.set_translation_pending(false);
[email protected]152b8ba32010-03-29 19:40:162012 PageTranslatedDetails details(original_lang, translated_lang, error_type);
[email protected]7e0831b2010-02-01 08:45:152013 NotificationService::current()->Notify(
2014 NotificationType::PAGE_TRANSLATED,
2015 Source<TabContents>(this),
[email protected]152b8ba32010-03-29 19:40:162016 Details<PageTranslatedDetails>(&details));
[email protected]7e0831b2010-02-01 08:45:152017}
2018
[email protected]bf5c2ff392009-07-08 16:24:332019void TabContents::DidStartProvisionalLoadForFrame(
2020 RenderViewHost* render_view_host,
2021 bool is_main_frame,
2022 const GURL& url) {
2023 ProvisionalLoadDetails details(is_main_frame,
2024 controller_.IsURLInPageNavigation(url),
2025 url, std::string(), false);
2026 NotificationService::current()->Notify(
2027 NotificationType::FRAME_PROVISIONAL_LOAD_START,
2028 Source<NavigationController>(&controller_),
2029 Details<ProvisionalLoadDetails>(&details));
2030}
2031
2032void TabContents::DidStartReceivingResourceResponse(
[email protected]3dbb80b2010-02-09 22:41:202033 const ResourceRequestDetails& details) {
[email protected]bf5c2ff392009-07-08 16:24:332034 NotificationService::current()->Notify(
2035 NotificationType::RESOURCE_RESPONSE_STARTED,
2036 Source<NavigationController>(&controller()),
[email protected]3dbb80b2010-02-09 22:41:202037 Details<const ResourceRequestDetails>(&details));
[email protected]bf5c2ff392009-07-08 16:24:332038}
2039
[email protected]3dbb80b2010-02-09 22:41:202040void TabContents::DidRedirectResource(
2041 const ResourceRedirectDetails& details) {
[email protected]bf5c2ff392009-07-08 16:24:332042 NotificationService::current()->Notify(
2043 NotificationType::RESOURCE_RECEIVED_REDIRECT,
2044 Source<NavigationController>(&controller()),
[email protected]3dbb80b2010-02-09 22:41:202045 Details<const ResourceRequestDetails>(&details));
[email protected]bf5c2ff392009-07-08 16:24:332046}
2047
2048void TabContents::DidLoadResourceFromMemoryCache(
2049 const GURL& url,
2050 const std::string& frame_origin,
2051 const std::string& main_frame_origin,
2052 const std::string& security_info) {
2053 // Send out a notification that we loaded a resource from our memory cache.
2054 int cert_id = 0, cert_status = 0, security_bits = 0;
2055 SSLManager::DeserializeSecurityInfo(security_info,
2056 &cert_id, &cert_status,
2057 &security_bits);
2058 LoadFromMemoryCacheDetails details(url, frame_origin, main_frame_origin,
[email protected]8cb5d5b2010-02-09 11:36:162059 GetRenderProcessHost()->id(), cert_id,
2060 cert_status);
[email protected]bf5c2ff392009-07-08 16:24:332061
2062 NotificationService::current()->Notify(
2063 NotificationType::LOAD_FROM_MEMORY_CACHE,
2064 Source<NavigationController>(&controller_),
2065 Details<LoadFromMemoryCacheDetails>(&details));
2066}
2067
[email protected]e3d60e5d2009-09-25 21:08:292068void TabContents::DidDisplayInsecureContent() {
[email protected]f17a0ee2010-05-17 17:38:472069 displayed_insecure_content_ = true;
2070 SSLManager::NotifySSLInternalStateChanged();
[email protected]e3d60e5d2009-09-25 21:08:292071}
2072
2073void TabContents::DidRunInsecureContent(const std::string& security_origin) {
[email protected]09843b5f2009-09-26 08:30:592074 controller_.ssl_manager()->DidRunInsecureContent(security_origin);
[email protected]e3d60e5d2009-09-25 21:08:292075}
2076
[email protected]bf5c2ff392009-07-08 16:24:332077void TabContents::DidFailProvisionalLoadWithError(
2078 RenderViewHost* render_view_host,
2079 bool is_main_frame,
2080 int error_code,
2081 const GURL& url,
2082 bool showing_repost_interstitial) {
2083 if (net::ERR_ABORTED == error_code) {
2084 // EVIL HACK ALERT! Ignore failed loads when we're showing interstitials.
2085 // This means that the interstitial won't be torn down properly, which is
2086 // bad. But if we have an interstitial, go back to another tab type, and
2087 // then load the same interstitial again, we could end up getting the first
2088 // interstitial's "failed" message (as a result of the cancel) when we're on
2089 // the second one.
2090 //
2091 // We can't tell this apart, so we think we're tearing down the current page
2092 // which will cause a crash later one. There is also some code in
2093 // RenderViewHostManager::RendererAbortedProvisionalLoad that is commented
2094 // out because of this problem.
2095 //
2096 // http://code.google.com/p/chromium/issues/detail?id=2855
2097 // Because this will not tear down the interstitial properly, if "back" is
2098 // back to another tab type, the interstitial will still be somewhat alive
2099 // in the previous tab type. If you navigate somewhere that activates the
2100 // tab with the interstitial again, you'll see a flash before the new load
2101 // commits of the interstitial page.
2102 if (showing_interstitial_page()) {
2103 LOG(WARNING) << "Discarding message during interstitial.";
2104 return;
2105 }
2106
2107 // This will discard our pending entry if we cancelled the load (e.g., if we
2108 // decided to download the file instead of load it). Only discard the
2109 // pending entry if the URLs match, otherwise the user initiated a navigate
2110 // before the page loaded so that the discard would discard the wrong entry.
2111 NavigationEntry* pending_entry = controller_.pending_entry();
2112 if (pending_entry && pending_entry->url() == url) {
2113 controller_.DiscardNonCommittedEntries();
2114 // Update the URL display.
2115 NotifyNavigationStateChanged(TabContents::INVALIDATE_URL);
2116 }
2117
2118 render_manager_.RendererAbortedProvisionalLoad(render_view_host);
2119 }
2120
2121 // Send out a notification that we failed a provisional load with an error.
2122 ProvisionalLoadDetails details(is_main_frame,
2123 controller_.IsURLInPageNavigation(url),
2124 url, std::string(), false);
2125 details.set_error_code(error_code);
2126
2127 NotificationService::current()->Notify(
2128 NotificationType::FAIL_PROVISIONAL_LOAD_WITH_ERROR,
2129 Source<NavigationController>(&controller_),
2130 Details<ProvisionalLoadDetails>(&details));
2131}
2132
2133void TabContents::DocumentLoadedInFrame() {
2134 controller_.DocumentLoadedInFrame();
[email protected]f017d8d2010-06-10 00:22:562135
2136 render_view_host()->SetDocumentLoaded(true);
[email protected]bf5c2ff392009-07-08 16:24:332137}
2138
[email protected]0de80162010-02-03 04:52:352139void TabContents::OnContentBlocked(ContentSettingsType type) {
[email protected]b1235062010-03-25 17:31:272140 // TODO(joth): remove once fully implemented.
2141 DCHECK(type != CONTENT_SETTINGS_TYPE_GEOLOCATION);
[email protected]0de80162010-02-03 04:52:352142 content_blocked_[type] = true;
2143 if (delegate_)
[email protected]20ad6a92010-04-21 14:07:242144 delegate_->OnContentSettingsChange(this);
[email protected]0de80162010-02-03 04:52:352145}
2146
[email protected]614c0e12010-03-25 10:12:562147void TabContents::OnGeolocationPermissionSet(const GURL& requesting_origin,
2148 bool allowed) {
[email protected]777a7b62010-05-11 13:28:162149 geolocation_settings_state_.OnGeolocationPermissionSet(requesting_origin,
2150 allowed);
[email protected]614c0e12010-03-25 10:12:562151 if (delegate_)
[email protected]20ad6a92010-04-21 14:07:242152 delegate_->OnContentSettingsChange(this);
[email protected]614c0e12010-03-25 10:12:562153}
2154
[email protected]8d3347f2009-07-09 22:00:212155RenderViewHostDelegate::View* TabContents::GetViewDelegate() {
[email protected]420ae012009-04-24 05:16:322156 return view_.get();
2157}
2158
[email protected]8d3347f2009-07-09 22:00:212159RenderViewHostDelegate::RendererManagement*
2160TabContents::GetRendererManagementDelegate() {
2161 return &render_manager_;
2162}
2163
[email protected]bf5c2ff392009-07-08 16:24:332164RenderViewHostDelegate::BrowserIntegration*
[email protected]8d3347f2009-07-09 22:00:212165 TabContents::GetBrowserIntegrationDelegate() {
2166 return this;
[email protected]bf5c2ff392009-07-08 16:24:332167}
2168
[email protected]8d3347f2009-07-09 22:00:212169RenderViewHostDelegate::Resource* TabContents::GetResourceDelegate() {
2170 return this;
[email protected]bf5c2ff392009-07-08 16:24:332171}
2172
[email protected]8d3347f2009-07-09 22:00:212173RenderViewHostDelegate::Save* TabContents::GetSaveDelegate() {
[email protected]420ae012009-04-24 05:16:322174 return save_package_.get(); // May be NULL, but we can return NULL.
2175}
2176
[email protected]8d3347f2009-07-09 22:00:212177RenderViewHostDelegate::Printing* TabContents::GetPrintingDelegate() {
2178 return &printing_;
2179}
2180
2181RenderViewHostDelegate::FavIcon* TabContents::GetFavIconDelegate() {
2182 return &fav_icon_helper_;
2183}
2184
[email protected]e8dbd674b2010-03-11 18:34:402185RenderViewHostDelegate::Autocomplete* TabContents::GetAutocompleteDelegate() {
[email protected]6db0def92010-03-02 19:55:452186 if (autocomplete_history_manager_.get() == NULL)
2187 autocomplete_history_manager_.reset(new AutocompleteHistoryManager(this));
2188 return autocomplete_history_manager_.get();
[email protected]bf5c2ff392009-07-08 16:24:332189}
2190
[email protected]21674df2009-10-28 19:51:212191RenderViewHostDelegate::AutoFill* TabContents::GetAutoFillDelegate() {
[email protected]cf28ec52010-04-09 22:56:342192 return GetAutoFillManager();
[email protected]21674df2009-10-28 19:51:212193}
2194
[email protected]fd571bb2010-03-19 01:23:542195AutomationResourceRoutingDelegate*
2196TabContents::GetAutomationResourceRoutingDelegate() {
2197 return delegate();
2198}
2199
[email protected]ced90ae12010-02-20 02:06:162200RenderViewHostDelegate::BookmarkDrag* TabContents::GetBookmarkDragDelegate() {
2201 return bookmark_drag_;
2202}
2203
2204void TabContents::SetBookmarkDragDelegate(
2205 RenderViewHostDelegate::BookmarkDrag* bookmark_drag) {
2206 bookmark_drag_ = bookmark_drag;
2207}
2208
[email protected]93623c5d2009-12-10 21:40:322209RendererPreferences TabContents::GetRendererPrefs(Profile* profile) const {
[email protected]840b1512009-07-21 16:53:462210 return renderer_preferences_;
[email protected]80d96fa2009-06-10 22:34:512211}
2212
[email protected]57c6a652009-05-04 07:58:342213TabContents* TabContents::GetAsTabContents() {
2214 return this;
2215}
2216
[email protected]7b291f92009-08-14 05:43:532217ViewType::Type TabContents::GetRenderViewType() const {
2218 return ViewType::TAB_CONTENTS;
2219}
2220
2221int TabContents::GetBrowserWindowID() const {
2222 return controller().window_id().id();
2223}
2224
[email protected]420ae012009-04-24 05:16:322225void TabContents::RenderViewCreated(RenderViewHost* render_view_host) {
[email protected]2e4633c2009-07-09 16:58:062226 NotificationService::current()->Notify(
2227 NotificationType::RENDER_VIEW_HOST_CREATED_FOR_TAB,
2228 Source<TabContents>(this),
2229 Details<RenderViewHost>(render_view_host));
[email protected]420ae012009-04-24 05:16:322230 NavigationEntry* entry = controller_.GetActiveEntry();
2231 if (!entry)
2232 return;
2233
2234 // When we're creating views, we're still doing initial setup, so we always
2235 // use the pending DOM UI rather than any possibly existing committed one.
2236 if (render_manager_.pending_dom_ui()) {
2237 render_manager_.pending_dom_ui()->RenderViewCreated(render_view_host);
2238 }
2239
2240 if (entry->IsViewSourceMode()) {
2241 // Put the renderer in view source mode.
2242 render_view_host->Send(
2243 new ViewMsg_EnableViewSourceMode(render_view_host->routing_id()));
2244 }
[email protected]0666aef2009-05-13 19:48:082245
2246 view()->RenderViewCreated(render_view_host);
[email protected]420ae012009-04-24 05:16:322247}
2248
2249void TabContents::RenderViewReady(RenderViewHost* rvh) {
2250 if (rvh != render_view_host()) {
2251 // Don't notify the world, since this came from a renderer in the
2252 // background.
2253 return;
2254 }
2255
2256 NotifyConnected();
[email protected]9d8a4642009-07-29 17:25:302257 bool was_crashed = is_crashed();
[email protected]420ae012009-04-24 05:16:322258 SetIsCrashed(false);
[email protected]9d8a4642009-07-29 17:25:302259
2260 // Restore the focus to the tab (otherwise the focus will be on the top
2261 // window).
2262 if (was_crashed && !FocusLocationBarByDefault())
2263 Focus();
[email protected]420ae012009-04-24 05:16:322264}
2265
2266void TabContents::RenderViewGone(RenderViewHost* rvh) {
2267 // Ask the print preview if this renderer was valuable.
2268 if (!printing_.OnRenderViewGone(rvh))
2269 return;
2270 if (rvh != render_view_host()) {
2271 // The pending page's RenderViewHost is gone.
2272 return;
2273 }
2274
2275 SetIsLoading(false, NULL);
2276 NotifyDisconnected();
2277 SetIsCrashed(true);
2278
[email protected]ff9efc62010-02-18 22:10:542279 // Remove all infobars.
[email protected]e5430c92010-02-18 22:39:212280 for (int i = infobar_delegate_count() - 1; i >=0 ; --i)
[email protected]ff9efc62010-02-18 22:10:542281 RemoveInfoBar(GetInfoBarDelegateAt(i));
2282
[email protected]8e537302009-06-10 21:19:582283 // Tell the view that we've crashed so it can prepare the sad tab page.
[email protected]cf68b092010-01-15 20:05:552284 // Only do this if we're not in browser shutdown, so that TabContents
2285 // objects that are not in a browser (e.g., HTML dialogs) and thus are
2286 // visible do not flash a sad tab page.
2287 if (browser_shutdown::GetShutdownType() == browser_shutdown::NOT_VALID)
2288 view_->OnTabCrashed();
[email protected]420ae012009-04-24 05:16:322289
2290 // Hide any visible hung renderer warning for this web contents' process.
[email protected]e7b8d832009-11-20 22:41:002291 hung_renderer_dialog::HideForTabContents(this);
[email protected]420ae012009-04-24 05:16:322292}
2293
[email protected]2e4633c2009-07-09 16:58:062294void TabContents::RenderViewDeleted(RenderViewHost* rvh) {
2295 NotificationService::current()->Notify(
2296 NotificationType::RENDER_VIEW_HOST_DELETED,
2297 Source<TabContents>(this),
2298 Details<RenderViewHost>(rvh));
[email protected]fb7b79f72009-11-06 18:00:482299 render_manager_.RenderViewDeleted(rvh);
[email protected]2e4633c2009-07-09 16:58:062300}
2301
[email protected]420ae012009-04-24 05:16:322302void TabContents::DidNavigate(RenderViewHost* rvh,
2303 const ViewHostMsg_FrameNavigate_Params& params) {
[email protected]8030f012009-09-25 18:09:372304 int extra_invalidate_flags = 0;
2305
[email protected]f017d8d2010-06-10 00:22:562306 render_view_host()->SetDocumentLoaded(false);
[email protected]b3df5a42010-05-11 14:31:092307
[email protected]8030f012009-09-25 18:09:372308 if (PageTransition::IsMainFrame(params.transition)) {
[email protected]20ede072009-10-06 00:28:352309 bool was_bookmark_bar_visible = ShouldShowBookmarkBar();
[email protected]8030f012009-09-25 18:09:372310 bool was_extension_shelf_visible = IsExtensionShelfAlwaysVisible();
2311
[email protected]420ae012009-04-24 05:16:322312 render_manager_.DidNavigateMainFrame(rvh);
2313
[email protected]20ede072009-10-06 00:28:352314 if (was_bookmark_bar_visible != ShouldShowBookmarkBar())
[email protected]8030f012009-09-25 18:09:372315 extra_invalidate_flags |= INVALIDATE_BOOKMARK_BAR;
2316 if (was_extension_shelf_visible != IsExtensionShelfAlwaysVisible())
2317 extra_invalidate_flags |= INVALIDATE_EXTENSION_SHELF;
2318 }
2319
[email protected]420ae012009-04-24 05:16:322320 // Update the site of the SiteInstance if it doesn't have one yet.
2321 if (!GetSiteInstance()->has_site())
2322 GetSiteInstance()->SetSite(params.url);
2323
2324 // Need to update MIME type here because it's referred to in
2325 // UpdateNavigationCommands() called by RendererDidNavigate() to
2326 // determine whether or not to enable the encoding menu.
2327 // It's updated only for the main frame. For a subframe,
2328 // RenderView::UpdateURL does not set params.contents_mime_type.
2329 // (see http://code.google.com/p/chromium/issues/detail?id=2929 )
2330 // TODO(jungshik): Add a test for the encoding menu to avoid
2331 // regressing it again.
2332 if (PageTransition::IsMainFrame(params.transition))
2333 contents_mime_type_ = params.contents_mime_type;
2334
2335 NavigationController::LoadCommittedDetails details;
[email protected]8030f012009-09-25 18:09:372336 bool did_navigate = controller_.RendererDidNavigate(
2337 params, extra_invalidate_flags, &details);
[email protected]3e90d4a2009-07-03 17:38:392338
2339 // Update history. Note that this needs to happen after the entry is complete,
2340 // which WillNavigate[Main,Sub]Frame will do before this function is called.
[email protected]2e29d922009-10-22 22:13:352341 if (params.should_update_history &&
2342 (!delegate() || delegate()->ShouldAddNavigationsToHistory())) {
[email protected]3e90d4a2009-07-03 17:38:392343 // Most of the time, the displayURL matches the loaded URL, but for about:
2344 // URLs, we use a data: URL as the real value. We actually want to save
2345 // the about: URL to the history db and keep the data: URL hidden. This is
2346 // what the TabContents' URL getter does.
2347 UpdateHistoryForNavigation(GetURL(), details, params);
2348 }
2349
[email protected]76543b92009-08-31 17:27:452350 if (!did_navigate)
2351 return; // No navigation happened.
[email protected]420ae012009-04-24 05:16:322352
2353 // DO NOT ADD MORE STUFF TO THIS FUNCTION! Your component should either listen
2354 // for the appropriate notification (best) or you can add it to
2355 // DidNavigateMainFramePostCommit / DidNavigateAnyFramePostCommit (only if
2356 // necessary, please).
2357
2358 // Run post-commit tasks.
2359 if (details.is_main_frame)
2360 DidNavigateMainFramePostCommit(details, params);
2361 DidNavigateAnyFramePostCommit(rvh, details, params);
2362}
2363
2364void TabContents::UpdateState(RenderViewHost* rvh,
2365 int32 page_id,
2366 const std::string& state) {
2367 DCHECK(rvh == render_view_host());
2368
2369 // We must be prepared to handle state updates for any page, these occur
2370 // when the user is scrolling and entering form data, as well as when we're
2371 // leaving a page, in which case our state may have already been moved to
2372 // the next page. The navigation controller will look up the appropriate
2373 // NavigationEntry and update it when it is notified via the delegate.
2374
2375 int entry_index = controller_.GetEntryIndexWithPageID(
2376 GetSiteInstance(), page_id);
2377 if (entry_index < 0)
2378 return;
2379 NavigationEntry* entry = controller_.GetEntryAtIndex(entry_index);
2380
2381 if (state == entry->content_state())
2382 return; // Nothing to update.
2383 entry->set_content_state(state);
2384 controller_.NotifyEntryChanged(entry, entry_index);
2385}
2386
2387void TabContents::UpdateTitle(RenderViewHost* rvh,
2388 int32 page_id, const std::wstring& title) {
2389 // If we have a title, that's a pretty good indication that we've started
2390 // getting useful data.
2391 SetNotWaitingForResponse();
2392
2393 DCHECK(rvh == render_view_host());
2394 NavigationEntry* entry = controller_.GetEntryWithPageID(GetSiteInstance(),
2395 page_id);
2396 if (!entry || !UpdateTitleForEntry(entry, title))
2397 return;
2398
2399 // Broadcast notifications when the UI should be updated.
2400 if (entry == controller_.GetEntryAtOffset(0))
[email protected]f1cd5e82009-10-23 17:19:032401 NotifyNavigationStateChanged(INVALIDATE_TITLE);
[email protected]420ae012009-04-24 05:16:322402}
2403
[email protected]420ae012009-04-24 05:16:322404void TabContents::UpdateEncoding(RenderViewHost* render_view_host,
[email protected]41fc0322009-09-04 22:23:402405 const std::string& encoding) {
[email protected]420ae012009-04-24 05:16:322406 set_encoding(encoding);
2407}
2408
2409void TabContents::UpdateTargetURL(int32 page_id, const GURL& url) {
2410 if (delegate())
2411 delegate()->UpdateTargetURL(this, url);
2412}
2413
2414void TabContents::UpdateThumbnail(const GURL& url,
2415 const SkBitmap& bitmap,
2416 const ThumbnailScore& score) {
2417 // Tell History about this thumbnail
[email protected]523623c2010-05-19 16:03:422418 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kTopSites)) {
[email protected]72d0fc72009-07-09 17:30:492419 if (!profile()->IsOffTheRecord())
[email protected]523623c2010-05-19 16:03:422420 profile()->GetTopSites()->SetPageThumbnail(url, bitmap, score);
[email protected]449478302009-06-09 20:04:282421 } else {
2422 HistoryService* hs;
2423 if (!profile()->IsOffTheRecord() &&
2424 (hs = profile()->GetHistoryService(Profile::IMPLICIT_ACCESS))) {
2425 hs->SetPageThumbnail(url, bitmap, score);
2426 }
[email protected]420ae012009-04-24 05:16:322427 }
2428}
2429
[email protected]c4e98902010-06-01 10:20:142430void TabContents::UpdateInspectorSetting(const std::string& key,
2431 const std::string& value) {
2432 DictionaryValue* inspector_settings =
2433 profile()->GetPrefs()->GetMutableDictionary(
2434 prefs::kWebKitInspectorSettings);
[email protected]628cf5a52010-06-07 14:25:332435 inspector_settings->SetWithoutPathExpansion(UTF8ToWide(key),
2436 Value::CreateStringValue(value));
[email protected]0df30122009-06-03 12:13:082437}
2438
[email protected]45aafc92010-06-07 14:18:532439void TabContents::ClearInspectorSettings() {
2440 DictionaryValue* inspector_settings =
2441 profile()->GetPrefs()->GetMutableDictionary(
2442 prefs::kWebKitInspectorSettings);
2443 inspector_settings->Clear();
2444}
2445
[email protected]420ae012009-04-24 05:16:322446void TabContents::Close(RenderViewHost* rvh) {
[email protected]07707302009-11-06 00:50:292447 // The UI may be in an event-tracking loop, such as between the
2448 // mouse-down and mouse-up in text selection or a button click.
2449 // Defer the close until after tracking is complete, so that we
2450 // don't free objects out from under the UI.
2451 // TODO(shess): This could probably be integrated with the
2452 // IsDoingDrag() test below. Punting for now because I need more
2453 // research to understand how this impacts platforms other than Mac.
2454 // TODO(shess): This could get more fine-grained. For instance,
2455 // closing a tab in another window while selecting text in the
2456 // current window's Omnibox should be just fine.
2457 if (view()->IsEventTracking()) {
2458 view()->CloseTabAfterEventTracking();
2459 return;
2460 }
2461
[email protected]24a4d1062009-07-10 23:10:422462 // If we close the tab while we're in the middle of a drag, we'll crash.
2463 // Instead, cancel the drag and close it as soon as the drag ends.
2464 if (view()->IsDoingDrag()) {
2465 view()->CancelDragAndCloseTab();
2466 return;
2467 }
2468
[email protected]420ae012009-04-24 05:16:322469 // Ignore this if it comes from a RenderViewHost that we aren't showing.
2470 if (delegate() && rvh == render_view_host())
2471 delegate()->CloseContents(this);
2472}
2473
2474void TabContents::RequestMove(const gfx::Rect& new_bounds) {
2475 if (delegate() && delegate()->IsPopup(this))
2476 delegate()->MoveContents(this, new_bounds);
2477}
2478
[email protected]7ab1e7d62009-10-14 23:32:012479void TabContents::DidStartLoading() {
[email protected]420ae012009-04-24 05:16:322480 SetIsLoading(true, NULL);
2481}
2482
[email protected]7ab1e7d62009-10-14 23:32:012483void TabContents::DidStopLoading() {
[email protected]420ae012009-04-24 05:16:322484 scoped_ptr<LoadNotificationDetails> details;
2485
2486 NavigationEntry* entry = controller_.GetActiveEntry();
2487 // An entry may not exist for a stop when loading an initial blank page or
2488 // if an iframe injected by script into a blank page finishes loading.
2489 if (entry) {
[email protected]420ae012009-04-24 05:16:322490 base::TimeDelta elapsed = base::TimeTicks::Now() - current_load_start_;
2491
2492 details.reset(new LoadNotificationDetails(
[email protected]ebe89e062009-08-13 23:16:542493 entry->virtual_url(),
[email protected]420ae012009-04-24 05:16:322494 entry->transition_type(),
2495 elapsed,
2496 &controller_,
2497 controller_.GetCurrentEntryIndex()));
2498 }
2499
2500 // Tell PasswordManager we've finished a page load, which serves as a
2501 // green light to save pending passwords and reset itself.
2502 GetPasswordManager()->DidStopLoading();
2503
2504 SetIsLoading(false, details.get());
2505}
2506
[email protected]420ae012009-04-24 05:16:322507void TabContents::DidRedirectProvisionalLoad(int32 page_id,
2508 const GURL& source_url,
2509 const GURL& target_url) {
2510 NavigationEntry* entry;
2511 if (page_id == -1)
2512 entry = controller_.pending_entry();
2513 else
2514 entry = controller_.GetEntryWithPageID(GetSiteInstance(), page_id);
2515 if (!entry || entry->url() != source_url)
2516 return;
2517 entry->set_url(target_url);
2518}
2519
[email protected]420ae012009-04-24 05:16:322520void TabContents::RequestOpenURL(const GURL& url, const GURL& referrer,
2521 WindowOpenDisposition disposition) {
2522 if (render_manager_.dom_ui()) {
2523 // When we're a DOM UI, it will provide a page transition type for us (this
2524 // is so the new tab page can specify AUTO_BOOKMARK for automatically
2525 // generated suggestions).
2526 //
2527 // Note also that we hide the referrer for DOM UI pages. We don't really
[email protected]60e448982009-05-06 04:21:162528 // want web sites to see a referrer of "chrome://blah" (and some
2529 // chrome: URLs might have search terms or other stuff we don't want to
[email protected]420ae012009-04-24 05:16:322530 // send to the site), so we send no referrer.
2531 OpenURL(url, GURL(), disposition,
2532 render_manager_.dom_ui()->link_transition_type());
2533 } else {
2534 OpenURL(url, referrer, disposition, PageTransition::LINK);
2535 }
2536}
2537
[email protected]1ee614862009-07-06 20:29:182538void TabContents::DomOperationResponse(const std::string& json_string,
2539 int automation_id) {
2540}
2541
[email protected]420ae012009-04-24 05:16:322542void TabContents::ProcessDOMUIMessage(const std::string& message,
[email protected]438c97d2010-05-21 23:30:152543 const ListValue* content,
[email protected]bb64b512010-04-02 21:01:392544 const GURL& source_url,
[email protected]811bfe32009-07-01 08:46:252545 int request_id,
2546 bool has_callback) {
[email protected]420ae012009-04-24 05:16:322547 if (!render_manager_.dom_ui()) {
[email protected]c2e74fe82009-09-03 17:57:442548 // This can happen if someone uses window.open() to open an extension URL
2549 // from a non-extension context.
2550 render_view_host()->BlockExtensionRequest(request_id);
[email protected]420ae012009-04-24 05:16:322551 return;
2552 }
[email protected]bb64b512010-04-02 21:01:392553 render_manager_.dom_ui()->ProcessDOMUIMessage(message, content, source_url,
2554 request_id,
[email protected]811bfe32009-07-01 08:46:252555 has_callback);
[email protected]420ae012009-04-24 05:16:322556}
2557
2558void TabContents::ProcessExternalHostMessage(const std::string& message,
2559 const std::string& origin,
2560 const std::string& target) {
2561 if (delegate())
2562 delegate()->ForwardMessageToExternalHost(message, origin, target);
2563}
2564
[email protected]cdaf8d02010-03-30 19:52:472565void TabContents::RunFileChooser(
2566 const ViewHostMsg_RunFileChooser_Params &params) {
[email protected]420ae012009-04-24 05:16:322567 if (!select_file_dialog_.get())
2568 select_file_dialog_ = SelectFileDialog::Create(this);
[email protected]cdaf8d02010-03-30 19:52:472569
2570 SelectFileDialog::Type dialog_type;
2571 switch (params.mode) {
2572 case ViewHostMsg_RunFileChooser_Params::Open:
2573 dialog_type = SelectFileDialog::SELECT_OPEN_FILE;
2574 break;
2575 case ViewHostMsg_RunFileChooser_Params::OpenMultiple:
2576 dialog_type = SelectFileDialog::SELECT_OPEN_MULTI_FILE;
2577 break;
2578 case ViewHostMsg_RunFileChooser_Params::Save:
2579 dialog_type = SelectFileDialog::SELECT_SAVEAS_FILE;
2580 break;
2581 default:
[email protected]bed31952010-03-30 20:07:592582 dialog_type = SelectFileDialog::SELECT_OPEN_FILE; // Prevent warning.
[email protected]cdaf8d02010-03-30 19:52:472583 NOTREACHED();
2584 }
[email protected]35896a32010-06-09 08:42:512585 FilePath default_file_name = params.default_file_name;
2586 if (default_file_name.empty())
2587 default_file_name = profile()->last_selected_directory();
[email protected]cdaf8d02010-03-30 19:52:472588 select_file_dialog_->SelectFile(dialog_type, params.title,
[email protected]35896a32010-06-09 08:42:512589 default_file_name,
[email protected]420ae012009-04-24 05:16:322590 NULL, 0, FILE_PATH_LITERAL(""),
2591 view_->GetTopLevelNativeWindow(), NULL);
2592}
2593
2594void TabContents::RunJavaScriptMessage(
2595 const std::wstring& message,
2596 const std::wstring& default_prompt,
2597 const GURL& frame_url,
2598 const int flags,
2599 IPC::Message* reply_msg,
2600 bool* did_suppress_message) {
2601 // Suppress javascript messages when requested and when inside a constrained
2602 // popup window (because that activates them and breaks them out of the
2603 // constrained window jail).
[email protected]91ed49a2009-07-07 21:29:552604 // Also suppress messages when showing an interstitial. The interstitial is
2605 // shown over the previous page, we don't want the hidden page dialogs to
2606 // interfere with the interstitial.
2607 bool suppress_this_message = suppress_javascript_messages_ ||
2608 showing_interstitial_page();
[email protected]420ae012009-04-24 05:16:322609 if (delegate())
2610 suppress_this_message |=
[email protected]b6c874582009-05-08 19:38:312611 (delegate()->GetConstrainingContents(this) != this);
[email protected]420ae012009-04-24 05:16:322612
2613 *did_suppress_message = suppress_this_message;
2614
2615 if (!suppress_this_message) {
2616 base::TimeDelta time_since_last_message(
2617 base::TimeTicks::Now() - last_javascript_message_dismissal_);
2618 bool show_suppress_checkbox = false;
2619 // Show a checkbox offering to suppress further messages if this message is
2620 // being displayed within kJavascriptMessageExpectedDelay of the last one.
2621 if (time_since_last_message <
2622 base::TimeDelta::FromMilliseconds(kJavascriptMessageExpectedDelay))
2623 show_suppress_checkbox = true;
2624
[email protected]d82ed61e2009-06-16 02:46:222625 RunJavascriptMessageBox(this, frame_url, flags, message, default_prompt,
[email protected]420ae012009-04-24 05:16:322626 show_suppress_checkbox, reply_msg);
2627 } else {
2628 // If we are suppressing messages, just reply as is if the user immediately
2629 // pressed "Cancel".
[email protected]beb440c2009-11-06 04:08:542630 OnMessageBoxClosed(reply_msg, false, std::wstring());
[email protected]420ae012009-04-24 05:16:322631 }
2632}
2633
2634void TabContents::RunBeforeUnloadConfirm(const std::wstring& message,
2635 IPC::Message* reply_msg) {
[email protected]7ab1e7d62009-10-14 23:32:012636 is_showing_before_unload_dialog_ = true;
[email protected]d82ed61e2009-06-16 02:46:222637 RunBeforeUnloadDialog(this, message, reply_msg);
[email protected]420ae012009-04-24 05:16:322638}
2639
2640void TabContents::ShowModalHTMLDialog(const GURL& url, int width, int height,
2641 const std::string& json_arguments,
2642 IPC::Message* reply_msg) {
2643 if (delegate()) {
2644 HtmlDialogUIDelegate* dialog_delegate =
2645 new ModalHtmlDialogDelegate(url, width, height, json_arguments,
[email protected]d82ed61e2009-06-16 02:46:222646 reply_msg, this);
[email protected]420ae012009-04-24 05:16:322647 delegate()->ShowHtmlDialog(dialog_delegate, NULL);
2648 }
2649}
2650
[email protected]4d44a1c2010-04-28 19:20:412651void TabContents::PasswordFormsFound(
[email protected]ed3fb032009-06-16 19:50:562652 const std::vector<webkit_glue::PasswordForm>& forms) {
[email protected]4d44a1c2010-04-28 19:20:412653 GetPasswordManager()->PasswordFormsFound(forms);
2654}
2655
2656void TabContents::PasswordFormsVisible(
2657 const std::vector<webkit_glue::PasswordForm>& visible_forms) {
2658 GetPasswordManager()->PasswordFormsVisible(visible_forms);
[email protected]420ae012009-04-24 05:16:322659}
2660
[email protected]420ae012009-04-24 05:16:322661// Checks to see if we should generate a keyword based on the OSDD, and if
2662// necessary uses TemplateURLFetcher to download the OSDD and create a keyword.
2663void TabContents::PageHasOSDD(RenderViewHost* render_view_host,
2664 int32 page_id, const GURL& url,
2665 bool autodetected) {
2666 // Make sure page_id is the current page, and the TemplateURLModel is loaded.
2667 DCHECK(url.is_valid());
2668 if (!IsActiveEntry(page_id))
2669 return;
2670 TemplateURLModel* url_model = profile()->GetTemplateURLModel();
2671 if (!url_model)
2672 return;
2673 if (!url_model->loaded()) {
2674 url_model->Load();
2675 return;
2676 }
2677 if (!profile()->GetTemplateURLFetcher())
2678 return;
2679
2680 if (profile()->IsOffTheRecord())
2681 return;
2682
2683 const NavigationEntry* entry = controller_.GetLastCommittedEntry();
2684 DCHECK(entry);
2685
2686 const NavigationEntry* base_entry = entry;
2687 if (IsFormSubmit(base_entry)) {
2688 // If the current page is a form submit, find the last page that was not
2689 // a form submit and use its url to generate the keyword from.
2690 int index = controller_.last_committed_entry_index() - 1;
2691 while (index >= 0 && IsFormSubmit(controller_.GetEntryAtIndex(index)))
2692 index--;
2693 if (index >= 0)
2694 base_entry = controller_.GetEntryAtIndex(index);
2695 else
2696 base_entry = NULL;
2697 }
2698
2699 // We want to use the user typed URL if available since that represents what
2700 // the user typed to get here, and fall back on the regular URL if not.
2701 if (!base_entry)
2702 return;
2703 GURL keyword_url = base_entry->user_typed_url().is_valid() ?
2704 base_entry->user_typed_url() : base_entry->url();
2705 if (!keyword_url.is_valid())
2706 return;
2707 std::wstring keyword = TemplateURLModel::GenerateKeyword(keyword_url,
2708 autodetected);
[email protected]a88dbdd52010-01-15 19:25:532709
2710 // For JS added OSDD empty keyword is OK because we will generate keyword
2711 // later from OSDD content.
2712 if (autodetected && keyword.empty())
[email protected]420ae012009-04-24 05:16:322713 return;
2714 const TemplateURL* template_url =
2715 url_model->GetTemplateURLForKeyword(keyword);
2716 if (template_url && (!template_url->safe_for_autoreplace() ||
2717 template_url->originating_url() == url)) {
2718 // Either there is a user created TemplateURL for this keyword, or the
2719 // keyword has the same OSDD url and we've parsed it.
2720 return;
2721 }
2722
2723 // Download the OpenSearch description document. If this is successful a
2724 // new keyword will be created when done.
[email protected]420ae012009-04-24 05:16:322725 profile()->GetTemplateURLFetcher()->ScheduleDownload(
2726 keyword,
2727 url,
2728 base_entry->favicon().url(),
[email protected]d0cc9fb52009-06-23 02:26:162729 this,
[email protected]420ae012009-04-24 05:16:322730 autodetected);
2731}
2732
[email protected]420ae012009-04-24 05:16:322733GURL TabContents::GetAlternateErrorPageURL() const {
2734 GURL url;
2735 // Disable alternate error pages when in OffTheRecord/Incognito mode.
2736 if (profile()->IsOffTheRecord())
2737 return url;
2738
2739 PrefService* prefs = profile()->GetPrefs();
2740 DCHECK(prefs);
2741 if (prefs->GetBoolean(prefs::kAlternateErrorPagesEnabled)) {
[email protected]a33457b82009-09-21 23:07:152742 url = google_util::AppendGoogleLocaleParam(
2743 GURL(google_util::kLinkDoctorBaseURL));
[email protected]420ae012009-04-24 05:16:322744 url = google_util::AppendGoogleTLDParam(url);
2745 }
2746 return url;
2747}
2748
2749WebPreferences TabContents::GetWebkitPrefs() {
[email protected]6c32ce72010-03-08 05:18:062750 Profile* profile = render_view_host()->process()->profile();
[email protected]7ccddb8c2009-08-04 17:36:552751 bool is_dom_ui = false;
[email protected]6c32ce72010-03-08 05:18:062752 return RenderViewHostDelegateHelper::GetWebkitPrefs(profile, is_dom_ui);
[email protected]420ae012009-04-24 05:16:322753}
2754
[email protected]fa1cf0b82010-01-15 21:49:442755void TabContents::OnIgnoredUIEvent() {
2756 if (constrained_window_count()) {
2757 ConstrainedWindow* window = *constrained_window_begin();
2758 window->FocusConstrainedWindow();
2759 }
2760}
2761
[email protected]420ae012009-04-24 05:16:322762void TabContents::OnJSOutOfMemory() {
2763 AddInfoBar(new SimpleAlertInfoBarDelegate(
[email protected]938e1f92010-04-01 18:09:422764 this, l10n_util::GetString(IDS_JS_OUT_OF_MEMORY_PROMPT), NULL, true));
[email protected]420ae012009-04-24 05:16:322765}
2766
[email protected]420ae012009-04-24 05:16:322767void TabContents::OnCrossSiteResponse(int new_render_process_host_id,
2768 int new_request_id) {
[email protected]57c6a652009-05-04 07:58:342769 // Allows the TabContents to react when a cross-site response is ready to be
[email protected]420ae012009-04-24 05:16:322770 // delivered to a pending RenderViewHost. We must first run the onunload
2771 // handler of the old RenderViewHost before we can allow it to proceed.
2772 render_manager_.OnCrossSiteResponse(new_render_process_host_id,
2773 new_request_id);
2774}
2775
2776bool TabContents::CanBlur() const {
2777 return delegate() ? delegate()->CanBlur() : true;
2778}
2779
2780gfx::Rect TabContents::GetRootWindowResizerRect() const {
2781 if (delegate())
2782 return delegate()->GetRootWindowResizerRect();
2783 return gfx::Rect();
2784}
2785
2786void TabContents::RendererUnresponsive(RenderViewHost* rvh,
2787 bool is_during_unload) {
2788 if (is_during_unload) {
2789 // Hang occurred while firing the beforeunload/unload handler.
2790 // Pretend the handler fired so tab closing continues as if it had.
[email protected]829e7612009-04-25 01:15:112791 rvh->set_sudden_termination_allowed(true);
[email protected]420ae012009-04-24 05:16:322792
2793 if (!render_manager_.ShouldCloseTabOnUnresponsiveRenderer())
2794 return;
2795
2796 // If the tab hangs in the beforeunload/unload handler there's really
2797 // nothing we can do to recover. Pretend the unload listeners have
2798 // all fired and close the tab. If the hang is in the beforeunload handler
2799 // then the user will not have the option of cancelling the close.
2800 Close(rvh);
2801 return;
2802 }
2803
2804 if (render_view_host() && render_view_host()->IsRenderViewLive())
[email protected]e7b8d832009-11-20 22:41:002805 hung_renderer_dialog::ShowForTabContents(this);
[email protected]420ae012009-04-24 05:16:322806}
2807
2808void TabContents::RendererResponsive(RenderViewHost* render_view_host) {
[email protected]e7b8d832009-11-20 22:41:002809 hung_renderer_dialog::HideForTabContents(this);
[email protected]420ae012009-04-24 05:16:322810}
2811
2812void TabContents::LoadStateChanged(const GURL& url,
[email protected]094e5b22009-09-25 04:23:562813 net::LoadState load_state,
2814 uint64 upload_position,
2815 uint64 upload_size) {
[email protected]420ae012009-04-24 05:16:322816 load_state_ = load_state;
[email protected]094e5b22009-09-25 04:23:562817 upload_position_ = upload_position;
2818 upload_size_ = upload_size;
[email protected]d686e812009-06-03 19:10:292819 std::wstring languages =
2820 profile()->GetPrefs()->GetString(prefs::kAcceptLanguages);
[email protected]d686e812009-06-03 19:10:292821 std::string host = url.host();
[email protected]ce85f602009-11-07 01:34:532822 load_state_host_ =
2823 net::IDNToUnicode(host.c_str(), host.size(), languages, NULL);
[email protected]420ae012009-04-24 05:16:322824 if (load_state_ == net::LOAD_STATE_READING_RESPONSE)
2825 SetNotWaitingForResponse();
2826 if (is_loading())
[email protected]c9cd2222009-05-06 05:16:502827 NotifyNavigationStateChanged(INVALIDATE_LOAD | INVALIDATE_TAB);
[email protected]420ae012009-04-24 05:16:322828}
2829
[email protected]1ee614862009-07-06 20:29:182830bool TabContents::IsExternalTabContainer() const {
2831 if (!delegate())
2832 return false;
2833
2834 return delegate()->IsExternalTabContainer();
2835}
2836
[email protected]1ee614862009-07-06 20:29:182837void TabContents::DidInsertCSS() {
2838 // This RVHDelegate function is used for extensions and not us.
[email protected]420ae012009-04-24 05:16:322839}
2840
[email protected]130efb02009-09-18 18:54:352841void TabContents::FocusedNodeChanged() {
2842 NotificationService::current()->Notify(
2843 NotificationType::FOCUS_CHANGED_IN_PAGE,
2844 Source<RenderViewHost>(render_view_host()),
2845 NotificationService::NoDetails());
2846}
2847
[email protected]420ae012009-04-24 05:16:322848void TabContents::FileSelected(const FilePath& path,
2849 int index, void* params) {
[email protected]35896a32010-06-09 08:42:512850 profile()->set_last_selected_directory(path.DirName());
[email protected]cdaf8d02010-03-30 19:52:472851 std::vector<FilePath> files;
2852 files.push_back(path);
2853 render_view_host()->FilesSelectedInChooser(files);
[email protected]420ae012009-04-24 05:16:322854}
2855
2856void TabContents::MultiFilesSelected(const std::vector<FilePath>& files,
2857 void* params) {
[email protected]35896a32010-06-09 08:42:512858 if (!files.empty())
2859 profile()->set_last_selected_directory(files[0].DirName());
[email protected]cdaf8d02010-03-30 19:52:472860 render_view_host()->FilesSelectedInChooser(files);
[email protected]420ae012009-04-24 05:16:322861}
2862
2863void TabContents::FileSelectionCanceled(void* params) {
2864 // If the user cancels choosing a file to upload we pass back an
2865 // empty vector.
[email protected]cdaf8d02010-03-30 19:52:472866 render_view_host()->FilesSelectedInChooser(std::vector<FilePath>());
[email protected]420ae012009-04-24 05:16:322867}
2868
2869void TabContents::BeforeUnloadFiredFromRenderManager(
2870 bool proceed,
2871 bool* proceed_to_fire_unload) {
2872 if (delegate())
2873 delegate()->BeforeUnloadFired(this, proceed, proceed_to_fire_unload);
2874}
2875
2876void TabContents::UpdateRenderViewSizeForRenderManager() {
2877 // TODO(brettw) this is a hack. See TabContentsView::SizeContents.
[email protected]0323ee42010-02-17 22:03:262878 gfx::Size size = view_->GetContainerSize();
2879 // 0x0 isn't a valid window size (minimal window size is 1x1) but it may be
2880 // here during container initialization and normal window size will be set
2881 // later. In case of tab duplication this resizing to 0x0 prevents setting
2882 // normal size later so just ignore it.
2883 if (!size.IsEmpty())
2884 view_->SizeContents(size);
[email protected]420ae012009-04-24 05:16:322885}
2886
2887DOMUI* TabContents::CreateDOMUIForRenderManager(const GURL& url) {
[email protected]d82ed61e2009-06-16 02:46:222888 return DOMUIFactory::CreateDOMUIForURL(this, url);
[email protected]420ae012009-04-24 05:16:322889}
2890
2891NavigationEntry*
2892TabContents::GetLastCommittedNavigationEntryForRenderManager() {
2893 return controller_.GetLastCommittedEntry();
2894}
2895
2896bool TabContents::CreateRenderViewForRenderManager(
2897 RenderViewHost* render_view_host) {
[email protected]420ae012009-04-24 05:16:322898 RenderWidgetHostView* rwh_view = view_->CreateViewForWidget(render_view_host);
[email protected]1a98a932009-11-17 00:12:522899
2900 scoped_refptr<URLRequestContextGetter> request_context = request_context_;
2901 if (!request_context.get())
2902 request_context = profile()->GetRequestContext();
2903
[email protected]8ab04652010-06-12 02:47:262904 if (!render_view_host->CreateRenderView(request_context, string16()))
[email protected]420ae012009-04-24 05:16:322905 return false;
2906
2907 // Now that the RenderView has been created, we need to tell it its size.
2908 rwh_view->SetSize(view_->GetContainerSize());
2909
2910 UpdateMaxPageIDIfNecessary(render_view_host->site_instance(),
2911 render_view_host);
2912 return true;
2913}
2914
2915void TabContents::Observe(NotificationType type,
2916 const NotificationSource& source,
2917 const NotificationDetails& details) {
2918 switch (type.value) {
2919 case NotificationType::BOOKMARK_MODEL_LOADED:
2920 // BookmarkModel finished loading, fall through to update starred state.
2921 case NotificationType::URLS_STARRED: {
2922 // Somewhere, a URL has been starred.
2923 // Ignore notifications for profiles other than our current one.
2924 Profile* source_profile = Source<Profile>(source).ptr();
2925 if (!source_profile || !source_profile->IsSameProfile(profile()))
2926 return;
2927
2928 UpdateStarredStateForCurrentURL();
2929 break;
2930 }
2931 case NotificationType::PREF_CHANGED: {
2932 std::wstring* pref_name_in = Details<std::wstring>(details).ptr();
2933 DCHECK(Source<PrefService>(source).ptr() == profile()->GetPrefs());
2934 if (*pref_name_in == prefs::kAlternateErrorPagesEnabled) {
2935 UpdateAlternateErrorPageURL();
2936 } else if (*pref_name_in == prefs::kDefaultCharset ||
2937 StartsWithASCII(WideToUTF8(*pref_name_in), "webkit.webprefs.", true)
2938 ) {
2939 UpdateWebPreferences();
2940 } else {
2941 NOTREACHED() << "unexpected pref change notification" << *pref_name_in;
2942 }
2943 break;
2944 }
2945 case NotificationType::RENDER_WIDGET_HOST_DESTROYED:
2946 view_->RenderWidgetHostDestroyed(Source<RenderWidgetHost>(source).ptr());
2947 break;
2948
2949 case NotificationType::NAV_ENTRY_COMMITTED: {
2950 DCHECK(&controller_ == Source<NavigationController>(source).ptr());
2951
2952 NavigationController::LoadCommittedDetails& committed_details =
2953 *(Details<NavigationController::LoadCommittedDetails>(details).ptr());
2954 ExpireInfoBars(committed_details);
2955 break;
2956 }
2957
[email protected]d3ba77272009-09-03 00:06:092958#if defined(OS_LINUX)
2959 case NotificationType::BROWSER_THEME_CHANGED: {
[email protected]7ff431e2010-01-07 18:18:542960 renderer_preferences_util::UpdateFromSystemSettings(
2961 &renderer_preferences_, profile());
[email protected]d3ba77272009-09-03 00:06:092962 render_view_host()->SyncRendererPrefs();
2963 break;
2964 }
2965#endif
2966
[email protected]6c32ce72010-03-08 05:18:062967 case NotificationType::USER_STYLE_SHEET_UPDATED:
2968 UpdateWebPreferences();
2969 break;
2970
[email protected]79580c62010-02-02 02:36:252971 case NotificationType::CONTENT_SETTINGS_CHANGED: {
[email protected]0314ae02010-04-08 09:18:292972 Details<const HostContentSettingsMap::ContentSettingsDetails>
[email protected]79580c62010-02-02 02:36:252973 settings_details(details);
[email protected]79580c62010-02-02 02:36:252974 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]da0c8e92010-02-09 23:25:492975 GURL entry_url;
[email protected]0314ae02010-04-08 09:18:292976 if (entry)
[email protected]da0c8e92010-02-09 23:25:492977 entry_url = entry->url();
[email protected]79580c62010-02-02 02:36:252978 Source<HostContentSettingsMap> content_settings(source);
[email protected]0314ae02010-04-08 09:18:292979 if (settings_details.ptr()->update_all() ||
2980 settings_details.ptr()->pattern().Matches(entry_url)) {
2981 render_view_host()->SendContentSettings(entry_url,
[email protected]da0c8e92010-02-09 23:25:492982 content_settings.ptr()->GetContentSettings(entry_url));
[email protected]79580c62010-02-02 02:36:252983 }
2984 break;
2985 }
2986
[email protected]e5adff72010-06-14 18:13:062987 case NotificationType::EXTENSION_LOADED:
[email protected]a888b29e62010-04-01 13:38:572988 break;
[email protected]a888b29e62010-04-01 13:38:572989
2990 case NotificationType::EXTENSION_UNLOADED:
[email protected]e5adff72010-06-14 18:13:062991 case NotificationType::EXTENSION_UNLOADED_DISABLED:
[email protected]a888b29e62010-04-01 13:38:572992 break;
[email protected]a888b29e62010-04-01 13:38:572993
[email protected]420ae012009-04-24 05:16:322994 default:
2995 NOTREACHED();
2996 }
2997}
[email protected]aacd7cc2009-09-02 14:51:102998
[email protected]98aa0b52010-05-06 17:03:082999void TabContents::UpdateExtensionAppIcon(Extension* extension) {
3000 extension_app_icon_.reset();
[email protected]f075fe0a22010-04-26 04:06:433001
[email protected]a888b29e62010-04-01 13:38:573002 if (extension) {
[email protected]98aa0b52010-05-06 17:03:083003 extension_app_image_loader_.reset(new ImageLoadingTracker(this));
3004 extension_app_image_loader_->LoadImage(
[email protected]a888b29e62010-04-01 13:38:573005 extension,
3006 extension->GetIconPath(Extension::EXTENSION_ICON_SMALLISH),
3007 gfx::Size(Extension::EXTENSION_ICON_SMALLISH,
3008 Extension::EXTENSION_ICON_SMALLISH),
3009 ImageLoadingTracker::CACHE);
3010 } else {
[email protected]98aa0b52010-05-06 17:03:083011 extension_app_image_loader_.reset(NULL);
[email protected]a888b29e62010-04-01 13:38:573012 }
3013}
3014
3015Extension* TabContents::GetExtensionContaining(const GURL& url) {
3016 ExtensionsService* extensions_service = profile()->GetExtensionsService();
3017 if (!extensions_service)
3018 return NULL;
3019
3020 Extension* extension = extensions_service->GetExtensionByURL(url);
3021 return extension ?
3022 extension : extensions_service->GetExtensionByWebExtent(url);
3023}
3024
[email protected]a888b29e62010-04-01 13:38:573025void TabContents::OnImageLoaded(SkBitmap* image, ExtensionResource resource,
3026 int index) {
3027 if (image) {
[email protected]98aa0b52010-05-06 17:03:083028 extension_app_icon_ = *image;
[email protected]a888b29e62010-04-01 13:38:573029 NotifyNavigationStateChanged(INVALIDATE_TAB);
3030 }
3031}
3032
[email protected]beb440c2009-11-06 04:08:543033std::wstring TabContents::GetMessageBoxTitle(const GURL& frame_url,
3034 bool is_alert) {
3035 if (!frame_url.has_host())
3036 return l10n_util::GetString(
3037 is_alert ? IDS_JAVASCRIPT_ALERT_DEFAULT_TITLE
3038 : IDS_JAVASCRIPT_MESSAGEBOX_DEFAULT_TITLE);
3039
3040 // We really only want the scheme, hostname, and port.
3041 GURL::Replacements replacements;
3042 replacements.ClearUsername();
3043 replacements.ClearPassword();
3044 replacements.ClearPath();
3045 replacements.ClearQuery();
3046 replacements.ClearRef();
3047 GURL clean_url = frame_url.ReplaceComponents(replacements);
3048
3049 // TODO(brettw) it should be easier than this to do the correct language
3050 // handling without getting the accept language from the profile.
3051 std::wstring base_address = gfx::ElideUrl(clean_url, gfx::Font(), 0,
3052 profile()->GetPrefs()->GetString(prefs::kAcceptLanguages));
3053 // Force URL to have LTR directionality.
[email protected]4a4afe32010-05-05 01:39:563054 base::i18n::GetDisplayStringInLTRDirectionality(&base_address);
[email protected]beb440c2009-11-06 04:08:543055
3056 return l10n_util::GetStringF(
3057 is_alert ? IDS_JAVASCRIPT_ALERT_TITLE : IDS_JAVASCRIPT_MESSAGEBOX_TITLE,
3058 base_address);
3059}
3060
3061gfx::NativeWindow TabContents::GetMessageBoxRootWindow() {
3062 return view_->GetTopLevelNativeWindow();
3063}
3064
3065void TabContents::OnMessageBoxClosed(IPC::Message* reply_msg,
3066 bool success,
3067 const std::wstring& prompt) {
3068 last_javascript_message_dismissal_ = base::TimeTicks::Now();
3069 if (is_showing_before_unload_dialog_ && !success) {
3070 // If a beforeunload dialog is canceled, we need to stop the throbber from
3071 // spinning, since we forced it to start spinning in Navigate.
3072 DidStopLoading();
3073
3074 tab_close_start_time_ = base::TimeTicks();
3075 }
3076 is_showing_before_unload_dialog_ = false;
3077 render_view_host()->JavaScriptMessageBoxClosed(reply_msg, success, prompt);
3078}
3079
3080void TabContents::SetSuppressMessageBoxes(bool suppress_message_boxes) {
3081 set_suppress_javascript_messages(suppress_message_boxes);
3082}
3083
[email protected]41fc0322009-09-04 22:23:403084void TabContents::set_encoding(const std::string& encoding) {
[email protected]aacd7cc2009-09-02 14:51:103085 encoding_ = CharacterEncoding::GetCanonicalEncodingNameByAliasName(encoding);
3086}
[email protected]f45d2a72010-03-08 23:28:353087
3088void TabContents::SetAppIcon(const SkBitmap& app_icon) {
3089 app_icon_ = app_icon;
3090 NotifyNavigationStateChanged(INVALIDATE_TITLE);
3091}
[email protected]ea68c06a2010-04-23 17:25:373092
3093// After a successful *new* login attempt, we take the PasswordFormManager in
3094// provisional_save_manager_ and move it to a SavePasswordInfoBarDelegate while
3095// the user makes up their mind with the "save password" infobar. Note if the
3096// login is one we already know about, the end of the line is
3097// provisional_save_manager_ because we just update it on success and so such
3098// forms never end up in an infobar.
3099class SavePasswordInfoBarDelegate : public ConfirmInfoBarDelegate {
3100 public:
3101 SavePasswordInfoBarDelegate(TabContents* tab_contents,
[email protected]12bb6822010-06-16 02:00:463102 PasswordFormManager* form_to_save)
3103 : ConfirmInfoBarDelegate(tab_contents),
3104 form_to_save_(form_to_save),
3105 infobar_response_(kNoResponse) {
[email protected]ea68c06a2010-04-23 17:25:373106 }
3107
3108 virtual ~SavePasswordInfoBarDelegate() { }
3109
3110 // Overridden from ConfirmInfoBarDelegate:
3111 virtual void InfoBarClosed() {
[email protected]12bb6822010-06-16 02:00:463112 UMA_HISTOGRAM_ENUMERATION("PasswordManager.InfoBarResponse",
3113 infobar_response_, kNumResponseTypes);
[email protected]ea68c06a2010-04-23 17:25:373114 delete this;
3115 }
3116
3117 virtual std::wstring GetMessageText() const {
3118 return l10n_util::GetString(IDS_PASSWORD_MANAGER_SAVE_PASSWORD_PROMPT);
3119 }
3120
3121 virtual SkBitmap* GetIcon() const {
3122 return ResourceBundle::GetSharedInstance().GetBitmapNamed(
3123 IDR_INFOBAR_SAVE_PASSWORD);
3124 }
3125
3126 virtual int GetButtons() const {
3127 return BUTTON_OK | BUTTON_CANCEL;
3128 }
3129
3130 virtual std::wstring GetButtonLabel(InfoBarButton button) const {
3131 if (button == BUTTON_OK)
3132 return l10n_util::GetString(IDS_PASSWORD_MANAGER_SAVE_BUTTON);
3133 if (button == BUTTON_CANCEL)
3134 return l10n_util::GetString(IDS_PASSWORD_MANAGER_BLACKLIST_BUTTON);
3135 NOTREACHED();
3136 return std::wstring();
3137 }
3138
3139 virtual bool Accept() {
3140 DCHECK(form_to_save_.get());
3141 form_to_save_->Save();
[email protected]12bb6822010-06-16 02:00:463142 infobar_response_ = kRememberPassword;
[email protected]ea68c06a2010-04-23 17:25:373143 return true;
3144 }
3145
3146 virtual bool Cancel() {
3147 DCHECK(form_to_save_.get());
3148 form_to_save_->PermanentlyBlacklist();
[email protected]12bb6822010-06-16 02:00:463149 infobar_response_ = kDontRememberPassword;
[email protected]ea68c06a2010-04-23 17:25:373150 return true;
3151 }
3152
3153 private:
3154 // The PasswordFormManager managing the form we're asking the user about,
3155 // and should update as per her decision.
3156 scoped_ptr<PasswordFormManager> form_to_save_;
3157
[email protected]12bb6822010-06-16 02:00:463158 // Used to track the results we get from the info bar.
3159 enum ResponseType {
3160 kNoResponse = 0,
3161 kRememberPassword,
3162 kDontRememberPassword,
3163 kNumResponseTypes,
3164 };
3165 ResponseType infobar_response_;
3166
[email protected]ea68c06a2010-04-23 17:25:373167 DISALLOW_COPY_AND_ASSIGN(SavePasswordInfoBarDelegate);
3168};
3169
3170void TabContents::FillPasswordForm(
3171 const webkit_glue::PasswordFormDomManager::FillData& form_data) {
3172 render_view_host()->FillPasswordForm(form_data);
3173}
3174
3175void TabContents::AddSavePasswordInfoBar(PasswordFormManager* form_to_save) {
3176 AddInfoBar(new SavePasswordInfoBarDelegate(this, form_to_save));
3177}
3178
3179Profile* TabContents::GetProfileForPasswordManager() {
3180 return profile();
3181}
3182
3183bool TabContents::DidLastPageLoadEncounterSSLErrors() {
3184 return controller().ssl_manager()->ProcessedSSLErrorFromRequest();
3185}