blob: 8f49bb1d2a58722c3be6ee5e3da5d5df6414fdbd [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]726bdbd72010-04-05 17:08:071208 if (!find_text_.empty())
1209 previous_find_text_ = find_text_;
[email protected]101c90e2009-10-26 21:14:441210 find_text_.clear();
[email protected]6e95706d2009-04-23 22:55:011211 find_op_aborted_ = true;
[email protected]e491f1c2009-05-22 20:28:121212 last_search_result_ = FindNotificationDetails();
[email protected]5bc8fe92010-03-11 18:19:001213 render_view_host()->StopFinding(selection_action);
[email protected]6e95706d2009-04-23 22:55:011214}
1215
[email protected]420ae012009-04-24 05:16:321216void TabContents::OnSavePage() {
1217 // If we can not save the page, try to download it.
1218 if (!SavePackage::IsSavableContents(contents_mime_type())) {
1219 DownloadManager* dlm = profile()->GetDownloadManager();
1220 const GURL& current_page_url = GetURL();
1221 if (dlm && current_page_url.is_valid())
[email protected]d82ed61e2009-06-16 02:46:221222 dlm->DownloadUrl(current_page_url, GURL(), "", this);
[email protected]420ae012009-04-24 05:16:321223 return;
1224 }
1225
1226 Stop();
1227
1228 // Create the save package and possibly prompt the user for the name to save
1229 // the page as. The user prompt is an asynchronous operation that runs on
1230 // another thread.
[email protected]d82ed61e2009-06-16 02:46:221231 save_package_ = new SavePackage(this);
[email protected]420ae012009-04-24 05:16:321232 save_package_->GetSaveInfo();
1233}
1234
1235// Used in automated testing to bypass prompting the user for file names.
1236// Instead, the names and paths are hard coded rather than running them through
1237// file name sanitation and extension / mime checking.
[email protected]828cabe2009-09-26 22:47:111238bool TabContents::SavePage(const FilePath& main_file, const FilePath& dir_path,
[email protected]420ae012009-04-24 05:16:321239 SavePackage::SavePackageType save_type) {
1240 // Stop the page from navigating.
1241 Stop();
1242
[email protected]828cabe2009-09-26 22:47:111243 save_package_ = new SavePackage(this, save_type, main_file, dir_path);
[email protected]cdf6cc82009-08-04 21:48:591244 return save_package_->Init();
[email protected]420ae012009-04-24 05:16:321245}
1246
[email protected]89d6e6e2009-12-03 23:06:511247void TabContents::EmailPageLocation() {
1248 std::string title = EscapeQueryParamValue(UTF16ToUTF8(GetTitle()), false);
1249 std::string page_url = EscapeQueryParamValue(GetURL().spec(), false);
1250 std::string mailto = std::string("mailto:?subject=Fwd:%20") +
1251 title + "&body=%0A%0A" + page_url;
1252 platform_util::OpenExternal(GURL(mailto));
1253}
1254
[email protected]420ae012009-04-24 05:16:321255void TabContents::PrintPreview() {
1256 // We don't show the print preview yet, only the print dialog.
1257 PrintNow();
1258}
1259
1260bool TabContents::PrintNow() {
1261 // We can't print interstitial page for now.
1262 if (showing_interstitial_page())
1263 return false;
1264
1265 return render_view_host()->PrintPages();
1266}
1267
[email protected]82270452009-06-19 15:58:011268void TabContents::PrintingDone(int document_cookie, bool success) {
1269 render_view_host()->PrintingDone(document_cookie, success);
1270}
1271
[email protected]420ae012009-04-24 05:16:321272bool TabContents::IsActiveEntry(int32 page_id) {
1273 NavigationEntry* active_entry = controller_.GetActiveEntry();
1274 return (active_entry != NULL &&
1275 active_entry->site_instance() == GetSiteInstance() &&
1276 active_entry->page_id() == page_id);
1277}
1278
[email protected]b2fe07d12010-02-09 14:38:081279void TabContents::SetOverrideEncoding(const std::string& encoding) {
[email protected]8cb5d5b2010-02-09 11:36:161280 set_encoding(encoding);
1281 render_view_host()->SetPageEncoding(encoding);
1282}
1283
[email protected]b2fe07d12010-02-09 14:38:081284void TabContents::ResetOverrideEncoding() {
[email protected]8cb5d5b2010-02-09 11:36:161285 reset_encoding();
1286 render_view_host()->ResetPageEncodingToDefault();
1287}
1288
1289void TabContents::WindowMoveOrResizeStarted() {
1290 render_view_host()->WindowMoveOrResizeStarted();
1291}
1292
[email protected]3f8eb7f2009-10-02 23:58:051293void TabContents::LogNewTabTime(const std::string& event_name) {
1294 // Not all new tab pages get timed. In those cases, we don't have a
1295 // new_tab_start_time_.
1296 if (new_tab_start_time_.is_null())
1297 return;
1298
1299 base::TimeDelta duration = base::TimeTicks::Now() - new_tab_start_time_;
[email protected]84abba62009-10-07 17:01:441300 MetricEventDurationDetails details(event_name,
1301 static_cast<int>(duration.InMilliseconds()));
1302
[email protected]7ab1e7d62009-10-14 23:32:011303 if (event_name == "Tab.NewTabScriptStart") {
1304 UMA_HISTOGRAM_TIMES("Tab.NewTabScriptStart", duration);
1305 } else if (event_name == "Tab.NewTabDOMContentLoaded") {
1306 UMA_HISTOGRAM_TIMES("Tab.NewTabDOMContentLoaded", duration);
1307 } else if (event_name == "Tab.NewTabOnload") {
1308 UMA_HISTOGRAM_TIMES("Tab.NewTabOnload", duration);
[email protected]3f8eb7f2009-10-02 23:58:051309 // The new tab page has finished loading; reset it.
1310 new_tab_start_time_ = base::TimeTicks();
[email protected]84abba62009-10-07 17:01:441311 } else {
1312 NOTREACHED();
[email protected]3f8eb7f2009-10-02 23:58:051313 }
[email protected]84abba62009-10-07 17:01:441314 NotificationService::current()->Notify(
1315 NotificationType::METRIC_EVENT_DURATION,
1316 Source<TabContents>(this),
1317 Details<MetricEventDurationDetails>(&details));
[email protected]3f8eb7f2009-10-02 23:58:051318}
1319
[email protected]7ab1e7d62009-10-14 23:32:011320void TabContents::OnCloseStarted() {
1321 if (tab_close_start_time_.is_null())
1322 tab_close_start_time_ = base::TimeTicks::Now();
1323}
1324
[email protected]c37e3b62010-01-22 00:15:171325TabContents* TabContents::CloneAndMakePhantom() {
[email protected]c37e3b62010-01-22 00:15:171326 TabNavigation tab_nav;
[email protected]df72ac72010-02-06 00:43:161327
1328 NavigationEntry* entry = controller().GetActiveEntry();
1329 if (app_extension_)
[email protected]867a73e12010-03-19 20:45:461330 tab_nav.set_url(app_extension_->GetFullLaunchURL());
[email protected]df72ac72010-02-06 00:43:161331 else if (entry)
1332 tab_nav.SetFromNavigationEntry(*entry);
1333
[email protected]c37e3b62010-01-22 00:15:171334 std::vector<TabNavigation> navigations;
1335 navigations.push_back(tab_nav);
1336
1337 TabContents* new_contents =
1338 new TabContents(profile(), NULL, MSG_ROUTING_NONE, NULL);
[email protected]3b355642010-02-05 16:01:491339 new_contents->SetAppExtension(app_extension_);
[email protected]c37e3b62010-01-22 00:15:171340 new_contents->controller().RestoreFromState(navigations, 0, false);
[email protected]c37e3b62010-01-22 00:15:171341
[email protected]df72ac72010-02-06 00:43:161342 if (!app_extension_ && entry)
1343 new_contents->controller().GetActiveEntry()->favicon() = entry->favicon();
1344
[email protected]c37e3b62010-01-22 00:15:171345 return new_contents;
1346}
1347
[email protected]0de80162010-02-03 04:52:351348// Resets the |content_blocked_| array.
1349void TabContents::ClearBlockedContentSettings() {
[email protected]15cf526b2010-02-12 06:33:491350 for (size_t i = 0; i < arraysize(content_blocked_); ++i)
[email protected]0de80162010-02-03 04:52:351351 content_blocked_[i] = false;
1352}
1353
[email protected]614c0e12010-03-25 10:12:561354// Resets the |geolocation_settings_| map.
1355void TabContents::ClearGeolocationContentSettings() {
1356 geolocation_content_settings_.clear();
1357}
1358
[email protected]96d185d2009-04-24 03:28:541359// Notifies the RenderWidgetHost instance about the fact that the page is
1360// loading, or done loading and calls the base implementation.
1361void TabContents::SetIsLoading(bool is_loading,
1362 LoadNotificationDetails* details) {
1363 if (is_loading == is_loading_)
1364 return;
1365
1366 if (!is_loading) {
1367 load_state_ = net::LOAD_STATE_IDLE;
1368 load_state_host_.clear();
[email protected]094e5b22009-09-25 04:23:561369 upload_size_ = 0;
1370 upload_position_ = 0;
[email protected]96d185d2009-04-24 03:28:541371 }
1372
1373 render_manager_.SetIsLoading(is_loading);
1374
1375 is_loading_ = is_loading;
1376 waiting_for_response_ = is_loading;
1377
1378 if (delegate_)
1379 delegate_->LoadingStateChanged(this);
[email protected]329581b2009-04-28 06:52:351380 NotifyNavigationStateChanged(INVALIDATE_LOAD);
[email protected]96d185d2009-04-24 03:28:541381
1382 NotificationType type = is_loading ? NotificationType::LOAD_START :
1383 NotificationType::LOAD_STOP;
[email protected]ce5c4502009-05-06 16:46:111384 NotificationDetails det = NotificationService::NoDetails();
[email protected]96d185d2009-04-24 03:28:541385 if (details)
1386 det = Details<LoadNotificationDetails>(details);
1387 NotificationService::current()->Notify(type,
1388 Source<NavigationController>(&controller_),
1389 det);
1390}
1391
[email protected]b6c874582009-05-08 19:38:311392void TabContents::AddPopup(TabContents* new_contents,
[email protected]7e7414ae2010-01-26 20:19:291393 const gfx::Rect& initial_pos) {
[email protected]8cc8d492010-02-02 10:40:491394 GURL url(GetURL());
1395 if (url.is_valid() &&
1396 profile()->GetHostContentSettingsMap()->GetContentSetting(
[email protected]da0c8e92010-02-09 23:25:491397 url, CONTENT_SETTINGS_TYPE_POPUPS) == CONTENT_SETTING_ALLOW) {
[email protected]8cc8d492010-02-02 10:40:491398 AddNewContents(new_contents, NEW_POPUP, initial_pos, true);
1399 } else {
1400 if (!blocked_popups_)
1401 blocked_popups_ = new BlockedPopupContainer(this);
1402 blocked_popups_->AddTabContents(new_contents, initial_pos);
1403 }
[email protected]b9681312008-11-07 00:08:261404}
[email protected]616ed5a2008-11-21 22:27:241405
1406namespace {
1407bool TransitionIsReload(PageTransition::Type transition) {
1408 return PageTransition::StripQualifier(transition) == PageTransition::RELOAD;
1409}
1410}
1411
1412void TabContents::ExpireInfoBars(
1413 const NavigationController::LoadCommittedDetails& details) {
1414 // Only hide InfoBars when the user has done something that makes the main
1415 // frame load. We don't want various automatic or subframe navigations making
1416 // it disappear.
1417 if (!details.is_user_initiated_main_frame_load())
1418 return;
1419
[email protected]f86a07022008-11-25 01:06:051420 for (int i = infobar_delegate_count() - 1; i >= 0; --i) {
[email protected]616ed5a2008-11-21 22:27:241421 InfoBarDelegate* delegate = GetInfoBarDelegateAt(i);
[email protected]f86a07022008-11-25 01:06:051422 if (delegate->ShouldExpire(details))
[email protected]616ed5a2008-11-21 22:27:241423 RemoveInfoBar(delegate);
[email protected]616ed5a2008-11-21 22:27:241424 }
1425}
[email protected]fdd61c62009-04-22 19:22:571426
[email protected]96d185d2009-04-24 03:28:541427DOMUI* TabContents::GetDOMUIForCurrentState() {
1428 // When there is a pending navigation entry, we want to use the pending DOMUI
1429 // that goes along with it to control the basic flags. For example, we want to
1430 // show the pending URL in the URL bar, so we want the display_url flag to
1431 // be from the pending entry.
1432 //
1433 // The confusion comes because there are multiple possibilities for the
1434 // initial load in a tab as a side effect of the way the RenderViewHostManager
1435 // works.
1436 //
1437 // - For the very first tab the load looks "normal". The new tab DOM UI is
1438 // the pending one, and we want it to apply here.
1439 //
1440 // - For subsequent new tabs, they'll get a new SiteInstance which will then
1441 // get switched to the one previously associated with the new tab pages.
1442 // This switching will cause the manager to commit the RVH/DOMUI. So we'll
1443 // have a committed DOM UI in this case.
1444 //
1445 // This condition handles all of these cases:
1446 //
1447 // - First load in first tab: no committed nav entry + pending nav entry +
1448 // pending dom ui:
1449 // -> Use pending DOM UI if any.
1450 //
1451 // - First load in second tab: no committed nav entry + pending nav entry +
1452 // no pending DOM UI:
1453 // -> Use the committed DOM UI if any.
1454 //
1455 // - Second navigation in any tab: committed nav entry + pending nav entry:
1456 // -> Use pending DOM UI if any.
1457 //
1458 // - Normal state with no load: committed nav entry + no pending nav entry:
1459 // -> Use committed DOM UI.
1460 if (controller_.pending_entry() &&
1461 (controller_.GetLastCommittedEntry() ||
1462 render_manager_.pending_dom_ui()))
1463 return render_manager_.pending_dom_ui();
1464 return render_manager_.dom_ui();
1465}
[email protected]420ae012009-04-24 05:16:321466
1467void TabContents::DidNavigateMainFramePostCommit(
1468 const NavigationController::LoadCommittedDetails& details,
1469 const ViewHostMsg_FrameNavigate_Params& params) {
[email protected]c2e74fe82009-09-03 17:57:441470 if (opener_dom_ui_type_ != DOMUIFactory::kNoDOMUI) {
1471 // If this is a window.open navigation, use the same DOMUI as the renderer
1472 // that opened the window, as long as both renderers have the same
1473 // privileges.
1474 if (opener_dom_ui_type_ == DOMUIFactory::GetDOMUIType(GetURL())) {
1475 DOMUI* dom_ui = DOMUIFactory::CreateDOMUIForURL(this, GetURL());
1476 // dom_ui might be NULL if the URL refers to a non-existent extension.
1477 if (dom_ui) {
1478 render_manager_.SetDOMUIPostCommit(dom_ui);
1479 dom_ui->RenderViewCreated(render_view_host());
1480 }
1481 }
1482 opener_dom_ui_type_ = DOMUIFactory::kNoDOMUI;
1483 }
1484
[email protected]420ae012009-04-24 05:16:321485 if (details.is_user_initiated_main_frame_load()) {
1486 // Clear the status bubble. This is a workaround for a bug where WebKit
1487 // doesn't let us know that the cursor left an element during a
1488 // transition (this is also why the mouse cursor remains as a hand after
1489 // clicking on a link); see bugs 1184641 and 980803. We don't want to
1490 // clear the bubble when a user navigates to a named anchor in the same
1491 // page.
1492 UpdateTargetURL(details.entry->page_id(), GURL());
1493
1494 // UpdateHelpersForDidNavigate will handle the case where the password_form
1495 // origin is valid.
1496 // TODO(brettw) bug 1343111: Password manager stuff in here needs to be
1497 // cleaned up and covered by tests.
1498 if (!params.password_form.origin.is_valid())
1499 GetPasswordManager()->DidNavigate();
1500 }
1501
1502 // The keyword generator uses the navigation entries, so must be called after
1503 // the commit.
1504 GenerateKeywordIfNecessary(params);
1505
1506 // Allow the new page to set the title again.
1507 received_page_title_ = false;
1508
1509 // Get the favicon, either from history or request it from the net.
1510 fav_icon_helper_.FetchFavIcon(details.entry->url());
1511
[email protected]0de80162010-02-03 04:52:351512 // Clear all page actions, blocked content notifications and browser actions
1513 // for this tab, unless this is an in-page navigation.
[email protected]a6e82fc2010-02-24 22:28:141514 if (!details.is_in_page) {
[email protected]92c6f9b92009-10-24 04:35:081515 ExtensionsService* service = profile()->GetExtensionsService();
1516 if (service) {
1517 for (size_t i = 0; i < service->extensions()->size(); ++i) {
[email protected]5d246db22009-10-27 06:17:571518 ExtensionAction* browser_action =
[email protected]92c6f9b92009-10-24 04:35:081519 service->extensions()->at(i)->browser_action();
[email protected]56ce6e52009-10-27 00:10:521520 if (browser_action) {
1521 browser_action->ClearAllValuesForTab(controller().session_id().id());
1522 NotificationService::current()->Notify(
1523 NotificationType::EXTENSION_BROWSER_ACTION_UPDATED,
[email protected]5d246db22009-10-27 06:17:571524 Source<ExtensionAction>(browser_action),
[email protected]56ce6e52009-10-27 00:10:521525 NotificationService::NoDetails());
1526 }
[email protected]92c6f9b92009-10-24 04:35:081527
[email protected]5d246db22009-10-27 06:17:571528 ExtensionAction* page_action =
[email protected]56ce6e52009-10-27 00:10:521529 service->extensions()->at(i)->page_action();
1530 if (page_action) {
1531 page_action->ClearAllValuesForTab(controller().session_id().id());
1532 PageActionStateChanged();
1533 }
[email protected]92c6f9b92009-10-24 04:35:081534 }
[email protected]aece2c7f2009-08-27 20:43:171535 }
[email protected]0de80162010-02-03 04:52:351536
1537 // Close blocked popups.
1538 if (blocked_popups_) {
1539 AutoReset auto_reset(&dont_notify_render_view_, true);
1540 blocked_popups_->Destroy();
[email protected]da7b5262010-02-25 21:12:461541 blocked_popups_ = NULL;
[email protected]0de80162010-02-03 04:52:351542 }
1543
1544 // Clear "blocked" flags.
1545 ClearBlockedContentSettings();
[email protected]614c0e12010-03-25 10:12:561546 ClearGeolocationContentSettings();
[email protected]0de80162010-02-03 04:52:351547 if (delegate_)
1548 delegate_->OnBlockedContentChange(this);
[email protected]aece2c7f2009-08-27 20:43:171549 }
[email protected]ce5c4502009-05-06 16:46:111550
[email protected]8cc8d492010-02-02 10:40:491551 // Close constrained windows if necessary.
1552 if (!net::RegistryControlledDomainService::SameDomainOrHost(
1553 details.previous_url, details.entry->url()))
1554 CloseConstrainedWindows();
1555
[email protected]420ae012009-04-24 05:16:321556 // Update the starred state.
1557 UpdateStarredStateForCurrentURL();
[email protected]c07c9752010-03-12 05:57:531558
1559 // Clear the cache of forms in AutoFill.
1560 if (autofill_manager_.get())
1561 autofill_manager_->Reset();
[email protected]a888b29e62010-04-01 13:38:571562
1563 // The url likely changed, see if there is an extension whose extent contains
1564 // the current page.
1565 UpdateAppExtensionForCurrentPage();
[email protected]420ae012009-04-24 05:16:321566}
1567
1568void TabContents::DidNavigateAnyFramePostCommit(
1569 RenderViewHost* render_view_host,
1570 const NavigationController::LoadCommittedDetails& details,
1571 const ViewHostMsg_FrameNavigate_Params& params) {
1572 // If we navigate, start showing messages again. This does nothing to prevent
1573 // a malicious script from spamming messages, since the script could just
1574 // reload the page to stop blocking.
1575 suppress_javascript_messages_ = false;
1576
[email protected]420ae012009-04-24 05:16:321577 // Notify the password manager of the navigation or form submit.
1578 // TODO(brettw) bug 1343111: Password manager stuff in here needs to be
1579 // cleaned up and covered by tests.
1580 if (params.password_form.origin.is_valid())
1581 GetPasswordManager()->ProvisionallySavePassword(params.password_form);
[email protected]3b1c1812010-02-12 21:53:551582
1583 // Let the LanguageState clear its state.
[email protected]34cec8d82010-03-05 18:27:011584 bool reload = details.entry->transition_type() == PageTransition::RELOAD ||
1585 details.type == NavigationType::SAME_PAGE;
1586 language_state_.DidNavigate(reload, details.is_in_page);
[email protected]420ae012009-04-24 05:16:321587}
1588
[email protected]8cc8d492010-02-02 10:40:491589void TabContents::CloseConstrainedWindows() {
1590 // Clear out any constrained windows since we are leaving this page entirely.
[email protected]420ae012009-04-24 05:16:321591 // We use indices instead of iterators in case CloseWindow does something
1592 // that may invalidate an iterator.
1593 int size = static_cast<int>(child_windows_.size());
1594 for (int i = size - 1; i >= 0; --i) {
1595 ConstrainedWindow* window = child_windows_[i];
[email protected]fa1cf0b82010-01-15 21:49:441596 if (window) {
[email protected]420ae012009-04-24 05:16:321597 window->CloseConstrainedWindow();
[email protected]fa1cf0b82010-01-15 21:49:441598 BlockTabContent(false);
1599 }
[email protected]420ae012009-04-24 05:16:321600 }
1601}
1602
1603void TabContents::UpdateStarredStateForCurrentURL() {
1604 BookmarkModel* model = profile()->GetBookmarkModel();
1605 const bool old_state = is_starred_;
1606 is_starred_ = (model && model->IsBookmarked(GetURL()));
1607
1608 if (is_starred_ != old_state && delegate())
1609 delegate()->URLStarredChanged(this, is_starred_);
1610}
1611
1612void TabContents::UpdateAlternateErrorPageURL() {
1613 GURL url = GetAlternateErrorPageURL();
1614 render_view_host()->SetAlternateErrorPageURL(url);
1615}
1616
1617void TabContents::UpdateWebPreferences() {
1618 render_view_host()->UpdateWebPreferences(GetWebkitPrefs());
1619}
1620
1621void TabContents::UpdateMaxPageIDIfNecessary(SiteInstance* site_instance,
1622 RenderViewHost* rvh) {
1623 // If we are creating a RVH for a restored controller, then we might
1624 // have more page IDs than the SiteInstance's current max page ID. We must
1625 // make sure that the max page ID is larger than any restored page ID.
1626 // Note that it is ok for conflicting page IDs to exist in another tab
1627 // (i.e., NavigationController), but if any page ID is larger than the max,
1628 // the back/forward list will get confused.
1629 int max_restored_page_id = controller_.max_restored_page_id();
1630 if (max_restored_page_id > 0) {
1631 int curr_max_page_id = site_instance->max_page_id();
1632 if (max_restored_page_id > curr_max_page_id) {
1633 // Need to update the site instance immediately.
1634 site_instance->UpdateMaxPageID(max_restored_page_id);
1635
1636 // Also tell the renderer to update its internal representation. We
1637 // need to reserve enough IDs to make all restored page IDs less than
1638 // the max.
1639 if (curr_max_page_id < 0)
1640 curr_max_page_id = 0;
1641 rvh->ReservePageIDRange(max_restored_page_id - curr_max_page_id);
1642 }
1643 }
1644}
1645
1646void TabContents::UpdateHistoryForNavigation(
[email protected]ebe89e062009-08-13 23:16:541647 const GURL& virtual_url,
[email protected]befd8d822009-07-01 04:51:471648 const NavigationController::LoadCommittedDetails& details,
[email protected]420ae012009-04-24 05:16:321649 const ViewHostMsg_FrameNavigate_Params& params) {
1650 if (profile()->IsOffTheRecord())
1651 return;
1652
1653 // Add to history service.
1654 HistoryService* hs = profile()->GetHistoryService(Profile::IMPLICIT_ACCESS);
1655 if (hs) {
1656 if (PageTransition::IsMainFrame(params.transition) &&
[email protected]ebe89e062009-08-13 23:16:541657 virtual_url != params.url) {
1658 // Hack on the "virtual" URL so that it will appear in history. For some
[email protected]420ae012009-04-24 05:16:321659 // types of URLs, we will display a magic URL that is different from where
1660 // the page is actually navigated. We want the user to see in history
[email protected]ebe89e062009-08-13 23:16:541661 // what they saw in the URL bar, so we add the virtual URL as a redirect.
1662 // This only applies to the main frame, as the virtual URL doesn't apply
[email protected]420ae012009-04-24 05:16:321663 // to sub-frames.
1664 std::vector<GURL> redirects = params.redirects;
1665 if (!redirects.empty())
[email protected]ebe89e062009-08-13 23:16:541666 redirects.back() = virtual_url;
1667 hs->AddPage(virtual_url, this, params.page_id, params.referrer,
[email protected]befd8d822009-07-01 04:51:471668 params.transition, redirects, details.did_replace_entry);
[email protected]420ae012009-04-24 05:16:321669 } else {
1670 hs->AddPage(params.url, this, params.page_id, params.referrer,
[email protected]befd8d822009-07-01 04:51:471671 params.transition, params.redirects,
1672 details.did_replace_entry);
[email protected]420ae012009-04-24 05:16:321673 }
1674 }
1675}
1676
1677bool TabContents::UpdateTitleForEntry(NavigationEntry* entry,
1678 const std::wstring& title) {
1679 // For file URLs without a title, use the pathname instead. In the case of a
1680 // synthesized title, we don't want the update to count toward the "one set
1681 // per page of the title to history."
1682 std::wstring final_title;
1683 bool explicit_set;
1684 if (entry->url().SchemeIsFile() && title.empty()) {
1685 final_title = UTF8ToWide(entry->url().ExtractFileName());
1686 explicit_set = false; // Don't count synthetic titles toward the set limit.
1687 } else {
1688 TrimWhitespace(title, TRIM_ALL, &final_title);
1689 explicit_set = true;
1690 }
1691
1692 if (final_title == UTF16ToWideHack(entry->title()))
1693 return false; // Nothing changed, don't bother.
1694
1695 entry->set_title(WideToUTF16Hack(final_title));
1696
1697 // Update the history system for this page.
1698 if (!profile()->IsOffTheRecord() && !received_page_title_) {
1699 HistoryService* hs =
1700 profile()->GetHistoryService(Profile::IMPLICIT_ACCESS);
1701 if (hs)
[email protected]ebe89e062009-08-13 23:16:541702 hs->SetPageTitle(entry->virtual_url(), final_title);
[email protected]420ae012009-04-24 05:16:321703
1704 // Don't allow the title to be saved again for explicitly set ones.
1705 received_page_title_ = explicit_set;
1706 }
1707
1708 // Lastly, set the title for the view.
1709 view_->SetPageTitle(final_title);
1710
1711 return true;
1712}
1713
1714void TabContents::NotifySwapped() {
1715 // After sending out a swap notification, we need to send a disconnect
1716 // notification so that clients that pick up a pointer to |this| can NULL the
1717 // pointer. See Bug 1230284.
1718 notify_disconnection_ = true;
1719 NotificationService::current()->Notify(
[email protected]57c6a652009-05-04 07:58:341720 NotificationType::TAB_CONTENTS_SWAPPED,
[email protected]d82ed61e2009-06-16 02:46:221721 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:321722 NotificationService::NoDetails());
1723}
1724
1725void TabContents::NotifyConnected() {
1726 notify_disconnection_ = true;
1727 NotificationService::current()->Notify(
[email protected]57c6a652009-05-04 07:58:341728 NotificationType::TAB_CONTENTS_CONNECTED,
[email protected]d82ed61e2009-06-16 02:46:221729 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:321730 NotificationService::NoDetails());
1731}
1732
1733void TabContents::NotifyDisconnected() {
1734 if (!notify_disconnection_)
1735 return;
1736
1737 notify_disconnection_ = false;
1738 NotificationService::current()->Notify(
[email protected]57c6a652009-05-04 07:58:341739 NotificationType::TAB_CONTENTS_DISCONNECTED,
[email protected]d82ed61e2009-06-16 02:46:221740 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:321741 NotificationService::NoDetails());
1742}
1743
1744void TabContents::GenerateKeywordIfNecessary(
1745 const ViewHostMsg_FrameNavigate_Params& params) {
1746 if (!params.searchable_form_url.is_valid())
1747 return;
1748
1749 if (profile()->IsOffTheRecord())
1750 return;
1751
1752 int last_index = controller_.last_committed_entry_index();
1753 // When there was no previous page, the last index will be 0. This is
1754 // normally due to a form submit that opened in a new tab.
1755 // TODO(brettw) bug 916126: we should support keywords when form submits
1756 // happen in new tabs.
1757 if (last_index <= 0)
1758 return;
1759 const NavigationEntry* previous_entry =
1760 controller_.GetEntryAtIndex(last_index - 1);
1761 if (IsFormSubmit(previous_entry)) {
1762 // Only generate a keyword if the previous page wasn't itself a form
1763 // submit.
1764 return;
1765 }
1766
1767 GURL keyword_url = previous_entry->user_typed_url().is_valid() ?
1768 previous_entry->user_typed_url() : previous_entry->url();
1769 std::wstring keyword =
1770 TemplateURLModel::GenerateKeyword(keyword_url, true); // autodetected
1771 if (keyword.empty())
1772 return;
1773
1774 TemplateURLModel* url_model = profile()->GetTemplateURLModel();
1775 if (!url_model)
1776 return;
1777
1778 if (!url_model->loaded()) {
1779 url_model->Load();
1780 return;
1781 }
1782
1783 const TemplateURL* current_url;
1784 std::wstring url = UTF8ToWide(params.searchable_form_url.spec());
1785 if (!url_model->CanReplaceKeyword(keyword, url, &current_url))
1786 return;
1787
1788 if (current_url) {
1789 if (current_url->originating_url().is_valid()) {
1790 // The existing keyword was generated from an OpenSearch description
1791 // document, don't regenerate.
1792 return;
1793 }
1794 url_model->Remove(current_url);
1795 }
1796 TemplateURL* new_url = new TemplateURL();
1797 new_url->set_keyword(keyword);
1798 new_url->set_short_name(keyword);
1799 new_url->SetURL(url, 0, 0);
1800 new_url->add_input_encoding(params.searchable_form_encoding);
1801 DCHECK(controller_.GetLastCommittedEntry());
1802 const GURL& favicon_url =
1803 controller_.GetLastCommittedEntry()->favicon().url();
1804 if (favicon_url.is_valid()) {
1805 new_url->SetFavIconURL(favicon_url);
1806 } else {
1807 // The favicon url isn't valid. This means there really isn't a favicon,
1808 // or the favicon url wasn't obtained before the load started. This assumes
1809 // the later.
1810 // TODO(sky): Need a way to set the favicon that doesn't involve generating
1811 // its url.
1812 new_url->SetFavIconURL(TemplateURL::GenerateFaviconURL(params.referrer));
1813 }
1814 new_url->set_safe_for_autoreplace(true);
1815 url_model->Add(new_url);
1816}
1817
[email protected]bf5c2ff392009-07-08 16:24:331818void TabContents::OnUserGesture() {
1819 // See comment in RenderViewHostDelegate::OnUserGesture as to why we do this.
1820 DownloadRequestManager* drm = g_browser_process->download_request_manager();
1821 if (drm)
1822 drm->OnUserGesture(this);
[email protected]e7eaedde2009-09-25 20:09:491823 ExternalProtocolHandler::OnUserGesture();
[email protected]bf5c2ff392009-07-08 16:24:331824 controller_.OnUserGesture();
1825}
1826
1827void TabContents::OnFindReply(int request_id,
1828 int number_of_matches,
1829 const gfx::Rect& selection_rect,
1830 int active_match_ordinal,
1831 bool final_update) {
[email protected]3ce7ced2009-07-24 18:52:421832 // Ignore responses for requests that have been aborted.
1833 if (find_op_aborted_)
1834 return;
1835
[email protected]bf5c2ff392009-07-08 16:24:331836 // Ignore responses for requests other than the one we have most recently
1837 // issued. That way we won't act on stale results when the user has
1838 // already typed in another query.
1839 if (request_id != current_find_request_id_)
1840 return;
1841
1842 if (number_of_matches == -1)
1843 number_of_matches = last_search_result_.number_of_matches();
1844 if (active_match_ordinal == -1)
1845 active_match_ordinal = last_search_result_.active_match_ordinal();
1846
1847 gfx::Rect selection = selection_rect;
1848 if (selection.IsEmpty())
1849 selection = last_search_result_.selection_rect();
1850
1851 // Notify the UI, automation and any other observers that a find result was
1852 // found.
1853 last_search_result_ = FindNotificationDetails(request_id, number_of_matches,
1854 selection, active_match_ordinal,
1855 final_update);
1856 NotificationService::current()->Notify(
1857 NotificationType::FIND_RESULT_AVAILABLE,
1858 Source<TabContents>(this),
1859 Details<FindNotificationDetails>(&last_search_result_));
1860}
1861
1862void TabContents::GoToEntryAtOffset(int offset) {
[email protected]f9cc4c452009-10-13 14:56:381863 if (!delegate_ || delegate_->OnGoToEntryOffset(offset))
1864 controller_.GoToOffset(offset);
[email protected]bf5c2ff392009-07-08 16:24:331865}
1866
[email protected]bf5c2ff392009-07-08 16:24:331867void TabContents::OnMissingPluginStatus(int status) {
1868#if defined(OS_WIN)
1869// TODO(PORT): pull in when plug-ins work
1870 GetPluginInstaller()->OnMissingPluginStatus(status);
1871#endif
1872}
1873
1874void TabContents::OnCrashedPlugin(const FilePath& plugin_path) {
[email protected]bf5c2ff392009-07-08 16:24:331875 DCHECK(!plugin_path.value().empty());
1876
1877 std::wstring plugin_name = plugin_path.ToWStringHack();
[email protected]d32d46182009-07-23 00:09:351878#if defined(OS_WIN) || defined(OS_MACOSX)
[email protected]bf5c2ff392009-07-08 16:24:331879 scoped_ptr<FileVersionInfo> version_info(
1880 FileVersionInfo::CreateFileVersionInfo(plugin_path));
1881 if (version_info.get()) {
1882 const std::wstring& product_name = version_info->product_name();
[email protected]996fd702009-09-04 19:12:371883 if (!product_name.empty()) {
[email protected]bf5c2ff392009-07-08 16:24:331884 plugin_name = product_name;
[email protected]996fd702009-09-04 19:12:371885#if defined(OS_MACOSX)
1886 // Many plugins on the Mac have .plugin in the actual name, which looks
1887 // terrible, so look for that and strip it off if present.
1888 const std::wstring plugin_extension(L".plugin");
1889 if (EndsWith(plugin_name, plugin_extension, true))
1890 plugin_name.erase(plugin_name.length() - plugin_extension.length());
1891#endif // OS_MACOSX
1892 }
[email protected]bf5c2ff392009-07-08 16:24:331893 }
[email protected]4a560ed2009-07-13 23:35:261894#else
1895 NOTIMPLEMENTED() << " convert plugin path to plugin name";
1896#endif
[email protected]bf5c2ff392009-07-08 16:24:331897 AddInfoBar(new SimpleAlertInfoBarDelegate(
1898 this, l10n_util::GetStringF(IDS_PLUGIN_CRASHED_PROMPT, plugin_name),
[email protected]938e1f92010-04-01 18:09:421899 NULL, true));
[email protected]bf5c2ff392009-07-08 16:24:331900}
1901
1902void TabContents::OnCrashedWorker() {
1903 AddInfoBar(new SimpleAlertInfoBarDelegate(
1904 this, l10n_util::GetString(IDS_WEBWORKER_CRASHED_PROMPT),
[email protected]938e1f92010-04-01 18:09:421905 NULL, true));
[email protected]bf5c2ff392009-07-08 16:24:331906}
1907
1908void TabContents::OnDidGetApplicationInfo(
1909 int32 page_id,
1910 const webkit_glue::WebApplicationInfo& info) {
[email protected]ed5431872009-11-17 08:39:511911 web_app_info_ = info;
[email protected]bf5c2ff392009-07-08 16:24:331912
[email protected]ed5431872009-11-17 08:39:511913 if (delegate())
1914 delegate()->OnDidGetApplicationInfo(this, page_id);
[email protected]bf5c2ff392009-07-08 16:24:331915}
1916
[email protected]a2cf65eb2010-01-18 08:20:381917void TabContents::OnPageContents(const GURL& url,
1918 int renderer_process_id,
1919 int32 page_id,
[email protected]a8a81292010-01-21 00:32:451920 const std::wstring& contents,
1921 const std::string& language) {
[email protected]a2cf65eb2010-01-18 08:20:381922 // Don't index any https pages. People generally don't want their bank
1923 // accounts, etc. indexed on their computer, especially since some of these
1924 // things are not marked cachable.
1925 // TODO(brettw) we may want to consider more elaborate heuristics such as
1926 // the cachability of the page. We may also want to consider subframes (this
1927 // test will still index subframes if the subframe is SSL).
1928 if (!url.SchemeIsSecure()) {
1929 Profile* p = profile();
1930 if (p && !p->IsOffTheRecord()) {
1931 HistoryService* hs = p->GetHistoryService(Profile::IMPLICIT_ACCESS);
1932 if (hs)
1933 hs->SetPageContents(url, contents);
1934 }
1935 }
[email protected]a8a81292010-01-21 00:32:451936
[email protected]3b1c1812010-02-12 21:53:551937 language_state_.LanguageDetermined(language);
[email protected]a8a81292010-01-21 00:32:451938
1939 std::string lang = language;
1940 NotificationService::current()->Notify(
1941 NotificationType::TAB_LANGUAGE_DETERMINED,
[email protected]7e0831b2010-02-01 08:45:151942 Source<TabContents>(this),
[email protected]a8a81292010-01-21 00:32:451943 Details<std::string>(&lang));
[email protected]a2cf65eb2010-01-18 08:20:381944}
1945
[email protected]7e0831b2010-02-01 08:45:151946void TabContents::OnPageTranslated(int32 page_id,
1947 const std::string& original_lang,
[email protected]152b8ba32010-03-29 19:40:161948 const std::string& translated_lang,
1949 TranslateErrors::Type error_type) {
[email protected]3b1c1812010-02-12 21:53:551950 language_state_.set_current_language(translated_lang);
1951 language_state_.set_translation_pending(false);
[email protected]152b8ba32010-03-29 19:40:161952 PageTranslatedDetails details(original_lang, translated_lang, error_type);
[email protected]7e0831b2010-02-01 08:45:151953 NotificationService::current()->Notify(
1954 NotificationType::PAGE_TRANSLATED,
1955 Source<TabContents>(this),
[email protected]152b8ba32010-03-29 19:40:161956 Details<PageTranslatedDetails>(&details));
[email protected]7e0831b2010-02-01 08:45:151957}
1958
[email protected]bf5c2ff392009-07-08 16:24:331959void TabContents::DidStartProvisionalLoadForFrame(
1960 RenderViewHost* render_view_host,
1961 bool is_main_frame,
1962 const GURL& url) {
1963 ProvisionalLoadDetails details(is_main_frame,
1964 controller_.IsURLInPageNavigation(url),
1965 url, std::string(), false);
1966 NotificationService::current()->Notify(
1967 NotificationType::FRAME_PROVISIONAL_LOAD_START,
1968 Source<NavigationController>(&controller_),
1969 Details<ProvisionalLoadDetails>(&details));
1970}
1971
1972void TabContents::DidStartReceivingResourceResponse(
[email protected]3dbb80b2010-02-09 22:41:201973 const ResourceRequestDetails& details) {
[email protected]bf5c2ff392009-07-08 16:24:331974 NotificationService::current()->Notify(
1975 NotificationType::RESOURCE_RESPONSE_STARTED,
1976 Source<NavigationController>(&controller()),
[email protected]3dbb80b2010-02-09 22:41:201977 Details<const ResourceRequestDetails>(&details));
[email protected]bf5c2ff392009-07-08 16:24:331978}
1979
[email protected]3dbb80b2010-02-09 22:41:201980void TabContents::DidRedirectResource(
1981 const ResourceRedirectDetails& details) {
[email protected]bf5c2ff392009-07-08 16:24:331982 NotificationService::current()->Notify(
1983 NotificationType::RESOURCE_RECEIVED_REDIRECT,
1984 Source<NavigationController>(&controller()),
[email protected]3dbb80b2010-02-09 22:41:201985 Details<const ResourceRequestDetails>(&details));
[email protected]bf5c2ff392009-07-08 16:24:331986}
1987
1988void TabContents::DidLoadResourceFromMemoryCache(
1989 const GURL& url,
1990 const std::string& frame_origin,
1991 const std::string& main_frame_origin,
1992 const std::string& security_info) {
1993 // Send out a notification that we loaded a resource from our memory cache.
1994 int cert_id = 0, cert_status = 0, security_bits = 0;
1995 SSLManager::DeserializeSecurityInfo(security_info,
1996 &cert_id, &cert_status,
1997 &security_bits);
1998 LoadFromMemoryCacheDetails details(url, frame_origin, main_frame_origin,
[email protected]8cb5d5b2010-02-09 11:36:161999 GetRenderProcessHost()->id(), cert_id,
2000 cert_status);
[email protected]bf5c2ff392009-07-08 16:24:332001
2002 NotificationService::current()->Notify(
2003 NotificationType::LOAD_FROM_MEMORY_CACHE,
2004 Source<NavigationController>(&controller_),
2005 Details<LoadFromMemoryCacheDetails>(&details));
2006}
2007
[email protected]e3d60e5d2009-09-25 21:08:292008void TabContents::DidDisplayInsecureContent() {
[email protected]09843b5f2009-09-26 08:30:592009 controller_.ssl_manager()->DidDisplayInsecureContent();
[email protected]e3d60e5d2009-09-25 21:08:292010}
2011
2012void TabContents::DidRunInsecureContent(const std::string& security_origin) {
[email protected]09843b5f2009-09-26 08:30:592013 controller_.ssl_manager()->DidRunInsecureContent(security_origin);
[email protected]e3d60e5d2009-09-25 21:08:292014}
2015
[email protected]bf5c2ff392009-07-08 16:24:332016void TabContents::DidFailProvisionalLoadWithError(
2017 RenderViewHost* render_view_host,
2018 bool is_main_frame,
2019 int error_code,
2020 const GURL& url,
2021 bool showing_repost_interstitial) {
2022 if (net::ERR_ABORTED == error_code) {
2023 // EVIL HACK ALERT! Ignore failed loads when we're showing interstitials.
2024 // This means that the interstitial won't be torn down properly, which is
2025 // bad. But if we have an interstitial, go back to another tab type, and
2026 // then load the same interstitial again, we could end up getting the first
2027 // interstitial's "failed" message (as a result of the cancel) when we're on
2028 // the second one.
2029 //
2030 // We can't tell this apart, so we think we're tearing down the current page
2031 // which will cause a crash later one. There is also some code in
2032 // RenderViewHostManager::RendererAbortedProvisionalLoad that is commented
2033 // out because of this problem.
2034 //
2035 // http://code.google.com/p/chromium/issues/detail?id=2855
2036 // Because this will not tear down the interstitial properly, if "back" is
2037 // back to another tab type, the interstitial will still be somewhat alive
2038 // in the previous tab type. If you navigate somewhere that activates the
2039 // tab with the interstitial again, you'll see a flash before the new load
2040 // commits of the interstitial page.
2041 if (showing_interstitial_page()) {
2042 LOG(WARNING) << "Discarding message during interstitial.";
2043 return;
2044 }
2045
2046 // This will discard our pending entry if we cancelled the load (e.g., if we
2047 // decided to download the file instead of load it). Only discard the
2048 // pending entry if the URLs match, otherwise the user initiated a navigate
2049 // before the page loaded so that the discard would discard the wrong entry.
2050 NavigationEntry* pending_entry = controller_.pending_entry();
2051 if (pending_entry && pending_entry->url() == url) {
2052 controller_.DiscardNonCommittedEntries();
2053 // Update the URL display.
2054 NotifyNavigationStateChanged(TabContents::INVALIDATE_URL);
2055 }
2056
2057 render_manager_.RendererAbortedProvisionalLoad(render_view_host);
2058 }
2059
2060 // Send out a notification that we failed a provisional load with an error.
2061 ProvisionalLoadDetails details(is_main_frame,
2062 controller_.IsURLInPageNavigation(url),
2063 url, std::string(), false);
2064 details.set_error_code(error_code);
2065
2066 NotificationService::current()->Notify(
2067 NotificationType::FAIL_PROVISIONAL_LOAD_WITH_ERROR,
2068 Source<NavigationController>(&controller_),
2069 Details<ProvisionalLoadDetails>(&details));
2070}
2071
2072void TabContents::DocumentLoadedInFrame() {
2073 controller_.DocumentLoadedInFrame();
2074}
2075
[email protected]0de80162010-02-03 04:52:352076void TabContents::OnContentBlocked(ContentSettingsType type) {
[email protected]b1235062010-03-25 17:31:272077 // TODO(joth): remove once fully implemented.
2078 DCHECK(type != CONTENT_SETTINGS_TYPE_GEOLOCATION);
[email protected]0de80162010-02-03 04:52:352079 content_blocked_[type] = true;
2080 if (delegate_)
2081 delegate_->OnBlockedContentChange(this);
2082}
2083
[email protected]614c0e12010-03-25 10:12:562084void TabContents::OnGeolocationPermissionSet(const GURL& requesting_origin,
2085 bool allowed) {
2086 geolocation_content_settings_[requesting_origin] =
2087 allowed ? CONTENT_SETTING_ALLOW : CONTENT_SETTING_BLOCK;
2088 // TODO(bulach): rename OnBlockedContentChange to OnContentSettingsChange.
2089 if (delegate_)
2090 delegate_->OnBlockedContentChange(this);
2091}
2092
[email protected]8d3347f2009-07-09 22:00:212093RenderViewHostDelegate::View* TabContents::GetViewDelegate() {
[email protected]420ae012009-04-24 05:16:322094 return view_.get();
2095}
2096
[email protected]8d3347f2009-07-09 22:00:212097RenderViewHostDelegate::RendererManagement*
2098TabContents::GetRendererManagementDelegate() {
2099 return &render_manager_;
2100}
2101
[email protected]bf5c2ff392009-07-08 16:24:332102RenderViewHostDelegate::BrowserIntegration*
[email protected]8d3347f2009-07-09 22:00:212103 TabContents::GetBrowserIntegrationDelegate() {
2104 return this;
[email protected]bf5c2ff392009-07-08 16:24:332105}
2106
[email protected]8d3347f2009-07-09 22:00:212107RenderViewHostDelegate::Resource* TabContents::GetResourceDelegate() {
2108 return this;
[email protected]bf5c2ff392009-07-08 16:24:332109}
2110
[email protected]8d3347f2009-07-09 22:00:212111RenderViewHostDelegate::Save* TabContents::GetSaveDelegate() {
[email protected]420ae012009-04-24 05:16:322112 return save_package_.get(); // May be NULL, but we can return NULL.
2113}
2114
[email protected]8d3347f2009-07-09 22:00:212115RenderViewHostDelegate::Printing* TabContents::GetPrintingDelegate() {
2116 return &printing_;
2117}
2118
2119RenderViewHostDelegate::FavIcon* TabContents::GetFavIconDelegate() {
2120 return &fav_icon_helper_;
2121}
2122
[email protected]e8dbd674b2010-03-11 18:34:402123RenderViewHostDelegate::Autocomplete* TabContents::GetAutocompleteDelegate() {
[email protected]6db0def92010-03-02 19:55:452124 if (autocomplete_history_manager_.get() == NULL)
2125 autocomplete_history_manager_.reset(new AutocompleteHistoryManager(this));
2126 return autocomplete_history_manager_.get();
[email protected]bf5c2ff392009-07-08 16:24:332127}
2128
[email protected]21674df2009-10-28 19:51:212129RenderViewHostDelegate::AutoFill* TabContents::GetAutoFillDelegate() {
2130 if (autofill_manager_.get() == NULL)
2131 autofill_manager_.reset(new AutoFillManager(this));
2132 return autofill_manager_.get();
2133}
2134
[email protected]fd571bb2010-03-19 01:23:542135AutomationResourceRoutingDelegate*
2136TabContents::GetAutomationResourceRoutingDelegate() {
2137 return delegate();
2138}
2139
[email protected]ced90ae12010-02-20 02:06:162140RenderViewHostDelegate::BookmarkDrag* TabContents::GetBookmarkDragDelegate() {
2141 return bookmark_drag_;
2142}
2143
2144void TabContents::SetBookmarkDragDelegate(
2145 RenderViewHostDelegate::BookmarkDrag* bookmark_drag) {
2146 bookmark_drag_ = bookmark_drag;
2147}
2148
[email protected]93623c5d2009-12-10 21:40:322149RendererPreferences TabContents::GetRendererPrefs(Profile* profile) const {
[email protected]840b1512009-07-21 16:53:462150 return renderer_preferences_;
[email protected]80d96fa2009-06-10 22:34:512151}
2152
[email protected]57c6a652009-05-04 07:58:342153TabContents* TabContents::GetAsTabContents() {
2154 return this;
2155}
2156
[email protected]7b291f92009-08-14 05:43:532157ViewType::Type TabContents::GetRenderViewType() const {
2158 return ViewType::TAB_CONTENTS;
2159}
2160
2161int TabContents::GetBrowserWindowID() const {
2162 return controller().window_id().id();
2163}
2164
[email protected]420ae012009-04-24 05:16:322165void TabContents::RenderViewCreated(RenderViewHost* render_view_host) {
[email protected]2e4633c2009-07-09 16:58:062166 NotificationService::current()->Notify(
2167 NotificationType::RENDER_VIEW_HOST_CREATED_FOR_TAB,
2168 Source<TabContents>(this),
2169 Details<RenderViewHost>(render_view_host));
[email protected]420ae012009-04-24 05:16:322170 NavigationEntry* entry = controller_.GetActiveEntry();
2171 if (!entry)
2172 return;
2173
2174 // When we're creating views, we're still doing initial setup, so we always
2175 // use the pending DOM UI rather than any possibly existing committed one.
2176 if (render_manager_.pending_dom_ui()) {
2177 render_manager_.pending_dom_ui()->RenderViewCreated(render_view_host);
2178 }
2179
2180 if (entry->IsViewSourceMode()) {
2181 // Put the renderer in view source mode.
2182 render_view_host->Send(
2183 new ViewMsg_EnableViewSourceMode(render_view_host->routing_id()));
2184 }
[email protected]0666aef2009-05-13 19:48:082185
2186 view()->RenderViewCreated(render_view_host);
[email protected]420ae012009-04-24 05:16:322187}
2188
2189void TabContents::RenderViewReady(RenderViewHost* rvh) {
2190 if (rvh != render_view_host()) {
2191 // Don't notify the world, since this came from a renderer in the
2192 // background.
2193 return;
2194 }
2195
2196 NotifyConnected();
[email protected]9d8a4642009-07-29 17:25:302197 bool was_crashed = is_crashed();
[email protected]420ae012009-04-24 05:16:322198 SetIsCrashed(false);
[email protected]9d8a4642009-07-29 17:25:302199
2200 // Restore the focus to the tab (otherwise the focus will be on the top
2201 // window).
2202 if (was_crashed && !FocusLocationBarByDefault())
2203 Focus();
[email protected]420ae012009-04-24 05:16:322204}
2205
2206void TabContents::RenderViewGone(RenderViewHost* rvh) {
2207 // Ask the print preview if this renderer was valuable.
2208 if (!printing_.OnRenderViewGone(rvh))
2209 return;
2210 if (rvh != render_view_host()) {
2211 // The pending page's RenderViewHost is gone.
2212 return;
2213 }
2214
2215 SetIsLoading(false, NULL);
2216 NotifyDisconnected();
2217 SetIsCrashed(true);
2218
[email protected]ff9efc62010-02-18 22:10:542219 // Remove all infobars.
[email protected]e5430c92010-02-18 22:39:212220 for (int i = infobar_delegate_count() - 1; i >=0 ; --i)
[email protected]ff9efc62010-02-18 22:10:542221 RemoveInfoBar(GetInfoBarDelegateAt(i));
2222
[email protected]8e537302009-06-10 21:19:582223 // Tell the view that we've crashed so it can prepare the sad tab page.
[email protected]cf68b092010-01-15 20:05:552224 // Only do this if we're not in browser shutdown, so that TabContents
2225 // objects that are not in a browser (e.g., HTML dialogs) and thus are
2226 // visible do not flash a sad tab page.
2227 if (browser_shutdown::GetShutdownType() == browser_shutdown::NOT_VALID)
2228 view_->OnTabCrashed();
[email protected]420ae012009-04-24 05:16:322229
2230 // Hide any visible hung renderer warning for this web contents' process.
[email protected]e7b8d832009-11-20 22:41:002231 hung_renderer_dialog::HideForTabContents(this);
[email protected]420ae012009-04-24 05:16:322232}
2233
[email protected]2e4633c2009-07-09 16:58:062234void TabContents::RenderViewDeleted(RenderViewHost* rvh) {
2235 NotificationService::current()->Notify(
2236 NotificationType::RENDER_VIEW_HOST_DELETED,
2237 Source<TabContents>(this),
2238 Details<RenderViewHost>(rvh));
[email protected]fb7b79f72009-11-06 18:00:482239 render_manager_.RenderViewDeleted(rvh);
[email protected]2e4633c2009-07-09 16:58:062240}
2241
[email protected]420ae012009-04-24 05:16:322242void TabContents::DidNavigate(RenderViewHost* rvh,
2243 const ViewHostMsg_FrameNavigate_Params& params) {
[email protected]8030f012009-09-25 18:09:372244 int extra_invalidate_flags = 0;
2245
2246 if (PageTransition::IsMainFrame(params.transition)) {
[email protected]20ede072009-10-06 00:28:352247 bool was_bookmark_bar_visible = ShouldShowBookmarkBar();
[email protected]8030f012009-09-25 18:09:372248 bool was_extension_shelf_visible = IsExtensionShelfAlwaysVisible();
2249
[email protected]420ae012009-04-24 05:16:322250 render_manager_.DidNavigateMainFrame(rvh);
2251
[email protected]20ede072009-10-06 00:28:352252 if (was_bookmark_bar_visible != ShouldShowBookmarkBar())
[email protected]8030f012009-09-25 18:09:372253 extra_invalidate_flags |= INVALIDATE_BOOKMARK_BAR;
2254 if (was_extension_shelf_visible != IsExtensionShelfAlwaysVisible())
2255 extra_invalidate_flags |= INVALIDATE_EXTENSION_SHELF;
2256 }
2257
[email protected]420ae012009-04-24 05:16:322258 // Update the site of the SiteInstance if it doesn't have one yet.
2259 if (!GetSiteInstance()->has_site())
2260 GetSiteInstance()->SetSite(params.url);
2261
2262 // Need to update MIME type here because it's referred to in
2263 // UpdateNavigationCommands() called by RendererDidNavigate() to
2264 // determine whether or not to enable the encoding menu.
2265 // It's updated only for the main frame. For a subframe,
2266 // RenderView::UpdateURL does not set params.contents_mime_type.
2267 // (see http://code.google.com/p/chromium/issues/detail?id=2929 )
2268 // TODO(jungshik): Add a test for the encoding menu to avoid
2269 // regressing it again.
2270 if (PageTransition::IsMainFrame(params.transition))
2271 contents_mime_type_ = params.contents_mime_type;
2272
2273 NavigationController::LoadCommittedDetails details;
[email protected]8030f012009-09-25 18:09:372274 bool did_navigate = controller_.RendererDidNavigate(
2275 params, extra_invalidate_flags, &details);
[email protected]3e90d4a2009-07-03 17:38:392276
2277 // Update history. Note that this needs to happen after the entry is complete,
2278 // which WillNavigate[Main,Sub]Frame will do before this function is called.
[email protected]2e29d922009-10-22 22:13:352279 if (params.should_update_history &&
2280 (!delegate() || delegate()->ShouldAddNavigationsToHistory())) {
[email protected]3e90d4a2009-07-03 17:38:392281 // Most of the time, the displayURL matches the loaded URL, but for about:
2282 // URLs, we use a data: URL as the real value. We actually want to save
2283 // the about: URL to the history db and keep the data: URL hidden. This is
2284 // what the TabContents' URL getter does.
2285 UpdateHistoryForNavigation(GetURL(), details, params);
2286 }
2287
[email protected]76543b92009-08-31 17:27:452288 if (!did_navigate)
2289 return; // No navigation happened.
[email protected]420ae012009-04-24 05:16:322290
2291 // DO NOT ADD MORE STUFF TO THIS FUNCTION! Your component should either listen
2292 // for the appropriate notification (best) or you can add it to
2293 // DidNavigateMainFramePostCommit / DidNavigateAnyFramePostCommit (only if
2294 // necessary, please).
2295
2296 // Run post-commit tasks.
2297 if (details.is_main_frame)
2298 DidNavigateMainFramePostCommit(details, params);
2299 DidNavigateAnyFramePostCommit(rvh, details, params);
2300}
2301
2302void TabContents::UpdateState(RenderViewHost* rvh,
2303 int32 page_id,
2304 const std::string& state) {
2305 DCHECK(rvh == render_view_host());
2306
2307 // We must be prepared to handle state updates for any page, these occur
2308 // when the user is scrolling and entering form data, as well as when we're
2309 // leaving a page, in which case our state may have already been moved to
2310 // the next page. The navigation controller will look up the appropriate
2311 // NavigationEntry and update it when it is notified via the delegate.
2312
2313 int entry_index = controller_.GetEntryIndexWithPageID(
2314 GetSiteInstance(), page_id);
2315 if (entry_index < 0)
2316 return;
2317 NavigationEntry* entry = controller_.GetEntryAtIndex(entry_index);
2318
2319 if (state == entry->content_state())
2320 return; // Nothing to update.
2321 entry->set_content_state(state);
2322 controller_.NotifyEntryChanged(entry, entry_index);
2323}
2324
2325void TabContents::UpdateTitle(RenderViewHost* rvh,
2326 int32 page_id, const std::wstring& title) {
2327 // If we have a title, that's a pretty good indication that we've started
2328 // getting useful data.
2329 SetNotWaitingForResponse();
2330
2331 DCHECK(rvh == render_view_host());
2332 NavigationEntry* entry = controller_.GetEntryWithPageID(GetSiteInstance(),
2333 page_id);
2334 if (!entry || !UpdateTitleForEntry(entry, title))
2335 return;
2336
2337 // Broadcast notifications when the UI should be updated.
2338 if (entry == controller_.GetEntryAtOffset(0))
[email protected]f1cd5e82009-10-23 17:19:032339 NotifyNavigationStateChanged(INVALIDATE_TITLE);
[email protected]420ae012009-04-24 05:16:322340}
2341
[email protected]420ae012009-04-24 05:16:322342void TabContents::UpdateEncoding(RenderViewHost* render_view_host,
[email protected]41fc0322009-09-04 22:23:402343 const std::string& encoding) {
[email protected]420ae012009-04-24 05:16:322344 set_encoding(encoding);
2345}
2346
2347void TabContents::UpdateTargetURL(int32 page_id, const GURL& url) {
2348 if (delegate())
2349 delegate()->UpdateTargetURL(this, url);
2350}
2351
2352void TabContents::UpdateThumbnail(const GURL& url,
2353 const SkBitmap& bitmap,
2354 const ThumbnailScore& score) {
2355 // Tell History about this thumbnail
[email protected]449478302009-06-09 20:04:282356 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kThumbnailStore)) {
[email protected]72d0fc72009-07-09 17:30:492357 if (!profile()->IsOffTheRecord())
[email protected]42339402009-07-17 17:27:192358 profile()->GetThumbnailStore()->SetPageThumbnail(url, bitmap,
2359 score, true);
[email protected]449478302009-06-09 20:04:282360 } else {
2361 HistoryService* hs;
2362 if (!profile()->IsOffTheRecord() &&
2363 (hs = profile()->GetHistoryService(Profile::IMPLICIT_ACCESS))) {
2364 hs->SetPageThumbnail(url, bitmap, score);
2365 }
[email protected]420ae012009-04-24 05:16:322366 }
2367}
2368
[email protected]8922e1f2009-10-03 05:01:262369void TabContents::UpdateInspectorSettings(const std::string& raw_settings) {
[email protected]0df30122009-06-03 12:13:082370 profile()->GetPrefs()->SetString(prefs::kWebKitInspectorSettings,
[email protected]8922e1f2009-10-03 05:01:262371 UTF8ToWide(raw_settings));
[email protected]0df30122009-06-03 12:13:082372}
2373
[email protected]420ae012009-04-24 05:16:322374void TabContents::Close(RenderViewHost* rvh) {
[email protected]07707302009-11-06 00:50:292375 // The UI may be in an event-tracking loop, such as between the
2376 // mouse-down and mouse-up in text selection or a button click.
2377 // Defer the close until after tracking is complete, so that we
2378 // don't free objects out from under the UI.
2379 // TODO(shess): This could probably be integrated with the
2380 // IsDoingDrag() test below. Punting for now because I need more
2381 // research to understand how this impacts platforms other than Mac.
2382 // TODO(shess): This could get more fine-grained. For instance,
2383 // closing a tab in another window while selecting text in the
2384 // current window's Omnibox should be just fine.
2385 if (view()->IsEventTracking()) {
2386 view()->CloseTabAfterEventTracking();
2387 return;
2388 }
2389
[email protected]24a4d1062009-07-10 23:10:422390 // If we close the tab while we're in the middle of a drag, we'll crash.
2391 // Instead, cancel the drag and close it as soon as the drag ends.
2392 if (view()->IsDoingDrag()) {
2393 view()->CancelDragAndCloseTab();
2394 return;
2395 }
2396
[email protected]420ae012009-04-24 05:16:322397 // Ignore this if it comes from a RenderViewHost that we aren't showing.
2398 if (delegate() && rvh == render_view_host())
2399 delegate()->CloseContents(this);
2400}
2401
2402void TabContents::RequestMove(const gfx::Rect& new_bounds) {
2403 if (delegate() && delegate()->IsPopup(this))
2404 delegate()->MoveContents(this, new_bounds);
2405}
2406
[email protected]7ab1e7d62009-10-14 23:32:012407void TabContents::DidStartLoading() {
[email protected]420ae012009-04-24 05:16:322408 SetIsLoading(true, NULL);
2409}
2410
[email protected]7ab1e7d62009-10-14 23:32:012411void TabContents::DidStopLoading() {
[email protected]420ae012009-04-24 05:16:322412 scoped_ptr<LoadNotificationDetails> details;
2413
2414 NavigationEntry* entry = controller_.GetActiveEntry();
2415 // An entry may not exist for a stop when loading an initial blank page or
2416 // if an iframe injected by script into a blank page finishes loading.
2417 if (entry) {
[email protected]420ae012009-04-24 05:16:322418 base::TimeDelta elapsed = base::TimeTicks::Now() - current_load_start_;
2419
2420 details.reset(new LoadNotificationDetails(
[email protected]ebe89e062009-08-13 23:16:542421 entry->virtual_url(),
[email protected]420ae012009-04-24 05:16:322422 entry->transition_type(),
2423 elapsed,
2424 &controller_,
2425 controller_.GetCurrentEntryIndex()));
2426 }
2427
2428 // Tell PasswordManager we've finished a page load, which serves as a
2429 // green light to save pending passwords and reset itself.
2430 GetPasswordManager()->DidStopLoading();
2431
2432 SetIsLoading(false, details.get());
2433}
2434
[email protected]420ae012009-04-24 05:16:322435void TabContents::DidRedirectProvisionalLoad(int32 page_id,
2436 const GURL& source_url,
2437 const GURL& target_url) {
2438 NavigationEntry* entry;
2439 if (page_id == -1)
2440 entry = controller_.pending_entry();
2441 else
2442 entry = controller_.GetEntryWithPageID(GetSiteInstance(), page_id);
2443 if (!entry || entry->url() != source_url)
2444 return;
2445 entry->set_url(target_url);
2446}
2447
[email protected]420ae012009-04-24 05:16:322448void TabContents::RequestOpenURL(const GURL& url, const GURL& referrer,
2449 WindowOpenDisposition disposition) {
2450 if (render_manager_.dom_ui()) {
2451 // When we're a DOM UI, it will provide a page transition type for us (this
2452 // is so the new tab page can specify AUTO_BOOKMARK for automatically
2453 // generated suggestions).
2454 //
2455 // Note also that we hide the referrer for DOM UI pages. We don't really
[email protected]60e448982009-05-06 04:21:162456 // want web sites to see a referrer of "chrome://blah" (and some
2457 // chrome: URLs might have search terms or other stuff we don't want to
[email protected]420ae012009-04-24 05:16:322458 // send to the site), so we send no referrer.
2459 OpenURL(url, GURL(), disposition,
2460 render_manager_.dom_ui()->link_transition_type());
2461 } else {
2462 OpenURL(url, referrer, disposition, PageTransition::LINK);
2463 }
2464}
2465
[email protected]1ee614862009-07-06 20:29:182466void TabContents::DomOperationResponse(const std::string& json_string,
2467 int automation_id) {
2468}
2469
[email protected]420ae012009-04-24 05:16:322470void TabContents::ProcessDOMUIMessage(const std::string& message,
[email protected]e4dad9fb2009-10-06 18:15:582471 const Value* content,
[email protected]bb64b512010-04-02 21:01:392472 const GURL& source_url,
[email protected]811bfe32009-07-01 08:46:252473 int request_id,
2474 bool has_callback) {
[email protected]420ae012009-04-24 05:16:322475 if (!render_manager_.dom_ui()) {
[email protected]c2e74fe82009-09-03 17:57:442476 // This can happen if someone uses window.open() to open an extension URL
2477 // from a non-extension context.
2478 render_view_host()->BlockExtensionRequest(request_id);
[email protected]420ae012009-04-24 05:16:322479 return;
2480 }
[email protected]bb64b512010-04-02 21:01:392481 render_manager_.dom_ui()->ProcessDOMUIMessage(message, content, source_url,
2482 request_id,
[email protected]811bfe32009-07-01 08:46:252483 has_callback);
[email protected]420ae012009-04-24 05:16:322484}
2485
2486void TabContents::ProcessExternalHostMessage(const std::string& message,
2487 const std::string& origin,
2488 const std::string& target) {
2489 if (delegate())
2490 delegate()->ForwardMessageToExternalHost(message, origin, target);
2491}
2492
[email protected]cdaf8d02010-03-30 19:52:472493void TabContents::RunFileChooser(
2494 const ViewHostMsg_RunFileChooser_Params &params) {
[email protected]420ae012009-04-24 05:16:322495 if (!select_file_dialog_.get())
2496 select_file_dialog_ = SelectFileDialog::Create(this);
[email protected]cdaf8d02010-03-30 19:52:472497
2498 SelectFileDialog::Type dialog_type;
2499 switch (params.mode) {
2500 case ViewHostMsg_RunFileChooser_Params::Open:
2501 dialog_type = SelectFileDialog::SELECT_OPEN_FILE;
2502 break;
2503 case ViewHostMsg_RunFileChooser_Params::OpenMultiple:
2504 dialog_type = SelectFileDialog::SELECT_OPEN_MULTI_FILE;
2505 break;
2506 case ViewHostMsg_RunFileChooser_Params::Save:
2507 dialog_type = SelectFileDialog::SELECT_SAVEAS_FILE;
2508 break;
2509 default:
[email protected]bed31952010-03-30 20:07:592510 dialog_type = SelectFileDialog::SELECT_OPEN_FILE; // Prevent warning.
[email protected]cdaf8d02010-03-30 19:52:472511 NOTREACHED();
2512 }
2513 select_file_dialog_->SelectFile(dialog_type, params.title,
2514 params.default_file_name,
[email protected]420ae012009-04-24 05:16:322515 NULL, 0, FILE_PATH_LITERAL(""),
2516 view_->GetTopLevelNativeWindow(), NULL);
2517}
2518
2519void TabContents::RunJavaScriptMessage(
2520 const std::wstring& message,
2521 const std::wstring& default_prompt,
2522 const GURL& frame_url,
2523 const int flags,
2524 IPC::Message* reply_msg,
2525 bool* did_suppress_message) {
2526 // Suppress javascript messages when requested and when inside a constrained
2527 // popup window (because that activates them and breaks them out of the
2528 // constrained window jail).
[email protected]91ed49a2009-07-07 21:29:552529 // Also suppress messages when showing an interstitial. The interstitial is
2530 // shown over the previous page, we don't want the hidden page dialogs to
2531 // interfere with the interstitial.
2532 bool suppress_this_message = suppress_javascript_messages_ ||
2533 showing_interstitial_page();
[email protected]420ae012009-04-24 05:16:322534 if (delegate())
2535 suppress_this_message |=
[email protected]b6c874582009-05-08 19:38:312536 (delegate()->GetConstrainingContents(this) != this);
[email protected]420ae012009-04-24 05:16:322537
2538 *did_suppress_message = suppress_this_message;
2539
2540 if (!suppress_this_message) {
2541 base::TimeDelta time_since_last_message(
2542 base::TimeTicks::Now() - last_javascript_message_dismissal_);
2543 bool show_suppress_checkbox = false;
2544 // Show a checkbox offering to suppress further messages if this message is
2545 // being displayed within kJavascriptMessageExpectedDelay of the last one.
2546 if (time_since_last_message <
2547 base::TimeDelta::FromMilliseconds(kJavascriptMessageExpectedDelay))
2548 show_suppress_checkbox = true;
2549
[email protected]d82ed61e2009-06-16 02:46:222550 RunJavascriptMessageBox(this, frame_url, flags, message, default_prompt,
[email protected]420ae012009-04-24 05:16:322551 show_suppress_checkbox, reply_msg);
2552 } else {
2553 // If we are suppressing messages, just reply as is if the user immediately
2554 // pressed "Cancel".
[email protected]beb440c2009-11-06 04:08:542555 OnMessageBoxClosed(reply_msg, false, std::wstring());
[email protected]420ae012009-04-24 05:16:322556 }
2557}
2558
2559void TabContents::RunBeforeUnloadConfirm(const std::wstring& message,
2560 IPC::Message* reply_msg) {
[email protected]7ab1e7d62009-10-14 23:32:012561 is_showing_before_unload_dialog_ = true;
[email protected]d82ed61e2009-06-16 02:46:222562 RunBeforeUnloadDialog(this, message, reply_msg);
[email protected]420ae012009-04-24 05:16:322563}
2564
2565void TabContents::ShowModalHTMLDialog(const GURL& url, int width, int height,
2566 const std::string& json_arguments,
2567 IPC::Message* reply_msg) {
2568 if (delegate()) {
2569 HtmlDialogUIDelegate* dialog_delegate =
2570 new ModalHtmlDialogDelegate(url, width, height, json_arguments,
[email protected]d82ed61e2009-06-16 02:46:222571 reply_msg, this);
[email protected]420ae012009-04-24 05:16:322572 delegate()->ShowHtmlDialog(dialog_delegate, NULL);
2573 }
2574}
2575
2576void TabContents::PasswordFormsSeen(
[email protected]ed3fb032009-06-16 19:50:562577 const std::vector<webkit_glue::PasswordForm>& forms) {
[email protected]420ae012009-04-24 05:16:322578 GetPasswordManager()->PasswordFormsSeen(forms);
2579}
2580
[email protected]420ae012009-04-24 05:16:322581// Checks to see if we should generate a keyword based on the OSDD, and if
2582// necessary uses TemplateURLFetcher to download the OSDD and create a keyword.
2583void TabContents::PageHasOSDD(RenderViewHost* render_view_host,
2584 int32 page_id, const GURL& url,
2585 bool autodetected) {
2586 // Make sure page_id is the current page, and the TemplateURLModel is loaded.
2587 DCHECK(url.is_valid());
2588 if (!IsActiveEntry(page_id))
2589 return;
2590 TemplateURLModel* url_model = profile()->GetTemplateURLModel();
2591 if (!url_model)
2592 return;
2593 if (!url_model->loaded()) {
2594 url_model->Load();
2595 return;
2596 }
2597 if (!profile()->GetTemplateURLFetcher())
2598 return;
2599
2600 if (profile()->IsOffTheRecord())
2601 return;
2602
2603 const NavigationEntry* entry = controller_.GetLastCommittedEntry();
2604 DCHECK(entry);
2605
2606 const NavigationEntry* base_entry = entry;
2607 if (IsFormSubmit(base_entry)) {
2608 // If the current page is a form submit, find the last page that was not
2609 // a form submit and use its url to generate the keyword from.
2610 int index = controller_.last_committed_entry_index() - 1;
2611 while (index >= 0 && IsFormSubmit(controller_.GetEntryAtIndex(index)))
2612 index--;
2613 if (index >= 0)
2614 base_entry = controller_.GetEntryAtIndex(index);
2615 else
2616 base_entry = NULL;
2617 }
2618
2619 // We want to use the user typed URL if available since that represents what
2620 // the user typed to get here, and fall back on the regular URL if not.
2621 if (!base_entry)
2622 return;
2623 GURL keyword_url = base_entry->user_typed_url().is_valid() ?
2624 base_entry->user_typed_url() : base_entry->url();
2625 if (!keyword_url.is_valid())
2626 return;
2627 std::wstring keyword = TemplateURLModel::GenerateKeyword(keyword_url,
2628 autodetected);
[email protected]a88dbdd52010-01-15 19:25:532629
2630 // For JS added OSDD empty keyword is OK because we will generate keyword
2631 // later from OSDD content.
2632 if (autodetected && keyword.empty())
[email protected]420ae012009-04-24 05:16:322633 return;
2634 const TemplateURL* template_url =
2635 url_model->GetTemplateURLForKeyword(keyword);
2636 if (template_url && (!template_url->safe_for_autoreplace() ||
2637 template_url->originating_url() == url)) {
2638 // Either there is a user created TemplateURL for this keyword, or the
2639 // keyword has the same OSDD url and we've parsed it.
2640 return;
2641 }
2642
2643 // Download the OpenSearch description document. If this is successful a
2644 // new keyword will be created when done.
[email protected]420ae012009-04-24 05:16:322645 profile()->GetTemplateURLFetcher()->ScheduleDownload(
2646 keyword,
2647 url,
2648 base_entry->favicon().url(),
[email protected]d0cc9fb52009-06-23 02:26:162649 this,
[email protected]420ae012009-04-24 05:16:322650 autodetected);
2651}
2652
[email protected]420ae012009-04-24 05:16:322653GURL TabContents::GetAlternateErrorPageURL() const {
2654 GURL url;
2655 // Disable alternate error pages when in OffTheRecord/Incognito mode.
2656 if (profile()->IsOffTheRecord())
2657 return url;
2658
2659 PrefService* prefs = profile()->GetPrefs();
2660 DCHECK(prefs);
2661 if (prefs->GetBoolean(prefs::kAlternateErrorPagesEnabled)) {
[email protected]a33457b82009-09-21 23:07:152662 url = google_util::AppendGoogleLocaleParam(
2663 GURL(google_util::kLinkDoctorBaseURL));
[email protected]420ae012009-04-24 05:16:322664 url = google_util::AppendGoogleTLDParam(url);
2665 }
2666 return url;
2667}
2668
2669WebPreferences TabContents::GetWebkitPrefs() {
[email protected]6c32ce72010-03-08 05:18:062670 Profile* profile = render_view_host()->process()->profile();
[email protected]7ccddb8c2009-08-04 17:36:552671 bool is_dom_ui = false;
[email protected]6c32ce72010-03-08 05:18:062672 return RenderViewHostDelegateHelper::GetWebkitPrefs(profile, is_dom_ui);
[email protected]420ae012009-04-24 05:16:322673}
2674
[email protected]fa1cf0b82010-01-15 21:49:442675void TabContents::OnIgnoredUIEvent() {
2676 if (constrained_window_count()) {
2677 ConstrainedWindow* window = *constrained_window_begin();
2678 window->FocusConstrainedWindow();
2679 }
2680}
2681
[email protected]420ae012009-04-24 05:16:322682void TabContents::OnJSOutOfMemory() {
2683 AddInfoBar(new SimpleAlertInfoBarDelegate(
[email protected]938e1f92010-04-01 18:09:422684 this, l10n_util::GetString(IDS_JS_OUT_OF_MEMORY_PROMPT), NULL, true));
[email protected]420ae012009-04-24 05:16:322685}
2686
[email protected]420ae012009-04-24 05:16:322687void TabContents::OnCrossSiteResponse(int new_render_process_host_id,
2688 int new_request_id) {
[email protected]57c6a652009-05-04 07:58:342689 // Allows the TabContents to react when a cross-site response is ready to be
[email protected]420ae012009-04-24 05:16:322690 // delivered to a pending RenderViewHost. We must first run the onunload
2691 // handler of the old RenderViewHost before we can allow it to proceed.
2692 render_manager_.OnCrossSiteResponse(new_render_process_host_id,
2693 new_request_id);
2694}
2695
2696bool TabContents::CanBlur() const {
2697 return delegate() ? delegate()->CanBlur() : true;
2698}
2699
2700gfx::Rect TabContents::GetRootWindowResizerRect() const {
2701 if (delegate())
2702 return delegate()->GetRootWindowResizerRect();
2703 return gfx::Rect();
2704}
2705
2706void TabContents::RendererUnresponsive(RenderViewHost* rvh,
2707 bool is_during_unload) {
2708 if (is_during_unload) {
2709 // Hang occurred while firing the beforeunload/unload handler.
2710 // Pretend the handler fired so tab closing continues as if it had.
[email protected]829e7612009-04-25 01:15:112711 rvh->set_sudden_termination_allowed(true);
[email protected]420ae012009-04-24 05:16:322712
2713 if (!render_manager_.ShouldCloseTabOnUnresponsiveRenderer())
2714 return;
2715
2716 // If the tab hangs in the beforeunload/unload handler there's really
2717 // nothing we can do to recover. Pretend the unload listeners have
2718 // all fired and close the tab. If the hang is in the beforeunload handler
2719 // then the user will not have the option of cancelling the close.
2720 Close(rvh);
2721 return;
2722 }
2723
2724 if (render_view_host() && render_view_host()->IsRenderViewLive())
[email protected]e7b8d832009-11-20 22:41:002725 hung_renderer_dialog::ShowForTabContents(this);
[email protected]420ae012009-04-24 05:16:322726}
2727
2728void TabContents::RendererResponsive(RenderViewHost* render_view_host) {
[email protected]e7b8d832009-11-20 22:41:002729 hung_renderer_dialog::HideForTabContents(this);
[email protected]420ae012009-04-24 05:16:322730}
2731
2732void TabContents::LoadStateChanged(const GURL& url,
[email protected]094e5b22009-09-25 04:23:562733 net::LoadState load_state,
2734 uint64 upload_position,
2735 uint64 upload_size) {
[email protected]420ae012009-04-24 05:16:322736 load_state_ = load_state;
[email protected]094e5b22009-09-25 04:23:562737 upload_position_ = upload_position;
2738 upload_size_ = upload_size;
[email protected]d686e812009-06-03 19:10:292739 std::wstring languages =
2740 profile()->GetPrefs()->GetString(prefs::kAcceptLanguages);
[email protected]d686e812009-06-03 19:10:292741 std::string host = url.host();
[email protected]ce85f602009-11-07 01:34:532742 load_state_host_ =
2743 net::IDNToUnicode(host.c_str(), host.size(), languages, NULL);
[email protected]420ae012009-04-24 05:16:322744 if (load_state_ == net::LOAD_STATE_READING_RESPONSE)
2745 SetNotWaitingForResponse();
2746 if (is_loading())
[email protected]c9cd2222009-05-06 05:16:502747 NotifyNavigationStateChanged(INVALIDATE_LOAD | INVALIDATE_TAB);
[email protected]420ae012009-04-24 05:16:322748}
2749
[email protected]1ee614862009-07-06 20:29:182750bool TabContents::IsExternalTabContainer() const {
2751 if (!delegate())
2752 return false;
2753
2754 return delegate()->IsExternalTabContainer();
2755}
2756
[email protected]1ee614862009-07-06 20:29:182757void TabContents::DidInsertCSS() {
2758 // This RVHDelegate function is used for extensions and not us.
[email protected]420ae012009-04-24 05:16:322759}
2760
[email protected]130efb02009-09-18 18:54:352761void TabContents::FocusedNodeChanged() {
2762 NotificationService::current()->Notify(
2763 NotificationType::FOCUS_CHANGED_IN_PAGE,
2764 Source<RenderViewHost>(render_view_host()),
2765 NotificationService::NoDetails());
2766}
2767
[email protected]420ae012009-04-24 05:16:322768void TabContents::FileSelected(const FilePath& path,
2769 int index, void* params) {
[email protected]cdaf8d02010-03-30 19:52:472770 std::vector<FilePath> files;
2771 files.push_back(path);
2772 render_view_host()->FilesSelectedInChooser(files);
[email protected]420ae012009-04-24 05:16:322773}
2774
2775void TabContents::MultiFilesSelected(const std::vector<FilePath>& files,
2776 void* params) {
[email protected]cdaf8d02010-03-30 19:52:472777 render_view_host()->FilesSelectedInChooser(files);
[email protected]420ae012009-04-24 05:16:322778}
2779
2780void TabContents::FileSelectionCanceled(void* params) {
2781 // If the user cancels choosing a file to upload we pass back an
2782 // empty vector.
[email protected]cdaf8d02010-03-30 19:52:472783 render_view_host()->FilesSelectedInChooser(std::vector<FilePath>());
[email protected]420ae012009-04-24 05:16:322784}
2785
2786void TabContents::BeforeUnloadFiredFromRenderManager(
2787 bool proceed,
2788 bool* proceed_to_fire_unload) {
2789 if (delegate())
2790 delegate()->BeforeUnloadFired(this, proceed, proceed_to_fire_unload);
2791}
2792
2793void TabContents::UpdateRenderViewSizeForRenderManager() {
2794 // TODO(brettw) this is a hack. See TabContentsView::SizeContents.
[email protected]0323ee42010-02-17 22:03:262795 gfx::Size size = view_->GetContainerSize();
2796 // 0x0 isn't a valid window size (minimal window size is 1x1) but it may be
2797 // here during container initialization and normal window size will be set
2798 // later. In case of tab duplication this resizing to 0x0 prevents setting
2799 // normal size later so just ignore it.
2800 if (!size.IsEmpty())
2801 view_->SizeContents(size);
[email protected]420ae012009-04-24 05:16:322802}
2803
2804DOMUI* TabContents::CreateDOMUIForRenderManager(const GURL& url) {
[email protected]d82ed61e2009-06-16 02:46:222805 return DOMUIFactory::CreateDOMUIForURL(this, url);
[email protected]420ae012009-04-24 05:16:322806}
2807
2808NavigationEntry*
2809TabContents::GetLastCommittedNavigationEntryForRenderManager() {
2810 return controller_.GetLastCommittedEntry();
2811}
2812
2813bool TabContents::CreateRenderViewForRenderManager(
2814 RenderViewHost* render_view_host) {
[email protected]811bfe32009-07-01 08:46:252815 // If the pending navigation is to a DOMUI, tell the RenderView about any
2816 // bindings it will need enabled.
[email protected]420ae012009-04-24 05:16:322817 if (render_manager_.pending_dom_ui())
[email protected]811bfe32009-07-01 08:46:252818 render_view_host->AllowBindings(
2819 render_manager_.pending_dom_ui()->bindings());
[email protected]1adff062009-06-02 18:39:552820
[email protected]420ae012009-04-24 05:16:322821 RenderWidgetHostView* rwh_view = view_->CreateViewForWidget(render_view_host);
[email protected]1a98a932009-11-17 00:12:522822
2823 scoped_refptr<URLRequestContextGetter> request_context = request_context_;
2824 if (!request_context.get())
2825 request_context = profile()->GetRequestContext();
2826
2827 if (!render_view_host->CreateRenderView(request_context))
[email protected]420ae012009-04-24 05:16:322828 return false;
2829
2830 // Now that the RenderView has been created, we need to tell it its size.
2831 rwh_view->SetSize(view_->GetContainerSize());
2832
2833 UpdateMaxPageIDIfNecessary(render_view_host->site_instance(),
2834 render_view_host);
2835 return true;
2836}
2837
2838void TabContents::Observe(NotificationType type,
2839 const NotificationSource& source,
2840 const NotificationDetails& details) {
2841 switch (type.value) {
2842 case NotificationType::BOOKMARK_MODEL_LOADED:
2843 // BookmarkModel finished loading, fall through to update starred state.
2844 case NotificationType::URLS_STARRED: {
2845 // Somewhere, a URL has been starred.
2846 // Ignore notifications for profiles other than our current one.
2847 Profile* source_profile = Source<Profile>(source).ptr();
2848 if (!source_profile || !source_profile->IsSameProfile(profile()))
2849 return;
2850
2851 UpdateStarredStateForCurrentURL();
2852 break;
2853 }
2854 case NotificationType::PREF_CHANGED: {
2855 std::wstring* pref_name_in = Details<std::wstring>(details).ptr();
2856 DCHECK(Source<PrefService>(source).ptr() == profile()->GetPrefs());
2857 if (*pref_name_in == prefs::kAlternateErrorPagesEnabled) {
2858 UpdateAlternateErrorPageURL();
2859 } else if (*pref_name_in == prefs::kDefaultCharset ||
2860 StartsWithASCII(WideToUTF8(*pref_name_in), "webkit.webprefs.", true)
2861 ) {
2862 UpdateWebPreferences();
2863 } else {
2864 NOTREACHED() << "unexpected pref change notification" << *pref_name_in;
2865 }
2866 break;
2867 }
2868 case NotificationType::RENDER_WIDGET_HOST_DESTROYED:
2869 view_->RenderWidgetHostDestroyed(Source<RenderWidgetHost>(source).ptr());
2870 break;
2871
2872 case NotificationType::NAV_ENTRY_COMMITTED: {
2873 DCHECK(&controller_ == Source<NavigationController>(source).ptr());
2874
2875 NavigationController::LoadCommittedDetails& committed_details =
2876 *(Details<NavigationController::LoadCommittedDetails>(details).ptr());
2877 ExpireInfoBars(committed_details);
2878 break;
2879 }
2880
[email protected]d3ba77272009-09-03 00:06:092881#if defined(OS_LINUX)
2882 case NotificationType::BROWSER_THEME_CHANGED: {
[email protected]7ff431e2010-01-07 18:18:542883 renderer_preferences_util::UpdateFromSystemSettings(
2884 &renderer_preferences_, profile());
[email protected]d3ba77272009-09-03 00:06:092885 render_view_host()->SyncRendererPrefs();
2886 break;
2887 }
2888#endif
2889
[email protected]6c32ce72010-03-08 05:18:062890 case NotificationType::USER_STYLE_SHEET_UPDATED:
2891 UpdateWebPreferences();
2892 break;
2893
[email protected]79580c62010-02-02 02:36:252894 case NotificationType::CONTENT_SETTINGS_CHANGED: {
2895 Details<HostContentSettingsMap::ContentSettingsDetails>
2896 settings_details(details);
[email protected]79580c62010-02-02 02:36:252897 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]da0c8e92010-02-09 23:25:492898 GURL entry_url;
2899 std::string host;
2900 if (entry) {
2901 entry_url = entry->url();
2902 host = entry_url.host();
2903 }
[email protected]79580c62010-02-02 02:36:252904 Source<HostContentSettingsMap> content_settings(source);
2905 if (settings_details.ptr()->host().empty() ||
2906 settings_details.ptr()->host() == host) {
2907 render_view_host()->SendContentSettings(host,
[email protected]da0c8e92010-02-09 23:25:492908 content_settings.ptr()->GetContentSettings(entry_url));
[email protected]79580c62010-02-02 02:36:252909 }
2910 break;
2911 }
2912
[email protected]a888b29e62010-04-01 13:38:572913 case NotificationType::EXTENSION_LOADED: {
2914 if (!app_extension_ && !app_extension_for_current_page_ &&
2915 Source<Profile>(source).ptr() == profile()) {
2916 UpdateAppExtensionForCurrentPage();
2917 if (app_extension_for_current_page_)
2918 NotifyNavigationStateChanged(INVALIDATE_TAB);
2919 }
2920 break;
2921 }
2922
2923 case NotificationType::EXTENSION_UNLOADED:
2924 case NotificationType::EXTENSION_UNLOADED_DISABLED: {
2925 if (app_extension_for_current_page_ ==
2926 Details<Extension>(details).ptr()) {
2927 app_extension_for_current_page_ = NULL;
2928 UpdateAppExtensionForCurrentPage();
2929 NotifyNavigationStateChanged(INVALIDATE_TAB);
2930 }
2931 break;
2932 }
2933
[email protected]420ae012009-04-24 05:16:322934 default:
2935 NOTREACHED();
2936 }
2937}
[email protected]aacd7cc2009-09-02 14:51:102938
[email protected]a888b29e62010-04-01 13:38:572939void TabContents::UpdateAppExtensionIcon(Extension* extension) {
2940 app_extension_icon_.reset();
2941 if (extension) {
2942 app_extension_image_loader_.reset(new ImageLoadingTracker(this));
2943 app_extension_image_loader_->LoadImage(
2944 extension,
2945 extension->GetIconPath(Extension::EXTENSION_ICON_SMALLISH),
2946 gfx::Size(Extension::EXTENSION_ICON_SMALLISH,
2947 Extension::EXTENSION_ICON_SMALLISH),
2948 ImageLoadingTracker::CACHE);
2949 } else {
2950 app_extension_image_loader_.reset(NULL);
2951 }
2952}
2953
2954Extension* TabContents::GetExtensionContaining(const GURL& url) {
2955 ExtensionsService* extensions_service = profile()->GetExtensionsService();
2956 if (!extensions_service)
2957 return NULL;
2958
2959 Extension* extension = extensions_service->GetExtensionByURL(url);
2960 return extension ?
2961 extension : extensions_service->GetExtensionByWebExtent(url);
2962}
2963
2964void TabContents::UpdateAppExtensionForCurrentPage() {
2965 if (app_extension_) {
2966 // Tab has an explicit app extension; nothing to do.
2967 return;
2968 }
2969
2970 // Check the current extension before iterating through all extensions.
2971 if (app_extension_for_current_page_ &&
2972 app_extension_for_current_page_->web_extent().ContainsURL(GetURL())) {
2973 return;
2974 }
2975
2976 app_extension_for_current_page_ = GetExtensionContaining(GetURL());
2977 UpdateAppExtensionIcon(app_extension_for_current_page_);
2978}
2979
2980void TabContents::OnImageLoaded(SkBitmap* image, ExtensionResource resource,
2981 int index) {
2982 if (image) {
2983 app_extension_icon_ = *image;
2984 NotifyNavigationStateChanged(INVALIDATE_TAB);
2985 }
2986}
2987
[email protected]beb440c2009-11-06 04:08:542988std::wstring TabContents::GetMessageBoxTitle(const GURL& frame_url,
2989 bool is_alert) {
2990 if (!frame_url.has_host())
2991 return l10n_util::GetString(
2992 is_alert ? IDS_JAVASCRIPT_ALERT_DEFAULT_TITLE
2993 : IDS_JAVASCRIPT_MESSAGEBOX_DEFAULT_TITLE);
2994
2995 // We really only want the scheme, hostname, and port.
2996 GURL::Replacements replacements;
2997 replacements.ClearUsername();
2998 replacements.ClearPassword();
2999 replacements.ClearPath();
3000 replacements.ClearQuery();
3001 replacements.ClearRef();
3002 GURL clean_url = frame_url.ReplaceComponents(replacements);
3003
3004 // TODO(brettw) it should be easier than this to do the correct language
3005 // handling without getting the accept language from the profile.
3006 std::wstring base_address = gfx::ElideUrl(clean_url, gfx::Font(), 0,
3007 profile()->GetPrefs()->GetString(prefs::kAcceptLanguages));
3008 // Force URL to have LTR directionality.
[email protected]7cf1b6ce2010-03-20 06:37:013009 if (base::i18n::IsRTL())
3010 base::i18n::WrapStringWithLTRFormatting(&base_address);
[email protected]beb440c2009-11-06 04:08:543011
3012 return l10n_util::GetStringF(
3013 is_alert ? IDS_JAVASCRIPT_ALERT_TITLE : IDS_JAVASCRIPT_MESSAGEBOX_TITLE,
3014 base_address);
3015}
3016
3017gfx::NativeWindow TabContents::GetMessageBoxRootWindow() {
3018 return view_->GetTopLevelNativeWindow();
3019}
3020
3021void TabContents::OnMessageBoxClosed(IPC::Message* reply_msg,
3022 bool success,
3023 const std::wstring& prompt) {
3024 last_javascript_message_dismissal_ = base::TimeTicks::Now();
3025 if (is_showing_before_unload_dialog_ && !success) {
3026 // If a beforeunload dialog is canceled, we need to stop the throbber from
3027 // spinning, since we forced it to start spinning in Navigate.
3028 DidStopLoading();
3029
3030 tab_close_start_time_ = base::TimeTicks();
3031 }
3032 is_showing_before_unload_dialog_ = false;
3033 render_view_host()->JavaScriptMessageBoxClosed(reply_msg, success, prompt);
3034}
3035
3036void TabContents::SetSuppressMessageBoxes(bool suppress_message_boxes) {
3037 set_suppress_javascript_messages(suppress_message_boxes);
3038}
3039
[email protected]41fc0322009-09-04 22:23:403040void TabContents::set_encoding(const std::string& encoding) {
[email protected]aacd7cc2009-09-02 14:51:103041 encoding_ = CharacterEncoding::GetCanonicalEncodingNameByAliasName(encoding);
3042}
[email protected]f45d2a72010-03-08 23:28:353043
3044void TabContents::SetAppIcon(const SkBitmap& app_icon) {
3045 app_icon_ = app_icon;
3046 NotifyNavigationStateChanged(INVALIDATE_TITLE);
3047}