blob: 24fd51a38aa43d402b612b3fa90b8bc820a0a49d [file] [log] [blame]
[email protected]ce5c4502009-05-06 16:46:111// Copyright (c) 2009 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
[email protected]f3ec7742009-01-15 00:59:165#include "chrome/browser/tab_contents/tab_contents.h"
initial.commit09911bf2008-07-26 23:55:296
[email protected]a92b8642009-05-05 23:38:567#include "app/l10n_util.h"
[email protected]9929da92009-05-05 02:05:118#include "app/resource_bundle.h"
[email protected]420ae012009-04-24 05:16:329#include "base/file_version_info.h"
10#include "base/process_util.h"
[email protected]96d185d2009-04-24 03:28:5411#include "base/string16.h"
[email protected]996fd702009-09-04 19:12:3712#include "base/string_util.h"
[email protected]96d185d2009-04-24 03:28:5413#include "base/time.h"
[email protected]fdd61c62009-04-22 19:22:5714#include "chrome/browser/autofill_manager.h"
[email protected]66ba4932009-06-04 19:22:1315#include "chrome/browser/blocked_popup_container.h"
[email protected]420ae012009-04-24 05:16:3216#include "chrome/browser/bookmarks/bookmark_model.h"
[email protected]96d185d2009-04-24 03:28:5417#include "chrome/browser/browser_process.h"
initial.commit09911bf2008-07-26 23:55:2918#include "chrome/browser/cert_store.h"
[email protected]aacd7cc2009-09-02 14:51:1019#include "chrome/browser/character_encoding.h"
[email protected]96d185d2009-04-24 03:28:5420#include "chrome/browser/debugger/devtools_manager.h"
[email protected]420ae012009-04-24 05:16:3221#include "chrome/browser/dom_operation_notification_details.h"
[email protected]96d185d2009-04-24 03:28:5422#include "chrome/browser/dom_ui/dom_ui.h"
23#include "chrome/browser/dom_ui/dom_ui_factory.h"
[email protected]bcef0dc02009-02-28 00:35:0224#include "chrome/browser/download/download_item_model.h"
[email protected]420ae012009-04-24 05:16:3225#include "chrome/browser/download/download_manager.h"
26#include "chrome/browser/download/download_request_manager.h"
[email protected]e7eaedde2009-09-25 20:09:4927#include "chrome/browser/external_protocol_handler.h"
[email protected]0189bc722009-08-28 21:56:4828#include "chrome/browser/favicon_service.h"
[email protected]420ae012009-04-24 05:16:3229#include "chrome/browser/gears_integration.h"
30#include "chrome/browser/google_util.h"
[email protected]8897c382009-05-06 17:53:2631#include "chrome/browser/hung_renderer_dialog.h"
[email protected]420ae012009-04-24 05:16:3232#include "chrome/browser/jsmessage_box_handler.h"
33#include "chrome/browser/load_from_memory_cache_details.h"
34#include "chrome/browser/load_notification_details.h"
[email protected]ea0e80892009-08-22 00:36:0535#include "chrome/browser/modal_html_dialog_delegate.h"
[email protected]c8865482009-07-23 20:40:1036#include "chrome/browser/omnibox_search_hint.h"
[email protected]fdd61c62009-04-22 19:22:5737#include "chrome/browser/password_manager/password_manager.h"
38#include "chrome/browser/plugin_installer.h"
[email protected]ce560f82009-06-03 09:39:4439#include "chrome/browser/profile.h"
[email protected]96d185d2009-04-24 03:28:5440#include "chrome/browser/renderer_host/render_widget_host_view.h"
[email protected]fd694982009-07-03 17:32:3741#include "chrome/browser/renderer_host/resource_request_details.h"
[email protected]96d185d2009-04-24 03:28:5442#include "chrome/browser/renderer_host/web_cache_manager.h"
[email protected]bd1ad682009-05-15 22:19:1743#include "chrome/browser/tab_contents/infobar_delegate.h"
[email protected]7e383692009-06-12 19:14:5444#include "chrome/browser/tab_contents/interstitial_page.h"
[email protected]f3ec7742009-01-15 00:59:1645#include "chrome/browser/tab_contents/navigation_entry.h"
[email protected]de34a962009-06-26 23:22:4346#include "chrome/browser/tab_contents/provisional_load_details.h"
[email protected]f3ec7742009-01-15 00:59:1647#include "chrome/browser/tab_contents/tab_contents_delegate.h"
[email protected]fdd61c62009-04-22 19:22:5748#include "chrome/browser/tab_contents/tab_contents_view.h"
[email protected]58dca552009-06-17 00:35:0249#include "chrome/browser/tab_contents/thumbnail_generator.h"
[email protected]449478302009-06-09 20:04:2850#include "chrome/browser/thumbnail_store.h"
[email protected]420ae012009-04-24 05:16:3251#include "chrome/browser/search_engines/template_url_fetcher.h"
52#include "chrome/browser/search_engines/template_url_model.h"
[email protected]35f7d212009-04-29 21:19:2753#include "chrome/common/chrome_switches.h"
[email protected]bfd04a62009-02-01 18:16:5654#include "chrome/common/notification_service.h"
[email protected]ce5c4502009-05-06 16:46:1155#include "chrome/common/page_action.h"
initial.commit09911bf2008-07-26 23:55:2956#include "chrome/common/pref_names.h"
[email protected]1eb89e82008-08-15 12:27:0357#include "chrome/common/pref_service.h"
[email protected]420ae012009-04-24 05:16:3258#include "chrome/common/render_messages.h"
[email protected]1ee614862009-07-06 20:29:1859#include "chrome/common/renderer_preferences.h"
[email protected]96d185d2009-04-24 03:28:5460#include "chrome/common/url_constants.h"
[email protected]8897c382009-05-06 17:53:2661#include "grit/generated_resources.h"
[email protected]420ae012009-04-24 05:16:3262#include "grit/locale_settings.h"
63#include "net/base/mime_util.h"
64#include "net/base/net_errors.h"
[email protected]d686e812009-06-03 19:10:2965#include "net/base/net_util.h"
[email protected]420ae012009-04-24 05:16:3266#include "net/base/registry_controlled_domain.h"
[email protected]1ee614862009-07-06 20:29:1867#include "webkit/glue/password_form.h"
68#include "webkit/glue/webpreferences.h"
[email protected]3e45ba92009-02-20 21:09:0069
[email protected]304f8792009-08-19 21:28:0970#if defined(OS_CHROMEOS)
71// For GdkScreen
72#include <gdk/gdk.h>
[email protected]0ba5c5e2009-08-20 16:47:1373#endif // defined(OS_CHROMEOS)
74
75#if defined(OS_LINUX)
76#include "chrome/browser/gtk/create_application_shortcuts_dialog_gtk.h"
[email protected]d3ba77272009-09-03 00:06:0977#include "chrome/browser/gtk/gtk_theme_provider.h"
[email protected]0ba5c5e2009-08-20 16:47:1378#endif // defined(OS_LINUX)
79
[email protected]420ae012009-04-24 05:16:3280// Cross-Site Navigations
81//
82// If a TabContents is told to navigate to a different web site (as determined
83// by SiteInstance), it will replace its current RenderViewHost with a new
84// RenderViewHost dedicated to the new SiteInstance. This works as follows:
85//
86// - Navigate determines whether the destination is cross-site, and if so,
87// it creates a pending_render_view_host_ and moves into the PENDING
88// RendererState.
89// - The pending RVH is "suspended," so that no navigation messages are sent to
90// its renderer until the onbeforeunload JavaScript handler has a chance to
91// run in the current RVH.
92// - The pending RVH tells CrossSiteRequestManager (a thread-safe singleton)
93// that it has a pending cross-site request. ResourceDispatcherHost will
94// check for this when the response arrives.
95// - The current RVH runs its onbeforeunload handler. If it returns false, we
96// cancel all the pending logic and go back to NORMAL. Otherwise we allow
97// the pending RVH to send the navigation request to its renderer.
98// - ResourceDispatcherHost receives a ResourceRequest on the IO thread. It
99// checks CrossSiteRequestManager to see that the RVH responsible has a
100// pending cross-site request, and then installs a CrossSiteEventHandler.
101// - When RDH receives a response, the BufferedEventHandler determines whether
102// it is a download. If so, it sends a message to the new renderer causing
103// it to cancel the request, and the download proceeds in the download
104// thread. For now, we stay in a PENDING state (with a pending RVH) until
[email protected]57c6a652009-05-04 07:58:34105// the next DidNavigate event for this TabContents. This isn't ideal, but it
[email protected]420ae012009-04-24 05:16:32106// doesn't affect any functionality.
107// - After RDH receives a response and determines that it is safe and not a
108// download, it pauses the response to first run the old page's onunload
109// handler. It does this by asynchronously calling the OnCrossSiteResponse
[email protected]57c6a652009-05-04 07:58:34110// method of TabContents on the UI thread, which sends a ClosePage message
[email protected]420ae012009-04-24 05:16:32111// to the current RVH.
112// - Once the onunload handler is finished, a ClosePage_ACK message is sent to
113// the ResourceDispatcherHost, who unpauses the response. Data is then sent
114// to the pending RVH.
115// - The pending renderer sends a FrameNavigate message that invokes the
116// DidNavigate method. This replaces the current RVH with the
117// pending RVH and goes back to the NORMAL RendererState.
118
119namespace {
120
121// Amount of time we wait between when a key event is received and the renderer
122// is queried for its state and pushed to the NavigationEntry.
123const int kQueryStateDelay = 5000;
124
125const int kSyncWaitDelay = 40;
126
[email protected]304f8792009-08-19 21:28:09127#if defined(OS_CHROMEOS)
128// If a popup window is bigger than this fraction of the screen on chrome os,
129// turn it into a tab
130const float kMaxWidthFactor = 0.5;
131const float kMaxHeightFactor = 0.6;
132#endif
133
[email protected]420ae012009-04-24 05:16:32134// If another javascript message box is displayed within
135// kJavascriptMessageExpectedDelay of a previous javascript message box being
136// dismissed, display an option to suppress future message boxes from this
137// contents.
138const int kJavascriptMessageExpectedDelay = 1000;
139
[email protected]420ae012009-04-24 05:16:32140// The list of prefs we want to observe.
141const wchar_t* kPrefsToObserve[] = {
142 prefs::kAlternateErrorPagesEnabled,
143 prefs::kWebKitJavaEnabled,
144 prefs::kWebKitJavascriptEnabled,
145 prefs::kWebKitLoadsImagesAutomatically,
146 prefs::kWebKitPluginsEnabled,
147 prefs::kWebKitUsesUniversalDetector,
148 prefs::kWebKitSerifFontFamily,
149 prefs::kWebKitSansSerifFontFamily,
150 prefs::kWebKitFixedFontFamily,
151 prefs::kWebKitDefaultFontSize,
152 prefs::kWebKitDefaultFixedFontSize,
153 prefs::kDefaultCharset
154 // kWebKitStandardFontIsSerif needs to be added
155 // if we let users pick which font to use, serif or sans-serif when
156 // no font is specified or a CSS generic family (serif or sans-serif)
157 // is not specified.
158};
159
160const int kPrefsToObserveLength = arraysize(kPrefsToObserve);
161
[email protected]420ae012009-04-24 05:16:32162// Returns true if the entry's transition type is FORM_SUBMIT.
163bool IsFormSubmit(const NavigationEntry* entry) {
164 return (PageTransition::StripQualifier(entry->transition_type()) ==
165 PageTransition::FORM_SUBMIT);
166}
167
168#if defined(OS_WIN)
169
170BOOL CALLBACK InvalidateWindow(HWND hwnd, LPARAM lparam) {
171 // Note: erase is required to properly paint some widgets borders. This can
172 // be seen with textfields.
173 InvalidateRect(hwnd, NULL, TRUE);
174 return TRUE;
175}
176#endif
177
[email protected]056de2d2009-06-26 16:41:34178void MakeNavigateParams(const NavigationEntry& entry, bool reload,
179 ViewMsg_Navigate_Params* params) {
180 params->page_id = entry.page_id();
181 params->url = entry.url();
182 params->referrer = entry.referrer();
183 params->transition = entry.transition_type();
184 params->state = entry.content_state();
185 params->reload = reload;
186 params->request_time = base::Time::Now();
187}
188
[email protected]420ae012009-04-24 05:16:32189} // namespace
190
191// -----------------------------------------------------------------------------
192
[email protected]fdd61c62009-04-22 19:22:57193// static
194int TabContents::find_request_id_counter_ = -1;
195
[email protected]420ae012009-04-24 05:16:32196class TabContents::GearsCreateShortcutCallbackFunctor {
197 public:
198 explicit GearsCreateShortcutCallbackFunctor(TabContents* contents)
199 : contents_(contents) {}
200
201 void Run(const GearsShortcutData2& shortcut_data, bool success) {
202 if (contents_)
203 contents_->OnGearsCreateShortcutDone(shortcut_data, success);
204 delete this;
205 }
206 void Cancel() {
207 contents_ = NULL;
208 }
209
210 private:
211 TabContents* contents_;
212};
213
[email protected]420ae012009-04-24 05:16:32214TabContents::TabContents(Profile* profile,
215 SiteInstance* site_instance,
216 int routing_id,
[email protected]34ac70502009-09-25 17:07:23217 base::WaitableEvent* modal_dialog_event,
218 const TabContents* base_tab_contents)
[email protected]b680ad22009-04-15 23:19:42219 : delegate_(NULL),
[email protected]66ba4932009-06-04 19:22:13220 ALLOW_THIS_IN_INITIALIZER_LIST(controller_(this, profile)),
221 ALLOW_THIS_IN_INITIALIZER_LIST(view_(
[email protected]d82ed61e2009-06-16 02:46:22222 TabContentsView::Create(this))),
223 ALLOW_THIS_IN_INITIALIZER_LIST(render_manager_(this, this)),
[email protected]fdd61c62009-04-22 19:22:57224 property_bag_(),
225 registrar_(),
[email protected]d82ed61e2009-06-16 02:46:22226 ALLOW_THIS_IN_INITIALIZER_LIST(printing_(*this)),
[email protected]fdd61c62009-04-22 19:22:57227 save_package_(),
228 cancelable_consumer_(),
229 autofill_manager_(),
230 password_manager_(),
231 plugin_installer_(),
[email protected]d82ed61e2009-06-16 02:46:22232 ALLOW_THIS_IN_INITIALIZER_LIST(fav_icon_helper_(this)),
[email protected]fdd61c62009-04-22 19:22:57233 select_file_dialog_(),
234 pending_install_(),
[email protected]d5f942ba2008-09-26 19:30:34235 is_loading_(false),
initial.commit09911bf2008-07-26 23:55:29236 is_crashed_(false),
[email protected]d5f942ba2008-09-26 19:30:34237 waiting_for_response_(false),
[email protected]d5f942ba2008-09-26 19:30:34238 max_page_id_(-1),
[email protected]fdd61c62009-04-22 19:22:57239 current_load_start_(),
240 load_state_(net::LOAD_STATE_IDLE),
241 load_state_host_(),
[email protected]094e5b22009-09-25 04:23:56242 upload_size_(0),
243 upload_position_(0),
[email protected]fdd61c62009-04-22 19:22:57244 received_page_title_(false),
245 is_starred_(false),
246 contents_mime_type_(),
247 encoding_(),
[email protected]332af7732009-03-11 13:21:35248 blocked_popups_(NULL),
[email protected]fdd61c62009-04-22 19:22:57249 infobar_delegates_(),
[email protected]fdd61c62009-04-22 19:22:57250 find_ui_active_(false),
251 find_op_aborted_(false),
252 current_find_request_id_(find_request_id_counter_++),
253 find_text_(),
[email protected]e491f1c2009-05-22 20:28:12254 last_search_case_sensitive_(false),
255 last_search_prepopulate_text_(NULL),
256 last_search_result_(),
[email protected]fdd61c62009-04-22 19:22:57257 capturing_contents_(false),
258 is_being_destroyed_(false),
259 notify_disconnection_(false),
260 history_requests_(),
261#if defined(OS_WIN)
262 message_box_active_(CreateEvent(NULL, TRUE, FALSE, NULL)),
263#endif
264 last_javascript_message_dismissal_(),
[email protected]c2e74fe82009-09-03 17:57:44265 suppress_javascript_messages_(false),
266 opener_dom_ui_type_(DOMUIFactory::kNoDOMUI) {
[email protected]420ae012009-04-24 05:16:32267 pending_install_.page_id = 0;
268 pending_install_.callback_functor = NULL;
269
[email protected]16779842009-07-08 23:45:29270#if defined(OS_CHROMEOS)
[email protected]58dca552009-06-17 00:35:02271 // Make sure the thumbnailer is started before starting the render manager.
272 // The thumbnailer will want to listen for RVH creations, one of which will
273 // happen in RVHManager::Init.
274 ThumbnailGenerator* generator = g_browser_process->GetThumbnailGenerator();
275 if (generator)
276 generator->StartThumbnailing();
277#endif
278
[email protected]420ae012009-04-24 05:16:32279 render_manager_.Init(profile, site_instance, routing_id, modal_dialog_event);
280
[email protected]34ac70502009-09-25 17:07:23281 // We have the initial size of the view be based on the size of the passed in
282 // tab contents (normally a tab from the same window).
283 view_->CreateView(base_tab_contents ?
284 base_tab_contents->view()->GetContainerSize() : gfx::Size());
[email protected]420ae012009-04-24 05:16:32285
286 // Register for notifications about all interested prefs change.
287 PrefService* prefs = profile->GetPrefs();
288 if (prefs) {
289 for (int i = 0; i < kPrefsToObserveLength; ++i)
290 prefs->AddPrefObserver(kPrefsToObserve[i], this);
291 }
292
293 // Register for notifications about URL starredness changing on any profile.
294 registrar_.Add(this, NotificationType::URLS_STARRED,
295 NotificationService::AllSources());
296 registrar_.Add(this, NotificationType::BOOKMARK_MODEL_LOADED,
297 NotificationService::AllSources());
298 registrar_.Add(this, NotificationType::RENDER_WIDGET_HOST_DESTROYED,
299 NotificationService::AllSources());
[email protected]d3ba77272009-09-03 00:06:09300#if defined(OS_LINUX)
301 registrar_.Add(this, NotificationType::BROWSER_THEME_CHANGED,
302 NotificationService::AllSources());
303#endif
[email protected]420ae012009-04-24 05:16:32304
305 // Keep a global copy of the previous search string (if any).
306 static string16 global_last_search = string16();
[email protected]e491f1c2009-05-22 20:28:12307 last_search_prepopulate_text_ = &global_last_search;
[email protected]840b1512009-07-21 16:53:46308
[email protected]c8865482009-07-23 20:40:10309 // Set-up the showing of the omnibox search infobar if applicable.
310 if (OmniboxSearchHint::IsEnabled(profile))
311 omnibox_search_hint_.reset(new OmniboxSearchHint(this));
312
[email protected]840b1512009-07-21 16:53:46313 view_->InitRendererPrefs(&renderer_preferences_);
[email protected]332af7732009-03-11 13:21:35314}
initial.commit09911bf2008-07-26 23:55:29315
316TabContents::~TabContents() {
[email protected]420ae012009-04-24 05:16:32317 is_being_destroyed_ = true;
318
319 // We don't want any notifications while we're runnign our destructor.
320 registrar_.RemoveAll();
321
322 // Unregister the notifications of all observed prefs change.
323 PrefService* prefs = profile()->GetPrefs();
324 if (prefs) {
325 for (int i = 0; i < kPrefsToObserveLength; ++i)
326 prefs->RemovePrefObserver(kPrefsToObserve[i], this);
327 }
328
329 // Clean up subwindows like plugins and the find in page bar.
330 view_->OnContentsDestroy();
331
332 NotifyDisconnected();
[email protected]d82ed61e2009-06-16 02:46:22333 HungRendererDialog::HideForTabContents(this);
[email protected]420ae012009-04-24 05:16:32334
335 if (pending_install_.callback_functor)
336 pending_install_.callback_functor->Cancel();
337
338 // First cleanly close all child windows.
339 // TODO(mpcomplete): handle case if MaybeCloseChildWindows() already asked
340 // some of these to close. CloseWindows is async, so it might get called
341 // twice before it runs.
342 int size = static_cast<int>(child_windows_.size());
343 for (int i = size - 1; i >= 0; --i) {
344 ConstrainedWindow* window = child_windows_[i];
345 if (window)
346 window->CloseConstrainedWindow();
347 }
348
[email protected]66ba4932009-06-04 19:22:13349 if (blocked_popups_)
350 blocked_popups_->Destroy();
351
[email protected]420ae012009-04-24 05:16:32352 // Notify any observer that have a reference on this tab contents.
353 NotificationService::current()->Notify(
354 NotificationType::TAB_CONTENTS_DESTROYED,
355 Source<TabContents>(this),
356 NotificationService::NoDetails());
357
[email protected]1e5e74f2009-05-27 20:55:12358 // Notify any lasting InfobarDelegates that have not yet been removed that
359 // whatever infobar they were handling in this TabContents has closed,
360 // because the TabContents is going away entirely.
361 // This must happen after the TAB_CONTENTS_DESTROYED notification as the
362 // notification may trigger infobars calls that access their delegate. (and
363 // some implementations of InfoBarDelegate do delete themselves on
364 // InfoBarClosed()).
365 for (int i = 0; i < infobar_delegate_count(); ++i) {
366 InfoBarDelegate* delegate = GetInfoBarDelegateAt(i);
367 delegate->InfoBarClosed();
368 }
369 infobar_delegates_.clear();
370
[email protected]420ae012009-04-24 05:16:32371 // TODO(brettw) this should be moved to the view.
372#if defined(OS_WIN)
373 // If we still have a window handle, destroy it. GetNativeView can return
374 // NULL if this contents was part of a window that closed.
375 if (GetNativeView())
376 ::DestroyWindow(GetNativeView());
377#endif
initial.commit09911bf2008-07-26 23:55:29378}
379
[email protected]d5f942ba2008-09-26 19:30:34380// static
381void TabContents::RegisterUserPrefs(PrefService* prefs) {
[email protected]420ae012009-04-24 05:16:32382 prefs->RegisterBooleanPref(prefs::kAlternateErrorPagesEnabled, true);
383
384 WebPreferences pref_defaults;
385 prefs->RegisterBooleanPref(prefs::kWebKitJavascriptEnabled,
386 pref_defaults.javascript_enabled);
387 prefs->RegisterBooleanPref(prefs::kWebKitWebSecurityEnabled,
388 pref_defaults.web_security_enabled);
389 prefs->RegisterBooleanPref(
390 prefs::kWebKitJavascriptCanOpenWindowsAutomatically, true);
391 prefs->RegisterBooleanPref(prefs::kWebKitLoadsImagesAutomatically,
392 pref_defaults.loads_images_automatically);
393 prefs->RegisterBooleanPref(prefs::kWebKitPluginsEnabled,
394 pref_defaults.plugins_enabled);
395 prefs->RegisterBooleanPref(prefs::kWebKitDomPasteEnabled,
396 pref_defaults.dom_paste_enabled);
397 prefs->RegisterBooleanPref(prefs::kWebKitShrinksStandaloneImagesToFit,
398 pref_defaults.shrinks_standalone_images_to_fit);
[email protected]0df30122009-06-03 12:13:08399 prefs->RegisterStringPref(prefs::kWebKitInspectorSettings,
400 pref_defaults.inspector_settings);
[email protected]420ae012009-04-24 05:16:32401 prefs->RegisterBooleanPref(prefs::kWebKitTextAreasAreResizable,
402 pref_defaults.text_areas_are_resizable);
403 prefs->RegisterBooleanPref(prefs::kWebKitJavaEnabled,
404 pref_defaults.java_enabled);
405
406 prefs->RegisterLocalizedStringPref(prefs::kAcceptLanguages,
407 IDS_ACCEPT_LANGUAGES);
408 prefs->RegisterLocalizedStringPref(prefs::kDefaultCharset,
409 IDS_DEFAULT_ENCODING);
410 prefs->RegisterLocalizedBooleanPref(prefs::kWebKitStandardFontIsSerif,
411 IDS_STANDARD_FONT_IS_SERIF);
412 prefs->RegisterLocalizedStringPref(prefs::kWebKitFixedFontFamily,
413 IDS_FIXED_FONT_FAMILY);
414 prefs->RegisterLocalizedStringPref(prefs::kWebKitSerifFontFamily,
415 IDS_SERIF_FONT_FAMILY);
416 prefs->RegisterLocalizedStringPref(prefs::kWebKitSansSerifFontFamily,
417 IDS_SANS_SERIF_FONT_FAMILY);
418 prefs->RegisterLocalizedStringPref(prefs::kWebKitCursiveFontFamily,
419 IDS_CURSIVE_FONT_FAMILY);
420 prefs->RegisterLocalizedStringPref(prefs::kWebKitFantasyFontFamily,
421 IDS_FANTASY_FONT_FAMILY);
422 prefs->RegisterLocalizedIntegerPref(prefs::kWebKitDefaultFontSize,
423 IDS_DEFAULT_FONT_SIZE);
424 prefs->RegisterLocalizedIntegerPref(prefs::kWebKitDefaultFixedFontSize,
425 IDS_DEFAULT_FIXED_FONT_SIZE);
426 prefs->RegisterLocalizedIntegerPref(prefs::kWebKitMinimumFontSize,
427 IDS_MINIMUM_FONT_SIZE);
428 prefs->RegisterLocalizedIntegerPref(prefs::kWebKitMinimumLogicalFontSize,
429 IDS_MINIMUM_LOGICAL_FONT_SIZE);
430 prefs->RegisterLocalizedBooleanPref(prefs::kWebKitUsesUniversalDetector,
431 IDS_USES_UNIVERSAL_DETECTOR);
432 prefs->RegisterLocalizedStringPref(prefs::kStaticEncodings,
433 IDS_STATIC_ENCODING_LIST);
initial.commit09911bf2008-07-26 23:55:29434}
435
[email protected]13367f72009-07-08 16:39:02436// Returns true if contains content rendered by an extension.
437bool TabContents::HostsExtension() const {
438 return GetURL().SchemeIs(chrome::kExtensionScheme);
[email protected]d5f942ba2008-09-26 19:30:34439}
440
[email protected]6e95706d2009-04-23 22:55:01441PasswordManager* TabContents::GetPasswordManager() {
442 if (password_manager_.get() == NULL)
443 password_manager_.reset(new PasswordManager(this));
444 return password_manager_.get();
445}
446
447PluginInstaller* TabContents::GetPluginInstaller() {
448 if (plugin_installer_.get() == NULL)
449 plugin_installer_.reset(new PluginInstaller(this));
450 return plugin_installer_.get();
451}
452
[email protected]d5f942ba2008-09-26 19:30:34453const GURL& TabContents::GetURL() const {
454 // We may not have a navigation entry yet
[email protected]ce3fa3c2009-04-20 19:55:57455 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]ebe89e062009-08-13 23:16:54456 return entry ? entry->virtual_url() : GURL::EmptyGURL();
[email protected]d5f942ba2008-09-26 19:30:34457}
458
[email protected]96d185d2009-04-24 03:28:54459const string16& TabContents::GetTitle() const {
[email protected]4c6092c5b2009-06-06 00:23:55460 // Transient entries take precedence. They are used for interstitial pages
461 // that are shown on top of existing pages.
462 NavigationEntry* entry = controller_.GetTransientEntry();
463 if (entry)
464 return entry->GetTitleForDisplay(&controller_);
465
[email protected]96d185d2009-04-24 03:28:54466 DOMUI* our_dom_ui = render_manager_.pending_dom_ui() ?
467 render_manager_.pending_dom_ui() : render_manager_.dom_ui();
468 if (our_dom_ui) {
469 // Don't override the title in view source mode.
[email protected]4c6092c5b2009-06-06 00:23:55470 entry = controller_.GetActiveEntry();
[email protected]96d185d2009-04-24 03:28:54471 if (!(entry && entry->IsViewSourceMode())) {
472 // Give the DOM UI the chance to override our title.
473 const string16& title = our_dom_ui->overridden_title();
474 if (!title.empty())
475 return title;
476 }
477 }
478
479 // We use the title for the last committed entry rather than a pending
480 // navigation entry. For example, when the user types in a URL, we want to
481 // keep the old page's title until the new load has committed and we get a new
482 // title.
[email protected]96d185d2009-04-24 03:28:54483 entry = controller_.GetLastCommittedEntry();
484 if (entry)
485 return entry->GetTitleForDisplay(&controller_);
[email protected]96d185d2009-04-24 03:28:54486 return EmptyString16();
487}
488
[email protected]d5f942ba2008-09-26 19:30:34489int32 TabContents::GetMaxPageID() {
490 if (GetSiteInstance())
491 return GetSiteInstance()->max_page_id();
492 else
493 return max_page_id_;
494}
495
496void TabContents::UpdateMaxPageID(int32 page_id) {
497 // Ensure both the SiteInstance and RenderProcessHost update their max page
[email protected]57c6a652009-05-04 07:58:34498 // IDs in sync. Only TabContents will also have site instances, except during
[email protected]d5f942ba2008-09-26 19:30:34499 // testing.
500 if (GetSiteInstance())
501 GetSiteInstance()->UpdateMaxPageID(page_id);
[email protected]57c6a652009-05-04 07:58:34502 process()->UpdateMaxPageID(page_id);
[email protected]d5f942ba2008-09-26 19:30:34503}
504
[email protected]96d185d2009-04-24 03:28:54505SiteInstance* TabContents::GetSiteInstance() const {
506 return render_manager_.current_host()->site_instance();
507}
508
[email protected]d5f942ba2008-09-26 19:30:34509const std::wstring TabContents::GetDefaultTitle() const {
510 return l10n_util::GetString(IDS_DEFAULT_TAB_TITLE);
511}
512
[email protected]96d185d2009-04-24 03:28:54513bool TabContents::ShouldDisplayURL() {
514 // Don't hide the url in view source mode.
515 NavigationEntry* entry = controller_.GetActiveEntry();
516 if (entry && entry->IsViewSourceMode())
517 return true;
[email protected]83e3895b2009-06-11 00:07:16518
[email protected]96d185d2009-04-24 03:28:54519 DOMUI* dom_ui = GetDOMUIForCurrentState();
520 if (dom_ui)
521 return !dom_ui->should_hide_url();
522 return true;
523}
524
[email protected]d5f942ba2008-09-26 19:30:34525SkBitmap TabContents::GetFavIcon() const {
526 // Like GetTitle(), we also want to use the favicon for the last committed
527 // entry rather than a pending navigation entry.
[email protected]ce3fa3c2009-04-20 19:55:57528 NavigationEntry* entry = controller_.GetTransientEntry();
[email protected]cbab76d2008-10-13 22:42:47529 if (entry)
530 return entry->favicon().bitmap();
531
[email protected]ce3fa3c2009-04-20 19:55:57532 entry = controller_.GetLastCommittedEntry();
[email protected]d5f942ba2008-09-26 19:30:34533 if (entry)
534 return entry->favicon().bitmap();
[email protected]d5f942ba2008-09-26 19:30:34535 return SkBitmap();
536}
537
[email protected]96d185d2009-04-24 03:28:54538bool TabContents::ShouldDisplayFavIcon() {
539 // Always display a throbber during pending loads.
540 if (controller_.GetLastCommittedEntry() && controller_.pending_entry())
541 return true;
542
543 DOMUI* dom_ui = GetDOMUIForCurrentState();
544 if (dom_ui)
545 return !dom_ui->hide_favicon();
546 return true;
547}
548
[email protected]96d185d2009-04-24 03:28:54549std::wstring TabContents::GetStatusText() const {
550 if (!is_loading() || load_state_ == net::LOAD_STATE_IDLE)
551 return std::wstring();
552
553 switch (load_state_) {
554 case net::LOAD_STATE_WAITING_FOR_CACHE:
555 return l10n_util::GetString(IDS_LOAD_STATE_WAITING_FOR_CACHE);
556 case net::LOAD_STATE_RESOLVING_PROXY_FOR_URL:
557 return l10n_util::GetString(IDS_LOAD_STATE_RESOLVING_PROXY_FOR_URL);
558 case net::LOAD_STATE_RESOLVING_HOST:
559 return l10n_util::GetString(IDS_LOAD_STATE_RESOLVING_HOST);
560 case net::LOAD_STATE_CONNECTING:
561 return l10n_util::GetString(IDS_LOAD_STATE_CONNECTING);
562 case net::LOAD_STATE_SENDING_REQUEST:
[email protected]094e5b22009-09-25 04:23:56563 if (upload_size_)
564 return l10n_util::GetStringF(
565 IDS_LOAD_STATE_SENDING_REQUEST_WITH_PROGRESS,
566 static_cast<int>((100 * upload_position_) / upload_size_));
567 else
568 return l10n_util::GetString(IDS_LOAD_STATE_SENDING_REQUEST);
[email protected]96d185d2009-04-24 03:28:54569 case net::LOAD_STATE_WAITING_FOR_RESPONSE:
570 return l10n_util::GetStringF(IDS_LOAD_STATE_WAITING_FOR_RESPONSE,
571 load_state_host_);
572 // Ignore net::LOAD_STATE_READING_RESPONSE and net::LOAD_STATE_IDLE
573 case net::LOAD_STATE_IDLE:
574 case net::LOAD_STATE_READING_RESPONSE:
575 break;
576 }
577
578 return std::wstring();
579}
580
[email protected]d5f942ba2008-09-26 19:30:34581void TabContents::SetIsCrashed(bool state) {
582 if (state == is_crashed_)
583 return;
584
585 is_crashed_ = state;
[email protected]c9cd2222009-05-06 05:16:50586 NotifyNavigationStateChanged(INVALIDATE_TAB);
[email protected]d5f942ba2008-09-26 19:30:34587}
588
[email protected]671e6c1ce2009-09-26 03:18:46589void TabContents::SetPageActionEnabled(const ContextualAction* page_action,
[email protected]d7eaf572009-07-01 21:57:00590 bool enable,
591 const std::string& title,
592 int icon_id) {
[email protected]ce5c4502009-05-06 16:46:11593 DCHECK(page_action);
594
[email protected]d7eaf572009-07-01 21:57:00595 if (!enable &&
596 enabled_page_actions_.end() == enabled_page_actions_.find(page_action)) {
597 return; // Don't need to disable twice.
598 }
[email protected]ce5c4502009-05-06 16:46:11599
[email protected]d7eaf572009-07-01 21:57:00600 if (enable) {
601 enabled_page_actions_[page_action].reset(
[email protected]671e6c1ce2009-09-26 03:18:46602 new ContextualActionState(title, icon_id));
[email protected]d7eaf572009-07-01 21:57:00603 } else {
[email protected]f9b1a7b2009-06-22 17:26:38604 enabled_page_actions_.erase(page_action);
[email protected]d7eaf572009-07-01 21:57:00605 }
[email protected]ce5c4502009-05-06 16:46:11606}
607
[email protected]671e6c1ce2009-09-26 03:18:46608const ContextualActionState* TabContents::GetPageActionState(
609 const ContextualAction* page_action) {
[email protected]d7eaf572009-07-01 21:57:00610 if (enabled_page_actions_.end() == enabled_page_actions_.find(page_action))
611 return NULL;
[email protected]ce5c4502009-05-06 16:46:11612
[email protected]d7eaf572009-07-01 21:57:00613 return enabled_page_actions_[page_action].get();
614}
[email protected]ce5c4502009-05-06 16:46:11615
[email protected]d5f942ba2008-09-26 19:30:34616void TabContents::NotifyNavigationStateChanged(unsigned changed_flags) {
617 if (delegate_)
618 delegate_->NavigationStateChanged(this, changed_flags);
619}
620
[email protected]96d185d2009-04-24 03:28:54621void TabContents::DidBecomeSelected() {
622 controller_.SetActive(true);
[email protected]789e9152009-08-04 21:59:43623 if (render_widget_host_view()) {
[email protected]96d185d2009-04-24 03:28:54624 render_widget_host_view()->DidBecomeSelected();
[email protected]789e9152009-08-04 21:59:43625#if defined(OS_MACOSX)
626 render_widget_host_view()->SetActive(true);
627#endif
628 }
[email protected]96d185d2009-04-24 03:28:54629
[email protected]76543b92009-08-31 17:27:45630 WebCacheManager::GetInstance()->ObserveActivity(process()->id());
[email protected]96d185d2009-04-24 03:28:54631}
632
633void TabContents::WasHidden() {
634 if (!capturing_contents()) {
635 // |render_view_host()| can be NULL if the user middle clicks a link to open
636 // a tab in then background, then closes the tab before selecting it. This
637 // is because closing the tab calls TabContents::Destroy(), which removes
638 // the |render_view_host()|; then when we actually destroy the window,
639 // OnWindowPosChanged() notices and calls HideContents() (which calls us).
640 if (render_widget_host_view())
641 render_widget_host_view()->WasHidden();
[email protected]96d185d2009-04-24 03:28:54642 }
643
644 NotificationService::current()->Notify(
645 NotificationType::TAB_CONTENTS_HIDDEN,
646 Source<TabContents>(this),
647 NotificationService::NoDetails());
648}
649
[email protected]d5f942ba2008-09-26 19:30:34650void TabContents::Activate() {
651 if (delegate_)
652 delegate_->ActivateContents(this);
653}
654
[email protected]96d185d2009-04-24 03:28:54655void TabContents::ShowContents() {
656 if (render_widget_host_view())
657 render_widget_host_view()->DidBecomeSelected();
[email protected]96d185d2009-04-24 03:28:54658}
659
660void TabContents::HideContents() {
661 // TODO(pkasting): http://b/1239839 Right now we purposefully don't call
662 // our superclass HideContents(), because some callers want to be very picky
663 // about the order in which these get called. In addition to making the code
664 // here practically impossible to understand, this also means we end up
665 // calling TabContents::WasHidden() twice if callers call both versions of
[email protected]57c6a652009-05-04 07:58:34666 // HideContents() on a TabContents.
[email protected]96d185d2009-04-24 03:28:54667 WasHidden();
668}
669
[email protected]c0588052008-10-27 23:01:50670void TabContents::OpenURL(const GURL& url, const GURL& referrer,
[email protected]d5f942ba2008-09-26 19:30:34671 WindowOpenDisposition disposition,
672 PageTransition::Type transition) {
673 if (delegate_)
[email protected]c0588052008-10-27 23:01:50674 delegate_->OpenURLFromTab(this, url, referrer, disposition, transition);
[email protected]d5f942ba2008-09-26 19:30:34675}
676
677bool TabContents::NavigateToPendingEntry(bool reload) {
[email protected]96d185d2009-04-24 03:28:54678 const NavigationEntry& entry = *controller_.pending_entry();
679
680 RenderViewHost* dest_render_view_host = render_manager_.Navigate(entry);
681 if (!dest_render_view_host)
682 return false; // Unable to create the desired render view host.
683
684 // Tell DevTools agent that it is attached prior to the navigation.
[email protected]d9f9b792009-06-24 13:17:12685 DevToolsManager* devtools_manager = DevToolsManager::GetInstance();
[email protected]7e8e6b62009-05-08 11:28:32686 if (devtools_manager) { // NULL in unit tests.
[email protected]684b4ba22009-05-07 06:27:41687 devtools_manager->OnNavigatingToPendingEntry(
688 render_view_host(),
689 dest_render_view_host,
690 controller_.pending_entry()->url());
[email protected]7e8e6b62009-05-08 11:28:32691 }
[email protected]96d185d2009-04-24 03:28:54692
693 // Used for page load time metrics.
694 current_load_start_ = base::TimeTicks::Now();
695
696 // Navigate in the desired RenderViewHost.
[email protected]056de2d2009-06-26 16:41:34697 ViewMsg_Navigate_Params navigate_params;
698 MakeNavigateParams(entry, reload, &navigate_params);
699 dest_render_view_host->Navigate(navigate_params);
[email protected]96d185d2009-04-24 03:28:54700
701 if (entry.page_id() == -1) {
702 // HACK!! This code suppresses javascript: URLs from being added to
703 // session history, which is what we want to do for javascript: URLs that
704 // do not generate content. What we really need is a message from the
705 // renderer telling us that a new page was not created. The same message
706 // could be used for mailto: URLs and the like.
707 if (entry.url().SchemeIs(chrome::kJavaScriptScheme))
708 return false;
709 }
710
711 // Clear any provisional password saves - this stops password infobars
712 // showing up on pages the user navigates to while the right page is
713 // loading.
714 GetPasswordManager()->ClearProvisionalSave();
715
716 if (reload && !profile()->IsOffTheRecord()) {
[email protected]0189bc722009-08-28 21:56:48717 FaviconService* favicon_service =
718 profile()->GetFaviconService(Profile::IMPLICIT_ACCESS);
719 if (favicon_service)
720 favicon_service->SetFaviconOutOfDateForPage(entry.url());
[email protected]96d185d2009-04-24 03:28:54721 }
722
[email protected]d5f942ba2008-09-26 19:30:34723 return true;
724}
725
[email protected]96d185d2009-04-24 03:28:54726void TabContents::Stop() {
727 render_manager_.Stop();
728 printing_.Stop();
729}
730
731void TabContents::Cut() {
732 render_view_host()->Cut();
733}
734
735void TabContents::Copy() {
736 render_view_host()->Copy();
737}
738
[email protected]c0cc3092009-09-12 08:27:27739#if defined(OS_MACOSX)
[email protected]a954bf72009-09-12 07:30:35740void TabContents::CopyToFindPboard() {
741 render_view_host()->CopyToFindPboard();
742}
[email protected]c0cc3092009-09-12 08:27:27743#endif
[email protected]a954bf72009-09-12 07:30:35744
[email protected]96d185d2009-04-24 03:28:54745void TabContents::Paste() {
746 render_view_host()->Paste();
747}
748
749void TabContents::DisassociateFromPopupCount() {
750 render_view_host()->DisassociateFromPopupCount();
751}
752
753TabContents* TabContents::Clone() {
754 // We create a new SiteInstance so that the new tab won't share processes
755 // with the old one. This can be changed in the future if we need it to share
756 // processes for some reason.
[email protected]420ae012009-04-24 05:16:32757 TabContents* tc = new TabContents(profile(),
[email protected]96d185d2009-04-24 03:28:54758 SiteInstance::CreateSiteInstance(profile()),
[email protected]34ac70502009-09-25 17:07:23759 MSG_ROUTING_NONE, NULL, this);
[email protected]96d185d2009-04-24 03:28:54760 tc->controller().CopyStateFrom(controller_);
761 return tc;
762}
763
[email protected]420ae012009-04-24 05:16:32764void TabContents::CreateShortcut() {
765 NavigationEntry* entry = controller_.GetLastCommittedEntry();
766 if (!entry)
767 return;
768
[email protected]70b8d6b2009-08-20 16:56:24769#if defined(OS_LINUX) && !defined(TOOLKIT_VIEWS)
[email protected]0ba5c5e2009-08-20 16:47:13770 CreateApplicationShortcutsDialogGtk::Show(view()->GetTopLevelNativeWindow(),
771 GetURL(), GetTitle());
[email protected]b96aa932009-08-12 21:34:49772#else
[email protected]420ae012009-04-24 05:16:32773 // We only allow one pending install request. By resetting the page id we
774 // effectively cancel the pending install request.
775 pending_install_.page_id = entry->page_id();
776 pending_install_.icon = GetFavIcon();
777 pending_install_.title = UTF16ToWideHack(GetTitle());
778 pending_install_.url = GetURL();
779 if (pending_install_.callback_functor) {
780 pending_install_.callback_functor->Cancel();
781 pending_install_.callback_functor = NULL;
782 }
783 DCHECK(!pending_install_.icon.isNull()) << "Menu item should be disabled.";
784 if (pending_install_.title.empty())
785 pending_install_.title = UTF8ToWide(GetURL().spec());
786
787 // Request the application info. When done OnDidGetApplicationInfo is invoked
788 // and we'll create the shortcut.
789 render_view_host()->GetApplicationInfo(pending_install_.page_id);
[email protected]b96aa932009-08-12 21:34:49790#endif
[email protected]420ae012009-04-24 05:16:32791}
792
[email protected]4d677202009-07-19 07:37:12793void TabContents::ShowPageInfo(const GURL& url,
794 const NavigationEntry::SSLStatus& ssl,
795 bool show_history) {
796 if (!delegate_)
797 return;
798
[email protected]bb678332009-07-21 00:15:50799 delegate_->ShowPageInfo(profile(), url, ssl, show_history);
[email protected]4d677202009-07-19 07:37:12800}
801
initial.commit09911bf2008-07-26 23:55:29802ConstrainedWindow* TabContents::CreateConstrainedDialog(
[email protected]e8382172009-06-19 22:16:28803 ConstrainedWindowDelegate* delegate) {
initial.commit09911bf2008-07-26 23:55:29804 ConstrainedWindow* window =
[email protected]e8382172009-06-19 22:16:28805 ConstrainedWindow::CreateConstrainedDialog(this, delegate);
initial.commit09911bf2008-07-26 23:55:29806 child_windows_.push_back(window);
807 return window;
808}
809
810void TabContents::AddNewContents(TabContents* new_contents,
811 WindowOpenDisposition disposition,
812 const gfx::Rect& initial_pos,
[email protected]c88a70fe2009-05-05 20:00:22813 bool user_gesture,
814 const GURL& creator_url) {
initial.commit09911bf2008-07-26 23:55:29815 if (!delegate_)
816 return;
817
[email protected]35f7d212009-04-29 21:19:27818 if ((disposition == NEW_POPUP) && !user_gesture &&
819 !CommandLine::ForCurrentProcess()->HasSwitch(
820 switches::kDisablePopupBlocking)) {
[email protected]2c4410d2009-05-06 23:46:22821 // Unrequested popups from normal pages are constrained unless they're in
[email protected]b6c874582009-05-08 19:38:31822 // the whitelist. The popup owner will handle checking this.
823 delegate_->GetConstrainingContents(this)->AddPopup(new_contents,
824 initial_pos,
825 creator_url.is_valid() ? creator_url.host() : std::string());
826 } else {
[email protected]304f8792009-08-19 21:28:09827#if defined(OS_CHROMEOS)
828 if (disposition == NEW_POPUP) {
829 // If the popup is bigger than a given factor of the screen, then
830 // turn it into a foreground tab (on chrome os only)
831 GdkScreen* screen = gdk_screen_get_default();
832 int max_width = gdk_screen_get_width(screen) * kMaxWidthFactor;
833 int max_height = gdk_screen_get_height(screen) * kMaxHeightFactor;
834 if (initial_pos.width() > max_width ||
835 initial_pos.height() > max_height) {
836 disposition = NEW_FOREGROUND_TAB;
837 }
838 }
839#endif
840
[email protected]0aa55312008-10-17 21:53:08841 new_contents->DisassociateFromPopupCount();
initial.commit09911bf2008-07-26 23:55:29842 delegate_->AddNewContents(this, new_contents, disposition, initial_pos,
843 user_gesture);
[email protected]7f37dd92009-09-15 16:35:49844 NotificationService::current()->Notify(
845 NotificationType::TAB_ADDED,
846 Source<TabContentsDelegate>(delegate_),
847 Details<TabContents>(this));
initial.commit09911bf2008-07-26 23:55:29848 }
[email protected]b6c874582009-05-08 19:38:31849 PopupNotificationVisibilityChanged(ShowingBlockedPopupNotification());
initial.commit09911bf2008-07-26 23:55:29850}
851
initial.commit09911bf2008-07-26 23:55:29852void TabContents::CloseAllSuppressedPopups() {
[email protected]d6598c052008-11-05 19:03:25853 if (blocked_popups_)
[email protected]2c4410d2009-05-06 23:46:22854 blocked_popups_->CloseAll();
initial.commit09911bf2008-07-26 23:55:29855}
856
[email protected]912256b32009-09-18 09:47:35857void TabContents::ExecuteCode(int request_id, const std::string& extension_id,
858 bool is_js_code, const std::string& code_string) {
859 render_view_host()->Send(
860 new ViewMsg_ExecuteCode(render_view_host()->routing_id(), request_id,
861 extension_id, is_js_code, code_string));
862}
863
[email protected]96d185d2009-04-24 03:28:54864void TabContents::PopupNotificationVisibilityChanged(bool visible) {
865 render_view_host()->PopupNotificationVisibilityChanged(visible);
866}
867
[email protected]5c9e97a2009-09-09 23:48:30868gfx::NativeView TabContents::GetContentNativeView() const {
[email protected]96d185d2009-04-24 03:28:54869 return view_->GetContentNativeView();
870}
871
872gfx::NativeView TabContents::GetNativeView() const {
873 return view_->GetNativeView();
874}
875
876void TabContents::GetContainerBounds(gfx::Rect *out) const {
877 view_->GetContainerBounds(out);
878}
879
880void TabContents::Focus() {
881 view_->Focus();
882}
883
[email protected]90daadb42009-06-08 21:27:28884void TabContents::FocusThroughTabTraversal(bool reverse) {
[email protected]7e383692009-06-12 19:14:54885 if (showing_interstitial_page()) {
886 render_manager_.interstitial_page()->FocusThroughTabTraversal(reverse);
887 return;
888 }
[email protected]96d185d2009-04-24 03:28:54889 render_view_host()->SetInitialFocus(reverse);
890}
891
892bool TabContents::FocusLocationBarByDefault() {
893 DOMUI* dom_ui = GetDOMUIForCurrentState();
894 if (dom_ui)
895 return dom_ui->focus_location_bar_by_default();
[email protected]3e3f0eb2009-06-22 18:33:43896 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]e0d481582009-09-15 21:06:25897 if (entry && entry->url() == GURL(chrome::kAboutBlankURL))
[email protected]3e3f0eb2009-06-22 18:33:43898 return true;
[email protected]96d185d2009-04-24 03:28:54899 return false;
900}
901
[email protected]616ed5a2008-11-21 22:27:24902void TabContents::AddInfoBar(InfoBarDelegate* delegate) {
903 // Look through the existing InfoBarDelegates we have for a match. If we've
904 // already got one that matches, then we don't add the new one.
[email protected]f86a07022008-11-25 01:06:05905 for (int i = 0; i < infobar_delegate_count(); ++i) {
[email protected]616ed5a2008-11-21 22:27:24906 if (GetInfoBarDelegateAt(i)->EqualsDelegate(delegate))
907 return;
908 }
909
910 infobar_delegates_.push_back(delegate);
[email protected]bfd04a62009-02-01 18:16:56911 NotificationService::current()->Notify(
912 NotificationType::TAB_CONTENTS_INFOBAR_ADDED,
913 Source<TabContents>(this),
914 Details<InfoBarDelegate>(delegate));
[email protected]616ed5a2008-11-21 22:27:24915
916 // Add ourselves as an observer for navigations the first time a delegate is
917 // added. We use this notification to expire InfoBars that need to expire on
918 // page transitions.
919 if (infobar_delegates_.size() == 1) {
[email protected]bfd04a62009-02-01 18:16:56920 registrar_.Add(this, NotificationType::NAV_ENTRY_COMMITTED,
[email protected]ce3fa3c2009-04-20 19:55:57921 Source<NavigationController>(&controller_));
[email protected]616ed5a2008-11-21 22:27:24922 }
923}
924
925void TabContents::RemoveInfoBar(InfoBarDelegate* delegate) {
926 std::vector<InfoBarDelegate*>::iterator it =
927 find(infobar_delegates_.begin(), infobar_delegates_.end(), delegate);
928 if (it != infobar_delegates_.end()) {
929 InfoBarDelegate* delegate = *it;
[email protected]bfd04a62009-02-01 18:16:56930 NotificationService::current()->Notify(
931 NotificationType::TAB_CONTENTS_INFOBAR_REMOVED,
932 Source<TabContents>(this),
933 Details<InfoBarDelegate>(delegate));
[email protected]f86a07022008-11-25 01:06:05934 infobar_delegates_.erase(it);
[email protected]616ed5a2008-11-21 22:27:24935
[email protected]6a02963e2009-01-06 16:58:03936 // Remove ourselves as an observer if we are tracking no more InfoBars.
937 if (infobar_delegates_.empty()) {
[email protected]bfd04a62009-02-01 18:16:56938 registrar_.Remove(this, NotificationType::NAV_ENTRY_COMMITTED,
[email protected]ce3fa3c2009-04-20 19:55:57939 Source<NavigationController>(&controller_));
[email protected]6a02963e2009-01-06 16:58:03940 }
[email protected]616ed5a2008-11-21 22:27:24941 }
942}
[email protected]3edd9522009-03-04 22:19:41943
[email protected]f81821c2009-08-07 01:29:47944void TabContents::ReplaceInfoBar(InfoBarDelegate* old_delegate,
945 InfoBarDelegate* new_delegate) {
946 std::vector<InfoBarDelegate*>::iterator it =
947 find(infobar_delegates_.begin(), infobar_delegates_.end(), old_delegate);
948 DCHECK(it != infobar_delegates_.end());
949
950 // Notify the container about the change of plans.
951 scoped_ptr<std::pair<InfoBarDelegate*, InfoBarDelegate*> > details(
952 new std::pair<InfoBarDelegate*, InfoBarDelegate*>(
953 old_delegate, new_delegate));
954 NotificationService::current()->Notify(
955 NotificationType::TAB_CONTENTS_INFOBAR_REPLACED,
956 Source<TabContents>(this),
957 Details<std::pair<InfoBarDelegate*, InfoBarDelegate*> >(details.get()));
958
959 // Remove the old one.
960 infobar_delegates_.erase(it);
961
962 // Add the new one.
963 infobar_delegates_.push_back(new_delegate);
964}
965
[email protected]96d185d2009-04-24 03:28:54966bool TabContents::IsBookmarkBarAlwaysVisible() {
967 // See GetDOMUIForCurrentState() comment for more info. This case is very
968 // similar, but for non-first loads, we want to use the committed entry. This
969 // is so the bookmarks bar disappears at the same time the page does.
970 if (controller_.GetLastCommittedEntry()) {
971 // Not the first load, always use the committed DOM UI.
972 if (render_manager_.dom_ui())
973 return render_manager_.dom_ui()->force_bookmark_bar_visible();
974 return false; // Default.
975 }
976
977 // When it's the first load, we know either the pending one or the committed
978 // one will have the DOM UI in it (see GetDOMUIForCurrentState), and only one
979 // of them will be valid, so we can just check both.
980 if (render_manager_.pending_dom_ui())
981 return render_manager_.pending_dom_ui()->force_bookmark_bar_visible();
982 if (render_manager_.dom_ui())
983 return render_manager_.dom_ui()->force_bookmark_bar_visible();
984 return false; // Default.
985}
986
[email protected]d8f16ae2009-08-30 03:47:26987bool TabContents::IsExtensionShelfAlwaysVisible() {
988 // See GetDOMUIForCurrentState() comment for more info. This case is very
989 // similar, but for non-first loads, we want to use the committed entry. This
990 // is so the bookmarks bar disappears at the same time the page does.
991 if (controller_.GetLastCommittedEntry()) {
992 // Not the first load, always use the committed DOM UI.
993 if (render_manager_.dom_ui())
994 return render_manager_.dom_ui()->force_extension_shelf_visible();
995 return false; // Default.
996 }
997
998 // When it's the first load, we know either the pending one or the committed
999 // one will have the DOM UI in it (see GetDOMUIForCurrentState), and only one
1000 // of them will be valid, so we can just check both.
1001 if (render_manager_.pending_dom_ui())
1002 return render_manager_.pending_dom_ui()->force_extension_shelf_visible();
1003 if (render_manager_.dom_ui())
1004 return render_manager_.dom_ui()->force_extension_shelf_visible();
1005 return false; // Default.
1006}
1007
[email protected]3edd9522009-03-04 22:19:411008void TabContents::ToolbarSizeChanged(bool is_animating) {
1009 TabContentsDelegate* d = delegate();
1010 if (d)
1011 d->ToolbarSizeChanged(this, is_animating);
1012}
[email protected]616ed5a2008-11-21 22:27:241013
[email protected]d5f942ba2008-09-26 19:30:341014void TabContents::OnStartDownload(DownloadItem* download) {
1015 DCHECK(download);
[email protected]d5f942ba2008-09-26 19:30:341016
1017 // Download in a constrained popup is shown in the tab that opened it.
[email protected]b6c874582009-05-08 19:38:311018 TabContents* tab_contents = delegate()->GetConstrainingContents(this);
[email protected]d5f942ba2008-09-26 19:30:341019
[email protected]59560e0b2009-06-04 03:30:221020 if (tab_contents && tab_contents->delegate())
1021 tab_contents->delegate()->OnStartDownload(download);
[email protected]3edd9522009-03-04 22:19:411022}
[email protected]3edd9522009-03-04 22:19:411023
[email protected]d5f942ba2008-09-26 19:30:341024void TabContents::WillClose(ConstrainedWindow* window) {
1025 ConstrainedWindowList::iterator it =
1026 find(child_windows_.begin(), child_windows_.end(), window);
1027 if (it != child_windows_.end())
1028 child_windows_.erase(it);
[email protected]66ba4932009-06-04 19:22:131029}
[email protected]d5f942ba2008-09-26 19:30:341030
[email protected]66ba4932009-06-04 19:22:131031void TabContents::WillCloseBlockedPopupContainer(
1032 BlockedPopupContainer* container) {
1033 DCHECK(blocked_popups_ == container);
1034 blocked_popups_ = NULL;
[email protected]d5f942ba2008-09-26 19:30:341035}
1036
[email protected]d5f942ba2008-09-26 19:30:341037void TabContents::DidMoveOrResize(ConstrainedWindow* window) {
[email protected]332af7732009-03-11 13:21:351038#if defined(OS_WIN)
[email protected]92edc472009-02-10 20:32:061039 UpdateWindow(GetNativeView());
[email protected]332af7732009-03-11 13:21:351040#endif
[email protected]d5f942ba2008-09-26 19:30:341041}
1042
[email protected]e491f1c2009-05-22 20:28:121043void TabContents::StartFinding(string16 find_text,
1044 bool forward_direction,
1045 bool case_sensitive) {
[email protected]6e95706d2009-04-23 22:55:011046 // If find_text is empty, it means FindNext was pressed with a keyboard
1047 // shortcut so unless we have something to search for we return early.
[email protected]e491f1c2009-05-22 20:28:121048 if (find_text.empty() && find_text_.empty()) {
1049 if (last_search_prepopulate_text_->empty())
1050 return;
1051 // Try whatever we searched for last in any tab.
1052 find_text = *last_search_prepopulate_text_;
1053 }
[email protected]6e95706d2009-04-23 22:55:011054
1055 // This is a FindNext operation if we are searching for the same text again,
1056 // or if the passed in search text is empty (FindNext keyboard shortcut). The
1057 // exception to this is if the Find was aborted (then we don't want FindNext
1058 // because the highlighting has been cleared and we need it to reappear). We
1059 // therefore treat FindNext after an aborted Find operation as a full fledged
1060 // Find.
1061 bool find_next = (find_text_ == find_text || find_text.empty()) &&
[email protected]e491f1c2009-05-22 20:28:121062 (last_search_case_sensitive_ == case_sensitive) &&
[email protected]6e95706d2009-04-23 22:55:011063 !find_op_aborted_;
1064 if (!find_next)
1065 current_find_request_id_ = find_request_id_counter_++;
1066
1067 if (!find_text.empty())
1068 find_text_ = find_text;
[email protected]e491f1c2009-05-22 20:28:121069 last_search_case_sensitive_ = case_sensitive;
[email protected]6e95706d2009-04-23 22:55:011070
1071 find_op_aborted_ = false;
1072
1073 // Keep track of what the last search was across the tabs.
[email protected]e491f1c2009-05-22 20:28:121074 *last_search_prepopulate_text_ = find_text;
[email protected]6e95706d2009-04-23 22:55:011075
1076 render_view_host()->StartFinding(current_find_request_id_,
1077 find_text_,
1078 forward_direction,
[email protected]e491f1c2009-05-22 20:28:121079 case_sensitive,
[email protected]6e95706d2009-04-23 22:55:011080 find_next);
1081}
1082
1083void TabContents::StopFinding(bool clear_selection) {
[email protected]2d2dffc2009-06-09 21:41:001084 // When |clear_selection| is true, it means the find string has been cleared
1085 // by the user, but the UI has not been dismissed.
1086 if (!clear_selection)
1087 find_ui_active_ = false;
[email protected]6e95706d2009-04-23 22:55:011088 find_op_aborted_ = true;
[email protected]e491f1c2009-05-22 20:28:121089 last_search_result_ = FindNotificationDetails();
[email protected]6e95706d2009-04-23 22:55:011090 render_view_host()->StopFinding(clear_selection);
1091}
1092
[email protected]5c4266922009-07-10 16:41:271093void TabContents::GetPageLanguage() {
1094 render_view_host()->GetPageLanguage();
1095}
1096
[email protected]420ae012009-04-24 05:16:321097void TabContents::OnJavaScriptMessageBoxClosed(IPC::Message* reply_msg,
1098 bool success,
1099 const std::wstring& prompt) {
1100 last_javascript_message_dismissal_ = base::TimeTicks::Now();
1101 render_manager_.OnJavaScriptMessageBoxClosed(reply_msg, success, prompt);
1102}
1103
[email protected]571e31152009-05-15 19:34:021104void TabContents::OnJavaScriptMessageBoxWindowDestroyed() {
1105 render_manager_.OnJavaScriptMessageBoxWindowDestroyed();
1106}
1107
[email protected]420ae012009-04-24 05:16:321108void TabContents::OnSavePage() {
1109 // If we can not save the page, try to download it.
1110 if (!SavePackage::IsSavableContents(contents_mime_type())) {
1111 DownloadManager* dlm = profile()->GetDownloadManager();
1112 const GURL& current_page_url = GetURL();
1113 if (dlm && current_page_url.is_valid())
[email protected]d82ed61e2009-06-16 02:46:221114 dlm->DownloadUrl(current_page_url, GURL(), "", this);
[email protected]420ae012009-04-24 05:16:321115 return;
1116 }
1117
1118 Stop();
1119
1120 // Create the save package and possibly prompt the user for the name to save
1121 // the page as. The user prompt is an asynchronous operation that runs on
1122 // another thread.
[email protected]d82ed61e2009-06-16 02:46:221123 save_package_ = new SavePackage(this);
[email protected]420ae012009-04-24 05:16:321124 save_package_->GetSaveInfo();
1125}
1126
1127// Used in automated testing to bypass prompting the user for file names.
1128// Instead, the names and paths are hard coded rather than running them through
1129// file name sanitation and extension / mime checking.
[email protected]cdf6cc82009-08-04 21:48:591130bool TabContents::SavePage(const std::wstring& main_file,
[email protected]420ae012009-04-24 05:16:321131 const std::wstring& dir_path,
1132 SavePackage::SavePackageType save_type) {
1133 // Stop the page from navigating.
1134 Stop();
1135
[email protected]d82ed61e2009-06-16 02:46:221136 save_package_ = new SavePackage(this, save_type,
[email protected]420ae012009-04-24 05:16:321137 FilePath::FromWStringHack(main_file),
1138 FilePath::FromWStringHack(dir_path));
[email protected]cdf6cc82009-08-04 21:48:591139 return save_package_->Init();
[email protected]420ae012009-04-24 05:16:321140}
1141
1142void TabContents::PrintPreview() {
1143 // We don't show the print preview yet, only the print dialog.
1144 PrintNow();
1145}
1146
1147bool TabContents::PrintNow() {
1148 // We can't print interstitial page for now.
1149 if (showing_interstitial_page())
1150 return false;
1151
1152 return render_view_host()->PrintPages();
1153}
1154
[email protected]82270452009-06-19 15:58:011155void TabContents::PrintingDone(int document_cookie, bool success) {
1156 render_view_host()->PrintingDone(document_cookie, success);
1157}
1158
[email protected]420ae012009-04-24 05:16:321159bool TabContents::IsActiveEntry(int32 page_id) {
1160 NavigationEntry* active_entry = controller_.GetActiveEntry();
1161 return (active_entry != NULL &&
1162 active_entry->site_instance() == GetSiteInstance() &&
1163 active_entry->page_id() == page_id);
1164}
1165
[email protected]96d185d2009-04-24 03:28:541166// Notifies the RenderWidgetHost instance about the fact that the page is
1167// loading, or done loading and calls the base implementation.
1168void TabContents::SetIsLoading(bool is_loading,
1169 LoadNotificationDetails* details) {
1170 if (is_loading == is_loading_)
1171 return;
1172
1173 if (!is_loading) {
1174 load_state_ = net::LOAD_STATE_IDLE;
1175 load_state_host_.clear();
[email protected]094e5b22009-09-25 04:23:561176 upload_size_ = 0;
1177 upload_position_ = 0;
[email protected]96d185d2009-04-24 03:28:541178 }
1179
1180 render_manager_.SetIsLoading(is_loading);
1181
1182 is_loading_ = is_loading;
1183 waiting_for_response_ = is_loading;
1184
1185 if (delegate_)
1186 delegate_->LoadingStateChanged(this);
[email protected]329581b2009-04-28 06:52:351187 NotifyNavigationStateChanged(INVALIDATE_LOAD);
[email protected]96d185d2009-04-24 03:28:541188
1189 NotificationType type = is_loading ? NotificationType::LOAD_START :
1190 NotificationType::LOAD_STOP;
[email protected]ce5c4502009-05-06 16:46:111191 NotificationDetails det = NotificationService::NoDetails();
[email protected]96d185d2009-04-24 03:28:541192 if (details)
1193 det = Details<LoadNotificationDetails>(details);
1194 NotificationService::current()->Notify(type,
1195 Source<NavigationController>(&controller_),
1196 det);
1197}
1198
[email protected]2c4410d2009-05-06 23:46:221199void TabContents::CreateBlockedPopupContainerIfNecessary() {
1200 if (blocked_popups_)
1201 return;
1202
[email protected]4d4c0b92009-06-08 23:13:011203 blocked_popups_ = BlockedPopupContainer::Create(this, profile());
[email protected]2c4410d2009-05-06 23:46:221204}
1205
[email protected]b6c874582009-05-08 19:38:311206void TabContents::AddPopup(TabContents* new_contents,
1207 const gfx::Rect& initial_pos,
1208 const std::string& host) {
[email protected]2c4410d2009-05-06 23:46:221209 CreateBlockedPopupContainerIfNecessary();
1210 blocked_popups_->AddTabContents(new_contents, initial_pos, host);
[email protected]2c4410d2009-05-06 23:46:221211}
1212
[email protected]e69bce3dd2009-04-20 22:05:121213// TODO(brettw) This should be on the TabContentsView.
[email protected]66ba4932009-06-04 19:22:131214void TabContents::RepositionSupressedPopupsToFit() {
[email protected]d6598c052008-11-05 19:03:251215 if (blocked_popups_)
[email protected]4d4c0b92009-06-08 23:13:011216 blocked_popups_->RepositionBlockedPopupContainer();
[email protected]d5f942ba2008-09-26 19:30:341217}
1218
[email protected]b9681312008-11-07 00:08:261219bool TabContents::ShowingBlockedPopupNotification() const {
1220 return blocked_popups_ != NULL &&
[email protected]2c4410d2009-05-06 23:46:221221 blocked_popups_->GetBlockedPopupCount() != 0;
[email protected]b9681312008-11-07 00:08:261222}
[email protected]616ed5a2008-11-21 22:27:241223
1224namespace {
1225bool TransitionIsReload(PageTransition::Type transition) {
1226 return PageTransition::StripQualifier(transition) == PageTransition::RELOAD;
1227}
1228}
1229
1230void TabContents::ExpireInfoBars(
1231 const NavigationController::LoadCommittedDetails& details) {
1232 // Only hide InfoBars when the user has done something that makes the main
1233 // frame load. We don't want various automatic or subframe navigations making
1234 // it disappear.
1235 if (!details.is_user_initiated_main_frame_load())
1236 return;
1237
[email protected]f86a07022008-11-25 01:06:051238 for (int i = infobar_delegate_count() - 1; i >= 0; --i) {
[email protected]616ed5a2008-11-21 22:27:241239 InfoBarDelegate* delegate = GetInfoBarDelegateAt(i);
[email protected]f86a07022008-11-25 01:06:051240 if (delegate->ShouldExpire(details))
[email protected]616ed5a2008-11-21 22:27:241241 RemoveInfoBar(delegate);
[email protected]616ed5a2008-11-21 22:27:241242 }
1243}
[email protected]fdd61c62009-04-22 19:22:571244
1245void TabContents::OnGearsCreateShortcutDone(
1246 const GearsShortcutData2& shortcut_data, bool success) {
1247 NavigationEntry* current_entry = controller_.GetLastCommittedEntry();
1248 bool same_page =
1249 current_entry && pending_install_.page_id == current_entry->page_id();
1250
1251 if (success && same_page) {
1252 // Only switch to app mode if the user chose to create a shortcut and
1253 // we're still on the same page that it corresponded to.
1254 if (delegate())
1255 delegate()->ConvertContentsToApplication(this);
1256 }
1257
1258 // Reset the page id to indicate no requests are pending.
1259 pending_install_.page_id = 0;
1260 pending_install_.callback_functor = NULL;
1261}
[email protected]96d185d2009-04-24 03:28:541262
1263DOMUI* TabContents::GetDOMUIForCurrentState() {
1264 // When there is a pending navigation entry, we want to use the pending DOMUI
1265 // that goes along with it to control the basic flags. For example, we want to
1266 // show the pending URL in the URL bar, so we want the display_url flag to
1267 // be from the pending entry.
1268 //
1269 // The confusion comes because there are multiple possibilities for the
1270 // initial load in a tab as a side effect of the way the RenderViewHostManager
1271 // works.
1272 //
1273 // - For the very first tab the load looks "normal". The new tab DOM UI is
1274 // the pending one, and we want it to apply here.
1275 //
1276 // - For subsequent new tabs, they'll get a new SiteInstance which will then
1277 // get switched to the one previously associated with the new tab pages.
1278 // This switching will cause the manager to commit the RVH/DOMUI. So we'll
1279 // have a committed DOM UI in this case.
1280 //
1281 // This condition handles all of these cases:
1282 //
1283 // - First load in first tab: no committed nav entry + pending nav entry +
1284 // pending dom ui:
1285 // -> Use pending DOM UI if any.
1286 //
1287 // - First load in second tab: no committed nav entry + pending nav entry +
1288 // no pending DOM UI:
1289 // -> Use the committed DOM UI if any.
1290 //
1291 // - Second navigation in any tab: committed nav entry + pending nav entry:
1292 // -> Use pending DOM UI if any.
1293 //
1294 // - Normal state with no load: committed nav entry + no pending nav entry:
1295 // -> Use committed DOM UI.
1296 if (controller_.pending_entry() &&
1297 (controller_.GetLastCommittedEntry() ||
1298 render_manager_.pending_dom_ui()))
1299 return render_manager_.pending_dom_ui();
1300 return render_manager_.dom_ui();
1301}
[email protected]420ae012009-04-24 05:16:321302
1303void TabContents::DidNavigateMainFramePostCommit(
1304 const NavigationController::LoadCommittedDetails& details,
1305 const ViewHostMsg_FrameNavigate_Params& params) {
[email protected]c2e74fe82009-09-03 17:57:441306 if (opener_dom_ui_type_ != DOMUIFactory::kNoDOMUI) {
1307 // If this is a window.open navigation, use the same DOMUI as the renderer
1308 // that opened the window, as long as both renderers have the same
1309 // privileges.
1310 if (opener_dom_ui_type_ == DOMUIFactory::GetDOMUIType(GetURL())) {
1311 DOMUI* dom_ui = DOMUIFactory::CreateDOMUIForURL(this, GetURL());
1312 // dom_ui might be NULL if the URL refers to a non-existent extension.
1313 if (dom_ui) {
1314 render_manager_.SetDOMUIPostCommit(dom_ui);
1315 dom_ui->RenderViewCreated(render_view_host());
1316 }
1317 }
1318 opener_dom_ui_type_ = DOMUIFactory::kNoDOMUI;
1319 }
1320
[email protected]420ae012009-04-24 05:16:321321 if (details.is_user_initiated_main_frame_load()) {
1322 // Clear the status bubble. This is a workaround for a bug where WebKit
1323 // doesn't let us know that the cursor left an element during a
1324 // transition (this is also why the mouse cursor remains as a hand after
1325 // clicking on a link); see bugs 1184641 and 980803. We don't want to
1326 // clear the bubble when a user navigates to a named anchor in the same
1327 // page.
1328 UpdateTargetURL(details.entry->page_id(), GURL());
1329
1330 // UpdateHelpersForDidNavigate will handle the case where the password_form
1331 // origin is valid.
1332 // TODO(brettw) bug 1343111: Password manager stuff in here needs to be
1333 // cleaned up and covered by tests.
1334 if (!params.password_form.origin.is_valid())
1335 GetPasswordManager()->DidNavigate();
1336 }
1337
1338 // The keyword generator uses the navigation entries, so must be called after
1339 // the commit.
1340 GenerateKeywordIfNecessary(params);
1341
1342 // Allow the new page to set the title again.
1343 received_page_title_ = false;
1344
1345 // Get the favicon, either from history or request it from the net.
1346 fav_icon_helper_.FetchFavIcon(details.entry->url());
1347
[email protected]aece2c7f2009-08-27 20:43:171348 // Disable all page actions, unless this is an in-page navigation.
1349 if (!enabled_page_actions_.empty()) {
1350 url_canon::Replacements<char> replacements;
1351 replacements.ClearRef();
1352 if (params.url.ReplaceComponents(replacements) !=
1353 params.referrer.ReplaceComponents(replacements)) {
1354 enabled_page_actions_.clear();
1355 }
1356 }
[email protected]ce5c4502009-05-06 16:46:111357
[email protected]420ae012009-04-24 05:16:321358 // Close constrained popups if necessary.
1359 MaybeCloseChildWindows(details.previous_url, details.entry->url());
1360
1361 // Update the starred state.
1362 UpdateStarredStateForCurrentURL();
1363}
1364
1365void TabContents::DidNavigateAnyFramePostCommit(
1366 RenderViewHost* render_view_host,
1367 const NavigationController::LoadCommittedDetails& details,
1368 const ViewHostMsg_FrameNavigate_Params& params) {
1369 // If we navigate, start showing messages again. This does nothing to prevent
1370 // a malicious script from spamming messages, since the script could just
1371 // reload the page to stop blocking.
1372 suppress_javascript_messages_ = false;
1373
[email protected]420ae012009-04-24 05:16:321374 // Notify the password manager of the navigation or form submit.
1375 // TODO(brettw) bug 1343111: Password manager stuff in here needs to be
1376 // cleaned up and covered by tests.
1377 if (params.password_form.origin.is_valid())
1378 GetPasswordManager()->ProvisionallySavePassword(params.password_form);
1379}
1380
1381void TabContents::MaybeCloseChildWindows(const GURL& previous_url,
1382 const GURL& current_url) {
1383 if (net::RegistryControlledDomainService::SameDomainOrHost(
1384 previous_url, current_url))
1385 return;
1386
1387 // Clear out any child windows since we are leaving this page entirely.
1388 // We use indices instead of iterators in case CloseWindow does something
1389 // that may invalidate an iterator.
1390 int size = static_cast<int>(child_windows_.size());
1391 for (int i = size - 1; i >= 0; --i) {
1392 ConstrainedWindow* window = child_windows_[i];
1393 if (window)
1394 window->CloseConstrainedWindow();
1395 }
[email protected]e29300cd2009-07-23 21:14:591396
1397 // Close the popup container.
1398 if (blocked_popups_) {
1399 blocked_popups_->Destroy();
1400 blocked_popups_ = NULL;
1401 }
[email protected]420ae012009-04-24 05:16:321402}
1403
1404void TabContents::UpdateStarredStateForCurrentURL() {
1405 BookmarkModel* model = profile()->GetBookmarkModel();
1406 const bool old_state = is_starred_;
1407 is_starred_ = (model && model->IsBookmarked(GetURL()));
1408
1409 if (is_starred_ != old_state && delegate())
1410 delegate()->URLStarredChanged(this, is_starred_);
1411}
1412
1413void TabContents::UpdateAlternateErrorPageURL() {
1414 GURL url = GetAlternateErrorPageURL();
1415 render_view_host()->SetAlternateErrorPageURL(url);
1416}
1417
1418void TabContents::UpdateWebPreferences() {
1419 render_view_host()->UpdateWebPreferences(GetWebkitPrefs());
1420}
1421
1422void TabContents::UpdateMaxPageIDIfNecessary(SiteInstance* site_instance,
1423 RenderViewHost* rvh) {
1424 // If we are creating a RVH for a restored controller, then we might
1425 // have more page IDs than the SiteInstance's current max page ID. We must
1426 // make sure that the max page ID is larger than any restored page ID.
1427 // Note that it is ok for conflicting page IDs to exist in another tab
1428 // (i.e., NavigationController), but if any page ID is larger than the max,
1429 // the back/forward list will get confused.
1430 int max_restored_page_id = controller_.max_restored_page_id();
1431 if (max_restored_page_id > 0) {
1432 int curr_max_page_id = site_instance->max_page_id();
1433 if (max_restored_page_id > curr_max_page_id) {
1434 // Need to update the site instance immediately.
1435 site_instance->UpdateMaxPageID(max_restored_page_id);
1436
1437 // Also tell the renderer to update its internal representation. We
1438 // need to reserve enough IDs to make all restored page IDs less than
1439 // the max.
1440 if (curr_max_page_id < 0)
1441 curr_max_page_id = 0;
1442 rvh->ReservePageIDRange(max_restored_page_id - curr_max_page_id);
1443 }
1444 }
1445}
1446
1447void TabContents::UpdateHistoryForNavigation(
[email protected]ebe89e062009-08-13 23:16:541448 const GURL& virtual_url,
[email protected]befd8d822009-07-01 04:51:471449 const NavigationController::LoadCommittedDetails& details,
[email protected]420ae012009-04-24 05:16:321450 const ViewHostMsg_FrameNavigate_Params& params) {
1451 if (profile()->IsOffTheRecord())
1452 return;
1453
1454 // Add to history service.
1455 HistoryService* hs = profile()->GetHistoryService(Profile::IMPLICIT_ACCESS);
1456 if (hs) {
1457 if (PageTransition::IsMainFrame(params.transition) &&
[email protected]ebe89e062009-08-13 23:16:541458 virtual_url != params.url) {
1459 // Hack on the "virtual" URL so that it will appear in history. For some
[email protected]420ae012009-04-24 05:16:321460 // types of URLs, we will display a magic URL that is different from where
1461 // the page is actually navigated. We want the user to see in history
[email protected]ebe89e062009-08-13 23:16:541462 // what they saw in the URL bar, so we add the virtual URL as a redirect.
1463 // This only applies to the main frame, as the virtual URL doesn't apply
[email protected]420ae012009-04-24 05:16:321464 // to sub-frames.
1465 std::vector<GURL> redirects = params.redirects;
1466 if (!redirects.empty())
[email protected]ebe89e062009-08-13 23:16:541467 redirects.back() = virtual_url;
1468 hs->AddPage(virtual_url, this, params.page_id, params.referrer,
[email protected]befd8d822009-07-01 04:51:471469 params.transition, redirects, details.did_replace_entry);
[email protected]420ae012009-04-24 05:16:321470 } else {
1471 hs->AddPage(params.url, this, params.page_id, params.referrer,
[email protected]befd8d822009-07-01 04:51:471472 params.transition, params.redirects,
1473 details.did_replace_entry);
[email protected]420ae012009-04-24 05:16:321474 }
1475 }
1476}
1477
1478bool TabContents::UpdateTitleForEntry(NavigationEntry* entry,
1479 const std::wstring& title) {
1480 // For file URLs without a title, use the pathname instead. In the case of a
1481 // synthesized title, we don't want the update to count toward the "one set
1482 // per page of the title to history."
1483 std::wstring final_title;
1484 bool explicit_set;
1485 if (entry->url().SchemeIsFile() && title.empty()) {
1486 final_title = UTF8ToWide(entry->url().ExtractFileName());
1487 explicit_set = false; // Don't count synthetic titles toward the set limit.
1488 } else {
1489 TrimWhitespace(title, TRIM_ALL, &final_title);
1490 explicit_set = true;
1491 }
1492
1493 if (final_title == UTF16ToWideHack(entry->title()))
1494 return false; // Nothing changed, don't bother.
1495
1496 entry->set_title(WideToUTF16Hack(final_title));
1497
1498 // Update the history system for this page.
1499 if (!profile()->IsOffTheRecord() && !received_page_title_) {
1500 HistoryService* hs =
1501 profile()->GetHistoryService(Profile::IMPLICIT_ACCESS);
1502 if (hs)
[email protected]ebe89e062009-08-13 23:16:541503 hs->SetPageTitle(entry->virtual_url(), final_title);
[email protected]420ae012009-04-24 05:16:321504
1505 // Don't allow the title to be saved again for explicitly set ones.
1506 received_page_title_ = explicit_set;
1507 }
1508
1509 // Lastly, set the title for the view.
1510 view_->SetPageTitle(final_title);
1511
1512 return true;
1513}
1514
1515void TabContents::NotifySwapped() {
1516 // After sending out a swap notification, we need to send a disconnect
1517 // notification so that clients that pick up a pointer to |this| can NULL the
1518 // pointer. See Bug 1230284.
1519 notify_disconnection_ = true;
1520 NotificationService::current()->Notify(
[email protected]57c6a652009-05-04 07:58:341521 NotificationType::TAB_CONTENTS_SWAPPED,
[email protected]d82ed61e2009-06-16 02:46:221522 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:321523 NotificationService::NoDetails());
1524}
1525
1526void TabContents::NotifyConnected() {
1527 notify_disconnection_ = true;
1528 NotificationService::current()->Notify(
[email protected]57c6a652009-05-04 07:58:341529 NotificationType::TAB_CONTENTS_CONNECTED,
[email protected]d82ed61e2009-06-16 02:46:221530 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:321531 NotificationService::NoDetails());
1532}
1533
1534void TabContents::NotifyDisconnected() {
1535 if (!notify_disconnection_)
1536 return;
1537
1538 notify_disconnection_ = false;
1539 NotificationService::current()->Notify(
[email protected]57c6a652009-05-04 07:58:341540 NotificationType::TAB_CONTENTS_DISCONNECTED,
[email protected]d82ed61e2009-06-16 02:46:221541 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:321542 NotificationService::NoDetails());
1543}
1544
1545void TabContents::GenerateKeywordIfNecessary(
1546 const ViewHostMsg_FrameNavigate_Params& params) {
1547 if (!params.searchable_form_url.is_valid())
1548 return;
1549
1550 if (profile()->IsOffTheRecord())
1551 return;
1552
1553 int last_index = controller_.last_committed_entry_index();
1554 // When there was no previous page, the last index will be 0. This is
1555 // normally due to a form submit that opened in a new tab.
1556 // TODO(brettw) bug 916126: we should support keywords when form submits
1557 // happen in new tabs.
1558 if (last_index <= 0)
1559 return;
1560 const NavigationEntry* previous_entry =
1561 controller_.GetEntryAtIndex(last_index - 1);
1562 if (IsFormSubmit(previous_entry)) {
1563 // Only generate a keyword if the previous page wasn't itself a form
1564 // submit.
1565 return;
1566 }
1567
1568 GURL keyword_url = previous_entry->user_typed_url().is_valid() ?
1569 previous_entry->user_typed_url() : previous_entry->url();
1570 std::wstring keyword =
1571 TemplateURLModel::GenerateKeyword(keyword_url, true); // autodetected
1572 if (keyword.empty())
1573 return;
1574
1575 TemplateURLModel* url_model = profile()->GetTemplateURLModel();
1576 if (!url_model)
1577 return;
1578
1579 if (!url_model->loaded()) {
1580 url_model->Load();
1581 return;
1582 }
1583
1584 const TemplateURL* current_url;
1585 std::wstring url = UTF8ToWide(params.searchable_form_url.spec());
1586 if (!url_model->CanReplaceKeyword(keyword, url, &current_url))
1587 return;
1588
1589 if (current_url) {
1590 if (current_url->originating_url().is_valid()) {
1591 // The existing keyword was generated from an OpenSearch description
1592 // document, don't regenerate.
1593 return;
1594 }
1595 url_model->Remove(current_url);
1596 }
1597 TemplateURL* new_url = new TemplateURL();
1598 new_url->set_keyword(keyword);
1599 new_url->set_short_name(keyword);
1600 new_url->SetURL(url, 0, 0);
1601 new_url->add_input_encoding(params.searchable_form_encoding);
1602 DCHECK(controller_.GetLastCommittedEntry());
1603 const GURL& favicon_url =
1604 controller_.GetLastCommittedEntry()->favicon().url();
1605 if (favicon_url.is_valid()) {
1606 new_url->SetFavIconURL(favicon_url);
1607 } else {
1608 // The favicon url isn't valid. This means there really isn't a favicon,
1609 // or the favicon url wasn't obtained before the load started. This assumes
1610 // the later.
1611 // TODO(sky): Need a way to set the favicon that doesn't involve generating
1612 // its url.
1613 new_url->SetFavIconURL(TemplateURL::GenerateFaviconURL(params.referrer));
1614 }
1615 new_url->set_safe_for_autoreplace(true);
1616 url_model->Add(new_url);
1617}
1618
[email protected]bf5c2ff392009-07-08 16:24:331619void TabContents::OnUserGesture() {
1620 // See comment in RenderViewHostDelegate::OnUserGesture as to why we do this.
1621 DownloadRequestManager* drm = g_browser_process->download_request_manager();
1622 if (drm)
1623 drm->OnUserGesture(this);
[email protected]e7eaedde2009-09-25 20:09:491624 ExternalProtocolHandler::OnUserGesture();
[email protected]bf5c2ff392009-07-08 16:24:331625 controller_.OnUserGesture();
1626}
1627
1628void TabContents::OnFindReply(int request_id,
1629 int number_of_matches,
1630 const gfx::Rect& selection_rect,
1631 int active_match_ordinal,
1632 bool final_update) {
[email protected]3ce7ced2009-07-24 18:52:421633 // Ignore responses for requests that have been aborted.
1634 if (find_op_aborted_)
1635 return;
1636
[email protected]bf5c2ff392009-07-08 16:24:331637 // Ignore responses for requests other than the one we have most recently
1638 // issued. That way we won't act on stale results when the user has
1639 // already typed in another query.
1640 if (request_id != current_find_request_id_)
1641 return;
1642
1643 if (number_of_matches == -1)
1644 number_of_matches = last_search_result_.number_of_matches();
1645 if (active_match_ordinal == -1)
1646 active_match_ordinal = last_search_result_.active_match_ordinal();
1647
1648 gfx::Rect selection = selection_rect;
1649 if (selection.IsEmpty())
1650 selection = last_search_result_.selection_rect();
1651
1652 // Notify the UI, automation and any other observers that a find result was
1653 // found.
1654 last_search_result_ = FindNotificationDetails(request_id, number_of_matches,
1655 selection, active_match_ordinal,
1656 final_update);
1657 NotificationService::current()->Notify(
1658 NotificationType::FIND_RESULT_AVAILABLE,
1659 Source<TabContents>(this),
1660 Details<FindNotificationDetails>(&last_search_result_));
1661}
1662
1663void TabContents::GoToEntryAtOffset(int offset) {
1664 controller_.GoToOffset(offset);
1665}
1666
1667void TabContents::GetHistoryListCount(int* back_list_count,
1668 int* forward_list_count) {
1669 int current_index = controller_.last_committed_entry_index();
1670 *back_list_count = current_index;
1671 *forward_list_count = controller_.entry_count() - current_index - 1;
1672}
1673
1674void TabContents::OnMissingPluginStatus(int status) {
1675#if defined(OS_WIN)
1676// TODO(PORT): pull in when plug-ins work
1677 GetPluginInstaller()->OnMissingPluginStatus(status);
1678#endif
1679}
1680
1681void TabContents::OnCrashedPlugin(const FilePath& plugin_path) {
[email protected]bf5c2ff392009-07-08 16:24:331682 DCHECK(!plugin_path.value().empty());
1683
1684 std::wstring plugin_name = plugin_path.ToWStringHack();
[email protected]d32d46182009-07-23 00:09:351685#if defined(OS_WIN) || defined(OS_MACOSX)
[email protected]bf5c2ff392009-07-08 16:24:331686 scoped_ptr<FileVersionInfo> version_info(
1687 FileVersionInfo::CreateFileVersionInfo(plugin_path));
1688 if (version_info.get()) {
1689 const std::wstring& product_name = version_info->product_name();
[email protected]996fd702009-09-04 19:12:371690 if (!product_name.empty()) {
[email protected]bf5c2ff392009-07-08 16:24:331691 plugin_name = product_name;
[email protected]996fd702009-09-04 19:12:371692#if defined(OS_MACOSX)
1693 // Many plugins on the Mac have .plugin in the actual name, which looks
1694 // terrible, so look for that and strip it off if present.
1695 const std::wstring plugin_extension(L".plugin");
1696 if (EndsWith(plugin_name, plugin_extension, true))
1697 plugin_name.erase(plugin_name.length() - plugin_extension.length());
1698#endif // OS_MACOSX
1699 }
[email protected]bf5c2ff392009-07-08 16:24:331700 }
[email protected]4a560ed2009-07-13 23:35:261701#else
1702 NOTIMPLEMENTED() << " convert plugin path to plugin name";
1703#endif
[email protected]bf5c2ff392009-07-08 16:24:331704 AddInfoBar(new SimpleAlertInfoBarDelegate(
1705 this, l10n_util::GetStringF(IDS_PLUGIN_CRASHED_PROMPT, plugin_name),
1706 NULL));
[email protected]bf5c2ff392009-07-08 16:24:331707}
1708
1709void TabContents::OnCrashedWorker() {
1710 AddInfoBar(new SimpleAlertInfoBarDelegate(
1711 this, l10n_util::GetString(IDS_WEBWORKER_CRASHED_PROMPT),
1712 NULL));
1713}
1714
1715void TabContents::OnDidGetApplicationInfo(
1716 int32 page_id,
1717 const webkit_glue::WebApplicationInfo& info) {
1718 if (pending_install_.page_id != page_id)
1719 return; // The user clicked create on a separate page. Ignore this.
1720
1721 pending_install_.callback_functor =
1722 new GearsCreateShortcutCallbackFunctor(this);
1723 GearsCreateShortcut(
1724 info, pending_install_.title, pending_install_.url, pending_install_.icon,
1725 NewCallback(pending_install_.callback_functor,
1726 &GearsCreateShortcutCallbackFunctor::Run));
1727}
1728
1729void TabContents::DidStartProvisionalLoadForFrame(
1730 RenderViewHost* render_view_host,
1731 bool is_main_frame,
1732 const GURL& url) {
1733 ProvisionalLoadDetails details(is_main_frame,
1734 controller_.IsURLInPageNavigation(url),
1735 url, std::string(), false);
1736 NotificationService::current()->Notify(
1737 NotificationType::FRAME_PROVISIONAL_LOAD_START,
1738 Source<NavigationController>(&controller_),
1739 Details<ProvisionalLoadDetails>(&details));
1740}
1741
1742void TabContents::DidStartReceivingResourceResponse(
1743 ResourceRequestDetails* details) {
1744 NotificationService::current()->Notify(
1745 NotificationType::RESOURCE_RESPONSE_STARTED,
1746 Source<NavigationController>(&controller()),
1747 Details<ResourceRequestDetails>(details));
1748}
1749
1750void TabContents::DidRedirectResource(ResourceRequestDetails* details) {
1751 NotificationService::current()->Notify(
1752 NotificationType::RESOURCE_RECEIVED_REDIRECT,
1753 Source<NavigationController>(&controller()),
1754 Details<ResourceRequestDetails>(details));
1755}
1756
1757void TabContents::DidLoadResourceFromMemoryCache(
1758 const GURL& url,
1759 const std::string& frame_origin,
1760 const std::string& main_frame_origin,
1761 const std::string& security_info) {
1762 // Send out a notification that we loaded a resource from our memory cache.
1763 int cert_id = 0, cert_status = 0, security_bits = 0;
1764 SSLManager::DeserializeSecurityInfo(security_info,
1765 &cert_id, &cert_status,
1766 &security_bits);
1767 LoadFromMemoryCacheDetails details(url, frame_origin, main_frame_origin,
[email protected]76543b92009-08-31 17:27:451768 process()->id(), cert_id, cert_status);
[email protected]bf5c2ff392009-07-08 16:24:331769
1770 NotificationService::current()->Notify(
1771 NotificationType::LOAD_FROM_MEMORY_CACHE,
1772 Source<NavigationController>(&controller_),
1773 Details<LoadFromMemoryCacheDetails>(&details));
1774}
1775
[email protected]e3d60e5d2009-09-25 21:08:291776void TabContents::DidDisplayInsecureContent() {
[email protected]09843b5f2009-09-26 08:30:591777 controller_.ssl_manager()->DidDisplayInsecureContent();
[email protected]e3d60e5d2009-09-25 21:08:291778}
1779
1780void TabContents::DidRunInsecureContent(const std::string& security_origin) {
[email protected]09843b5f2009-09-26 08:30:591781 controller_.ssl_manager()->DidRunInsecureContent(security_origin);
[email protected]e3d60e5d2009-09-25 21:08:291782}
1783
[email protected]bf5c2ff392009-07-08 16:24:331784void TabContents::DidFailProvisionalLoadWithError(
1785 RenderViewHost* render_view_host,
1786 bool is_main_frame,
1787 int error_code,
1788 const GURL& url,
1789 bool showing_repost_interstitial) {
1790 if (net::ERR_ABORTED == error_code) {
1791 // EVIL HACK ALERT! Ignore failed loads when we're showing interstitials.
1792 // This means that the interstitial won't be torn down properly, which is
1793 // bad. But if we have an interstitial, go back to another tab type, and
1794 // then load the same interstitial again, we could end up getting the first
1795 // interstitial's "failed" message (as a result of the cancel) when we're on
1796 // the second one.
1797 //
1798 // We can't tell this apart, so we think we're tearing down the current page
1799 // which will cause a crash later one. There is also some code in
1800 // RenderViewHostManager::RendererAbortedProvisionalLoad that is commented
1801 // out because of this problem.
1802 //
1803 // http://code.google.com/p/chromium/issues/detail?id=2855
1804 // Because this will not tear down the interstitial properly, if "back" is
1805 // back to another tab type, the interstitial will still be somewhat alive
1806 // in the previous tab type. If you navigate somewhere that activates the
1807 // tab with the interstitial again, you'll see a flash before the new load
1808 // commits of the interstitial page.
1809 if (showing_interstitial_page()) {
1810 LOG(WARNING) << "Discarding message during interstitial.";
1811 return;
1812 }
1813
1814 // This will discard our pending entry if we cancelled the load (e.g., if we
1815 // decided to download the file instead of load it). Only discard the
1816 // pending entry if the URLs match, otherwise the user initiated a navigate
1817 // before the page loaded so that the discard would discard the wrong entry.
1818 NavigationEntry* pending_entry = controller_.pending_entry();
1819 if (pending_entry && pending_entry->url() == url) {
1820 controller_.DiscardNonCommittedEntries();
1821 // Update the URL display.
1822 NotifyNavigationStateChanged(TabContents::INVALIDATE_URL);
1823 }
1824
1825 render_manager_.RendererAbortedProvisionalLoad(render_view_host);
1826 }
1827
1828 // Send out a notification that we failed a provisional load with an error.
1829 ProvisionalLoadDetails details(is_main_frame,
1830 controller_.IsURLInPageNavigation(url),
1831 url, std::string(), false);
1832 details.set_error_code(error_code);
1833
1834 NotificationService::current()->Notify(
1835 NotificationType::FAIL_PROVISIONAL_LOAD_WITH_ERROR,
1836 Source<NavigationController>(&controller_),
1837 Details<ProvisionalLoadDetails>(&details));
1838}
1839
1840void TabContents::DocumentLoadedInFrame() {
1841 controller_.DocumentLoadedInFrame();
1842}
1843
[email protected]8d3347f2009-07-09 22:00:211844RenderViewHostDelegate::View* TabContents::GetViewDelegate() {
[email protected]420ae012009-04-24 05:16:321845 return view_.get();
1846}
1847
[email protected]8d3347f2009-07-09 22:00:211848RenderViewHostDelegate::RendererManagement*
1849TabContents::GetRendererManagementDelegate() {
1850 return &render_manager_;
1851}
1852
[email protected]bf5c2ff392009-07-08 16:24:331853RenderViewHostDelegate::BrowserIntegration*
[email protected]8d3347f2009-07-09 22:00:211854 TabContents::GetBrowserIntegrationDelegate() {
1855 return this;
[email protected]bf5c2ff392009-07-08 16:24:331856}
1857
[email protected]8d3347f2009-07-09 22:00:211858RenderViewHostDelegate::Resource* TabContents::GetResourceDelegate() {
1859 return this;
[email protected]bf5c2ff392009-07-08 16:24:331860}
1861
[email protected]8d3347f2009-07-09 22:00:211862RenderViewHostDelegate::Save* TabContents::GetSaveDelegate() {
[email protected]420ae012009-04-24 05:16:321863 return save_package_.get(); // May be NULL, but we can return NULL.
1864}
1865
[email protected]8d3347f2009-07-09 22:00:211866RenderViewHostDelegate::Printing* TabContents::GetPrintingDelegate() {
1867 return &printing_;
1868}
1869
1870RenderViewHostDelegate::FavIcon* TabContents::GetFavIconDelegate() {
1871 return &fav_icon_helper_;
1872}
1873
1874RenderViewHostDelegate::Autofill* TabContents::GetAutofillDelegate() {
1875 if (autofill_manager_.get() == NULL)
1876 autofill_manager_.reset(new AutofillManager(this));
1877 return autofill_manager_.get();
[email protected]bf5c2ff392009-07-08 16:24:331878}
1879
[email protected]80d96fa2009-06-10 22:34:511880RendererPreferences TabContents::GetRendererPrefs() const {
[email protected]840b1512009-07-21 16:53:461881 return renderer_preferences_;
[email protected]80d96fa2009-06-10 22:34:511882}
1883
[email protected]57c6a652009-05-04 07:58:341884TabContents* TabContents::GetAsTabContents() {
1885 return this;
1886}
1887
[email protected]3e7cf1362009-08-21 17:47:081888void TabContents::AddBlockedNotice(const GURL& url, const string16& reason) {
1889 CreateBlockedPopupContainerIfNecessary();
1890 blocked_popups_->AddBlockedNotice(url, reason);
1891}
1892
[email protected]7b291f92009-08-14 05:43:531893ViewType::Type TabContents::GetRenderViewType() const {
1894 return ViewType::TAB_CONTENTS;
1895}
1896
1897int TabContents::GetBrowserWindowID() const {
1898 return controller().window_id().id();
1899}
1900
[email protected]420ae012009-04-24 05:16:321901void TabContents::RenderViewCreated(RenderViewHost* render_view_host) {
[email protected]2e4633c2009-07-09 16:58:061902 NotificationService::current()->Notify(
1903 NotificationType::RENDER_VIEW_HOST_CREATED_FOR_TAB,
1904 Source<TabContents>(this),
1905 Details<RenderViewHost>(render_view_host));
[email protected]420ae012009-04-24 05:16:321906 NavigationEntry* entry = controller_.GetActiveEntry();
1907 if (!entry)
1908 return;
1909
1910 // When we're creating views, we're still doing initial setup, so we always
1911 // use the pending DOM UI rather than any possibly existing committed one.
1912 if (render_manager_.pending_dom_ui()) {
1913 render_manager_.pending_dom_ui()->RenderViewCreated(render_view_host);
1914 }
1915
1916 if (entry->IsViewSourceMode()) {
1917 // Put the renderer in view source mode.
1918 render_view_host->Send(
1919 new ViewMsg_EnableViewSourceMode(render_view_host->routing_id()));
1920 }
[email protected]0666aef2009-05-13 19:48:081921
1922 view()->RenderViewCreated(render_view_host);
[email protected]420ae012009-04-24 05:16:321923}
1924
1925void TabContents::RenderViewReady(RenderViewHost* rvh) {
1926 if (rvh != render_view_host()) {
1927 // Don't notify the world, since this came from a renderer in the
1928 // background.
1929 return;
1930 }
1931
1932 NotifyConnected();
[email protected]9d8a4642009-07-29 17:25:301933 bool was_crashed = is_crashed();
[email protected]420ae012009-04-24 05:16:321934 SetIsCrashed(false);
[email protected]9d8a4642009-07-29 17:25:301935
1936 // Restore the focus to the tab (otherwise the focus will be on the top
1937 // window).
1938 if (was_crashed && !FocusLocationBarByDefault())
1939 Focus();
[email protected]420ae012009-04-24 05:16:321940}
1941
1942void TabContents::RenderViewGone(RenderViewHost* rvh) {
1943 // Ask the print preview if this renderer was valuable.
1944 if (!printing_.OnRenderViewGone(rvh))
1945 return;
1946 if (rvh != render_view_host()) {
1947 // The pending page's RenderViewHost is gone.
1948 return;
1949 }
1950
1951 SetIsLoading(false, NULL);
1952 NotifyDisconnected();
1953 SetIsCrashed(true);
1954
[email protected]8e537302009-06-10 21:19:581955 // Tell the view that we've crashed so it can prepare the sad tab page.
1956 view_->OnTabCrashed();
[email protected]420ae012009-04-24 05:16:321957
1958 // Hide any visible hung renderer warning for this web contents' process.
[email protected]d82ed61e2009-06-16 02:46:221959 HungRendererDialog::HideForTabContents(this);
[email protected]420ae012009-04-24 05:16:321960}
1961
[email protected]2e4633c2009-07-09 16:58:061962void TabContents::RenderViewDeleted(RenderViewHost* rvh) {
1963 NotificationService::current()->Notify(
1964 NotificationType::RENDER_VIEW_HOST_DELETED,
1965 Source<TabContents>(this),
1966 Details<RenderViewHost>(rvh));
1967}
1968
[email protected]420ae012009-04-24 05:16:321969void TabContents::DidNavigate(RenderViewHost* rvh,
1970 const ViewHostMsg_FrameNavigate_Params& params) {
[email protected]8030f012009-09-25 18:09:371971 int extra_invalidate_flags = 0;
1972
1973 if (PageTransition::IsMainFrame(params.transition)) {
1974 bool was_bookmark_bar_visible = IsBookmarkBarAlwaysVisible();
1975 bool was_extension_shelf_visible = IsExtensionShelfAlwaysVisible();
1976
[email protected]420ae012009-04-24 05:16:321977 render_manager_.DidNavigateMainFrame(rvh);
1978
[email protected]8030f012009-09-25 18:09:371979 if (was_bookmark_bar_visible != IsBookmarkBarAlwaysVisible())
1980 extra_invalidate_flags |= INVALIDATE_BOOKMARK_BAR;
1981 if (was_extension_shelf_visible != IsExtensionShelfAlwaysVisible())
1982 extra_invalidate_flags |= INVALIDATE_EXTENSION_SHELF;
1983 }
1984
[email protected]420ae012009-04-24 05:16:321985 // Update the site of the SiteInstance if it doesn't have one yet.
1986 if (!GetSiteInstance()->has_site())
1987 GetSiteInstance()->SetSite(params.url);
1988
1989 // Need to update MIME type here because it's referred to in
1990 // UpdateNavigationCommands() called by RendererDidNavigate() to
1991 // determine whether or not to enable the encoding menu.
1992 // It's updated only for the main frame. For a subframe,
1993 // RenderView::UpdateURL does not set params.contents_mime_type.
1994 // (see http://code.google.com/p/chromium/issues/detail?id=2929 )
1995 // TODO(jungshik): Add a test for the encoding menu to avoid
1996 // regressing it again.
1997 if (PageTransition::IsMainFrame(params.transition))
1998 contents_mime_type_ = params.contents_mime_type;
1999
2000 NavigationController::LoadCommittedDetails details;
[email protected]8030f012009-09-25 18:09:372001 bool did_navigate = controller_.RendererDidNavigate(
2002 params, extra_invalidate_flags, &details);
[email protected]3e90d4a2009-07-03 17:38:392003
2004 // Update history. Note that this needs to happen after the entry is complete,
2005 // which WillNavigate[Main,Sub]Frame will do before this function is called.
2006 if (params.should_update_history) {
2007 // Most of the time, the displayURL matches the loaded URL, but for about:
2008 // URLs, we use a data: URL as the real value. We actually want to save
2009 // the about: URL to the history db and keep the data: URL hidden. This is
2010 // what the TabContents' URL getter does.
2011 UpdateHistoryForNavigation(GetURL(), details, params);
2012 }
2013
[email protected]76543b92009-08-31 17:27:452014 if (!did_navigate)
2015 return; // No navigation happened.
[email protected]420ae012009-04-24 05:16:322016
2017 // DO NOT ADD MORE STUFF TO THIS FUNCTION! Your component should either listen
2018 // for the appropriate notification (best) or you can add it to
2019 // DidNavigateMainFramePostCommit / DidNavigateAnyFramePostCommit (only if
2020 // necessary, please).
2021
2022 // Run post-commit tasks.
2023 if (details.is_main_frame)
2024 DidNavigateMainFramePostCommit(details, params);
2025 DidNavigateAnyFramePostCommit(rvh, details, params);
2026}
2027
2028void TabContents::UpdateState(RenderViewHost* rvh,
2029 int32 page_id,
2030 const std::string& state) {
2031 DCHECK(rvh == render_view_host());
2032
2033 // We must be prepared to handle state updates for any page, these occur
2034 // when the user is scrolling and entering form data, as well as when we're
2035 // leaving a page, in which case our state may have already been moved to
2036 // the next page. The navigation controller will look up the appropriate
2037 // NavigationEntry and update it when it is notified via the delegate.
2038
2039 int entry_index = controller_.GetEntryIndexWithPageID(
2040 GetSiteInstance(), page_id);
2041 if (entry_index < 0)
2042 return;
2043 NavigationEntry* entry = controller_.GetEntryAtIndex(entry_index);
2044
2045 if (state == entry->content_state())
2046 return; // Nothing to update.
2047 entry->set_content_state(state);
2048 controller_.NotifyEntryChanged(entry, entry_index);
2049}
2050
2051void TabContents::UpdateTitle(RenderViewHost* rvh,
2052 int32 page_id, const std::wstring& title) {
2053 // If we have a title, that's a pretty good indication that we've started
2054 // getting useful data.
2055 SetNotWaitingForResponse();
2056
2057 DCHECK(rvh == render_view_host());
2058 NavigationEntry* entry = controller_.GetEntryWithPageID(GetSiteInstance(),
2059 page_id);
2060 if (!entry || !UpdateTitleForEntry(entry, title))
2061 return;
2062
2063 // Broadcast notifications when the UI should be updated.
2064 if (entry == controller_.GetEntryAtOffset(0))
[email protected]c9cd2222009-05-06 05:16:502065 NotifyNavigationStateChanged(INVALIDATE_TAB);
[email protected]420ae012009-04-24 05:16:322066}
2067
[email protected]420ae012009-04-24 05:16:322068void TabContents::UpdateEncoding(RenderViewHost* render_view_host,
[email protected]41fc0322009-09-04 22:23:402069 const std::string& encoding) {
[email protected]420ae012009-04-24 05:16:322070 set_encoding(encoding);
2071}
2072
2073void TabContents::UpdateTargetURL(int32 page_id, const GURL& url) {
2074 if (delegate())
2075 delegate()->UpdateTargetURL(this, url);
2076}
2077
2078void TabContents::UpdateThumbnail(const GURL& url,
2079 const SkBitmap& bitmap,
2080 const ThumbnailScore& score) {
2081 // Tell History about this thumbnail
[email protected]449478302009-06-09 20:04:282082 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kThumbnailStore)) {
[email protected]72d0fc72009-07-09 17:30:492083 if (!profile()->IsOffTheRecord())
[email protected]42339402009-07-17 17:27:192084 profile()->GetThumbnailStore()->SetPageThumbnail(url, bitmap,
2085 score, true);
[email protected]449478302009-06-09 20:04:282086 } else {
2087 HistoryService* hs;
2088 if (!profile()->IsOffTheRecord() &&
2089 (hs = profile()->GetHistoryService(Profile::IMPLICIT_ACCESS))) {
2090 hs->SetPageThumbnail(url, bitmap, score);
2091 }
[email protected]420ae012009-04-24 05:16:322092 }
2093}
2094
[email protected]0df30122009-06-03 12:13:082095void TabContents::UpdateInspectorSettings(const std::wstring& raw_settings) {
2096 profile()->GetPrefs()->SetString(prefs::kWebKitInspectorSettings,
2097 raw_settings);
2098}
2099
[email protected]420ae012009-04-24 05:16:322100void TabContents::Close(RenderViewHost* rvh) {
[email protected]24a4d1062009-07-10 23:10:422101 // If we close the tab while we're in the middle of a drag, we'll crash.
2102 // Instead, cancel the drag and close it as soon as the drag ends.
2103 if (view()->IsDoingDrag()) {
2104 view()->CancelDragAndCloseTab();
2105 return;
2106 }
2107
[email protected]420ae012009-04-24 05:16:322108 // Ignore this if it comes from a RenderViewHost that we aren't showing.
2109 if (delegate() && rvh == render_view_host())
2110 delegate()->CloseContents(this);
2111}
2112
2113void TabContents::RequestMove(const gfx::Rect& new_bounds) {
2114 if (delegate() && delegate()->IsPopup(this))
2115 delegate()->MoveContents(this, new_bounds);
2116}
2117
[email protected]329581b2009-04-28 06:52:352118void TabContents::DidStartLoading(RenderViewHost* rvh) {
[email protected]420ae012009-04-24 05:16:322119 SetIsLoading(true, NULL);
2120}
2121
[email protected]329581b2009-04-28 06:52:352122void TabContents::DidStopLoading(RenderViewHost* rvh) {
[email protected]420ae012009-04-24 05:16:322123 scoped_ptr<LoadNotificationDetails> details;
2124
2125 NavigationEntry* entry = controller_.GetActiveEntry();
2126 // An entry may not exist for a stop when loading an initial blank page or
2127 // if an iframe injected by script into a blank page finishes loading.
2128 if (entry) {
2129 scoped_ptr<base::ProcessMetrics> metrics(
2130 base::ProcessMetrics::CreateProcessMetrics(
2131 process()->process().handle()));
2132
2133 base::TimeDelta elapsed = base::TimeTicks::Now() - current_load_start_;
2134
2135 details.reset(new LoadNotificationDetails(
[email protected]ebe89e062009-08-13 23:16:542136 entry->virtual_url(),
[email protected]420ae012009-04-24 05:16:322137 entry->transition_type(),
2138 elapsed,
2139 &controller_,
2140 controller_.GetCurrentEntryIndex()));
2141 }
2142
2143 // Tell PasswordManager we've finished a page load, which serves as a
2144 // green light to save pending passwords and reset itself.
2145 GetPasswordManager()->DidStopLoading();
2146
2147 SetIsLoading(false, details.get());
2148}
2149
[email protected]420ae012009-04-24 05:16:322150void TabContents::DidRedirectProvisionalLoad(int32 page_id,
2151 const GURL& source_url,
2152 const GURL& target_url) {
2153 NavigationEntry* entry;
2154 if (page_id == -1)
2155 entry = controller_.pending_entry();
2156 else
2157 entry = controller_.GetEntryWithPageID(GetSiteInstance(), page_id);
2158 if (!entry || entry->url() != source_url)
2159 return;
2160 entry->set_url(target_url);
2161}
2162
[email protected]420ae012009-04-24 05:16:322163void TabContents::RequestOpenURL(const GURL& url, const GURL& referrer,
2164 WindowOpenDisposition disposition) {
2165 if (render_manager_.dom_ui()) {
2166 // When we're a DOM UI, it will provide a page transition type for us (this
2167 // is so the new tab page can specify AUTO_BOOKMARK for automatically
2168 // generated suggestions).
2169 //
2170 // Note also that we hide the referrer for DOM UI pages. We don't really
[email protected]60e448982009-05-06 04:21:162171 // want web sites to see a referrer of "chrome://blah" (and some
2172 // chrome: URLs might have search terms or other stuff we don't want to
[email protected]420ae012009-04-24 05:16:322173 // send to the site), so we send no referrer.
2174 OpenURL(url, GURL(), disposition,
2175 render_manager_.dom_ui()->link_transition_type());
2176 } else {
2177 OpenURL(url, referrer, disposition, PageTransition::LINK);
2178 }
2179}
2180
[email protected]1ee614862009-07-06 20:29:182181void TabContents::DomOperationResponse(const std::string& json_string,
2182 int automation_id) {
2183}
2184
[email protected]420ae012009-04-24 05:16:322185void TabContents::ProcessDOMUIMessage(const std::string& message,
[email protected]811bfe32009-07-01 08:46:252186 const std::string& content,
2187 int request_id,
2188 bool has_callback) {
[email protected]420ae012009-04-24 05:16:322189 if (!render_manager_.dom_ui()) {
[email protected]c2e74fe82009-09-03 17:57:442190 // This can happen if someone uses window.open() to open an extension URL
2191 // from a non-extension context.
2192 render_view_host()->BlockExtensionRequest(request_id);
[email protected]420ae012009-04-24 05:16:322193 return;
2194 }
[email protected]811bfe32009-07-01 08:46:252195 render_manager_.dom_ui()->ProcessDOMUIMessage(message, content, request_id,
2196 has_callback);
[email protected]420ae012009-04-24 05:16:322197}
2198
2199void TabContents::ProcessExternalHostMessage(const std::string& message,
2200 const std::string& origin,
2201 const std::string& target) {
2202 if (delegate())
2203 delegate()->ForwardMessageToExternalHost(message, origin, target);
2204}
2205
[email protected]420ae012009-04-24 05:16:322206void TabContents::RunFileChooser(bool multiple_files,
2207 const string16& title,
2208 const FilePath& default_file) {
2209 if (!select_file_dialog_.get())
2210 select_file_dialog_ = SelectFileDialog::Create(this);
2211 SelectFileDialog::Type dialog_type =
2212 multiple_files ? SelectFileDialog::SELECT_OPEN_MULTI_FILE :
2213 SelectFileDialog::SELECT_OPEN_FILE;
2214 select_file_dialog_->SelectFile(dialog_type, title, default_file,
2215 NULL, 0, FILE_PATH_LITERAL(""),
2216 view_->GetTopLevelNativeWindow(), NULL);
2217}
2218
2219void TabContents::RunJavaScriptMessage(
2220 const std::wstring& message,
2221 const std::wstring& default_prompt,
2222 const GURL& frame_url,
2223 const int flags,
2224 IPC::Message* reply_msg,
2225 bool* did_suppress_message) {
2226 // Suppress javascript messages when requested and when inside a constrained
2227 // popup window (because that activates them and breaks them out of the
2228 // constrained window jail).
[email protected]91ed49a2009-07-07 21:29:552229 // Also suppress messages when showing an interstitial. The interstitial is
2230 // shown over the previous page, we don't want the hidden page dialogs to
2231 // interfere with the interstitial.
2232 bool suppress_this_message = suppress_javascript_messages_ ||
2233 showing_interstitial_page();
[email protected]420ae012009-04-24 05:16:322234 if (delegate())
2235 suppress_this_message |=
[email protected]b6c874582009-05-08 19:38:312236 (delegate()->GetConstrainingContents(this) != this);
[email protected]420ae012009-04-24 05:16:322237
2238 *did_suppress_message = suppress_this_message;
2239
2240 if (!suppress_this_message) {
2241 base::TimeDelta time_since_last_message(
2242 base::TimeTicks::Now() - last_javascript_message_dismissal_);
2243 bool show_suppress_checkbox = false;
2244 // Show a checkbox offering to suppress further messages if this message is
2245 // being displayed within kJavascriptMessageExpectedDelay of the last one.
2246 if (time_since_last_message <
2247 base::TimeDelta::FromMilliseconds(kJavascriptMessageExpectedDelay))
2248 show_suppress_checkbox = true;
2249
[email protected]d82ed61e2009-06-16 02:46:222250 RunJavascriptMessageBox(this, frame_url, flags, message, default_prompt,
[email protected]420ae012009-04-24 05:16:322251 show_suppress_checkbox, reply_msg);
2252 } else {
2253 // If we are suppressing messages, just reply as is if the user immediately
2254 // pressed "Cancel".
2255 OnJavaScriptMessageBoxClosed(reply_msg, false, std::wstring());
2256 }
2257}
2258
2259void TabContents::RunBeforeUnloadConfirm(const std::wstring& message,
2260 IPC::Message* reply_msg) {
[email protected]d82ed61e2009-06-16 02:46:222261 RunBeforeUnloadDialog(this, message, reply_msg);
[email protected]420ae012009-04-24 05:16:322262}
2263
2264void TabContents::ShowModalHTMLDialog(const GURL& url, int width, int height,
2265 const std::string& json_arguments,
2266 IPC::Message* reply_msg) {
2267 if (delegate()) {
2268 HtmlDialogUIDelegate* dialog_delegate =
2269 new ModalHtmlDialogDelegate(url, width, height, json_arguments,
[email protected]d82ed61e2009-06-16 02:46:222270 reply_msg, this);
[email protected]420ae012009-04-24 05:16:322271 delegate()->ShowHtmlDialog(dialog_delegate, NULL);
2272 }
2273}
2274
2275void TabContents::PasswordFormsSeen(
[email protected]ed3fb032009-06-16 19:50:562276 const std::vector<webkit_glue::PasswordForm>& forms) {
[email protected]420ae012009-04-24 05:16:322277 GetPasswordManager()->PasswordFormsSeen(forms);
2278}
2279
[email protected]420ae012009-04-24 05:16:322280// Checks to see if we should generate a keyword based on the OSDD, and if
2281// necessary uses TemplateURLFetcher to download the OSDD and create a keyword.
2282void TabContents::PageHasOSDD(RenderViewHost* render_view_host,
2283 int32 page_id, const GURL& url,
2284 bool autodetected) {
2285 // Make sure page_id is the current page, and the TemplateURLModel is loaded.
2286 DCHECK(url.is_valid());
2287 if (!IsActiveEntry(page_id))
2288 return;
2289 TemplateURLModel* url_model = profile()->GetTemplateURLModel();
2290 if (!url_model)
2291 return;
2292 if (!url_model->loaded()) {
2293 url_model->Load();
2294 return;
2295 }
2296 if (!profile()->GetTemplateURLFetcher())
2297 return;
2298
2299 if (profile()->IsOffTheRecord())
2300 return;
2301
2302 const NavigationEntry* entry = controller_.GetLastCommittedEntry();
2303 DCHECK(entry);
2304
2305 const NavigationEntry* base_entry = entry;
2306 if (IsFormSubmit(base_entry)) {
2307 // If the current page is a form submit, find the last page that was not
2308 // a form submit and use its url to generate the keyword from.
2309 int index = controller_.last_committed_entry_index() - 1;
2310 while (index >= 0 && IsFormSubmit(controller_.GetEntryAtIndex(index)))
2311 index--;
2312 if (index >= 0)
2313 base_entry = controller_.GetEntryAtIndex(index);
2314 else
2315 base_entry = NULL;
2316 }
2317
2318 // We want to use the user typed URL if available since that represents what
2319 // the user typed to get here, and fall back on the regular URL if not.
2320 if (!base_entry)
2321 return;
2322 GURL keyword_url = base_entry->user_typed_url().is_valid() ?
2323 base_entry->user_typed_url() : base_entry->url();
2324 if (!keyword_url.is_valid())
2325 return;
2326 std::wstring keyword = TemplateURLModel::GenerateKeyword(keyword_url,
2327 autodetected);
2328 if (keyword.empty())
2329 return;
2330 const TemplateURL* template_url =
2331 url_model->GetTemplateURLForKeyword(keyword);
2332 if (template_url && (!template_url->safe_for_autoreplace() ||
2333 template_url->originating_url() == url)) {
2334 // Either there is a user created TemplateURL for this keyword, or the
2335 // keyword has the same OSDD url and we've parsed it.
2336 return;
2337 }
2338
2339 // Download the OpenSearch description document. If this is successful a
2340 // new keyword will be created when done.
[email protected]420ae012009-04-24 05:16:322341 profile()->GetTemplateURLFetcher()->ScheduleDownload(
2342 keyword,
2343 url,
2344 base_entry->favicon().url(),
[email protected]d0cc9fb52009-06-23 02:26:162345 this,
[email protected]420ae012009-04-24 05:16:322346 autodetected);
2347}
2348
[email protected]420ae012009-04-24 05:16:322349GURL TabContents::GetAlternateErrorPageURL() const {
2350 GURL url;
2351 // Disable alternate error pages when in OffTheRecord/Incognito mode.
2352 if (profile()->IsOffTheRecord())
2353 return url;
2354
2355 PrefService* prefs = profile()->GetPrefs();
2356 DCHECK(prefs);
2357 if (prefs->GetBoolean(prefs::kAlternateErrorPagesEnabled)) {
[email protected]a33457b82009-09-21 23:07:152358 url = google_util::AppendGoogleLocaleParam(
2359 GURL(google_util::kLinkDoctorBaseURL));
[email protected]420ae012009-04-24 05:16:322360 url = google_util::AppendGoogleTLDParam(url);
2361 }
2362 return url;
2363}
2364
2365WebPreferences TabContents::GetWebkitPrefs() {
2366 PrefService* prefs = render_view_host()->process()->profile()->GetPrefs();
[email protected]7ccddb8c2009-08-04 17:36:552367 bool is_dom_ui = false;
2368 return RenderViewHostDelegateHelper::GetWebkitPrefs(prefs, is_dom_ui);
[email protected]420ae012009-04-24 05:16:322369}
2370
[email protected]420ae012009-04-24 05:16:322371void TabContents::OnJSOutOfMemory() {
2372 AddInfoBar(new SimpleAlertInfoBarDelegate(
2373 this, l10n_util::GetString(IDS_JS_OUT_OF_MEMORY_PROMPT), NULL));
2374}
2375
[email protected]420ae012009-04-24 05:16:322376void TabContents::OnCrossSiteResponse(int new_render_process_host_id,
2377 int new_request_id) {
[email protected]57c6a652009-05-04 07:58:342378 // Allows the TabContents to react when a cross-site response is ready to be
[email protected]420ae012009-04-24 05:16:322379 // delivered to a pending RenderViewHost. We must first run the onunload
2380 // handler of the old RenderViewHost before we can allow it to proceed.
2381 render_manager_.OnCrossSiteResponse(new_render_process_host_id,
2382 new_request_id);
2383}
2384
2385bool TabContents::CanBlur() const {
2386 return delegate() ? delegate()->CanBlur() : true;
2387}
2388
2389gfx::Rect TabContents::GetRootWindowResizerRect() const {
2390 if (delegate())
2391 return delegate()->GetRootWindowResizerRect();
2392 return gfx::Rect();
2393}
2394
2395void TabContents::RendererUnresponsive(RenderViewHost* rvh,
2396 bool is_during_unload) {
2397 if (is_during_unload) {
2398 // Hang occurred while firing the beforeunload/unload handler.
2399 // Pretend the handler fired so tab closing continues as if it had.
[email protected]829e7612009-04-25 01:15:112400 rvh->set_sudden_termination_allowed(true);
[email protected]420ae012009-04-24 05:16:322401
2402 if (!render_manager_.ShouldCloseTabOnUnresponsiveRenderer())
2403 return;
2404
2405 // If the tab hangs in the beforeunload/unload handler there's really
2406 // nothing we can do to recover. Pretend the unload listeners have
2407 // all fired and close the tab. If the hang is in the beforeunload handler
2408 // then the user will not have the option of cancelling the close.
2409 Close(rvh);
2410 return;
2411 }
2412
2413 if (render_view_host() && render_view_host()->IsRenderViewLive())
[email protected]d82ed61e2009-06-16 02:46:222414 HungRendererDialog::ShowForTabContents(this);
[email protected]420ae012009-04-24 05:16:322415}
2416
2417void TabContents::RendererResponsive(RenderViewHost* render_view_host) {
[email protected]d82ed61e2009-06-16 02:46:222418 HungRendererDialog::HideForTabContents(this);
[email protected]420ae012009-04-24 05:16:322419}
2420
2421void TabContents::LoadStateChanged(const GURL& url,
[email protected]094e5b22009-09-25 04:23:562422 net::LoadState load_state,
2423 uint64 upload_position,
2424 uint64 upload_size) {
[email protected]420ae012009-04-24 05:16:322425 load_state_ = load_state;
[email protected]094e5b22009-09-25 04:23:562426 upload_position_ = upload_position;
2427 upload_size_ = upload_size;
[email protected]d686e812009-06-03 19:10:292428 std::wstring languages =
2429 profile()->GetPrefs()->GetString(prefs::kAcceptLanguages);
2430 load_state_host_.clear();
2431 std::string host = url.host();
2432 net::IDNToUnicode(host.c_str(), host.size(), languages, &load_state_host_);
[email protected]420ae012009-04-24 05:16:322433 if (load_state_ == net::LOAD_STATE_READING_RESPONSE)
2434 SetNotWaitingForResponse();
2435 if (is_loading())
[email protected]c9cd2222009-05-06 05:16:502436 NotifyNavigationStateChanged(INVALIDATE_LOAD | INVALIDATE_TAB);
[email protected]420ae012009-04-24 05:16:322437}
2438
[email protected]1ee614862009-07-06 20:29:182439bool TabContents::IsExternalTabContainer() const {
2440 if (!delegate())
2441 return false;
2442
2443 return delegate()->IsExternalTabContainer();
2444}
2445
[email protected]1ee614862009-07-06 20:29:182446void TabContents::DidInsertCSS() {
2447 // This RVHDelegate function is used for extensions and not us.
[email protected]420ae012009-04-24 05:16:322448}
2449
[email protected]130efb02009-09-18 18:54:352450void TabContents::FocusedNodeChanged() {
2451 NotificationService::current()->Notify(
2452 NotificationType::FOCUS_CHANGED_IN_PAGE,
2453 Source<RenderViewHost>(render_view_host()),
2454 NotificationService::NoDetails());
2455}
2456
[email protected]420ae012009-04-24 05:16:322457void TabContents::FileSelected(const FilePath& path,
2458 int index, void* params) {
2459 render_view_host()->FileSelected(path);
2460}
2461
2462void TabContents::MultiFilesSelected(const std::vector<FilePath>& files,
2463 void* params) {
2464 render_view_host()->MultiFilesSelected(files);
2465}
2466
2467void TabContents::FileSelectionCanceled(void* params) {
2468 // If the user cancels choosing a file to upload we pass back an
2469 // empty vector.
2470 render_view_host()->MultiFilesSelected(std::vector<FilePath>());
2471}
2472
2473void TabContents::BeforeUnloadFiredFromRenderManager(
2474 bool proceed,
2475 bool* proceed_to_fire_unload) {
2476 if (delegate())
2477 delegate()->BeforeUnloadFired(this, proceed, proceed_to_fire_unload);
2478}
2479
2480void TabContents::UpdateRenderViewSizeForRenderManager() {
2481 // TODO(brettw) this is a hack. See TabContentsView::SizeContents.
2482 view_->SizeContents(view_->GetContainerSize());
2483}
2484
2485DOMUI* TabContents::CreateDOMUIForRenderManager(const GURL& url) {
[email protected]d82ed61e2009-06-16 02:46:222486 return DOMUIFactory::CreateDOMUIForURL(this, url);
[email protected]420ae012009-04-24 05:16:322487}
2488
2489NavigationEntry*
2490TabContents::GetLastCommittedNavigationEntryForRenderManager() {
2491 return controller_.GetLastCommittedEntry();
2492}
2493
2494bool TabContents::CreateRenderViewForRenderManager(
2495 RenderViewHost* render_view_host) {
[email protected]811bfe32009-07-01 08:46:252496 // If the pending navigation is to a DOMUI, tell the RenderView about any
2497 // bindings it will need enabled.
[email protected]420ae012009-04-24 05:16:322498 if (render_manager_.pending_dom_ui())
[email protected]811bfe32009-07-01 08:46:252499 render_view_host->AllowBindings(
2500 render_manager_.pending_dom_ui()->bindings());
[email protected]1adff062009-06-02 18:39:552501
[email protected]420ae012009-04-24 05:16:322502 RenderWidgetHostView* rwh_view = view_->CreateViewForWidget(render_view_host);
2503 if (!render_view_host->CreateRenderView())
2504 return false;
2505
2506 // Now that the RenderView has been created, we need to tell it its size.
2507 rwh_view->SetSize(view_->GetContainerSize());
2508
2509 UpdateMaxPageIDIfNecessary(render_view_host->site_instance(),
2510 render_view_host);
2511 return true;
2512}
2513
2514void TabContents::Observe(NotificationType type,
2515 const NotificationSource& source,
2516 const NotificationDetails& details) {
2517 switch (type.value) {
2518 case NotificationType::BOOKMARK_MODEL_LOADED:
2519 // BookmarkModel finished loading, fall through to update starred state.
2520 case NotificationType::URLS_STARRED: {
2521 // Somewhere, a URL has been starred.
2522 // Ignore notifications for profiles other than our current one.
2523 Profile* source_profile = Source<Profile>(source).ptr();
2524 if (!source_profile || !source_profile->IsSameProfile(profile()))
2525 return;
2526
2527 UpdateStarredStateForCurrentURL();
2528 break;
2529 }
2530 case NotificationType::PREF_CHANGED: {
2531 std::wstring* pref_name_in = Details<std::wstring>(details).ptr();
2532 DCHECK(Source<PrefService>(source).ptr() == profile()->GetPrefs());
2533 if (*pref_name_in == prefs::kAlternateErrorPagesEnabled) {
2534 UpdateAlternateErrorPageURL();
2535 } else if (*pref_name_in == prefs::kDefaultCharset ||
2536 StartsWithASCII(WideToUTF8(*pref_name_in), "webkit.webprefs.", true)
2537 ) {
2538 UpdateWebPreferences();
2539 } else {
2540 NOTREACHED() << "unexpected pref change notification" << *pref_name_in;
2541 }
2542 break;
2543 }
2544 case NotificationType::RENDER_WIDGET_HOST_DESTROYED:
2545 view_->RenderWidgetHostDestroyed(Source<RenderWidgetHost>(source).ptr());
2546 break;
2547
2548 case NotificationType::NAV_ENTRY_COMMITTED: {
2549 DCHECK(&controller_ == Source<NavigationController>(source).ptr());
2550
2551 NavigationController::LoadCommittedDetails& committed_details =
2552 *(Details<NavigationController::LoadCommittedDetails>(details).ptr());
2553 ExpireInfoBars(committed_details);
2554 break;
2555 }
2556
[email protected]d3ba77272009-09-03 00:06:092557#if defined(OS_LINUX)
2558 case NotificationType::BROWSER_THEME_CHANGED: {
2559 render_view_host()->SyncRendererPrefs();
2560 break;
2561 }
2562#endif
2563
[email protected]420ae012009-04-24 05:16:322564 default:
2565 NOTREACHED();
2566 }
2567}
[email protected]aacd7cc2009-09-02 14:51:102568
[email protected]41fc0322009-09-04 22:23:402569void TabContents::set_encoding(const std::string& encoding) {
[email protected]aacd7cc2009-09-02 14:51:102570 encoding_ = CharacterEncoding::GetCanonicalEncodingNameByAliasName(encoding);
2571}
2572