blob: 5457a0d9073636fc8608bbcc85d47135e6d86e3d [file] [log] [blame]
[email protected]7e7414ae2010-01-26 20:19:291// Copyright (c) 2010 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
[email protected]f3ec7742009-01-15 00:59:165#include "chrome/browser/tab_contents/tab_contents.h"
initial.commit09911bf2008-07-26 23:55:296
[email protected]06821f42d2010-05-03 22:34:357#if defined(OS_CHROMEOS)
8// For GdkScreen
9#include <gdk/gdk.h>
10#endif // defined(OS_CHROMEOS)
11
[email protected]a92b8642009-05-05 23:38:5612#include "app/l10n_util.h"
[email protected]9929da92009-05-05 02:05:1113#include "app/resource_bundle.h"
[email protected]614519812010-03-19 08:20:5614#include "app/text_elider.h"
[email protected]8cc8d492010-02-02 10:40:4915#include "base/auto_reset.h"
[email protected]420ae012009-04-24 05:16:3216#include "base/file_version_info.h"
[email protected]7cf1b6ce2010-03-20 06:37:0117#include "base/i18n/rtl.h"
[email protected]420ae012009-04-24 05:16:3218#include "base/process_util.h"
[email protected]96d185d2009-04-24 03:28:5419#include "base/string16.h"
[email protected]996fd702009-09-04 19:12:3720#include "base/string_util.h"
[email protected]96d185d2009-04-24 03:28:5421#include "base/time.h"
[email protected]6db0def92010-03-02 19:55:4522#include "chrome/browser/autocomplete_history_manager.h"
[email protected]21674df2009-10-28 19:51:2123#include "chrome/browser/autofill/autofill_manager.h"
[email protected]66ba4932009-06-04 19:22:1324#include "chrome/browser/blocked_popup_container.h"
[email protected]420ae012009-04-24 05:16:3225#include "chrome/browser/bookmarks/bookmark_model.h"
[email protected]281918912010-05-27 22:05:1326#include "chrome/browser/browser.h"
[email protected]96d185d2009-04-24 03:28:5427#include "chrome/browser/browser_process.h"
[email protected]cf68b092010-01-15 20:05:5528#include "chrome/browser/browser_shutdown.h"
initial.commit09911bf2008-07-26 23:55:2929#include "chrome/browser/cert_store.h"
[email protected]aacd7cc2009-09-02 14:51:1030#include "chrome/browser/character_encoding.h"
[email protected]96d185d2009-04-24 03:28:5431#include "chrome/browser/debugger/devtools_manager.h"
[email protected]420ae012009-04-24 05:16:3232#include "chrome/browser/dom_operation_notification_details.h"
[email protected]96d185d2009-04-24 03:28:5433#include "chrome/browser/dom_ui/dom_ui.h"
34#include "chrome/browser/dom_ui/dom_ui_factory.h"
[email protected]bcef0dc02009-02-28 00:35:0235#include "chrome/browser/download/download_item_model.h"
[email protected]420ae012009-04-24 05:16:3236#include "chrome/browser/download/download_manager.h"
37#include "chrome/browser/download/download_request_manager.h"
[email protected]e7eaedde2009-09-25 20:09:4938#include "chrome/browser/external_protocol_handler.h"
[email protected]92c6f9b92009-10-24 04:35:0839#include "chrome/browser/extensions/extensions_service.h"
[email protected]523623c2010-05-19 16:03:4240#include "chrome/browser/history/top_sites.h"
[email protected]0189bc722009-08-28 21:56:4841#include "chrome/browser/favicon_service.h"
[email protected]e5d2f7132010-04-09 18:13:5342#include "chrome/browser/find_bar_state.h"
[email protected]420ae012009-04-24 05:16:3243#include "chrome/browser/google_util.h"
[email protected]79580c62010-02-02 02:36:2544#include "chrome/browser/host_content_settings_map.h"
[email protected]8897c382009-05-06 17:53:2645#include "chrome/browser/hung_renderer_dialog.h"
[email protected]12f74a92010-02-05 22:32:1446#include "chrome/browser/message_box_handler.h"
[email protected]420ae012009-04-24 05:16:3247#include "chrome/browser/load_from_memory_cache_details.h"
48#include "chrome/browser/load_notification_details.h"
[email protected]84abba62009-10-07 17:01:4449#include "chrome/browser/metrics/metric_event_duration_details.h"
[email protected]ea0e80892009-08-22 00:36:0550#include "chrome/browser/modal_html_dialog_delegate.h"
[email protected]c8865482009-07-23 20:40:1051#include "chrome/browser/omnibox_search_hint.h"
[email protected]fdd61c62009-04-22 19:22:5752#include "chrome/browser/password_manager/password_manager.h"
[email protected]14a000d2010-04-29 21:44:2453#include "chrome/browser/platform_util.h"
[email protected]fdd61c62009-04-22 19:22:5754#include "chrome/browser/plugin_installer.h"
[email protected]052313b2010-02-19 09:43:0855#include "chrome/browser/pref_service.h"
[email protected]12802702010-07-09 19:43:0956#include "chrome/browser/printing/print_view_manager.h"
[email protected]ce560f82009-06-03 09:39:4457#include "chrome/browser/profile.h"
[email protected]1db6ff152009-10-12 15:32:0758#include "chrome/browser/renderer_host/render_process_host.h"
[email protected]8cb5d5b2010-02-09 11:36:1659#include "chrome/browser/renderer_host/render_view_host.h"
[email protected]96d185d2009-04-24 03:28:5460#include "chrome/browser/renderer_host/render_widget_host_view.h"
[email protected]fd694982009-07-03 17:32:3761#include "chrome/browser/renderer_host/resource_request_details.h"
[email protected]1db6ff152009-10-12 15:32:0762#include "chrome/browser/renderer_host/site_instance.h"
[email protected]96d185d2009-04-24 03:28:5463#include "chrome/browser/renderer_host/web_cache_manager.h"
[email protected]93623c5d2009-12-10 21:40:3264#include "chrome/browser/renderer_preferences_util.h"
[email protected]152b8ba32010-03-29 19:40:1665#include "chrome/browser/search_engines/template_url_fetcher.h"
66#include "chrome/browser/search_engines/template_url_model.h"
[email protected]c37e3b62010-01-22 00:15:1767#include "chrome/browser/sessions/session_types.h"
[email protected]bd1ad682009-05-15 22:19:1768#include "chrome/browser/tab_contents/infobar_delegate.h"
[email protected]7e383692009-06-12 19:14:5469#include "chrome/browser/tab_contents/interstitial_page.h"
[email protected]f3ec7742009-01-15 00:59:1670#include "chrome/browser/tab_contents/navigation_entry.h"
[email protected]de34a962009-06-26 23:22:4371#include "chrome/browser/tab_contents/provisional_load_details.h"
[email protected]f3ec7742009-01-15 00:59:1672#include "chrome/browser/tab_contents/tab_contents_delegate.h"
[email protected]fdd61c62009-04-22 19:22:5773#include "chrome/browser/tab_contents/tab_contents_view.h"
[email protected]58dca552009-06-17 00:35:0274#include "chrome/browser/tab_contents/thumbnail_generator.h"
[email protected]152b8ba32010-03-29 19:40:1675#include "chrome/browser/translate/page_translated_details.h"
[email protected]54ec6472010-04-09 19:39:5876#include "chrome/common/bindings_policy.h"
[email protected]35f7d212009-04-29 21:19:2777#include "chrome/common/chrome_switches.h"
[email protected]3b355642010-02-05 16:01:4978#include "chrome/common/extensions/extension.h"
[email protected]5d246db22009-10-27 06:17:5779#include "chrome/common/extensions/extension_action.h"
[email protected]942690b132010-05-11 06:42:1480#include "chrome/common/extensions/extension_resource.h"
[email protected]12802702010-07-09 19:43:0981#include "chrome/common/extensions/url_pattern.h"
82#include "chrome/common/navigation_types.h"
83#include "chrome/common/net/url_request_context_getter.h"
[email protected]bfd04a62009-02-01 18:16:5684#include "chrome/common/notification_service.h"
initial.commit09911bf2008-07-26 23:55:2985#include "chrome/common/pref_names.h"
[email protected]420ae012009-04-24 05:16:3286#include "chrome/common/render_messages.h"
[email protected]1ee614862009-07-06 20:29:1887#include "chrome/common/renderer_preferences.h"
[email protected]96d185d2009-04-24 03:28:5488#include "chrome/common/url_constants.h"
[email protected]ea68c06a2010-04-23 17:25:3789#include "grit/chromium_strings.h"
[email protected]8897c382009-05-06 17:53:2690#include "grit/generated_resources.h"
[email protected]420ae012009-04-24 05:16:3291#include "grit/locale_settings.h"
[email protected]fd626782010-01-22 05:26:0692#include "grit/platform_locale_settings.h"
[email protected]ea68c06a2010-04-23 17:25:3793#include "grit/theme_resources.h"
[email protected]420ae012009-04-24 05:16:3294#include "net/base/mime_util.h"
95#include "net/base/net_errors.h"
[email protected]d686e812009-06-03 19:10:2996#include "net/base/net_util.h"
[email protected]420ae012009-04-24 05:16:3297#include "net/base/registry_controlled_domain.h"
[email protected]1ee614862009-07-06 20:29:1898#include "webkit/glue/password_form.h"
99#include "webkit/glue/webpreferences.h"
[email protected]3e45ba92009-02-20 21:09:00100
[email protected]420ae012009-04-24 05:16:32101// Cross-Site Navigations
102//
103// If a TabContents is told to navigate to a different web site (as determined
104// by SiteInstance), it will replace its current RenderViewHost with a new
105// RenderViewHost dedicated to the new SiteInstance. This works as follows:
106//
107// - Navigate determines whether the destination is cross-site, and if so,
108// it creates a pending_render_view_host_ and moves into the PENDING
109// RendererState.
110// - The pending RVH is "suspended," so that no navigation messages are sent to
111// its renderer until the onbeforeunload JavaScript handler has a chance to
112// run in the current RVH.
113// - The pending RVH tells CrossSiteRequestManager (a thread-safe singleton)
114// that it has a pending cross-site request. ResourceDispatcherHost will
115// check for this when the response arrives.
116// - The current RVH runs its onbeforeunload handler. If it returns false, we
117// cancel all the pending logic and go back to NORMAL. Otherwise we allow
118// the pending RVH to send the navigation request to its renderer.
119// - ResourceDispatcherHost receives a ResourceRequest on the IO thread. It
120// checks CrossSiteRequestManager to see that the RVH responsible has a
121// pending cross-site request, and then installs a CrossSiteEventHandler.
122// - When RDH receives a response, the BufferedEventHandler determines whether
123// it is a download. If so, it sends a message to the new renderer causing
124// it to cancel the request, and the download proceeds in the download
125// thread. For now, we stay in a PENDING state (with a pending RVH) until
[email protected]57c6a652009-05-04 07:58:34126// the next DidNavigate event for this TabContents. This isn't ideal, but it
[email protected]420ae012009-04-24 05:16:32127// doesn't affect any functionality.
128// - After RDH receives a response and determines that it is safe and not a
129// download, it pauses the response to first run the old page's onunload
130// handler. It does this by asynchronously calling the OnCrossSiteResponse
[email protected]57c6a652009-05-04 07:58:34131// method of TabContents on the UI thread, which sends a ClosePage message
[email protected]420ae012009-04-24 05:16:32132// to the current RVH.
133// - Once the onunload handler is finished, a ClosePage_ACK message is sent to
134// the ResourceDispatcherHost, who unpauses the response. Data is then sent
135// to the pending RVH.
136// - The pending renderer sends a FrameNavigate message that invokes the
137// DidNavigate method. This replaces the current RVH with the
138// pending RVH and goes back to the NORMAL RendererState.
139
140namespace {
141
142// Amount of time we wait between when a key event is received and the renderer
143// is queried for its state and pushed to the NavigationEntry.
144const int kQueryStateDelay = 5000;
145
146const int kSyncWaitDelay = 40;
147
[email protected]304f8792009-08-19 21:28:09148#if defined(OS_CHROMEOS)
149// If a popup window is bigger than this fraction of the screen on chrome os,
150// turn it into a tab
151const float kMaxWidthFactor = 0.5;
152const float kMaxHeightFactor = 0.6;
153#endif
154
[email protected]420ae012009-04-24 05:16:32155// If another javascript message box is displayed within
156// kJavascriptMessageExpectedDelay of a previous javascript message box being
157// dismissed, display an option to suppress future message boxes from this
158// contents.
159const int kJavascriptMessageExpectedDelay = 1000;
160
[email protected]420ae012009-04-24 05:16:32161// The list of prefs we want to observe.
162const wchar_t* kPrefsToObserve[] = {
163 prefs::kAlternateErrorPagesEnabled,
164 prefs::kWebKitJavaEnabled,
165 prefs::kWebKitJavascriptEnabled,
166 prefs::kWebKitLoadsImagesAutomatically,
167 prefs::kWebKitPluginsEnabled,
168 prefs::kWebKitUsesUniversalDetector,
169 prefs::kWebKitSerifFontFamily,
170 prefs::kWebKitSansSerifFontFamily,
171 prefs::kWebKitFixedFontFamily,
172 prefs::kWebKitDefaultFontSize,
173 prefs::kWebKitDefaultFixedFontSize,
[email protected]1661a3c02010-05-20 16:32:36174 prefs::kWebkitTabsToLinks,
[email protected]420ae012009-04-24 05:16:32175 prefs::kDefaultCharset
176 // kWebKitStandardFontIsSerif needs to be added
177 // if we let users pick which font to use, serif or sans-serif when
178 // no font is specified or a CSS generic family (serif or sans-serif)
179 // is not specified.
180};
181
182const int kPrefsToObserveLength = arraysize(kPrefsToObserve);
183
[email protected]420ae012009-04-24 05:16:32184// Returns true if the entry's transition type is FORM_SUBMIT.
185bool IsFormSubmit(const NavigationEntry* entry) {
186 return (PageTransition::StripQualifier(entry->transition_type()) ==
187 PageTransition::FORM_SUBMIT);
188}
189
190#if defined(OS_WIN)
191
192BOOL CALLBACK InvalidateWindow(HWND hwnd, LPARAM lparam) {
193 // Note: erase is required to properly paint some widgets borders. This can
194 // be seen with textfields.
195 InvalidateRect(hwnd, NULL, TRUE);
196 return TRUE;
197}
198#endif
199
[email protected]5e369672009-11-03 23:48:30200ViewMsg_Navigate_Params::NavigationType GetNavigationType(
[email protected]1ccb3568d2010-02-19 10:51:16201 Profile* profile, const NavigationEntry& entry,
202 NavigationController::ReloadType reload_type) {
203 switch (reload_type) {
204 case NavigationController::RELOAD:
205 return ViewMsg_Navigate_Params::RELOAD;
206 case NavigationController::RELOAD_IGNORING_CACHE:
207 return ViewMsg_Navigate_Params::RELOAD_IGNORING_CACHE;
208 case NavigationController::NO_RELOAD:
209 break; // Fall through to rest of function.
210 }
[email protected]5e369672009-11-03 23:48:30211
212 if (entry.restore_type() == NavigationEntry::RESTORE_LAST_SESSION &&
213 profile->DidLastSessionExitCleanly())
214 return ViewMsg_Navigate_Params::RESTORE;
215
216 return ViewMsg_Navigate_Params::NORMAL;
217}
218
[email protected]3cc72b12010-03-18 23:03:00219void MakeNavigateParams(const NavigationController& controller,
[email protected]1ccb3568d2010-02-19 10:51:16220 NavigationController::ReloadType reload_type,
221 ViewMsg_Navigate_Params* params) {
[email protected]3cc72b12010-03-18 23:03:00222 const NavigationEntry& entry = *controller.pending_entry();
[email protected]056de2d2009-06-26 16:41:34223 params->page_id = entry.page_id();
[email protected]3cc72b12010-03-18 23:03:00224 params->pending_history_list_offset = controller.pending_entry_index();
225 params->current_history_list_offset = controller.last_committed_entry_index();
226 params->current_history_list_length = controller.entry_count();
[email protected]056de2d2009-06-26 16:41:34227 params->url = entry.url();
228 params->referrer = entry.referrer();
229 params->transition = entry.transition_type();
230 params->state = entry.content_state();
[email protected]3cc72b12010-03-18 23:03:00231 params->navigation_type =
232 GetNavigationType(controller.profile(), entry, reload_type);
[email protected]056de2d2009-06-26 16:41:34233 params->request_time = base::Time::Now();
234}
235
[email protected]420ae012009-04-24 05:16:32236} // namespace
237
238// -----------------------------------------------------------------------------
239
[email protected]fdd61c62009-04-22 19:22:57240// static
241int TabContents::find_request_id_counter_ = -1;
242
[email protected]420ae012009-04-24 05:16:32243
[email protected]420ae012009-04-24 05:16:32244TabContents::TabContents(Profile* profile,
245 SiteInstance* site_instance,
246 int routing_id,
[email protected]34ac70502009-09-25 17:07:23247 const TabContents* base_tab_contents)
[email protected]b680ad22009-04-15 23:19:42248 : delegate_(NULL),
[email protected]66ba4932009-06-04 19:22:13249 ALLOW_THIS_IN_INITIALIZER_LIST(controller_(this, profile)),
250 ALLOW_THIS_IN_INITIALIZER_LIST(view_(
[email protected]d82ed61e2009-06-16 02:46:22251 TabContentsView::Create(this))),
252 ALLOW_THIS_IN_INITIALIZER_LIST(render_manager_(this, this)),
[email protected]fdd61c62009-04-22 19:22:57253 property_bag_(),
254 registrar_(),
[email protected]12802702010-07-09 19:43:09255 ALLOW_THIS_IN_INITIALIZER_LIST(printing_(
256 new printing::PrintViewManager(*this))),
[email protected]fdd61c62009-04-22 19:22:57257 save_package_(),
[email protected]6db0def92010-03-02 19:55:45258 autocomplete_history_manager_(),
[email protected]21674df2009-10-28 19:51:21259 autofill_manager_(),
[email protected]fdd61c62009-04-22 19:22:57260 password_manager_(),
261 plugin_installer_(),
[email protected]6f821dd2010-02-23 00:54:06262 bookmark_drag_(NULL),
[email protected]d82ed61e2009-06-16 02:46:22263 ALLOW_THIS_IN_INITIALIZER_LIST(fav_icon_helper_(this)),
[email protected]fdd61c62009-04-22 19:22:57264 select_file_dialog_(),
[email protected]d5f942ba2008-09-26 19:30:34265 is_loading_(false),
initial.commit09911bf2008-07-26 23:55:29266 is_crashed_(false),
[email protected]d5f942ba2008-09-26 19:30:34267 waiting_for_response_(false),
[email protected]d5f942ba2008-09-26 19:30:34268 max_page_id_(-1),
[email protected]fdd61c62009-04-22 19:22:57269 current_load_start_(),
270 load_state_(net::LOAD_STATE_IDLE),
271 load_state_host_(),
[email protected]094e5b22009-09-25 04:23:56272 upload_size_(0),
273 upload_position_(0),
[email protected]fdd61c62009-04-22 19:22:57274 received_page_title_(false),
275 is_starred_(false),
276 contents_mime_type_(),
277 encoding_(),
[email protected]332af7732009-03-11 13:21:35278 blocked_popups_(NULL),
[email protected]8cc8d492010-02-02 10:40:49279 dont_notify_render_view_(false),
[email protected]f17a0ee2010-05-17 17:38:47280 displayed_insecure_content_(false),
[email protected]fdd61c62009-04-22 19:22:57281 infobar_delegates_(),
[email protected]fdd61c62009-04-22 19:22:57282 find_ui_active_(false),
283 find_op_aborted_(false),
284 current_find_request_id_(find_request_id_counter_++),
[email protected]e491f1c2009-05-22 20:28:12285 last_search_case_sensitive_(false),
[email protected]e491f1c2009-05-22 20:28:12286 last_search_result_(),
[email protected]98aa0b52010-05-06 17:03:08287 extension_app_(NULL),
[email protected]fdd61c62009-04-22 19:22:57288 capturing_contents_(false),
289 is_being_destroyed_(false),
290 notify_disconnection_(false),
291 history_requests_(),
292#if defined(OS_WIN)
293 message_box_active_(CreateEvent(NULL, TRUE, FALSE, NULL)),
294#endif
295 last_javascript_message_dismissal_(),
[email protected]c2e74fe82009-09-03 17:57:44296 suppress_javascript_messages_(false),
[email protected]7ab1e7d62009-10-14 23:32:01297 is_showing_before_unload_dialog_(false),
[email protected]7ff431e2010-01-07 18:18:54298 renderer_preferences_(),
[email protected]1a242c32010-02-02 21:20:54299 opener_dom_ui_type_(DOMUIFactory::kNoDOMUI),
[email protected]777a7b62010-05-11 13:28:16300 language_state_(&controller_),
[email protected]0cce15f2010-05-21 16:58:39301 closed_by_user_gesture_(false) {
[email protected]7ff431e2010-01-07 18:18:54302 renderer_preferences_util::UpdateFromSystemSettings(
303 &renderer_preferences_, profile);
304
[email protected]9fb83e82010-07-02 18:24:55305 content_settings_delegate_.reset(
306 new TabSpecificContentSettings(this, profile));
307
[email protected]16779842009-07-08 23:45:29308#if defined(OS_CHROMEOS)
[email protected]58dca552009-06-17 00:35:02309 // Make sure the thumbnailer is started before starting the render manager.
310 // The thumbnailer will want to listen for RVH creations, one of which will
311 // happen in RVHManager::Init.
312 ThumbnailGenerator* generator = g_browser_process->GetThumbnailGenerator();
313 if (generator)
314 generator->StartThumbnailing();
315#endif
316
[email protected]12636df2009-09-28 22:32:21317 render_manager_.Init(profile, site_instance, routing_id);
[email protected]420ae012009-04-24 05:16:32318
[email protected]34ac70502009-09-25 17:07:23319 // We have the initial size of the view be based on the size of the passed in
320 // tab contents (normally a tab from the same window).
321 view_->CreateView(base_tab_contents ?
322 base_tab_contents->view()->GetContainerSize() : gfx::Size());
[email protected]420ae012009-04-24 05:16:32323
324 // Register for notifications about all interested prefs change.
325 PrefService* prefs = profile->GetPrefs();
326 if (prefs) {
327 for (int i = 0; i < kPrefsToObserveLength; ++i)
328 prefs->AddPrefObserver(kPrefsToObserve[i], this);
329 }
330
331 // Register for notifications about URL starredness changing on any profile.
332 registrar_.Add(this, NotificationType::URLS_STARRED,
333 NotificationService::AllSources());
334 registrar_.Add(this, NotificationType::BOOKMARK_MODEL_LOADED,
335 NotificationService::AllSources());
336 registrar_.Add(this, NotificationType::RENDER_WIDGET_HOST_DESTROYED,
337 NotificationService::AllSources());
[email protected]d3ba77272009-09-03 00:06:09338#if defined(OS_LINUX)
339 registrar_.Add(this, NotificationType::BROWSER_THEME_CHANGED,
340 NotificationService::AllSources());
341#endif
[email protected]420ae012009-04-24 05:16:32342
[email protected]6c32ce72010-03-08 05:18:06343 registrar_.Add(this, NotificationType::USER_STYLE_SHEET_UPDATED,
344 NotificationService::AllSources());
345
[email protected]79580c62010-02-02 02:36:25346 // Register for notifications about content setting changes.
347 registrar_.Add(this, NotificationType::CONTENT_SETTINGS_CHANGED,
348 NotificationService::AllSources());
349
[email protected]a888b29e62010-04-01 13:38:57350 // Listen for extension changes so we can update extension_for_current_page_.
351 registrar_.Add(this, NotificationType::EXTENSION_LOADED,
352 NotificationService::AllSources());
353 registrar_.Add(this, NotificationType::EXTENSION_UNLOADED,
354 NotificationService::AllSources());
355 registrar_.Add(this, NotificationType::EXTENSION_UNLOADED_DISABLED,
356 NotificationService::AllSources());
357
[email protected]c8865482009-07-23 20:40:10358 // Set-up the showing of the omnibox search infobar if applicable.
359 if (OmniboxSearchHint::IsEnabled(profile))
360 omnibox_search_hint_.reset(new OmniboxSearchHint(this));
[email protected]332af7732009-03-11 13:21:35361}
initial.commit09911bf2008-07-26 23:55:29362
363TabContents::~TabContents() {
[email protected]420ae012009-04-24 05:16:32364 is_being_destroyed_ = true;
365
[email protected]bfe4c1582009-10-02 18:11:09366 // We don't want any notifications while we're running our destructor.
[email protected]420ae012009-04-24 05:16:32367 registrar_.RemoveAll();
368
369 // Unregister the notifications of all observed prefs change.
370 PrefService* prefs = profile()->GetPrefs();
371 if (prefs) {
372 for (int i = 0; i < kPrefsToObserveLength; ++i)
373 prefs->RemovePrefObserver(kPrefsToObserve[i], this);
374 }
375
[email protected]420ae012009-04-24 05:16:32376 NotifyDisconnected();
[email protected]e7b8d832009-11-20 22:41:00377 hung_renderer_dialog::HideForTabContents(this);
[email protected]420ae012009-04-24 05:16:32378
[email protected]420ae012009-04-24 05:16:32379 // First cleanly close all child windows.
380 // TODO(mpcomplete): handle case if MaybeCloseChildWindows() already asked
381 // some of these to close. CloseWindows is async, so it might get called
382 // twice before it runs.
[email protected]8cc8d492010-02-02 10:40:49383 CloseConstrainedWindows();
[email protected]420ae012009-04-24 05:16:32384
[email protected]8cc8d492010-02-02 10:40:49385 // Close all blocked popups.
[email protected]66ba4932009-06-04 19:22:13386 if (blocked_popups_)
387 blocked_popups_->Destroy();
388
[email protected]420ae012009-04-24 05:16:32389 // Notify any observer that have a reference on this tab contents.
390 NotificationService::current()->Notify(
391 NotificationType::TAB_CONTENTS_DESTROYED,
392 Source<TabContents>(this),
393 NotificationService::NoDetails());
394
[email protected]1e5e74f2009-05-27 20:55:12395 // Notify any lasting InfobarDelegates that have not yet been removed that
396 // whatever infobar they were handling in this TabContents has closed,
397 // because the TabContents is going away entirely.
398 // This must happen after the TAB_CONTENTS_DESTROYED notification as the
399 // notification may trigger infobars calls that access their delegate. (and
400 // some implementations of InfoBarDelegate do delete themselves on
401 // InfoBarClosed()).
402 for (int i = 0; i < infobar_delegate_count(); ++i) {
403 InfoBarDelegate* delegate = GetInfoBarDelegateAt(i);
404 delegate->InfoBarClosed();
405 }
406 infobar_delegates_.clear();
407
[email protected]420ae012009-04-24 05:16:32408 // TODO(brettw) this should be moved to the view.
409#if defined(OS_WIN)
410 // If we still have a window handle, destroy it. GetNativeView can return
411 // NULL if this contents was part of a window that closed.
412 if (GetNativeView())
413 ::DestroyWindow(GetNativeView());
414#endif
[email protected]7ab1e7d62009-10-14 23:32:01415
416 // OnCloseStarted isn't called in unit tests.
417 if (!tab_close_start_time_.is_null()) {
418 UMA_HISTOGRAM_TIMES("Tab.Close",
419 base::TimeTicks::Now() - tab_close_start_time_);
420 }
initial.commit09911bf2008-07-26 23:55:29421}
422
[email protected]d5f942ba2008-09-26 19:30:34423// static
424void TabContents::RegisterUserPrefs(PrefService* prefs) {
[email protected]420ae012009-04-24 05:16:32425 prefs->RegisterBooleanPref(prefs::kAlternateErrorPagesEnabled, true);
426
427 WebPreferences pref_defaults;
428 prefs->RegisterBooleanPref(prefs::kWebKitJavascriptEnabled,
429 pref_defaults.javascript_enabled);
430 prefs->RegisterBooleanPref(prefs::kWebKitWebSecurityEnabled,
431 pref_defaults.web_security_enabled);
432 prefs->RegisterBooleanPref(
433 prefs::kWebKitJavascriptCanOpenWindowsAutomatically, true);
434 prefs->RegisterBooleanPref(prefs::kWebKitLoadsImagesAutomatically,
435 pref_defaults.loads_images_automatically);
436 prefs->RegisterBooleanPref(prefs::kWebKitPluginsEnabled,
437 pref_defaults.plugins_enabled);
438 prefs->RegisterBooleanPref(prefs::kWebKitDomPasteEnabled,
439 pref_defaults.dom_paste_enabled);
440 prefs->RegisterBooleanPref(prefs::kWebKitShrinksStandaloneImagesToFit,
441 pref_defaults.shrinks_standalone_images_to_fit);
[email protected]c4e98902010-06-01 10:20:14442 prefs->RegisterDictionaryPref(prefs::kWebKitInspectorSettings);
[email protected]420ae012009-04-24 05:16:32443 prefs->RegisterBooleanPref(prefs::kWebKitTextAreasAreResizable,
444 pref_defaults.text_areas_are_resizable);
445 prefs->RegisterBooleanPref(prefs::kWebKitJavaEnabled,
446 pref_defaults.java_enabled);
[email protected]1661a3c02010-05-20 16:32:36447 prefs->RegisterBooleanPref(prefs::kWebkitTabsToLinks,
448 pref_defaults.tabs_to_links);
[email protected]420ae012009-04-24 05:16:32449
450 prefs->RegisterLocalizedStringPref(prefs::kAcceptLanguages,
451 IDS_ACCEPT_LANGUAGES);
452 prefs->RegisterLocalizedStringPref(prefs::kDefaultCharset,
453 IDS_DEFAULT_ENCODING);
454 prefs->RegisterLocalizedBooleanPref(prefs::kWebKitStandardFontIsSerif,
455 IDS_STANDARD_FONT_IS_SERIF);
456 prefs->RegisterLocalizedStringPref(prefs::kWebKitFixedFontFamily,
457 IDS_FIXED_FONT_FAMILY);
458 prefs->RegisterLocalizedStringPref(prefs::kWebKitSerifFontFamily,
459 IDS_SERIF_FONT_FAMILY);
460 prefs->RegisterLocalizedStringPref(prefs::kWebKitSansSerifFontFamily,
461 IDS_SANS_SERIF_FONT_FAMILY);
462 prefs->RegisterLocalizedStringPref(prefs::kWebKitCursiveFontFamily,
463 IDS_CURSIVE_FONT_FAMILY);
464 prefs->RegisterLocalizedStringPref(prefs::kWebKitFantasyFontFamily,
465 IDS_FANTASY_FONT_FAMILY);
466 prefs->RegisterLocalizedIntegerPref(prefs::kWebKitDefaultFontSize,
467 IDS_DEFAULT_FONT_SIZE);
468 prefs->RegisterLocalizedIntegerPref(prefs::kWebKitDefaultFixedFontSize,
469 IDS_DEFAULT_FIXED_FONT_SIZE);
470 prefs->RegisterLocalizedIntegerPref(prefs::kWebKitMinimumFontSize,
471 IDS_MINIMUM_FONT_SIZE);
472 prefs->RegisterLocalizedIntegerPref(prefs::kWebKitMinimumLogicalFontSize,
473 IDS_MINIMUM_LOGICAL_FONT_SIZE);
474 prefs->RegisterLocalizedBooleanPref(prefs::kWebKitUsesUniversalDetector,
475 IDS_USES_UNIVERSAL_DETECTOR);
476 prefs->RegisterLocalizedStringPref(prefs::kStaticEncodings,
477 IDS_STATIC_ENCODING_LIST);
initial.commit09911bf2008-07-26 23:55:29478}
479
[email protected]13367f72009-07-08 16:39:02480// Returns true if contains content rendered by an extension.
481bool TabContents::HostsExtension() const {
482 return GetURL().SchemeIs(chrome::kExtensionScheme);
[email protected]d5f942ba2008-09-26 19:30:34483}
484
[email protected]cf28ec52010-04-09 22:56:34485AutoFillManager* TabContents::GetAutoFillManager() {
486 if (autofill_manager_.get() == NULL)
487 autofill_manager_.reset(new AutoFillManager(this));
488 return autofill_manager_.get();
489}
490
[email protected]6e95706d2009-04-23 22:55:01491PasswordManager* TabContents::GetPasswordManager() {
492 if (password_manager_.get() == NULL)
493 password_manager_.reset(new PasswordManager(this));
494 return password_manager_.get();
495}
496
497PluginInstaller* TabContents::GetPluginInstaller() {
498 if (plugin_installer_.get() == NULL)
499 plugin_installer_.reset(new PluginInstaller(this));
500 return plugin_installer_.get();
501}
502
[email protected]8cb5d5b2010-02-09 11:36:16503RenderProcessHost* TabContents::GetRenderProcessHost() const {
504 return render_manager_.current_host()->process();
505}
506
[email protected]98aa0b52010-05-06 17:03:08507void TabContents::SetExtensionApp(Extension* extension) {
[email protected]867a73e12010-03-19 20:45:46508 DCHECK(!extension || extension->GetFullLaunchURL().is_valid());
[email protected]98aa0b52010-05-06 17:03:08509 extension_app_ = extension;
[email protected]fca656c2010-02-10 20:30:10510
[email protected]98aa0b52010-05-06 17:03:08511 UpdateExtensionAppIcon(extension_app_);
[email protected]a888b29e62010-04-01 13:38:57512
[email protected]fca656c2010-02-10 20:30:10513 NotificationService::current()->Notify(
514 NotificationType::TAB_CONTENTS_APPLICATION_EXTENSION_CHANGED,
515 Source<TabContents>(this),
516 NotificationService::NoDetails());
[email protected]3b355642010-02-05 16:01:49517}
518
[email protected]98aa0b52010-05-06 17:03:08519void TabContents::SetExtensionAppById(const std::string& extension_app_id) {
520 if (extension_app_id.empty())
[email protected]911f8152010-03-18 16:46:40521 return;
522
523 ExtensionsService* extension_service = profile()->GetExtensionsService();
524 if (extension_service && extension_service->is_ready()) {
525 Extension* extension =
[email protected]98aa0b52010-05-06 17:03:08526 extension_service->GetExtensionById(extension_app_id, false);
[email protected]911f8152010-03-18 16:46:40527 if (extension)
[email protected]98aa0b52010-05-06 17:03:08528 SetExtensionApp(extension);
[email protected]911f8152010-03-18 16:46:40529 }
530}
531
[email protected]98aa0b52010-05-06 17:03:08532SkBitmap* TabContents::GetExtensionAppIcon() {
[email protected]8eb043b2010-04-30 07:19:51533 // We don't show the big icons in tabs for TYPE_EXTENSION_APP windows because
534 // for those windows, we already have a big icon in the top-left outside any
535 // tab. Having big tab icons too looks kinda redonk.
[email protected]06821f42d2010-05-03 22:34:35536 Browser* browser = delegate_ ? delegate_->GetBrowser() : NULL;
537 if (browser && browser->type() == Browser::TYPE_EXTENSION_APP)
[email protected]8eb043b2010-04-30 07:19:51538 return NULL;
[email protected]8eb043b2010-04-30 07:19:51539
[email protected]98aa0b52010-05-06 17:03:08540 if (extension_app_icon_.empty())
[email protected]8eb043b2010-04-30 07:19:51541 return NULL;
542
[email protected]98aa0b52010-05-06 17:03:08543 return &extension_app_icon_;
[email protected]8eb043b2010-04-30 07:19:51544}
545
[email protected]d5f942ba2008-09-26 19:30:34546const GURL& TabContents::GetURL() const {
547 // We may not have a navigation entry yet
[email protected]ce3fa3c2009-04-20 19:55:57548 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]ebe89e062009-08-13 23:16:54549 return entry ? entry->virtual_url() : GURL::EmptyGURL();
[email protected]d5f942ba2008-09-26 19:30:34550}
551
[email protected]96d185d2009-04-24 03:28:54552const string16& TabContents::GetTitle() const {
[email protected]4c6092c5b2009-06-06 00:23:55553 // Transient entries take precedence. They are used for interstitial pages
554 // that are shown on top of existing pages.
555 NavigationEntry* entry = controller_.GetTransientEntry();
556 if (entry)
557 return entry->GetTitleForDisplay(&controller_);
558
[email protected]96d185d2009-04-24 03:28:54559 DOMUI* our_dom_ui = render_manager_.pending_dom_ui() ?
560 render_manager_.pending_dom_ui() : render_manager_.dom_ui();
561 if (our_dom_ui) {
562 // Don't override the title in view source mode.
[email protected]4c6092c5b2009-06-06 00:23:55563 entry = controller_.GetActiveEntry();
[email protected]96d185d2009-04-24 03:28:54564 if (!(entry && entry->IsViewSourceMode())) {
565 // Give the DOM UI the chance to override our title.
566 const string16& title = our_dom_ui->overridden_title();
567 if (!title.empty())
568 return title;
569 }
570 }
571
572 // We use the title for the last committed entry rather than a pending
573 // navigation entry. For example, when the user types in a URL, we want to
574 // keep the old page's title until the new load has committed and we get a new
575 // title.
[email protected]96d185d2009-04-24 03:28:54576 entry = controller_.GetLastCommittedEntry();
577 if (entry)
578 return entry->GetTitleForDisplay(&controller_);
[email protected]96d185d2009-04-24 03:28:54579 return EmptyString16();
580}
581
[email protected]bbbc1ef2010-02-12 18:03:41582// static
583string16 TabContents::GetDefaultTitle() {
584 return l10n_util::GetStringUTF16(IDS_DEFAULT_TAB_TITLE);
585}
586
[email protected]d5f942ba2008-09-26 19:30:34587int32 TabContents::GetMaxPageID() {
588 if (GetSiteInstance())
589 return GetSiteInstance()->max_page_id();
590 else
591 return max_page_id_;
592}
593
594void TabContents::UpdateMaxPageID(int32 page_id) {
595 // Ensure both the SiteInstance and RenderProcessHost update their max page
[email protected]57c6a652009-05-04 07:58:34596 // IDs in sync. Only TabContents will also have site instances, except during
[email protected]d5f942ba2008-09-26 19:30:34597 // testing.
598 if (GetSiteInstance())
599 GetSiteInstance()->UpdateMaxPageID(page_id);
[email protected]8cb5d5b2010-02-09 11:36:16600 GetRenderProcessHost()->UpdateMaxPageID(page_id);
[email protected]d5f942ba2008-09-26 19:30:34601}
602
[email protected]96d185d2009-04-24 03:28:54603SiteInstance* TabContents::GetSiteInstance() const {
604 return render_manager_.current_host()->site_instance();
605}
606
[email protected]96d185d2009-04-24 03:28:54607bool TabContents::ShouldDisplayURL() {
[email protected]8ab24cc2009-11-13 20:56:15608 // Don't hide the url in view source mode and with interstitials.
[email protected]96d185d2009-04-24 03:28:54609 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]8ab24cc2009-11-13 20:56:15610 if (entry && (entry->IsViewSourceMode() ||
611 entry->page_type() == NavigationEntry::INTERSTITIAL_PAGE)) {
[email protected]96d185d2009-04-24 03:28:54612 return true;
[email protected]8ab24cc2009-11-13 20:56:15613 }
[email protected]83e3895b2009-06-11 00:07:16614
[email protected]96d185d2009-04-24 03:28:54615 DOMUI* dom_ui = GetDOMUIForCurrentState();
616 if (dom_ui)
617 return !dom_ui->should_hide_url();
618 return true;
619}
620
[email protected]d5f942ba2008-09-26 19:30:34621SkBitmap TabContents::GetFavIcon() const {
622 // Like GetTitle(), we also want to use the favicon for the last committed
623 // entry rather than a pending navigation entry.
[email protected]ce3fa3c2009-04-20 19:55:57624 NavigationEntry* entry = controller_.GetTransientEntry();
[email protected]cbab76d2008-10-13 22:42:47625 if (entry)
626 return entry->favicon().bitmap();
627
[email protected]ce3fa3c2009-04-20 19:55:57628 entry = controller_.GetLastCommittedEntry();
[email protected]d5f942ba2008-09-26 19:30:34629 if (entry)
630 return entry->favicon().bitmap();
[email protected]d5f942ba2008-09-26 19:30:34631 return SkBitmap();
632}
633
[email protected]0b303cc2009-09-28 22:35:15634bool TabContents::FavIconIsValid() const {
635 NavigationEntry* entry = controller_.GetTransientEntry();
636 if (entry)
637 return entry->favicon().is_valid();
638
639 entry = controller_.GetLastCommittedEntry();
640 if (entry)
641 return entry->favicon().is_valid();
642
643 return false;
644}
645
[email protected]96d185d2009-04-24 03:28:54646bool TabContents::ShouldDisplayFavIcon() {
647 // Always display a throbber during pending loads.
648 if (controller_.GetLastCommittedEntry() && controller_.pending_entry())
649 return true;
650
651 DOMUI* dom_ui = GetDOMUIForCurrentState();
652 if (dom_ui)
653 return !dom_ui->hide_favicon();
654 return true;
655}
656
[email protected]96d185d2009-04-24 03:28:54657std::wstring TabContents::GetStatusText() const {
658 if (!is_loading() || load_state_ == net::LOAD_STATE_IDLE)
659 return std::wstring();
660
661 switch (load_state_) {
662 case net::LOAD_STATE_WAITING_FOR_CACHE:
663 return l10n_util::GetString(IDS_LOAD_STATE_WAITING_FOR_CACHE);
[email protected]8a1f3312010-05-25 19:25:04664 case net::LOAD_STATE_ESTABLISHING_PROXY_TUNNEL:
665 return l10n_util::GetString(IDS_LOAD_STATE_ESTABLISHING_PROXY_TUNNEL);
[email protected]96d185d2009-04-24 03:28:54666 case net::LOAD_STATE_RESOLVING_PROXY_FOR_URL:
667 return l10n_util::GetString(IDS_LOAD_STATE_RESOLVING_PROXY_FOR_URL);
668 case net::LOAD_STATE_RESOLVING_HOST:
669 return l10n_util::GetString(IDS_LOAD_STATE_RESOLVING_HOST);
670 case net::LOAD_STATE_CONNECTING:
671 return l10n_util::GetString(IDS_LOAD_STATE_CONNECTING);
[email protected]375e3112010-05-27 20:32:06672 case net::LOAD_STATE_SSL_HANDSHAKE:
673 return l10n_util::GetString(IDS_LOAD_STATE_SSL_HANDSHAKE);
[email protected]96d185d2009-04-24 03:28:54674 case net::LOAD_STATE_SENDING_REQUEST:
[email protected]094e5b22009-09-25 04:23:56675 if (upload_size_)
676 return l10n_util::GetStringF(
677 IDS_LOAD_STATE_SENDING_REQUEST_WITH_PROGRESS,
678 static_cast<int>((100 * upload_position_) / upload_size_));
679 else
680 return l10n_util::GetString(IDS_LOAD_STATE_SENDING_REQUEST);
[email protected]96d185d2009-04-24 03:28:54681 case net::LOAD_STATE_WAITING_FOR_RESPONSE:
682 return l10n_util::GetStringF(IDS_LOAD_STATE_WAITING_FOR_RESPONSE,
683 load_state_host_);
684 // Ignore net::LOAD_STATE_READING_RESPONSE and net::LOAD_STATE_IDLE
685 case net::LOAD_STATE_IDLE:
686 case net::LOAD_STATE_READING_RESPONSE:
687 break;
688 }
689
690 return std::wstring();
691}
692
[email protected]d5f942ba2008-09-26 19:30:34693void TabContents::SetIsCrashed(bool state) {
694 if (state == is_crashed_)
695 return;
696
697 is_crashed_ = state;
[email protected]c9cd2222009-05-06 05:16:50698 NotifyNavigationStateChanged(INVALIDATE_TAB);
[email protected]d5f942ba2008-09-26 19:30:34699}
700
[email protected]744ef172009-10-16 21:53:46701void TabContents::PageActionStateChanged() {
702 NotifyNavigationStateChanged(TabContents::INVALIDATE_PAGE_ACTIONS);
[email protected]d7eaf572009-07-01 21:57:00703}
[email protected]ce5c4502009-05-06 16:46:11704
[email protected]d5f942ba2008-09-26 19:30:34705void TabContents::NotifyNavigationStateChanged(unsigned changed_flags) {
706 if (delegate_)
707 delegate_->NavigationStateChanged(this, changed_flags);
708}
709
[email protected]96d185d2009-04-24 03:28:54710void TabContents::DidBecomeSelected() {
711 controller_.SetActive(true);
[email protected]8cb5d5b2010-02-09 11:36:16712 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
713 if (rwhv) {
714 rwhv->DidBecomeSelected();
[email protected]789e9152009-08-04 21:59:43715#if defined(OS_MACOSX)
[email protected]8cb5d5b2010-02-09 11:36:16716 rwhv->SetActive(true);
[email protected]789e9152009-08-04 21:59:43717#endif
718 }
[email protected]96d185d2009-04-24 03:28:54719
[email protected]8cb5d5b2010-02-09 11:36:16720 WebCacheManager::GetInstance()->ObserveActivity(GetRenderProcessHost()->id());
[email protected]96d185d2009-04-24 03:28:54721}
722
723void TabContents::WasHidden() {
724 if (!capturing_contents()) {
725 // |render_view_host()| can be NULL if the user middle clicks a link to open
726 // a tab in then background, then closes the tab before selecting it. This
727 // is because closing the tab calls TabContents::Destroy(), which removes
728 // the |render_view_host()|; then when we actually destroy the window,
729 // OnWindowPosChanged() notices and calls HideContents() (which calls us).
[email protected]8cb5d5b2010-02-09 11:36:16730 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
731 if (rwhv)
732 rwhv->WasHidden();
[email protected]96d185d2009-04-24 03:28:54733 }
734
735 NotificationService::current()->Notify(
736 NotificationType::TAB_CONTENTS_HIDDEN,
737 Source<TabContents>(this),
738 NotificationService::NoDetails());
739}
740
[email protected]d5f942ba2008-09-26 19:30:34741void TabContents::Activate() {
742 if (delegate_)
743 delegate_->ActivateContents(this);
744}
745
[email protected]96d185d2009-04-24 03:28:54746void TabContents::ShowContents() {
[email protected]8cb5d5b2010-02-09 11:36:16747 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
748 if (rwhv)
749 rwhv->DidBecomeSelected();
[email protected]96d185d2009-04-24 03:28:54750}
751
752void TabContents::HideContents() {
753 // TODO(pkasting): http://b/1239839 Right now we purposefully don't call
754 // our superclass HideContents(), because some callers want to be very picky
755 // about the order in which these get called. In addition to making the code
756 // here practically impossible to understand, this also means we end up
757 // calling TabContents::WasHidden() twice if callers call both versions of
[email protected]57c6a652009-05-04 07:58:34758 // HideContents() on a TabContents.
[email protected]96d185d2009-04-24 03:28:54759 WasHidden();
760}
761
[email protected]c0588052008-10-27 23:01:50762void TabContents::OpenURL(const GURL& url, const GURL& referrer,
[email protected]d5f942ba2008-09-26 19:30:34763 WindowOpenDisposition disposition,
764 PageTransition::Type transition) {
765 if (delegate_)
[email protected]c0588052008-10-27 23:01:50766 delegate_->OpenURLFromTab(this, url, referrer, disposition, transition);
[email protected]d5f942ba2008-09-26 19:30:34767}
768
[email protected]1ccb3568d2010-02-19 10:51:16769bool TabContents::NavigateToPendingEntry(
770 NavigationController::ReloadType reload_type) {
[email protected]96d185d2009-04-24 03:28:54771 const NavigationEntry& entry = *controller_.pending_entry();
772
773 RenderViewHost* dest_render_view_host = render_manager_.Navigate(entry);
774 if (!dest_render_view_host)
775 return false; // Unable to create the desired render view host.
776
[email protected]770dd8b2010-05-24 18:11:39777 if (delegate_ && delegate_->ShouldEnablePreferredSizeNotifications()) {
778 dest_render_view_host->EnablePreferredSizeChangedMode(
779 kPreferredSizeWidth | kPreferredSizeHeightThisIsSlow);
780 }
[email protected]9fb325e2010-05-06 18:23:24781
[email protected]0352e5472010-04-29 00:11:40782 // For security, we should never send non-DOM-UI URLs (other than about:blank)
783 // to a DOM UI renderer. Double check that here.
[email protected]54ec6472010-04-09 19:39:58784 int enabled_bindings = dest_render_view_host->enabled_bindings();
[email protected]0352e5472010-04-29 00:11:40785 bool is_allowed_in_dom_ui_renderer =
786 DOMUIFactory::UseDOMUIForURL(entry.url()) ||
787 entry.url() == GURL(chrome::kAboutBlankURL);
[email protected]54ec6472010-04-09 19:39:58788 CHECK(!BindingsPolicy::is_dom_ui_enabled(enabled_bindings) ||
[email protected]0352e5472010-04-29 00:11:40789 is_allowed_in_dom_ui_renderer);
[email protected]54ec6472010-04-09 19:39:58790
[email protected]96d185d2009-04-24 03:28:54791 // Tell DevTools agent that it is attached prior to the navigation.
[email protected]d9f9b792009-06-24 13:17:12792 DevToolsManager* devtools_manager = DevToolsManager::GetInstance();
[email protected]7e8e6b62009-05-08 11:28:32793 if (devtools_manager) { // NULL in unit tests.
[email protected]3cc72b12010-03-18 23:03:00794 devtools_manager->OnNavigatingToPendingEntry(render_view_host(),
795 dest_render_view_host,
796 entry.url());
[email protected]7e8e6b62009-05-08 11:28:32797 }
[email protected]96d185d2009-04-24 03:28:54798
799 // Used for page load time metrics.
800 current_load_start_ = base::TimeTicks::Now();
801
802 // Navigate in the desired RenderViewHost.
[email protected]056de2d2009-06-26 16:41:34803 ViewMsg_Navigate_Params navigate_params;
[email protected]3cc72b12010-03-18 23:03:00804 MakeNavigateParams(controller_, reload_type, &navigate_params);
[email protected]056de2d2009-06-26 16:41:34805 dest_render_view_host->Navigate(navigate_params);
[email protected]96d185d2009-04-24 03:28:54806
807 if (entry.page_id() == -1) {
808 // HACK!! This code suppresses javascript: URLs from being added to
809 // session history, which is what we want to do for javascript: URLs that
810 // do not generate content. What we really need is a message from the
811 // renderer telling us that a new page was not created. The same message
812 // could be used for mailto: URLs and the like.
813 if (entry.url().SchemeIs(chrome::kJavaScriptScheme))
814 return false;
815 }
816
817 // Clear any provisional password saves - this stops password infobars
818 // showing up on pages the user navigates to while the right page is
819 // loading.
820 GetPasswordManager()->ClearProvisionalSave();
821
[email protected]1ccb3568d2010-02-19 10:51:16822 if (reload_type != NavigationController::NO_RELOAD &&
823 !profile()->IsOffTheRecord()) {
[email protected]0189bc722009-08-28 21:56:48824 FaviconService* favicon_service =
825 profile()->GetFaviconService(Profile::IMPLICIT_ACCESS);
826 if (favicon_service)
827 favicon_service->SetFaviconOutOfDateForPage(entry.url());
[email protected]96d185d2009-04-24 03:28:54828 }
829
[email protected]d5f942ba2008-09-26 19:30:34830 return true;
831}
832
[email protected]96d185d2009-04-24 03:28:54833void TabContents::Stop() {
834 render_manager_.Stop();
[email protected]12802702010-07-09 19:43:09835 printing_->Stop();
[email protected]96d185d2009-04-24 03:28:54836}
837
[email protected]96d185d2009-04-24 03:28:54838void TabContents::DisassociateFromPopupCount() {
839 render_view_host()->DisassociateFromPopupCount();
840}
841
842TabContents* TabContents::Clone() {
843 // We create a new SiteInstance so that the new tab won't share processes
844 // with the old one. This can be changed in the future if we need it to share
845 // processes for some reason.
[email protected]420ae012009-04-24 05:16:32846 TabContents* tc = new TabContents(profile(),
[email protected]96d185d2009-04-24 03:28:54847 SiteInstance::CreateSiteInstance(profile()),
[email protected]12636df2009-09-28 22:32:21848 MSG_ROUTING_NONE, this);
[email protected]96d185d2009-04-24 03:28:54849 tc->controller().CopyStateFrom(controller_);
[email protected]98aa0b52010-05-06 17:03:08850 tc->extension_app_ = extension_app_;
[email protected]98aa0b52010-05-06 17:03:08851 tc->extension_app_icon_ = extension_app_icon_;
[email protected]96d185d2009-04-24 03:28:54852 return tc;
853}
854
[email protected]4d677202009-07-19 07:37:12855void TabContents::ShowPageInfo(const GURL& url,
856 const NavigationEntry::SSLStatus& ssl,
857 bool show_history) {
858 if (!delegate_)
859 return;
860
[email protected]bb678332009-07-21 00:15:50861 delegate_->ShowPageInfo(profile(), url, ssl, show_history);
[email protected]4d677202009-07-19 07:37:12862}
863
initial.commit09911bf2008-07-26 23:55:29864ConstrainedWindow* TabContents::CreateConstrainedDialog(
[email protected]e8382172009-06-19 22:16:28865 ConstrainedWindowDelegate* delegate) {
initial.commit09911bf2008-07-26 23:55:29866 ConstrainedWindow* window =
[email protected]e8382172009-06-19 22:16:28867 ConstrainedWindow::CreateConstrainedDialog(this, delegate);
initial.commit09911bf2008-07-26 23:55:29868 child_windows_.push_back(window);
[email protected]fa1cf0b82010-01-15 21:49:44869
870 if (child_windows_.size() == 1) {
871 window->ShowConstrainedWindow();
872 BlockTabContent(true);
873 }
874
initial.commit09911bf2008-07-26 23:55:29875 return window;
876}
877
[email protected]fa1cf0b82010-01-15 21:49:44878void TabContents::BlockTabContent(bool blocked) {
[email protected]8dccd7cb2010-02-25 05:19:55879 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
880 if (rwhv)
881 rwhv->SetVisuallyDeemphasized(blocked);
[email protected]fa1cf0b82010-01-15 21:49:44882 render_view_host()->set_ignore_input_events(blocked);
883 if (delegate_)
884 delegate_->SetTabContentBlocked(this, blocked);
885}
886
initial.commit09911bf2008-07-26 23:55:29887void TabContents::AddNewContents(TabContents* new_contents,
888 WindowOpenDisposition disposition,
889 const gfx::Rect& initial_pos,
[email protected]7e7414ae2010-01-26 20:19:29890 bool user_gesture) {
initial.commit09911bf2008-07-26 23:55:29891 if (!delegate_)
892 return;
893
[email protected]35f7d212009-04-29 21:19:27894 if ((disposition == NEW_POPUP) && !user_gesture &&
895 !CommandLine::ForCurrentProcess()->HasSwitch(
896 switches::kDisablePopupBlocking)) {
[email protected]2c4410d2009-05-06 23:46:22897 // Unrequested popups from normal pages are constrained unless they're in
[email protected]b6c874582009-05-08 19:38:31898 // the whitelist. The popup owner will handle checking this.
[email protected]7e7414ae2010-01-26 20:19:29899 delegate_->GetConstrainingContents(this)->AddPopup(
900 new_contents, initial_pos);
[email protected]b6c874582009-05-08 19:38:31901 } else {
[email protected]304f8792009-08-19 21:28:09902#if defined(OS_CHROMEOS)
903 if (disposition == NEW_POPUP) {
904 // If the popup is bigger than a given factor of the screen, then
905 // turn it into a foreground tab (on chrome os only)
906 GdkScreen* screen = gdk_screen_get_default();
907 int max_width = gdk_screen_get_width(screen) * kMaxWidthFactor;
908 int max_height = gdk_screen_get_height(screen) * kMaxHeightFactor;
909 if (initial_pos.width() > max_width ||
910 initial_pos.height() > max_height) {
911 disposition = NEW_FOREGROUND_TAB;
912 }
913 }
914#endif
915
[email protected]0aa55312008-10-17 21:53:08916 new_contents->DisassociateFromPopupCount();
initial.commit09911bf2008-07-26 23:55:29917 delegate_->AddNewContents(this, new_contents, disposition, initial_pos,
918 user_gesture);
[email protected]7f37dd92009-09-15 16:35:49919 NotificationService::current()->Notify(
920 NotificationType::TAB_ADDED,
921 Source<TabContentsDelegate>(delegate_),
922 Details<TabContents>(this));
initial.commit09911bf2008-07-26 23:55:29923 }
initial.commit09911bf2008-07-26 23:55:29924
[email protected]8cc8d492010-02-02 10:40:49925 // TODO(pkasting): Why is this necessary?
926 PopupNotificationVisibilityChanged(blocked_popups_ != NULL);
initial.commit09911bf2008-07-26 23:55:29927}
928
[email protected]bcd9dc42010-01-23 16:38:18929bool TabContents::ExecuteCode(int request_id, const std::string& extension_id,
[email protected]61f5a7b2009-12-22 22:21:20930 const std::vector<URLPattern>& host_permissions,
[email protected]20ad2692009-11-20 18:27:20931 bool is_js_code, const std::string& code_string,
932 bool all_frames) {
[email protected]ebd0eae72010-02-16 23:50:51933 RenderViewHost* host = render_view_host();
934 if (!host)
935 return false;
936
937 return host->Send(new ViewMsg_ExecuteCode(host->routing_id(),
938 ViewMsg_ExecuteCode_Params(request_id, extension_id, host_permissions,
939 is_js_code, code_string, all_frames)));
[email protected]912256b32009-09-18 09:47:35940}
941
[email protected]96d185d2009-04-24 03:28:54942void TabContents::PopupNotificationVisibilityChanged(bool visible) {
[email protected]8cc8d492010-02-02 10:40:49943 if (is_being_destroyed_)
944 return;
[email protected]9fb83e82010-07-02 18:24:55945 content_settings_delegate_->SetPopupsBlocked(visible);
[email protected]8cc8d492010-02-02 10:40:49946 if (!dont_notify_render_view_)
[email protected]e8345242010-05-06 03:00:40947 render_view_host()->AllowScriptToClose(!visible);
[email protected]96d185d2009-04-24 03:28:54948}
949
[email protected]5c9e97a2009-09-09 23:48:30950gfx::NativeView TabContents::GetContentNativeView() const {
[email protected]96d185d2009-04-24 03:28:54951 return view_->GetContentNativeView();
952}
953
954gfx::NativeView TabContents::GetNativeView() const {
955 return view_->GetNativeView();
956}
957
958void TabContents::GetContainerBounds(gfx::Rect *out) const {
959 view_->GetContainerBounds(out);
960}
961
962void TabContents::Focus() {
963 view_->Focus();
964}
965
[email protected]90daadb42009-06-08 21:27:28966void TabContents::FocusThroughTabTraversal(bool reverse) {
[email protected]7e383692009-06-12 19:14:54967 if (showing_interstitial_page()) {
968 render_manager_.interstitial_page()->FocusThroughTabTraversal(reverse);
969 return;
970 }
[email protected]96d185d2009-04-24 03:28:54971 render_view_host()->SetInitialFocus(reverse);
972}
973
974bool TabContents::FocusLocationBarByDefault() {
975 DOMUI* dom_ui = GetDOMUIForCurrentState();
976 if (dom_ui)
977 return dom_ui->focus_location_bar_by_default();
[email protected]3e3f0eb2009-06-22 18:33:43978 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]e0d481582009-09-15 21:06:25979 if (entry && entry->url() == GURL(chrome::kAboutBlankURL))
[email protected]3e3f0eb2009-06-22 18:33:43980 return true;
[email protected]96d185d2009-04-24 03:28:54981 return false;
982}
983
[email protected]a26dc362010-04-23 01:48:58984void TabContents::SetFocusToLocationBar(bool select_all) {
[email protected]a11aaf22010-03-30 00:03:38985 if (delegate())
[email protected]a26dc362010-04-23 01:48:58986 delegate()->SetFocusToLocationBar(select_all);
[email protected]a11aaf22010-03-30 00:03:38987}
988
[email protected]616ed5a2008-11-21 22:27:24989void TabContents::AddInfoBar(InfoBarDelegate* delegate) {
[email protected]f7019302010-03-26 19:58:32990 if (delegate_ && !delegate_->infobars_enabled()) {
991 delegate->InfoBarClosed();
992 return;
993 }
994
[email protected]616ed5a2008-11-21 22:27:24995 // Look through the existing InfoBarDelegates we have for a match. If we've
996 // already got one that matches, then we don't add the new one.
[email protected]f86a07022008-11-25 01:06:05997 for (int i = 0; i < infobar_delegate_count(); ++i) {
[email protected]6d27a7b2009-12-18 23:25:45998 if (GetInfoBarDelegateAt(i)->EqualsDelegate(delegate)) {
999 // Tell the new infobar to close so that it can clean itself up.
1000 delegate->InfoBarClosed();
[email protected]616ed5a2008-11-21 22:27:241001 return;
[email protected]6d27a7b2009-12-18 23:25:451002 }
[email protected]616ed5a2008-11-21 22:27:241003 }
1004
1005 infobar_delegates_.push_back(delegate);
[email protected]bfd04a62009-02-01 18:16:561006 NotificationService::current()->Notify(
1007 NotificationType::TAB_CONTENTS_INFOBAR_ADDED,
1008 Source<TabContents>(this),
1009 Details<InfoBarDelegate>(delegate));
[email protected]616ed5a2008-11-21 22:27:241010
1011 // Add ourselves as an observer for navigations the first time a delegate is
1012 // added. We use this notification to expire InfoBars that need to expire on
1013 // page transitions.
1014 if (infobar_delegates_.size() == 1) {
[email protected]bfd04a62009-02-01 18:16:561015 registrar_.Add(this, NotificationType::NAV_ENTRY_COMMITTED,
[email protected]ce3fa3c2009-04-20 19:55:571016 Source<NavigationController>(&controller_));
[email protected]616ed5a2008-11-21 22:27:241017 }
1018}
1019
1020void TabContents::RemoveInfoBar(InfoBarDelegate* delegate) {
[email protected]f7019302010-03-26 19:58:321021 if (delegate_ && !delegate_->infobars_enabled()) {
1022 return;
1023 }
1024
[email protected]616ed5a2008-11-21 22:27:241025 std::vector<InfoBarDelegate*>::iterator it =
1026 find(infobar_delegates_.begin(), infobar_delegates_.end(), delegate);
1027 if (it != infobar_delegates_.end()) {
1028 InfoBarDelegate* delegate = *it;
[email protected]bfd04a62009-02-01 18:16:561029 NotificationService::current()->Notify(
1030 NotificationType::TAB_CONTENTS_INFOBAR_REMOVED,
1031 Source<TabContents>(this),
1032 Details<InfoBarDelegate>(delegate));
[email protected]f86a07022008-11-25 01:06:051033 infobar_delegates_.erase(it);
[email protected]616ed5a2008-11-21 22:27:241034
[email protected]6a02963e2009-01-06 16:58:031035 // Remove ourselves as an observer if we are tracking no more InfoBars.
1036 if (infobar_delegates_.empty()) {
[email protected]bfd04a62009-02-01 18:16:561037 registrar_.Remove(this, NotificationType::NAV_ENTRY_COMMITTED,
[email protected]ce3fa3c2009-04-20 19:55:571038 Source<NavigationController>(&controller_));
[email protected]6a02963e2009-01-06 16:58:031039 }
[email protected]616ed5a2008-11-21 22:27:241040 }
1041}
[email protected]3edd9522009-03-04 22:19:411042
[email protected]f81821c2009-08-07 01:29:471043void TabContents::ReplaceInfoBar(InfoBarDelegate* old_delegate,
1044 InfoBarDelegate* new_delegate) {
[email protected]f7019302010-03-26 19:58:321045 if (delegate_ && !delegate_->infobars_enabled()) {
1046 new_delegate->InfoBarClosed();
1047 return;
1048 }
1049
[email protected]f81821c2009-08-07 01:29:471050 std::vector<InfoBarDelegate*>::iterator it =
1051 find(infobar_delegates_.begin(), infobar_delegates_.end(), old_delegate);
1052 DCHECK(it != infobar_delegates_.end());
1053
1054 // Notify the container about the change of plans.
1055 scoped_ptr<std::pair<InfoBarDelegate*, InfoBarDelegate*> > details(
1056 new std::pair<InfoBarDelegate*, InfoBarDelegate*>(
1057 old_delegate, new_delegate));
1058 NotificationService::current()->Notify(
1059 NotificationType::TAB_CONTENTS_INFOBAR_REPLACED,
1060 Source<TabContents>(this),
1061 Details<std::pair<InfoBarDelegate*, InfoBarDelegate*> >(details.get()));
1062
1063 // Remove the old one.
1064 infobar_delegates_.erase(it);
1065
1066 // Add the new one.
1067 infobar_delegates_.push_back(new_delegate);
1068}
1069
[email protected]20ede072009-10-06 00:28:351070bool TabContents::ShouldShowBookmarkBar() {
[email protected]aa9196f2009-10-22 16:20:271071 if (showing_interstitial_page())
1072 return false;
1073
[email protected]96d185d2009-04-24 03:28:541074 // See GetDOMUIForCurrentState() comment for more info. This case is very
1075 // similar, but for non-first loads, we want to use the committed entry. This
1076 // is so the bookmarks bar disappears at the same time the page does.
1077 if (controller_.GetLastCommittedEntry()) {
1078 // Not the first load, always use the committed DOM UI.
1079 if (render_manager_.dom_ui())
1080 return render_manager_.dom_ui()->force_bookmark_bar_visible();
1081 return false; // Default.
1082 }
1083
1084 // When it's the first load, we know either the pending one or the committed
1085 // one will have the DOM UI in it (see GetDOMUIForCurrentState), and only one
1086 // of them will be valid, so we can just check both.
1087 if (render_manager_.pending_dom_ui())
1088 return render_manager_.pending_dom_ui()->force_bookmark_bar_visible();
1089 if (render_manager_.dom_ui())
1090 return render_manager_.dom_ui()->force_bookmark_bar_visible();
1091 return false; // Default.
1092}
1093
[email protected]d8f16ae2009-08-30 03:47:261094bool TabContents::IsExtensionShelfAlwaysVisible() {
1095 // See GetDOMUIForCurrentState() comment for more info. This case is very
1096 // similar, but for non-first loads, we want to use the committed entry. This
1097 // is so the bookmarks bar disappears at the same time the page does.
1098 if (controller_.GetLastCommittedEntry()) {
1099 // Not the first load, always use the committed DOM UI.
1100 if (render_manager_.dom_ui())
1101 return render_manager_.dom_ui()->force_extension_shelf_visible();
1102 return false; // Default.
1103 }
1104
1105 // When it's the first load, we know either the pending one or the committed
1106 // one will have the DOM UI in it (see GetDOMUIForCurrentState), and only one
1107 // of them will be valid, so we can just check both.
1108 if (render_manager_.pending_dom_ui())
1109 return render_manager_.pending_dom_ui()->force_extension_shelf_visible();
1110 if (render_manager_.dom_ui())
1111 return render_manager_.dom_ui()->force_extension_shelf_visible();
1112 return false; // Default.
1113}
1114
[email protected]3edd9522009-03-04 22:19:411115void TabContents::ToolbarSizeChanged(bool is_animating) {
1116 TabContentsDelegate* d = delegate();
1117 if (d)
1118 d->ToolbarSizeChanged(this, is_animating);
1119}
[email protected]616ed5a2008-11-21 22:27:241120
[email protected]fc6fb7fb2009-11-07 02:35:041121bool TabContents::CanDownload(int request_id) {
1122 TabContentsDelegate* d = delegate();
1123 if (d)
1124 return d->CanDownload(request_id);
1125 return true;
1126}
1127
[email protected]d5f942ba2008-09-26 19:30:341128void TabContents::OnStartDownload(DownloadItem* download) {
1129 DCHECK(download);
[email protected]d5f942ba2008-09-26 19:30:341130
1131 // Download in a constrained popup is shown in the tab that opened it.
[email protected]b6c874582009-05-08 19:38:311132 TabContents* tab_contents = delegate()->GetConstrainingContents(this);
[email protected]d5f942ba2008-09-26 19:30:341133
[email protected]c70f9b82010-04-21 07:31:111134 if (tab_contents && tab_contents->delegate()) {
[email protected]59560e0b2009-06-04 03:30:221135 tab_contents->delegate()->OnStartDownload(download);
[email protected]c70f9b82010-04-21 07:31:111136 // If the download occurs in a new tab, close it
1137 if (controller_.IsInitialNavigation() && (tab_contents == this))
1138 delegate()->CloseContents(this);
1139 }
[email protected]3edd9522009-03-04 22:19:411140}
[email protected]3edd9522009-03-04 22:19:411141
[email protected]d5f942ba2008-09-26 19:30:341142void TabContents::WillClose(ConstrainedWindow* window) {
1143 ConstrainedWindowList::iterator it =
1144 find(child_windows_.begin(), child_windows_.end(), window);
[email protected]fa1cf0b82010-01-15 21:49:441145 bool removed_topmost_window = it == child_windows_.begin();
[email protected]d5f942ba2008-09-26 19:30:341146 if (it != child_windows_.end())
1147 child_windows_.erase(it);
[email protected]55098792010-03-24 18:20:371148 if (child_windows_.size() > 0) {
1149 if (removed_topmost_window) {
1150 child_windows_[0]->ShowConstrainedWindow();
1151 }
[email protected]fa1cf0b82010-01-15 21:49:441152 BlockTabContent(true);
1153 } else {
1154 BlockTabContent(false);
1155 }
[email protected]66ba4932009-06-04 19:22:131156}
[email protected]d5f942ba2008-09-26 19:30:341157
[email protected]66ba4932009-06-04 19:22:131158void TabContents::WillCloseBlockedPopupContainer(
1159 BlockedPopupContainer* container) {
1160 DCHECK(blocked_popups_ == container);
1161 blocked_popups_ = NULL;
[email protected]8cc8d492010-02-02 10:40:491162 PopupNotificationVisibilityChanged(false);
[email protected]d5f942ba2008-09-26 19:30:341163}
1164
[email protected]d5f942ba2008-09-26 19:30:341165void TabContents::DidMoveOrResize(ConstrainedWindow* window) {
[email protected]332af7732009-03-11 13:21:351166#if defined(OS_WIN)
[email protected]92edc472009-02-10 20:32:061167 UpdateWindow(GetNativeView());
[email protected]332af7732009-03-11 13:21:351168#endif
[email protected]d5f942ba2008-09-26 19:30:341169}
1170
[email protected]c1dc9f132009-11-20 23:13:211171void TabContents::StartFinding(string16 search_string,
[email protected]e491f1c2009-05-22 20:28:121172 bool forward_direction,
1173 bool case_sensitive) {
[email protected]c1dc9f132009-11-20 23:13:211174 // If search_string is empty, it means FindNext was pressed with a keyboard
[email protected]6e95706d2009-04-23 22:55:011175 // shortcut so unless we have something to search for we return early.
[email protected]c1dc9f132009-11-20 23:13:211176 if (search_string.empty() && find_text_.empty()) {
[email protected]e5d2f7132010-04-09 18:13:531177 string16 last_search_prepopulate_text =
1178 FindBarState::GetLastPrepopulateText(profile());
1179
[email protected]87fae512009-12-11 02:20:211180 // Try the last thing we searched for on this tab, then the last thing
1181 // searched for on any tab.
1182 if (!previous_find_text_.empty())
1183 search_string = previous_find_text_;
[email protected]e5d2f7132010-04-09 18:13:531184 else if (!last_search_prepopulate_text.empty())
1185 search_string = last_search_prepopulate_text;
[email protected]87fae512009-12-11 02:20:211186 else
[email protected]e491f1c2009-05-22 20:28:121187 return;
[email protected]e491f1c2009-05-22 20:28:121188 }
[email protected]6e95706d2009-04-23 22:55:011189
[email protected]101c90e2009-10-26 21:14:441190 // Keep track of the previous search.
1191 previous_find_text_ = find_text_;
1192
[email protected]6e95706d2009-04-23 22:55:011193 // This is a FindNext operation if we are searching for the same text again,
1194 // or if the passed in search text is empty (FindNext keyboard shortcut). The
1195 // exception to this is if the Find was aborted (then we don't want FindNext
1196 // because the highlighting has been cleared and we need it to reappear). We
1197 // therefore treat FindNext after an aborted Find operation as a full fledged
1198 // Find.
[email protected]c1dc9f132009-11-20 23:13:211199 bool find_next = (find_text_ == search_string || search_string.empty()) &&
[email protected]e491f1c2009-05-22 20:28:121200 (last_search_case_sensitive_ == case_sensitive) &&
[email protected]6e95706d2009-04-23 22:55:011201 !find_op_aborted_;
1202 if (!find_next)
1203 current_find_request_id_ = find_request_id_counter_++;
1204
[email protected]c1dc9f132009-11-20 23:13:211205 if (!search_string.empty())
1206 find_text_ = search_string;
[email protected]e491f1c2009-05-22 20:28:121207 last_search_case_sensitive_ = case_sensitive;
[email protected]6e95706d2009-04-23 22:55:011208
1209 find_op_aborted_ = false;
1210
1211 // Keep track of what the last search was across the tabs.
[email protected]e5d2f7132010-04-09 18:13:531212 FindBarState* find_bar_state = profile()->GetFindBarState();
1213 find_bar_state->set_last_prepopulate_text(find_text_);
[email protected]6e95706d2009-04-23 22:55:011214 render_view_host()->StartFinding(current_find_request_id_,
1215 find_text_,
1216 forward_direction,
[email protected]e491f1c2009-05-22 20:28:121217 case_sensitive,
[email protected]6e95706d2009-04-23 22:55:011218 find_next);
1219}
1220
[email protected]5bc8fe92010-03-11 18:19:001221void TabContents::StopFinding(
1222 FindBarController::SelectionAction selection_action) {
[email protected]d6e8d332010-06-04 22:26:011223 if (selection_action == FindBarController::kClearSelection) {
1224 // kClearSelection means the find string has been cleared by the user, but
1225 // the UI has not been dismissed. In that case we want to clear the
1226 // previously remembered search (http://crbug.com/42639).
1227 previous_find_text_ = string16();
1228 } else {
[email protected]2d2dffc2009-06-09 21:41:001229 find_ui_active_ = false;
[email protected]d6e8d332010-06-04 22:26:011230 if (!find_text_.empty())
1231 previous_find_text_ = find_text_;
1232 }
[email protected]101c90e2009-10-26 21:14:441233 find_text_.clear();
[email protected]6e95706d2009-04-23 22:55:011234 find_op_aborted_ = true;
[email protected]e491f1c2009-05-22 20:28:121235 last_search_result_ = FindNotificationDetails();
[email protected]5bc8fe92010-03-11 18:19:001236 render_view_host()->StopFinding(selection_action);
[email protected]6e95706d2009-04-23 22:55:011237}
1238
[email protected]420ae012009-04-24 05:16:321239void TabContents::OnSavePage() {
1240 // If we can not save the page, try to download it.
1241 if (!SavePackage::IsSavableContents(contents_mime_type())) {
1242 DownloadManager* dlm = profile()->GetDownloadManager();
1243 const GURL& current_page_url = GetURL();
1244 if (dlm && current_page_url.is_valid())
[email protected]d82ed61e2009-06-16 02:46:221245 dlm->DownloadUrl(current_page_url, GURL(), "", this);
[email protected]420ae012009-04-24 05:16:321246 return;
1247 }
1248
1249 Stop();
1250
1251 // Create the save package and possibly prompt the user for the name to save
1252 // the page as. The user prompt is an asynchronous operation that runs on
1253 // another thread.
[email protected]d82ed61e2009-06-16 02:46:221254 save_package_ = new SavePackage(this);
[email protected]420ae012009-04-24 05:16:321255 save_package_->GetSaveInfo();
1256}
1257
1258// Used in automated testing to bypass prompting the user for file names.
1259// Instead, the names and paths are hard coded rather than running them through
1260// file name sanitation and extension / mime checking.
[email protected]828cabe2009-09-26 22:47:111261bool TabContents::SavePage(const FilePath& main_file, const FilePath& dir_path,
[email protected]420ae012009-04-24 05:16:321262 SavePackage::SavePackageType save_type) {
1263 // Stop the page from navigating.
1264 Stop();
1265
[email protected]828cabe2009-09-26 22:47:111266 save_package_ = new SavePackage(this, save_type, main_file, dir_path);
[email protected]cdf6cc82009-08-04 21:48:591267 return save_package_->Init();
[email protected]420ae012009-04-24 05:16:321268}
1269
[email protected]89d6e6e2009-12-03 23:06:511270void TabContents::EmailPageLocation() {
1271 std::string title = EscapeQueryParamValue(UTF16ToUTF8(GetTitle()), false);
1272 std::string page_url = EscapeQueryParamValue(GetURL().spec(), false);
1273 std::string mailto = std::string("mailto:?subject=Fwd:%20") +
1274 title + "&body=%0A%0A" + page_url;
1275 platform_util::OpenExternal(GURL(mailto));
1276}
1277
[email protected]420ae012009-04-24 05:16:321278void TabContents::PrintPreview() {
1279 // We don't show the print preview yet, only the print dialog.
1280 PrintNow();
1281}
1282
1283bool TabContents::PrintNow() {
1284 // We can't print interstitial page for now.
1285 if (showing_interstitial_page())
1286 return false;
1287
1288 return render_view_host()->PrintPages();
1289}
1290
[email protected]82270452009-06-19 15:58:011291void TabContents::PrintingDone(int document_cookie, bool success) {
1292 render_view_host()->PrintingDone(document_cookie, success);
1293}
1294
[email protected]420ae012009-04-24 05:16:321295bool TabContents::IsActiveEntry(int32 page_id) {
1296 NavigationEntry* active_entry = controller_.GetActiveEntry();
1297 return (active_entry != NULL &&
1298 active_entry->site_instance() == GetSiteInstance() &&
1299 active_entry->page_id() == page_id);
1300}
1301
[email protected]b2fe07d12010-02-09 14:38:081302void TabContents::SetOverrideEncoding(const std::string& encoding) {
[email protected]8cb5d5b2010-02-09 11:36:161303 set_encoding(encoding);
1304 render_view_host()->SetPageEncoding(encoding);
1305}
1306
[email protected]b2fe07d12010-02-09 14:38:081307void TabContents::ResetOverrideEncoding() {
[email protected]8cb5d5b2010-02-09 11:36:161308 reset_encoding();
1309 render_view_host()->ResetPageEncodingToDefault();
1310}
1311
1312void TabContents::WindowMoveOrResizeStarted() {
1313 render_view_host()->WindowMoveOrResizeStarted();
1314}
1315
[email protected]3f8eb7f2009-10-02 23:58:051316void TabContents::LogNewTabTime(const std::string& event_name) {
1317 // Not all new tab pages get timed. In those cases, we don't have a
1318 // new_tab_start_time_.
1319 if (new_tab_start_time_.is_null())
1320 return;
1321
1322 base::TimeDelta duration = base::TimeTicks::Now() - new_tab_start_time_;
[email protected]84abba62009-10-07 17:01:441323 MetricEventDurationDetails details(event_name,
1324 static_cast<int>(duration.InMilliseconds()));
1325
[email protected]7ab1e7d62009-10-14 23:32:011326 if (event_name == "Tab.NewTabScriptStart") {
1327 UMA_HISTOGRAM_TIMES("Tab.NewTabScriptStart", duration);
1328 } else if (event_name == "Tab.NewTabDOMContentLoaded") {
1329 UMA_HISTOGRAM_TIMES("Tab.NewTabDOMContentLoaded", duration);
1330 } else if (event_name == "Tab.NewTabOnload") {
1331 UMA_HISTOGRAM_TIMES("Tab.NewTabOnload", duration);
[email protected]3f8eb7f2009-10-02 23:58:051332 // The new tab page has finished loading; reset it.
1333 new_tab_start_time_ = base::TimeTicks();
[email protected]84abba62009-10-07 17:01:441334 } else {
1335 NOTREACHED();
[email protected]3f8eb7f2009-10-02 23:58:051336 }
[email protected]84abba62009-10-07 17:01:441337 NotificationService::current()->Notify(
1338 NotificationType::METRIC_EVENT_DURATION,
1339 Source<TabContents>(this),
1340 Details<MetricEventDurationDetails>(&details));
[email protected]3f8eb7f2009-10-02 23:58:051341}
1342
[email protected]7ab1e7d62009-10-14 23:32:011343void TabContents::OnCloseStarted() {
1344 if (tab_close_start_time_.is_null())
1345 tab_close_start_time_ = base::TimeTicks::Now();
1346}
1347
[email protected]b5558cf22010-07-12 17:30:061348void TabContents::set_request_context(URLRequestContextGetter* context) {
1349 request_context_ = context;
1350}
1351
[email protected]46624bf2010-06-09 16:04:191352bool TabContents::ShouldAcceptDragAndDrop() const {
1353#if defined(OS_CHROMEOS)
1354 // ChromeOS panels (pop-ups) do not take drag-n-drop.
1355 // See http://crosbug.com/2413
1356 return delegate() && !delegate()->IsPopup(const_cast<TabContents*>(this));
1357#else
1358 return true;
1359#endif
1360}
1361
[email protected]c37e3b62010-01-22 00:15:171362TabContents* TabContents::CloneAndMakePhantom() {
[email protected]c37e3b62010-01-22 00:15:171363 TabNavigation tab_nav;
[email protected]df72ac72010-02-06 00:43:161364
1365 NavigationEntry* entry = controller().GetActiveEntry();
[email protected]98aa0b52010-05-06 17:03:081366 if (extension_app_)
1367 tab_nav.set_virtual_url(extension_app_->GetFullLaunchURL());
[email protected]df72ac72010-02-06 00:43:161368 else if (entry)
1369 tab_nav.SetFromNavigationEntry(*entry);
1370
[email protected]c37e3b62010-01-22 00:15:171371 std::vector<TabNavigation> navigations;
1372 navigations.push_back(tab_nav);
1373
1374 TabContents* new_contents =
1375 new TabContents(profile(), NULL, MSG_ROUTING_NONE, NULL);
[email protected]98aa0b52010-05-06 17:03:081376 new_contents->SetExtensionApp(extension_app_);
[email protected]c37e3b62010-01-22 00:15:171377 new_contents->controller().RestoreFromState(navigations, 0, false);
[email protected]c37e3b62010-01-22 00:15:171378
[email protected]98aa0b52010-05-06 17:03:081379 if (!extension_app_ && entry)
[email protected]df72ac72010-02-06 00:43:161380 new_contents->controller().GetActiveEntry()->favicon() = entry->favicon();
1381
[email protected]c37e3b62010-01-22 00:15:171382 return new_contents;
1383}
1384
[email protected]96d185d2009-04-24 03:28:541385// Notifies the RenderWidgetHost instance about the fact that the page is
1386// loading, or done loading and calls the base implementation.
1387void TabContents::SetIsLoading(bool is_loading,
1388 LoadNotificationDetails* details) {
1389 if (is_loading == is_loading_)
1390 return;
1391
1392 if (!is_loading) {
1393 load_state_ = net::LOAD_STATE_IDLE;
1394 load_state_host_.clear();
[email protected]094e5b22009-09-25 04:23:561395 upload_size_ = 0;
1396 upload_position_ = 0;
[email protected]96d185d2009-04-24 03:28:541397 }
1398
1399 render_manager_.SetIsLoading(is_loading);
1400
1401 is_loading_ = is_loading;
1402 waiting_for_response_ = is_loading;
1403
1404 if (delegate_)
1405 delegate_->LoadingStateChanged(this);
[email protected]329581b2009-04-28 06:52:351406 NotifyNavigationStateChanged(INVALIDATE_LOAD);
[email protected]96d185d2009-04-24 03:28:541407
1408 NotificationType type = is_loading ? NotificationType::LOAD_START :
1409 NotificationType::LOAD_STOP;
[email protected]ce5c4502009-05-06 16:46:111410 NotificationDetails det = NotificationService::NoDetails();
[email protected]96d185d2009-04-24 03:28:541411 if (details)
1412 det = Details<LoadNotificationDetails>(details);
1413 NotificationService::current()->Notify(type,
1414 Source<NavigationController>(&controller_),
1415 det);
1416}
1417
[email protected]b6c874582009-05-08 19:38:311418void TabContents::AddPopup(TabContents* new_contents,
[email protected]7e7414ae2010-01-26 20:19:291419 const gfx::Rect& initial_pos) {
[email protected]57eda82a2010-05-05 21:28:061420 // A page can't spawn popups (or do anything else, either) until its load
1421 // commits, so when we reach here, the popup was spawned by the
1422 // NavigationController's last committed entry, not the active entry. For
1423 // example, if a page opens a popup in an onunload() handler, then the active
1424 // entry is the page to be loaded as we navigate away from the unloading
1425 // page. For this reason, we can't use GetURL() to get the opener URL,
1426 // because it returns the active entry.
1427 NavigationEntry* entry = controller_.GetLastCommittedEntry();
1428 GURL creator = entry ? entry->virtual_url() : GURL::EmptyGURL();
1429
1430 if (creator.is_valid() &&
[email protected]8cc8d492010-02-02 10:40:491431 profile()->GetHostContentSettingsMap()->GetContentSetting(
[email protected]57eda82a2010-05-05 21:28:061432 creator, CONTENT_SETTINGS_TYPE_POPUPS) == CONTENT_SETTING_ALLOW) {
[email protected]8cc8d492010-02-02 10:40:491433 AddNewContents(new_contents, NEW_POPUP, initial_pos, true);
1434 } else {
1435 if (!blocked_popups_)
1436 blocked_popups_ = new BlockedPopupContainer(this);
1437 blocked_popups_->AddTabContents(new_contents, initial_pos);
[email protected]9fb83e82010-07-02 18:24:551438 content_settings_delegate_->OnContentBlocked(CONTENT_SETTINGS_TYPE_POPUPS);
[email protected]8cc8d492010-02-02 10:40:491439 }
[email protected]b9681312008-11-07 00:08:261440}
[email protected]616ed5a2008-11-21 22:27:241441
1442namespace {
1443bool TransitionIsReload(PageTransition::Type transition) {
1444 return PageTransition::StripQualifier(transition) == PageTransition::RELOAD;
1445}
1446}
1447
1448void TabContents::ExpireInfoBars(
1449 const NavigationController::LoadCommittedDetails& details) {
1450 // Only hide InfoBars when the user has done something that makes the main
1451 // frame load. We don't want various automatic or subframe navigations making
1452 // it disappear.
1453 if (!details.is_user_initiated_main_frame_load())
1454 return;
1455
[email protected]f86a07022008-11-25 01:06:051456 for (int i = infobar_delegate_count() - 1; i >= 0; --i) {
[email protected]616ed5a2008-11-21 22:27:241457 InfoBarDelegate* delegate = GetInfoBarDelegateAt(i);
[email protected]f86a07022008-11-25 01:06:051458 if (delegate->ShouldExpire(details))
[email protected]616ed5a2008-11-21 22:27:241459 RemoveInfoBar(delegate);
[email protected]616ed5a2008-11-21 22:27:241460 }
1461}
[email protected]fdd61c62009-04-22 19:22:571462
[email protected]96d185d2009-04-24 03:28:541463DOMUI* TabContents::GetDOMUIForCurrentState() {
1464 // When there is a pending navigation entry, we want to use the pending DOMUI
1465 // that goes along with it to control the basic flags. For example, we want to
1466 // show the pending URL in the URL bar, so we want the display_url flag to
1467 // be from the pending entry.
1468 //
1469 // The confusion comes because there are multiple possibilities for the
1470 // initial load in a tab as a side effect of the way the RenderViewHostManager
1471 // works.
1472 //
1473 // - For the very first tab the load looks "normal". The new tab DOM UI is
1474 // the pending one, and we want it to apply here.
1475 //
1476 // - For subsequent new tabs, they'll get a new SiteInstance which will then
1477 // get switched to the one previously associated with the new tab pages.
1478 // This switching will cause the manager to commit the RVH/DOMUI. So we'll
1479 // have a committed DOM UI in this case.
1480 //
1481 // This condition handles all of these cases:
1482 //
1483 // - First load in first tab: no committed nav entry + pending nav entry +
1484 // pending dom ui:
1485 // -> Use pending DOM UI if any.
1486 //
1487 // - First load in second tab: no committed nav entry + pending nav entry +
1488 // no pending DOM UI:
1489 // -> Use the committed DOM UI if any.
1490 //
1491 // - Second navigation in any tab: committed nav entry + pending nav entry:
1492 // -> Use pending DOM UI if any.
1493 //
1494 // - Normal state with no load: committed nav entry + no pending nav entry:
1495 // -> Use committed DOM UI.
1496 if (controller_.pending_entry() &&
1497 (controller_.GetLastCommittedEntry() ||
1498 render_manager_.pending_dom_ui()))
1499 return render_manager_.pending_dom_ui();
1500 return render_manager_.dom_ui();
1501}
[email protected]420ae012009-04-24 05:16:321502
1503void TabContents::DidNavigateMainFramePostCommit(
1504 const NavigationController::LoadCommittedDetails& details,
1505 const ViewHostMsg_FrameNavigate_Params& params) {
[email protected]c2e74fe82009-09-03 17:57:441506 if (opener_dom_ui_type_ != DOMUIFactory::kNoDOMUI) {
1507 // If this is a window.open navigation, use the same DOMUI as the renderer
1508 // that opened the window, as long as both renderers have the same
1509 // privileges.
1510 if (opener_dom_ui_type_ == DOMUIFactory::GetDOMUIType(GetURL())) {
1511 DOMUI* dom_ui = DOMUIFactory::CreateDOMUIForURL(this, GetURL());
1512 // dom_ui might be NULL if the URL refers to a non-existent extension.
1513 if (dom_ui) {
1514 render_manager_.SetDOMUIPostCommit(dom_ui);
1515 dom_ui->RenderViewCreated(render_view_host());
1516 }
1517 }
1518 opener_dom_ui_type_ = DOMUIFactory::kNoDOMUI;
1519 }
1520
[email protected]420ae012009-04-24 05:16:321521 if (details.is_user_initiated_main_frame_load()) {
1522 // Clear the status bubble. This is a workaround for a bug where WebKit
1523 // doesn't let us know that the cursor left an element during a
1524 // transition (this is also why the mouse cursor remains as a hand after
1525 // clicking on a link); see bugs 1184641 and 980803. We don't want to
1526 // clear the bubble when a user navigates to a named anchor in the same
1527 // page.
1528 UpdateTargetURL(details.entry->page_id(), GURL());
1529
1530 // UpdateHelpersForDidNavigate will handle the case where the password_form
1531 // origin is valid.
1532 // TODO(brettw) bug 1343111: Password manager stuff in here needs to be
1533 // cleaned up and covered by tests.
1534 if (!params.password_form.origin.is_valid())
1535 GetPasswordManager()->DidNavigate();
1536 }
1537
1538 // The keyword generator uses the navigation entries, so must be called after
1539 // the commit.
1540 GenerateKeywordIfNecessary(params);
1541
1542 // Allow the new page to set the title again.
1543 received_page_title_ = false;
1544
1545 // Get the favicon, either from history or request it from the net.
1546 fav_icon_helper_.FetchFavIcon(details.entry->url());
1547
[email protected]0de80162010-02-03 04:52:351548 // Clear all page actions, blocked content notifications and browser actions
1549 // for this tab, unless this is an in-page navigation.
[email protected]a6e82fc2010-02-24 22:28:141550 if (!details.is_in_page) {
[email protected]92c6f9b92009-10-24 04:35:081551 ExtensionsService* service = profile()->GetExtensionsService();
1552 if (service) {
1553 for (size_t i = 0; i < service->extensions()->size(); ++i) {
[email protected]5d246db22009-10-27 06:17:571554 ExtensionAction* browser_action =
[email protected]92c6f9b92009-10-24 04:35:081555 service->extensions()->at(i)->browser_action();
[email protected]56ce6e52009-10-27 00:10:521556 if (browser_action) {
1557 browser_action->ClearAllValuesForTab(controller().session_id().id());
1558 NotificationService::current()->Notify(
1559 NotificationType::EXTENSION_BROWSER_ACTION_UPDATED,
[email protected]5d246db22009-10-27 06:17:571560 Source<ExtensionAction>(browser_action),
[email protected]56ce6e52009-10-27 00:10:521561 NotificationService::NoDetails());
1562 }
[email protected]92c6f9b92009-10-24 04:35:081563
[email protected]5d246db22009-10-27 06:17:571564 ExtensionAction* page_action =
[email protected]56ce6e52009-10-27 00:10:521565 service->extensions()->at(i)->page_action();
1566 if (page_action) {
1567 page_action->ClearAllValuesForTab(controller().session_id().id());
1568 PageActionStateChanged();
1569 }
[email protected]92c6f9b92009-10-24 04:35:081570 }
[email protected]aece2c7f2009-08-27 20:43:171571 }
[email protected]0de80162010-02-03 04:52:351572
1573 // Close blocked popups.
1574 if (blocked_popups_) {
[email protected]0fbd70332010-06-01 19:28:341575 AutoReset<bool> auto_reset(&dont_notify_render_view_, true);
[email protected]0de80162010-02-03 04:52:351576 blocked_popups_->Destroy();
[email protected]da7b5262010-02-25 21:12:461577 blocked_popups_ = NULL;
[email protected]0de80162010-02-03 04:52:351578 }
1579
1580 // Clear "blocked" flags.
[email protected]9fb83e82010-07-02 18:24:551581 content_settings_delegate_->ClearBlockedContentSettings();
1582 content_settings_delegate_->GeolocationDidNavigate(details);
[email protected]f17a0ee2010-05-17 17:38:471583
1584 // Once the main frame is navigated, we're no longer considered to have
1585 // displayed insecure content.
1586 displayed_insecure_content_ = false;
[email protected]aece2c7f2009-08-27 20:43:171587 }
[email protected]ce5c4502009-05-06 16:46:111588
[email protected]8cc8d492010-02-02 10:40:491589 // Close constrained windows if necessary.
1590 if (!net::RegistryControlledDomainService::SameDomainOrHost(
1591 details.previous_url, details.entry->url()))
1592 CloseConstrainedWindows();
1593
[email protected]420ae012009-04-24 05:16:321594 // Update the starred state.
1595 UpdateStarredStateForCurrentURL();
[email protected]c07c9752010-03-12 05:57:531596
1597 // Clear the cache of forms in AutoFill.
1598 if (autofill_manager_.get())
1599 autofill_manager_->Reset();
[email protected]420ae012009-04-24 05:16:321600}
1601
1602void TabContents::DidNavigateAnyFramePostCommit(
1603 RenderViewHost* render_view_host,
1604 const NavigationController::LoadCommittedDetails& details,
1605 const ViewHostMsg_FrameNavigate_Params& params) {
1606 // If we navigate, start showing messages again. This does nothing to prevent
1607 // a malicious script from spamming messages, since the script could just
1608 // reload the page to stop blocking.
1609 suppress_javascript_messages_ = false;
1610
[email protected]420ae012009-04-24 05:16:321611 // Notify the password manager of the navigation or form submit.
1612 // TODO(brettw) bug 1343111: Password manager stuff in here needs to be
1613 // cleaned up and covered by tests.
1614 if (params.password_form.origin.is_valid())
1615 GetPasswordManager()->ProvisionallySavePassword(params.password_form);
[email protected]3b1c1812010-02-12 21:53:551616
1617 // Let the LanguageState clear its state.
[email protected]34cec8d82010-03-05 18:27:011618 bool reload = details.entry->transition_type() == PageTransition::RELOAD ||
1619 details.type == NavigationType::SAME_PAGE;
1620 language_state_.DidNavigate(reload, details.is_in_page);
[email protected]420ae012009-04-24 05:16:321621}
1622
[email protected]8cc8d492010-02-02 10:40:491623void TabContents::CloseConstrainedWindows() {
1624 // Clear out any constrained windows since we are leaving this page entirely.
[email protected]420ae012009-04-24 05:16:321625 // We use indices instead of iterators in case CloseWindow does something
1626 // that may invalidate an iterator.
1627 int size = static_cast<int>(child_windows_.size());
1628 for (int i = size - 1; i >= 0; --i) {
1629 ConstrainedWindow* window = child_windows_[i];
[email protected]fa1cf0b82010-01-15 21:49:441630 if (window) {
[email protected]420ae012009-04-24 05:16:321631 window->CloseConstrainedWindow();
[email protected]fa1cf0b82010-01-15 21:49:441632 BlockTabContent(false);
1633 }
[email protected]420ae012009-04-24 05:16:321634 }
1635}
1636
1637void TabContents::UpdateStarredStateForCurrentURL() {
1638 BookmarkModel* model = profile()->GetBookmarkModel();
1639 const bool old_state = is_starred_;
1640 is_starred_ = (model && model->IsBookmarked(GetURL()));
1641
1642 if (is_starred_ != old_state && delegate())
1643 delegate()->URLStarredChanged(this, is_starred_);
1644}
1645
1646void TabContents::UpdateAlternateErrorPageURL() {
1647 GURL url = GetAlternateErrorPageURL();
1648 render_view_host()->SetAlternateErrorPageURL(url);
1649}
1650
1651void TabContents::UpdateWebPreferences() {
1652 render_view_host()->UpdateWebPreferences(GetWebkitPrefs());
1653}
1654
1655void TabContents::UpdateMaxPageIDIfNecessary(SiteInstance* site_instance,
1656 RenderViewHost* rvh) {
1657 // If we are creating a RVH for a restored controller, then we might
1658 // have more page IDs than the SiteInstance's current max page ID. We must
1659 // make sure that the max page ID is larger than any restored page ID.
1660 // Note that it is ok for conflicting page IDs to exist in another tab
1661 // (i.e., NavigationController), but if any page ID is larger than the max,
1662 // the back/forward list will get confused.
1663 int max_restored_page_id = controller_.max_restored_page_id();
1664 if (max_restored_page_id > 0) {
1665 int curr_max_page_id = site_instance->max_page_id();
1666 if (max_restored_page_id > curr_max_page_id) {
1667 // Need to update the site instance immediately.
1668 site_instance->UpdateMaxPageID(max_restored_page_id);
1669
1670 // Also tell the renderer to update its internal representation. We
1671 // need to reserve enough IDs to make all restored page IDs less than
1672 // the max.
1673 if (curr_max_page_id < 0)
1674 curr_max_page_id = 0;
1675 rvh->ReservePageIDRange(max_restored_page_id - curr_max_page_id);
1676 }
1677 }
1678}
1679
1680void TabContents::UpdateHistoryForNavigation(
[email protected]ebe89e062009-08-13 23:16:541681 const GURL& virtual_url,
[email protected]befd8d822009-07-01 04:51:471682 const NavigationController::LoadCommittedDetails& details,
[email protected]420ae012009-04-24 05:16:321683 const ViewHostMsg_FrameNavigate_Params& params) {
1684 if (profile()->IsOffTheRecord())
1685 return;
1686
1687 // Add to history service.
1688 HistoryService* hs = profile()->GetHistoryService(Profile::IMPLICIT_ACCESS);
1689 if (hs) {
1690 if (PageTransition::IsMainFrame(params.transition) &&
[email protected]ebe89e062009-08-13 23:16:541691 virtual_url != params.url) {
1692 // Hack on the "virtual" URL so that it will appear in history. For some
[email protected]420ae012009-04-24 05:16:321693 // types of URLs, we will display a magic URL that is different from where
1694 // the page is actually navigated. We want the user to see in history
[email protected]ebe89e062009-08-13 23:16:541695 // what they saw in the URL bar, so we add the virtual URL as a redirect.
1696 // This only applies to the main frame, as the virtual URL doesn't apply
[email protected]420ae012009-04-24 05:16:321697 // to sub-frames.
1698 std::vector<GURL> redirects = params.redirects;
1699 if (!redirects.empty())
[email protected]ebe89e062009-08-13 23:16:541700 redirects.back() = virtual_url;
1701 hs->AddPage(virtual_url, this, params.page_id, params.referrer,
[email protected]befd8d822009-07-01 04:51:471702 params.transition, redirects, details.did_replace_entry);
[email protected]420ae012009-04-24 05:16:321703 } else {
1704 hs->AddPage(params.url, this, params.page_id, params.referrer,
[email protected]befd8d822009-07-01 04:51:471705 params.transition, params.redirects,
1706 details.did_replace_entry);
[email protected]420ae012009-04-24 05:16:321707 }
1708 }
1709}
1710
1711bool TabContents::UpdateTitleForEntry(NavigationEntry* entry,
1712 const std::wstring& title) {
1713 // For file URLs without a title, use the pathname instead. In the case of a
1714 // synthesized title, we don't want the update to count toward the "one set
1715 // per page of the title to history."
[email protected]e53668962010-06-23 15:35:251716 string16 final_title;
[email protected]420ae012009-04-24 05:16:321717 bool explicit_set;
1718 if (entry->url().SchemeIsFile() && title.empty()) {
[email protected]e53668962010-06-23 15:35:251719 final_title = UTF8ToUTF16(entry->url().ExtractFileName());
[email protected]420ae012009-04-24 05:16:321720 explicit_set = false; // Don't count synthetic titles toward the set limit.
1721 } else {
[email protected]e53668962010-06-23 15:35:251722 TrimWhitespace(WideToUTF16Hack(title), TRIM_ALL, &final_title);
[email protected]420ae012009-04-24 05:16:321723 explicit_set = true;
1724 }
1725
[email protected]e53668962010-06-23 15:35:251726 if (final_title == entry->title())
[email protected]420ae012009-04-24 05:16:321727 return false; // Nothing changed, don't bother.
1728
[email protected]e53668962010-06-23 15:35:251729 entry->set_title(final_title);
[email protected]420ae012009-04-24 05:16:321730
1731 // Update the history system for this page.
1732 if (!profile()->IsOffTheRecord() && !received_page_title_) {
1733 HistoryService* hs =
1734 profile()->GetHistoryService(Profile::IMPLICIT_ACCESS);
1735 if (hs)
[email protected]ebe89e062009-08-13 23:16:541736 hs->SetPageTitle(entry->virtual_url(), final_title);
[email protected]420ae012009-04-24 05:16:321737
1738 // Don't allow the title to be saved again for explicitly set ones.
1739 received_page_title_ = explicit_set;
1740 }
1741
1742 // Lastly, set the title for the view.
[email protected]e53668962010-06-23 15:35:251743 view_->SetPageTitle(UTF16ToWideHack(final_title));
[email protected]420ae012009-04-24 05:16:321744
[email protected]cbc0e1b2010-04-12 18:33:041745 NotificationService::current()->Notify(
1746 NotificationType::TAB_CONTENTS_TITLE_UPDATED,
1747 Source<TabContents>(this),
1748 NotificationService::NoDetails());
1749
[email protected]420ae012009-04-24 05:16:321750 return true;
1751}
1752
1753void TabContents::NotifySwapped() {
1754 // After sending out a swap notification, we need to send a disconnect
1755 // notification so that clients that pick up a pointer to |this| can NULL the
1756 // pointer. See Bug 1230284.
1757 notify_disconnection_ = true;
1758 NotificationService::current()->Notify(
[email protected]57c6a652009-05-04 07:58:341759 NotificationType::TAB_CONTENTS_SWAPPED,
[email protected]d82ed61e2009-06-16 02:46:221760 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:321761 NotificationService::NoDetails());
1762}
1763
1764void TabContents::NotifyConnected() {
1765 notify_disconnection_ = true;
1766 NotificationService::current()->Notify(
[email protected]57c6a652009-05-04 07:58:341767 NotificationType::TAB_CONTENTS_CONNECTED,
[email protected]d82ed61e2009-06-16 02:46:221768 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:321769 NotificationService::NoDetails());
1770}
1771
1772void TabContents::NotifyDisconnected() {
1773 if (!notify_disconnection_)
1774 return;
1775
1776 notify_disconnection_ = false;
1777 NotificationService::current()->Notify(
[email protected]57c6a652009-05-04 07:58:341778 NotificationType::TAB_CONTENTS_DISCONNECTED,
[email protected]d82ed61e2009-06-16 02:46:221779 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:321780 NotificationService::NoDetails());
1781}
1782
1783void TabContents::GenerateKeywordIfNecessary(
1784 const ViewHostMsg_FrameNavigate_Params& params) {
1785 if (!params.searchable_form_url.is_valid())
1786 return;
1787
1788 if (profile()->IsOffTheRecord())
1789 return;
1790
1791 int last_index = controller_.last_committed_entry_index();
1792 // When there was no previous page, the last index will be 0. This is
1793 // normally due to a form submit that opened in a new tab.
1794 // TODO(brettw) bug 916126: we should support keywords when form submits
1795 // happen in new tabs.
1796 if (last_index <= 0)
1797 return;
1798 const NavigationEntry* previous_entry =
1799 controller_.GetEntryAtIndex(last_index - 1);
1800 if (IsFormSubmit(previous_entry)) {
1801 // Only generate a keyword if the previous page wasn't itself a form
1802 // submit.
1803 return;
1804 }
1805
1806 GURL keyword_url = previous_entry->user_typed_url().is_valid() ?
1807 previous_entry->user_typed_url() : previous_entry->url();
1808 std::wstring keyword =
1809 TemplateURLModel::GenerateKeyword(keyword_url, true); // autodetected
1810 if (keyword.empty())
1811 return;
1812
1813 TemplateURLModel* url_model = profile()->GetTemplateURLModel();
1814 if (!url_model)
1815 return;
1816
1817 if (!url_model->loaded()) {
1818 url_model->Load();
1819 return;
1820 }
1821
1822 const TemplateURL* current_url;
[email protected]ddd231e2010-06-29 20:35:191823 GURL url = params.searchable_form_url;
[email protected]420ae012009-04-24 05:16:321824 if (!url_model->CanReplaceKeyword(keyword, url, &current_url))
1825 return;
1826
1827 if (current_url) {
1828 if (current_url->originating_url().is_valid()) {
1829 // The existing keyword was generated from an OpenSearch description
1830 // document, don't regenerate.
1831 return;
1832 }
1833 url_model->Remove(current_url);
1834 }
1835 TemplateURL* new_url = new TemplateURL();
1836 new_url->set_keyword(keyword);
1837 new_url->set_short_name(keyword);
[email protected]ddd231e2010-06-29 20:35:191838 new_url->SetURL(url.spec(), 0, 0);
[email protected]420ae012009-04-24 05:16:321839 new_url->add_input_encoding(params.searchable_form_encoding);
1840 DCHECK(controller_.GetLastCommittedEntry());
1841 const GURL& favicon_url =
1842 controller_.GetLastCommittedEntry()->favicon().url();
1843 if (favicon_url.is_valid()) {
1844 new_url->SetFavIconURL(favicon_url);
1845 } else {
1846 // The favicon url isn't valid. This means there really isn't a favicon,
1847 // or the favicon url wasn't obtained before the load started. This assumes
1848 // the later.
1849 // TODO(sky): Need a way to set the favicon that doesn't involve generating
1850 // its url.
1851 new_url->SetFavIconURL(TemplateURL::GenerateFaviconURL(params.referrer));
1852 }
1853 new_url->set_safe_for_autoreplace(true);
1854 url_model->Add(new_url);
1855}
1856
[email protected]bf5c2ff392009-07-08 16:24:331857void TabContents::OnUserGesture() {
1858 // See comment in RenderViewHostDelegate::OnUserGesture as to why we do this.
1859 DownloadRequestManager* drm = g_browser_process->download_request_manager();
1860 if (drm)
1861 drm->OnUserGesture(this);
[email protected]e7eaedde2009-09-25 20:09:491862 ExternalProtocolHandler::OnUserGesture();
[email protected]bf5c2ff392009-07-08 16:24:331863 controller_.OnUserGesture();
1864}
1865
1866void TabContents::OnFindReply(int request_id,
1867 int number_of_matches,
1868 const gfx::Rect& selection_rect,
1869 int active_match_ordinal,
1870 bool final_update) {
[email protected]3ce7ced2009-07-24 18:52:421871 // Ignore responses for requests that have been aborted.
1872 if (find_op_aborted_)
1873 return;
1874
[email protected]bf5c2ff392009-07-08 16:24:331875 // Ignore responses for requests other than the one we have most recently
1876 // issued. That way we won't act on stale results when the user has
1877 // already typed in another query.
1878 if (request_id != current_find_request_id_)
1879 return;
1880
1881 if (number_of_matches == -1)
1882 number_of_matches = last_search_result_.number_of_matches();
1883 if (active_match_ordinal == -1)
1884 active_match_ordinal = last_search_result_.active_match_ordinal();
1885
1886 gfx::Rect selection = selection_rect;
1887 if (selection.IsEmpty())
1888 selection = last_search_result_.selection_rect();
1889
1890 // Notify the UI, automation and any other observers that a find result was
1891 // found.
1892 last_search_result_ = FindNotificationDetails(request_id, number_of_matches,
1893 selection, active_match_ordinal,
1894 final_update);
1895 NotificationService::current()->Notify(
1896 NotificationType::FIND_RESULT_AVAILABLE,
1897 Source<TabContents>(this),
1898 Details<FindNotificationDetails>(&last_search_result_));
1899}
1900
1901void TabContents::GoToEntryAtOffset(int offset) {
[email protected]f9cc4c452009-10-13 14:56:381902 if (!delegate_ || delegate_->OnGoToEntryOffset(offset))
1903 controller_.GoToOffset(offset);
[email protected]bf5c2ff392009-07-08 16:24:331904}
1905
[email protected]bf5c2ff392009-07-08 16:24:331906void TabContents::OnMissingPluginStatus(int status) {
1907#if defined(OS_WIN)
1908// TODO(PORT): pull in when plug-ins work
1909 GetPluginInstaller()->OnMissingPluginStatus(status);
1910#endif
1911}
1912
1913void TabContents::OnCrashedPlugin(const FilePath& plugin_path) {
[email protected]bf5c2ff392009-07-08 16:24:331914 DCHECK(!plugin_path.value().empty());
1915
1916 std::wstring plugin_name = plugin_path.ToWStringHack();
[email protected]d32d46182009-07-23 00:09:351917#if defined(OS_WIN) || defined(OS_MACOSX)
[email protected]bf5c2ff392009-07-08 16:24:331918 scoped_ptr<FileVersionInfo> version_info(
1919 FileVersionInfo::CreateFileVersionInfo(plugin_path));
1920 if (version_info.get()) {
1921 const std::wstring& product_name = version_info->product_name();
[email protected]996fd702009-09-04 19:12:371922 if (!product_name.empty()) {
[email protected]bf5c2ff392009-07-08 16:24:331923 plugin_name = product_name;
[email protected]996fd702009-09-04 19:12:371924#if defined(OS_MACOSX)
1925 // Many plugins on the Mac have .plugin in the actual name, which looks
1926 // terrible, so look for that and strip it off if present.
1927 const std::wstring plugin_extension(L".plugin");
1928 if (EndsWith(plugin_name, plugin_extension, true))
1929 plugin_name.erase(plugin_name.length() - plugin_extension.length());
1930#endif // OS_MACOSX
1931 }
[email protected]bf5c2ff392009-07-08 16:24:331932 }
[email protected]4a560ed2009-07-13 23:35:261933#else
1934 NOTIMPLEMENTED() << " convert plugin path to plugin name";
1935#endif
[email protected]2ae8a922010-06-28 20:27:531936 SkBitmap* crash_icon = ResourceBundle::GetSharedInstance().GetBitmapNamed(
1937 IDR_INFOBAR_PLUGIN_CRASHED);
[email protected]bf5c2ff392009-07-08 16:24:331938 AddInfoBar(new SimpleAlertInfoBarDelegate(
1939 this, l10n_util::GetStringF(IDS_PLUGIN_CRASHED_PROMPT, plugin_name),
[email protected]2ae8a922010-06-28 20:27:531940 crash_icon, true));
[email protected]bf5c2ff392009-07-08 16:24:331941}
1942
1943void TabContents::OnCrashedWorker() {
1944 AddInfoBar(new SimpleAlertInfoBarDelegate(
1945 this, l10n_util::GetString(IDS_WEBWORKER_CRASHED_PROMPT),
[email protected]938e1f92010-04-01 18:09:421946 NULL, true));
[email protected]bf5c2ff392009-07-08 16:24:331947}
1948
1949void TabContents::OnDidGetApplicationInfo(
1950 int32 page_id,
1951 const webkit_glue::WebApplicationInfo& info) {
[email protected]ed5431872009-11-17 08:39:511952 web_app_info_ = info;
[email protected]bf5c2ff392009-07-08 16:24:331953
[email protected]ed5431872009-11-17 08:39:511954 if (delegate())
1955 delegate()->OnDidGetApplicationInfo(this, page_id);
[email protected]bf5c2ff392009-07-08 16:24:331956}
1957
[email protected]a2cf65eb2010-01-18 08:20:381958void TabContents::OnPageContents(const GURL& url,
1959 int renderer_process_id,
1960 int32 page_id,
[email protected]e5106202010-06-11 21:12:361961 const string16& contents,
[email protected]1c57b7b2010-07-12 17:38:101962 const std::string& language,
1963 bool page_translatable) {
[email protected]a2cf65eb2010-01-18 08:20:381964 // Don't index any https pages. People generally don't want their bank
1965 // accounts, etc. indexed on their computer, especially since some of these
1966 // things are not marked cachable.
1967 // TODO(brettw) we may want to consider more elaborate heuristics such as
1968 // the cachability of the page. We may also want to consider subframes (this
1969 // test will still index subframes if the subframe is SSL).
[email protected]ccd40a52010-05-11 16:15:271970 // TODO(zelidrag) bug chromium-os:2808 - figure out if we want to reenable
1971 // content indexing for chromeos in some future releases.
1972#if !defined(OS_CHROMEOS)
[email protected]a2cf65eb2010-01-18 08:20:381973 if (!url.SchemeIsSecure()) {
1974 Profile* p = profile();
1975 if (p && !p->IsOffTheRecord()) {
1976 HistoryService* hs = p->GetHistoryService(Profile::IMPLICIT_ACCESS);
1977 if (hs)
1978 hs->SetPageContents(url, contents);
1979 }
1980 }
[email protected]ccd40a52010-05-11 16:15:271981#endif
[email protected]a8a81292010-01-21 00:32:451982
[email protected]1c57b7b2010-07-12 17:38:101983 language_state_.LanguageDetermined(language, page_translatable);
[email protected]a8a81292010-01-21 00:32:451984
1985 std::string lang = language;
1986 NotificationService::current()->Notify(
1987 NotificationType::TAB_LANGUAGE_DETERMINED,
[email protected]7e0831b2010-02-01 08:45:151988 Source<TabContents>(this),
[email protected]a8a81292010-01-21 00:32:451989 Details<std::string>(&lang));
[email protected]a2cf65eb2010-01-18 08:20:381990}
1991
[email protected]7e0831b2010-02-01 08:45:151992void TabContents::OnPageTranslated(int32 page_id,
1993 const std::string& original_lang,
[email protected]152b8ba32010-03-29 19:40:161994 const std::string& translated_lang,
1995 TranslateErrors::Type error_type) {
[email protected]3b1c1812010-02-12 21:53:551996 language_state_.set_current_language(translated_lang);
1997 language_state_.set_translation_pending(false);
[email protected]152b8ba32010-03-29 19:40:161998 PageTranslatedDetails details(original_lang, translated_lang, error_type);
[email protected]7e0831b2010-02-01 08:45:151999 NotificationService::current()->Notify(
2000 NotificationType::PAGE_TRANSLATED,
2001 Source<TabContents>(this),
[email protected]152b8ba32010-03-29 19:40:162002 Details<PageTranslatedDetails>(&details));
[email protected]7e0831b2010-02-01 08:45:152003}
2004
[email protected]bf5c2ff392009-07-08 16:24:332005void TabContents::DidStartProvisionalLoadForFrame(
2006 RenderViewHost* render_view_host,
2007 bool is_main_frame,
2008 const GURL& url) {
2009 ProvisionalLoadDetails details(is_main_frame,
2010 controller_.IsURLInPageNavigation(url),
2011 url, std::string(), false);
2012 NotificationService::current()->Notify(
2013 NotificationType::FRAME_PROVISIONAL_LOAD_START,
2014 Source<NavigationController>(&controller_),
2015 Details<ProvisionalLoadDetails>(&details));
2016}
2017
2018void TabContents::DidStartReceivingResourceResponse(
[email protected]3dbb80b2010-02-09 22:41:202019 const ResourceRequestDetails& details) {
[email protected]bf5c2ff392009-07-08 16:24:332020 NotificationService::current()->Notify(
2021 NotificationType::RESOURCE_RESPONSE_STARTED,
2022 Source<NavigationController>(&controller()),
[email protected]3dbb80b2010-02-09 22:41:202023 Details<const ResourceRequestDetails>(&details));
[email protected]bf5c2ff392009-07-08 16:24:332024}
2025
[email protected]3dbb80b2010-02-09 22:41:202026void TabContents::DidRedirectResource(
2027 const ResourceRedirectDetails& details) {
[email protected]bf5c2ff392009-07-08 16:24:332028 NotificationService::current()->Notify(
2029 NotificationType::RESOURCE_RECEIVED_REDIRECT,
2030 Source<NavigationController>(&controller()),
[email protected]3dbb80b2010-02-09 22:41:202031 Details<const ResourceRequestDetails>(&details));
[email protected]bf5c2ff392009-07-08 16:24:332032}
2033
2034void TabContents::DidLoadResourceFromMemoryCache(
2035 const GURL& url,
2036 const std::string& frame_origin,
2037 const std::string& main_frame_origin,
2038 const std::string& security_info) {
2039 // Send out a notification that we loaded a resource from our memory cache.
[email protected]fc7de492010-07-12 14:49:042040 int cert_id = 0, cert_status = 0, security_bits = 0, connection_status = 0;
[email protected]bf5c2ff392009-07-08 16:24:332041 SSLManager::DeserializeSecurityInfo(security_info,
2042 &cert_id, &cert_status,
[email protected]fc7de492010-07-12 14:49:042043 &security_bits,
2044 &connection_status);
[email protected]bf5c2ff392009-07-08 16:24:332045 LoadFromMemoryCacheDetails details(url, frame_origin, main_frame_origin,
[email protected]8cb5d5b2010-02-09 11:36:162046 GetRenderProcessHost()->id(), cert_id,
2047 cert_status);
[email protected]bf5c2ff392009-07-08 16:24:332048
2049 NotificationService::current()->Notify(
2050 NotificationType::LOAD_FROM_MEMORY_CACHE,
2051 Source<NavigationController>(&controller_),
2052 Details<LoadFromMemoryCacheDetails>(&details));
2053}
2054
[email protected]e3d60e5d2009-09-25 21:08:292055void TabContents::DidDisplayInsecureContent() {
[email protected]f17a0ee2010-05-17 17:38:472056 displayed_insecure_content_ = true;
2057 SSLManager::NotifySSLInternalStateChanged();
[email protected]e3d60e5d2009-09-25 21:08:292058}
2059
2060void TabContents::DidRunInsecureContent(const std::string& security_origin) {
[email protected]09843b5f2009-09-26 08:30:592061 controller_.ssl_manager()->DidRunInsecureContent(security_origin);
[email protected]e3d60e5d2009-09-25 21:08:292062}
2063
[email protected]bf5c2ff392009-07-08 16:24:332064void TabContents::DidFailProvisionalLoadWithError(
2065 RenderViewHost* render_view_host,
2066 bool is_main_frame,
2067 int error_code,
2068 const GURL& url,
2069 bool showing_repost_interstitial) {
2070 if (net::ERR_ABORTED == error_code) {
2071 // EVIL HACK ALERT! Ignore failed loads when we're showing interstitials.
2072 // This means that the interstitial won't be torn down properly, which is
2073 // bad. But if we have an interstitial, go back to another tab type, and
2074 // then load the same interstitial again, we could end up getting the first
2075 // interstitial's "failed" message (as a result of the cancel) when we're on
2076 // the second one.
2077 //
2078 // We can't tell this apart, so we think we're tearing down the current page
2079 // which will cause a crash later one. There is also some code in
2080 // RenderViewHostManager::RendererAbortedProvisionalLoad that is commented
2081 // out because of this problem.
2082 //
2083 // http://code.google.com/p/chromium/issues/detail?id=2855
2084 // Because this will not tear down the interstitial properly, if "back" is
2085 // back to another tab type, the interstitial will still be somewhat alive
2086 // in the previous tab type. If you navigate somewhere that activates the
2087 // tab with the interstitial again, you'll see a flash before the new load
2088 // commits of the interstitial page.
2089 if (showing_interstitial_page()) {
2090 LOG(WARNING) << "Discarding message during interstitial.";
2091 return;
2092 }
2093
2094 // This will discard our pending entry if we cancelled the load (e.g., if we
2095 // decided to download the file instead of load it). Only discard the
2096 // pending entry if the URLs match, otherwise the user initiated a navigate
2097 // before the page loaded so that the discard would discard the wrong entry.
2098 NavigationEntry* pending_entry = controller_.pending_entry();
2099 if (pending_entry && pending_entry->url() == url) {
2100 controller_.DiscardNonCommittedEntries();
2101 // Update the URL display.
2102 NotifyNavigationStateChanged(TabContents::INVALIDATE_URL);
2103 }
2104
2105 render_manager_.RendererAbortedProvisionalLoad(render_view_host);
2106 }
2107
2108 // Send out a notification that we failed a provisional load with an error.
2109 ProvisionalLoadDetails details(is_main_frame,
2110 controller_.IsURLInPageNavigation(url),
2111 url, std::string(), false);
2112 details.set_error_code(error_code);
2113
2114 NotificationService::current()->Notify(
2115 NotificationType::FAIL_PROVISIONAL_LOAD_WITH_ERROR,
2116 Source<NavigationController>(&controller_),
2117 Details<ProvisionalLoadDetails>(&details));
2118}
2119
2120void TabContents::DocumentLoadedInFrame() {
2121 controller_.DocumentLoadedInFrame();
2122}
2123
[email protected]9fb83e82010-07-02 18:24:552124void TabContents::OnContentSettingsChange() {
[email protected]614c0e12010-03-25 10:12:562125 if (delegate_)
[email protected]20ad6a92010-04-21 14:07:242126 delegate_->OnContentSettingsChange(this);
[email protected]614c0e12010-03-25 10:12:562127}
2128
[email protected]8d3347f2009-07-09 22:00:212129RenderViewHostDelegate::View* TabContents::GetViewDelegate() {
[email protected]420ae012009-04-24 05:16:322130 return view_.get();
2131}
2132
[email protected]8d3347f2009-07-09 22:00:212133RenderViewHostDelegate::RendererManagement*
2134TabContents::GetRendererManagementDelegate() {
2135 return &render_manager_;
2136}
2137
[email protected]bf5c2ff392009-07-08 16:24:332138RenderViewHostDelegate::BrowserIntegration*
[email protected]8d3347f2009-07-09 22:00:212139 TabContents::GetBrowserIntegrationDelegate() {
2140 return this;
[email protected]bf5c2ff392009-07-08 16:24:332141}
2142
[email protected]8d3347f2009-07-09 22:00:212143RenderViewHostDelegate::Resource* TabContents::GetResourceDelegate() {
2144 return this;
[email protected]bf5c2ff392009-07-08 16:24:332145}
2146
[email protected]46ba9082010-06-22 10:00:582147RenderViewHostDelegate::ContentSettings*
2148TabContents::GetContentSettingsDelegate() {
[email protected]9fb83e82010-07-02 18:24:552149 return content_settings_delegate_.get();
[email protected]46ba9082010-06-22 10:00:582150}
2151
[email protected]8d3347f2009-07-09 22:00:212152RenderViewHostDelegate::Save* TabContents::GetSaveDelegate() {
[email protected]420ae012009-04-24 05:16:322153 return save_package_.get(); // May be NULL, but we can return NULL.
2154}
2155
[email protected]8d3347f2009-07-09 22:00:212156RenderViewHostDelegate::Printing* TabContents::GetPrintingDelegate() {
[email protected]12802702010-07-09 19:43:092157 return printing_.get();
[email protected]8d3347f2009-07-09 22:00:212158}
2159
2160RenderViewHostDelegate::FavIcon* TabContents::GetFavIconDelegate() {
2161 return &fav_icon_helper_;
2162}
2163
[email protected]e8dbd674b2010-03-11 18:34:402164RenderViewHostDelegate::Autocomplete* TabContents::GetAutocompleteDelegate() {
[email protected]6db0def92010-03-02 19:55:452165 if (autocomplete_history_manager_.get() == NULL)
2166 autocomplete_history_manager_.reset(new AutocompleteHistoryManager(this));
2167 return autocomplete_history_manager_.get();
[email protected]bf5c2ff392009-07-08 16:24:332168}
2169
[email protected]21674df2009-10-28 19:51:212170RenderViewHostDelegate::AutoFill* TabContents::GetAutoFillDelegate() {
[email protected]cf28ec52010-04-09 22:56:342171 return GetAutoFillManager();
[email protected]21674df2009-10-28 19:51:212172}
2173
[email protected]fd571bb2010-03-19 01:23:542174AutomationResourceRoutingDelegate*
2175TabContents::GetAutomationResourceRoutingDelegate() {
2176 return delegate();
2177}
2178
[email protected]ced90ae12010-02-20 02:06:162179RenderViewHostDelegate::BookmarkDrag* TabContents::GetBookmarkDragDelegate() {
2180 return bookmark_drag_;
2181}
2182
2183void TabContents::SetBookmarkDragDelegate(
2184 RenderViewHostDelegate::BookmarkDrag* bookmark_drag) {
2185 bookmark_drag_ = bookmark_drag;
2186}
2187
[email protected]9fb83e82010-07-02 18:24:552188TabSpecificContentSettings* TabContents::GetTabSpecificContentSettings() const {
2189 return content_settings_delegate_.get();
2190}
2191
[email protected]93623c5d2009-12-10 21:40:322192RendererPreferences TabContents::GetRendererPrefs(Profile* profile) const {
[email protected]840b1512009-07-21 16:53:462193 return renderer_preferences_;
[email protected]80d96fa2009-06-10 22:34:512194}
2195
[email protected]57c6a652009-05-04 07:58:342196TabContents* TabContents::GetAsTabContents() {
2197 return this;
2198}
2199
[email protected]7b291f92009-08-14 05:43:532200ViewType::Type TabContents::GetRenderViewType() const {
2201 return ViewType::TAB_CONTENTS;
2202}
2203
2204int TabContents::GetBrowserWindowID() const {
2205 return controller().window_id().id();
2206}
2207
[email protected]420ae012009-04-24 05:16:322208void TabContents::RenderViewCreated(RenderViewHost* render_view_host) {
[email protected]2e4633c2009-07-09 16:58:062209 NotificationService::current()->Notify(
2210 NotificationType::RENDER_VIEW_HOST_CREATED_FOR_TAB,
2211 Source<TabContents>(this),
2212 Details<RenderViewHost>(render_view_host));
[email protected]420ae012009-04-24 05:16:322213 NavigationEntry* entry = controller_.GetActiveEntry();
2214 if (!entry)
2215 return;
2216
2217 // When we're creating views, we're still doing initial setup, so we always
2218 // use the pending DOM UI rather than any possibly existing committed one.
2219 if (render_manager_.pending_dom_ui()) {
2220 render_manager_.pending_dom_ui()->RenderViewCreated(render_view_host);
2221 }
2222
2223 if (entry->IsViewSourceMode()) {
2224 // Put the renderer in view source mode.
2225 render_view_host->Send(
2226 new ViewMsg_EnableViewSourceMode(render_view_host->routing_id()));
2227 }
[email protected]0666aef2009-05-13 19:48:082228
2229 view()->RenderViewCreated(render_view_host);
[email protected]420ae012009-04-24 05:16:322230}
2231
2232void TabContents::RenderViewReady(RenderViewHost* rvh) {
2233 if (rvh != render_view_host()) {
2234 // Don't notify the world, since this came from a renderer in the
2235 // background.
2236 return;
2237 }
2238
2239 NotifyConnected();
[email protected]9d8a4642009-07-29 17:25:302240 bool was_crashed = is_crashed();
[email protected]420ae012009-04-24 05:16:322241 SetIsCrashed(false);
[email protected]9d8a4642009-07-29 17:25:302242
2243 // Restore the focus to the tab (otherwise the focus will be on the top
2244 // window).
2245 if (was_crashed && !FocusLocationBarByDefault())
2246 Focus();
[email protected]420ae012009-04-24 05:16:322247}
2248
2249void TabContents::RenderViewGone(RenderViewHost* rvh) {
2250 // Ask the print preview if this renderer was valuable.
[email protected]12802702010-07-09 19:43:092251 if (!printing_->OnRenderViewGone(rvh))
[email protected]420ae012009-04-24 05:16:322252 return;
2253 if (rvh != render_view_host()) {
2254 // The pending page's RenderViewHost is gone.
2255 return;
2256 }
2257
2258 SetIsLoading(false, NULL);
2259 NotifyDisconnected();
2260 SetIsCrashed(true);
2261
[email protected]ff9efc62010-02-18 22:10:542262 // Remove all infobars.
[email protected]e5430c92010-02-18 22:39:212263 for (int i = infobar_delegate_count() - 1; i >=0 ; --i)
[email protected]ff9efc62010-02-18 22:10:542264 RemoveInfoBar(GetInfoBarDelegateAt(i));
2265
[email protected]8e537302009-06-10 21:19:582266 // Tell the view that we've crashed so it can prepare the sad tab page.
[email protected]cf68b092010-01-15 20:05:552267 // Only do this if we're not in browser shutdown, so that TabContents
2268 // objects that are not in a browser (e.g., HTML dialogs) and thus are
2269 // visible do not flash a sad tab page.
2270 if (browser_shutdown::GetShutdownType() == browser_shutdown::NOT_VALID)
2271 view_->OnTabCrashed();
[email protected]420ae012009-04-24 05:16:322272
2273 // Hide any visible hung renderer warning for this web contents' process.
[email protected]e7b8d832009-11-20 22:41:002274 hung_renderer_dialog::HideForTabContents(this);
[email protected]420ae012009-04-24 05:16:322275}
2276
[email protected]2e4633c2009-07-09 16:58:062277void TabContents::RenderViewDeleted(RenderViewHost* rvh) {
2278 NotificationService::current()->Notify(
2279 NotificationType::RENDER_VIEW_HOST_DELETED,
2280 Source<TabContents>(this),
2281 Details<RenderViewHost>(rvh));
[email protected]fb7b79f72009-11-06 18:00:482282 render_manager_.RenderViewDeleted(rvh);
[email protected]2e4633c2009-07-09 16:58:062283}
2284
[email protected]420ae012009-04-24 05:16:322285void TabContents::DidNavigate(RenderViewHost* rvh,
2286 const ViewHostMsg_FrameNavigate_Params& params) {
[email protected]8030f012009-09-25 18:09:372287 int extra_invalidate_flags = 0;
2288
2289 if (PageTransition::IsMainFrame(params.transition)) {
[email protected]20ede072009-10-06 00:28:352290 bool was_bookmark_bar_visible = ShouldShowBookmarkBar();
[email protected]8030f012009-09-25 18:09:372291 bool was_extension_shelf_visible = IsExtensionShelfAlwaysVisible();
2292
[email protected]420ae012009-04-24 05:16:322293 render_manager_.DidNavigateMainFrame(rvh);
2294
[email protected]20ede072009-10-06 00:28:352295 if (was_bookmark_bar_visible != ShouldShowBookmarkBar())
[email protected]8030f012009-09-25 18:09:372296 extra_invalidate_flags |= INVALIDATE_BOOKMARK_BAR;
2297 if (was_extension_shelf_visible != IsExtensionShelfAlwaysVisible())
2298 extra_invalidate_flags |= INVALIDATE_EXTENSION_SHELF;
2299 }
2300
[email protected]420ae012009-04-24 05:16:322301 // Update the site of the SiteInstance if it doesn't have one yet.
2302 if (!GetSiteInstance()->has_site())
2303 GetSiteInstance()->SetSite(params.url);
2304
2305 // Need to update MIME type here because it's referred to in
2306 // UpdateNavigationCommands() called by RendererDidNavigate() to
2307 // determine whether or not to enable the encoding menu.
2308 // It's updated only for the main frame. For a subframe,
2309 // RenderView::UpdateURL does not set params.contents_mime_type.
2310 // (see http://code.google.com/p/chromium/issues/detail?id=2929 )
2311 // TODO(jungshik): Add a test for the encoding menu to avoid
2312 // regressing it again.
2313 if (PageTransition::IsMainFrame(params.transition))
2314 contents_mime_type_ = params.contents_mime_type;
2315
2316 NavigationController::LoadCommittedDetails details;
[email protected]8030f012009-09-25 18:09:372317 bool did_navigate = controller_.RendererDidNavigate(
2318 params, extra_invalidate_flags, &details);
[email protected]3e90d4a2009-07-03 17:38:392319
2320 // Update history. Note that this needs to happen after the entry is complete,
2321 // which WillNavigate[Main,Sub]Frame will do before this function is called.
[email protected]2e29d922009-10-22 22:13:352322 if (params.should_update_history &&
2323 (!delegate() || delegate()->ShouldAddNavigationsToHistory())) {
[email protected]3e90d4a2009-07-03 17:38:392324 // Most of the time, the displayURL matches the loaded URL, but for about:
2325 // URLs, we use a data: URL as the real value. We actually want to save
2326 // the about: URL to the history db and keep the data: URL hidden. This is
2327 // what the TabContents' URL getter does.
2328 UpdateHistoryForNavigation(GetURL(), details, params);
2329 }
2330
[email protected]76543b92009-08-31 17:27:452331 if (!did_navigate)
2332 return; // No navigation happened.
[email protected]420ae012009-04-24 05:16:322333
2334 // DO NOT ADD MORE STUFF TO THIS FUNCTION! Your component should either listen
2335 // for the appropriate notification (best) or you can add it to
2336 // DidNavigateMainFramePostCommit / DidNavigateAnyFramePostCommit (only if
2337 // necessary, please).
2338
2339 // Run post-commit tasks.
2340 if (details.is_main_frame)
2341 DidNavigateMainFramePostCommit(details, params);
2342 DidNavigateAnyFramePostCommit(rvh, details, params);
2343}
2344
2345void TabContents::UpdateState(RenderViewHost* rvh,
2346 int32 page_id,
2347 const std::string& state) {
2348 DCHECK(rvh == render_view_host());
2349
2350 // We must be prepared to handle state updates for any page, these occur
2351 // when the user is scrolling and entering form data, as well as when we're
2352 // leaving a page, in which case our state may have already been moved to
2353 // the next page. The navigation controller will look up the appropriate
2354 // NavigationEntry and update it when it is notified via the delegate.
2355
2356 int entry_index = controller_.GetEntryIndexWithPageID(
2357 GetSiteInstance(), page_id);
2358 if (entry_index < 0)
2359 return;
2360 NavigationEntry* entry = controller_.GetEntryAtIndex(entry_index);
2361
2362 if (state == entry->content_state())
2363 return; // Nothing to update.
2364 entry->set_content_state(state);
2365 controller_.NotifyEntryChanged(entry, entry_index);
2366}
2367
2368void TabContents::UpdateTitle(RenderViewHost* rvh,
2369 int32 page_id, const std::wstring& title) {
2370 // If we have a title, that's a pretty good indication that we've started
2371 // getting useful data.
2372 SetNotWaitingForResponse();
2373
2374 DCHECK(rvh == render_view_host());
2375 NavigationEntry* entry = controller_.GetEntryWithPageID(GetSiteInstance(),
2376 page_id);
2377 if (!entry || !UpdateTitleForEntry(entry, title))
2378 return;
2379
2380 // Broadcast notifications when the UI should be updated.
2381 if (entry == controller_.GetEntryAtOffset(0))
[email protected]f1cd5e82009-10-23 17:19:032382 NotifyNavigationStateChanged(INVALIDATE_TITLE);
[email protected]420ae012009-04-24 05:16:322383}
2384
[email protected]420ae012009-04-24 05:16:322385void TabContents::UpdateEncoding(RenderViewHost* render_view_host,
[email protected]41fc0322009-09-04 22:23:402386 const std::string& encoding) {
[email protected]420ae012009-04-24 05:16:322387 set_encoding(encoding);
2388}
2389
2390void TabContents::UpdateTargetURL(int32 page_id, const GURL& url) {
2391 if (delegate())
2392 delegate()->UpdateTargetURL(this, url);
2393}
2394
2395void TabContents::UpdateThumbnail(const GURL& url,
2396 const SkBitmap& bitmap,
2397 const ThumbnailScore& score) {
2398 // Tell History about this thumbnail
[email protected]523623c2010-05-19 16:03:422399 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kTopSites)) {
[email protected]72d0fc72009-07-09 17:30:492400 if (!profile()->IsOffTheRecord())
[email protected]523623c2010-05-19 16:03:422401 profile()->GetTopSites()->SetPageThumbnail(url, bitmap, score);
[email protected]449478302009-06-09 20:04:282402 } else {
2403 HistoryService* hs;
2404 if (!profile()->IsOffTheRecord() &&
2405 (hs = profile()->GetHistoryService(Profile::IMPLICIT_ACCESS))) {
2406 hs->SetPageThumbnail(url, bitmap, score);
2407 }
[email protected]420ae012009-04-24 05:16:322408 }
2409}
2410
[email protected]c4e98902010-06-01 10:20:142411void TabContents::UpdateInspectorSetting(const std::string& key,
2412 const std::string& value) {
2413 DictionaryValue* inspector_settings =
2414 profile()->GetPrefs()->GetMutableDictionary(
2415 prefs::kWebKitInspectorSettings);
[email protected]628cf5a52010-06-07 14:25:332416 inspector_settings->SetWithoutPathExpansion(UTF8ToWide(key),
2417 Value::CreateStringValue(value));
[email protected]0df30122009-06-03 12:13:082418}
2419
[email protected]45aafc92010-06-07 14:18:532420void TabContents::ClearInspectorSettings() {
2421 DictionaryValue* inspector_settings =
2422 profile()->GetPrefs()->GetMutableDictionary(
2423 prefs::kWebKitInspectorSettings);
2424 inspector_settings->Clear();
2425}
2426
[email protected]420ae012009-04-24 05:16:322427void TabContents::Close(RenderViewHost* rvh) {
[email protected]07707302009-11-06 00:50:292428 // The UI may be in an event-tracking loop, such as between the
2429 // mouse-down and mouse-up in text selection or a button click.
2430 // Defer the close until after tracking is complete, so that we
2431 // don't free objects out from under the UI.
2432 // TODO(shess): This could probably be integrated with the
2433 // IsDoingDrag() test below. Punting for now because I need more
2434 // research to understand how this impacts platforms other than Mac.
2435 // TODO(shess): This could get more fine-grained. For instance,
2436 // closing a tab in another window while selecting text in the
2437 // current window's Omnibox should be just fine.
2438 if (view()->IsEventTracking()) {
2439 view()->CloseTabAfterEventTracking();
2440 return;
2441 }
2442
[email protected]24a4d1062009-07-10 23:10:422443 // If we close the tab while we're in the middle of a drag, we'll crash.
2444 // Instead, cancel the drag and close it as soon as the drag ends.
2445 if (view()->IsDoingDrag()) {
2446 view()->CancelDragAndCloseTab();
2447 return;
2448 }
2449
[email protected]420ae012009-04-24 05:16:322450 // Ignore this if it comes from a RenderViewHost that we aren't showing.
2451 if (delegate() && rvh == render_view_host())
2452 delegate()->CloseContents(this);
2453}
2454
2455void TabContents::RequestMove(const gfx::Rect& new_bounds) {
2456 if (delegate() && delegate()->IsPopup(this))
2457 delegate()->MoveContents(this, new_bounds);
2458}
2459
[email protected]7ab1e7d62009-10-14 23:32:012460void TabContents::DidStartLoading() {
[email protected]420ae012009-04-24 05:16:322461 SetIsLoading(true, NULL);
2462}
2463
[email protected]7ab1e7d62009-10-14 23:32:012464void TabContents::DidStopLoading() {
[email protected]420ae012009-04-24 05:16:322465 scoped_ptr<LoadNotificationDetails> details;
2466
2467 NavigationEntry* entry = controller_.GetActiveEntry();
2468 // An entry may not exist for a stop when loading an initial blank page or
2469 // if an iframe injected by script into a blank page finishes loading.
2470 if (entry) {
[email protected]420ae012009-04-24 05:16:322471 base::TimeDelta elapsed = base::TimeTicks::Now() - current_load_start_;
2472
2473 details.reset(new LoadNotificationDetails(
[email protected]ebe89e062009-08-13 23:16:542474 entry->virtual_url(),
[email protected]420ae012009-04-24 05:16:322475 entry->transition_type(),
2476 elapsed,
2477 &controller_,
2478 controller_.GetCurrentEntryIndex()));
2479 }
2480
2481 // Tell PasswordManager we've finished a page load, which serves as a
2482 // green light to save pending passwords and reset itself.
2483 GetPasswordManager()->DidStopLoading();
2484
2485 SetIsLoading(false, details.get());
2486}
2487
[email protected]420ae012009-04-24 05:16:322488void TabContents::DidRedirectProvisionalLoad(int32 page_id,
2489 const GURL& source_url,
2490 const GURL& target_url) {
2491 NavigationEntry* entry;
2492 if (page_id == -1)
2493 entry = controller_.pending_entry();
2494 else
2495 entry = controller_.GetEntryWithPageID(GetSiteInstance(), page_id);
2496 if (!entry || entry->url() != source_url)
2497 return;
2498 entry->set_url(target_url);
2499}
2500
[email protected]420ae012009-04-24 05:16:322501void TabContents::RequestOpenURL(const GURL& url, const GURL& referrer,
2502 WindowOpenDisposition disposition) {
2503 if (render_manager_.dom_ui()) {
2504 // When we're a DOM UI, it will provide a page transition type for us (this
2505 // is so the new tab page can specify AUTO_BOOKMARK for automatically
2506 // generated suggestions).
2507 //
2508 // Note also that we hide the referrer for DOM UI pages. We don't really
[email protected]60e448982009-05-06 04:21:162509 // want web sites to see a referrer of "chrome://blah" (and some
2510 // chrome: URLs might have search terms or other stuff we don't want to
[email protected]420ae012009-04-24 05:16:322511 // send to the site), so we send no referrer.
2512 OpenURL(url, GURL(), disposition,
2513 render_manager_.dom_ui()->link_transition_type());
2514 } else {
2515 OpenURL(url, referrer, disposition, PageTransition::LINK);
2516 }
2517}
2518
[email protected]1ee614862009-07-06 20:29:182519void TabContents::DomOperationResponse(const std::string& json_string,
2520 int automation_id) {
2521}
2522
[email protected]420ae012009-04-24 05:16:322523void TabContents::ProcessDOMUIMessage(const std::string& message,
[email protected]438c97d2010-05-21 23:30:152524 const ListValue* content,
[email protected]bb64b512010-04-02 21:01:392525 const GURL& source_url,
[email protected]811bfe32009-07-01 08:46:252526 int request_id,
2527 bool has_callback) {
[email protected]420ae012009-04-24 05:16:322528 if (!render_manager_.dom_ui()) {
[email protected]c2e74fe82009-09-03 17:57:442529 // This can happen if someone uses window.open() to open an extension URL
2530 // from a non-extension context.
2531 render_view_host()->BlockExtensionRequest(request_id);
[email protected]420ae012009-04-24 05:16:322532 return;
2533 }
[email protected]bb64b512010-04-02 21:01:392534 render_manager_.dom_ui()->ProcessDOMUIMessage(message, content, source_url,
2535 request_id,
[email protected]811bfe32009-07-01 08:46:252536 has_callback);
[email protected]420ae012009-04-24 05:16:322537}
2538
2539void TabContents::ProcessExternalHostMessage(const std::string& message,
2540 const std::string& origin,
2541 const std::string& target) {
2542 if (delegate())
2543 delegate()->ForwardMessageToExternalHost(message, origin, target);
2544}
2545
[email protected]cdaf8d02010-03-30 19:52:472546void TabContents::RunFileChooser(
2547 const ViewHostMsg_RunFileChooser_Params &params) {
[email protected]420ae012009-04-24 05:16:322548 if (!select_file_dialog_.get())
2549 select_file_dialog_ = SelectFileDialog::Create(this);
[email protected]cdaf8d02010-03-30 19:52:472550
2551 SelectFileDialog::Type dialog_type;
2552 switch (params.mode) {
2553 case ViewHostMsg_RunFileChooser_Params::Open:
2554 dialog_type = SelectFileDialog::SELECT_OPEN_FILE;
2555 break;
2556 case ViewHostMsg_RunFileChooser_Params::OpenMultiple:
2557 dialog_type = SelectFileDialog::SELECT_OPEN_MULTI_FILE;
2558 break;
2559 case ViewHostMsg_RunFileChooser_Params::Save:
2560 dialog_type = SelectFileDialog::SELECT_SAVEAS_FILE;
2561 break;
2562 default:
[email protected]bed31952010-03-30 20:07:592563 dialog_type = SelectFileDialog::SELECT_OPEN_FILE; // Prevent warning.
[email protected]cdaf8d02010-03-30 19:52:472564 NOTREACHED();
2565 }
[email protected]35896a32010-06-09 08:42:512566 FilePath default_file_name = params.default_file_name;
2567 if (default_file_name.empty())
2568 default_file_name = profile()->last_selected_directory();
[email protected]cdaf8d02010-03-30 19:52:472569 select_file_dialog_->SelectFile(dialog_type, params.title,
[email protected]35896a32010-06-09 08:42:512570 default_file_name,
[email protected]420ae012009-04-24 05:16:322571 NULL, 0, FILE_PATH_LITERAL(""),
2572 view_->GetTopLevelNativeWindow(), NULL);
2573}
2574
2575void TabContents::RunJavaScriptMessage(
2576 const std::wstring& message,
2577 const std::wstring& default_prompt,
2578 const GURL& frame_url,
2579 const int flags,
2580 IPC::Message* reply_msg,
2581 bool* did_suppress_message) {
2582 // Suppress javascript messages when requested and when inside a constrained
2583 // popup window (because that activates them and breaks them out of the
2584 // constrained window jail).
[email protected]91ed49a2009-07-07 21:29:552585 // Also suppress messages when showing an interstitial. The interstitial is
2586 // shown over the previous page, we don't want the hidden page dialogs to
2587 // interfere with the interstitial.
2588 bool suppress_this_message = suppress_javascript_messages_ ||
2589 showing_interstitial_page();
[email protected]420ae012009-04-24 05:16:322590 if (delegate())
2591 suppress_this_message |=
[email protected]b6c874582009-05-08 19:38:312592 (delegate()->GetConstrainingContents(this) != this);
[email protected]420ae012009-04-24 05:16:322593
2594 *did_suppress_message = suppress_this_message;
2595
2596 if (!suppress_this_message) {
2597 base::TimeDelta time_since_last_message(
2598 base::TimeTicks::Now() - last_javascript_message_dismissal_);
2599 bool show_suppress_checkbox = false;
2600 // Show a checkbox offering to suppress further messages if this message is
2601 // being displayed within kJavascriptMessageExpectedDelay of the last one.
2602 if (time_since_last_message <
2603 base::TimeDelta::FromMilliseconds(kJavascriptMessageExpectedDelay))
2604 show_suppress_checkbox = true;
2605
[email protected]d82ed61e2009-06-16 02:46:222606 RunJavascriptMessageBox(this, frame_url, flags, message, default_prompt,
[email protected]420ae012009-04-24 05:16:322607 show_suppress_checkbox, reply_msg);
2608 } else {
2609 // If we are suppressing messages, just reply as is if the user immediately
2610 // pressed "Cancel".
[email protected]beb440c2009-11-06 04:08:542611 OnMessageBoxClosed(reply_msg, false, std::wstring());
[email protected]420ae012009-04-24 05:16:322612 }
2613}
2614
2615void TabContents::RunBeforeUnloadConfirm(const std::wstring& message,
2616 IPC::Message* reply_msg) {
[email protected]7ab1e7d62009-10-14 23:32:012617 is_showing_before_unload_dialog_ = true;
[email protected]d82ed61e2009-06-16 02:46:222618 RunBeforeUnloadDialog(this, message, reply_msg);
[email protected]420ae012009-04-24 05:16:322619}
2620
2621void TabContents::ShowModalHTMLDialog(const GURL& url, int width, int height,
2622 const std::string& json_arguments,
2623 IPC::Message* reply_msg) {
2624 if (delegate()) {
2625 HtmlDialogUIDelegate* dialog_delegate =
2626 new ModalHtmlDialogDelegate(url, width, height, json_arguments,
[email protected]d82ed61e2009-06-16 02:46:222627 reply_msg, this);
[email protected]420ae012009-04-24 05:16:322628 delegate()->ShowHtmlDialog(dialog_delegate, NULL);
2629 }
2630}
2631
[email protected]4d44a1c2010-04-28 19:20:412632void TabContents::PasswordFormsFound(
[email protected]ed3fb032009-06-16 19:50:562633 const std::vector<webkit_glue::PasswordForm>& forms) {
[email protected]4d44a1c2010-04-28 19:20:412634 GetPasswordManager()->PasswordFormsFound(forms);
2635}
2636
2637void TabContents::PasswordFormsVisible(
2638 const std::vector<webkit_glue::PasswordForm>& visible_forms) {
2639 GetPasswordManager()->PasswordFormsVisible(visible_forms);
[email protected]420ae012009-04-24 05:16:322640}
2641
[email protected]420ae012009-04-24 05:16:322642// Checks to see if we should generate a keyword based on the OSDD, and if
2643// necessary uses TemplateURLFetcher to download the OSDD and create a keyword.
2644void TabContents::PageHasOSDD(RenderViewHost* render_view_host,
2645 int32 page_id, const GURL& url,
2646 bool autodetected) {
2647 // Make sure page_id is the current page, and the TemplateURLModel is loaded.
2648 DCHECK(url.is_valid());
2649 if (!IsActiveEntry(page_id))
2650 return;
2651 TemplateURLModel* url_model = profile()->GetTemplateURLModel();
2652 if (!url_model)
2653 return;
2654 if (!url_model->loaded()) {
2655 url_model->Load();
2656 return;
2657 }
2658 if (!profile()->GetTemplateURLFetcher())
2659 return;
2660
2661 if (profile()->IsOffTheRecord())
2662 return;
2663
2664 const NavigationEntry* entry = controller_.GetLastCommittedEntry();
2665 DCHECK(entry);
2666
2667 const NavigationEntry* base_entry = entry;
2668 if (IsFormSubmit(base_entry)) {
2669 // If the current page is a form submit, find the last page that was not
2670 // a form submit and use its url to generate the keyword from.
2671 int index = controller_.last_committed_entry_index() - 1;
2672 while (index >= 0 && IsFormSubmit(controller_.GetEntryAtIndex(index)))
2673 index--;
2674 if (index >= 0)
2675 base_entry = controller_.GetEntryAtIndex(index);
2676 else
2677 base_entry = NULL;
2678 }
2679
2680 // We want to use the user typed URL if available since that represents what
2681 // the user typed to get here, and fall back on the regular URL if not.
2682 if (!base_entry)
2683 return;
2684 GURL keyword_url = base_entry->user_typed_url().is_valid() ?
2685 base_entry->user_typed_url() : base_entry->url();
2686 if (!keyword_url.is_valid())
2687 return;
2688 std::wstring keyword = TemplateURLModel::GenerateKeyword(keyword_url,
2689 autodetected);
[email protected]a88dbdd52010-01-15 19:25:532690
2691 // For JS added OSDD empty keyword is OK because we will generate keyword
2692 // later from OSDD content.
2693 if (autodetected && keyword.empty())
[email protected]420ae012009-04-24 05:16:322694 return;
2695 const TemplateURL* template_url =
2696 url_model->GetTemplateURLForKeyword(keyword);
2697 if (template_url && (!template_url->safe_for_autoreplace() ||
2698 template_url->originating_url() == url)) {
2699 // Either there is a user created TemplateURL for this keyword, or the
2700 // keyword has the same OSDD url and we've parsed it.
2701 return;
2702 }
2703
2704 // Download the OpenSearch description document. If this is successful a
2705 // new keyword will be created when done.
[email protected]420ae012009-04-24 05:16:322706 profile()->GetTemplateURLFetcher()->ScheduleDownload(
2707 keyword,
2708 url,
2709 base_entry->favicon().url(),
[email protected]d0cc9fb52009-06-23 02:26:162710 this,
[email protected]420ae012009-04-24 05:16:322711 autodetected);
2712}
2713
[email protected]420ae012009-04-24 05:16:322714GURL TabContents::GetAlternateErrorPageURL() const {
2715 GURL url;
2716 // Disable alternate error pages when in OffTheRecord/Incognito mode.
2717 if (profile()->IsOffTheRecord())
2718 return url;
2719
2720 PrefService* prefs = profile()->GetPrefs();
2721 DCHECK(prefs);
2722 if (prefs->GetBoolean(prefs::kAlternateErrorPagesEnabled)) {
[email protected]a33457b82009-09-21 23:07:152723 url = google_util::AppendGoogleLocaleParam(
2724 GURL(google_util::kLinkDoctorBaseURL));
[email protected]420ae012009-04-24 05:16:322725 url = google_util::AppendGoogleTLDParam(url);
2726 }
2727 return url;
2728}
2729
2730WebPreferences TabContents::GetWebkitPrefs() {
[email protected]6c32ce72010-03-08 05:18:062731 Profile* profile = render_view_host()->process()->profile();
[email protected]7ccddb8c2009-08-04 17:36:552732 bool is_dom_ui = false;
[email protected]6c32ce72010-03-08 05:18:062733 return RenderViewHostDelegateHelper::GetWebkitPrefs(profile, is_dom_ui);
[email protected]420ae012009-04-24 05:16:322734}
2735
[email protected]fa1cf0b82010-01-15 21:49:442736void TabContents::OnIgnoredUIEvent() {
2737 if (constrained_window_count()) {
2738 ConstrainedWindow* window = *constrained_window_begin();
2739 window->FocusConstrainedWindow();
2740 }
2741}
2742
[email protected]420ae012009-04-24 05:16:322743void TabContents::OnJSOutOfMemory() {
2744 AddInfoBar(new SimpleAlertInfoBarDelegate(
[email protected]938e1f92010-04-01 18:09:422745 this, l10n_util::GetString(IDS_JS_OUT_OF_MEMORY_PROMPT), NULL, true));
[email protected]420ae012009-04-24 05:16:322746}
2747
[email protected]420ae012009-04-24 05:16:322748void TabContents::OnCrossSiteResponse(int new_render_process_host_id,
2749 int new_request_id) {
[email protected]57c6a652009-05-04 07:58:342750 // Allows the TabContents to react when a cross-site response is ready to be
[email protected]420ae012009-04-24 05:16:322751 // delivered to a pending RenderViewHost. We must first run the onunload
2752 // handler of the old RenderViewHost before we can allow it to proceed.
2753 render_manager_.OnCrossSiteResponse(new_render_process_host_id,
2754 new_request_id);
2755}
2756
2757bool TabContents::CanBlur() const {
2758 return delegate() ? delegate()->CanBlur() : true;
2759}
2760
2761gfx::Rect TabContents::GetRootWindowResizerRect() const {
2762 if (delegate())
2763 return delegate()->GetRootWindowResizerRect();
2764 return gfx::Rect();
2765}
2766
2767void TabContents::RendererUnresponsive(RenderViewHost* rvh,
2768 bool is_during_unload) {
2769 if (is_during_unload) {
2770 // Hang occurred while firing the beforeunload/unload handler.
2771 // Pretend the handler fired so tab closing continues as if it had.
[email protected]829e7612009-04-25 01:15:112772 rvh->set_sudden_termination_allowed(true);
[email protected]420ae012009-04-24 05:16:322773
2774 if (!render_manager_.ShouldCloseTabOnUnresponsiveRenderer())
2775 return;
2776
2777 // If the tab hangs in the beforeunload/unload handler there's really
2778 // nothing we can do to recover. Pretend the unload listeners have
2779 // all fired and close the tab. If the hang is in the beforeunload handler
2780 // then the user will not have the option of cancelling the close.
2781 Close(rvh);
2782 return;
2783 }
2784
2785 if (render_view_host() && render_view_host()->IsRenderViewLive())
[email protected]e7b8d832009-11-20 22:41:002786 hung_renderer_dialog::ShowForTabContents(this);
[email protected]420ae012009-04-24 05:16:322787}
2788
2789void TabContents::RendererResponsive(RenderViewHost* render_view_host) {
[email protected]e7b8d832009-11-20 22:41:002790 hung_renderer_dialog::HideForTabContents(this);
[email protected]420ae012009-04-24 05:16:322791}
2792
2793void TabContents::LoadStateChanged(const GURL& url,
[email protected]094e5b22009-09-25 04:23:562794 net::LoadState load_state,
2795 uint64 upload_position,
2796 uint64 upload_size) {
[email protected]420ae012009-04-24 05:16:322797 load_state_ = load_state;
[email protected]094e5b22009-09-25 04:23:562798 upload_position_ = upload_position;
2799 upload_size_ = upload_size;
[email protected]d686e812009-06-03 19:10:292800 std::wstring languages =
[email protected]ddd231e2010-06-29 20:35:192801 UTF8ToWide(profile()->GetPrefs()->GetString(prefs::kAcceptLanguages));
[email protected]d686e812009-06-03 19:10:292802 std::string host = url.host();
[email protected]ce85f602009-11-07 01:34:532803 load_state_host_ =
2804 net::IDNToUnicode(host.c_str(), host.size(), languages, NULL);
[email protected]420ae012009-04-24 05:16:322805 if (load_state_ == net::LOAD_STATE_READING_RESPONSE)
2806 SetNotWaitingForResponse();
2807 if (is_loading())
[email protected]c9cd2222009-05-06 05:16:502808 NotifyNavigationStateChanged(INVALIDATE_LOAD | INVALIDATE_TAB);
[email protected]420ae012009-04-24 05:16:322809}
2810
[email protected]1ee614862009-07-06 20:29:182811bool TabContents::IsExternalTabContainer() const {
2812 if (!delegate())
2813 return false;
2814
2815 return delegate()->IsExternalTabContainer();
2816}
2817
[email protected]1ee614862009-07-06 20:29:182818void TabContents::DidInsertCSS() {
2819 // This RVHDelegate function is used for extensions and not us.
[email protected]420ae012009-04-24 05:16:322820}
2821
[email protected]130efb02009-09-18 18:54:352822void TabContents::FocusedNodeChanged() {
2823 NotificationService::current()->Notify(
2824 NotificationType::FOCUS_CHANGED_IN_PAGE,
2825 Source<RenderViewHost>(render_view_host()),
2826 NotificationService::NoDetails());
2827}
2828
[email protected]420ae012009-04-24 05:16:322829void TabContents::FileSelected(const FilePath& path,
2830 int index, void* params) {
[email protected]35896a32010-06-09 08:42:512831 profile()->set_last_selected_directory(path.DirName());
[email protected]cdaf8d02010-03-30 19:52:472832 std::vector<FilePath> files;
2833 files.push_back(path);
2834 render_view_host()->FilesSelectedInChooser(files);
[email protected]420ae012009-04-24 05:16:322835}
2836
2837void TabContents::MultiFilesSelected(const std::vector<FilePath>& files,
2838 void* params) {
[email protected]35896a32010-06-09 08:42:512839 if (!files.empty())
2840 profile()->set_last_selected_directory(files[0].DirName());
[email protected]cdaf8d02010-03-30 19:52:472841 render_view_host()->FilesSelectedInChooser(files);
[email protected]420ae012009-04-24 05:16:322842}
2843
2844void TabContents::FileSelectionCanceled(void* params) {
2845 // If the user cancels choosing a file to upload we pass back an
2846 // empty vector.
[email protected]cdaf8d02010-03-30 19:52:472847 render_view_host()->FilesSelectedInChooser(std::vector<FilePath>());
[email protected]420ae012009-04-24 05:16:322848}
2849
2850void TabContents::BeforeUnloadFiredFromRenderManager(
2851 bool proceed,
2852 bool* proceed_to_fire_unload) {
2853 if (delegate())
2854 delegate()->BeforeUnloadFired(this, proceed, proceed_to_fire_unload);
2855}
2856
2857void TabContents::UpdateRenderViewSizeForRenderManager() {
2858 // TODO(brettw) this is a hack. See TabContentsView::SizeContents.
[email protected]0323ee42010-02-17 22:03:262859 gfx::Size size = view_->GetContainerSize();
2860 // 0x0 isn't a valid window size (minimal window size is 1x1) but it may be
2861 // here during container initialization and normal window size will be set
2862 // later. In case of tab duplication this resizing to 0x0 prevents setting
2863 // normal size later so just ignore it.
2864 if (!size.IsEmpty())
2865 view_->SizeContents(size);
[email protected]420ae012009-04-24 05:16:322866}
2867
2868DOMUI* TabContents::CreateDOMUIForRenderManager(const GURL& url) {
[email protected]d82ed61e2009-06-16 02:46:222869 return DOMUIFactory::CreateDOMUIForURL(this, url);
[email protected]420ae012009-04-24 05:16:322870}
2871
2872NavigationEntry*
2873TabContents::GetLastCommittedNavigationEntryForRenderManager() {
2874 return controller_.GetLastCommittedEntry();
2875}
2876
2877bool TabContents::CreateRenderViewForRenderManager(
2878 RenderViewHost* render_view_host) {
[email protected]420ae012009-04-24 05:16:322879 RenderWidgetHostView* rwh_view = view_->CreateViewForWidget(render_view_host);
[email protected]1a98a932009-11-17 00:12:522880
2881 scoped_refptr<URLRequestContextGetter> request_context = request_context_;
2882 if (!request_context.get())
2883 request_context = profile()->GetRequestContext();
2884
[email protected]8ab04652010-06-12 02:47:262885 if (!render_view_host->CreateRenderView(request_context, string16()))
[email protected]420ae012009-04-24 05:16:322886 return false;
2887
2888 // Now that the RenderView has been created, we need to tell it its size.
2889 rwh_view->SetSize(view_->GetContainerSize());
2890
2891 UpdateMaxPageIDIfNecessary(render_view_host->site_instance(),
2892 render_view_host);
2893 return true;
2894}
2895
2896void TabContents::Observe(NotificationType type,
2897 const NotificationSource& source,
2898 const NotificationDetails& details) {
2899 switch (type.value) {
2900 case NotificationType::BOOKMARK_MODEL_LOADED:
2901 // BookmarkModel finished loading, fall through to update starred state.
2902 case NotificationType::URLS_STARRED: {
2903 // Somewhere, a URL has been starred.
2904 // Ignore notifications for profiles other than our current one.
2905 Profile* source_profile = Source<Profile>(source).ptr();
2906 if (!source_profile || !source_profile->IsSameProfile(profile()))
2907 return;
2908
2909 UpdateStarredStateForCurrentURL();
2910 break;
2911 }
2912 case NotificationType::PREF_CHANGED: {
2913 std::wstring* pref_name_in = Details<std::wstring>(details).ptr();
2914 DCHECK(Source<PrefService>(source).ptr() == profile()->GetPrefs());
2915 if (*pref_name_in == prefs::kAlternateErrorPagesEnabled) {
2916 UpdateAlternateErrorPageURL();
2917 } else if (*pref_name_in == prefs::kDefaultCharset ||
2918 StartsWithASCII(WideToUTF8(*pref_name_in), "webkit.webprefs.", true)
2919 ) {
2920 UpdateWebPreferences();
2921 } else {
2922 NOTREACHED() << "unexpected pref change notification" << *pref_name_in;
2923 }
2924 break;
2925 }
2926 case NotificationType::RENDER_WIDGET_HOST_DESTROYED:
2927 view_->RenderWidgetHostDestroyed(Source<RenderWidgetHost>(source).ptr());
2928 break;
2929
2930 case NotificationType::NAV_ENTRY_COMMITTED: {
2931 DCHECK(&controller_ == Source<NavigationController>(source).ptr());
2932
2933 NavigationController::LoadCommittedDetails& committed_details =
2934 *(Details<NavigationController::LoadCommittedDetails>(details).ptr());
2935 ExpireInfoBars(committed_details);
2936 break;
2937 }
2938
[email protected]d3ba77272009-09-03 00:06:092939#if defined(OS_LINUX)
2940 case NotificationType::BROWSER_THEME_CHANGED: {
[email protected]7ff431e2010-01-07 18:18:542941 renderer_preferences_util::UpdateFromSystemSettings(
2942 &renderer_preferences_, profile());
[email protected]d3ba77272009-09-03 00:06:092943 render_view_host()->SyncRendererPrefs();
2944 break;
2945 }
2946#endif
2947
[email protected]6c32ce72010-03-08 05:18:062948 case NotificationType::USER_STYLE_SHEET_UPDATED:
2949 UpdateWebPreferences();
2950 break;
2951
[email protected]79580c62010-02-02 02:36:252952 case NotificationType::CONTENT_SETTINGS_CHANGED: {
[email protected]0314ae02010-04-08 09:18:292953 Details<const HostContentSettingsMap::ContentSettingsDetails>
[email protected]79580c62010-02-02 02:36:252954 settings_details(details);
[email protected]79580c62010-02-02 02:36:252955 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]da0c8e92010-02-09 23:25:492956 GURL entry_url;
[email protected]0314ae02010-04-08 09:18:292957 if (entry)
[email protected]da0c8e92010-02-09 23:25:492958 entry_url = entry->url();
[email protected]0314ae02010-04-08 09:18:292959 if (settings_details.ptr()->update_all() ||
2960 settings_details.ptr()->pattern().Matches(entry_url)) {
2961 render_view_host()->SendContentSettings(entry_url,
[email protected]b9a4ea7f2010-06-25 07:11:232962 profile()->GetHostContentSettingsMap()->
2963 GetContentSettings(entry_url));
[email protected]79580c62010-02-02 02:36:252964 }
2965 break;
2966 }
2967
[email protected]e5adff72010-06-14 18:13:062968 case NotificationType::EXTENSION_LOADED:
[email protected]a888b29e62010-04-01 13:38:572969 break;
[email protected]a888b29e62010-04-01 13:38:572970
2971 case NotificationType::EXTENSION_UNLOADED:
[email protected]e5adff72010-06-14 18:13:062972 case NotificationType::EXTENSION_UNLOADED_DISABLED:
[email protected]a888b29e62010-04-01 13:38:572973 break;
[email protected]a888b29e62010-04-01 13:38:572974
[email protected]420ae012009-04-24 05:16:322975 default:
2976 NOTREACHED();
2977 }
2978}
[email protected]aacd7cc2009-09-02 14:51:102979
[email protected]98aa0b52010-05-06 17:03:082980void TabContents::UpdateExtensionAppIcon(Extension* extension) {
2981 extension_app_icon_.reset();
[email protected]f075fe0a22010-04-26 04:06:432982
[email protected]a888b29e62010-04-01 13:38:572983 if (extension) {
[email protected]98aa0b52010-05-06 17:03:082984 extension_app_image_loader_.reset(new ImageLoadingTracker(this));
2985 extension_app_image_loader_->LoadImage(
[email protected]a888b29e62010-04-01 13:38:572986 extension,
2987 extension->GetIconPath(Extension::EXTENSION_ICON_SMALLISH),
2988 gfx::Size(Extension::EXTENSION_ICON_SMALLISH,
2989 Extension::EXTENSION_ICON_SMALLISH),
2990 ImageLoadingTracker::CACHE);
2991 } else {
[email protected]98aa0b52010-05-06 17:03:082992 extension_app_image_loader_.reset(NULL);
[email protected]a888b29e62010-04-01 13:38:572993 }
2994}
2995
2996Extension* TabContents::GetExtensionContaining(const GURL& url) {
2997 ExtensionsService* extensions_service = profile()->GetExtensionsService();
2998 if (!extensions_service)
2999 return NULL;
3000
3001 Extension* extension = extensions_service->GetExtensionByURL(url);
3002 return extension ?
3003 extension : extensions_service->GetExtensionByWebExtent(url);
3004}
3005
[email protected]a888b29e62010-04-01 13:38:573006void TabContents::OnImageLoaded(SkBitmap* image, ExtensionResource resource,
3007 int index) {
3008 if (image) {
[email protected]98aa0b52010-05-06 17:03:083009 extension_app_icon_ = *image;
[email protected]a888b29e62010-04-01 13:38:573010 NotifyNavigationStateChanged(INVALIDATE_TAB);
3011 }
3012}
3013
[email protected]beb440c2009-11-06 04:08:543014std::wstring TabContents::GetMessageBoxTitle(const GURL& frame_url,
3015 bool is_alert) {
3016 if (!frame_url.has_host())
3017 return l10n_util::GetString(
3018 is_alert ? IDS_JAVASCRIPT_ALERT_DEFAULT_TITLE
3019 : IDS_JAVASCRIPT_MESSAGEBOX_DEFAULT_TITLE);
3020
3021 // We really only want the scheme, hostname, and port.
3022 GURL::Replacements replacements;
3023 replacements.ClearUsername();
3024 replacements.ClearPassword();
3025 replacements.ClearPath();
3026 replacements.ClearQuery();
3027 replacements.ClearRef();
3028 GURL clean_url = frame_url.ReplaceComponents(replacements);
3029
3030 // TODO(brettw) it should be easier than this to do the correct language
3031 // handling without getting the accept language from the profile.
3032 std::wstring base_address = gfx::ElideUrl(clean_url, gfx::Font(), 0,
[email protected]ddd231e2010-06-29 20:35:193033 UTF8ToWide(profile()->GetPrefs()->GetString(prefs::kAcceptLanguages)));
[email protected]beb440c2009-11-06 04:08:543034 // Force URL to have LTR directionality.
[email protected]4a4afe32010-05-05 01:39:563035 base::i18n::GetDisplayStringInLTRDirectionality(&base_address);
[email protected]beb440c2009-11-06 04:08:543036
3037 return l10n_util::GetStringF(
3038 is_alert ? IDS_JAVASCRIPT_ALERT_TITLE : IDS_JAVASCRIPT_MESSAGEBOX_TITLE,
3039 base_address);
3040}
3041
3042gfx::NativeWindow TabContents::GetMessageBoxRootWindow() {
3043 return view_->GetTopLevelNativeWindow();
3044}
3045
3046void TabContents::OnMessageBoxClosed(IPC::Message* reply_msg,
3047 bool success,
3048 const std::wstring& prompt) {
3049 last_javascript_message_dismissal_ = base::TimeTicks::Now();
3050 if (is_showing_before_unload_dialog_ && !success) {
3051 // If a beforeunload dialog is canceled, we need to stop the throbber from
3052 // spinning, since we forced it to start spinning in Navigate.
3053 DidStopLoading();
3054
3055 tab_close_start_time_ = base::TimeTicks();
3056 }
3057 is_showing_before_unload_dialog_ = false;
3058 render_view_host()->JavaScriptMessageBoxClosed(reply_msg, success, prompt);
3059}
3060
3061void TabContents::SetSuppressMessageBoxes(bool suppress_message_boxes) {
3062 set_suppress_javascript_messages(suppress_message_boxes);
3063}
3064
[email protected]41fc0322009-09-04 22:23:403065void TabContents::set_encoding(const std::string& encoding) {
[email protected]aacd7cc2009-09-02 14:51:103066 encoding_ = CharacterEncoding::GetCanonicalEncodingNameByAliasName(encoding);
3067}
[email protected]f45d2a72010-03-08 23:28:353068
3069void TabContents::SetAppIcon(const SkBitmap& app_icon) {
3070 app_icon_ = app_icon;
3071 NotifyNavigationStateChanged(INVALIDATE_TITLE);
3072}
[email protected]ea68c06a2010-04-23 17:25:373073
3074// After a successful *new* login attempt, we take the PasswordFormManager in
3075// provisional_save_manager_ and move it to a SavePasswordInfoBarDelegate while
3076// the user makes up their mind with the "save password" infobar. Note if the
3077// login is one we already know about, the end of the line is
3078// provisional_save_manager_ because we just update it on success and so such
3079// forms never end up in an infobar.
3080class SavePasswordInfoBarDelegate : public ConfirmInfoBarDelegate {
3081 public:
3082 SavePasswordInfoBarDelegate(TabContents* tab_contents,
[email protected]12bb6822010-06-16 02:00:463083 PasswordFormManager* form_to_save)
3084 : ConfirmInfoBarDelegate(tab_contents),
3085 form_to_save_(form_to_save),
3086 infobar_response_(kNoResponse) {
[email protected]ea68c06a2010-04-23 17:25:373087 }
3088
3089 virtual ~SavePasswordInfoBarDelegate() { }
3090
3091 // Overridden from ConfirmInfoBarDelegate:
3092 virtual void InfoBarClosed() {
[email protected]12bb6822010-06-16 02:00:463093 UMA_HISTOGRAM_ENUMERATION("PasswordManager.InfoBarResponse",
3094 infobar_response_, kNumResponseTypes);
[email protected]ea68c06a2010-04-23 17:25:373095 delete this;
3096 }
3097
3098 virtual std::wstring GetMessageText() const {
3099 return l10n_util::GetString(IDS_PASSWORD_MANAGER_SAVE_PASSWORD_PROMPT);
3100 }
3101
3102 virtual SkBitmap* GetIcon() const {
3103 return ResourceBundle::GetSharedInstance().GetBitmapNamed(
3104 IDR_INFOBAR_SAVE_PASSWORD);
3105 }
3106
3107 virtual int GetButtons() const {
3108 return BUTTON_OK | BUTTON_CANCEL;
3109 }
3110
3111 virtual std::wstring GetButtonLabel(InfoBarButton button) const {
3112 if (button == BUTTON_OK)
3113 return l10n_util::GetString(IDS_PASSWORD_MANAGER_SAVE_BUTTON);
3114 if (button == BUTTON_CANCEL)
3115 return l10n_util::GetString(IDS_PASSWORD_MANAGER_BLACKLIST_BUTTON);
3116 NOTREACHED();
3117 return std::wstring();
3118 }
3119
3120 virtual bool Accept() {
3121 DCHECK(form_to_save_.get());
3122 form_to_save_->Save();
[email protected]12bb6822010-06-16 02:00:463123 infobar_response_ = kRememberPassword;
[email protected]ea68c06a2010-04-23 17:25:373124 return true;
3125 }
3126
3127 virtual bool Cancel() {
3128 DCHECK(form_to_save_.get());
3129 form_to_save_->PermanentlyBlacklist();
[email protected]12bb6822010-06-16 02:00:463130 infobar_response_ = kDontRememberPassword;
[email protected]ea68c06a2010-04-23 17:25:373131 return true;
3132 }
3133
3134 private:
3135 // The PasswordFormManager managing the form we're asking the user about,
3136 // and should update as per her decision.
3137 scoped_ptr<PasswordFormManager> form_to_save_;
3138
[email protected]12bb6822010-06-16 02:00:463139 // Used to track the results we get from the info bar.
3140 enum ResponseType {
3141 kNoResponse = 0,
3142 kRememberPassword,
3143 kDontRememberPassword,
3144 kNumResponseTypes,
3145 };
3146 ResponseType infobar_response_;
3147
[email protected]ea68c06a2010-04-23 17:25:373148 DISALLOW_COPY_AND_ASSIGN(SavePasswordInfoBarDelegate);
3149};
3150
3151void TabContents::FillPasswordForm(
3152 const webkit_glue::PasswordFormDomManager::FillData& form_data) {
3153 render_view_host()->FillPasswordForm(form_data);
3154}
3155
3156void TabContents::AddSavePasswordInfoBar(PasswordFormManager* form_to_save) {
3157 AddInfoBar(new SavePasswordInfoBarDelegate(this, form_to_save));
3158}
3159
3160Profile* TabContents::GetProfileForPasswordManager() {
3161 return profile();
3162}
3163
3164bool TabContents::DidLastPageLoadEncounterSSLErrors() {
3165 return controller().ssl_manager()->ProcessedSSLErrorFromRequest();
3166}