blob: cdd5d1f6cfa4b329fca2acdf44ff1a3020debb6c [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]a92b8642009-05-05 23:38:567#include "app/l10n_util.h"
[email protected]9929da92009-05-05 02:05:118#include "app/resource_bundle.h"
[email protected]614519812010-03-19 08:20:569#include "app/text_elider.h"
[email protected]8cc8d492010-02-02 10:40:4910#include "base/auto_reset.h"
[email protected]420ae012009-04-24 05:16:3211#include "base/file_version_info.h"
[email protected]7cf1b6ce2010-03-20 06:37:0112#include "base/i18n/rtl.h"
[email protected]420ae012009-04-24 05:16:3213#include "base/process_util.h"
[email protected]96d185d2009-04-24 03:28:5414#include "base/string16.h"
[email protected]996fd702009-09-04 19:12:3715#include "base/string_util.h"
[email protected]96d185d2009-04-24 03:28:5416#include "base/time.h"
[email protected]6db0def92010-03-02 19:55:4517#include "chrome/browser/autocomplete_history_manager.h"
[email protected]21674df2009-10-28 19:51:2118#include "chrome/browser/autofill/autofill_manager.h"
[email protected]66ba4932009-06-04 19:22:1319#include "chrome/browser/blocked_popup_container.h"
[email protected]420ae012009-04-24 05:16:3220#include "chrome/browser/bookmarks/bookmark_model.h"
[email protected]96d185d2009-04-24 03:28:5421#include "chrome/browser/browser_process.h"
[email protected]cf68b092010-01-15 20:05:5522#include "chrome/browser/browser_shutdown.h"
initial.commit09911bf2008-07-26 23:55:2923#include "chrome/browser/cert_store.h"
[email protected]aacd7cc2009-09-02 14:51:1024#include "chrome/browser/character_encoding.h"
[email protected]96d185d2009-04-24 03:28:5425#include "chrome/browser/debugger/devtools_manager.h"
[email protected]420ae012009-04-24 05:16:3226#include "chrome/browser/dom_operation_notification_details.h"
[email protected]96d185d2009-04-24 03:28:5427#include "chrome/browser/dom_ui/dom_ui.h"
28#include "chrome/browser/dom_ui/dom_ui_factory.h"
[email protected]bcef0dc02009-02-28 00:35:0229#include "chrome/browser/download/download_item_model.h"
[email protected]420ae012009-04-24 05:16:3230#include "chrome/browser/download/download_manager.h"
31#include "chrome/browser/download/download_request_manager.h"
[email protected]e7eaedde2009-09-25 20:09:4932#include "chrome/browser/external_protocol_handler.h"
[email protected]92c6f9b92009-10-24 04:35:0833#include "chrome/browser/extensions/extensions_service.h"
[email protected]0189bc722009-08-28 21:56:4834#include "chrome/browser/favicon_service.h"
[email protected]420ae012009-04-24 05:16:3235#include "chrome/browser/google_util.h"
[email protected]79580c62010-02-02 02:36:2536#include "chrome/browser/host_content_settings_map.h"
[email protected]8897c382009-05-06 17:53:2637#include "chrome/browser/hung_renderer_dialog.h"
[email protected]12f74a92010-02-05 22:32:1438#include "chrome/browser/message_box_handler.h"
[email protected]420ae012009-04-24 05:16:3239#include "chrome/browser/load_from_memory_cache_details.h"
40#include "chrome/browser/load_notification_details.h"
[email protected]84abba62009-10-07 17:01:4441#include "chrome/browser/metrics/metric_event_duration_details.h"
[email protected]ea0e80892009-08-22 00:36:0542#include "chrome/browser/modal_html_dialog_delegate.h"
[email protected]c8865482009-07-23 20:40:1043#include "chrome/browser/omnibox_search_hint.h"
[email protected]fdd61c62009-04-22 19:22:5744#include "chrome/browser/password_manager/password_manager.h"
45#include "chrome/browser/plugin_installer.h"
[email protected]052313b2010-02-19 09:43:0846#include "chrome/browser/pref_service.h"
[email protected]ce560f82009-06-03 09:39:4447#include "chrome/browser/profile.h"
[email protected]1db6ff152009-10-12 15:32:0748#include "chrome/browser/renderer_host/render_process_host.h"
[email protected]8cb5d5b2010-02-09 11:36:1649#include "chrome/browser/renderer_host/render_view_host.h"
[email protected]96d185d2009-04-24 03:28:5450#include "chrome/browser/renderer_host/render_widget_host_view.h"
[email protected]fd694982009-07-03 17:32:3751#include "chrome/browser/renderer_host/resource_request_details.h"
[email protected]1db6ff152009-10-12 15:32:0752#include "chrome/browser/renderer_host/site_instance.h"
[email protected]d4a00a72010-01-29 01:44:4253#include "chrome/browser/renderer_host/translation_service.h"
[email protected]96d185d2009-04-24 03:28:5454#include "chrome/browser/renderer_host/web_cache_manager.h"
[email protected]93623c5d2009-12-10 21:40:3255#include "chrome/browser/renderer_preferences_util.h"
[email protected]152b8ba32010-03-29 19:40:1656#include "chrome/browser/search_engines/template_url_fetcher.h"
57#include "chrome/browser/search_engines/template_url_model.h"
[email protected]c37e3b62010-01-22 00:15:1758#include "chrome/browser/sessions/session_types.h"
[email protected]bd1ad682009-05-15 22:19:1759#include "chrome/browser/tab_contents/infobar_delegate.h"
[email protected]7e383692009-06-12 19:14:5460#include "chrome/browser/tab_contents/interstitial_page.h"
[email protected]f3ec7742009-01-15 00:59:1661#include "chrome/browser/tab_contents/navigation_entry.h"
[email protected]de34a962009-06-26 23:22:4362#include "chrome/browser/tab_contents/provisional_load_details.h"
[email protected]f3ec7742009-01-15 00:59:1663#include "chrome/browser/tab_contents/tab_contents_delegate.h"
[email protected]fdd61c62009-04-22 19:22:5764#include "chrome/browser/tab_contents/tab_contents_view.h"
[email protected]58dca552009-06-17 00:35:0265#include "chrome/browser/tab_contents/thumbnail_generator.h"
[email protected]449478302009-06-09 20:04:2866#include "chrome/browser/thumbnail_store.h"
[email protected]152b8ba32010-03-29 19:40:1667#include "chrome/browser/translate/page_translated_details.h"
[email protected]35f7d212009-04-29 21:19:2768#include "chrome/common/chrome_switches.h"
[email protected]3b355642010-02-05 16:01:4969#include "chrome/common/extensions/extension.h"
[email protected]5d246db22009-10-27 06:17:5770#include "chrome/common/extensions/extension_action.h"
[email protected]bfd04a62009-02-01 18:16:5671#include "chrome/common/notification_service.h"
[email protected]89d6e6e2009-12-03 23:06:5172#include "chrome/common/platform_util.h"
initial.commit09911bf2008-07-26 23:55:2973#include "chrome/common/pref_names.h"
[email protected]420ae012009-04-24 05:16:3274#include "chrome/common/render_messages.h"
[email protected]1ee614862009-07-06 20:29:1875#include "chrome/common/renderer_preferences.h"
[email protected]96d185d2009-04-24 03:28:5476#include "chrome/common/url_constants.h"
[email protected]8897c382009-05-06 17:53:2677#include "grit/generated_resources.h"
[email protected]420ae012009-04-24 05:16:3278#include "grit/locale_settings.h"
[email protected]fd626782010-01-22 05:26:0679#include "grit/platform_locale_settings.h"
[email protected]420ae012009-04-24 05:16:3280#include "net/base/mime_util.h"
81#include "net/base/net_errors.h"
[email protected]d686e812009-06-03 19:10:2982#include "net/base/net_util.h"
[email protected]420ae012009-04-24 05:16:3283#include "net/base/registry_controlled_domain.h"
[email protected]1ee614862009-07-06 20:29:1884#include "webkit/glue/password_form.h"
85#include "webkit/glue/webpreferences.h"
[email protected]3e45ba92009-02-20 21:09:0086
[email protected]304f8792009-08-19 21:28:0987#if defined(OS_CHROMEOS)
88// For GdkScreen
89#include <gdk/gdk.h>
[email protected]0ba5c5e2009-08-20 16:47:1390#endif // defined(OS_CHROMEOS)
91
[email protected]420ae012009-04-24 05:16:3292// Cross-Site Navigations
93//
94// If a TabContents is told to navigate to a different web site (as determined
95// by SiteInstance), it will replace its current RenderViewHost with a new
96// RenderViewHost dedicated to the new SiteInstance. This works as follows:
97//
98// - Navigate determines whether the destination is cross-site, and if so,
99// it creates a pending_render_view_host_ and moves into the PENDING
100// RendererState.
101// - The pending RVH is "suspended," so that no navigation messages are sent to
102// its renderer until the onbeforeunload JavaScript handler has a chance to
103// run in the current RVH.
104// - The pending RVH tells CrossSiteRequestManager (a thread-safe singleton)
105// that it has a pending cross-site request. ResourceDispatcherHost will
106// check for this when the response arrives.
107// - The current RVH runs its onbeforeunload handler. If it returns false, we
108// cancel all the pending logic and go back to NORMAL. Otherwise we allow
109// the pending RVH to send the navigation request to its renderer.
110// - ResourceDispatcherHost receives a ResourceRequest on the IO thread. It
111// checks CrossSiteRequestManager to see that the RVH responsible has a
112// pending cross-site request, and then installs a CrossSiteEventHandler.
113// - When RDH receives a response, the BufferedEventHandler determines whether
114// it is a download. If so, it sends a message to the new renderer causing
115// it to cancel the request, and the download proceeds in the download
116// thread. For now, we stay in a PENDING state (with a pending RVH) until
[email protected]57c6a652009-05-04 07:58:34117// the next DidNavigate event for this TabContents. This isn't ideal, but it
[email protected]420ae012009-04-24 05:16:32118// doesn't affect any functionality.
119// - After RDH receives a response and determines that it is safe and not a
120// download, it pauses the response to first run the old page's onunload
121// handler. It does this by asynchronously calling the OnCrossSiteResponse
[email protected]57c6a652009-05-04 07:58:34122// method of TabContents on the UI thread, which sends a ClosePage message
[email protected]420ae012009-04-24 05:16:32123// to the current RVH.
124// - Once the onunload handler is finished, a ClosePage_ACK message is sent to
125// the ResourceDispatcherHost, who unpauses the response. Data is then sent
126// to the pending RVH.
127// - The pending renderer sends a FrameNavigate message that invokes the
128// DidNavigate method. This replaces the current RVH with the
129// pending RVH and goes back to the NORMAL RendererState.
130
131namespace {
132
133// Amount of time we wait between when a key event is received and the renderer
134// is queried for its state and pushed to the NavigationEntry.
135const int kQueryStateDelay = 5000;
136
137const int kSyncWaitDelay = 40;
138
[email protected]304f8792009-08-19 21:28:09139#if defined(OS_CHROMEOS)
140// If a popup window is bigger than this fraction of the screen on chrome os,
141// turn it into a tab
142const float kMaxWidthFactor = 0.5;
143const float kMaxHeightFactor = 0.6;
144#endif
145
[email protected]420ae012009-04-24 05:16:32146// If another javascript message box is displayed within
147// kJavascriptMessageExpectedDelay of a previous javascript message box being
148// dismissed, display an option to suppress future message boxes from this
149// contents.
150const int kJavascriptMessageExpectedDelay = 1000;
151
[email protected]420ae012009-04-24 05:16:32152// The list of prefs we want to observe.
153const wchar_t* kPrefsToObserve[] = {
154 prefs::kAlternateErrorPagesEnabled,
155 prefs::kWebKitJavaEnabled,
156 prefs::kWebKitJavascriptEnabled,
157 prefs::kWebKitLoadsImagesAutomatically,
158 prefs::kWebKitPluginsEnabled,
159 prefs::kWebKitUsesUniversalDetector,
160 prefs::kWebKitSerifFontFamily,
161 prefs::kWebKitSansSerifFontFamily,
162 prefs::kWebKitFixedFontFamily,
163 prefs::kWebKitDefaultFontSize,
164 prefs::kWebKitDefaultFixedFontSize,
165 prefs::kDefaultCharset
166 // kWebKitStandardFontIsSerif needs to be added
167 // if we let users pick which font to use, serif or sans-serif when
168 // no font is specified or a CSS generic family (serif or sans-serif)
169 // is not specified.
170};
171
172const int kPrefsToObserveLength = arraysize(kPrefsToObserve);
173
[email protected]420ae012009-04-24 05:16:32174// Returns true if the entry's transition type is FORM_SUBMIT.
175bool IsFormSubmit(const NavigationEntry* entry) {
176 return (PageTransition::StripQualifier(entry->transition_type()) ==
177 PageTransition::FORM_SUBMIT);
178}
179
180#if defined(OS_WIN)
181
182BOOL CALLBACK InvalidateWindow(HWND hwnd, LPARAM lparam) {
183 // Note: erase is required to properly paint some widgets borders. This can
184 // be seen with textfields.
185 InvalidateRect(hwnd, NULL, TRUE);
186 return TRUE;
187}
188#endif
189
[email protected]5e369672009-11-03 23:48:30190ViewMsg_Navigate_Params::NavigationType GetNavigationType(
[email protected]1ccb3568d2010-02-19 10:51:16191 Profile* profile, const NavigationEntry& entry,
192 NavigationController::ReloadType reload_type) {
193 switch (reload_type) {
194 case NavigationController::RELOAD:
195 return ViewMsg_Navigate_Params::RELOAD;
196 case NavigationController::RELOAD_IGNORING_CACHE:
197 return ViewMsg_Navigate_Params::RELOAD_IGNORING_CACHE;
198 case NavigationController::NO_RELOAD:
199 break; // Fall through to rest of function.
200 }
[email protected]5e369672009-11-03 23:48:30201
202 if (entry.restore_type() == NavigationEntry::RESTORE_LAST_SESSION &&
203 profile->DidLastSessionExitCleanly())
204 return ViewMsg_Navigate_Params::RESTORE;
205
206 return ViewMsg_Navigate_Params::NORMAL;
207}
208
[email protected]3cc72b12010-03-18 23:03:00209void MakeNavigateParams(const NavigationController& controller,
[email protected]1ccb3568d2010-02-19 10:51:16210 NavigationController::ReloadType reload_type,
211 ViewMsg_Navigate_Params* params) {
[email protected]3cc72b12010-03-18 23:03:00212 const NavigationEntry& entry = *controller.pending_entry();
[email protected]056de2d2009-06-26 16:41:34213 params->page_id = entry.page_id();
[email protected]3cc72b12010-03-18 23:03:00214 params->pending_history_list_offset = controller.pending_entry_index();
215 params->current_history_list_offset = controller.last_committed_entry_index();
216 params->current_history_list_length = controller.entry_count();
[email protected]056de2d2009-06-26 16:41:34217 params->url = entry.url();
218 params->referrer = entry.referrer();
219 params->transition = entry.transition_type();
220 params->state = entry.content_state();
[email protected]3cc72b12010-03-18 23:03:00221 params->navigation_type =
222 GetNavigationType(controller.profile(), entry, reload_type);
[email protected]056de2d2009-06-26 16:41:34223 params->request_time = base::Time::Now();
224}
225
[email protected]420ae012009-04-24 05:16:32226} // namespace
227
228// -----------------------------------------------------------------------------
229
[email protected]fdd61c62009-04-22 19:22:57230// static
231int TabContents::find_request_id_counter_ = -1;
232
[email protected]420ae012009-04-24 05:16:32233
[email protected]420ae012009-04-24 05:16:32234TabContents::TabContents(Profile* profile,
235 SiteInstance* site_instance,
236 int routing_id,
[email protected]34ac70502009-09-25 17:07:23237 const TabContents* base_tab_contents)
[email protected]b680ad22009-04-15 23:19:42238 : delegate_(NULL),
[email protected]66ba4932009-06-04 19:22:13239 ALLOW_THIS_IN_INITIALIZER_LIST(controller_(this, profile)),
240 ALLOW_THIS_IN_INITIALIZER_LIST(view_(
[email protected]d82ed61e2009-06-16 02:46:22241 TabContentsView::Create(this))),
242 ALLOW_THIS_IN_INITIALIZER_LIST(render_manager_(this, this)),
[email protected]fdd61c62009-04-22 19:22:57243 property_bag_(),
244 registrar_(),
[email protected]d82ed61e2009-06-16 02:46:22245 ALLOW_THIS_IN_INITIALIZER_LIST(printing_(*this)),
[email protected]fdd61c62009-04-22 19:22:57246 save_package_(),
[email protected]6db0def92010-03-02 19:55:45247 autocomplete_history_manager_(),
[email protected]21674df2009-10-28 19:51:21248 autofill_manager_(),
[email protected]fdd61c62009-04-22 19:22:57249 password_manager_(),
250 plugin_installer_(),
[email protected]6f821dd2010-02-23 00:54:06251 bookmark_drag_(NULL),
[email protected]d82ed61e2009-06-16 02:46:22252 ALLOW_THIS_IN_INITIALIZER_LIST(fav_icon_helper_(this)),
[email protected]fdd61c62009-04-22 19:22:57253 select_file_dialog_(),
[email protected]d5f942ba2008-09-26 19:30:34254 is_loading_(false),
initial.commit09911bf2008-07-26 23:55:29255 is_crashed_(false),
[email protected]d5f942ba2008-09-26 19:30:34256 waiting_for_response_(false),
[email protected]d5f942ba2008-09-26 19:30:34257 max_page_id_(-1),
[email protected]fdd61c62009-04-22 19:22:57258 current_load_start_(),
259 load_state_(net::LOAD_STATE_IDLE),
260 load_state_host_(),
[email protected]094e5b22009-09-25 04:23:56261 upload_size_(0),
262 upload_position_(0),
[email protected]fdd61c62009-04-22 19:22:57263 received_page_title_(false),
264 is_starred_(false),
265 contents_mime_type_(),
266 encoding_(),
[email protected]332af7732009-03-11 13:21:35267 blocked_popups_(NULL),
[email protected]8cc8d492010-02-02 10:40:49268 dont_notify_render_view_(false),
[email protected]fdd61c62009-04-22 19:22:57269 infobar_delegates_(),
[email protected]fdd61c62009-04-22 19:22:57270 find_ui_active_(false),
271 find_op_aborted_(false),
272 current_find_request_id_(find_request_id_counter_++),
[email protected]e491f1c2009-05-22 20:28:12273 last_search_case_sensitive_(false),
274 last_search_prepopulate_text_(NULL),
275 last_search_result_(),
[email protected]a888b29e62010-04-01 13:38:57276 app_extension_(NULL),
277 app_extension_for_current_page_(NULL),
[email protected]fdd61c62009-04-22 19:22:57278 capturing_contents_(false),
279 is_being_destroyed_(false),
280 notify_disconnection_(false),
281 history_requests_(),
282#if defined(OS_WIN)
283 message_box_active_(CreateEvent(NULL, TRUE, FALSE, NULL)),
284#endif
285 last_javascript_message_dismissal_(),
[email protected]c2e74fe82009-09-03 17:57:44286 suppress_javascript_messages_(false),
[email protected]7ab1e7d62009-10-14 23:32:01287 is_showing_before_unload_dialog_(false),
[email protected]7ff431e2010-01-07 18:18:54288 renderer_preferences_(),
[email protected]1a242c32010-02-02 21:20:54289 opener_dom_ui_type_(DOMUIFactory::kNoDOMUI),
[email protected]3b1c1812010-02-12 21:53:55290 language_state_(&controller_) {
[email protected]0de80162010-02-03 04:52:35291 ClearBlockedContentSettings();
[email protected]7ff431e2010-01-07 18:18:54292 renderer_preferences_util::UpdateFromSystemSettings(
293 &renderer_preferences_, profile);
294
[email protected]16779842009-07-08 23:45:29295#if defined(OS_CHROMEOS)
[email protected]58dca552009-06-17 00:35:02296 // Make sure the thumbnailer is started before starting the render manager.
297 // The thumbnailer will want to listen for RVH creations, one of which will
298 // happen in RVHManager::Init.
299 ThumbnailGenerator* generator = g_browser_process->GetThumbnailGenerator();
300 if (generator)
301 generator->StartThumbnailing();
302#endif
303
[email protected]12636df2009-09-28 22:32:21304 render_manager_.Init(profile, site_instance, routing_id);
[email protected]420ae012009-04-24 05:16:32305
[email protected]34ac70502009-09-25 17:07:23306 // We have the initial size of the view be based on the size of the passed in
307 // tab contents (normally a tab from the same window).
308 view_->CreateView(base_tab_contents ?
309 base_tab_contents->view()->GetContainerSize() : gfx::Size());
[email protected]420ae012009-04-24 05:16:32310
311 // Register for notifications about all interested prefs change.
312 PrefService* prefs = profile->GetPrefs();
313 if (prefs) {
314 for (int i = 0; i < kPrefsToObserveLength; ++i)
315 prefs->AddPrefObserver(kPrefsToObserve[i], this);
316 }
317
318 // Register for notifications about URL starredness changing on any profile.
319 registrar_.Add(this, NotificationType::URLS_STARRED,
320 NotificationService::AllSources());
321 registrar_.Add(this, NotificationType::BOOKMARK_MODEL_LOADED,
322 NotificationService::AllSources());
323 registrar_.Add(this, NotificationType::RENDER_WIDGET_HOST_DESTROYED,
324 NotificationService::AllSources());
[email protected]d3ba77272009-09-03 00:06:09325#if defined(OS_LINUX)
326 registrar_.Add(this, NotificationType::BROWSER_THEME_CHANGED,
327 NotificationService::AllSources());
328#endif
[email protected]420ae012009-04-24 05:16:32329
[email protected]6c32ce72010-03-08 05:18:06330 registrar_.Add(this, NotificationType::USER_STYLE_SHEET_UPDATED,
331 NotificationService::AllSources());
332
[email protected]79580c62010-02-02 02:36:25333 // Register for notifications about content setting changes.
334 registrar_.Add(this, NotificationType::CONTENT_SETTINGS_CHANGED,
335 NotificationService::AllSources());
336
[email protected]a888b29e62010-04-01 13:38:57337 // Listen for extension changes so we can update extension_for_current_page_.
338 registrar_.Add(this, NotificationType::EXTENSION_LOADED,
339 NotificationService::AllSources());
340 registrar_.Add(this, NotificationType::EXTENSION_UNLOADED,
341 NotificationService::AllSources());
342 registrar_.Add(this, NotificationType::EXTENSION_UNLOADED_DISABLED,
343 NotificationService::AllSources());
344
[email protected]420ae012009-04-24 05:16:32345 // Keep a global copy of the previous search string (if any).
346 static string16 global_last_search = string16();
[email protected]e491f1c2009-05-22 20:28:12347 last_search_prepopulate_text_ = &global_last_search;
[email protected]840b1512009-07-21 16:53:46348
[email protected]c8865482009-07-23 20:40:10349 // Set-up the showing of the omnibox search infobar if applicable.
350 if (OmniboxSearchHint::IsEnabled(profile))
351 omnibox_search_hint_.reset(new OmniboxSearchHint(this));
[email protected]332af7732009-03-11 13:21:35352}
initial.commit09911bf2008-07-26 23:55:29353
354TabContents::~TabContents() {
[email protected]420ae012009-04-24 05:16:32355 is_being_destroyed_ = true;
356
[email protected]bfe4c1582009-10-02 18:11:09357 // We don't want any notifications while we're running our destructor.
[email protected]420ae012009-04-24 05:16:32358 registrar_.RemoveAll();
359
360 // Unregister the notifications of all observed prefs change.
361 PrefService* prefs = profile()->GetPrefs();
362 if (prefs) {
363 for (int i = 0; i < kPrefsToObserveLength; ++i)
364 prefs->RemovePrefObserver(kPrefsToObserve[i], this);
365 }
366
[email protected]420ae012009-04-24 05:16:32367 NotifyDisconnected();
[email protected]e7b8d832009-11-20 22:41:00368 hung_renderer_dialog::HideForTabContents(this);
[email protected]420ae012009-04-24 05:16:32369
[email protected]420ae012009-04-24 05:16:32370 // First cleanly close all child windows.
371 // TODO(mpcomplete): handle case if MaybeCloseChildWindows() already asked
372 // some of these to close. CloseWindows is async, so it might get called
373 // twice before it runs.
[email protected]8cc8d492010-02-02 10:40:49374 CloseConstrainedWindows();
[email protected]420ae012009-04-24 05:16:32375
[email protected]8cc8d492010-02-02 10:40:49376 // Close all blocked popups.
[email protected]66ba4932009-06-04 19:22:13377 if (blocked_popups_)
378 blocked_popups_->Destroy();
379
[email protected]420ae012009-04-24 05:16:32380 // Notify any observer that have a reference on this tab contents.
381 NotificationService::current()->Notify(
382 NotificationType::TAB_CONTENTS_DESTROYED,
383 Source<TabContents>(this),
384 NotificationService::NoDetails());
385
[email protected]1e5e74f2009-05-27 20:55:12386 // Notify any lasting InfobarDelegates that have not yet been removed that
387 // whatever infobar they were handling in this TabContents has closed,
388 // because the TabContents is going away entirely.
389 // This must happen after the TAB_CONTENTS_DESTROYED notification as the
390 // notification may trigger infobars calls that access their delegate. (and
391 // some implementations of InfoBarDelegate do delete themselves on
392 // InfoBarClosed()).
393 for (int i = 0; i < infobar_delegate_count(); ++i) {
394 InfoBarDelegate* delegate = GetInfoBarDelegateAt(i);
395 delegate->InfoBarClosed();
396 }
397 infobar_delegates_.clear();
398
[email protected]420ae012009-04-24 05:16:32399 // TODO(brettw) this should be moved to the view.
400#if defined(OS_WIN)
401 // If we still have a window handle, destroy it. GetNativeView can return
402 // NULL if this contents was part of a window that closed.
403 if (GetNativeView())
404 ::DestroyWindow(GetNativeView());
405#endif
[email protected]7ab1e7d62009-10-14 23:32:01406
407 // OnCloseStarted isn't called in unit tests.
408 if (!tab_close_start_time_.is_null()) {
409 UMA_HISTOGRAM_TIMES("Tab.Close",
410 base::TimeTicks::Now() - tab_close_start_time_);
411 }
initial.commit09911bf2008-07-26 23:55:29412}
413
[email protected]d5f942ba2008-09-26 19:30:34414// static
415void TabContents::RegisterUserPrefs(PrefService* prefs) {
[email protected]420ae012009-04-24 05:16:32416 prefs->RegisterBooleanPref(prefs::kAlternateErrorPagesEnabled, true);
417
418 WebPreferences pref_defaults;
419 prefs->RegisterBooleanPref(prefs::kWebKitJavascriptEnabled,
420 pref_defaults.javascript_enabled);
421 prefs->RegisterBooleanPref(prefs::kWebKitWebSecurityEnabled,
422 pref_defaults.web_security_enabled);
423 prefs->RegisterBooleanPref(
424 prefs::kWebKitJavascriptCanOpenWindowsAutomatically, true);
425 prefs->RegisterBooleanPref(prefs::kWebKitLoadsImagesAutomatically,
426 pref_defaults.loads_images_automatically);
427 prefs->RegisterBooleanPref(prefs::kWebKitPluginsEnabled,
428 pref_defaults.plugins_enabled);
429 prefs->RegisterBooleanPref(prefs::kWebKitDomPasteEnabled,
430 pref_defaults.dom_paste_enabled);
431 prefs->RegisterBooleanPref(prefs::kWebKitShrinksStandaloneImagesToFit,
432 pref_defaults.shrinks_standalone_images_to_fit);
[email protected]0df30122009-06-03 12:13:08433 prefs->RegisterStringPref(prefs::kWebKitInspectorSettings,
[email protected]8922e1f2009-10-03 05:01:26434 UTF8ToWide(pref_defaults.inspector_settings));
[email protected]420ae012009-04-24 05:16:32435 prefs->RegisterBooleanPref(prefs::kWebKitTextAreasAreResizable,
436 pref_defaults.text_areas_are_resizable);
437 prefs->RegisterBooleanPref(prefs::kWebKitJavaEnabled,
438 pref_defaults.java_enabled);
439
440 prefs->RegisterLocalizedStringPref(prefs::kAcceptLanguages,
441 IDS_ACCEPT_LANGUAGES);
442 prefs->RegisterLocalizedStringPref(prefs::kDefaultCharset,
443 IDS_DEFAULT_ENCODING);
444 prefs->RegisterLocalizedBooleanPref(prefs::kWebKitStandardFontIsSerif,
445 IDS_STANDARD_FONT_IS_SERIF);
446 prefs->RegisterLocalizedStringPref(prefs::kWebKitFixedFontFamily,
447 IDS_FIXED_FONT_FAMILY);
448 prefs->RegisterLocalizedStringPref(prefs::kWebKitSerifFontFamily,
449 IDS_SERIF_FONT_FAMILY);
450 prefs->RegisterLocalizedStringPref(prefs::kWebKitSansSerifFontFamily,
451 IDS_SANS_SERIF_FONT_FAMILY);
452 prefs->RegisterLocalizedStringPref(prefs::kWebKitCursiveFontFamily,
453 IDS_CURSIVE_FONT_FAMILY);
454 prefs->RegisterLocalizedStringPref(prefs::kWebKitFantasyFontFamily,
455 IDS_FANTASY_FONT_FAMILY);
456 prefs->RegisterLocalizedIntegerPref(prefs::kWebKitDefaultFontSize,
457 IDS_DEFAULT_FONT_SIZE);
458 prefs->RegisterLocalizedIntegerPref(prefs::kWebKitDefaultFixedFontSize,
459 IDS_DEFAULT_FIXED_FONT_SIZE);
460 prefs->RegisterLocalizedIntegerPref(prefs::kWebKitMinimumFontSize,
461 IDS_MINIMUM_FONT_SIZE);
462 prefs->RegisterLocalizedIntegerPref(prefs::kWebKitMinimumLogicalFontSize,
463 IDS_MINIMUM_LOGICAL_FONT_SIZE);
464 prefs->RegisterLocalizedBooleanPref(prefs::kWebKitUsesUniversalDetector,
465 IDS_USES_UNIVERSAL_DETECTOR);
466 prefs->RegisterLocalizedStringPref(prefs::kStaticEncodings,
467 IDS_STATIC_ENCODING_LIST);
initial.commit09911bf2008-07-26 23:55:29468}
469
[email protected]13367f72009-07-08 16:39:02470// Returns true if contains content rendered by an extension.
471bool TabContents::HostsExtension() const {
472 return GetURL().SchemeIs(chrome::kExtensionScheme);
[email protected]d5f942ba2008-09-26 19:30:34473}
474
[email protected]6e95706d2009-04-23 22:55:01475PasswordManager* TabContents::GetPasswordManager() {
476 if (password_manager_.get() == NULL)
477 password_manager_.reset(new PasswordManager(this));
478 return password_manager_.get();
479}
480
481PluginInstaller* TabContents::GetPluginInstaller() {
482 if (plugin_installer_.get() == NULL)
483 plugin_installer_.reset(new PluginInstaller(this));
484 return plugin_installer_.get();
485}
486
[email protected]8cb5d5b2010-02-09 11:36:16487RenderProcessHost* TabContents::GetRenderProcessHost() const {
488 return render_manager_.current_host()->process();
489}
490
[email protected]3b355642010-02-05 16:01:49491void TabContents::SetAppExtension(Extension* extension) {
[email protected]867a73e12010-03-19 20:45:46492 DCHECK(!extension || extension->GetFullLaunchURL().is_valid());
[email protected]3b355642010-02-05 16:01:49493 app_extension_ = extension;
[email protected]fca656c2010-02-10 20:30:10494
[email protected]a888b29e62010-04-01 13:38:57495 UpdateAppExtensionIcon(app_extension_);
496
[email protected]fca656c2010-02-10 20:30:10497 NotificationService::current()->Notify(
498 NotificationType::TAB_CONTENTS_APPLICATION_EXTENSION_CHANGED,
499 Source<TabContents>(this),
500 NotificationService::NoDetails());
[email protected]3b355642010-02-05 16:01:49501}
502
[email protected]911f8152010-03-18 16:46:40503void TabContents::SetAppExtensionById(const std::string& app_extension_id) {
504 if (app_extension_id.empty())
505 return;
506
507 ExtensionsService* extension_service = profile()->GetExtensionsService();
508 if (extension_service && extension_service->is_ready()) {
509 Extension* extension =
510 extension_service->GetExtensionById(app_extension_id, false);
511 if (extension)
512 SetAppExtension(extension);
513 }
514}
515
[email protected]d5f942ba2008-09-26 19:30:34516const GURL& TabContents::GetURL() const {
517 // We may not have a navigation entry yet
[email protected]ce3fa3c2009-04-20 19:55:57518 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]ebe89e062009-08-13 23:16:54519 return entry ? entry->virtual_url() : GURL::EmptyGURL();
[email protected]d5f942ba2008-09-26 19:30:34520}
521
[email protected]96d185d2009-04-24 03:28:54522const string16& TabContents::GetTitle() const {
[email protected]4c6092c5b2009-06-06 00:23:55523 // Transient entries take precedence. They are used for interstitial pages
524 // that are shown on top of existing pages.
525 NavigationEntry* entry = controller_.GetTransientEntry();
526 if (entry)
527 return entry->GetTitleForDisplay(&controller_);
528
[email protected]96d185d2009-04-24 03:28:54529 DOMUI* our_dom_ui = render_manager_.pending_dom_ui() ?
530 render_manager_.pending_dom_ui() : render_manager_.dom_ui();
531 if (our_dom_ui) {
532 // Don't override the title in view source mode.
[email protected]4c6092c5b2009-06-06 00:23:55533 entry = controller_.GetActiveEntry();
[email protected]96d185d2009-04-24 03:28:54534 if (!(entry && entry->IsViewSourceMode())) {
535 // Give the DOM UI the chance to override our title.
536 const string16& title = our_dom_ui->overridden_title();
537 if (!title.empty())
538 return title;
539 }
540 }
541
542 // We use the title for the last committed entry rather than a pending
543 // navigation entry. For example, when the user types in a URL, we want to
544 // keep the old page's title until the new load has committed and we get a new
545 // title.
[email protected]96d185d2009-04-24 03:28:54546 entry = controller_.GetLastCommittedEntry();
547 if (entry)
548 return entry->GetTitleForDisplay(&controller_);
[email protected]96d185d2009-04-24 03:28:54549 return EmptyString16();
550}
551
[email protected]bbbc1ef2010-02-12 18:03:41552// static
553string16 TabContents::GetDefaultTitle() {
554 return l10n_util::GetStringUTF16(IDS_DEFAULT_TAB_TITLE);
555}
556
[email protected]d5f942ba2008-09-26 19:30:34557int32 TabContents::GetMaxPageID() {
558 if (GetSiteInstance())
559 return GetSiteInstance()->max_page_id();
560 else
561 return max_page_id_;
562}
563
564void TabContents::UpdateMaxPageID(int32 page_id) {
565 // Ensure both the SiteInstance and RenderProcessHost update their max page
[email protected]57c6a652009-05-04 07:58:34566 // IDs in sync. Only TabContents will also have site instances, except during
[email protected]d5f942ba2008-09-26 19:30:34567 // testing.
568 if (GetSiteInstance())
569 GetSiteInstance()->UpdateMaxPageID(page_id);
[email protected]8cb5d5b2010-02-09 11:36:16570 GetRenderProcessHost()->UpdateMaxPageID(page_id);
[email protected]d5f942ba2008-09-26 19:30:34571}
572
[email protected]96d185d2009-04-24 03:28:54573SiteInstance* TabContents::GetSiteInstance() const {
574 return render_manager_.current_host()->site_instance();
575}
576
[email protected]96d185d2009-04-24 03:28:54577bool TabContents::ShouldDisplayURL() {
[email protected]8ab24cc2009-11-13 20:56:15578 // Don't hide the url in view source mode and with interstitials.
[email protected]96d185d2009-04-24 03:28:54579 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]8ab24cc2009-11-13 20:56:15580 if (entry && (entry->IsViewSourceMode() ||
581 entry->page_type() == NavigationEntry::INTERSTITIAL_PAGE)) {
[email protected]96d185d2009-04-24 03:28:54582 return true;
[email protected]8ab24cc2009-11-13 20:56:15583 }
[email protected]83e3895b2009-06-11 00:07:16584
[email protected]96d185d2009-04-24 03:28:54585 DOMUI* dom_ui = GetDOMUIForCurrentState();
586 if (dom_ui)
587 return !dom_ui->should_hide_url();
588 return true;
589}
590
[email protected]d5f942ba2008-09-26 19:30:34591SkBitmap TabContents::GetFavIcon() const {
592 // Like GetTitle(), we also want to use the favicon for the last committed
593 // entry rather than a pending navigation entry.
[email protected]ce3fa3c2009-04-20 19:55:57594 NavigationEntry* entry = controller_.GetTransientEntry();
[email protected]cbab76d2008-10-13 22:42:47595 if (entry)
596 return entry->favicon().bitmap();
597
[email protected]ce3fa3c2009-04-20 19:55:57598 entry = controller_.GetLastCommittedEntry();
[email protected]d5f942ba2008-09-26 19:30:34599 if (entry)
600 return entry->favicon().bitmap();
[email protected]d5f942ba2008-09-26 19:30:34601 return SkBitmap();
602}
603
[email protected]0b303cc2009-09-28 22:35:15604bool TabContents::FavIconIsValid() const {
605 NavigationEntry* entry = controller_.GetTransientEntry();
606 if (entry)
607 return entry->favicon().is_valid();
608
609 entry = controller_.GetLastCommittedEntry();
610 if (entry)
611 return entry->favicon().is_valid();
612
613 return false;
614}
615
[email protected]96d185d2009-04-24 03:28:54616bool TabContents::ShouldDisplayFavIcon() {
617 // Always display a throbber during pending loads.
618 if (controller_.GetLastCommittedEntry() && controller_.pending_entry())
619 return true;
620
621 DOMUI* dom_ui = GetDOMUIForCurrentState();
622 if (dom_ui)
623 return !dom_ui->hide_favicon();
624 return true;
625}
626
[email protected]2d5a3592010-01-30 04:29:52627bool TabContents::IsContentBlocked(ContentSettingsType content_type) const {
[email protected]8cc8d492010-02-02 10:40:49628 if (content_type == CONTENT_SETTINGS_TYPE_POPUPS)
629 return blocked_popups_ != NULL;
630
[email protected]0de80162010-02-03 04:52:35631 if (content_type == CONTENT_SETTINGS_TYPE_IMAGES ||
632 content_type == CONTENT_SETTINGS_TYPE_JAVASCRIPT ||
[email protected]3dbb80b2010-02-09 22:41:20633 content_type == CONTENT_SETTINGS_TYPE_PLUGINS ||
634 content_type == CONTENT_SETTINGS_TYPE_COOKIES)
[email protected]0de80162010-02-03 04:52:35635 return content_blocked_[content_type];
636
[email protected]b1235062010-03-25 17:31:27637 // TODO(joth): remove once fully implemented.
638 if (content_type == CONTENT_SETTINGS_TYPE_GEOLOCATION)
639 return false;
640
[email protected]3dbb80b2010-02-09 22:41:20641 NOTREACHED();
[email protected]2d5a3592010-01-30 04:29:52642 return false;
643}
644
[email protected]96d185d2009-04-24 03:28:54645std::wstring TabContents::GetStatusText() const {
646 if (!is_loading() || load_state_ == net::LOAD_STATE_IDLE)
647 return std::wstring();
648
649 switch (load_state_) {
650 case net::LOAD_STATE_WAITING_FOR_CACHE:
651 return l10n_util::GetString(IDS_LOAD_STATE_WAITING_FOR_CACHE);
652 case net::LOAD_STATE_RESOLVING_PROXY_FOR_URL:
653 return l10n_util::GetString(IDS_LOAD_STATE_RESOLVING_PROXY_FOR_URL);
654 case net::LOAD_STATE_RESOLVING_HOST:
655 return l10n_util::GetString(IDS_LOAD_STATE_RESOLVING_HOST);
656 case net::LOAD_STATE_CONNECTING:
657 return l10n_util::GetString(IDS_LOAD_STATE_CONNECTING);
658 case net::LOAD_STATE_SENDING_REQUEST:
[email protected]094e5b22009-09-25 04:23:56659 if (upload_size_)
660 return l10n_util::GetStringF(
661 IDS_LOAD_STATE_SENDING_REQUEST_WITH_PROGRESS,
662 static_cast<int>((100 * upload_position_) / upload_size_));
663 else
664 return l10n_util::GetString(IDS_LOAD_STATE_SENDING_REQUEST);
[email protected]96d185d2009-04-24 03:28:54665 case net::LOAD_STATE_WAITING_FOR_RESPONSE:
666 return l10n_util::GetStringF(IDS_LOAD_STATE_WAITING_FOR_RESPONSE,
667 load_state_host_);
668 // Ignore net::LOAD_STATE_READING_RESPONSE and net::LOAD_STATE_IDLE
669 case net::LOAD_STATE_IDLE:
670 case net::LOAD_STATE_READING_RESPONSE:
671 break;
672 }
673
674 return std::wstring();
675}
676
[email protected]d5f942ba2008-09-26 19:30:34677void TabContents::SetIsCrashed(bool state) {
678 if (state == is_crashed_)
679 return;
680
681 is_crashed_ = state;
[email protected]c9cd2222009-05-06 05:16:50682 NotifyNavigationStateChanged(INVALIDATE_TAB);
[email protected]d5f942ba2008-09-26 19:30:34683}
684
[email protected]744ef172009-10-16 21:53:46685void TabContents::PageActionStateChanged() {
686 NotifyNavigationStateChanged(TabContents::INVALIDATE_PAGE_ACTIONS);
[email protected]d7eaf572009-07-01 21:57:00687}
[email protected]ce5c4502009-05-06 16:46:11688
[email protected]d5f942ba2008-09-26 19:30:34689void TabContents::NotifyNavigationStateChanged(unsigned changed_flags) {
690 if (delegate_)
691 delegate_->NavigationStateChanged(this, changed_flags);
692}
693
[email protected]96d185d2009-04-24 03:28:54694void TabContents::DidBecomeSelected() {
695 controller_.SetActive(true);
[email protected]8cb5d5b2010-02-09 11:36:16696 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
697 if (rwhv) {
698 rwhv->DidBecomeSelected();
[email protected]789e9152009-08-04 21:59:43699#if defined(OS_MACOSX)
[email protected]8cb5d5b2010-02-09 11:36:16700 rwhv->SetActive(true);
[email protected]789e9152009-08-04 21:59:43701#endif
702 }
[email protected]96d185d2009-04-24 03:28:54703
[email protected]8cb5d5b2010-02-09 11:36:16704 WebCacheManager::GetInstance()->ObserveActivity(GetRenderProcessHost()->id());
[email protected]96d185d2009-04-24 03:28:54705}
706
707void TabContents::WasHidden() {
708 if (!capturing_contents()) {
709 // |render_view_host()| can be NULL if the user middle clicks a link to open
710 // a tab in then background, then closes the tab before selecting it. This
711 // is because closing the tab calls TabContents::Destroy(), which removes
712 // the |render_view_host()|; then when we actually destroy the window,
713 // OnWindowPosChanged() notices and calls HideContents() (which calls us).
[email protected]8cb5d5b2010-02-09 11:36:16714 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
715 if (rwhv)
716 rwhv->WasHidden();
[email protected]96d185d2009-04-24 03:28:54717 }
718
719 NotificationService::current()->Notify(
720 NotificationType::TAB_CONTENTS_HIDDEN,
721 Source<TabContents>(this),
722 NotificationService::NoDetails());
723}
724
[email protected]d5f942ba2008-09-26 19:30:34725void TabContents::Activate() {
726 if (delegate_)
727 delegate_->ActivateContents(this);
728}
729
[email protected]96d185d2009-04-24 03:28:54730void TabContents::ShowContents() {
[email protected]8cb5d5b2010-02-09 11:36:16731 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
732 if (rwhv)
733 rwhv->DidBecomeSelected();
[email protected]96d185d2009-04-24 03:28:54734}
735
736void TabContents::HideContents() {
737 // TODO(pkasting): http://b/1239839 Right now we purposefully don't call
738 // our superclass HideContents(), because some callers want to be very picky
739 // about the order in which these get called. In addition to making the code
740 // here practically impossible to understand, this also means we end up
741 // calling TabContents::WasHidden() twice if callers call both versions of
[email protected]57c6a652009-05-04 07:58:34742 // HideContents() on a TabContents.
[email protected]96d185d2009-04-24 03:28:54743 WasHidden();
744}
745
[email protected]c0588052008-10-27 23:01:50746void TabContents::OpenURL(const GURL& url, const GURL& referrer,
[email protected]d5f942ba2008-09-26 19:30:34747 WindowOpenDisposition disposition,
748 PageTransition::Type transition) {
749 if (delegate_)
[email protected]c0588052008-10-27 23:01:50750 delegate_->OpenURLFromTab(this, url, referrer, disposition, transition);
[email protected]d5f942ba2008-09-26 19:30:34751}
752
[email protected]1ccb3568d2010-02-19 10:51:16753bool TabContents::NavigateToPendingEntry(
754 NavigationController::ReloadType reload_type) {
[email protected]96d185d2009-04-24 03:28:54755 const NavigationEntry& entry = *controller_.pending_entry();
756
757 RenderViewHost* dest_render_view_host = render_manager_.Navigate(entry);
758 if (!dest_render_view_host)
759 return false; // Unable to create the desired render view host.
760
761 // Tell DevTools agent that it is attached prior to the navigation.
[email protected]d9f9b792009-06-24 13:17:12762 DevToolsManager* devtools_manager = DevToolsManager::GetInstance();
[email protected]7e8e6b62009-05-08 11:28:32763 if (devtools_manager) { // NULL in unit tests.
[email protected]3cc72b12010-03-18 23:03:00764 devtools_manager->OnNavigatingToPendingEntry(render_view_host(),
765 dest_render_view_host,
766 entry.url());
[email protected]7e8e6b62009-05-08 11:28:32767 }
[email protected]96d185d2009-04-24 03:28:54768
769 // Used for page load time metrics.
770 current_load_start_ = base::TimeTicks::Now();
771
772 // Navigate in the desired RenderViewHost.
[email protected]056de2d2009-06-26 16:41:34773 ViewMsg_Navigate_Params navigate_params;
[email protected]3cc72b12010-03-18 23:03:00774 MakeNavigateParams(controller_, reload_type, &navigate_params);
[email protected]056de2d2009-06-26 16:41:34775 dest_render_view_host->Navigate(navigate_params);
[email protected]96d185d2009-04-24 03:28:54776
777 if (entry.page_id() == -1) {
778 // HACK!! This code suppresses javascript: URLs from being added to
779 // session history, which is what we want to do for javascript: URLs that
780 // do not generate content. What we really need is a message from the
781 // renderer telling us that a new page was not created. The same message
782 // could be used for mailto: URLs and the like.
783 if (entry.url().SchemeIs(chrome::kJavaScriptScheme))
784 return false;
785 }
786
787 // Clear any provisional password saves - this stops password infobars
788 // showing up on pages the user navigates to while the right page is
789 // loading.
790 GetPasswordManager()->ClearProvisionalSave();
791
[email protected]1ccb3568d2010-02-19 10:51:16792 if (reload_type != NavigationController::NO_RELOAD &&
793 !profile()->IsOffTheRecord()) {
[email protected]0189bc722009-08-28 21:56:48794 FaviconService* favicon_service =
795 profile()->GetFaviconService(Profile::IMPLICIT_ACCESS);
796 if (favicon_service)
797 favicon_service->SetFaviconOutOfDateForPage(entry.url());
[email protected]96d185d2009-04-24 03:28:54798 }
799
[email protected]a888b29e62010-04-01 13:38:57800 // The url likely changed, see if there is an extension whose extent contains
801 // the current page.
802 UpdateAppExtensionForCurrentPage();
803
[email protected]d5f942ba2008-09-26 19:30:34804 return true;
805}
806
[email protected]96d185d2009-04-24 03:28:54807void TabContents::Stop() {
808 render_manager_.Stop();
809 printing_.Stop();
810}
811
[email protected]96d185d2009-04-24 03:28:54812void TabContents::DisassociateFromPopupCount() {
813 render_view_host()->DisassociateFromPopupCount();
814}
815
816TabContents* TabContents::Clone() {
817 // We create a new SiteInstance so that the new tab won't share processes
818 // with the old one. This can be changed in the future if we need it to share
819 // processes for some reason.
[email protected]420ae012009-04-24 05:16:32820 TabContents* tc = new TabContents(profile(),
[email protected]96d185d2009-04-24 03:28:54821 SiteInstance::CreateSiteInstance(profile()),
[email protected]12636df2009-09-28 22:32:21822 MSG_ROUTING_NONE, this);
[email protected]96d185d2009-04-24 03:28:54823 tc->controller().CopyStateFrom(controller_);
824 return tc;
825}
826
[email protected]4d677202009-07-19 07:37:12827void TabContents::ShowPageInfo(const GURL& url,
828 const NavigationEntry::SSLStatus& ssl,
829 bool show_history) {
830 if (!delegate_)
831 return;
832
[email protected]bb678332009-07-21 00:15:50833 delegate_->ShowPageInfo(profile(), url, ssl, show_history);
[email protected]4d677202009-07-19 07:37:12834}
835
[email protected]7e0831b2010-02-01 08:45:15836void TabContents::TranslatePage(const std::string& source_lang,
837 const std::string& target_lang) {
838 NavigationEntry* entry = controller_.GetActiveEntry();
839 if (!entry) {
840 NOTREACHED();
841 return;
842 }
[email protected]3b1c1812010-02-12 21:53:55843 language_state_.set_translation_pending(true);
[email protected]7e0831b2010-02-01 08:45:15844 render_view_host()->TranslatePage(entry->page_id(), source_lang, target_lang);
845}
846
[email protected]d7f380f2010-03-15 21:48:50847void TabContents::RevertTranslatedPage() {
848 // TODO(jcampan): revert translated page to original and remove translate
849 // infobar.
850}
851
initial.commit09911bf2008-07-26 23:55:29852ConstrainedWindow* TabContents::CreateConstrainedDialog(
[email protected]e8382172009-06-19 22:16:28853 ConstrainedWindowDelegate* delegate) {
initial.commit09911bf2008-07-26 23:55:29854 ConstrainedWindow* window =
[email protected]e8382172009-06-19 22:16:28855 ConstrainedWindow::CreateConstrainedDialog(this, delegate);
initial.commit09911bf2008-07-26 23:55:29856 child_windows_.push_back(window);
[email protected]fa1cf0b82010-01-15 21:49:44857
858 if (child_windows_.size() == 1) {
859 window->ShowConstrainedWindow();
860 BlockTabContent(true);
861 }
862
initial.commit09911bf2008-07-26 23:55:29863 return window;
864}
865
[email protected]fa1cf0b82010-01-15 21:49:44866void TabContents::BlockTabContent(bool blocked) {
[email protected]8dccd7cb2010-02-25 05:19:55867 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
868 if (rwhv)
869 rwhv->SetVisuallyDeemphasized(blocked);
[email protected]fa1cf0b82010-01-15 21:49:44870 render_view_host()->set_ignore_input_events(blocked);
871 if (delegate_)
872 delegate_->SetTabContentBlocked(this, blocked);
873}
874
initial.commit09911bf2008-07-26 23:55:29875void TabContents::AddNewContents(TabContents* new_contents,
876 WindowOpenDisposition disposition,
877 const gfx::Rect& initial_pos,
[email protected]7e7414ae2010-01-26 20:19:29878 bool user_gesture) {
initial.commit09911bf2008-07-26 23:55:29879 if (!delegate_)
880 return;
881
[email protected]35f7d212009-04-29 21:19:27882 if ((disposition == NEW_POPUP) && !user_gesture &&
883 !CommandLine::ForCurrentProcess()->HasSwitch(
884 switches::kDisablePopupBlocking)) {
[email protected]2c4410d2009-05-06 23:46:22885 // Unrequested popups from normal pages are constrained unless they're in
[email protected]b6c874582009-05-08 19:38:31886 // the whitelist. The popup owner will handle checking this.
[email protected]7e7414ae2010-01-26 20:19:29887 delegate_->GetConstrainingContents(this)->AddPopup(
888 new_contents, initial_pos);
[email protected]b6c874582009-05-08 19:38:31889 } else {
[email protected]304f8792009-08-19 21:28:09890#if defined(OS_CHROMEOS)
891 if (disposition == NEW_POPUP) {
892 // If the popup is bigger than a given factor of the screen, then
893 // turn it into a foreground tab (on chrome os only)
894 GdkScreen* screen = gdk_screen_get_default();
895 int max_width = gdk_screen_get_width(screen) * kMaxWidthFactor;
896 int max_height = gdk_screen_get_height(screen) * kMaxHeightFactor;
897 if (initial_pos.width() > max_width ||
898 initial_pos.height() > max_height) {
899 disposition = NEW_FOREGROUND_TAB;
900 }
901 }
902#endif
903
[email protected]0aa55312008-10-17 21:53:08904 new_contents->DisassociateFromPopupCount();
initial.commit09911bf2008-07-26 23:55:29905 delegate_->AddNewContents(this, new_contents, disposition, initial_pos,
906 user_gesture);
[email protected]7f37dd92009-09-15 16:35:49907 NotificationService::current()->Notify(
908 NotificationType::TAB_ADDED,
909 Source<TabContentsDelegate>(delegate_),
910 Details<TabContents>(this));
initial.commit09911bf2008-07-26 23:55:29911 }
initial.commit09911bf2008-07-26 23:55:29912
[email protected]8cc8d492010-02-02 10:40:49913 // TODO(pkasting): Why is this necessary?
914 PopupNotificationVisibilityChanged(blocked_popups_ != NULL);
initial.commit09911bf2008-07-26 23:55:29915}
916
[email protected]bcd9dc42010-01-23 16:38:18917bool TabContents::ExecuteCode(int request_id, const std::string& extension_id,
[email protected]61f5a7b2009-12-22 22:21:20918 const std::vector<URLPattern>& host_permissions,
[email protected]20ad2692009-11-20 18:27:20919 bool is_js_code, const std::string& code_string,
920 bool all_frames) {
[email protected]ebd0eae72010-02-16 23:50:51921 RenderViewHost* host = render_view_host();
922 if (!host)
923 return false;
924
925 return host->Send(new ViewMsg_ExecuteCode(host->routing_id(),
926 ViewMsg_ExecuteCode_Params(request_id, extension_id, host_permissions,
927 is_js_code, code_string, all_frames)));
[email protected]912256b32009-09-18 09:47:35928}
929
[email protected]96d185d2009-04-24 03:28:54930void TabContents::PopupNotificationVisibilityChanged(bool visible) {
[email protected]8cc8d492010-02-02 10:40:49931 if (is_being_destroyed_)
932 return;
933 if (!dont_notify_render_view_)
934 render_view_host()->PopupNotificationVisibilityChanged(visible);
[email protected]0de80162010-02-03 04:52:35935 if (delegate_)
936 delegate_->OnBlockedContentChange(this);
[email protected]96d185d2009-04-24 03:28:54937}
938
[email protected]5c9e97a2009-09-09 23:48:30939gfx::NativeView TabContents::GetContentNativeView() const {
[email protected]96d185d2009-04-24 03:28:54940 return view_->GetContentNativeView();
941}
942
943gfx::NativeView TabContents::GetNativeView() const {
944 return view_->GetNativeView();
945}
946
947void TabContents::GetContainerBounds(gfx::Rect *out) const {
948 view_->GetContainerBounds(out);
949}
950
951void TabContents::Focus() {
952 view_->Focus();
953}
954
[email protected]90daadb42009-06-08 21:27:28955void TabContents::FocusThroughTabTraversal(bool reverse) {
[email protected]7e383692009-06-12 19:14:54956 if (showing_interstitial_page()) {
957 render_manager_.interstitial_page()->FocusThroughTabTraversal(reverse);
958 return;
959 }
[email protected]96d185d2009-04-24 03:28:54960 render_view_host()->SetInitialFocus(reverse);
961}
962
963bool TabContents::FocusLocationBarByDefault() {
964 DOMUI* dom_ui = GetDOMUIForCurrentState();
965 if (dom_ui)
966 return dom_ui->focus_location_bar_by_default();
[email protected]3e3f0eb2009-06-22 18:33:43967 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]e0d481582009-09-15 21:06:25968 if (entry && entry->url() == GURL(chrome::kAboutBlankURL))
[email protected]3e3f0eb2009-06-22 18:33:43969 return true;
[email protected]96d185d2009-04-24 03:28:54970 return false;
971}
972
[email protected]a11aaf22010-03-30 00:03:38973void TabContents::SetFocusToLocationBar() {
974 if (delegate())
975 delegate()->SetFocusToLocationBar();
976}
977
[email protected]616ed5a2008-11-21 22:27:24978void TabContents::AddInfoBar(InfoBarDelegate* delegate) {
[email protected]f7019302010-03-26 19:58:32979 if (delegate_ && !delegate_->infobars_enabled()) {
980 delegate->InfoBarClosed();
981 return;
982 }
983
[email protected]616ed5a2008-11-21 22:27:24984 // Look through the existing InfoBarDelegates we have for a match. If we've
985 // already got one that matches, then we don't add the new one.
[email protected]f86a07022008-11-25 01:06:05986 for (int i = 0; i < infobar_delegate_count(); ++i) {
[email protected]6d27a7b2009-12-18 23:25:45987 if (GetInfoBarDelegateAt(i)->EqualsDelegate(delegate)) {
988 // Tell the new infobar to close so that it can clean itself up.
989 delegate->InfoBarClosed();
[email protected]616ed5a2008-11-21 22:27:24990 return;
[email protected]6d27a7b2009-12-18 23:25:45991 }
[email protected]616ed5a2008-11-21 22:27:24992 }
993
994 infobar_delegates_.push_back(delegate);
[email protected]bfd04a62009-02-01 18:16:56995 NotificationService::current()->Notify(
996 NotificationType::TAB_CONTENTS_INFOBAR_ADDED,
997 Source<TabContents>(this),
998 Details<InfoBarDelegate>(delegate));
[email protected]616ed5a2008-11-21 22:27:24999
1000 // Add ourselves as an observer for navigations the first time a delegate is
1001 // added. We use this notification to expire InfoBars that need to expire on
1002 // page transitions.
1003 if (infobar_delegates_.size() == 1) {
[email protected]bfd04a62009-02-01 18:16:561004 registrar_.Add(this, NotificationType::NAV_ENTRY_COMMITTED,
[email protected]ce3fa3c2009-04-20 19:55:571005 Source<NavigationController>(&controller_));
[email protected]616ed5a2008-11-21 22:27:241006 }
1007}
1008
1009void TabContents::RemoveInfoBar(InfoBarDelegate* delegate) {
[email protected]f7019302010-03-26 19:58:321010 if (delegate_ && !delegate_->infobars_enabled()) {
1011 return;
1012 }
1013
[email protected]616ed5a2008-11-21 22:27:241014 std::vector<InfoBarDelegate*>::iterator it =
1015 find(infobar_delegates_.begin(), infobar_delegates_.end(), delegate);
1016 if (it != infobar_delegates_.end()) {
1017 InfoBarDelegate* delegate = *it;
[email protected]bfd04a62009-02-01 18:16:561018 NotificationService::current()->Notify(
1019 NotificationType::TAB_CONTENTS_INFOBAR_REMOVED,
1020 Source<TabContents>(this),
1021 Details<InfoBarDelegate>(delegate));
[email protected]f86a07022008-11-25 01:06:051022 infobar_delegates_.erase(it);
[email protected]616ed5a2008-11-21 22:27:241023
[email protected]6a02963e2009-01-06 16:58:031024 // Remove ourselves as an observer if we are tracking no more InfoBars.
1025 if (infobar_delegates_.empty()) {
[email protected]bfd04a62009-02-01 18:16:561026 registrar_.Remove(this, NotificationType::NAV_ENTRY_COMMITTED,
[email protected]ce3fa3c2009-04-20 19:55:571027 Source<NavigationController>(&controller_));
[email protected]6a02963e2009-01-06 16:58:031028 }
[email protected]616ed5a2008-11-21 22:27:241029 }
1030}
[email protected]3edd9522009-03-04 22:19:411031
[email protected]f81821c2009-08-07 01:29:471032void TabContents::ReplaceInfoBar(InfoBarDelegate* old_delegate,
1033 InfoBarDelegate* new_delegate) {
[email protected]f7019302010-03-26 19:58:321034 if (delegate_ && !delegate_->infobars_enabled()) {
1035 new_delegate->InfoBarClosed();
1036 return;
1037 }
1038
[email protected]f81821c2009-08-07 01:29:471039 std::vector<InfoBarDelegate*>::iterator it =
1040 find(infobar_delegates_.begin(), infobar_delegates_.end(), old_delegate);
1041 DCHECK(it != infobar_delegates_.end());
1042
1043 // Notify the container about the change of plans.
1044 scoped_ptr<std::pair<InfoBarDelegate*, InfoBarDelegate*> > details(
1045 new std::pair<InfoBarDelegate*, InfoBarDelegate*>(
1046 old_delegate, new_delegate));
1047 NotificationService::current()->Notify(
1048 NotificationType::TAB_CONTENTS_INFOBAR_REPLACED,
1049 Source<TabContents>(this),
1050 Details<std::pair<InfoBarDelegate*, InfoBarDelegate*> >(details.get()));
1051
1052 // Remove the old one.
1053 infobar_delegates_.erase(it);
1054
1055 // Add the new one.
1056 infobar_delegates_.push_back(new_delegate);
1057}
1058
[email protected]20ede072009-10-06 00:28:351059bool TabContents::ShouldShowBookmarkBar() {
[email protected]aa9196f2009-10-22 16:20:271060 if (showing_interstitial_page())
1061 return false;
1062
[email protected]96d185d2009-04-24 03:28:541063 // See GetDOMUIForCurrentState() comment for more info. This case is very
1064 // similar, but for non-first loads, we want to use the committed entry. This
1065 // is so the bookmarks bar disappears at the same time the page does.
1066 if (controller_.GetLastCommittedEntry()) {
1067 // Not the first load, always use the committed DOM UI.
1068 if (render_manager_.dom_ui())
1069 return render_manager_.dom_ui()->force_bookmark_bar_visible();
1070 return false; // Default.
1071 }
1072
1073 // When it's the first load, we know either the pending one or the committed
1074 // one will have the DOM UI in it (see GetDOMUIForCurrentState), and only one
1075 // of them will be valid, so we can just check both.
1076 if (render_manager_.pending_dom_ui())
1077 return render_manager_.pending_dom_ui()->force_bookmark_bar_visible();
1078 if (render_manager_.dom_ui())
1079 return render_manager_.dom_ui()->force_bookmark_bar_visible();
1080 return false; // Default.
1081}
1082
[email protected]d8f16ae2009-08-30 03:47:261083bool TabContents::IsExtensionShelfAlwaysVisible() {
1084 // See GetDOMUIForCurrentState() comment for more info. This case is very
1085 // similar, but for non-first loads, we want to use the committed entry. This
1086 // is so the bookmarks bar disappears at the same time the page does.
1087 if (controller_.GetLastCommittedEntry()) {
1088 // Not the first load, always use the committed DOM UI.
1089 if (render_manager_.dom_ui())
1090 return render_manager_.dom_ui()->force_extension_shelf_visible();
1091 return false; // Default.
1092 }
1093
1094 // When it's the first load, we know either the pending one or the committed
1095 // one will have the DOM UI in it (see GetDOMUIForCurrentState), and only one
1096 // of them will be valid, so we can just check both.
1097 if (render_manager_.pending_dom_ui())
1098 return render_manager_.pending_dom_ui()->force_extension_shelf_visible();
1099 if (render_manager_.dom_ui())
1100 return render_manager_.dom_ui()->force_extension_shelf_visible();
1101 return false; // Default.
1102}
1103
[email protected]3edd9522009-03-04 22:19:411104void TabContents::ToolbarSizeChanged(bool is_animating) {
1105 TabContentsDelegate* d = delegate();
1106 if (d)
1107 d->ToolbarSizeChanged(this, is_animating);
1108}
[email protected]616ed5a2008-11-21 22:27:241109
[email protected]fc6fb7fb2009-11-07 02:35:041110bool TabContents::CanDownload(int request_id) {
1111 TabContentsDelegate* d = delegate();
1112 if (d)
1113 return d->CanDownload(request_id);
1114 return true;
1115}
1116
[email protected]d5f942ba2008-09-26 19:30:341117void TabContents::OnStartDownload(DownloadItem* download) {
1118 DCHECK(download);
[email protected]d5f942ba2008-09-26 19:30:341119
1120 // Download in a constrained popup is shown in the tab that opened it.
[email protected]b6c874582009-05-08 19:38:311121 TabContents* tab_contents = delegate()->GetConstrainingContents(this);
[email protected]d5f942ba2008-09-26 19:30:341122
[email protected]59560e0b2009-06-04 03:30:221123 if (tab_contents && tab_contents->delegate())
1124 tab_contents->delegate()->OnStartDownload(download);
[email protected]3edd9522009-03-04 22:19:411125}
[email protected]3edd9522009-03-04 22:19:411126
[email protected]d5f942ba2008-09-26 19:30:341127void TabContents::WillClose(ConstrainedWindow* window) {
1128 ConstrainedWindowList::iterator it =
1129 find(child_windows_.begin(), child_windows_.end(), window);
[email protected]fa1cf0b82010-01-15 21:49:441130 bool removed_topmost_window = it == child_windows_.begin();
[email protected]d5f942ba2008-09-26 19:30:341131 if (it != child_windows_.end())
1132 child_windows_.erase(it);
[email protected]55098792010-03-24 18:20:371133 if (child_windows_.size() > 0) {
1134 if (removed_topmost_window) {
1135 child_windows_[0]->ShowConstrainedWindow();
1136 }
[email protected]fa1cf0b82010-01-15 21:49:441137 BlockTabContent(true);
1138 } else {
1139 BlockTabContent(false);
1140 }
[email protected]66ba4932009-06-04 19:22:131141}
[email protected]d5f942ba2008-09-26 19:30:341142
[email protected]66ba4932009-06-04 19:22:131143void TabContents::WillCloseBlockedPopupContainer(
1144 BlockedPopupContainer* container) {
1145 DCHECK(blocked_popups_ == container);
1146 blocked_popups_ = NULL;
[email protected]8cc8d492010-02-02 10:40:491147 PopupNotificationVisibilityChanged(false);
[email protected]d5f942ba2008-09-26 19:30:341148}
1149
[email protected]d5f942ba2008-09-26 19:30:341150void TabContents::DidMoveOrResize(ConstrainedWindow* window) {
[email protected]332af7732009-03-11 13:21:351151#if defined(OS_WIN)
[email protected]92edc472009-02-10 20:32:061152 UpdateWindow(GetNativeView());
[email protected]332af7732009-03-11 13:21:351153#endif
[email protected]d5f942ba2008-09-26 19:30:341154}
1155
[email protected]c1dc9f132009-11-20 23:13:211156void TabContents::StartFinding(string16 search_string,
[email protected]e491f1c2009-05-22 20:28:121157 bool forward_direction,
1158 bool case_sensitive) {
[email protected]c1dc9f132009-11-20 23:13:211159 // If search_string is empty, it means FindNext was pressed with a keyboard
[email protected]6e95706d2009-04-23 22:55:011160 // shortcut so unless we have something to search for we return early.
[email protected]c1dc9f132009-11-20 23:13:211161 if (search_string.empty() && find_text_.empty()) {
[email protected]87fae512009-12-11 02:20:211162 // Try the last thing we searched for on this tab, then the last thing
1163 // searched for on any tab.
1164 if (!previous_find_text_.empty())
1165 search_string = previous_find_text_;
1166 else if (!last_search_prepopulate_text_->empty())
1167 search_string = *last_search_prepopulate_text_;
1168 else
[email protected]e491f1c2009-05-22 20:28:121169 return;
[email protected]e491f1c2009-05-22 20:28:121170 }
[email protected]6e95706d2009-04-23 22:55:011171
[email protected]101c90e2009-10-26 21:14:441172 // Keep track of the previous search.
1173 previous_find_text_ = find_text_;
1174
[email protected]6e95706d2009-04-23 22:55:011175 // This is a FindNext operation if we are searching for the same text again,
1176 // or if the passed in search text is empty (FindNext keyboard shortcut). The
1177 // exception to this is if the Find was aborted (then we don't want FindNext
1178 // because the highlighting has been cleared and we need it to reappear). We
1179 // therefore treat FindNext after an aborted Find operation as a full fledged
1180 // Find.
[email protected]c1dc9f132009-11-20 23:13:211181 bool find_next = (find_text_ == search_string || search_string.empty()) &&
[email protected]e491f1c2009-05-22 20:28:121182 (last_search_case_sensitive_ == case_sensitive) &&
[email protected]6e95706d2009-04-23 22:55:011183 !find_op_aborted_;
1184 if (!find_next)
1185 current_find_request_id_ = find_request_id_counter_++;
1186
[email protected]c1dc9f132009-11-20 23:13:211187 if (!search_string.empty())
1188 find_text_ = search_string;
[email protected]e491f1c2009-05-22 20:28:121189 last_search_case_sensitive_ = case_sensitive;
[email protected]6e95706d2009-04-23 22:55:011190
1191 find_op_aborted_ = false;
1192
1193 // Keep track of what the last search was across the tabs.
[email protected]c1dc9f132009-11-20 23:13:211194 *last_search_prepopulate_text_ = find_text_;
[email protected]6e95706d2009-04-23 22:55:011195 render_view_host()->StartFinding(current_find_request_id_,
1196 find_text_,
1197 forward_direction,
[email protected]e491f1c2009-05-22 20:28:121198 case_sensitive,
[email protected]6e95706d2009-04-23 22:55:011199 find_next);
1200}
1201
[email protected]5bc8fe92010-03-11 18:19:001202void TabContents::StopFinding(
1203 FindBarController::SelectionAction selection_action) {
1204 // When the action is kClearAction, it means the find string has been cleared
[email protected]2d2dffc2009-06-09 21:41:001205 // by the user, but the UI has not been dismissed.
[email protected]5bc8fe92010-03-11 18:19:001206 if (selection_action != FindBarController::kClearSelection)
[email protected]2d2dffc2009-06-09 21:41:001207 find_ui_active_ = false;
[email protected]87fae512009-12-11 02:20:211208 previous_find_text_ = find_text_;
[email protected]101c90e2009-10-26 21:14:441209 find_text_.clear();
[email protected]6e95706d2009-04-23 22:55:011210 find_op_aborted_ = true;
[email protected]e491f1c2009-05-22 20:28:121211 last_search_result_ = FindNotificationDetails();
[email protected]5bc8fe92010-03-11 18:19:001212 render_view_host()->StopFinding(selection_action);
[email protected]6e95706d2009-04-23 22:55:011213}
1214
[email protected]420ae012009-04-24 05:16:321215void TabContents::OnSavePage() {
1216 // If we can not save the page, try to download it.
1217 if (!SavePackage::IsSavableContents(contents_mime_type())) {
1218 DownloadManager* dlm = profile()->GetDownloadManager();
1219 const GURL& current_page_url = GetURL();
1220 if (dlm && current_page_url.is_valid())
[email protected]d82ed61e2009-06-16 02:46:221221 dlm->DownloadUrl(current_page_url, GURL(), "", this);
[email protected]420ae012009-04-24 05:16:321222 return;
1223 }
1224
1225 Stop();
1226
1227 // Create the save package and possibly prompt the user for the name to save
1228 // the page as. The user prompt is an asynchronous operation that runs on
1229 // another thread.
[email protected]d82ed61e2009-06-16 02:46:221230 save_package_ = new SavePackage(this);
[email protected]420ae012009-04-24 05:16:321231 save_package_->GetSaveInfo();
1232}
1233
1234// Used in automated testing to bypass prompting the user for file names.
1235// Instead, the names and paths are hard coded rather than running them through
1236// file name sanitation and extension / mime checking.
[email protected]828cabe2009-09-26 22:47:111237bool TabContents::SavePage(const FilePath& main_file, const FilePath& dir_path,
[email protected]420ae012009-04-24 05:16:321238 SavePackage::SavePackageType save_type) {
1239 // Stop the page from navigating.
1240 Stop();
1241
[email protected]828cabe2009-09-26 22:47:111242 save_package_ = new SavePackage(this, save_type, main_file, dir_path);
[email protected]cdf6cc82009-08-04 21:48:591243 return save_package_->Init();
[email protected]420ae012009-04-24 05:16:321244}
1245
[email protected]89d6e6e2009-12-03 23:06:511246void TabContents::EmailPageLocation() {
1247 std::string title = EscapeQueryParamValue(UTF16ToUTF8(GetTitle()), false);
1248 std::string page_url = EscapeQueryParamValue(GetURL().spec(), false);
1249 std::string mailto = std::string("mailto:?subject=Fwd:%20") +
1250 title + "&body=%0A%0A" + page_url;
1251 platform_util::OpenExternal(GURL(mailto));
1252}
1253
[email protected]420ae012009-04-24 05:16:321254void TabContents::PrintPreview() {
1255 // We don't show the print preview yet, only the print dialog.
1256 PrintNow();
1257}
1258
1259bool TabContents::PrintNow() {
1260 // We can't print interstitial page for now.
1261 if (showing_interstitial_page())
1262 return false;
1263
1264 return render_view_host()->PrintPages();
1265}
1266
[email protected]82270452009-06-19 15:58:011267void TabContents::PrintingDone(int document_cookie, bool success) {
1268 render_view_host()->PrintingDone(document_cookie, success);
1269}
1270
[email protected]420ae012009-04-24 05:16:321271bool TabContents::IsActiveEntry(int32 page_id) {
1272 NavigationEntry* active_entry = controller_.GetActiveEntry();
1273 return (active_entry != NULL &&
1274 active_entry->site_instance() == GetSiteInstance() &&
1275 active_entry->page_id() == page_id);
1276}
1277
[email protected]b2fe07d12010-02-09 14:38:081278void TabContents::SetOverrideEncoding(const std::string& encoding) {
[email protected]8cb5d5b2010-02-09 11:36:161279 set_encoding(encoding);
1280 render_view_host()->SetPageEncoding(encoding);
1281}
1282
[email protected]b2fe07d12010-02-09 14:38:081283void TabContents::ResetOverrideEncoding() {
[email protected]8cb5d5b2010-02-09 11:36:161284 reset_encoding();
1285 render_view_host()->ResetPageEncodingToDefault();
1286}
1287
1288void TabContents::WindowMoveOrResizeStarted() {
1289 render_view_host()->WindowMoveOrResizeStarted();
1290}
1291
[email protected]3f8eb7f2009-10-02 23:58:051292void TabContents::LogNewTabTime(const std::string& event_name) {
1293 // Not all new tab pages get timed. In those cases, we don't have a
1294 // new_tab_start_time_.
1295 if (new_tab_start_time_.is_null())
1296 return;
1297
1298 base::TimeDelta duration = base::TimeTicks::Now() - new_tab_start_time_;
[email protected]84abba62009-10-07 17:01:441299 MetricEventDurationDetails details(event_name,
1300 static_cast<int>(duration.InMilliseconds()));
1301
[email protected]7ab1e7d62009-10-14 23:32:011302 if (event_name == "Tab.NewTabScriptStart") {
1303 UMA_HISTOGRAM_TIMES("Tab.NewTabScriptStart", duration);
1304 } else if (event_name == "Tab.NewTabDOMContentLoaded") {
1305 UMA_HISTOGRAM_TIMES("Tab.NewTabDOMContentLoaded", duration);
1306 } else if (event_name == "Tab.NewTabOnload") {
1307 UMA_HISTOGRAM_TIMES("Tab.NewTabOnload", duration);
[email protected]3f8eb7f2009-10-02 23:58:051308 // The new tab page has finished loading; reset it.
1309 new_tab_start_time_ = base::TimeTicks();
[email protected]84abba62009-10-07 17:01:441310 } else {
1311 NOTREACHED();
[email protected]3f8eb7f2009-10-02 23:58:051312 }
[email protected]84abba62009-10-07 17:01:441313 NotificationService::current()->Notify(
1314 NotificationType::METRIC_EVENT_DURATION,
1315 Source<TabContents>(this),
1316 Details<MetricEventDurationDetails>(&details));
[email protected]3f8eb7f2009-10-02 23:58:051317}
1318
[email protected]7ab1e7d62009-10-14 23:32:011319void TabContents::OnCloseStarted() {
1320 if (tab_close_start_time_.is_null())
1321 tab_close_start_time_ = base::TimeTicks::Now();
1322}
1323
[email protected]c37e3b62010-01-22 00:15:171324TabContents* TabContents::CloneAndMakePhantom() {
[email protected]c37e3b62010-01-22 00:15:171325 TabNavigation tab_nav;
[email protected]df72ac72010-02-06 00:43:161326
1327 NavigationEntry* entry = controller().GetActiveEntry();
1328 if (app_extension_)
[email protected]867a73e12010-03-19 20:45:461329 tab_nav.set_url(app_extension_->GetFullLaunchURL());
[email protected]df72ac72010-02-06 00:43:161330 else if (entry)
1331 tab_nav.SetFromNavigationEntry(*entry);
1332
[email protected]c37e3b62010-01-22 00:15:171333 std::vector<TabNavigation> navigations;
1334 navigations.push_back(tab_nav);
1335
1336 TabContents* new_contents =
1337 new TabContents(profile(), NULL, MSG_ROUTING_NONE, NULL);
[email protected]3b355642010-02-05 16:01:491338 new_contents->SetAppExtension(app_extension_);
[email protected]c37e3b62010-01-22 00:15:171339 new_contents->controller().RestoreFromState(navigations, 0, false);
[email protected]c37e3b62010-01-22 00:15:171340
[email protected]df72ac72010-02-06 00:43:161341 if (!app_extension_ && entry)
1342 new_contents->controller().GetActiveEntry()->favicon() = entry->favicon();
1343
[email protected]c37e3b62010-01-22 00:15:171344 return new_contents;
1345}
1346
[email protected]0de80162010-02-03 04:52:351347// Resets the |content_blocked_| array.
1348void TabContents::ClearBlockedContentSettings() {
[email protected]15cf526b2010-02-12 06:33:491349 for (size_t i = 0; i < arraysize(content_blocked_); ++i)
[email protected]0de80162010-02-03 04:52:351350 content_blocked_[i] = false;
1351}
1352
[email protected]614c0e12010-03-25 10:12:561353// Resets the |geolocation_settings_| map.
1354void TabContents::ClearGeolocationContentSettings() {
1355 geolocation_content_settings_.clear();
1356}
1357
[email protected]96d185d2009-04-24 03:28:541358// Notifies the RenderWidgetHost instance about the fact that the page is
1359// loading, or done loading and calls the base implementation.
1360void TabContents::SetIsLoading(bool is_loading,
1361 LoadNotificationDetails* details) {
1362 if (is_loading == is_loading_)
1363 return;
1364
1365 if (!is_loading) {
1366 load_state_ = net::LOAD_STATE_IDLE;
1367 load_state_host_.clear();
[email protected]094e5b22009-09-25 04:23:561368 upload_size_ = 0;
1369 upload_position_ = 0;
[email protected]96d185d2009-04-24 03:28:541370 }
1371
1372 render_manager_.SetIsLoading(is_loading);
1373
1374 is_loading_ = is_loading;
1375 waiting_for_response_ = is_loading;
1376
1377 if (delegate_)
1378 delegate_->LoadingStateChanged(this);
[email protected]329581b2009-04-28 06:52:351379 NotifyNavigationStateChanged(INVALIDATE_LOAD);
[email protected]96d185d2009-04-24 03:28:541380
1381 NotificationType type = is_loading ? NotificationType::LOAD_START :
1382 NotificationType::LOAD_STOP;
[email protected]ce5c4502009-05-06 16:46:111383 NotificationDetails det = NotificationService::NoDetails();
[email protected]96d185d2009-04-24 03:28:541384 if (details)
1385 det = Details<LoadNotificationDetails>(details);
1386 NotificationService::current()->Notify(type,
1387 Source<NavigationController>(&controller_),
1388 det);
1389}
1390
[email protected]b6c874582009-05-08 19:38:311391void TabContents::AddPopup(TabContents* new_contents,
[email protected]7e7414ae2010-01-26 20:19:291392 const gfx::Rect& initial_pos) {
[email protected]8cc8d492010-02-02 10:40:491393 GURL url(GetURL());
1394 if (url.is_valid() &&
1395 profile()->GetHostContentSettingsMap()->GetContentSetting(
[email protected]da0c8e92010-02-09 23:25:491396 url, CONTENT_SETTINGS_TYPE_POPUPS) == CONTENT_SETTING_ALLOW) {
[email protected]8cc8d492010-02-02 10:40:491397 AddNewContents(new_contents, NEW_POPUP, initial_pos, true);
1398 } else {
1399 if (!blocked_popups_)
1400 blocked_popups_ = new BlockedPopupContainer(this);
1401 blocked_popups_->AddTabContents(new_contents, initial_pos);
1402 }
[email protected]b9681312008-11-07 00:08:261403}
[email protected]616ed5a2008-11-21 22:27:241404
1405namespace {
1406bool TransitionIsReload(PageTransition::Type transition) {
1407 return PageTransition::StripQualifier(transition) == PageTransition::RELOAD;
1408}
1409}
1410
1411void TabContents::ExpireInfoBars(
1412 const NavigationController::LoadCommittedDetails& details) {
1413 // Only hide InfoBars when the user has done something that makes the main
1414 // frame load. We don't want various automatic or subframe navigations making
1415 // it disappear.
1416 if (!details.is_user_initiated_main_frame_load())
1417 return;
1418
[email protected]f86a07022008-11-25 01:06:051419 for (int i = infobar_delegate_count() - 1; i >= 0; --i) {
[email protected]616ed5a2008-11-21 22:27:241420 InfoBarDelegate* delegate = GetInfoBarDelegateAt(i);
[email protected]f86a07022008-11-25 01:06:051421 if (delegate->ShouldExpire(details))
[email protected]616ed5a2008-11-21 22:27:241422 RemoveInfoBar(delegate);
[email protected]616ed5a2008-11-21 22:27:241423 }
1424}
[email protected]fdd61c62009-04-22 19:22:571425
[email protected]96d185d2009-04-24 03:28:541426DOMUI* TabContents::GetDOMUIForCurrentState() {
1427 // When there is a pending navigation entry, we want to use the pending DOMUI
1428 // that goes along with it to control the basic flags. For example, we want to
1429 // show the pending URL in the URL bar, so we want the display_url flag to
1430 // be from the pending entry.
1431 //
1432 // The confusion comes because there are multiple possibilities for the
1433 // initial load in a tab as a side effect of the way the RenderViewHostManager
1434 // works.
1435 //
1436 // - For the very first tab the load looks "normal". The new tab DOM UI is
1437 // the pending one, and we want it to apply here.
1438 //
1439 // - For subsequent new tabs, they'll get a new SiteInstance which will then
1440 // get switched to the one previously associated with the new tab pages.
1441 // This switching will cause the manager to commit the RVH/DOMUI. So we'll
1442 // have a committed DOM UI in this case.
1443 //
1444 // This condition handles all of these cases:
1445 //
1446 // - First load in first tab: no committed nav entry + pending nav entry +
1447 // pending dom ui:
1448 // -> Use pending DOM UI if any.
1449 //
1450 // - First load in second tab: no committed nav entry + pending nav entry +
1451 // no pending DOM UI:
1452 // -> Use the committed DOM UI if any.
1453 //
1454 // - Second navigation in any tab: committed nav entry + pending nav entry:
1455 // -> Use pending DOM UI if any.
1456 //
1457 // - Normal state with no load: committed nav entry + no pending nav entry:
1458 // -> Use committed DOM UI.
1459 if (controller_.pending_entry() &&
1460 (controller_.GetLastCommittedEntry() ||
1461 render_manager_.pending_dom_ui()))
1462 return render_manager_.pending_dom_ui();
1463 return render_manager_.dom_ui();
1464}
[email protected]420ae012009-04-24 05:16:321465
1466void TabContents::DidNavigateMainFramePostCommit(
1467 const NavigationController::LoadCommittedDetails& details,
1468 const ViewHostMsg_FrameNavigate_Params& params) {
[email protected]c2e74fe82009-09-03 17:57:441469 if (opener_dom_ui_type_ != DOMUIFactory::kNoDOMUI) {
1470 // If this is a window.open navigation, use the same DOMUI as the renderer
1471 // that opened the window, as long as both renderers have the same
1472 // privileges.
1473 if (opener_dom_ui_type_ == DOMUIFactory::GetDOMUIType(GetURL())) {
1474 DOMUI* dom_ui = DOMUIFactory::CreateDOMUIForURL(this, GetURL());
1475 // dom_ui might be NULL if the URL refers to a non-existent extension.
1476 if (dom_ui) {
1477 render_manager_.SetDOMUIPostCommit(dom_ui);
1478 dom_ui->RenderViewCreated(render_view_host());
1479 }
1480 }
1481 opener_dom_ui_type_ = DOMUIFactory::kNoDOMUI;
1482 }
1483
[email protected]420ae012009-04-24 05:16:321484 if (details.is_user_initiated_main_frame_load()) {
1485 // Clear the status bubble. This is a workaround for a bug where WebKit
1486 // doesn't let us know that the cursor left an element during a
1487 // transition (this is also why the mouse cursor remains as a hand after
1488 // clicking on a link); see bugs 1184641 and 980803. We don't want to
1489 // clear the bubble when a user navigates to a named anchor in the same
1490 // page.
1491 UpdateTargetURL(details.entry->page_id(), GURL());
1492
1493 // UpdateHelpersForDidNavigate will handle the case where the password_form
1494 // origin is valid.
1495 // TODO(brettw) bug 1343111: Password manager stuff in here needs to be
1496 // cleaned up and covered by tests.
1497 if (!params.password_form.origin.is_valid())
1498 GetPasswordManager()->DidNavigate();
1499 }
1500
1501 // The keyword generator uses the navigation entries, so must be called after
1502 // the commit.
1503 GenerateKeywordIfNecessary(params);
1504
1505 // Allow the new page to set the title again.
1506 received_page_title_ = false;
1507
1508 // Get the favicon, either from history or request it from the net.
1509 fav_icon_helper_.FetchFavIcon(details.entry->url());
1510
[email protected]0de80162010-02-03 04:52:351511 // Clear all page actions, blocked content notifications and browser actions
1512 // for this tab, unless this is an in-page navigation.
[email protected]a6e82fc2010-02-24 22:28:141513 if (!details.is_in_page) {
[email protected]92c6f9b92009-10-24 04:35:081514 ExtensionsService* service = profile()->GetExtensionsService();
1515 if (service) {
1516 for (size_t i = 0; i < service->extensions()->size(); ++i) {
[email protected]5d246db22009-10-27 06:17:571517 ExtensionAction* browser_action =
[email protected]92c6f9b92009-10-24 04:35:081518 service->extensions()->at(i)->browser_action();
[email protected]56ce6e52009-10-27 00:10:521519 if (browser_action) {
1520 browser_action->ClearAllValuesForTab(controller().session_id().id());
1521 NotificationService::current()->Notify(
1522 NotificationType::EXTENSION_BROWSER_ACTION_UPDATED,
[email protected]5d246db22009-10-27 06:17:571523 Source<ExtensionAction>(browser_action),
[email protected]56ce6e52009-10-27 00:10:521524 NotificationService::NoDetails());
1525 }
[email protected]92c6f9b92009-10-24 04:35:081526
[email protected]5d246db22009-10-27 06:17:571527 ExtensionAction* page_action =
[email protected]56ce6e52009-10-27 00:10:521528 service->extensions()->at(i)->page_action();
1529 if (page_action) {
1530 page_action->ClearAllValuesForTab(controller().session_id().id());
1531 PageActionStateChanged();
1532 }
[email protected]92c6f9b92009-10-24 04:35:081533 }
[email protected]aece2c7f2009-08-27 20:43:171534 }
[email protected]0de80162010-02-03 04:52:351535
1536 // Close blocked popups.
1537 if (blocked_popups_) {
1538 AutoReset auto_reset(&dont_notify_render_view_, true);
1539 blocked_popups_->Destroy();
[email protected]da7b5262010-02-25 21:12:461540 blocked_popups_ = NULL;
[email protected]0de80162010-02-03 04:52:351541 }
1542
1543 // Clear "blocked" flags.
1544 ClearBlockedContentSettings();
[email protected]614c0e12010-03-25 10:12:561545 ClearGeolocationContentSettings();
[email protected]0de80162010-02-03 04:52:351546 if (delegate_)
1547 delegate_->OnBlockedContentChange(this);
[email protected]aece2c7f2009-08-27 20:43:171548 }
[email protected]ce5c4502009-05-06 16:46:111549
[email protected]8cc8d492010-02-02 10:40:491550 // Close constrained windows if necessary.
1551 if (!net::RegistryControlledDomainService::SameDomainOrHost(
1552 details.previous_url, details.entry->url()))
1553 CloseConstrainedWindows();
1554
[email protected]420ae012009-04-24 05:16:321555 // Update the starred state.
1556 UpdateStarredStateForCurrentURL();
[email protected]c07c9752010-03-12 05:57:531557
1558 // Clear the cache of forms in AutoFill.
1559 if (autofill_manager_.get())
1560 autofill_manager_->Reset();
[email protected]a888b29e62010-04-01 13:38:571561
1562 // The url likely changed, see if there is an extension whose extent contains
1563 // the current page.
1564 UpdateAppExtensionForCurrentPage();
[email protected]420ae012009-04-24 05:16:321565}
1566
1567void TabContents::DidNavigateAnyFramePostCommit(
1568 RenderViewHost* render_view_host,
1569 const NavigationController::LoadCommittedDetails& details,
1570 const ViewHostMsg_FrameNavigate_Params& params) {
1571 // If we navigate, start showing messages again. This does nothing to prevent
1572 // a malicious script from spamming messages, since the script could just
1573 // reload the page to stop blocking.
1574 suppress_javascript_messages_ = false;
1575
[email protected]420ae012009-04-24 05:16:321576 // Notify the password manager of the navigation or form submit.
1577 // TODO(brettw) bug 1343111: Password manager stuff in here needs to be
1578 // cleaned up and covered by tests.
1579 if (params.password_form.origin.is_valid())
1580 GetPasswordManager()->ProvisionallySavePassword(params.password_form);
[email protected]3b1c1812010-02-12 21:53:551581
1582 // Let the LanguageState clear its state.
[email protected]34cec8d82010-03-05 18:27:011583 bool reload = details.entry->transition_type() == PageTransition::RELOAD ||
1584 details.type == NavigationType::SAME_PAGE;
1585 language_state_.DidNavigate(reload, details.is_in_page);
[email protected]420ae012009-04-24 05:16:321586}
1587
[email protected]8cc8d492010-02-02 10:40:491588void TabContents::CloseConstrainedWindows() {
1589 // Clear out any constrained windows since we are leaving this page entirely.
[email protected]420ae012009-04-24 05:16:321590 // We use indices instead of iterators in case CloseWindow does something
1591 // that may invalidate an iterator.
1592 int size = static_cast<int>(child_windows_.size());
1593 for (int i = size - 1; i >= 0; --i) {
1594 ConstrainedWindow* window = child_windows_[i];
[email protected]fa1cf0b82010-01-15 21:49:441595 if (window) {
[email protected]420ae012009-04-24 05:16:321596 window->CloseConstrainedWindow();
[email protected]fa1cf0b82010-01-15 21:49:441597 BlockTabContent(false);
1598 }
[email protected]420ae012009-04-24 05:16:321599 }
1600}
1601
1602void TabContents::UpdateStarredStateForCurrentURL() {
1603 BookmarkModel* model = profile()->GetBookmarkModel();
1604 const bool old_state = is_starred_;
1605 is_starred_ = (model && model->IsBookmarked(GetURL()));
1606
1607 if (is_starred_ != old_state && delegate())
1608 delegate()->URLStarredChanged(this, is_starred_);
1609}
1610
1611void TabContents::UpdateAlternateErrorPageURL() {
1612 GURL url = GetAlternateErrorPageURL();
1613 render_view_host()->SetAlternateErrorPageURL(url);
1614}
1615
1616void TabContents::UpdateWebPreferences() {
1617 render_view_host()->UpdateWebPreferences(GetWebkitPrefs());
1618}
1619
1620void TabContents::UpdateMaxPageIDIfNecessary(SiteInstance* site_instance,
1621 RenderViewHost* rvh) {
1622 // If we are creating a RVH for a restored controller, then we might
1623 // have more page IDs than the SiteInstance's current max page ID. We must
1624 // make sure that the max page ID is larger than any restored page ID.
1625 // Note that it is ok for conflicting page IDs to exist in another tab
1626 // (i.e., NavigationController), but if any page ID is larger than the max,
1627 // the back/forward list will get confused.
1628 int max_restored_page_id = controller_.max_restored_page_id();
1629 if (max_restored_page_id > 0) {
1630 int curr_max_page_id = site_instance->max_page_id();
1631 if (max_restored_page_id > curr_max_page_id) {
1632 // Need to update the site instance immediately.
1633 site_instance->UpdateMaxPageID(max_restored_page_id);
1634
1635 // Also tell the renderer to update its internal representation. We
1636 // need to reserve enough IDs to make all restored page IDs less than
1637 // the max.
1638 if (curr_max_page_id < 0)
1639 curr_max_page_id = 0;
1640 rvh->ReservePageIDRange(max_restored_page_id - curr_max_page_id);
1641 }
1642 }
1643}
1644
1645void TabContents::UpdateHistoryForNavigation(
[email protected]ebe89e062009-08-13 23:16:541646 const GURL& virtual_url,
[email protected]befd8d822009-07-01 04:51:471647 const NavigationController::LoadCommittedDetails& details,
[email protected]420ae012009-04-24 05:16:321648 const ViewHostMsg_FrameNavigate_Params& params) {
1649 if (profile()->IsOffTheRecord())
1650 return;
1651
1652 // Add to history service.
1653 HistoryService* hs = profile()->GetHistoryService(Profile::IMPLICIT_ACCESS);
1654 if (hs) {
1655 if (PageTransition::IsMainFrame(params.transition) &&
[email protected]ebe89e062009-08-13 23:16:541656 virtual_url != params.url) {
1657 // Hack on the "virtual" URL so that it will appear in history. For some
[email protected]420ae012009-04-24 05:16:321658 // types of URLs, we will display a magic URL that is different from where
1659 // the page is actually navigated. We want the user to see in history
[email protected]ebe89e062009-08-13 23:16:541660 // what they saw in the URL bar, so we add the virtual URL as a redirect.
1661 // This only applies to the main frame, as the virtual URL doesn't apply
[email protected]420ae012009-04-24 05:16:321662 // to sub-frames.
1663 std::vector<GURL> redirects = params.redirects;
1664 if (!redirects.empty())
[email protected]ebe89e062009-08-13 23:16:541665 redirects.back() = virtual_url;
1666 hs->AddPage(virtual_url, this, params.page_id, params.referrer,
[email protected]befd8d822009-07-01 04:51:471667 params.transition, redirects, details.did_replace_entry);
[email protected]420ae012009-04-24 05:16:321668 } else {
1669 hs->AddPage(params.url, this, params.page_id, params.referrer,
[email protected]befd8d822009-07-01 04:51:471670 params.transition, params.redirects,
1671 details.did_replace_entry);
[email protected]420ae012009-04-24 05:16:321672 }
1673 }
1674}
1675
1676bool TabContents::UpdateTitleForEntry(NavigationEntry* entry,
1677 const std::wstring& title) {
1678 // For file URLs without a title, use the pathname instead. In the case of a
1679 // synthesized title, we don't want the update to count toward the "one set
1680 // per page of the title to history."
1681 std::wstring final_title;
1682 bool explicit_set;
1683 if (entry->url().SchemeIsFile() && title.empty()) {
1684 final_title = UTF8ToWide(entry->url().ExtractFileName());
1685 explicit_set = false; // Don't count synthetic titles toward the set limit.
1686 } else {
1687 TrimWhitespace(title, TRIM_ALL, &final_title);
1688 explicit_set = true;
1689 }
1690
1691 if (final_title == UTF16ToWideHack(entry->title()))
1692 return false; // Nothing changed, don't bother.
1693
1694 entry->set_title(WideToUTF16Hack(final_title));
1695
1696 // Update the history system for this page.
1697 if (!profile()->IsOffTheRecord() && !received_page_title_) {
1698 HistoryService* hs =
1699 profile()->GetHistoryService(Profile::IMPLICIT_ACCESS);
1700 if (hs)
[email protected]ebe89e062009-08-13 23:16:541701 hs->SetPageTitle(entry->virtual_url(), final_title);
[email protected]420ae012009-04-24 05:16:321702
1703 // Don't allow the title to be saved again for explicitly set ones.
1704 received_page_title_ = explicit_set;
1705 }
1706
1707 // Lastly, set the title for the view.
1708 view_->SetPageTitle(final_title);
1709
1710 return true;
1711}
1712
1713void TabContents::NotifySwapped() {
1714 // After sending out a swap notification, we need to send a disconnect
1715 // notification so that clients that pick up a pointer to |this| can NULL the
1716 // pointer. See Bug 1230284.
1717 notify_disconnection_ = true;
1718 NotificationService::current()->Notify(
[email protected]57c6a652009-05-04 07:58:341719 NotificationType::TAB_CONTENTS_SWAPPED,
[email protected]d82ed61e2009-06-16 02:46:221720 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:321721 NotificationService::NoDetails());
1722}
1723
1724void TabContents::NotifyConnected() {
1725 notify_disconnection_ = true;
1726 NotificationService::current()->Notify(
[email protected]57c6a652009-05-04 07:58:341727 NotificationType::TAB_CONTENTS_CONNECTED,
[email protected]d82ed61e2009-06-16 02:46:221728 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:321729 NotificationService::NoDetails());
1730}
1731
1732void TabContents::NotifyDisconnected() {
1733 if (!notify_disconnection_)
1734 return;
1735
1736 notify_disconnection_ = false;
1737 NotificationService::current()->Notify(
[email protected]57c6a652009-05-04 07:58:341738 NotificationType::TAB_CONTENTS_DISCONNECTED,
[email protected]d82ed61e2009-06-16 02:46:221739 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:321740 NotificationService::NoDetails());
1741}
1742
1743void TabContents::GenerateKeywordIfNecessary(
1744 const ViewHostMsg_FrameNavigate_Params& params) {
1745 if (!params.searchable_form_url.is_valid())
1746 return;
1747
1748 if (profile()->IsOffTheRecord())
1749 return;
1750
1751 int last_index = controller_.last_committed_entry_index();
1752 // When there was no previous page, the last index will be 0. This is
1753 // normally due to a form submit that opened in a new tab.
1754 // TODO(brettw) bug 916126: we should support keywords when form submits
1755 // happen in new tabs.
1756 if (last_index <= 0)
1757 return;
1758 const NavigationEntry* previous_entry =
1759 controller_.GetEntryAtIndex(last_index - 1);
1760 if (IsFormSubmit(previous_entry)) {
1761 // Only generate a keyword if the previous page wasn't itself a form
1762 // submit.
1763 return;
1764 }
1765
1766 GURL keyword_url = previous_entry->user_typed_url().is_valid() ?
1767 previous_entry->user_typed_url() : previous_entry->url();
1768 std::wstring keyword =
1769 TemplateURLModel::GenerateKeyword(keyword_url, true); // autodetected
1770 if (keyword.empty())
1771 return;
1772
1773 TemplateURLModel* url_model = profile()->GetTemplateURLModel();
1774 if (!url_model)
1775 return;
1776
1777 if (!url_model->loaded()) {
1778 url_model->Load();
1779 return;
1780 }
1781
1782 const TemplateURL* current_url;
1783 std::wstring url = UTF8ToWide(params.searchable_form_url.spec());
1784 if (!url_model->CanReplaceKeyword(keyword, url, &current_url))
1785 return;
1786
1787 if (current_url) {
1788 if (current_url->originating_url().is_valid()) {
1789 // The existing keyword was generated from an OpenSearch description
1790 // document, don't regenerate.
1791 return;
1792 }
1793 url_model->Remove(current_url);
1794 }
1795 TemplateURL* new_url = new TemplateURL();
1796 new_url->set_keyword(keyword);
1797 new_url->set_short_name(keyword);
1798 new_url->SetURL(url, 0, 0);
1799 new_url->add_input_encoding(params.searchable_form_encoding);
1800 DCHECK(controller_.GetLastCommittedEntry());
1801 const GURL& favicon_url =
1802 controller_.GetLastCommittedEntry()->favicon().url();
1803 if (favicon_url.is_valid()) {
1804 new_url->SetFavIconURL(favicon_url);
1805 } else {
1806 // The favicon url isn't valid. This means there really isn't a favicon,
1807 // or the favicon url wasn't obtained before the load started. This assumes
1808 // the later.
1809 // TODO(sky): Need a way to set the favicon that doesn't involve generating
1810 // its url.
1811 new_url->SetFavIconURL(TemplateURL::GenerateFaviconURL(params.referrer));
1812 }
1813 new_url->set_safe_for_autoreplace(true);
1814 url_model->Add(new_url);
1815}
1816
[email protected]bf5c2ff392009-07-08 16:24:331817void TabContents::OnUserGesture() {
1818 // See comment in RenderViewHostDelegate::OnUserGesture as to why we do this.
1819 DownloadRequestManager* drm = g_browser_process->download_request_manager();
1820 if (drm)
1821 drm->OnUserGesture(this);
[email protected]e7eaedde2009-09-25 20:09:491822 ExternalProtocolHandler::OnUserGesture();
[email protected]bf5c2ff392009-07-08 16:24:331823 controller_.OnUserGesture();
1824}
1825
1826void TabContents::OnFindReply(int request_id,
1827 int number_of_matches,
1828 const gfx::Rect& selection_rect,
1829 int active_match_ordinal,
1830 bool final_update) {
[email protected]3ce7ced2009-07-24 18:52:421831 // Ignore responses for requests that have been aborted.
1832 if (find_op_aborted_)
1833 return;
1834
[email protected]bf5c2ff392009-07-08 16:24:331835 // Ignore responses for requests other than the one we have most recently
1836 // issued. That way we won't act on stale results when the user has
1837 // already typed in another query.
1838 if (request_id != current_find_request_id_)
1839 return;
1840
1841 if (number_of_matches == -1)
1842 number_of_matches = last_search_result_.number_of_matches();
1843 if (active_match_ordinal == -1)
1844 active_match_ordinal = last_search_result_.active_match_ordinal();
1845
1846 gfx::Rect selection = selection_rect;
1847 if (selection.IsEmpty())
1848 selection = last_search_result_.selection_rect();
1849
1850 // Notify the UI, automation and any other observers that a find result was
1851 // found.
1852 last_search_result_ = FindNotificationDetails(request_id, number_of_matches,
1853 selection, active_match_ordinal,
1854 final_update);
1855 NotificationService::current()->Notify(
1856 NotificationType::FIND_RESULT_AVAILABLE,
1857 Source<TabContents>(this),
1858 Details<FindNotificationDetails>(&last_search_result_));
1859}
1860
1861void TabContents::GoToEntryAtOffset(int offset) {
[email protected]f9cc4c452009-10-13 14:56:381862 if (!delegate_ || delegate_->OnGoToEntryOffset(offset))
1863 controller_.GoToOffset(offset);
[email protected]bf5c2ff392009-07-08 16:24:331864}
1865
[email protected]bf5c2ff392009-07-08 16:24:331866void TabContents::OnMissingPluginStatus(int status) {
1867#if defined(OS_WIN)
1868// TODO(PORT): pull in when plug-ins work
1869 GetPluginInstaller()->OnMissingPluginStatus(status);
1870#endif
1871}
1872
1873void TabContents::OnCrashedPlugin(const FilePath& plugin_path) {
[email protected]bf5c2ff392009-07-08 16:24:331874 DCHECK(!plugin_path.value().empty());
1875
1876 std::wstring plugin_name = plugin_path.ToWStringHack();
[email protected]d32d46182009-07-23 00:09:351877#if defined(OS_WIN) || defined(OS_MACOSX)
[email protected]bf5c2ff392009-07-08 16:24:331878 scoped_ptr<FileVersionInfo> version_info(
1879 FileVersionInfo::CreateFileVersionInfo(plugin_path));
1880 if (version_info.get()) {
1881 const std::wstring& product_name = version_info->product_name();
[email protected]996fd702009-09-04 19:12:371882 if (!product_name.empty()) {
[email protected]bf5c2ff392009-07-08 16:24:331883 plugin_name = product_name;
[email protected]996fd702009-09-04 19:12:371884#if defined(OS_MACOSX)
1885 // Many plugins on the Mac have .plugin in the actual name, which looks
1886 // terrible, so look for that and strip it off if present.
1887 const std::wstring plugin_extension(L".plugin");
1888 if (EndsWith(plugin_name, plugin_extension, true))
1889 plugin_name.erase(plugin_name.length() - plugin_extension.length());
1890#endif // OS_MACOSX
1891 }
[email protected]bf5c2ff392009-07-08 16:24:331892 }
[email protected]4a560ed2009-07-13 23:35:261893#else
1894 NOTIMPLEMENTED() << " convert plugin path to plugin name";
1895#endif
[email protected]bf5c2ff392009-07-08 16:24:331896 AddInfoBar(new SimpleAlertInfoBarDelegate(
1897 this, l10n_util::GetStringF(IDS_PLUGIN_CRASHED_PROMPT, plugin_name),
[email protected]938e1f92010-04-01 18:09:421898 NULL, true));
[email protected]bf5c2ff392009-07-08 16:24:331899}
1900
1901void TabContents::OnCrashedWorker() {
1902 AddInfoBar(new SimpleAlertInfoBarDelegate(
1903 this, l10n_util::GetString(IDS_WEBWORKER_CRASHED_PROMPT),
[email protected]938e1f92010-04-01 18:09:421904 NULL, true));
[email protected]bf5c2ff392009-07-08 16:24:331905}
1906
1907void TabContents::OnDidGetApplicationInfo(
1908 int32 page_id,
1909 const webkit_glue::WebApplicationInfo& info) {
[email protected]ed5431872009-11-17 08:39:511910 web_app_info_ = info;
[email protected]bf5c2ff392009-07-08 16:24:331911
[email protected]ed5431872009-11-17 08:39:511912 if (delegate())
1913 delegate()->OnDidGetApplicationInfo(this, page_id);
[email protected]bf5c2ff392009-07-08 16:24:331914}
1915
[email protected]a2cf65eb2010-01-18 08:20:381916void TabContents::OnPageContents(const GURL& url,
1917 int renderer_process_id,
1918 int32 page_id,
[email protected]a8a81292010-01-21 00:32:451919 const std::wstring& contents,
1920 const std::string& language) {
[email protected]a2cf65eb2010-01-18 08:20:381921 // Don't index any https pages. People generally don't want their bank
1922 // accounts, etc. indexed on their computer, especially since some of these
1923 // things are not marked cachable.
1924 // TODO(brettw) we may want to consider more elaborate heuristics such as
1925 // the cachability of the page. We may also want to consider subframes (this
1926 // test will still index subframes if the subframe is SSL).
1927 if (!url.SchemeIsSecure()) {
1928 Profile* p = profile();
1929 if (p && !p->IsOffTheRecord()) {
1930 HistoryService* hs = p->GetHistoryService(Profile::IMPLICIT_ACCESS);
1931 if (hs)
1932 hs->SetPageContents(url, contents);
1933 }
1934 }
[email protected]a8a81292010-01-21 00:32:451935
[email protected]3b1c1812010-02-12 21:53:551936 language_state_.LanguageDetermined(language);
[email protected]a8a81292010-01-21 00:32:451937
1938 std::string lang = language;
1939 NotificationService::current()->Notify(
1940 NotificationType::TAB_LANGUAGE_DETERMINED,
[email protected]7e0831b2010-02-01 08:45:151941 Source<TabContents>(this),
[email protected]a8a81292010-01-21 00:32:451942 Details<std::string>(&lang));
[email protected]a2cf65eb2010-01-18 08:20:381943}
1944
[email protected]7e0831b2010-02-01 08:45:151945void TabContents::OnPageTranslated(int32 page_id,
1946 const std::string& original_lang,
[email protected]152b8ba32010-03-29 19:40:161947 const std::string& translated_lang,
1948 TranslateErrors::Type error_type) {
[email protected]3b1c1812010-02-12 21:53:551949 language_state_.set_current_language(translated_lang);
1950 language_state_.set_translation_pending(false);
[email protected]152b8ba32010-03-29 19:40:161951 PageTranslatedDetails details(original_lang, translated_lang, error_type);
[email protected]7e0831b2010-02-01 08:45:151952 NotificationService::current()->Notify(
1953 NotificationType::PAGE_TRANSLATED,
1954 Source<TabContents>(this),
[email protected]152b8ba32010-03-29 19:40:161955 Details<PageTranslatedDetails>(&details));
[email protected]7e0831b2010-02-01 08:45:151956}
1957
[email protected]bf5c2ff392009-07-08 16:24:331958void TabContents::DidStartProvisionalLoadForFrame(
1959 RenderViewHost* render_view_host,
1960 bool is_main_frame,
1961 const GURL& url) {
1962 ProvisionalLoadDetails details(is_main_frame,
1963 controller_.IsURLInPageNavigation(url),
1964 url, std::string(), false);
1965 NotificationService::current()->Notify(
1966 NotificationType::FRAME_PROVISIONAL_LOAD_START,
1967 Source<NavigationController>(&controller_),
1968 Details<ProvisionalLoadDetails>(&details));
1969}
1970
1971void TabContents::DidStartReceivingResourceResponse(
[email protected]3dbb80b2010-02-09 22:41:201972 const ResourceRequestDetails& details) {
[email protected]bf5c2ff392009-07-08 16:24:331973 NotificationService::current()->Notify(
1974 NotificationType::RESOURCE_RESPONSE_STARTED,
1975 Source<NavigationController>(&controller()),
[email protected]3dbb80b2010-02-09 22:41:201976 Details<const ResourceRequestDetails>(&details));
[email protected]bf5c2ff392009-07-08 16:24:331977}
1978
[email protected]3dbb80b2010-02-09 22:41:201979void TabContents::DidRedirectResource(
1980 const ResourceRedirectDetails& details) {
[email protected]bf5c2ff392009-07-08 16:24:331981 NotificationService::current()->Notify(
1982 NotificationType::RESOURCE_RECEIVED_REDIRECT,
1983 Source<NavigationController>(&controller()),
[email protected]3dbb80b2010-02-09 22:41:201984 Details<const ResourceRequestDetails>(&details));
[email protected]bf5c2ff392009-07-08 16:24:331985}
1986
1987void TabContents::DidLoadResourceFromMemoryCache(
1988 const GURL& url,
1989 const std::string& frame_origin,
1990 const std::string& main_frame_origin,
1991 const std::string& security_info) {
1992 // Send out a notification that we loaded a resource from our memory cache.
1993 int cert_id = 0, cert_status = 0, security_bits = 0;
1994 SSLManager::DeserializeSecurityInfo(security_info,
1995 &cert_id, &cert_status,
1996 &security_bits);
1997 LoadFromMemoryCacheDetails details(url, frame_origin, main_frame_origin,
[email protected]8cb5d5b2010-02-09 11:36:161998 GetRenderProcessHost()->id(), cert_id,
1999 cert_status);
[email protected]bf5c2ff392009-07-08 16:24:332000
2001 NotificationService::current()->Notify(
2002 NotificationType::LOAD_FROM_MEMORY_CACHE,
2003 Source<NavigationController>(&controller_),
2004 Details<LoadFromMemoryCacheDetails>(&details));
2005}
2006
[email protected]e3d60e5d2009-09-25 21:08:292007void TabContents::DidDisplayInsecureContent() {
[email protected]09843b5f2009-09-26 08:30:592008 controller_.ssl_manager()->DidDisplayInsecureContent();
[email protected]e3d60e5d2009-09-25 21:08:292009}
2010
2011void TabContents::DidRunInsecureContent(const std::string& security_origin) {
[email protected]09843b5f2009-09-26 08:30:592012 controller_.ssl_manager()->DidRunInsecureContent(security_origin);
[email protected]e3d60e5d2009-09-25 21:08:292013}
2014
[email protected]bf5c2ff392009-07-08 16:24:332015void TabContents::DidFailProvisionalLoadWithError(
2016 RenderViewHost* render_view_host,
2017 bool is_main_frame,
2018 int error_code,
2019 const GURL& url,
2020 bool showing_repost_interstitial) {
2021 if (net::ERR_ABORTED == error_code) {
2022 // EVIL HACK ALERT! Ignore failed loads when we're showing interstitials.
2023 // This means that the interstitial won't be torn down properly, which is
2024 // bad. But if we have an interstitial, go back to another tab type, and
2025 // then load the same interstitial again, we could end up getting the first
2026 // interstitial's "failed" message (as a result of the cancel) when we're on
2027 // the second one.
2028 //
2029 // We can't tell this apart, so we think we're tearing down the current page
2030 // which will cause a crash later one. There is also some code in
2031 // RenderViewHostManager::RendererAbortedProvisionalLoad that is commented
2032 // out because of this problem.
2033 //
2034 // http://code.google.com/p/chromium/issues/detail?id=2855
2035 // Because this will not tear down the interstitial properly, if "back" is
2036 // back to another tab type, the interstitial will still be somewhat alive
2037 // in the previous tab type. If you navigate somewhere that activates the
2038 // tab with the interstitial again, you'll see a flash before the new load
2039 // commits of the interstitial page.
2040 if (showing_interstitial_page()) {
2041 LOG(WARNING) << "Discarding message during interstitial.";
2042 return;
2043 }
2044
2045 // This will discard our pending entry if we cancelled the load (e.g., if we
2046 // decided to download the file instead of load it). Only discard the
2047 // pending entry if the URLs match, otherwise the user initiated a navigate
2048 // before the page loaded so that the discard would discard the wrong entry.
2049 NavigationEntry* pending_entry = controller_.pending_entry();
2050 if (pending_entry && pending_entry->url() == url) {
2051 controller_.DiscardNonCommittedEntries();
2052 // Update the URL display.
2053 NotifyNavigationStateChanged(TabContents::INVALIDATE_URL);
2054 }
2055
2056 render_manager_.RendererAbortedProvisionalLoad(render_view_host);
2057 }
2058
2059 // Send out a notification that we failed a provisional load with an error.
2060 ProvisionalLoadDetails details(is_main_frame,
2061 controller_.IsURLInPageNavigation(url),
2062 url, std::string(), false);
2063 details.set_error_code(error_code);
2064
2065 NotificationService::current()->Notify(
2066 NotificationType::FAIL_PROVISIONAL_LOAD_WITH_ERROR,
2067 Source<NavigationController>(&controller_),
2068 Details<ProvisionalLoadDetails>(&details));
2069}
2070
2071void TabContents::DocumentLoadedInFrame() {
2072 controller_.DocumentLoadedInFrame();
2073}
2074
[email protected]0de80162010-02-03 04:52:352075void TabContents::OnContentBlocked(ContentSettingsType type) {
[email protected]b1235062010-03-25 17:31:272076 // TODO(joth): remove once fully implemented.
2077 DCHECK(type != CONTENT_SETTINGS_TYPE_GEOLOCATION);
[email protected]0de80162010-02-03 04:52:352078 content_blocked_[type] = true;
2079 if (delegate_)
2080 delegate_->OnBlockedContentChange(this);
2081}
2082
[email protected]614c0e12010-03-25 10:12:562083void TabContents::OnGeolocationPermissionSet(const GURL& requesting_origin,
2084 bool allowed) {
2085 geolocation_content_settings_[requesting_origin] =
2086 allowed ? CONTENT_SETTING_ALLOW : CONTENT_SETTING_BLOCK;
2087 // TODO(bulach): rename OnBlockedContentChange to OnContentSettingsChange.
2088 if (delegate_)
2089 delegate_->OnBlockedContentChange(this);
2090}
2091
[email protected]8d3347f2009-07-09 22:00:212092RenderViewHostDelegate::View* TabContents::GetViewDelegate() {
[email protected]420ae012009-04-24 05:16:322093 return view_.get();
2094}
2095
[email protected]8d3347f2009-07-09 22:00:212096RenderViewHostDelegate::RendererManagement*
2097TabContents::GetRendererManagementDelegate() {
2098 return &render_manager_;
2099}
2100
[email protected]bf5c2ff392009-07-08 16:24:332101RenderViewHostDelegate::BrowserIntegration*
[email protected]8d3347f2009-07-09 22:00:212102 TabContents::GetBrowserIntegrationDelegate() {
2103 return this;
[email protected]bf5c2ff392009-07-08 16:24:332104}
2105
[email protected]8d3347f2009-07-09 22:00:212106RenderViewHostDelegate::Resource* TabContents::GetResourceDelegate() {
2107 return this;
[email protected]bf5c2ff392009-07-08 16:24:332108}
2109
[email protected]8d3347f2009-07-09 22:00:212110RenderViewHostDelegate::Save* TabContents::GetSaveDelegate() {
[email protected]420ae012009-04-24 05:16:322111 return save_package_.get(); // May be NULL, but we can return NULL.
2112}
2113
[email protected]8d3347f2009-07-09 22:00:212114RenderViewHostDelegate::Printing* TabContents::GetPrintingDelegate() {
2115 return &printing_;
2116}
2117
2118RenderViewHostDelegate::FavIcon* TabContents::GetFavIconDelegate() {
2119 return &fav_icon_helper_;
2120}
2121
[email protected]e8dbd674b2010-03-11 18:34:402122RenderViewHostDelegate::Autocomplete* TabContents::GetAutocompleteDelegate() {
[email protected]6db0def92010-03-02 19:55:452123 if (autocomplete_history_manager_.get() == NULL)
2124 autocomplete_history_manager_.reset(new AutocompleteHistoryManager(this));
2125 return autocomplete_history_manager_.get();
[email protected]bf5c2ff392009-07-08 16:24:332126}
2127
[email protected]21674df2009-10-28 19:51:212128RenderViewHostDelegate::AutoFill* TabContents::GetAutoFillDelegate() {
2129 if (autofill_manager_.get() == NULL)
2130 autofill_manager_.reset(new AutoFillManager(this));
2131 return autofill_manager_.get();
2132}
2133
[email protected]fd571bb2010-03-19 01:23:542134AutomationResourceRoutingDelegate*
2135TabContents::GetAutomationResourceRoutingDelegate() {
2136 return delegate();
2137}
2138
[email protected]ced90ae12010-02-20 02:06:162139RenderViewHostDelegate::BookmarkDrag* TabContents::GetBookmarkDragDelegate() {
2140 return bookmark_drag_;
2141}
2142
2143void TabContents::SetBookmarkDragDelegate(
2144 RenderViewHostDelegate::BookmarkDrag* bookmark_drag) {
2145 bookmark_drag_ = bookmark_drag;
2146}
2147
[email protected]93623c5d2009-12-10 21:40:322148RendererPreferences TabContents::GetRendererPrefs(Profile* profile) const {
[email protected]840b1512009-07-21 16:53:462149 return renderer_preferences_;
[email protected]80d96fa2009-06-10 22:34:512150}
2151
[email protected]57c6a652009-05-04 07:58:342152TabContents* TabContents::GetAsTabContents() {
2153 return this;
2154}
2155
[email protected]7b291f92009-08-14 05:43:532156ViewType::Type TabContents::GetRenderViewType() const {
2157 return ViewType::TAB_CONTENTS;
2158}
2159
2160int TabContents::GetBrowserWindowID() const {
2161 return controller().window_id().id();
2162}
2163
[email protected]420ae012009-04-24 05:16:322164void TabContents::RenderViewCreated(RenderViewHost* render_view_host) {
[email protected]2e4633c2009-07-09 16:58:062165 NotificationService::current()->Notify(
2166 NotificationType::RENDER_VIEW_HOST_CREATED_FOR_TAB,
2167 Source<TabContents>(this),
2168 Details<RenderViewHost>(render_view_host));
[email protected]420ae012009-04-24 05:16:322169 NavigationEntry* entry = controller_.GetActiveEntry();
2170 if (!entry)
2171 return;
2172
2173 // When we're creating views, we're still doing initial setup, so we always
2174 // use the pending DOM UI rather than any possibly existing committed one.
2175 if (render_manager_.pending_dom_ui()) {
2176 render_manager_.pending_dom_ui()->RenderViewCreated(render_view_host);
2177 }
2178
2179 if (entry->IsViewSourceMode()) {
2180 // Put the renderer in view source mode.
2181 render_view_host->Send(
2182 new ViewMsg_EnableViewSourceMode(render_view_host->routing_id()));
2183 }
[email protected]0666aef2009-05-13 19:48:082184
2185 view()->RenderViewCreated(render_view_host);
[email protected]420ae012009-04-24 05:16:322186}
2187
2188void TabContents::RenderViewReady(RenderViewHost* rvh) {
2189 if (rvh != render_view_host()) {
2190 // Don't notify the world, since this came from a renderer in the
2191 // background.
2192 return;
2193 }
2194
2195 NotifyConnected();
[email protected]9d8a4642009-07-29 17:25:302196 bool was_crashed = is_crashed();
[email protected]420ae012009-04-24 05:16:322197 SetIsCrashed(false);
[email protected]9d8a4642009-07-29 17:25:302198
2199 // Restore the focus to the tab (otherwise the focus will be on the top
2200 // window).
2201 if (was_crashed && !FocusLocationBarByDefault())
2202 Focus();
[email protected]420ae012009-04-24 05:16:322203}
2204
2205void TabContents::RenderViewGone(RenderViewHost* rvh) {
2206 // Ask the print preview if this renderer was valuable.
2207 if (!printing_.OnRenderViewGone(rvh))
2208 return;
2209 if (rvh != render_view_host()) {
2210 // The pending page's RenderViewHost is gone.
2211 return;
2212 }
2213
2214 SetIsLoading(false, NULL);
2215 NotifyDisconnected();
2216 SetIsCrashed(true);
2217
[email protected]ff9efc62010-02-18 22:10:542218 // Remove all infobars.
[email protected]e5430c92010-02-18 22:39:212219 for (int i = infobar_delegate_count() - 1; i >=0 ; --i)
[email protected]ff9efc62010-02-18 22:10:542220 RemoveInfoBar(GetInfoBarDelegateAt(i));
2221
[email protected]8e537302009-06-10 21:19:582222 // Tell the view that we've crashed so it can prepare the sad tab page.
[email protected]cf68b092010-01-15 20:05:552223 // Only do this if we're not in browser shutdown, so that TabContents
2224 // objects that are not in a browser (e.g., HTML dialogs) and thus are
2225 // visible do not flash a sad tab page.
2226 if (browser_shutdown::GetShutdownType() == browser_shutdown::NOT_VALID)
2227 view_->OnTabCrashed();
[email protected]420ae012009-04-24 05:16:322228
2229 // Hide any visible hung renderer warning for this web contents' process.
[email protected]e7b8d832009-11-20 22:41:002230 hung_renderer_dialog::HideForTabContents(this);
[email protected]420ae012009-04-24 05:16:322231}
2232
[email protected]2e4633c2009-07-09 16:58:062233void TabContents::RenderViewDeleted(RenderViewHost* rvh) {
2234 NotificationService::current()->Notify(
2235 NotificationType::RENDER_VIEW_HOST_DELETED,
2236 Source<TabContents>(this),
2237 Details<RenderViewHost>(rvh));
[email protected]fb7b79f72009-11-06 18:00:482238 render_manager_.RenderViewDeleted(rvh);
[email protected]2e4633c2009-07-09 16:58:062239}
2240
[email protected]420ae012009-04-24 05:16:322241void TabContents::DidNavigate(RenderViewHost* rvh,
2242 const ViewHostMsg_FrameNavigate_Params& params) {
[email protected]8030f012009-09-25 18:09:372243 int extra_invalidate_flags = 0;
2244
2245 if (PageTransition::IsMainFrame(params.transition)) {
[email protected]20ede072009-10-06 00:28:352246 bool was_bookmark_bar_visible = ShouldShowBookmarkBar();
[email protected]8030f012009-09-25 18:09:372247 bool was_extension_shelf_visible = IsExtensionShelfAlwaysVisible();
2248
[email protected]420ae012009-04-24 05:16:322249 render_manager_.DidNavigateMainFrame(rvh);
2250
[email protected]20ede072009-10-06 00:28:352251 if (was_bookmark_bar_visible != ShouldShowBookmarkBar())
[email protected]8030f012009-09-25 18:09:372252 extra_invalidate_flags |= INVALIDATE_BOOKMARK_BAR;
2253 if (was_extension_shelf_visible != IsExtensionShelfAlwaysVisible())
2254 extra_invalidate_flags |= INVALIDATE_EXTENSION_SHELF;
2255 }
2256
[email protected]420ae012009-04-24 05:16:322257 // Update the site of the SiteInstance if it doesn't have one yet.
2258 if (!GetSiteInstance()->has_site())
2259 GetSiteInstance()->SetSite(params.url);
2260
2261 // Need to update MIME type here because it's referred to in
2262 // UpdateNavigationCommands() called by RendererDidNavigate() to
2263 // determine whether or not to enable the encoding menu.
2264 // It's updated only for the main frame. For a subframe,
2265 // RenderView::UpdateURL does not set params.contents_mime_type.
2266 // (see http://code.google.com/p/chromium/issues/detail?id=2929 )
2267 // TODO(jungshik): Add a test for the encoding menu to avoid
2268 // regressing it again.
2269 if (PageTransition::IsMainFrame(params.transition))
2270 contents_mime_type_ = params.contents_mime_type;
2271
2272 NavigationController::LoadCommittedDetails details;
[email protected]8030f012009-09-25 18:09:372273 bool did_navigate = controller_.RendererDidNavigate(
2274 params, extra_invalidate_flags, &details);
[email protected]3e90d4a2009-07-03 17:38:392275
2276 // Update history. Note that this needs to happen after the entry is complete,
2277 // which WillNavigate[Main,Sub]Frame will do before this function is called.
[email protected]2e29d922009-10-22 22:13:352278 if (params.should_update_history &&
2279 (!delegate() || delegate()->ShouldAddNavigationsToHistory())) {
[email protected]3e90d4a2009-07-03 17:38:392280 // Most of the time, the displayURL matches the loaded URL, but for about:
2281 // URLs, we use a data: URL as the real value. We actually want to save
2282 // the about: URL to the history db and keep the data: URL hidden. This is
2283 // what the TabContents' URL getter does.
2284 UpdateHistoryForNavigation(GetURL(), details, params);
2285 }
2286
[email protected]76543b92009-08-31 17:27:452287 if (!did_navigate)
2288 return; // No navigation happened.
[email protected]420ae012009-04-24 05:16:322289
2290 // DO NOT ADD MORE STUFF TO THIS FUNCTION! Your component should either listen
2291 // for the appropriate notification (best) or you can add it to
2292 // DidNavigateMainFramePostCommit / DidNavigateAnyFramePostCommit (only if
2293 // necessary, please).
2294
2295 // Run post-commit tasks.
2296 if (details.is_main_frame)
2297 DidNavigateMainFramePostCommit(details, params);
2298 DidNavigateAnyFramePostCommit(rvh, details, params);
2299}
2300
2301void TabContents::UpdateState(RenderViewHost* rvh,
2302 int32 page_id,
2303 const std::string& state) {
2304 DCHECK(rvh == render_view_host());
2305
2306 // We must be prepared to handle state updates for any page, these occur
2307 // when the user is scrolling and entering form data, as well as when we're
2308 // leaving a page, in which case our state may have already been moved to
2309 // the next page. The navigation controller will look up the appropriate
2310 // NavigationEntry and update it when it is notified via the delegate.
2311
2312 int entry_index = controller_.GetEntryIndexWithPageID(
2313 GetSiteInstance(), page_id);
2314 if (entry_index < 0)
2315 return;
2316 NavigationEntry* entry = controller_.GetEntryAtIndex(entry_index);
2317
2318 if (state == entry->content_state())
2319 return; // Nothing to update.
2320 entry->set_content_state(state);
2321 controller_.NotifyEntryChanged(entry, entry_index);
2322}
2323
2324void TabContents::UpdateTitle(RenderViewHost* rvh,
2325 int32 page_id, const std::wstring& title) {
2326 // If we have a title, that's a pretty good indication that we've started
2327 // getting useful data.
2328 SetNotWaitingForResponse();
2329
2330 DCHECK(rvh == render_view_host());
2331 NavigationEntry* entry = controller_.GetEntryWithPageID(GetSiteInstance(),
2332 page_id);
2333 if (!entry || !UpdateTitleForEntry(entry, title))
2334 return;
2335
2336 // Broadcast notifications when the UI should be updated.
2337 if (entry == controller_.GetEntryAtOffset(0))
[email protected]f1cd5e82009-10-23 17:19:032338 NotifyNavigationStateChanged(INVALIDATE_TITLE);
[email protected]420ae012009-04-24 05:16:322339}
2340
[email protected]420ae012009-04-24 05:16:322341void TabContents::UpdateEncoding(RenderViewHost* render_view_host,
[email protected]41fc0322009-09-04 22:23:402342 const std::string& encoding) {
[email protected]420ae012009-04-24 05:16:322343 set_encoding(encoding);
2344}
2345
2346void TabContents::UpdateTargetURL(int32 page_id, const GURL& url) {
2347 if (delegate())
2348 delegate()->UpdateTargetURL(this, url);
2349}
2350
2351void TabContents::UpdateThumbnail(const GURL& url,
2352 const SkBitmap& bitmap,
2353 const ThumbnailScore& score) {
2354 // Tell History about this thumbnail
[email protected]449478302009-06-09 20:04:282355 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kThumbnailStore)) {
[email protected]72d0fc72009-07-09 17:30:492356 if (!profile()->IsOffTheRecord())
[email protected]42339402009-07-17 17:27:192357 profile()->GetThumbnailStore()->SetPageThumbnail(url, bitmap,
2358 score, true);
[email protected]449478302009-06-09 20:04:282359 } else {
2360 HistoryService* hs;
2361 if (!profile()->IsOffTheRecord() &&
2362 (hs = profile()->GetHistoryService(Profile::IMPLICIT_ACCESS))) {
2363 hs->SetPageThumbnail(url, bitmap, score);
2364 }
[email protected]420ae012009-04-24 05:16:322365 }
2366}
2367
[email protected]8922e1f2009-10-03 05:01:262368void TabContents::UpdateInspectorSettings(const std::string& raw_settings) {
[email protected]0df30122009-06-03 12:13:082369 profile()->GetPrefs()->SetString(prefs::kWebKitInspectorSettings,
[email protected]8922e1f2009-10-03 05:01:262370 UTF8ToWide(raw_settings));
[email protected]0df30122009-06-03 12:13:082371}
2372
[email protected]420ae012009-04-24 05:16:322373void TabContents::Close(RenderViewHost* rvh) {
[email protected]07707302009-11-06 00:50:292374 // The UI may be in an event-tracking loop, such as between the
2375 // mouse-down and mouse-up in text selection or a button click.
2376 // Defer the close until after tracking is complete, so that we
2377 // don't free objects out from under the UI.
2378 // TODO(shess): This could probably be integrated with the
2379 // IsDoingDrag() test below. Punting for now because I need more
2380 // research to understand how this impacts platforms other than Mac.
2381 // TODO(shess): This could get more fine-grained. For instance,
2382 // closing a tab in another window while selecting text in the
2383 // current window's Omnibox should be just fine.
2384 if (view()->IsEventTracking()) {
2385 view()->CloseTabAfterEventTracking();
2386 return;
2387 }
2388
[email protected]24a4d1062009-07-10 23:10:422389 // If we close the tab while we're in the middle of a drag, we'll crash.
2390 // Instead, cancel the drag and close it as soon as the drag ends.
2391 if (view()->IsDoingDrag()) {
2392 view()->CancelDragAndCloseTab();
2393 return;
2394 }
2395
[email protected]420ae012009-04-24 05:16:322396 // Ignore this if it comes from a RenderViewHost that we aren't showing.
2397 if (delegate() && rvh == render_view_host())
2398 delegate()->CloseContents(this);
2399}
2400
2401void TabContents::RequestMove(const gfx::Rect& new_bounds) {
2402 if (delegate() && delegate()->IsPopup(this))
2403 delegate()->MoveContents(this, new_bounds);
2404}
2405
[email protected]7ab1e7d62009-10-14 23:32:012406void TabContents::DidStartLoading() {
[email protected]420ae012009-04-24 05:16:322407 SetIsLoading(true, NULL);
2408}
2409
[email protected]7ab1e7d62009-10-14 23:32:012410void TabContents::DidStopLoading() {
[email protected]420ae012009-04-24 05:16:322411 scoped_ptr<LoadNotificationDetails> details;
2412
2413 NavigationEntry* entry = controller_.GetActiveEntry();
2414 // An entry may not exist for a stop when loading an initial blank page or
2415 // if an iframe injected by script into a blank page finishes loading.
2416 if (entry) {
[email protected]420ae012009-04-24 05:16:322417 base::TimeDelta elapsed = base::TimeTicks::Now() - current_load_start_;
2418
2419 details.reset(new LoadNotificationDetails(
[email protected]ebe89e062009-08-13 23:16:542420 entry->virtual_url(),
[email protected]420ae012009-04-24 05:16:322421 entry->transition_type(),
2422 elapsed,
2423 &controller_,
2424 controller_.GetCurrentEntryIndex()));
2425 }
2426
2427 // Tell PasswordManager we've finished a page load, which serves as a
2428 // green light to save pending passwords and reset itself.
2429 GetPasswordManager()->DidStopLoading();
2430
2431 SetIsLoading(false, details.get());
2432}
2433
[email protected]420ae012009-04-24 05:16:322434void TabContents::DidRedirectProvisionalLoad(int32 page_id,
2435 const GURL& source_url,
2436 const GURL& target_url) {
2437 NavigationEntry* entry;
2438 if (page_id == -1)
2439 entry = controller_.pending_entry();
2440 else
2441 entry = controller_.GetEntryWithPageID(GetSiteInstance(), page_id);
2442 if (!entry || entry->url() != source_url)
2443 return;
2444 entry->set_url(target_url);
2445}
2446
[email protected]420ae012009-04-24 05:16:322447void TabContents::RequestOpenURL(const GURL& url, const GURL& referrer,
2448 WindowOpenDisposition disposition) {
2449 if (render_manager_.dom_ui()) {
2450 // When we're a DOM UI, it will provide a page transition type for us (this
2451 // is so the new tab page can specify AUTO_BOOKMARK for automatically
2452 // generated suggestions).
2453 //
2454 // Note also that we hide the referrer for DOM UI pages. We don't really
[email protected]60e448982009-05-06 04:21:162455 // want web sites to see a referrer of "chrome://blah" (and some
2456 // chrome: URLs might have search terms or other stuff we don't want to
[email protected]420ae012009-04-24 05:16:322457 // send to the site), so we send no referrer.
2458 OpenURL(url, GURL(), disposition,
2459 render_manager_.dom_ui()->link_transition_type());
2460 } else {
2461 OpenURL(url, referrer, disposition, PageTransition::LINK);
2462 }
2463}
2464
[email protected]1ee614862009-07-06 20:29:182465void TabContents::DomOperationResponse(const std::string& json_string,
2466 int automation_id) {
2467}
2468
[email protected]420ae012009-04-24 05:16:322469void TabContents::ProcessDOMUIMessage(const std::string& message,
[email protected]e4dad9fb2009-10-06 18:15:582470 const Value* content,
[email protected]bb64b512010-04-02 21:01:392471 const GURL& source_url,
[email protected]811bfe32009-07-01 08:46:252472 int request_id,
2473 bool has_callback) {
[email protected]420ae012009-04-24 05:16:322474 if (!render_manager_.dom_ui()) {
[email protected]c2e74fe82009-09-03 17:57:442475 // This can happen if someone uses window.open() to open an extension URL
2476 // from a non-extension context.
2477 render_view_host()->BlockExtensionRequest(request_id);
[email protected]420ae012009-04-24 05:16:322478 return;
2479 }
[email protected]bb64b512010-04-02 21:01:392480 render_manager_.dom_ui()->ProcessDOMUIMessage(message, content, source_url,
2481 request_id,
[email protected]811bfe32009-07-01 08:46:252482 has_callback);
[email protected]420ae012009-04-24 05:16:322483}
2484
2485void TabContents::ProcessExternalHostMessage(const std::string& message,
2486 const std::string& origin,
2487 const std::string& target) {
2488 if (delegate())
2489 delegate()->ForwardMessageToExternalHost(message, origin, target);
2490}
2491
[email protected]cdaf8d02010-03-30 19:52:472492void TabContents::RunFileChooser(
2493 const ViewHostMsg_RunFileChooser_Params &params) {
[email protected]420ae012009-04-24 05:16:322494 if (!select_file_dialog_.get())
2495 select_file_dialog_ = SelectFileDialog::Create(this);
[email protected]cdaf8d02010-03-30 19:52:472496
2497 SelectFileDialog::Type dialog_type;
2498 switch (params.mode) {
2499 case ViewHostMsg_RunFileChooser_Params::Open:
2500 dialog_type = SelectFileDialog::SELECT_OPEN_FILE;
2501 break;
2502 case ViewHostMsg_RunFileChooser_Params::OpenMultiple:
2503 dialog_type = SelectFileDialog::SELECT_OPEN_MULTI_FILE;
2504 break;
2505 case ViewHostMsg_RunFileChooser_Params::Save:
2506 dialog_type = SelectFileDialog::SELECT_SAVEAS_FILE;
2507 break;
2508 default:
[email protected]bed31952010-03-30 20:07:592509 dialog_type = SelectFileDialog::SELECT_OPEN_FILE; // Prevent warning.
[email protected]cdaf8d02010-03-30 19:52:472510 NOTREACHED();
2511 }
2512 select_file_dialog_->SelectFile(dialog_type, params.title,
2513 params.default_file_name,
[email protected]420ae012009-04-24 05:16:322514 NULL, 0, FILE_PATH_LITERAL(""),
2515 view_->GetTopLevelNativeWindow(), NULL);
2516}
2517
2518void TabContents::RunJavaScriptMessage(
2519 const std::wstring& message,
2520 const std::wstring& default_prompt,
2521 const GURL& frame_url,
2522 const int flags,
2523 IPC::Message* reply_msg,
2524 bool* did_suppress_message) {
2525 // Suppress javascript messages when requested and when inside a constrained
2526 // popup window (because that activates them and breaks them out of the
2527 // constrained window jail).
[email protected]91ed49a2009-07-07 21:29:552528 // Also suppress messages when showing an interstitial. The interstitial is
2529 // shown over the previous page, we don't want the hidden page dialogs to
2530 // interfere with the interstitial.
2531 bool suppress_this_message = suppress_javascript_messages_ ||
2532 showing_interstitial_page();
[email protected]420ae012009-04-24 05:16:322533 if (delegate())
2534 suppress_this_message |=
[email protected]b6c874582009-05-08 19:38:312535 (delegate()->GetConstrainingContents(this) != this);
[email protected]420ae012009-04-24 05:16:322536
2537 *did_suppress_message = suppress_this_message;
2538
2539 if (!suppress_this_message) {
2540 base::TimeDelta time_since_last_message(
2541 base::TimeTicks::Now() - last_javascript_message_dismissal_);
2542 bool show_suppress_checkbox = false;
2543 // Show a checkbox offering to suppress further messages if this message is
2544 // being displayed within kJavascriptMessageExpectedDelay of the last one.
2545 if (time_since_last_message <
2546 base::TimeDelta::FromMilliseconds(kJavascriptMessageExpectedDelay))
2547 show_suppress_checkbox = true;
2548
[email protected]d82ed61e2009-06-16 02:46:222549 RunJavascriptMessageBox(this, frame_url, flags, message, default_prompt,
[email protected]420ae012009-04-24 05:16:322550 show_suppress_checkbox, reply_msg);
2551 } else {
2552 // If we are suppressing messages, just reply as is if the user immediately
2553 // pressed "Cancel".
[email protected]beb440c2009-11-06 04:08:542554 OnMessageBoxClosed(reply_msg, false, std::wstring());
[email protected]420ae012009-04-24 05:16:322555 }
2556}
2557
2558void TabContents::RunBeforeUnloadConfirm(const std::wstring& message,
2559 IPC::Message* reply_msg) {
[email protected]7ab1e7d62009-10-14 23:32:012560 is_showing_before_unload_dialog_ = true;
[email protected]d82ed61e2009-06-16 02:46:222561 RunBeforeUnloadDialog(this, message, reply_msg);
[email protected]420ae012009-04-24 05:16:322562}
2563
2564void TabContents::ShowModalHTMLDialog(const GURL& url, int width, int height,
2565 const std::string& json_arguments,
2566 IPC::Message* reply_msg) {
2567 if (delegate()) {
2568 HtmlDialogUIDelegate* dialog_delegate =
2569 new ModalHtmlDialogDelegate(url, width, height, json_arguments,
[email protected]d82ed61e2009-06-16 02:46:222570 reply_msg, this);
[email protected]420ae012009-04-24 05:16:322571 delegate()->ShowHtmlDialog(dialog_delegate, NULL);
2572 }
2573}
2574
2575void TabContents::PasswordFormsSeen(
[email protected]ed3fb032009-06-16 19:50:562576 const std::vector<webkit_glue::PasswordForm>& forms) {
[email protected]420ae012009-04-24 05:16:322577 GetPasswordManager()->PasswordFormsSeen(forms);
2578}
2579
[email protected]420ae012009-04-24 05:16:322580// Checks to see if we should generate a keyword based on the OSDD, and if
2581// necessary uses TemplateURLFetcher to download the OSDD and create a keyword.
2582void TabContents::PageHasOSDD(RenderViewHost* render_view_host,
2583 int32 page_id, const GURL& url,
2584 bool autodetected) {
2585 // Make sure page_id is the current page, and the TemplateURLModel is loaded.
2586 DCHECK(url.is_valid());
2587 if (!IsActiveEntry(page_id))
2588 return;
2589 TemplateURLModel* url_model = profile()->GetTemplateURLModel();
2590 if (!url_model)
2591 return;
2592 if (!url_model->loaded()) {
2593 url_model->Load();
2594 return;
2595 }
2596 if (!profile()->GetTemplateURLFetcher())
2597 return;
2598
2599 if (profile()->IsOffTheRecord())
2600 return;
2601
2602 const NavigationEntry* entry = controller_.GetLastCommittedEntry();
2603 DCHECK(entry);
2604
2605 const NavigationEntry* base_entry = entry;
2606 if (IsFormSubmit(base_entry)) {
2607 // If the current page is a form submit, find the last page that was not
2608 // a form submit and use its url to generate the keyword from.
2609 int index = controller_.last_committed_entry_index() - 1;
2610 while (index >= 0 && IsFormSubmit(controller_.GetEntryAtIndex(index)))
2611 index--;
2612 if (index >= 0)
2613 base_entry = controller_.GetEntryAtIndex(index);
2614 else
2615 base_entry = NULL;
2616 }
2617
2618 // We want to use the user typed URL if available since that represents what
2619 // the user typed to get here, and fall back on the regular URL if not.
2620 if (!base_entry)
2621 return;
2622 GURL keyword_url = base_entry->user_typed_url().is_valid() ?
2623 base_entry->user_typed_url() : base_entry->url();
2624 if (!keyword_url.is_valid())
2625 return;
2626 std::wstring keyword = TemplateURLModel::GenerateKeyword(keyword_url,
2627 autodetected);
[email protected]a88dbdd52010-01-15 19:25:532628
2629 // For JS added OSDD empty keyword is OK because we will generate keyword
2630 // later from OSDD content.
2631 if (autodetected && keyword.empty())
[email protected]420ae012009-04-24 05:16:322632 return;
2633 const TemplateURL* template_url =
2634 url_model->GetTemplateURLForKeyword(keyword);
2635 if (template_url && (!template_url->safe_for_autoreplace() ||
2636 template_url->originating_url() == url)) {
2637 // Either there is a user created TemplateURL for this keyword, or the
2638 // keyword has the same OSDD url and we've parsed it.
2639 return;
2640 }
2641
2642 // Download the OpenSearch description document. If this is successful a
2643 // new keyword will be created when done.
[email protected]420ae012009-04-24 05:16:322644 profile()->GetTemplateURLFetcher()->ScheduleDownload(
2645 keyword,
2646 url,
2647 base_entry->favicon().url(),
[email protected]d0cc9fb52009-06-23 02:26:162648 this,
[email protected]420ae012009-04-24 05:16:322649 autodetected);
2650}
2651
[email protected]420ae012009-04-24 05:16:322652GURL TabContents::GetAlternateErrorPageURL() const {
2653 GURL url;
2654 // Disable alternate error pages when in OffTheRecord/Incognito mode.
2655 if (profile()->IsOffTheRecord())
2656 return url;
2657
2658 PrefService* prefs = profile()->GetPrefs();
2659 DCHECK(prefs);
2660 if (prefs->GetBoolean(prefs::kAlternateErrorPagesEnabled)) {
[email protected]a33457b82009-09-21 23:07:152661 url = google_util::AppendGoogleLocaleParam(
2662 GURL(google_util::kLinkDoctorBaseURL));
[email protected]420ae012009-04-24 05:16:322663 url = google_util::AppendGoogleTLDParam(url);
2664 }
2665 return url;
2666}
2667
2668WebPreferences TabContents::GetWebkitPrefs() {
[email protected]6c32ce72010-03-08 05:18:062669 Profile* profile = render_view_host()->process()->profile();
[email protected]7ccddb8c2009-08-04 17:36:552670 bool is_dom_ui = false;
[email protected]6c32ce72010-03-08 05:18:062671 return RenderViewHostDelegateHelper::GetWebkitPrefs(profile, is_dom_ui);
[email protected]420ae012009-04-24 05:16:322672}
2673
[email protected]fa1cf0b82010-01-15 21:49:442674void TabContents::OnIgnoredUIEvent() {
2675 if (constrained_window_count()) {
2676 ConstrainedWindow* window = *constrained_window_begin();
2677 window->FocusConstrainedWindow();
2678 }
2679}
2680
[email protected]420ae012009-04-24 05:16:322681void TabContents::OnJSOutOfMemory() {
2682 AddInfoBar(new SimpleAlertInfoBarDelegate(
[email protected]938e1f92010-04-01 18:09:422683 this, l10n_util::GetString(IDS_JS_OUT_OF_MEMORY_PROMPT), NULL, true));
[email protected]420ae012009-04-24 05:16:322684}
2685
[email protected]420ae012009-04-24 05:16:322686void TabContents::OnCrossSiteResponse(int new_render_process_host_id,
2687 int new_request_id) {
[email protected]57c6a652009-05-04 07:58:342688 // Allows the TabContents to react when a cross-site response is ready to be
[email protected]420ae012009-04-24 05:16:322689 // delivered to a pending RenderViewHost. We must first run the onunload
2690 // handler of the old RenderViewHost before we can allow it to proceed.
2691 render_manager_.OnCrossSiteResponse(new_render_process_host_id,
2692 new_request_id);
2693}
2694
2695bool TabContents::CanBlur() const {
2696 return delegate() ? delegate()->CanBlur() : true;
2697}
2698
2699gfx::Rect TabContents::GetRootWindowResizerRect() const {
2700 if (delegate())
2701 return delegate()->GetRootWindowResizerRect();
2702 return gfx::Rect();
2703}
2704
2705void TabContents::RendererUnresponsive(RenderViewHost* rvh,
2706 bool is_during_unload) {
2707 if (is_during_unload) {
2708 // Hang occurred while firing the beforeunload/unload handler.
2709 // Pretend the handler fired so tab closing continues as if it had.
[email protected]829e7612009-04-25 01:15:112710 rvh->set_sudden_termination_allowed(true);
[email protected]420ae012009-04-24 05:16:322711
2712 if (!render_manager_.ShouldCloseTabOnUnresponsiveRenderer())
2713 return;
2714
2715 // If the tab hangs in the beforeunload/unload handler there's really
2716 // nothing we can do to recover. Pretend the unload listeners have
2717 // all fired and close the tab. If the hang is in the beforeunload handler
2718 // then the user will not have the option of cancelling the close.
2719 Close(rvh);
2720 return;
2721 }
2722
2723 if (render_view_host() && render_view_host()->IsRenderViewLive())
[email protected]e7b8d832009-11-20 22:41:002724 hung_renderer_dialog::ShowForTabContents(this);
[email protected]420ae012009-04-24 05:16:322725}
2726
2727void TabContents::RendererResponsive(RenderViewHost* render_view_host) {
[email protected]e7b8d832009-11-20 22:41:002728 hung_renderer_dialog::HideForTabContents(this);
[email protected]420ae012009-04-24 05:16:322729}
2730
2731void TabContents::LoadStateChanged(const GURL& url,
[email protected]094e5b22009-09-25 04:23:562732 net::LoadState load_state,
2733 uint64 upload_position,
2734 uint64 upload_size) {
[email protected]420ae012009-04-24 05:16:322735 load_state_ = load_state;
[email protected]094e5b22009-09-25 04:23:562736 upload_position_ = upload_position;
2737 upload_size_ = upload_size;
[email protected]d686e812009-06-03 19:10:292738 std::wstring languages =
2739 profile()->GetPrefs()->GetString(prefs::kAcceptLanguages);
[email protected]d686e812009-06-03 19:10:292740 std::string host = url.host();
[email protected]ce85f602009-11-07 01:34:532741 load_state_host_ =
2742 net::IDNToUnicode(host.c_str(), host.size(), languages, NULL);
[email protected]420ae012009-04-24 05:16:322743 if (load_state_ == net::LOAD_STATE_READING_RESPONSE)
2744 SetNotWaitingForResponse();
2745 if (is_loading())
[email protected]c9cd2222009-05-06 05:16:502746 NotifyNavigationStateChanged(INVALIDATE_LOAD | INVALIDATE_TAB);
[email protected]420ae012009-04-24 05:16:322747}
2748
[email protected]1ee614862009-07-06 20:29:182749bool TabContents::IsExternalTabContainer() const {
2750 if (!delegate())
2751 return false;
2752
2753 return delegate()->IsExternalTabContainer();
2754}
2755
[email protected]1ee614862009-07-06 20:29:182756void TabContents::DidInsertCSS() {
2757 // This RVHDelegate function is used for extensions and not us.
[email protected]420ae012009-04-24 05:16:322758}
2759
[email protected]130efb02009-09-18 18:54:352760void TabContents::FocusedNodeChanged() {
2761 NotificationService::current()->Notify(
2762 NotificationType::FOCUS_CHANGED_IN_PAGE,
2763 Source<RenderViewHost>(render_view_host()),
2764 NotificationService::NoDetails());
2765}
2766
[email protected]420ae012009-04-24 05:16:322767void TabContents::FileSelected(const FilePath& path,
2768 int index, void* params) {
[email protected]cdaf8d02010-03-30 19:52:472769 std::vector<FilePath> files;
2770 files.push_back(path);
2771 render_view_host()->FilesSelectedInChooser(files);
[email protected]420ae012009-04-24 05:16:322772}
2773
2774void TabContents::MultiFilesSelected(const std::vector<FilePath>& files,
2775 void* params) {
[email protected]cdaf8d02010-03-30 19:52:472776 render_view_host()->FilesSelectedInChooser(files);
[email protected]420ae012009-04-24 05:16:322777}
2778
2779void TabContents::FileSelectionCanceled(void* params) {
2780 // If the user cancels choosing a file to upload we pass back an
2781 // empty vector.
[email protected]cdaf8d02010-03-30 19:52:472782 render_view_host()->FilesSelectedInChooser(std::vector<FilePath>());
[email protected]420ae012009-04-24 05:16:322783}
2784
2785void TabContents::BeforeUnloadFiredFromRenderManager(
2786 bool proceed,
2787 bool* proceed_to_fire_unload) {
2788 if (delegate())
2789 delegate()->BeforeUnloadFired(this, proceed, proceed_to_fire_unload);
2790}
2791
2792void TabContents::UpdateRenderViewSizeForRenderManager() {
2793 // TODO(brettw) this is a hack. See TabContentsView::SizeContents.
[email protected]0323ee42010-02-17 22:03:262794 gfx::Size size = view_->GetContainerSize();
2795 // 0x0 isn't a valid window size (minimal window size is 1x1) but it may be
2796 // here during container initialization and normal window size will be set
2797 // later. In case of tab duplication this resizing to 0x0 prevents setting
2798 // normal size later so just ignore it.
2799 if (!size.IsEmpty())
2800 view_->SizeContents(size);
[email protected]420ae012009-04-24 05:16:322801}
2802
2803DOMUI* TabContents::CreateDOMUIForRenderManager(const GURL& url) {
[email protected]d82ed61e2009-06-16 02:46:222804 return DOMUIFactory::CreateDOMUIForURL(this, url);
[email protected]420ae012009-04-24 05:16:322805}
2806
2807NavigationEntry*
2808TabContents::GetLastCommittedNavigationEntryForRenderManager() {
2809 return controller_.GetLastCommittedEntry();
2810}
2811
2812bool TabContents::CreateRenderViewForRenderManager(
2813 RenderViewHost* render_view_host) {
[email protected]811bfe32009-07-01 08:46:252814 // If the pending navigation is to a DOMUI, tell the RenderView about any
2815 // bindings it will need enabled.
[email protected]420ae012009-04-24 05:16:322816 if (render_manager_.pending_dom_ui())
[email protected]811bfe32009-07-01 08:46:252817 render_view_host->AllowBindings(
2818 render_manager_.pending_dom_ui()->bindings());
[email protected]1adff062009-06-02 18:39:552819
[email protected]420ae012009-04-24 05:16:322820 RenderWidgetHostView* rwh_view = view_->CreateViewForWidget(render_view_host);
[email protected]1a98a932009-11-17 00:12:522821
2822 scoped_refptr<URLRequestContextGetter> request_context = request_context_;
2823 if (!request_context.get())
2824 request_context = profile()->GetRequestContext();
2825
2826 if (!render_view_host->CreateRenderView(request_context))
[email protected]420ae012009-04-24 05:16:322827 return false;
2828
2829 // Now that the RenderView has been created, we need to tell it its size.
2830 rwh_view->SetSize(view_->GetContainerSize());
2831
2832 UpdateMaxPageIDIfNecessary(render_view_host->site_instance(),
2833 render_view_host);
2834 return true;
2835}
2836
2837void TabContents::Observe(NotificationType type,
2838 const NotificationSource& source,
2839 const NotificationDetails& details) {
2840 switch (type.value) {
2841 case NotificationType::BOOKMARK_MODEL_LOADED:
2842 // BookmarkModel finished loading, fall through to update starred state.
2843 case NotificationType::URLS_STARRED: {
2844 // Somewhere, a URL has been starred.
2845 // Ignore notifications for profiles other than our current one.
2846 Profile* source_profile = Source<Profile>(source).ptr();
2847 if (!source_profile || !source_profile->IsSameProfile(profile()))
2848 return;
2849
2850 UpdateStarredStateForCurrentURL();
2851 break;
2852 }
2853 case NotificationType::PREF_CHANGED: {
2854 std::wstring* pref_name_in = Details<std::wstring>(details).ptr();
2855 DCHECK(Source<PrefService>(source).ptr() == profile()->GetPrefs());
2856 if (*pref_name_in == prefs::kAlternateErrorPagesEnabled) {
2857 UpdateAlternateErrorPageURL();
2858 } else if (*pref_name_in == prefs::kDefaultCharset ||
2859 StartsWithASCII(WideToUTF8(*pref_name_in), "webkit.webprefs.", true)
2860 ) {
2861 UpdateWebPreferences();
2862 } else {
2863 NOTREACHED() << "unexpected pref change notification" << *pref_name_in;
2864 }
2865 break;
2866 }
2867 case NotificationType::RENDER_WIDGET_HOST_DESTROYED:
2868 view_->RenderWidgetHostDestroyed(Source<RenderWidgetHost>(source).ptr());
2869 break;
2870
2871 case NotificationType::NAV_ENTRY_COMMITTED: {
2872 DCHECK(&controller_ == Source<NavigationController>(source).ptr());
2873
2874 NavigationController::LoadCommittedDetails& committed_details =
2875 *(Details<NavigationController::LoadCommittedDetails>(details).ptr());
2876 ExpireInfoBars(committed_details);
2877 break;
2878 }
2879
[email protected]d3ba77272009-09-03 00:06:092880#if defined(OS_LINUX)
2881 case NotificationType::BROWSER_THEME_CHANGED: {
[email protected]7ff431e2010-01-07 18:18:542882 renderer_preferences_util::UpdateFromSystemSettings(
2883 &renderer_preferences_, profile());
[email protected]d3ba77272009-09-03 00:06:092884 render_view_host()->SyncRendererPrefs();
2885 break;
2886 }
2887#endif
2888
[email protected]6c32ce72010-03-08 05:18:062889 case NotificationType::USER_STYLE_SHEET_UPDATED:
2890 UpdateWebPreferences();
2891 break;
2892
[email protected]79580c62010-02-02 02:36:252893 case NotificationType::CONTENT_SETTINGS_CHANGED: {
2894 Details<HostContentSettingsMap::ContentSettingsDetails>
2895 settings_details(details);
[email protected]79580c62010-02-02 02:36:252896 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]da0c8e92010-02-09 23:25:492897 GURL entry_url;
2898 std::string host;
2899 if (entry) {
2900 entry_url = entry->url();
2901 host = entry_url.host();
2902 }
[email protected]79580c62010-02-02 02:36:252903 Source<HostContentSettingsMap> content_settings(source);
2904 if (settings_details.ptr()->host().empty() ||
2905 settings_details.ptr()->host() == host) {
2906 render_view_host()->SendContentSettings(host,
[email protected]da0c8e92010-02-09 23:25:492907 content_settings.ptr()->GetContentSettings(entry_url));
[email protected]79580c62010-02-02 02:36:252908 }
2909 break;
2910 }
2911
[email protected]a888b29e62010-04-01 13:38:572912 case NotificationType::EXTENSION_LOADED: {
2913 if (!app_extension_ && !app_extension_for_current_page_ &&
2914 Source<Profile>(source).ptr() == profile()) {
2915 UpdateAppExtensionForCurrentPage();
2916 if (app_extension_for_current_page_)
2917 NotifyNavigationStateChanged(INVALIDATE_TAB);
2918 }
2919 break;
2920 }
2921
2922 case NotificationType::EXTENSION_UNLOADED:
2923 case NotificationType::EXTENSION_UNLOADED_DISABLED: {
2924 if (app_extension_for_current_page_ ==
2925 Details<Extension>(details).ptr()) {
2926 app_extension_for_current_page_ = NULL;
2927 UpdateAppExtensionForCurrentPage();
2928 NotifyNavigationStateChanged(INVALIDATE_TAB);
2929 }
2930 break;
2931 }
2932
[email protected]420ae012009-04-24 05:16:322933 default:
2934 NOTREACHED();
2935 }
2936}
[email protected]aacd7cc2009-09-02 14:51:102937
[email protected]a888b29e62010-04-01 13:38:572938void TabContents::UpdateAppExtensionIcon(Extension* extension) {
2939 app_extension_icon_.reset();
2940 if (extension) {
2941 app_extension_image_loader_.reset(new ImageLoadingTracker(this));
2942 app_extension_image_loader_->LoadImage(
2943 extension,
2944 extension->GetIconPath(Extension::EXTENSION_ICON_SMALLISH),
2945 gfx::Size(Extension::EXTENSION_ICON_SMALLISH,
2946 Extension::EXTENSION_ICON_SMALLISH),
2947 ImageLoadingTracker::CACHE);
2948 } else {
2949 app_extension_image_loader_.reset(NULL);
2950 }
2951}
2952
2953Extension* TabContents::GetExtensionContaining(const GURL& url) {
2954 ExtensionsService* extensions_service = profile()->GetExtensionsService();
2955 if (!extensions_service)
2956 return NULL;
2957
2958 Extension* extension = extensions_service->GetExtensionByURL(url);
2959 return extension ?
2960 extension : extensions_service->GetExtensionByWebExtent(url);
2961}
2962
2963void TabContents::UpdateAppExtensionForCurrentPage() {
2964 if (app_extension_) {
2965 // Tab has an explicit app extension; nothing to do.
2966 return;
2967 }
2968
2969 // Check the current extension before iterating through all extensions.
2970 if (app_extension_for_current_page_ &&
2971 app_extension_for_current_page_->web_extent().ContainsURL(GetURL())) {
2972 return;
2973 }
2974
2975 app_extension_for_current_page_ = GetExtensionContaining(GetURL());
2976 UpdateAppExtensionIcon(app_extension_for_current_page_);
2977}
2978
2979void TabContents::OnImageLoaded(SkBitmap* image, ExtensionResource resource,
2980 int index) {
2981 if (image) {
2982 app_extension_icon_ = *image;
2983 NotifyNavigationStateChanged(INVALIDATE_TAB);
2984 }
2985}
2986
[email protected]beb440c2009-11-06 04:08:542987std::wstring TabContents::GetMessageBoxTitle(const GURL& frame_url,
2988 bool is_alert) {
2989 if (!frame_url.has_host())
2990 return l10n_util::GetString(
2991 is_alert ? IDS_JAVASCRIPT_ALERT_DEFAULT_TITLE
2992 : IDS_JAVASCRIPT_MESSAGEBOX_DEFAULT_TITLE);
2993
2994 // We really only want the scheme, hostname, and port.
2995 GURL::Replacements replacements;
2996 replacements.ClearUsername();
2997 replacements.ClearPassword();
2998 replacements.ClearPath();
2999 replacements.ClearQuery();
3000 replacements.ClearRef();
3001 GURL clean_url = frame_url.ReplaceComponents(replacements);
3002
3003 // TODO(brettw) it should be easier than this to do the correct language
3004 // handling without getting the accept language from the profile.
3005 std::wstring base_address = gfx::ElideUrl(clean_url, gfx::Font(), 0,
3006 profile()->GetPrefs()->GetString(prefs::kAcceptLanguages));
3007 // Force URL to have LTR directionality.
[email protected]7cf1b6ce2010-03-20 06:37:013008 if (base::i18n::IsRTL())
3009 base::i18n::WrapStringWithLTRFormatting(&base_address);
[email protected]beb440c2009-11-06 04:08:543010
3011 return l10n_util::GetStringF(
3012 is_alert ? IDS_JAVASCRIPT_ALERT_TITLE : IDS_JAVASCRIPT_MESSAGEBOX_TITLE,
3013 base_address);
3014}
3015
3016gfx::NativeWindow TabContents::GetMessageBoxRootWindow() {
3017 return view_->GetTopLevelNativeWindow();
3018}
3019
3020void TabContents::OnMessageBoxClosed(IPC::Message* reply_msg,
3021 bool success,
3022 const std::wstring& prompt) {
3023 last_javascript_message_dismissal_ = base::TimeTicks::Now();
3024 if (is_showing_before_unload_dialog_ && !success) {
3025 // If a beforeunload dialog is canceled, we need to stop the throbber from
3026 // spinning, since we forced it to start spinning in Navigate.
3027 DidStopLoading();
3028
3029 tab_close_start_time_ = base::TimeTicks();
3030 }
3031 is_showing_before_unload_dialog_ = false;
3032 render_view_host()->JavaScriptMessageBoxClosed(reply_msg, success, prompt);
3033}
3034
3035void TabContents::SetSuppressMessageBoxes(bool suppress_message_boxes) {
3036 set_suppress_javascript_messages(suppress_message_boxes);
3037}
3038
[email protected]41fc0322009-09-04 22:23:403039void TabContents::set_encoding(const std::string& encoding) {
[email protected]aacd7cc2009-09-02 14:51:103040 encoding_ = CharacterEncoding::GetCanonicalEncodingNameByAliasName(encoding);
3041}
[email protected]f45d2a72010-03-08 23:28:353042
3043void TabContents::SetAppIcon(const SkBitmap& app_icon) {
3044 app_icon_ = app_icon;
3045 NotifyNavigationStateChanged(INVALIDATE_TITLE);
3046}