blob: 704428d28fa422e0d6a7a189976e9349a8036381 [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]beb440c2009-11-06 04:08:547#include "app/gfx/text_elider.h"
[email protected]a92b8642009-05-05 23:38:568#include "app/l10n_util.h"
[email protected]9929da92009-05-05 02:05:119#include "app/resource_bundle.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"
12#include "base/process_util.h"
[email protected]96d185d2009-04-24 03:28:5413#include "base/string16.h"
[email protected]996fd702009-09-04 19:12:3714#include "base/string_util.h"
[email protected]96d185d2009-04-24 03:28:5415#include "base/time.h"
[email protected]21674df2009-10-28 19:51:2116#include "chrome/browser/autofill/autofill_manager.h"
[email protected]66ba4932009-06-04 19:22:1317#include "chrome/browser/blocked_popup_container.h"
[email protected]420ae012009-04-24 05:16:3218#include "chrome/browser/bookmarks/bookmark_model.h"
[email protected]96d185d2009-04-24 03:28:5419#include "chrome/browser/browser_process.h"
[email protected]cf68b092010-01-15 20:05:5520#include "chrome/browser/browser_shutdown.h"
initial.commit09911bf2008-07-26 23:55:2921#include "chrome/browser/cert_store.h"
[email protected]aacd7cc2009-09-02 14:51:1022#include "chrome/browser/character_encoding.h"
[email protected]96d185d2009-04-24 03:28:5423#include "chrome/browser/debugger/devtools_manager.h"
[email protected]420ae012009-04-24 05:16:3224#include "chrome/browser/dom_operation_notification_details.h"
[email protected]96d185d2009-04-24 03:28:5425#include "chrome/browser/dom_ui/dom_ui.h"
26#include "chrome/browser/dom_ui/dom_ui_factory.h"
[email protected]bcef0dc02009-02-28 00:35:0227#include "chrome/browser/download/download_item_model.h"
[email protected]420ae012009-04-24 05:16:3228#include "chrome/browser/download/download_manager.h"
29#include "chrome/browser/download/download_request_manager.h"
[email protected]e7eaedde2009-09-25 20:09:4930#include "chrome/browser/external_protocol_handler.h"
[email protected]92c6f9b92009-10-24 04:35:0831#include "chrome/browser/extensions/extensions_service.h"
[email protected]0189bc722009-08-28 21:56:4832#include "chrome/browser/favicon_service.h"
[email protected]19289d02009-10-21 21:47:0033#include "chrome/browser/form_field_history_manager.h"
[email protected]420ae012009-04-24 05:16:3234#include "chrome/browser/google_util.h"
[email protected]79580c62010-02-02 02:36:2535#include "chrome/browser/host_content_settings_map.h"
[email protected]8897c382009-05-06 17:53:2636#include "chrome/browser/hung_renderer_dialog.h"
[email protected]12f74a92010-02-05 22:32:1437#include "chrome/browser/message_box_handler.h"
[email protected]420ae012009-04-24 05:16:3238#include "chrome/browser/load_from_memory_cache_details.h"
39#include "chrome/browser/load_notification_details.h"
[email protected]84abba62009-10-07 17:01:4440#include "chrome/browser/metrics/metric_event_duration_details.h"
[email protected]ea0e80892009-08-22 00:36:0541#include "chrome/browser/modal_html_dialog_delegate.h"
[email protected]c8865482009-07-23 20:40:1042#include "chrome/browser/omnibox_search_hint.h"
[email protected]fdd61c62009-04-22 19:22:5743#include "chrome/browser/password_manager/password_manager.h"
44#include "chrome/browser/plugin_installer.h"
[email protected]ce560f82009-06-03 09:39:4445#include "chrome/browser/profile.h"
[email protected]1db6ff152009-10-12 15:32:0746#include "chrome/browser/renderer_host/render_process_host.h"
[email protected]8cb5d5b2010-02-09 11:36:1647#include "chrome/browser/renderer_host/render_view_host.h"
[email protected]96d185d2009-04-24 03:28:5448#include "chrome/browser/renderer_host/render_widget_host_view.h"
[email protected]fd694982009-07-03 17:32:3749#include "chrome/browser/renderer_host/resource_request_details.h"
[email protected]1db6ff152009-10-12 15:32:0750#include "chrome/browser/renderer_host/site_instance.h"
[email protected]d4a00a72010-01-29 01:44:4251#include "chrome/browser/renderer_host/translation_service.h"
[email protected]96d185d2009-04-24 03:28:5452#include "chrome/browser/renderer_host/web_cache_manager.h"
[email protected]93623c5d2009-12-10 21:40:3253#include "chrome/browser/renderer_preferences_util.h"
[email protected]c37e3b62010-01-22 00:15:1754#include "chrome/browser/sessions/session_types.h"
[email protected]bd1ad682009-05-15 22:19:1755#include "chrome/browser/tab_contents/infobar_delegate.h"
[email protected]7e383692009-06-12 19:14:5456#include "chrome/browser/tab_contents/interstitial_page.h"
[email protected]f3ec7742009-01-15 00:59:1657#include "chrome/browser/tab_contents/navigation_entry.h"
[email protected]de34a962009-06-26 23:22:4358#include "chrome/browser/tab_contents/provisional_load_details.h"
[email protected]f3ec7742009-01-15 00:59:1659#include "chrome/browser/tab_contents/tab_contents_delegate.h"
[email protected]fdd61c62009-04-22 19:22:5760#include "chrome/browser/tab_contents/tab_contents_view.h"
[email protected]58dca552009-06-17 00:35:0261#include "chrome/browser/tab_contents/thumbnail_generator.h"
[email protected]449478302009-06-09 20:04:2862#include "chrome/browser/thumbnail_store.h"
[email protected]420ae012009-04-24 05:16:3263#include "chrome/browser/search_engines/template_url_fetcher.h"
64#include "chrome/browser/search_engines/template_url_model.h"
[email protected]35f7d212009-04-29 21:19:2765#include "chrome/common/chrome_switches.h"
[email protected]3b355642010-02-05 16:01:4966#include "chrome/common/extensions/extension.h"
[email protected]5d246db22009-10-27 06:17:5767#include "chrome/common/extensions/extension_action.h"
[email protected]bfd04a62009-02-01 18:16:5668#include "chrome/common/notification_service.h"
[email protected]89d6e6e2009-12-03 23:06:5169#include "chrome/common/platform_util.h"
initial.commit09911bf2008-07-26 23:55:2970#include "chrome/common/pref_names.h"
[email protected]1eb89e82008-08-15 12:27:0371#include "chrome/common/pref_service.h"
[email protected]420ae012009-04-24 05:16:3272#include "chrome/common/render_messages.h"
[email protected]1ee614862009-07-06 20:29:1873#include "chrome/common/renderer_preferences.h"
[email protected]96d185d2009-04-24 03:28:5474#include "chrome/common/url_constants.h"
[email protected]8897c382009-05-06 17:53:2675#include "grit/generated_resources.h"
[email protected]420ae012009-04-24 05:16:3276#include "grit/locale_settings.h"
[email protected]fd626782010-01-22 05:26:0677#include "grit/platform_locale_settings.h"
[email protected]420ae012009-04-24 05:16:3278#include "net/base/mime_util.h"
79#include "net/base/net_errors.h"
[email protected]d686e812009-06-03 19:10:2980#include "net/base/net_util.h"
[email protected]420ae012009-04-24 05:16:3281#include "net/base/registry_controlled_domain.h"
[email protected]1ee614862009-07-06 20:29:1882#include "webkit/glue/password_form.h"
83#include "webkit/glue/webpreferences.h"
[email protected]3e45ba92009-02-20 21:09:0084
[email protected]304f8792009-08-19 21:28:0985#if defined(OS_CHROMEOS)
86// For GdkScreen
87#include <gdk/gdk.h>
[email protected]0ba5c5e2009-08-20 16:47:1388#endif // defined(OS_CHROMEOS)
89
[email protected]420ae012009-04-24 05:16:3290// Cross-Site Navigations
91//
92// If a TabContents is told to navigate to a different web site (as determined
93// by SiteInstance), it will replace its current RenderViewHost with a new
94// RenderViewHost dedicated to the new SiteInstance. This works as follows:
95//
96// - Navigate determines whether the destination is cross-site, and if so,
97// it creates a pending_render_view_host_ and moves into the PENDING
98// RendererState.
99// - The pending RVH is "suspended," so that no navigation messages are sent to
100// its renderer until the onbeforeunload JavaScript handler has a chance to
101// run in the current RVH.
102// - The pending RVH tells CrossSiteRequestManager (a thread-safe singleton)
103// that it has a pending cross-site request. ResourceDispatcherHost will
104// check for this when the response arrives.
105// - The current RVH runs its onbeforeunload handler. If it returns false, we
106// cancel all the pending logic and go back to NORMAL. Otherwise we allow
107// the pending RVH to send the navigation request to its renderer.
108// - ResourceDispatcherHost receives a ResourceRequest on the IO thread. It
109// checks CrossSiteRequestManager to see that the RVH responsible has a
110// pending cross-site request, and then installs a CrossSiteEventHandler.
111// - When RDH receives a response, the BufferedEventHandler determines whether
112// it is a download. If so, it sends a message to the new renderer causing
113// it to cancel the request, and the download proceeds in the download
114// thread. For now, we stay in a PENDING state (with a pending RVH) until
[email protected]57c6a652009-05-04 07:58:34115// the next DidNavigate event for this TabContents. This isn't ideal, but it
[email protected]420ae012009-04-24 05:16:32116// doesn't affect any functionality.
117// - After RDH receives a response and determines that it is safe and not a
118// download, it pauses the response to first run the old page's onunload
119// handler. It does this by asynchronously calling the OnCrossSiteResponse
[email protected]57c6a652009-05-04 07:58:34120// method of TabContents on the UI thread, which sends a ClosePage message
[email protected]420ae012009-04-24 05:16:32121// to the current RVH.
122// - Once the onunload handler is finished, a ClosePage_ACK message is sent to
123// the ResourceDispatcherHost, who unpauses the response. Data is then sent
124// to the pending RVH.
125// - The pending renderer sends a FrameNavigate message that invokes the
126// DidNavigate method. This replaces the current RVH with the
127// pending RVH and goes back to the NORMAL RendererState.
128
129namespace {
130
131// Amount of time we wait between when a key event is received and the renderer
132// is queried for its state and pushed to the NavigationEntry.
133const int kQueryStateDelay = 5000;
134
135const int kSyncWaitDelay = 40;
136
[email protected]304f8792009-08-19 21:28:09137#if defined(OS_CHROMEOS)
138// If a popup window is bigger than this fraction of the screen on chrome os,
139// turn it into a tab
140const float kMaxWidthFactor = 0.5;
141const float kMaxHeightFactor = 0.6;
142#endif
143
[email protected]420ae012009-04-24 05:16:32144// If another javascript message box is displayed within
145// kJavascriptMessageExpectedDelay of a previous javascript message box being
146// dismissed, display an option to suppress future message boxes from this
147// contents.
148const int kJavascriptMessageExpectedDelay = 1000;
149
[email protected]420ae012009-04-24 05:16:32150// The list of prefs we want to observe.
151const wchar_t* kPrefsToObserve[] = {
152 prefs::kAlternateErrorPagesEnabled,
153 prefs::kWebKitJavaEnabled,
154 prefs::kWebKitJavascriptEnabled,
155 prefs::kWebKitLoadsImagesAutomatically,
156 prefs::kWebKitPluginsEnabled,
157 prefs::kWebKitUsesUniversalDetector,
158 prefs::kWebKitSerifFontFamily,
159 prefs::kWebKitSansSerifFontFamily,
160 prefs::kWebKitFixedFontFamily,
161 prefs::kWebKitDefaultFontSize,
162 prefs::kWebKitDefaultFixedFontSize,
163 prefs::kDefaultCharset
164 // kWebKitStandardFontIsSerif needs to be added
165 // if we let users pick which font to use, serif or sans-serif when
166 // no font is specified or a CSS generic family (serif or sans-serif)
167 // is not specified.
168};
169
170const int kPrefsToObserveLength = arraysize(kPrefsToObserve);
171
[email protected]420ae012009-04-24 05:16:32172// Returns true if the entry's transition type is FORM_SUBMIT.
173bool IsFormSubmit(const NavigationEntry* entry) {
174 return (PageTransition::StripQualifier(entry->transition_type()) ==
175 PageTransition::FORM_SUBMIT);
176}
177
178#if defined(OS_WIN)
179
180BOOL CALLBACK InvalidateWindow(HWND hwnd, LPARAM lparam) {
181 // Note: erase is required to properly paint some widgets borders. This can
182 // be seen with textfields.
183 InvalidateRect(hwnd, NULL, TRUE);
184 return TRUE;
185}
186#endif
187
[email protected]5e369672009-11-03 23:48:30188ViewMsg_Navigate_Params::NavigationType GetNavigationType(
189 Profile* profile, const NavigationEntry& entry, bool reload) {
190 if (reload)
191 return ViewMsg_Navigate_Params::RELOAD;
192
193 if (entry.restore_type() == NavigationEntry::RESTORE_LAST_SESSION &&
194 profile->DidLastSessionExitCleanly())
195 return ViewMsg_Navigate_Params::RESTORE;
196
197 return ViewMsg_Navigate_Params::NORMAL;
198}
199
200void MakeNavigateParams(Profile* profile, const NavigationEntry& entry,
201 bool reload, ViewMsg_Navigate_Params* params) {
[email protected]056de2d2009-06-26 16:41:34202 params->page_id = entry.page_id();
203 params->url = entry.url();
204 params->referrer = entry.referrer();
205 params->transition = entry.transition_type();
206 params->state = entry.content_state();
[email protected]5e369672009-11-03 23:48:30207 params->navigation_type = GetNavigationType(profile, entry, reload);
[email protected]056de2d2009-06-26 16:41:34208 params->request_time = base::Time::Now();
209}
210
[email protected]420ae012009-04-24 05:16:32211} // namespace
212
213// -----------------------------------------------------------------------------
214
[email protected]fdd61c62009-04-22 19:22:57215// static
216int TabContents::find_request_id_counter_ = -1;
217
[email protected]420ae012009-04-24 05:16:32218
[email protected]420ae012009-04-24 05:16:32219TabContents::TabContents(Profile* profile,
220 SiteInstance* site_instance,
221 int routing_id,
[email protected]34ac70502009-09-25 17:07:23222 const TabContents* base_tab_contents)
[email protected]b680ad22009-04-15 23:19:42223 : delegate_(NULL),
[email protected]66ba4932009-06-04 19:22:13224 ALLOW_THIS_IN_INITIALIZER_LIST(controller_(this, profile)),
225 ALLOW_THIS_IN_INITIALIZER_LIST(view_(
[email protected]d82ed61e2009-06-16 02:46:22226 TabContentsView::Create(this))),
227 ALLOW_THIS_IN_INITIALIZER_LIST(render_manager_(this, this)),
[email protected]fdd61c62009-04-22 19:22:57228 property_bag_(),
229 registrar_(),
[email protected]d82ed61e2009-06-16 02:46:22230 ALLOW_THIS_IN_INITIALIZER_LIST(printing_(*this)),
[email protected]fdd61c62009-04-22 19:22:57231 save_package_(),
[email protected]19289d02009-10-21 21:47:00232 form_field_history_manager_(),
[email protected]21674df2009-10-28 19:51:21233 autofill_manager_(),
[email protected]fdd61c62009-04-22 19:22:57234 password_manager_(),
235 plugin_installer_(),
[email protected]d82ed61e2009-06-16 02:46:22236 ALLOW_THIS_IN_INITIALIZER_LIST(fav_icon_helper_(this)),
[email protected]fdd61c62009-04-22 19:22:57237 select_file_dialog_(),
[email protected]d5f942ba2008-09-26 19:30:34238 is_loading_(false),
initial.commit09911bf2008-07-26 23:55:29239 is_crashed_(false),
[email protected]d5f942ba2008-09-26 19:30:34240 waiting_for_response_(false),
[email protected]d5f942ba2008-09-26 19:30:34241 max_page_id_(-1),
[email protected]fdd61c62009-04-22 19:22:57242 current_load_start_(),
243 load_state_(net::LOAD_STATE_IDLE),
244 load_state_host_(),
[email protected]094e5b22009-09-25 04:23:56245 upload_size_(0),
246 upload_position_(0),
[email protected]fdd61c62009-04-22 19:22:57247 received_page_title_(false),
248 is_starred_(false),
249 contents_mime_type_(),
250 encoding_(),
[email protected]332af7732009-03-11 13:21:35251 blocked_popups_(NULL),
[email protected]8cc8d492010-02-02 10:40:49252 dont_notify_render_view_(false),
[email protected]fdd61c62009-04-22 19:22:57253 infobar_delegates_(),
[email protected]fdd61c62009-04-22 19:22:57254 find_ui_active_(false),
255 find_op_aborted_(false),
256 current_find_request_id_(find_request_id_counter_++),
[email protected]e491f1c2009-05-22 20:28:12257 last_search_case_sensitive_(false),
258 last_search_prepopulate_text_(NULL),
259 last_search_result_(),
[email protected]fdd61c62009-04-22 19:22:57260 capturing_contents_(false),
261 is_being_destroyed_(false),
262 notify_disconnection_(false),
263 history_requests_(),
264#if defined(OS_WIN)
265 message_box_active_(CreateEvent(NULL, TRUE, FALSE, NULL)),
266#endif
267 last_javascript_message_dismissal_(),
[email protected]c2e74fe82009-09-03 17:57:44268 suppress_javascript_messages_(false),
[email protected]7ab1e7d62009-10-14 23:32:01269 is_showing_before_unload_dialog_(false),
[email protected]7ff431e2010-01-07 18:18:54270 renderer_preferences_(),
[email protected]1a242c32010-02-02 21:20:54271 opener_dom_ui_type_(DOMUIFactory::kNoDOMUI),
[email protected]3b355642010-02-05 16:01:49272 app_extension_(NULL) {
[email protected]0de80162010-02-03 04:52:35273 ClearBlockedContentSettings();
[email protected]7ff431e2010-01-07 18:18:54274 renderer_preferences_util::UpdateFromSystemSettings(
275 &renderer_preferences_, profile);
276
[email protected]16779842009-07-08 23:45:29277#if defined(OS_CHROMEOS)
[email protected]58dca552009-06-17 00:35:02278 // Make sure the thumbnailer is started before starting the render manager.
279 // The thumbnailer will want to listen for RVH creations, one of which will
280 // happen in RVHManager::Init.
281 ThumbnailGenerator* generator = g_browser_process->GetThumbnailGenerator();
282 if (generator)
283 generator->StartThumbnailing();
284#endif
285
[email protected]12636df2009-09-28 22:32:21286 render_manager_.Init(profile, site_instance, routing_id);
[email protected]420ae012009-04-24 05:16:32287
[email protected]34ac70502009-09-25 17:07:23288 // We have the initial size of the view be based on the size of the passed in
289 // tab contents (normally a tab from the same window).
290 view_->CreateView(base_tab_contents ?
291 base_tab_contents->view()->GetContainerSize() : gfx::Size());
[email protected]420ae012009-04-24 05:16:32292
293 // Register for notifications about all interested prefs change.
294 PrefService* prefs = profile->GetPrefs();
295 if (prefs) {
296 for (int i = 0; i < kPrefsToObserveLength; ++i)
297 prefs->AddPrefObserver(kPrefsToObserve[i], this);
298 }
299
300 // Register for notifications about URL starredness changing on any profile.
301 registrar_.Add(this, NotificationType::URLS_STARRED,
302 NotificationService::AllSources());
303 registrar_.Add(this, NotificationType::BOOKMARK_MODEL_LOADED,
304 NotificationService::AllSources());
305 registrar_.Add(this, NotificationType::RENDER_WIDGET_HOST_DESTROYED,
306 NotificationService::AllSources());
[email protected]d3ba77272009-09-03 00:06:09307#if defined(OS_LINUX)
308 registrar_.Add(this, NotificationType::BROWSER_THEME_CHANGED,
309 NotificationService::AllSources());
310#endif
[email protected]420ae012009-04-24 05:16:32311
[email protected]79580c62010-02-02 02:36:25312 // Register for notifications about content setting changes.
313 registrar_.Add(this, NotificationType::CONTENT_SETTINGS_CHANGED,
314 NotificationService::AllSources());
315
[email protected]420ae012009-04-24 05:16:32316 // Keep a global copy of the previous search string (if any).
317 static string16 global_last_search = string16();
[email protected]e491f1c2009-05-22 20:28:12318 last_search_prepopulate_text_ = &global_last_search;
[email protected]840b1512009-07-21 16:53:46319
[email protected]c8865482009-07-23 20:40:10320 // Set-up the showing of the omnibox search infobar if applicable.
321 if (OmniboxSearchHint::IsEnabled(profile))
322 omnibox_search_hint_.reset(new OmniboxSearchHint(this));
[email protected]332af7732009-03-11 13:21:35323}
initial.commit09911bf2008-07-26 23:55:29324
325TabContents::~TabContents() {
[email protected]420ae012009-04-24 05:16:32326 is_being_destroyed_ = true;
327
[email protected]bfe4c1582009-10-02 18:11:09328 // We don't want any notifications while we're running our destructor.
[email protected]420ae012009-04-24 05:16:32329 registrar_.RemoveAll();
330
331 // Unregister the notifications of all observed prefs change.
332 PrefService* prefs = profile()->GetPrefs();
333 if (prefs) {
334 for (int i = 0; i < kPrefsToObserveLength; ++i)
335 prefs->RemovePrefObserver(kPrefsToObserve[i], this);
336 }
337
[email protected]420ae012009-04-24 05:16:32338 NotifyDisconnected();
[email protected]e7b8d832009-11-20 22:41:00339 hung_renderer_dialog::HideForTabContents(this);
[email protected]420ae012009-04-24 05:16:32340
[email protected]420ae012009-04-24 05:16:32341 // First cleanly close all child windows.
342 // TODO(mpcomplete): handle case if MaybeCloseChildWindows() already asked
343 // some of these to close. CloseWindows is async, so it might get called
344 // twice before it runs.
[email protected]8cc8d492010-02-02 10:40:49345 CloseConstrainedWindows();
[email protected]420ae012009-04-24 05:16:32346
[email protected]8cc8d492010-02-02 10:40:49347 // Close all blocked popups.
[email protected]66ba4932009-06-04 19:22:13348 if (blocked_popups_)
349 blocked_popups_->Destroy();
350
[email protected]420ae012009-04-24 05:16:32351 // Notify any observer that have a reference on this tab contents.
352 NotificationService::current()->Notify(
353 NotificationType::TAB_CONTENTS_DESTROYED,
354 Source<TabContents>(this),
355 NotificationService::NoDetails());
356
[email protected]1e5e74f2009-05-27 20:55:12357 // Notify any lasting InfobarDelegates that have not yet been removed that
358 // whatever infobar they were handling in this TabContents has closed,
359 // because the TabContents is going away entirely.
360 // This must happen after the TAB_CONTENTS_DESTROYED notification as the
361 // notification may trigger infobars calls that access their delegate. (and
362 // some implementations of InfoBarDelegate do delete themselves on
363 // InfoBarClosed()).
364 for (int i = 0; i < infobar_delegate_count(); ++i) {
365 InfoBarDelegate* delegate = GetInfoBarDelegateAt(i);
366 delegate->InfoBarClosed();
367 }
368 infobar_delegates_.clear();
369
[email protected]420ae012009-04-24 05:16:32370 // TODO(brettw) this should be moved to the view.
371#if defined(OS_WIN)
372 // If we still have a window handle, destroy it. GetNativeView can return
373 // NULL if this contents was part of a window that closed.
374 if (GetNativeView())
375 ::DestroyWindow(GetNativeView());
376#endif
[email protected]7ab1e7d62009-10-14 23:32:01377
378 // OnCloseStarted isn't called in unit tests.
379 if (!tab_close_start_time_.is_null()) {
380 UMA_HISTOGRAM_TIMES("Tab.Close",
381 base::TimeTicks::Now() - tab_close_start_time_);
382 }
initial.commit09911bf2008-07-26 23:55:29383}
384
[email protected]d5f942ba2008-09-26 19:30:34385// static
386void TabContents::RegisterUserPrefs(PrefService* prefs) {
[email protected]420ae012009-04-24 05:16:32387 prefs->RegisterBooleanPref(prefs::kAlternateErrorPagesEnabled, true);
388
389 WebPreferences pref_defaults;
390 prefs->RegisterBooleanPref(prefs::kWebKitJavascriptEnabled,
391 pref_defaults.javascript_enabled);
392 prefs->RegisterBooleanPref(prefs::kWebKitWebSecurityEnabled,
393 pref_defaults.web_security_enabled);
394 prefs->RegisterBooleanPref(
395 prefs::kWebKitJavascriptCanOpenWindowsAutomatically, true);
396 prefs->RegisterBooleanPref(prefs::kWebKitLoadsImagesAutomatically,
397 pref_defaults.loads_images_automatically);
398 prefs->RegisterBooleanPref(prefs::kWebKitPluginsEnabled,
399 pref_defaults.plugins_enabled);
400 prefs->RegisterBooleanPref(prefs::kWebKitDomPasteEnabled,
401 pref_defaults.dom_paste_enabled);
402 prefs->RegisterBooleanPref(prefs::kWebKitShrinksStandaloneImagesToFit,
403 pref_defaults.shrinks_standalone_images_to_fit);
[email protected]0df30122009-06-03 12:13:08404 prefs->RegisterStringPref(prefs::kWebKitInspectorSettings,
[email protected]8922e1f2009-10-03 05:01:26405 UTF8ToWide(pref_defaults.inspector_settings));
[email protected]420ae012009-04-24 05:16:32406 prefs->RegisterBooleanPref(prefs::kWebKitTextAreasAreResizable,
407 pref_defaults.text_areas_are_resizable);
408 prefs->RegisterBooleanPref(prefs::kWebKitJavaEnabled,
409 pref_defaults.java_enabled);
410
411 prefs->RegisterLocalizedStringPref(prefs::kAcceptLanguages,
412 IDS_ACCEPT_LANGUAGES);
413 prefs->RegisterLocalizedStringPref(prefs::kDefaultCharset,
414 IDS_DEFAULT_ENCODING);
415 prefs->RegisterLocalizedBooleanPref(prefs::kWebKitStandardFontIsSerif,
416 IDS_STANDARD_FONT_IS_SERIF);
417 prefs->RegisterLocalizedStringPref(prefs::kWebKitFixedFontFamily,
418 IDS_FIXED_FONT_FAMILY);
419 prefs->RegisterLocalizedStringPref(prefs::kWebKitSerifFontFamily,
420 IDS_SERIF_FONT_FAMILY);
421 prefs->RegisterLocalizedStringPref(prefs::kWebKitSansSerifFontFamily,
422 IDS_SANS_SERIF_FONT_FAMILY);
423 prefs->RegisterLocalizedStringPref(prefs::kWebKitCursiveFontFamily,
424 IDS_CURSIVE_FONT_FAMILY);
425 prefs->RegisterLocalizedStringPref(prefs::kWebKitFantasyFontFamily,
426 IDS_FANTASY_FONT_FAMILY);
427 prefs->RegisterLocalizedIntegerPref(prefs::kWebKitDefaultFontSize,
428 IDS_DEFAULT_FONT_SIZE);
429 prefs->RegisterLocalizedIntegerPref(prefs::kWebKitDefaultFixedFontSize,
430 IDS_DEFAULT_FIXED_FONT_SIZE);
431 prefs->RegisterLocalizedIntegerPref(prefs::kWebKitMinimumFontSize,
432 IDS_MINIMUM_FONT_SIZE);
433 prefs->RegisterLocalizedIntegerPref(prefs::kWebKitMinimumLogicalFontSize,
434 IDS_MINIMUM_LOGICAL_FONT_SIZE);
435 prefs->RegisterLocalizedBooleanPref(prefs::kWebKitUsesUniversalDetector,
436 IDS_USES_UNIVERSAL_DETECTOR);
437 prefs->RegisterLocalizedStringPref(prefs::kStaticEncodings,
438 IDS_STATIC_ENCODING_LIST);
initial.commit09911bf2008-07-26 23:55:29439}
440
[email protected]13367f72009-07-08 16:39:02441// Returns true if contains content rendered by an extension.
442bool TabContents::HostsExtension() const {
443 return GetURL().SchemeIs(chrome::kExtensionScheme);
[email protected]d5f942ba2008-09-26 19:30:34444}
445
[email protected]6e95706d2009-04-23 22:55:01446PasswordManager* TabContents::GetPasswordManager() {
447 if (password_manager_.get() == NULL)
448 password_manager_.reset(new PasswordManager(this));
449 return password_manager_.get();
450}
451
452PluginInstaller* TabContents::GetPluginInstaller() {
453 if (plugin_installer_.get() == NULL)
454 plugin_installer_.reset(new PluginInstaller(this));
455 return plugin_installer_.get();
456}
457
[email protected]8cb5d5b2010-02-09 11:36:16458RenderProcessHost* TabContents::GetRenderProcessHost() const {
459 return render_manager_.current_host()->process();
460}
461
[email protected]3b355642010-02-05 16:01:49462void TabContents::SetAppExtension(Extension* extension) {
463 DCHECK(!extension || extension->IsApp());
464 app_extension_ = extension;
[email protected]fca656c2010-02-10 20:30:10465
466 NotificationService::current()->Notify(
467 NotificationType::TAB_CONTENTS_APPLICATION_EXTENSION_CHANGED,
468 Source<TabContents>(this),
469 NotificationService::NoDetails());
[email protected]3b355642010-02-05 16:01:49470}
471
[email protected]d5f942ba2008-09-26 19:30:34472const GURL& TabContents::GetURL() const {
473 // We may not have a navigation entry yet
[email protected]ce3fa3c2009-04-20 19:55:57474 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]ebe89e062009-08-13 23:16:54475 return entry ? entry->virtual_url() : GURL::EmptyGURL();
[email protected]d5f942ba2008-09-26 19:30:34476}
477
[email protected]96d185d2009-04-24 03:28:54478const string16& TabContents::GetTitle() const {
[email protected]4c6092c5b2009-06-06 00:23:55479 // Transient entries take precedence. They are used for interstitial pages
480 // that are shown on top of existing pages.
481 NavigationEntry* entry = controller_.GetTransientEntry();
482 if (entry)
483 return entry->GetTitleForDisplay(&controller_);
484
[email protected]96d185d2009-04-24 03:28:54485 DOMUI* our_dom_ui = render_manager_.pending_dom_ui() ?
486 render_manager_.pending_dom_ui() : render_manager_.dom_ui();
487 if (our_dom_ui) {
488 // Don't override the title in view source mode.
[email protected]4c6092c5b2009-06-06 00:23:55489 entry = controller_.GetActiveEntry();
[email protected]96d185d2009-04-24 03:28:54490 if (!(entry && entry->IsViewSourceMode())) {
491 // Give the DOM UI the chance to override our title.
492 const string16& title = our_dom_ui->overridden_title();
493 if (!title.empty())
494 return title;
495 }
496 }
497
498 // We use the title for the last committed entry rather than a pending
499 // navigation entry. For example, when the user types in a URL, we want to
500 // keep the old page's title until the new load has committed and we get a new
501 // title.
[email protected]96d185d2009-04-24 03:28:54502 entry = controller_.GetLastCommittedEntry();
503 if (entry)
504 return entry->GetTitleForDisplay(&controller_);
[email protected]96d185d2009-04-24 03:28:54505 return EmptyString16();
506}
507
[email protected]d5f942ba2008-09-26 19:30:34508int32 TabContents::GetMaxPageID() {
509 if (GetSiteInstance())
510 return GetSiteInstance()->max_page_id();
511 else
512 return max_page_id_;
513}
514
515void TabContents::UpdateMaxPageID(int32 page_id) {
516 // Ensure both the SiteInstance and RenderProcessHost update their max page
[email protected]57c6a652009-05-04 07:58:34517 // IDs in sync. Only TabContents will also have site instances, except during
[email protected]d5f942ba2008-09-26 19:30:34518 // testing.
519 if (GetSiteInstance())
520 GetSiteInstance()->UpdateMaxPageID(page_id);
[email protected]8cb5d5b2010-02-09 11:36:16521 GetRenderProcessHost()->UpdateMaxPageID(page_id);
[email protected]d5f942ba2008-09-26 19:30:34522}
523
[email protected]96d185d2009-04-24 03:28:54524SiteInstance* TabContents::GetSiteInstance() const {
525 return render_manager_.current_host()->site_instance();
526}
527
[email protected]d5f942ba2008-09-26 19:30:34528const std::wstring TabContents::GetDefaultTitle() const {
529 return l10n_util::GetString(IDS_DEFAULT_TAB_TITLE);
530}
531
[email protected]96d185d2009-04-24 03:28:54532bool TabContents::ShouldDisplayURL() {
[email protected]8ab24cc2009-11-13 20:56:15533 // Don't hide the url in view source mode and with interstitials.
[email protected]96d185d2009-04-24 03:28:54534 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]8ab24cc2009-11-13 20:56:15535 if (entry && (entry->IsViewSourceMode() ||
536 entry->page_type() == NavigationEntry::INTERSTITIAL_PAGE)) {
[email protected]96d185d2009-04-24 03:28:54537 return true;
[email protected]8ab24cc2009-11-13 20:56:15538 }
[email protected]83e3895b2009-06-11 00:07:16539
[email protected]96d185d2009-04-24 03:28:54540 DOMUI* dom_ui = GetDOMUIForCurrentState();
541 if (dom_ui)
542 return !dom_ui->should_hide_url();
543 return true;
544}
545
[email protected]d5f942ba2008-09-26 19:30:34546SkBitmap TabContents::GetFavIcon() const {
547 // Like GetTitle(), we also want to use the favicon for the last committed
548 // entry rather than a pending navigation entry.
[email protected]ce3fa3c2009-04-20 19:55:57549 NavigationEntry* entry = controller_.GetTransientEntry();
[email protected]cbab76d2008-10-13 22:42:47550 if (entry)
551 return entry->favicon().bitmap();
552
[email protected]ce3fa3c2009-04-20 19:55:57553 entry = controller_.GetLastCommittedEntry();
[email protected]d5f942ba2008-09-26 19:30:34554 if (entry)
555 return entry->favicon().bitmap();
[email protected]d5f942ba2008-09-26 19:30:34556 return SkBitmap();
557}
558
[email protected]0b303cc2009-09-28 22:35:15559bool TabContents::FavIconIsValid() const {
560 NavigationEntry* entry = controller_.GetTransientEntry();
561 if (entry)
562 return entry->favicon().is_valid();
563
564 entry = controller_.GetLastCommittedEntry();
565 if (entry)
566 return entry->favicon().is_valid();
567
568 return false;
569}
570
[email protected]96d185d2009-04-24 03:28:54571bool TabContents::ShouldDisplayFavIcon() {
572 // Always display a throbber during pending loads.
573 if (controller_.GetLastCommittedEntry() && controller_.pending_entry())
574 return true;
575
576 DOMUI* dom_ui = GetDOMUIForCurrentState();
577 if (dom_ui)
578 return !dom_ui->hide_favicon();
579 return true;
580}
581
[email protected]2d5a3592010-01-30 04:29:52582bool TabContents::IsContentBlocked(ContentSettingsType content_type) const {
[email protected]8cc8d492010-02-02 10:40:49583 if (content_type == CONTENT_SETTINGS_TYPE_POPUPS)
584 return blocked_popups_ != NULL;
585
[email protected]0de80162010-02-03 04:52:35586 if (content_type == CONTENT_SETTINGS_TYPE_IMAGES ||
587 content_type == CONTENT_SETTINGS_TYPE_JAVASCRIPT ||
[email protected]3dbb80b2010-02-09 22:41:20588 content_type == CONTENT_SETTINGS_TYPE_PLUGINS ||
589 content_type == CONTENT_SETTINGS_TYPE_COOKIES)
[email protected]0de80162010-02-03 04:52:35590 return content_blocked_[content_type];
591
[email protected]3dbb80b2010-02-09 22:41:20592 NOTREACHED();
[email protected]2d5a3592010-01-30 04:29:52593 return false;
594}
595
[email protected]96d185d2009-04-24 03:28:54596std::wstring TabContents::GetStatusText() const {
597 if (!is_loading() || load_state_ == net::LOAD_STATE_IDLE)
598 return std::wstring();
599
600 switch (load_state_) {
601 case net::LOAD_STATE_WAITING_FOR_CACHE:
602 return l10n_util::GetString(IDS_LOAD_STATE_WAITING_FOR_CACHE);
603 case net::LOAD_STATE_RESOLVING_PROXY_FOR_URL:
604 return l10n_util::GetString(IDS_LOAD_STATE_RESOLVING_PROXY_FOR_URL);
605 case net::LOAD_STATE_RESOLVING_HOST:
606 return l10n_util::GetString(IDS_LOAD_STATE_RESOLVING_HOST);
607 case net::LOAD_STATE_CONNECTING:
608 return l10n_util::GetString(IDS_LOAD_STATE_CONNECTING);
609 case net::LOAD_STATE_SENDING_REQUEST:
[email protected]094e5b22009-09-25 04:23:56610 if (upload_size_)
611 return l10n_util::GetStringF(
612 IDS_LOAD_STATE_SENDING_REQUEST_WITH_PROGRESS,
613 static_cast<int>((100 * upload_position_) / upload_size_));
614 else
615 return l10n_util::GetString(IDS_LOAD_STATE_SENDING_REQUEST);
[email protected]96d185d2009-04-24 03:28:54616 case net::LOAD_STATE_WAITING_FOR_RESPONSE:
617 return l10n_util::GetStringF(IDS_LOAD_STATE_WAITING_FOR_RESPONSE,
618 load_state_host_);
619 // Ignore net::LOAD_STATE_READING_RESPONSE and net::LOAD_STATE_IDLE
620 case net::LOAD_STATE_IDLE:
621 case net::LOAD_STATE_READING_RESPONSE:
622 break;
623 }
624
625 return std::wstring();
626}
627
[email protected]d5f942ba2008-09-26 19:30:34628void TabContents::SetIsCrashed(bool state) {
629 if (state == is_crashed_)
630 return;
631
632 is_crashed_ = state;
[email protected]c9cd2222009-05-06 05:16:50633 NotifyNavigationStateChanged(INVALIDATE_TAB);
[email protected]d5f942ba2008-09-26 19:30:34634}
635
[email protected]744ef172009-10-16 21:53:46636void TabContents::PageActionStateChanged() {
637 NotifyNavigationStateChanged(TabContents::INVALIDATE_PAGE_ACTIONS);
[email protected]d7eaf572009-07-01 21:57:00638}
[email protected]ce5c4502009-05-06 16:46:11639
[email protected]d5f942ba2008-09-26 19:30:34640void TabContents::NotifyNavigationStateChanged(unsigned changed_flags) {
641 if (delegate_)
642 delegate_->NavigationStateChanged(this, changed_flags);
643}
644
[email protected]96d185d2009-04-24 03:28:54645void TabContents::DidBecomeSelected() {
646 controller_.SetActive(true);
[email protected]8cb5d5b2010-02-09 11:36:16647 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
648 if (rwhv) {
649 rwhv->DidBecomeSelected();
[email protected]789e9152009-08-04 21:59:43650#if defined(OS_MACOSX)
[email protected]8cb5d5b2010-02-09 11:36:16651 rwhv->SetActive(true);
[email protected]789e9152009-08-04 21:59:43652#endif
653 }
[email protected]96d185d2009-04-24 03:28:54654
[email protected]8cb5d5b2010-02-09 11:36:16655 WebCacheManager::GetInstance()->ObserveActivity(GetRenderProcessHost()->id());
[email protected]96d185d2009-04-24 03:28:54656}
657
658void TabContents::WasHidden() {
659 if (!capturing_contents()) {
660 // |render_view_host()| can be NULL if the user middle clicks a link to open
661 // a tab in then background, then closes the tab before selecting it. This
662 // is because closing the tab calls TabContents::Destroy(), which removes
663 // the |render_view_host()|; then when we actually destroy the window,
664 // OnWindowPosChanged() notices and calls HideContents() (which calls us).
[email protected]8cb5d5b2010-02-09 11:36:16665 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
666 if (rwhv)
667 rwhv->WasHidden();
[email protected]96d185d2009-04-24 03:28:54668 }
669
670 NotificationService::current()->Notify(
671 NotificationType::TAB_CONTENTS_HIDDEN,
672 Source<TabContents>(this),
673 NotificationService::NoDetails());
674}
675
[email protected]d5f942ba2008-09-26 19:30:34676void TabContents::Activate() {
677 if (delegate_)
678 delegate_->ActivateContents(this);
679}
680
[email protected]96d185d2009-04-24 03:28:54681void TabContents::ShowContents() {
[email protected]8cb5d5b2010-02-09 11:36:16682 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
683 if (rwhv)
684 rwhv->DidBecomeSelected();
[email protected]96d185d2009-04-24 03:28:54685}
686
687void TabContents::HideContents() {
688 // TODO(pkasting): http://b/1239839 Right now we purposefully don't call
689 // our superclass HideContents(), because some callers want to be very picky
690 // about the order in which these get called. In addition to making the code
691 // here practically impossible to understand, this also means we end up
692 // calling TabContents::WasHidden() twice if callers call both versions of
[email protected]57c6a652009-05-04 07:58:34693 // HideContents() on a TabContents.
[email protected]96d185d2009-04-24 03:28:54694 WasHidden();
695}
696
[email protected]c0588052008-10-27 23:01:50697void TabContents::OpenURL(const GURL& url, const GURL& referrer,
[email protected]d5f942ba2008-09-26 19:30:34698 WindowOpenDisposition disposition,
699 PageTransition::Type transition) {
700 if (delegate_)
[email protected]c0588052008-10-27 23:01:50701 delegate_->OpenURLFromTab(this, url, referrer, disposition, transition);
[email protected]d5f942ba2008-09-26 19:30:34702}
703
704bool TabContents::NavigateToPendingEntry(bool reload) {
[email protected]96d185d2009-04-24 03:28:54705 const NavigationEntry& entry = *controller_.pending_entry();
706
707 RenderViewHost* dest_render_view_host = render_manager_.Navigate(entry);
708 if (!dest_render_view_host)
709 return false; // Unable to create the desired render view host.
710
711 // Tell DevTools agent that it is attached prior to the navigation.
[email protected]d9f9b792009-06-24 13:17:12712 DevToolsManager* devtools_manager = DevToolsManager::GetInstance();
[email protected]7e8e6b62009-05-08 11:28:32713 if (devtools_manager) { // NULL in unit tests.
[email protected]684b4ba22009-05-07 06:27:41714 devtools_manager->OnNavigatingToPendingEntry(
715 render_view_host(),
716 dest_render_view_host,
717 controller_.pending_entry()->url());
[email protected]7e8e6b62009-05-08 11:28:32718 }
[email protected]96d185d2009-04-24 03:28:54719
720 // Used for page load time metrics.
721 current_load_start_ = base::TimeTicks::Now();
722
723 // Navigate in the desired RenderViewHost.
[email protected]056de2d2009-06-26 16:41:34724 ViewMsg_Navigate_Params navigate_params;
[email protected]5e369672009-11-03 23:48:30725 MakeNavigateParams(profile(), entry, reload, &navigate_params);
[email protected]056de2d2009-06-26 16:41:34726 dest_render_view_host->Navigate(navigate_params);
[email protected]96d185d2009-04-24 03:28:54727
728 if (entry.page_id() == -1) {
729 // HACK!! This code suppresses javascript: URLs from being added to
730 // session history, which is what we want to do for javascript: URLs that
731 // do not generate content. What we really need is a message from the
732 // renderer telling us that a new page was not created. The same message
733 // could be used for mailto: URLs and the like.
734 if (entry.url().SchemeIs(chrome::kJavaScriptScheme))
735 return false;
736 }
737
738 // Clear any provisional password saves - this stops password infobars
739 // showing up on pages the user navigates to while the right page is
740 // loading.
741 GetPasswordManager()->ClearProvisionalSave();
742
743 if (reload && !profile()->IsOffTheRecord()) {
[email protected]0189bc722009-08-28 21:56:48744 FaviconService* favicon_service =
745 profile()->GetFaviconService(Profile::IMPLICIT_ACCESS);
746 if (favicon_service)
747 favicon_service->SetFaviconOutOfDateForPage(entry.url());
[email protected]96d185d2009-04-24 03:28:54748 }
749
[email protected]d5f942ba2008-09-26 19:30:34750 return true;
751}
752
[email protected]96d185d2009-04-24 03:28:54753void TabContents::Stop() {
754 render_manager_.Stop();
755 printing_.Stop();
756}
757
758void TabContents::Cut() {
759 render_view_host()->Cut();
760}
761
762void TabContents::Copy() {
763 render_view_host()->Copy();
764}
765
[email protected]c0cc3092009-09-12 08:27:27766#if defined(OS_MACOSX)
[email protected]a954bf72009-09-12 07:30:35767void TabContents::CopyToFindPboard() {
768 render_view_host()->CopyToFindPboard();
769}
[email protected]c0cc3092009-09-12 08:27:27770#endif
[email protected]a954bf72009-09-12 07:30:35771
[email protected]96d185d2009-04-24 03:28:54772void TabContents::Paste() {
773 render_view_host()->Paste();
774}
775
[email protected]7e786822009-11-07 00:00:27776#if defined(OS_MACOSX)
777void TabContents::PasteAndMatchStyle() {
778 render_view_host()->ForwardEditCommand("PasteAndMatchStyle", "");
779}
780#endif
781
[email protected]96d185d2009-04-24 03:28:54782void TabContents::DisassociateFromPopupCount() {
783 render_view_host()->DisassociateFromPopupCount();
784}
785
786TabContents* TabContents::Clone() {
787 // We create a new SiteInstance so that the new tab won't share processes
788 // with the old one. This can be changed in the future if we need it to share
789 // processes for some reason.
[email protected]420ae012009-04-24 05:16:32790 TabContents* tc = new TabContents(profile(),
[email protected]96d185d2009-04-24 03:28:54791 SiteInstance::CreateSiteInstance(profile()),
[email protected]12636df2009-09-28 22:32:21792 MSG_ROUTING_NONE, this);
[email protected]96d185d2009-04-24 03:28:54793 tc->controller().CopyStateFrom(controller_);
794 return tc;
795}
796
[email protected]4d677202009-07-19 07:37:12797void TabContents::ShowPageInfo(const GURL& url,
798 const NavigationEntry::SSLStatus& ssl,
799 bool show_history) {
800 if (!delegate_)
801 return;
802
[email protected]bb678332009-07-21 00:15:50803 delegate_->ShowPageInfo(profile(), url, ssl, show_history);
[email protected]4d677202009-07-19 07:37:12804}
805
[email protected]7e0831b2010-02-01 08:45:15806void TabContents::TranslatePage(const std::string& source_lang,
807 const std::string& target_lang) {
808 NavigationEntry* entry = controller_.GetActiveEntry();
809 if (!entry) {
810 NOTREACHED();
811 return;
812 }
813 render_view_host()->TranslatePage(entry->page_id(), source_lang, target_lang);
814}
815
initial.commit09911bf2008-07-26 23:55:29816ConstrainedWindow* TabContents::CreateConstrainedDialog(
[email protected]e8382172009-06-19 22:16:28817 ConstrainedWindowDelegate* delegate) {
initial.commit09911bf2008-07-26 23:55:29818 ConstrainedWindow* window =
[email protected]e8382172009-06-19 22:16:28819 ConstrainedWindow::CreateConstrainedDialog(this, delegate);
initial.commit09911bf2008-07-26 23:55:29820 child_windows_.push_back(window);
[email protected]fa1cf0b82010-01-15 21:49:44821
822 if (child_windows_.size() == 1) {
823 window->ShowConstrainedWindow();
824 BlockTabContent(true);
825 }
826
initial.commit09911bf2008-07-26 23:55:29827 return window;
828}
829
[email protected]fa1cf0b82010-01-15 21:49:44830void TabContents::BlockTabContent(bool blocked) {
831 render_view_host()->set_ignore_input_events(blocked);
832 if (delegate_)
833 delegate_->SetTabContentBlocked(this, blocked);
834}
835
initial.commit09911bf2008-07-26 23:55:29836void TabContents::AddNewContents(TabContents* new_contents,
837 WindowOpenDisposition disposition,
838 const gfx::Rect& initial_pos,
[email protected]7e7414ae2010-01-26 20:19:29839 bool user_gesture) {
initial.commit09911bf2008-07-26 23:55:29840 if (!delegate_)
841 return;
842
[email protected]35f7d212009-04-29 21:19:27843 if ((disposition == NEW_POPUP) && !user_gesture &&
844 !CommandLine::ForCurrentProcess()->HasSwitch(
845 switches::kDisablePopupBlocking)) {
[email protected]2c4410d2009-05-06 23:46:22846 // Unrequested popups from normal pages are constrained unless they're in
[email protected]b6c874582009-05-08 19:38:31847 // the whitelist. The popup owner will handle checking this.
[email protected]7e7414ae2010-01-26 20:19:29848 delegate_->GetConstrainingContents(this)->AddPopup(
849 new_contents, initial_pos);
[email protected]b6c874582009-05-08 19:38:31850 } else {
[email protected]304f8792009-08-19 21:28:09851#if defined(OS_CHROMEOS)
852 if (disposition == NEW_POPUP) {
853 // If the popup is bigger than a given factor of the screen, then
854 // turn it into a foreground tab (on chrome os only)
855 GdkScreen* screen = gdk_screen_get_default();
856 int max_width = gdk_screen_get_width(screen) * kMaxWidthFactor;
857 int max_height = gdk_screen_get_height(screen) * kMaxHeightFactor;
858 if (initial_pos.width() > max_width ||
859 initial_pos.height() > max_height) {
860 disposition = NEW_FOREGROUND_TAB;
861 }
862 }
863#endif
864
[email protected]0aa55312008-10-17 21:53:08865 new_contents->DisassociateFromPopupCount();
initial.commit09911bf2008-07-26 23:55:29866 delegate_->AddNewContents(this, new_contents, disposition, initial_pos,
867 user_gesture);
[email protected]7f37dd92009-09-15 16:35:49868 NotificationService::current()->Notify(
869 NotificationType::TAB_ADDED,
870 Source<TabContentsDelegate>(delegate_),
871 Details<TabContents>(this));
initial.commit09911bf2008-07-26 23:55:29872 }
initial.commit09911bf2008-07-26 23:55:29873
[email protected]8cc8d492010-02-02 10:40:49874 // TODO(pkasting): Why is this necessary?
875 PopupNotificationVisibilityChanged(blocked_popups_ != NULL);
initial.commit09911bf2008-07-26 23:55:29876}
877
[email protected]bcd9dc42010-01-23 16:38:18878bool TabContents::ExecuteCode(int request_id, const std::string& extension_id,
[email protected]61f5a7b2009-12-22 22:21:20879 const std::vector<URLPattern>& host_permissions,
[email protected]20ad2692009-11-20 18:27:20880 bool is_js_code, const std::string& code_string,
881 bool all_frames) {
[email protected]bcd9dc42010-01-23 16:38:18882 return render_view_host()->Send(
[email protected]61f5a7b2009-12-22 22:21:20883 new ViewMsg_ExecuteCode(render_view_host()->routing_id(),
884 ViewMsg_ExecuteCode_Params(request_id, extension_id, host_permissions,
885 is_js_code, code_string, all_frames)));
[email protected]912256b32009-09-18 09:47:35886}
887
[email protected]96d185d2009-04-24 03:28:54888void TabContents::PopupNotificationVisibilityChanged(bool visible) {
[email protected]8cc8d492010-02-02 10:40:49889 if (is_being_destroyed_)
890 return;
891 if (!dont_notify_render_view_)
892 render_view_host()->PopupNotificationVisibilityChanged(visible);
[email protected]0de80162010-02-03 04:52:35893 if (delegate_)
894 delegate_->OnBlockedContentChange(this);
[email protected]96d185d2009-04-24 03:28:54895}
896
[email protected]5c9e97a2009-09-09 23:48:30897gfx::NativeView TabContents::GetContentNativeView() const {
[email protected]96d185d2009-04-24 03:28:54898 return view_->GetContentNativeView();
899}
900
901gfx::NativeView TabContents::GetNativeView() const {
902 return view_->GetNativeView();
903}
904
905void TabContents::GetContainerBounds(gfx::Rect *out) const {
906 view_->GetContainerBounds(out);
907}
908
909void TabContents::Focus() {
910 view_->Focus();
911}
912
[email protected]90daadb42009-06-08 21:27:28913void TabContents::FocusThroughTabTraversal(bool reverse) {
[email protected]7e383692009-06-12 19:14:54914 if (showing_interstitial_page()) {
915 render_manager_.interstitial_page()->FocusThroughTabTraversal(reverse);
916 return;
917 }
[email protected]96d185d2009-04-24 03:28:54918 render_view_host()->SetInitialFocus(reverse);
919}
920
921bool TabContents::FocusLocationBarByDefault() {
922 DOMUI* dom_ui = GetDOMUIForCurrentState();
923 if (dom_ui)
924 return dom_ui->focus_location_bar_by_default();
[email protected]3e3f0eb2009-06-22 18:33:43925 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]e0d481582009-09-15 21:06:25926 if (entry && entry->url() == GURL(chrome::kAboutBlankURL))
[email protected]3e3f0eb2009-06-22 18:33:43927 return true;
[email protected]96d185d2009-04-24 03:28:54928 return false;
929}
930
[email protected]616ed5a2008-11-21 22:27:24931void TabContents::AddInfoBar(InfoBarDelegate* delegate) {
932 // Look through the existing InfoBarDelegates we have for a match. If we've
933 // already got one that matches, then we don't add the new one.
[email protected]f86a07022008-11-25 01:06:05934 for (int i = 0; i < infobar_delegate_count(); ++i) {
[email protected]6d27a7b2009-12-18 23:25:45935 if (GetInfoBarDelegateAt(i)->EqualsDelegate(delegate)) {
936 // Tell the new infobar to close so that it can clean itself up.
937 delegate->InfoBarClosed();
[email protected]616ed5a2008-11-21 22:27:24938 return;
[email protected]6d27a7b2009-12-18 23:25:45939 }
[email protected]616ed5a2008-11-21 22:27:24940 }
941
942 infobar_delegates_.push_back(delegate);
[email protected]bfd04a62009-02-01 18:16:56943 NotificationService::current()->Notify(
944 NotificationType::TAB_CONTENTS_INFOBAR_ADDED,
945 Source<TabContents>(this),
946 Details<InfoBarDelegate>(delegate));
[email protected]616ed5a2008-11-21 22:27:24947
948 // Add ourselves as an observer for navigations the first time a delegate is
949 // added. We use this notification to expire InfoBars that need to expire on
950 // page transitions.
951 if (infobar_delegates_.size() == 1) {
[email protected]bfd04a62009-02-01 18:16:56952 registrar_.Add(this, NotificationType::NAV_ENTRY_COMMITTED,
[email protected]ce3fa3c2009-04-20 19:55:57953 Source<NavigationController>(&controller_));
[email protected]616ed5a2008-11-21 22:27:24954 }
955}
956
957void TabContents::RemoveInfoBar(InfoBarDelegate* delegate) {
958 std::vector<InfoBarDelegate*>::iterator it =
959 find(infobar_delegates_.begin(), infobar_delegates_.end(), delegate);
960 if (it != infobar_delegates_.end()) {
961 InfoBarDelegate* delegate = *it;
[email protected]bfd04a62009-02-01 18:16:56962 NotificationService::current()->Notify(
963 NotificationType::TAB_CONTENTS_INFOBAR_REMOVED,
964 Source<TabContents>(this),
965 Details<InfoBarDelegate>(delegate));
[email protected]f86a07022008-11-25 01:06:05966 infobar_delegates_.erase(it);
[email protected]616ed5a2008-11-21 22:27:24967
[email protected]6a02963e2009-01-06 16:58:03968 // Remove ourselves as an observer if we are tracking no more InfoBars.
969 if (infobar_delegates_.empty()) {
[email protected]bfd04a62009-02-01 18:16:56970 registrar_.Remove(this, NotificationType::NAV_ENTRY_COMMITTED,
[email protected]ce3fa3c2009-04-20 19:55:57971 Source<NavigationController>(&controller_));
[email protected]6a02963e2009-01-06 16:58:03972 }
[email protected]616ed5a2008-11-21 22:27:24973 }
974}
[email protected]3edd9522009-03-04 22:19:41975
[email protected]f81821c2009-08-07 01:29:47976void TabContents::ReplaceInfoBar(InfoBarDelegate* old_delegate,
977 InfoBarDelegate* new_delegate) {
978 std::vector<InfoBarDelegate*>::iterator it =
979 find(infobar_delegates_.begin(), infobar_delegates_.end(), old_delegate);
980 DCHECK(it != infobar_delegates_.end());
981
982 // Notify the container about the change of plans.
983 scoped_ptr<std::pair<InfoBarDelegate*, InfoBarDelegate*> > details(
984 new std::pair<InfoBarDelegate*, InfoBarDelegate*>(
985 old_delegate, new_delegate));
986 NotificationService::current()->Notify(
987 NotificationType::TAB_CONTENTS_INFOBAR_REPLACED,
988 Source<TabContents>(this),
989 Details<std::pair<InfoBarDelegate*, InfoBarDelegate*> >(details.get()));
990
991 // Remove the old one.
992 infobar_delegates_.erase(it);
993
994 // Add the new one.
995 infobar_delegates_.push_back(new_delegate);
996}
997
[email protected]20ede072009-10-06 00:28:35998bool TabContents::ShouldShowBookmarkBar() {
[email protected]aa9196f2009-10-22 16:20:27999 if (showing_interstitial_page())
1000 return false;
1001
[email protected]96d185d2009-04-24 03:28:541002 // See GetDOMUIForCurrentState() comment for more info. This case is very
1003 // similar, but for non-first loads, we want to use the committed entry. This
1004 // is so the bookmarks bar disappears at the same time the page does.
1005 if (controller_.GetLastCommittedEntry()) {
1006 // Not the first load, always use the committed DOM UI.
1007 if (render_manager_.dom_ui())
1008 return render_manager_.dom_ui()->force_bookmark_bar_visible();
1009 return false; // Default.
1010 }
1011
1012 // When it's the first load, we know either the pending one or the committed
1013 // one will have the DOM UI in it (see GetDOMUIForCurrentState), and only one
1014 // of them will be valid, so we can just check both.
1015 if (render_manager_.pending_dom_ui())
1016 return render_manager_.pending_dom_ui()->force_bookmark_bar_visible();
1017 if (render_manager_.dom_ui())
1018 return render_manager_.dom_ui()->force_bookmark_bar_visible();
1019 return false; // Default.
1020}
1021
[email protected]d8f16ae2009-08-30 03:47:261022bool TabContents::IsExtensionShelfAlwaysVisible() {
1023 // See GetDOMUIForCurrentState() comment for more info. This case is very
1024 // similar, but for non-first loads, we want to use the committed entry. This
1025 // is so the bookmarks bar disappears at the same time the page does.
1026 if (controller_.GetLastCommittedEntry()) {
1027 // Not the first load, always use the committed DOM UI.
1028 if (render_manager_.dom_ui())
1029 return render_manager_.dom_ui()->force_extension_shelf_visible();
1030 return false; // Default.
1031 }
1032
1033 // When it's the first load, we know either the pending one or the committed
1034 // one will have the DOM UI in it (see GetDOMUIForCurrentState), and only one
1035 // of them will be valid, so we can just check both.
1036 if (render_manager_.pending_dom_ui())
1037 return render_manager_.pending_dom_ui()->force_extension_shelf_visible();
1038 if (render_manager_.dom_ui())
1039 return render_manager_.dom_ui()->force_extension_shelf_visible();
1040 return false; // Default.
1041}
1042
[email protected]3edd9522009-03-04 22:19:411043void TabContents::ToolbarSizeChanged(bool is_animating) {
1044 TabContentsDelegate* d = delegate();
1045 if (d)
1046 d->ToolbarSizeChanged(this, is_animating);
1047}
[email protected]616ed5a2008-11-21 22:27:241048
[email protected]fc6fb7fb2009-11-07 02:35:041049bool TabContents::CanDownload(int request_id) {
1050 TabContentsDelegate* d = delegate();
1051 if (d)
1052 return d->CanDownload(request_id);
1053 return true;
1054}
1055
[email protected]d5f942ba2008-09-26 19:30:341056void TabContents::OnStartDownload(DownloadItem* download) {
1057 DCHECK(download);
[email protected]d5f942ba2008-09-26 19:30:341058
1059 // Download in a constrained popup is shown in the tab that opened it.
[email protected]b6c874582009-05-08 19:38:311060 TabContents* tab_contents = delegate()->GetConstrainingContents(this);
[email protected]d5f942ba2008-09-26 19:30:341061
[email protected]59560e0b2009-06-04 03:30:221062 if (tab_contents && tab_contents->delegate())
1063 tab_contents->delegate()->OnStartDownload(download);
[email protected]3edd9522009-03-04 22:19:411064}
[email protected]3edd9522009-03-04 22:19:411065
[email protected]d5f942ba2008-09-26 19:30:341066void TabContents::WillClose(ConstrainedWindow* window) {
1067 ConstrainedWindowList::iterator it =
1068 find(child_windows_.begin(), child_windows_.end(), window);
[email protected]fa1cf0b82010-01-15 21:49:441069 bool removed_topmost_window = it == child_windows_.begin();
[email protected]d5f942ba2008-09-26 19:30:341070 if (it != child_windows_.end())
1071 child_windows_.erase(it);
[email protected]fa1cf0b82010-01-15 21:49:441072 if (removed_topmost_window && child_windows_.size() > 0) {
1073 child_windows_[0]->ShowConstrainedWindow();
1074 BlockTabContent(true);
1075 } else {
1076 BlockTabContent(false);
1077 }
[email protected]66ba4932009-06-04 19:22:131078}
[email protected]d5f942ba2008-09-26 19:30:341079
[email protected]66ba4932009-06-04 19:22:131080void TabContents::WillCloseBlockedPopupContainer(
1081 BlockedPopupContainer* container) {
1082 DCHECK(blocked_popups_ == container);
1083 blocked_popups_ = NULL;
[email protected]8cc8d492010-02-02 10:40:491084 PopupNotificationVisibilityChanged(false);
[email protected]d5f942ba2008-09-26 19:30:341085}
1086
[email protected]d5f942ba2008-09-26 19:30:341087void TabContents::DidMoveOrResize(ConstrainedWindow* window) {
[email protected]332af7732009-03-11 13:21:351088#if defined(OS_WIN)
[email protected]92edc472009-02-10 20:32:061089 UpdateWindow(GetNativeView());
[email protected]332af7732009-03-11 13:21:351090#endif
[email protected]d5f942ba2008-09-26 19:30:341091}
1092
[email protected]c1dc9f132009-11-20 23:13:211093void TabContents::StartFinding(string16 search_string,
[email protected]e491f1c2009-05-22 20:28:121094 bool forward_direction,
1095 bool case_sensitive) {
[email protected]c1dc9f132009-11-20 23:13:211096 // If search_string is empty, it means FindNext was pressed with a keyboard
[email protected]6e95706d2009-04-23 22:55:011097 // shortcut so unless we have something to search for we return early.
[email protected]c1dc9f132009-11-20 23:13:211098 if (search_string.empty() && find_text_.empty()) {
[email protected]87fae512009-12-11 02:20:211099 // Try the last thing we searched for on this tab, then the last thing
1100 // searched for on any tab.
1101 if (!previous_find_text_.empty())
1102 search_string = previous_find_text_;
1103 else if (!last_search_prepopulate_text_->empty())
1104 search_string = *last_search_prepopulate_text_;
1105 else
[email protected]e491f1c2009-05-22 20:28:121106 return;
[email protected]e491f1c2009-05-22 20:28:121107 }
[email protected]6e95706d2009-04-23 22:55:011108
[email protected]101c90e2009-10-26 21:14:441109 // Keep track of the previous search.
1110 previous_find_text_ = find_text_;
1111
[email protected]6e95706d2009-04-23 22:55:011112 // This is a FindNext operation if we are searching for the same text again,
1113 // or if the passed in search text is empty (FindNext keyboard shortcut). The
1114 // exception to this is if the Find was aborted (then we don't want FindNext
1115 // because the highlighting has been cleared and we need it to reappear). We
1116 // therefore treat FindNext after an aborted Find operation as a full fledged
1117 // Find.
[email protected]c1dc9f132009-11-20 23:13:211118 bool find_next = (find_text_ == search_string || search_string.empty()) &&
[email protected]e491f1c2009-05-22 20:28:121119 (last_search_case_sensitive_ == case_sensitive) &&
[email protected]6e95706d2009-04-23 22:55:011120 !find_op_aborted_;
1121 if (!find_next)
1122 current_find_request_id_ = find_request_id_counter_++;
1123
[email protected]c1dc9f132009-11-20 23:13:211124 if (!search_string.empty())
1125 find_text_ = search_string;
[email protected]e491f1c2009-05-22 20:28:121126 last_search_case_sensitive_ = case_sensitive;
[email protected]6e95706d2009-04-23 22:55:011127
1128 find_op_aborted_ = false;
1129
1130 // Keep track of what the last search was across the tabs.
[email protected]c1dc9f132009-11-20 23:13:211131 *last_search_prepopulate_text_ = find_text_;
[email protected]6e95706d2009-04-23 22:55:011132 render_view_host()->StartFinding(current_find_request_id_,
1133 find_text_,
1134 forward_direction,
[email protected]e491f1c2009-05-22 20:28:121135 case_sensitive,
[email protected]6e95706d2009-04-23 22:55:011136 find_next);
1137}
1138
1139void TabContents::StopFinding(bool clear_selection) {
[email protected]2d2dffc2009-06-09 21:41:001140 // When |clear_selection| is true, it means the find string has been cleared
1141 // by the user, but the UI has not been dismissed.
1142 if (!clear_selection)
1143 find_ui_active_ = false;
[email protected]87fae512009-12-11 02:20:211144 previous_find_text_ = find_text_;
[email protected]101c90e2009-10-26 21:14:441145 find_text_.clear();
[email protected]6e95706d2009-04-23 22:55:011146 find_op_aborted_ = true;
[email protected]e491f1c2009-05-22 20:28:121147 last_search_result_ = FindNotificationDetails();
[email protected]6e95706d2009-04-23 22:55:011148 render_view_host()->StopFinding(clear_selection);
1149}
1150
[email protected]420ae012009-04-24 05:16:321151void TabContents::OnSavePage() {
1152 // If we can not save the page, try to download it.
1153 if (!SavePackage::IsSavableContents(contents_mime_type())) {
1154 DownloadManager* dlm = profile()->GetDownloadManager();
1155 const GURL& current_page_url = GetURL();
1156 if (dlm && current_page_url.is_valid())
[email protected]d82ed61e2009-06-16 02:46:221157 dlm->DownloadUrl(current_page_url, GURL(), "", this);
[email protected]420ae012009-04-24 05:16:321158 return;
1159 }
1160
1161 Stop();
1162
1163 // Create the save package and possibly prompt the user for the name to save
1164 // the page as. The user prompt is an asynchronous operation that runs on
1165 // another thread.
[email protected]d82ed61e2009-06-16 02:46:221166 save_package_ = new SavePackage(this);
[email protected]420ae012009-04-24 05:16:321167 save_package_->GetSaveInfo();
1168}
1169
1170// Used in automated testing to bypass prompting the user for file names.
1171// Instead, the names and paths are hard coded rather than running them through
1172// file name sanitation and extension / mime checking.
[email protected]828cabe2009-09-26 22:47:111173bool TabContents::SavePage(const FilePath& main_file, const FilePath& dir_path,
[email protected]420ae012009-04-24 05:16:321174 SavePackage::SavePackageType save_type) {
1175 // Stop the page from navigating.
1176 Stop();
1177
[email protected]828cabe2009-09-26 22:47:111178 save_package_ = new SavePackage(this, save_type, main_file, dir_path);
[email protected]cdf6cc82009-08-04 21:48:591179 return save_package_->Init();
[email protected]420ae012009-04-24 05:16:321180}
1181
[email protected]89d6e6e2009-12-03 23:06:511182void TabContents::EmailPageLocation() {
1183 std::string title = EscapeQueryParamValue(UTF16ToUTF8(GetTitle()), false);
1184 std::string page_url = EscapeQueryParamValue(GetURL().spec(), false);
1185 std::string mailto = std::string("mailto:?subject=Fwd:%20") +
1186 title + "&body=%0A%0A" + page_url;
1187 platform_util::OpenExternal(GURL(mailto));
1188}
1189
[email protected]420ae012009-04-24 05:16:321190void TabContents::PrintPreview() {
1191 // We don't show the print preview yet, only the print dialog.
1192 PrintNow();
1193}
1194
1195bool TabContents::PrintNow() {
1196 // We can't print interstitial page for now.
1197 if (showing_interstitial_page())
1198 return false;
1199
1200 return render_view_host()->PrintPages();
1201}
1202
[email protected]82270452009-06-19 15:58:011203void TabContents::PrintingDone(int document_cookie, bool success) {
1204 render_view_host()->PrintingDone(document_cookie, success);
1205}
1206
[email protected]420ae012009-04-24 05:16:321207bool TabContents::IsActiveEntry(int32 page_id) {
1208 NavigationEntry* active_entry = controller_.GetActiveEntry();
1209 return (active_entry != NULL &&
1210 active_entry->site_instance() == GetSiteInstance() &&
1211 active_entry->page_id() == page_id);
1212}
1213
[email protected]b2fe07d12010-02-09 14:38:081214void TabContents::SetOverrideEncoding(const std::string& encoding) {
[email protected]8cb5d5b2010-02-09 11:36:161215 set_encoding(encoding);
1216 render_view_host()->SetPageEncoding(encoding);
1217}
1218
[email protected]b2fe07d12010-02-09 14:38:081219void TabContents::ResetOverrideEncoding() {
[email protected]8cb5d5b2010-02-09 11:36:161220 reset_encoding();
1221 render_view_host()->ResetPageEncodingToDefault();
1222}
1223
1224void TabContents::WindowMoveOrResizeStarted() {
1225 render_view_host()->WindowMoveOrResizeStarted();
1226}
1227
[email protected]3f8eb7f2009-10-02 23:58:051228void TabContents::LogNewTabTime(const std::string& event_name) {
1229 // Not all new tab pages get timed. In those cases, we don't have a
1230 // new_tab_start_time_.
1231 if (new_tab_start_time_.is_null())
1232 return;
1233
1234 base::TimeDelta duration = base::TimeTicks::Now() - new_tab_start_time_;
[email protected]84abba62009-10-07 17:01:441235 MetricEventDurationDetails details(event_name,
1236 static_cast<int>(duration.InMilliseconds()));
1237
[email protected]7ab1e7d62009-10-14 23:32:011238 if (event_name == "Tab.NewTabScriptStart") {
1239 UMA_HISTOGRAM_TIMES("Tab.NewTabScriptStart", duration);
1240 } else if (event_name == "Tab.NewTabDOMContentLoaded") {
1241 UMA_HISTOGRAM_TIMES("Tab.NewTabDOMContentLoaded", duration);
1242 } else if (event_name == "Tab.NewTabOnload") {
1243 UMA_HISTOGRAM_TIMES("Tab.NewTabOnload", duration);
[email protected]3f8eb7f2009-10-02 23:58:051244 // The new tab page has finished loading; reset it.
1245 new_tab_start_time_ = base::TimeTicks();
[email protected]84abba62009-10-07 17:01:441246 } else {
1247 NOTREACHED();
[email protected]3f8eb7f2009-10-02 23:58:051248 }
[email protected]84abba62009-10-07 17:01:441249 NotificationService::current()->Notify(
1250 NotificationType::METRIC_EVENT_DURATION,
1251 Source<TabContents>(this),
1252 Details<MetricEventDurationDetails>(&details));
[email protected]3f8eb7f2009-10-02 23:58:051253}
1254
[email protected]7ab1e7d62009-10-14 23:32:011255void TabContents::OnCloseStarted() {
1256 if (tab_close_start_time_.is_null())
1257 tab_close_start_time_ = base::TimeTicks::Now();
1258}
1259
[email protected]c37e3b62010-01-22 00:15:171260TabContents* TabContents::CloneAndMakePhantom() {
[email protected]c37e3b62010-01-22 00:15:171261 TabNavigation tab_nav;
[email protected]df72ac72010-02-06 00:43:161262
1263 NavigationEntry* entry = controller().GetActiveEntry();
1264 if (app_extension_)
1265 tab_nav.set_url(app_extension_->app_launch_url());
1266 else if (entry)
1267 tab_nav.SetFromNavigationEntry(*entry);
1268
[email protected]c37e3b62010-01-22 00:15:171269 std::vector<TabNavigation> navigations;
1270 navigations.push_back(tab_nav);
1271
1272 TabContents* new_contents =
1273 new TabContents(profile(), NULL, MSG_ROUTING_NONE, NULL);
[email protected]3b355642010-02-05 16:01:491274 new_contents->SetAppExtension(app_extension_);
[email protected]c37e3b62010-01-22 00:15:171275 new_contents->controller().RestoreFromState(navigations, 0, false);
[email protected]c37e3b62010-01-22 00:15:171276
[email protected]df72ac72010-02-06 00:43:161277 if (!app_extension_ && entry)
1278 new_contents->controller().GetActiveEntry()->favicon() = entry->favicon();
1279
[email protected]c37e3b62010-01-22 00:15:171280 return new_contents;
1281}
1282
[email protected]0de80162010-02-03 04:52:351283// Resets the |content_blocked_| array.
1284void TabContents::ClearBlockedContentSettings() {
1285 DCHECK_EQ(static_cast<size_t>(CONTENT_SETTINGS_NUM_TYPES),
1286 arraysize(content_blocked_));
1287 for (int i = 0; i < CONTENT_SETTINGS_NUM_TYPES; ++i)
1288 content_blocked_[i] = false;
1289}
1290
[email protected]96d185d2009-04-24 03:28:541291// Notifies the RenderWidgetHost instance about the fact that the page is
1292// loading, or done loading and calls the base implementation.
1293void TabContents::SetIsLoading(bool is_loading,
1294 LoadNotificationDetails* details) {
1295 if (is_loading == is_loading_)
1296 return;
1297
1298 if (!is_loading) {
1299 load_state_ = net::LOAD_STATE_IDLE;
1300 load_state_host_.clear();
[email protected]094e5b22009-09-25 04:23:561301 upload_size_ = 0;
1302 upload_position_ = 0;
[email protected]96d185d2009-04-24 03:28:541303 }
1304
1305 render_manager_.SetIsLoading(is_loading);
1306
1307 is_loading_ = is_loading;
1308 waiting_for_response_ = is_loading;
1309
1310 if (delegate_)
1311 delegate_->LoadingStateChanged(this);
[email protected]329581b2009-04-28 06:52:351312 NotifyNavigationStateChanged(INVALIDATE_LOAD);
[email protected]96d185d2009-04-24 03:28:541313
1314 NotificationType type = is_loading ? NotificationType::LOAD_START :
1315 NotificationType::LOAD_STOP;
[email protected]ce5c4502009-05-06 16:46:111316 NotificationDetails det = NotificationService::NoDetails();
[email protected]96d185d2009-04-24 03:28:541317 if (details)
1318 det = Details<LoadNotificationDetails>(details);
1319 NotificationService::current()->Notify(type,
1320 Source<NavigationController>(&controller_),
1321 det);
1322}
1323
[email protected]b6c874582009-05-08 19:38:311324void TabContents::AddPopup(TabContents* new_contents,
[email protected]7e7414ae2010-01-26 20:19:291325 const gfx::Rect& initial_pos) {
[email protected]8cc8d492010-02-02 10:40:491326 GURL url(GetURL());
1327 if (url.is_valid() &&
1328 profile()->GetHostContentSettingsMap()->GetContentSetting(
[email protected]da0c8e92010-02-09 23:25:491329 url, CONTENT_SETTINGS_TYPE_POPUPS) == CONTENT_SETTING_ALLOW) {
[email protected]8cc8d492010-02-02 10:40:491330 AddNewContents(new_contents, NEW_POPUP, initial_pos, true);
1331 } else {
1332 if (!blocked_popups_)
1333 blocked_popups_ = new BlockedPopupContainer(this);
1334 blocked_popups_->AddTabContents(new_contents, initial_pos);
1335 }
[email protected]b9681312008-11-07 00:08:261336}
[email protected]616ed5a2008-11-21 22:27:241337
1338namespace {
1339bool TransitionIsReload(PageTransition::Type transition) {
1340 return PageTransition::StripQualifier(transition) == PageTransition::RELOAD;
1341}
1342}
1343
1344void TabContents::ExpireInfoBars(
1345 const NavigationController::LoadCommittedDetails& details) {
1346 // Only hide InfoBars when the user has done something that makes the main
1347 // frame load. We don't want various automatic or subframe navigations making
1348 // it disappear.
1349 if (!details.is_user_initiated_main_frame_load())
1350 return;
1351
[email protected]f86a07022008-11-25 01:06:051352 for (int i = infobar_delegate_count() - 1; i >= 0; --i) {
[email protected]616ed5a2008-11-21 22:27:241353 InfoBarDelegate* delegate = GetInfoBarDelegateAt(i);
[email protected]f86a07022008-11-25 01:06:051354 if (delegate->ShouldExpire(details))
[email protected]616ed5a2008-11-21 22:27:241355 RemoveInfoBar(delegate);
[email protected]616ed5a2008-11-21 22:27:241356 }
1357}
[email protected]fdd61c62009-04-22 19:22:571358
[email protected]96d185d2009-04-24 03:28:541359DOMUI* TabContents::GetDOMUIForCurrentState() {
1360 // When there is a pending navigation entry, we want to use the pending DOMUI
1361 // that goes along with it to control the basic flags. For example, we want to
1362 // show the pending URL in the URL bar, so we want the display_url flag to
1363 // be from the pending entry.
1364 //
1365 // The confusion comes because there are multiple possibilities for the
1366 // initial load in a tab as a side effect of the way the RenderViewHostManager
1367 // works.
1368 //
1369 // - For the very first tab the load looks "normal". The new tab DOM UI is
1370 // the pending one, and we want it to apply here.
1371 //
1372 // - For subsequent new tabs, they'll get a new SiteInstance which will then
1373 // get switched to the one previously associated with the new tab pages.
1374 // This switching will cause the manager to commit the RVH/DOMUI. So we'll
1375 // have a committed DOM UI in this case.
1376 //
1377 // This condition handles all of these cases:
1378 //
1379 // - First load in first tab: no committed nav entry + pending nav entry +
1380 // pending dom ui:
1381 // -> Use pending DOM UI if any.
1382 //
1383 // - First load in second tab: no committed nav entry + pending nav entry +
1384 // no pending DOM UI:
1385 // -> Use the committed DOM UI if any.
1386 //
1387 // - Second navigation in any tab: committed nav entry + pending nav entry:
1388 // -> Use pending DOM UI if any.
1389 //
1390 // - Normal state with no load: committed nav entry + no pending nav entry:
1391 // -> Use committed DOM UI.
1392 if (controller_.pending_entry() &&
1393 (controller_.GetLastCommittedEntry() ||
1394 render_manager_.pending_dom_ui()))
1395 return render_manager_.pending_dom_ui();
1396 return render_manager_.dom_ui();
1397}
[email protected]420ae012009-04-24 05:16:321398
1399void TabContents::DidNavigateMainFramePostCommit(
1400 const NavigationController::LoadCommittedDetails& details,
1401 const ViewHostMsg_FrameNavigate_Params& params) {
[email protected]c2e74fe82009-09-03 17:57:441402 if (opener_dom_ui_type_ != DOMUIFactory::kNoDOMUI) {
1403 // If this is a window.open navigation, use the same DOMUI as the renderer
1404 // that opened the window, as long as both renderers have the same
1405 // privileges.
1406 if (opener_dom_ui_type_ == DOMUIFactory::GetDOMUIType(GetURL())) {
1407 DOMUI* dom_ui = DOMUIFactory::CreateDOMUIForURL(this, GetURL());
1408 // dom_ui might be NULL if the URL refers to a non-existent extension.
1409 if (dom_ui) {
1410 render_manager_.SetDOMUIPostCommit(dom_ui);
1411 dom_ui->RenderViewCreated(render_view_host());
1412 }
1413 }
1414 opener_dom_ui_type_ = DOMUIFactory::kNoDOMUI;
1415 }
1416
[email protected]420ae012009-04-24 05:16:321417 if (details.is_user_initiated_main_frame_load()) {
1418 // Clear the status bubble. This is a workaround for a bug where WebKit
1419 // doesn't let us know that the cursor left an element during a
1420 // transition (this is also why the mouse cursor remains as a hand after
1421 // clicking on a link); see bugs 1184641 and 980803. We don't want to
1422 // clear the bubble when a user navigates to a named anchor in the same
1423 // page.
1424 UpdateTargetURL(details.entry->page_id(), GURL());
1425
1426 // UpdateHelpersForDidNavigate will handle the case where the password_form
1427 // origin is valid.
1428 // TODO(brettw) bug 1343111: Password manager stuff in here needs to be
1429 // cleaned up and covered by tests.
1430 if (!params.password_form.origin.is_valid())
1431 GetPasswordManager()->DidNavigate();
1432 }
1433
1434 // The keyword generator uses the navigation entries, so must be called after
1435 // the commit.
1436 GenerateKeywordIfNecessary(params);
1437
1438 // Allow the new page to set the title again.
1439 received_page_title_ = false;
1440
1441 // Get the favicon, either from history or request it from the net.
1442 fav_icon_helper_.FetchFavIcon(details.entry->url());
1443
[email protected]0de80162010-02-03 04:52:351444 // Clear all page actions, blocked content notifications and browser actions
1445 // for this tab, unless this is an in-page navigation.
[email protected]92c6f9b92009-10-24 04:35:081446 url_canon::Replacements<char> replacements;
1447 replacements.ClearRef();
1448 if (params.url.ReplaceComponents(replacements) !=
1449 params.referrer.ReplaceComponents(replacements)) {
[email protected]92c6f9b92009-10-24 04:35:081450 ExtensionsService* service = profile()->GetExtensionsService();
1451 if (service) {
1452 for (size_t i = 0; i < service->extensions()->size(); ++i) {
[email protected]5d246db22009-10-27 06:17:571453 ExtensionAction* browser_action =
[email protected]92c6f9b92009-10-24 04:35:081454 service->extensions()->at(i)->browser_action();
[email protected]56ce6e52009-10-27 00:10:521455 if (browser_action) {
1456 browser_action->ClearAllValuesForTab(controller().session_id().id());
1457 NotificationService::current()->Notify(
1458 NotificationType::EXTENSION_BROWSER_ACTION_UPDATED,
[email protected]5d246db22009-10-27 06:17:571459 Source<ExtensionAction>(browser_action),
[email protected]56ce6e52009-10-27 00:10:521460 NotificationService::NoDetails());
1461 }
[email protected]92c6f9b92009-10-24 04:35:081462
[email protected]5d246db22009-10-27 06:17:571463 ExtensionAction* page_action =
[email protected]56ce6e52009-10-27 00:10:521464 service->extensions()->at(i)->page_action();
1465 if (page_action) {
1466 page_action->ClearAllValuesForTab(controller().session_id().id());
1467 PageActionStateChanged();
1468 }
[email protected]92c6f9b92009-10-24 04:35:081469 }
[email protected]aece2c7f2009-08-27 20:43:171470 }
[email protected]0de80162010-02-03 04:52:351471
1472 // Close blocked popups.
1473 if (blocked_popups_) {
1474 AutoReset auto_reset(&dont_notify_render_view_, true);
1475 blocked_popups_->Destroy();
1476 }
1477
1478 // Clear "blocked" flags.
1479 ClearBlockedContentSettings();
1480 if (delegate_)
1481 delegate_->OnBlockedContentChange(this);
[email protected]aece2c7f2009-08-27 20:43:171482 }
[email protected]ce5c4502009-05-06 16:46:111483
[email protected]8cc8d492010-02-02 10:40:491484 // Close constrained windows if necessary.
1485 if (!net::RegistryControlledDomainService::SameDomainOrHost(
1486 details.previous_url, details.entry->url()))
1487 CloseConstrainedWindows();
1488
[email protected]420ae012009-04-24 05:16:321489 // Update the starred state.
1490 UpdateStarredStateForCurrentURL();
1491}
1492
1493void TabContents::DidNavigateAnyFramePostCommit(
1494 RenderViewHost* render_view_host,
1495 const NavigationController::LoadCommittedDetails& details,
1496 const ViewHostMsg_FrameNavigate_Params& params) {
1497 // If we navigate, start showing messages again. This does nothing to prevent
1498 // a malicious script from spamming messages, since the script could just
1499 // reload the page to stop blocking.
1500 suppress_javascript_messages_ = false;
1501
[email protected]420ae012009-04-24 05:16:321502 // Notify the password manager of the navigation or form submit.
1503 // TODO(brettw) bug 1343111: Password manager stuff in here needs to be
1504 // cleaned up and covered by tests.
1505 if (params.password_form.origin.is_valid())
1506 GetPasswordManager()->ProvisionallySavePassword(params.password_form);
1507}
1508
[email protected]8cc8d492010-02-02 10:40:491509void TabContents::CloseConstrainedWindows() {
1510 // Clear out any constrained windows since we are leaving this page entirely.
[email protected]420ae012009-04-24 05:16:321511 // We use indices instead of iterators in case CloseWindow does something
1512 // that may invalidate an iterator.
1513 int size = static_cast<int>(child_windows_.size());
1514 for (int i = size - 1; i >= 0; --i) {
1515 ConstrainedWindow* window = child_windows_[i];
[email protected]fa1cf0b82010-01-15 21:49:441516 if (window) {
[email protected]420ae012009-04-24 05:16:321517 window->CloseConstrainedWindow();
[email protected]fa1cf0b82010-01-15 21:49:441518 BlockTabContent(false);
1519 }
[email protected]420ae012009-04-24 05:16:321520 }
1521}
1522
1523void TabContents::UpdateStarredStateForCurrentURL() {
1524 BookmarkModel* model = profile()->GetBookmarkModel();
1525 const bool old_state = is_starred_;
1526 is_starred_ = (model && model->IsBookmarked(GetURL()));
1527
1528 if (is_starred_ != old_state && delegate())
1529 delegate()->URLStarredChanged(this, is_starred_);
1530}
1531
1532void TabContents::UpdateAlternateErrorPageURL() {
1533 GURL url = GetAlternateErrorPageURL();
1534 render_view_host()->SetAlternateErrorPageURL(url);
1535}
1536
1537void TabContents::UpdateWebPreferences() {
1538 render_view_host()->UpdateWebPreferences(GetWebkitPrefs());
1539}
1540
1541void TabContents::UpdateMaxPageIDIfNecessary(SiteInstance* site_instance,
1542 RenderViewHost* rvh) {
1543 // If we are creating a RVH for a restored controller, then we might
1544 // have more page IDs than the SiteInstance's current max page ID. We must
1545 // make sure that the max page ID is larger than any restored page ID.
1546 // Note that it is ok for conflicting page IDs to exist in another tab
1547 // (i.e., NavigationController), but if any page ID is larger than the max,
1548 // the back/forward list will get confused.
1549 int max_restored_page_id = controller_.max_restored_page_id();
1550 if (max_restored_page_id > 0) {
1551 int curr_max_page_id = site_instance->max_page_id();
1552 if (max_restored_page_id > curr_max_page_id) {
1553 // Need to update the site instance immediately.
1554 site_instance->UpdateMaxPageID(max_restored_page_id);
1555
1556 // Also tell the renderer to update its internal representation. We
1557 // need to reserve enough IDs to make all restored page IDs less than
1558 // the max.
1559 if (curr_max_page_id < 0)
1560 curr_max_page_id = 0;
1561 rvh->ReservePageIDRange(max_restored_page_id - curr_max_page_id);
1562 }
1563 }
1564}
1565
1566void TabContents::UpdateHistoryForNavigation(
[email protected]ebe89e062009-08-13 23:16:541567 const GURL& virtual_url,
[email protected]befd8d822009-07-01 04:51:471568 const NavigationController::LoadCommittedDetails& details,
[email protected]420ae012009-04-24 05:16:321569 const ViewHostMsg_FrameNavigate_Params& params) {
1570 if (profile()->IsOffTheRecord())
1571 return;
1572
1573 // Add to history service.
1574 HistoryService* hs = profile()->GetHistoryService(Profile::IMPLICIT_ACCESS);
1575 if (hs) {
1576 if (PageTransition::IsMainFrame(params.transition) &&
[email protected]ebe89e062009-08-13 23:16:541577 virtual_url != params.url) {
1578 // Hack on the "virtual" URL so that it will appear in history. For some
[email protected]420ae012009-04-24 05:16:321579 // types of URLs, we will display a magic URL that is different from where
1580 // the page is actually navigated. We want the user to see in history
[email protected]ebe89e062009-08-13 23:16:541581 // what they saw in the URL bar, so we add the virtual URL as a redirect.
1582 // This only applies to the main frame, as the virtual URL doesn't apply
[email protected]420ae012009-04-24 05:16:321583 // to sub-frames.
1584 std::vector<GURL> redirects = params.redirects;
1585 if (!redirects.empty())
[email protected]ebe89e062009-08-13 23:16:541586 redirects.back() = virtual_url;
1587 hs->AddPage(virtual_url, this, params.page_id, params.referrer,
[email protected]befd8d822009-07-01 04:51:471588 params.transition, redirects, details.did_replace_entry);
[email protected]420ae012009-04-24 05:16:321589 } else {
1590 hs->AddPage(params.url, this, params.page_id, params.referrer,
[email protected]befd8d822009-07-01 04:51:471591 params.transition, params.redirects,
1592 details.did_replace_entry);
[email protected]420ae012009-04-24 05:16:321593 }
1594 }
1595}
1596
1597bool TabContents::UpdateTitleForEntry(NavigationEntry* entry,
1598 const std::wstring& title) {
1599 // For file URLs without a title, use the pathname instead. In the case of a
1600 // synthesized title, we don't want the update to count toward the "one set
1601 // per page of the title to history."
1602 std::wstring final_title;
1603 bool explicit_set;
1604 if (entry->url().SchemeIsFile() && title.empty()) {
1605 final_title = UTF8ToWide(entry->url().ExtractFileName());
1606 explicit_set = false; // Don't count synthetic titles toward the set limit.
1607 } else {
1608 TrimWhitespace(title, TRIM_ALL, &final_title);
1609 explicit_set = true;
1610 }
1611
1612 if (final_title == UTF16ToWideHack(entry->title()))
1613 return false; // Nothing changed, don't bother.
1614
1615 entry->set_title(WideToUTF16Hack(final_title));
1616
1617 // Update the history system for this page.
1618 if (!profile()->IsOffTheRecord() && !received_page_title_) {
1619 HistoryService* hs =
1620 profile()->GetHistoryService(Profile::IMPLICIT_ACCESS);
1621 if (hs)
[email protected]ebe89e062009-08-13 23:16:541622 hs->SetPageTitle(entry->virtual_url(), final_title);
[email protected]420ae012009-04-24 05:16:321623
1624 // Don't allow the title to be saved again for explicitly set ones.
1625 received_page_title_ = explicit_set;
1626 }
1627
1628 // Lastly, set the title for the view.
1629 view_->SetPageTitle(final_title);
1630
1631 return true;
1632}
1633
1634void TabContents::NotifySwapped() {
1635 // After sending out a swap notification, we need to send a disconnect
1636 // notification so that clients that pick up a pointer to |this| can NULL the
1637 // pointer. See Bug 1230284.
1638 notify_disconnection_ = true;
1639 NotificationService::current()->Notify(
[email protected]57c6a652009-05-04 07:58:341640 NotificationType::TAB_CONTENTS_SWAPPED,
[email protected]d82ed61e2009-06-16 02:46:221641 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:321642 NotificationService::NoDetails());
1643}
1644
1645void TabContents::NotifyConnected() {
1646 notify_disconnection_ = true;
1647 NotificationService::current()->Notify(
[email protected]57c6a652009-05-04 07:58:341648 NotificationType::TAB_CONTENTS_CONNECTED,
[email protected]d82ed61e2009-06-16 02:46:221649 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:321650 NotificationService::NoDetails());
1651}
1652
1653void TabContents::NotifyDisconnected() {
1654 if (!notify_disconnection_)
1655 return;
1656
1657 notify_disconnection_ = false;
1658 NotificationService::current()->Notify(
[email protected]57c6a652009-05-04 07:58:341659 NotificationType::TAB_CONTENTS_DISCONNECTED,
[email protected]d82ed61e2009-06-16 02:46:221660 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:321661 NotificationService::NoDetails());
1662}
1663
1664void TabContents::GenerateKeywordIfNecessary(
1665 const ViewHostMsg_FrameNavigate_Params& params) {
1666 if (!params.searchable_form_url.is_valid())
1667 return;
1668
1669 if (profile()->IsOffTheRecord())
1670 return;
1671
1672 int last_index = controller_.last_committed_entry_index();
1673 // When there was no previous page, the last index will be 0. This is
1674 // normally due to a form submit that opened in a new tab.
1675 // TODO(brettw) bug 916126: we should support keywords when form submits
1676 // happen in new tabs.
1677 if (last_index <= 0)
1678 return;
1679 const NavigationEntry* previous_entry =
1680 controller_.GetEntryAtIndex(last_index - 1);
1681 if (IsFormSubmit(previous_entry)) {
1682 // Only generate a keyword if the previous page wasn't itself a form
1683 // submit.
1684 return;
1685 }
1686
1687 GURL keyword_url = previous_entry->user_typed_url().is_valid() ?
1688 previous_entry->user_typed_url() : previous_entry->url();
1689 std::wstring keyword =
1690 TemplateURLModel::GenerateKeyword(keyword_url, true); // autodetected
1691 if (keyword.empty())
1692 return;
1693
1694 TemplateURLModel* url_model = profile()->GetTemplateURLModel();
1695 if (!url_model)
1696 return;
1697
1698 if (!url_model->loaded()) {
1699 url_model->Load();
1700 return;
1701 }
1702
1703 const TemplateURL* current_url;
1704 std::wstring url = UTF8ToWide(params.searchable_form_url.spec());
1705 if (!url_model->CanReplaceKeyword(keyword, url, &current_url))
1706 return;
1707
1708 if (current_url) {
1709 if (current_url->originating_url().is_valid()) {
1710 // The existing keyword was generated from an OpenSearch description
1711 // document, don't regenerate.
1712 return;
1713 }
1714 url_model->Remove(current_url);
1715 }
1716 TemplateURL* new_url = new TemplateURL();
1717 new_url->set_keyword(keyword);
1718 new_url->set_short_name(keyword);
1719 new_url->SetURL(url, 0, 0);
1720 new_url->add_input_encoding(params.searchable_form_encoding);
1721 DCHECK(controller_.GetLastCommittedEntry());
1722 const GURL& favicon_url =
1723 controller_.GetLastCommittedEntry()->favicon().url();
1724 if (favicon_url.is_valid()) {
1725 new_url->SetFavIconURL(favicon_url);
1726 } else {
1727 // The favicon url isn't valid. This means there really isn't a favicon,
1728 // or the favicon url wasn't obtained before the load started. This assumes
1729 // the later.
1730 // TODO(sky): Need a way to set the favicon that doesn't involve generating
1731 // its url.
1732 new_url->SetFavIconURL(TemplateURL::GenerateFaviconURL(params.referrer));
1733 }
1734 new_url->set_safe_for_autoreplace(true);
1735 url_model->Add(new_url);
1736}
1737
[email protected]bf5c2ff392009-07-08 16:24:331738void TabContents::OnUserGesture() {
1739 // See comment in RenderViewHostDelegate::OnUserGesture as to why we do this.
1740 DownloadRequestManager* drm = g_browser_process->download_request_manager();
1741 if (drm)
1742 drm->OnUserGesture(this);
[email protected]e7eaedde2009-09-25 20:09:491743 ExternalProtocolHandler::OnUserGesture();
[email protected]bf5c2ff392009-07-08 16:24:331744 controller_.OnUserGesture();
1745}
1746
1747void TabContents::OnFindReply(int request_id,
1748 int number_of_matches,
1749 const gfx::Rect& selection_rect,
1750 int active_match_ordinal,
1751 bool final_update) {
[email protected]3ce7ced2009-07-24 18:52:421752 // Ignore responses for requests that have been aborted.
1753 if (find_op_aborted_)
1754 return;
1755
[email protected]bf5c2ff392009-07-08 16:24:331756 // Ignore responses for requests other than the one we have most recently
1757 // issued. That way we won't act on stale results when the user has
1758 // already typed in another query.
1759 if (request_id != current_find_request_id_)
1760 return;
1761
1762 if (number_of_matches == -1)
1763 number_of_matches = last_search_result_.number_of_matches();
1764 if (active_match_ordinal == -1)
1765 active_match_ordinal = last_search_result_.active_match_ordinal();
1766
1767 gfx::Rect selection = selection_rect;
1768 if (selection.IsEmpty())
1769 selection = last_search_result_.selection_rect();
1770
1771 // Notify the UI, automation and any other observers that a find result was
1772 // found.
1773 last_search_result_ = FindNotificationDetails(request_id, number_of_matches,
1774 selection, active_match_ordinal,
1775 final_update);
1776 NotificationService::current()->Notify(
1777 NotificationType::FIND_RESULT_AVAILABLE,
1778 Source<TabContents>(this),
1779 Details<FindNotificationDetails>(&last_search_result_));
1780}
1781
1782void TabContents::GoToEntryAtOffset(int offset) {
[email protected]f9cc4c452009-10-13 14:56:381783 if (!delegate_ || delegate_->OnGoToEntryOffset(offset))
1784 controller_.GoToOffset(offset);
[email protected]bf5c2ff392009-07-08 16:24:331785}
1786
1787void TabContents::GetHistoryListCount(int* back_list_count,
1788 int* forward_list_count) {
1789 int current_index = controller_.last_committed_entry_index();
1790 *back_list_count = current_index;
1791 *forward_list_count = controller_.entry_count() - current_index - 1;
1792}
1793
1794void TabContents::OnMissingPluginStatus(int status) {
1795#if defined(OS_WIN)
1796// TODO(PORT): pull in when plug-ins work
1797 GetPluginInstaller()->OnMissingPluginStatus(status);
1798#endif
1799}
1800
1801void TabContents::OnCrashedPlugin(const FilePath& plugin_path) {
[email protected]bf5c2ff392009-07-08 16:24:331802 DCHECK(!plugin_path.value().empty());
1803
1804 std::wstring plugin_name = plugin_path.ToWStringHack();
[email protected]d32d46182009-07-23 00:09:351805#if defined(OS_WIN) || defined(OS_MACOSX)
[email protected]bf5c2ff392009-07-08 16:24:331806 scoped_ptr<FileVersionInfo> version_info(
1807 FileVersionInfo::CreateFileVersionInfo(plugin_path));
1808 if (version_info.get()) {
1809 const std::wstring& product_name = version_info->product_name();
[email protected]996fd702009-09-04 19:12:371810 if (!product_name.empty()) {
[email protected]bf5c2ff392009-07-08 16:24:331811 plugin_name = product_name;
[email protected]996fd702009-09-04 19:12:371812#if defined(OS_MACOSX)
1813 // Many plugins on the Mac have .plugin in the actual name, which looks
1814 // terrible, so look for that and strip it off if present.
1815 const std::wstring plugin_extension(L".plugin");
1816 if (EndsWith(plugin_name, plugin_extension, true))
1817 plugin_name.erase(plugin_name.length() - plugin_extension.length());
1818#endif // OS_MACOSX
1819 }
[email protected]bf5c2ff392009-07-08 16:24:331820 }
[email protected]4a560ed2009-07-13 23:35:261821#else
1822 NOTIMPLEMENTED() << " convert plugin path to plugin name";
1823#endif
[email protected]bf5c2ff392009-07-08 16:24:331824 AddInfoBar(new SimpleAlertInfoBarDelegate(
1825 this, l10n_util::GetStringF(IDS_PLUGIN_CRASHED_PROMPT, plugin_name),
1826 NULL));
[email protected]bf5c2ff392009-07-08 16:24:331827}
1828
1829void TabContents::OnCrashedWorker() {
1830 AddInfoBar(new SimpleAlertInfoBarDelegate(
1831 this, l10n_util::GetString(IDS_WEBWORKER_CRASHED_PROMPT),
1832 NULL));
1833}
1834
1835void TabContents::OnDidGetApplicationInfo(
1836 int32 page_id,
1837 const webkit_glue::WebApplicationInfo& info) {
[email protected]ed5431872009-11-17 08:39:511838 web_app_info_ = info;
[email protected]bf5c2ff392009-07-08 16:24:331839
[email protected]ed5431872009-11-17 08:39:511840 if (delegate())
1841 delegate()->OnDidGetApplicationInfo(this, page_id);
[email protected]bf5c2ff392009-07-08 16:24:331842}
1843
[email protected]a2cf65eb2010-01-18 08:20:381844void TabContents::OnPageContents(const GURL& url,
1845 int renderer_process_id,
1846 int32 page_id,
[email protected]a8a81292010-01-21 00:32:451847 const std::wstring& contents,
1848 const std::string& language) {
[email protected]a2cf65eb2010-01-18 08:20:381849 // Don't index any https pages. People generally don't want their bank
1850 // accounts, etc. indexed on their computer, especially since some of these
1851 // things are not marked cachable.
1852 // TODO(brettw) we may want to consider more elaborate heuristics such as
1853 // the cachability of the page. We may also want to consider subframes (this
1854 // test will still index subframes if the subframe is SSL).
1855 if (!url.SchemeIsSecure()) {
1856 Profile* p = profile();
1857 if (p && !p->IsOffTheRecord()) {
1858 HistoryService* hs = p->GetHistoryService(Profile::IMPLICIT_ACCESS);
1859 if (hs)
1860 hs->SetPageContents(url, contents);
1861 }
1862 }
[email protected]a8a81292010-01-21 00:32:451863
1864 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]8cb5d5b2010-02-09 11:36:161865 if (GetRenderProcessHost()->id() == renderer_process_id &&
[email protected]a8a81292010-01-21 00:32:451866 entry && entry->page_id() == page_id) {
1867 entry->set_language(language);
1868 }
1869
1870 std::string lang = language;
1871 NotificationService::current()->Notify(
1872 NotificationType::TAB_LANGUAGE_DETERMINED,
[email protected]7e0831b2010-02-01 08:45:151873 Source<TabContents>(this),
[email protected]a8a81292010-01-21 00:32:451874 Details<std::string>(&lang));
[email protected]a2cf65eb2010-01-18 08:20:381875}
1876
[email protected]7e0831b2010-02-01 08:45:151877void TabContents::OnPageTranslated(int32 page_id,
1878 const std::string& original_lang,
1879 const std::string& translated_lang) {
1880 std::pair<std::string, std::string> lang_pair =
1881 std::make_pair(original_lang, translated_lang);
1882 NotificationService::current()->Notify(
1883 NotificationType::PAGE_TRANSLATED,
1884 Source<TabContents>(this),
1885 Details<std::pair<std::string, std::string> >(&lang_pair));
1886}
1887
[email protected]bf5c2ff392009-07-08 16:24:331888void TabContents::DidStartProvisionalLoadForFrame(
1889 RenderViewHost* render_view_host,
1890 bool is_main_frame,
1891 const GURL& url) {
1892 ProvisionalLoadDetails details(is_main_frame,
1893 controller_.IsURLInPageNavigation(url),
1894 url, std::string(), false);
1895 NotificationService::current()->Notify(
1896 NotificationType::FRAME_PROVISIONAL_LOAD_START,
1897 Source<NavigationController>(&controller_),
1898 Details<ProvisionalLoadDetails>(&details));
1899}
1900
1901void TabContents::DidStartReceivingResourceResponse(
[email protected]3dbb80b2010-02-09 22:41:201902 const ResourceRequestDetails& details) {
[email protected]bf5c2ff392009-07-08 16:24:331903 NotificationService::current()->Notify(
1904 NotificationType::RESOURCE_RESPONSE_STARTED,
1905 Source<NavigationController>(&controller()),
[email protected]3dbb80b2010-02-09 22:41:201906 Details<const ResourceRequestDetails>(&details));
[email protected]bf5c2ff392009-07-08 16:24:331907}
1908
[email protected]3dbb80b2010-02-09 22:41:201909void TabContents::DidRedirectResource(
1910 const ResourceRedirectDetails& details) {
[email protected]bf5c2ff392009-07-08 16:24:331911 NotificationService::current()->Notify(
1912 NotificationType::RESOURCE_RECEIVED_REDIRECT,
1913 Source<NavigationController>(&controller()),
[email protected]3dbb80b2010-02-09 22:41:201914 Details<const ResourceRequestDetails>(&details));
[email protected]bf5c2ff392009-07-08 16:24:331915}
1916
1917void TabContents::DidLoadResourceFromMemoryCache(
1918 const GURL& url,
1919 const std::string& frame_origin,
1920 const std::string& main_frame_origin,
1921 const std::string& security_info) {
1922 // Send out a notification that we loaded a resource from our memory cache.
1923 int cert_id = 0, cert_status = 0, security_bits = 0;
1924 SSLManager::DeserializeSecurityInfo(security_info,
1925 &cert_id, &cert_status,
1926 &security_bits);
1927 LoadFromMemoryCacheDetails details(url, frame_origin, main_frame_origin,
[email protected]8cb5d5b2010-02-09 11:36:161928 GetRenderProcessHost()->id(), cert_id,
1929 cert_status);
[email protected]bf5c2ff392009-07-08 16:24:331930
1931 NotificationService::current()->Notify(
1932 NotificationType::LOAD_FROM_MEMORY_CACHE,
1933 Source<NavigationController>(&controller_),
1934 Details<LoadFromMemoryCacheDetails>(&details));
1935}
1936
[email protected]e3d60e5d2009-09-25 21:08:291937void TabContents::DidDisplayInsecureContent() {
[email protected]09843b5f2009-09-26 08:30:591938 controller_.ssl_manager()->DidDisplayInsecureContent();
[email protected]e3d60e5d2009-09-25 21:08:291939}
1940
1941void TabContents::DidRunInsecureContent(const std::string& security_origin) {
[email protected]09843b5f2009-09-26 08:30:591942 controller_.ssl_manager()->DidRunInsecureContent(security_origin);
[email protected]e3d60e5d2009-09-25 21:08:291943}
1944
[email protected]bf5c2ff392009-07-08 16:24:331945void TabContents::DidFailProvisionalLoadWithError(
1946 RenderViewHost* render_view_host,
1947 bool is_main_frame,
1948 int error_code,
1949 const GURL& url,
1950 bool showing_repost_interstitial) {
1951 if (net::ERR_ABORTED == error_code) {
1952 // EVIL HACK ALERT! Ignore failed loads when we're showing interstitials.
1953 // This means that the interstitial won't be torn down properly, which is
1954 // bad. But if we have an interstitial, go back to another tab type, and
1955 // then load the same interstitial again, we could end up getting the first
1956 // interstitial's "failed" message (as a result of the cancel) when we're on
1957 // the second one.
1958 //
1959 // We can't tell this apart, so we think we're tearing down the current page
1960 // which will cause a crash later one. There is also some code in
1961 // RenderViewHostManager::RendererAbortedProvisionalLoad that is commented
1962 // out because of this problem.
1963 //
1964 // http://code.google.com/p/chromium/issues/detail?id=2855
1965 // Because this will not tear down the interstitial properly, if "back" is
1966 // back to another tab type, the interstitial will still be somewhat alive
1967 // in the previous tab type. If you navigate somewhere that activates the
1968 // tab with the interstitial again, you'll see a flash before the new load
1969 // commits of the interstitial page.
1970 if (showing_interstitial_page()) {
1971 LOG(WARNING) << "Discarding message during interstitial.";
1972 return;
1973 }
1974
1975 // This will discard our pending entry if we cancelled the load (e.g., if we
1976 // decided to download the file instead of load it). Only discard the
1977 // pending entry if the URLs match, otherwise the user initiated a navigate
1978 // before the page loaded so that the discard would discard the wrong entry.
1979 NavigationEntry* pending_entry = controller_.pending_entry();
1980 if (pending_entry && pending_entry->url() == url) {
1981 controller_.DiscardNonCommittedEntries();
1982 // Update the URL display.
1983 NotifyNavigationStateChanged(TabContents::INVALIDATE_URL);
1984 }
1985
1986 render_manager_.RendererAbortedProvisionalLoad(render_view_host);
1987 }
1988
1989 // Send out a notification that we failed a provisional load with an error.
1990 ProvisionalLoadDetails details(is_main_frame,
1991 controller_.IsURLInPageNavigation(url),
1992 url, std::string(), false);
1993 details.set_error_code(error_code);
1994
1995 NotificationService::current()->Notify(
1996 NotificationType::FAIL_PROVISIONAL_LOAD_WITH_ERROR,
1997 Source<NavigationController>(&controller_),
1998 Details<ProvisionalLoadDetails>(&details));
1999}
2000
2001void TabContents::DocumentLoadedInFrame() {
2002 controller_.DocumentLoadedInFrame();
2003}
2004
[email protected]0de80162010-02-03 04:52:352005void TabContents::OnContentBlocked(ContentSettingsType type) {
2006 content_blocked_[type] = true;
2007 if (delegate_)
2008 delegate_->OnBlockedContentChange(this);
2009}
2010
[email protected]8d3347f2009-07-09 22:00:212011RenderViewHostDelegate::View* TabContents::GetViewDelegate() {
[email protected]420ae012009-04-24 05:16:322012 return view_.get();
2013}
2014
[email protected]8d3347f2009-07-09 22:00:212015RenderViewHostDelegate::RendererManagement*
2016TabContents::GetRendererManagementDelegate() {
2017 return &render_manager_;
2018}
2019
[email protected]bf5c2ff392009-07-08 16:24:332020RenderViewHostDelegate::BrowserIntegration*
[email protected]8d3347f2009-07-09 22:00:212021 TabContents::GetBrowserIntegrationDelegate() {
2022 return this;
[email protected]bf5c2ff392009-07-08 16:24:332023}
2024
[email protected]8d3347f2009-07-09 22:00:212025RenderViewHostDelegate::Resource* TabContents::GetResourceDelegate() {
2026 return this;
[email protected]bf5c2ff392009-07-08 16:24:332027}
2028
[email protected]8d3347f2009-07-09 22:00:212029RenderViewHostDelegate::Save* TabContents::GetSaveDelegate() {
[email protected]420ae012009-04-24 05:16:322030 return save_package_.get(); // May be NULL, but we can return NULL.
2031}
2032
[email protected]8d3347f2009-07-09 22:00:212033RenderViewHostDelegate::Printing* TabContents::GetPrintingDelegate() {
2034 return &printing_;
2035}
2036
2037RenderViewHostDelegate::FavIcon* TabContents::GetFavIconDelegate() {
2038 return &fav_icon_helper_;
2039}
2040
[email protected]c54d269e2009-10-21 18:07:382041RenderViewHostDelegate::FormFieldHistory*
2042TabContents::GetFormFieldHistoryDelegate() {
[email protected]19289d02009-10-21 21:47:002043 if (form_field_history_manager_.get() == NULL)
2044 form_field_history_manager_.reset(new FormFieldHistoryManager(this));
2045 return form_field_history_manager_.get();
[email protected]bf5c2ff392009-07-08 16:24:332046}
2047
[email protected]21674df2009-10-28 19:51:212048RenderViewHostDelegate::AutoFill* TabContents::GetAutoFillDelegate() {
2049 if (autofill_manager_.get() == NULL)
2050 autofill_manager_.reset(new AutoFillManager(this));
2051 return autofill_manager_.get();
2052}
2053
[email protected]93623c5d2009-12-10 21:40:322054RendererPreferences TabContents::GetRendererPrefs(Profile* profile) const {
[email protected]840b1512009-07-21 16:53:462055 return renderer_preferences_;
[email protected]80d96fa2009-06-10 22:34:512056}
2057
[email protected]57c6a652009-05-04 07:58:342058TabContents* TabContents::GetAsTabContents() {
2059 return this;
2060}
2061
[email protected]7b291f92009-08-14 05:43:532062ViewType::Type TabContents::GetRenderViewType() const {
2063 return ViewType::TAB_CONTENTS;
2064}
2065
2066int TabContents::GetBrowserWindowID() const {
2067 return controller().window_id().id();
2068}
2069
[email protected]420ae012009-04-24 05:16:322070void TabContents::RenderViewCreated(RenderViewHost* render_view_host) {
[email protected]2e4633c2009-07-09 16:58:062071 NotificationService::current()->Notify(
2072 NotificationType::RENDER_VIEW_HOST_CREATED_FOR_TAB,
2073 Source<TabContents>(this),
2074 Details<RenderViewHost>(render_view_host));
[email protected]420ae012009-04-24 05:16:322075 NavigationEntry* entry = controller_.GetActiveEntry();
2076 if (!entry)
2077 return;
2078
2079 // When we're creating views, we're still doing initial setup, so we always
2080 // use the pending DOM UI rather than any possibly existing committed one.
2081 if (render_manager_.pending_dom_ui()) {
2082 render_manager_.pending_dom_ui()->RenderViewCreated(render_view_host);
2083 }
2084
2085 if (entry->IsViewSourceMode()) {
2086 // Put the renderer in view source mode.
2087 render_view_host->Send(
2088 new ViewMsg_EnableViewSourceMode(render_view_host->routing_id()));
2089 }
[email protected]0666aef2009-05-13 19:48:082090
2091 view()->RenderViewCreated(render_view_host);
[email protected]420ae012009-04-24 05:16:322092}
2093
2094void TabContents::RenderViewReady(RenderViewHost* rvh) {
2095 if (rvh != render_view_host()) {
2096 // Don't notify the world, since this came from a renderer in the
2097 // background.
2098 return;
2099 }
2100
2101 NotifyConnected();
[email protected]9d8a4642009-07-29 17:25:302102 bool was_crashed = is_crashed();
[email protected]420ae012009-04-24 05:16:322103 SetIsCrashed(false);
[email protected]9d8a4642009-07-29 17:25:302104
2105 // Restore the focus to the tab (otherwise the focus will be on the top
2106 // window).
2107 if (was_crashed && !FocusLocationBarByDefault())
2108 Focus();
[email protected]420ae012009-04-24 05:16:322109}
2110
2111void TabContents::RenderViewGone(RenderViewHost* rvh) {
2112 // Ask the print preview if this renderer was valuable.
2113 if (!printing_.OnRenderViewGone(rvh))
2114 return;
2115 if (rvh != render_view_host()) {
2116 // The pending page's RenderViewHost is gone.
2117 return;
2118 }
2119
2120 SetIsLoading(false, NULL);
2121 NotifyDisconnected();
2122 SetIsCrashed(true);
2123
[email protected]8e537302009-06-10 21:19:582124 // Tell the view that we've crashed so it can prepare the sad tab page.
[email protected]cf68b092010-01-15 20:05:552125 // Only do this if we're not in browser shutdown, so that TabContents
2126 // objects that are not in a browser (e.g., HTML dialogs) and thus are
2127 // visible do not flash a sad tab page.
2128 if (browser_shutdown::GetShutdownType() == browser_shutdown::NOT_VALID)
2129 view_->OnTabCrashed();
[email protected]420ae012009-04-24 05:16:322130
2131 // Hide any visible hung renderer warning for this web contents' process.
[email protected]e7b8d832009-11-20 22:41:002132 hung_renderer_dialog::HideForTabContents(this);
[email protected]420ae012009-04-24 05:16:322133}
2134
[email protected]2e4633c2009-07-09 16:58:062135void TabContents::RenderViewDeleted(RenderViewHost* rvh) {
2136 NotificationService::current()->Notify(
2137 NotificationType::RENDER_VIEW_HOST_DELETED,
2138 Source<TabContents>(this),
2139 Details<RenderViewHost>(rvh));
[email protected]fb7b79f72009-11-06 18:00:482140 render_manager_.RenderViewDeleted(rvh);
[email protected]2e4633c2009-07-09 16:58:062141}
2142
[email protected]420ae012009-04-24 05:16:322143void TabContents::DidNavigate(RenderViewHost* rvh,
2144 const ViewHostMsg_FrameNavigate_Params& params) {
[email protected]8030f012009-09-25 18:09:372145 int extra_invalidate_flags = 0;
2146
2147 if (PageTransition::IsMainFrame(params.transition)) {
[email protected]20ede072009-10-06 00:28:352148 bool was_bookmark_bar_visible = ShouldShowBookmarkBar();
[email protected]8030f012009-09-25 18:09:372149 bool was_extension_shelf_visible = IsExtensionShelfAlwaysVisible();
2150
[email protected]420ae012009-04-24 05:16:322151 render_manager_.DidNavigateMainFrame(rvh);
2152
[email protected]20ede072009-10-06 00:28:352153 if (was_bookmark_bar_visible != ShouldShowBookmarkBar())
[email protected]8030f012009-09-25 18:09:372154 extra_invalidate_flags |= INVALIDATE_BOOKMARK_BAR;
2155 if (was_extension_shelf_visible != IsExtensionShelfAlwaysVisible())
2156 extra_invalidate_flags |= INVALIDATE_EXTENSION_SHELF;
2157 }
2158
[email protected]420ae012009-04-24 05:16:322159 // Update the site of the SiteInstance if it doesn't have one yet.
2160 if (!GetSiteInstance()->has_site())
2161 GetSiteInstance()->SetSite(params.url);
2162
2163 // Need to update MIME type here because it's referred to in
2164 // UpdateNavigationCommands() called by RendererDidNavigate() to
2165 // determine whether or not to enable the encoding menu.
2166 // It's updated only for the main frame. For a subframe,
2167 // RenderView::UpdateURL does not set params.contents_mime_type.
2168 // (see http://code.google.com/p/chromium/issues/detail?id=2929 )
2169 // TODO(jungshik): Add a test for the encoding menu to avoid
2170 // regressing it again.
2171 if (PageTransition::IsMainFrame(params.transition))
2172 contents_mime_type_ = params.contents_mime_type;
2173
2174 NavigationController::LoadCommittedDetails details;
[email protected]8030f012009-09-25 18:09:372175 bool did_navigate = controller_.RendererDidNavigate(
2176 params, extra_invalidate_flags, &details);
[email protected]3e90d4a2009-07-03 17:38:392177
2178 // Update history. Note that this needs to happen after the entry is complete,
2179 // which WillNavigate[Main,Sub]Frame will do before this function is called.
[email protected]2e29d922009-10-22 22:13:352180 if (params.should_update_history &&
2181 (!delegate() || delegate()->ShouldAddNavigationsToHistory())) {
[email protected]3e90d4a2009-07-03 17:38:392182 // Most of the time, the displayURL matches the loaded URL, but for about:
2183 // URLs, we use a data: URL as the real value. We actually want to save
2184 // the about: URL to the history db and keep the data: URL hidden. This is
2185 // what the TabContents' URL getter does.
2186 UpdateHistoryForNavigation(GetURL(), details, params);
2187 }
2188
[email protected]76543b92009-08-31 17:27:452189 if (!did_navigate)
2190 return; // No navigation happened.
[email protected]420ae012009-04-24 05:16:322191
2192 // DO NOT ADD MORE STUFF TO THIS FUNCTION! Your component should either listen
2193 // for the appropriate notification (best) or you can add it to
2194 // DidNavigateMainFramePostCommit / DidNavigateAnyFramePostCommit (only if
2195 // necessary, please).
2196
2197 // Run post-commit tasks.
2198 if (details.is_main_frame)
2199 DidNavigateMainFramePostCommit(details, params);
2200 DidNavigateAnyFramePostCommit(rvh, details, params);
2201}
2202
2203void TabContents::UpdateState(RenderViewHost* rvh,
2204 int32 page_id,
2205 const std::string& state) {
2206 DCHECK(rvh == render_view_host());
2207
2208 // We must be prepared to handle state updates for any page, these occur
2209 // when the user is scrolling and entering form data, as well as when we're
2210 // leaving a page, in which case our state may have already been moved to
2211 // the next page. The navigation controller will look up the appropriate
2212 // NavigationEntry and update it when it is notified via the delegate.
2213
2214 int entry_index = controller_.GetEntryIndexWithPageID(
2215 GetSiteInstance(), page_id);
2216 if (entry_index < 0)
2217 return;
2218 NavigationEntry* entry = controller_.GetEntryAtIndex(entry_index);
2219
2220 if (state == entry->content_state())
2221 return; // Nothing to update.
2222 entry->set_content_state(state);
2223 controller_.NotifyEntryChanged(entry, entry_index);
2224}
2225
2226void TabContents::UpdateTitle(RenderViewHost* rvh,
2227 int32 page_id, const std::wstring& title) {
2228 // If we have a title, that's a pretty good indication that we've started
2229 // getting useful data.
2230 SetNotWaitingForResponse();
2231
2232 DCHECK(rvh == render_view_host());
2233 NavigationEntry* entry = controller_.GetEntryWithPageID(GetSiteInstance(),
2234 page_id);
2235 if (!entry || !UpdateTitleForEntry(entry, title))
2236 return;
2237
2238 // Broadcast notifications when the UI should be updated.
2239 if (entry == controller_.GetEntryAtOffset(0))
[email protected]f1cd5e82009-10-23 17:19:032240 NotifyNavigationStateChanged(INVALIDATE_TITLE);
[email protected]420ae012009-04-24 05:16:322241}
2242
[email protected]420ae012009-04-24 05:16:322243void TabContents::UpdateEncoding(RenderViewHost* render_view_host,
[email protected]41fc0322009-09-04 22:23:402244 const std::string& encoding) {
[email protected]420ae012009-04-24 05:16:322245 set_encoding(encoding);
2246}
2247
2248void TabContents::UpdateTargetURL(int32 page_id, const GURL& url) {
2249 if (delegate())
2250 delegate()->UpdateTargetURL(this, url);
2251}
2252
2253void TabContents::UpdateThumbnail(const GURL& url,
2254 const SkBitmap& bitmap,
2255 const ThumbnailScore& score) {
2256 // Tell History about this thumbnail
[email protected]449478302009-06-09 20:04:282257 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kThumbnailStore)) {
[email protected]72d0fc72009-07-09 17:30:492258 if (!profile()->IsOffTheRecord())
[email protected]42339402009-07-17 17:27:192259 profile()->GetThumbnailStore()->SetPageThumbnail(url, bitmap,
2260 score, true);
[email protected]449478302009-06-09 20:04:282261 } else {
2262 HistoryService* hs;
2263 if (!profile()->IsOffTheRecord() &&
2264 (hs = profile()->GetHistoryService(Profile::IMPLICIT_ACCESS))) {
2265 hs->SetPageThumbnail(url, bitmap, score);
2266 }
[email protected]420ae012009-04-24 05:16:322267 }
2268}
2269
[email protected]8922e1f2009-10-03 05:01:262270void TabContents::UpdateInspectorSettings(const std::string& raw_settings) {
[email protected]0df30122009-06-03 12:13:082271 profile()->GetPrefs()->SetString(prefs::kWebKitInspectorSettings,
[email protected]8922e1f2009-10-03 05:01:262272 UTF8ToWide(raw_settings));
[email protected]0df30122009-06-03 12:13:082273}
2274
[email protected]420ae012009-04-24 05:16:322275void TabContents::Close(RenderViewHost* rvh) {
[email protected]07707302009-11-06 00:50:292276 // The UI may be in an event-tracking loop, such as between the
2277 // mouse-down and mouse-up in text selection or a button click.
2278 // Defer the close until after tracking is complete, so that we
2279 // don't free objects out from under the UI.
2280 // TODO(shess): This could probably be integrated with the
2281 // IsDoingDrag() test below. Punting for now because I need more
2282 // research to understand how this impacts platforms other than Mac.
2283 // TODO(shess): This could get more fine-grained. For instance,
2284 // closing a tab in another window while selecting text in the
2285 // current window's Omnibox should be just fine.
2286 if (view()->IsEventTracking()) {
2287 view()->CloseTabAfterEventTracking();
2288 return;
2289 }
2290
[email protected]24a4d1062009-07-10 23:10:422291 // If we close the tab while we're in the middle of a drag, we'll crash.
2292 // Instead, cancel the drag and close it as soon as the drag ends.
2293 if (view()->IsDoingDrag()) {
2294 view()->CancelDragAndCloseTab();
2295 return;
2296 }
2297
[email protected]420ae012009-04-24 05:16:322298 // Ignore this if it comes from a RenderViewHost that we aren't showing.
2299 if (delegate() && rvh == render_view_host())
2300 delegate()->CloseContents(this);
2301}
2302
2303void TabContents::RequestMove(const gfx::Rect& new_bounds) {
2304 if (delegate() && delegate()->IsPopup(this))
2305 delegate()->MoveContents(this, new_bounds);
2306}
2307
[email protected]7ab1e7d62009-10-14 23:32:012308void TabContents::DidStartLoading() {
[email protected]420ae012009-04-24 05:16:322309 SetIsLoading(true, NULL);
2310}
2311
[email protected]7ab1e7d62009-10-14 23:32:012312void TabContents::DidStopLoading() {
[email protected]420ae012009-04-24 05:16:322313 scoped_ptr<LoadNotificationDetails> details;
2314
2315 NavigationEntry* entry = controller_.GetActiveEntry();
2316 // An entry may not exist for a stop when loading an initial blank page or
2317 // if an iframe injected by script into a blank page finishes loading.
2318 if (entry) {
[email protected]420ae012009-04-24 05:16:322319 base::TimeDelta elapsed = base::TimeTicks::Now() - current_load_start_;
2320
2321 details.reset(new LoadNotificationDetails(
[email protected]ebe89e062009-08-13 23:16:542322 entry->virtual_url(),
[email protected]420ae012009-04-24 05:16:322323 entry->transition_type(),
2324 elapsed,
2325 &controller_,
2326 controller_.GetCurrentEntryIndex()));
2327 }
2328
2329 // Tell PasswordManager we've finished a page load, which serves as a
2330 // green light to save pending passwords and reset itself.
2331 GetPasswordManager()->DidStopLoading();
2332
2333 SetIsLoading(false, details.get());
2334}
2335
[email protected]420ae012009-04-24 05:16:322336void TabContents::DidRedirectProvisionalLoad(int32 page_id,
2337 const GURL& source_url,
2338 const GURL& target_url) {
2339 NavigationEntry* entry;
2340 if (page_id == -1)
2341 entry = controller_.pending_entry();
2342 else
2343 entry = controller_.GetEntryWithPageID(GetSiteInstance(), page_id);
2344 if (!entry || entry->url() != source_url)
2345 return;
2346 entry->set_url(target_url);
2347}
2348
[email protected]420ae012009-04-24 05:16:322349void TabContents::RequestOpenURL(const GURL& url, const GURL& referrer,
2350 WindowOpenDisposition disposition) {
2351 if (render_manager_.dom_ui()) {
2352 // When we're a DOM UI, it will provide a page transition type for us (this
2353 // is so the new tab page can specify AUTO_BOOKMARK for automatically
2354 // generated suggestions).
2355 //
2356 // Note also that we hide the referrer for DOM UI pages. We don't really
[email protected]60e448982009-05-06 04:21:162357 // want web sites to see a referrer of "chrome://blah" (and some
2358 // chrome: URLs might have search terms or other stuff we don't want to
[email protected]420ae012009-04-24 05:16:322359 // send to the site), so we send no referrer.
2360 OpenURL(url, GURL(), disposition,
2361 render_manager_.dom_ui()->link_transition_type());
2362 } else {
2363 OpenURL(url, referrer, disposition, PageTransition::LINK);
2364 }
2365}
2366
[email protected]1ee614862009-07-06 20:29:182367void TabContents::DomOperationResponse(const std::string& json_string,
2368 int automation_id) {
2369}
2370
[email protected]420ae012009-04-24 05:16:322371void TabContents::ProcessDOMUIMessage(const std::string& message,
[email protected]e4dad9fb2009-10-06 18:15:582372 const Value* content,
[email protected]811bfe32009-07-01 08:46:252373 int request_id,
2374 bool has_callback) {
[email protected]420ae012009-04-24 05:16:322375 if (!render_manager_.dom_ui()) {
[email protected]c2e74fe82009-09-03 17:57:442376 // This can happen if someone uses window.open() to open an extension URL
2377 // from a non-extension context.
2378 render_view_host()->BlockExtensionRequest(request_id);
[email protected]420ae012009-04-24 05:16:322379 return;
2380 }
[email protected]811bfe32009-07-01 08:46:252381 render_manager_.dom_ui()->ProcessDOMUIMessage(message, content, request_id,
2382 has_callback);
[email protected]420ae012009-04-24 05:16:322383}
2384
2385void TabContents::ProcessExternalHostMessage(const std::string& message,
2386 const std::string& origin,
2387 const std::string& target) {
2388 if (delegate())
2389 delegate()->ForwardMessageToExternalHost(message, origin, target);
2390}
2391
[email protected]420ae012009-04-24 05:16:322392void TabContents::RunFileChooser(bool multiple_files,
2393 const string16& title,
2394 const FilePath& default_file) {
2395 if (!select_file_dialog_.get())
2396 select_file_dialog_ = SelectFileDialog::Create(this);
2397 SelectFileDialog::Type dialog_type =
2398 multiple_files ? SelectFileDialog::SELECT_OPEN_MULTI_FILE :
2399 SelectFileDialog::SELECT_OPEN_FILE;
2400 select_file_dialog_->SelectFile(dialog_type, title, default_file,
2401 NULL, 0, FILE_PATH_LITERAL(""),
2402 view_->GetTopLevelNativeWindow(), NULL);
2403}
2404
2405void TabContents::RunJavaScriptMessage(
2406 const std::wstring& message,
2407 const std::wstring& default_prompt,
2408 const GURL& frame_url,
2409 const int flags,
2410 IPC::Message* reply_msg,
2411 bool* did_suppress_message) {
2412 // Suppress javascript messages when requested and when inside a constrained
2413 // popup window (because that activates them and breaks them out of the
2414 // constrained window jail).
[email protected]91ed49a2009-07-07 21:29:552415 // Also suppress messages when showing an interstitial. The interstitial is
2416 // shown over the previous page, we don't want the hidden page dialogs to
2417 // interfere with the interstitial.
2418 bool suppress_this_message = suppress_javascript_messages_ ||
2419 showing_interstitial_page();
[email protected]420ae012009-04-24 05:16:322420 if (delegate())
2421 suppress_this_message |=
[email protected]b6c874582009-05-08 19:38:312422 (delegate()->GetConstrainingContents(this) != this);
[email protected]420ae012009-04-24 05:16:322423
2424 *did_suppress_message = suppress_this_message;
2425
2426 if (!suppress_this_message) {
2427 base::TimeDelta time_since_last_message(
2428 base::TimeTicks::Now() - last_javascript_message_dismissal_);
2429 bool show_suppress_checkbox = false;
2430 // Show a checkbox offering to suppress further messages if this message is
2431 // being displayed within kJavascriptMessageExpectedDelay of the last one.
2432 if (time_since_last_message <
2433 base::TimeDelta::FromMilliseconds(kJavascriptMessageExpectedDelay))
2434 show_suppress_checkbox = true;
2435
[email protected]d82ed61e2009-06-16 02:46:222436 RunJavascriptMessageBox(this, frame_url, flags, message, default_prompt,
[email protected]420ae012009-04-24 05:16:322437 show_suppress_checkbox, reply_msg);
2438 } else {
2439 // If we are suppressing messages, just reply as is if the user immediately
2440 // pressed "Cancel".
[email protected]beb440c2009-11-06 04:08:542441 OnMessageBoxClosed(reply_msg, false, std::wstring());
[email protected]420ae012009-04-24 05:16:322442 }
2443}
2444
2445void TabContents::RunBeforeUnloadConfirm(const std::wstring& message,
2446 IPC::Message* reply_msg) {
[email protected]7ab1e7d62009-10-14 23:32:012447 is_showing_before_unload_dialog_ = true;
[email protected]d82ed61e2009-06-16 02:46:222448 RunBeforeUnloadDialog(this, message, reply_msg);
[email protected]420ae012009-04-24 05:16:322449}
2450
2451void TabContents::ShowModalHTMLDialog(const GURL& url, int width, int height,
2452 const std::string& json_arguments,
2453 IPC::Message* reply_msg) {
2454 if (delegate()) {
2455 HtmlDialogUIDelegate* dialog_delegate =
2456 new ModalHtmlDialogDelegate(url, width, height, json_arguments,
[email protected]d82ed61e2009-06-16 02:46:222457 reply_msg, this);
[email protected]420ae012009-04-24 05:16:322458 delegate()->ShowHtmlDialog(dialog_delegate, NULL);
2459 }
2460}
2461
2462void TabContents::PasswordFormsSeen(
[email protected]ed3fb032009-06-16 19:50:562463 const std::vector<webkit_glue::PasswordForm>& forms) {
[email protected]420ae012009-04-24 05:16:322464 GetPasswordManager()->PasswordFormsSeen(forms);
2465}
2466
[email protected]420ae012009-04-24 05:16:322467// Checks to see if we should generate a keyword based on the OSDD, and if
2468// necessary uses TemplateURLFetcher to download the OSDD and create a keyword.
2469void TabContents::PageHasOSDD(RenderViewHost* render_view_host,
2470 int32 page_id, const GURL& url,
2471 bool autodetected) {
2472 // Make sure page_id is the current page, and the TemplateURLModel is loaded.
2473 DCHECK(url.is_valid());
2474 if (!IsActiveEntry(page_id))
2475 return;
2476 TemplateURLModel* url_model = profile()->GetTemplateURLModel();
2477 if (!url_model)
2478 return;
2479 if (!url_model->loaded()) {
2480 url_model->Load();
2481 return;
2482 }
2483 if (!profile()->GetTemplateURLFetcher())
2484 return;
2485
2486 if (profile()->IsOffTheRecord())
2487 return;
2488
2489 const NavigationEntry* entry = controller_.GetLastCommittedEntry();
2490 DCHECK(entry);
2491
2492 const NavigationEntry* base_entry = entry;
2493 if (IsFormSubmit(base_entry)) {
2494 // If the current page is a form submit, find the last page that was not
2495 // a form submit and use its url to generate the keyword from.
2496 int index = controller_.last_committed_entry_index() - 1;
2497 while (index >= 0 && IsFormSubmit(controller_.GetEntryAtIndex(index)))
2498 index--;
2499 if (index >= 0)
2500 base_entry = controller_.GetEntryAtIndex(index);
2501 else
2502 base_entry = NULL;
2503 }
2504
2505 // We want to use the user typed URL if available since that represents what
2506 // the user typed to get here, and fall back on the regular URL if not.
2507 if (!base_entry)
2508 return;
2509 GURL keyword_url = base_entry->user_typed_url().is_valid() ?
2510 base_entry->user_typed_url() : base_entry->url();
2511 if (!keyword_url.is_valid())
2512 return;
2513 std::wstring keyword = TemplateURLModel::GenerateKeyword(keyword_url,
2514 autodetected);
[email protected]a88dbdd52010-01-15 19:25:532515
2516 // For JS added OSDD empty keyword is OK because we will generate keyword
2517 // later from OSDD content.
2518 if (autodetected && keyword.empty())
[email protected]420ae012009-04-24 05:16:322519 return;
2520 const TemplateURL* template_url =
2521 url_model->GetTemplateURLForKeyword(keyword);
2522 if (template_url && (!template_url->safe_for_autoreplace() ||
2523 template_url->originating_url() == url)) {
2524 // Either there is a user created TemplateURL for this keyword, or the
2525 // keyword has the same OSDD url and we've parsed it.
2526 return;
2527 }
2528
2529 // Download the OpenSearch description document. If this is successful a
2530 // new keyword will be created when done.
[email protected]420ae012009-04-24 05:16:322531 profile()->GetTemplateURLFetcher()->ScheduleDownload(
2532 keyword,
2533 url,
2534 base_entry->favicon().url(),
[email protected]d0cc9fb52009-06-23 02:26:162535 this,
[email protected]420ae012009-04-24 05:16:322536 autodetected);
2537}
2538
[email protected]420ae012009-04-24 05:16:322539GURL TabContents::GetAlternateErrorPageURL() const {
2540 GURL url;
2541 // Disable alternate error pages when in OffTheRecord/Incognito mode.
2542 if (profile()->IsOffTheRecord())
2543 return url;
2544
2545 PrefService* prefs = profile()->GetPrefs();
2546 DCHECK(prefs);
2547 if (prefs->GetBoolean(prefs::kAlternateErrorPagesEnabled)) {
[email protected]a33457b82009-09-21 23:07:152548 url = google_util::AppendGoogleLocaleParam(
2549 GURL(google_util::kLinkDoctorBaseURL));
[email protected]420ae012009-04-24 05:16:322550 url = google_util::AppendGoogleTLDParam(url);
2551 }
2552 return url;
2553}
2554
2555WebPreferences TabContents::GetWebkitPrefs() {
2556 PrefService* prefs = render_view_host()->process()->profile()->GetPrefs();
[email protected]7ccddb8c2009-08-04 17:36:552557 bool is_dom_ui = false;
2558 return RenderViewHostDelegateHelper::GetWebkitPrefs(prefs, is_dom_ui);
[email protected]420ae012009-04-24 05:16:322559}
2560
[email protected]fa1cf0b82010-01-15 21:49:442561void TabContents::OnIgnoredUIEvent() {
2562 if (constrained_window_count()) {
2563 ConstrainedWindow* window = *constrained_window_begin();
2564 window->FocusConstrainedWindow();
2565 }
2566}
2567
[email protected]420ae012009-04-24 05:16:322568void TabContents::OnJSOutOfMemory() {
2569 AddInfoBar(new SimpleAlertInfoBarDelegate(
2570 this, l10n_util::GetString(IDS_JS_OUT_OF_MEMORY_PROMPT), NULL));
2571}
2572
[email protected]420ae012009-04-24 05:16:322573void TabContents::OnCrossSiteResponse(int new_render_process_host_id,
2574 int new_request_id) {
[email protected]57c6a652009-05-04 07:58:342575 // Allows the TabContents to react when a cross-site response is ready to be
[email protected]420ae012009-04-24 05:16:322576 // delivered to a pending RenderViewHost. We must first run the onunload
2577 // handler of the old RenderViewHost before we can allow it to proceed.
2578 render_manager_.OnCrossSiteResponse(new_render_process_host_id,
2579 new_request_id);
2580}
2581
2582bool TabContents::CanBlur() const {
2583 return delegate() ? delegate()->CanBlur() : true;
2584}
2585
2586gfx::Rect TabContents::GetRootWindowResizerRect() const {
2587 if (delegate())
2588 return delegate()->GetRootWindowResizerRect();
2589 return gfx::Rect();
2590}
2591
2592void TabContents::RendererUnresponsive(RenderViewHost* rvh,
2593 bool is_during_unload) {
2594 if (is_during_unload) {
2595 // Hang occurred while firing the beforeunload/unload handler.
2596 // Pretend the handler fired so tab closing continues as if it had.
[email protected]829e7612009-04-25 01:15:112597 rvh->set_sudden_termination_allowed(true);
[email protected]420ae012009-04-24 05:16:322598
2599 if (!render_manager_.ShouldCloseTabOnUnresponsiveRenderer())
2600 return;
2601
2602 // If the tab hangs in the beforeunload/unload handler there's really
2603 // nothing we can do to recover. Pretend the unload listeners have
2604 // all fired and close the tab. If the hang is in the beforeunload handler
2605 // then the user will not have the option of cancelling the close.
2606 Close(rvh);
2607 return;
2608 }
2609
2610 if (render_view_host() && render_view_host()->IsRenderViewLive())
[email protected]e7b8d832009-11-20 22:41:002611 hung_renderer_dialog::ShowForTabContents(this);
[email protected]420ae012009-04-24 05:16:322612}
2613
2614void TabContents::RendererResponsive(RenderViewHost* render_view_host) {
[email protected]e7b8d832009-11-20 22:41:002615 hung_renderer_dialog::HideForTabContents(this);
[email protected]420ae012009-04-24 05:16:322616}
2617
2618void TabContents::LoadStateChanged(const GURL& url,
[email protected]094e5b22009-09-25 04:23:562619 net::LoadState load_state,
2620 uint64 upload_position,
2621 uint64 upload_size) {
[email protected]420ae012009-04-24 05:16:322622 load_state_ = load_state;
[email protected]094e5b22009-09-25 04:23:562623 upload_position_ = upload_position;
2624 upload_size_ = upload_size;
[email protected]d686e812009-06-03 19:10:292625 std::wstring languages =
2626 profile()->GetPrefs()->GetString(prefs::kAcceptLanguages);
[email protected]d686e812009-06-03 19:10:292627 std::string host = url.host();
[email protected]ce85f602009-11-07 01:34:532628 load_state_host_ =
2629 net::IDNToUnicode(host.c_str(), host.size(), languages, NULL);
[email protected]420ae012009-04-24 05:16:322630 if (load_state_ == net::LOAD_STATE_READING_RESPONSE)
2631 SetNotWaitingForResponse();
2632 if (is_loading())
[email protected]c9cd2222009-05-06 05:16:502633 NotifyNavigationStateChanged(INVALIDATE_LOAD | INVALIDATE_TAB);
[email protected]420ae012009-04-24 05:16:322634}
2635
[email protected]1ee614862009-07-06 20:29:182636bool TabContents::IsExternalTabContainer() const {
2637 if (!delegate())
2638 return false;
2639
2640 return delegate()->IsExternalTabContainer();
2641}
2642
[email protected]1ee614862009-07-06 20:29:182643void TabContents::DidInsertCSS() {
2644 // This RVHDelegate function is used for extensions and not us.
[email protected]420ae012009-04-24 05:16:322645}
2646
[email protected]130efb02009-09-18 18:54:352647void TabContents::FocusedNodeChanged() {
2648 NotificationService::current()->Notify(
2649 NotificationType::FOCUS_CHANGED_IN_PAGE,
2650 Source<RenderViewHost>(render_view_host()),
2651 NotificationService::NoDetails());
2652}
2653
[email protected]420ae012009-04-24 05:16:322654void TabContents::FileSelected(const FilePath& path,
2655 int index, void* params) {
2656 render_view_host()->FileSelected(path);
2657}
2658
2659void TabContents::MultiFilesSelected(const std::vector<FilePath>& files,
2660 void* params) {
2661 render_view_host()->MultiFilesSelected(files);
2662}
2663
2664void TabContents::FileSelectionCanceled(void* params) {
2665 // If the user cancels choosing a file to upload we pass back an
2666 // empty vector.
2667 render_view_host()->MultiFilesSelected(std::vector<FilePath>());
2668}
2669
2670void TabContents::BeforeUnloadFiredFromRenderManager(
2671 bool proceed,
2672 bool* proceed_to_fire_unload) {
2673 if (delegate())
2674 delegate()->BeforeUnloadFired(this, proceed, proceed_to_fire_unload);
2675}
2676
2677void TabContents::UpdateRenderViewSizeForRenderManager() {
2678 // TODO(brettw) this is a hack. See TabContentsView::SizeContents.
2679 view_->SizeContents(view_->GetContainerSize());
2680}
2681
2682DOMUI* TabContents::CreateDOMUIForRenderManager(const GURL& url) {
[email protected]d82ed61e2009-06-16 02:46:222683 return DOMUIFactory::CreateDOMUIForURL(this, url);
[email protected]420ae012009-04-24 05:16:322684}
2685
2686NavigationEntry*
2687TabContents::GetLastCommittedNavigationEntryForRenderManager() {
2688 return controller_.GetLastCommittedEntry();
2689}
2690
2691bool TabContents::CreateRenderViewForRenderManager(
2692 RenderViewHost* render_view_host) {
[email protected]811bfe32009-07-01 08:46:252693 // If the pending navigation is to a DOMUI, tell the RenderView about any
2694 // bindings it will need enabled.
[email protected]420ae012009-04-24 05:16:322695 if (render_manager_.pending_dom_ui())
[email protected]811bfe32009-07-01 08:46:252696 render_view_host->AllowBindings(
2697 render_manager_.pending_dom_ui()->bindings());
[email protected]1adff062009-06-02 18:39:552698
[email protected]420ae012009-04-24 05:16:322699 RenderWidgetHostView* rwh_view = view_->CreateViewForWidget(render_view_host);
[email protected]1a98a932009-11-17 00:12:522700
2701 scoped_refptr<URLRequestContextGetter> request_context = request_context_;
2702 if (!request_context.get())
2703 request_context = profile()->GetRequestContext();
2704
2705 if (!render_view_host->CreateRenderView(request_context))
[email protected]420ae012009-04-24 05:16:322706 return false;
2707
2708 // Now that the RenderView has been created, we need to tell it its size.
2709 rwh_view->SetSize(view_->GetContainerSize());
2710
2711 UpdateMaxPageIDIfNecessary(render_view_host->site_instance(),
2712 render_view_host);
2713 return true;
2714}
2715
2716void TabContents::Observe(NotificationType type,
2717 const NotificationSource& source,
2718 const NotificationDetails& details) {
2719 switch (type.value) {
2720 case NotificationType::BOOKMARK_MODEL_LOADED:
2721 // BookmarkModel finished loading, fall through to update starred state.
2722 case NotificationType::URLS_STARRED: {
2723 // Somewhere, a URL has been starred.
2724 // Ignore notifications for profiles other than our current one.
2725 Profile* source_profile = Source<Profile>(source).ptr();
2726 if (!source_profile || !source_profile->IsSameProfile(profile()))
2727 return;
2728
2729 UpdateStarredStateForCurrentURL();
2730 break;
2731 }
2732 case NotificationType::PREF_CHANGED: {
2733 std::wstring* pref_name_in = Details<std::wstring>(details).ptr();
2734 DCHECK(Source<PrefService>(source).ptr() == profile()->GetPrefs());
2735 if (*pref_name_in == prefs::kAlternateErrorPagesEnabled) {
2736 UpdateAlternateErrorPageURL();
2737 } else if (*pref_name_in == prefs::kDefaultCharset ||
2738 StartsWithASCII(WideToUTF8(*pref_name_in), "webkit.webprefs.", true)
2739 ) {
2740 UpdateWebPreferences();
2741 } else {
2742 NOTREACHED() << "unexpected pref change notification" << *pref_name_in;
2743 }
2744 break;
2745 }
2746 case NotificationType::RENDER_WIDGET_HOST_DESTROYED:
2747 view_->RenderWidgetHostDestroyed(Source<RenderWidgetHost>(source).ptr());
2748 break;
2749
2750 case NotificationType::NAV_ENTRY_COMMITTED: {
2751 DCHECK(&controller_ == Source<NavigationController>(source).ptr());
2752
2753 NavigationController::LoadCommittedDetails& committed_details =
2754 *(Details<NavigationController::LoadCommittedDetails>(details).ptr());
2755 ExpireInfoBars(committed_details);
2756 break;
2757 }
2758
[email protected]d3ba77272009-09-03 00:06:092759#if defined(OS_LINUX)
2760 case NotificationType::BROWSER_THEME_CHANGED: {
[email protected]7ff431e2010-01-07 18:18:542761 renderer_preferences_util::UpdateFromSystemSettings(
2762 &renderer_preferences_, profile());
[email protected]d3ba77272009-09-03 00:06:092763 render_view_host()->SyncRendererPrefs();
2764 break;
2765 }
2766#endif
2767
[email protected]79580c62010-02-02 02:36:252768 case NotificationType::CONTENT_SETTINGS_CHANGED: {
2769 Details<HostContentSettingsMap::ContentSettingsDetails>
2770 settings_details(details);
[email protected]79580c62010-02-02 02:36:252771 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]da0c8e92010-02-09 23:25:492772 GURL entry_url;
2773 std::string host;
2774 if (entry) {
2775 entry_url = entry->url();
2776 host = entry_url.host();
2777 }
[email protected]79580c62010-02-02 02:36:252778 Source<HostContentSettingsMap> content_settings(source);
2779 if (settings_details.ptr()->host().empty() ||
2780 settings_details.ptr()->host() == host) {
2781 render_view_host()->SendContentSettings(host,
[email protected]da0c8e92010-02-09 23:25:492782 content_settings.ptr()->GetContentSettings(entry_url));
[email protected]79580c62010-02-02 02:36:252783 }
2784 break;
2785 }
2786
[email protected]420ae012009-04-24 05:16:322787 default:
2788 NOTREACHED();
2789 }
2790}
[email protected]aacd7cc2009-09-02 14:51:102791
[email protected]beb440c2009-11-06 04:08:542792std::wstring TabContents::GetMessageBoxTitle(const GURL& frame_url,
2793 bool is_alert) {
2794 if (!frame_url.has_host())
2795 return l10n_util::GetString(
2796 is_alert ? IDS_JAVASCRIPT_ALERT_DEFAULT_TITLE
2797 : IDS_JAVASCRIPT_MESSAGEBOX_DEFAULT_TITLE);
2798
2799 // We really only want the scheme, hostname, and port.
2800 GURL::Replacements replacements;
2801 replacements.ClearUsername();
2802 replacements.ClearPassword();
2803 replacements.ClearPath();
2804 replacements.ClearQuery();
2805 replacements.ClearRef();
2806 GURL clean_url = frame_url.ReplaceComponents(replacements);
2807
2808 // TODO(brettw) it should be easier than this to do the correct language
2809 // handling without getting the accept language from the profile.
2810 std::wstring base_address = gfx::ElideUrl(clean_url, gfx::Font(), 0,
2811 profile()->GetPrefs()->GetString(prefs::kAcceptLanguages));
2812 // Force URL to have LTR directionality.
2813 if (l10n_util::GetTextDirection() == l10n_util::RIGHT_TO_LEFT)
2814 l10n_util::WrapStringWithLTRFormatting(&base_address);
2815
2816 return l10n_util::GetStringF(
2817 is_alert ? IDS_JAVASCRIPT_ALERT_TITLE : IDS_JAVASCRIPT_MESSAGEBOX_TITLE,
2818 base_address);
2819}
2820
2821gfx::NativeWindow TabContents::GetMessageBoxRootWindow() {
2822 return view_->GetTopLevelNativeWindow();
2823}
2824
2825void TabContents::OnMessageBoxClosed(IPC::Message* reply_msg,
2826 bool success,
2827 const std::wstring& prompt) {
2828 last_javascript_message_dismissal_ = base::TimeTicks::Now();
2829 if (is_showing_before_unload_dialog_ && !success) {
2830 // If a beforeunload dialog is canceled, we need to stop the throbber from
2831 // spinning, since we forced it to start spinning in Navigate.
2832 DidStopLoading();
2833
2834 tab_close_start_time_ = base::TimeTicks();
2835 }
2836 is_showing_before_unload_dialog_ = false;
2837 render_view_host()->JavaScriptMessageBoxClosed(reply_msg, success, prompt);
2838}
2839
2840void TabContents::SetSuppressMessageBoxes(bool suppress_message_boxes) {
2841 set_suppress_javascript_messages(suppress_message_boxes);
2842}
2843
[email protected]41fc0322009-09-04 22:23:402844void TabContents::set_encoding(const std::string& encoding) {
[email protected]aacd7cc2009-09-02 14:51:102845 encoding_ = CharacterEncoding::GetCanonicalEncodingNameByAliasName(encoding);
2846}