blob: b5395ccf89530655321df0903b032a1dd1740d97 [file] [log] [blame]
[email protected]7e7414ae2010-01-26 20:19:291// Copyright (c) 2010 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
[email protected]f3ec7742009-01-15 00:59:165#include "chrome/browser/tab_contents/tab_contents.h"
initial.commit09911bf2008-07-26 23:55:296
[email protected]06821f42d2010-05-03 22:34:357#if defined(OS_CHROMEOS)
8// For GdkScreen
9#include <gdk/gdk.h>
10#endif // defined(OS_CHROMEOS)
11
[email protected]a92b8642009-05-05 23:38:5612#include "app/l10n_util.h"
[email protected]9929da92009-05-05 02:05:1113#include "app/resource_bundle.h"
[email protected]614519812010-03-19 08:20:5614#include "app/text_elider.h"
[email protected]8cc8d492010-02-02 10:40:4915#include "base/auto_reset.h"
[email protected]420ae012009-04-24 05:16:3216#include "base/file_version_info.h"
[email protected]7cf1b6ce2010-03-20 06:37:0117#include "base/i18n/rtl.h"
[email protected]420ae012009-04-24 05:16:3218#include "base/process_util.h"
[email protected]96d185d2009-04-24 03:28:5419#include "base/string16.h"
[email protected]996fd702009-09-04 19:12:3720#include "base/string_util.h"
[email protected]96d185d2009-04-24 03:28:5421#include "base/time.h"
[email protected]6db0def92010-03-02 19:55:4522#include "chrome/browser/autocomplete_history_manager.h"
[email protected]21674df2009-10-28 19:51:2123#include "chrome/browser/autofill/autofill_manager.h"
[email protected]66ba4932009-06-04 19:22:1324#include "chrome/browser/blocked_popup_container.h"
[email protected]420ae012009-04-24 05:16:3225#include "chrome/browser/bookmarks/bookmark_model.h"
[email protected]281918912010-05-27 22:05:1326#include "chrome/browser/browser.h"
[email protected]96d185d2009-04-24 03:28:5427#include "chrome/browser/browser_process.h"
[email protected]cf68b092010-01-15 20:05:5528#include "chrome/browser/browser_shutdown.h"
initial.commit09911bf2008-07-26 23:55:2929#include "chrome/browser/cert_store.h"
[email protected]aacd7cc2009-09-02 14:51:1030#include "chrome/browser/character_encoding.h"
[email protected]96d185d2009-04-24 03:28:5431#include "chrome/browser/debugger/devtools_manager.h"
[email protected]420ae012009-04-24 05:16:3232#include "chrome/browser/dom_operation_notification_details.h"
[email protected]96d185d2009-04-24 03:28:5433#include "chrome/browser/dom_ui/dom_ui.h"
34#include "chrome/browser/dom_ui/dom_ui_factory.h"
[email protected]bcef0dc02009-02-28 00:35:0235#include "chrome/browser/download/download_item_model.h"
[email protected]420ae012009-04-24 05:16:3236#include "chrome/browser/download/download_manager.h"
[email protected]41291322010-07-15 17:09:0137#include "chrome/browser/download/download_request_limiter.h"
[email protected]e7eaedde2009-09-25 20:09:4938#include "chrome/browser/external_protocol_handler.h"
[email protected]92c6f9b92009-10-24 04:35:0839#include "chrome/browser/extensions/extensions_service.h"
[email protected]523623c2010-05-19 16:03:4240#include "chrome/browser/history/top_sites.h"
[email protected]0189bc722009-08-28 21:56:4841#include "chrome/browser/favicon_service.h"
[email protected]e5d2f7132010-04-09 18:13:5342#include "chrome/browser/find_bar_state.h"
[email protected]420ae012009-04-24 05:16:3243#include "chrome/browser/google_util.h"
[email protected]79580c62010-02-02 02:36:2544#include "chrome/browser/host_content_settings_map.h"
[email protected]8897c382009-05-06 17:53:2645#include "chrome/browser/hung_renderer_dialog.h"
[email protected]12f74a92010-02-05 22:32:1446#include "chrome/browser/message_box_handler.h"
[email protected]420ae012009-04-24 05:16:3247#include "chrome/browser/load_from_memory_cache_details.h"
48#include "chrome/browser/load_notification_details.h"
[email protected]84abba62009-10-07 17:01:4449#include "chrome/browser/metrics/metric_event_duration_details.h"
[email protected]ea0e80892009-08-22 00:36:0550#include "chrome/browser/modal_html_dialog_delegate.h"
[email protected]c8865482009-07-23 20:40:1051#include "chrome/browser/omnibox_search_hint.h"
[email protected]fdd61c62009-04-22 19:22:5752#include "chrome/browser/password_manager/password_manager.h"
[email protected]14a000d2010-04-29 21:44:2453#include "chrome/browser/platform_util.h"
[email protected]fdd61c62009-04-22 19:22:5754#include "chrome/browser/plugin_installer.h"
[email protected]052313b2010-02-19 09:43:0855#include "chrome/browser/pref_service.h"
[email protected]12802702010-07-09 19:43:0956#include "chrome/browser/printing/print_view_manager.h"
[email protected]ce560f82009-06-03 09:39:4457#include "chrome/browser/profile.h"
[email protected]1db6ff152009-10-12 15:32:0758#include "chrome/browser/renderer_host/render_process_host.h"
[email protected]8cb5d5b2010-02-09 11:36:1659#include "chrome/browser/renderer_host/render_view_host.h"
[email protected]96d185d2009-04-24 03:28:5460#include "chrome/browser/renderer_host/render_widget_host_view.h"
[email protected]fd694982009-07-03 17:32:3761#include "chrome/browser/renderer_host/resource_request_details.h"
[email protected]1db6ff152009-10-12 15:32:0762#include "chrome/browser/renderer_host/site_instance.h"
[email protected]96d185d2009-04-24 03:28:5463#include "chrome/browser/renderer_host/web_cache_manager.h"
[email protected]93623c5d2009-12-10 21:40:3264#include "chrome/browser/renderer_preferences_util.h"
[email protected]152b8ba32010-03-29 19:40:1665#include "chrome/browser/search_engines/template_url_fetcher.h"
66#include "chrome/browser/search_engines/template_url_model.h"
[email protected]c37e3b62010-01-22 00:15:1767#include "chrome/browser/sessions/session_types.h"
[email protected]bd1ad682009-05-15 22:19:1768#include "chrome/browser/tab_contents/infobar_delegate.h"
[email protected]7e383692009-06-12 19:14:5469#include "chrome/browser/tab_contents/interstitial_page.h"
[email protected]f3ec7742009-01-15 00:59:1670#include "chrome/browser/tab_contents/navigation_entry.h"
[email protected]de34a962009-06-26 23:22:4371#include "chrome/browser/tab_contents/provisional_load_details.h"
[email protected]f3ec7742009-01-15 00:59:1672#include "chrome/browser/tab_contents/tab_contents_delegate.h"
[email protected]fdd61c62009-04-22 19:22:5773#include "chrome/browser/tab_contents/tab_contents_view.h"
[email protected]58dca552009-06-17 00:35:0274#include "chrome/browser/tab_contents/thumbnail_generator.h"
[email protected]152b8ba32010-03-29 19:40:1675#include "chrome/browser/translate/page_translated_details.h"
[email protected]54ec6472010-04-09 19:39:5876#include "chrome/common/bindings_policy.h"
[email protected]35f7d212009-04-29 21:19:2777#include "chrome/common/chrome_switches.h"
[email protected]3b355642010-02-05 16:01:4978#include "chrome/common/extensions/extension.h"
[email protected]5d246db22009-10-27 06:17:5779#include "chrome/common/extensions/extension_action.h"
[email protected]942690b132010-05-11 06:42:1480#include "chrome/common/extensions/extension_resource.h"
[email protected]12802702010-07-09 19:43:0981#include "chrome/common/extensions/url_pattern.h"
82#include "chrome/common/navigation_types.h"
83#include "chrome/common/net/url_request_context_getter.h"
[email protected]bfd04a62009-02-01 18:16:5684#include "chrome/common/notification_service.h"
initial.commit09911bf2008-07-26 23:55:2985#include "chrome/common/pref_names.h"
[email protected]420ae012009-04-24 05:16:3286#include "chrome/common/render_messages.h"
[email protected]1ee614862009-07-06 20:29:1887#include "chrome/common/renderer_preferences.h"
[email protected]96d185d2009-04-24 03:28:5488#include "chrome/common/url_constants.h"
[email protected]ea68c06a2010-04-23 17:25:3789#include "grit/chromium_strings.h"
[email protected]8897c382009-05-06 17:53:2690#include "grit/generated_resources.h"
[email protected]420ae012009-04-24 05:16:3291#include "grit/locale_settings.h"
[email protected]fd626782010-01-22 05:26:0692#include "grit/platform_locale_settings.h"
[email protected]ea68c06a2010-04-23 17:25:3793#include "grit/theme_resources.h"
[email protected]420ae012009-04-24 05:16:3294#include "net/base/mime_util.h"
95#include "net/base/net_errors.h"
[email protected]d686e812009-06-03 19:10:2996#include "net/base/net_util.h"
[email protected]420ae012009-04-24 05:16:3297#include "net/base/registry_controlled_domain.h"
[email protected]1ee614862009-07-06 20:29:1898#include "webkit/glue/webpreferences.h"
[email protected]3e45ba92009-02-20 21:09:0099
[email protected]420ae012009-04-24 05:16:32100// Cross-Site Navigations
101//
102// If a TabContents is told to navigate to a different web site (as determined
103// by SiteInstance), it will replace its current RenderViewHost with a new
104// RenderViewHost dedicated to the new SiteInstance. This works as follows:
105//
106// - Navigate determines whether the destination is cross-site, and if so,
107// it creates a pending_render_view_host_ and moves into the PENDING
108// RendererState.
109// - The pending RVH is "suspended," so that no navigation messages are sent to
110// its renderer until the onbeforeunload JavaScript handler has a chance to
111// run in the current RVH.
112// - The pending RVH tells CrossSiteRequestManager (a thread-safe singleton)
113// that it has a pending cross-site request. ResourceDispatcherHost will
114// check for this when the response arrives.
115// - The current RVH runs its onbeforeunload handler. If it returns false, we
116// cancel all the pending logic and go back to NORMAL. Otherwise we allow
117// the pending RVH to send the navigation request to its renderer.
118// - ResourceDispatcherHost receives a ResourceRequest on the IO thread. It
119// checks CrossSiteRequestManager to see that the RVH responsible has a
120// pending cross-site request, and then installs a CrossSiteEventHandler.
121// - When RDH receives a response, the BufferedEventHandler determines whether
122// it is a download. If so, it sends a message to the new renderer causing
123// it to cancel the request, and the download proceeds in the download
124// thread. For now, we stay in a PENDING state (with a pending RVH) until
[email protected]57c6a652009-05-04 07:58:34125// the next DidNavigate event for this TabContents. This isn't ideal, but it
[email protected]420ae012009-04-24 05:16:32126// doesn't affect any functionality.
127// - After RDH receives a response and determines that it is safe and not a
128// download, it pauses the response to first run the old page's onunload
129// handler. It does this by asynchronously calling the OnCrossSiteResponse
[email protected]57c6a652009-05-04 07:58:34130// method of TabContents on the UI thread, which sends a ClosePage message
[email protected]420ae012009-04-24 05:16:32131// to the current RVH.
132// - Once the onunload handler is finished, a ClosePage_ACK message is sent to
133// the ResourceDispatcherHost, who unpauses the response. Data is then sent
134// to the pending RVH.
135// - The pending renderer sends a FrameNavigate message that invokes the
136// DidNavigate method. This replaces the current RVH with the
137// pending RVH and goes back to the NORMAL RendererState.
138
139namespace {
140
141// Amount of time we wait between when a key event is received and the renderer
142// is queried for its state and pushed to the NavigationEntry.
143const int kQueryStateDelay = 5000;
144
145const int kSyncWaitDelay = 40;
146
[email protected]304f8792009-08-19 21:28:09147#if defined(OS_CHROMEOS)
148// If a popup window is bigger than this fraction of the screen on chrome os,
149// turn it into a tab
150const float kMaxWidthFactor = 0.5;
151const float kMaxHeightFactor = 0.6;
152#endif
153
[email protected]420ae012009-04-24 05:16:32154// If another javascript message box is displayed within
155// kJavascriptMessageExpectedDelay of a previous javascript message box being
156// dismissed, display an option to suppress future message boxes from this
157// contents.
158const int kJavascriptMessageExpectedDelay = 1000;
159
[email protected]420ae012009-04-24 05:16:32160// The list of prefs we want to observe.
161const wchar_t* kPrefsToObserve[] = {
162 prefs::kAlternateErrorPagesEnabled,
163 prefs::kWebKitJavaEnabled,
164 prefs::kWebKitJavascriptEnabled,
165 prefs::kWebKitLoadsImagesAutomatically,
166 prefs::kWebKitPluginsEnabled,
167 prefs::kWebKitUsesUniversalDetector,
168 prefs::kWebKitSerifFontFamily,
169 prefs::kWebKitSansSerifFontFamily,
170 prefs::kWebKitFixedFontFamily,
171 prefs::kWebKitDefaultFontSize,
172 prefs::kWebKitDefaultFixedFontSize,
[email protected]1661a3c02010-05-20 16:32:36173 prefs::kWebkitTabsToLinks,
[email protected]420ae012009-04-24 05:16:32174 prefs::kDefaultCharset
175 // kWebKitStandardFontIsSerif needs to be added
176 // if we let users pick which font to use, serif or sans-serif when
177 // no font is specified or a CSS generic family (serif or sans-serif)
178 // is not specified.
179};
180
181const int kPrefsToObserveLength = arraysize(kPrefsToObserve);
182
[email protected]420ae012009-04-24 05:16:32183// Returns true if the entry's transition type is FORM_SUBMIT.
184bool IsFormSubmit(const NavigationEntry* entry) {
185 return (PageTransition::StripQualifier(entry->transition_type()) ==
186 PageTransition::FORM_SUBMIT);
187}
188
189#if defined(OS_WIN)
190
191BOOL CALLBACK InvalidateWindow(HWND hwnd, LPARAM lparam) {
192 // Note: erase is required to properly paint some widgets borders. This can
193 // be seen with textfields.
194 InvalidateRect(hwnd, NULL, TRUE);
195 return TRUE;
196}
197#endif
198
[email protected]5e369672009-11-03 23:48:30199ViewMsg_Navigate_Params::NavigationType GetNavigationType(
[email protected]1ccb3568d2010-02-19 10:51:16200 Profile* profile, const NavigationEntry& entry,
201 NavigationController::ReloadType reload_type) {
202 switch (reload_type) {
203 case NavigationController::RELOAD:
204 return ViewMsg_Navigate_Params::RELOAD;
205 case NavigationController::RELOAD_IGNORING_CACHE:
206 return ViewMsg_Navigate_Params::RELOAD_IGNORING_CACHE;
207 case NavigationController::NO_RELOAD:
208 break; // Fall through to rest of function.
209 }
[email protected]5e369672009-11-03 23:48:30210
211 if (entry.restore_type() == NavigationEntry::RESTORE_LAST_SESSION &&
212 profile->DidLastSessionExitCleanly())
213 return ViewMsg_Navigate_Params::RESTORE;
214
215 return ViewMsg_Navigate_Params::NORMAL;
216}
217
[email protected]3cc72b12010-03-18 23:03:00218void MakeNavigateParams(const NavigationController& controller,
[email protected]1ccb3568d2010-02-19 10:51:16219 NavigationController::ReloadType reload_type,
220 ViewMsg_Navigate_Params* params) {
[email protected]3cc72b12010-03-18 23:03:00221 const NavigationEntry& entry = *controller.pending_entry();
[email protected]056de2d2009-06-26 16:41:34222 params->page_id = entry.page_id();
[email protected]3cc72b12010-03-18 23:03:00223 params->pending_history_list_offset = controller.pending_entry_index();
224 params->current_history_list_offset = controller.last_committed_entry_index();
225 params->current_history_list_length = controller.entry_count();
[email protected]056de2d2009-06-26 16:41:34226 params->url = entry.url();
227 params->referrer = entry.referrer();
228 params->transition = entry.transition_type();
229 params->state = entry.content_state();
[email protected]3cc72b12010-03-18 23:03:00230 params->navigation_type =
231 GetNavigationType(controller.profile(), entry, reload_type);
[email protected]056de2d2009-06-26 16:41:34232 params->request_time = base::Time::Now();
233}
234
[email protected]420ae012009-04-24 05:16:32235} // namespace
236
237// -----------------------------------------------------------------------------
238
[email protected]fdd61c62009-04-22 19:22:57239// static
240int TabContents::find_request_id_counter_ = -1;
241
[email protected]420ae012009-04-24 05:16:32242
[email protected]420ae012009-04-24 05:16:32243TabContents::TabContents(Profile* profile,
244 SiteInstance* site_instance,
245 int routing_id,
[email protected]34ac70502009-09-25 17:07:23246 const TabContents* base_tab_contents)
[email protected]b680ad22009-04-15 23:19:42247 : delegate_(NULL),
[email protected]66ba4932009-06-04 19:22:13248 ALLOW_THIS_IN_INITIALIZER_LIST(controller_(this, profile)),
249 ALLOW_THIS_IN_INITIALIZER_LIST(view_(
[email protected]d82ed61e2009-06-16 02:46:22250 TabContentsView::Create(this))),
251 ALLOW_THIS_IN_INITIALIZER_LIST(render_manager_(this, this)),
[email protected]fdd61c62009-04-22 19:22:57252 property_bag_(),
253 registrar_(),
[email protected]12802702010-07-09 19:43:09254 ALLOW_THIS_IN_INITIALIZER_LIST(printing_(
255 new printing::PrintViewManager(*this))),
[email protected]fdd61c62009-04-22 19:22:57256 save_package_(),
[email protected]6db0def92010-03-02 19:55:45257 autocomplete_history_manager_(),
[email protected]21674df2009-10-28 19:51:21258 autofill_manager_(),
[email protected]fdd61c62009-04-22 19:22:57259 password_manager_(),
260 plugin_installer_(),
[email protected]6f821dd2010-02-23 00:54:06261 bookmark_drag_(NULL),
[email protected]d82ed61e2009-06-16 02:46:22262 ALLOW_THIS_IN_INITIALIZER_LIST(fav_icon_helper_(this)),
[email protected]fdd61c62009-04-22 19:22:57263 select_file_dialog_(),
[email protected]d5f942ba2008-09-26 19:30:34264 is_loading_(false),
initial.commit09911bf2008-07-26 23:55:29265 is_crashed_(false),
[email protected]d5f942ba2008-09-26 19:30:34266 waiting_for_response_(false),
[email protected]d5f942ba2008-09-26 19:30:34267 max_page_id_(-1),
[email protected]fdd61c62009-04-22 19:22:57268 current_load_start_(),
269 load_state_(net::LOAD_STATE_IDLE),
270 load_state_host_(),
[email protected]094e5b22009-09-25 04:23:56271 upload_size_(0),
272 upload_position_(0),
[email protected]fdd61c62009-04-22 19:22:57273 received_page_title_(false),
274 is_starred_(false),
275 contents_mime_type_(),
276 encoding_(),
[email protected]332af7732009-03-11 13:21:35277 blocked_popups_(NULL),
[email protected]8cc8d492010-02-02 10:40:49278 dont_notify_render_view_(false),
[email protected]f17a0ee2010-05-17 17:38:47279 displayed_insecure_content_(false),
[email protected]fdd61c62009-04-22 19:22:57280 infobar_delegates_(),
[email protected]fdd61c62009-04-22 19:22:57281 find_ui_active_(false),
282 find_op_aborted_(false),
283 current_find_request_id_(find_request_id_counter_++),
[email protected]e491f1c2009-05-22 20:28:12284 last_search_case_sensitive_(false),
[email protected]e491f1c2009-05-22 20:28:12285 last_search_result_(),
[email protected]98aa0b52010-05-06 17:03:08286 extension_app_(NULL),
[email protected]fdd61c62009-04-22 19:22:57287 capturing_contents_(false),
288 is_being_destroyed_(false),
289 notify_disconnection_(false),
290 history_requests_(),
291#if defined(OS_WIN)
292 message_box_active_(CreateEvent(NULL, TRUE, FALSE, NULL)),
293#endif
294 last_javascript_message_dismissal_(),
[email protected]c2e74fe82009-09-03 17:57:44295 suppress_javascript_messages_(false),
[email protected]7ab1e7d62009-10-14 23:32:01296 is_showing_before_unload_dialog_(false),
[email protected]7ff431e2010-01-07 18:18:54297 renderer_preferences_(),
[email protected]1a242c32010-02-02 21:20:54298 opener_dom_ui_type_(DOMUIFactory::kNoDOMUI),
[email protected]777a7b62010-05-11 13:28:16299 language_state_(&controller_),
[email protected]0cce15f2010-05-21 16:58:39300 closed_by_user_gesture_(false) {
[email protected]7ff431e2010-01-07 18:18:54301 renderer_preferences_util::UpdateFromSystemSettings(
302 &renderer_preferences_, profile);
303
[email protected]9fb83e82010-07-02 18:24:55304 content_settings_delegate_.reset(
305 new TabSpecificContentSettings(this, profile));
306
[email protected]16779842009-07-08 23:45:29307#if defined(OS_CHROMEOS)
[email protected]58dca552009-06-17 00:35:02308 // Make sure the thumbnailer is started before starting the render manager.
309 // The thumbnailer will want to listen for RVH creations, one of which will
310 // happen in RVHManager::Init.
311 ThumbnailGenerator* generator = g_browser_process->GetThumbnailGenerator();
312 if (generator)
313 generator->StartThumbnailing();
314#endif
315
[email protected]12636df2009-09-28 22:32:21316 render_manager_.Init(profile, site_instance, routing_id);
[email protected]420ae012009-04-24 05:16:32317
[email protected]34ac70502009-09-25 17:07:23318 // We have the initial size of the view be based on the size of the passed in
319 // tab contents (normally a tab from the same window).
320 view_->CreateView(base_tab_contents ?
321 base_tab_contents->view()->GetContainerSize() : gfx::Size());
[email protected]420ae012009-04-24 05:16:32322
323 // Register for notifications about all interested prefs change.
324 PrefService* prefs = profile->GetPrefs();
325 if (prefs) {
326 for (int i = 0; i < kPrefsToObserveLength; ++i)
327 prefs->AddPrefObserver(kPrefsToObserve[i], this);
328 }
329
330 // Register for notifications about URL starredness changing on any profile.
331 registrar_.Add(this, NotificationType::URLS_STARRED,
332 NotificationService::AllSources());
333 registrar_.Add(this, NotificationType::BOOKMARK_MODEL_LOADED,
334 NotificationService::AllSources());
335 registrar_.Add(this, NotificationType::RENDER_WIDGET_HOST_DESTROYED,
336 NotificationService::AllSources());
[email protected]d3ba77272009-09-03 00:06:09337#if defined(OS_LINUX)
338 registrar_.Add(this, NotificationType::BROWSER_THEME_CHANGED,
339 NotificationService::AllSources());
340#endif
[email protected]420ae012009-04-24 05:16:32341
[email protected]6c32ce72010-03-08 05:18:06342 registrar_.Add(this, NotificationType::USER_STYLE_SHEET_UPDATED,
343 NotificationService::AllSources());
344
[email protected]79580c62010-02-02 02:36:25345 // Register for notifications about content setting changes.
346 registrar_.Add(this, NotificationType::CONTENT_SETTINGS_CHANGED,
347 NotificationService::AllSources());
348
[email protected]a888b29e62010-04-01 13:38:57349 // Listen for extension changes so we can update extension_for_current_page_.
350 registrar_.Add(this, NotificationType::EXTENSION_LOADED,
351 NotificationService::AllSources());
352 registrar_.Add(this, NotificationType::EXTENSION_UNLOADED,
353 NotificationService::AllSources());
354 registrar_.Add(this, NotificationType::EXTENSION_UNLOADED_DISABLED,
355 NotificationService::AllSources());
356
[email protected]c8865482009-07-23 20:40:10357 // Set-up the showing of the omnibox search infobar if applicable.
358 if (OmniboxSearchHint::IsEnabled(profile))
359 omnibox_search_hint_.reset(new OmniboxSearchHint(this));
[email protected]332af7732009-03-11 13:21:35360}
initial.commit09911bf2008-07-26 23:55:29361
362TabContents::~TabContents() {
[email protected]420ae012009-04-24 05:16:32363 is_being_destroyed_ = true;
364
[email protected]bfe4c1582009-10-02 18:11:09365 // We don't want any notifications while we're running our destructor.
[email protected]420ae012009-04-24 05:16:32366 registrar_.RemoveAll();
367
368 // Unregister the notifications of all observed prefs change.
369 PrefService* prefs = profile()->GetPrefs();
370 if (prefs) {
371 for (int i = 0; i < kPrefsToObserveLength; ++i)
372 prefs->RemovePrefObserver(kPrefsToObserve[i], this);
373 }
374
[email protected]420ae012009-04-24 05:16:32375 NotifyDisconnected();
[email protected]e7b8d832009-11-20 22:41:00376 hung_renderer_dialog::HideForTabContents(this);
[email protected]420ae012009-04-24 05:16:32377
[email protected]420ae012009-04-24 05:16:32378 // First cleanly close all child windows.
379 // TODO(mpcomplete): handle case if MaybeCloseChildWindows() already asked
380 // some of these to close. CloseWindows is async, so it might get called
381 // twice before it runs.
[email protected]8cc8d492010-02-02 10:40:49382 CloseConstrainedWindows();
[email protected]420ae012009-04-24 05:16:32383
[email protected]8cc8d492010-02-02 10:40:49384 // Close all blocked popups.
[email protected]66ba4932009-06-04 19:22:13385 if (blocked_popups_)
386 blocked_popups_->Destroy();
387
[email protected]420ae012009-04-24 05:16:32388 // Notify any observer that have a reference on this tab contents.
389 NotificationService::current()->Notify(
390 NotificationType::TAB_CONTENTS_DESTROYED,
391 Source<TabContents>(this),
392 NotificationService::NoDetails());
393
[email protected]1e5e74f2009-05-27 20:55:12394 // Notify any lasting InfobarDelegates that have not yet been removed that
395 // whatever infobar they were handling in this TabContents has closed,
396 // because the TabContents is going away entirely.
397 // This must happen after the TAB_CONTENTS_DESTROYED notification as the
398 // notification may trigger infobars calls that access their delegate. (and
399 // some implementations of InfoBarDelegate do delete themselves on
400 // InfoBarClosed()).
401 for (int i = 0; i < infobar_delegate_count(); ++i) {
402 InfoBarDelegate* delegate = GetInfoBarDelegateAt(i);
403 delegate->InfoBarClosed();
404 }
405 infobar_delegates_.clear();
406
[email protected]420ae012009-04-24 05:16:32407 // TODO(brettw) this should be moved to the view.
408#if defined(OS_WIN)
409 // If we still have a window handle, destroy it. GetNativeView can return
410 // NULL if this contents was part of a window that closed.
411 if (GetNativeView())
412 ::DestroyWindow(GetNativeView());
413#endif
[email protected]7ab1e7d62009-10-14 23:32:01414
415 // OnCloseStarted isn't called in unit tests.
416 if (!tab_close_start_time_.is_null()) {
417 UMA_HISTOGRAM_TIMES("Tab.Close",
418 base::TimeTicks::Now() - tab_close_start_time_);
419 }
initial.commit09911bf2008-07-26 23:55:29420}
421
[email protected]d5f942ba2008-09-26 19:30:34422// static
423void TabContents::RegisterUserPrefs(PrefService* prefs) {
[email protected]420ae012009-04-24 05:16:32424 prefs->RegisterBooleanPref(prefs::kAlternateErrorPagesEnabled, true);
425
426 WebPreferences pref_defaults;
427 prefs->RegisterBooleanPref(prefs::kWebKitJavascriptEnabled,
428 pref_defaults.javascript_enabled);
429 prefs->RegisterBooleanPref(prefs::kWebKitWebSecurityEnabled,
430 pref_defaults.web_security_enabled);
431 prefs->RegisterBooleanPref(
432 prefs::kWebKitJavascriptCanOpenWindowsAutomatically, true);
433 prefs->RegisterBooleanPref(prefs::kWebKitLoadsImagesAutomatically,
434 pref_defaults.loads_images_automatically);
435 prefs->RegisterBooleanPref(prefs::kWebKitPluginsEnabled,
436 pref_defaults.plugins_enabled);
437 prefs->RegisterBooleanPref(prefs::kWebKitDomPasteEnabled,
438 pref_defaults.dom_paste_enabled);
439 prefs->RegisterBooleanPref(prefs::kWebKitShrinksStandaloneImagesToFit,
440 pref_defaults.shrinks_standalone_images_to_fit);
[email protected]c4e98902010-06-01 10:20:14441 prefs->RegisterDictionaryPref(prefs::kWebKitInspectorSettings);
[email protected]420ae012009-04-24 05:16:32442 prefs->RegisterBooleanPref(prefs::kWebKitTextAreasAreResizable,
443 pref_defaults.text_areas_are_resizable);
444 prefs->RegisterBooleanPref(prefs::kWebKitJavaEnabled,
445 pref_defaults.java_enabled);
[email protected]1661a3c02010-05-20 16:32:36446 prefs->RegisterBooleanPref(prefs::kWebkitTabsToLinks,
447 pref_defaults.tabs_to_links);
[email protected]420ae012009-04-24 05:16:32448
449 prefs->RegisterLocalizedStringPref(prefs::kAcceptLanguages,
450 IDS_ACCEPT_LANGUAGES);
451 prefs->RegisterLocalizedStringPref(prefs::kDefaultCharset,
452 IDS_DEFAULT_ENCODING);
453 prefs->RegisterLocalizedBooleanPref(prefs::kWebKitStandardFontIsSerif,
454 IDS_STANDARD_FONT_IS_SERIF);
455 prefs->RegisterLocalizedStringPref(prefs::kWebKitFixedFontFamily,
456 IDS_FIXED_FONT_FAMILY);
457 prefs->RegisterLocalizedStringPref(prefs::kWebKitSerifFontFamily,
458 IDS_SERIF_FONT_FAMILY);
459 prefs->RegisterLocalizedStringPref(prefs::kWebKitSansSerifFontFamily,
460 IDS_SANS_SERIF_FONT_FAMILY);
461 prefs->RegisterLocalizedStringPref(prefs::kWebKitCursiveFontFamily,
462 IDS_CURSIVE_FONT_FAMILY);
463 prefs->RegisterLocalizedStringPref(prefs::kWebKitFantasyFontFamily,
464 IDS_FANTASY_FONT_FAMILY);
465 prefs->RegisterLocalizedIntegerPref(prefs::kWebKitDefaultFontSize,
466 IDS_DEFAULT_FONT_SIZE);
467 prefs->RegisterLocalizedIntegerPref(prefs::kWebKitDefaultFixedFontSize,
468 IDS_DEFAULT_FIXED_FONT_SIZE);
469 prefs->RegisterLocalizedIntegerPref(prefs::kWebKitMinimumFontSize,
470 IDS_MINIMUM_FONT_SIZE);
471 prefs->RegisterLocalizedIntegerPref(prefs::kWebKitMinimumLogicalFontSize,
472 IDS_MINIMUM_LOGICAL_FONT_SIZE);
473 prefs->RegisterLocalizedBooleanPref(prefs::kWebKitUsesUniversalDetector,
474 IDS_USES_UNIVERSAL_DETECTOR);
475 prefs->RegisterLocalizedStringPref(prefs::kStaticEncodings,
476 IDS_STATIC_ENCODING_LIST);
initial.commit09911bf2008-07-26 23:55:29477}
478
[email protected]13367f72009-07-08 16:39:02479// Returns true if contains content rendered by an extension.
480bool TabContents::HostsExtension() const {
481 return GetURL().SchemeIs(chrome::kExtensionScheme);
[email protected]d5f942ba2008-09-26 19:30:34482}
483
[email protected]cf28ec52010-04-09 22:56:34484AutoFillManager* TabContents::GetAutoFillManager() {
485 if (autofill_manager_.get() == NULL)
486 autofill_manager_.reset(new AutoFillManager(this));
487 return autofill_manager_.get();
488}
489
[email protected]6e95706d2009-04-23 22:55:01490PasswordManager* TabContents::GetPasswordManager() {
491 if (password_manager_.get() == NULL)
492 password_manager_.reset(new PasswordManager(this));
493 return password_manager_.get();
494}
495
496PluginInstaller* TabContents::GetPluginInstaller() {
497 if (plugin_installer_.get() == NULL)
498 plugin_installer_.reset(new PluginInstaller(this));
499 return plugin_installer_.get();
500}
501
[email protected]8cb5d5b2010-02-09 11:36:16502RenderProcessHost* TabContents::GetRenderProcessHost() const {
503 return render_manager_.current_host()->process();
504}
505
[email protected]98aa0b52010-05-06 17:03:08506void TabContents::SetExtensionApp(Extension* extension) {
[email protected]867a73e12010-03-19 20:45:46507 DCHECK(!extension || extension->GetFullLaunchURL().is_valid());
[email protected]98aa0b52010-05-06 17:03:08508 extension_app_ = extension;
[email protected]fca656c2010-02-10 20:30:10509
[email protected]98aa0b52010-05-06 17:03:08510 UpdateExtensionAppIcon(extension_app_);
[email protected]a888b29e62010-04-01 13:38:57511
[email protected]fca656c2010-02-10 20:30:10512 NotificationService::current()->Notify(
513 NotificationType::TAB_CONTENTS_APPLICATION_EXTENSION_CHANGED,
514 Source<TabContents>(this),
515 NotificationService::NoDetails());
[email protected]3b355642010-02-05 16:01:49516}
517
[email protected]98aa0b52010-05-06 17:03:08518void TabContents::SetExtensionAppById(const std::string& extension_app_id) {
519 if (extension_app_id.empty())
[email protected]911f8152010-03-18 16:46:40520 return;
521
522 ExtensionsService* extension_service = profile()->GetExtensionsService();
523 if (extension_service && extension_service->is_ready()) {
524 Extension* extension =
[email protected]98aa0b52010-05-06 17:03:08525 extension_service->GetExtensionById(extension_app_id, false);
[email protected]911f8152010-03-18 16:46:40526 if (extension)
[email protected]98aa0b52010-05-06 17:03:08527 SetExtensionApp(extension);
[email protected]911f8152010-03-18 16:46:40528 }
529}
530
[email protected]98aa0b52010-05-06 17:03:08531SkBitmap* TabContents::GetExtensionAppIcon() {
[email protected]8eb043b2010-04-30 07:19:51532 // We don't show the big icons in tabs for TYPE_EXTENSION_APP windows because
533 // for those windows, we already have a big icon in the top-left outside any
534 // tab. Having big tab icons too looks kinda redonk.
[email protected]06821f42d2010-05-03 22:34:35535 Browser* browser = delegate_ ? delegate_->GetBrowser() : NULL;
536 if (browser && browser->type() == Browser::TYPE_EXTENSION_APP)
[email protected]8eb043b2010-04-30 07:19:51537 return NULL;
[email protected]8eb043b2010-04-30 07:19:51538
[email protected]98aa0b52010-05-06 17:03:08539 if (extension_app_icon_.empty())
[email protected]8eb043b2010-04-30 07:19:51540 return NULL;
541
[email protected]98aa0b52010-05-06 17:03:08542 return &extension_app_icon_;
[email protected]8eb043b2010-04-30 07:19:51543}
544
[email protected]d5f942ba2008-09-26 19:30:34545const GURL& TabContents::GetURL() const {
546 // We may not have a navigation entry yet
[email protected]ce3fa3c2009-04-20 19:55:57547 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]ebe89e062009-08-13 23:16:54548 return entry ? entry->virtual_url() : GURL::EmptyGURL();
[email protected]d5f942ba2008-09-26 19:30:34549}
550
[email protected]96d185d2009-04-24 03:28:54551const string16& TabContents::GetTitle() const {
[email protected]4c6092c5b2009-06-06 00:23:55552 // Transient entries take precedence. They are used for interstitial pages
553 // that are shown on top of existing pages.
554 NavigationEntry* entry = controller_.GetTransientEntry();
555 if (entry)
556 return entry->GetTitleForDisplay(&controller_);
557
[email protected]96d185d2009-04-24 03:28:54558 DOMUI* our_dom_ui = render_manager_.pending_dom_ui() ?
559 render_manager_.pending_dom_ui() : render_manager_.dom_ui();
560 if (our_dom_ui) {
561 // Don't override the title in view source mode.
[email protected]4c6092c5b2009-06-06 00:23:55562 entry = controller_.GetActiveEntry();
[email protected]96d185d2009-04-24 03:28:54563 if (!(entry && entry->IsViewSourceMode())) {
564 // Give the DOM UI the chance to override our title.
565 const string16& title = our_dom_ui->overridden_title();
566 if (!title.empty())
567 return title;
568 }
569 }
570
571 // We use the title for the last committed entry rather than a pending
572 // navigation entry. For example, when the user types in a URL, we want to
573 // keep the old page's title until the new load has committed and we get a new
574 // title.
[email protected]96d185d2009-04-24 03:28:54575 entry = controller_.GetLastCommittedEntry();
576 if (entry)
577 return entry->GetTitleForDisplay(&controller_);
[email protected]96d185d2009-04-24 03:28:54578 return EmptyString16();
579}
580
[email protected]bbbc1ef2010-02-12 18:03:41581// static
582string16 TabContents::GetDefaultTitle() {
583 return l10n_util::GetStringUTF16(IDS_DEFAULT_TAB_TITLE);
584}
585
[email protected]d5f942ba2008-09-26 19:30:34586int32 TabContents::GetMaxPageID() {
587 if (GetSiteInstance())
588 return GetSiteInstance()->max_page_id();
589 else
590 return max_page_id_;
591}
592
593void TabContents::UpdateMaxPageID(int32 page_id) {
594 // Ensure both the SiteInstance and RenderProcessHost update their max page
[email protected]57c6a652009-05-04 07:58:34595 // IDs in sync. Only TabContents will also have site instances, except during
[email protected]d5f942ba2008-09-26 19:30:34596 // testing.
597 if (GetSiteInstance())
598 GetSiteInstance()->UpdateMaxPageID(page_id);
[email protected]8cb5d5b2010-02-09 11:36:16599 GetRenderProcessHost()->UpdateMaxPageID(page_id);
[email protected]d5f942ba2008-09-26 19:30:34600}
601
[email protected]96d185d2009-04-24 03:28:54602SiteInstance* TabContents::GetSiteInstance() const {
603 return render_manager_.current_host()->site_instance();
604}
605
[email protected]96d185d2009-04-24 03:28:54606bool TabContents::ShouldDisplayURL() {
[email protected]8ab24cc2009-11-13 20:56:15607 // Don't hide the url in view source mode and with interstitials.
[email protected]96d185d2009-04-24 03:28:54608 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]8ab24cc2009-11-13 20:56:15609 if (entry && (entry->IsViewSourceMode() ||
610 entry->page_type() == NavigationEntry::INTERSTITIAL_PAGE)) {
[email protected]96d185d2009-04-24 03:28:54611 return true;
[email protected]8ab24cc2009-11-13 20:56:15612 }
[email protected]83e3895b2009-06-11 00:07:16613
[email protected]96d185d2009-04-24 03:28:54614 DOMUI* dom_ui = GetDOMUIForCurrentState();
615 if (dom_ui)
616 return !dom_ui->should_hide_url();
617 return true;
618}
619
[email protected]d5f942ba2008-09-26 19:30:34620SkBitmap TabContents::GetFavIcon() const {
621 // Like GetTitle(), we also want to use the favicon for the last committed
622 // entry rather than a pending navigation entry.
[email protected]ce3fa3c2009-04-20 19:55:57623 NavigationEntry* entry = controller_.GetTransientEntry();
[email protected]cbab76d2008-10-13 22:42:47624 if (entry)
625 return entry->favicon().bitmap();
626
[email protected]ce3fa3c2009-04-20 19:55:57627 entry = controller_.GetLastCommittedEntry();
[email protected]d5f942ba2008-09-26 19:30:34628 if (entry)
629 return entry->favicon().bitmap();
[email protected]d5f942ba2008-09-26 19:30:34630 return SkBitmap();
631}
632
[email protected]0b303cc2009-09-28 22:35:15633bool TabContents::FavIconIsValid() const {
634 NavigationEntry* entry = controller_.GetTransientEntry();
635 if (entry)
636 return entry->favicon().is_valid();
637
638 entry = controller_.GetLastCommittedEntry();
639 if (entry)
640 return entry->favicon().is_valid();
641
642 return false;
643}
644
[email protected]96d185d2009-04-24 03:28:54645bool TabContents::ShouldDisplayFavIcon() {
646 // Always display a throbber during pending loads.
647 if (controller_.GetLastCommittedEntry() && controller_.pending_entry())
648 return true;
649
650 DOMUI* dom_ui = GetDOMUIForCurrentState();
651 if (dom_ui)
652 return !dom_ui->hide_favicon();
653 return true;
654}
655
[email protected]96d185d2009-04-24 03:28:54656std::wstring TabContents::GetStatusText() const {
657 if (!is_loading() || load_state_ == net::LOAD_STATE_IDLE)
658 return std::wstring();
659
660 switch (load_state_) {
661 case net::LOAD_STATE_WAITING_FOR_CACHE:
662 return l10n_util::GetString(IDS_LOAD_STATE_WAITING_FOR_CACHE);
[email protected]8a1f3312010-05-25 19:25:04663 case net::LOAD_STATE_ESTABLISHING_PROXY_TUNNEL:
664 return l10n_util::GetString(IDS_LOAD_STATE_ESTABLISHING_PROXY_TUNNEL);
[email protected]96d185d2009-04-24 03:28:54665 case net::LOAD_STATE_RESOLVING_PROXY_FOR_URL:
666 return l10n_util::GetString(IDS_LOAD_STATE_RESOLVING_PROXY_FOR_URL);
667 case net::LOAD_STATE_RESOLVING_HOST:
668 return l10n_util::GetString(IDS_LOAD_STATE_RESOLVING_HOST);
669 case net::LOAD_STATE_CONNECTING:
670 return l10n_util::GetString(IDS_LOAD_STATE_CONNECTING);
[email protected]375e3112010-05-27 20:32:06671 case net::LOAD_STATE_SSL_HANDSHAKE:
672 return l10n_util::GetString(IDS_LOAD_STATE_SSL_HANDSHAKE);
[email protected]96d185d2009-04-24 03:28:54673 case net::LOAD_STATE_SENDING_REQUEST:
[email protected]094e5b22009-09-25 04:23:56674 if (upload_size_)
675 return l10n_util::GetStringF(
676 IDS_LOAD_STATE_SENDING_REQUEST_WITH_PROGRESS,
677 static_cast<int>((100 * upload_position_) / upload_size_));
678 else
679 return l10n_util::GetString(IDS_LOAD_STATE_SENDING_REQUEST);
[email protected]96d185d2009-04-24 03:28:54680 case net::LOAD_STATE_WAITING_FOR_RESPONSE:
681 return l10n_util::GetStringF(IDS_LOAD_STATE_WAITING_FOR_RESPONSE,
682 load_state_host_);
683 // Ignore net::LOAD_STATE_READING_RESPONSE and net::LOAD_STATE_IDLE
684 case net::LOAD_STATE_IDLE:
685 case net::LOAD_STATE_READING_RESPONSE:
686 break;
687 }
688
689 return std::wstring();
690}
691
[email protected]d5f942ba2008-09-26 19:30:34692void TabContents::SetIsCrashed(bool state) {
693 if (state == is_crashed_)
694 return;
695
696 is_crashed_ = state;
[email protected]c9cd2222009-05-06 05:16:50697 NotifyNavigationStateChanged(INVALIDATE_TAB);
[email protected]d5f942ba2008-09-26 19:30:34698}
699
[email protected]744ef172009-10-16 21:53:46700void TabContents::PageActionStateChanged() {
701 NotifyNavigationStateChanged(TabContents::INVALIDATE_PAGE_ACTIONS);
[email protected]d7eaf572009-07-01 21:57:00702}
[email protected]ce5c4502009-05-06 16:46:11703
[email protected]d5f942ba2008-09-26 19:30:34704void TabContents::NotifyNavigationStateChanged(unsigned changed_flags) {
705 if (delegate_)
706 delegate_->NavigationStateChanged(this, changed_flags);
707}
708
[email protected]96d185d2009-04-24 03:28:54709void TabContents::DidBecomeSelected() {
710 controller_.SetActive(true);
[email protected]8cb5d5b2010-02-09 11:36:16711 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
712 if (rwhv) {
713 rwhv->DidBecomeSelected();
[email protected]789e9152009-08-04 21:59:43714#if defined(OS_MACOSX)
[email protected]8cb5d5b2010-02-09 11:36:16715 rwhv->SetActive(true);
[email protected]789e9152009-08-04 21:59:43716#endif
717 }
[email protected]96d185d2009-04-24 03:28:54718
[email protected]8cb5d5b2010-02-09 11:36:16719 WebCacheManager::GetInstance()->ObserveActivity(GetRenderProcessHost()->id());
[email protected]96d185d2009-04-24 03:28:54720}
721
722void TabContents::WasHidden() {
723 if (!capturing_contents()) {
724 // |render_view_host()| can be NULL if the user middle clicks a link to open
725 // a tab in then background, then closes the tab before selecting it. This
726 // is because closing the tab calls TabContents::Destroy(), which removes
727 // the |render_view_host()|; then when we actually destroy the window,
728 // OnWindowPosChanged() notices and calls HideContents() (which calls us).
[email protected]8cb5d5b2010-02-09 11:36:16729 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
730 if (rwhv)
731 rwhv->WasHidden();
[email protected]96d185d2009-04-24 03:28:54732 }
733
734 NotificationService::current()->Notify(
735 NotificationType::TAB_CONTENTS_HIDDEN,
736 Source<TabContents>(this),
737 NotificationService::NoDetails());
738}
739
[email protected]d5f942ba2008-09-26 19:30:34740void TabContents::Activate() {
741 if (delegate_)
742 delegate_->ActivateContents(this);
743}
744
[email protected]96d185d2009-04-24 03:28:54745void TabContents::ShowContents() {
[email protected]8cb5d5b2010-02-09 11:36:16746 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
747 if (rwhv)
748 rwhv->DidBecomeSelected();
[email protected]96d185d2009-04-24 03:28:54749}
750
751void TabContents::HideContents() {
752 // TODO(pkasting): http://b/1239839 Right now we purposefully don't call
753 // our superclass HideContents(), because some callers want to be very picky
754 // about the order in which these get called. In addition to making the code
755 // here practically impossible to understand, this also means we end up
756 // calling TabContents::WasHidden() twice if callers call both versions of
[email protected]57c6a652009-05-04 07:58:34757 // HideContents() on a TabContents.
[email protected]96d185d2009-04-24 03:28:54758 WasHidden();
759}
760
[email protected]c0588052008-10-27 23:01:50761void TabContents::OpenURL(const GURL& url, const GURL& referrer,
[email protected]d5f942ba2008-09-26 19:30:34762 WindowOpenDisposition disposition,
763 PageTransition::Type transition) {
764 if (delegate_)
[email protected]c0588052008-10-27 23:01:50765 delegate_->OpenURLFromTab(this, url, referrer, disposition, transition);
[email protected]d5f942ba2008-09-26 19:30:34766}
767
[email protected]1ccb3568d2010-02-19 10:51:16768bool TabContents::NavigateToPendingEntry(
769 NavigationController::ReloadType reload_type) {
[email protected]96d185d2009-04-24 03:28:54770 const NavigationEntry& entry = *controller_.pending_entry();
771
772 RenderViewHost* dest_render_view_host = render_manager_.Navigate(entry);
773 if (!dest_render_view_host)
774 return false; // Unable to create the desired render view host.
775
[email protected]770dd8b2010-05-24 18:11:39776 if (delegate_ && delegate_->ShouldEnablePreferredSizeNotifications()) {
777 dest_render_view_host->EnablePreferredSizeChangedMode(
778 kPreferredSizeWidth | kPreferredSizeHeightThisIsSlow);
779 }
[email protected]9fb325e2010-05-06 18:23:24780
[email protected]0352e5472010-04-29 00:11:40781 // For security, we should never send non-DOM-UI URLs (other than about:blank)
782 // to a DOM UI renderer. Double check that here.
[email protected]54ec6472010-04-09 19:39:58783 int enabled_bindings = dest_render_view_host->enabled_bindings();
[email protected]0352e5472010-04-29 00:11:40784 bool is_allowed_in_dom_ui_renderer =
785 DOMUIFactory::UseDOMUIForURL(entry.url()) ||
786 entry.url() == GURL(chrome::kAboutBlankURL);
[email protected]54ec6472010-04-09 19:39:58787 CHECK(!BindingsPolicy::is_dom_ui_enabled(enabled_bindings) ||
[email protected]0352e5472010-04-29 00:11:40788 is_allowed_in_dom_ui_renderer);
[email protected]54ec6472010-04-09 19:39:58789
[email protected]96d185d2009-04-24 03:28:54790 // Tell DevTools agent that it is attached prior to the navigation.
[email protected]d9f9b792009-06-24 13:17:12791 DevToolsManager* devtools_manager = DevToolsManager::GetInstance();
[email protected]7e8e6b62009-05-08 11:28:32792 if (devtools_manager) { // NULL in unit tests.
[email protected]3cc72b12010-03-18 23:03:00793 devtools_manager->OnNavigatingToPendingEntry(render_view_host(),
794 dest_render_view_host,
795 entry.url());
[email protected]7e8e6b62009-05-08 11:28:32796 }
[email protected]96d185d2009-04-24 03:28:54797
798 // Used for page load time metrics.
799 current_load_start_ = base::TimeTicks::Now();
800
801 // Navigate in the desired RenderViewHost.
[email protected]056de2d2009-06-26 16:41:34802 ViewMsg_Navigate_Params navigate_params;
[email protected]3cc72b12010-03-18 23:03:00803 MakeNavigateParams(controller_, reload_type, &navigate_params);
[email protected]056de2d2009-06-26 16:41:34804 dest_render_view_host->Navigate(navigate_params);
[email protected]96d185d2009-04-24 03:28:54805
806 if (entry.page_id() == -1) {
807 // HACK!! This code suppresses javascript: URLs from being added to
808 // session history, which is what we want to do for javascript: URLs that
809 // do not generate content. What we really need is a message from the
810 // renderer telling us that a new page was not created. The same message
811 // could be used for mailto: URLs and the like.
812 if (entry.url().SchemeIs(chrome::kJavaScriptScheme))
813 return false;
814 }
815
816 // Clear any provisional password saves - this stops password infobars
817 // showing up on pages the user navigates to while the right page is
818 // loading.
819 GetPasswordManager()->ClearProvisionalSave();
820
[email protected]1ccb3568d2010-02-19 10:51:16821 if (reload_type != NavigationController::NO_RELOAD &&
822 !profile()->IsOffTheRecord()) {
[email protected]0189bc722009-08-28 21:56:48823 FaviconService* favicon_service =
824 profile()->GetFaviconService(Profile::IMPLICIT_ACCESS);
825 if (favicon_service)
826 favicon_service->SetFaviconOutOfDateForPage(entry.url());
[email protected]96d185d2009-04-24 03:28:54827 }
828
[email protected]d5f942ba2008-09-26 19:30:34829 return true;
830}
831
[email protected]96d185d2009-04-24 03:28:54832void TabContents::Stop() {
833 render_manager_.Stop();
[email protected]12802702010-07-09 19:43:09834 printing_->Stop();
[email protected]96d185d2009-04-24 03:28:54835}
836
[email protected]96d185d2009-04-24 03:28:54837void TabContents::DisassociateFromPopupCount() {
838 render_view_host()->DisassociateFromPopupCount();
839}
840
841TabContents* TabContents::Clone() {
842 // We create a new SiteInstance so that the new tab won't share processes
843 // with the old one. This can be changed in the future if we need it to share
844 // processes for some reason.
[email protected]420ae012009-04-24 05:16:32845 TabContents* tc = new TabContents(profile(),
[email protected]96d185d2009-04-24 03:28:54846 SiteInstance::CreateSiteInstance(profile()),
[email protected]12636df2009-09-28 22:32:21847 MSG_ROUTING_NONE, this);
[email protected]96d185d2009-04-24 03:28:54848 tc->controller().CopyStateFrom(controller_);
[email protected]98aa0b52010-05-06 17:03:08849 tc->extension_app_ = extension_app_;
[email protected]98aa0b52010-05-06 17:03:08850 tc->extension_app_icon_ = extension_app_icon_;
[email protected]96d185d2009-04-24 03:28:54851 return tc;
852}
853
[email protected]4d677202009-07-19 07:37:12854void TabContents::ShowPageInfo(const GURL& url,
855 const NavigationEntry::SSLStatus& ssl,
856 bool show_history) {
857 if (!delegate_)
858 return;
859
[email protected]bb678332009-07-21 00:15:50860 delegate_->ShowPageInfo(profile(), url, ssl, show_history);
[email protected]4d677202009-07-19 07:37:12861}
862
initial.commit09911bf2008-07-26 23:55:29863ConstrainedWindow* TabContents::CreateConstrainedDialog(
[email protected]e8382172009-06-19 22:16:28864 ConstrainedWindowDelegate* delegate) {
initial.commit09911bf2008-07-26 23:55:29865 ConstrainedWindow* window =
[email protected]e8382172009-06-19 22:16:28866 ConstrainedWindow::CreateConstrainedDialog(this, delegate);
initial.commit09911bf2008-07-26 23:55:29867 child_windows_.push_back(window);
[email protected]fa1cf0b82010-01-15 21:49:44868
869 if (child_windows_.size() == 1) {
870 window->ShowConstrainedWindow();
871 BlockTabContent(true);
872 }
873
initial.commit09911bf2008-07-26 23:55:29874 return window;
875}
876
[email protected]fa1cf0b82010-01-15 21:49:44877void TabContents::BlockTabContent(bool blocked) {
[email protected]8dccd7cb2010-02-25 05:19:55878 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
879 if (rwhv)
880 rwhv->SetVisuallyDeemphasized(blocked);
[email protected]fa1cf0b82010-01-15 21:49:44881 render_view_host()->set_ignore_input_events(blocked);
882 if (delegate_)
883 delegate_->SetTabContentBlocked(this, blocked);
884}
885
initial.commit09911bf2008-07-26 23:55:29886void TabContents::AddNewContents(TabContents* new_contents,
887 WindowOpenDisposition disposition,
888 const gfx::Rect& initial_pos,
[email protected]7e7414ae2010-01-26 20:19:29889 bool user_gesture) {
initial.commit09911bf2008-07-26 23:55:29890 if (!delegate_)
891 return;
892
[email protected]35f7d212009-04-29 21:19:27893 if ((disposition == NEW_POPUP) && !user_gesture &&
894 !CommandLine::ForCurrentProcess()->HasSwitch(
895 switches::kDisablePopupBlocking)) {
[email protected]2c4410d2009-05-06 23:46:22896 // Unrequested popups from normal pages are constrained unless they're in
[email protected]b6c874582009-05-08 19:38:31897 // the whitelist. The popup owner will handle checking this.
[email protected]7e7414ae2010-01-26 20:19:29898 delegate_->GetConstrainingContents(this)->AddPopup(
899 new_contents, initial_pos);
[email protected]b6c874582009-05-08 19:38:31900 } else {
[email protected]304f8792009-08-19 21:28:09901#if defined(OS_CHROMEOS)
902 if (disposition == NEW_POPUP) {
903 // If the popup is bigger than a given factor of the screen, then
904 // turn it into a foreground tab (on chrome os only)
[email protected]8323b082010-07-16 16:56:11905 // Also check for width or height == 0, which would otherwise indicate
906 // a tab sized popup window.
[email protected]304f8792009-08-19 21:28:09907 GdkScreen* screen = gdk_screen_get_default();
908 int max_width = gdk_screen_get_width(screen) * kMaxWidthFactor;
909 int max_height = gdk_screen_get_height(screen) * kMaxHeightFactor;
[email protected]8323b082010-07-16 16:56:11910 if (initial_pos.width() > max_width || initial_pos.width() == 0 ||
911 initial_pos.height() > max_height || initial_pos.height() == 0) {
[email protected]304f8792009-08-19 21:28:09912 disposition = NEW_FOREGROUND_TAB;
913 }
914 }
915#endif
916
[email protected]0aa55312008-10-17 21:53:08917 new_contents->DisassociateFromPopupCount();
initial.commit09911bf2008-07-26 23:55:29918 delegate_->AddNewContents(this, new_contents, disposition, initial_pos,
919 user_gesture);
[email protected]7f37dd92009-09-15 16:35:49920 NotificationService::current()->Notify(
921 NotificationType::TAB_ADDED,
922 Source<TabContentsDelegate>(delegate_),
923 Details<TabContents>(this));
initial.commit09911bf2008-07-26 23:55:29924 }
initial.commit09911bf2008-07-26 23:55:29925
[email protected]8cc8d492010-02-02 10:40:49926 // TODO(pkasting): Why is this necessary?
927 PopupNotificationVisibilityChanged(blocked_popups_ != NULL);
initial.commit09911bf2008-07-26 23:55:29928}
929
[email protected]bcd9dc42010-01-23 16:38:18930bool TabContents::ExecuteCode(int request_id, const std::string& extension_id,
[email protected]61f5a7b2009-12-22 22:21:20931 const std::vector<URLPattern>& host_permissions,
[email protected]20ad2692009-11-20 18:27:20932 bool is_js_code, const std::string& code_string,
933 bool all_frames) {
[email protected]ebd0eae72010-02-16 23:50:51934 RenderViewHost* host = render_view_host();
935 if (!host)
936 return false;
937
938 return host->Send(new ViewMsg_ExecuteCode(host->routing_id(),
939 ViewMsg_ExecuteCode_Params(request_id, extension_id, host_permissions,
940 is_js_code, code_string, all_frames)));
[email protected]912256b32009-09-18 09:47:35941}
942
[email protected]96d185d2009-04-24 03:28:54943void TabContents::PopupNotificationVisibilityChanged(bool visible) {
[email protected]8cc8d492010-02-02 10:40:49944 if (is_being_destroyed_)
945 return;
[email protected]9fb83e82010-07-02 18:24:55946 content_settings_delegate_->SetPopupsBlocked(visible);
[email protected]8cc8d492010-02-02 10:40:49947 if (!dont_notify_render_view_)
[email protected]e8345242010-05-06 03:00:40948 render_view_host()->AllowScriptToClose(!visible);
[email protected]96d185d2009-04-24 03:28:54949}
950
[email protected]5c9e97a2009-09-09 23:48:30951gfx::NativeView TabContents::GetContentNativeView() const {
[email protected]96d185d2009-04-24 03:28:54952 return view_->GetContentNativeView();
953}
954
955gfx::NativeView TabContents::GetNativeView() const {
956 return view_->GetNativeView();
957}
958
959void TabContents::GetContainerBounds(gfx::Rect *out) const {
960 view_->GetContainerBounds(out);
961}
962
963void TabContents::Focus() {
964 view_->Focus();
965}
966
[email protected]90daadb42009-06-08 21:27:28967void TabContents::FocusThroughTabTraversal(bool reverse) {
[email protected]7e383692009-06-12 19:14:54968 if (showing_interstitial_page()) {
969 render_manager_.interstitial_page()->FocusThroughTabTraversal(reverse);
970 return;
971 }
[email protected]96d185d2009-04-24 03:28:54972 render_view_host()->SetInitialFocus(reverse);
973}
974
975bool TabContents::FocusLocationBarByDefault() {
976 DOMUI* dom_ui = GetDOMUIForCurrentState();
977 if (dom_ui)
978 return dom_ui->focus_location_bar_by_default();
[email protected]3e3f0eb2009-06-22 18:33:43979 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]e0d481582009-09-15 21:06:25980 if (entry && entry->url() == GURL(chrome::kAboutBlankURL))
[email protected]3e3f0eb2009-06-22 18:33:43981 return true;
[email protected]96d185d2009-04-24 03:28:54982 return false;
983}
984
[email protected]a26dc362010-04-23 01:48:58985void TabContents::SetFocusToLocationBar(bool select_all) {
[email protected]a11aaf22010-03-30 00:03:38986 if (delegate())
[email protected]a26dc362010-04-23 01:48:58987 delegate()->SetFocusToLocationBar(select_all);
[email protected]a11aaf22010-03-30 00:03:38988}
989
[email protected]616ed5a2008-11-21 22:27:24990void TabContents::AddInfoBar(InfoBarDelegate* delegate) {
[email protected]f7019302010-03-26 19:58:32991 if (delegate_ && !delegate_->infobars_enabled()) {
992 delegate->InfoBarClosed();
993 return;
994 }
995
[email protected]616ed5a2008-11-21 22:27:24996 // Look through the existing InfoBarDelegates we have for a match. If we've
997 // already got one that matches, then we don't add the new one.
[email protected]f86a07022008-11-25 01:06:05998 for (int i = 0; i < infobar_delegate_count(); ++i) {
[email protected]6d27a7b2009-12-18 23:25:45999 if (GetInfoBarDelegateAt(i)->EqualsDelegate(delegate)) {
1000 // Tell the new infobar to close so that it can clean itself up.
1001 delegate->InfoBarClosed();
[email protected]616ed5a2008-11-21 22:27:241002 return;
[email protected]6d27a7b2009-12-18 23:25:451003 }
[email protected]616ed5a2008-11-21 22:27:241004 }
1005
1006 infobar_delegates_.push_back(delegate);
[email protected]bfd04a62009-02-01 18:16:561007 NotificationService::current()->Notify(
1008 NotificationType::TAB_CONTENTS_INFOBAR_ADDED,
1009 Source<TabContents>(this),
1010 Details<InfoBarDelegate>(delegate));
[email protected]616ed5a2008-11-21 22:27:241011
1012 // Add ourselves as an observer for navigations the first time a delegate is
1013 // added. We use this notification to expire InfoBars that need to expire on
1014 // page transitions.
1015 if (infobar_delegates_.size() == 1) {
[email protected]bfd04a62009-02-01 18:16:561016 registrar_.Add(this, NotificationType::NAV_ENTRY_COMMITTED,
[email protected]ce3fa3c2009-04-20 19:55:571017 Source<NavigationController>(&controller_));
[email protected]616ed5a2008-11-21 22:27:241018 }
1019}
1020
1021void TabContents::RemoveInfoBar(InfoBarDelegate* delegate) {
[email protected]f7019302010-03-26 19:58:321022 if (delegate_ && !delegate_->infobars_enabled()) {
1023 return;
1024 }
1025
[email protected]616ed5a2008-11-21 22:27:241026 std::vector<InfoBarDelegate*>::iterator it =
1027 find(infobar_delegates_.begin(), infobar_delegates_.end(), delegate);
1028 if (it != infobar_delegates_.end()) {
1029 InfoBarDelegate* delegate = *it;
[email protected]bfd04a62009-02-01 18:16:561030 NotificationService::current()->Notify(
1031 NotificationType::TAB_CONTENTS_INFOBAR_REMOVED,
1032 Source<TabContents>(this),
1033 Details<InfoBarDelegate>(delegate));
[email protected]f86a07022008-11-25 01:06:051034 infobar_delegates_.erase(it);
[email protected]616ed5a2008-11-21 22:27:241035
[email protected]6a02963e2009-01-06 16:58:031036 // Remove ourselves as an observer if we are tracking no more InfoBars.
1037 if (infobar_delegates_.empty()) {
[email protected]bfd04a62009-02-01 18:16:561038 registrar_.Remove(this, NotificationType::NAV_ENTRY_COMMITTED,
[email protected]ce3fa3c2009-04-20 19:55:571039 Source<NavigationController>(&controller_));
[email protected]6a02963e2009-01-06 16:58:031040 }
[email protected]616ed5a2008-11-21 22:27:241041 }
1042}
[email protected]3edd9522009-03-04 22:19:411043
[email protected]f81821c2009-08-07 01:29:471044void TabContents::ReplaceInfoBar(InfoBarDelegate* old_delegate,
1045 InfoBarDelegate* new_delegate) {
[email protected]f7019302010-03-26 19:58:321046 if (delegate_ && !delegate_->infobars_enabled()) {
1047 new_delegate->InfoBarClosed();
1048 return;
1049 }
1050
[email protected]f81821c2009-08-07 01:29:471051 std::vector<InfoBarDelegate*>::iterator it =
1052 find(infobar_delegates_.begin(), infobar_delegates_.end(), old_delegate);
1053 DCHECK(it != infobar_delegates_.end());
1054
1055 // Notify the container about the change of plans.
1056 scoped_ptr<std::pair<InfoBarDelegate*, InfoBarDelegate*> > details(
1057 new std::pair<InfoBarDelegate*, InfoBarDelegate*>(
1058 old_delegate, new_delegate));
1059 NotificationService::current()->Notify(
1060 NotificationType::TAB_CONTENTS_INFOBAR_REPLACED,
1061 Source<TabContents>(this),
1062 Details<std::pair<InfoBarDelegate*, InfoBarDelegate*> >(details.get()));
1063
1064 // Remove the old one.
1065 infobar_delegates_.erase(it);
1066
1067 // Add the new one.
1068 infobar_delegates_.push_back(new_delegate);
1069}
1070
[email protected]20ede072009-10-06 00:28:351071bool TabContents::ShouldShowBookmarkBar() {
[email protected]aa9196f2009-10-22 16:20:271072 if (showing_interstitial_page())
1073 return false;
1074
[email protected]96d185d2009-04-24 03:28:541075 // See GetDOMUIForCurrentState() comment for more info. This case is very
1076 // similar, but for non-first loads, we want to use the committed entry. This
1077 // is so the bookmarks bar disappears at the same time the page does.
1078 if (controller_.GetLastCommittedEntry()) {
1079 // Not the first load, always use the committed DOM UI.
1080 if (render_manager_.dom_ui())
1081 return render_manager_.dom_ui()->force_bookmark_bar_visible();
1082 return false; // Default.
1083 }
1084
1085 // When it's the first load, we know either the pending one or the committed
1086 // one will have the DOM UI in it (see GetDOMUIForCurrentState), and only one
1087 // of them will be valid, so we can just check both.
1088 if (render_manager_.pending_dom_ui())
1089 return render_manager_.pending_dom_ui()->force_bookmark_bar_visible();
1090 if (render_manager_.dom_ui())
1091 return render_manager_.dom_ui()->force_bookmark_bar_visible();
1092 return false; // Default.
1093}
1094
[email protected]d8f16ae2009-08-30 03:47:261095bool TabContents::IsExtensionShelfAlwaysVisible() {
1096 // See GetDOMUIForCurrentState() comment for more info. This case is very
1097 // similar, but for non-first loads, we want to use the committed entry. This
1098 // is so the bookmarks bar disappears at the same time the page does.
1099 if (controller_.GetLastCommittedEntry()) {
1100 // Not the first load, always use the committed DOM UI.
1101 if (render_manager_.dom_ui())
1102 return render_manager_.dom_ui()->force_extension_shelf_visible();
1103 return false; // Default.
1104 }
1105
1106 // When it's the first load, we know either the pending one or the committed
1107 // one will have the DOM UI in it (see GetDOMUIForCurrentState), and only one
1108 // of them will be valid, so we can just check both.
1109 if (render_manager_.pending_dom_ui())
1110 return render_manager_.pending_dom_ui()->force_extension_shelf_visible();
1111 if (render_manager_.dom_ui())
1112 return render_manager_.dom_ui()->force_extension_shelf_visible();
1113 return false; // Default.
1114}
1115
[email protected]3edd9522009-03-04 22:19:411116void TabContents::ToolbarSizeChanged(bool is_animating) {
1117 TabContentsDelegate* d = delegate();
1118 if (d)
1119 d->ToolbarSizeChanged(this, is_animating);
1120}
[email protected]616ed5a2008-11-21 22:27:241121
[email protected]fc6fb7fb2009-11-07 02:35:041122bool TabContents::CanDownload(int request_id) {
1123 TabContentsDelegate* d = delegate();
1124 if (d)
1125 return d->CanDownload(request_id);
1126 return true;
1127}
1128
[email protected]d5f942ba2008-09-26 19:30:341129void TabContents::OnStartDownload(DownloadItem* download) {
1130 DCHECK(download);
[email protected]d5f942ba2008-09-26 19:30:341131
1132 // Download in a constrained popup is shown in the tab that opened it.
[email protected]b6c874582009-05-08 19:38:311133 TabContents* tab_contents = delegate()->GetConstrainingContents(this);
[email protected]d5f942ba2008-09-26 19:30:341134
[email protected]c70f9b82010-04-21 07:31:111135 if (tab_contents && tab_contents->delegate()) {
[email protected]59560e0b2009-06-04 03:30:221136 tab_contents->delegate()->OnStartDownload(download);
[email protected]c70f9b82010-04-21 07:31:111137 // If the download occurs in a new tab, close it
1138 if (controller_.IsInitialNavigation() && (tab_contents == this))
1139 delegate()->CloseContents(this);
1140 }
[email protected]3edd9522009-03-04 22:19:411141}
[email protected]3edd9522009-03-04 22:19:411142
[email protected]d5f942ba2008-09-26 19:30:341143void TabContents::WillClose(ConstrainedWindow* window) {
1144 ConstrainedWindowList::iterator it =
1145 find(child_windows_.begin(), child_windows_.end(), window);
[email protected]fa1cf0b82010-01-15 21:49:441146 bool removed_topmost_window = it == child_windows_.begin();
[email protected]d5f942ba2008-09-26 19:30:341147 if (it != child_windows_.end())
1148 child_windows_.erase(it);
[email protected]55098792010-03-24 18:20:371149 if (child_windows_.size() > 0) {
1150 if (removed_topmost_window) {
1151 child_windows_[0]->ShowConstrainedWindow();
1152 }
[email protected]fa1cf0b82010-01-15 21:49:441153 BlockTabContent(true);
1154 } else {
1155 BlockTabContent(false);
1156 }
[email protected]66ba4932009-06-04 19:22:131157}
[email protected]d5f942ba2008-09-26 19:30:341158
[email protected]66ba4932009-06-04 19:22:131159void TabContents::WillCloseBlockedPopupContainer(
1160 BlockedPopupContainer* container) {
1161 DCHECK(blocked_popups_ == container);
1162 blocked_popups_ = NULL;
[email protected]8cc8d492010-02-02 10:40:491163 PopupNotificationVisibilityChanged(false);
[email protected]d5f942ba2008-09-26 19:30:341164}
1165
[email protected]d5f942ba2008-09-26 19:30:341166void TabContents::DidMoveOrResize(ConstrainedWindow* window) {
[email protected]332af7732009-03-11 13:21:351167#if defined(OS_WIN)
[email protected]92edc472009-02-10 20:32:061168 UpdateWindow(GetNativeView());
[email protected]332af7732009-03-11 13:21:351169#endif
[email protected]d5f942ba2008-09-26 19:30:341170}
1171
[email protected]c1dc9f132009-11-20 23:13:211172void TabContents::StartFinding(string16 search_string,
[email protected]e491f1c2009-05-22 20:28:121173 bool forward_direction,
1174 bool case_sensitive) {
[email protected]c1dc9f132009-11-20 23:13:211175 // If search_string is empty, it means FindNext was pressed with a keyboard
[email protected]6e95706d2009-04-23 22:55:011176 // shortcut so unless we have something to search for we return early.
[email protected]c1dc9f132009-11-20 23:13:211177 if (search_string.empty() && find_text_.empty()) {
[email protected]e5d2f7132010-04-09 18:13:531178 string16 last_search_prepopulate_text =
1179 FindBarState::GetLastPrepopulateText(profile());
1180
[email protected]87fae512009-12-11 02:20:211181 // Try the last thing we searched for on this tab, then the last thing
1182 // searched for on any tab.
1183 if (!previous_find_text_.empty())
1184 search_string = previous_find_text_;
[email protected]e5d2f7132010-04-09 18:13:531185 else if (!last_search_prepopulate_text.empty())
1186 search_string = last_search_prepopulate_text;
[email protected]87fae512009-12-11 02:20:211187 else
[email protected]e491f1c2009-05-22 20:28:121188 return;
[email protected]e491f1c2009-05-22 20:28:121189 }
[email protected]6e95706d2009-04-23 22:55:011190
[email protected]101c90e2009-10-26 21:14:441191 // Keep track of the previous search.
1192 previous_find_text_ = find_text_;
1193
[email protected]6e95706d2009-04-23 22:55:011194 // This is a FindNext operation if we are searching for the same text again,
1195 // or if the passed in search text is empty (FindNext keyboard shortcut). The
1196 // exception to this is if the Find was aborted (then we don't want FindNext
1197 // because the highlighting has been cleared and we need it to reappear). We
1198 // therefore treat FindNext after an aborted Find operation as a full fledged
1199 // Find.
[email protected]c1dc9f132009-11-20 23:13:211200 bool find_next = (find_text_ == search_string || search_string.empty()) &&
[email protected]e491f1c2009-05-22 20:28:121201 (last_search_case_sensitive_ == case_sensitive) &&
[email protected]6e95706d2009-04-23 22:55:011202 !find_op_aborted_;
1203 if (!find_next)
1204 current_find_request_id_ = find_request_id_counter_++;
1205
[email protected]c1dc9f132009-11-20 23:13:211206 if (!search_string.empty())
1207 find_text_ = search_string;
[email protected]e491f1c2009-05-22 20:28:121208 last_search_case_sensitive_ = case_sensitive;
[email protected]6e95706d2009-04-23 22:55:011209
1210 find_op_aborted_ = false;
1211
1212 // Keep track of what the last search was across the tabs.
[email protected]e5d2f7132010-04-09 18:13:531213 FindBarState* find_bar_state = profile()->GetFindBarState();
1214 find_bar_state->set_last_prepopulate_text(find_text_);
[email protected]6e95706d2009-04-23 22:55:011215 render_view_host()->StartFinding(current_find_request_id_,
1216 find_text_,
1217 forward_direction,
[email protected]e491f1c2009-05-22 20:28:121218 case_sensitive,
[email protected]6e95706d2009-04-23 22:55:011219 find_next);
1220}
1221
[email protected]5bc8fe92010-03-11 18:19:001222void TabContents::StopFinding(
1223 FindBarController::SelectionAction selection_action) {
[email protected]d6e8d332010-06-04 22:26:011224 if (selection_action == FindBarController::kClearSelection) {
1225 // kClearSelection means the find string has been cleared by the user, but
1226 // the UI has not been dismissed. In that case we want to clear the
1227 // previously remembered search (http://crbug.com/42639).
1228 previous_find_text_ = string16();
1229 } else {
[email protected]2d2dffc2009-06-09 21:41:001230 find_ui_active_ = false;
[email protected]d6e8d332010-06-04 22:26:011231 if (!find_text_.empty())
1232 previous_find_text_ = find_text_;
1233 }
[email protected]101c90e2009-10-26 21:14:441234 find_text_.clear();
[email protected]6e95706d2009-04-23 22:55:011235 find_op_aborted_ = true;
[email protected]e491f1c2009-05-22 20:28:121236 last_search_result_ = FindNotificationDetails();
[email protected]5bc8fe92010-03-11 18:19:001237 render_view_host()->StopFinding(selection_action);
[email protected]6e95706d2009-04-23 22:55:011238}
1239
[email protected]420ae012009-04-24 05:16:321240void TabContents::OnSavePage() {
1241 // If we can not save the page, try to download it.
1242 if (!SavePackage::IsSavableContents(contents_mime_type())) {
1243 DownloadManager* dlm = profile()->GetDownloadManager();
1244 const GURL& current_page_url = GetURL();
1245 if (dlm && current_page_url.is_valid())
[email protected]d82ed61e2009-06-16 02:46:221246 dlm->DownloadUrl(current_page_url, GURL(), "", this);
[email protected]420ae012009-04-24 05:16:321247 return;
1248 }
1249
1250 Stop();
1251
1252 // Create the save package and possibly prompt the user for the name to save
1253 // the page as. The user prompt is an asynchronous operation that runs on
1254 // another thread.
[email protected]d82ed61e2009-06-16 02:46:221255 save_package_ = new SavePackage(this);
[email protected]420ae012009-04-24 05:16:321256 save_package_->GetSaveInfo();
1257}
1258
1259// Used in automated testing to bypass prompting the user for file names.
1260// Instead, the names and paths are hard coded rather than running them through
1261// file name sanitation and extension / mime checking.
[email protected]828cabe2009-09-26 22:47:111262bool TabContents::SavePage(const FilePath& main_file, const FilePath& dir_path,
[email protected]420ae012009-04-24 05:16:321263 SavePackage::SavePackageType save_type) {
1264 // Stop the page from navigating.
1265 Stop();
1266
[email protected]828cabe2009-09-26 22:47:111267 save_package_ = new SavePackage(this, save_type, main_file, dir_path);
[email protected]cdf6cc82009-08-04 21:48:591268 return save_package_->Init();
[email protected]420ae012009-04-24 05:16:321269}
1270
[email protected]89d6e6e2009-12-03 23:06:511271void TabContents::EmailPageLocation() {
1272 std::string title = EscapeQueryParamValue(UTF16ToUTF8(GetTitle()), false);
1273 std::string page_url = EscapeQueryParamValue(GetURL().spec(), false);
1274 std::string mailto = std::string("mailto:?subject=Fwd:%20") +
1275 title + "&body=%0A%0A" + page_url;
1276 platform_util::OpenExternal(GURL(mailto));
1277}
1278
[email protected]420ae012009-04-24 05:16:321279void TabContents::PrintPreview() {
1280 // We don't show the print preview yet, only the print dialog.
1281 PrintNow();
1282}
1283
1284bool TabContents::PrintNow() {
1285 // We can't print interstitial page for now.
1286 if (showing_interstitial_page())
1287 return false;
1288
1289 return render_view_host()->PrintPages();
1290}
1291
[email protected]82270452009-06-19 15:58:011292void TabContents::PrintingDone(int document_cookie, bool success) {
1293 render_view_host()->PrintingDone(document_cookie, success);
1294}
1295
[email protected]420ae012009-04-24 05:16:321296bool TabContents::IsActiveEntry(int32 page_id) {
1297 NavigationEntry* active_entry = controller_.GetActiveEntry();
1298 return (active_entry != NULL &&
1299 active_entry->site_instance() == GetSiteInstance() &&
1300 active_entry->page_id() == page_id);
1301}
1302
[email protected]b2fe07d12010-02-09 14:38:081303void TabContents::SetOverrideEncoding(const std::string& encoding) {
[email protected]8cb5d5b2010-02-09 11:36:161304 set_encoding(encoding);
1305 render_view_host()->SetPageEncoding(encoding);
1306}
1307
[email protected]b2fe07d12010-02-09 14:38:081308void TabContents::ResetOverrideEncoding() {
[email protected]8cb5d5b2010-02-09 11:36:161309 reset_encoding();
1310 render_view_host()->ResetPageEncodingToDefault();
1311}
1312
1313void TabContents::WindowMoveOrResizeStarted() {
1314 render_view_host()->WindowMoveOrResizeStarted();
1315}
1316
[email protected]3f8eb7f2009-10-02 23:58:051317void TabContents::LogNewTabTime(const std::string& event_name) {
1318 // Not all new tab pages get timed. In those cases, we don't have a
1319 // new_tab_start_time_.
1320 if (new_tab_start_time_.is_null())
1321 return;
1322
1323 base::TimeDelta duration = base::TimeTicks::Now() - new_tab_start_time_;
[email protected]84abba62009-10-07 17:01:441324 MetricEventDurationDetails details(event_name,
1325 static_cast<int>(duration.InMilliseconds()));
1326
[email protected]7ab1e7d62009-10-14 23:32:011327 if (event_name == "Tab.NewTabScriptStart") {
1328 UMA_HISTOGRAM_TIMES("Tab.NewTabScriptStart", duration);
1329 } else if (event_name == "Tab.NewTabDOMContentLoaded") {
1330 UMA_HISTOGRAM_TIMES("Tab.NewTabDOMContentLoaded", duration);
1331 } else if (event_name == "Tab.NewTabOnload") {
1332 UMA_HISTOGRAM_TIMES("Tab.NewTabOnload", duration);
[email protected]3f8eb7f2009-10-02 23:58:051333 // The new tab page has finished loading; reset it.
1334 new_tab_start_time_ = base::TimeTicks();
[email protected]84abba62009-10-07 17:01:441335 } else {
1336 NOTREACHED();
[email protected]3f8eb7f2009-10-02 23:58:051337 }
[email protected]84abba62009-10-07 17:01:441338 NotificationService::current()->Notify(
1339 NotificationType::METRIC_EVENT_DURATION,
1340 Source<TabContents>(this),
1341 Details<MetricEventDurationDetails>(&details));
[email protected]3f8eb7f2009-10-02 23:58:051342}
1343
[email protected]7ab1e7d62009-10-14 23:32:011344void TabContents::OnCloseStarted() {
1345 if (tab_close_start_time_.is_null())
1346 tab_close_start_time_ = base::TimeTicks::Now();
1347}
1348
[email protected]b5558cf22010-07-12 17:30:061349void TabContents::set_request_context(URLRequestContextGetter* context) {
1350 request_context_ = context;
1351}
1352
[email protected]46624bf2010-06-09 16:04:191353bool TabContents::ShouldAcceptDragAndDrop() const {
1354#if defined(OS_CHROMEOS)
1355 // ChromeOS panels (pop-ups) do not take drag-n-drop.
1356 // See http://crosbug.com/2413
1357 return delegate() && !delegate()->IsPopup(const_cast<TabContents*>(this));
1358#else
1359 return true;
1360#endif
1361}
1362
[email protected]c37e3b62010-01-22 00:15:171363TabContents* TabContents::CloneAndMakePhantom() {
[email protected]c37e3b62010-01-22 00:15:171364 TabNavigation tab_nav;
[email protected]df72ac72010-02-06 00:43:161365
1366 NavigationEntry* entry = controller().GetActiveEntry();
[email protected]98aa0b52010-05-06 17:03:081367 if (extension_app_)
1368 tab_nav.set_virtual_url(extension_app_->GetFullLaunchURL());
[email protected]df72ac72010-02-06 00:43:161369 else if (entry)
1370 tab_nav.SetFromNavigationEntry(*entry);
1371
[email protected]c37e3b62010-01-22 00:15:171372 std::vector<TabNavigation> navigations;
1373 navigations.push_back(tab_nav);
1374
1375 TabContents* new_contents =
1376 new TabContents(profile(), NULL, MSG_ROUTING_NONE, NULL);
[email protected]98aa0b52010-05-06 17:03:081377 new_contents->SetExtensionApp(extension_app_);
[email protected]c37e3b62010-01-22 00:15:171378 new_contents->controller().RestoreFromState(navigations, 0, false);
[email protected]c37e3b62010-01-22 00:15:171379
[email protected]98aa0b52010-05-06 17:03:081380 if (!extension_app_ && entry)
[email protected]df72ac72010-02-06 00:43:161381 new_contents->controller().GetActiveEntry()->favicon() = entry->favicon();
1382
[email protected]c37e3b62010-01-22 00:15:171383 return new_contents;
1384}
1385
[email protected]96d185d2009-04-24 03:28:541386// Notifies the RenderWidgetHost instance about the fact that the page is
1387// loading, or done loading and calls the base implementation.
1388void TabContents::SetIsLoading(bool is_loading,
1389 LoadNotificationDetails* details) {
1390 if (is_loading == is_loading_)
1391 return;
1392
1393 if (!is_loading) {
1394 load_state_ = net::LOAD_STATE_IDLE;
1395 load_state_host_.clear();
[email protected]094e5b22009-09-25 04:23:561396 upload_size_ = 0;
1397 upload_position_ = 0;
[email protected]96d185d2009-04-24 03:28:541398 }
1399
1400 render_manager_.SetIsLoading(is_loading);
1401
1402 is_loading_ = is_loading;
1403 waiting_for_response_ = is_loading;
1404
1405 if (delegate_)
1406 delegate_->LoadingStateChanged(this);
[email protected]329581b2009-04-28 06:52:351407 NotifyNavigationStateChanged(INVALIDATE_LOAD);
[email protected]96d185d2009-04-24 03:28:541408
1409 NotificationType type = is_loading ? NotificationType::LOAD_START :
1410 NotificationType::LOAD_STOP;
[email protected]ce5c4502009-05-06 16:46:111411 NotificationDetails det = NotificationService::NoDetails();
[email protected]96d185d2009-04-24 03:28:541412 if (details)
1413 det = Details<LoadNotificationDetails>(details);
1414 NotificationService::current()->Notify(type,
1415 Source<NavigationController>(&controller_),
1416 det);
1417}
1418
[email protected]b6c874582009-05-08 19:38:311419void TabContents::AddPopup(TabContents* new_contents,
[email protected]7e7414ae2010-01-26 20:19:291420 const gfx::Rect& initial_pos) {
[email protected]57eda82a2010-05-05 21:28:061421 // A page can't spawn popups (or do anything else, either) until its load
1422 // commits, so when we reach here, the popup was spawned by the
1423 // NavigationController's last committed entry, not the active entry. For
1424 // example, if a page opens a popup in an onunload() handler, then the active
1425 // entry is the page to be loaded as we navigate away from the unloading
1426 // page. For this reason, we can't use GetURL() to get the opener URL,
1427 // because it returns the active entry.
1428 NavigationEntry* entry = controller_.GetLastCommittedEntry();
1429 GURL creator = entry ? entry->virtual_url() : GURL::EmptyGURL();
1430
1431 if (creator.is_valid() &&
[email protected]8cc8d492010-02-02 10:40:491432 profile()->GetHostContentSettingsMap()->GetContentSetting(
[email protected]57eda82a2010-05-05 21:28:061433 creator, CONTENT_SETTINGS_TYPE_POPUPS) == CONTENT_SETTING_ALLOW) {
[email protected]8cc8d492010-02-02 10:40:491434 AddNewContents(new_contents, NEW_POPUP, initial_pos, true);
1435 } else {
1436 if (!blocked_popups_)
1437 blocked_popups_ = new BlockedPopupContainer(this);
1438 blocked_popups_->AddTabContents(new_contents, initial_pos);
[email protected]9fb83e82010-07-02 18:24:551439 content_settings_delegate_->OnContentBlocked(CONTENT_SETTINGS_TYPE_POPUPS);
[email protected]8cc8d492010-02-02 10:40:491440 }
[email protected]b9681312008-11-07 00:08:261441}
[email protected]616ed5a2008-11-21 22:27:241442
1443namespace {
1444bool TransitionIsReload(PageTransition::Type transition) {
1445 return PageTransition::StripQualifier(transition) == PageTransition::RELOAD;
1446}
1447}
1448
1449void TabContents::ExpireInfoBars(
1450 const NavigationController::LoadCommittedDetails& details) {
1451 // Only hide InfoBars when the user has done something that makes the main
1452 // frame load. We don't want various automatic or subframe navigations making
1453 // it disappear.
1454 if (!details.is_user_initiated_main_frame_load())
1455 return;
1456
[email protected]f86a07022008-11-25 01:06:051457 for (int i = infobar_delegate_count() - 1; i >= 0; --i) {
[email protected]616ed5a2008-11-21 22:27:241458 InfoBarDelegate* delegate = GetInfoBarDelegateAt(i);
[email protected]f86a07022008-11-25 01:06:051459 if (delegate->ShouldExpire(details))
[email protected]616ed5a2008-11-21 22:27:241460 RemoveInfoBar(delegate);
[email protected]616ed5a2008-11-21 22:27:241461 }
1462}
[email protected]fdd61c62009-04-22 19:22:571463
[email protected]96d185d2009-04-24 03:28:541464DOMUI* TabContents::GetDOMUIForCurrentState() {
1465 // When there is a pending navigation entry, we want to use the pending DOMUI
1466 // that goes along with it to control the basic flags. For example, we want to
1467 // show the pending URL in the URL bar, so we want the display_url flag to
1468 // be from the pending entry.
1469 //
1470 // The confusion comes because there are multiple possibilities for the
1471 // initial load in a tab as a side effect of the way the RenderViewHostManager
1472 // works.
1473 //
1474 // - For the very first tab the load looks "normal". The new tab DOM UI is
1475 // the pending one, and we want it to apply here.
1476 //
1477 // - For subsequent new tabs, they'll get a new SiteInstance which will then
1478 // get switched to the one previously associated with the new tab pages.
1479 // This switching will cause the manager to commit the RVH/DOMUI. So we'll
1480 // have a committed DOM UI in this case.
1481 //
1482 // This condition handles all of these cases:
1483 //
1484 // - First load in first tab: no committed nav entry + pending nav entry +
1485 // pending dom ui:
1486 // -> Use pending DOM UI if any.
1487 //
1488 // - First load in second tab: no committed nav entry + pending nav entry +
1489 // no pending DOM UI:
1490 // -> Use the committed DOM UI if any.
1491 //
1492 // - Second navigation in any tab: committed nav entry + pending nav entry:
1493 // -> Use pending DOM UI if any.
1494 //
1495 // - Normal state with no load: committed nav entry + no pending nav entry:
1496 // -> Use committed DOM UI.
1497 if (controller_.pending_entry() &&
1498 (controller_.GetLastCommittedEntry() ||
1499 render_manager_.pending_dom_ui()))
1500 return render_manager_.pending_dom_ui();
1501 return render_manager_.dom_ui();
1502}
[email protected]420ae012009-04-24 05:16:321503
1504void TabContents::DidNavigateMainFramePostCommit(
1505 const NavigationController::LoadCommittedDetails& details,
1506 const ViewHostMsg_FrameNavigate_Params& params) {
[email protected]c2e74fe82009-09-03 17:57:441507 if (opener_dom_ui_type_ != DOMUIFactory::kNoDOMUI) {
1508 // If this is a window.open navigation, use the same DOMUI as the renderer
1509 // that opened the window, as long as both renderers have the same
1510 // privileges.
1511 if (opener_dom_ui_type_ == DOMUIFactory::GetDOMUIType(GetURL())) {
1512 DOMUI* dom_ui = DOMUIFactory::CreateDOMUIForURL(this, GetURL());
1513 // dom_ui might be NULL if the URL refers to a non-existent extension.
1514 if (dom_ui) {
1515 render_manager_.SetDOMUIPostCommit(dom_ui);
1516 dom_ui->RenderViewCreated(render_view_host());
1517 }
1518 }
1519 opener_dom_ui_type_ = DOMUIFactory::kNoDOMUI;
1520 }
1521
[email protected]420ae012009-04-24 05:16:321522 if (details.is_user_initiated_main_frame_load()) {
1523 // Clear the status bubble. This is a workaround for a bug where WebKit
1524 // doesn't let us know that the cursor left an element during a
1525 // transition (this is also why the mouse cursor remains as a hand after
1526 // clicking on a link); see bugs 1184641 and 980803. We don't want to
1527 // clear the bubble when a user navigates to a named anchor in the same
1528 // page.
1529 UpdateTargetURL(details.entry->page_id(), GURL());
1530
1531 // UpdateHelpersForDidNavigate will handle the case where the password_form
1532 // origin is valid.
1533 // TODO(brettw) bug 1343111: Password manager stuff in here needs to be
1534 // cleaned up and covered by tests.
1535 if (!params.password_form.origin.is_valid())
1536 GetPasswordManager()->DidNavigate();
1537 }
1538
1539 // The keyword generator uses the navigation entries, so must be called after
1540 // the commit.
1541 GenerateKeywordIfNecessary(params);
1542
1543 // Allow the new page to set the title again.
1544 received_page_title_ = false;
1545
1546 // Get the favicon, either from history or request it from the net.
1547 fav_icon_helper_.FetchFavIcon(details.entry->url());
1548
[email protected]0de80162010-02-03 04:52:351549 // Clear all page actions, blocked content notifications and browser actions
1550 // for this tab, unless this is an in-page navigation.
[email protected]a6e82fc2010-02-24 22:28:141551 if (!details.is_in_page) {
[email protected]92c6f9b92009-10-24 04:35:081552 ExtensionsService* service = profile()->GetExtensionsService();
1553 if (service) {
1554 for (size_t i = 0; i < service->extensions()->size(); ++i) {
[email protected]5d246db22009-10-27 06:17:571555 ExtensionAction* browser_action =
[email protected]92c6f9b92009-10-24 04:35:081556 service->extensions()->at(i)->browser_action();
[email protected]56ce6e52009-10-27 00:10:521557 if (browser_action) {
1558 browser_action->ClearAllValuesForTab(controller().session_id().id());
1559 NotificationService::current()->Notify(
1560 NotificationType::EXTENSION_BROWSER_ACTION_UPDATED,
[email protected]5d246db22009-10-27 06:17:571561 Source<ExtensionAction>(browser_action),
[email protected]56ce6e52009-10-27 00:10:521562 NotificationService::NoDetails());
1563 }
[email protected]92c6f9b92009-10-24 04:35:081564
[email protected]5d246db22009-10-27 06:17:571565 ExtensionAction* page_action =
[email protected]56ce6e52009-10-27 00:10:521566 service->extensions()->at(i)->page_action();
1567 if (page_action) {
1568 page_action->ClearAllValuesForTab(controller().session_id().id());
1569 PageActionStateChanged();
1570 }
[email protected]92c6f9b92009-10-24 04:35:081571 }
[email protected]aece2c7f2009-08-27 20:43:171572 }
[email protected]0de80162010-02-03 04:52:351573
1574 // Close blocked popups.
1575 if (blocked_popups_) {
[email protected]0fbd70332010-06-01 19:28:341576 AutoReset<bool> auto_reset(&dont_notify_render_view_, true);
[email protected]0de80162010-02-03 04:52:351577 blocked_popups_->Destroy();
[email protected]da7b5262010-02-25 21:12:461578 blocked_popups_ = NULL;
[email protected]0de80162010-02-03 04:52:351579 }
1580
1581 // Clear "blocked" flags.
[email protected]9fb83e82010-07-02 18:24:551582 content_settings_delegate_->ClearBlockedContentSettings();
1583 content_settings_delegate_->GeolocationDidNavigate(details);
[email protected]f17a0ee2010-05-17 17:38:471584
1585 // Once the main frame is navigated, we're no longer considered to have
1586 // displayed insecure content.
1587 displayed_insecure_content_ = false;
[email protected]aece2c7f2009-08-27 20:43:171588 }
[email protected]ce5c4502009-05-06 16:46:111589
[email protected]8cc8d492010-02-02 10:40:491590 // Close constrained windows if necessary.
1591 if (!net::RegistryControlledDomainService::SameDomainOrHost(
1592 details.previous_url, details.entry->url()))
1593 CloseConstrainedWindows();
1594
[email protected]420ae012009-04-24 05:16:321595 // Update the starred state.
1596 UpdateStarredStateForCurrentURL();
[email protected]c07c9752010-03-12 05:57:531597
1598 // Clear the cache of forms in AutoFill.
1599 if (autofill_manager_.get())
1600 autofill_manager_->Reset();
[email protected]420ae012009-04-24 05:16:321601}
1602
1603void TabContents::DidNavigateAnyFramePostCommit(
1604 RenderViewHost* render_view_host,
1605 const NavigationController::LoadCommittedDetails& details,
1606 const ViewHostMsg_FrameNavigate_Params& params) {
1607 // If we navigate, start showing messages again. This does nothing to prevent
1608 // a malicious script from spamming messages, since the script could just
1609 // reload the page to stop blocking.
1610 suppress_javascript_messages_ = false;
1611
[email protected]420ae012009-04-24 05:16:321612 // Notify the password manager of the navigation or form submit.
1613 // TODO(brettw) bug 1343111: Password manager stuff in here needs to be
1614 // cleaned up and covered by tests.
1615 if (params.password_form.origin.is_valid())
1616 GetPasswordManager()->ProvisionallySavePassword(params.password_form);
[email protected]3b1c1812010-02-12 21:53:551617
1618 // Let the LanguageState clear its state.
[email protected]34cec8d82010-03-05 18:27:011619 bool reload = details.entry->transition_type() == PageTransition::RELOAD ||
1620 details.type == NavigationType::SAME_PAGE;
1621 language_state_.DidNavigate(reload, details.is_in_page);
[email protected]420ae012009-04-24 05:16:321622}
1623
[email protected]8cc8d492010-02-02 10:40:491624void TabContents::CloseConstrainedWindows() {
1625 // Clear out any constrained windows since we are leaving this page entirely.
[email protected]420ae012009-04-24 05:16:321626 // We use indices instead of iterators in case CloseWindow does something
1627 // that may invalidate an iterator.
1628 int size = static_cast<int>(child_windows_.size());
1629 for (int i = size - 1; i >= 0; --i) {
1630 ConstrainedWindow* window = child_windows_[i];
[email protected]fa1cf0b82010-01-15 21:49:441631 if (window) {
[email protected]420ae012009-04-24 05:16:321632 window->CloseConstrainedWindow();
[email protected]fa1cf0b82010-01-15 21:49:441633 BlockTabContent(false);
1634 }
[email protected]420ae012009-04-24 05:16:321635 }
1636}
1637
1638void TabContents::UpdateStarredStateForCurrentURL() {
1639 BookmarkModel* model = profile()->GetBookmarkModel();
1640 const bool old_state = is_starred_;
1641 is_starred_ = (model && model->IsBookmarked(GetURL()));
1642
1643 if (is_starred_ != old_state && delegate())
1644 delegate()->URLStarredChanged(this, is_starred_);
1645}
1646
1647void TabContents::UpdateAlternateErrorPageURL() {
1648 GURL url = GetAlternateErrorPageURL();
1649 render_view_host()->SetAlternateErrorPageURL(url);
1650}
1651
1652void TabContents::UpdateWebPreferences() {
1653 render_view_host()->UpdateWebPreferences(GetWebkitPrefs());
1654}
1655
1656void TabContents::UpdateMaxPageIDIfNecessary(SiteInstance* site_instance,
1657 RenderViewHost* rvh) {
1658 // If we are creating a RVH for a restored controller, then we might
1659 // have more page IDs than the SiteInstance's current max page ID. We must
1660 // make sure that the max page ID is larger than any restored page ID.
1661 // Note that it is ok for conflicting page IDs to exist in another tab
1662 // (i.e., NavigationController), but if any page ID is larger than the max,
1663 // the back/forward list will get confused.
1664 int max_restored_page_id = controller_.max_restored_page_id();
1665 if (max_restored_page_id > 0) {
1666 int curr_max_page_id = site_instance->max_page_id();
1667 if (max_restored_page_id > curr_max_page_id) {
1668 // Need to update the site instance immediately.
1669 site_instance->UpdateMaxPageID(max_restored_page_id);
1670
1671 // Also tell the renderer to update its internal representation. We
1672 // need to reserve enough IDs to make all restored page IDs less than
1673 // the max.
1674 if (curr_max_page_id < 0)
1675 curr_max_page_id = 0;
1676 rvh->ReservePageIDRange(max_restored_page_id - curr_max_page_id);
1677 }
1678 }
1679}
1680
1681void TabContents::UpdateHistoryForNavigation(
[email protected]ebe89e062009-08-13 23:16:541682 const GURL& virtual_url,
[email protected]befd8d822009-07-01 04:51:471683 const NavigationController::LoadCommittedDetails& details,
[email protected]420ae012009-04-24 05:16:321684 const ViewHostMsg_FrameNavigate_Params& params) {
1685 if (profile()->IsOffTheRecord())
1686 return;
1687
1688 // Add to history service.
1689 HistoryService* hs = profile()->GetHistoryService(Profile::IMPLICIT_ACCESS);
1690 if (hs) {
1691 if (PageTransition::IsMainFrame(params.transition) &&
[email protected]ebe89e062009-08-13 23:16:541692 virtual_url != params.url) {
1693 // Hack on the "virtual" URL so that it will appear in history. For some
[email protected]420ae012009-04-24 05:16:321694 // types of URLs, we will display a magic URL that is different from where
1695 // the page is actually navigated. We want the user to see in history
[email protected]ebe89e062009-08-13 23:16:541696 // what they saw in the URL bar, so we add the virtual URL as a redirect.
1697 // This only applies to the main frame, as the virtual URL doesn't apply
[email protected]420ae012009-04-24 05:16:321698 // to sub-frames.
1699 std::vector<GURL> redirects = params.redirects;
1700 if (!redirects.empty())
[email protected]ebe89e062009-08-13 23:16:541701 redirects.back() = virtual_url;
1702 hs->AddPage(virtual_url, this, params.page_id, params.referrer,
[email protected]befd8d822009-07-01 04:51:471703 params.transition, redirects, details.did_replace_entry);
[email protected]420ae012009-04-24 05:16:321704 } else {
1705 hs->AddPage(params.url, this, params.page_id, params.referrer,
[email protected]befd8d822009-07-01 04:51:471706 params.transition, params.redirects,
1707 details.did_replace_entry);
[email protected]420ae012009-04-24 05:16:321708 }
1709 }
1710}
1711
1712bool TabContents::UpdateTitleForEntry(NavigationEntry* entry,
1713 const std::wstring& title) {
1714 // For file URLs without a title, use the pathname instead. In the case of a
1715 // synthesized title, we don't want the update to count toward the "one set
1716 // per page of the title to history."
[email protected]e53668962010-06-23 15:35:251717 string16 final_title;
[email protected]420ae012009-04-24 05:16:321718 bool explicit_set;
1719 if (entry->url().SchemeIsFile() && title.empty()) {
[email protected]e53668962010-06-23 15:35:251720 final_title = UTF8ToUTF16(entry->url().ExtractFileName());
[email protected]420ae012009-04-24 05:16:321721 explicit_set = false; // Don't count synthetic titles toward the set limit.
1722 } else {
[email protected]e53668962010-06-23 15:35:251723 TrimWhitespace(WideToUTF16Hack(title), TRIM_ALL, &final_title);
[email protected]420ae012009-04-24 05:16:321724 explicit_set = true;
1725 }
1726
[email protected]e53668962010-06-23 15:35:251727 if (final_title == entry->title())
[email protected]420ae012009-04-24 05:16:321728 return false; // Nothing changed, don't bother.
1729
[email protected]e53668962010-06-23 15:35:251730 entry->set_title(final_title);
[email protected]420ae012009-04-24 05:16:321731
1732 // Update the history system for this page.
1733 if (!profile()->IsOffTheRecord() && !received_page_title_) {
1734 HistoryService* hs =
1735 profile()->GetHistoryService(Profile::IMPLICIT_ACCESS);
1736 if (hs)
[email protected]ebe89e062009-08-13 23:16:541737 hs->SetPageTitle(entry->virtual_url(), final_title);
[email protected]420ae012009-04-24 05:16:321738
1739 // Don't allow the title to be saved again for explicitly set ones.
1740 received_page_title_ = explicit_set;
1741 }
1742
1743 // Lastly, set the title for the view.
[email protected]e53668962010-06-23 15:35:251744 view_->SetPageTitle(UTF16ToWideHack(final_title));
[email protected]420ae012009-04-24 05:16:321745
[email protected]cbc0e1b2010-04-12 18:33:041746 NotificationService::current()->Notify(
1747 NotificationType::TAB_CONTENTS_TITLE_UPDATED,
1748 Source<TabContents>(this),
1749 NotificationService::NoDetails());
1750
[email protected]420ae012009-04-24 05:16:321751 return true;
1752}
1753
1754void TabContents::NotifySwapped() {
1755 // After sending out a swap notification, we need to send a disconnect
1756 // notification so that clients that pick up a pointer to |this| can NULL the
1757 // pointer. See Bug 1230284.
1758 notify_disconnection_ = true;
1759 NotificationService::current()->Notify(
[email protected]57c6a652009-05-04 07:58:341760 NotificationType::TAB_CONTENTS_SWAPPED,
[email protected]d82ed61e2009-06-16 02:46:221761 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:321762 NotificationService::NoDetails());
1763}
1764
1765void TabContents::NotifyConnected() {
1766 notify_disconnection_ = true;
1767 NotificationService::current()->Notify(
[email protected]57c6a652009-05-04 07:58:341768 NotificationType::TAB_CONTENTS_CONNECTED,
[email protected]d82ed61e2009-06-16 02:46:221769 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:321770 NotificationService::NoDetails());
1771}
1772
1773void TabContents::NotifyDisconnected() {
1774 if (!notify_disconnection_)
1775 return;
1776
1777 notify_disconnection_ = false;
1778 NotificationService::current()->Notify(
[email protected]57c6a652009-05-04 07:58:341779 NotificationType::TAB_CONTENTS_DISCONNECTED,
[email protected]d82ed61e2009-06-16 02:46:221780 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:321781 NotificationService::NoDetails());
1782}
1783
1784void TabContents::GenerateKeywordIfNecessary(
1785 const ViewHostMsg_FrameNavigate_Params& params) {
1786 if (!params.searchable_form_url.is_valid())
1787 return;
1788
1789 if (profile()->IsOffTheRecord())
1790 return;
1791
1792 int last_index = controller_.last_committed_entry_index();
1793 // When there was no previous page, the last index will be 0. This is
1794 // normally due to a form submit that opened in a new tab.
1795 // TODO(brettw) bug 916126: we should support keywords when form submits
1796 // happen in new tabs.
1797 if (last_index <= 0)
1798 return;
1799 const NavigationEntry* previous_entry =
1800 controller_.GetEntryAtIndex(last_index - 1);
1801 if (IsFormSubmit(previous_entry)) {
1802 // Only generate a keyword if the previous page wasn't itself a form
1803 // submit.
1804 return;
1805 }
1806
1807 GURL keyword_url = previous_entry->user_typed_url().is_valid() ?
1808 previous_entry->user_typed_url() : previous_entry->url();
1809 std::wstring keyword =
1810 TemplateURLModel::GenerateKeyword(keyword_url, true); // autodetected
1811 if (keyword.empty())
1812 return;
1813
1814 TemplateURLModel* url_model = profile()->GetTemplateURLModel();
1815 if (!url_model)
1816 return;
1817
1818 if (!url_model->loaded()) {
1819 url_model->Load();
1820 return;
1821 }
1822
1823 const TemplateURL* current_url;
[email protected]ddd231e2010-06-29 20:35:191824 GURL url = params.searchable_form_url;
[email protected]420ae012009-04-24 05:16:321825 if (!url_model->CanReplaceKeyword(keyword, url, &current_url))
1826 return;
1827
1828 if (current_url) {
1829 if (current_url->originating_url().is_valid()) {
1830 // The existing keyword was generated from an OpenSearch description
1831 // document, don't regenerate.
1832 return;
1833 }
1834 url_model->Remove(current_url);
1835 }
1836 TemplateURL* new_url = new TemplateURL();
1837 new_url->set_keyword(keyword);
1838 new_url->set_short_name(keyword);
[email protected]ddd231e2010-06-29 20:35:191839 new_url->SetURL(url.spec(), 0, 0);
[email protected]420ae012009-04-24 05:16:321840 new_url->add_input_encoding(params.searchable_form_encoding);
1841 DCHECK(controller_.GetLastCommittedEntry());
1842 const GURL& favicon_url =
1843 controller_.GetLastCommittedEntry()->favicon().url();
1844 if (favicon_url.is_valid()) {
1845 new_url->SetFavIconURL(favicon_url);
1846 } else {
1847 // The favicon url isn't valid. This means there really isn't a favicon,
1848 // or the favicon url wasn't obtained before the load started. This assumes
1849 // the later.
1850 // TODO(sky): Need a way to set the favicon that doesn't involve generating
1851 // its url.
1852 new_url->SetFavIconURL(TemplateURL::GenerateFaviconURL(params.referrer));
1853 }
1854 new_url->set_safe_for_autoreplace(true);
1855 url_model->Add(new_url);
1856}
1857
[email protected]bf5c2ff392009-07-08 16:24:331858void TabContents::OnUserGesture() {
1859 // See comment in RenderViewHostDelegate::OnUserGesture as to why we do this.
[email protected]41291322010-07-15 17:09:011860 DownloadRequestLimiter* limiter =
1861 g_browser_process->download_request_limiter();
1862 if (limiter)
1863 limiter->OnUserGesture(this);
[email protected]e7eaedde2009-09-25 20:09:491864 ExternalProtocolHandler::OnUserGesture();
[email protected]bf5c2ff392009-07-08 16:24:331865 controller_.OnUserGesture();
1866}
1867
1868void TabContents::OnFindReply(int request_id,
1869 int number_of_matches,
1870 const gfx::Rect& selection_rect,
1871 int active_match_ordinal,
1872 bool final_update) {
[email protected]3ce7ced2009-07-24 18:52:421873 // Ignore responses for requests that have been aborted.
1874 if (find_op_aborted_)
1875 return;
1876
[email protected]bf5c2ff392009-07-08 16:24:331877 // Ignore responses for requests other than the one we have most recently
1878 // issued. That way we won't act on stale results when the user has
1879 // already typed in another query.
1880 if (request_id != current_find_request_id_)
1881 return;
1882
1883 if (number_of_matches == -1)
1884 number_of_matches = last_search_result_.number_of_matches();
1885 if (active_match_ordinal == -1)
1886 active_match_ordinal = last_search_result_.active_match_ordinal();
1887
1888 gfx::Rect selection = selection_rect;
1889 if (selection.IsEmpty())
1890 selection = last_search_result_.selection_rect();
1891
1892 // Notify the UI, automation and any other observers that a find result was
1893 // found.
1894 last_search_result_ = FindNotificationDetails(request_id, number_of_matches,
1895 selection, active_match_ordinal,
1896 final_update);
1897 NotificationService::current()->Notify(
1898 NotificationType::FIND_RESULT_AVAILABLE,
1899 Source<TabContents>(this),
1900 Details<FindNotificationDetails>(&last_search_result_));
1901}
1902
1903void TabContents::GoToEntryAtOffset(int offset) {
[email protected]f9cc4c452009-10-13 14:56:381904 if (!delegate_ || delegate_->OnGoToEntryOffset(offset))
1905 controller_.GoToOffset(offset);
[email protected]bf5c2ff392009-07-08 16:24:331906}
1907
[email protected]bf5c2ff392009-07-08 16:24:331908void TabContents::OnMissingPluginStatus(int status) {
1909#if defined(OS_WIN)
1910// TODO(PORT): pull in when plug-ins work
1911 GetPluginInstaller()->OnMissingPluginStatus(status);
1912#endif
1913}
1914
1915void TabContents::OnCrashedPlugin(const FilePath& plugin_path) {
[email protected]bf5c2ff392009-07-08 16:24:331916 DCHECK(!plugin_path.value().empty());
1917
1918 std::wstring plugin_name = plugin_path.ToWStringHack();
[email protected]d32d46182009-07-23 00:09:351919#if defined(OS_WIN) || defined(OS_MACOSX)
[email protected]bf5c2ff392009-07-08 16:24:331920 scoped_ptr<FileVersionInfo> version_info(
1921 FileVersionInfo::CreateFileVersionInfo(plugin_path));
1922 if (version_info.get()) {
1923 const std::wstring& product_name = version_info->product_name();
[email protected]996fd702009-09-04 19:12:371924 if (!product_name.empty()) {
[email protected]bf5c2ff392009-07-08 16:24:331925 plugin_name = product_name;
[email protected]996fd702009-09-04 19:12:371926#if defined(OS_MACOSX)
1927 // Many plugins on the Mac have .plugin in the actual name, which looks
1928 // terrible, so look for that and strip it off if present.
1929 const std::wstring plugin_extension(L".plugin");
1930 if (EndsWith(plugin_name, plugin_extension, true))
1931 plugin_name.erase(plugin_name.length() - plugin_extension.length());
1932#endif // OS_MACOSX
1933 }
[email protected]bf5c2ff392009-07-08 16:24:331934 }
[email protected]4a560ed2009-07-13 23:35:261935#else
1936 NOTIMPLEMENTED() << " convert plugin path to plugin name";
1937#endif
[email protected]2ae8a922010-06-28 20:27:531938 SkBitmap* crash_icon = ResourceBundle::GetSharedInstance().GetBitmapNamed(
1939 IDR_INFOBAR_PLUGIN_CRASHED);
[email protected]bf5c2ff392009-07-08 16:24:331940 AddInfoBar(new SimpleAlertInfoBarDelegate(
1941 this, l10n_util::GetStringF(IDS_PLUGIN_CRASHED_PROMPT, plugin_name),
[email protected]2ae8a922010-06-28 20:27:531942 crash_icon, true));
[email protected]bf5c2ff392009-07-08 16:24:331943}
1944
1945void TabContents::OnCrashedWorker() {
1946 AddInfoBar(new SimpleAlertInfoBarDelegate(
1947 this, l10n_util::GetString(IDS_WEBWORKER_CRASHED_PROMPT),
[email protected]938e1f92010-04-01 18:09:421948 NULL, true));
[email protected]bf5c2ff392009-07-08 16:24:331949}
1950
1951void TabContents::OnDidGetApplicationInfo(
1952 int32 page_id,
1953 const webkit_glue::WebApplicationInfo& info) {
[email protected]ed5431872009-11-17 08:39:511954 web_app_info_ = info;
[email protected]bf5c2ff392009-07-08 16:24:331955
[email protected]ed5431872009-11-17 08:39:511956 if (delegate())
1957 delegate()->OnDidGetApplicationInfo(this, page_id);
[email protected]bf5c2ff392009-07-08 16:24:331958}
1959
[email protected]a2cf65eb2010-01-18 08:20:381960void TabContents::OnPageContents(const GURL& url,
1961 int renderer_process_id,
1962 int32 page_id,
[email protected]e5106202010-06-11 21:12:361963 const string16& contents,
[email protected]1c57b7b2010-07-12 17:38:101964 const std::string& language,
1965 bool page_translatable) {
[email protected]a2cf65eb2010-01-18 08:20:381966 // Don't index any https pages. People generally don't want their bank
1967 // accounts, etc. indexed on their computer, especially since some of these
1968 // things are not marked cachable.
1969 // TODO(brettw) we may want to consider more elaborate heuristics such as
1970 // the cachability of the page. We may also want to consider subframes (this
1971 // test will still index subframes if the subframe is SSL).
[email protected]ccd40a52010-05-11 16:15:271972 // TODO(zelidrag) bug chromium-os:2808 - figure out if we want to reenable
1973 // content indexing for chromeos in some future releases.
1974#if !defined(OS_CHROMEOS)
[email protected]a2cf65eb2010-01-18 08:20:381975 if (!url.SchemeIsSecure()) {
1976 Profile* p = profile();
1977 if (p && !p->IsOffTheRecord()) {
1978 HistoryService* hs = p->GetHistoryService(Profile::IMPLICIT_ACCESS);
1979 if (hs)
1980 hs->SetPageContents(url, contents);
1981 }
1982 }
[email protected]ccd40a52010-05-11 16:15:271983#endif
[email protected]a8a81292010-01-21 00:32:451984
[email protected]1c57b7b2010-07-12 17:38:101985 language_state_.LanguageDetermined(language, page_translatable);
[email protected]a8a81292010-01-21 00:32:451986
1987 std::string lang = language;
1988 NotificationService::current()->Notify(
1989 NotificationType::TAB_LANGUAGE_DETERMINED,
[email protected]7e0831b2010-02-01 08:45:151990 Source<TabContents>(this),
[email protected]a8a81292010-01-21 00:32:451991 Details<std::string>(&lang));
[email protected]a2cf65eb2010-01-18 08:20:381992}
1993
[email protected]7e0831b2010-02-01 08:45:151994void TabContents::OnPageTranslated(int32 page_id,
1995 const std::string& original_lang,
[email protected]152b8ba32010-03-29 19:40:161996 const std::string& translated_lang,
1997 TranslateErrors::Type error_type) {
[email protected]3b1c1812010-02-12 21:53:551998 language_state_.set_current_language(translated_lang);
1999 language_state_.set_translation_pending(false);
[email protected]152b8ba32010-03-29 19:40:162000 PageTranslatedDetails details(original_lang, translated_lang, error_type);
[email protected]7e0831b2010-02-01 08:45:152001 NotificationService::current()->Notify(
2002 NotificationType::PAGE_TRANSLATED,
2003 Source<TabContents>(this),
[email protected]152b8ba32010-03-29 19:40:162004 Details<PageTranslatedDetails>(&details));
[email protected]7e0831b2010-02-01 08:45:152005}
2006
[email protected]bf5c2ff392009-07-08 16:24:332007void TabContents::DidStartProvisionalLoadForFrame(
2008 RenderViewHost* render_view_host,
2009 bool is_main_frame,
2010 const GURL& url) {
2011 ProvisionalLoadDetails details(is_main_frame,
2012 controller_.IsURLInPageNavigation(url),
2013 url, std::string(), false);
2014 NotificationService::current()->Notify(
2015 NotificationType::FRAME_PROVISIONAL_LOAD_START,
2016 Source<NavigationController>(&controller_),
2017 Details<ProvisionalLoadDetails>(&details));
2018}
2019
2020void TabContents::DidStartReceivingResourceResponse(
[email protected]3dbb80b2010-02-09 22:41:202021 const ResourceRequestDetails& details) {
[email protected]bf5c2ff392009-07-08 16:24:332022 NotificationService::current()->Notify(
2023 NotificationType::RESOURCE_RESPONSE_STARTED,
2024 Source<NavigationController>(&controller()),
[email protected]3dbb80b2010-02-09 22:41:202025 Details<const ResourceRequestDetails>(&details));
[email protected]bf5c2ff392009-07-08 16:24:332026}
2027
[email protected]3dbb80b2010-02-09 22:41:202028void TabContents::DidRedirectResource(
2029 const ResourceRedirectDetails& details) {
[email protected]bf5c2ff392009-07-08 16:24:332030 NotificationService::current()->Notify(
2031 NotificationType::RESOURCE_RECEIVED_REDIRECT,
2032 Source<NavigationController>(&controller()),
[email protected]3dbb80b2010-02-09 22:41:202033 Details<const ResourceRequestDetails>(&details));
[email protected]bf5c2ff392009-07-08 16:24:332034}
2035
2036void TabContents::DidLoadResourceFromMemoryCache(
2037 const GURL& url,
2038 const std::string& frame_origin,
2039 const std::string& main_frame_origin,
2040 const std::string& security_info) {
2041 // Send out a notification that we loaded a resource from our memory cache.
[email protected]fc7de492010-07-12 14:49:042042 int cert_id = 0, cert_status = 0, security_bits = 0, connection_status = 0;
[email protected]bf5c2ff392009-07-08 16:24:332043 SSLManager::DeserializeSecurityInfo(security_info,
2044 &cert_id, &cert_status,
[email protected]fc7de492010-07-12 14:49:042045 &security_bits,
2046 &connection_status);
[email protected]bf5c2ff392009-07-08 16:24:332047 LoadFromMemoryCacheDetails details(url, frame_origin, main_frame_origin,
[email protected]8cb5d5b2010-02-09 11:36:162048 GetRenderProcessHost()->id(), cert_id,
2049 cert_status);
[email protected]bf5c2ff392009-07-08 16:24:332050
2051 NotificationService::current()->Notify(
2052 NotificationType::LOAD_FROM_MEMORY_CACHE,
2053 Source<NavigationController>(&controller_),
2054 Details<LoadFromMemoryCacheDetails>(&details));
2055}
2056
[email protected]e3d60e5d2009-09-25 21:08:292057void TabContents::DidDisplayInsecureContent() {
[email protected]f17a0ee2010-05-17 17:38:472058 displayed_insecure_content_ = true;
2059 SSLManager::NotifySSLInternalStateChanged();
[email protected]e3d60e5d2009-09-25 21:08:292060}
2061
2062void TabContents::DidRunInsecureContent(const std::string& security_origin) {
[email protected]09843b5f2009-09-26 08:30:592063 controller_.ssl_manager()->DidRunInsecureContent(security_origin);
[email protected]e3d60e5d2009-09-25 21:08:292064}
2065
[email protected]bf5c2ff392009-07-08 16:24:332066void TabContents::DidFailProvisionalLoadWithError(
2067 RenderViewHost* render_view_host,
2068 bool is_main_frame,
2069 int error_code,
2070 const GURL& url,
2071 bool showing_repost_interstitial) {
2072 if (net::ERR_ABORTED == error_code) {
2073 // EVIL HACK ALERT! Ignore failed loads when we're showing interstitials.
2074 // This means that the interstitial won't be torn down properly, which is
2075 // bad. But if we have an interstitial, go back to another tab type, and
2076 // then load the same interstitial again, we could end up getting the first
2077 // interstitial's "failed" message (as a result of the cancel) when we're on
2078 // the second one.
2079 //
2080 // We can't tell this apart, so we think we're tearing down the current page
2081 // which will cause a crash later one. There is also some code in
2082 // RenderViewHostManager::RendererAbortedProvisionalLoad that is commented
2083 // out because of this problem.
2084 //
2085 // http://code.google.com/p/chromium/issues/detail?id=2855
2086 // Because this will not tear down the interstitial properly, if "back" is
2087 // back to another tab type, the interstitial will still be somewhat alive
2088 // in the previous tab type. If you navigate somewhere that activates the
2089 // tab with the interstitial again, you'll see a flash before the new load
2090 // commits of the interstitial page.
2091 if (showing_interstitial_page()) {
2092 LOG(WARNING) << "Discarding message during interstitial.";
2093 return;
2094 }
2095
2096 // This will discard our pending entry if we cancelled the load (e.g., if we
2097 // decided to download the file instead of load it). Only discard the
2098 // pending entry if the URLs match, otherwise the user initiated a navigate
2099 // before the page loaded so that the discard would discard the wrong entry.
2100 NavigationEntry* pending_entry = controller_.pending_entry();
2101 if (pending_entry && pending_entry->url() == url) {
2102 controller_.DiscardNonCommittedEntries();
2103 // Update the URL display.
2104 NotifyNavigationStateChanged(TabContents::INVALIDATE_URL);
2105 }
2106
2107 render_manager_.RendererAbortedProvisionalLoad(render_view_host);
2108 }
2109
2110 // Send out a notification that we failed a provisional load with an error.
2111 ProvisionalLoadDetails details(is_main_frame,
2112 controller_.IsURLInPageNavigation(url),
2113 url, std::string(), false);
2114 details.set_error_code(error_code);
2115
2116 NotificationService::current()->Notify(
2117 NotificationType::FAIL_PROVISIONAL_LOAD_WITH_ERROR,
2118 Source<NavigationController>(&controller_),
2119 Details<ProvisionalLoadDetails>(&details));
2120}
2121
2122void TabContents::DocumentLoadedInFrame() {
2123 controller_.DocumentLoadedInFrame();
2124}
2125
[email protected]9fb83e82010-07-02 18:24:552126void TabContents::OnContentSettingsChange() {
[email protected]614c0e12010-03-25 10:12:562127 if (delegate_)
[email protected]20ad6a92010-04-21 14:07:242128 delegate_->OnContentSettingsChange(this);
[email protected]614c0e12010-03-25 10:12:562129}
2130
[email protected]8d3347f2009-07-09 22:00:212131RenderViewHostDelegate::View* TabContents::GetViewDelegate() {
[email protected]420ae012009-04-24 05:16:322132 return view_.get();
2133}
2134
[email protected]8d3347f2009-07-09 22:00:212135RenderViewHostDelegate::RendererManagement*
2136TabContents::GetRendererManagementDelegate() {
2137 return &render_manager_;
2138}
2139
[email protected]bf5c2ff392009-07-08 16:24:332140RenderViewHostDelegate::BrowserIntegration*
[email protected]8d3347f2009-07-09 22:00:212141 TabContents::GetBrowserIntegrationDelegate() {
2142 return this;
[email protected]bf5c2ff392009-07-08 16:24:332143}
2144
[email protected]8d3347f2009-07-09 22:00:212145RenderViewHostDelegate::Resource* TabContents::GetResourceDelegate() {
2146 return this;
[email protected]bf5c2ff392009-07-08 16:24:332147}
2148
[email protected]46ba9082010-06-22 10:00:582149RenderViewHostDelegate::ContentSettings*
2150TabContents::GetContentSettingsDelegate() {
[email protected]9fb83e82010-07-02 18:24:552151 return content_settings_delegate_.get();
[email protected]46ba9082010-06-22 10:00:582152}
2153
[email protected]8d3347f2009-07-09 22:00:212154RenderViewHostDelegate::Save* TabContents::GetSaveDelegate() {
[email protected]420ae012009-04-24 05:16:322155 return save_package_.get(); // May be NULL, but we can return NULL.
2156}
2157
[email protected]8d3347f2009-07-09 22:00:212158RenderViewHostDelegate::Printing* TabContents::GetPrintingDelegate() {
[email protected]12802702010-07-09 19:43:092159 return printing_.get();
[email protected]8d3347f2009-07-09 22:00:212160}
2161
2162RenderViewHostDelegate::FavIcon* TabContents::GetFavIconDelegate() {
2163 return &fav_icon_helper_;
2164}
2165
[email protected]e8dbd674b2010-03-11 18:34:402166RenderViewHostDelegate::Autocomplete* TabContents::GetAutocompleteDelegate() {
[email protected]6db0def92010-03-02 19:55:452167 if (autocomplete_history_manager_.get() == NULL)
2168 autocomplete_history_manager_.reset(new AutocompleteHistoryManager(this));
2169 return autocomplete_history_manager_.get();
[email protected]bf5c2ff392009-07-08 16:24:332170}
2171
[email protected]21674df2009-10-28 19:51:212172RenderViewHostDelegate::AutoFill* TabContents::GetAutoFillDelegate() {
[email protected]cf28ec52010-04-09 22:56:342173 return GetAutoFillManager();
[email protected]21674df2009-10-28 19:51:212174}
2175
[email protected]fd571bb2010-03-19 01:23:542176AutomationResourceRoutingDelegate*
2177TabContents::GetAutomationResourceRoutingDelegate() {
2178 return delegate();
2179}
2180
[email protected]ced90ae12010-02-20 02:06:162181RenderViewHostDelegate::BookmarkDrag* TabContents::GetBookmarkDragDelegate() {
2182 return bookmark_drag_;
2183}
2184
2185void TabContents::SetBookmarkDragDelegate(
2186 RenderViewHostDelegate::BookmarkDrag* bookmark_drag) {
2187 bookmark_drag_ = bookmark_drag;
2188}
2189
[email protected]9fb83e82010-07-02 18:24:552190TabSpecificContentSettings* TabContents::GetTabSpecificContentSettings() const {
2191 return content_settings_delegate_.get();
2192}
2193
[email protected]93623c5d2009-12-10 21:40:322194RendererPreferences TabContents::GetRendererPrefs(Profile* profile) const {
[email protected]840b1512009-07-21 16:53:462195 return renderer_preferences_;
[email protected]80d96fa2009-06-10 22:34:512196}
2197
[email protected]57c6a652009-05-04 07:58:342198TabContents* TabContents::GetAsTabContents() {
2199 return this;
2200}
2201
[email protected]7b291f92009-08-14 05:43:532202ViewType::Type TabContents::GetRenderViewType() const {
2203 return ViewType::TAB_CONTENTS;
2204}
2205
2206int TabContents::GetBrowserWindowID() const {
2207 return controller().window_id().id();
2208}
2209
[email protected]420ae012009-04-24 05:16:322210void TabContents::RenderViewCreated(RenderViewHost* render_view_host) {
[email protected]2e4633c2009-07-09 16:58:062211 NotificationService::current()->Notify(
2212 NotificationType::RENDER_VIEW_HOST_CREATED_FOR_TAB,
2213 Source<TabContents>(this),
2214 Details<RenderViewHost>(render_view_host));
[email protected]420ae012009-04-24 05:16:322215 NavigationEntry* entry = controller_.GetActiveEntry();
2216 if (!entry)
2217 return;
2218
2219 // When we're creating views, we're still doing initial setup, so we always
2220 // use the pending DOM UI rather than any possibly existing committed one.
2221 if (render_manager_.pending_dom_ui()) {
2222 render_manager_.pending_dom_ui()->RenderViewCreated(render_view_host);
2223 }
2224
2225 if (entry->IsViewSourceMode()) {
2226 // Put the renderer in view source mode.
2227 render_view_host->Send(
2228 new ViewMsg_EnableViewSourceMode(render_view_host->routing_id()));
2229 }
[email protected]0666aef2009-05-13 19:48:082230
2231 view()->RenderViewCreated(render_view_host);
[email protected]420ae012009-04-24 05:16:322232}
2233
2234void TabContents::RenderViewReady(RenderViewHost* rvh) {
2235 if (rvh != render_view_host()) {
2236 // Don't notify the world, since this came from a renderer in the
2237 // background.
2238 return;
2239 }
2240
2241 NotifyConnected();
[email protected]9d8a4642009-07-29 17:25:302242 bool was_crashed = is_crashed();
[email protected]420ae012009-04-24 05:16:322243 SetIsCrashed(false);
[email protected]9d8a4642009-07-29 17:25:302244
2245 // Restore the focus to the tab (otherwise the focus will be on the top
2246 // window).
2247 if (was_crashed && !FocusLocationBarByDefault())
2248 Focus();
[email protected]420ae012009-04-24 05:16:322249}
2250
2251void TabContents::RenderViewGone(RenderViewHost* rvh) {
2252 // Ask the print preview if this renderer was valuable.
[email protected]12802702010-07-09 19:43:092253 if (!printing_->OnRenderViewGone(rvh))
[email protected]420ae012009-04-24 05:16:322254 return;
2255 if (rvh != render_view_host()) {
2256 // The pending page's RenderViewHost is gone.
2257 return;
2258 }
2259
2260 SetIsLoading(false, NULL);
2261 NotifyDisconnected();
2262 SetIsCrashed(true);
2263
[email protected]ff9efc62010-02-18 22:10:542264 // Remove all infobars.
[email protected]e5430c92010-02-18 22:39:212265 for (int i = infobar_delegate_count() - 1; i >=0 ; --i)
[email protected]ff9efc62010-02-18 22:10:542266 RemoveInfoBar(GetInfoBarDelegateAt(i));
2267
[email protected]8e537302009-06-10 21:19:582268 // Tell the view that we've crashed so it can prepare the sad tab page.
[email protected]cf68b092010-01-15 20:05:552269 // Only do this if we're not in browser shutdown, so that TabContents
2270 // objects that are not in a browser (e.g., HTML dialogs) and thus are
2271 // visible do not flash a sad tab page.
2272 if (browser_shutdown::GetShutdownType() == browser_shutdown::NOT_VALID)
2273 view_->OnTabCrashed();
[email protected]420ae012009-04-24 05:16:322274
2275 // Hide any visible hung renderer warning for this web contents' process.
[email protected]e7b8d832009-11-20 22:41:002276 hung_renderer_dialog::HideForTabContents(this);
[email protected]420ae012009-04-24 05:16:322277}
2278
[email protected]2e4633c2009-07-09 16:58:062279void TabContents::RenderViewDeleted(RenderViewHost* rvh) {
2280 NotificationService::current()->Notify(
2281 NotificationType::RENDER_VIEW_HOST_DELETED,
2282 Source<TabContents>(this),
2283 Details<RenderViewHost>(rvh));
[email protected]fb7b79f72009-11-06 18:00:482284 render_manager_.RenderViewDeleted(rvh);
[email protected]2e4633c2009-07-09 16:58:062285}
2286
[email protected]420ae012009-04-24 05:16:322287void TabContents::DidNavigate(RenderViewHost* rvh,
2288 const ViewHostMsg_FrameNavigate_Params& params) {
[email protected]8030f012009-09-25 18:09:372289 int extra_invalidate_flags = 0;
2290
2291 if (PageTransition::IsMainFrame(params.transition)) {
[email protected]20ede072009-10-06 00:28:352292 bool was_bookmark_bar_visible = ShouldShowBookmarkBar();
[email protected]8030f012009-09-25 18:09:372293 bool was_extension_shelf_visible = IsExtensionShelfAlwaysVisible();
2294
[email protected]420ae012009-04-24 05:16:322295 render_manager_.DidNavigateMainFrame(rvh);
2296
[email protected]20ede072009-10-06 00:28:352297 if (was_bookmark_bar_visible != ShouldShowBookmarkBar())
[email protected]8030f012009-09-25 18:09:372298 extra_invalidate_flags |= INVALIDATE_BOOKMARK_BAR;
2299 if (was_extension_shelf_visible != IsExtensionShelfAlwaysVisible())
2300 extra_invalidate_flags |= INVALIDATE_EXTENSION_SHELF;
2301 }
2302
[email protected]420ae012009-04-24 05:16:322303 // Update the site of the SiteInstance if it doesn't have one yet.
2304 if (!GetSiteInstance()->has_site())
2305 GetSiteInstance()->SetSite(params.url);
2306
2307 // Need to update MIME type here because it's referred to in
2308 // UpdateNavigationCommands() called by RendererDidNavigate() to
2309 // determine whether or not to enable the encoding menu.
2310 // It's updated only for the main frame. For a subframe,
2311 // RenderView::UpdateURL does not set params.contents_mime_type.
2312 // (see http://code.google.com/p/chromium/issues/detail?id=2929 )
2313 // TODO(jungshik): Add a test for the encoding menu to avoid
2314 // regressing it again.
2315 if (PageTransition::IsMainFrame(params.transition))
2316 contents_mime_type_ = params.contents_mime_type;
2317
2318 NavigationController::LoadCommittedDetails details;
[email protected]8030f012009-09-25 18:09:372319 bool did_navigate = controller_.RendererDidNavigate(
2320 params, extra_invalidate_flags, &details);
[email protected]3e90d4a2009-07-03 17:38:392321
2322 // Update history. Note that this needs to happen after the entry is complete,
2323 // which WillNavigate[Main,Sub]Frame will do before this function is called.
[email protected]2e29d922009-10-22 22:13:352324 if (params.should_update_history &&
2325 (!delegate() || delegate()->ShouldAddNavigationsToHistory())) {
[email protected]3e90d4a2009-07-03 17:38:392326 // Most of the time, the displayURL matches the loaded URL, but for about:
2327 // URLs, we use a data: URL as the real value. We actually want to save
2328 // the about: URL to the history db and keep the data: URL hidden. This is
2329 // what the TabContents' URL getter does.
2330 UpdateHistoryForNavigation(GetURL(), details, params);
2331 }
2332
[email protected]76543b92009-08-31 17:27:452333 if (!did_navigate)
2334 return; // No navigation happened.
[email protected]420ae012009-04-24 05:16:322335
2336 // DO NOT ADD MORE STUFF TO THIS FUNCTION! Your component should either listen
2337 // for the appropriate notification (best) or you can add it to
2338 // DidNavigateMainFramePostCommit / DidNavigateAnyFramePostCommit (only if
2339 // necessary, please).
2340
2341 // Run post-commit tasks.
2342 if (details.is_main_frame)
2343 DidNavigateMainFramePostCommit(details, params);
2344 DidNavigateAnyFramePostCommit(rvh, details, params);
2345}
2346
2347void TabContents::UpdateState(RenderViewHost* rvh,
2348 int32 page_id,
2349 const std::string& state) {
2350 DCHECK(rvh == render_view_host());
2351
2352 // We must be prepared to handle state updates for any page, these occur
2353 // when the user is scrolling and entering form data, as well as when we're
2354 // leaving a page, in which case our state may have already been moved to
2355 // the next page. The navigation controller will look up the appropriate
2356 // NavigationEntry and update it when it is notified via the delegate.
2357
2358 int entry_index = controller_.GetEntryIndexWithPageID(
2359 GetSiteInstance(), page_id);
2360 if (entry_index < 0)
2361 return;
2362 NavigationEntry* entry = controller_.GetEntryAtIndex(entry_index);
2363
2364 if (state == entry->content_state())
2365 return; // Nothing to update.
2366 entry->set_content_state(state);
2367 controller_.NotifyEntryChanged(entry, entry_index);
2368}
2369
2370void TabContents::UpdateTitle(RenderViewHost* rvh,
2371 int32 page_id, const std::wstring& title) {
2372 // If we have a title, that's a pretty good indication that we've started
2373 // getting useful data.
2374 SetNotWaitingForResponse();
2375
2376 DCHECK(rvh == render_view_host());
2377 NavigationEntry* entry = controller_.GetEntryWithPageID(GetSiteInstance(),
2378 page_id);
2379 if (!entry || !UpdateTitleForEntry(entry, title))
2380 return;
2381
2382 // Broadcast notifications when the UI should be updated.
2383 if (entry == controller_.GetEntryAtOffset(0))
[email protected]f1cd5e82009-10-23 17:19:032384 NotifyNavigationStateChanged(INVALIDATE_TITLE);
[email protected]420ae012009-04-24 05:16:322385}
2386
[email protected]420ae012009-04-24 05:16:322387void TabContents::UpdateEncoding(RenderViewHost* render_view_host,
[email protected]41fc0322009-09-04 22:23:402388 const std::string& encoding) {
[email protected]420ae012009-04-24 05:16:322389 set_encoding(encoding);
2390}
2391
2392void TabContents::UpdateTargetURL(int32 page_id, const GURL& url) {
2393 if (delegate())
2394 delegate()->UpdateTargetURL(this, url);
2395}
2396
2397void TabContents::UpdateThumbnail(const GURL& url,
2398 const SkBitmap& bitmap,
2399 const ThumbnailScore& score) {
2400 // Tell History about this thumbnail
[email protected]523623c2010-05-19 16:03:422401 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kTopSites)) {
[email protected]72d0fc72009-07-09 17:30:492402 if (!profile()->IsOffTheRecord())
[email protected]523623c2010-05-19 16:03:422403 profile()->GetTopSites()->SetPageThumbnail(url, bitmap, score);
[email protected]449478302009-06-09 20:04:282404 } else {
2405 HistoryService* hs;
2406 if (!profile()->IsOffTheRecord() &&
2407 (hs = profile()->GetHistoryService(Profile::IMPLICIT_ACCESS))) {
2408 hs->SetPageThumbnail(url, bitmap, score);
2409 }
[email protected]420ae012009-04-24 05:16:322410 }
2411}
2412
[email protected]c4e98902010-06-01 10:20:142413void TabContents::UpdateInspectorSetting(const std::string& key,
2414 const std::string& value) {
2415 DictionaryValue* inspector_settings =
2416 profile()->GetPrefs()->GetMutableDictionary(
2417 prefs::kWebKitInspectorSettings);
[email protected]628cf5a52010-06-07 14:25:332418 inspector_settings->SetWithoutPathExpansion(UTF8ToWide(key),
2419 Value::CreateStringValue(value));
[email protected]0df30122009-06-03 12:13:082420}
2421
[email protected]45aafc92010-06-07 14:18:532422void TabContents::ClearInspectorSettings() {
2423 DictionaryValue* inspector_settings =
2424 profile()->GetPrefs()->GetMutableDictionary(
2425 prefs::kWebKitInspectorSettings);
2426 inspector_settings->Clear();
2427}
2428
[email protected]420ae012009-04-24 05:16:322429void TabContents::Close(RenderViewHost* rvh) {
[email protected]07707302009-11-06 00:50:292430 // The UI may be in an event-tracking loop, such as between the
2431 // mouse-down and mouse-up in text selection or a button click.
2432 // Defer the close until after tracking is complete, so that we
2433 // don't free objects out from under the UI.
2434 // TODO(shess): This could probably be integrated with the
2435 // IsDoingDrag() test below. Punting for now because I need more
2436 // research to understand how this impacts platforms other than Mac.
2437 // TODO(shess): This could get more fine-grained. For instance,
2438 // closing a tab in another window while selecting text in the
2439 // current window's Omnibox should be just fine.
2440 if (view()->IsEventTracking()) {
2441 view()->CloseTabAfterEventTracking();
2442 return;
2443 }
2444
[email protected]24a4d1062009-07-10 23:10:422445 // If we close the tab while we're in the middle of a drag, we'll crash.
2446 // Instead, cancel the drag and close it as soon as the drag ends.
2447 if (view()->IsDoingDrag()) {
2448 view()->CancelDragAndCloseTab();
2449 return;
2450 }
2451
[email protected]420ae012009-04-24 05:16:322452 // Ignore this if it comes from a RenderViewHost that we aren't showing.
2453 if (delegate() && rvh == render_view_host())
2454 delegate()->CloseContents(this);
2455}
2456
2457void TabContents::RequestMove(const gfx::Rect& new_bounds) {
2458 if (delegate() && delegate()->IsPopup(this))
2459 delegate()->MoveContents(this, new_bounds);
2460}
2461
[email protected]7ab1e7d62009-10-14 23:32:012462void TabContents::DidStartLoading() {
[email protected]420ae012009-04-24 05:16:322463 SetIsLoading(true, NULL);
2464}
2465
[email protected]7ab1e7d62009-10-14 23:32:012466void TabContents::DidStopLoading() {
[email protected]420ae012009-04-24 05:16:322467 scoped_ptr<LoadNotificationDetails> details;
2468
2469 NavigationEntry* entry = controller_.GetActiveEntry();
2470 // An entry may not exist for a stop when loading an initial blank page or
2471 // if an iframe injected by script into a blank page finishes loading.
2472 if (entry) {
[email protected]420ae012009-04-24 05:16:322473 base::TimeDelta elapsed = base::TimeTicks::Now() - current_load_start_;
2474
2475 details.reset(new LoadNotificationDetails(
[email protected]ebe89e062009-08-13 23:16:542476 entry->virtual_url(),
[email protected]420ae012009-04-24 05:16:322477 entry->transition_type(),
2478 elapsed,
2479 &controller_,
2480 controller_.GetCurrentEntryIndex()));
2481 }
2482
2483 // Tell PasswordManager we've finished a page load, which serves as a
2484 // green light to save pending passwords and reset itself.
2485 GetPasswordManager()->DidStopLoading();
2486
2487 SetIsLoading(false, details.get());
2488}
2489
[email protected]420ae012009-04-24 05:16:322490void TabContents::DidRedirectProvisionalLoad(int32 page_id,
2491 const GURL& source_url,
2492 const GURL& target_url) {
2493 NavigationEntry* entry;
2494 if (page_id == -1)
2495 entry = controller_.pending_entry();
2496 else
2497 entry = controller_.GetEntryWithPageID(GetSiteInstance(), page_id);
2498 if (!entry || entry->url() != source_url)
2499 return;
2500 entry->set_url(target_url);
2501}
2502
[email protected]420ae012009-04-24 05:16:322503void TabContents::RequestOpenURL(const GURL& url, const GURL& referrer,
2504 WindowOpenDisposition disposition) {
2505 if (render_manager_.dom_ui()) {
2506 // When we're a DOM UI, it will provide a page transition type for us (this
2507 // is so the new tab page can specify AUTO_BOOKMARK for automatically
2508 // generated suggestions).
2509 //
2510 // Note also that we hide the referrer for DOM UI pages. We don't really
[email protected]60e448982009-05-06 04:21:162511 // want web sites to see a referrer of "chrome://blah" (and some
2512 // chrome: URLs might have search terms or other stuff we don't want to
[email protected]420ae012009-04-24 05:16:322513 // send to the site), so we send no referrer.
2514 OpenURL(url, GURL(), disposition,
2515 render_manager_.dom_ui()->link_transition_type());
2516 } else {
2517 OpenURL(url, referrer, disposition, PageTransition::LINK);
2518 }
2519}
2520
[email protected]1ee614862009-07-06 20:29:182521void TabContents::DomOperationResponse(const std::string& json_string,
2522 int automation_id) {
2523}
2524
[email protected]420ae012009-04-24 05:16:322525void TabContents::ProcessDOMUIMessage(const std::string& message,
[email protected]438c97d2010-05-21 23:30:152526 const ListValue* content,
[email protected]bb64b512010-04-02 21:01:392527 const GURL& source_url,
[email protected]811bfe32009-07-01 08:46:252528 int request_id,
2529 bool has_callback) {
[email protected]420ae012009-04-24 05:16:322530 if (!render_manager_.dom_ui()) {
[email protected]c2e74fe82009-09-03 17:57:442531 // This can happen if someone uses window.open() to open an extension URL
2532 // from a non-extension context.
2533 render_view_host()->BlockExtensionRequest(request_id);
[email protected]420ae012009-04-24 05:16:322534 return;
2535 }
[email protected]bb64b512010-04-02 21:01:392536 render_manager_.dom_ui()->ProcessDOMUIMessage(message, content, source_url,
2537 request_id,
[email protected]811bfe32009-07-01 08:46:252538 has_callback);
[email protected]420ae012009-04-24 05:16:322539}
2540
2541void TabContents::ProcessExternalHostMessage(const std::string& message,
2542 const std::string& origin,
2543 const std::string& target) {
2544 if (delegate())
2545 delegate()->ForwardMessageToExternalHost(message, origin, target);
2546}
2547
[email protected]cdaf8d02010-03-30 19:52:472548void TabContents::RunFileChooser(
2549 const ViewHostMsg_RunFileChooser_Params &params) {
[email protected]420ae012009-04-24 05:16:322550 if (!select_file_dialog_.get())
2551 select_file_dialog_ = SelectFileDialog::Create(this);
[email protected]cdaf8d02010-03-30 19:52:472552
2553 SelectFileDialog::Type dialog_type;
2554 switch (params.mode) {
2555 case ViewHostMsg_RunFileChooser_Params::Open:
2556 dialog_type = SelectFileDialog::SELECT_OPEN_FILE;
2557 break;
2558 case ViewHostMsg_RunFileChooser_Params::OpenMultiple:
2559 dialog_type = SelectFileDialog::SELECT_OPEN_MULTI_FILE;
2560 break;
2561 case ViewHostMsg_RunFileChooser_Params::Save:
2562 dialog_type = SelectFileDialog::SELECT_SAVEAS_FILE;
2563 break;
2564 default:
[email protected]bed31952010-03-30 20:07:592565 dialog_type = SelectFileDialog::SELECT_OPEN_FILE; // Prevent warning.
[email protected]cdaf8d02010-03-30 19:52:472566 NOTREACHED();
2567 }
[email protected]35896a32010-06-09 08:42:512568 FilePath default_file_name = params.default_file_name;
2569 if (default_file_name.empty())
2570 default_file_name = profile()->last_selected_directory();
[email protected]cdaf8d02010-03-30 19:52:472571 select_file_dialog_->SelectFile(dialog_type, params.title,
[email protected]35896a32010-06-09 08:42:512572 default_file_name,
[email protected]420ae012009-04-24 05:16:322573 NULL, 0, FILE_PATH_LITERAL(""),
2574 view_->GetTopLevelNativeWindow(), NULL);
2575}
2576
2577void TabContents::RunJavaScriptMessage(
2578 const std::wstring& message,
2579 const std::wstring& default_prompt,
2580 const GURL& frame_url,
2581 const int flags,
2582 IPC::Message* reply_msg,
2583 bool* did_suppress_message) {
2584 // Suppress javascript messages when requested and when inside a constrained
2585 // popup window (because that activates them and breaks them out of the
2586 // constrained window jail).
[email protected]91ed49a2009-07-07 21:29:552587 // Also suppress messages when showing an interstitial. The interstitial is
2588 // shown over the previous page, we don't want the hidden page dialogs to
2589 // interfere with the interstitial.
2590 bool suppress_this_message = suppress_javascript_messages_ ||
2591 showing_interstitial_page();
[email protected]420ae012009-04-24 05:16:322592 if (delegate())
2593 suppress_this_message |=
[email protected]b6c874582009-05-08 19:38:312594 (delegate()->GetConstrainingContents(this) != this);
[email protected]420ae012009-04-24 05:16:322595
2596 *did_suppress_message = suppress_this_message;
2597
2598 if (!suppress_this_message) {
2599 base::TimeDelta time_since_last_message(
2600 base::TimeTicks::Now() - last_javascript_message_dismissal_);
2601 bool show_suppress_checkbox = false;
2602 // Show a checkbox offering to suppress further messages if this message is
2603 // being displayed within kJavascriptMessageExpectedDelay of the last one.
2604 if (time_since_last_message <
2605 base::TimeDelta::FromMilliseconds(kJavascriptMessageExpectedDelay))
2606 show_suppress_checkbox = true;
2607
[email protected]d82ed61e2009-06-16 02:46:222608 RunJavascriptMessageBox(this, frame_url, flags, message, default_prompt,
[email protected]420ae012009-04-24 05:16:322609 show_suppress_checkbox, reply_msg);
2610 } else {
2611 // If we are suppressing messages, just reply as is if the user immediately
2612 // pressed "Cancel".
[email protected]beb440c2009-11-06 04:08:542613 OnMessageBoxClosed(reply_msg, false, std::wstring());
[email protected]420ae012009-04-24 05:16:322614 }
2615}
2616
2617void TabContents::RunBeforeUnloadConfirm(const std::wstring& message,
2618 IPC::Message* reply_msg) {
[email protected]7ab1e7d62009-10-14 23:32:012619 is_showing_before_unload_dialog_ = true;
[email protected]d82ed61e2009-06-16 02:46:222620 RunBeforeUnloadDialog(this, message, reply_msg);
[email protected]420ae012009-04-24 05:16:322621}
2622
2623void TabContents::ShowModalHTMLDialog(const GURL& url, int width, int height,
2624 const std::string& json_arguments,
2625 IPC::Message* reply_msg) {
2626 if (delegate()) {
2627 HtmlDialogUIDelegate* dialog_delegate =
2628 new ModalHtmlDialogDelegate(url, width, height, json_arguments,
[email protected]d82ed61e2009-06-16 02:46:222629 reply_msg, this);
[email protected]420ae012009-04-24 05:16:322630 delegate()->ShowHtmlDialog(dialog_delegate, NULL);
2631 }
2632}
2633
[email protected]4d44a1c2010-04-28 19:20:412634void TabContents::PasswordFormsFound(
[email protected]ed3fb032009-06-16 19:50:562635 const std::vector<webkit_glue::PasswordForm>& forms) {
[email protected]4d44a1c2010-04-28 19:20:412636 GetPasswordManager()->PasswordFormsFound(forms);
2637}
2638
2639void TabContents::PasswordFormsVisible(
2640 const std::vector<webkit_glue::PasswordForm>& visible_forms) {
2641 GetPasswordManager()->PasswordFormsVisible(visible_forms);
[email protected]420ae012009-04-24 05:16:322642}
2643
[email protected]420ae012009-04-24 05:16:322644// Checks to see if we should generate a keyword based on the OSDD, and if
2645// necessary uses TemplateURLFetcher to download the OSDD and create a keyword.
2646void TabContents::PageHasOSDD(RenderViewHost* render_view_host,
2647 int32 page_id, const GURL& url,
2648 bool autodetected) {
2649 // Make sure page_id is the current page, and the TemplateURLModel is loaded.
2650 DCHECK(url.is_valid());
2651 if (!IsActiveEntry(page_id))
2652 return;
2653 TemplateURLModel* url_model = profile()->GetTemplateURLModel();
2654 if (!url_model)
2655 return;
2656 if (!url_model->loaded()) {
2657 url_model->Load();
2658 return;
2659 }
2660 if (!profile()->GetTemplateURLFetcher())
2661 return;
2662
2663 if (profile()->IsOffTheRecord())
2664 return;
2665
2666 const NavigationEntry* entry = controller_.GetLastCommittedEntry();
2667 DCHECK(entry);
2668
2669 const NavigationEntry* base_entry = entry;
2670 if (IsFormSubmit(base_entry)) {
2671 // If the current page is a form submit, find the last page that was not
2672 // a form submit and use its url to generate the keyword from.
2673 int index = controller_.last_committed_entry_index() - 1;
2674 while (index >= 0 && IsFormSubmit(controller_.GetEntryAtIndex(index)))
2675 index--;
2676 if (index >= 0)
2677 base_entry = controller_.GetEntryAtIndex(index);
2678 else
2679 base_entry = NULL;
2680 }
2681
2682 // We want to use the user typed URL if available since that represents what
2683 // the user typed to get here, and fall back on the regular URL if not.
2684 if (!base_entry)
2685 return;
2686 GURL keyword_url = base_entry->user_typed_url().is_valid() ?
2687 base_entry->user_typed_url() : base_entry->url();
2688 if (!keyword_url.is_valid())
2689 return;
2690 std::wstring keyword = TemplateURLModel::GenerateKeyword(keyword_url,
2691 autodetected);
[email protected]a88dbdd52010-01-15 19:25:532692
2693 // For JS added OSDD empty keyword is OK because we will generate keyword
2694 // later from OSDD content.
2695 if (autodetected && keyword.empty())
[email protected]420ae012009-04-24 05:16:322696 return;
2697 const TemplateURL* template_url =
2698 url_model->GetTemplateURLForKeyword(keyword);
2699 if (template_url && (!template_url->safe_for_autoreplace() ||
2700 template_url->originating_url() == url)) {
2701 // Either there is a user created TemplateURL for this keyword, or the
2702 // keyword has the same OSDD url and we've parsed it.
2703 return;
2704 }
2705
2706 // Download the OpenSearch description document. If this is successful a
2707 // new keyword will be created when done.
[email protected]420ae012009-04-24 05:16:322708 profile()->GetTemplateURLFetcher()->ScheduleDownload(
2709 keyword,
2710 url,
2711 base_entry->favicon().url(),
[email protected]d0cc9fb52009-06-23 02:26:162712 this,
[email protected]420ae012009-04-24 05:16:322713 autodetected);
2714}
2715
[email protected]155f35e2010-07-17 00:30:182716// Indicates if the two inputs have the same security origin.
2717// |requested_origin| should only be a security origin (no path, etc.).
2718// It is ok if |template_url| is NULL.
2719static bool IsSameOrigin(const GURL& requested_origin,
2720 const TemplateURL* template_url) {
2721 DCHECK(requested_origin == requested_origin.GetOrigin());
2722 return template_url && requested_origin ==
2723 TemplateURLModel::GenerateSearchURL(template_url).GetOrigin();
2724}
2725
2726ViewHostMsg_GetSearchProviderInstallState_Params
2727 TabContents::GetSearchProviderInstallState(const GURL& requested_host) {
2728 // Get the last committed entry since that is the page executing the
2729 // javascript as opposed to a page being navigated to. We don't want
2730 // to trust the page to tell us the url to avoid using potentially
2731 // compromised information.
2732 NavigationEntry* entry = controller_.GetLastCommittedEntry();
2733 GURL page_origin = entry ? entry->virtual_url().GetOrigin() : GURL();
2734 GURL requested_origin = requested_host.GetOrigin();
2735 // Do the security check before any others to avoid information leaks.
2736 if (page_origin != requested_origin)
2737 return ViewHostMsg_GetSearchProviderInstallState_Params::Denied();
2738
2739 // In incognito mode, no search information is exposed. (This check must be
2740 // done after the security check or else a web site can detect that the
2741 // user is in incognito mode just by doing a cross origin request.)
2742 if (profile()->IsOffTheRecord())
2743 return ViewHostMsg_GetSearchProviderInstallState_Params::NotInstalled();
2744
2745 TemplateURLModel* url_model = profile()->GetTemplateURLModel();
2746 if (!url_model)
2747 return ViewHostMsg_GetSearchProviderInstallState_Params::NotInstalled();
2748 if (!url_model->loaded())
2749 url_model->Load();
2750
2751 // First check to see if the url is the default search provider.
2752 if (IsSameOrigin(requested_origin, url_model->GetDefaultSearchProvider())) {
2753 return ViewHostMsg_GetSearchProviderInstallState_Params::
2754 InstalledAsDefault();
2755 }
2756
2757 // Is the url any search provider?
2758 std::vector<const TemplateURL*> urls = url_model->GetTemplateURLs();
2759 for (std::vector<const TemplateURL*>::iterator i = urls.begin();
2760 i != urls.end(); ++i) {
2761 const TemplateURL* template_url = (*i);
2762 if (IsSameOrigin(requested_origin, template_url)) {
2763 return ViewHostMsg_GetSearchProviderInstallState_Params::
2764 InstallButNotDefault();
2765 }
2766 }
2767 return ViewHostMsg_GetSearchProviderInstallState_Params::NotInstalled();
2768}
2769
[email protected]420ae012009-04-24 05:16:322770GURL TabContents::GetAlternateErrorPageURL() const {
2771 GURL url;
2772 // Disable alternate error pages when in OffTheRecord/Incognito mode.
2773 if (profile()->IsOffTheRecord())
2774 return url;
2775
2776 PrefService* prefs = profile()->GetPrefs();
2777 DCHECK(prefs);
2778 if (prefs->GetBoolean(prefs::kAlternateErrorPagesEnabled)) {
[email protected]a33457b82009-09-21 23:07:152779 url = google_util::AppendGoogleLocaleParam(
2780 GURL(google_util::kLinkDoctorBaseURL));
[email protected]420ae012009-04-24 05:16:322781 url = google_util::AppendGoogleTLDParam(url);
2782 }
2783 return url;
2784}
2785
2786WebPreferences TabContents::GetWebkitPrefs() {
[email protected]6c32ce72010-03-08 05:18:062787 Profile* profile = render_view_host()->process()->profile();
[email protected]7ccddb8c2009-08-04 17:36:552788 bool is_dom_ui = false;
[email protected]6c32ce72010-03-08 05:18:062789 return RenderViewHostDelegateHelper::GetWebkitPrefs(profile, is_dom_ui);
[email protected]420ae012009-04-24 05:16:322790}
2791
[email protected]fa1cf0b82010-01-15 21:49:442792void TabContents::OnIgnoredUIEvent() {
2793 if (constrained_window_count()) {
2794 ConstrainedWindow* window = *constrained_window_begin();
2795 window->FocusConstrainedWindow();
2796 }
2797}
2798
[email protected]420ae012009-04-24 05:16:322799void TabContents::OnJSOutOfMemory() {
2800 AddInfoBar(new SimpleAlertInfoBarDelegate(
[email protected]938e1f92010-04-01 18:09:422801 this, l10n_util::GetString(IDS_JS_OUT_OF_MEMORY_PROMPT), NULL, true));
[email protected]420ae012009-04-24 05:16:322802}
2803
[email protected]420ae012009-04-24 05:16:322804void TabContents::OnCrossSiteResponse(int new_render_process_host_id,
2805 int new_request_id) {
[email protected]57c6a652009-05-04 07:58:342806 // Allows the TabContents to react when a cross-site response is ready to be
[email protected]420ae012009-04-24 05:16:322807 // delivered to a pending RenderViewHost. We must first run the onunload
2808 // handler of the old RenderViewHost before we can allow it to proceed.
2809 render_manager_.OnCrossSiteResponse(new_render_process_host_id,
2810 new_request_id);
2811}
2812
[email protected]420ae012009-04-24 05:16:322813gfx::Rect TabContents::GetRootWindowResizerRect() const {
2814 if (delegate())
2815 return delegate()->GetRootWindowResizerRect();
2816 return gfx::Rect();
2817}
2818
2819void TabContents::RendererUnresponsive(RenderViewHost* rvh,
2820 bool is_during_unload) {
2821 if (is_during_unload) {
2822 // Hang occurred while firing the beforeunload/unload handler.
2823 // Pretend the handler fired so tab closing continues as if it had.
[email protected]829e7612009-04-25 01:15:112824 rvh->set_sudden_termination_allowed(true);
[email protected]420ae012009-04-24 05:16:322825
2826 if (!render_manager_.ShouldCloseTabOnUnresponsiveRenderer())
2827 return;
2828
2829 // If the tab hangs in the beforeunload/unload handler there's really
2830 // nothing we can do to recover. Pretend the unload listeners have
2831 // all fired and close the tab. If the hang is in the beforeunload handler
2832 // then the user will not have the option of cancelling the close.
2833 Close(rvh);
2834 return;
2835 }
2836
2837 if (render_view_host() && render_view_host()->IsRenderViewLive())
[email protected]e7b8d832009-11-20 22:41:002838 hung_renderer_dialog::ShowForTabContents(this);
[email protected]420ae012009-04-24 05:16:322839}
2840
2841void TabContents::RendererResponsive(RenderViewHost* render_view_host) {
[email protected]e7b8d832009-11-20 22:41:002842 hung_renderer_dialog::HideForTabContents(this);
[email protected]420ae012009-04-24 05:16:322843}
2844
2845void TabContents::LoadStateChanged(const GURL& url,
[email protected]094e5b22009-09-25 04:23:562846 net::LoadState load_state,
2847 uint64 upload_position,
2848 uint64 upload_size) {
[email protected]420ae012009-04-24 05:16:322849 load_state_ = load_state;
[email protected]094e5b22009-09-25 04:23:562850 upload_position_ = upload_position;
2851 upload_size_ = upload_size;
[email protected]d686e812009-06-03 19:10:292852 std::wstring languages =
[email protected]ddd231e2010-06-29 20:35:192853 UTF8ToWide(profile()->GetPrefs()->GetString(prefs::kAcceptLanguages));
[email protected]d686e812009-06-03 19:10:292854 std::string host = url.host();
[email protected]ce85f602009-11-07 01:34:532855 load_state_host_ =
2856 net::IDNToUnicode(host.c_str(), host.size(), languages, NULL);
[email protected]420ae012009-04-24 05:16:322857 if (load_state_ == net::LOAD_STATE_READING_RESPONSE)
2858 SetNotWaitingForResponse();
2859 if (is_loading())
[email protected]c9cd2222009-05-06 05:16:502860 NotifyNavigationStateChanged(INVALIDATE_LOAD | INVALIDATE_TAB);
[email protected]420ae012009-04-24 05:16:322861}
2862
[email protected]1ee614862009-07-06 20:29:182863bool TabContents::IsExternalTabContainer() const {
2864 if (!delegate())
2865 return false;
2866
2867 return delegate()->IsExternalTabContainer();
2868}
2869
[email protected]1ee614862009-07-06 20:29:182870void TabContents::DidInsertCSS() {
2871 // This RVHDelegate function is used for extensions and not us.
[email protected]420ae012009-04-24 05:16:322872}
2873
[email protected]130efb02009-09-18 18:54:352874void TabContents::FocusedNodeChanged() {
2875 NotificationService::current()->Notify(
2876 NotificationType::FOCUS_CHANGED_IN_PAGE,
2877 Source<RenderViewHost>(render_view_host()),
2878 NotificationService::NoDetails());
2879}
2880
[email protected]420ae012009-04-24 05:16:322881void TabContents::FileSelected(const FilePath& path,
2882 int index, void* params) {
[email protected]35896a32010-06-09 08:42:512883 profile()->set_last_selected_directory(path.DirName());
[email protected]cdaf8d02010-03-30 19:52:472884 std::vector<FilePath> files;
2885 files.push_back(path);
2886 render_view_host()->FilesSelectedInChooser(files);
[email protected]420ae012009-04-24 05:16:322887}
2888
2889void TabContents::MultiFilesSelected(const std::vector<FilePath>& files,
2890 void* params) {
[email protected]35896a32010-06-09 08:42:512891 if (!files.empty())
2892 profile()->set_last_selected_directory(files[0].DirName());
[email protected]cdaf8d02010-03-30 19:52:472893 render_view_host()->FilesSelectedInChooser(files);
[email protected]420ae012009-04-24 05:16:322894}
2895
2896void TabContents::FileSelectionCanceled(void* params) {
2897 // If the user cancels choosing a file to upload we pass back an
2898 // empty vector.
[email protected]cdaf8d02010-03-30 19:52:472899 render_view_host()->FilesSelectedInChooser(std::vector<FilePath>());
[email protected]420ae012009-04-24 05:16:322900}
2901
2902void TabContents::BeforeUnloadFiredFromRenderManager(
2903 bool proceed,
2904 bool* proceed_to_fire_unload) {
2905 if (delegate())
2906 delegate()->BeforeUnloadFired(this, proceed, proceed_to_fire_unload);
2907}
2908
[email protected]3a3d47472010-07-15 21:03:542909void TabContents::DidStartLoadingFromRenderManager(
2910 RenderViewHost* render_view_host) {
2911 DidStartLoading();
2912}
2913
2914void TabContents::RenderViewGoneFromRenderManager(
2915 RenderViewHost* render_view_host) {
2916 RenderViewGone(render_view_host);
2917}
2918
[email protected]420ae012009-04-24 05:16:322919void TabContents::UpdateRenderViewSizeForRenderManager() {
2920 // TODO(brettw) this is a hack. See TabContentsView::SizeContents.
[email protected]0323ee42010-02-17 22:03:262921 gfx::Size size = view_->GetContainerSize();
2922 // 0x0 isn't a valid window size (minimal window size is 1x1) but it may be
2923 // here during container initialization and normal window size will be set
2924 // later. In case of tab duplication this resizing to 0x0 prevents setting
2925 // normal size later so just ignore it.
2926 if (!size.IsEmpty())
2927 view_->SizeContents(size);
[email protected]420ae012009-04-24 05:16:322928}
2929
[email protected]3a3d47472010-07-15 21:03:542930void TabContents::NotifySwappedFromRenderManager() {
2931 NotifySwapped();
2932}
2933
2934NavigationController& TabContents::GetControllerForRenderManager() {
2935 return controller();
2936}
2937
[email protected]420ae012009-04-24 05:16:322938DOMUI* TabContents::CreateDOMUIForRenderManager(const GURL& url) {
[email protected]d82ed61e2009-06-16 02:46:222939 return DOMUIFactory::CreateDOMUIForURL(this, url);
[email protected]420ae012009-04-24 05:16:322940}
2941
2942NavigationEntry*
2943TabContents::GetLastCommittedNavigationEntryForRenderManager() {
2944 return controller_.GetLastCommittedEntry();
2945}
2946
2947bool TabContents::CreateRenderViewForRenderManager(
2948 RenderViewHost* render_view_host) {
[email protected]420ae012009-04-24 05:16:322949 RenderWidgetHostView* rwh_view = view_->CreateViewForWidget(render_view_host);
[email protected]1a98a932009-11-17 00:12:522950
2951 scoped_refptr<URLRequestContextGetter> request_context = request_context_;
2952 if (!request_context.get())
2953 request_context = profile()->GetRequestContext();
2954
[email protected]8ab04652010-06-12 02:47:262955 if (!render_view_host->CreateRenderView(request_context, string16()))
[email protected]420ae012009-04-24 05:16:322956 return false;
2957
2958 // Now that the RenderView has been created, we need to tell it its size.
2959 rwh_view->SetSize(view_->GetContainerSize());
2960
2961 UpdateMaxPageIDIfNecessary(render_view_host->site_instance(),
2962 render_view_host);
2963 return true;
2964}
2965
2966void TabContents::Observe(NotificationType type,
2967 const NotificationSource& source,
2968 const NotificationDetails& details) {
2969 switch (type.value) {
2970 case NotificationType::BOOKMARK_MODEL_LOADED:
2971 // BookmarkModel finished loading, fall through to update starred state.
2972 case NotificationType::URLS_STARRED: {
2973 // Somewhere, a URL has been starred.
2974 // Ignore notifications for profiles other than our current one.
2975 Profile* source_profile = Source<Profile>(source).ptr();
2976 if (!source_profile || !source_profile->IsSameProfile(profile()))
2977 return;
2978
2979 UpdateStarredStateForCurrentURL();
2980 break;
2981 }
2982 case NotificationType::PREF_CHANGED: {
2983 std::wstring* pref_name_in = Details<std::wstring>(details).ptr();
2984 DCHECK(Source<PrefService>(source).ptr() == profile()->GetPrefs());
2985 if (*pref_name_in == prefs::kAlternateErrorPagesEnabled) {
2986 UpdateAlternateErrorPageURL();
2987 } else if (*pref_name_in == prefs::kDefaultCharset ||
2988 StartsWithASCII(WideToUTF8(*pref_name_in), "webkit.webprefs.", true)
2989 ) {
2990 UpdateWebPreferences();
2991 } else {
2992 NOTREACHED() << "unexpected pref change notification" << *pref_name_in;
2993 }
2994 break;
2995 }
2996 case NotificationType::RENDER_WIDGET_HOST_DESTROYED:
2997 view_->RenderWidgetHostDestroyed(Source<RenderWidgetHost>(source).ptr());
2998 break;
2999
3000 case NotificationType::NAV_ENTRY_COMMITTED: {
3001 DCHECK(&controller_ == Source<NavigationController>(source).ptr());
3002
3003 NavigationController::LoadCommittedDetails& committed_details =
3004 *(Details<NavigationController::LoadCommittedDetails>(details).ptr());
3005 ExpireInfoBars(committed_details);
3006 break;
3007 }
3008
[email protected]d3ba77272009-09-03 00:06:093009#if defined(OS_LINUX)
3010 case NotificationType::BROWSER_THEME_CHANGED: {
[email protected]7ff431e2010-01-07 18:18:543011 renderer_preferences_util::UpdateFromSystemSettings(
3012 &renderer_preferences_, profile());
[email protected]d3ba77272009-09-03 00:06:093013 render_view_host()->SyncRendererPrefs();
3014 break;
3015 }
3016#endif
3017
[email protected]6c32ce72010-03-08 05:18:063018 case NotificationType::USER_STYLE_SHEET_UPDATED:
3019 UpdateWebPreferences();
3020 break;
3021
[email protected]79580c62010-02-02 02:36:253022 case NotificationType::CONTENT_SETTINGS_CHANGED: {
[email protected]0314ae02010-04-08 09:18:293023 Details<const HostContentSettingsMap::ContentSettingsDetails>
[email protected]79580c62010-02-02 02:36:253024 settings_details(details);
[email protected]79580c62010-02-02 02:36:253025 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]da0c8e92010-02-09 23:25:493026 GURL entry_url;
[email protected]0314ae02010-04-08 09:18:293027 if (entry)
[email protected]da0c8e92010-02-09 23:25:493028 entry_url = entry->url();
[email protected]0314ae02010-04-08 09:18:293029 if (settings_details.ptr()->update_all() ||
3030 settings_details.ptr()->pattern().Matches(entry_url)) {
3031 render_view_host()->SendContentSettings(entry_url,
[email protected]b9a4ea7f2010-06-25 07:11:233032 profile()->GetHostContentSettingsMap()->
3033 GetContentSettings(entry_url));
[email protected]79580c62010-02-02 02:36:253034 }
3035 break;
3036 }
3037
[email protected]e5adff72010-06-14 18:13:063038 case NotificationType::EXTENSION_LOADED:
[email protected]a888b29e62010-04-01 13:38:573039 break;
[email protected]a888b29e62010-04-01 13:38:573040
3041 case NotificationType::EXTENSION_UNLOADED:
[email protected]e5adff72010-06-14 18:13:063042 case NotificationType::EXTENSION_UNLOADED_DISABLED:
[email protected]a888b29e62010-04-01 13:38:573043 break;
[email protected]a888b29e62010-04-01 13:38:573044
[email protected]420ae012009-04-24 05:16:323045 default:
3046 NOTREACHED();
3047 }
3048}
[email protected]aacd7cc2009-09-02 14:51:103049
[email protected]98aa0b52010-05-06 17:03:083050void TabContents::UpdateExtensionAppIcon(Extension* extension) {
3051 extension_app_icon_.reset();
[email protected]f075fe0a22010-04-26 04:06:433052
[email protected]a888b29e62010-04-01 13:38:573053 if (extension) {
[email protected]98aa0b52010-05-06 17:03:083054 extension_app_image_loader_.reset(new ImageLoadingTracker(this));
3055 extension_app_image_loader_->LoadImage(
[email protected]a888b29e62010-04-01 13:38:573056 extension,
3057 extension->GetIconPath(Extension::EXTENSION_ICON_SMALLISH),
3058 gfx::Size(Extension::EXTENSION_ICON_SMALLISH,
3059 Extension::EXTENSION_ICON_SMALLISH),
3060 ImageLoadingTracker::CACHE);
3061 } else {
[email protected]98aa0b52010-05-06 17:03:083062 extension_app_image_loader_.reset(NULL);
[email protected]a888b29e62010-04-01 13:38:573063 }
3064}
3065
3066Extension* TabContents::GetExtensionContaining(const GURL& url) {
3067 ExtensionsService* extensions_service = profile()->GetExtensionsService();
3068 if (!extensions_service)
3069 return NULL;
3070
3071 Extension* extension = extensions_service->GetExtensionByURL(url);
3072 return extension ?
3073 extension : extensions_service->GetExtensionByWebExtent(url);
3074}
3075
[email protected]a888b29e62010-04-01 13:38:573076void TabContents::OnImageLoaded(SkBitmap* image, ExtensionResource resource,
3077 int index) {
3078 if (image) {
[email protected]98aa0b52010-05-06 17:03:083079 extension_app_icon_ = *image;
[email protected]a888b29e62010-04-01 13:38:573080 NotifyNavigationStateChanged(INVALIDATE_TAB);
3081 }
3082}
3083
[email protected]beb440c2009-11-06 04:08:543084std::wstring TabContents::GetMessageBoxTitle(const GURL& frame_url,
3085 bool is_alert) {
3086 if (!frame_url.has_host())
3087 return l10n_util::GetString(
3088 is_alert ? IDS_JAVASCRIPT_ALERT_DEFAULT_TITLE
3089 : IDS_JAVASCRIPT_MESSAGEBOX_DEFAULT_TITLE);
3090
3091 // We really only want the scheme, hostname, and port.
3092 GURL::Replacements replacements;
3093 replacements.ClearUsername();
3094 replacements.ClearPassword();
3095 replacements.ClearPath();
3096 replacements.ClearQuery();
3097 replacements.ClearRef();
3098 GURL clean_url = frame_url.ReplaceComponents(replacements);
3099
3100 // TODO(brettw) it should be easier than this to do the correct language
3101 // handling without getting the accept language from the profile.
3102 std::wstring base_address = gfx::ElideUrl(clean_url, gfx::Font(), 0,
[email protected]ddd231e2010-06-29 20:35:193103 UTF8ToWide(profile()->GetPrefs()->GetString(prefs::kAcceptLanguages)));
[email protected]beb440c2009-11-06 04:08:543104 // Force URL to have LTR directionality.
[email protected]4a4afe32010-05-05 01:39:563105 base::i18n::GetDisplayStringInLTRDirectionality(&base_address);
[email protected]beb440c2009-11-06 04:08:543106
3107 return l10n_util::GetStringF(
3108 is_alert ? IDS_JAVASCRIPT_ALERT_TITLE : IDS_JAVASCRIPT_MESSAGEBOX_TITLE,
3109 base_address);
3110}
3111
3112gfx::NativeWindow TabContents::GetMessageBoxRootWindow() {
3113 return view_->GetTopLevelNativeWindow();
3114}
3115
3116void TabContents::OnMessageBoxClosed(IPC::Message* reply_msg,
3117 bool success,
3118 const std::wstring& prompt) {
3119 last_javascript_message_dismissal_ = base::TimeTicks::Now();
3120 if (is_showing_before_unload_dialog_ && !success) {
3121 // If a beforeunload dialog is canceled, we need to stop the throbber from
3122 // spinning, since we forced it to start spinning in Navigate.
3123 DidStopLoading();
3124
3125 tab_close_start_time_ = base::TimeTicks();
3126 }
3127 is_showing_before_unload_dialog_ = false;
3128 render_view_host()->JavaScriptMessageBoxClosed(reply_msg, success, prompt);
3129}
3130
3131void TabContents::SetSuppressMessageBoxes(bool suppress_message_boxes) {
3132 set_suppress_javascript_messages(suppress_message_boxes);
3133}
3134
[email protected]3a3d47472010-07-15 21:03:543135TabContents* TabContents::AsTabContents() {
3136 return this;
3137}
3138
3139ExtensionHost* TabContents::AsExtensionHost() {
3140 return NULL;
3141}
3142
[email protected]41fc0322009-09-04 22:23:403143void TabContents::set_encoding(const std::string& encoding) {
[email protected]aacd7cc2009-09-02 14:51:103144 encoding_ = CharacterEncoding::GetCanonicalEncodingNameByAliasName(encoding);
3145}
[email protected]f45d2a72010-03-08 23:28:353146
3147void TabContents::SetAppIcon(const SkBitmap& app_icon) {
3148 app_icon_ = app_icon;
3149 NotifyNavigationStateChanged(INVALIDATE_TITLE);
3150}
[email protected]ea68c06a2010-04-23 17:25:373151
3152// After a successful *new* login attempt, we take the PasswordFormManager in
3153// provisional_save_manager_ and move it to a SavePasswordInfoBarDelegate while
3154// the user makes up their mind with the "save password" infobar. Note if the
3155// login is one we already know about, the end of the line is
3156// provisional_save_manager_ because we just update it on success and so such
3157// forms never end up in an infobar.
3158class SavePasswordInfoBarDelegate : public ConfirmInfoBarDelegate {
3159 public:
3160 SavePasswordInfoBarDelegate(TabContents* tab_contents,
[email protected]12bb6822010-06-16 02:00:463161 PasswordFormManager* form_to_save)
3162 : ConfirmInfoBarDelegate(tab_contents),
3163 form_to_save_(form_to_save),
3164 infobar_response_(kNoResponse) {
[email protected]ea68c06a2010-04-23 17:25:373165 }
3166
3167 virtual ~SavePasswordInfoBarDelegate() { }
3168
3169 // Overridden from ConfirmInfoBarDelegate:
3170 virtual void InfoBarClosed() {
[email protected]12bb6822010-06-16 02:00:463171 UMA_HISTOGRAM_ENUMERATION("PasswordManager.InfoBarResponse",
3172 infobar_response_, kNumResponseTypes);
[email protected]ea68c06a2010-04-23 17:25:373173 delete this;
3174 }
3175
3176 virtual std::wstring GetMessageText() const {
3177 return l10n_util::GetString(IDS_PASSWORD_MANAGER_SAVE_PASSWORD_PROMPT);
3178 }
3179
3180 virtual SkBitmap* GetIcon() const {
3181 return ResourceBundle::GetSharedInstance().GetBitmapNamed(
3182 IDR_INFOBAR_SAVE_PASSWORD);
3183 }
3184
3185 virtual int GetButtons() const {
3186 return BUTTON_OK | BUTTON_CANCEL;
3187 }
3188
3189 virtual std::wstring GetButtonLabel(InfoBarButton button) const {
3190 if (button == BUTTON_OK)
3191 return l10n_util::GetString(IDS_PASSWORD_MANAGER_SAVE_BUTTON);
3192 if (button == BUTTON_CANCEL)
3193 return l10n_util::GetString(IDS_PASSWORD_MANAGER_BLACKLIST_BUTTON);
3194 NOTREACHED();
3195 return std::wstring();
3196 }
3197
3198 virtual bool Accept() {
3199 DCHECK(form_to_save_.get());
3200 form_to_save_->Save();
[email protected]12bb6822010-06-16 02:00:463201 infobar_response_ = kRememberPassword;
[email protected]ea68c06a2010-04-23 17:25:373202 return true;
3203 }
3204
3205 virtual bool Cancel() {
3206 DCHECK(form_to_save_.get());
3207 form_to_save_->PermanentlyBlacklist();
[email protected]12bb6822010-06-16 02:00:463208 infobar_response_ = kDontRememberPassword;
[email protected]ea68c06a2010-04-23 17:25:373209 return true;
3210 }
3211
3212 private:
3213 // The PasswordFormManager managing the form we're asking the user about,
3214 // and should update as per her decision.
3215 scoped_ptr<PasswordFormManager> form_to_save_;
3216
[email protected]12bb6822010-06-16 02:00:463217 // Used to track the results we get from the info bar.
3218 enum ResponseType {
3219 kNoResponse = 0,
3220 kRememberPassword,
3221 kDontRememberPassword,
3222 kNumResponseTypes,
3223 };
3224 ResponseType infobar_response_;
3225
[email protected]ea68c06a2010-04-23 17:25:373226 DISALLOW_COPY_AND_ASSIGN(SavePasswordInfoBarDelegate);
3227};
3228
3229void TabContents::FillPasswordForm(
[email protected]e768baf2010-07-16 19:36:163230 const webkit_glue::PasswordFormFillData& form_data) {
[email protected]ea68c06a2010-04-23 17:25:373231 render_view_host()->FillPasswordForm(form_data);
3232}
3233
3234void TabContents::AddSavePasswordInfoBar(PasswordFormManager* form_to_save) {
3235 AddInfoBar(new SavePasswordInfoBarDelegate(this, form_to_save));
3236}
3237
3238Profile* TabContents::GetProfileForPasswordManager() {
3239 return profile();
3240}
3241
3242bool TabContents::DidLastPageLoadEncounterSSLErrors() {
3243 return controller().ssl_manager()->ProcessedSSLErrorFromRequest();
3244}