blob: f4df639fb5c1b8c39ff2a296d20b16d63f8085fb [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]96d185d2009-04-24 03:28:5426#include "chrome/browser/browser_process.h"
[email protected]cf68b092010-01-15 20:05:5527#include "chrome/browser/browser_shutdown.h"
initial.commit09911bf2008-07-26 23:55:2928#include "chrome/browser/cert_store.h"
[email protected]aacd7cc2009-09-02 14:51:1029#include "chrome/browser/character_encoding.h"
[email protected]96d185d2009-04-24 03:28:5430#include "chrome/browser/debugger/devtools_manager.h"
[email protected]420ae012009-04-24 05:16:3231#include "chrome/browser/dom_operation_notification_details.h"
[email protected]96d185d2009-04-24 03:28:5432#include "chrome/browser/dom_ui/dom_ui.h"
33#include "chrome/browser/dom_ui/dom_ui_factory.h"
[email protected]bcef0dc02009-02-28 00:35:0234#include "chrome/browser/download/download_item_model.h"
[email protected]420ae012009-04-24 05:16:3235#include "chrome/browser/download/download_manager.h"
36#include "chrome/browser/download/download_request_manager.h"
[email protected]e7eaedde2009-09-25 20:09:4937#include "chrome/browser/external_protocol_handler.h"
[email protected]92c6f9b92009-10-24 04:35:0838#include "chrome/browser/extensions/extensions_service.h"
[email protected]0189bc722009-08-28 21:56:4839#include "chrome/browser/favicon_service.h"
[email protected]e5d2f7132010-04-09 18:13:5340#include "chrome/browser/find_bar_state.h"
[email protected]9d062b12010-04-14 09:36:5541#include "chrome/browser/geolocation/geolocation_content_settings_map.h"
[email protected]420ae012009-04-24 05:16:3242#include "chrome/browser/google_util.h"
[email protected]79580c62010-02-02 02:36:2543#include "chrome/browser/host_content_settings_map.h"
[email protected]8897c382009-05-06 17:53:2644#include "chrome/browser/hung_renderer_dialog.h"
[email protected]12f74a92010-02-05 22:32:1445#include "chrome/browser/message_box_handler.h"
[email protected]420ae012009-04-24 05:16:3246#include "chrome/browser/load_from_memory_cache_details.h"
47#include "chrome/browser/load_notification_details.h"
[email protected]84abba62009-10-07 17:01:4448#include "chrome/browser/metrics/metric_event_duration_details.h"
[email protected]ea0e80892009-08-22 00:36:0549#include "chrome/browser/modal_html_dialog_delegate.h"
[email protected]c8865482009-07-23 20:40:1050#include "chrome/browser/omnibox_search_hint.h"
[email protected]fdd61c62009-04-22 19:22:5751#include "chrome/browser/password_manager/password_manager.h"
[email protected]14a000d2010-04-29 21:44:2452#include "chrome/browser/platform_util.h"
[email protected]fdd61c62009-04-22 19:22:5753#include "chrome/browser/plugin_installer.h"
[email protected]052313b2010-02-19 09:43:0854#include "chrome/browser/pref_service.h"
[email protected]ce560f82009-06-03 09:39:4455#include "chrome/browser/profile.h"
[email protected]1db6ff152009-10-12 15:32:0756#include "chrome/browser/renderer_host/render_process_host.h"
[email protected]8cb5d5b2010-02-09 11:36:1657#include "chrome/browser/renderer_host/render_view_host.h"
[email protected]96d185d2009-04-24 03:28:5458#include "chrome/browser/renderer_host/render_widget_host_view.h"
[email protected]fd694982009-07-03 17:32:3759#include "chrome/browser/renderer_host/resource_request_details.h"
[email protected]1db6ff152009-10-12 15:32:0760#include "chrome/browser/renderer_host/site_instance.h"
[email protected]96d185d2009-04-24 03:28:5461#include "chrome/browser/renderer_host/web_cache_manager.h"
[email protected]93623c5d2009-12-10 21:40:3262#include "chrome/browser/renderer_preferences_util.h"
[email protected]152b8ba32010-03-29 19:40:1663#include "chrome/browser/search_engines/template_url_fetcher.h"
64#include "chrome/browser/search_engines/template_url_model.h"
[email protected]c37e3b62010-01-22 00:15:1765#include "chrome/browser/sessions/session_types.h"
[email protected]bd1ad682009-05-15 22:19:1766#include "chrome/browser/tab_contents/infobar_delegate.h"
[email protected]7e383692009-06-12 19:14:5467#include "chrome/browser/tab_contents/interstitial_page.h"
[email protected]f3ec7742009-01-15 00:59:1668#include "chrome/browser/tab_contents/navigation_entry.h"
[email protected]de34a962009-06-26 23:22:4369#include "chrome/browser/tab_contents/provisional_load_details.h"
[email protected]f3ec7742009-01-15 00:59:1670#include "chrome/browser/tab_contents/tab_contents_delegate.h"
[email protected]fdd61c62009-04-22 19:22:5771#include "chrome/browser/tab_contents/tab_contents_view.h"
[email protected]58dca552009-06-17 00:35:0272#include "chrome/browser/tab_contents/thumbnail_generator.h"
[email protected]449478302009-06-09 20:04:2873#include "chrome/browser/thumbnail_store.h"
[email protected]152b8ba32010-03-29 19:40:1674#include "chrome/browser/translate/page_translated_details.h"
[email protected]54ec6472010-04-09 19:39:5875#include "chrome/common/bindings_policy.h"
[email protected]35f7d212009-04-29 21:19:2776#include "chrome/common/chrome_switches.h"
[email protected]3b355642010-02-05 16:01:4977#include "chrome/common/extensions/extension.h"
[email protected]5d246db22009-10-27 06:17:5778#include "chrome/common/extensions/extension_action.h"
[email protected]bfd04a62009-02-01 18:16:5679#include "chrome/common/notification_service.h"
initial.commit09911bf2008-07-26 23:55:2980#include "chrome/common/pref_names.h"
[email protected]420ae012009-04-24 05:16:3281#include "chrome/common/render_messages.h"
[email protected]1ee614862009-07-06 20:29:1882#include "chrome/common/renderer_preferences.h"
[email protected]96d185d2009-04-24 03:28:5483#include "chrome/common/url_constants.h"
[email protected]ea68c06a2010-04-23 17:25:3784#include "grit/chromium_strings.h"
[email protected]8897c382009-05-06 17:53:2685#include "grit/generated_resources.h"
[email protected]420ae012009-04-24 05:16:3286#include "grit/locale_settings.h"
[email protected]fd626782010-01-22 05:26:0687#include "grit/platform_locale_settings.h"
[email protected]ea68c06a2010-04-23 17:25:3788#include "grit/theme_resources.h"
[email protected]420ae012009-04-24 05:16:3289#include "net/base/mime_util.h"
90#include "net/base/net_errors.h"
[email protected]d686e812009-06-03 19:10:2991#include "net/base/net_util.h"
[email protected]420ae012009-04-24 05:16:3292#include "net/base/registry_controlled_domain.h"
[email protected]1ee614862009-07-06 20:29:1893#include "webkit/glue/password_form.h"
94#include "webkit/glue/webpreferences.h"
[email protected]3e45ba92009-02-20 21:09:0095
[email protected]420ae012009-04-24 05:16:3296// Cross-Site Navigations
97//
98// If a TabContents is told to navigate to a different web site (as determined
99// by SiteInstance), it will replace its current RenderViewHost with a new
100// RenderViewHost dedicated to the new SiteInstance. This works as follows:
101//
102// - Navigate determines whether the destination is cross-site, and if so,
103// it creates a pending_render_view_host_ and moves into the PENDING
104// RendererState.
105// - The pending RVH is "suspended," so that no navigation messages are sent to
106// its renderer until the onbeforeunload JavaScript handler has a chance to
107// run in the current RVH.
108// - The pending RVH tells CrossSiteRequestManager (a thread-safe singleton)
109// that it has a pending cross-site request. ResourceDispatcherHost will
110// check for this when the response arrives.
111// - The current RVH runs its onbeforeunload handler. If it returns false, we
112// cancel all the pending logic and go back to NORMAL. Otherwise we allow
113// the pending RVH to send the navigation request to its renderer.
114// - ResourceDispatcherHost receives a ResourceRequest on the IO thread. It
115// checks CrossSiteRequestManager to see that the RVH responsible has a
116// pending cross-site request, and then installs a CrossSiteEventHandler.
117// - When RDH receives a response, the BufferedEventHandler determines whether
118// it is a download. If so, it sends a message to the new renderer causing
119// it to cancel the request, and the download proceeds in the download
120// thread. For now, we stay in a PENDING state (with a pending RVH) until
[email protected]57c6a652009-05-04 07:58:34121// the next DidNavigate event for this TabContents. This isn't ideal, but it
[email protected]420ae012009-04-24 05:16:32122// doesn't affect any functionality.
123// - After RDH receives a response and determines that it is safe and not a
124// download, it pauses the response to first run the old page's onunload
125// handler. It does this by asynchronously calling the OnCrossSiteResponse
[email protected]57c6a652009-05-04 07:58:34126// method of TabContents on the UI thread, which sends a ClosePage message
[email protected]420ae012009-04-24 05:16:32127// to the current RVH.
128// - Once the onunload handler is finished, a ClosePage_ACK message is sent to
129// the ResourceDispatcherHost, who unpauses the response. Data is then sent
130// to the pending RVH.
131// - The pending renderer sends a FrameNavigate message that invokes the
132// DidNavigate method. This replaces the current RVH with the
133// pending RVH and goes back to the NORMAL RendererState.
134
135namespace {
136
137// Amount of time we wait between when a key event is received and the renderer
138// is queried for its state and pushed to the NavigationEntry.
139const int kQueryStateDelay = 5000;
140
141const int kSyncWaitDelay = 40;
142
[email protected]304f8792009-08-19 21:28:09143#if defined(OS_CHROMEOS)
144// If a popup window is bigger than this fraction of the screen on chrome os,
145// turn it into a tab
146const float kMaxWidthFactor = 0.5;
147const float kMaxHeightFactor = 0.6;
148#endif
149
[email protected]420ae012009-04-24 05:16:32150// If another javascript message box is displayed within
151// kJavascriptMessageExpectedDelay of a previous javascript message box being
152// dismissed, display an option to suppress future message boxes from this
153// contents.
154const int kJavascriptMessageExpectedDelay = 1000;
155
[email protected]420ae012009-04-24 05:16:32156// The list of prefs we want to observe.
157const wchar_t* kPrefsToObserve[] = {
158 prefs::kAlternateErrorPagesEnabled,
159 prefs::kWebKitJavaEnabled,
160 prefs::kWebKitJavascriptEnabled,
161 prefs::kWebKitLoadsImagesAutomatically,
162 prefs::kWebKitPluginsEnabled,
163 prefs::kWebKitUsesUniversalDetector,
164 prefs::kWebKitSerifFontFamily,
165 prefs::kWebKitSansSerifFontFamily,
166 prefs::kWebKitFixedFontFamily,
167 prefs::kWebKitDefaultFontSize,
168 prefs::kWebKitDefaultFixedFontSize,
169 prefs::kDefaultCharset
170 // kWebKitStandardFontIsSerif needs to be added
171 // if we let users pick which font to use, serif or sans-serif when
172 // no font is specified or a CSS generic family (serif or sans-serif)
173 // is not specified.
174};
175
176const int kPrefsToObserveLength = arraysize(kPrefsToObserve);
177
[email protected]420ae012009-04-24 05:16:32178// Returns true if the entry's transition type is FORM_SUBMIT.
179bool IsFormSubmit(const NavigationEntry* entry) {
180 return (PageTransition::StripQualifier(entry->transition_type()) ==
181 PageTransition::FORM_SUBMIT);
182}
183
184#if defined(OS_WIN)
185
186BOOL CALLBACK InvalidateWindow(HWND hwnd, LPARAM lparam) {
187 // Note: erase is required to properly paint some widgets borders. This can
188 // be seen with textfields.
189 InvalidateRect(hwnd, NULL, TRUE);
190 return TRUE;
191}
192#endif
193
[email protected]5e369672009-11-03 23:48:30194ViewMsg_Navigate_Params::NavigationType GetNavigationType(
[email protected]1ccb3568d2010-02-19 10:51:16195 Profile* profile, const NavigationEntry& entry,
196 NavigationController::ReloadType reload_type) {
197 switch (reload_type) {
198 case NavigationController::RELOAD:
199 return ViewMsg_Navigate_Params::RELOAD;
200 case NavigationController::RELOAD_IGNORING_CACHE:
201 return ViewMsg_Navigate_Params::RELOAD_IGNORING_CACHE;
202 case NavigationController::NO_RELOAD:
203 break; // Fall through to rest of function.
204 }
[email protected]5e369672009-11-03 23:48:30205
206 if (entry.restore_type() == NavigationEntry::RESTORE_LAST_SESSION &&
207 profile->DidLastSessionExitCleanly())
208 return ViewMsg_Navigate_Params::RESTORE;
209
210 return ViewMsg_Navigate_Params::NORMAL;
211}
212
[email protected]3cc72b12010-03-18 23:03:00213void MakeNavigateParams(const NavigationController& controller,
[email protected]1ccb3568d2010-02-19 10:51:16214 NavigationController::ReloadType reload_type,
215 ViewMsg_Navigate_Params* params) {
[email protected]3cc72b12010-03-18 23:03:00216 const NavigationEntry& entry = *controller.pending_entry();
[email protected]056de2d2009-06-26 16:41:34217 params->page_id = entry.page_id();
[email protected]3cc72b12010-03-18 23:03:00218 params->pending_history_list_offset = controller.pending_entry_index();
219 params->current_history_list_offset = controller.last_committed_entry_index();
220 params->current_history_list_length = controller.entry_count();
[email protected]056de2d2009-06-26 16:41:34221 params->url = entry.url();
222 params->referrer = entry.referrer();
223 params->transition = entry.transition_type();
224 params->state = entry.content_state();
[email protected]3cc72b12010-03-18 23:03:00225 params->navigation_type =
226 GetNavigationType(controller.profile(), entry, reload_type);
[email protected]056de2d2009-06-26 16:41:34227 params->request_time = base::Time::Now();
228}
229
[email protected]420ae012009-04-24 05:16:32230} // namespace
231
232// -----------------------------------------------------------------------------
233
[email protected]fdd61c62009-04-22 19:22:57234// static
235int TabContents::find_request_id_counter_ = -1;
236
[email protected]420ae012009-04-24 05:16:32237
[email protected]420ae012009-04-24 05:16:32238TabContents::TabContents(Profile* profile,
239 SiteInstance* site_instance,
240 int routing_id,
[email protected]34ac70502009-09-25 17:07:23241 const TabContents* base_tab_contents)
[email protected]b680ad22009-04-15 23:19:42242 : delegate_(NULL),
[email protected]66ba4932009-06-04 19:22:13243 ALLOW_THIS_IN_INITIALIZER_LIST(controller_(this, profile)),
244 ALLOW_THIS_IN_INITIALIZER_LIST(view_(
[email protected]d82ed61e2009-06-16 02:46:22245 TabContentsView::Create(this))),
246 ALLOW_THIS_IN_INITIALIZER_LIST(render_manager_(this, this)),
[email protected]fdd61c62009-04-22 19:22:57247 property_bag_(),
248 registrar_(),
[email protected]d82ed61e2009-06-16 02:46:22249 ALLOW_THIS_IN_INITIALIZER_LIST(printing_(*this)),
[email protected]fdd61c62009-04-22 19:22:57250 save_package_(),
[email protected]6db0def92010-03-02 19:55:45251 autocomplete_history_manager_(),
[email protected]21674df2009-10-28 19:51:21252 autofill_manager_(),
[email protected]fdd61c62009-04-22 19:22:57253 password_manager_(),
254 plugin_installer_(),
[email protected]6f821dd2010-02-23 00:54:06255 bookmark_drag_(NULL),
[email protected]d82ed61e2009-06-16 02:46:22256 ALLOW_THIS_IN_INITIALIZER_LIST(fav_icon_helper_(this)),
[email protected]fdd61c62009-04-22 19:22:57257 select_file_dialog_(),
[email protected]d5f942ba2008-09-26 19:30:34258 is_loading_(false),
initial.commit09911bf2008-07-26 23:55:29259 is_crashed_(false),
[email protected]d5f942ba2008-09-26 19:30:34260 waiting_for_response_(false),
[email protected]d5f942ba2008-09-26 19:30:34261 max_page_id_(-1),
[email protected]fdd61c62009-04-22 19:22:57262 current_load_start_(),
263 load_state_(net::LOAD_STATE_IDLE),
264 load_state_host_(),
[email protected]094e5b22009-09-25 04:23:56265 upload_size_(0),
266 upload_position_(0),
[email protected]fdd61c62009-04-22 19:22:57267 received_page_title_(false),
268 is_starred_(false),
269 contents_mime_type_(),
270 encoding_(),
[email protected]332af7732009-03-11 13:21:35271 blocked_popups_(NULL),
[email protected]8cc8d492010-02-02 10:40:49272 dont_notify_render_view_(false),
[email protected]fdd61c62009-04-22 19:22:57273 infobar_delegates_(),
[email protected]fdd61c62009-04-22 19:22:57274 find_ui_active_(false),
275 find_op_aborted_(false),
276 current_find_request_id_(find_request_id_counter_++),
[email protected]e491f1c2009-05-22 20:28:12277 last_search_case_sensitive_(false),
[email protected]e491f1c2009-05-22 20:28:12278 last_search_result_(),
[email protected]98aa0b52010-05-06 17:03:08279 extension_app_(NULL),
280 extension_app_for_current_page_(NULL),
[email protected]fdd61c62009-04-22 19:22:57281 capturing_contents_(false),
282 is_being_destroyed_(false),
283 notify_disconnection_(false),
284 history_requests_(),
285#if defined(OS_WIN)
286 message_box_active_(CreateEvent(NULL, TRUE, FALSE, NULL)),
287#endif
288 last_javascript_message_dismissal_(),
[email protected]c2e74fe82009-09-03 17:57:44289 suppress_javascript_messages_(false),
[email protected]7ab1e7d62009-10-14 23:32:01290 is_showing_before_unload_dialog_(false),
[email protected]7ff431e2010-01-07 18:18:54291 renderer_preferences_(),
[email protected]1a242c32010-02-02 21:20:54292 opener_dom_ui_type_(DOMUIFactory::kNoDOMUI),
[email protected]70102972010-05-06 14:49:40293 language_state_(&controller_),
294 requested_accessibility_tree_(false) {
[email protected]0de80162010-02-03 04:52:35295 ClearBlockedContentSettings();
[email protected]7ff431e2010-01-07 18:18:54296 renderer_preferences_util::UpdateFromSystemSettings(
297 &renderer_preferences_, profile);
298
[email protected]16779842009-07-08 23:45:29299#if defined(OS_CHROMEOS)
[email protected]58dca552009-06-17 00:35:02300 // Make sure the thumbnailer is started before starting the render manager.
301 // The thumbnailer will want to listen for RVH creations, one of which will
302 // happen in RVHManager::Init.
303 ThumbnailGenerator* generator = g_browser_process->GetThumbnailGenerator();
304 if (generator)
305 generator->StartThumbnailing();
306#endif
307
[email protected]12636df2009-09-28 22:32:21308 render_manager_.Init(profile, site_instance, routing_id);
[email protected]420ae012009-04-24 05:16:32309
[email protected]34ac70502009-09-25 17:07:23310 // We have the initial size of the view be based on the size of the passed in
311 // tab contents (normally a tab from the same window).
312 view_->CreateView(base_tab_contents ?
313 base_tab_contents->view()->GetContainerSize() : gfx::Size());
[email protected]420ae012009-04-24 05:16:32314
315 // Register for notifications about all interested prefs change.
316 PrefService* prefs = profile->GetPrefs();
317 if (prefs) {
318 for (int i = 0; i < kPrefsToObserveLength; ++i)
319 prefs->AddPrefObserver(kPrefsToObserve[i], this);
320 }
321
322 // Register for notifications about URL starredness changing on any profile.
323 registrar_.Add(this, NotificationType::URLS_STARRED,
324 NotificationService::AllSources());
325 registrar_.Add(this, NotificationType::BOOKMARK_MODEL_LOADED,
326 NotificationService::AllSources());
327 registrar_.Add(this, NotificationType::RENDER_WIDGET_HOST_DESTROYED,
328 NotificationService::AllSources());
[email protected]d3ba77272009-09-03 00:06:09329#if defined(OS_LINUX)
330 registrar_.Add(this, NotificationType::BROWSER_THEME_CHANGED,
331 NotificationService::AllSources());
332#endif
[email protected]420ae012009-04-24 05:16:32333
[email protected]6c32ce72010-03-08 05:18:06334 registrar_.Add(this, NotificationType::USER_STYLE_SHEET_UPDATED,
335 NotificationService::AllSources());
336
[email protected]79580c62010-02-02 02:36:25337 // Register for notifications about content setting changes.
338 registrar_.Add(this, NotificationType::CONTENT_SETTINGS_CHANGED,
339 NotificationService::AllSources());
340
[email protected]a888b29e62010-04-01 13:38:57341 // Listen for extension changes so we can update extension_for_current_page_.
342 registrar_.Add(this, NotificationType::EXTENSION_LOADED,
343 NotificationService::AllSources());
344 registrar_.Add(this, NotificationType::EXTENSION_UNLOADED,
345 NotificationService::AllSources());
346 registrar_.Add(this, NotificationType::EXTENSION_UNLOADED_DISABLED,
347 NotificationService::AllSources());
348
[email protected]c8865482009-07-23 20:40:10349 // Set-up the showing of the omnibox search infobar if applicable.
350 if (OmniboxSearchHint::IsEnabled(profile))
351 omnibox_search_hint_.reset(new OmniboxSearchHint(this));
[email protected]70102972010-05-06 14:49:40352
353 renderer_accessible_ =
354 CommandLine::ForCurrentProcess()->HasSwitch(
355 switches::kEnableRendererAccessibility);
[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]0df30122009-06-03 12:13:08437 prefs->RegisterStringPref(prefs::kWebKitInspectorSettings,
[email protected]8922e1f2009-10-03 05:01:26438 UTF8ToWide(pref_defaults.inspector_settings));
[email protected]420ae012009-04-24 05:16:32439 prefs->RegisterBooleanPref(prefs::kWebKitTextAreasAreResizable,
440 pref_defaults.text_areas_are_resizable);
441 prefs->RegisterBooleanPref(prefs::kWebKitJavaEnabled,
442 pref_defaults.java_enabled);
443
444 prefs->RegisterLocalizedStringPref(prefs::kAcceptLanguages,
445 IDS_ACCEPT_LANGUAGES);
446 prefs->RegisterLocalizedStringPref(prefs::kDefaultCharset,
447 IDS_DEFAULT_ENCODING);
448 prefs->RegisterLocalizedBooleanPref(prefs::kWebKitStandardFontIsSerif,
449 IDS_STANDARD_FONT_IS_SERIF);
450 prefs->RegisterLocalizedStringPref(prefs::kWebKitFixedFontFamily,
451 IDS_FIXED_FONT_FAMILY);
452 prefs->RegisterLocalizedStringPref(prefs::kWebKitSerifFontFamily,
453 IDS_SERIF_FONT_FAMILY);
454 prefs->RegisterLocalizedStringPref(prefs::kWebKitSansSerifFontFamily,
455 IDS_SANS_SERIF_FONT_FAMILY);
456 prefs->RegisterLocalizedStringPref(prefs::kWebKitCursiveFontFamily,
457 IDS_CURSIVE_FONT_FAMILY);
458 prefs->RegisterLocalizedStringPref(prefs::kWebKitFantasyFontFamily,
459 IDS_FANTASY_FONT_FAMILY);
460 prefs->RegisterLocalizedIntegerPref(prefs::kWebKitDefaultFontSize,
461 IDS_DEFAULT_FONT_SIZE);
462 prefs->RegisterLocalizedIntegerPref(prefs::kWebKitDefaultFixedFontSize,
463 IDS_DEFAULT_FIXED_FONT_SIZE);
464 prefs->RegisterLocalizedIntegerPref(prefs::kWebKitMinimumFontSize,
465 IDS_MINIMUM_FONT_SIZE);
466 prefs->RegisterLocalizedIntegerPref(prefs::kWebKitMinimumLogicalFontSize,
467 IDS_MINIMUM_LOGICAL_FONT_SIZE);
468 prefs->RegisterLocalizedBooleanPref(prefs::kWebKitUsesUniversalDetector,
469 IDS_USES_UNIVERSAL_DETECTOR);
470 prefs->RegisterLocalizedStringPref(prefs::kStaticEncodings,
471 IDS_STATIC_ENCODING_LIST);
initial.commit09911bf2008-07-26 23:55:29472}
473
[email protected]13367f72009-07-08 16:39:02474// Returns true if contains content rendered by an extension.
475bool TabContents::HostsExtension() const {
476 return GetURL().SchemeIs(chrome::kExtensionScheme);
[email protected]d5f942ba2008-09-26 19:30:34477}
478
[email protected]cf28ec52010-04-09 22:56:34479AutoFillManager* TabContents::GetAutoFillManager() {
480 if (autofill_manager_.get() == NULL)
481 autofill_manager_.reset(new AutoFillManager(this));
482 return autofill_manager_.get();
483}
484
[email protected]6e95706d2009-04-23 22:55:01485PasswordManager* TabContents::GetPasswordManager() {
486 if (password_manager_.get() == NULL)
487 password_manager_.reset(new PasswordManager(this));
488 return password_manager_.get();
489}
490
491PluginInstaller* TabContents::GetPluginInstaller() {
492 if (plugin_installer_.get() == NULL)
493 plugin_installer_.reset(new PluginInstaller(this));
494 return plugin_installer_.get();
495}
496
[email protected]8cb5d5b2010-02-09 11:36:16497RenderProcessHost* TabContents::GetRenderProcessHost() const {
498 return render_manager_.current_host()->process();
499}
500
[email protected]98aa0b52010-05-06 17:03:08501void TabContents::SetExtensionApp(Extension* extension) {
[email protected]867a73e12010-03-19 20:45:46502 DCHECK(!extension || extension->GetFullLaunchURL().is_valid());
[email protected]98aa0b52010-05-06 17:03:08503 extension_app_ = extension;
[email protected]fca656c2010-02-10 20:30:10504
[email protected]98aa0b52010-05-06 17:03:08505 UpdateExtensionAppIcon(extension_app_);
[email protected]a888b29e62010-04-01 13:38:57506
[email protected]fca656c2010-02-10 20:30:10507 NotificationService::current()->Notify(
508 NotificationType::TAB_CONTENTS_APPLICATION_EXTENSION_CHANGED,
509 Source<TabContents>(this),
510 NotificationService::NoDetails());
[email protected]3b355642010-02-05 16:01:49511}
512
[email protected]98aa0b52010-05-06 17:03:08513void TabContents::SetExtensionAppById(const std::string& extension_app_id) {
514 if (extension_app_id.empty())
[email protected]911f8152010-03-18 16:46:40515 return;
516
517 ExtensionsService* extension_service = profile()->GetExtensionsService();
518 if (extension_service && extension_service->is_ready()) {
519 Extension* extension =
[email protected]98aa0b52010-05-06 17:03:08520 extension_service->GetExtensionById(extension_app_id, false);
[email protected]911f8152010-03-18 16:46:40521 if (extension)
[email protected]98aa0b52010-05-06 17:03:08522 SetExtensionApp(extension);
[email protected]911f8152010-03-18 16:46:40523 }
524}
525
[email protected]98aa0b52010-05-06 17:03:08526SkBitmap* TabContents::GetExtensionAppIcon() {
[email protected]8eb043b2010-04-30 07:19:51527 // We don't show the big icons in tabs for TYPE_EXTENSION_APP windows because
528 // for those windows, we already have a big icon in the top-left outside any
529 // tab. Having big tab icons too looks kinda redonk.
[email protected]06821f42d2010-05-03 22:34:35530 Browser* browser = delegate_ ? delegate_->GetBrowser() : NULL;
531 if (browser && browser->type() == Browser::TYPE_EXTENSION_APP)
[email protected]8eb043b2010-04-30 07:19:51532 return NULL;
[email protected]8eb043b2010-04-30 07:19:51533
[email protected]98aa0b52010-05-06 17:03:08534 if (extension_app_icon_.empty())
[email protected]8eb043b2010-04-30 07:19:51535 return NULL;
536
[email protected]98aa0b52010-05-06 17:03:08537 return &extension_app_icon_;
[email protected]8eb043b2010-04-30 07:19:51538}
539
[email protected]d5f942ba2008-09-26 19:30:34540const GURL& TabContents::GetURL() const {
541 // We may not have a navigation entry yet
[email protected]ce3fa3c2009-04-20 19:55:57542 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]ebe89e062009-08-13 23:16:54543 return entry ? entry->virtual_url() : GURL::EmptyGURL();
[email protected]d5f942ba2008-09-26 19:30:34544}
545
[email protected]96d185d2009-04-24 03:28:54546const string16& TabContents::GetTitle() const {
[email protected]4c6092c5b2009-06-06 00:23:55547 // Transient entries take precedence. They are used for interstitial pages
548 // that are shown on top of existing pages.
549 NavigationEntry* entry = controller_.GetTransientEntry();
550 if (entry)
551 return entry->GetTitleForDisplay(&controller_);
552
[email protected]96d185d2009-04-24 03:28:54553 DOMUI* our_dom_ui = render_manager_.pending_dom_ui() ?
554 render_manager_.pending_dom_ui() : render_manager_.dom_ui();
555 if (our_dom_ui) {
556 // Don't override the title in view source mode.
[email protected]4c6092c5b2009-06-06 00:23:55557 entry = controller_.GetActiveEntry();
[email protected]96d185d2009-04-24 03:28:54558 if (!(entry && entry->IsViewSourceMode())) {
559 // Give the DOM UI the chance to override our title.
560 const string16& title = our_dom_ui->overridden_title();
561 if (!title.empty())
562 return title;
563 }
564 }
565
566 // We use the title for the last committed entry rather than a pending
567 // navigation entry. For example, when the user types in a URL, we want to
568 // keep the old page's title until the new load has committed and we get a new
569 // title.
[email protected]96d185d2009-04-24 03:28:54570 entry = controller_.GetLastCommittedEntry();
571 if (entry)
572 return entry->GetTitleForDisplay(&controller_);
[email protected]96d185d2009-04-24 03:28:54573 return EmptyString16();
574}
575
[email protected]bbbc1ef2010-02-12 18:03:41576// static
577string16 TabContents::GetDefaultTitle() {
578 return l10n_util::GetStringUTF16(IDS_DEFAULT_TAB_TITLE);
579}
580
[email protected]d5f942ba2008-09-26 19:30:34581int32 TabContents::GetMaxPageID() {
582 if (GetSiteInstance())
583 return GetSiteInstance()->max_page_id();
584 else
585 return max_page_id_;
586}
587
588void TabContents::UpdateMaxPageID(int32 page_id) {
589 // Ensure both the SiteInstance and RenderProcessHost update their max page
[email protected]57c6a652009-05-04 07:58:34590 // IDs in sync. Only TabContents will also have site instances, except during
[email protected]d5f942ba2008-09-26 19:30:34591 // testing.
592 if (GetSiteInstance())
593 GetSiteInstance()->UpdateMaxPageID(page_id);
[email protected]8cb5d5b2010-02-09 11:36:16594 GetRenderProcessHost()->UpdateMaxPageID(page_id);
[email protected]d5f942ba2008-09-26 19:30:34595}
596
[email protected]96d185d2009-04-24 03:28:54597SiteInstance* TabContents::GetSiteInstance() const {
598 return render_manager_.current_host()->site_instance();
599}
600
[email protected]96d185d2009-04-24 03:28:54601bool TabContents::ShouldDisplayURL() {
[email protected]8ab24cc2009-11-13 20:56:15602 // Don't hide the url in view source mode and with interstitials.
[email protected]96d185d2009-04-24 03:28:54603 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]8ab24cc2009-11-13 20:56:15604 if (entry && (entry->IsViewSourceMode() ||
605 entry->page_type() == NavigationEntry::INTERSTITIAL_PAGE)) {
[email protected]96d185d2009-04-24 03:28:54606 return true;
[email protected]8ab24cc2009-11-13 20:56:15607 }
[email protected]83e3895b2009-06-11 00:07:16608
[email protected]96d185d2009-04-24 03:28:54609 DOMUI* dom_ui = GetDOMUIForCurrentState();
610 if (dom_ui)
611 return !dom_ui->should_hide_url();
612 return true;
613}
614
[email protected]d5f942ba2008-09-26 19:30:34615SkBitmap TabContents::GetFavIcon() const {
616 // Like GetTitle(), we also want to use the favicon for the last committed
617 // entry rather than a pending navigation entry.
[email protected]ce3fa3c2009-04-20 19:55:57618 NavigationEntry* entry = controller_.GetTransientEntry();
[email protected]cbab76d2008-10-13 22:42:47619 if (entry)
620 return entry->favicon().bitmap();
621
[email protected]ce3fa3c2009-04-20 19:55:57622 entry = controller_.GetLastCommittedEntry();
[email protected]d5f942ba2008-09-26 19:30:34623 if (entry)
624 return entry->favicon().bitmap();
[email protected]d5f942ba2008-09-26 19:30:34625 return SkBitmap();
626}
627
[email protected]0b303cc2009-09-28 22:35:15628bool TabContents::FavIconIsValid() const {
629 NavigationEntry* entry = controller_.GetTransientEntry();
630 if (entry)
631 return entry->favicon().is_valid();
632
633 entry = controller_.GetLastCommittedEntry();
634 if (entry)
635 return entry->favicon().is_valid();
636
637 return false;
638}
639
[email protected]96d185d2009-04-24 03:28:54640bool TabContents::ShouldDisplayFavIcon() {
641 // Always display a throbber during pending loads.
642 if (controller_.GetLastCommittedEntry() && controller_.pending_entry())
643 return true;
644
645 DOMUI* dom_ui = GetDOMUIForCurrentState();
646 if (dom_ui)
647 return !dom_ui->hide_favicon();
648 return true;
649}
650
[email protected]2d5a3592010-01-30 04:29:52651bool TabContents::IsContentBlocked(ContentSettingsType content_type) const {
[email protected]8cc8d492010-02-02 10:40:49652 if (content_type == CONTENT_SETTINGS_TYPE_POPUPS)
653 return blocked_popups_ != NULL;
654
[email protected]0de80162010-02-03 04:52:35655 if (content_type == CONTENT_SETTINGS_TYPE_IMAGES ||
656 content_type == CONTENT_SETTINGS_TYPE_JAVASCRIPT ||
[email protected]3dbb80b2010-02-09 22:41:20657 content_type == CONTENT_SETTINGS_TYPE_PLUGINS ||
658 content_type == CONTENT_SETTINGS_TYPE_COOKIES)
[email protected]0de80162010-02-03 04:52:35659 return content_blocked_[content_type];
660
[email protected]b1235062010-03-25 17:31:27661 // TODO(joth): remove once fully implemented.
662 if (content_type == CONTENT_SETTINGS_TYPE_GEOLOCATION)
663 return false;
664
[email protected]3dbb80b2010-02-09 22:41:20665 NOTREACHED();
[email protected]2d5a3592010-01-30 04:29:52666 return false;
667}
668
[email protected]96d185d2009-04-24 03:28:54669std::wstring TabContents::GetStatusText() const {
670 if (!is_loading() || load_state_ == net::LOAD_STATE_IDLE)
671 return std::wstring();
672
673 switch (load_state_) {
674 case net::LOAD_STATE_WAITING_FOR_CACHE:
675 return l10n_util::GetString(IDS_LOAD_STATE_WAITING_FOR_CACHE);
676 case net::LOAD_STATE_RESOLVING_PROXY_FOR_URL:
677 return l10n_util::GetString(IDS_LOAD_STATE_RESOLVING_PROXY_FOR_URL);
678 case net::LOAD_STATE_RESOLVING_HOST:
679 return l10n_util::GetString(IDS_LOAD_STATE_RESOLVING_HOST);
680 case net::LOAD_STATE_CONNECTING:
681 return l10n_util::GetString(IDS_LOAD_STATE_CONNECTING);
682 case net::LOAD_STATE_SENDING_REQUEST:
[email protected]094e5b22009-09-25 04:23:56683 if (upload_size_)
684 return l10n_util::GetStringF(
685 IDS_LOAD_STATE_SENDING_REQUEST_WITH_PROGRESS,
686 static_cast<int>((100 * upload_position_) / upload_size_));
687 else
688 return l10n_util::GetString(IDS_LOAD_STATE_SENDING_REQUEST);
[email protected]96d185d2009-04-24 03:28:54689 case net::LOAD_STATE_WAITING_FOR_RESPONSE:
690 return l10n_util::GetStringF(IDS_LOAD_STATE_WAITING_FOR_RESPONSE,
691 load_state_host_);
692 // Ignore net::LOAD_STATE_READING_RESPONSE and net::LOAD_STATE_IDLE
693 case net::LOAD_STATE_IDLE:
694 case net::LOAD_STATE_READING_RESPONSE:
695 break;
696 }
697
698 return std::wstring();
699}
700
[email protected]d5f942ba2008-09-26 19:30:34701void TabContents::SetIsCrashed(bool state) {
702 if (state == is_crashed_)
703 return;
704
705 is_crashed_ = state;
[email protected]c9cd2222009-05-06 05:16:50706 NotifyNavigationStateChanged(INVALIDATE_TAB);
[email protected]d5f942ba2008-09-26 19:30:34707}
708
[email protected]744ef172009-10-16 21:53:46709void TabContents::PageActionStateChanged() {
710 NotifyNavigationStateChanged(TabContents::INVALIDATE_PAGE_ACTIONS);
[email protected]d7eaf572009-07-01 21:57:00711}
[email protected]ce5c4502009-05-06 16:46:11712
[email protected]d5f942ba2008-09-26 19:30:34713void TabContents::NotifyNavigationStateChanged(unsigned changed_flags) {
714 if (delegate_)
715 delegate_->NavigationStateChanged(this, changed_flags);
716}
717
[email protected]96d185d2009-04-24 03:28:54718void TabContents::DidBecomeSelected() {
719 controller_.SetActive(true);
[email protected]8cb5d5b2010-02-09 11:36:16720 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
721 if (rwhv) {
722 rwhv->DidBecomeSelected();
[email protected]789e9152009-08-04 21:59:43723#if defined(OS_MACOSX)
[email protected]8cb5d5b2010-02-09 11:36:16724 rwhv->SetActive(true);
[email protected]789e9152009-08-04 21:59:43725#endif
726 }
[email protected]96d185d2009-04-24 03:28:54727
[email protected]8cb5d5b2010-02-09 11:36:16728 WebCacheManager::GetInstance()->ObserveActivity(GetRenderProcessHost()->id());
[email protected]96d185d2009-04-24 03:28:54729}
730
731void TabContents::WasHidden() {
732 if (!capturing_contents()) {
733 // |render_view_host()| can be NULL if the user middle clicks a link to open
734 // a tab in then background, then closes the tab before selecting it. This
735 // is because closing the tab calls TabContents::Destroy(), which removes
736 // the |render_view_host()|; then when we actually destroy the window,
737 // OnWindowPosChanged() notices and calls HideContents() (which calls us).
[email protected]8cb5d5b2010-02-09 11:36:16738 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
739 if (rwhv)
740 rwhv->WasHidden();
[email protected]96d185d2009-04-24 03:28:54741 }
742
743 NotificationService::current()->Notify(
744 NotificationType::TAB_CONTENTS_HIDDEN,
745 Source<TabContents>(this),
746 NotificationService::NoDetails());
747}
748
[email protected]d5f942ba2008-09-26 19:30:34749void TabContents::Activate() {
750 if (delegate_)
751 delegate_->ActivateContents(this);
752}
753
[email protected]96d185d2009-04-24 03:28:54754void TabContents::ShowContents() {
[email protected]8cb5d5b2010-02-09 11:36:16755 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
756 if (rwhv)
757 rwhv->DidBecomeSelected();
[email protected]96d185d2009-04-24 03:28:54758}
759
760void TabContents::HideContents() {
761 // TODO(pkasting): http://b/1239839 Right now we purposefully don't call
762 // our superclass HideContents(), because some callers want to be very picky
763 // about the order in which these get called. In addition to making the code
764 // here practically impossible to understand, this also means we end up
765 // calling TabContents::WasHidden() twice if callers call both versions of
[email protected]57c6a652009-05-04 07:58:34766 // HideContents() on a TabContents.
[email protected]96d185d2009-04-24 03:28:54767 WasHidden();
768}
769
[email protected]c0588052008-10-27 23:01:50770void TabContents::OpenURL(const GURL& url, const GURL& referrer,
[email protected]d5f942ba2008-09-26 19:30:34771 WindowOpenDisposition disposition,
772 PageTransition::Type transition) {
773 if (delegate_)
[email protected]c0588052008-10-27 23:01:50774 delegate_->OpenURLFromTab(this, url, referrer, disposition, transition);
[email protected]d5f942ba2008-09-26 19:30:34775}
776
[email protected]1ccb3568d2010-02-19 10:51:16777bool TabContents::NavigateToPendingEntry(
778 NavigationController::ReloadType reload_type) {
[email protected]96d185d2009-04-24 03:28:54779 const NavigationEntry& entry = *controller_.pending_entry();
780
781 RenderViewHost* dest_render_view_host = render_manager_.Navigate(entry);
782 if (!dest_render_view_host)
783 return false; // Unable to create the desired render view host.
784
[email protected]0352e5472010-04-29 00:11:40785 // For security, we should never send non-DOM-UI URLs (other than about:blank)
786 // to a DOM UI renderer. Double check that here.
[email protected]54ec6472010-04-09 19:39:58787 int enabled_bindings = dest_render_view_host->enabled_bindings();
[email protected]0352e5472010-04-29 00:11:40788 bool is_allowed_in_dom_ui_renderer =
789 DOMUIFactory::UseDOMUIForURL(entry.url()) ||
790 entry.url() == GURL(chrome::kAboutBlankURL);
[email protected]54ec6472010-04-09 19:39:58791 CHECK(!BindingsPolicy::is_dom_ui_enabled(enabled_bindings) ||
[email protected]0352e5472010-04-29 00:11:40792 is_allowed_in_dom_ui_renderer);
[email protected]54ec6472010-04-09 19:39:58793
[email protected]96d185d2009-04-24 03:28:54794 // Tell DevTools agent that it is attached prior to the navigation.
[email protected]d9f9b792009-06-24 13:17:12795 DevToolsManager* devtools_manager = DevToolsManager::GetInstance();
[email protected]7e8e6b62009-05-08 11:28:32796 if (devtools_manager) { // NULL in unit tests.
[email protected]3cc72b12010-03-18 23:03:00797 devtools_manager->OnNavigatingToPendingEntry(render_view_host(),
798 dest_render_view_host,
799 entry.url());
[email protected]7e8e6b62009-05-08 11:28:32800 }
[email protected]96d185d2009-04-24 03:28:54801
802 // Used for page load time metrics.
803 current_load_start_ = base::TimeTicks::Now();
804
805 // Navigate in the desired RenderViewHost.
[email protected]056de2d2009-06-26 16:41:34806 ViewMsg_Navigate_Params navigate_params;
[email protected]3cc72b12010-03-18 23:03:00807 MakeNavigateParams(controller_, reload_type, &navigate_params);
[email protected]056de2d2009-06-26 16:41:34808 dest_render_view_host->Navigate(navigate_params);
[email protected]96d185d2009-04-24 03:28:54809
810 if (entry.page_id() == -1) {
811 // HACK!! This code suppresses javascript: URLs from being added to
812 // session history, which is what we want to do for javascript: URLs that
813 // do not generate content. What we really need is a message from the
814 // renderer telling us that a new page was not created. The same message
815 // could be used for mailto: URLs and the like.
816 if (entry.url().SchemeIs(chrome::kJavaScriptScheme))
817 return false;
818 }
819
820 // Clear any provisional password saves - this stops password infobars
821 // showing up on pages the user navigates to while the right page is
822 // loading.
823 GetPasswordManager()->ClearProvisionalSave();
824
[email protected]1ccb3568d2010-02-19 10:51:16825 if (reload_type != NavigationController::NO_RELOAD &&
826 !profile()->IsOffTheRecord()) {
[email protected]0189bc722009-08-28 21:56:48827 FaviconService* favicon_service =
828 profile()->GetFaviconService(Profile::IMPLICIT_ACCESS);
829 if (favicon_service)
830 favicon_service->SetFaviconOutOfDateForPage(entry.url());
[email protected]96d185d2009-04-24 03:28:54831 }
832
[email protected]a888b29e62010-04-01 13:38:57833 // The url likely changed, see if there is an extension whose extent contains
834 // the current page.
[email protected]98aa0b52010-05-06 17:03:08835 UpdateExtensionAppForCurrentPage();
[email protected]a888b29e62010-04-01 13:38:57836
[email protected]d5f942ba2008-09-26 19:30:34837 return true;
838}
839
[email protected]96d185d2009-04-24 03:28:54840void TabContents::Stop() {
841 render_manager_.Stop();
842 printing_.Stop();
843}
844
[email protected]96d185d2009-04-24 03:28:54845void TabContents::DisassociateFromPopupCount() {
846 render_view_host()->DisassociateFromPopupCount();
847}
848
849TabContents* TabContents::Clone() {
850 // We create a new SiteInstance so that the new tab won't share processes
851 // with the old one. This can be changed in the future if we need it to share
852 // processes for some reason.
[email protected]420ae012009-04-24 05:16:32853 TabContents* tc = new TabContents(profile(),
[email protected]96d185d2009-04-24 03:28:54854 SiteInstance::CreateSiteInstance(profile()),
[email protected]12636df2009-09-28 22:32:21855 MSG_ROUTING_NONE, this);
[email protected]96d185d2009-04-24 03:28:54856 tc->controller().CopyStateFrom(controller_);
[email protected]98aa0b52010-05-06 17:03:08857 tc->extension_app_ = extension_app_;
858 tc->extension_app_for_current_page_ = extension_app_for_current_page_;
859 tc->extension_app_icon_ = extension_app_icon_;
[email protected]96d185d2009-04-24 03:28:54860 return tc;
861}
862
[email protected]4d677202009-07-19 07:37:12863void TabContents::ShowPageInfo(const GURL& url,
864 const NavigationEntry::SSLStatus& ssl,
865 bool show_history) {
866 if (!delegate_)
867 return;
868
[email protected]bb678332009-07-21 00:15:50869 delegate_->ShowPageInfo(profile(), url, ssl, show_history);
[email protected]4d677202009-07-19 07:37:12870}
871
initial.commit09911bf2008-07-26 23:55:29872ConstrainedWindow* TabContents::CreateConstrainedDialog(
[email protected]e8382172009-06-19 22:16:28873 ConstrainedWindowDelegate* delegate) {
initial.commit09911bf2008-07-26 23:55:29874 ConstrainedWindow* window =
[email protected]e8382172009-06-19 22:16:28875 ConstrainedWindow::CreateConstrainedDialog(this, delegate);
initial.commit09911bf2008-07-26 23:55:29876 child_windows_.push_back(window);
[email protected]fa1cf0b82010-01-15 21:49:44877
878 if (child_windows_.size() == 1) {
879 window->ShowConstrainedWindow();
880 BlockTabContent(true);
881 }
882
initial.commit09911bf2008-07-26 23:55:29883 return window;
884}
885
[email protected]fa1cf0b82010-01-15 21:49:44886void TabContents::BlockTabContent(bool blocked) {
[email protected]8dccd7cb2010-02-25 05:19:55887 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
888 if (rwhv)
889 rwhv->SetVisuallyDeemphasized(blocked);
[email protected]fa1cf0b82010-01-15 21:49:44890 render_view_host()->set_ignore_input_events(blocked);
891 if (delegate_)
892 delegate_->SetTabContentBlocked(this, blocked);
893}
894
initial.commit09911bf2008-07-26 23:55:29895void TabContents::AddNewContents(TabContents* new_contents,
896 WindowOpenDisposition disposition,
897 const gfx::Rect& initial_pos,
[email protected]7e7414ae2010-01-26 20:19:29898 bool user_gesture) {
initial.commit09911bf2008-07-26 23:55:29899 if (!delegate_)
900 return;
901
[email protected]35f7d212009-04-29 21:19:27902 if ((disposition == NEW_POPUP) && !user_gesture &&
903 !CommandLine::ForCurrentProcess()->HasSwitch(
904 switches::kDisablePopupBlocking)) {
[email protected]2c4410d2009-05-06 23:46:22905 // Unrequested popups from normal pages are constrained unless they're in
[email protected]b6c874582009-05-08 19:38:31906 // the whitelist. The popup owner will handle checking this.
[email protected]7e7414ae2010-01-26 20:19:29907 delegate_->GetConstrainingContents(this)->AddPopup(
908 new_contents, initial_pos);
[email protected]b6c874582009-05-08 19:38:31909 } else {
[email protected]304f8792009-08-19 21:28:09910#if defined(OS_CHROMEOS)
911 if (disposition == NEW_POPUP) {
912 // If the popup is bigger than a given factor of the screen, then
913 // turn it into a foreground tab (on chrome os only)
914 GdkScreen* screen = gdk_screen_get_default();
915 int max_width = gdk_screen_get_width(screen) * kMaxWidthFactor;
916 int max_height = gdk_screen_get_height(screen) * kMaxHeightFactor;
917 if (initial_pos.width() > max_width ||
918 initial_pos.height() > max_height) {
919 disposition = NEW_FOREGROUND_TAB;
920 }
921 }
922#endif
923
[email protected]0aa55312008-10-17 21:53:08924 new_contents->DisassociateFromPopupCount();
initial.commit09911bf2008-07-26 23:55:29925 delegate_->AddNewContents(this, new_contents, disposition, initial_pos,
926 user_gesture);
[email protected]7f37dd92009-09-15 16:35:49927 NotificationService::current()->Notify(
928 NotificationType::TAB_ADDED,
929 Source<TabContentsDelegate>(delegate_),
930 Details<TabContents>(this));
initial.commit09911bf2008-07-26 23:55:29931 }
initial.commit09911bf2008-07-26 23:55:29932
[email protected]8cc8d492010-02-02 10:40:49933 // TODO(pkasting): Why is this necessary?
934 PopupNotificationVisibilityChanged(blocked_popups_ != NULL);
initial.commit09911bf2008-07-26 23:55:29935}
936
[email protected]bcd9dc42010-01-23 16:38:18937bool TabContents::ExecuteCode(int request_id, const std::string& extension_id,
[email protected]61f5a7b2009-12-22 22:21:20938 const std::vector<URLPattern>& host_permissions,
[email protected]20ad2692009-11-20 18:27:20939 bool is_js_code, const std::string& code_string,
940 bool all_frames) {
[email protected]ebd0eae72010-02-16 23:50:51941 RenderViewHost* host = render_view_host();
942 if (!host)
943 return false;
944
945 return host->Send(new ViewMsg_ExecuteCode(host->routing_id(),
946 ViewMsg_ExecuteCode_Params(request_id, extension_id, host_permissions,
947 is_js_code, code_string, all_frames)));
[email protected]912256b32009-09-18 09:47:35948}
949
[email protected]96d185d2009-04-24 03:28:54950void TabContents::PopupNotificationVisibilityChanged(bool visible) {
[email protected]8cc8d492010-02-02 10:40:49951 if (is_being_destroyed_)
952 return;
953 if (!dont_notify_render_view_)
[email protected]e8345242010-05-06 03:00:40954 render_view_host()->AllowScriptToClose(!visible);
[email protected]0de80162010-02-03 04:52:35955 if (delegate_)
[email protected]20ad6a92010-04-21 14:07:24956 delegate_->OnContentSettingsChange(this);
[email protected]96d185d2009-04-24 03:28:54957}
958
[email protected]5c9e97a2009-09-09 23:48:30959gfx::NativeView TabContents::GetContentNativeView() const {
[email protected]96d185d2009-04-24 03:28:54960 return view_->GetContentNativeView();
961}
962
963gfx::NativeView TabContents::GetNativeView() const {
964 return view_->GetNativeView();
965}
966
967void TabContents::GetContainerBounds(gfx::Rect *out) const {
968 view_->GetContainerBounds(out);
969}
970
971void TabContents::Focus() {
972 view_->Focus();
973}
974
[email protected]90daadb42009-06-08 21:27:28975void TabContents::FocusThroughTabTraversal(bool reverse) {
[email protected]7e383692009-06-12 19:14:54976 if (showing_interstitial_page()) {
977 render_manager_.interstitial_page()->FocusThroughTabTraversal(reverse);
978 return;
979 }
[email protected]96d185d2009-04-24 03:28:54980 render_view_host()->SetInitialFocus(reverse);
981}
982
983bool TabContents::FocusLocationBarByDefault() {
984 DOMUI* dom_ui = GetDOMUIForCurrentState();
985 if (dom_ui)
986 return dom_ui->focus_location_bar_by_default();
[email protected]3e3f0eb2009-06-22 18:33:43987 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]e0d481582009-09-15 21:06:25988 if (entry && entry->url() == GURL(chrome::kAboutBlankURL))
[email protected]3e3f0eb2009-06-22 18:33:43989 return true;
[email protected]96d185d2009-04-24 03:28:54990 return false;
991}
992
[email protected]a26dc362010-04-23 01:48:58993void TabContents::SetFocusToLocationBar(bool select_all) {
[email protected]a11aaf22010-03-30 00:03:38994 if (delegate())
[email protected]a26dc362010-04-23 01:48:58995 delegate()->SetFocusToLocationBar(select_all);
[email protected]a11aaf22010-03-30 00:03:38996}
997
[email protected]616ed5a2008-11-21 22:27:24998void TabContents::AddInfoBar(InfoBarDelegate* delegate) {
[email protected]f7019302010-03-26 19:58:32999 if (delegate_ && !delegate_->infobars_enabled()) {
1000 delegate->InfoBarClosed();
1001 return;
1002 }
1003
[email protected]616ed5a2008-11-21 22:27:241004 // Look through the existing InfoBarDelegates we have for a match. If we've
1005 // already got one that matches, then we don't add the new one.
[email protected]f86a07022008-11-25 01:06:051006 for (int i = 0; i < infobar_delegate_count(); ++i) {
[email protected]6d27a7b2009-12-18 23:25:451007 if (GetInfoBarDelegateAt(i)->EqualsDelegate(delegate)) {
1008 // Tell the new infobar to close so that it can clean itself up.
1009 delegate->InfoBarClosed();
[email protected]616ed5a2008-11-21 22:27:241010 return;
[email protected]6d27a7b2009-12-18 23:25:451011 }
[email protected]616ed5a2008-11-21 22:27:241012 }
1013
1014 infobar_delegates_.push_back(delegate);
[email protected]bfd04a62009-02-01 18:16:561015 NotificationService::current()->Notify(
1016 NotificationType::TAB_CONTENTS_INFOBAR_ADDED,
1017 Source<TabContents>(this),
1018 Details<InfoBarDelegate>(delegate));
[email protected]616ed5a2008-11-21 22:27:241019
1020 // Add ourselves as an observer for navigations the first time a delegate is
1021 // added. We use this notification to expire InfoBars that need to expire on
1022 // page transitions.
1023 if (infobar_delegates_.size() == 1) {
[email protected]bfd04a62009-02-01 18:16:561024 registrar_.Add(this, NotificationType::NAV_ENTRY_COMMITTED,
[email protected]ce3fa3c2009-04-20 19:55:571025 Source<NavigationController>(&controller_));
[email protected]616ed5a2008-11-21 22:27:241026 }
1027}
1028
1029void TabContents::RemoveInfoBar(InfoBarDelegate* delegate) {
[email protected]f7019302010-03-26 19:58:321030 if (delegate_ && !delegate_->infobars_enabled()) {
1031 return;
1032 }
1033
[email protected]616ed5a2008-11-21 22:27:241034 std::vector<InfoBarDelegate*>::iterator it =
1035 find(infobar_delegates_.begin(), infobar_delegates_.end(), delegate);
1036 if (it != infobar_delegates_.end()) {
1037 InfoBarDelegate* delegate = *it;
[email protected]bfd04a62009-02-01 18:16:561038 NotificationService::current()->Notify(
1039 NotificationType::TAB_CONTENTS_INFOBAR_REMOVED,
1040 Source<TabContents>(this),
1041 Details<InfoBarDelegate>(delegate));
[email protected]f86a07022008-11-25 01:06:051042 infobar_delegates_.erase(it);
[email protected]616ed5a2008-11-21 22:27:241043
[email protected]6a02963e2009-01-06 16:58:031044 // Remove ourselves as an observer if we are tracking no more InfoBars.
1045 if (infobar_delegates_.empty()) {
[email protected]bfd04a62009-02-01 18:16:561046 registrar_.Remove(this, NotificationType::NAV_ENTRY_COMMITTED,
[email protected]ce3fa3c2009-04-20 19:55:571047 Source<NavigationController>(&controller_));
[email protected]6a02963e2009-01-06 16:58:031048 }
[email protected]616ed5a2008-11-21 22:27:241049 }
1050}
[email protected]3edd9522009-03-04 22:19:411051
[email protected]f81821c2009-08-07 01:29:471052void TabContents::ReplaceInfoBar(InfoBarDelegate* old_delegate,
1053 InfoBarDelegate* new_delegate) {
[email protected]f7019302010-03-26 19:58:321054 if (delegate_ && !delegate_->infobars_enabled()) {
1055 new_delegate->InfoBarClosed();
1056 return;
1057 }
1058
[email protected]f81821c2009-08-07 01:29:471059 std::vector<InfoBarDelegate*>::iterator it =
1060 find(infobar_delegates_.begin(), infobar_delegates_.end(), old_delegate);
1061 DCHECK(it != infobar_delegates_.end());
1062
1063 // Notify the container about the change of plans.
1064 scoped_ptr<std::pair<InfoBarDelegate*, InfoBarDelegate*> > details(
1065 new std::pair<InfoBarDelegate*, InfoBarDelegate*>(
1066 old_delegate, new_delegate));
1067 NotificationService::current()->Notify(
1068 NotificationType::TAB_CONTENTS_INFOBAR_REPLACED,
1069 Source<TabContents>(this),
1070 Details<std::pair<InfoBarDelegate*, InfoBarDelegate*> >(details.get()));
1071
1072 // Remove the old one.
1073 infobar_delegates_.erase(it);
1074
1075 // Add the new one.
1076 infobar_delegates_.push_back(new_delegate);
1077}
1078
[email protected]20ede072009-10-06 00:28:351079bool TabContents::ShouldShowBookmarkBar() {
[email protected]aa9196f2009-10-22 16:20:271080 if (showing_interstitial_page())
1081 return false;
1082
[email protected]96d185d2009-04-24 03:28:541083 // See GetDOMUIForCurrentState() comment for more info. This case is very
1084 // similar, but for non-first loads, we want to use the committed entry. This
1085 // is so the bookmarks bar disappears at the same time the page does.
1086 if (controller_.GetLastCommittedEntry()) {
1087 // Not the first load, always use the committed DOM UI.
1088 if (render_manager_.dom_ui())
1089 return render_manager_.dom_ui()->force_bookmark_bar_visible();
1090 return false; // Default.
1091 }
1092
1093 // When it's the first load, we know either the pending one or the committed
1094 // one will have the DOM UI in it (see GetDOMUIForCurrentState), and only one
1095 // of them will be valid, so we can just check both.
1096 if (render_manager_.pending_dom_ui())
1097 return render_manager_.pending_dom_ui()->force_bookmark_bar_visible();
1098 if (render_manager_.dom_ui())
1099 return render_manager_.dom_ui()->force_bookmark_bar_visible();
1100 return false; // Default.
1101}
1102
[email protected]d8f16ae2009-08-30 03:47:261103bool TabContents::IsExtensionShelfAlwaysVisible() {
1104 // See GetDOMUIForCurrentState() comment for more info. This case is very
1105 // similar, but for non-first loads, we want to use the committed entry. This
1106 // is so the bookmarks bar disappears at the same time the page does.
1107 if (controller_.GetLastCommittedEntry()) {
1108 // Not the first load, always use the committed DOM UI.
1109 if (render_manager_.dom_ui())
1110 return render_manager_.dom_ui()->force_extension_shelf_visible();
1111 return false; // Default.
1112 }
1113
1114 // When it's the first load, we know either the pending one or the committed
1115 // one will have the DOM UI in it (see GetDOMUIForCurrentState), and only one
1116 // of them will be valid, so we can just check both.
1117 if (render_manager_.pending_dom_ui())
1118 return render_manager_.pending_dom_ui()->force_extension_shelf_visible();
1119 if (render_manager_.dom_ui())
1120 return render_manager_.dom_ui()->force_extension_shelf_visible();
1121 return false; // Default.
1122}
1123
[email protected]3edd9522009-03-04 22:19:411124void TabContents::ToolbarSizeChanged(bool is_animating) {
1125 TabContentsDelegate* d = delegate();
1126 if (d)
1127 d->ToolbarSizeChanged(this, is_animating);
1128}
[email protected]616ed5a2008-11-21 22:27:241129
[email protected]fc6fb7fb2009-11-07 02:35:041130bool TabContents::CanDownload(int request_id) {
1131 TabContentsDelegate* d = delegate();
1132 if (d)
1133 return d->CanDownload(request_id);
1134 return true;
1135}
1136
[email protected]d5f942ba2008-09-26 19:30:341137void TabContents::OnStartDownload(DownloadItem* download) {
1138 DCHECK(download);
[email protected]d5f942ba2008-09-26 19:30:341139
1140 // Download in a constrained popup is shown in the tab that opened it.
[email protected]b6c874582009-05-08 19:38:311141 TabContents* tab_contents = delegate()->GetConstrainingContents(this);
[email protected]d5f942ba2008-09-26 19:30:341142
[email protected]c70f9b82010-04-21 07:31:111143 if (tab_contents && tab_contents->delegate()) {
[email protected]59560e0b2009-06-04 03:30:221144 tab_contents->delegate()->OnStartDownload(download);
[email protected]c70f9b82010-04-21 07:31:111145 // If the download occurs in a new tab, close it
1146 if (controller_.IsInitialNavigation() && (tab_contents == this))
1147 delegate()->CloseContents(this);
1148 }
[email protected]3edd9522009-03-04 22:19:411149}
[email protected]3edd9522009-03-04 22:19:411150
[email protected]d5f942ba2008-09-26 19:30:341151void TabContents::WillClose(ConstrainedWindow* window) {
1152 ConstrainedWindowList::iterator it =
1153 find(child_windows_.begin(), child_windows_.end(), window);
[email protected]fa1cf0b82010-01-15 21:49:441154 bool removed_topmost_window = it == child_windows_.begin();
[email protected]d5f942ba2008-09-26 19:30:341155 if (it != child_windows_.end())
1156 child_windows_.erase(it);
[email protected]55098792010-03-24 18:20:371157 if (child_windows_.size() > 0) {
1158 if (removed_topmost_window) {
1159 child_windows_[0]->ShowConstrainedWindow();
1160 }
[email protected]fa1cf0b82010-01-15 21:49:441161 BlockTabContent(true);
1162 } else {
1163 BlockTabContent(false);
1164 }
[email protected]66ba4932009-06-04 19:22:131165}
[email protected]d5f942ba2008-09-26 19:30:341166
[email protected]66ba4932009-06-04 19:22:131167void TabContents::WillCloseBlockedPopupContainer(
1168 BlockedPopupContainer* container) {
1169 DCHECK(blocked_popups_ == container);
1170 blocked_popups_ = NULL;
[email protected]8cc8d492010-02-02 10:40:491171 PopupNotificationVisibilityChanged(false);
[email protected]d5f942ba2008-09-26 19:30:341172}
1173
[email protected]d5f942ba2008-09-26 19:30:341174void TabContents::DidMoveOrResize(ConstrainedWindow* window) {
[email protected]332af7732009-03-11 13:21:351175#if defined(OS_WIN)
[email protected]92edc472009-02-10 20:32:061176 UpdateWindow(GetNativeView());
[email protected]332af7732009-03-11 13:21:351177#endif
[email protected]d5f942ba2008-09-26 19:30:341178}
1179
[email protected]c1dc9f132009-11-20 23:13:211180void TabContents::StartFinding(string16 search_string,
[email protected]e491f1c2009-05-22 20:28:121181 bool forward_direction,
1182 bool case_sensitive) {
[email protected]c1dc9f132009-11-20 23:13:211183 // If search_string is empty, it means FindNext was pressed with a keyboard
[email protected]6e95706d2009-04-23 22:55:011184 // shortcut so unless we have something to search for we return early.
[email protected]c1dc9f132009-11-20 23:13:211185 if (search_string.empty() && find_text_.empty()) {
[email protected]e5d2f7132010-04-09 18:13:531186 string16 last_search_prepopulate_text =
1187 FindBarState::GetLastPrepopulateText(profile());
1188
[email protected]87fae512009-12-11 02:20:211189 // Try the last thing we searched for on this tab, then the last thing
1190 // searched for on any tab.
1191 if (!previous_find_text_.empty())
1192 search_string = previous_find_text_;
[email protected]e5d2f7132010-04-09 18:13:531193 else if (!last_search_prepopulate_text.empty())
1194 search_string = last_search_prepopulate_text;
[email protected]87fae512009-12-11 02:20:211195 else
[email protected]e491f1c2009-05-22 20:28:121196 return;
[email protected]e491f1c2009-05-22 20:28:121197 }
[email protected]6e95706d2009-04-23 22:55:011198
[email protected]101c90e2009-10-26 21:14:441199 // Keep track of the previous search.
1200 previous_find_text_ = find_text_;
1201
[email protected]6e95706d2009-04-23 22:55:011202 // This is a FindNext operation if we are searching for the same text again,
1203 // or if the passed in search text is empty (FindNext keyboard shortcut). The
1204 // exception to this is if the Find was aborted (then we don't want FindNext
1205 // because the highlighting has been cleared and we need it to reappear). We
1206 // therefore treat FindNext after an aborted Find operation as a full fledged
1207 // Find.
[email protected]c1dc9f132009-11-20 23:13:211208 bool find_next = (find_text_ == search_string || search_string.empty()) &&
[email protected]e491f1c2009-05-22 20:28:121209 (last_search_case_sensitive_ == case_sensitive) &&
[email protected]6e95706d2009-04-23 22:55:011210 !find_op_aborted_;
1211 if (!find_next)
1212 current_find_request_id_ = find_request_id_counter_++;
1213
[email protected]c1dc9f132009-11-20 23:13:211214 if (!search_string.empty())
1215 find_text_ = search_string;
[email protected]e491f1c2009-05-22 20:28:121216 last_search_case_sensitive_ = case_sensitive;
[email protected]6e95706d2009-04-23 22:55:011217
1218 find_op_aborted_ = false;
1219
1220 // Keep track of what the last search was across the tabs.
[email protected]e5d2f7132010-04-09 18:13:531221 FindBarState* find_bar_state = profile()->GetFindBarState();
1222 find_bar_state->set_last_prepopulate_text(find_text_);
[email protected]6e95706d2009-04-23 22:55:011223 render_view_host()->StartFinding(current_find_request_id_,
1224 find_text_,
1225 forward_direction,
[email protected]e491f1c2009-05-22 20:28:121226 case_sensitive,
[email protected]6e95706d2009-04-23 22:55:011227 find_next);
1228}
1229
[email protected]5bc8fe92010-03-11 18:19:001230void TabContents::StopFinding(
1231 FindBarController::SelectionAction selection_action) {
1232 // When the action is kClearAction, it means the find string has been cleared
[email protected]2d2dffc2009-06-09 21:41:001233 // by the user, but the UI has not been dismissed.
[email protected]5bc8fe92010-03-11 18:19:001234 if (selection_action != FindBarController::kClearSelection)
[email protected]2d2dffc2009-06-09 21:41:001235 find_ui_active_ = false;
[email protected]726bdbd72010-04-05 17:08:071236 if (!find_text_.empty())
1237 previous_find_text_ = find_text_;
[email protected]101c90e2009-10-26 21:14:441238 find_text_.clear();
[email protected]6e95706d2009-04-23 22:55:011239 find_op_aborted_ = true;
[email protected]e491f1c2009-05-22 20:28:121240 last_search_result_ = FindNotificationDetails();
[email protected]5bc8fe92010-03-11 18:19:001241 render_view_host()->StopFinding(selection_action);
[email protected]6e95706d2009-04-23 22:55:011242}
1243
[email protected]420ae012009-04-24 05:16:321244void TabContents::OnSavePage() {
1245 // If we can not save the page, try to download it.
1246 if (!SavePackage::IsSavableContents(contents_mime_type())) {
1247 DownloadManager* dlm = profile()->GetDownloadManager();
1248 const GURL& current_page_url = GetURL();
1249 if (dlm && current_page_url.is_valid())
[email protected]d82ed61e2009-06-16 02:46:221250 dlm->DownloadUrl(current_page_url, GURL(), "", this);
[email protected]420ae012009-04-24 05:16:321251 return;
1252 }
1253
1254 Stop();
1255
1256 // Create the save package and possibly prompt the user for the name to save
1257 // the page as. The user prompt is an asynchronous operation that runs on
1258 // another thread.
[email protected]d82ed61e2009-06-16 02:46:221259 save_package_ = new SavePackage(this);
[email protected]420ae012009-04-24 05:16:321260 save_package_->GetSaveInfo();
1261}
1262
1263// Used in automated testing to bypass prompting the user for file names.
1264// Instead, the names and paths are hard coded rather than running them through
1265// file name sanitation and extension / mime checking.
[email protected]828cabe2009-09-26 22:47:111266bool TabContents::SavePage(const FilePath& main_file, const FilePath& dir_path,
[email protected]420ae012009-04-24 05:16:321267 SavePackage::SavePackageType save_type) {
1268 // Stop the page from navigating.
1269 Stop();
1270
[email protected]828cabe2009-09-26 22:47:111271 save_package_ = new SavePackage(this, save_type, main_file, dir_path);
[email protected]cdf6cc82009-08-04 21:48:591272 return save_package_->Init();
[email protected]420ae012009-04-24 05:16:321273}
1274
[email protected]89d6e6e2009-12-03 23:06:511275void TabContents::EmailPageLocation() {
1276 std::string title = EscapeQueryParamValue(UTF16ToUTF8(GetTitle()), false);
1277 std::string page_url = EscapeQueryParamValue(GetURL().spec(), false);
1278 std::string mailto = std::string("mailto:?subject=Fwd:%20") +
1279 title + "&body=%0A%0A" + page_url;
1280 platform_util::OpenExternal(GURL(mailto));
1281}
1282
[email protected]420ae012009-04-24 05:16:321283void TabContents::PrintPreview() {
1284 // We don't show the print preview yet, only the print dialog.
1285 PrintNow();
1286}
1287
1288bool TabContents::PrintNow() {
1289 // We can't print interstitial page for now.
1290 if (showing_interstitial_page())
1291 return false;
1292
1293 return render_view_host()->PrintPages();
1294}
1295
[email protected]82270452009-06-19 15:58:011296void TabContents::PrintingDone(int document_cookie, bool success) {
1297 render_view_host()->PrintingDone(document_cookie, success);
1298}
1299
[email protected]420ae012009-04-24 05:16:321300bool TabContents::IsActiveEntry(int32 page_id) {
1301 NavigationEntry* active_entry = controller_.GetActiveEntry();
1302 return (active_entry != NULL &&
1303 active_entry->site_instance() == GetSiteInstance() &&
1304 active_entry->page_id() == page_id);
1305}
1306
[email protected]b2fe07d12010-02-09 14:38:081307void TabContents::SetOverrideEncoding(const std::string& encoding) {
[email protected]8cb5d5b2010-02-09 11:36:161308 set_encoding(encoding);
1309 render_view_host()->SetPageEncoding(encoding);
1310}
1311
[email protected]b2fe07d12010-02-09 14:38:081312void TabContents::ResetOverrideEncoding() {
[email protected]8cb5d5b2010-02-09 11:36:161313 reset_encoding();
1314 render_view_host()->ResetPageEncodingToDefault();
1315}
1316
1317void TabContents::WindowMoveOrResizeStarted() {
1318 render_view_host()->WindowMoveOrResizeStarted();
1319}
1320
[email protected]3f8eb7f2009-10-02 23:58:051321void TabContents::LogNewTabTime(const std::string& event_name) {
1322 // Not all new tab pages get timed. In those cases, we don't have a
1323 // new_tab_start_time_.
1324 if (new_tab_start_time_.is_null())
1325 return;
1326
1327 base::TimeDelta duration = base::TimeTicks::Now() - new_tab_start_time_;
[email protected]84abba62009-10-07 17:01:441328 MetricEventDurationDetails details(event_name,
1329 static_cast<int>(duration.InMilliseconds()));
1330
[email protected]7ab1e7d62009-10-14 23:32:011331 if (event_name == "Tab.NewTabScriptStart") {
1332 UMA_HISTOGRAM_TIMES("Tab.NewTabScriptStart", duration);
1333 } else if (event_name == "Tab.NewTabDOMContentLoaded") {
1334 UMA_HISTOGRAM_TIMES("Tab.NewTabDOMContentLoaded", duration);
1335 } else if (event_name == "Tab.NewTabOnload") {
1336 UMA_HISTOGRAM_TIMES("Tab.NewTabOnload", duration);
[email protected]3f8eb7f2009-10-02 23:58:051337 // The new tab page has finished loading; reset it.
1338 new_tab_start_time_ = base::TimeTicks();
[email protected]84abba62009-10-07 17:01:441339 } else {
1340 NOTREACHED();
[email protected]3f8eb7f2009-10-02 23:58:051341 }
[email protected]84abba62009-10-07 17:01:441342 NotificationService::current()->Notify(
1343 NotificationType::METRIC_EVENT_DURATION,
1344 Source<TabContents>(this),
1345 Details<MetricEventDurationDetails>(&details));
[email protected]3f8eb7f2009-10-02 23:58:051346}
1347
[email protected]7ab1e7d62009-10-14 23:32:011348void TabContents::OnCloseStarted() {
1349 if (tab_close_start_time_.is_null())
1350 tab_close_start_time_ = base::TimeTicks::Now();
1351}
1352
[email protected]c37e3b62010-01-22 00:15:171353TabContents* TabContents::CloneAndMakePhantom() {
[email protected]c37e3b62010-01-22 00:15:171354 TabNavigation tab_nav;
[email protected]df72ac72010-02-06 00:43:161355
1356 NavigationEntry* entry = controller().GetActiveEntry();
[email protected]98aa0b52010-05-06 17:03:081357 if (extension_app_)
1358 tab_nav.set_virtual_url(extension_app_->GetFullLaunchURL());
[email protected]df72ac72010-02-06 00:43:161359 else if (entry)
1360 tab_nav.SetFromNavigationEntry(*entry);
1361
[email protected]c37e3b62010-01-22 00:15:171362 std::vector<TabNavigation> navigations;
1363 navigations.push_back(tab_nav);
1364
1365 TabContents* new_contents =
1366 new TabContents(profile(), NULL, MSG_ROUTING_NONE, NULL);
[email protected]98aa0b52010-05-06 17:03:081367 new_contents->SetExtensionApp(extension_app_);
[email protected]c37e3b62010-01-22 00:15:171368 new_contents->controller().RestoreFromState(navigations, 0, false);
[email protected]c37e3b62010-01-22 00:15:171369
[email protected]98aa0b52010-05-06 17:03:081370 if (!extension_app_ && entry)
[email protected]df72ac72010-02-06 00:43:161371 new_contents->controller().GetActiveEntry()->favicon() = entry->favicon();
1372
[email protected]c37e3b62010-01-22 00:15:171373 return new_contents;
1374}
1375
[email protected]0de80162010-02-03 04:52:351376// Resets the |content_blocked_| array.
1377void TabContents::ClearBlockedContentSettings() {
[email protected]15cf526b2010-02-12 06:33:491378 for (size_t i = 0; i < arraysize(content_blocked_); ++i)
[email protected]0de80162010-02-03 04:52:351379 content_blocked_[i] = false;
1380}
1381
[email protected]614c0e12010-03-25 10:12:561382// Resets the |geolocation_settings_| map.
[email protected]9d062b12010-04-14 09:36:551383void TabContents::ClearGeolocationContentSettings(
1384 const NavigationController::LoadCommittedDetails& details) {
1385 if (!geolocation_content_settings_.empty()) {
1386 // Clear the geolocation settings only if we're going to a new origin,
1387 // or if we're staying at the same origin and its setting is ASK.
1388 // This is to prevent clearing the icon by just changing some URL param.
1389 // TODO(bulach): refactor this logic into its own class and remove the
1390 // duplication from ContentSettingDomainListBubbleModel.
1391 if (!details.entry ||
1392 details.previous_url.GetOrigin() != details.entry->url().GetOrigin() ||
1393 (details.entry->url().is_valid() &&
1394 profile()->GetGeolocationContentSettingsMap()->GetContentSetting(
1395 details.entry->url(), details.entry->url()) ==
1396 CONTENT_SETTING_ASK)) {
1397 geolocation_content_settings_.clear();
1398 }
1399 }
[email protected]614c0e12010-03-25 10:12:561400}
1401
[email protected]96d185d2009-04-24 03:28:541402// Notifies the RenderWidgetHost instance about the fact that the page is
1403// loading, or done loading and calls the base implementation.
1404void TabContents::SetIsLoading(bool is_loading,
1405 LoadNotificationDetails* details) {
1406 if (is_loading == is_loading_)
1407 return;
1408
1409 if (!is_loading) {
1410 load_state_ = net::LOAD_STATE_IDLE;
1411 load_state_host_.clear();
[email protected]094e5b22009-09-25 04:23:561412 upload_size_ = 0;
1413 upload_position_ = 0;
[email protected]96d185d2009-04-24 03:28:541414 }
1415
1416 render_manager_.SetIsLoading(is_loading);
1417
1418 is_loading_ = is_loading;
1419 waiting_for_response_ = is_loading;
1420
1421 if (delegate_)
1422 delegate_->LoadingStateChanged(this);
[email protected]329581b2009-04-28 06:52:351423 NotifyNavigationStateChanged(INVALIDATE_LOAD);
[email protected]96d185d2009-04-24 03:28:541424
1425 NotificationType type = is_loading ? NotificationType::LOAD_START :
1426 NotificationType::LOAD_STOP;
[email protected]ce5c4502009-05-06 16:46:111427 NotificationDetails det = NotificationService::NoDetails();
[email protected]96d185d2009-04-24 03:28:541428 if (details)
1429 det = Details<LoadNotificationDetails>(details);
1430 NotificationService::current()->Notify(type,
1431 Source<NavigationController>(&controller_),
1432 det);
1433}
1434
[email protected]b6c874582009-05-08 19:38:311435void TabContents::AddPopup(TabContents* new_contents,
[email protected]7e7414ae2010-01-26 20:19:291436 const gfx::Rect& initial_pos) {
[email protected]57eda82a2010-05-05 21:28:061437 // A page can't spawn popups (or do anything else, either) until its load
1438 // commits, so when we reach here, the popup was spawned by the
1439 // NavigationController's last committed entry, not the active entry. For
1440 // example, if a page opens a popup in an onunload() handler, then the active
1441 // entry is the page to be loaded as we navigate away from the unloading
1442 // page. For this reason, we can't use GetURL() to get the opener URL,
1443 // because it returns the active entry.
1444 NavigationEntry* entry = controller_.GetLastCommittedEntry();
1445 GURL creator = entry ? entry->virtual_url() : GURL::EmptyGURL();
1446
1447 if (creator.is_valid() &&
[email protected]8cc8d492010-02-02 10:40:491448 profile()->GetHostContentSettingsMap()->GetContentSetting(
[email protected]57eda82a2010-05-05 21:28:061449 creator, CONTENT_SETTINGS_TYPE_POPUPS) == CONTENT_SETTING_ALLOW) {
[email protected]8cc8d492010-02-02 10:40:491450 AddNewContents(new_contents, NEW_POPUP, initial_pos, true);
1451 } else {
1452 if (!blocked_popups_)
1453 blocked_popups_ = new BlockedPopupContainer(this);
1454 blocked_popups_->AddTabContents(new_contents, initial_pos);
1455 }
[email protected]b9681312008-11-07 00:08:261456}
[email protected]616ed5a2008-11-21 22:27:241457
1458namespace {
1459bool TransitionIsReload(PageTransition::Type transition) {
1460 return PageTransition::StripQualifier(transition) == PageTransition::RELOAD;
1461}
1462}
1463
1464void TabContents::ExpireInfoBars(
1465 const NavigationController::LoadCommittedDetails& details) {
1466 // Only hide InfoBars when the user has done something that makes the main
1467 // frame load. We don't want various automatic or subframe navigations making
1468 // it disappear.
1469 if (!details.is_user_initiated_main_frame_load())
1470 return;
1471
[email protected]f86a07022008-11-25 01:06:051472 for (int i = infobar_delegate_count() - 1; i >= 0; --i) {
[email protected]616ed5a2008-11-21 22:27:241473 InfoBarDelegate* delegate = GetInfoBarDelegateAt(i);
[email protected]f86a07022008-11-25 01:06:051474 if (delegate->ShouldExpire(details))
[email protected]616ed5a2008-11-21 22:27:241475 RemoveInfoBar(delegate);
[email protected]616ed5a2008-11-21 22:27:241476 }
1477}
[email protected]fdd61c62009-04-22 19:22:571478
[email protected]96d185d2009-04-24 03:28:541479DOMUI* TabContents::GetDOMUIForCurrentState() {
1480 // When there is a pending navigation entry, we want to use the pending DOMUI
1481 // that goes along with it to control the basic flags. For example, we want to
1482 // show the pending URL in the URL bar, so we want the display_url flag to
1483 // be from the pending entry.
1484 //
1485 // The confusion comes because there are multiple possibilities for the
1486 // initial load in a tab as a side effect of the way the RenderViewHostManager
1487 // works.
1488 //
1489 // - For the very first tab the load looks "normal". The new tab DOM UI is
1490 // the pending one, and we want it to apply here.
1491 //
1492 // - For subsequent new tabs, they'll get a new SiteInstance which will then
1493 // get switched to the one previously associated with the new tab pages.
1494 // This switching will cause the manager to commit the RVH/DOMUI. So we'll
1495 // have a committed DOM UI in this case.
1496 //
1497 // This condition handles all of these cases:
1498 //
1499 // - First load in first tab: no committed nav entry + pending nav entry +
1500 // pending dom ui:
1501 // -> Use pending DOM UI if any.
1502 //
1503 // - First load in second tab: no committed nav entry + pending nav entry +
1504 // no pending DOM UI:
1505 // -> Use the committed DOM UI if any.
1506 //
1507 // - Second navigation in any tab: committed nav entry + pending nav entry:
1508 // -> Use pending DOM UI if any.
1509 //
1510 // - Normal state with no load: committed nav entry + no pending nav entry:
1511 // -> Use committed DOM UI.
1512 if (controller_.pending_entry() &&
1513 (controller_.GetLastCommittedEntry() ||
1514 render_manager_.pending_dom_ui()))
1515 return render_manager_.pending_dom_ui();
1516 return render_manager_.dom_ui();
1517}
[email protected]420ae012009-04-24 05:16:321518
1519void TabContents::DidNavigateMainFramePostCommit(
1520 const NavigationController::LoadCommittedDetails& details,
1521 const ViewHostMsg_FrameNavigate_Params& params) {
[email protected]c2e74fe82009-09-03 17:57:441522 if (opener_dom_ui_type_ != DOMUIFactory::kNoDOMUI) {
1523 // If this is a window.open navigation, use the same DOMUI as the renderer
1524 // that opened the window, as long as both renderers have the same
1525 // privileges.
1526 if (opener_dom_ui_type_ == DOMUIFactory::GetDOMUIType(GetURL())) {
1527 DOMUI* dom_ui = DOMUIFactory::CreateDOMUIForURL(this, GetURL());
1528 // dom_ui might be NULL if the URL refers to a non-existent extension.
1529 if (dom_ui) {
1530 render_manager_.SetDOMUIPostCommit(dom_ui);
1531 dom_ui->RenderViewCreated(render_view_host());
1532 }
1533 }
1534 opener_dom_ui_type_ = DOMUIFactory::kNoDOMUI;
1535 }
1536
[email protected]420ae012009-04-24 05:16:321537 if (details.is_user_initiated_main_frame_load()) {
1538 // Clear the status bubble. This is a workaround for a bug where WebKit
1539 // doesn't let us know that the cursor left an element during a
1540 // transition (this is also why the mouse cursor remains as a hand after
1541 // clicking on a link); see bugs 1184641 and 980803. We don't want to
1542 // clear the bubble when a user navigates to a named anchor in the same
1543 // page.
1544 UpdateTargetURL(details.entry->page_id(), GURL());
1545
1546 // UpdateHelpersForDidNavigate will handle the case where the password_form
1547 // origin is valid.
1548 // TODO(brettw) bug 1343111: Password manager stuff in here needs to be
1549 // cleaned up and covered by tests.
1550 if (!params.password_form.origin.is_valid())
1551 GetPasswordManager()->DidNavigate();
1552 }
1553
1554 // The keyword generator uses the navigation entries, so must be called after
1555 // the commit.
1556 GenerateKeywordIfNecessary(params);
1557
1558 // Allow the new page to set the title again.
1559 received_page_title_ = false;
1560
1561 // Get the favicon, either from history or request it from the net.
1562 fav_icon_helper_.FetchFavIcon(details.entry->url());
1563
[email protected]0de80162010-02-03 04:52:351564 // Clear all page actions, blocked content notifications and browser actions
1565 // for this tab, unless this is an in-page navigation.
[email protected]a6e82fc2010-02-24 22:28:141566 if (!details.is_in_page) {
[email protected]92c6f9b92009-10-24 04:35:081567 ExtensionsService* service = profile()->GetExtensionsService();
1568 if (service) {
1569 for (size_t i = 0; i < service->extensions()->size(); ++i) {
[email protected]5d246db22009-10-27 06:17:571570 ExtensionAction* browser_action =
[email protected]92c6f9b92009-10-24 04:35:081571 service->extensions()->at(i)->browser_action();
[email protected]56ce6e52009-10-27 00:10:521572 if (browser_action) {
1573 browser_action->ClearAllValuesForTab(controller().session_id().id());
1574 NotificationService::current()->Notify(
1575 NotificationType::EXTENSION_BROWSER_ACTION_UPDATED,
[email protected]5d246db22009-10-27 06:17:571576 Source<ExtensionAction>(browser_action),
[email protected]56ce6e52009-10-27 00:10:521577 NotificationService::NoDetails());
1578 }
[email protected]92c6f9b92009-10-24 04:35:081579
[email protected]5d246db22009-10-27 06:17:571580 ExtensionAction* page_action =
[email protected]56ce6e52009-10-27 00:10:521581 service->extensions()->at(i)->page_action();
1582 if (page_action) {
1583 page_action->ClearAllValuesForTab(controller().session_id().id());
1584 PageActionStateChanged();
1585 }
[email protected]92c6f9b92009-10-24 04:35:081586 }
[email protected]aece2c7f2009-08-27 20:43:171587 }
[email protected]0de80162010-02-03 04:52:351588
1589 // Close blocked popups.
1590 if (blocked_popups_) {
1591 AutoReset auto_reset(&dont_notify_render_view_, true);
1592 blocked_popups_->Destroy();
[email protected]da7b5262010-02-25 21:12:461593 blocked_popups_ = NULL;
[email protected]0de80162010-02-03 04:52:351594 }
1595
1596 // Clear "blocked" flags.
1597 ClearBlockedContentSettings();
[email protected]9d062b12010-04-14 09:36:551598 ClearGeolocationContentSettings(details);
[email protected]0de80162010-02-03 04:52:351599 if (delegate_)
[email protected]20ad6a92010-04-21 14:07:241600 delegate_->OnContentSettingsChange(this);
[email protected]aece2c7f2009-08-27 20:43:171601 }
[email protected]ce5c4502009-05-06 16:46:111602
[email protected]8cc8d492010-02-02 10:40:491603 // Close constrained windows if necessary.
1604 if (!net::RegistryControlledDomainService::SameDomainOrHost(
1605 details.previous_url, details.entry->url()))
1606 CloseConstrainedWindows();
1607
[email protected]420ae012009-04-24 05:16:321608 // Update the starred state.
1609 UpdateStarredStateForCurrentURL();
[email protected]c07c9752010-03-12 05:57:531610
1611 // Clear the cache of forms in AutoFill.
1612 if (autofill_manager_.get())
1613 autofill_manager_->Reset();
[email protected]a888b29e62010-04-01 13:38:571614
1615 // The url likely changed, see if there is an extension whose extent contains
1616 // the current page.
[email protected]98aa0b52010-05-06 17:03:081617 UpdateExtensionAppForCurrentPage();
[email protected]420ae012009-04-24 05:16:321618}
1619
1620void TabContents::DidNavigateAnyFramePostCommit(
1621 RenderViewHost* render_view_host,
1622 const NavigationController::LoadCommittedDetails& details,
1623 const ViewHostMsg_FrameNavigate_Params& params) {
1624 // If we navigate, start showing messages again. This does nothing to prevent
1625 // a malicious script from spamming messages, since the script could just
1626 // reload the page to stop blocking.
1627 suppress_javascript_messages_ = false;
1628
[email protected]420ae012009-04-24 05:16:321629 // Notify the password manager of the navigation or form submit.
1630 // TODO(brettw) bug 1343111: Password manager stuff in here needs to be
1631 // cleaned up and covered by tests.
1632 if (params.password_form.origin.is_valid())
1633 GetPasswordManager()->ProvisionallySavePassword(params.password_form);
[email protected]3b1c1812010-02-12 21:53:551634
1635 // Let the LanguageState clear its state.
[email protected]34cec8d82010-03-05 18:27:011636 bool reload = details.entry->transition_type() == PageTransition::RELOAD ||
1637 details.type == NavigationType::SAME_PAGE;
1638 language_state_.DidNavigate(reload, details.is_in_page);
[email protected]420ae012009-04-24 05:16:321639}
1640
[email protected]8cc8d492010-02-02 10:40:491641void TabContents::CloseConstrainedWindows() {
1642 // Clear out any constrained windows since we are leaving this page entirely.
[email protected]420ae012009-04-24 05:16:321643 // We use indices instead of iterators in case CloseWindow does something
1644 // that may invalidate an iterator.
1645 int size = static_cast<int>(child_windows_.size());
1646 for (int i = size - 1; i >= 0; --i) {
1647 ConstrainedWindow* window = child_windows_[i];
[email protected]fa1cf0b82010-01-15 21:49:441648 if (window) {
[email protected]420ae012009-04-24 05:16:321649 window->CloseConstrainedWindow();
[email protected]fa1cf0b82010-01-15 21:49:441650 BlockTabContent(false);
1651 }
[email protected]420ae012009-04-24 05:16:321652 }
1653}
1654
1655void TabContents::UpdateStarredStateForCurrentURL() {
1656 BookmarkModel* model = profile()->GetBookmarkModel();
1657 const bool old_state = is_starred_;
1658 is_starred_ = (model && model->IsBookmarked(GetURL()));
1659
1660 if (is_starred_ != old_state && delegate())
1661 delegate()->URLStarredChanged(this, is_starred_);
1662}
1663
1664void TabContents::UpdateAlternateErrorPageURL() {
1665 GURL url = GetAlternateErrorPageURL();
1666 render_view_host()->SetAlternateErrorPageURL(url);
1667}
1668
1669void TabContents::UpdateWebPreferences() {
1670 render_view_host()->UpdateWebPreferences(GetWebkitPrefs());
1671}
1672
1673void TabContents::UpdateMaxPageIDIfNecessary(SiteInstance* site_instance,
1674 RenderViewHost* rvh) {
1675 // If we are creating a RVH for a restored controller, then we might
1676 // have more page IDs than the SiteInstance's current max page ID. We must
1677 // make sure that the max page ID is larger than any restored page ID.
1678 // Note that it is ok for conflicting page IDs to exist in another tab
1679 // (i.e., NavigationController), but if any page ID is larger than the max,
1680 // the back/forward list will get confused.
1681 int max_restored_page_id = controller_.max_restored_page_id();
1682 if (max_restored_page_id > 0) {
1683 int curr_max_page_id = site_instance->max_page_id();
1684 if (max_restored_page_id > curr_max_page_id) {
1685 // Need to update the site instance immediately.
1686 site_instance->UpdateMaxPageID(max_restored_page_id);
1687
1688 // Also tell the renderer to update its internal representation. We
1689 // need to reserve enough IDs to make all restored page IDs less than
1690 // the max.
1691 if (curr_max_page_id < 0)
1692 curr_max_page_id = 0;
1693 rvh->ReservePageIDRange(max_restored_page_id - curr_max_page_id);
1694 }
1695 }
1696}
1697
1698void TabContents::UpdateHistoryForNavigation(
[email protected]ebe89e062009-08-13 23:16:541699 const GURL& virtual_url,
[email protected]befd8d822009-07-01 04:51:471700 const NavigationController::LoadCommittedDetails& details,
[email protected]420ae012009-04-24 05:16:321701 const ViewHostMsg_FrameNavigate_Params& params) {
1702 if (profile()->IsOffTheRecord())
1703 return;
1704
1705 // Add to history service.
1706 HistoryService* hs = profile()->GetHistoryService(Profile::IMPLICIT_ACCESS);
1707 if (hs) {
1708 if (PageTransition::IsMainFrame(params.transition) &&
[email protected]ebe89e062009-08-13 23:16:541709 virtual_url != params.url) {
1710 // Hack on the "virtual" URL so that it will appear in history. For some
[email protected]420ae012009-04-24 05:16:321711 // types of URLs, we will display a magic URL that is different from where
1712 // the page is actually navigated. We want the user to see in history
[email protected]ebe89e062009-08-13 23:16:541713 // what they saw in the URL bar, so we add the virtual URL as a redirect.
1714 // This only applies to the main frame, as the virtual URL doesn't apply
[email protected]420ae012009-04-24 05:16:321715 // to sub-frames.
1716 std::vector<GURL> redirects = params.redirects;
1717 if (!redirects.empty())
[email protected]ebe89e062009-08-13 23:16:541718 redirects.back() = virtual_url;
1719 hs->AddPage(virtual_url, this, params.page_id, params.referrer,
[email protected]befd8d822009-07-01 04:51:471720 params.transition, redirects, details.did_replace_entry);
[email protected]420ae012009-04-24 05:16:321721 } else {
1722 hs->AddPage(params.url, this, params.page_id, params.referrer,
[email protected]befd8d822009-07-01 04:51:471723 params.transition, params.redirects,
1724 details.did_replace_entry);
[email protected]420ae012009-04-24 05:16:321725 }
1726 }
1727}
1728
1729bool TabContents::UpdateTitleForEntry(NavigationEntry* entry,
1730 const std::wstring& title) {
1731 // For file URLs without a title, use the pathname instead. In the case of a
1732 // synthesized title, we don't want the update to count toward the "one set
1733 // per page of the title to history."
1734 std::wstring final_title;
1735 bool explicit_set;
1736 if (entry->url().SchemeIsFile() && title.empty()) {
1737 final_title = UTF8ToWide(entry->url().ExtractFileName());
1738 explicit_set = false; // Don't count synthetic titles toward the set limit.
1739 } else {
1740 TrimWhitespace(title, TRIM_ALL, &final_title);
1741 explicit_set = true;
1742 }
1743
1744 if (final_title == UTF16ToWideHack(entry->title()))
1745 return false; // Nothing changed, don't bother.
1746
1747 entry->set_title(WideToUTF16Hack(final_title));
1748
1749 // Update the history system for this page.
1750 if (!profile()->IsOffTheRecord() && !received_page_title_) {
1751 HistoryService* hs =
1752 profile()->GetHistoryService(Profile::IMPLICIT_ACCESS);
1753 if (hs)
[email protected]ebe89e062009-08-13 23:16:541754 hs->SetPageTitle(entry->virtual_url(), final_title);
[email protected]420ae012009-04-24 05:16:321755
1756 // Don't allow the title to be saved again for explicitly set ones.
1757 received_page_title_ = explicit_set;
1758 }
1759
1760 // Lastly, set the title for the view.
1761 view_->SetPageTitle(final_title);
1762
[email protected]cbc0e1b2010-04-12 18:33:041763 NotificationService::current()->Notify(
1764 NotificationType::TAB_CONTENTS_TITLE_UPDATED,
1765 Source<TabContents>(this),
1766 NotificationService::NoDetails());
1767
[email protected]420ae012009-04-24 05:16:321768 return true;
1769}
1770
1771void TabContents::NotifySwapped() {
1772 // After sending out a swap notification, we need to send a disconnect
1773 // notification so that clients that pick up a pointer to |this| can NULL the
1774 // pointer. See Bug 1230284.
1775 notify_disconnection_ = true;
1776 NotificationService::current()->Notify(
[email protected]57c6a652009-05-04 07:58:341777 NotificationType::TAB_CONTENTS_SWAPPED,
[email protected]d82ed61e2009-06-16 02:46:221778 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:321779 NotificationService::NoDetails());
1780}
1781
1782void TabContents::NotifyConnected() {
1783 notify_disconnection_ = true;
1784 NotificationService::current()->Notify(
[email protected]57c6a652009-05-04 07:58:341785 NotificationType::TAB_CONTENTS_CONNECTED,
[email protected]d82ed61e2009-06-16 02:46:221786 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:321787 NotificationService::NoDetails());
1788}
1789
1790void TabContents::NotifyDisconnected() {
1791 if (!notify_disconnection_)
1792 return;
1793
1794 notify_disconnection_ = false;
1795 NotificationService::current()->Notify(
[email protected]57c6a652009-05-04 07:58:341796 NotificationType::TAB_CONTENTS_DISCONNECTED,
[email protected]d82ed61e2009-06-16 02:46:221797 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:321798 NotificationService::NoDetails());
1799}
1800
1801void TabContents::GenerateKeywordIfNecessary(
1802 const ViewHostMsg_FrameNavigate_Params& params) {
1803 if (!params.searchable_form_url.is_valid())
1804 return;
1805
1806 if (profile()->IsOffTheRecord())
1807 return;
1808
1809 int last_index = controller_.last_committed_entry_index();
1810 // When there was no previous page, the last index will be 0. This is
1811 // normally due to a form submit that opened in a new tab.
1812 // TODO(brettw) bug 916126: we should support keywords when form submits
1813 // happen in new tabs.
1814 if (last_index <= 0)
1815 return;
1816 const NavigationEntry* previous_entry =
1817 controller_.GetEntryAtIndex(last_index - 1);
1818 if (IsFormSubmit(previous_entry)) {
1819 // Only generate a keyword if the previous page wasn't itself a form
1820 // submit.
1821 return;
1822 }
1823
1824 GURL keyword_url = previous_entry->user_typed_url().is_valid() ?
1825 previous_entry->user_typed_url() : previous_entry->url();
1826 std::wstring keyword =
1827 TemplateURLModel::GenerateKeyword(keyword_url, true); // autodetected
1828 if (keyword.empty())
1829 return;
1830
1831 TemplateURLModel* url_model = profile()->GetTemplateURLModel();
1832 if (!url_model)
1833 return;
1834
1835 if (!url_model->loaded()) {
1836 url_model->Load();
1837 return;
1838 }
1839
1840 const TemplateURL* current_url;
1841 std::wstring url = UTF8ToWide(params.searchable_form_url.spec());
1842 if (!url_model->CanReplaceKeyword(keyword, url, &current_url))
1843 return;
1844
1845 if (current_url) {
1846 if (current_url->originating_url().is_valid()) {
1847 // The existing keyword was generated from an OpenSearch description
1848 // document, don't regenerate.
1849 return;
1850 }
1851 url_model->Remove(current_url);
1852 }
1853 TemplateURL* new_url = new TemplateURL();
1854 new_url->set_keyword(keyword);
1855 new_url->set_short_name(keyword);
1856 new_url->SetURL(url, 0, 0);
1857 new_url->add_input_encoding(params.searchable_form_encoding);
1858 DCHECK(controller_.GetLastCommittedEntry());
1859 const GURL& favicon_url =
1860 controller_.GetLastCommittedEntry()->favicon().url();
1861 if (favicon_url.is_valid()) {
1862 new_url->SetFavIconURL(favicon_url);
1863 } else {
1864 // The favicon url isn't valid. This means there really isn't a favicon,
1865 // or the favicon url wasn't obtained before the load started. This assumes
1866 // the later.
1867 // TODO(sky): Need a way to set the favicon that doesn't involve generating
1868 // its url.
1869 new_url->SetFavIconURL(TemplateURL::GenerateFaviconURL(params.referrer));
1870 }
1871 new_url->set_safe_for_autoreplace(true);
1872 url_model->Add(new_url);
1873}
1874
[email protected]bf5c2ff392009-07-08 16:24:331875void TabContents::OnUserGesture() {
1876 // See comment in RenderViewHostDelegate::OnUserGesture as to why we do this.
1877 DownloadRequestManager* drm = g_browser_process->download_request_manager();
1878 if (drm)
1879 drm->OnUserGesture(this);
[email protected]e7eaedde2009-09-25 20:09:491880 ExternalProtocolHandler::OnUserGesture();
[email protected]bf5c2ff392009-07-08 16:24:331881 controller_.OnUserGesture();
1882}
1883
1884void TabContents::OnFindReply(int request_id,
1885 int number_of_matches,
1886 const gfx::Rect& selection_rect,
1887 int active_match_ordinal,
1888 bool final_update) {
[email protected]3ce7ced2009-07-24 18:52:421889 // Ignore responses for requests that have been aborted.
1890 if (find_op_aborted_)
1891 return;
1892
[email protected]bf5c2ff392009-07-08 16:24:331893 // Ignore responses for requests other than the one we have most recently
1894 // issued. That way we won't act on stale results when the user has
1895 // already typed in another query.
1896 if (request_id != current_find_request_id_)
1897 return;
1898
1899 if (number_of_matches == -1)
1900 number_of_matches = last_search_result_.number_of_matches();
1901 if (active_match_ordinal == -1)
1902 active_match_ordinal = last_search_result_.active_match_ordinal();
1903
1904 gfx::Rect selection = selection_rect;
1905 if (selection.IsEmpty())
1906 selection = last_search_result_.selection_rect();
1907
1908 // Notify the UI, automation and any other observers that a find result was
1909 // found.
1910 last_search_result_ = FindNotificationDetails(request_id, number_of_matches,
1911 selection, active_match_ordinal,
1912 final_update);
1913 NotificationService::current()->Notify(
1914 NotificationType::FIND_RESULT_AVAILABLE,
1915 Source<TabContents>(this),
1916 Details<FindNotificationDetails>(&last_search_result_));
1917}
1918
1919void TabContents::GoToEntryAtOffset(int offset) {
[email protected]f9cc4c452009-10-13 14:56:381920 if (!delegate_ || delegate_->OnGoToEntryOffset(offset))
1921 controller_.GoToOffset(offset);
[email protected]bf5c2ff392009-07-08 16:24:331922}
1923
[email protected]bf5c2ff392009-07-08 16:24:331924void TabContents::OnMissingPluginStatus(int status) {
1925#if defined(OS_WIN)
1926// TODO(PORT): pull in when plug-ins work
1927 GetPluginInstaller()->OnMissingPluginStatus(status);
1928#endif
1929}
1930
1931void TabContents::OnCrashedPlugin(const FilePath& plugin_path) {
[email protected]bf5c2ff392009-07-08 16:24:331932 DCHECK(!plugin_path.value().empty());
1933
1934 std::wstring plugin_name = plugin_path.ToWStringHack();
[email protected]d32d46182009-07-23 00:09:351935#if defined(OS_WIN) || defined(OS_MACOSX)
[email protected]bf5c2ff392009-07-08 16:24:331936 scoped_ptr<FileVersionInfo> version_info(
1937 FileVersionInfo::CreateFileVersionInfo(plugin_path));
1938 if (version_info.get()) {
1939 const std::wstring& product_name = version_info->product_name();
[email protected]996fd702009-09-04 19:12:371940 if (!product_name.empty()) {
[email protected]bf5c2ff392009-07-08 16:24:331941 plugin_name = product_name;
[email protected]996fd702009-09-04 19:12:371942#if defined(OS_MACOSX)
1943 // Many plugins on the Mac have .plugin in the actual name, which looks
1944 // terrible, so look for that and strip it off if present.
1945 const std::wstring plugin_extension(L".plugin");
1946 if (EndsWith(plugin_name, plugin_extension, true))
1947 plugin_name.erase(plugin_name.length() - plugin_extension.length());
1948#endif // OS_MACOSX
1949 }
[email protected]bf5c2ff392009-07-08 16:24:331950 }
[email protected]4a560ed2009-07-13 23:35:261951#else
1952 NOTIMPLEMENTED() << " convert plugin path to plugin name";
1953#endif
[email protected]bf5c2ff392009-07-08 16:24:331954 AddInfoBar(new SimpleAlertInfoBarDelegate(
1955 this, l10n_util::GetStringF(IDS_PLUGIN_CRASHED_PROMPT, plugin_name),
[email protected]938e1f92010-04-01 18:09:421956 NULL, true));
[email protected]bf5c2ff392009-07-08 16:24:331957}
1958
1959void TabContents::OnCrashedWorker() {
1960 AddInfoBar(new SimpleAlertInfoBarDelegate(
1961 this, l10n_util::GetString(IDS_WEBWORKER_CRASHED_PROMPT),
[email protected]938e1f92010-04-01 18:09:421962 NULL, true));
[email protected]bf5c2ff392009-07-08 16:24:331963}
1964
1965void TabContents::OnDidGetApplicationInfo(
1966 int32 page_id,
1967 const webkit_glue::WebApplicationInfo& info) {
[email protected]ed5431872009-11-17 08:39:511968 web_app_info_ = info;
[email protected]bf5c2ff392009-07-08 16:24:331969
[email protected]ed5431872009-11-17 08:39:511970 if (delegate())
1971 delegate()->OnDidGetApplicationInfo(this, page_id);
[email protected]bf5c2ff392009-07-08 16:24:331972}
1973
[email protected]a2cf65eb2010-01-18 08:20:381974void TabContents::OnPageContents(const GURL& url,
1975 int renderer_process_id,
1976 int32 page_id,
[email protected]a8a81292010-01-21 00:32:451977 const std::wstring& contents,
1978 const std::string& language) {
[email protected]a2cf65eb2010-01-18 08:20:381979 // Don't index any https pages. People generally don't want their bank
1980 // accounts, etc. indexed on their computer, especially since some of these
1981 // things are not marked cachable.
1982 // TODO(brettw) we may want to consider more elaborate heuristics such as
1983 // the cachability of the page. We may also want to consider subframes (this
1984 // test will still index subframes if the subframe is SSL).
1985 if (!url.SchemeIsSecure()) {
1986 Profile* p = profile();
1987 if (p && !p->IsOffTheRecord()) {
1988 HistoryService* hs = p->GetHistoryService(Profile::IMPLICIT_ACCESS);
1989 if (hs)
1990 hs->SetPageContents(url, contents);
1991 }
1992 }
[email protected]a8a81292010-01-21 00:32:451993
[email protected]3b1c1812010-02-12 21:53:551994 language_state_.LanguageDetermined(language);
[email protected]a8a81292010-01-21 00:32:451995
1996 std::string lang = language;
1997 NotificationService::current()->Notify(
1998 NotificationType::TAB_LANGUAGE_DETERMINED,
[email protected]7e0831b2010-02-01 08:45:151999 Source<TabContents>(this),
[email protected]a8a81292010-01-21 00:32:452000 Details<std::string>(&lang));
[email protected]a2cf65eb2010-01-18 08:20:382001}
2002
[email protected]7e0831b2010-02-01 08:45:152003void TabContents::OnPageTranslated(int32 page_id,
2004 const std::string& original_lang,
[email protected]152b8ba32010-03-29 19:40:162005 const std::string& translated_lang,
2006 TranslateErrors::Type error_type) {
[email protected]3b1c1812010-02-12 21:53:552007 language_state_.set_current_language(translated_lang);
2008 language_state_.set_translation_pending(false);
[email protected]152b8ba32010-03-29 19:40:162009 PageTranslatedDetails details(original_lang, translated_lang, error_type);
[email protected]7e0831b2010-02-01 08:45:152010 NotificationService::current()->Notify(
2011 NotificationType::PAGE_TRANSLATED,
2012 Source<TabContents>(this),
[email protected]152b8ba32010-03-29 19:40:162013 Details<PageTranslatedDetails>(&details));
[email protected]7e0831b2010-02-01 08:45:152014}
2015
[email protected]bf5c2ff392009-07-08 16:24:332016void TabContents::DidStartProvisionalLoadForFrame(
2017 RenderViewHost* render_view_host,
2018 bool is_main_frame,
2019 const GURL& url) {
2020 ProvisionalLoadDetails details(is_main_frame,
2021 controller_.IsURLInPageNavigation(url),
2022 url, std::string(), false);
2023 NotificationService::current()->Notify(
2024 NotificationType::FRAME_PROVISIONAL_LOAD_START,
2025 Source<NavigationController>(&controller_),
2026 Details<ProvisionalLoadDetails>(&details));
2027}
2028
2029void TabContents::DidStartReceivingResourceResponse(
[email protected]3dbb80b2010-02-09 22:41:202030 const ResourceRequestDetails& details) {
[email protected]bf5c2ff392009-07-08 16:24:332031 NotificationService::current()->Notify(
2032 NotificationType::RESOURCE_RESPONSE_STARTED,
2033 Source<NavigationController>(&controller()),
[email protected]3dbb80b2010-02-09 22:41:202034 Details<const ResourceRequestDetails>(&details));
[email protected]bf5c2ff392009-07-08 16:24:332035}
2036
[email protected]3dbb80b2010-02-09 22:41:202037void TabContents::DidRedirectResource(
2038 const ResourceRedirectDetails& details) {
[email protected]bf5c2ff392009-07-08 16:24:332039 NotificationService::current()->Notify(
2040 NotificationType::RESOURCE_RECEIVED_REDIRECT,
2041 Source<NavigationController>(&controller()),
[email protected]3dbb80b2010-02-09 22:41:202042 Details<const ResourceRequestDetails>(&details));
[email protected]bf5c2ff392009-07-08 16:24:332043}
2044
2045void TabContents::DidLoadResourceFromMemoryCache(
2046 const GURL& url,
2047 const std::string& frame_origin,
2048 const std::string& main_frame_origin,
2049 const std::string& security_info) {
2050 // Send out a notification that we loaded a resource from our memory cache.
2051 int cert_id = 0, cert_status = 0, security_bits = 0;
2052 SSLManager::DeserializeSecurityInfo(security_info,
2053 &cert_id, &cert_status,
2054 &security_bits);
2055 LoadFromMemoryCacheDetails details(url, frame_origin, main_frame_origin,
[email protected]8cb5d5b2010-02-09 11:36:162056 GetRenderProcessHost()->id(), cert_id,
2057 cert_status);
[email protected]bf5c2ff392009-07-08 16:24:332058
2059 NotificationService::current()->Notify(
2060 NotificationType::LOAD_FROM_MEMORY_CACHE,
2061 Source<NavigationController>(&controller_),
2062 Details<LoadFromMemoryCacheDetails>(&details));
2063}
2064
[email protected]e3d60e5d2009-09-25 21:08:292065void TabContents::DidDisplayInsecureContent() {
[email protected]09843b5f2009-09-26 08:30:592066 controller_.ssl_manager()->DidDisplayInsecureContent();
[email protected]e3d60e5d2009-09-25 21:08:292067}
2068
2069void TabContents::DidRunInsecureContent(const std::string& security_origin) {
[email protected]09843b5f2009-09-26 08:30:592070 controller_.ssl_manager()->DidRunInsecureContent(security_origin);
[email protected]e3d60e5d2009-09-25 21:08:292071}
2072
[email protected]bf5c2ff392009-07-08 16:24:332073void TabContents::DidFailProvisionalLoadWithError(
2074 RenderViewHost* render_view_host,
2075 bool is_main_frame,
2076 int error_code,
2077 const GURL& url,
2078 bool showing_repost_interstitial) {
2079 if (net::ERR_ABORTED == error_code) {
2080 // EVIL HACK ALERT! Ignore failed loads when we're showing interstitials.
2081 // This means that the interstitial won't be torn down properly, which is
2082 // bad. But if we have an interstitial, go back to another tab type, and
2083 // then load the same interstitial again, we could end up getting the first
2084 // interstitial's "failed" message (as a result of the cancel) when we're on
2085 // the second one.
2086 //
2087 // We can't tell this apart, so we think we're tearing down the current page
2088 // which will cause a crash later one. There is also some code in
2089 // RenderViewHostManager::RendererAbortedProvisionalLoad that is commented
2090 // out because of this problem.
2091 //
2092 // http://code.google.com/p/chromium/issues/detail?id=2855
2093 // Because this will not tear down the interstitial properly, if "back" is
2094 // back to another tab type, the interstitial will still be somewhat alive
2095 // in the previous tab type. If you navigate somewhere that activates the
2096 // tab with the interstitial again, you'll see a flash before the new load
2097 // commits of the interstitial page.
2098 if (showing_interstitial_page()) {
2099 LOG(WARNING) << "Discarding message during interstitial.";
2100 return;
2101 }
2102
2103 // This will discard our pending entry if we cancelled the load (e.g., if we
2104 // decided to download the file instead of load it). Only discard the
2105 // pending entry if the URLs match, otherwise the user initiated a navigate
2106 // before the page loaded so that the discard would discard the wrong entry.
2107 NavigationEntry* pending_entry = controller_.pending_entry();
2108 if (pending_entry && pending_entry->url() == url) {
2109 controller_.DiscardNonCommittedEntries();
2110 // Update the URL display.
2111 NotifyNavigationStateChanged(TabContents::INVALIDATE_URL);
2112 }
2113
2114 render_manager_.RendererAbortedProvisionalLoad(render_view_host);
2115 }
2116
2117 // Send out a notification that we failed a provisional load with an error.
2118 ProvisionalLoadDetails details(is_main_frame,
2119 controller_.IsURLInPageNavigation(url),
2120 url, std::string(), false);
2121 details.set_error_code(error_code);
2122
2123 NotificationService::current()->Notify(
2124 NotificationType::FAIL_PROVISIONAL_LOAD_WITH_ERROR,
2125 Source<NavigationController>(&controller_),
2126 Details<ProvisionalLoadDetails>(&details));
2127}
2128
2129void TabContents::DocumentLoadedInFrame() {
2130 controller_.DocumentLoadedInFrame();
[email protected]70102972010-05-06 14:49:402131 if (renderer_accessible_ && !requested_accessibility_tree_) {
2132 render_view_host()->RequestAccessibilityTree();
2133 requested_accessibility_tree_ = true;
2134 }
[email protected]bf5c2ff392009-07-08 16:24:332135}
2136
[email protected]0de80162010-02-03 04:52:352137void TabContents::OnContentBlocked(ContentSettingsType type) {
[email protected]b1235062010-03-25 17:31:272138 // TODO(joth): remove once fully implemented.
2139 DCHECK(type != CONTENT_SETTINGS_TYPE_GEOLOCATION);
[email protected]0de80162010-02-03 04:52:352140 content_blocked_[type] = true;
2141 if (delegate_)
[email protected]20ad6a92010-04-21 14:07:242142 delegate_->OnContentSettingsChange(this);
[email protected]0de80162010-02-03 04:52:352143}
2144
[email protected]614c0e12010-03-25 10:12:562145void TabContents::OnGeolocationPermissionSet(const GURL& requesting_origin,
2146 bool allowed) {
2147 geolocation_content_settings_[requesting_origin] =
2148 allowed ? CONTENT_SETTING_ALLOW : CONTENT_SETTING_BLOCK;
[email protected]614c0e12010-03-25 10:12:562149 if (delegate_)
[email protected]20ad6a92010-04-21 14:07:242150 delegate_->OnContentSettingsChange(this);
[email protected]614c0e12010-03-25 10:12:562151}
2152
[email protected]8d3347f2009-07-09 22:00:212153RenderViewHostDelegate::View* TabContents::GetViewDelegate() {
[email protected]420ae012009-04-24 05:16:322154 return view_.get();
2155}
2156
[email protected]8d3347f2009-07-09 22:00:212157RenderViewHostDelegate::RendererManagement*
2158TabContents::GetRendererManagementDelegate() {
2159 return &render_manager_;
2160}
2161
[email protected]bf5c2ff392009-07-08 16:24:332162RenderViewHostDelegate::BrowserIntegration*
[email protected]8d3347f2009-07-09 22:00:212163 TabContents::GetBrowserIntegrationDelegate() {
2164 return this;
[email protected]bf5c2ff392009-07-08 16:24:332165}
2166
[email protected]8d3347f2009-07-09 22:00:212167RenderViewHostDelegate::Resource* TabContents::GetResourceDelegate() {
2168 return this;
[email protected]bf5c2ff392009-07-08 16:24:332169}
2170
[email protected]8d3347f2009-07-09 22:00:212171RenderViewHostDelegate::Save* TabContents::GetSaveDelegate() {
[email protected]420ae012009-04-24 05:16:322172 return save_package_.get(); // May be NULL, but we can return NULL.
2173}
2174
[email protected]8d3347f2009-07-09 22:00:212175RenderViewHostDelegate::Printing* TabContents::GetPrintingDelegate() {
2176 return &printing_;
2177}
2178
2179RenderViewHostDelegate::FavIcon* TabContents::GetFavIconDelegate() {
2180 return &fav_icon_helper_;
2181}
2182
[email protected]e8dbd674b2010-03-11 18:34:402183RenderViewHostDelegate::Autocomplete* TabContents::GetAutocompleteDelegate() {
[email protected]6db0def92010-03-02 19:55:452184 if (autocomplete_history_manager_.get() == NULL)
2185 autocomplete_history_manager_.reset(new AutocompleteHistoryManager(this));
2186 return autocomplete_history_manager_.get();
[email protected]bf5c2ff392009-07-08 16:24:332187}
2188
[email protected]21674df2009-10-28 19:51:212189RenderViewHostDelegate::AutoFill* TabContents::GetAutoFillDelegate() {
[email protected]cf28ec52010-04-09 22:56:342190 return GetAutoFillManager();
[email protected]21674df2009-10-28 19:51:212191}
2192
[email protected]fd571bb2010-03-19 01:23:542193AutomationResourceRoutingDelegate*
2194TabContents::GetAutomationResourceRoutingDelegate() {
2195 return delegate();
2196}
2197
[email protected]ced90ae12010-02-20 02:06:162198RenderViewHostDelegate::BookmarkDrag* TabContents::GetBookmarkDragDelegate() {
2199 return bookmark_drag_;
2200}
2201
2202void TabContents::SetBookmarkDragDelegate(
2203 RenderViewHostDelegate::BookmarkDrag* bookmark_drag) {
2204 bookmark_drag_ = bookmark_drag;
2205}
2206
[email protected]93623c5d2009-12-10 21:40:322207RendererPreferences TabContents::GetRendererPrefs(Profile* profile) const {
[email protected]840b1512009-07-21 16:53:462208 return renderer_preferences_;
[email protected]80d96fa2009-06-10 22:34:512209}
2210
[email protected]57c6a652009-05-04 07:58:342211TabContents* TabContents::GetAsTabContents() {
2212 return this;
2213}
2214
[email protected]7b291f92009-08-14 05:43:532215ViewType::Type TabContents::GetRenderViewType() const {
2216 return ViewType::TAB_CONTENTS;
2217}
2218
2219int TabContents::GetBrowserWindowID() const {
2220 return controller().window_id().id();
2221}
2222
[email protected]420ae012009-04-24 05:16:322223void TabContents::RenderViewCreated(RenderViewHost* render_view_host) {
[email protected]2e4633c2009-07-09 16:58:062224 NotificationService::current()->Notify(
2225 NotificationType::RENDER_VIEW_HOST_CREATED_FOR_TAB,
2226 Source<TabContents>(this),
2227 Details<RenderViewHost>(render_view_host));
[email protected]420ae012009-04-24 05:16:322228 NavigationEntry* entry = controller_.GetActiveEntry();
2229 if (!entry)
2230 return;
2231
2232 // When we're creating views, we're still doing initial setup, so we always
2233 // use the pending DOM UI rather than any possibly existing committed one.
2234 if (render_manager_.pending_dom_ui()) {
2235 render_manager_.pending_dom_ui()->RenderViewCreated(render_view_host);
2236 }
2237
2238 if (entry->IsViewSourceMode()) {
2239 // Put the renderer in view source mode.
2240 render_view_host->Send(
2241 new ViewMsg_EnableViewSourceMode(render_view_host->routing_id()));
2242 }
[email protected]0666aef2009-05-13 19:48:082243
2244 view()->RenderViewCreated(render_view_host);
[email protected]420ae012009-04-24 05:16:322245}
2246
2247void TabContents::RenderViewReady(RenderViewHost* rvh) {
2248 if (rvh != render_view_host()) {
2249 // Don't notify the world, since this came from a renderer in the
2250 // background.
2251 return;
2252 }
2253
2254 NotifyConnected();
[email protected]9d8a4642009-07-29 17:25:302255 bool was_crashed = is_crashed();
[email protected]420ae012009-04-24 05:16:322256 SetIsCrashed(false);
[email protected]9d8a4642009-07-29 17:25:302257
2258 // Restore the focus to the tab (otherwise the focus will be on the top
2259 // window).
2260 if (was_crashed && !FocusLocationBarByDefault())
2261 Focus();
[email protected]420ae012009-04-24 05:16:322262}
2263
2264void TabContents::RenderViewGone(RenderViewHost* rvh) {
2265 // Ask the print preview if this renderer was valuable.
2266 if (!printing_.OnRenderViewGone(rvh))
2267 return;
2268 if (rvh != render_view_host()) {
2269 // The pending page's RenderViewHost is gone.
2270 return;
2271 }
2272
2273 SetIsLoading(false, NULL);
2274 NotifyDisconnected();
2275 SetIsCrashed(true);
2276
[email protected]ff9efc62010-02-18 22:10:542277 // Remove all infobars.
[email protected]e5430c92010-02-18 22:39:212278 for (int i = infobar_delegate_count() - 1; i >=0 ; --i)
[email protected]ff9efc62010-02-18 22:10:542279 RemoveInfoBar(GetInfoBarDelegateAt(i));
2280
[email protected]8e537302009-06-10 21:19:582281 // Tell the view that we've crashed so it can prepare the sad tab page.
[email protected]cf68b092010-01-15 20:05:552282 // Only do this if we're not in browser shutdown, so that TabContents
2283 // objects that are not in a browser (e.g., HTML dialogs) and thus are
2284 // visible do not flash a sad tab page.
2285 if (browser_shutdown::GetShutdownType() == browser_shutdown::NOT_VALID)
2286 view_->OnTabCrashed();
[email protected]420ae012009-04-24 05:16:322287
2288 // Hide any visible hung renderer warning for this web contents' process.
[email protected]e7b8d832009-11-20 22:41:002289 hung_renderer_dialog::HideForTabContents(this);
[email protected]420ae012009-04-24 05:16:322290}
2291
[email protected]2e4633c2009-07-09 16:58:062292void TabContents::RenderViewDeleted(RenderViewHost* rvh) {
2293 NotificationService::current()->Notify(
2294 NotificationType::RENDER_VIEW_HOST_DELETED,
2295 Source<TabContents>(this),
2296 Details<RenderViewHost>(rvh));
[email protected]fb7b79f72009-11-06 18:00:482297 render_manager_.RenderViewDeleted(rvh);
[email protected]2e4633c2009-07-09 16:58:062298}
2299
[email protected]420ae012009-04-24 05:16:322300void TabContents::DidNavigate(RenderViewHost* rvh,
2301 const ViewHostMsg_FrameNavigate_Params& params) {
[email protected]8030f012009-09-25 18:09:372302 int extra_invalidate_flags = 0;
2303
[email protected]70102972010-05-06 14:49:402304 requested_accessibility_tree_ = false;
2305
[email protected]8030f012009-09-25 18:09:372306 if (PageTransition::IsMainFrame(params.transition)) {
[email protected]20ede072009-10-06 00:28:352307 bool was_bookmark_bar_visible = ShouldShowBookmarkBar();
[email protected]8030f012009-09-25 18:09:372308 bool was_extension_shelf_visible = IsExtensionShelfAlwaysVisible();
2309
[email protected]420ae012009-04-24 05:16:322310 render_manager_.DidNavigateMainFrame(rvh);
2311
[email protected]20ede072009-10-06 00:28:352312 if (was_bookmark_bar_visible != ShouldShowBookmarkBar())
[email protected]8030f012009-09-25 18:09:372313 extra_invalidate_flags |= INVALIDATE_BOOKMARK_BAR;
2314 if (was_extension_shelf_visible != IsExtensionShelfAlwaysVisible())
2315 extra_invalidate_flags |= INVALIDATE_EXTENSION_SHELF;
2316 }
2317
[email protected]420ae012009-04-24 05:16:322318 // Update the site of the SiteInstance if it doesn't have one yet.
2319 if (!GetSiteInstance()->has_site())
2320 GetSiteInstance()->SetSite(params.url);
2321
2322 // Need to update MIME type here because it's referred to in
2323 // UpdateNavigationCommands() called by RendererDidNavigate() to
2324 // determine whether or not to enable the encoding menu.
2325 // It's updated only for the main frame. For a subframe,
2326 // RenderView::UpdateURL does not set params.contents_mime_type.
2327 // (see http://code.google.com/p/chromium/issues/detail?id=2929 )
2328 // TODO(jungshik): Add a test for the encoding menu to avoid
2329 // regressing it again.
2330 if (PageTransition::IsMainFrame(params.transition))
2331 contents_mime_type_ = params.contents_mime_type;
2332
2333 NavigationController::LoadCommittedDetails details;
[email protected]8030f012009-09-25 18:09:372334 bool did_navigate = controller_.RendererDidNavigate(
2335 params, extra_invalidate_flags, &details);
[email protected]3e90d4a2009-07-03 17:38:392336
2337 // Update history. Note that this needs to happen after the entry is complete,
2338 // which WillNavigate[Main,Sub]Frame will do before this function is called.
[email protected]2e29d922009-10-22 22:13:352339 if (params.should_update_history &&
2340 (!delegate() || delegate()->ShouldAddNavigationsToHistory())) {
[email protected]3e90d4a2009-07-03 17:38:392341 // Most of the time, the displayURL matches the loaded URL, but for about:
2342 // URLs, we use a data: URL as the real value. We actually want to save
2343 // the about: URL to the history db and keep the data: URL hidden. This is
2344 // what the TabContents' URL getter does.
2345 UpdateHistoryForNavigation(GetURL(), details, params);
2346 }
2347
[email protected]76543b92009-08-31 17:27:452348 if (!did_navigate)
2349 return; // No navigation happened.
[email protected]420ae012009-04-24 05:16:322350
2351 // DO NOT ADD MORE STUFF TO THIS FUNCTION! Your component should either listen
2352 // for the appropriate notification (best) or you can add it to
2353 // DidNavigateMainFramePostCommit / DidNavigateAnyFramePostCommit (only if
2354 // necessary, please).
2355
2356 // Run post-commit tasks.
2357 if (details.is_main_frame)
2358 DidNavigateMainFramePostCommit(details, params);
2359 DidNavigateAnyFramePostCommit(rvh, details, params);
2360}
2361
2362void TabContents::UpdateState(RenderViewHost* rvh,
2363 int32 page_id,
2364 const std::string& state) {
2365 DCHECK(rvh == render_view_host());
2366
2367 // We must be prepared to handle state updates for any page, these occur
2368 // when the user is scrolling and entering form data, as well as when we're
2369 // leaving a page, in which case our state may have already been moved to
2370 // the next page. The navigation controller will look up the appropriate
2371 // NavigationEntry and update it when it is notified via the delegate.
2372
2373 int entry_index = controller_.GetEntryIndexWithPageID(
2374 GetSiteInstance(), page_id);
2375 if (entry_index < 0)
2376 return;
2377 NavigationEntry* entry = controller_.GetEntryAtIndex(entry_index);
2378
2379 if (state == entry->content_state())
2380 return; // Nothing to update.
2381 entry->set_content_state(state);
2382 controller_.NotifyEntryChanged(entry, entry_index);
2383}
2384
2385void TabContents::UpdateTitle(RenderViewHost* rvh,
2386 int32 page_id, const std::wstring& title) {
2387 // If we have a title, that's a pretty good indication that we've started
2388 // getting useful data.
2389 SetNotWaitingForResponse();
2390
2391 DCHECK(rvh == render_view_host());
2392 NavigationEntry* entry = controller_.GetEntryWithPageID(GetSiteInstance(),
2393 page_id);
2394 if (!entry || !UpdateTitleForEntry(entry, title))
2395 return;
2396
2397 // Broadcast notifications when the UI should be updated.
2398 if (entry == controller_.GetEntryAtOffset(0))
[email protected]f1cd5e82009-10-23 17:19:032399 NotifyNavigationStateChanged(INVALIDATE_TITLE);
[email protected]420ae012009-04-24 05:16:322400}
2401
[email protected]420ae012009-04-24 05:16:322402void TabContents::UpdateEncoding(RenderViewHost* render_view_host,
[email protected]41fc0322009-09-04 22:23:402403 const std::string& encoding) {
[email protected]420ae012009-04-24 05:16:322404 set_encoding(encoding);
2405}
2406
2407void TabContents::UpdateTargetURL(int32 page_id, const GURL& url) {
2408 if (delegate())
2409 delegate()->UpdateTargetURL(this, url);
2410}
2411
2412void TabContents::UpdateThumbnail(const GURL& url,
2413 const SkBitmap& bitmap,
2414 const ThumbnailScore& score) {
2415 // Tell History about this thumbnail
[email protected]449478302009-06-09 20:04:282416 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kThumbnailStore)) {
[email protected]72d0fc72009-07-09 17:30:492417 if (!profile()->IsOffTheRecord())
[email protected]42339402009-07-17 17:27:192418 profile()->GetThumbnailStore()->SetPageThumbnail(url, bitmap,
2419 score, true);
[email protected]449478302009-06-09 20:04:282420 } else {
2421 HistoryService* hs;
2422 if (!profile()->IsOffTheRecord() &&
2423 (hs = profile()->GetHistoryService(Profile::IMPLICIT_ACCESS))) {
2424 hs->SetPageThumbnail(url, bitmap, score);
2425 }
[email protected]420ae012009-04-24 05:16:322426 }
2427}
2428
[email protected]8922e1f2009-10-03 05:01:262429void TabContents::UpdateInspectorSettings(const std::string& raw_settings) {
[email protected]0df30122009-06-03 12:13:082430 profile()->GetPrefs()->SetString(prefs::kWebKitInspectorSettings,
[email protected]8922e1f2009-10-03 05:01:262431 UTF8ToWide(raw_settings));
[email protected]0df30122009-06-03 12:13:082432}
2433
[email protected]420ae012009-04-24 05:16:322434void TabContents::Close(RenderViewHost* rvh) {
[email protected]07707302009-11-06 00:50:292435 // The UI may be in an event-tracking loop, such as between the
2436 // mouse-down and mouse-up in text selection or a button click.
2437 // Defer the close until after tracking is complete, so that we
2438 // don't free objects out from under the UI.
2439 // TODO(shess): This could probably be integrated with the
2440 // IsDoingDrag() test below. Punting for now because I need more
2441 // research to understand how this impacts platforms other than Mac.
2442 // TODO(shess): This could get more fine-grained. For instance,
2443 // closing a tab in another window while selecting text in the
2444 // current window's Omnibox should be just fine.
2445 if (view()->IsEventTracking()) {
2446 view()->CloseTabAfterEventTracking();
2447 return;
2448 }
2449
[email protected]24a4d1062009-07-10 23:10:422450 // If we close the tab while we're in the middle of a drag, we'll crash.
2451 // Instead, cancel the drag and close it as soon as the drag ends.
2452 if (view()->IsDoingDrag()) {
2453 view()->CancelDragAndCloseTab();
2454 return;
2455 }
2456
[email protected]420ae012009-04-24 05:16:322457 // Ignore this if it comes from a RenderViewHost that we aren't showing.
2458 if (delegate() && rvh == render_view_host())
2459 delegate()->CloseContents(this);
2460}
2461
2462void TabContents::RequestMove(const gfx::Rect& new_bounds) {
2463 if (delegate() && delegate()->IsPopup(this))
2464 delegate()->MoveContents(this, new_bounds);
2465}
2466
[email protected]7ab1e7d62009-10-14 23:32:012467void TabContents::DidStartLoading() {
[email protected]420ae012009-04-24 05:16:322468 SetIsLoading(true, NULL);
2469}
2470
[email protected]7ab1e7d62009-10-14 23:32:012471void TabContents::DidStopLoading() {
[email protected]420ae012009-04-24 05:16:322472 scoped_ptr<LoadNotificationDetails> details;
2473
2474 NavigationEntry* entry = controller_.GetActiveEntry();
2475 // An entry may not exist for a stop when loading an initial blank page or
2476 // if an iframe injected by script into a blank page finishes loading.
2477 if (entry) {
[email protected]420ae012009-04-24 05:16:322478 base::TimeDelta elapsed = base::TimeTicks::Now() - current_load_start_;
2479
2480 details.reset(new LoadNotificationDetails(
[email protected]ebe89e062009-08-13 23:16:542481 entry->virtual_url(),
[email protected]420ae012009-04-24 05:16:322482 entry->transition_type(),
2483 elapsed,
2484 &controller_,
2485 controller_.GetCurrentEntryIndex()));
2486 }
2487
2488 // Tell PasswordManager we've finished a page load, which serves as a
2489 // green light to save pending passwords and reset itself.
2490 GetPasswordManager()->DidStopLoading();
2491
2492 SetIsLoading(false, details.get());
2493}
2494
[email protected]420ae012009-04-24 05:16:322495void TabContents::DidRedirectProvisionalLoad(int32 page_id,
2496 const GURL& source_url,
2497 const GURL& target_url) {
2498 NavigationEntry* entry;
2499 if (page_id == -1)
2500 entry = controller_.pending_entry();
2501 else
2502 entry = controller_.GetEntryWithPageID(GetSiteInstance(), page_id);
2503 if (!entry || entry->url() != source_url)
2504 return;
2505 entry->set_url(target_url);
2506}
2507
[email protected]420ae012009-04-24 05:16:322508void TabContents::RequestOpenURL(const GURL& url, const GURL& referrer,
2509 WindowOpenDisposition disposition) {
2510 if (render_manager_.dom_ui()) {
2511 // When we're a DOM UI, it will provide a page transition type for us (this
2512 // is so the new tab page can specify AUTO_BOOKMARK for automatically
2513 // generated suggestions).
2514 //
2515 // Note also that we hide the referrer for DOM UI pages. We don't really
[email protected]60e448982009-05-06 04:21:162516 // want web sites to see a referrer of "chrome://blah" (and some
2517 // chrome: URLs might have search terms or other stuff we don't want to
[email protected]420ae012009-04-24 05:16:322518 // send to the site), so we send no referrer.
2519 OpenURL(url, GURL(), disposition,
2520 render_manager_.dom_ui()->link_transition_type());
2521 } else {
2522 OpenURL(url, referrer, disposition, PageTransition::LINK);
2523 }
2524}
2525
[email protected]1ee614862009-07-06 20:29:182526void TabContents::DomOperationResponse(const std::string& json_string,
2527 int automation_id) {
2528}
2529
[email protected]420ae012009-04-24 05:16:322530void TabContents::ProcessDOMUIMessage(const std::string& message,
[email protected]e4dad9fb2009-10-06 18:15:582531 const Value* content,
[email protected]bb64b512010-04-02 21:01:392532 const GURL& source_url,
[email protected]811bfe32009-07-01 08:46:252533 int request_id,
2534 bool has_callback) {
[email protected]420ae012009-04-24 05:16:322535 if (!render_manager_.dom_ui()) {
[email protected]c2e74fe82009-09-03 17:57:442536 // This can happen if someone uses window.open() to open an extension URL
2537 // from a non-extension context.
2538 render_view_host()->BlockExtensionRequest(request_id);
[email protected]420ae012009-04-24 05:16:322539 return;
2540 }
[email protected]bb64b512010-04-02 21:01:392541 render_manager_.dom_ui()->ProcessDOMUIMessage(message, content, source_url,
2542 request_id,
[email protected]811bfe32009-07-01 08:46:252543 has_callback);
[email protected]420ae012009-04-24 05:16:322544}
2545
2546void TabContents::ProcessExternalHostMessage(const std::string& message,
2547 const std::string& origin,
2548 const std::string& target) {
2549 if (delegate())
2550 delegate()->ForwardMessageToExternalHost(message, origin, target);
2551}
2552
[email protected]cdaf8d02010-03-30 19:52:472553void TabContents::RunFileChooser(
2554 const ViewHostMsg_RunFileChooser_Params &params) {
[email protected]420ae012009-04-24 05:16:322555 if (!select_file_dialog_.get())
2556 select_file_dialog_ = SelectFileDialog::Create(this);
[email protected]cdaf8d02010-03-30 19:52:472557
2558 SelectFileDialog::Type dialog_type;
2559 switch (params.mode) {
2560 case ViewHostMsg_RunFileChooser_Params::Open:
2561 dialog_type = SelectFileDialog::SELECT_OPEN_FILE;
2562 break;
2563 case ViewHostMsg_RunFileChooser_Params::OpenMultiple:
2564 dialog_type = SelectFileDialog::SELECT_OPEN_MULTI_FILE;
2565 break;
2566 case ViewHostMsg_RunFileChooser_Params::Save:
2567 dialog_type = SelectFileDialog::SELECT_SAVEAS_FILE;
2568 break;
2569 default:
[email protected]bed31952010-03-30 20:07:592570 dialog_type = SelectFileDialog::SELECT_OPEN_FILE; // Prevent warning.
[email protected]cdaf8d02010-03-30 19:52:472571 NOTREACHED();
2572 }
2573 select_file_dialog_->SelectFile(dialog_type, params.title,
2574 params.default_file_name,
[email protected]420ae012009-04-24 05:16:322575 NULL, 0, FILE_PATH_LITERAL(""),
2576 view_->GetTopLevelNativeWindow(), NULL);
2577}
2578
2579void TabContents::RunJavaScriptMessage(
2580 const std::wstring& message,
2581 const std::wstring& default_prompt,
2582 const GURL& frame_url,
2583 const int flags,
2584 IPC::Message* reply_msg,
2585 bool* did_suppress_message) {
2586 // Suppress javascript messages when requested and when inside a constrained
2587 // popup window (because that activates them and breaks them out of the
2588 // constrained window jail).
[email protected]91ed49a2009-07-07 21:29:552589 // Also suppress messages when showing an interstitial. The interstitial is
2590 // shown over the previous page, we don't want the hidden page dialogs to
2591 // interfere with the interstitial.
2592 bool suppress_this_message = suppress_javascript_messages_ ||
2593 showing_interstitial_page();
[email protected]420ae012009-04-24 05:16:322594 if (delegate())
2595 suppress_this_message |=
[email protected]b6c874582009-05-08 19:38:312596 (delegate()->GetConstrainingContents(this) != this);
[email protected]420ae012009-04-24 05:16:322597
2598 *did_suppress_message = suppress_this_message;
2599
2600 if (!suppress_this_message) {
2601 base::TimeDelta time_since_last_message(
2602 base::TimeTicks::Now() - last_javascript_message_dismissal_);
2603 bool show_suppress_checkbox = false;
2604 // Show a checkbox offering to suppress further messages if this message is
2605 // being displayed within kJavascriptMessageExpectedDelay of the last one.
2606 if (time_since_last_message <
2607 base::TimeDelta::FromMilliseconds(kJavascriptMessageExpectedDelay))
2608 show_suppress_checkbox = true;
2609
[email protected]d82ed61e2009-06-16 02:46:222610 RunJavascriptMessageBox(this, frame_url, flags, message, default_prompt,
[email protected]420ae012009-04-24 05:16:322611 show_suppress_checkbox, reply_msg);
2612 } else {
2613 // If we are suppressing messages, just reply as is if the user immediately
2614 // pressed "Cancel".
[email protected]beb440c2009-11-06 04:08:542615 OnMessageBoxClosed(reply_msg, false, std::wstring());
[email protected]420ae012009-04-24 05:16:322616 }
2617}
2618
2619void TabContents::RunBeforeUnloadConfirm(const std::wstring& message,
2620 IPC::Message* reply_msg) {
[email protected]7ab1e7d62009-10-14 23:32:012621 is_showing_before_unload_dialog_ = true;
[email protected]d82ed61e2009-06-16 02:46:222622 RunBeforeUnloadDialog(this, message, reply_msg);
[email protected]420ae012009-04-24 05:16:322623}
2624
2625void TabContents::ShowModalHTMLDialog(const GURL& url, int width, int height,
2626 const std::string& json_arguments,
2627 IPC::Message* reply_msg) {
2628 if (delegate()) {
2629 HtmlDialogUIDelegate* dialog_delegate =
2630 new ModalHtmlDialogDelegate(url, width, height, json_arguments,
[email protected]d82ed61e2009-06-16 02:46:222631 reply_msg, this);
[email protected]420ae012009-04-24 05:16:322632 delegate()->ShowHtmlDialog(dialog_delegate, NULL);
2633 }
2634}
2635
[email protected]4d44a1c2010-04-28 19:20:412636void TabContents::PasswordFormsFound(
[email protected]ed3fb032009-06-16 19:50:562637 const std::vector<webkit_glue::PasswordForm>& forms) {
[email protected]4d44a1c2010-04-28 19:20:412638 GetPasswordManager()->PasswordFormsFound(forms);
2639}
2640
2641void TabContents::PasswordFormsVisible(
2642 const std::vector<webkit_glue::PasswordForm>& visible_forms) {
2643 GetPasswordManager()->PasswordFormsVisible(visible_forms);
[email protected]420ae012009-04-24 05:16:322644}
2645
[email protected]420ae012009-04-24 05:16:322646// Checks to see if we should generate a keyword based on the OSDD, and if
2647// necessary uses TemplateURLFetcher to download the OSDD and create a keyword.
2648void TabContents::PageHasOSDD(RenderViewHost* render_view_host,
2649 int32 page_id, const GURL& url,
2650 bool autodetected) {
2651 // Make sure page_id is the current page, and the TemplateURLModel is loaded.
2652 DCHECK(url.is_valid());
2653 if (!IsActiveEntry(page_id))
2654 return;
2655 TemplateURLModel* url_model = profile()->GetTemplateURLModel();
2656 if (!url_model)
2657 return;
2658 if (!url_model->loaded()) {
2659 url_model->Load();
2660 return;
2661 }
2662 if (!profile()->GetTemplateURLFetcher())
2663 return;
2664
2665 if (profile()->IsOffTheRecord())
2666 return;
2667
2668 const NavigationEntry* entry = controller_.GetLastCommittedEntry();
2669 DCHECK(entry);
2670
2671 const NavigationEntry* base_entry = entry;
2672 if (IsFormSubmit(base_entry)) {
2673 // If the current page is a form submit, find the last page that was not
2674 // a form submit and use its url to generate the keyword from.
2675 int index = controller_.last_committed_entry_index() - 1;
2676 while (index >= 0 && IsFormSubmit(controller_.GetEntryAtIndex(index)))
2677 index--;
2678 if (index >= 0)
2679 base_entry = controller_.GetEntryAtIndex(index);
2680 else
2681 base_entry = NULL;
2682 }
2683
2684 // We want to use the user typed URL if available since that represents what
2685 // the user typed to get here, and fall back on the regular URL if not.
2686 if (!base_entry)
2687 return;
2688 GURL keyword_url = base_entry->user_typed_url().is_valid() ?
2689 base_entry->user_typed_url() : base_entry->url();
2690 if (!keyword_url.is_valid())
2691 return;
2692 std::wstring keyword = TemplateURLModel::GenerateKeyword(keyword_url,
2693 autodetected);
[email protected]a88dbdd52010-01-15 19:25:532694
2695 // For JS added OSDD empty keyword is OK because we will generate keyword
2696 // later from OSDD content.
2697 if (autodetected && keyword.empty())
[email protected]420ae012009-04-24 05:16:322698 return;
2699 const TemplateURL* template_url =
2700 url_model->GetTemplateURLForKeyword(keyword);
2701 if (template_url && (!template_url->safe_for_autoreplace() ||
2702 template_url->originating_url() == url)) {
2703 // Either there is a user created TemplateURL for this keyword, or the
2704 // keyword has the same OSDD url and we've parsed it.
2705 return;
2706 }
2707
2708 // Download the OpenSearch description document. If this is successful a
2709 // new keyword will be created when done.
[email protected]420ae012009-04-24 05:16:322710 profile()->GetTemplateURLFetcher()->ScheduleDownload(
2711 keyword,
2712 url,
2713 base_entry->favicon().url(),
[email protected]d0cc9fb52009-06-23 02:26:162714 this,
[email protected]420ae012009-04-24 05:16:322715 autodetected);
2716}
2717
[email protected]420ae012009-04-24 05:16:322718GURL TabContents::GetAlternateErrorPageURL() const {
2719 GURL url;
2720 // Disable alternate error pages when in OffTheRecord/Incognito mode.
2721 if (profile()->IsOffTheRecord())
2722 return url;
2723
2724 PrefService* prefs = profile()->GetPrefs();
2725 DCHECK(prefs);
2726 if (prefs->GetBoolean(prefs::kAlternateErrorPagesEnabled)) {
[email protected]a33457b82009-09-21 23:07:152727 url = google_util::AppendGoogleLocaleParam(
2728 GURL(google_util::kLinkDoctorBaseURL));
[email protected]420ae012009-04-24 05:16:322729 url = google_util::AppendGoogleTLDParam(url);
2730 }
2731 return url;
2732}
2733
2734WebPreferences TabContents::GetWebkitPrefs() {
[email protected]6c32ce72010-03-08 05:18:062735 Profile* profile = render_view_host()->process()->profile();
[email protected]7ccddb8c2009-08-04 17:36:552736 bool is_dom_ui = false;
[email protected]6c32ce72010-03-08 05:18:062737 return RenderViewHostDelegateHelper::GetWebkitPrefs(profile, is_dom_ui);
[email protected]420ae012009-04-24 05:16:322738}
2739
[email protected]fa1cf0b82010-01-15 21:49:442740void TabContents::OnIgnoredUIEvent() {
2741 if (constrained_window_count()) {
2742 ConstrainedWindow* window = *constrained_window_begin();
2743 window->FocusConstrainedWindow();
2744 }
2745}
2746
[email protected]420ae012009-04-24 05:16:322747void TabContents::OnJSOutOfMemory() {
2748 AddInfoBar(new SimpleAlertInfoBarDelegate(
[email protected]938e1f92010-04-01 18:09:422749 this, l10n_util::GetString(IDS_JS_OUT_OF_MEMORY_PROMPT), NULL, true));
[email protected]420ae012009-04-24 05:16:322750}
2751
[email protected]420ae012009-04-24 05:16:322752void TabContents::OnCrossSiteResponse(int new_render_process_host_id,
2753 int new_request_id) {
[email protected]57c6a652009-05-04 07:58:342754 // Allows the TabContents to react when a cross-site response is ready to be
[email protected]420ae012009-04-24 05:16:322755 // delivered to a pending RenderViewHost. We must first run the onunload
2756 // handler of the old RenderViewHost before we can allow it to proceed.
2757 render_manager_.OnCrossSiteResponse(new_render_process_host_id,
2758 new_request_id);
2759}
2760
2761bool TabContents::CanBlur() const {
2762 return delegate() ? delegate()->CanBlur() : true;
2763}
2764
2765gfx::Rect TabContents::GetRootWindowResizerRect() const {
2766 if (delegate())
2767 return delegate()->GetRootWindowResizerRect();
2768 return gfx::Rect();
2769}
2770
2771void TabContents::RendererUnresponsive(RenderViewHost* rvh,
2772 bool is_during_unload) {
2773 if (is_during_unload) {
2774 // Hang occurred while firing the beforeunload/unload handler.
2775 // Pretend the handler fired so tab closing continues as if it had.
[email protected]829e7612009-04-25 01:15:112776 rvh->set_sudden_termination_allowed(true);
[email protected]420ae012009-04-24 05:16:322777
2778 if (!render_manager_.ShouldCloseTabOnUnresponsiveRenderer())
2779 return;
2780
2781 // If the tab hangs in the beforeunload/unload handler there's really
2782 // nothing we can do to recover. Pretend the unload listeners have
2783 // all fired and close the tab. If the hang is in the beforeunload handler
2784 // then the user will not have the option of cancelling the close.
2785 Close(rvh);
2786 return;
2787 }
2788
2789 if (render_view_host() && render_view_host()->IsRenderViewLive())
[email protected]e7b8d832009-11-20 22:41:002790 hung_renderer_dialog::ShowForTabContents(this);
[email protected]420ae012009-04-24 05:16:322791}
2792
2793void TabContents::RendererResponsive(RenderViewHost* render_view_host) {
[email protected]e7b8d832009-11-20 22:41:002794 hung_renderer_dialog::HideForTabContents(this);
[email protected]420ae012009-04-24 05:16:322795}
2796
2797void TabContents::LoadStateChanged(const GURL& url,
[email protected]094e5b22009-09-25 04:23:562798 net::LoadState load_state,
2799 uint64 upload_position,
2800 uint64 upload_size) {
[email protected]420ae012009-04-24 05:16:322801 load_state_ = load_state;
[email protected]094e5b22009-09-25 04:23:562802 upload_position_ = upload_position;
2803 upload_size_ = upload_size;
[email protected]d686e812009-06-03 19:10:292804 std::wstring languages =
2805 profile()->GetPrefs()->GetString(prefs::kAcceptLanguages);
[email protected]d686e812009-06-03 19:10:292806 std::string host = url.host();
[email protected]ce85f602009-11-07 01:34:532807 load_state_host_ =
2808 net::IDNToUnicode(host.c_str(), host.size(), languages, NULL);
[email protected]420ae012009-04-24 05:16:322809 if (load_state_ == net::LOAD_STATE_READING_RESPONSE)
2810 SetNotWaitingForResponse();
2811 if (is_loading())
[email protected]c9cd2222009-05-06 05:16:502812 NotifyNavigationStateChanged(INVALIDATE_LOAD | INVALIDATE_TAB);
[email protected]420ae012009-04-24 05:16:322813}
2814
[email protected]1ee614862009-07-06 20:29:182815bool TabContents::IsExternalTabContainer() const {
2816 if (!delegate())
2817 return false;
2818
2819 return delegate()->IsExternalTabContainer();
2820}
2821
[email protected]1ee614862009-07-06 20:29:182822void TabContents::DidInsertCSS() {
2823 // This RVHDelegate function is used for extensions and not us.
[email protected]420ae012009-04-24 05:16:322824}
2825
[email protected]130efb02009-09-18 18:54:352826void TabContents::FocusedNodeChanged() {
2827 NotificationService::current()->Notify(
2828 NotificationType::FOCUS_CHANGED_IN_PAGE,
2829 Source<RenderViewHost>(render_view_host()),
2830 NotificationService::NoDetails());
2831}
2832
[email protected]420ae012009-04-24 05:16:322833void TabContents::FileSelected(const FilePath& path,
2834 int index, void* params) {
[email protected]cdaf8d02010-03-30 19:52:472835 std::vector<FilePath> files;
2836 files.push_back(path);
2837 render_view_host()->FilesSelectedInChooser(files);
[email protected]420ae012009-04-24 05:16:322838}
2839
2840void TabContents::MultiFilesSelected(const std::vector<FilePath>& files,
2841 void* params) {
[email protected]cdaf8d02010-03-30 19:52:472842 render_view_host()->FilesSelectedInChooser(files);
[email protected]420ae012009-04-24 05:16:322843}
2844
2845void TabContents::FileSelectionCanceled(void* params) {
2846 // If the user cancels choosing a file to upload we pass back an
2847 // empty vector.
[email protected]cdaf8d02010-03-30 19:52:472848 render_view_host()->FilesSelectedInChooser(std::vector<FilePath>());
[email protected]420ae012009-04-24 05:16:322849}
2850
2851void TabContents::BeforeUnloadFiredFromRenderManager(
2852 bool proceed,
2853 bool* proceed_to_fire_unload) {
2854 if (delegate())
2855 delegate()->BeforeUnloadFired(this, proceed, proceed_to_fire_unload);
2856}
2857
2858void TabContents::UpdateRenderViewSizeForRenderManager() {
2859 // TODO(brettw) this is a hack. See TabContentsView::SizeContents.
[email protected]0323ee42010-02-17 22:03:262860 gfx::Size size = view_->GetContainerSize();
2861 // 0x0 isn't a valid window size (minimal window size is 1x1) but it may be
2862 // here during container initialization and normal window size will be set
2863 // later. In case of tab duplication this resizing to 0x0 prevents setting
2864 // normal size later so just ignore it.
2865 if (!size.IsEmpty())
2866 view_->SizeContents(size);
[email protected]420ae012009-04-24 05:16:322867}
2868
2869DOMUI* TabContents::CreateDOMUIForRenderManager(const GURL& url) {
[email protected]d82ed61e2009-06-16 02:46:222870 return DOMUIFactory::CreateDOMUIForURL(this, url);
[email protected]420ae012009-04-24 05:16:322871}
2872
2873NavigationEntry*
2874TabContents::GetLastCommittedNavigationEntryForRenderManager() {
2875 return controller_.GetLastCommittedEntry();
2876}
2877
2878bool TabContents::CreateRenderViewForRenderManager(
2879 RenderViewHost* render_view_host) {
[email protected]420ae012009-04-24 05:16:322880 RenderWidgetHostView* rwh_view = view_->CreateViewForWidget(render_view_host);
[email protected]1a98a932009-11-17 00:12:522881
2882 scoped_refptr<URLRequestContextGetter> request_context = request_context_;
2883 if (!request_context.get())
2884 request_context = profile()->GetRequestContext();
2885
2886 if (!render_view_host->CreateRenderView(request_context))
[email protected]420ae012009-04-24 05:16:322887 return false;
2888
2889 // Now that the RenderView has been created, we need to tell it its size.
2890 rwh_view->SetSize(view_->GetContainerSize());
2891
2892 UpdateMaxPageIDIfNecessary(render_view_host->site_instance(),
2893 render_view_host);
2894 return true;
2895}
2896
2897void TabContents::Observe(NotificationType type,
2898 const NotificationSource& source,
2899 const NotificationDetails& details) {
2900 switch (type.value) {
2901 case NotificationType::BOOKMARK_MODEL_LOADED:
2902 // BookmarkModel finished loading, fall through to update starred state.
2903 case NotificationType::URLS_STARRED: {
2904 // Somewhere, a URL has been starred.
2905 // Ignore notifications for profiles other than our current one.
2906 Profile* source_profile = Source<Profile>(source).ptr();
2907 if (!source_profile || !source_profile->IsSameProfile(profile()))
2908 return;
2909
2910 UpdateStarredStateForCurrentURL();
2911 break;
2912 }
2913 case NotificationType::PREF_CHANGED: {
2914 std::wstring* pref_name_in = Details<std::wstring>(details).ptr();
2915 DCHECK(Source<PrefService>(source).ptr() == profile()->GetPrefs());
2916 if (*pref_name_in == prefs::kAlternateErrorPagesEnabled) {
2917 UpdateAlternateErrorPageURL();
2918 } else if (*pref_name_in == prefs::kDefaultCharset ||
2919 StartsWithASCII(WideToUTF8(*pref_name_in), "webkit.webprefs.", true)
2920 ) {
2921 UpdateWebPreferences();
2922 } else {
2923 NOTREACHED() << "unexpected pref change notification" << *pref_name_in;
2924 }
2925 break;
2926 }
2927 case NotificationType::RENDER_WIDGET_HOST_DESTROYED:
2928 view_->RenderWidgetHostDestroyed(Source<RenderWidgetHost>(source).ptr());
2929 break;
2930
2931 case NotificationType::NAV_ENTRY_COMMITTED: {
2932 DCHECK(&controller_ == Source<NavigationController>(source).ptr());
2933
2934 NavigationController::LoadCommittedDetails& committed_details =
2935 *(Details<NavigationController::LoadCommittedDetails>(details).ptr());
2936 ExpireInfoBars(committed_details);
2937 break;
2938 }
2939
[email protected]d3ba77272009-09-03 00:06:092940#if defined(OS_LINUX)
2941 case NotificationType::BROWSER_THEME_CHANGED: {
[email protected]7ff431e2010-01-07 18:18:542942 renderer_preferences_util::UpdateFromSystemSettings(
2943 &renderer_preferences_, profile());
[email protected]d3ba77272009-09-03 00:06:092944 render_view_host()->SyncRendererPrefs();
2945 break;
2946 }
2947#endif
2948
[email protected]6c32ce72010-03-08 05:18:062949 case NotificationType::USER_STYLE_SHEET_UPDATED:
2950 UpdateWebPreferences();
2951 break;
2952
[email protected]79580c62010-02-02 02:36:252953 case NotificationType::CONTENT_SETTINGS_CHANGED: {
[email protected]0314ae02010-04-08 09:18:292954 Details<const HostContentSettingsMap::ContentSettingsDetails>
[email protected]79580c62010-02-02 02:36:252955 settings_details(details);
[email protected]79580c62010-02-02 02:36:252956 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]da0c8e92010-02-09 23:25:492957 GURL entry_url;
[email protected]0314ae02010-04-08 09:18:292958 if (entry)
[email protected]da0c8e92010-02-09 23:25:492959 entry_url = entry->url();
[email protected]79580c62010-02-02 02:36:252960 Source<HostContentSettingsMap> content_settings(source);
[email protected]0314ae02010-04-08 09:18:292961 if (settings_details.ptr()->update_all() ||
2962 settings_details.ptr()->pattern().Matches(entry_url)) {
2963 render_view_host()->SendContentSettings(entry_url,
[email protected]da0c8e92010-02-09 23:25:492964 content_settings.ptr()->GetContentSettings(entry_url));
[email protected]79580c62010-02-02 02:36:252965 }
2966 break;
2967 }
2968
[email protected]a888b29e62010-04-01 13:38:572969 case NotificationType::EXTENSION_LOADED: {
[email protected]98aa0b52010-05-06 17:03:082970 if (!extension_app_ && !extension_app_for_current_page_ &&
[email protected]a888b29e62010-04-01 13:38:572971 Source<Profile>(source).ptr() == profile()) {
[email protected]98aa0b52010-05-06 17:03:082972 UpdateExtensionAppForCurrentPage();
2973 if (extension_app_for_current_page_)
[email protected]a888b29e62010-04-01 13:38:572974 NotifyNavigationStateChanged(INVALIDATE_TAB);
2975 }
2976 break;
2977 }
2978
2979 case NotificationType::EXTENSION_UNLOADED:
2980 case NotificationType::EXTENSION_UNLOADED_DISABLED: {
[email protected]98aa0b52010-05-06 17:03:082981 if (extension_app_for_current_page_ ==
[email protected]a888b29e62010-04-01 13:38:572982 Details<Extension>(details).ptr()) {
[email protected]98aa0b52010-05-06 17:03:082983 extension_app_for_current_page_ = NULL;
2984 UpdateExtensionAppForCurrentPage();
[email protected]a888b29e62010-04-01 13:38:572985 NotifyNavigationStateChanged(INVALIDATE_TAB);
2986 }
2987 break;
2988 }
2989
[email protected]420ae012009-04-24 05:16:322990 default:
2991 NOTREACHED();
2992 }
2993}
[email protected]aacd7cc2009-09-02 14:51:102994
[email protected]98aa0b52010-05-06 17:03:082995void TabContents::UpdateExtensionAppIcon(Extension* extension) {
2996 extension_app_icon_.reset();
[email protected]f075fe0a22010-04-26 04:06:432997
[email protected]a888b29e62010-04-01 13:38:572998 if (extension) {
[email protected]98aa0b52010-05-06 17:03:082999 extension_app_image_loader_.reset(new ImageLoadingTracker(this));
3000 extension_app_image_loader_->LoadImage(
[email protected]a888b29e62010-04-01 13:38:573001 extension,
3002 extension->GetIconPath(Extension::EXTENSION_ICON_SMALLISH),
3003 gfx::Size(Extension::EXTENSION_ICON_SMALLISH,
3004 Extension::EXTENSION_ICON_SMALLISH),
3005 ImageLoadingTracker::CACHE);
3006 } else {
[email protected]98aa0b52010-05-06 17:03:083007 extension_app_image_loader_.reset(NULL);
[email protected]a888b29e62010-04-01 13:38:573008 }
3009}
3010
3011Extension* TabContents::GetExtensionContaining(const GURL& url) {
3012 ExtensionsService* extensions_service = profile()->GetExtensionsService();
3013 if (!extensions_service)
3014 return NULL;
3015
3016 Extension* extension = extensions_service->GetExtensionByURL(url);
3017 return extension ?
3018 extension : extensions_service->GetExtensionByWebExtent(url);
3019}
3020
[email protected]98aa0b52010-05-06 17:03:083021void TabContents::UpdateExtensionAppForCurrentPage() {
3022 if (extension_app_) {
[email protected]a888b29e62010-04-01 13:38:573023 // Tab has an explicit app extension; nothing to do.
3024 return;
3025 }
3026
3027 // Check the current extension before iterating through all extensions.
[email protected]98aa0b52010-05-06 17:03:083028 if (extension_app_for_current_page_ &&
3029 extension_app_for_current_page_->web_extent().ContainsURL(GetURL())) {
[email protected]a888b29e62010-04-01 13:38:573030 return;
3031 }
3032
[email protected]98aa0b52010-05-06 17:03:083033 extension_app_for_current_page_ = GetExtensionContaining(GetURL());
3034 UpdateExtensionAppIcon(extension_app_for_current_page_);
[email protected]a888b29e62010-04-01 13:38:573035}
3036
3037void TabContents::OnImageLoaded(SkBitmap* image, ExtensionResource resource,
3038 int index) {
3039 if (image) {
[email protected]98aa0b52010-05-06 17:03:083040 extension_app_icon_ = *image;
[email protected]a888b29e62010-04-01 13:38:573041 NotifyNavigationStateChanged(INVALIDATE_TAB);
3042 }
3043}
3044
[email protected]beb440c2009-11-06 04:08:543045std::wstring TabContents::GetMessageBoxTitle(const GURL& frame_url,
3046 bool is_alert) {
3047 if (!frame_url.has_host())
3048 return l10n_util::GetString(
3049 is_alert ? IDS_JAVASCRIPT_ALERT_DEFAULT_TITLE
3050 : IDS_JAVASCRIPT_MESSAGEBOX_DEFAULT_TITLE);
3051
3052 // We really only want the scheme, hostname, and port.
3053 GURL::Replacements replacements;
3054 replacements.ClearUsername();
3055 replacements.ClearPassword();
3056 replacements.ClearPath();
3057 replacements.ClearQuery();
3058 replacements.ClearRef();
3059 GURL clean_url = frame_url.ReplaceComponents(replacements);
3060
3061 // TODO(brettw) it should be easier than this to do the correct language
3062 // handling without getting the accept language from the profile.
3063 std::wstring base_address = gfx::ElideUrl(clean_url, gfx::Font(), 0,
3064 profile()->GetPrefs()->GetString(prefs::kAcceptLanguages));
3065 // Force URL to have LTR directionality.
[email protected]4a4afe32010-05-05 01:39:563066 base::i18n::GetDisplayStringInLTRDirectionality(&base_address);
[email protected]beb440c2009-11-06 04:08:543067
3068 return l10n_util::GetStringF(
3069 is_alert ? IDS_JAVASCRIPT_ALERT_TITLE : IDS_JAVASCRIPT_MESSAGEBOX_TITLE,
3070 base_address);
3071}
3072
3073gfx::NativeWindow TabContents::GetMessageBoxRootWindow() {
3074 return view_->GetTopLevelNativeWindow();
3075}
3076
3077void TabContents::OnMessageBoxClosed(IPC::Message* reply_msg,
3078 bool success,
3079 const std::wstring& prompt) {
3080 last_javascript_message_dismissal_ = base::TimeTicks::Now();
3081 if (is_showing_before_unload_dialog_ && !success) {
3082 // If a beforeunload dialog is canceled, we need to stop the throbber from
3083 // spinning, since we forced it to start spinning in Navigate.
3084 DidStopLoading();
3085
3086 tab_close_start_time_ = base::TimeTicks();
3087 }
3088 is_showing_before_unload_dialog_ = false;
3089 render_view_host()->JavaScriptMessageBoxClosed(reply_msg, success, prompt);
3090}
3091
3092void TabContents::SetSuppressMessageBoxes(bool suppress_message_boxes) {
3093 set_suppress_javascript_messages(suppress_message_boxes);
3094}
3095
[email protected]41fc0322009-09-04 22:23:403096void TabContents::set_encoding(const std::string& encoding) {
[email protected]aacd7cc2009-09-02 14:51:103097 encoding_ = CharacterEncoding::GetCanonicalEncodingNameByAliasName(encoding);
3098}
[email protected]f45d2a72010-03-08 23:28:353099
3100void TabContents::SetAppIcon(const SkBitmap& app_icon) {
3101 app_icon_ = app_icon;
3102 NotifyNavigationStateChanged(INVALIDATE_TITLE);
3103}
[email protected]ea68c06a2010-04-23 17:25:373104
3105// After a successful *new* login attempt, we take the PasswordFormManager in
3106// provisional_save_manager_ and move it to a SavePasswordInfoBarDelegate while
3107// the user makes up their mind with the "save password" infobar. Note if the
3108// login is one we already know about, the end of the line is
3109// provisional_save_manager_ because we just update it on success and so such
3110// forms never end up in an infobar.
3111class SavePasswordInfoBarDelegate : public ConfirmInfoBarDelegate {
3112 public:
3113 SavePasswordInfoBarDelegate(TabContents* tab_contents,
3114 PasswordFormManager* form_to_save) :
3115 ConfirmInfoBarDelegate(tab_contents),
3116 form_to_save_(form_to_save) {
3117 }
3118
3119 virtual ~SavePasswordInfoBarDelegate() { }
3120
3121 // Overridden from ConfirmInfoBarDelegate:
3122 virtual void InfoBarClosed() {
3123 delete this;
3124 }
3125
3126 virtual std::wstring GetMessageText() const {
3127 return l10n_util::GetString(IDS_PASSWORD_MANAGER_SAVE_PASSWORD_PROMPT);
3128 }
3129
3130 virtual SkBitmap* GetIcon() const {
3131 return ResourceBundle::GetSharedInstance().GetBitmapNamed(
3132 IDR_INFOBAR_SAVE_PASSWORD);
3133 }
3134
3135 virtual int GetButtons() const {
3136 return BUTTON_OK | BUTTON_CANCEL;
3137 }
3138
3139 virtual std::wstring GetButtonLabel(InfoBarButton button) const {
3140 if (button == BUTTON_OK)
3141 return l10n_util::GetString(IDS_PASSWORD_MANAGER_SAVE_BUTTON);
3142 if (button == BUTTON_CANCEL)
3143 return l10n_util::GetString(IDS_PASSWORD_MANAGER_BLACKLIST_BUTTON);
3144 NOTREACHED();
3145 return std::wstring();
3146 }
3147
3148 virtual bool Accept() {
3149 DCHECK(form_to_save_.get());
3150 form_to_save_->Save();
3151 return true;
3152 }
3153
3154 virtual bool Cancel() {
3155 DCHECK(form_to_save_.get());
3156 form_to_save_->PermanentlyBlacklist();
3157 return true;
3158 }
3159
3160 private:
3161 // The PasswordFormManager managing the form we're asking the user about,
3162 // and should update as per her decision.
3163 scoped_ptr<PasswordFormManager> form_to_save_;
3164
3165 DISALLOW_COPY_AND_ASSIGN(SavePasswordInfoBarDelegate);
3166};
3167
3168void TabContents::FillPasswordForm(
3169 const webkit_glue::PasswordFormDomManager::FillData& form_data) {
3170 render_view_host()->FillPasswordForm(form_data);
3171}
3172
3173void TabContents::AddSavePasswordInfoBar(PasswordFormManager* form_to_save) {
3174 AddInfoBar(new SavePasswordInfoBarDelegate(this, form_to_save));
3175}
3176
3177Profile* TabContents::GetProfileForPasswordManager() {
3178 return profile();
3179}
3180
3181bool TabContents::DidLastPageLoadEncounterSSLErrors() {
3182 return controller().ssl_manager()->ProcessedSSLErrorFromRequest();
3183}