blob: c23e8baacac1273e2c4f184e9f7268a8e198042b [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]66ba4932009-06-04 19:22:1314#include "chrome/browser/blocked_popup_container.h"
[email protected]420ae012009-04-24 05:16:3215#include "chrome/browser/bookmarks/bookmark_model.h"
[email protected]96d185d2009-04-24 03:28:5416#include "chrome/browser/browser_process.h"
initial.commit09911bf2008-07-26 23:55:2917#include "chrome/browser/cert_store.h"
[email protected]aacd7cc2009-09-02 14:51:1018#include "chrome/browser/character_encoding.h"
[email protected]96d185d2009-04-24 03:28:5419#include "chrome/browser/debugger/devtools_manager.h"
[email protected]420ae012009-04-24 05:16:3220#include "chrome/browser/dom_operation_notification_details.h"
[email protected]96d185d2009-04-24 03:28:5421#include "chrome/browser/dom_ui/dom_ui.h"
22#include "chrome/browser/dom_ui/dom_ui_factory.h"
[email protected]bcef0dc02009-02-28 00:35:0223#include "chrome/browser/download/download_item_model.h"
[email protected]420ae012009-04-24 05:16:3224#include "chrome/browser/download/download_manager.h"
25#include "chrome/browser/download/download_request_manager.h"
[email protected]e7eaedde2009-09-25 20:09:4926#include "chrome/browser/external_protocol_handler.h"
[email protected]0189bc722009-08-28 21:56:4827#include "chrome/browser/favicon_service.h"
[email protected]19289d02009-10-21 21:47:0028#include "chrome/browser/form_field_history_manager.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]84abba62009-10-07 17:01:4435#include "chrome/browser/metrics/metric_event_duration_details.h"
[email protected]ea0e80892009-08-22 00:36:0536#include "chrome/browser/modal_html_dialog_delegate.h"
[email protected]c8865482009-07-23 20:40:1037#include "chrome/browser/omnibox_search_hint.h"
[email protected]fdd61c62009-04-22 19:22:5738#include "chrome/browser/password_manager/password_manager.h"
39#include "chrome/browser/plugin_installer.h"
[email protected]ce560f82009-06-03 09:39:4440#include "chrome/browser/profile.h"
[email protected]1db6ff152009-10-12 15:32:0741#include "chrome/browser/renderer_host/render_process_host.h"
[email protected]96d185d2009-04-24 03:28:5442#include "chrome/browser/renderer_host/render_widget_host_view.h"
[email protected]fd694982009-07-03 17:32:3743#include "chrome/browser/renderer_host/resource_request_details.h"
[email protected]1db6ff152009-10-12 15:32:0744#include "chrome/browser/renderer_host/site_instance.h"
[email protected]96d185d2009-04-24 03:28:5445#include "chrome/browser/renderer_host/web_cache_manager.h"
[email protected]bd1ad682009-05-15 22:19:1746#include "chrome/browser/tab_contents/infobar_delegate.h"
[email protected]7e383692009-06-12 19:14:5447#include "chrome/browser/tab_contents/interstitial_page.h"
[email protected]f3ec7742009-01-15 00:59:1648#include "chrome/browser/tab_contents/navigation_entry.h"
[email protected]de34a962009-06-26 23:22:4349#include "chrome/browser/tab_contents/provisional_load_details.h"
[email protected]f3ec7742009-01-15 00:59:1650#include "chrome/browser/tab_contents/tab_contents_delegate.h"
[email protected]fdd61c62009-04-22 19:22:5751#include "chrome/browser/tab_contents/tab_contents_view.h"
[email protected]58dca552009-06-17 00:35:0252#include "chrome/browser/tab_contents/thumbnail_generator.h"
[email protected]449478302009-06-09 20:04:2853#include "chrome/browser/thumbnail_store.h"
[email protected]420ae012009-04-24 05:16:3254#include "chrome/browser/search_engines/template_url_fetcher.h"
55#include "chrome/browser/search_engines/template_url_model.h"
[email protected]35f7d212009-04-29 21:19:2756#include "chrome/common/chrome_switches.h"
[email protected]ba69a7d2009-09-28 21:09:5657#include "chrome/common/extensions/extension_action.h"
[email protected]bfd04a62009-02-01 18:16:5658#include "chrome/common/notification_service.h"
initial.commit09911bf2008-07-26 23:55:2959#include "chrome/common/pref_names.h"
[email protected]1eb89e82008-08-15 12:27:0360#include "chrome/common/pref_service.h"
[email protected]420ae012009-04-24 05:16:3261#include "chrome/common/render_messages.h"
[email protected]1ee614862009-07-06 20:29:1862#include "chrome/common/renderer_preferences.h"
[email protected]96d185d2009-04-24 03:28:5463#include "chrome/common/url_constants.h"
[email protected]8897c382009-05-06 17:53:2664#include "grit/generated_resources.h"
[email protected]420ae012009-04-24 05:16:3265#include "grit/locale_settings.h"
66#include "net/base/mime_util.h"
67#include "net/base/net_errors.h"
[email protected]d686e812009-06-03 19:10:2968#include "net/base/net_util.h"
[email protected]420ae012009-04-24 05:16:3269#include "net/base/registry_controlled_domain.h"
[email protected]1ee614862009-07-06 20:29:1870#include "webkit/glue/password_form.h"
71#include "webkit/glue/webpreferences.h"
[email protected]3e45ba92009-02-20 21:09:0072
[email protected]304f8792009-08-19 21:28:0973#if defined(OS_CHROMEOS)
74// For GdkScreen
75#include <gdk/gdk.h>
[email protected]0ba5c5e2009-08-20 16:47:1376#endif // defined(OS_CHROMEOS)
77
78#if defined(OS_LINUX)
79#include "chrome/browser/gtk/create_application_shortcuts_dialog_gtk.h"
[email protected]d3ba77272009-09-03 00:06:0980#include "chrome/browser/gtk/gtk_theme_provider.h"
[email protected]0ba5c5e2009-08-20 16:47:1381#endif // defined(OS_LINUX)
82
[email protected]420ae012009-04-24 05:16:3283// Cross-Site Navigations
84//
85// If a TabContents is told to navigate to a different web site (as determined
86// by SiteInstance), it will replace its current RenderViewHost with a new
87// RenderViewHost dedicated to the new SiteInstance. This works as follows:
88//
89// - Navigate determines whether the destination is cross-site, and if so,
90// it creates a pending_render_view_host_ and moves into the PENDING
91// RendererState.
92// - The pending RVH is "suspended," so that no navigation messages are sent to
93// its renderer until the onbeforeunload JavaScript handler has a chance to
94// run in the current RVH.
95// - The pending RVH tells CrossSiteRequestManager (a thread-safe singleton)
96// that it has a pending cross-site request. ResourceDispatcherHost will
97// check for this when the response arrives.
98// - The current RVH runs its onbeforeunload handler. If it returns false, we
99// cancel all the pending logic and go back to NORMAL. Otherwise we allow
100// the pending RVH to send the navigation request to its renderer.
101// - ResourceDispatcherHost receives a ResourceRequest on the IO thread. It
102// checks CrossSiteRequestManager to see that the RVH responsible has a
103// pending cross-site request, and then installs a CrossSiteEventHandler.
104// - When RDH receives a response, the BufferedEventHandler determines whether
105// it is a download. If so, it sends a message to the new renderer causing
106// it to cancel the request, and the download proceeds in the download
107// thread. For now, we stay in a PENDING state (with a pending RVH) until
[email protected]57c6a652009-05-04 07:58:34108// the next DidNavigate event for this TabContents. This isn't ideal, but it
[email protected]420ae012009-04-24 05:16:32109// doesn't affect any functionality.
110// - After RDH receives a response and determines that it is safe and not a
111// download, it pauses the response to first run the old page's onunload
112// handler. It does this by asynchronously calling the OnCrossSiteResponse
[email protected]57c6a652009-05-04 07:58:34113// method of TabContents on the UI thread, which sends a ClosePage message
[email protected]420ae012009-04-24 05:16:32114// to the current RVH.
115// - Once the onunload handler is finished, a ClosePage_ACK message is sent to
116// the ResourceDispatcherHost, who unpauses the response. Data is then sent
117// to the pending RVH.
118// - The pending renderer sends a FrameNavigate message that invokes the
119// DidNavigate method. This replaces the current RVH with the
120// pending RVH and goes back to the NORMAL RendererState.
121
122namespace {
123
124// Amount of time we wait between when a key event is received and the renderer
125// is queried for its state and pushed to the NavigationEntry.
126const int kQueryStateDelay = 5000;
127
128const int kSyncWaitDelay = 40;
129
[email protected]304f8792009-08-19 21:28:09130#if defined(OS_CHROMEOS)
131// If a popup window is bigger than this fraction of the screen on chrome os,
132// turn it into a tab
133const float kMaxWidthFactor = 0.5;
134const float kMaxHeightFactor = 0.6;
135#endif
136
[email protected]420ae012009-04-24 05:16:32137// If another javascript message box is displayed within
138// kJavascriptMessageExpectedDelay of a previous javascript message box being
139// dismissed, display an option to suppress future message boxes from this
140// contents.
141const int kJavascriptMessageExpectedDelay = 1000;
142
[email protected]420ae012009-04-24 05:16:32143// The list of prefs we want to observe.
144const wchar_t* kPrefsToObserve[] = {
145 prefs::kAlternateErrorPagesEnabled,
146 prefs::kWebKitJavaEnabled,
147 prefs::kWebKitJavascriptEnabled,
148 prefs::kWebKitLoadsImagesAutomatically,
149 prefs::kWebKitPluginsEnabled,
150 prefs::kWebKitUsesUniversalDetector,
151 prefs::kWebKitSerifFontFamily,
152 prefs::kWebKitSansSerifFontFamily,
153 prefs::kWebKitFixedFontFamily,
154 prefs::kWebKitDefaultFontSize,
155 prefs::kWebKitDefaultFixedFontSize,
156 prefs::kDefaultCharset
157 // kWebKitStandardFontIsSerif needs to be added
158 // if we let users pick which font to use, serif or sans-serif when
159 // no font is specified or a CSS generic family (serif or sans-serif)
160 // is not specified.
161};
162
163const int kPrefsToObserveLength = arraysize(kPrefsToObserve);
164
[email protected]420ae012009-04-24 05:16:32165// Returns true if the entry's transition type is FORM_SUBMIT.
166bool IsFormSubmit(const NavigationEntry* entry) {
167 return (PageTransition::StripQualifier(entry->transition_type()) ==
168 PageTransition::FORM_SUBMIT);
169}
170
171#if defined(OS_WIN)
172
173BOOL CALLBACK InvalidateWindow(HWND hwnd, LPARAM lparam) {
174 // Note: erase is required to properly paint some widgets borders. This can
175 // be seen with textfields.
176 InvalidateRect(hwnd, NULL, TRUE);
177 return TRUE;
178}
179#endif
180
[email protected]056de2d2009-06-26 16:41:34181void MakeNavigateParams(const NavigationEntry& entry, bool reload,
182 ViewMsg_Navigate_Params* params) {
183 params->page_id = entry.page_id();
184 params->url = entry.url();
185 params->referrer = entry.referrer();
186 params->transition = entry.transition_type();
187 params->state = entry.content_state();
188 params->reload = reload;
189 params->request_time = base::Time::Now();
190}
191
[email protected]420ae012009-04-24 05:16:32192} // namespace
193
194// -----------------------------------------------------------------------------
195
[email protected]fdd61c62009-04-22 19:22:57196// static
197int TabContents::find_request_id_counter_ = -1;
198
[email protected]420ae012009-04-24 05:16:32199class TabContents::GearsCreateShortcutCallbackFunctor {
200 public:
201 explicit GearsCreateShortcutCallbackFunctor(TabContents* contents)
202 : contents_(contents) {}
203
204 void Run(const GearsShortcutData2& shortcut_data, bool success) {
205 if (contents_)
206 contents_->OnGearsCreateShortcutDone(shortcut_data, success);
207 delete this;
208 }
209 void Cancel() {
210 contents_ = NULL;
211 }
212
213 private:
214 TabContents* contents_;
215};
216
[email protected]420ae012009-04-24 05:16:32217TabContents::TabContents(Profile* profile,
218 SiteInstance* site_instance,
219 int routing_id,
[email protected]34ac70502009-09-25 17:07:23220 const TabContents* base_tab_contents)
[email protected]b680ad22009-04-15 23:19:42221 : delegate_(NULL),
[email protected]66ba4932009-06-04 19:22:13222 ALLOW_THIS_IN_INITIALIZER_LIST(controller_(this, profile)),
223 ALLOW_THIS_IN_INITIALIZER_LIST(view_(
[email protected]d82ed61e2009-06-16 02:46:22224 TabContentsView::Create(this))),
225 ALLOW_THIS_IN_INITIALIZER_LIST(render_manager_(this, this)),
[email protected]fdd61c62009-04-22 19:22:57226 property_bag_(),
227 registrar_(),
[email protected]d82ed61e2009-06-16 02:46:22228 ALLOW_THIS_IN_INITIALIZER_LIST(printing_(*this)),
[email protected]fdd61c62009-04-22 19:22:57229 save_package_(),
230 cancelable_consumer_(),
[email protected]19289d02009-10-21 21:47:00231 form_field_history_manager_(),
[email protected]fdd61c62009-04-22 19:22:57232 password_manager_(),
233 plugin_installer_(),
[email protected]d82ed61e2009-06-16 02:46:22234 ALLOW_THIS_IN_INITIALIZER_LIST(fav_icon_helper_(this)),
[email protected]fdd61c62009-04-22 19:22:57235 select_file_dialog_(),
236 pending_install_(),
[email protected]d5f942ba2008-09-26 19:30:34237 is_loading_(false),
initial.commit09911bf2008-07-26 23:55:29238 is_crashed_(false),
[email protected]d5f942ba2008-09-26 19:30:34239 waiting_for_response_(false),
[email protected]d5f942ba2008-09-26 19:30:34240 max_page_id_(-1),
[email protected]fdd61c62009-04-22 19:22:57241 current_load_start_(),
242 load_state_(net::LOAD_STATE_IDLE),
243 load_state_host_(),
[email protected]094e5b22009-09-25 04:23:56244 upload_size_(0),
245 upload_position_(0),
[email protected]fdd61c62009-04-22 19:22:57246 received_page_title_(false),
247 is_starred_(false),
248 contents_mime_type_(),
249 encoding_(),
[email protected]332af7732009-03-11 13:21:35250 blocked_popups_(NULL),
[email protected]fdd61c62009-04-22 19:22:57251 infobar_delegates_(),
[email protected]fdd61c62009-04-22 19:22:57252 find_ui_active_(false),
253 find_op_aborted_(false),
254 current_find_request_id_(find_request_id_counter_++),
255 find_text_(),
[email protected]e491f1c2009-05-22 20:28:12256 last_search_case_sensitive_(false),
257 last_search_prepopulate_text_(NULL),
258 last_search_result_(),
[email protected]fdd61c62009-04-22 19:22:57259 capturing_contents_(false),
260 is_being_destroyed_(false),
261 notify_disconnection_(false),
262 history_requests_(),
263#if defined(OS_WIN)
264 message_box_active_(CreateEvent(NULL, TRUE, FALSE, NULL)),
265#endif
266 last_javascript_message_dismissal_(),
[email protected]c2e74fe82009-09-03 17:57:44267 suppress_javascript_messages_(false),
[email protected]7ab1e7d62009-10-14 23:32:01268 is_showing_before_unload_dialog_(false),
[email protected]c2e74fe82009-09-03 17:57:44269 opener_dom_ui_type_(DOMUIFactory::kNoDOMUI) {
[email protected]420ae012009-04-24 05:16:32270 pending_install_.page_id = 0;
271 pending_install_.callback_functor = NULL;
272
[email protected]16779842009-07-08 23:45:29273#if defined(OS_CHROMEOS)
[email protected]58dca552009-06-17 00:35:02274 // Make sure the thumbnailer is started before starting the render manager.
275 // The thumbnailer will want to listen for RVH creations, one of which will
276 // happen in RVHManager::Init.
277 ThumbnailGenerator* generator = g_browser_process->GetThumbnailGenerator();
278 if (generator)
279 generator->StartThumbnailing();
280#endif
281
[email protected]12636df2009-09-28 22:32:21282 render_manager_.Init(profile, site_instance, routing_id);
[email protected]420ae012009-04-24 05:16:32283
[email protected]34ac70502009-09-25 17:07:23284 // We have the initial size of the view be based on the size of the passed in
285 // tab contents (normally a tab from the same window).
286 view_->CreateView(base_tab_contents ?
287 base_tab_contents->view()->GetContainerSize() : gfx::Size());
[email protected]420ae012009-04-24 05:16:32288
289 // Register for notifications about all interested prefs change.
290 PrefService* prefs = profile->GetPrefs();
291 if (prefs) {
292 for (int i = 0; i < kPrefsToObserveLength; ++i)
293 prefs->AddPrefObserver(kPrefsToObserve[i], this);
294 }
295
296 // Register for notifications about URL starredness changing on any profile.
297 registrar_.Add(this, NotificationType::URLS_STARRED,
298 NotificationService::AllSources());
299 registrar_.Add(this, NotificationType::BOOKMARK_MODEL_LOADED,
300 NotificationService::AllSources());
301 registrar_.Add(this, NotificationType::RENDER_WIDGET_HOST_DESTROYED,
302 NotificationService::AllSources());
[email protected]d3ba77272009-09-03 00:06:09303#if defined(OS_LINUX)
304 registrar_.Add(this, NotificationType::BROWSER_THEME_CHANGED,
305 NotificationService::AllSources());
306#endif
[email protected]420ae012009-04-24 05:16:32307
308 // Keep a global copy of the previous search string (if any).
309 static string16 global_last_search = string16();
[email protected]e491f1c2009-05-22 20:28:12310 last_search_prepopulate_text_ = &global_last_search;
[email protected]840b1512009-07-21 16:53:46311
[email protected]c8865482009-07-23 20:40:10312 // Set-up the showing of the omnibox search infobar if applicable.
313 if (OmniboxSearchHint::IsEnabled(profile))
314 omnibox_search_hint_.reset(new OmniboxSearchHint(this));
315
[email protected]840b1512009-07-21 16:53:46316 view_->InitRendererPrefs(&renderer_preferences_);
[email protected]332af7732009-03-11 13:21:35317}
initial.commit09911bf2008-07-26 23:55:29318
319TabContents::~TabContents() {
[email protected]420ae012009-04-24 05:16:32320 is_being_destroyed_ = true;
321
[email protected]bfe4c1582009-10-02 18:11:09322 // We don't want any notifications while we're running our destructor.
[email protected]420ae012009-04-24 05:16:32323 registrar_.RemoveAll();
324
325 // Unregister the notifications of all observed prefs change.
326 PrefService* prefs = profile()->GetPrefs();
327 if (prefs) {
328 for (int i = 0; i < kPrefsToObserveLength; ++i)
329 prefs->RemovePrefObserver(kPrefsToObserve[i], this);
330 }
331
[email protected]420ae012009-04-24 05:16:32332 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
[email protected]7ab1e7d62009-10-14 23:32:01378
379 // OnCloseStarted isn't called in unit tests.
380 if (!tab_close_start_time_.is_null()) {
381 UMA_HISTOGRAM_TIMES("Tab.Close",
382 base::TimeTicks::Now() - tab_close_start_time_);
383 }
initial.commit09911bf2008-07-26 23:55:29384}
385
[email protected]d5f942ba2008-09-26 19:30:34386// static
387void TabContents::RegisterUserPrefs(PrefService* prefs) {
[email protected]420ae012009-04-24 05:16:32388 prefs->RegisterBooleanPref(prefs::kAlternateErrorPagesEnabled, true);
389
390 WebPreferences pref_defaults;
391 prefs->RegisterBooleanPref(prefs::kWebKitJavascriptEnabled,
392 pref_defaults.javascript_enabled);
393 prefs->RegisterBooleanPref(prefs::kWebKitWebSecurityEnabled,
394 pref_defaults.web_security_enabled);
395 prefs->RegisterBooleanPref(
396 prefs::kWebKitJavascriptCanOpenWindowsAutomatically, true);
397 prefs->RegisterBooleanPref(prefs::kWebKitLoadsImagesAutomatically,
398 pref_defaults.loads_images_automatically);
399 prefs->RegisterBooleanPref(prefs::kWebKitPluginsEnabled,
400 pref_defaults.plugins_enabled);
401 prefs->RegisterBooleanPref(prefs::kWebKitDomPasteEnabled,
402 pref_defaults.dom_paste_enabled);
403 prefs->RegisterBooleanPref(prefs::kWebKitShrinksStandaloneImagesToFit,
404 pref_defaults.shrinks_standalone_images_to_fit);
[email protected]0df30122009-06-03 12:13:08405 prefs->RegisterStringPref(prefs::kWebKitInspectorSettings,
[email protected]8922e1f2009-10-03 05:01:26406 UTF8ToWide(pref_defaults.inspector_settings));
[email protected]420ae012009-04-24 05:16:32407 prefs->RegisterBooleanPref(prefs::kWebKitTextAreasAreResizable,
408 pref_defaults.text_areas_are_resizable);
409 prefs->RegisterBooleanPref(prefs::kWebKitJavaEnabled,
410 pref_defaults.java_enabled);
411
412 prefs->RegisterLocalizedStringPref(prefs::kAcceptLanguages,
413 IDS_ACCEPT_LANGUAGES);
414 prefs->RegisterLocalizedStringPref(prefs::kDefaultCharset,
415 IDS_DEFAULT_ENCODING);
416 prefs->RegisterLocalizedBooleanPref(prefs::kWebKitStandardFontIsSerif,
417 IDS_STANDARD_FONT_IS_SERIF);
418 prefs->RegisterLocalizedStringPref(prefs::kWebKitFixedFontFamily,
419 IDS_FIXED_FONT_FAMILY);
420 prefs->RegisterLocalizedStringPref(prefs::kWebKitSerifFontFamily,
421 IDS_SERIF_FONT_FAMILY);
422 prefs->RegisterLocalizedStringPref(prefs::kWebKitSansSerifFontFamily,
423 IDS_SANS_SERIF_FONT_FAMILY);
424 prefs->RegisterLocalizedStringPref(prefs::kWebKitCursiveFontFamily,
425 IDS_CURSIVE_FONT_FAMILY);
426 prefs->RegisterLocalizedStringPref(prefs::kWebKitFantasyFontFamily,
427 IDS_FANTASY_FONT_FAMILY);
428 prefs->RegisterLocalizedIntegerPref(prefs::kWebKitDefaultFontSize,
429 IDS_DEFAULT_FONT_SIZE);
430 prefs->RegisterLocalizedIntegerPref(prefs::kWebKitDefaultFixedFontSize,
431 IDS_DEFAULT_FIXED_FONT_SIZE);
432 prefs->RegisterLocalizedIntegerPref(prefs::kWebKitMinimumFontSize,
433 IDS_MINIMUM_FONT_SIZE);
434 prefs->RegisterLocalizedIntegerPref(prefs::kWebKitMinimumLogicalFontSize,
435 IDS_MINIMUM_LOGICAL_FONT_SIZE);
436 prefs->RegisterLocalizedBooleanPref(prefs::kWebKitUsesUniversalDetector,
437 IDS_USES_UNIVERSAL_DETECTOR);
438 prefs->RegisterLocalizedStringPref(prefs::kStaticEncodings,
439 IDS_STATIC_ENCODING_LIST);
initial.commit09911bf2008-07-26 23:55:29440}
441
[email protected]13367f72009-07-08 16:39:02442// Returns true if contains content rendered by an extension.
443bool TabContents::HostsExtension() const {
444 return GetURL().SchemeIs(chrome::kExtensionScheme);
[email protected]d5f942ba2008-09-26 19:30:34445}
446
[email protected]6e95706d2009-04-23 22:55:01447PasswordManager* TabContents::GetPasswordManager() {
448 if (password_manager_.get() == NULL)
449 password_manager_.reset(new PasswordManager(this));
450 return password_manager_.get();
451}
452
453PluginInstaller* TabContents::GetPluginInstaller() {
454 if (plugin_installer_.get() == NULL)
455 plugin_installer_.reset(new PluginInstaller(this));
456 return plugin_installer_.get();
457}
458
[email protected]d5f942ba2008-09-26 19:30:34459const GURL& TabContents::GetURL() const {
460 // We may not have a navigation entry yet
[email protected]ce3fa3c2009-04-20 19:55:57461 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]ebe89e062009-08-13 23:16:54462 return entry ? entry->virtual_url() : GURL::EmptyGURL();
[email protected]d5f942ba2008-09-26 19:30:34463}
464
[email protected]96d185d2009-04-24 03:28:54465const string16& TabContents::GetTitle() const {
[email protected]4c6092c5b2009-06-06 00:23:55466 // Transient entries take precedence. They are used for interstitial pages
467 // that are shown on top of existing pages.
468 NavigationEntry* entry = controller_.GetTransientEntry();
469 if (entry)
470 return entry->GetTitleForDisplay(&controller_);
471
[email protected]96d185d2009-04-24 03:28:54472 DOMUI* our_dom_ui = render_manager_.pending_dom_ui() ?
473 render_manager_.pending_dom_ui() : render_manager_.dom_ui();
474 if (our_dom_ui) {
475 // Don't override the title in view source mode.
[email protected]4c6092c5b2009-06-06 00:23:55476 entry = controller_.GetActiveEntry();
[email protected]96d185d2009-04-24 03:28:54477 if (!(entry && entry->IsViewSourceMode())) {
478 // Give the DOM UI the chance to override our title.
479 const string16& title = our_dom_ui->overridden_title();
480 if (!title.empty())
481 return title;
482 }
483 }
484
485 // We use the title for the last committed entry rather than a pending
486 // navigation entry. For example, when the user types in a URL, we want to
487 // keep the old page's title until the new load has committed and we get a new
488 // title.
[email protected]96d185d2009-04-24 03:28:54489 entry = controller_.GetLastCommittedEntry();
490 if (entry)
491 return entry->GetTitleForDisplay(&controller_);
[email protected]96d185d2009-04-24 03:28:54492 return EmptyString16();
493}
494
[email protected]d5f942ba2008-09-26 19:30:34495int32 TabContents::GetMaxPageID() {
496 if (GetSiteInstance())
497 return GetSiteInstance()->max_page_id();
498 else
499 return max_page_id_;
500}
501
502void TabContents::UpdateMaxPageID(int32 page_id) {
503 // Ensure both the SiteInstance and RenderProcessHost update their max page
[email protected]57c6a652009-05-04 07:58:34504 // IDs in sync. Only TabContents will also have site instances, except during
[email protected]d5f942ba2008-09-26 19:30:34505 // testing.
506 if (GetSiteInstance())
507 GetSiteInstance()->UpdateMaxPageID(page_id);
[email protected]57c6a652009-05-04 07:58:34508 process()->UpdateMaxPageID(page_id);
[email protected]d5f942ba2008-09-26 19:30:34509}
510
[email protected]96d185d2009-04-24 03:28:54511SiteInstance* TabContents::GetSiteInstance() const {
512 return render_manager_.current_host()->site_instance();
513}
514
[email protected]d5f942ba2008-09-26 19:30:34515const std::wstring TabContents::GetDefaultTitle() const {
516 return l10n_util::GetString(IDS_DEFAULT_TAB_TITLE);
517}
518
[email protected]96d185d2009-04-24 03:28:54519bool TabContents::ShouldDisplayURL() {
520 // Don't hide the url in view source mode.
521 NavigationEntry* entry = controller_.GetActiveEntry();
522 if (entry && entry->IsViewSourceMode())
523 return true;
[email protected]83e3895b2009-06-11 00:07:16524
[email protected]96d185d2009-04-24 03:28:54525 DOMUI* dom_ui = GetDOMUIForCurrentState();
526 if (dom_ui)
527 return !dom_ui->should_hide_url();
528 return true;
529}
530
[email protected]d5f942ba2008-09-26 19:30:34531SkBitmap TabContents::GetFavIcon() const {
532 // Like GetTitle(), we also want to use the favicon for the last committed
533 // entry rather than a pending navigation entry.
[email protected]ce3fa3c2009-04-20 19:55:57534 NavigationEntry* entry = controller_.GetTransientEntry();
[email protected]cbab76d2008-10-13 22:42:47535 if (entry)
536 return entry->favicon().bitmap();
537
[email protected]ce3fa3c2009-04-20 19:55:57538 entry = controller_.GetLastCommittedEntry();
[email protected]d5f942ba2008-09-26 19:30:34539 if (entry)
540 return entry->favicon().bitmap();
[email protected]d5f942ba2008-09-26 19:30:34541 return SkBitmap();
542}
543
[email protected]0b303cc2009-09-28 22:35:15544bool TabContents::FavIconIsValid() const {
545 NavigationEntry* entry = controller_.GetTransientEntry();
546 if (entry)
547 return entry->favicon().is_valid();
548
549 entry = controller_.GetLastCommittedEntry();
550 if (entry)
551 return entry->favicon().is_valid();
552
553 return false;
554}
555
[email protected]96d185d2009-04-24 03:28:54556bool TabContents::ShouldDisplayFavIcon() {
557 // Always display a throbber during pending loads.
558 if (controller_.GetLastCommittedEntry() && controller_.pending_entry())
559 return true;
560
561 DOMUI* dom_ui = GetDOMUIForCurrentState();
562 if (dom_ui)
563 return !dom_ui->hide_favicon();
564 return true;
565}
566
[email protected]96d185d2009-04-24 03:28:54567std::wstring TabContents::GetStatusText() const {
568 if (!is_loading() || load_state_ == net::LOAD_STATE_IDLE)
569 return std::wstring();
570
571 switch (load_state_) {
572 case net::LOAD_STATE_WAITING_FOR_CACHE:
573 return l10n_util::GetString(IDS_LOAD_STATE_WAITING_FOR_CACHE);
574 case net::LOAD_STATE_RESOLVING_PROXY_FOR_URL:
575 return l10n_util::GetString(IDS_LOAD_STATE_RESOLVING_PROXY_FOR_URL);
576 case net::LOAD_STATE_RESOLVING_HOST:
577 return l10n_util::GetString(IDS_LOAD_STATE_RESOLVING_HOST);
578 case net::LOAD_STATE_CONNECTING:
579 return l10n_util::GetString(IDS_LOAD_STATE_CONNECTING);
580 case net::LOAD_STATE_SENDING_REQUEST:
[email protected]094e5b22009-09-25 04:23:56581 if (upload_size_)
582 return l10n_util::GetStringF(
583 IDS_LOAD_STATE_SENDING_REQUEST_WITH_PROGRESS,
584 static_cast<int>((100 * upload_position_) / upload_size_));
585 else
586 return l10n_util::GetString(IDS_LOAD_STATE_SENDING_REQUEST);
[email protected]96d185d2009-04-24 03:28:54587 case net::LOAD_STATE_WAITING_FOR_RESPONSE:
588 return l10n_util::GetStringF(IDS_LOAD_STATE_WAITING_FOR_RESPONSE,
589 load_state_host_);
590 // Ignore net::LOAD_STATE_READING_RESPONSE and net::LOAD_STATE_IDLE
591 case net::LOAD_STATE_IDLE:
592 case net::LOAD_STATE_READING_RESPONSE:
593 break;
594 }
595
596 return std::wstring();
597}
598
[email protected]d5f942ba2008-09-26 19:30:34599void TabContents::SetIsCrashed(bool state) {
600 if (state == is_crashed_)
601 return;
602
603 is_crashed_ = state;
[email protected]c9cd2222009-05-06 05:16:50604 NotifyNavigationStateChanged(INVALIDATE_TAB);
[email protected]d5f942ba2008-09-26 19:30:34605}
606
[email protected]ba69a7d2009-09-28 21:09:56607void TabContents::SetPageActionEnabled(const ExtensionAction* page_action,
[email protected]d7eaf572009-07-01 21:57:00608 bool enable,
609 const std::string& title,
610 int icon_id) {
[email protected]ce5c4502009-05-06 16:46:11611 DCHECK(page_action);
[email protected]744ef172009-10-16 21:53:46612 ExtensionActionState* state = GetOrCreatePageActionState(page_action);
613 state->set_hidden(!enable);
614 state->set_title(title);
615 state->set_icon_index(icon_id);
616 state->set_icon(NULL);
[email protected]ce5c4502009-05-06 16:46:11617}
618
[email protected]ba69a7d2009-09-28 21:09:56619const ExtensionActionState* TabContents::GetPageActionState(
620 const ExtensionAction* page_action) {
[email protected]744ef172009-10-16 21:53:46621 if (page_actions_.end() == page_actions_.find(page_action))
[email protected]d7eaf572009-07-01 21:57:00622 return NULL;
[email protected]ce5c4502009-05-06 16:46:11623
[email protected]744ef172009-10-16 21:53:46624 return page_actions_[page_action].get();
625}
626
627ExtensionActionState* TabContents::GetOrCreatePageActionState(
628 const ExtensionAction* page_action) {
629 if (page_actions_.end() == page_actions_.find(page_action)) {
630 page_actions_[page_action].reset(
631 new ExtensionActionState(page_action->title(), 0));
632 }
633
634 return page_actions_[page_action].get();
635}
636
637void TabContents::PageActionStateChanged() {
638 NotifyNavigationStateChanged(TabContents::INVALIDATE_PAGE_ACTIONS);
[email protected]d7eaf572009-07-01 21:57:00639}
[email protected]ce5c4502009-05-06 16:46:11640
[email protected]d5f942ba2008-09-26 19:30:34641void TabContents::NotifyNavigationStateChanged(unsigned changed_flags) {
642 if (delegate_)
643 delegate_->NavigationStateChanged(this, changed_flags);
644}
645
[email protected]96d185d2009-04-24 03:28:54646void TabContents::DidBecomeSelected() {
647 controller_.SetActive(true);
[email protected]789e9152009-08-04 21:59:43648 if (render_widget_host_view()) {
[email protected]96d185d2009-04-24 03:28:54649 render_widget_host_view()->DidBecomeSelected();
[email protected]789e9152009-08-04 21:59:43650#if defined(OS_MACOSX)
651 render_widget_host_view()->SetActive(true);
652#endif
653 }
[email protected]96d185d2009-04-24 03:28:54654
[email protected]76543b92009-08-31 17:27:45655 WebCacheManager::GetInstance()->ObserveActivity(process()->id());
[email protected]96d185d2009-04-24 03:28:54656}
657
658void TabContents::WasHidden() {
659 if (!capturing_contents()) {
660 // |render_view_host()| can be NULL if the user middle clicks a link to open
661 // a tab in then background, then closes the tab before selecting it. This
662 // is because closing the tab calls TabContents::Destroy(), which removes
663 // the |render_view_host()|; then when we actually destroy the window,
664 // OnWindowPosChanged() notices and calls HideContents() (which calls us).
665 if (render_widget_host_view())
666 render_widget_host_view()->WasHidden();
[email protected]96d185d2009-04-24 03:28:54667 }
668
669 NotificationService::current()->Notify(
670 NotificationType::TAB_CONTENTS_HIDDEN,
671 Source<TabContents>(this),
672 NotificationService::NoDetails());
673}
674
[email protected]d5f942ba2008-09-26 19:30:34675void TabContents::Activate() {
676 if (delegate_)
677 delegate_->ActivateContents(this);
678}
679
[email protected]96d185d2009-04-24 03:28:54680void TabContents::ShowContents() {
681 if (render_widget_host_view())
682 render_widget_host_view()->DidBecomeSelected();
[email protected]96d185d2009-04-24 03:28:54683}
684
685void TabContents::HideContents() {
686 // TODO(pkasting): http://b/1239839 Right now we purposefully don't call
687 // our superclass HideContents(), because some callers want to be very picky
688 // about the order in which these get called. In addition to making the code
689 // here practically impossible to understand, this also means we end up
690 // calling TabContents::WasHidden() twice if callers call both versions of
[email protected]57c6a652009-05-04 07:58:34691 // HideContents() on a TabContents.
[email protected]96d185d2009-04-24 03:28:54692 WasHidden();
693}
694
[email protected]c0588052008-10-27 23:01:50695void TabContents::OpenURL(const GURL& url, const GURL& referrer,
[email protected]d5f942ba2008-09-26 19:30:34696 WindowOpenDisposition disposition,
697 PageTransition::Type transition) {
698 if (delegate_)
[email protected]c0588052008-10-27 23:01:50699 delegate_->OpenURLFromTab(this, url, referrer, disposition, transition);
[email protected]d5f942ba2008-09-26 19:30:34700}
701
702bool TabContents::NavigateToPendingEntry(bool reload) {
[email protected]96d185d2009-04-24 03:28:54703 const NavigationEntry& entry = *controller_.pending_entry();
704
705 RenderViewHost* dest_render_view_host = render_manager_.Navigate(entry);
706 if (!dest_render_view_host)
707 return false; // Unable to create the desired render view host.
708
709 // Tell DevTools agent that it is attached prior to the navigation.
[email protected]d9f9b792009-06-24 13:17:12710 DevToolsManager* devtools_manager = DevToolsManager::GetInstance();
[email protected]7e8e6b62009-05-08 11:28:32711 if (devtools_manager) { // NULL in unit tests.
[email protected]684b4ba22009-05-07 06:27:41712 devtools_manager->OnNavigatingToPendingEntry(
713 render_view_host(),
714 dest_render_view_host,
715 controller_.pending_entry()->url());
[email protected]7e8e6b62009-05-08 11:28:32716 }
[email protected]96d185d2009-04-24 03:28:54717
718 // Used for page load time metrics.
719 current_load_start_ = base::TimeTicks::Now();
720
721 // Navigate in the desired RenderViewHost.
[email protected]056de2d2009-06-26 16:41:34722 ViewMsg_Navigate_Params navigate_params;
723 MakeNavigateParams(entry, reload, &navigate_params);
724 dest_render_view_host->Navigate(navigate_params);
[email protected]96d185d2009-04-24 03:28:54725
726 if (entry.page_id() == -1) {
727 // HACK!! This code suppresses javascript: URLs from being added to
728 // session history, which is what we want to do for javascript: URLs that
729 // do not generate content. What we really need is a message from the
730 // renderer telling us that a new page was not created. The same message
731 // could be used for mailto: URLs and the like.
732 if (entry.url().SchemeIs(chrome::kJavaScriptScheme))
733 return false;
734 }
735
736 // Clear any provisional password saves - this stops password infobars
737 // showing up on pages the user navigates to while the right page is
738 // loading.
739 GetPasswordManager()->ClearProvisionalSave();
740
741 if (reload && !profile()->IsOffTheRecord()) {
[email protected]0189bc722009-08-28 21:56:48742 FaviconService* favicon_service =
743 profile()->GetFaviconService(Profile::IMPLICIT_ACCESS);
744 if (favicon_service)
745 favicon_service->SetFaviconOutOfDateForPage(entry.url());
[email protected]96d185d2009-04-24 03:28:54746 }
747
[email protected]d5f942ba2008-09-26 19:30:34748 return true;
749}
750
[email protected]96d185d2009-04-24 03:28:54751void TabContents::Stop() {
752 render_manager_.Stop();
753 printing_.Stop();
754}
755
756void TabContents::Cut() {
757 render_view_host()->Cut();
758}
759
760void TabContents::Copy() {
761 render_view_host()->Copy();
762}
763
[email protected]c0cc3092009-09-12 08:27:27764#if defined(OS_MACOSX)
[email protected]a954bf72009-09-12 07:30:35765void TabContents::CopyToFindPboard() {
766 render_view_host()->CopyToFindPboard();
767}
[email protected]c0cc3092009-09-12 08:27:27768#endif
[email protected]a954bf72009-09-12 07:30:35769
[email protected]96d185d2009-04-24 03:28:54770void TabContents::Paste() {
771 render_view_host()->Paste();
772}
773
774void TabContents::DisassociateFromPopupCount() {
775 render_view_host()->DisassociateFromPopupCount();
776}
777
778TabContents* TabContents::Clone() {
779 // We create a new SiteInstance so that the new tab won't share processes
780 // with the old one. This can be changed in the future if we need it to share
781 // processes for some reason.
[email protected]420ae012009-04-24 05:16:32782 TabContents* tc = new TabContents(profile(),
[email protected]96d185d2009-04-24 03:28:54783 SiteInstance::CreateSiteInstance(profile()),
[email protected]12636df2009-09-28 22:32:21784 MSG_ROUTING_NONE, this);
[email protected]96d185d2009-04-24 03:28:54785 tc->controller().CopyStateFrom(controller_);
786 return tc;
787}
788
[email protected]420ae012009-04-24 05:16:32789void TabContents::CreateShortcut() {
790 NavigationEntry* entry = controller_.GetLastCommittedEntry();
791 if (!entry)
792 return;
793
[email protected]70b8d6b2009-08-20 16:56:24794#if defined(OS_LINUX) && !defined(TOOLKIT_VIEWS)
[email protected]0b303cc2009-09-28 22:35:15795 SkBitmap bitmap;
796 if (FavIconIsValid())
797 bitmap = GetFavIcon();
[email protected]0ba5c5e2009-08-20 16:47:13798 CreateApplicationShortcutsDialogGtk::Show(view()->GetTopLevelNativeWindow(),
[email protected]0b303cc2009-09-28 22:35:15799 GetURL(), GetTitle(), bitmap);
[email protected]b96aa932009-08-12 21:34:49800#else
[email protected]420ae012009-04-24 05:16:32801 // We only allow one pending install request. By resetting the page id we
802 // effectively cancel the pending install request.
803 pending_install_.page_id = entry->page_id();
804 pending_install_.icon = GetFavIcon();
[email protected]00e3f152009-10-20 19:25:42805 pending_install_.title = GetTitle();
[email protected]420ae012009-04-24 05:16:32806 pending_install_.url = GetURL();
807 if (pending_install_.callback_functor) {
808 pending_install_.callback_functor->Cancel();
809 pending_install_.callback_functor = NULL;
810 }
811 DCHECK(!pending_install_.icon.isNull()) << "Menu item should be disabled.";
812 if (pending_install_.title.empty())
[email protected]00e3f152009-10-20 19:25:42813 pending_install_.title = UTF8ToUTF16(GetURL().spec());
[email protected]420ae012009-04-24 05:16:32814
815 // Request the application info. When done OnDidGetApplicationInfo is invoked
816 // and we'll create the shortcut.
817 render_view_host()->GetApplicationInfo(pending_install_.page_id);
[email protected]b96aa932009-08-12 21:34:49818#endif
[email protected]420ae012009-04-24 05:16:32819}
820
[email protected]4d677202009-07-19 07:37:12821void TabContents::ShowPageInfo(const GURL& url,
822 const NavigationEntry::SSLStatus& ssl,
823 bool show_history) {
824 if (!delegate_)
825 return;
826
[email protected]bb678332009-07-21 00:15:50827 delegate_->ShowPageInfo(profile(), url, ssl, show_history);
[email protected]4d677202009-07-19 07:37:12828}
829
initial.commit09911bf2008-07-26 23:55:29830ConstrainedWindow* TabContents::CreateConstrainedDialog(
[email protected]e8382172009-06-19 22:16:28831 ConstrainedWindowDelegate* delegate) {
initial.commit09911bf2008-07-26 23:55:29832 ConstrainedWindow* window =
[email protected]e8382172009-06-19 22:16:28833 ConstrainedWindow::CreateConstrainedDialog(this, delegate);
initial.commit09911bf2008-07-26 23:55:29834 child_windows_.push_back(window);
835 return window;
836}
837
838void TabContents::AddNewContents(TabContents* new_contents,
839 WindowOpenDisposition disposition,
840 const gfx::Rect& initial_pos,
[email protected]c88a70fe2009-05-05 20:00:22841 bool user_gesture,
842 const GURL& creator_url) {
initial.commit09911bf2008-07-26 23:55:29843 if (!delegate_)
844 return;
845
[email protected]35f7d212009-04-29 21:19:27846 if ((disposition == NEW_POPUP) && !user_gesture &&
847 !CommandLine::ForCurrentProcess()->HasSwitch(
848 switches::kDisablePopupBlocking)) {
[email protected]2c4410d2009-05-06 23:46:22849 // Unrequested popups from normal pages are constrained unless they're in
[email protected]b6c874582009-05-08 19:38:31850 // the whitelist. The popup owner will handle checking this.
851 delegate_->GetConstrainingContents(this)->AddPopup(new_contents,
852 initial_pos,
853 creator_url.is_valid() ? creator_url.host() : std::string());
854 } else {
[email protected]304f8792009-08-19 21:28:09855#if defined(OS_CHROMEOS)
856 if (disposition == NEW_POPUP) {
857 // If the popup is bigger than a given factor of the screen, then
858 // turn it into a foreground tab (on chrome os only)
859 GdkScreen* screen = gdk_screen_get_default();
860 int max_width = gdk_screen_get_width(screen) * kMaxWidthFactor;
861 int max_height = gdk_screen_get_height(screen) * kMaxHeightFactor;
862 if (initial_pos.width() > max_width ||
863 initial_pos.height() > max_height) {
864 disposition = NEW_FOREGROUND_TAB;
865 }
866 }
867#endif
868
[email protected]0aa55312008-10-17 21:53:08869 new_contents->DisassociateFromPopupCount();
initial.commit09911bf2008-07-26 23:55:29870 delegate_->AddNewContents(this, new_contents, disposition, initial_pos,
871 user_gesture);
[email protected]7f37dd92009-09-15 16:35:49872 NotificationService::current()->Notify(
873 NotificationType::TAB_ADDED,
874 Source<TabContentsDelegate>(delegate_),
875 Details<TabContents>(this));
initial.commit09911bf2008-07-26 23:55:29876 }
[email protected]b6c874582009-05-08 19:38:31877 PopupNotificationVisibilityChanged(ShowingBlockedPopupNotification());
initial.commit09911bf2008-07-26 23:55:29878}
879
initial.commit09911bf2008-07-26 23:55:29880void TabContents::CloseAllSuppressedPopups() {
[email protected]d6598c052008-11-05 19:03:25881 if (blocked_popups_)
[email protected]2c4410d2009-05-06 23:46:22882 blocked_popups_->CloseAll();
initial.commit09911bf2008-07-26 23:55:29883}
884
[email protected]912256b32009-09-18 09:47:35885void TabContents::ExecuteCode(int request_id, const std::string& extension_id,
886 bool is_js_code, const std::string& code_string) {
887 render_view_host()->Send(
888 new ViewMsg_ExecuteCode(render_view_host()->routing_id(), request_id,
889 extension_id, is_js_code, code_string));
890}
891
[email protected]96d185d2009-04-24 03:28:54892void TabContents::PopupNotificationVisibilityChanged(bool visible) {
893 render_view_host()->PopupNotificationVisibilityChanged(visible);
894}
895
[email protected]5c9e97a2009-09-09 23:48:30896gfx::NativeView TabContents::GetContentNativeView() const {
[email protected]96d185d2009-04-24 03:28:54897 return view_->GetContentNativeView();
898}
899
900gfx::NativeView TabContents::GetNativeView() const {
901 return view_->GetNativeView();
902}
903
904void TabContents::GetContainerBounds(gfx::Rect *out) const {
905 view_->GetContainerBounds(out);
906}
907
908void TabContents::Focus() {
909 view_->Focus();
910}
911
[email protected]90daadb42009-06-08 21:27:28912void TabContents::FocusThroughTabTraversal(bool reverse) {
[email protected]7e383692009-06-12 19:14:54913 if (showing_interstitial_page()) {
914 render_manager_.interstitial_page()->FocusThroughTabTraversal(reverse);
915 return;
916 }
[email protected]96d185d2009-04-24 03:28:54917 render_view_host()->SetInitialFocus(reverse);
918}
919
920bool TabContents::FocusLocationBarByDefault() {
921 DOMUI* dom_ui = GetDOMUIForCurrentState();
922 if (dom_ui)
923 return dom_ui->focus_location_bar_by_default();
[email protected]3e3f0eb2009-06-22 18:33:43924 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]e0d481582009-09-15 21:06:25925 if (entry && entry->url() == GURL(chrome::kAboutBlankURL))
[email protected]3e3f0eb2009-06-22 18:33:43926 return true;
[email protected]96d185d2009-04-24 03:28:54927 return false;
928}
929
[email protected]616ed5a2008-11-21 22:27:24930void TabContents::AddInfoBar(InfoBarDelegate* delegate) {
931 // Look through the existing InfoBarDelegates we have for a match. If we've
932 // already got one that matches, then we don't add the new one.
[email protected]f86a07022008-11-25 01:06:05933 for (int i = 0; i < infobar_delegate_count(); ++i) {
[email protected]616ed5a2008-11-21 22:27:24934 if (GetInfoBarDelegateAt(i)->EqualsDelegate(delegate))
935 return;
936 }
937
938 infobar_delegates_.push_back(delegate);
[email protected]bfd04a62009-02-01 18:16:56939 NotificationService::current()->Notify(
940 NotificationType::TAB_CONTENTS_INFOBAR_ADDED,
941 Source<TabContents>(this),
942 Details<InfoBarDelegate>(delegate));
[email protected]616ed5a2008-11-21 22:27:24943
944 // Add ourselves as an observer for navigations the first time a delegate is
945 // added. We use this notification to expire InfoBars that need to expire on
946 // page transitions.
947 if (infobar_delegates_.size() == 1) {
[email protected]bfd04a62009-02-01 18:16:56948 registrar_.Add(this, NotificationType::NAV_ENTRY_COMMITTED,
[email protected]ce3fa3c2009-04-20 19:55:57949 Source<NavigationController>(&controller_));
[email protected]616ed5a2008-11-21 22:27:24950 }
951}
952
953void TabContents::RemoveInfoBar(InfoBarDelegate* delegate) {
954 std::vector<InfoBarDelegate*>::iterator it =
955 find(infobar_delegates_.begin(), infobar_delegates_.end(), delegate);
956 if (it != infobar_delegates_.end()) {
957 InfoBarDelegate* delegate = *it;
[email protected]bfd04a62009-02-01 18:16:56958 NotificationService::current()->Notify(
959 NotificationType::TAB_CONTENTS_INFOBAR_REMOVED,
960 Source<TabContents>(this),
961 Details<InfoBarDelegate>(delegate));
[email protected]f86a07022008-11-25 01:06:05962 infobar_delegates_.erase(it);
[email protected]616ed5a2008-11-21 22:27:24963
[email protected]6a02963e2009-01-06 16:58:03964 // Remove ourselves as an observer if we are tracking no more InfoBars.
965 if (infobar_delegates_.empty()) {
[email protected]bfd04a62009-02-01 18:16:56966 registrar_.Remove(this, NotificationType::NAV_ENTRY_COMMITTED,
[email protected]ce3fa3c2009-04-20 19:55:57967 Source<NavigationController>(&controller_));
[email protected]6a02963e2009-01-06 16:58:03968 }
[email protected]616ed5a2008-11-21 22:27:24969 }
970}
[email protected]3edd9522009-03-04 22:19:41971
[email protected]f81821c2009-08-07 01:29:47972void TabContents::ReplaceInfoBar(InfoBarDelegate* old_delegate,
973 InfoBarDelegate* new_delegate) {
974 std::vector<InfoBarDelegate*>::iterator it =
975 find(infobar_delegates_.begin(), infobar_delegates_.end(), old_delegate);
976 DCHECK(it != infobar_delegates_.end());
977
978 // Notify the container about the change of plans.
979 scoped_ptr<std::pair<InfoBarDelegate*, InfoBarDelegate*> > details(
980 new std::pair<InfoBarDelegate*, InfoBarDelegate*>(
981 old_delegate, new_delegate));
982 NotificationService::current()->Notify(
983 NotificationType::TAB_CONTENTS_INFOBAR_REPLACED,
984 Source<TabContents>(this),
985 Details<std::pair<InfoBarDelegate*, InfoBarDelegate*> >(details.get()));
986
987 // Remove the old one.
988 infobar_delegates_.erase(it);
989
990 // Add the new one.
991 infobar_delegates_.push_back(new_delegate);
992}
993
[email protected]20ede072009-10-06 00:28:35994bool TabContents::ShouldShowBookmarkBar() {
[email protected]aa9196f2009-10-22 16:20:27995 if (showing_interstitial_page())
996 return false;
997
[email protected]96d185d2009-04-24 03:28:54998 // See GetDOMUIForCurrentState() comment for more info. This case is very
999 // similar, but for non-first loads, we want to use the committed entry. This
1000 // is so the bookmarks bar disappears at the same time the page does.
1001 if (controller_.GetLastCommittedEntry()) {
1002 // Not the first load, always use the committed DOM UI.
1003 if (render_manager_.dom_ui())
1004 return render_manager_.dom_ui()->force_bookmark_bar_visible();
1005 return false; // Default.
1006 }
1007
1008 // When it's the first load, we know either the pending one or the committed
1009 // one will have the DOM UI in it (see GetDOMUIForCurrentState), and only one
1010 // of them will be valid, so we can just check both.
1011 if (render_manager_.pending_dom_ui())
1012 return render_manager_.pending_dom_ui()->force_bookmark_bar_visible();
1013 if (render_manager_.dom_ui())
1014 return render_manager_.dom_ui()->force_bookmark_bar_visible();
1015 return false; // Default.
1016}
1017
[email protected]d8f16ae2009-08-30 03:47:261018bool TabContents::IsExtensionShelfAlwaysVisible() {
1019 // See GetDOMUIForCurrentState() comment for more info. This case is very
1020 // similar, but for non-first loads, we want to use the committed entry. This
1021 // is so the bookmarks bar disappears at the same time the page does.
1022 if (controller_.GetLastCommittedEntry()) {
1023 // Not the first load, always use the committed DOM UI.
1024 if (render_manager_.dom_ui())
1025 return render_manager_.dom_ui()->force_extension_shelf_visible();
1026 return false; // Default.
1027 }
1028
1029 // When it's the first load, we know either the pending one or the committed
1030 // one will have the DOM UI in it (see GetDOMUIForCurrentState), and only one
1031 // of them will be valid, so we can just check both.
1032 if (render_manager_.pending_dom_ui())
1033 return render_manager_.pending_dom_ui()->force_extension_shelf_visible();
1034 if (render_manager_.dom_ui())
1035 return render_manager_.dom_ui()->force_extension_shelf_visible();
1036 return false; // Default.
1037}
1038
[email protected]3edd9522009-03-04 22:19:411039void TabContents::ToolbarSizeChanged(bool is_animating) {
1040 TabContentsDelegate* d = delegate();
1041 if (d)
1042 d->ToolbarSizeChanged(this, is_animating);
1043}
[email protected]616ed5a2008-11-21 22:27:241044
[email protected]d5f942ba2008-09-26 19:30:341045void TabContents::OnStartDownload(DownloadItem* download) {
1046 DCHECK(download);
[email protected]d5f942ba2008-09-26 19:30:341047
1048 // Download in a constrained popup is shown in the tab that opened it.
[email protected]b6c874582009-05-08 19:38:311049 TabContents* tab_contents = delegate()->GetConstrainingContents(this);
[email protected]d5f942ba2008-09-26 19:30:341050
[email protected]59560e0b2009-06-04 03:30:221051 if (tab_contents && tab_contents->delegate())
1052 tab_contents->delegate()->OnStartDownload(download);
[email protected]3edd9522009-03-04 22:19:411053}
[email protected]3edd9522009-03-04 22:19:411054
[email protected]d5f942ba2008-09-26 19:30:341055void TabContents::WillClose(ConstrainedWindow* window) {
1056 ConstrainedWindowList::iterator it =
1057 find(child_windows_.begin(), child_windows_.end(), window);
1058 if (it != child_windows_.end())
1059 child_windows_.erase(it);
[email protected]66ba4932009-06-04 19:22:131060}
[email protected]d5f942ba2008-09-26 19:30:341061
[email protected]66ba4932009-06-04 19:22:131062void TabContents::WillCloseBlockedPopupContainer(
1063 BlockedPopupContainer* container) {
1064 DCHECK(blocked_popups_ == container);
1065 blocked_popups_ = NULL;
[email protected]d5f942ba2008-09-26 19:30:341066}
1067
[email protected]d5f942ba2008-09-26 19:30:341068void TabContents::DidMoveOrResize(ConstrainedWindow* window) {
[email protected]332af7732009-03-11 13:21:351069#if defined(OS_WIN)
[email protected]92edc472009-02-10 20:32:061070 UpdateWindow(GetNativeView());
[email protected]332af7732009-03-11 13:21:351071#endif
[email protected]d5f942ba2008-09-26 19:30:341072}
1073
[email protected]e491f1c2009-05-22 20:28:121074void TabContents::StartFinding(string16 find_text,
1075 bool forward_direction,
1076 bool case_sensitive) {
[email protected]6e95706d2009-04-23 22:55:011077 // If find_text is empty, it means FindNext was pressed with a keyboard
1078 // shortcut so unless we have something to search for we return early.
[email protected]e491f1c2009-05-22 20:28:121079 if (find_text.empty() && find_text_.empty()) {
1080 if (last_search_prepopulate_text_->empty())
1081 return;
1082 // Try whatever we searched for last in any tab.
1083 find_text = *last_search_prepopulate_text_;
1084 }
[email protected]6e95706d2009-04-23 22:55:011085
1086 // This is a FindNext operation if we are searching for the same text again,
1087 // or if the passed in search text is empty (FindNext keyboard shortcut). The
1088 // exception to this is if the Find was aborted (then we don't want FindNext
1089 // because the highlighting has been cleared and we need it to reappear). We
1090 // therefore treat FindNext after an aborted Find operation as a full fledged
1091 // Find.
1092 bool find_next = (find_text_ == find_text || find_text.empty()) &&
[email protected]e491f1c2009-05-22 20:28:121093 (last_search_case_sensitive_ == case_sensitive) &&
[email protected]6e95706d2009-04-23 22:55:011094 !find_op_aborted_;
1095 if (!find_next)
1096 current_find_request_id_ = find_request_id_counter_++;
1097
1098 if (!find_text.empty())
1099 find_text_ = find_text;
[email protected]e491f1c2009-05-22 20:28:121100 last_search_case_sensitive_ = case_sensitive;
[email protected]6e95706d2009-04-23 22:55:011101
1102 find_op_aborted_ = false;
1103
1104 // Keep track of what the last search was across the tabs.
[email protected]e491f1c2009-05-22 20:28:121105 *last_search_prepopulate_text_ = find_text;
[email protected]6e95706d2009-04-23 22:55:011106
1107 render_view_host()->StartFinding(current_find_request_id_,
1108 find_text_,
1109 forward_direction,
[email protected]e491f1c2009-05-22 20:28:121110 case_sensitive,
[email protected]6e95706d2009-04-23 22:55:011111 find_next);
1112}
1113
1114void TabContents::StopFinding(bool clear_selection) {
[email protected]2d2dffc2009-06-09 21:41:001115 // When |clear_selection| is true, it means the find string has been cleared
1116 // by the user, but the UI has not been dismissed.
1117 if (!clear_selection)
1118 find_ui_active_ = false;
[email protected]6e95706d2009-04-23 22:55:011119 find_op_aborted_ = true;
[email protected]e491f1c2009-05-22 20:28:121120 last_search_result_ = FindNotificationDetails();
[email protected]6e95706d2009-04-23 22:55:011121 render_view_host()->StopFinding(clear_selection);
1122}
1123
[email protected]5c4266922009-07-10 16:41:271124void TabContents::GetPageLanguage() {
1125 render_view_host()->GetPageLanguage();
1126}
1127
[email protected]420ae012009-04-24 05:16:321128void TabContents::OnJavaScriptMessageBoxClosed(IPC::Message* reply_msg,
1129 bool success,
1130 const std::wstring& prompt) {
1131 last_javascript_message_dismissal_ = base::TimeTicks::Now();
[email protected]7ab1e7d62009-10-14 23:32:011132 if (is_showing_before_unload_dialog_ && !success) {
1133 // If a beforeunload dialog is canceled, we need to stop the throbber from
1134 // spinning, since we forced it to start spinning in Navigate.
1135 DidStopLoading();
1136
1137 tab_close_start_time_ = base::TimeTicks();
1138 }
1139 is_showing_before_unload_dialog_ = false;
1140 render_view_host()->JavaScriptMessageBoxClosed(reply_msg, success, prompt);
[email protected]420ae012009-04-24 05:16:321141}
1142
1143void TabContents::OnSavePage() {
1144 // If we can not save the page, try to download it.
1145 if (!SavePackage::IsSavableContents(contents_mime_type())) {
1146 DownloadManager* dlm = profile()->GetDownloadManager();
1147 const GURL& current_page_url = GetURL();
1148 if (dlm && current_page_url.is_valid())
[email protected]d82ed61e2009-06-16 02:46:221149 dlm->DownloadUrl(current_page_url, GURL(), "", this);
[email protected]420ae012009-04-24 05:16:321150 return;
1151 }
1152
1153 Stop();
1154
1155 // Create the save package and possibly prompt the user for the name to save
1156 // the page as. The user prompt is an asynchronous operation that runs on
1157 // another thread.
[email protected]d82ed61e2009-06-16 02:46:221158 save_package_ = new SavePackage(this);
[email protected]420ae012009-04-24 05:16:321159 save_package_->GetSaveInfo();
1160}
1161
1162// Used in automated testing to bypass prompting the user for file names.
1163// Instead, the names and paths are hard coded rather than running them through
1164// file name sanitation and extension / mime checking.
[email protected]828cabe2009-09-26 22:47:111165bool TabContents::SavePage(const FilePath& main_file, const FilePath& dir_path,
[email protected]420ae012009-04-24 05:16:321166 SavePackage::SavePackageType save_type) {
1167 // Stop the page from navigating.
1168 Stop();
1169
[email protected]828cabe2009-09-26 22:47:111170 save_package_ = new SavePackage(this, save_type, main_file, dir_path);
[email protected]cdf6cc82009-08-04 21:48:591171 return save_package_->Init();
[email protected]420ae012009-04-24 05:16:321172}
1173
1174void TabContents::PrintPreview() {
1175 // We don't show the print preview yet, only the print dialog.
1176 PrintNow();
1177}
1178
1179bool TabContents::PrintNow() {
1180 // We can't print interstitial page for now.
1181 if (showing_interstitial_page())
1182 return false;
1183
1184 return render_view_host()->PrintPages();
1185}
1186
[email protected]82270452009-06-19 15:58:011187void TabContents::PrintingDone(int document_cookie, bool success) {
1188 render_view_host()->PrintingDone(document_cookie, success);
1189}
1190
[email protected]420ae012009-04-24 05:16:321191bool TabContents::IsActiveEntry(int32 page_id) {
1192 NavigationEntry* active_entry = controller_.GetActiveEntry();
1193 return (active_entry != NULL &&
1194 active_entry->site_instance() == GetSiteInstance() &&
1195 active_entry->page_id() == page_id);
1196}
1197
[email protected]3f8eb7f2009-10-02 23:58:051198void TabContents::LogNewTabTime(const std::string& event_name) {
1199 // Not all new tab pages get timed. In those cases, we don't have a
1200 // new_tab_start_time_.
1201 if (new_tab_start_time_.is_null())
1202 return;
1203
1204 base::TimeDelta duration = base::TimeTicks::Now() - new_tab_start_time_;
[email protected]84abba62009-10-07 17:01:441205 MetricEventDurationDetails details(event_name,
1206 static_cast<int>(duration.InMilliseconds()));
1207
[email protected]7ab1e7d62009-10-14 23:32:011208 if (event_name == "Tab.NewTabScriptStart") {
1209 UMA_HISTOGRAM_TIMES("Tab.NewTabScriptStart", duration);
1210 } else if (event_name == "Tab.NewTabDOMContentLoaded") {
1211 UMA_HISTOGRAM_TIMES("Tab.NewTabDOMContentLoaded", duration);
1212 } else if (event_name == "Tab.NewTabOnload") {
1213 UMA_HISTOGRAM_TIMES("Tab.NewTabOnload", duration);
[email protected]3f8eb7f2009-10-02 23:58:051214 // The new tab page has finished loading; reset it.
1215 new_tab_start_time_ = base::TimeTicks();
[email protected]84abba62009-10-07 17:01:441216 } else {
1217 NOTREACHED();
[email protected]3f8eb7f2009-10-02 23:58:051218 }
[email protected]84abba62009-10-07 17:01:441219 NotificationService::current()->Notify(
1220 NotificationType::METRIC_EVENT_DURATION,
1221 Source<TabContents>(this),
1222 Details<MetricEventDurationDetails>(&details));
[email protected]3f8eb7f2009-10-02 23:58:051223}
1224
[email protected]7ab1e7d62009-10-14 23:32:011225void TabContents::OnCloseStarted() {
1226 if (tab_close_start_time_.is_null())
1227 tab_close_start_time_ = base::TimeTicks::Now();
1228}
1229
[email protected]96d185d2009-04-24 03:28:541230// Notifies the RenderWidgetHost instance about the fact that the page is
1231// loading, or done loading and calls the base implementation.
1232void TabContents::SetIsLoading(bool is_loading,
1233 LoadNotificationDetails* details) {
1234 if (is_loading == is_loading_)
1235 return;
1236
1237 if (!is_loading) {
1238 load_state_ = net::LOAD_STATE_IDLE;
1239 load_state_host_.clear();
[email protected]094e5b22009-09-25 04:23:561240 upload_size_ = 0;
1241 upload_position_ = 0;
[email protected]96d185d2009-04-24 03:28:541242 }
1243
1244 render_manager_.SetIsLoading(is_loading);
1245
1246 is_loading_ = is_loading;
1247 waiting_for_response_ = is_loading;
1248
1249 if (delegate_)
1250 delegate_->LoadingStateChanged(this);
[email protected]329581b2009-04-28 06:52:351251 NotifyNavigationStateChanged(INVALIDATE_LOAD);
[email protected]96d185d2009-04-24 03:28:541252
1253 NotificationType type = is_loading ? NotificationType::LOAD_START :
1254 NotificationType::LOAD_STOP;
[email protected]ce5c4502009-05-06 16:46:111255 NotificationDetails det = NotificationService::NoDetails();
[email protected]96d185d2009-04-24 03:28:541256 if (details)
1257 det = Details<LoadNotificationDetails>(details);
1258 NotificationService::current()->Notify(type,
1259 Source<NavigationController>(&controller_),
1260 det);
1261}
1262
[email protected]2c4410d2009-05-06 23:46:221263void TabContents::CreateBlockedPopupContainerIfNecessary() {
1264 if (blocked_popups_)
1265 return;
1266
[email protected]4d4c0b92009-06-08 23:13:011267 blocked_popups_ = BlockedPopupContainer::Create(this, profile());
[email protected]2c4410d2009-05-06 23:46:221268}
1269
[email protected]b6c874582009-05-08 19:38:311270void TabContents::AddPopup(TabContents* new_contents,
1271 const gfx::Rect& initial_pos,
1272 const std::string& host) {
[email protected]2c4410d2009-05-06 23:46:221273 CreateBlockedPopupContainerIfNecessary();
1274 blocked_popups_->AddTabContents(new_contents, initial_pos, host);
[email protected]2c4410d2009-05-06 23:46:221275}
1276
[email protected]e69bce3dd2009-04-20 22:05:121277// TODO(brettw) This should be on the TabContentsView.
[email protected]66ba4932009-06-04 19:22:131278void TabContents::RepositionSupressedPopupsToFit() {
[email protected]d6598c052008-11-05 19:03:251279 if (blocked_popups_)
[email protected]4d4c0b92009-06-08 23:13:011280 blocked_popups_->RepositionBlockedPopupContainer();
[email protected]d5f942ba2008-09-26 19:30:341281}
1282
[email protected]b9681312008-11-07 00:08:261283bool TabContents::ShowingBlockedPopupNotification() const {
1284 return blocked_popups_ != NULL &&
[email protected]2c4410d2009-05-06 23:46:221285 blocked_popups_->GetBlockedPopupCount() != 0;
[email protected]b9681312008-11-07 00:08:261286}
[email protected]616ed5a2008-11-21 22:27:241287
1288namespace {
1289bool TransitionIsReload(PageTransition::Type transition) {
1290 return PageTransition::StripQualifier(transition) == PageTransition::RELOAD;
1291}
1292}
1293
1294void TabContents::ExpireInfoBars(
1295 const NavigationController::LoadCommittedDetails& details) {
1296 // Only hide InfoBars when the user has done something that makes the main
1297 // frame load. We don't want various automatic or subframe navigations making
1298 // it disappear.
1299 if (!details.is_user_initiated_main_frame_load())
1300 return;
1301
[email protected]f86a07022008-11-25 01:06:051302 for (int i = infobar_delegate_count() - 1; i >= 0; --i) {
[email protected]616ed5a2008-11-21 22:27:241303 InfoBarDelegate* delegate = GetInfoBarDelegateAt(i);
[email protected]f86a07022008-11-25 01:06:051304 if (delegate->ShouldExpire(details))
[email protected]616ed5a2008-11-21 22:27:241305 RemoveInfoBar(delegate);
[email protected]616ed5a2008-11-21 22:27:241306 }
1307}
[email protected]fdd61c62009-04-22 19:22:571308
1309void TabContents::OnGearsCreateShortcutDone(
1310 const GearsShortcutData2& shortcut_data, bool success) {
1311 NavigationEntry* current_entry = controller_.GetLastCommittedEntry();
1312 bool same_page =
1313 current_entry && pending_install_.page_id == current_entry->page_id();
1314
1315 if (success && same_page) {
1316 // Only switch to app mode if the user chose to create a shortcut and
1317 // we're still on the same page that it corresponded to.
1318 if (delegate())
1319 delegate()->ConvertContentsToApplication(this);
1320 }
1321
1322 // Reset the page id to indicate no requests are pending.
1323 pending_install_.page_id = 0;
1324 pending_install_.callback_functor = NULL;
1325}
[email protected]96d185d2009-04-24 03:28:541326
1327DOMUI* TabContents::GetDOMUIForCurrentState() {
1328 // When there is a pending navigation entry, we want to use the pending DOMUI
1329 // that goes along with it to control the basic flags. For example, we want to
1330 // show the pending URL in the URL bar, so we want the display_url flag to
1331 // be from the pending entry.
1332 //
1333 // The confusion comes because there are multiple possibilities for the
1334 // initial load in a tab as a side effect of the way the RenderViewHostManager
1335 // works.
1336 //
1337 // - For the very first tab the load looks "normal". The new tab DOM UI is
1338 // the pending one, and we want it to apply here.
1339 //
1340 // - For subsequent new tabs, they'll get a new SiteInstance which will then
1341 // get switched to the one previously associated with the new tab pages.
1342 // This switching will cause the manager to commit the RVH/DOMUI. So we'll
1343 // have a committed DOM UI in this case.
1344 //
1345 // This condition handles all of these cases:
1346 //
1347 // - First load in first tab: no committed nav entry + pending nav entry +
1348 // pending dom ui:
1349 // -> Use pending DOM UI if any.
1350 //
1351 // - First load in second tab: no committed nav entry + pending nav entry +
1352 // no pending DOM UI:
1353 // -> Use the committed DOM UI if any.
1354 //
1355 // - Second navigation in any tab: committed nav entry + pending nav entry:
1356 // -> Use pending DOM UI if any.
1357 //
1358 // - Normal state with no load: committed nav entry + no pending nav entry:
1359 // -> Use committed DOM UI.
1360 if (controller_.pending_entry() &&
1361 (controller_.GetLastCommittedEntry() ||
1362 render_manager_.pending_dom_ui()))
1363 return render_manager_.pending_dom_ui();
1364 return render_manager_.dom_ui();
1365}
[email protected]420ae012009-04-24 05:16:321366
1367void TabContents::DidNavigateMainFramePostCommit(
1368 const NavigationController::LoadCommittedDetails& details,
1369 const ViewHostMsg_FrameNavigate_Params& params) {
[email protected]c2e74fe82009-09-03 17:57:441370 if (opener_dom_ui_type_ != DOMUIFactory::kNoDOMUI) {
1371 // If this is a window.open navigation, use the same DOMUI as the renderer
1372 // that opened the window, as long as both renderers have the same
1373 // privileges.
1374 if (opener_dom_ui_type_ == DOMUIFactory::GetDOMUIType(GetURL())) {
1375 DOMUI* dom_ui = DOMUIFactory::CreateDOMUIForURL(this, GetURL());
1376 // dom_ui might be NULL if the URL refers to a non-existent extension.
1377 if (dom_ui) {
1378 render_manager_.SetDOMUIPostCommit(dom_ui);
1379 dom_ui->RenderViewCreated(render_view_host());
1380 }
1381 }
1382 opener_dom_ui_type_ = DOMUIFactory::kNoDOMUI;
1383 }
1384
[email protected]420ae012009-04-24 05:16:321385 if (details.is_user_initiated_main_frame_load()) {
1386 // Clear the status bubble. This is a workaround for a bug where WebKit
1387 // doesn't let us know that the cursor left an element during a
1388 // transition (this is also why the mouse cursor remains as a hand after
1389 // clicking on a link); see bugs 1184641 and 980803. We don't want to
1390 // clear the bubble when a user navigates to a named anchor in the same
1391 // page.
1392 UpdateTargetURL(details.entry->page_id(), GURL());
1393
1394 // UpdateHelpersForDidNavigate will handle the case where the password_form
1395 // origin is valid.
1396 // TODO(brettw) bug 1343111: Password manager stuff in here needs to be
1397 // cleaned up and covered by tests.
1398 if (!params.password_form.origin.is_valid())
1399 GetPasswordManager()->DidNavigate();
1400 }
1401
1402 // The keyword generator uses the navigation entries, so must be called after
1403 // the commit.
1404 GenerateKeywordIfNecessary(params);
1405
1406 // Allow the new page to set the title again.
1407 received_page_title_ = false;
1408
1409 // Get the favicon, either from history or request it from the net.
1410 fav_icon_helper_.FetchFavIcon(details.entry->url());
1411
[email protected]aece2c7f2009-08-27 20:43:171412 // Disable all page actions, unless this is an in-page navigation.
[email protected]744ef172009-10-16 21:53:461413 if (!page_actions_.empty()) {
[email protected]aece2c7f2009-08-27 20:43:171414 url_canon::Replacements<char> replacements;
1415 replacements.ClearRef();
1416 if (params.url.ReplaceComponents(replacements) !=
1417 params.referrer.ReplaceComponents(replacements)) {
[email protected]744ef172009-10-16 21:53:461418 page_actions_.clear();
[email protected]aece2c7f2009-08-27 20:43:171419 }
1420 }
[email protected]ce5c4502009-05-06 16:46:111421
[email protected]420ae012009-04-24 05:16:321422 // Close constrained popups if necessary.
1423 MaybeCloseChildWindows(details.previous_url, details.entry->url());
1424
1425 // Update the starred state.
1426 UpdateStarredStateForCurrentURL();
1427}
1428
1429void TabContents::DidNavigateAnyFramePostCommit(
1430 RenderViewHost* render_view_host,
1431 const NavigationController::LoadCommittedDetails& details,
1432 const ViewHostMsg_FrameNavigate_Params& params) {
1433 // If we navigate, start showing messages again. This does nothing to prevent
1434 // a malicious script from spamming messages, since the script could just
1435 // reload the page to stop blocking.
1436 suppress_javascript_messages_ = false;
1437
[email protected]420ae012009-04-24 05:16:321438 // Notify the password manager of the navigation or form submit.
1439 // TODO(brettw) bug 1343111: Password manager stuff in here needs to be
1440 // cleaned up and covered by tests.
1441 if (params.password_form.origin.is_valid())
1442 GetPasswordManager()->ProvisionallySavePassword(params.password_form);
1443}
1444
1445void TabContents::MaybeCloseChildWindows(const GURL& previous_url,
1446 const GURL& current_url) {
1447 if (net::RegistryControlledDomainService::SameDomainOrHost(
1448 previous_url, current_url))
1449 return;
1450
1451 // Clear out any child windows since we are leaving this page entirely.
1452 // We use indices instead of iterators in case CloseWindow does something
1453 // that may invalidate an iterator.
1454 int size = static_cast<int>(child_windows_.size());
1455 for (int i = size - 1; i >= 0; --i) {
1456 ConstrainedWindow* window = child_windows_[i];
1457 if (window)
1458 window->CloseConstrainedWindow();
1459 }
[email protected]e29300cd2009-07-23 21:14:591460
1461 // Close the popup container.
1462 if (blocked_popups_) {
1463 blocked_popups_->Destroy();
1464 blocked_popups_ = NULL;
1465 }
[email protected]420ae012009-04-24 05:16:321466}
1467
1468void TabContents::UpdateStarredStateForCurrentURL() {
1469 BookmarkModel* model = profile()->GetBookmarkModel();
1470 const bool old_state = is_starred_;
1471 is_starred_ = (model && model->IsBookmarked(GetURL()));
1472
1473 if (is_starred_ != old_state && delegate())
1474 delegate()->URLStarredChanged(this, is_starred_);
1475}
1476
1477void TabContents::UpdateAlternateErrorPageURL() {
1478 GURL url = GetAlternateErrorPageURL();
1479 render_view_host()->SetAlternateErrorPageURL(url);
1480}
1481
1482void TabContents::UpdateWebPreferences() {
1483 render_view_host()->UpdateWebPreferences(GetWebkitPrefs());
1484}
1485
1486void TabContents::UpdateMaxPageIDIfNecessary(SiteInstance* site_instance,
1487 RenderViewHost* rvh) {
1488 // If we are creating a RVH for a restored controller, then we might
1489 // have more page IDs than the SiteInstance's current max page ID. We must
1490 // make sure that the max page ID is larger than any restored page ID.
1491 // Note that it is ok for conflicting page IDs to exist in another tab
1492 // (i.e., NavigationController), but if any page ID is larger than the max,
1493 // the back/forward list will get confused.
1494 int max_restored_page_id = controller_.max_restored_page_id();
1495 if (max_restored_page_id > 0) {
1496 int curr_max_page_id = site_instance->max_page_id();
1497 if (max_restored_page_id > curr_max_page_id) {
1498 // Need to update the site instance immediately.
1499 site_instance->UpdateMaxPageID(max_restored_page_id);
1500
1501 // Also tell the renderer to update its internal representation. We
1502 // need to reserve enough IDs to make all restored page IDs less than
1503 // the max.
1504 if (curr_max_page_id < 0)
1505 curr_max_page_id = 0;
1506 rvh->ReservePageIDRange(max_restored_page_id - curr_max_page_id);
1507 }
1508 }
1509}
1510
1511void TabContents::UpdateHistoryForNavigation(
[email protected]ebe89e062009-08-13 23:16:541512 const GURL& virtual_url,
[email protected]befd8d822009-07-01 04:51:471513 const NavigationController::LoadCommittedDetails& details,
[email protected]420ae012009-04-24 05:16:321514 const ViewHostMsg_FrameNavigate_Params& params) {
1515 if (profile()->IsOffTheRecord())
1516 return;
1517
1518 // Add to history service.
1519 HistoryService* hs = profile()->GetHistoryService(Profile::IMPLICIT_ACCESS);
1520 if (hs) {
1521 if (PageTransition::IsMainFrame(params.transition) &&
[email protected]ebe89e062009-08-13 23:16:541522 virtual_url != params.url) {
1523 // Hack on the "virtual" URL so that it will appear in history. For some
[email protected]420ae012009-04-24 05:16:321524 // types of URLs, we will display a magic URL that is different from where
1525 // the page is actually navigated. We want the user to see in history
[email protected]ebe89e062009-08-13 23:16:541526 // what they saw in the URL bar, so we add the virtual URL as a redirect.
1527 // This only applies to the main frame, as the virtual URL doesn't apply
[email protected]420ae012009-04-24 05:16:321528 // to sub-frames.
1529 std::vector<GURL> redirects = params.redirects;
1530 if (!redirects.empty())
[email protected]ebe89e062009-08-13 23:16:541531 redirects.back() = virtual_url;
1532 hs->AddPage(virtual_url, this, params.page_id, params.referrer,
[email protected]befd8d822009-07-01 04:51:471533 params.transition, redirects, details.did_replace_entry);
[email protected]420ae012009-04-24 05:16:321534 } else {
1535 hs->AddPage(params.url, this, params.page_id, params.referrer,
[email protected]befd8d822009-07-01 04:51:471536 params.transition, params.redirects,
1537 details.did_replace_entry);
[email protected]420ae012009-04-24 05:16:321538 }
1539 }
1540}
1541
1542bool TabContents::UpdateTitleForEntry(NavigationEntry* entry,
1543 const std::wstring& title) {
1544 // For file URLs without a title, use the pathname instead. In the case of a
1545 // synthesized title, we don't want the update to count toward the "one set
1546 // per page of the title to history."
1547 std::wstring final_title;
1548 bool explicit_set;
1549 if (entry->url().SchemeIsFile() && title.empty()) {
1550 final_title = UTF8ToWide(entry->url().ExtractFileName());
1551 explicit_set = false; // Don't count synthetic titles toward the set limit.
1552 } else {
1553 TrimWhitespace(title, TRIM_ALL, &final_title);
1554 explicit_set = true;
1555 }
1556
1557 if (final_title == UTF16ToWideHack(entry->title()))
1558 return false; // Nothing changed, don't bother.
1559
1560 entry->set_title(WideToUTF16Hack(final_title));
1561
1562 // Update the history system for this page.
1563 if (!profile()->IsOffTheRecord() && !received_page_title_) {
1564 HistoryService* hs =
1565 profile()->GetHistoryService(Profile::IMPLICIT_ACCESS);
1566 if (hs)
[email protected]ebe89e062009-08-13 23:16:541567 hs->SetPageTitle(entry->virtual_url(), final_title);
[email protected]420ae012009-04-24 05:16:321568
1569 // Don't allow the title to be saved again for explicitly set ones.
1570 received_page_title_ = explicit_set;
1571 }
1572
1573 // Lastly, set the title for the view.
1574 view_->SetPageTitle(final_title);
1575
1576 return true;
1577}
1578
1579void TabContents::NotifySwapped() {
1580 // After sending out a swap notification, we need to send a disconnect
1581 // notification so that clients that pick up a pointer to |this| can NULL the
1582 // pointer. See Bug 1230284.
1583 notify_disconnection_ = true;
1584 NotificationService::current()->Notify(
[email protected]57c6a652009-05-04 07:58:341585 NotificationType::TAB_CONTENTS_SWAPPED,
[email protected]d82ed61e2009-06-16 02:46:221586 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:321587 NotificationService::NoDetails());
1588}
1589
1590void TabContents::NotifyConnected() {
1591 notify_disconnection_ = true;
1592 NotificationService::current()->Notify(
[email protected]57c6a652009-05-04 07:58:341593 NotificationType::TAB_CONTENTS_CONNECTED,
[email protected]d82ed61e2009-06-16 02:46:221594 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:321595 NotificationService::NoDetails());
1596}
1597
1598void TabContents::NotifyDisconnected() {
1599 if (!notify_disconnection_)
1600 return;
1601
1602 notify_disconnection_ = false;
1603 NotificationService::current()->Notify(
[email protected]57c6a652009-05-04 07:58:341604 NotificationType::TAB_CONTENTS_DISCONNECTED,
[email protected]d82ed61e2009-06-16 02:46:221605 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:321606 NotificationService::NoDetails());
1607}
1608
1609void TabContents::GenerateKeywordIfNecessary(
1610 const ViewHostMsg_FrameNavigate_Params& params) {
1611 if (!params.searchable_form_url.is_valid())
1612 return;
1613
1614 if (profile()->IsOffTheRecord())
1615 return;
1616
1617 int last_index = controller_.last_committed_entry_index();
1618 // When there was no previous page, the last index will be 0. This is
1619 // normally due to a form submit that opened in a new tab.
1620 // TODO(brettw) bug 916126: we should support keywords when form submits
1621 // happen in new tabs.
1622 if (last_index <= 0)
1623 return;
1624 const NavigationEntry* previous_entry =
1625 controller_.GetEntryAtIndex(last_index - 1);
1626 if (IsFormSubmit(previous_entry)) {
1627 // Only generate a keyword if the previous page wasn't itself a form
1628 // submit.
1629 return;
1630 }
1631
1632 GURL keyword_url = previous_entry->user_typed_url().is_valid() ?
1633 previous_entry->user_typed_url() : previous_entry->url();
1634 std::wstring keyword =
1635 TemplateURLModel::GenerateKeyword(keyword_url, true); // autodetected
1636 if (keyword.empty())
1637 return;
1638
1639 TemplateURLModel* url_model = profile()->GetTemplateURLModel();
1640 if (!url_model)
1641 return;
1642
1643 if (!url_model->loaded()) {
1644 url_model->Load();
1645 return;
1646 }
1647
1648 const TemplateURL* current_url;
1649 std::wstring url = UTF8ToWide(params.searchable_form_url.spec());
1650 if (!url_model->CanReplaceKeyword(keyword, url, &current_url))
1651 return;
1652
1653 if (current_url) {
1654 if (current_url->originating_url().is_valid()) {
1655 // The existing keyword was generated from an OpenSearch description
1656 // document, don't regenerate.
1657 return;
1658 }
1659 url_model->Remove(current_url);
1660 }
1661 TemplateURL* new_url = new TemplateURL();
1662 new_url->set_keyword(keyword);
1663 new_url->set_short_name(keyword);
1664 new_url->SetURL(url, 0, 0);
1665 new_url->add_input_encoding(params.searchable_form_encoding);
1666 DCHECK(controller_.GetLastCommittedEntry());
1667 const GURL& favicon_url =
1668 controller_.GetLastCommittedEntry()->favicon().url();
1669 if (favicon_url.is_valid()) {
1670 new_url->SetFavIconURL(favicon_url);
1671 } else {
1672 // The favicon url isn't valid. This means there really isn't a favicon,
1673 // or the favicon url wasn't obtained before the load started. This assumes
1674 // the later.
1675 // TODO(sky): Need a way to set the favicon that doesn't involve generating
1676 // its url.
1677 new_url->SetFavIconURL(TemplateURL::GenerateFaviconURL(params.referrer));
1678 }
1679 new_url->set_safe_for_autoreplace(true);
1680 url_model->Add(new_url);
1681}
1682
[email protected]bf5c2ff392009-07-08 16:24:331683void TabContents::OnUserGesture() {
1684 // See comment in RenderViewHostDelegate::OnUserGesture as to why we do this.
1685 DownloadRequestManager* drm = g_browser_process->download_request_manager();
1686 if (drm)
1687 drm->OnUserGesture(this);
[email protected]e7eaedde2009-09-25 20:09:491688 ExternalProtocolHandler::OnUserGesture();
[email protected]bf5c2ff392009-07-08 16:24:331689 controller_.OnUserGesture();
1690}
1691
1692void TabContents::OnFindReply(int request_id,
1693 int number_of_matches,
1694 const gfx::Rect& selection_rect,
1695 int active_match_ordinal,
1696 bool final_update) {
[email protected]3ce7ced2009-07-24 18:52:421697 // Ignore responses for requests that have been aborted.
1698 if (find_op_aborted_)
1699 return;
1700
[email protected]bf5c2ff392009-07-08 16:24:331701 // Ignore responses for requests other than the one we have most recently
1702 // issued. That way we won't act on stale results when the user has
1703 // already typed in another query.
1704 if (request_id != current_find_request_id_)
1705 return;
1706
1707 if (number_of_matches == -1)
1708 number_of_matches = last_search_result_.number_of_matches();
1709 if (active_match_ordinal == -1)
1710 active_match_ordinal = last_search_result_.active_match_ordinal();
1711
1712 gfx::Rect selection = selection_rect;
1713 if (selection.IsEmpty())
1714 selection = last_search_result_.selection_rect();
1715
1716 // Notify the UI, automation and any other observers that a find result was
1717 // found.
1718 last_search_result_ = FindNotificationDetails(request_id, number_of_matches,
1719 selection, active_match_ordinal,
1720 final_update);
1721 NotificationService::current()->Notify(
1722 NotificationType::FIND_RESULT_AVAILABLE,
1723 Source<TabContents>(this),
1724 Details<FindNotificationDetails>(&last_search_result_));
1725}
1726
1727void TabContents::GoToEntryAtOffset(int offset) {
[email protected]f9cc4c452009-10-13 14:56:381728 if (!delegate_ || delegate_->OnGoToEntryOffset(offset))
1729 controller_.GoToOffset(offset);
[email protected]bf5c2ff392009-07-08 16:24:331730}
1731
1732void TabContents::GetHistoryListCount(int* back_list_count,
1733 int* forward_list_count) {
1734 int current_index = controller_.last_committed_entry_index();
1735 *back_list_count = current_index;
1736 *forward_list_count = controller_.entry_count() - current_index - 1;
1737}
1738
1739void TabContents::OnMissingPluginStatus(int status) {
1740#if defined(OS_WIN)
1741// TODO(PORT): pull in when plug-ins work
1742 GetPluginInstaller()->OnMissingPluginStatus(status);
1743#endif
1744}
1745
1746void TabContents::OnCrashedPlugin(const FilePath& plugin_path) {
[email protected]bf5c2ff392009-07-08 16:24:331747 DCHECK(!plugin_path.value().empty());
1748
1749 std::wstring plugin_name = plugin_path.ToWStringHack();
[email protected]d32d46182009-07-23 00:09:351750#if defined(OS_WIN) || defined(OS_MACOSX)
[email protected]bf5c2ff392009-07-08 16:24:331751 scoped_ptr<FileVersionInfo> version_info(
1752 FileVersionInfo::CreateFileVersionInfo(plugin_path));
1753 if (version_info.get()) {
1754 const std::wstring& product_name = version_info->product_name();
[email protected]996fd702009-09-04 19:12:371755 if (!product_name.empty()) {
[email protected]bf5c2ff392009-07-08 16:24:331756 plugin_name = product_name;
[email protected]996fd702009-09-04 19:12:371757#if defined(OS_MACOSX)
1758 // Many plugins on the Mac have .plugin in the actual name, which looks
1759 // terrible, so look for that and strip it off if present.
1760 const std::wstring plugin_extension(L".plugin");
1761 if (EndsWith(plugin_name, plugin_extension, true))
1762 plugin_name.erase(plugin_name.length() - plugin_extension.length());
1763#endif // OS_MACOSX
1764 }
[email protected]bf5c2ff392009-07-08 16:24:331765 }
[email protected]4a560ed2009-07-13 23:35:261766#else
1767 NOTIMPLEMENTED() << " convert plugin path to plugin name";
1768#endif
[email protected]bf5c2ff392009-07-08 16:24:331769 AddInfoBar(new SimpleAlertInfoBarDelegate(
1770 this, l10n_util::GetStringF(IDS_PLUGIN_CRASHED_PROMPT, plugin_name),
1771 NULL));
[email protected]bf5c2ff392009-07-08 16:24:331772}
1773
1774void TabContents::OnCrashedWorker() {
1775 AddInfoBar(new SimpleAlertInfoBarDelegate(
1776 this, l10n_util::GetString(IDS_WEBWORKER_CRASHED_PROMPT),
1777 NULL));
1778}
1779
1780void TabContents::OnDidGetApplicationInfo(
1781 int32 page_id,
1782 const webkit_glue::WebApplicationInfo& info) {
1783 if (pending_install_.page_id != page_id)
1784 return; // The user clicked create on a separate page. Ignore this.
1785
1786 pending_install_.callback_functor =
1787 new GearsCreateShortcutCallbackFunctor(this);
1788 GearsCreateShortcut(
1789 info, pending_install_.title, pending_install_.url, pending_install_.icon,
1790 NewCallback(pending_install_.callback_functor,
1791 &GearsCreateShortcutCallbackFunctor::Run));
1792}
1793
1794void TabContents::DidStartProvisionalLoadForFrame(
1795 RenderViewHost* render_view_host,
1796 bool is_main_frame,
1797 const GURL& url) {
1798 ProvisionalLoadDetails details(is_main_frame,
1799 controller_.IsURLInPageNavigation(url),
1800 url, std::string(), false);
1801 NotificationService::current()->Notify(
1802 NotificationType::FRAME_PROVISIONAL_LOAD_START,
1803 Source<NavigationController>(&controller_),
1804 Details<ProvisionalLoadDetails>(&details));
1805}
1806
1807void TabContents::DidStartReceivingResourceResponse(
1808 ResourceRequestDetails* details) {
1809 NotificationService::current()->Notify(
1810 NotificationType::RESOURCE_RESPONSE_STARTED,
1811 Source<NavigationController>(&controller()),
1812 Details<ResourceRequestDetails>(details));
1813}
1814
1815void TabContents::DidRedirectResource(ResourceRequestDetails* details) {
1816 NotificationService::current()->Notify(
1817 NotificationType::RESOURCE_RECEIVED_REDIRECT,
1818 Source<NavigationController>(&controller()),
1819 Details<ResourceRequestDetails>(details));
1820}
1821
1822void TabContents::DidLoadResourceFromMemoryCache(
1823 const GURL& url,
1824 const std::string& frame_origin,
1825 const std::string& main_frame_origin,
1826 const std::string& security_info) {
1827 // Send out a notification that we loaded a resource from our memory cache.
1828 int cert_id = 0, cert_status = 0, security_bits = 0;
1829 SSLManager::DeserializeSecurityInfo(security_info,
1830 &cert_id, &cert_status,
1831 &security_bits);
1832 LoadFromMemoryCacheDetails details(url, frame_origin, main_frame_origin,
[email protected]76543b92009-08-31 17:27:451833 process()->id(), cert_id, cert_status);
[email protected]bf5c2ff392009-07-08 16:24:331834
1835 NotificationService::current()->Notify(
1836 NotificationType::LOAD_FROM_MEMORY_CACHE,
1837 Source<NavigationController>(&controller_),
1838 Details<LoadFromMemoryCacheDetails>(&details));
1839}
1840
[email protected]e3d60e5d2009-09-25 21:08:291841void TabContents::DidDisplayInsecureContent() {
[email protected]09843b5f2009-09-26 08:30:591842 controller_.ssl_manager()->DidDisplayInsecureContent();
[email protected]e3d60e5d2009-09-25 21:08:291843}
1844
1845void TabContents::DidRunInsecureContent(const std::string& security_origin) {
[email protected]09843b5f2009-09-26 08:30:591846 controller_.ssl_manager()->DidRunInsecureContent(security_origin);
[email protected]e3d60e5d2009-09-25 21:08:291847}
1848
[email protected]bf5c2ff392009-07-08 16:24:331849void TabContents::DidFailProvisionalLoadWithError(
1850 RenderViewHost* render_view_host,
1851 bool is_main_frame,
1852 int error_code,
1853 const GURL& url,
1854 bool showing_repost_interstitial) {
1855 if (net::ERR_ABORTED == error_code) {
1856 // EVIL HACK ALERT! Ignore failed loads when we're showing interstitials.
1857 // This means that the interstitial won't be torn down properly, which is
1858 // bad. But if we have an interstitial, go back to another tab type, and
1859 // then load the same interstitial again, we could end up getting the first
1860 // interstitial's "failed" message (as a result of the cancel) when we're on
1861 // the second one.
1862 //
1863 // We can't tell this apart, so we think we're tearing down the current page
1864 // which will cause a crash later one. There is also some code in
1865 // RenderViewHostManager::RendererAbortedProvisionalLoad that is commented
1866 // out because of this problem.
1867 //
1868 // http://code.google.com/p/chromium/issues/detail?id=2855
1869 // Because this will not tear down the interstitial properly, if "back" is
1870 // back to another tab type, the interstitial will still be somewhat alive
1871 // in the previous tab type. If you navigate somewhere that activates the
1872 // tab with the interstitial again, you'll see a flash before the new load
1873 // commits of the interstitial page.
1874 if (showing_interstitial_page()) {
1875 LOG(WARNING) << "Discarding message during interstitial.";
1876 return;
1877 }
1878
1879 // This will discard our pending entry if we cancelled the load (e.g., if we
1880 // decided to download the file instead of load it). Only discard the
1881 // pending entry if the URLs match, otherwise the user initiated a navigate
1882 // before the page loaded so that the discard would discard the wrong entry.
1883 NavigationEntry* pending_entry = controller_.pending_entry();
1884 if (pending_entry && pending_entry->url() == url) {
1885 controller_.DiscardNonCommittedEntries();
1886 // Update the URL display.
1887 NotifyNavigationStateChanged(TabContents::INVALIDATE_URL);
1888 }
1889
1890 render_manager_.RendererAbortedProvisionalLoad(render_view_host);
1891 }
1892
1893 // Send out a notification that we failed a provisional load with an error.
1894 ProvisionalLoadDetails details(is_main_frame,
1895 controller_.IsURLInPageNavigation(url),
1896 url, std::string(), false);
1897 details.set_error_code(error_code);
1898
1899 NotificationService::current()->Notify(
1900 NotificationType::FAIL_PROVISIONAL_LOAD_WITH_ERROR,
1901 Source<NavigationController>(&controller_),
1902 Details<ProvisionalLoadDetails>(&details));
1903}
1904
1905void TabContents::DocumentLoadedInFrame() {
1906 controller_.DocumentLoadedInFrame();
1907}
1908
[email protected]8d3347f2009-07-09 22:00:211909RenderViewHostDelegate::View* TabContents::GetViewDelegate() {
[email protected]420ae012009-04-24 05:16:321910 return view_.get();
1911}
1912
[email protected]8d3347f2009-07-09 22:00:211913RenderViewHostDelegate::RendererManagement*
1914TabContents::GetRendererManagementDelegate() {
1915 return &render_manager_;
1916}
1917
[email protected]bf5c2ff392009-07-08 16:24:331918RenderViewHostDelegate::BrowserIntegration*
[email protected]8d3347f2009-07-09 22:00:211919 TabContents::GetBrowserIntegrationDelegate() {
1920 return this;
[email protected]bf5c2ff392009-07-08 16:24:331921}
1922
[email protected]8d3347f2009-07-09 22:00:211923RenderViewHostDelegate::Resource* TabContents::GetResourceDelegate() {
1924 return this;
[email protected]bf5c2ff392009-07-08 16:24:331925}
1926
[email protected]8d3347f2009-07-09 22:00:211927RenderViewHostDelegate::Save* TabContents::GetSaveDelegate() {
[email protected]420ae012009-04-24 05:16:321928 return save_package_.get(); // May be NULL, but we can return NULL.
1929}
1930
[email protected]8d3347f2009-07-09 22:00:211931RenderViewHostDelegate::Printing* TabContents::GetPrintingDelegate() {
1932 return &printing_;
1933}
1934
1935RenderViewHostDelegate::FavIcon* TabContents::GetFavIconDelegate() {
1936 return &fav_icon_helper_;
1937}
1938
[email protected]c54d269e2009-10-21 18:07:381939RenderViewHostDelegate::FormFieldHistory*
1940TabContents::GetFormFieldHistoryDelegate() {
[email protected]19289d02009-10-21 21:47:001941 if (form_field_history_manager_.get() == NULL)
1942 form_field_history_manager_.reset(new FormFieldHistoryManager(this));
1943 return form_field_history_manager_.get();
[email protected]bf5c2ff392009-07-08 16:24:331944}
1945
[email protected]80d96fa2009-06-10 22:34:511946RendererPreferences TabContents::GetRendererPrefs() const {
[email protected]840b1512009-07-21 16:53:461947 return renderer_preferences_;
[email protected]80d96fa2009-06-10 22:34:511948}
1949
[email protected]57c6a652009-05-04 07:58:341950TabContents* TabContents::GetAsTabContents() {
1951 return this;
1952}
1953
[email protected]3e7cf1362009-08-21 17:47:081954void TabContents::AddBlockedNotice(const GURL& url, const string16& reason) {
1955 CreateBlockedPopupContainerIfNecessary();
1956 blocked_popups_->AddBlockedNotice(url, reason);
1957}
1958
[email protected]7b291f92009-08-14 05:43:531959ViewType::Type TabContents::GetRenderViewType() const {
1960 return ViewType::TAB_CONTENTS;
1961}
1962
1963int TabContents::GetBrowserWindowID() const {
1964 return controller().window_id().id();
1965}
1966
[email protected]420ae012009-04-24 05:16:321967void TabContents::RenderViewCreated(RenderViewHost* render_view_host) {
[email protected]2e4633c2009-07-09 16:58:061968 NotificationService::current()->Notify(
1969 NotificationType::RENDER_VIEW_HOST_CREATED_FOR_TAB,
1970 Source<TabContents>(this),
1971 Details<RenderViewHost>(render_view_host));
[email protected]420ae012009-04-24 05:16:321972 NavigationEntry* entry = controller_.GetActiveEntry();
1973 if (!entry)
1974 return;
1975
1976 // When we're creating views, we're still doing initial setup, so we always
1977 // use the pending DOM UI rather than any possibly existing committed one.
1978 if (render_manager_.pending_dom_ui()) {
1979 render_manager_.pending_dom_ui()->RenderViewCreated(render_view_host);
1980 }
1981
1982 if (entry->IsViewSourceMode()) {
1983 // Put the renderer in view source mode.
1984 render_view_host->Send(
1985 new ViewMsg_EnableViewSourceMode(render_view_host->routing_id()));
1986 }
[email protected]0666aef2009-05-13 19:48:081987
1988 view()->RenderViewCreated(render_view_host);
[email protected]420ae012009-04-24 05:16:321989}
1990
1991void TabContents::RenderViewReady(RenderViewHost* rvh) {
1992 if (rvh != render_view_host()) {
1993 // Don't notify the world, since this came from a renderer in the
1994 // background.
1995 return;
1996 }
1997
1998 NotifyConnected();
[email protected]9d8a4642009-07-29 17:25:301999 bool was_crashed = is_crashed();
[email protected]420ae012009-04-24 05:16:322000 SetIsCrashed(false);
[email protected]9d8a4642009-07-29 17:25:302001
2002 // Restore the focus to the tab (otherwise the focus will be on the top
2003 // window).
2004 if (was_crashed && !FocusLocationBarByDefault())
2005 Focus();
[email protected]420ae012009-04-24 05:16:322006}
2007
2008void TabContents::RenderViewGone(RenderViewHost* rvh) {
2009 // Ask the print preview if this renderer was valuable.
2010 if (!printing_.OnRenderViewGone(rvh))
2011 return;
2012 if (rvh != render_view_host()) {
2013 // The pending page's RenderViewHost is gone.
2014 return;
2015 }
2016
2017 SetIsLoading(false, NULL);
2018 NotifyDisconnected();
2019 SetIsCrashed(true);
2020
[email protected]8e537302009-06-10 21:19:582021 // Tell the view that we've crashed so it can prepare the sad tab page.
2022 view_->OnTabCrashed();
[email protected]420ae012009-04-24 05:16:322023
2024 // Hide any visible hung renderer warning for this web contents' process.
[email protected]d82ed61e2009-06-16 02:46:222025 HungRendererDialog::HideForTabContents(this);
[email protected]420ae012009-04-24 05:16:322026}
2027
[email protected]2e4633c2009-07-09 16:58:062028void TabContents::RenderViewDeleted(RenderViewHost* rvh) {
2029 NotificationService::current()->Notify(
2030 NotificationType::RENDER_VIEW_HOST_DELETED,
2031 Source<TabContents>(this),
2032 Details<RenderViewHost>(rvh));
2033}
2034
[email protected]420ae012009-04-24 05:16:322035void TabContents::DidNavigate(RenderViewHost* rvh,
2036 const ViewHostMsg_FrameNavigate_Params& params) {
[email protected]8030f012009-09-25 18:09:372037 int extra_invalidate_flags = 0;
2038
2039 if (PageTransition::IsMainFrame(params.transition)) {
[email protected]20ede072009-10-06 00:28:352040 bool was_bookmark_bar_visible = ShouldShowBookmarkBar();
[email protected]8030f012009-09-25 18:09:372041 bool was_extension_shelf_visible = IsExtensionShelfAlwaysVisible();
2042
[email protected]420ae012009-04-24 05:16:322043 render_manager_.DidNavigateMainFrame(rvh);
2044
[email protected]20ede072009-10-06 00:28:352045 if (was_bookmark_bar_visible != ShouldShowBookmarkBar())
[email protected]8030f012009-09-25 18:09:372046 extra_invalidate_flags |= INVALIDATE_BOOKMARK_BAR;
2047 if (was_extension_shelf_visible != IsExtensionShelfAlwaysVisible())
2048 extra_invalidate_flags |= INVALIDATE_EXTENSION_SHELF;
2049 }
2050
[email protected]420ae012009-04-24 05:16:322051 // Update the site of the SiteInstance if it doesn't have one yet.
2052 if (!GetSiteInstance()->has_site())
2053 GetSiteInstance()->SetSite(params.url);
2054
2055 // Need to update MIME type here because it's referred to in
2056 // UpdateNavigationCommands() called by RendererDidNavigate() to
2057 // determine whether or not to enable the encoding menu.
2058 // It's updated only for the main frame. For a subframe,
2059 // RenderView::UpdateURL does not set params.contents_mime_type.
2060 // (see http://code.google.com/p/chromium/issues/detail?id=2929 )
2061 // TODO(jungshik): Add a test for the encoding menu to avoid
2062 // regressing it again.
2063 if (PageTransition::IsMainFrame(params.transition))
2064 contents_mime_type_ = params.contents_mime_type;
2065
2066 NavigationController::LoadCommittedDetails details;
[email protected]8030f012009-09-25 18:09:372067 bool did_navigate = controller_.RendererDidNavigate(
2068 params, extra_invalidate_flags, &details);
[email protected]3e90d4a2009-07-03 17:38:392069
2070 // Update history. Note that this needs to happen after the entry is complete,
2071 // which WillNavigate[Main,Sub]Frame will do before this function is called.
2072 if (params.should_update_history) {
2073 // Most of the time, the displayURL matches the loaded URL, but for about:
2074 // URLs, we use a data: URL as the real value. We actually want to save
2075 // the about: URL to the history db and keep the data: URL hidden. This is
2076 // what the TabContents' URL getter does.
2077 UpdateHistoryForNavigation(GetURL(), details, params);
2078 }
2079
[email protected]76543b92009-08-31 17:27:452080 if (!did_navigate)
2081 return; // No navigation happened.
[email protected]420ae012009-04-24 05:16:322082
2083 // DO NOT ADD MORE STUFF TO THIS FUNCTION! Your component should either listen
2084 // for the appropriate notification (best) or you can add it to
2085 // DidNavigateMainFramePostCommit / DidNavigateAnyFramePostCommit (only if
2086 // necessary, please).
2087
2088 // Run post-commit tasks.
2089 if (details.is_main_frame)
2090 DidNavigateMainFramePostCommit(details, params);
2091 DidNavigateAnyFramePostCommit(rvh, details, params);
2092}
2093
2094void TabContents::UpdateState(RenderViewHost* rvh,
2095 int32 page_id,
2096 const std::string& state) {
2097 DCHECK(rvh == render_view_host());
2098
2099 // We must be prepared to handle state updates for any page, these occur
2100 // when the user is scrolling and entering form data, as well as when we're
2101 // leaving a page, in which case our state may have already been moved to
2102 // the next page. The navigation controller will look up the appropriate
2103 // NavigationEntry and update it when it is notified via the delegate.
2104
2105 int entry_index = controller_.GetEntryIndexWithPageID(
2106 GetSiteInstance(), page_id);
2107 if (entry_index < 0)
2108 return;
2109 NavigationEntry* entry = controller_.GetEntryAtIndex(entry_index);
2110
2111 if (state == entry->content_state())
2112 return; // Nothing to update.
2113 entry->set_content_state(state);
2114 controller_.NotifyEntryChanged(entry, entry_index);
2115}
2116
2117void TabContents::UpdateTitle(RenderViewHost* rvh,
2118 int32 page_id, const std::wstring& title) {
2119 // If we have a title, that's a pretty good indication that we've started
2120 // getting useful data.
2121 SetNotWaitingForResponse();
2122
2123 DCHECK(rvh == render_view_host());
2124 NavigationEntry* entry = controller_.GetEntryWithPageID(GetSiteInstance(),
2125 page_id);
2126 if (!entry || !UpdateTitleForEntry(entry, title))
2127 return;
2128
2129 // Broadcast notifications when the UI should be updated.
2130 if (entry == controller_.GetEntryAtOffset(0))
[email protected]c9cd2222009-05-06 05:16:502131 NotifyNavigationStateChanged(INVALIDATE_TAB);
[email protected]420ae012009-04-24 05:16:322132}
2133
[email protected]420ae012009-04-24 05:16:322134void TabContents::UpdateEncoding(RenderViewHost* render_view_host,
[email protected]41fc0322009-09-04 22:23:402135 const std::string& encoding) {
[email protected]420ae012009-04-24 05:16:322136 set_encoding(encoding);
2137}
2138
2139void TabContents::UpdateTargetURL(int32 page_id, const GURL& url) {
2140 if (delegate())
2141 delegate()->UpdateTargetURL(this, url);
2142}
2143
2144void TabContents::UpdateThumbnail(const GURL& url,
2145 const SkBitmap& bitmap,
2146 const ThumbnailScore& score) {
2147 // Tell History about this thumbnail
[email protected]449478302009-06-09 20:04:282148 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kThumbnailStore)) {
[email protected]72d0fc72009-07-09 17:30:492149 if (!profile()->IsOffTheRecord())
[email protected]42339402009-07-17 17:27:192150 profile()->GetThumbnailStore()->SetPageThumbnail(url, bitmap,
2151 score, true);
[email protected]449478302009-06-09 20:04:282152 } else {
2153 HistoryService* hs;
2154 if (!profile()->IsOffTheRecord() &&
2155 (hs = profile()->GetHistoryService(Profile::IMPLICIT_ACCESS))) {
2156 hs->SetPageThumbnail(url, bitmap, score);
2157 }
[email protected]420ae012009-04-24 05:16:322158 }
2159}
2160
[email protected]8922e1f2009-10-03 05:01:262161void TabContents::UpdateInspectorSettings(const std::string& raw_settings) {
[email protected]0df30122009-06-03 12:13:082162 profile()->GetPrefs()->SetString(prefs::kWebKitInspectorSettings,
[email protected]8922e1f2009-10-03 05:01:262163 UTF8ToWide(raw_settings));
[email protected]0df30122009-06-03 12:13:082164}
2165
[email protected]420ae012009-04-24 05:16:322166void TabContents::Close(RenderViewHost* rvh) {
[email protected]24a4d1062009-07-10 23:10:422167 // If we close the tab while we're in the middle of a drag, we'll crash.
2168 // Instead, cancel the drag and close it as soon as the drag ends.
2169 if (view()->IsDoingDrag()) {
2170 view()->CancelDragAndCloseTab();
2171 return;
2172 }
2173
[email protected]420ae012009-04-24 05:16:322174 // Ignore this if it comes from a RenderViewHost that we aren't showing.
2175 if (delegate() && rvh == render_view_host())
2176 delegate()->CloseContents(this);
2177}
2178
2179void TabContents::RequestMove(const gfx::Rect& new_bounds) {
2180 if (delegate() && delegate()->IsPopup(this))
2181 delegate()->MoveContents(this, new_bounds);
2182}
2183
[email protected]7ab1e7d62009-10-14 23:32:012184void TabContents::DidStartLoading() {
[email protected]420ae012009-04-24 05:16:322185 SetIsLoading(true, NULL);
2186}
2187
[email protected]7ab1e7d62009-10-14 23:32:012188void TabContents::DidStopLoading() {
[email protected]420ae012009-04-24 05:16:322189 scoped_ptr<LoadNotificationDetails> details;
2190
2191 NavigationEntry* entry = controller_.GetActiveEntry();
2192 // An entry may not exist for a stop when loading an initial blank page or
2193 // if an iframe injected by script into a blank page finishes loading.
2194 if (entry) {
2195 scoped_ptr<base::ProcessMetrics> metrics(
2196 base::ProcessMetrics::CreateProcessMetrics(
2197 process()->process().handle()));
2198
2199 base::TimeDelta elapsed = base::TimeTicks::Now() - current_load_start_;
2200
2201 details.reset(new LoadNotificationDetails(
[email protected]ebe89e062009-08-13 23:16:542202 entry->virtual_url(),
[email protected]420ae012009-04-24 05:16:322203 entry->transition_type(),
2204 elapsed,
2205 &controller_,
2206 controller_.GetCurrentEntryIndex()));
2207 }
2208
2209 // Tell PasswordManager we've finished a page load, which serves as a
2210 // green light to save pending passwords and reset itself.
2211 GetPasswordManager()->DidStopLoading();
2212
2213 SetIsLoading(false, details.get());
2214}
2215
[email protected]420ae012009-04-24 05:16:322216void TabContents::DidRedirectProvisionalLoad(int32 page_id,
2217 const GURL& source_url,
2218 const GURL& target_url) {
2219 NavigationEntry* entry;
2220 if (page_id == -1)
2221 entry = controller_.pending_entry();
2222 else
2223 entry = controller_.GetEntryWithPageID(GetSiteInstance(), page_id);
2224 if (!entry || entry->url() != source_url)
2225 return;
2226 entry->set_url(target_url);
2227}
2228
[email protected]420ae012009-04-24 05:16:322229void TabContents::RequestOpenURL(const GURL& url, const GURL& referrer,
2230 WindowOpenDisposition disposition) {
2231 if (render_manager_.dom_ui()) {
2232 // When we're a DOM UI, it will provide a page transition type for us (this
2233 // is so the new tab page can specify AUTO_BOOKMARK for automatically
2234 // generated suggestions).
2235 //
2236 // Note also that we hide the referrer for DOM UI pages. We don't really
[email protected]60e448982009-05-06 04:21:162237 // want web sites to see a referrer of "chrome://blah" (and some
2238 // chrome: URLs might have search terms or other stuff we don't want to
[email protected]420ae012009-04-24 05:16:322239 // send to the site), so we send no referrer.
2240 OpenURL(url, GURL(), disposition,
2241 render_manager_.dom_ui()->link_transition_type());
2242 } else {
2243 OpenURL(url, referrer, disposition, PageTransition::LINK);
2244 }
2245}
2246
[email protected]1ee614862009-07-06 20:29:182247void TabContents::DomOperationResponse(const std::string& json_string,
2248 int automation_id) {
2249}
2250
[email protected]420ae012009-04-24 05:16:322251void TabContents::ProcessDOMUIMessage(const std::string& message,
[email protected]e4dad9fb2009-10-06 18:15:582252 const Value* content,
[email protected]811bfe32009-07-01 08:46:252253 int request_id,
2254 bool has_callback) {
[email protected]420ae012009-04-24 05:16:322255 if (!render_manager_.dom_ui()) {
[email protected]c2e74fe82009-09-03 17:57:442256 // This can happen if someone uses window.open() to open an extension URL
2257 // from a non-extension context.
2258 render_view_host()->BlockExtensionRequest(request_id);
[email protected]420ae012009-04-24 05:16:322259 return;
2260 }
[email protected]811bfe32009-07-01 08:46:252261 render_manager_.dom_ui()->ProcessDOMUIMessage(message, content, request_id,
2262 has_callback);
[email protected]420ae012009-04-24 05:16:322263}
2264
2265void TabContents::ProcessExternalHostMessage(const std::string& message,
2266 const std::string& origin,
2267 const std::string& target) {
2268 if (delegate())
2269 delegate()->ForwardMessageToExternalHost(message, origin, target);
2270}
2271
[email protected]420ae012009-04-24 05:16:322272void TabContents::RunFileChooser(bool multiple_files,
2273 const string16& title,
2274 const FilePath& default_file) {
2275 if (!select_file_dialog_.get())
2276 select_file_dialog_ = SelectFileDialog::Create(this);
2277 SelectFileDialog::Type dialog_type =
2278 multiple_files ? SelectFileDialog::SELECT_OPEN_MULTI_FILE :
2279 SelectFileDialog::SELECT_OPEN_FILE;
2280 select_file_dialog_->SelectFile(dialog_type, title, default_file,
2281 NULL, 0, FILE_PATH_LITERAL(""),
2282 view_->GetTopLevelNativeWindow(), NULL);
2283}
2284
2285void TabContents::RunJavaScriptMessage(
2286 const std::wstring& message,
2287 const std::wstring& default_prompt,
2288 const GURL& frame_url,
2289 const int flags,
2290 IPC::Message* reply_msg,
2291 bool* did_suppress_message) {
2292 // Suppress javascript messages when requested and when inside a constrained
2293 // popup window (because that activates them and breaks them out of the
2294 // constrained window jail).
[email protected]91ed49a2009-07-07 21:29:552295 // Also suppress messages when showing an interstitial. The interstitial is
2296 // shown over the previous page, we don't want the hidden page dialogs to
2297 // interfere with the interstitial.
2298 bool suppress_this_message = suppress_javascript_messages_ ||
2299 showing_interstitial_page();
[email protected]420ae012009-04-24 05:16:322300 if (delegate())
2301 suppress_this_message |=
[email protected]b6c874582009-05-08 19:38:312302 (delegate()->GetConstrainingContents(this) != this);
[email protected]420ae012009-04-24 05:16:322303
2304 *did_suppress_message = suppress_this_message;
2305
2306 if (!suppress_this_message) {
2307 base::TimeDelta time_since_last_message(
2308 base::TimeTicks::Now() - last_javascript_message_dismissal_);
2309 bool show_suppress_checkbox = false;
2310 // Show a checkbox offering to suppress further messages if this message is
2311 // being displayed within kJavascriptMessageExpectedDelay of the last one.
2312 if (time_since_last_message <
2313 base::TimeDelta::FromMilliseconds(kJavascriptMessageExpectedDelay))
2314 show_suppress_checkbox = true;
2315
[email protected]d82ed61e2009-06-16 02:46:222316 RunJavascriptMessageBox(this, frame_url, flags, message, default_prompt,
[email protected]420ae012009-04-24 05:16:322317 show_suppress_checkbox, reply_msg);
2318 } else {
2319 // If we are suppressing messages, just reply as is if the user immediately
2320 // pressed "Cancel".
2321 OnJavaScriptMessageBoxClosed(reply_msg, false, std::wstring());
2322 }
2323}
2324
2325void TabContents::RunBeforeUnloadConfirm(const std::wstring& message,
2326 IPC::Message* reply_msg) {
[email protected]7ab1e7d62009-10-14 23:32:012327 is_showing_before_unload_dialog_ = true;
[email protected]d82ed61e2009-06-16 02:46:222328 RunBeforeUnloadDialog(this, message, reply_msg);
[email protected]420ae012009-04-24 05:16:322329}
2330
2331void TabContents::ShowModalHTMLDialog(const GURL& url, int width, int height,
2332 const std::string& json_arguments,
2333 IPC::Message* reply_msg) {
2334 if (delegate()) {
2335 HtmlDialogUIDelegate* dialog_delegate =
2336 new ModalHtmlDialogDelegate(url, width, height, json_arguments,
[email protected]d82ed61e2009-06-16 02:46:222337 reply_msg, this);
[email protected]420ae012009-04-24 05:16:322338 delegate()->ShowHtmlDialog(dialog_delegate, NULL);
2339 }
2340}
2341
2342void TabContents::PasswordFormsSeen(
[email protected]ed3fb032009-06-16 19:50:562343 const std::vector<webkit_glue::PasswordForm>& forms) {
[email protected]420ae012009-04-24 05:16:322344 GetPasswordManager()->PasswordFormsSeen(forms);
2345}
2346
[email protected]420ae012009-04-24 05:16:322347// Checks to see if we should generate a keyword based on the OSDD, and if
2348// necessary uses TemplateURLFetcher to download the OSDD and create a keyword.
2349void TabContents::PageHasOSDD(RenderViewHost* render_view_host,
2350 int32 page_id, const GURL& url,
2351 bool autodetected) {
2352 // Make sure page_id is the current page, and the TemplateURLModel is loaded.
2353 DCHECK(url.is_valid());
2354 if (!IsActiveEntry(page_id))
2355 return;
2356 TemplateURLModel* url_model = profile()->GetTemplateURLModel();
2357 if (!url_model)
2358 return;
2359 if (!url_model->loaded()) {
2360 url_model->Load();
2361 return;
2362 }
2363 if (!profile()->GetTemplateURLFetcher())
2364 return;
2365
2366 if (profile()->IsOffTheRecord())
2367 return;
2368
2369 const NavigationEntry* entry = controller_.GetLastCommittedEntry();
2370 DCHECK(entry);
2371
2372 const NavigationEntry* base_entry = entry;
2373 if (IsFormSubmit(base_entry)) {
2374 // If the current page is a form submit, find the last page that was not
2375 // a form submit and use its url to generate the keyword from.
2376 int index = controller_.last_committed_entry_index() - 1;
2377 while (index >= 0 && IsFormSubmit(controller_.GetEntryAtIndex(index)))
2378 index--;
2379 if (index >= 0)
2380 base_entry = controller_.GetEntryAtIndex(index);
2381 else
2382 base_entry = NULL;
2383 }
2384
2385 // We want to use the user typed URL if available since that represents what
2386 // the user typed to get here, and fall back on the regular URL if not.
2387 if (!base_entry)
2388 return;
2389 GURL keyword_url = base_entry->user_typed_url().is_valid() ?
2390 base_entry->user_typed_url() : base_entry->url();
2391 if (!keyword_url.is_valid())
2392 return;
2393 std::wstring keyword = TemplateURLModel::GenerateKeyword(keyword_url,
2394 autodetected);
2395 if (keyword.empty())
2396 return;
2397 const TemplateURL* template_url =
2398 url_model->GetTemplateURLForKeyword(keyword);
2399 if (template_url && (!template_url->safe_for_autoreplace() ||
2400 template_url->originating_url() == url)) {
2401 // Either there is a user created TemplateURL for this keyword, or the
2402 // keyword has the same OSDD url and we've parsed it.
2403 return;
2404 }
2405
2406 // Download the OpenSearch description document. If this is successful a
2407 // new keyword will be created when done.
[email protected]420ae012009-04-24 05:16:322408 profile()->GetTemplateURLFetcher()->ScheduleDownload(
2409 keyword,
2410 url,
2411 base_entry->favicon().url(),
[email protected]d0cc9fb52009-06-23 02:26:162412 this,
[email protected]420ae012009-04-24 05:16:322413 autodetected);
2414}
2415
[email protected]420ae012009-04-24 05:16:322416GURL TabContents::GetAlternateErrorPageURL() const {
2417 GURL url;
2418 // Disable alternate error pages when in OffTheRecord/Incognito mode.
2419 if (profile()->IsOffTheRecord())
2420 return url;
2421
2422 PrefService* prefs = profile()->GetPrefs();
2423 DCHECK(prefs);
2424 if (prefs->GetBoolean(prefs::kAlternateErrorPagesEnabled)) {
[email protected]a33457b82009-09-21 23:07:152425 url = google_util::AppendGoogleLocaleParam(
2426 GURL(google_util::kLinkDoctorBaseURL));
[email protected]420ae012009-04-24 05:16:322427 url = google_util::AppendGoogleTLDParam(url);
2428 }
2429 return url;
2430}
2431
2432WebPreferences TabContents::GetWebkitPrefs() {
2433 PrefService* prefs = render_view_host()->process()->profile()->GetPrefs();
[email protected]7ccddb8c2009-08-04 17:36:552434 bool is_dom_ui = false;
2435 return RenderViewHostDelegateHelper::GetWebkitPrefs(prefs, is_dom_ui);
[email protected]420ae012009-04-24 05:16:322436}
2437
[email protected]420ae012009-04-24 05:16:322438void TabContents::OnJSOutOfMemory() {
2439 AddInfoBar(new SimpleAlertInfoBarDelegate(
2440 this, l10n_util::GetString(IDS_JS_OUT_OF_MEMORY_PROMPT), NULL));
2441}
2442
[email protected]420ae012009-04-24 05:16:322443void TabContents::OnCrossSiteResponse(int new_render_process_host_id,
2444 int new_request_id) {
[email protected]57c6a652009-05-04 07:58:342445 // Allows the TabContents to react when a cross-site response is ready to be
[email protected]420ae012009-04-24 05:16:322446 // delivered to a pending RenderViewHost. We must first run the onunload
2447 // handler of the old RenderViewHost before we can allow it to proceed.
2448 render_manager_.OnCrossSiteResponse(new_render_process_host_id,
2449 new_request_id);
2450}
2451
2452bool TabContents::CanBlur() const {
2453 return delegate() ? delegate()->CanBlur() : true;
2454}
2455
2456gfx::Rect TabContents::GetRootWindowResizerRect() const {
2457 if (delegate())
2458 return delegate()->GetRootWindowResizerRect();
2459 return gfx::Rect();
2460}
2461
2462void TabContents::RendererUnresponsive(RenderViewHost* rvh,
2463 bool is_during_unload) {
2464 if (is_during_unload) {
2465 // Hang occurred while firing the beforeunload/unload handler.
2466 // Pretend the handler fired so tab closing continues as if it had.
[email protected]829e7612009-04-25 01:15:112467 rvh->set_sudden_termination_allowed(true);
[email protected]420ae012009-04-24 05:16:322468
2469 if (!render_manager_.ShouldCloseTabOnUnresponsiveRenderer())
2470 return;
2471
2472 // If the tab hangs in the beforeunload/unload handler there's really
2473 // nothing we can do to recover. Pretend the unload listeners have
2474 // all fired and close the tab. If the hang is in the beforeunload handler
2475 // then the user will not have the option of cancelling the close.
2476 Close(rvh);
2477 return;
2478 }
2479
2480 if (render_view_host() && render_view_host()->IsRenderViewLive())
[email protected]d82ed61e2009-06-16 02:46:222481 HungRendererDialog::ShowForTabContents(this);
[email protected]420ae012009-04-24 05:16:322482}
2483
2484void TabContents::RendererResponsive(RenderViewHost* render_view_host) {
[email protected]d82ed61e2009-06-16 02:46:222485 HungRendererDialog::HideForTabContents(this);
[email protected]420ae012009-04-24 05:16:322486}
2487
2488void TabContents::LoadStateChanged(const GURL& url,
[email protected]094e5b22009-09-25 04:23:562489 net::LoadState load_state,
2490 uint64 upload_position,
2491 uint64 upload_size) {
[email protected]420ae012009-04-24 05:16:322492 load_state_ = load_state;
[email protected]094e5b22009-09-25 04:23:562493 upload_position_ = upload_position;
2494 upload_size_ = upload_size;
[email protected]d686e812009-06-03 19:10:292495 std::wstring languages =
2496 profile()->GetPrefs()->GetString(prefs::kAcceptLanguages);
2497 load_state_host_.clear();
2498 std::string host = url.host();
2499 net::IDNToUnicode(host.c_str(), host.size(), languages, &load_state_host_);
[email protected]420ae012009-04-24 05:16:322500 if (load_state_ == net::LOAD_STATE_READING_RESPONSE)
2501 SetNotWaitingForResponse();
2502 if (is_loading())
[email protected]c9cd2222009-05-06 05:16:502503 NotifyNavigationStateChanged(INVALIDATE_LOAD | INVALIDATE_TAB);
[email protected]420ae012009-04-24 05:16:322504}
2505
[email protected]1ee614862009-07-06 20:29:182506bool TabContents::IsExternalTabContainer() const {
2507 if (!delegate())
2508 return false;
2509
2510 return delegate()->IsExternalTabContainer();
2511}
2512
[email protected]1ee614862009-07-06 20:29:182513void TabContents::DidInsertCSS() {
2514 // This RVHDelegate function is used for extensions and not us.
[email protected]420ae012009-04-24 05:16:322515}
2516
[email protected]130efb02009-09-18 18:54:352517void TabContents::FocusedNodeChanged() {
2518 NotificationService::current()->Notify(
2519 NotificationType::FOCUS_CHANGED_IN_PAGE,
2520 Source<RenderViewHost>(render_view_host()),
2521 NotificationService::NoDetails());
2522}
2523
[email protected]420ae012009-04-24 05:16:322524void TabContents::FileSelected(const FilePath& path,
2525 int index, void* params) {
2526 render_view_host()->FileSelected(path);
2527}
2528
2529void TabContents::MultiFilesSelected(const std::vector<FilePath>& files,
2530 void* params) {
2531 render_view_host()->MultiFilesSelected(files);
2532}
2533
2534void TabContents::FileSelectionCanceled(void* params) {
2535 // If the user cancels choosing a file to upload we pass back an
2536 // empty vector.
2537 render_view_host()->MultiFilesSelected(std::vector<FilePath>());
2538}
2539
2540void TabContents::BeforeUnloadFiredFromRenderManager(
2541 bool proceed,
2542 bool* proceed_to_fire_unload) {
2543 if (delegate())
2544 delegate()->BeforeUnloadFired(this, proceed, proceed_to_fire_unload);
2545}
2546
2547void TabContents::UpdateRenderViewSizeForRenderManager() {
2548 // TODO(brettw) this is a hack. See TabContentsView::SizeContents.
2549 view_->SizeContents(view_->GetContainerSize());
2550}
2551
2552DOMUI* TabContents::CreateDOMUIForRenderManager(const GURL& url) {
[email protected]d82ed61e2009-06-16 02:46:222553 return DOMUIFactory::CreateDOMUIForURL(this, url);
[email protected]420ae012009-04-24 05:16:322554}
2555
2556NavigationEntry*
2557TabContents::GetLastCommittedNavigationEntryForRenderManager() {
2558 return controller_.GetLastCommittedEntry();
2559}
2560
2561bool TabContents::CreateRenderViewForRenderManager(
2562 RenderViewHost* render_view_host) {
[email protected]811bfe32009-07-01 08:46:252563 // If the pending navigation is to a DOMUI, tell the RenderView about any
2564 // bindings it will need enabled.
[email protected]420ae012009-04-24 05:16:322565 if (render_manager_.pending_dom_ui())
[email protected]811bfe32009-07-01 08:46:252566 render_view_host->AllowBindings(
2567 render_manager_.pending_dom_ui()->bindings());
[email protected]1adff062009-06-02 18:39:552568
[email protected]420ae012009-04-24 05:16:322569 RenderWidgetHostView* rwh_view = view_->CreateViewForWidget(render_view_host);
2570 if (!render_view_host->CreateRenderView())
2571 return false;
2572
2573 // Now that the RenderView has been created, we need to tell it its size.
2574 rwh_view->SetSize(view_->GetContainerSize());
2575
2576 UpdateMaxPageIDIfNecessary(render_view_host->site_instance(),
2577 render_view_host);
2578 return true;
2579}
2580
2581void TabContents::Observe(NotificationType type,
2582 const NotificationSource& source,
2583 const NotificationDetails& details) {
2584 switch (type.value) {
2585 case NotificationType::BOOKMARK_MODEL_LOADED:
2586 // BookmarkModel finished loading, fall through to update starred state.
2587 case NotificationType::URLS_STARRED: {
2588 // Somewhere, a URL has been starred.
2589 // Ignore notifications for profiles other than our current one.
2590 Profile* source_profile = Source<Profile>(source).ptr();
2591 if (!source_profile || !source_profile->IsSameProfile(profile()))
2592 return;
2593
2594 UpdateStarredStateForCurrentURL();
2595 break;
2596 }
2597 case NotificationType::PREF_CHANGED: {
2598 std::wstring* pref_name_in = Details<std::wstring>(details).ptr();
2599 DCHECK(Source<PrefService>(source).ptr() == profile()->GetPrefs());
2600 if (*pref_name_in == prefs::kAlternateErrorPagesEnabled) {
2601 UpdateAlternateErrorPageURL();
2602 } else if (*pref_name_in == prefs::kDefaultCharset ||
2603 StartsWithASCII(WideToUTF8(*pref_name_in), "webkit.webprefs.", true)
2604 ) {
2605 UpdateWebPreferences();
2606 } else {
2607 NOTREACHED() << "unexpected pref change notification" << *pref_name_in;
2608 }
2609 break;
2610 }
2611 case NotificationType::RENDER_WIDGET_HOST_DESTROYED:
2612 view_->RenderWidgetHostDestroyed(Source<RenderWidgetHost>(source).ptr());
2613 break;
2614
2615 case NotificationType::NAV_ENTRY_COMMITTED: {
2616 DCHECK(&controller_ == Source<NavigationController>(source).ptr());
2617
2618 NavigationController::LoadCommittedDetails& committed_details =
2619 *(Details<NavigationController::LoadCommittedDetails>(details).ptr());
2620 ExpireInfoBars(committed_details);
2621 break;
2622 }
2623
[email protected]d3ba77272009-09-03 00:06:092624#if defined(OS_LINUX)
2625 case NotificationType::BROWSER_THEME_CHANGED: {
2626 render_view_host()->SyncRendererPrefs();
2627 break;
2628 }
2629#endif
2630
[email protected]420ae012009-04-24 05:16:322631 default:
2632 NOTREACHED();
2633 }
2634}
[email protected]aacd7cc2009-09-02 14:51:102635
[email protected]41fc0322009-09-04 22:23:402636void TabContents::set_encoding(const std::string& encoding) {
[email protected]aacd7cc2009-09-02 14:51:102637 encoding_ = CharacterEncoding::GetCanonicalEncodingNameByAliasName(encoding);
2638}