blob: 35e9b80b6c689576472310fee6ab286dd69ee23b [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]21674df2009-10-28 19:51:2114#include "chrome/browser/autofill/autofill_manager.h"
[email protected]66ba4932009-06-04 19:22:1315#include "chrome/browser/blocked_popup_container.h"
[email protected]420ae012009-04-24 05:16:3216#include "chrome/browser/bookmarks/bookmark_model.h"
[email protected]96d185d2009-04-24 03:28:5417#include "chrome/browser/browser_process.h"
initial.commit09911bf2008-07-26 23:55:2918#include "chrome/browser/cert_store.h"
[email protected]aacd7cc2009-09-02 14:51:1019#include "chrome/browser/character_encoding.h"
[email protected]96d185d2009-04-24 03:28:5420#include "chrome/browser/debugger/devtools_manager.h"
[email protected]420ae012009-04-24 05:16:3221#include "chrome/browser/dom_operation_notification_details.h"
[email protected]96d185d2009-04-24 03:28:5422#include "chrome/browser/dom_ui/dom_ui.h"
23#include "chrome/browser/dom_ui/dom_ui_factory.h"
[email protected]bcef0dc02009-02-28 00:35:0224#include "chrome/browser/download/download_item_model.h"
[email protected]420ae012009-04-24 05:16:3225#include "chrome/browser/download/download_manager.h"
26#include "chrome/browser/download/download_request_manager.h"
[email protected]e7eaedde2009-09-25 20:09:4927#include "chrome/browser/external_protocol_handler.h"
[email protected]92c6f9b92009-10-24 04:35:0828#include "chrome/browser/extensions/extensions_service.h"
[email protected]0189bc722009-08-28 21:56:4829#include "chrome/browser/favicon_service.h"
[email protected]19289d02009-10-21 21:47:0030#include "chrome/browser/form_field_history_manager.h"
[email protected]420ae012009-04-24 05:16:3231#include "chrome/browser/gears_integration.h"
32#include "chrome/browser/google_util.h"
[email protected]8897c382009-05-06 17:53:2633#include "chrome/browser/hung_renderer_dialog.h"
[email protected]420ae012009-04-24 05:16:3234#include "chrome/browser/jsmessage_box_handler.h"
35#include "chrome/browser/load_from_memory_cache_details.h"
36#include "chrome/browser/load_notification_details.h"
[email protected]84abba62009-10-07 17:01:4437#include "chrome/browser/metrics/metric_event_duration_details.h"
[email protected]ea0e80892009-08-22 00:36:0538#include "chrome/browser/modal_html_dialog_delegate.h"
[email protected]c8865482009-07-23 20:40:1039#include "chrome/browser/omnibox_search_hint.h"
[email protected]fdd61c62009-04-22 19:22:5740#include "chrome/browser/password_manager/password_manager.h"
41#include "chrome/browser/plugin_installer.h"
[email protected]ce560f82009-06-03 09:39:4442#include "chrome/browser/profile.h"
[email protected]1db6ff152009-10-12 15:32:0743#include "chrome/browser/renderer_host/render_process_host.h"
[email protected]96d185d2009-04-24 03:28:5444#include "chrome/browser/renderer_host/render_widget_host_view.h"
[email protected]fd694982009-07-03 17:32:3745#include "chrome/browser/renderer_host/resource_request_details.h"
[email protected]1db6ff152009-10-12 15:32:0746#include "chrome/browser/renderer_host/site_instance.h"
[email protected]96d185d2009-04-24 03:28:5447#include "chrome/browser/renderer_host/web_cache_manager.h"
[email protected]bd1ad682009-05-15 22:19:1748#include "chrome/browser/tab_contents/infobar_delegate.h"
[email protected]7e383692009-06-12 19:14:5449#include "chrome/browser/tab_contents/interstitial_page.h"
[email protected]f3ec7742009-01-15 00:59:1650#include "chrome/browser/tab_contents/navigation_entry.h"
[email protected]de34a962009-06-26 23:22:4351#include "chrome/browser/tab_contents/provisional_load_details.h"
[email protected]f3ec7742009-01-15 00:59:1652#include "chrome/browser/tab_contents/tab_contents_delegate.h"
[email protected]fdd61c62009-04-22 19:22:5753#include "chrome/browser/tab_contents/tab_contents_view.h"
[email protected]58dca552009-06-17 00:35:0254#include "chrome/browser/tab_contents/thumbnail_generator.h"
[email protected]449478302009-06-09 20:04:2855#include "chrome/browser/thumbnail_store.h"
[email protected]420ae012009-04-24 05:16:3256#include "chrome/browser/search_engines/template_url_fetcher.h"
57#include "chrome/browser/search_engines/template_url_model.h"
[email protected]35f7d212009-04-29 21:19:2758#include "chrome/common/chrome_switches.h"
[email protected]5d246db22009-10-27 06:17:5759#include "chrome/common/extensions/extension_action.h"
[email protected]bfd04a62009-02-01 18:16:5660#include "chrome/common/notification_service.h"
initial.commit09911bf2008-07-26 23:55:2961#include "chrome/common/pref_names.h"
[email protected]1eb89e82008-08-15 12:27:0362#include "chrome/common/pref_service.h"
[email protected]420ae012009-04-24 05:16:3263#include "chrome/common/render_messages.h"
[email protected]1ee614862009-07-06 20:29:1864#include "chrome/common/renderer_preferences.h"
[email protected]96d185d2009-04-24 03:28:5465#include "chrome/common/url_constants.h"
[email protected]8897c382009-05-06 17:53:2666#include "grit/generated_resources.h"
[email protected]420ae012009-04-24 05:16:3267#include "grit/locale_settings.h"
68#include "net/base/mime_util.h"
69#include "net/base/net_errors.h"
[email protected]d686e812009-06-03 19:10:2970#include "net/base/net_util.h"
[email protected]420ae012009-04-24 05:16:3271#include "net/base/registry_controlled_domain.h"
[email protected]1ee614862009-07-06 20:29:1872#include "webkit/glue/password_form.h"
73#include "webkit/glue/webpreferences.h"
[email protected]3e45ba92009-02-20 21:09:0074
[email protected]304f8792009-08-19 21:28:0975#if defined(OS_CHROMEOS)
76// For GdkScreen
77#include <gdk/gdk.h>
[email protected]0ba5c5e2009-08-20 16:47:1378#endif // defined(OS_CHROMEOS)
79
80#if defined(OS_LINUX)
81#include "chrome/browser/gtk/create_application_shortcuts_dialog_gtk.h"
[email protected]d3ba77272009-09-03 00:06:0982#include "chrome/browser/gtk/gtk_theme_provider.h"
[email protected]0ba5c5e2009-08-20 16:47:1383#endif // defined(OS_LINUX)
84
[email protected]420ae012009-04-24 05:16:3285// Cross-Site Navigations
86//
87// If a TabContents is told to navigate to a different web site (as determined
88// by SiteInstance), it will replace its current RenderViewHost with a new
89// RenderViewHost dedicated to the new SiteInstance. This works as follows:
90//
91// - Navigate determines whether the destination is cross-site, and if so,
92// it creates a pending_render_view_host_ and moves into the PENDING
93// RendererState.
94// - The pending RVH is "suspended," so that no navigation messages are sent to
95// its renderer until the onbeforeunload JavaScript handler has a chance to
96// run in the current RVH.
97// - The pending RVH tells CrossSiteRequestManager (a thread-safe singleton)
98// that it has a pending cross-site request. ResourceDispatcherHost will
99// check for this when the response arrives.
100// - The current RVH runs its onbeforeunload handler. If it returns false, we
101// cancel all the pending logic and go back to NORMAL. Otherwise we allow
102// the pending RVH to send the navigation request to its renderer.
103// - ResourceDispatcherHost receives a ResourceRequest on the IO thread. It
104// checks CrossSiteRequestManager to see that the RVH responsible has a
105// pending cross-site request, and then installs a CrossSiteEventHandler.
106// - When RDH receives a response, the BufferedEventHandler determines whether
107// it is a download. If so, it sends a message to the new renderer causing
108// it to cancel the request, and the download proceeds in the download
109// thread. For now, we stay in a PENDING state (with a pending RVH) until
[email protected]57c6a652009-05-04 07:58:34110// the next DidNavigate event for this TabContents. This isn't ideal, but it
[email protected]420ae012009-04-24 05:16:32111// doesn't affect any functionality.
112// - After RDH receives a response and determines that it is safe and not a
113// download, it pauses the response to first run the old page's onunload
114// handler. It does this by asynchronously calling the OnCrossSiteResponse
[email protected]57c6a652009-05-04 07:58:34115// method of TabContents on the UI thread, which sends a ClosePage message
[email protected]420ae012009-04-24 05:16:32116// to the current RVH.
117// - Once the onunload handler is finished, a ClosePage_ACK message is sent to
118// the ResourceDispatcherHost, who unpauses the response. Data is then sent
119// to the pending RVH.
120// - The pending renderer sends a FrameNavigate message that invokes the
121// DidNavigate method. This replaces the current RVH with the
122// pending RVH and goes back to the NORMAL RendererState.
123
124namespace {
125
126// Amount of time we wait between when a key event is received and the renderer
127// is queried for its state and pushed to the NavigationEntry.
128const int kQueryStateDelay = 5000;
129
130const int kSyncWaitDelay = 40;
131
[email protected]304f8792009-08-19 21:28:09132#if defined(OS_CHROMEOS)
133// If a popup window is bigger than this fraction of the screen on chrome os,
134// turn it into a tab
135const float kMaxWidthFactor = 0.5;
136const float kMaxHeightFactor = 0.6;
137#endif
138
[email protected]420ae012009-04-24 05:16:32139// If another javascript message box is displayed within
140// kJavascriptMessageExpectedDelay of a previous javascript message box being
141// dismissed, display an option to suppress future message boxes from this
142// contents.
143const int kJavascriptMessageExpectedDelay = 1000;
144
[email protected]420ae012009-04-24 05:16:32145// The list of prefs we want to observe.
146const wchar_t* kPrefsToObserve[] = {
147 prefs::kAlternateErrorPagesEnabled,
148 prefs::kWebKitJavaEnabled,
149 prefs::kWebKitJavascriptEnabled,
150 prefs::kWebKitLoadsImagesAutomatically,
151 prefs::kWebKitPluginsEnabled,
152 prefs::kWebKitUsesUniversalDetector,
153 prefs::kWebKitSerifFontFamily,
154 prefs::kWebKitSansSerifFontFamily,
155 prefs::kWebKitFixedFontFamily,
156 prefs::kWebKitDefaultFontSize,
157 prefs::kWebKitDefaultFixedFontSize,
158 prefs::kDefaultCharset
159 // kWebKitStandardFontIsSerif needs to be added
160 // if we let users pick which font to use, serif or sans-serif when
161 // no font is specified or a CSS generic family (serif or sans-serif)
162 // is not specified.
163};
164
165const int kPrefsToObserveLength = arraysize(kPrefsToObserve);
166
[email protected]420ae012009-04-24 05:16:32167// Returns true if the entry's transition type is FORM_SUBMIT.
168bool IsFormSubmit(const NavigationEntry* entry) {
169 return (PageTransition::StripQualifier(entry->transition_type()) ==
170 PageTransition::FORM_SUBMIT);
171}
172
173#if defined(OS_WIN)
174
175BOOL CALLBACK InvalidateWindow(HWND hwnd, LPARAM lparam) {
176 // Note: erase is required to properly paint some widgets borders. This can
177 // be seen with textfields.
178 InvalidateRect(hwnd, NULL, TRUE);
179 return TRUE;
180}
181#endif
182
[email protected]5e369672009-11-03 23:48:30183ViewMsg_Navigate_Params::NavigationType GetNavigationType(
184 Profile* profile, const NavigationEntry& entry, bool reload) {
185 if (reload)
186 return ViewMsg_Navigate_Params::RELOAD;
187
188 if (entry.restore_type() == NavigationEntry::RESTORE_LAST_SESSION &&
189 profile->DidLastSessionExitCleanly())
190 return ViewMsg_Navigate_Params::RESTORE;
191
192 return ViewMsg_Navigate_Params::NORMAL;
193}
194
195void MakeNavigateParams(Profile* profile, const NavigationEntry& entry,
196 bool reload, ViewMsg_Navigate_Params* params) {
[email protected]056de2d2009-06-26 16:41:34197 params->page_id = entry.page_id();
198 params->url = entry.url();
199 params->referrer = entry.referrer();
200 params->transition = entry.transition_type();
201 params->state = entry.content_state();
[email protected]5e369672009-11-03 23:48:30202 params->navigation_type = GetNavigationType(profile, entry, reload);
[email protected]056de2d2009-06-26 16:41:34203 params->request_time = base::Time::Now();
204}
205
[email protected]420ae012009-04-24 05:16:32206} // namespace
207
208// -----------------------------------------------------------------------------
209
[email protected]fdd61c62009-04-22 19:22:57210// static
211int TabContents::find_request_id_counter_ = -1;
212
[email protected]420ae012009-04-24 05:16:32213class TabContents::GearsCreateShortcutCallbackFunctor {
214 public:
215 explicit GearsCreateShortcutCallbackFunctor(TabContents* contents)
216 : contents_(contents) {}
217
218 void Run(const GearsShortcutData2& shortcut_data, bool success) {
219 if (contents_)
220 contents_->OnGearsCreateShortcutDone(shortcut_data, success);
221 delete this;
222 }
223 void Cancel() {
224 contents_ = NULL;
225 }
226
227 private:
228 TabContents* contents_;
229};
230
[email protected]420ae012009-04-24 05:16:32231TabContents::TabContents(Profile* profile,
232 SiteInstance* site_instance,
233 int routing_id,
[email protected]34ac70502009-09-25 17:07:23234 const TabContents* base_tab_contents)
[email protected]b680ad22009-04-15 23:19:42235 : delegate_(NULL),
[email protected]66ba4932009-06-04 19:22:13236 ALLOW_THIS_IN_INITIALIZER_LIST(controller_(this, profile)),
237 ALLOW_THIS_IN_INITIALIZER_LIST(view_(
[email protected]d82ed61e2009-06-16 02:46:22238 TabContentsView::Create(this))),
239 ALLOW_THIS_IN_INITIALIZER_LIST(render_manager_(this, this)),
[email protected]fdd61c62009-04-22 19:22:57240 property_bag_(),
241 registrar_(),
[email protected]d82ed61e2009-06-16 02:46:22242 ALLOW_THIS_IN_INITIALIZER_LIST(printing_(*this)),
[email protected]fdd61c62009-04-22 19:22:57243 save_package_(),
244 cancelable_consumer_(),
[email protected]19289d02009-10-21 21:47:00245 form_field_history_manager_(),
[email protected]21674df2009-10-28 19:51:21246 autofill_manager_(),
[email protected]fdd61c62009-04-22 19:22:57247 password_manager_(),
248 plugin_installer_(),
[email protected]d82ed61e2009-06-16 02:46:22249 ALLOW_THIS_IN_INITIALIZER_LIST(fav_icon_helper_(this)),
[email protected]fdd61c62009-04-22 19:22:57250 select_file_dialog_(),
251 pending_install_(),
[email protected]d5f942ba2008-09-26 19:30:34252 is_loading_(false),
initial.commit09911bf2008-07-26 23:55:29253 is_crashed_(false),
[email protected]d5f942ba2008-09-26 19:30:34254 waiting_for_response_(false),
[email protected]d5f942ba2008-09-26 19:30:34255 max_page_id_(-1),
[email protected]fdd61c62009-04-22 19:22:57256 current_load_start_(),
257 load_state_(net::LOAD_STATE_IDLE),
258 load_state_host_(),
[email protected]094e5b22009-09-25 04:23:56259 upload_size_(0),
260 upload_position_(0),
[email protected]fdd61c62009-04-22 19:22:57261 received_page_title_(false),
262 is_starred_(false),
263 contents_mime_type_(),
264 encoding_(),
[email protected]332af7732009-03-11 13:21:35265 blocked_popups_(NULL),
[email protected]fdd61c62009-04-22 19:22:57266 infobar_delegates_(),
[email protected]fdd61c62009-04-22 19:22:57267 find_ui_active_(false),
268 find_op_aborted_(false),
269 current_find_request_id_(find_request_id_counter_++),
270 find_text_(),
[email protected]e491f1c2009-05-22 20:28:12271 last_search_case_sensitive_(false),
272 last_search_prepopulate_text_(NULL),
273 last_search_result_(),
[email protected]fdd61c62009-04-22 19:22:57274 capturing_contents_(false),
275 is_being_destroyed_(false),
276 notify_disconnection_(false),
277 history_requests_(),
278#if defined(OS_WIN)
279 message_box_active_(CreateEvent(NULL, TRUE, FALSE, NULL)),
280#endif
281 last_javascript_message_dismissal_(),
[email protected]c2e74fe82009-09-03 17:57:44282 suppress_javascript_messages_(false),
[email protected]7ab1e7d62009-10-14 23:32:01283 is_showing_before_unload_dialog_(false),
[email protected]c2e74fe82009-09-03 17:57:44284 opener_dom_ui_type_(DOMUIFactory::kNoDOMUI) {
[email protected]420ae012009-04-24 05:16:32285 pending_install_.page_id = 0;
286 pending_install_.callback_functor = NULL;
287
[email protected]16779842009-07-08 23:45:29288#if defined(OS_CHROMEOS)
[email protected]58dca552009-06-17 00:35:02289 // Make sure the thumbnailer is started before starting the render manager.
290 // The thumbnailer will want to listen for RVH creations, one of which will
291 // happen in RVHManager::Init.
292 ThumbnailGenerator* generator = g_browser_process->GetThumbnailGenerator();
293 if (generator)
294 generator->StartThumbnailing();
295#endif
296
[email protected]12636df2009-09-28 22:32:21297 render_manager_.Init(profile, site_instance, routing_id);
[email protected]420ae012009-04-24 05:16:32298
[email protected]34ac70502009-09-25 17:07:23299 // We have the initial size of the view be based on the size of the passed in
300 // tab contents (normally a tab from the same window).
301 view_->CreateView(base_tab_contents ?
302 base_tab_contents->view()->GetContainerSize() : gfx::Size());
[email protected]420ae012009-04-24 05:16:32303
304 // Register for notifications about all interested prefs change.
305 PrefService* prefs = profile->GetPrefs();
306 if (prefs) {
307 for (int i = 0; i < kPrefsToObserveLength; ++i)
308 prefs->AddPrefObserver(kPrefsToObserve[i], this);
309 }
310
311 // Register for notifications about URL starredness changing on any profile.
312 registrar_.Add(this, NotificationType::URLS_STARRED,
313 NotificationService::AllSources());
314 registrar_.Add(this, NotificationType::BOOKMARK_MODEL_LOADED,
315 NotificationService::AllSources());
316 registrar_.Add(this, NotificationType::RENDER_WIDGET_HOST_DESTROYED,
317 NotificationService::AllSources());
[email protected]d3ba77272009-09-03 00:06:09318#if defined(OS_LINUX)
319 registrar_.Add(this, NotificationType::BROWSER_THEME_CHANGED,
320 NotificationService::AllSources());
321#endif
[email protected]420ae012009-04-24 05:16:32322
323 // Keep a global copy of the previous search string (if any).
324 static string16 global_last_search = string16();
[email protected]e491f1c2009-05-22 20:28:12325 last_search_prepopulate_text_ = &global_last_search;
[email protected]840b1512009-07-21 16:53:46326
[email protected]c8865482009-07-23 20:40:10327 // Set-up the showing of the omnibox search infobar if applicable.
328 if (OmniboxSearchHint::IsEnabled(profile))
329 omnibox_search_hint_.reset(new OmniboxSearchHint(this));
330
[email protected]840b1512009-07-21 16:53:46331 view_->InitRendererPrefs(&renderer_preferences_);
[email protected]332af7732009-03-11 13:21:35332}
initial.commit09911bf2008-07-26 23:55:29333
334TabContents::~TabContents() {
[email protected]420ae012009-04-24 05:16:32335 is_being_destroyed_ = true;
336
[email protected]bfe4c1582009-10-02 18:11:09337 // We don't want any notifications while we're running our destructor.
[email protected]420ae012009-04-24 05:16:32338 registrar_.RemoveAll();
339
340 // Unregister the notifications of all observed prefs change.
341 PrefService* prefs = profile()->GetPrefs();
342 if (prefs) {
343 for (int i = 0; i < kPrefsToObserveLength; ++i)
344 prefs->RemovePrefObserver(kPrefsToObserve[i], this);
345 }
346
[email protected]420ae012009-04-24 05:16:32347 NotifyDisconnected();
[email protected]d82ed61e2009-06-16 02:46:22348 HungRendererDialog::HideForTabContents(this);
[email protected]420ae012009-04-24 05:16:32349
350 if (pending_install_.callback_functor)
351 pending_install_.callback_functor->Cancel();
352
353 // First cleanly close all child windows.
354 // TODO(mpcomplete): handle case if MaybeCloseChildWindows() already asked
355 // some of these to close. CloseWindows is async, so it might get called
356 // twice before it runs.
357 int size = static_cast<int>(child_windows_.size());
358 for (int i = size - 1; i >= 0; --i) {
359 ConstrainedWindow* window = child_windows_[i];
360 if (window)
361 window->CloseConstrainedWindow();
362 }
363
[email protected]66ba4932009-06-04 19:22:13364 if (blocked_popups_)
365 blocked_popups_->Destroy();
366
[email protected]420ae012009-04-24 05:16:32367 // Notify any observer that have a reference on this tab contents.
368 NotificationService::current()->Notify(
369 NotificationType::TAB_CONTENTS_DESTROYED,
370 Source<TabContents>(this),
371 NotificationService::NoDetails());
372
[email protected]1e5e74f2009-05-27 20:55:12373 // Notify any lasting InfobarDelegates that have not yet been removed that
374 // whatever infobar they were handling in this TabContents has closed,
375 // because the TabContents is going away entirely.
376 // This must happen after the TAB_CONTENTS_DESTROYED notification as the
377 // notification may trigger infobars calls that access their delegate. (and
378 // some implementations of InfoBarDelegate do delete themselves on
379 // InfoBarClosed()).
380 for (int i = 0; i < infobar_delegate_count(); ++i) {
381 InfoBarDelegate* delegate = GetInfoBarDelegateAt(i);
382 delegate->InfoBarClosed();
383 }
384 infobar_delegates_.clear();
385
[email protected]420ae012009-04-24 05:16:32386 // TODO(brettw) this should be moved to the view.
387#if defined(OS_WIN)
388 // If we still have a window handle, destroy it. GetNativeView can return
389 // NULL if this contents was part of a window that closed.
390 if (GetNativeView())
391 ::DestroyWindow(GetNativeView());
392#endif
[email protected]7ab1e7d62009-10-14 23:32:01393
394 // OnCloseStarted isn't called in unit tests.
395 if (!tab_close_start_time_.is_null()) {
396 UMA_HISTOGRAM_TIMES("Tab.Close",
397 base::TimeTicks::Now() - tab_close_start_time_);
398 }
initial.commit09911bf2008-07-26 23:55:29399}
400
[email protected]d5f942ba2008-09-26 19:30:34401// static
402void TabContents::RegisterUserPrefs(PrefService* prefs) {
[email protected]420ae012009-04-24 05:16:32403 prefs->RegisterBooleanPref(prefs::kAlternateErrorPagesEnabled, true);
404
405 WebPreferences pref_defaults;
406 prefs->RegisterBooleanPref(prefs::kWebKitJavascriptEnabled,
407 pref_defaults.javascript_enabled);
408 prefs->RegisterBooleanPref(prefs::kWebKitWebSecurityEnabled,
409 pref_defaults.web_security_enabled);
410 prefs->RegisterBooleanPref(
411 prefs::kWebKitJavascriptCanOpenWindowsAutomatically, true);
412 prefs->RegisterBooleanPref(prefs::kWebKitLoadsImagesAutomatically,
413 pref_defaults.loads_images_automatically);
414 prefs->RegisterBooleanPref(prefs::kWebKitPluginsEnabled,
415 pref_defaults.plugins_enabled);
416 prefs->RegisterBooleanPref(prefs::kWebKitDomPasteEnabled,
417 pref_defaults.dom_paste_enabled);
418 prefs->RegisterBooleanPref(prefs::kWebKitShrinksStandaloneImagesToFit,
419 pref_defaults.shrinks_standalone_images_to_fit);
[email protected]0df30122009-06-03 12:13:08420 prefs->RegisterStringPref(prefs::kWebKitInspectorSettings,
[email protected]8922e1f2009-10-03 05:01:26421 UTF8ToWide(pref_defaults.inspector_settings));
[email protected]420ae012009-04-24 05:16:32422 prefs->RegisterBooleanPref(prefs::kWebKitTextAreasAreResizable,
423 pref_defaults.text_areas_are_resizable);
424 prefs->RegisterBooleanPref(prefs::kWebKitJavaEnabled,
425 pref_defaults.java_enabled);
426
427 prefs->RegisterLocalizedStringPref(prefs::kAcceptLanguages,
428 IDS_ACCEPT_LANGUAGES);
429 prefs->RegisterLocalizedStringPref(prefs::kDefaultCharset,
430 IDS_DEFAULT_ENCODING);
431 prefs->RegisterLocalizedBooleanPref(prefs::kWebKitStandardFontIsSerif,
432 IDS_STANDARD_FONT_IS_SERIF);
433 prefs->RegisterLocalizedStringPref(prefs::kWebKitFixedFontFamily,
434 IDS_FIXED_FONT_FAMILY);
435 prefs->RegisterLocalizedStringPref(prefs::kWebKitSerifFontFamily,
436 IDS_SERIF_FONT_FAMILY);
437 prefs->RegisterLocalizedStringPref(prefs::kWebKitSansSerifFontFamily,
438 IDS_SANS_SERIF_FONT_FAMILY);
439 prefs->RegisterLocalizedStringPref(prefs::kWebKitCursiveFontFamily,
440 IDS_CURSIVE_FONT_FAMILY);
441 prefs->RegisterLocalizedStringPref(prefs::kWebKitFantasyFontFamily,
442 IDS_FANTASY_FONT_FAMILY);
443 prefs->RegisterLocalizedIntegerPref(prefs::kWebKitDefaultFontSize,
444 IDS_DEFAULT_FONT_SIZE);
445 prefs->RegisterLocalizedIntegerPref(prefs::kWebKitDefaultFixedFontSize,
446 IDS_DEFAULT_FIXED_FONT_SIZE);
447 prefs->RegisterLocalizedIntegerPref(prefs::kWebKitMinimumFontSize,
448 IDS_MINIMUM_FONT_SIZE);
449 prefs->RegisterLocalizedIntegerPref(prefs::kWebKitMinimumLogicalFontSize,
450 IDS_MINIMUM_LOGICAL_FONT_SIZE);
451 prefs->RegisterLocalizedBooleanPref(prefs::kWebKitUsesUniversalDetector,
452 IDS_USES_UNIVERSAL_DETECTOR);
453 prefs->RegisterLocalizedStringPref(prefs::kStaticEncodings,
454 IDS_STATIC_ENCODING_LIST);
initial.commit09911bf2008-07-26 23:55:29455}
456
[email protected]13367f72009-07-08 16:39:02457// Returns true if contains content rendered by an extension.
458bool TabContents::HostsExtension() const {
459 return GetURL().SchemeIs(chrome::kExtensionScheme);
[email protected]d5f942ba2008-09-26 19:30:34460}
461
[email protected]6e95706d2009-04-23 22:55:01462PasswordManager* TabContents::GetPasswordManager() {
463 if (password_manager_.get() == NULL)
464 password_manager_.reset(new PasswordManager(this));
465 return password_manager_.get();
466}
467
468PluginInstaller* TabContents::GetPluginInstaller() {
469 if (plugin_installer_.get() == NULL)
470 plugin_installer_.reset(new PluginInstaller(this));
471 return plugin_installer_.get();
472}
473
[email protected]d5f942ba2008-09-26 19:30:34474const GURL& TabContents::GetURL() const {
475 // We may not have a navigation entry yet
[email protected]ce3fa3c2009-04-20 19:55:57476 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]ebe89e062009-08-13 23:16:54477 return entry ? entry->virtual_url() : GURL::EmptyGURL();
[email protected]d5f942ba2008-09-26 19:30:34478}
479
[email protected]96d185d2009-04-24 03:28:54480const string16& TabContents::GetTitle() const {
[email protected]4c6092c5b2009-06-06 00:23:55481 // Transient entries take precedence. They are used for interstitial pages
482 // that are shown on top of existing pages.
483 NavigationEntry* entry = controller_.GetTransientEntry();
484 if (entry)
485 return entry->GetTitleForDisplay(&controller_);
486
[email protected]96d185d2009-04-24 03:28:54487 DOMUI* our_dom_ui = render_manager_.pending_dom_ui() ?
488 render_manager_.pending_dom_ui() : render_manager_.dom_ui();
489 if (our_dom_ui) {
490 // Don't override the title in view source mode.
[email protected]4c6092c5b2009-06-06 00:23:55491 entry = controller_.GetActiveEntry();
[email protected]96d185d2009-04-24 03:28:54492 if (!(entry && entry->IsViewSourceMode())) {
493 // Give the DOM UI the chance to override our title.
494 const string16& title = our_dom_ui->overridden_title();
495 if (!title.empty())
496 return title;
497 }
498 }
499
500 // We use the title for the last committed entry rather than a pending
501 // navigation entry. For example, when the user types in a URL, we want to
502 // keep the old page's title until the new load has committed and we get a new
503 // title.
[email protected]96d185d2009-04-24 03:28:54504 entry = controller_.GetLastCommittedEntry();
505 if (entry)
506 return entry->GetTitleForDisplay(&controller_);
[email protected]96d185d2009-04-24 03:28:54507 return EmptyString16();
508}
509
[email protected]d5f942ba2008-09-26 19:30:34510int32 TabContents::GetMaxPageID() {
511 if (GetSiteInstance())
512 return GetSiteInstance()->max_page_id();
513 else
514 return max_page_id_;
515}
516
517void TabContents::UpdateMaxPageID(int32 page_id) {
518 // Ensure both the SiteInstance and RenderProcessHost update their max page
[email protected]57c6a652009-05-04 07:58:34519 // IDs in sync. Only TabContents will also have site instances, except during
[email protected]d5f942ba2008-09-26 19:30:34520 // testing.
521 if (GetSiteInstance())
522 GetSiteInstance()->UpdateMaxPageID(page_id);
[email protected]57c6a652009-05-04 07:58:34523 process()->UpdateMaxPageID(page_id);
[email protected]d5f942ba2008-09-26 19:30:34524}
525
[email protected]96d185d2009-04-24 03:28:54526SiteInstance* TabContents::GetSiteInstance() const {
527 return render_manager_.current_host()->site_instance();
528}
529
[email protected]d5f942ba2008-09-26 19:30:34530const std::wstring TabContents::GetDefaultTitle() const {
531 return l10n_util::GetString(IDS_DEFAULT_TAB_TITLE);
532}
533
[email protected]96d185d2009-04-24 03:28:54534bool TabContents::ShouldDisplayURL() {
535 // Don't hide the url in view source mode.
536 NavigationEntry* entry = controller_.GetActiveEntry();
537 if (entry && entry->IsViewSourceMode())
538 return true;
[email protected]83e3895b2009-06-11 00:07:16539
[email protected]96d185d2009-04-24 03:28:54540 DOMUI* dom_ui = GetDOMUIForCurrentState();
541 if (dom_ui)
542 return !dom_ui->should_hide_url();
543 return true;
544}
545
[email protected]d5f942ba2008-09-26 19:30:34546SkBitmap TabContents::GetFavIcon() const {
547 // Like GetTitle(), we also want to use the favicon for the last committed
548 // entry rather than a pending navigation entry.
[email protected]ce3fa3c2009-04-20 19:55:57549 NavigationEntry* entry = controller_.GetTransientEntry();
[email protected]cbab76d2008-10-13 22:42:47550 if (entry)
551 return entry->favicon().bitmap();
552
[email protected]ce3fa3c2009-04-20 19:55:57553 entry = controller_.GetLastCommittedEntry();
[email protected]d5f942ba2008-09-26 19:30:34554 if (entry)
555 return entry->favicon().bitmap();
[email protected]d5f942ba2008-09-26 19:30:34556 return SkBitmap();
557}
558
[email protected]0b303cc2009-09-28 22:35:15559bool TabContents::FavIconIsValid() const {
560 NavigationEntry* entry = controller_.GetTransientEntry();
561 if (entry)
562 return entry->favicon().is_valid();
563
564 entry = controller_.GetLastCommittedEntry();
565 if (entry)
566 return entry->favicon().is_valid();
567
568 return false;
569}
570
[email protected]96d185d2009-04-24 03:28:54571bool TabContents::ShouldDisplayFavIcon() {
572 // Always display a throbber during pending loads.
573 if (controller_.GetLastCommittedEntry() && controller_.pending_entry())
574 return true;
575
576 DOMUI* dom_ui = GetDOMUIForCurrentState();
577 if (dom_ui)
578 return !dom_ui->hide_favicon();
579 return true;
580}
581
[email protected]96d185d2009-04-24 03:28:54582std::wstring TabContents::GetStatusText() const {
583 if (!is_loading() || load_state_ == net::LOAD_STATE_IDLE)
584 return std::wstring();
585
586 switch (load_state_) {
587 case net::LOAD_STATE_WAITING_FOR_CACHE:
588 return l10n_util::GetString(IDS_LOAD_STATE_WAITING_FOR_CACHE);
589 case net::LOAD_STATE_RESOLVING_PROXY_FOR_URL:
590 return l10n_util::GetString(IDS_LOAD_STATE_RESOLVING_PROXY_FOR_URL);
591 case net::LOAD_STATE_RESOLVING_HOST:
592 return l10n_util::GetString(IDS_LOAD_STATE_RESOLVING_HOST);
593 case net::LOAD_STATE_CONNECTING:
594 return l10n_util::GetString(IDS_LOAD_STATE_CONNECTING);
595 case net::LOAD_STATE_SENDING_REQUEST:
[email protected]094e5b22009-09-25 04:23:56596 if (upload_size_)
597 return l10n_util::GetStringF(
598 IDS_LOAD_STATE_SENDING_REQUEST_WITH_PROGRESS,
599 static_cast<int>((100 * upload_position_) / upload_size_));
600 else
601 return l10n_util::GetString(IDS_LOAD_STATE_SENDING_REQUEST);
[email protected]96d185d2009-04-24 03:28:54602 case net::LOAD_STATE_WAITING_FOR_RESPONSE:
603 return l10n_util::GetStringF(IDS_LOAD_STATE_WAITING_FOR_RESPONSE,
604 load_state_host_);
605 // Ignore net::LOAD_STATE_READING_RESPONSE and net::LOAD_STATE_IDLE
606 case net::LOAD_STATE_IDLE:
607 case net::LOAD_STATE_READING_RESPONSE:
608 break;
609 }
610
611 return std::wstring();
612}
613
[email protected]d5f942ba2008-09-26 19:30:34614void TabContents::SetIsCrashed(bool state) {
615 if (state == is_crashed_)
616 return;
617
618 is_crashed_ = state;
[email protected]c9cd2222009-05-06 05:16:50619 NotifyNavigationStateChanged(INVALIDATE_TAB);
[email protected]d5f942ba2008-09-26 19:30:34620}
621
[email protected]744ef172009-10-16 21:53:46622void TabContents::PageActionStateChanged() {
623 NotifyNavigationStateChanged(TabContents::INVALIDATE_PAGE_ACTIONS);
[email protected]d7eaf572009-07-01 21:57:00624}
[email protected]ce5c4502009-05-06 16:46:11625
[email protected]d5f942ba2008-09-26 19:30:34626void TabContents::NotifyNavigationStateChanged(unsigned changed_flags) {
627 if (delegate_)
628 delegate_->NavigationStateChanged(this, changed_flags);
629}
630
[email protected]96d185d2009-04-24 03:28:54631void TabContents::DidBecomeSelected() {
632 controller_.SetActive(true);
[email protected]789e9152009-08-04 21:59:43633 if (render_widget_host_view()) {
[email protected]96d185d2009-04-24 03:28:54634 render_widget_host_view()->DidBecomeSelected();
[email protected]789e9152009-08-04 21:59:43635#if defined(OS_MACOSX)
636 render_widget_host_view()->SetActive(true);
637#endif
638 }
[email protected]96d185d2009-04-24 03:28:54639
[email protected]76543b92009-08-31 17:27:45640 WebCacheManager::GetInstance()->ObserveActivity(process()->id());
[email protected]96d185d2009-04-24 03:28:54641}
642
643void TabContents::WasHidden() {
644 if (!capturing_contents()) {
645 // |render_view_host()| can be NULL if the user middle clicks a link to open
646 // a tab in then background, then closes the tab before selecting it. This
647 // is because closing the tab calls TabContents::Destroy(), which removes
648 // the |render_view_host()|; then when we actually destroy the window,
649 // OnWindowPosChanged() notices and calls HideContents() (which calls us).
650 if (render_widget_host_view())
651 render_widget_host_view()->WasHidden();
[email protected]96d185d2009-04-24 03:28:54652 }
653
654 NotificationService::current()->Notify(
655 NotificationType::TAB_CONTENTS_HIDDEN,
656 Source<TabContents>(this),
657 NotificationService::NoDetails());
658}
659
[email protected]d5f942ba2008-09-26 19:30:34660void TabContents::Activate() {
661 if (delegate_)
662 delegate_->ActivateContents(this);
663}
664
[email protected]96d185d2009-04-24 03:28:54665void TabContents::ShowContents() {
666 if (render_widget_host_view())
667 render_widget_host_view()->DidBecomeSelected();
[email protected]96d185d2009-04-24 03:28:54668}
669
670void TabContents::HideContents() {
671 // TODO(pkasting): http://b/1239839 Right now we purposefully don't call
672 // our superclass HideContents(), because some callers want to be very picky
673 // about the order in which these get called. In addition to making the code
674 // here practically impossible to understand, this also means we end up
675 // calling TabContents::WasHidden() twice if callers call both versions of
[email protected]57c6a652009-05-04 07:58:34676 // HideContents() on a TabContents.
[email protected]96d185d2009-04-24 03:28:54677 WasHidden();
678}
679
[email protected]c0588052008-10-27 23:01:50680void TabContents::OpenURL(const GURL& url, const GURL& referrer,
[email protected]d5f942ba2008-09-26 19:30:34681 WindowOpenDisposition disposition,
682 PageTransition::Type transition) {
683 if (delegate_)
[email protected]c0588052008-10-27 23:01:50684 delegate_->OpenURLFromTab(this, url, referrer, disposition, transition);
[email protected]d5f942ba2008-09-26 19:30:34685}
686
687bool TabContents::NavigateToPendingEntry(bool reload) {
[email protected]96d185d2009-04-24 03:28:54688 const NavigationEntry& entry = *controller_.pending_entry();
689
690 RenderViewHost* dest_render_view_host = render_manager_.Navigate(entry);
691 if (!dest_render_view_host)
692 return false; // Unable to create the desired render view host.
693
694 // Tell DevTools agent that it is attached prior to the navigation.
[email protected]d9f9b792009-06-24 13:17:12695 DevToolsManager* devtools_manager = DevToolsManager::GetInstance();
[email protected]7e8e6b62009-05-08 11:28:32696 if (devtools_manager) { // NULL in unit tests.
[email protected]684b4ba22009-05-07 06:27:41697 devtools_manager->OnNavigatingToPendingEntry(
698 render_view_host(),
699 dest_render_view_host,
700 controller_.pending_entry()->url());
[email protected]7e8e6b62009-05-08 11:28:32701 }
[email protected]96d185d2009-04-24 03:28:54702
703 // Used for page load time metrics.
704 current_load_start_ = base::TimeTicks::Now();
705
706 // Navigate in the desired RenderViewHost.
[email protected]056de2d2009-06-26 16:41:34707 ViewMsg_Navigate_Params navigate_params;
[email protected]5e369672009-11-03 23:48:30708 MakeNavigateParams(profile(), entry, reload, &navigate_params);
[email protected]056de2d2009-06-26 16:41:34709 dest_render_view_host->Navigate(navigate_params);
[email protected]96d185d2009-04-24 03:28:54710
711 if (entry.page_id() == -1) {
712 // HACK!! This code suppresses javascript: URLs from being added to
713 // session history, which is what we want to do for javascript: URLs that
714 // do not generate content. What we really need is a message from the
715 // renderer telling us that a new page was not created. The same message
716 // could be used for mailto: URLs and the like.
717 if (entry.url().SchemeIs(chrome::kJavaScriptScheme))
718 return false;
719 }
720
721 // Clear any provisional password saves - this stops password infobars
722 // showing up on pages the user navigates to while the right page is
723 // loading.
724 GetPasswordManager()->ClearProvisionalSave();
725
726 if (reload && !profile()->IsOffTheRecord()) {
[email protected]0189bc722009-08-28 21:56:48727 FaviconService* favicon_service =
728 profile()->GetFaviconService(Profile::IMPLICIT_ACCESS);
729 if (favicon_service)
730 favicon_service->SetFaviconOutOfDateForPage(entry.url());
[email protected]96d185d2009-04-24 03:28:54731 }
732
[email protected]d5f942ba2008-09-26 19:30:34733 return true;
734}
735
[email protected]96d185d2009-04-24 03:28:54736void TabContents::Stop() {
737 render_manager_.Stop();
738 printing_.Stop();
739}
740
741void TabContents::Cut() {
742 render_view_host()->Cut();
743}
744
745void TabContents::Copy() {
746 render_view_host()->Copy();
747}
748
[email protected]c0cc3092009-09-12 08:27:27749#if defined(OS_MACOSX)
[email protected]a954bf72009-09-12 07:30:35750void TabContents::CopyToFindPboard() {
751 render_view_host()->CopyToFindPboard();
752}
[email protected]c0cc3092009-09-12 08:27:27753#endif
[email protected]a954bf72009-09-12 07:30:35754
[email protected]96d185d2009-04-24 03:28:54755void TabContents::Paste() {
756 render_view_host()->Paste();
757}
758
759void TabContents::DisassociateFromPopupCount() {
760 render_view_host()->DisassociateFromPopupCount();
761}
762
763TabContents* TabContents::Clone() {
764 // We create a new SiteInstance so that the new tab won't share processes
765 // with the old one. This can be changed in the future if we need it to share
766 // processes for some reason.
[email protected]420ae012009-04-24 05:16:32767 TabContents* tc = new TabContents(profile(),
[email protected]96d185d2009-04-24 03:28:54768 SiteInstance::CreateSiteInstance(profile()),
[email protected]12636df2009-09-28 22:32:21769 MSG_ROUTING_NONE, this);
[email protected]96d185d2009-04-24 03:28:54770 tc->controller().CopyStateFrom(controller_);
771 return tc;
772}
773
[email protected]420ae012009-04-24 05:16:32774void TabContents::CreateShortcut() {
775 NavigationEntry* entry = controller_.GetLastCommittedEntry();
776 if (!entry)
777 return;
778
[email protected]70b8d6b2009-08-20 16:56:24779#if defined(OS_LINUX) && !defined(TOOLKIT_VIEWS)
[email protected]0b303cc2009-09-28 22:35:15780 SkBitmap bitmap;
781 if (FavIconIsValid())
782 bitmap = GetFavIcon();
[email protected]0ba5c5e2009-08-20 16:47:13783 CreateApplicationShortcutsDialogGtk::Show(view()->GetTopLevelNativeWindow(),
[email protected]0b303cc2009-09-28 22:35:15784 GetURL(), GetTitle(), bitmap);
[email protected]b96aa932009-08-12 21:34:49785#else
[email protected]420ae012009-04-24 05:16:32786 // We only allow one pending install request. By resetting the page id we
787 // effectively cancel the pending install request.
788 pending_install_.page_id = entry->page_id();
789 pending_install_.icon = GetFavIcon();
[email protected]00e3f152009-10-20 19:25:42790 pending_install_.title = GetTitle();
[email protected]420ae012009-04-24 05:16:32791 pending_install_.url = GetURL();
792 if (pending_install_.callback_functor) {
793 pending_install_.callback_functor->Cancel();
794 pending_install_.callback_functor = NULL;
795 }
796 DCHECK(!pending_install_.icon.isNull()) << "Menu item should be disabled.";
797 if (pending_install_.title.empty())
[email protected]00e3f152009-10-20 19:25:42798 pending_install_.title = UTF8ToUTF16(GetURL().spec());
[email protected]420ae012009-04-24 05:16:32799
800 // Request the application info. When done OnDidGetApplicationInfo is invoked
801 // and we'll create the shortcut.
802 render_view_host()->GetApplicationInfo(pending_install_.page_id);
[email protected]b96aa932009-08-12 21:34:49803#endif
[email protected]420ae012009-04-24 05:16:32804}
805
[email protected]4d677202009-07-19 07:37:12806void TabContents::ShowPageInfo(const GURL& url,
807 const NavigationEntry::SSLStatus& ssl,
808 bool show_history) {
809 if (!delegate_)
810 return;
811
[email protected]bb678332009-07-21 00:15:50812 delegate_->ShowPageInfo(profile(), url, ssl, show_history);
[email protected]4d677202009-07-19 07:37:12813}
814
initial.commit09911bf2008-07-26 23:55:29815ConstrainedWindow* TabContents::CreateConstrainedDialog(
[email protected]e8382172009-06-19 22:16:28816 ConstrainedWindowDelegate* delegate) {
initial.commit09911bf2008-07-26 23:55:29817 ConstrainedWindow* window =
[email protected]e8382172009-06-19 22:16:28818 ConstrainedWindow::CreateConstrainedDialog(this, delegate);
initial.commit09911bf2008-07-26 23:55:29819 child_windows_.push_back(window);
820 return window;
821}
822
823void TabContents::AddNewContents(TabContents* new_contents,
824 WindowOpenDisposition disposition,
825 const gfx::Rect& initial_pos,
[email protected]c88a70fe2009-05-05 20:00:22826 bool user_gesture,
827 const GURL& creator_url) {
initial.commit09911bf2008-07-26 23:55:29828 if (!delegate_)
829 return;
830
[email protected]35f7d212009-04-29 21:19:27831 if ((disposition == NEW_POPUP) && !user_gesture &&
832 !CommandLine::ForCurrentProcess()->HasSwitch(
833 switches::kDisablePopupBlocking)) {
[email protected]2c4410d2009-05-06 23:46:22834 // Unrequested popups from normal pages are constrained unless they're in
[email protected]b6c874582009-05-08 19:38:31835 // the whitelist. The popup owner will handle checking this.
836 delegate_->GetConstrainingContents(this)->AddPopup(new_contents,
837 initial_pos,
838 creator_url.is_valid() ? creator_url.host() : std::string());
839 } else {
[email protected]304f8792009-08-19 21:28:09840#if defined(OS_CHROMEOS)
841 if (disposition == NEW_POPUP) {
842 // If the popup is bigger than a given factor of the screen, then
843 // turn it into a foreground tab (on chrome os only)
844 GdkScreen* screen = gdk_screen_get_default();
845 int max_width = gdk_screen_get_width(screen) * kMaxWidthFactor;
846 int max_height = gdk_screen_get_height(screen) * kMaxHeightFactor;
847 if (initial_pos.width() > max_width ||
848 initial_pos.height() > max_height) {
849 disposition = NEW_FOREGROUND_TAB;
850 }
851 }
852#endif
853
[email protected]0aa55312008-10-17 21:53:08854 new_contents->DisassociateFromPopupCount();
initial.commit09911bf2008-07-26 23:55:29855 delegate_->AddNewContents(this, new_contents, disposition, initial_pos,
856 user_gesture);
[email protected]7f37dd92009-09-15 16:35:49857 NotificationService::current()->Notify(
858 NotificationType::TAB_ADDED,
859 Source<TabContentsDelegate>(delegate_),
860 Details<TabContents>(this));
initial.commit09911bf2008-07-26 23:55:29861 }
[email protected]b6c874582009-05-08 19:38:31862 PopupNotificationVisibilityChanged(ShowingBlockedPopupNotification());
initial.commit09911bf2008-07-26 23:55:29863}
864
initial.commit09911bf2008-07-26 23:55:29865void TabContents::CloseAllSuppressedPopups() {
[email protected]d6598c052008-11-05 19:03:25866 if (blocked_popups_)
[email protected]2c4410d2009-05-06 23:46:22867 blocked_popups_->CloseAll();
initial.commit09911bf2008-07-26 23:55:29868}
869
[email protected]912256b32009-09-18 09:47:35870void TabContents::ExecuteCode(int request_id, const std::string& extension_id,
871 bool is_js_code, const std::string& code_string) {
872 render_view_host()->Send(
873 new ViewMsg_ExecuteCode(render_view_host()->routing_id(), request_id,
874 extension_id, is_js_code, code_string));
875}
876
[email protected]96d185d2009-04-24 03:28:54877void TabContents::PopupNotificationVisibilityChanged(bool visible) {
878 render_view_host()->PopupNotificationVisibilityChanged(visible);
879}
880
[email protected]5c9e97a2009-09-09 23:48:30881gfx::NativeView TabContents::GetContentNativeView() const {
[email protected]96d185d2009-04-24 03:28:54882 return view_->GetContentNativeView();
883}
884
885gfx::NativeView TabContents::GetNativeView() const {
886 return view_->GetNativeView();
887}
888
889void TabContents::GetContainerBounds(gfx::Rect *out) const {
890 view_->GetContainerBounds(out);
891}
892
893void TabContents::Focus() {
894 view_->Focus();
895}
896
[email protected]90daadb42009-06-08 21:27:28897void TabContents::FocusThroughTabTraversal(bool reverse) {
[email protected]7e383692009-06-12 19:14:54898 if (showing_interstitial_page()) {
899 render_manager_.interstitial_page()->FocusThroughTabTraversal(reverse);
900 return;
901 }
[email protected]96d185d2009-04-24 03:28:54902 render_view_host()->SetInitialFocus(reverse);
903}
904
905bool TabContents::FocusLocationBarByDefault() {
906 DOMUI* dom_ui = GetDOMUIForCurrentState();
907 if (dom_ui)
908 return dom_ui->focus_location_bar_by_default();
[email protected]3e3f0eb2009-06-22 18:33:43909 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]e0d481582009-09-15 21:06:25910 if (entry && entry->url() == GURL(chrome::kAboutBlankURL))
[email protected]3e3f0eb2009-06-22 18:33:43911 return true;
[email protected]96d185d2009-04-24 03:28:54912 return false;
913}
914
[email protected]616ed5a2008-11-21 22:27:24915void TabContents::AddInfoBar(InfoBarDelegate* delegate) {
916 // Look through the existing InfoBarDelegates we have for a match. If we've
917 // already got one that matches, then we don't add the new one.
[email protected]f86a07022008-11-25 01:06:05918 for (int i = 0; i < infobar_delegate_count(); ++i) {
[email protected]616ed5a2008-11-21 22:27:24919 if (GetInfoBarDelegateAt(i)->EqualsDelegate(delegate))
920 return;
921 }
922
923 infobar_delegates_.push_back(delegate);
[email protected]bfd04a62009-02-01 18:16:56924 NotificationService::current()->Notify(
925 NotificationType::TAB_CONTENTS_INFOBAR_ADDED,
926 Source<TabContents>(this),
927 Details<InfoBarDelegate>(delegate));
[email protected]616ed5a2008-11-21 22:27:24928
929 // Add ourselves as an observer for navigations the first time a delegate is
930 // added. We use this notification to expire InfoBars that need to expire on
931 // page transitions.
932 if (infobar_delegates_.size() == 1) {
[email protected]bfd04a62009-02-01 18:16:56933 registrar_.Add(this, NotificationType::NAV_ENTRY_COMMITTED,
[email protected]ce3fa3c2009-04-20 19:55:57934 Source<NavigationController>(&controller_));
[email protected]616ed5a2008-11-21 22:27:24935 }
936}
937
938void TabContents::RemoveInfoBar(InfoBarDelegate* delegate) {
939 std::vector<InfoBarDelegate*>::iterator it =
940 find(infobar_delegates_.begin(), infobar_delegates_.end(), delegate);
941 if (it != infobar_delegates_.end()) {
942 InfoBarDelegate* delegate = *it;
[email protected]bfd04a62009-02-01 18:16:56943 NotificationService::current()->Notify(
944 NotificationType::TAB_CONTENTS_INFOBAR_REMOVED,
945 Source<TabContents>(this),
946 Details<InfoBarDelegate>(delegate));
[email protected]f86a07022008-11-25 01:06:05947 infobar_delegates_.erase(it);
[email protected]616ed5a2008-11-21 22:27:24948
[email protected]6a02963e2009-01-06 16:58:03949 // Remove ourselves as an observer if we are tracking no more InfoBars.
950 if (infobar_delegates_.empty()) {
[email protected]bfd04a62009-02-01 18:16:56951 registrar_.Remove(this, NotificationType::NAV_ENTRY_COMMITTED,
[email protected]ce3fa3c2009-04-20 19:55:57952 Source<NavigationController>(&controller_));
[email protected]6a02963e2009-01-06 16:58:03953 }
[email protected]616ed5a2008-11-21 22:27:24954 }
955}
[email protected]3edd9522009-03-04 22:19:41956
[email protected]f81821c2009-08-07 01:29:47957void TabContents::ReplaceInfoBar(InfoBarDelegate* old_delegate,
958 InfoBarDelegate* new_delegate) {
959 std::vector<InfoBarDelegate*>::iterator it =
960 find(infobar_delegates_.begin(), infobar_delegates_.end(), old_delegate);
961 DCHECK(it != infobar_delegates_.end());
962
963 // Notify the container about the change of plans.
964 scoped_ptr<std::pair<InfoBarDelegate*, InfoBarDelegate*> > details(
965 new std::pair<InfoBarDelegate*, InfoBarDelegate*>(
966 old_delegate, new_delegate));
967 NotificationService::current()->Notify(
968 NotificationType::TAB_CONTENTS_INFOBAR_REPLACED,
969 Source<TabContents>(this),
970 Details<std::pair<InfoBarDelegate*, InfoBarDelegate*> >(details.get()));
971
972 // Remove the old one.
973 infobar_delegates_.erase(it);
974
975 // Add the new one.
976 infobar_delegates_.push_back(new_delegate);
977}
978
[email protected]20ede072009-10-06 00:28:35979bool TabContents::ShouldShowBookmarkBar() {
[email protected]aa9196f2009-10-22 16:20:27980 if (showing_interstitial_page())
981 return false;
982
[email protected]96d185d2009-04-24 03:28:54983 // See GetDOMUIForCurrentState() comment for more info. This case is very
984 // similar, but for non-first loads, we want to use the committed entry. This
985 // is so the bookmarks bar disappears at the same time the page does.
986 if (controller_.GetLastCommittedEntry()) {
987 // Not the first load, always use the committed DOM UI.
988 if (render_manager_.dom_ui())
989 return render_manager_.dom_ui()->force_bookmark_bar_visible();
990 return false; // Default.
991 }
992
993 // When it's the first load, we know either the pending one or the committed
994 // one will have the DOM UI in it (see GetDOMUIForCurrentState), and only one
995 // of them will be valid, so we can just check both.
996 if (render_manager_.pending_dom_ui())
997 return render_manager_.pending_dom_ui()->force_bookmark_bar_visible();
998 if (render_manager_.dom_ui())
999 return render_manager_.dom_ui()->force_bookmark_bar_visible();
1000 return false; // Default.
1001}
1002
[email protected]d8f16ae2009-08-30 03:47:261003bool TabContents::IsExtensionShelfAlwaysVisible() {
1004 // See GetDOMUIForCurrentState() comment for more info. This case is very
1005 // similar, but for non-first loads, we want to use the committed entry. This
1006 // is so the bookmarks bar disappears at the same time the page does.
1007 if (controller_.GetLastCommittedEntry()) {
1008 // Not the first load, always use the committed DOM UI.
1009 if (render_manager_.dom_ui())
1010 return render_manager_.dom_ui()->force_extension_shelf_visible();
1011 return false; // Default.
1012 }
1013
1014 // When it's the first load, we know either the pending one or the committed
1015 // one will have the DOM UI in it (see GetDOMUIForCurrentState), and only one
1016 // of them will be valid, so we can just check both.
1017 if (render_manager_.pending_dom_ui())
1018 return render_manager_.pending_dom_ui()->force_extension_shelf_visible();
1019 if (render_manager_.dom_ui())
1020 return render_manager_.dom_ui()->force_extension_shelf_visible();
1021 return false; // Default.
1022}
1023
[email protected]3edd9522009-03-04 22:19:411024void TabContents::ToolbarSizeChanged(bool is_animating) {
1025 TabContentsDelegate* d = delegate();
1026 if (d)
1027 d->ToolbarSizeChanged(this, is_animating);
1028}
[email protected]616ed5a2008-11-21 22:27:241029
[email protected]d5f942ba2008-09-26 19:30:341030void TabContents::OnStartDownload(DownloadItem* download) {
1031 DCHECK(download);
[email protected]d5f942ba2008-09-26 19:30:341032
1033 // Download in a constrained popup is shown in the tab that opened it.
[email protected]b6c874582009-05-08 19:38:311034 TabContents* tab_contents = delegate()->GetConstrainingContents(this);
[email protected]d5f942ba2008-09-26 19:30:341035
[email protected]59560e0b2009-06-04 03:30:221036 if (tab_contents && tab_contents->delegate())
1037 tab_contents->delegate()->OnStartDownload(download);
[email protected]3edd9522009-03-04 22:19:411038}
[email protected]3edd9522009-03-04 22:19:411039
[email protected]d5f942ba2008-09-26 19:30:341040void TabContents::WillClose(ConstrainedWindow* window) {
1041 ConstrainedWindowList::iterator it =
1042 find(child_windows_.begin(), child_windows_.end(), window);
1043 if (it != child_windows_.end())
1044 child_windows_.erase(it);
[email protected]66ba4932009-06-04 19:22:131045}
[email protected]d5f942ba2008-09-26 19:30:341046
[email protected]66ba4932009-06-04 19:22:131047void TabContents::WillCloseBlockedPopupContainer(
1048 BlockedPopupContainer* container) {
1049 DCHECK(blocked_popups_ == container);
1050 blocked_popups_ = NULL;
[email protected]d5f942ba2008-09-26 19:30:341051}
1052
[email protected]d5f942ba2008-09-26 19:30:341053void TabContents::DidMoveOrResize(ConstrainedWindow* window) {
[email protected]332af7732009-03-11 13:21:351054#if defined(OS_WIN)
[email protected]92edc472009-02-10 20:32:061055 UpdateWindow(GetNativeView());
[email protected]332af7732009-03-11 13:21:351056#endif
[email protected]d5f942ba2008-09-26 19:30:341057}
1058
[email protected]e491f1c2009-05-22 20:28:121059void TabContents::StartFinding(string16 find_text,
1060 bool forward_direction,
1061 bool case_sensitive) {
[email protected]6e95706d2009-04-23 22:55:011062 // If find_text is empty, it means FindNext was pressed with a keyboard
1063 // shortcut so unless we have something to search for we return early.
[email protected]e491f1c2009-05-22 20:28:121064 if (find_text.empty() && find_text_.empty()) {
1065 if (last_search_prepopulate_text_->empty())
1066 return;
1067 // Try whatever we searched for last in any tab.
1068 find_text = *last_search_prepopulate_text_;
1069 }
[email protected]6e95706d2009-04-23 22:55:011070
[email protected]101c90e2009-10-26 21:14:441071 // Keep track of the previous search.
1072 previous_find_text_ = find_text_;
1073
[email protected]6e95706d2009-04-23 22:55:011074 // This is a FindNext operation if we are searching for the same text again,
1075 // or if the passed in search text is empty (FindNext keyboard shortcut). The
1076 // exception to this is if the Find was aborted (then we don't want FindNext
1077 // because the highlighting has been cleared and we need it to reappear). We
1078 // therefore treat FindNext after an aborted Find operation as a full fledged
1079 // Find.
1080 bool find_next = (find_text_ == find_text || find_text.empty()) &&
[email protected]e491f1c2009-05-22 20:28:121081 (last_search_case_sensitive_ == case_sensitive) &&
[email protected]6e95706d2009-04-23 22:55:011082 !find_op_aborted_;
1083 if (!find_next)
1084 current_find_request_id_ = find_request_id_counter_++;
1085
1086 if (!find_text.empty())
1087 find_text_ = find_text;
[email protected]e491f1c2009-05-22 20:28:121088 last_search_case_sensitive_ = case_sensitive;
[email protected]6e95706d2009-04-23 22:55:011089
1090 find_op_aborted_ = false;
1091
1092 // Keep track of what the last search was across the tabs.
[email protected]e491f1c2009-05-22 20:28:121093 *last_search_prepopulate_text_ = find_text;
[email protected]6e95706d2009-04-23 22:55:011094
1095 render_view_host()->StartFinding(current_find_request_id_,
1096 find_text_,
1097 forward_direction,
[email protected]e491f1c2009-05-22 20:28:121098 case_sensitive,
[email protected]6e95706d2009-04-23 22:55:011099 find_next);
1100}
1101
1102void TabContents::StopFinding(bool clear_selection) {
[email protected]2d2dffc2009-06-09 21:41:001103 // When |clear_selection| is true, it means the find string has been cleared
1104 // by the user, but the UI has not been dismissed.
1105 if (!clear_selection)
1106 find_ui_active_ = false;
[email protected]101c90e2009-10-26 21:14:441107 find_text_.clear();
[email protected]6e95706d2009-04-23 22:55:011108 find_op_aborted_ = true;
[email protected]e491f1c2009-05-22 20:28:121109 last_search_result_ = FindNotificationDetails();
[email protected]6e95706d2009-04-23 22:55:011110 render_view_host()->StopFinding(clear_selection);
1111}
1112
[email protected]5c4266922009-07-10 16:41:271113void TabContents::GetPageLanguage() {
1114 render_view_host()->GetPageLanguage();
1115}
1116
[email protected]8710e40c2009-11-05 21:11:571117void TabContents::OnJavaScriptMessageBoxClosed(IPC::Message* reply_msg,
1118 bool success,
1119 const std::wstring& prompt) {
1120 last_javascript_message_dismissal_ = base::TimeTicks::Now();
1121 if (is_showing_before_unload_dialog_ && !success) {
1122 // If a beforeunload dialog is canceled, we need to stop the throbber from
1123 // spinning, since we forced it to start spinning in Navigate.
1124 DidStopLoading();
1125
1126 tab_close_start_time_ = base::TimeTicks();
1127 }
1128 is_showing_before_unload_dialog_ = false;
1129 render_view_host()->JavaScriptMessageBoxClosed(reply_msg, success, prompt);
1130}
1131
[email protected]420ae012009-04-24 05:16:321132void TabContents::OnSavePage() {
1133 // If we can not save the page, try to download it.
1134 if (!SavePackage::IsSavableContents(contents_mime_type())) {
1135 DownloadManager* dlm = profile()->GetDownloadManager();
1136 const GURL& current_page_url = GetURL();
1137 if (dlm && current_page_url.is_valid())
[email protected]d82ed61e2009-06-16 02:46:221138 dlm->DownloadUrl(current_page_url, GURL(), "", this);
[email protected]420ae012009-04-24 05:16:321139 return;
1140 }
1141
1142 Stop();
1143
1144 // Create the save package and possibly prompt the user for the name to save
1145 // the page as. The user prompt is an asynchronous operation that runs on
1146 // another thread.
[email protected]d82ed61e2009-06-16 02:46:221147 save_package_ = new SavePackage(this);
[email protected]420ae012009-04-24 05:16:321148 save_package_->GetSaveInfo();
1149}
1150
1151// Used in automated testing to bypass prompting the user for file names.
1152// Instead, the names and paths are hard coded rather than running them through
1153// file name sanitation and extension / mime checking.
[email protected]828cabe2009-09-26 22:47:111154bool TabContents::SavePage(const FilePath& main_file, const FilePath& dir_path,
[email protected]420ae012009-04-24 05:16:321155 SavePackage::SavePackageType save_type) {
1156 // Stop the page from navigating.
1157 Stop();
1158
[email protected]828cabe2009-09-26 22:47:111159 save_package_ = new SavePackage(this, save_type, main_file, dir_path);
[email protected]cdf6cc82009-08-04 21:48:591160 return save_package_->Init();
[email protected]420ae012009-04-24 05:16:321161}
1162
1163void TabContents::PrintPreview() {
1164 // We don't show the print preview yet, only the print dialog.
1165 PrintNow();
1166}
1167
1168bool TabContents::PrintNow() {
1169 // We can't print interstitial page for now.
1170 if (showing_interstitial_page())
1171 return false;
1172
1173 return render_view_host()->PrintPages();
1174}
1175
[email protected]82270452009-06-19 15:58:011176void TabContents::PrintingDone(int document_cookie, bool success) {
1177 render_view_host()->PrintingDone(document_cookie, success);
1178}
1179
[email protected]420ae012009-04-24 05:16:321180bool TabContents::IsActiveEntry(int32 page_id) {
1181 NavigationEntry* active_entry = controller_.GetActiveEntry();
1182 return (active_entry != NULL &&
1183 active_entry->site_instance() == GetSiteInstance() &&
1184 active_entry->page_id() == page_id);
1185}
1186
[email protected]3f8eb7f2009-10-02 23:58:051187void TabContents::LogNewTabTime(const std::string& event_name) {
1188 // Not all new tab pages get timed. In those cases, we don't have a
1189 // new_tab_start_time_.
1190 if (new_tab_start_time_.is_null())
1191 return;
1192
1193 base::TimeDelta duration = base::TimeTicks::Now() - new_tab_start_time_;
[email protected]84abba62009-10-07 17:01:441194 MetricEventDurationDetails details(event_name,
1195 static_cast<int>(duration.InMilliseconds()));
1196
[email protected]7ab1e7d62009-10-14 23:32:011197 if (event_name == "Tab.NewTabScriptStart") {
1198 UMA_HISTOGRAM_TIMES("Tab.NewTabScriptStart", duration);
1199 } else if (event_name == "Tab.NewTabDOMContentLoaded") {
1200 UMA_HISTOGRAM_TIMES("Tab.NewTabDOMContentLoaded", duration);
1201 } else if (event_name == "Tab.NewTabOnload") {
1202 UMA_HISTOGRAM_TIMES("Tab.NewTabOnload", duration);
[email protected]3f8eb7f2009-10-02 23:58:051203 // The new tab page has finished loading; reset it.
1204 new_tab_start_time_ = base::TimeTicks();
[email protected]84abba62009-10-07 17:01:441205 } else {
1206 NOTREACHED();
[email protected]3f8eb7f2009-10-02 23:58:051207 }
[email protected]84abba62009-10-07 17:01:441208 NotificationService::current()->Notify(
1209 NotificationType::METRIC_EVENT_DURATION,
1210 Source<TabContents>(this),
1211 Details<MetricEventDurationDetails>(&details));
[email protected]3f8eb7f2009-10-02 23:58:051212}
1213
[email protected]7ab1e7d62009-10-14 23:32:011214void TabContents::OnCloseStarted() {
1215 if (tab_close_start_time_.is_null())
1216 tab_close_start_time_ = base::TimeTicks::Now();
1217}
1218
[email protected]96d185d2009-04-24 03:28:541219// Notifies the RenderWidgetHost instance about the fact that the page is
1220// loading, or done loading and calls the base implementation.
1221void TabContents::SetIsLoading(bool is_loading,
1222 LoadNotificationDetails* details) {
1223 if (is_loading == is_loading_)
1224 return;
1225
1226 if (!is_loading) {
1227 load_state_ = net::LOAD_STATE_IDLE;
1228 load_state_host_.clear();
[email protected]094e5b22009-09-25 04:23:561229 upload_size_ = 0;
1230 upload_position_ = 0;
[email protected]96d185d2009-04-24 03:28:541231 }
1232
1233 render_manager_.SetIsLoading(is_loading);
1234
1235 is_loading_ = is_loading;
1236 waiting_for_response_ = is_loading;
1237
1238 if (delegate_)
1239 delegate_->LoadingStateChanged(this);
[email protected]329581b2009-04-28 06:52:351240 NotifyNavigationStateChanged(INVALIDATE_LOAD);
[email protected]96d185d2009-04-24 03:28:541241
1242 NotificationType type = is_loading ? NotificationType::LOAD_START :
1243 NotificationType::LOAD_STOP;
[email protected]ce5c4502009-05-06 16:46:111244 NotificationDetails det = NotificationService::NoDetails();
[email protected]96d185d2009-04-24 03:28:541245 if (details)
1246 det = Details<LoadNotificationDetails>(details);
1247 NotificationService::current()->Notify(type,
1248 Source<NavigationController>(&controller_),
1249 det);
1250}
1251
[email protected]2c4410d2009-05-06 23:46:221252void TabContents::CreateBlockedPopupContainerIfNecessary() {
1253 if (blocked_popups_)
1254 return;
1255
[email protected]4d4c0b92009-06-08 23:13:011256 blocked_popups_ = BlockedPopupContainer::Create(this, profile());
[email protected]2c4410d2009-05-06 23:46:221257}
1258
[email protected]b6c874582009-05-08 19:38:311259void TabContents::AddPopup(TabContents* new_contents,
1260 const gfx::Rect& initial_pos,
1261 const std::string& host) {
[email protected]2c4410d2009-05-06 23:46:221262 CreateBlockedPopupContainerIfNecessary();
1263 blocked_popups_->AddTabContents(new_contents, initial_pos, host);
[email protected]2c4410d2009-05-06 23:46:221264}
1265
[email protected]e69bce3dd2009-04-20 22:05:121266// TODO(brettw) This should be on the TabContentsView.
[email protected]66ba4932009-06-04 19:22:131267void TabContents::RepositionSupressedPopupsToFit() {
[email protected]d6598c052008-11-05 19:03:251268 if (blocked_popups_)
[email protected]4d4c0b92009-06-08 23:13:011269 blocked_popups_->RepositionBlockedPopupContainer();
[email protected]d5f942ba2008-09-26 19:30:341270}
1271
[email protected]b9681312008-11-07 00:08:261272bool TabContents::ShowingBlockedPopupNotification() const {
1273 return blocked_popups_ != NULL &&
[email protected]2c4410d2009-05-06 23:46:221274 blocked_popups_->GetBlockedPopupCount() != 0;
[email protected]b9681312008-11-07 00:08:261275}
[email protected]616ed5a2008-11-21 22:27:241276
1277namespace {
1278bool TransitionIsReload(PageTransition::Type transition) {
1279 return PageTransition::StripQualifier(transition) == PageTransition::RELOAD;
1280}
1281}
1282
1283void TabContents::ExpireInfoBars(
1284 const NavigationController::LoadCommittedDetails& details) {
1285 // Only hide InfoBars when the user has done something that makes the main
1286 // frame load. We don't want various automatic or subframe navigations making
1287 // it disappear.
1288 if (!details.is_user_initiated_main_frame_load())
1289 return;
1290
[email protected]f86a07022008-11-25 01:06:051291 for (int i = infobar_delegate_count() - 1; i >= 0; --i) {
[email protected]616ed5a2008-11-21 22:27:241292 InfoBarDelegate* delegate = GetInfoBarDelegateAt(i);
[email protected]f86a07022008-11-25 01:06:051293 if (delegate->ShouldExpire(details))
[email protected]616ed5a2008-11-21 22:27:241294 RemoveInfoBar(delegate);
[email protected]616ed5a2008-11-21 22:27:241295 }
1296}
[email protected]fdd61c62009-04-22 19:22:571297
1298void TabContents::OnGearsCreateShortcutDone(
1299 const GearsShortcutData2& shortcut_data, bool success) {
1300 NavigationEntry* current_entry = controller_.GetLastCommittedEntry();
1301 bool same_page =
1302 current_entry && pending_install_.page_id == current_entry->page_id();
1303
1304 if (success && same_page) {
1305 // Only switch to app mode if the user chose to create a shortcut and
1306 // we're still on the same page that it corresponded to.
1307 if (delegate())
1308 delegate()->ConvertContentsToApplication(this);
1309 }
1310
1311 // Reset the page id to indicate no requests are pending.
1312 pending_install_.page_id = 0;
1313 pending_install_.callback_functor = NULL;
1314}
[email protected]96d185d2009-04-24 03:28:541315
1316DOMUI* TabContents::GetDOMUIForCurrentState() {
1317 // When there is a pending navigation entry, we want to use the pending DOMUI
1318 // that goes along with it to control the basic flags. For example, we want to
1319 // show the pending URL in the URL bar, so we want the display_url flag to
1320 // be from the pending entry.
1321 //
1322 // The confusion comes because there are multiple possibilities for the
1323 // initial load in a tab as a side effect of the way the RenderViewHostManager
1324 // works.
1325 //
1326 // - For the very first tab the load looks "normal". The new tab DOM UI is
1327 // the pending one, and we want it to apply here.
1328 //
1329 // - For subsequent new tabs, they'll get a new SiteInstance which will then
1330 // get switched to the one previously associated with the new tab pages.
1331 // This switching will cause the manager to commit the RVH/DOMUI. So we'll
1332 // have a committed DOM UI in this case.
1333 //
1334 // This condition handles all of these cases:
1335 //
1336 // - First load in first tab: no committed nav entry + pending nav entry +
1337 // pending dom ui:
1338 // -> Use pending DOM UI if any.
1339 //
1340 // - First load in second tab: no committed nav entry + pending nav entry +
1341 // no pending DOM UI:
1342 // -> Use the committed DOM UI if any.
1343 //
1344 // - Second navigation in any tab: committed nav entry + pending nav entry:
1345 // -> Use pending DOM UI if any.
1346 //
1347 // - Normal state with no load: committed nav entry + no pending nav entry:
1348 // -> Use committed DOM UI.
1349 if (controller_.pending_entry() &&
1350 (controller_.GetLastCommittedEntry() ||
1351 render_manager_.pending_dom_ui()))
1352 return render_manager_.pending_dom_ui();
1353 return render_manager_.dom_ui();
1354}
[email protected]420ae012009-04-24 05:16:321355
1356void TabContents::DidNavigateMainFramePostCommit(
1357 const NavigationController::LoadCommittedDetails& details,
1358 const ViewHostMsg_FrameNavigate_Params& params) {
[email protected]c2e74fe82009-09-03 17:57:441359 if (opener_dom_ui_type_ != DOMUIFactory::kNoDOMUI) {
1360 // If this is a window.open navigation, use the same DOMUI as the renderer
1361 // that opened the window, as long as both renderers have the same
1362 // privileges.
1363 if (opener_dom_ui_type_ == DOMUIFactory::GetDOMUIType(GetURL())) {
1364 DOMUI* dom_ui = DOMUIFactory::CreateDOMUIForURL(this, GetURL());
1365 // dom_ui might be NULL if the URL refers to a non-existent extension.
1366 if (dom_ui) {
1367 render_manager_.SetDOMUIPostCommit(dom_ui);
1368 dom_ui->RenderViewCreated(render_view_host());
1369 }
1370 }
1371 opener_dom_ui_type_ = DOMUIFactory::kNoDOMUI;
1372 }
1373
[email protected]420ae012009-04-24 05:16:321374 if (details.is_user_initiated_main_frame_load()) {
1375 // Clear the status bubble. This is a workaround for a bug where WebKit
1376 // doesn't let us know that the cursor left an element during a
1377 // transition (this is also why the mouse cursor remains as a hand after
1378 // clicking on a link); see bugs 1184641 and 980803. We don't want to
1379 // clear the bubble when a user navigates to a named anchor in the same
1380 // page.
1381 UpdateTargetURL(details.entry->page_id(), GURL());
1382
1383 // UpdateHelpersForDidNavigate will handle the case where the password_form
1384 // origin is valid.
1385 // TODO(brettw) bug 1343111: Password manager stuff in here needs to be
1386 // cleaned up and covered by tests.
1387 if (!params.password_form.origin.is_valid())
1388 GetPasswordManager()->DidNavigate();
1389 }
1390
1391 // The keyword generator uses the navigation entries, so must be called after
1392 // the commit.
1393 GenerateKeywordIfNecessary(params);
1394
1395 // Allow the new page to set the title again.
1396 received_page_title_ = false;
1397
1398 // Get the favicon, either from history or request it from the net.
1399 fav_icon_helper_.FetchFavIcon(details.entry->url());
1400
[email protected]56ce6e52009-10-27 00:10:521401 // Clear all page and browser action state for this tab, unless this is an
1402 // in-page navigation.
[email protected]92c6f9b92009-10-24 04:35:081403 url_canon::Replacements<char> replacements;
1404 replacements.ClearRef();
1405 if (params.url.ReplaceComponents(replacements) !=
1406 params.referrer.ReplaceComponents(replacements)) {
[email protected]92c6f9b92009-10-24 04:35:081407 ExtensionsService* service = profile()->GetExtensionsService();
1408 if (service) {
1409 for (size_t i = 0; i < service->extensions()->size(); ++i) {
[email protected]5d246db22009-10-27 06:17:571410 ExtensionAction* browser_action =
[email protected]92c6f9b92009-10-24 04:35:081411 service->extensions()->at(i)->browser_action();
[email protected]56ce6e52009-10-27 00:10:521412 if (browser_action) {
1413 browser_action->ClearAllValuesForTab(controller().session_id().id());
1414 NotificationService::current()->Notify(
1415 NotificationType::EXTENSION_BROWSER_ACTION_UPDATED,
[email protected]5d246db22009-10-27 06:17:571416 Source<ExtensionAction>(browser_action),
[email protected]56ce6e52009-10-27 00:10:521417 NotificationService::NoDetails());
1418 }
[email protected]92c6f9b92009-10-24 04:35:081419
[email protected]5d246db22009-10-27 06:17:571420 ExtensionAction* page_action =
[email protected]56ce6e52009-10-27 00:10:521421 service->extensions()->at(i)->page_action();
1422 if (page_action) {
1423 page_action->ClearAllValuesForTab(controller().session_id().id());
1424 PageActionStateChanged();
1425 }
[email protected]92c6f9b92009-10-24 04:35:081426 }
[email protected]aece2c7f2009-08-27 20:43:171427 }
1428 }
[email protected]ce5c4502009-05-06 16:46:111429
[email protected]420ae012009-04-24 05:16:321430 // Close constrained popups if necessary.
1431 MaybeCloseChildWindows(details.previous_url, details.entry->url());
1432
1433 // Update the starred state.
1434 UpdateStarredStateForCurrentURL();
1435}
1436
1437void TabContents::DidNavigateAnyFramePostCommit(
1438 RenderViewHost* render_view_host,
1439 const NavigationController::LoadCommittedDetails& details,
1440 const ViewHostMsg_FrameNavigate_Params& params) {
1441 // If we navigate, start showing messages again. This does nothing to prevent
1442 // a malicious script from spamming messages, since the script could just
1443 // reload the page to stop blocking.
1444 suppress_javascript_messages_ = false;
1445
[email protected]420ae012009-04-24 05:16:321446 // Notify the password manager of the navigation or form submit.
1447 // TODO(brettw) bug 1343111: Password manager stuff in here needs to be
1448 // cleaned up and covered by tests.
1449 if (params.password_form.origin.is_valid())
1450 GetPasswordManager()->ProvisionallySavePassword(params.password_form);
1451}
1452
1453void TabContents::MaybeCloseChildWindows(const GURL& previous_url,
1454 const GURL& current_url) {
1455 if (net::RegistryControlledDomainService::SameDomainOrHost(
1456 previous_url, current_url))
1457 return;
1458
1459 // Clear out any child windows since we are leaving this page entirely.
1460 // We use indices instead of iterators in case CloseWindow does something
1461 // that may invalidate an iterator.
1462 int size = static_cast<int>(child_windows_.size());
1463 for (int i = size - 1; i >= 0; --i) {
1464 ConstrainedWindow* window = child_windows_[i];
1465 if (window)
1466 window->CloseConstrainedWindow();
1467 }
[email protected]e29300cd2009-07-23 21:14:591468
1469 // Close the popup container.
1470 if (blocked_popups_) {
1471 blocked_popups_->Destroy();
1472 blocked_popups_ = NULL;
1473 }
[email protected]420ae012009-04-24 05:16:321474}
1475
1476void TabContents::UpdateStarredStateForCurrentURL() {
1477 BookmarkModel* model = profile()->GetBookmarkModel();
1478 const bool old_state = is_starred_;
1479 is_starred_ = (model && model->IsBookmarked(GetURL()));
1480
1481 if (is_starred_ != old_state && delegate())
1482 delegate()->URLStarredChanged(this, is_starred_);
1483}
1484
1485void TabContents::UpdateAlternateErrorPageURL() {
1486 GURL url = GetAlternateErrorPageURL();
1487 render_view_host()->SetAlternateErrorPageURL(url);
1488}
1489
1490void TabContents::UpdateWebPreferences() {
1491 render_view_host()->UpdateWebPreferences(GetWebkitPrefs());
1492}
1493
1494void TabContents::UpdateMaxPageIDIfNecessary(SiteInstance* site_instance,
1495 RenderViewHost* rvh) {
1496 // If we are creating a RVH for a restored controller, then we might
1497 // have more page IDs than the SiteInstance's current max page ID. We must
1498 // make sure that the max page ID is larger than any restored page ID.
1499 // Note that it is ok for conflicting page IDs to exist in another tab
1500 // (i.e., NavigationController), but if any page ID is larger than the max,
1501 // the back/forward list will get confused.
1502 int max_restored_page_id = controller_.max_restored_page_id();
1503 if (max_restored_page_id > 0) {
1504 int curr_max_page_id = site_instance->max_page_id();
1505 if (max_restored_page_id > curr_max_page_id) {
1506 // Need to update the site instance immediately.
1507 site_instance->UpdateMaxPageID(max_restored_page_id);
1508
1509 // Also tell the renderer to update its internal representation. We
1510 // need to reserve enough IDs to make all restored page IDs less than
1511 // the max.
1512 if (curr_max_page_id < 0)
1513 curr_max_page_id = 0;
1514 rvh->ReservePageIDRange(max_restored_page_id - curr_max_page_id);
1515 }
1516 }
1517}
1518
1519void TabContents::UpdateHistoryForNavigation(
[email protected]ebe89e062009-08-13 23:16:541520 const GURL& virtual_url,
[email protected]befd8d822009-07-01 04:51:471521 const NavigationController::LoadCommittedDetails& details,
[email protected]420ae012009-04-24 05:16:321522 const ViewHostMsg_FrameNavigate_Params& params) {
1523 if (profile()->IsOffTheRecord())
1524 return;
1525
1526 // Add to history service.
1527 HistoryService* hs = profile()->GetHistoryService(Profile::IMPLICIT_ACCESS);
1528 if (hs) {
1529 if (PageTransition::IsMainFrame(params.transition) &&
[email protected]ebe89e062009-08-13 23:16:541530 virtual_url != params.url) {
1531 // Hack on the "virtual" URL so that it will appear in history. For some
[email protected]420ae012009-04-24 05:16:321532 // types of URLs, we will display a magic URL that is different from where
1533 // the page is actually navigated. We want the user to see in history
[email protected]ebe89e062009-08-13 23:16:541534 // what they saw in the URL bar, so we add the virtual URL as a redirect.
1535 // This only applies to the main frame, as the virtual URL doesn't apply
[email protected]420ae012009-04-24 05:16:321536 // to sub-frames.
1537 std::vector<GURL> redirects = params.redirects;
1538 if (!redirects.empty())
[email protected]ebe89e062009-08-13 23:16:541539 redirects.back() = virtual_url;
1540 hs->AddPage(virtual_url, this, params.page_id, params.referrer,
[email protected]befd8d822009-07-01 04:51:471541 params.transition, redirects, details.did_replace_entry);
[email protected]420ae012009-04-24 05:16:321542 } else {
1543 hs->AddPage(params.url, this, params.page_id, params.referrer,
[email protected]befd8d822009-07-01 04:51:471544 params.transition, params.redirects,
1545 details.did_replace_entry);
[email protected]420ae012009-04-24 05:16:321546 }
1547 }
1548}
1549
1550bool TabContents::UpdateTitleForEntry(NavigationEntry* entry,
1551 const std::wstring& title) {
1552 // For file URLs without a title, use the pathname instead. In the case of a
1553 // synthesized title, we don't want the update to count toward the "one set
1554 // per page of the title to history."
1555 std::wstring final_title;
1556 bool explicit_set;
1557 if (entry->url().SchemeIsFile() && title.empty()) {
1558 final_title = UTF8ToWide(entry->url().ExtractFileName());
1559 explicit_set = false; // Don't count synthetic titles toward the set limit.
1560 } else {
1561 TrimWhitespace(title, TRIM_ALL, &final_title);
1562 explicit_set = true;
1563 }
1564
1565 if (final_title == UTF16ToWideHack(entry->title()))
1566 return false; // Nothing changed, don't bother.
1567
1568 entry->set_title(WideToUTF16Hack(final_title));
1569
1570 // Update the history system for this page.
1571 if (!profile()->IsOffTheRecord() && !received_page_title_) {
1572 HistoryService* hs =
1573 profile()->GetHistoryService(Profile::IMPLICIT_ACCESS);
1574 if (hs)
[email protected]ebe89e062009-08-13 23:16:541575 hs->SetPageTitle(entry->virtual_url(), final_title);
[email protected]420ae012009-04-24 05:16:321576
1577 // Don't allow the title to be saved again for explicitly set ones.
1578 received_page_title_ = explicit_set;
1579 }
1580
1581 // Lastly, set the title for the view.
1582 view_->SetPageTitle(final_title);
1583
1584 return true;
1585}
1586
1587void TabContents::NotifySwapped() {
1588 // After sending out a swap notification, we need to send a disconnect
1589 // notification so that clients that pick up a pointer to |this| can NULL the
1590 // pointer. See Bug 1230284.
1591 notify_disconnection_ = true;
1592 NotificationService::current()->Notify(
[email protected]57c6a652009-05-04 07:58:341593 NotificationType::TAB_CONTENTS_SWAPPED,
[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::NotifyConnected() {
1599 notify_disconnection_ = true;
1600 NotificationService::current()->Notify(
[email protected]57c6a652009-05-04 07:58:341601 NotificationType::TAB_CONTENTS_CONNECTED,
[email protected]d82ed61e2009-06-16 02:46:221602 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:321603 NotificationService::NoDetails());
1604}
1605
1606void TabContents::NotifyDisconnected() {
1607 if (!notify_disconnection_)
1608 return;
1609
1610 notify_disconnection_ = false;
1611 NotificationService::current()->Notify(
[email protected]57c6a652009-05-04 07:58:341612 NotificationType::TAB_CONTENTS_DISCONNECTED,
[email protected]d82ed61e2009-06-16 02:46:221613 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:321614 NotificationService::NoDetails());
1615}
1616
1617void TabContents::GenerateKeywordIfNecessary(
1618 const ViewHostMsg_FrameNavigate_Params& params) {
1619 if (!params.searchable_form_url.is_valid())
1620 return;
1621
1622 if (profile()->IsOffTheRecord())
1623 return;
1624
1625 int last_index = controller_.last_committed_entry_index();
1626 // When there was no previous page, the last index will be 0. This is
1627 // normally due to a form submit that opened in a new tab.
1628 // TODO(brettw) bug 916126: we should support keywords when form submits
1629 // happen in new tabs.
1630 if (last_index <= 0)
1631 return;
1632 const NavigationEntry* previous_entry =
1633 controller_.GetEntryAtIndex(last_index - 1);
1634 if (IsFormSubmit(previous_entry)) {
1635 // Only generate a keyword if the previous page wasn't itself a form
1636 // submit.
1637 return;
1638 }
1639
1640 GURL keyword_url = previous_entry->user_typed_url().is_valid() ?
1641 previous_entry->user_typed_url() : previous_entry->url();
1642 std::wstring keyword =
1643 TemplateURLModel::GenerateKeyword(keyword_url, true); // autodetected
1644 if (keyword.empty())
1645 return;
1646
1647 TemplateURLModel* url_model = profile()->GetTemplateURLModel();
1648 if (!url_model)
1649 return;
1650
1651 if (!url_model->loaded()) {
1652 url_model->Load();
1653 return;
1654 }
1655
1656 const TemplateURL* current_url;
1657 std::wstring url = UTF8ToWide(params.searchable_form_url.spec());
1658 if (!url_model->CanReplaceKeyword(keyword, url, &current_url))
1659 return;
1660
1661 if (current_url) {
1662 if (current_url->originating_url().is_valid()) {
1663 // The existing keyword was generated from an OpenSearch description
1664 // document, don't regenerate.
1665 return;
1666 }
1667 url_model->Remove(current_url);
1668 }
1669 TemplateURL* new_url = new TemplateURL();
1670 new_url->set_keyword(keyword);
1671 new_url->set_short_name(keyword);
1672 new_url->SetURL(url, 0, 0);
1673 new_url->add_input_encoding(params.searchable_form_encoding);
1674 DCHECK(controller_.GetLastCommittedEntry());
1675 const GURL& favicon_url =
1676 controller_.GetLastCommittedEntry()->favicon().url();
1677 if (favicon_url.is_valid()) {
1678 new_url->SetFavIconURL(favicon_url);
1679 } else {
1680 // The favicon url isn't valid. This means there really isn't a favicon,
1681 // or the favicon url wasn't obtained before the load started. This assumes
1682 // the later.
1683 // TODO(sky): Need a way to set the favicon that doesn't involve generating
1684 // its url.
1685 new_url->SetFavIconURL(TemplateURL::GenerateFaviconURL(params.referrer));
1686 }
1687 new_url->set_safe_for_autoreplace(true);
1688 url_model->Add(new_url);
1689}
1690
[email protected]bf5c2ff392009-07-08 16:24:331691void TabContents::OnUserGesture() {
1692 // See comment in RenderViewHostDelegate::OnUserGesture as to why we do this.
1693 DownloadRequestManager* drm = g_browser_process->download_request_manager();
1694 if (drm)
1695 drm->OnUserGesture(this);
[email protected]e7eaedde2009-09-25 20:09:491696 ExternalProtocolHandler::OnUserGesture();
[email protected]bf5c2ff392009-07-08 16:24:331697 controller_.OnUserGesture();
1698}
1699
1700void TabContents::OnFindReply(int request_id,
1701 int number_of_matches,
1702 const gfx::Rect& selection_rect,
1703 int active_match_ordinal,
1704 bool final_update) {
[email protected]3ce7ced2009-07-24 18:52:421705 // Ignore responses for requests that have been aborted.
1706 if (find_op_aborted_)
1707 return;
1708
[email protected]bf5c2ff392009-07-08 16:24:331709 // Ignore responses for requests other than the one we have most recently
1710 // issued. That way we won't act on stale results when the user has
1711 // already typed in another query.
1712 if (request_id != current_find_request_id_)
1713 return;
1714
1715 if (number_of_matches == -1)
1716 number_of_matches = last_search_result_.number_of_matches();
1717 if (active_match_ordinal == -1)
1718 active_match_ordinal = last_search_result_.active_match_ordinal();
1719
1720 gfx::Rect selection = selection_rect;
1721 if (selection.IsEmpty())
1722 selection = last_search_result_.selection_rect();
1723
1724 // Notify the UI, automation and any other observers that a find result was
1725 // found.
1726 last_search_result_ = FindNotificationDetails(request_id, number_of_matches,
1727 selection, active_match_ordinal,
1728 final_update);
1729 NotificationService::current()->Notify(
1730 NotificationType::FIND_RESULT_AVAILABLE,
1731 Source<TabContents>(this),
1732 Details<FindNotificationDetails>(&last_search_result_));
1733}
1734
1735void TabContents::GoToEntryAtOffset(int offset) {
[email protected]f9cc4c452009-10-13 14:56:381736 if (!delegate_ || delegate_->OnGoToEntryOffset(offset))
1737 controller_.GoToOffset(offset);
[email protected]bf5c2ff392009-07-08 16:24:331738}
1739
1740void TabContents::GetHistoryListCount(int* back_list_count,
1741 int* forward_list_count) {
1742 int current_index = controller_.last_committed_entry_index();
1743 *back_list_count = current_index;
1744 *forward_list_count = controller_.entry_count() - current_index - 1;
1745}
1746
1747void TabContents::OnMissingPluginStatus(int status) {
1748#if defined(OS_WIN)
1749// TODO(PORT): pull in when plug-ins work
1750 GetPluginInstaller()->OnMissingPluginStatus(status);
1751#endif
1752}
1753
1754void TabContents::OnCrashedPlugin(const FilePath& plugin_path) {
[email protected]bf5c2ff392009-07-08 16:24:331755 DCHECK(!plugin_path.value().empty());
1756
1757 std::wstring plugin_name = plugin_path.ToWStringHack();
[email protected]d32d46182009-07-23 00:09:351758#if defined(OS_WIN) || defined(OS_MACOSX)
[email protected]bf5c2ff392009-07-08 16:24:331759 scoped_ptr<FileVersionInfo> version_info(
1760 FileVersionInfo::CreateFileVersionInfo(plugin_path));
1761 if (version_info.get()) {
1762 const std::wstring& product_name = version_info->product_name();
[email protected]996fd702009-09-04 19:12:371763 if (!product_name.empty()) {
[email protected]bf5c2ff392009-07-08 16:24:331764 plugin_name = product_name;
[email protected]996fd702009-09-04 19:12:371765#if defined(OS_MACOSX)
1766 // Many plugins on the Mac have .plugin in the actual name, which looks
1767 // terrible, so look for that and strip it off if present.
1768 const std::wstring plugin_extension(L".plugin");
1769 if (EndsWith(plugin_name, plugin_extension, true))
1770 plugin_name.erase(plugin_name.length() - plugin_extension.length());
1771#endif // OS_MACOSX
1772 }
[email protected]bf5c2ff392009-07-08 16:24:331773 }
[email protected]4a560ed2009-07-13 23:35:261774#else
1775 NOTIMPLEMENTED() << " convert plugin path to plugin name";
1776#endif
[email protected]bf5c2ff392009-07-08 16:24:331777 AddInfoBar(new SimpleAlertInfoBarDelegate(
1778 this, l10n_util::GetStringF(IDS_PLUGIN_CRASHED_PROMPT, plugin_name),
1779 NULL));
[email protected]bf5c2ff392009-07-08 16:24:331780}
1781
1782void TabContents::OnCrashedWorker() {
1783 AddInfoBar(new SimpleAlertInfoBarDelegate(
1784 this, l10n_util::GetString(IDS_WEBWORKER_CRASHED_PROMPT),
1785 NULL));
1786}
1787
1788void TabContents::OnDidGetApplicationInfo(
1789 int32 page_id,
1790 const webkit_glue::WebApplicationInfo& info) {
1791 if (pending_install_.page_id != page_id)
1792 return; // The user clicked create on a separate page. Ignore this.
1793
1794 pending_install_.callback_functor =
1795 new GearsCreateShortcutCallbackFunctor(this);
1796 GearsCreateShortcut(
1797 info, pending_install_.title, pending_install_.url, pending_install_.icon,
1798 NewCallback(pending_install_.callback_functor,
1799 &GearsCreateShortcutCallbackFunctor::Run));
1800}
1801
1802void TabContents::DidStartProvisionalLoadForFrame(
1803 RenderViewHost* render_view_host,
1804 bool is_main_frame,
1805 const GURL& url) {
1806 ProvisionalLoadDetails details(is_main_frame,
1807 controller_.IsURLInPageNavigation(url),
1808 url, std::string(), false);
1809 NotificationService::current()->Notify(
1810 NotificationType::FRAME_PROVISIONAL_LOAD_START,
1811 Source<NavigationController>(&controller_),
1812 Details<ProvisionalLoadDetails>(&details));
1813}
1814
1815void TabContents::DidStartReceivingResourceResponse(
1816 ResourceRequestDetails* details) {
1817 NotificationService::current()->Notify(
1818 NotificationType::RESOURCE_RESPONSE_STARTED,
1819 Source<NavigationController>(&controller()),
1820 Details<ResourceRequestDetails>(details));
1821}
1822
1823void TabContents::DidRedirectResource(ResourceRequestDetails* details) {
1824 NotificationService::current()->Notify(
1825 NotificationType::RESOURCE_RECEIVED_REDIRECT,
1826 Source<NavigationController>(&controller()),
1827 Details<ResourceRequestDetails>(details));
1828}
1829
1830void TabContents::DidLoadResourceFromMemoryCache(
1831 const GURL& url,
1832 const std::string& frame_origin,
1833 const std::string& main_frame_origin,
1834 const std::string& security_info) {
1835 // Send out a notification that we loaded a resource from our memory cache.
1836 int cert_id = 0, cert_status = 0, security_bits = 0;
1837 SSLManager::DeserializeSecurityInfo(security_info,
1838 &cert_id, &cert_status,
1839 &security_bits);
1840 LoadFromMemoryCacheDetails details(url, frame_origin, main_frame_origin,
[email protected]76543b92009-08-31 17:27:451841 process()->id(), cert_id, cert_status);
[email protected]bf5c2ff392009-07-08 16:24:331842
1843 NotificationService::current()->Notify(
1844 NotificationType::LOAD_FROM_MEMORY_CACHE,
1845 Source<NavigationController>(&controller_),
1846 Details<LoadFromMemoryCacheDetails>(&details));
1847}
1848
[email protected]e3d60e5d2009-09-25 21:08:291849void TabContents::DidDisplayInsecureContent() {
[email protected]09843b5f2009-09-26 08:30:591850 controller_.ssl_manager()->DidDisplayInsecureContent();
[email protected]e3d60e5d2009-09-25 21:08:291851}
1852
1853void TabContents::DidRunInsecureContent(const std::string& security_origin) {
[email protected]09843b5f2009-09-26 08:30:591854 controller_.ssl_manager()->DidRunInsecureContent(security_origin);
[email protected]e3d60e5d2009-09-25 21:08:291855}
1856
[email protected]bf5c2ff392009-07-08 16:24:331857void TabContents::DidFailProvisionalLoadWithError(
1858 RenderViewHost* render_view_host,
1859 bool is_main_frame,
1860 int error_code,
1861 const GURL& url,
1862 bool showing_repost_interstitial) {
1863 if (net::ERR_ABORTED == error_code) {
1864 // EVIL HACK ALERT! Ignore failed loads when we're showing interstitials.
1865 // This means that the interstitial won't be torn down properly, which is
1866 // bad. But if we have an interstitial, go back to another tab type, and
1867 // then load the same interstitial again, we could end up getting the first
1868 // interstitial's "failed" message (as a result of the cancel) when we're on
1869 // the second one.
1870 //
1871 // We can't tell this apart, so we think we're tearing down the current page
1872 // which will cause a crash later one. There is also some code in
1873 // RenderViewHostManager::RendererAbortedProvisionalLoad that is commented
1874 // out because of this problem.
1875 //
1876 // http://code.google.com/p/chromium/issues/detail?id=2855
1877 // Because this will not tear down the interstitial properly, if "back" is
1878 // back to another tab type, the interstitial will still be somewhat alive
1879 // in the previous tab type. If you navigate somewhere that activates the
1880 // tab with the interstitial again, you'll see a flash before the new load
1881 // commits of the interstitial page.
1882 if (showing_interstitial_page()) {
1883 LOG(WARNING) << "Discarding message during interstitial.";
1884 return;
1885 }
1886
1887 // This will discard our pending entry if we cancelled the load (e.g., if we
1888 // decided to download the file instead of load it). Only discard the
1889 // pending entry if the URLs match, otherwise the user initiated a navigate
1890 // before the page loaded so that the discard would discard the wrong entry.
1891 NavigationEntry* pending_entry = controller_.pending_entry();
1892 if (pending_entry && pending_entry->url() == url) {
1893 controller_.DiscardNonCommittedEntries();
1894 // Update the URL display.
1895 NotifyNavigationStateChanged(TabContents::INVALIDATE_URL);
1896 }
1897
1898 render_manager_.RendererAbortedProvisionalLoad(render_view_host);
1899 }
1900
1901 // Send out a notification that we failed a provisional load with an error.
1902 ProvisionalLoadDetails details(is_main_frame,
1903 controller_.IsURLInPageNavigation(url),
1904 url, std::string(), false);
1905 details.set_error_code(error_code);
1906
1907 NotificationService::current()->Notify(
1908 NotificationType::FAIL_PROVISIONAL_LOAD_WITH_ERROR,
1909 Source<NavigationController>(&controller_),
1910 Details<ProvisionalLoadDetails>(&details));
1911}
1912
1913void TabContents::DocumentLoadedInFrame() {
1914 controller_.DocumentLoadedInFrame();
1915}
1916
[email protected]8d3347f2009-07-09 22:00:211917RenderViewHostDelegate::View* TabContents::GetViewDelegate() {
[email protected]420ae012009-04-24 05:16:321918 return view_.get();
1919}
1920
[email protected]8d3347f2009-07-09 22:00:211921RenderViewHostDelegate::RendererManagement*
1922TabContents::GetRendererManagementDelegate() {
1923 return &render_manager_;
1924}
1925
[email protected]bf5c2ff392009-07-08 16:24:331926RenderViewHostDelegate::BrowserIntegration*
[email protected]8d3347f2009-07-09 22:00:211927 TabContents::GetBrowserIntegrationDelegate() {
1928 return this;
[email protected]bf5c2ff392009-07-08 16:24:331929}
1930
[email protected]8d3347f2009-07-09 22:00:211931RenderViewHostDelegate::Resource* TabContents::GetResourceDelegate() {
1932 return this;
[email protected]bf5c2ff392009-07-08 16:24:331933}
1934
[email protected]8d3347f2009-07-09 22:00:211935RenderViewHostDelegate::Save* TabContents::GetSaveDelegate() {
[email protected]420ae012009-04-24 05:16:321936 return save_package_.get(); // May be NULL, but we can return NULL.
1937}
1938
[email protected]8d3347f2009-07-09 22:00:211939RenderViewHostDelegate::Printing* TabContents::GetPrintingDelegate() {
1940 return &printing_;
1941}
1942
1943RenderViewHostDelegate::FavIcon* TabContents::GetFavIconDelegate() {
1944 return &fav_icon_helper_;
1945}
1946
[email protected]c54d269e2009-10-21 18:07:381947RenderViewHostDelegate::FormFieldHistory*
1948TabContents::GetFormFieldHistoryDelegate() {
[email protected]19289d02009-10-21 21:47:001949 if (form_field_history_manager_.get() == NULL)
1950 form_field_history_manager_.reset(new FormFieldHistoryManager(this));
1951 return form_field_history_manager_.get();
[email protected]bf5c2ff392009-07-08 16:24:331952}
1953
[email protected]21674df2009-10-28 19:51:211954RenderViewHostDelegate::AutoFill* TabContents::GetAutoFillDelegate() {
1955 if (autofill_manager_.get() == NULL)
1956 autofill_manager_.reset(new AutoFillManager(this));
1957 return autofill_manager_.get();
1958}
1959
[email protected]80d96fa2009-06-10 22:34:511960RendererPreferences TabContents::GetRendererPrefs() const {
[email protected]840b1512009-07-21 16:53:461961 return renderer_preferences_;
[email protected]80d96fa2009-06-10 22:34:511962}
1963
[email protected]57c6a652009-05-04 07:58:341964TabContents* TabContents::GetAsTabContents() {
1965 return this;
1966}
1967
[email protected]3e7cf1362009-08-21 17:47:081968void TabContents::AddBlockedNotice(const GURL& url, const string16& reason) {
1969 CreateBlockedPopupContainerIfNecessary();
1970 blocked_popups_->AddBlockedNotice(url, reason);
1971}
1972
[email protected]7b291f92009-08-14 05:43:531973ViewType::Type TabContents::GetRenderViewType() const {
1974 return ViewType::TAB_CONTENTS;
1975}
1976
1977int TabContents::GetBrowserWindowID() const {
1978 return controller().window_id().id();
1979}
1980
[email protected]420ae012009-04-24 05:16:321981void TabContents::RenderViewCreated(RenderViewHost* render_view_host) {
[email protected]2e4633c2009-07-09 16:58:061982 NotificationService::current()->Notify(
1983 NotificationType::RENDER_VIEW_HOST_CREATED_FOR_TAB,
1984 Source<TabContents>(this),
1985 Details<RenderViewHost>(render_view_host));
[email protected]420ae012009-04-24 05:16:321986 NavigationEntry* entry = controller_.GetActiveEntry();
1987 if (!entry)
1988 return;
1989
1990 // When we're creating views, we're still doing initial setup, so we always
1991 // use the pending DOM UI rather than any possibly existing committed one.
1992 if (render_manager_.pending_dom_ui()) {
1993 render_manager_.pending_dom_ui()->RenderViewCreated(render_view_host);
1994 }
1995
1996 if (entry->IsViewSourceMode()) {
1997 // Put the renderer in view source mode.
1998 render_view_host->Send(
1999 new ViewMsg_EnableViewSourceMode(render_view_host->routing_id()));
2000 }
[email protected]0666aef2009-05-13 19:48:082001
2002 view()->RenderViewCreated(render_view_host);
[email protected]420ae012009-04-24 05:16:322003}
2004
2005void TabContents::RenderViewReady(RenderViewHost* rvh) {
2006 if (rvh != render_view_host()) {
2007 // Don't notify the world, since this came from a renderer in the
2008 // background.
2009 return;
2010 }
2011
2012 NotifyConnected();
[email protected]9d8a4642009-07-29 17:25:302013 bool was_crashed = is_crashed();
[email protected]420ae012009-04-24 05:16:322014 SetIsCrashed(false);
[email protected]9d8a4642009-07-29 17:25:302015
2016 // Restore the focus to the tab (otherwise the focus will be on the top
2017 // window).
2018 if (was_crashed && !FocusLocationBarByDefault())
2019 Focus();
[email protected]420ae012009-04-24 05:16:322020}
2021
2022void TabContents::RenderViewGone(RenderViewHost* rvh) {
2023 // Ask the print preview if this renderer was valuable.
2024 if (!printing_.OnRenderViewGone(rvh))
2025 return;
2026 if (rvh != render_view_host()) {
2027 // The pending page's RenderViewHost is gone.
2028 return;
2029 }
2030
2031 SetIsLoading(false, NULL);
2032 NotifyDisconnected();
2033 SetIsCrashed(true);
2034
[email protected]8e537302009-06-10 21:19:582035 // Tell the view that we've crashed so it can prepare the sad tab page.
2036 view_->OnTabCrashed();
[email protected]420ae012009-04-24 05:16:322037
2038 // Hide any visible hung renderer warning for this web contents' process.
[email protected]d82ed61e2009-06-16 02:46:222039 HungRendererDialog::HideForTabContents(this);
[email protected]420ae012009-04-24 05:16:322040}
2041
[email protected]2e4633c2009-07-09 16:58:062042void TabContents::RenderViewDeleted(RenderViewHost* rvh) {
2043 NotificationService::current()->Notify(
2044 NotificationType::RENDER_VIEW_HOST_DELETED,
2045 Source<TabContents>(this),
2046 Details<RenderViewHost>(rvh));
2047}
2048
[email protected]420ae012009-04-24 05:16:322049void TabContents::DidNavigate(RenderViewHost* rvh,
2050 const ViewHostMsg_FrameNavigate_Params& params) {
[email protected]8030f012009-09-25 18:09:372051 int extra_invalidate_flags = 0;
2052
2053 if (PageTransition::IsMainFrame(params.transition)) {
[email protected]20ede072009-10-06 00:28:352054 bool was_bookmark_bar_visible = ShouldShowBookmarkBar();
[email protected]8030f012009-09-25 18:09:372055 bool was_extension_shelf_visible = IsExtensionShelfAlwaysVisible();
2056
[email protected]420ae012009-04-24 05:16:322057 render_manager_.DidNavigateMainFrame(rvh);
2058
[email protected]20ede072009-10-06 00:28:352059 if (was_bookmark_bar_visible != ShouldShowBookmarkBar())
[email protected]8030f012009-09-25 18:09:372060 extra_invalidate_flags |= INVALIDATE_BOOKMARK_BAR;
2061 if (was_extension_shelf_visible != IsExtensionShelfAlwaysVisible())
2062 extra_invalidate_flags |= INVALIDATE_EXTENSION_SHELF;
2063 }
2064
[email protected]420ae012009-04-24 05:16:322065 // Update the site of the SiteInstance if it doesn't have one yet.
2066 if (!GetSiteInstance()->has_site())
2067 GetSiteInstance()->SetSite(params.url);
2068
2069 // Need to update MIME type here because it's referred to in
2070 // UpdateNavigationCommands() called by RendererDidNavigate() to
2071 // determine whether or not to enable the encoding menu.
2072 // It's updated only for the main frame. For a subframe,
2073 // RenderView::UpdateURL does not set params.contents_mime_type.
2074 // (see http://code.google.com/p/chromium/issues/detail?id=2929 )
2075 // TODO(jungshik): Add a test for the encoding menu to avoid
2076 // regressing it again.
2077 if (PageTransition::IsMainFrame(params.transition))
2078 contents_mime_type_ = params.contents_mime_type;
2079
2080 NavigationController::LoadCommittedDetails details;
[email protected]8030f012009-09-25 18:09:372081 bool did_navigate = controller_.RendererDidNavigate(
2082 params, extra_invalidate_flags, &details);
[email protected]3e90d4a2009-07-03 17:38:392083
2084 // Update history. Note that this needs to happen after the entry is complete,
2085 // which WillNavigate[Main,Sub]Frame will do before this function is called.
[email protected]2e29d922009-10-22 22:13:352086 if (params.should_update_history &&
2087 (!delegate() || delegate()->ShouldAddNavigationsToHistory())) {
[email protected]3e90d4a2009-07-03 17:38:392088 // Most of the time, the displayURL matches the loaded URL, but for about:
2089 // URLs, we use a data: URL as the real value. We actually want to save
2090 // the about: URL to the history db and keep the data: URL hidden. This is
2091 // what the TabContents' URL getter does.
2092 UpdateHistoryForNavigation(GetURL(), details, params);
2093 }
2094
[email protected]76543b92009-08-31 17:27:452095 if (!did_navigate)
2096 return; // No navigation happened.
[email protected]420ae012009-04-24 05:16:322097
2098 // DO NOT ADD MORE STUFF TO THIS FUNCTION! Your component should either listen
2099 // for the appropriate notification (best) or you can add it to
2100 // DidNavigateMainFramePostCommit / DidNavigateAnyFramePostCommit (only if
2101 // necessary, please).
2102
2103 // Run post-commit tasks.
2104 if (details.is_main_frame)
2105 DidNavigateMainFramePostCommit(details, params);
2106 DidNavigateAnyFramePostCommit(rvh, details, params);
2107}
2108
2109void TabContents::UpdateState(RenderViewHost* rvh,
2110 int32 page_id,
2111 const std::string& state) {
2112 DCHECK(rvh == render_view_host());
2113
2114 // We must be prepared to handle state updates for any page, these occur
2115 // when the user is scrolling and entering form data, as well as when we're
2116 // leaving a page, in which case our state may have already been moved to
2117 // the next page. The navigation controller will look up the appropriate
2118 // NavigationEntry and update it when it is notified via the delegate.
2119
2120 int entry_index = controller_.GetEntryIndexWithPageID(
2121 GetSiteInstance(), page_id);
2122 if (entry_index < 0)
2123 return;
2124 NavigationEntry* entry = controller_.GetEntryAtIndex(entry_index);
2125
2126 if (state == entry->content_state())
2127 return; // Nothing to update.
2128 entry->set_content_state(state);
2129 controller_.NotifyEntryChanged(entry, entry_index);
2130}
2131
2132void TabContents::UpdateTitle(RenderViewHost* rvh,
2133 int32 page_id, const std::wstring& title) {
2134 // If we have a title, that's a pretty good indication that we've started
2135 // getting useful data.
2136 SetNotWaitingForResponse();
2137
2138 DCHECK(rvh == render_view_host());
2139 NavigationEntry* entry = controller_.GetEntryWithPageID(GetSiteInstance(),
2140 page_id);
2141 if (!entry || !UpdateTitleForEntry(entry, title))
2142 return;
2143
2144 // Broadcast notifications when the UI should be updated.
2145 if (entry == controller_.GetEntryAtOffset(0))
[email protected]f1cd5e82009-10-23 17:19:032146 NotifyNavigationStateChanged(INVALIDATE_TITLE);
[email protected]420ae012009-04-24 05:16:322147}
2148
[email protected]420ae012009-04-24 05:16:322149void TabContents::UpdateEncoding(RenderViewHost* render_view_host,
[email protected]41fc0322009-09-04 22:23:402150 const std::string& encoding) {
[email protected]420ae012009-04-24 05:16:322151 set_encoding(encoding);
2152}
2153
2154void TabContents::UpdateTargetURL(int32 page_id, const GURL& url) {
2155 if (delegate())
2156 delegate()->UpdateTargetURL(this, url);
2157}
2158
2159void TabContents::UpdateThumbnail(const GURL& url,
2160 const SkBitmap& bitmap,
2161 const ThumbnailScore& score) {
2162 // Tell History about this thumbnail
[email protected]449478302009-06-09 20:04:282163 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kThumbnailStore)) {
[email protected]72d0fc72009-07-09 17:30:492164 if (!profile()->IsOffTheRecord())
[email protected]42339402009-07-17 17:27:192165 profile()->GetThumbnailStore()->SetPageThumbnail(url, bitmap,
2166 score, true);
[email protected]449478302009-06-09 20:04:282167 } else {
2168 HistoryService* hs;
2169 if (!profile()->IsOffTheRecord() &&
2170 (hs = profile()->GetHistoryService(Profile::IMPLICIT_ACCESS))) {
2171 hs->SetPageThumbnail(url, bitmap, score);
2172 }
[email protected]420ae012009-04-24 05:16:322173 }
2174}
2175
[email protected]8922e1f2009-10-03 05:01:262176void TabContents::UpdateInspectorSettings(const std::string& raw_settings) {
[email protected]0df30122009-06-03 12:13:082177 profile()->GetPrefs()->SetString(prefs::kWebKitInspectorSettings,
[email protected]8922e1f2009-10-03 05:01:262178 UTF8ToWide(raw_settings));
[email protected]0df30122009-06-03 12:13:082179}
2180
[email protected]420ae012009-04-24 05:16:322181void TabContents::Close(RenderViewHost* rvh) {
[email protected]07707302009-11-06 00:50:292182 // The UI may be in an event-tracking loop, such as between the
2183 // mouse-down and mouse-up in text selection or a button click.
2184 // Defer the close until after tracking is complete, so that we
2185 // don't free objects out from under the UI.
2186 // TODO(shess): This could probably be integrated with the
2187 // IsDoingDrag() test below. Punting for now because I need more
2188 // research to understand how this impacts platforms other than Mac.
2189 // TODO(shess): This could get more fine-grained. For instance,
2190 // closing a tab in another window while selecting text in the
2191 // current window's Omnibox should be just fine.
2192 if (view()->IsEventTracking()) {
2193 view()->CloseTabAfterEventTracking();
2194 return;
2195 }
2196
[email protected]24a4d1062009-07-10 23:10:422197 // If we close the tab while we're in the middle of a drag, we'll crash.
2198 // Instead, cancel the drag and close it as soon as the drag ends.
2199 if (view()->IsDoingDrag()) {
2200 view()->CancelDragAndCloseTab();
2201 return;
2202 }
2203
[email protected]420ae012009-04-24 05:16:322204 // Ignore this if it comes from a RenderViewHost that we aren't showing.
2205 if (delegate() && rvh == render_view_host())
2206 delegate()->CloseContents(this);
2207}
2208
2209void TabContents::RequestMove(const gfx::Rect& new_bounds) {
2210 if (delegate() && delegate()->IsPopup(this))
2211 delegate()->MoveContents(this, new_bounds);
2212}
2213
[email protected]7ab1e7d62009-10-14 23:32:012214void TabContents::DidStartLoading() {
[email protected]420ae012009-04-24 05:16:322215 SetIsLoading(true, NULL);
2216}
2217
[email protected]7ab1e7d62009-10-14 23:32:012218void TabContents::DidStopLoading() {
[email protected]420ae012009-04-24 05:16:322219 scoped_ptr<LoadNotificationDetails> details;
2220
2221 NavigationEntry* entry = controller_.GetActiveEntry();
2222 // An entry may not exist for a stop when loading an initial blank page or
2223 // if an iframe injected by script into a blank page finishes loading.
2224 if (entry) {
2225 scoped_ptr<base::ProcessMetrics> metrics(
2226 base::ProcessMetrics::CreateProcessMetrics(
2227 process()->process().handle()));
2228
2229 base::TimeDelta elapsed = base::TimeTicks::Now() - current_load_start_;
2230
2231 details.reset(new LoadNotificationDetails(
[email protected]ebe89e062009-08-13 23:16:542232 entry->virtual_url(),
[email protected]420ae012009-04-24 05:16:322233 entry->transition_type(),
2234 elapsed,
2235 &controller_,
2236 controller_.GetCurrentEntryIndex()));
2237 }
2238
2239 // Tell PasswordManager we've finished a page load, which serves as a
2240 // green light to save pending passwords and reset itself.
2241 GetPasswordManager()->DidStopLoading();
2242
2243 SetIsLoading(false, details.get());
2244}
2245
[email protected]420ae012009-04-24 05:16:322246void TabContents::DidRedirectProvisionalLoad(int32 page_id,
2247 const GURL& source_url,
2248 const GURL& target_url) {
2249 NavigationEntry* entry;
2250 if (page_id == -1)
2251 entry = controller_.pending_entry();
2252 else
2253 entry = controller_.GetEntryWithPageID(GetSiteInstance(), page_id);
2254 if (!entry || entry->url() != source_url)
2255 return;
2256 entry->set_url(target_url);
2257}
2258
[email protected]420ae012009-04-24 05:16:322259void TabContents::RequestOpenURL(const GURL& url, const GURL& referrer,
2260 WindowOpenDisposition disposition) {
2261 if (render_manager_.dom_ui()) {
2262 // When we're a DOM UI, it will provide a page transition type for us (this
2263 // is so the new tab page can specify AUTO_BOOKMARK for automatically
2264 // generated suggestions).
2265 //
2266 // Note also that we hide the referrer for DOM UI pages. We don't really
[email protected]60e448982009-05-06 04:21:162267 // want web sites to see a referrer of "chrome://blah" (and some
2268 // chrome: URLs might have search terms or other stuff we don't want to
[email protected]420ae012009-04-24 05:16:322269 // send to the site), so we send no referrer.
2270 OpenURL(url, GURL(), disposition,
2271 render_manager_.dom_ui()->link_transition_type());
2272 } else {
2273 OpenURL(url, referrer, disposition, PageTransition::LINK);
2274 }
2275}
2276
[email protected]1ee614862009-07-06 20:29:182277void TabContents::DomOperationResponse(const std::string& json_string,
2278 int automation_id) {
2279}
2280
[email protected]420ae012009-04-24 05:16:322281void TabContents::ProcessDOMUIMessage(const std::string& message,
[email protected]e4dad9fb2009-10-06 18:15:582282 const Value* content,
[email protected]811bfe32009-07-01 08:46:252283 int request_id,
2284 bool has_callback) {
[email protected]420ae012009-04-24 05:16:322285 if (!render_manager_.dom_ui()) {
[email protected]c2e74fe82009-09-03 17:57:442286 // This can happen if someone uses window.open() to open an extension URL
2287 // from a non-extension context.
2288 render_view_host()->BlockExtensionRequest(request_id);
[email protected]420ae012009-04-24 05:16:322289 return;
2290 }
[email protected]811bfe32009-07-01 08:46:252291 render_manager_.dom_ui()->ProcessDOMUIMessage(message, content, request_id,
2292 has_callback);
[email protected]420ae012009-04-24 05:16:322293}
2294
2295void TabContents::ProcessExternalHostMessage(const std::string& message,
2296 const std::string& origin,
2297 const std::string& target) {
2298 if (delegate())
2299 delegate()->ForwardMessageToExternalHost(message, origin, target);
2300}
2301
[email protected]420ae012009-04-24 05:16:322302void TabContents::RunFileChooser(bool multiple_files,
2303 const string16& title,
2304 const FilePath& default_file) {
2305 if (!select_file_dialog_.get())
2306 select_file_dialog_ = SelectFileDialog::Create(this);
2307 SelectFileDialog::Type dialog_type =
2308 multiple_files ? SelectFileDialog::SELECT_OPEN_MULTI_FILE :
2309 SelectFileDialog::SELECT_OPEN_FILE;
2310 select_file_dialog_->SelectFile(dialog_type, title, default_file,
2311 NULL, 0, FILE_PATH_LITERAL(""),
2312 view_->GetTopLevelNativeWindow(), NULL);
2313}
2314
2315void TabContents::RunJavaScriptMessage(
2316 const std::wstring& message,
2317 const std::wstring& default_prompt,
2318 const GURL& frame_url,
2319 const int flags,
2320 IPC::Message* reply_msg,
2321 bool* did_suppress_message) {
2322 // Suppress javascript messages when requested and when inside a constrained
2323 // popup window (because that activates them and breaks them out of the
2324 // constrained window jail).
[email protected]91ed49a2009-07-07 21:29:552325 // Also suppress messages when showing an interstitial. The interstitial is
2326 // shown over the previous page, we don't want the hidden page dialogs to
2327 // interfere with the interstitial.
2328 bool suppress_this_message = suppress_javascript_messages_ ||
2329 showing_interstitial_page();
[email protected]420ae012009-04-24 05:16:322330 if (delegate())
2331 suppress_this_message |=
[email protected]b6c874582009-05-08 19:38:312332 (delegate()->GetConstrainingContents(this) != this);
[email protected]420ae012009-04-24 05:16:322333
2334 *did_suppress_message = suppress_this_message;
2335
2336 if (!suppress_this_message) {
2337 base::TimeDelta time_since_last_message(
2338 base::TimeTicks::Now() - last_javascript_message_dismissal_);
2339 bool show_suppress_checkbox = false;
2340 // Show a checkbox offering to suppress further messages if this message is
2341 // being displayed within kJavascriptMessageExpectedDelay of the last one.
2342 if (time_since_last_message <
2343 base::TimeDelta::FromMilliseconds(kJavascriptMessageExpectedDelay))
2344 show_suppress_checkbox = true;
2345
[email protected]d82ed61e2009-06-16 02:46:222346 RunJavascriptMessageBox(this, frame_url, flags, message, default_prompt,
[email protected]420ae012009-04-24 05:16:322347 show_suppress_checkbox, reply_msg);
2348 } else {
2349 // If we are suppressing messages, just reply as is if the user immediately
2350 // pressed "Cancel".
[email protected]8710e40c2009-11-05 21:11:572351 OnJavaScriptMessageBoxClosed(reply_msg, false, std::wstring());
[email protected]420ae012009-04-24 05:16:322352 }
2353}
2354
2355void TabContents::RunBeforeUnloadConfirm(const std::wstring& message,
2356 IPC::Message* reply_msg) {
[email protected]7ab1e7d62009-10-14 23:32:012357 is_showing_before_unload_dialog_ = true;
[email protected]d82ed61e2009-06-16 02:46:222358 RunBeforeUnloadDialog(this, message, reply_msg);
[email protected]420ae012009-04-24 05:16:322359}
2360
2361void TabContents::ShowModalHTMLDialog(const GURL& url, int width, int height,
2362 const std::string& json_arguments,
2363 IPC::Message* reply_msg) {
2364 if (delegate()) {
2365 HtmlDialogUIDelegate* dialog_delegate =
2366 new ModalHtmlDialogDelegate(url, width, height, json_arguments,
[email protected]d82ed61e2009-06-16 02:46:222367 reply_msg, this);
[email protected]420ae012009-04-24 05:16:322368 delegate()->ShowHtmlDialog(dialog_delegate, NULL);
2369 }
2370}
2371
2372void TabContents::PasswordFormsSeen(
[email protected]ed3fb032009-06-16 19:50:562373 const std::vector<webkit_glue::PasswordForm>& forms) {
[email protected]420ae012009-04-24 05:16:322374 GetPasswordManager()->PasswordFormsSeen(forms);
2375}
2376
[email protected]420ae012009-04-24 05:16:322377// Checks to see if we should generate a keyword based on the OSDD, and if
2378// necessary uses TemplateURLFetcher to download the OSDD and create a keyword.
2379void TabContents::PageHasOSDD(RenderViewHost* render_view_host,
2380 int32 page_id, const GURL& url,
2381 bool autodetected) {
2382 // Make sure page_id is the current page, and the TemplateURLModel is loaded.
2383 DCHECK(url.is_valid());
2384 if (!IsActiveEntry(page_id))
2385 return;
2386 TemplateURLModel* url_model = profile()->GetTemplateURLModel();
2387 if (!url_model)
2388 return;
2389 if (!url_model->loaded()) {
2390 url_model->Load();
2391 return;
2392 }
2393 if (!profile()->GetTemplateURLFetcher())
2394 return;
2395
2396 if (profile()->IsOffTheRecord())
2397 return;
2398
2399 const NavigationEntry* entry = controller_.GetLastCommittedEntry();
2400 DCHECK(entry);
2401
2402 const NavigationEntry* base_entry = entry;
2403 if (IsFormSubmit(base_entry)) {
2404 // If the current page is a form submit, find the last page that was not
2405 // a form submit and use its url to generate the keyword from.
2406 int index = controller_.last_committed_entry_index() - 1;
2407 while (index >= 0 && IsFormSubmit(controller_.GetEntryAtIndex(index)))
2408 index--;
2409 if (index >= 0)
2410 base_entry = controller_.GetEntryAtIndex(index);
2411 else
2412 base_entry = NULL;
2413 }
2414
2415 // We want to use the user typed URL if available since that represents what
2416 // the user typed to get here, and fall back on the regular URL if not.
2417 if (!base_entry)
2418 return;
2419 GURL keyword_url = base_entry->user_typed_url().is_valid() ?
2420 base_entry->user_typed_url() : base_entry->url();
2421 if (!keyword_url.is_valid())
2422 return;
2423 std::wstring keyword = TemplateURLModel::GenerateKeyword(keyword_url,
2424 autodetected);
2425 if (keyword.empty())
2426 return;
2427 const TemplateURL* template_url =
2428 url_model->GetTemplateURLForKeyword(keyword);
2429 if (template_url && (!template_url->safe_for_autoreplace() ||
2430 template_url->originating_url() == url)) {
2431 // Either there is a user created TemplateURL for this keyword, or the
2432 // keyword has the same OSDD url and we've parsed it.
2433 return;
2434 }
2435
2436 // Download the OpenSearch description document. If this is successful a
2437 // new keyword will be created when done.
[email protected]420ae012009-04-24 05:16:322438 profile()->GetTemplateURLFetcher()->ScheduleDownload(
2439 keyword,
2440 url,
2441 base_entry->favicon().url(),
[email protected]d0cc9fb52009-06-23 02:26:162442 this,
[email protected]420ae012009-04-24 05:16:322443 autodetected);
2444}
2445
[email protected]420ae012009-04-24 05:16:322446GURL TabContents::GetAlternateErrorPageURL() const {
2447 GURL url;
2448 // Disable alternate error pages when in OffTheRecord/Incognito mode.
2449 if (profile()->IsOffTheRecord())
2450 return url;
2451
2452 PrefService* prefs = profile()->GetPrefs();
2453 DCHECK(prefs);
2454 if (prefs->GetBoolean(prefs::kAlternateErrorPagesEnabled)) {
[email protected]a33457b82009-09-21 23:07:152455 url = google_util::AppendGoogleLocaleParam(
2456 GURL(google_util::kLinkDoctorBaseURL));
[email protected]420ae012009-04-24 05:16:322457 url = google_util::AppendGoogleTLDParam(url);
2458 }
2459 return url;
2460}
2461
2462WebPreferences TabContents::GetWebkitPrefs() {
2463 PrefService* prefs = render_view_host()->process()->profile()->GetPrefs();
[email protected]7ccddb8c2009-08-04 17:36:552464 bool is_dom_ui = false;
2465 return RenderViewHostDelegateHelper::GetWebkitPrefs(prefs, is_dom_ui);
[email protected]420ae012009-04-24 05:16:322466}
2467
[email protected]420ae012009-04-24 05:16:322468void TabContents::OnJSOutOfMemory() {
2469 AddInfoBar(new SimpleAlertInfoBarDelegate(
2470 this, l10n_util::GetString(IDS_JS_OUT_OF_MEMORY_PROMPT), NULL));
2471}
2472
[email protected]420ae012009-04-24 05:16:322473void TabContents::OnCrossSiteResponse(int new_render_process_host_id,
2474 int new_request_id) {
[email protected]57c6a652009-05-04 07:58:342475 // Allows the TabContents to react when a cross-site response is ready to be
[email protected]420ae012009-04-24 05:16:322476 // delivered to a pending RenderViewHost. We must first run the onunload
2477 // handler of the old RenderViewHost before we can allow it to proceed.
2478 render_manager_.OnCrossSiteResponse(new_render_process_host_id,
2479 new_request_id);
2480}
2481
2482bool TabContents::CanBlur() const {
2483 return delegate() ? delegate()->CanBlur() : true;
2484}
2485
2486gfx::Rect TabContents::GetRootWindowResizerRect() const {
2487 if (delegate())
2488 return delegate()->GetRootWindowResizerRect();
2489 return gfx::Rect();
2490}
2491
2492void TabContents::RendererUnresponsive(RenderViewHost* rvh,
2493 bool is_during_unload) {
2494 if (is_during_unload) {
2495 // Hang occurred while firing the beforeunload/unload handler.
2496 // Pretend the handler fired so tab closing continues as if it had.
[email protected]829e7612009-04-25 01:15:112497 rvh->set_sudden_termination_allowed(true);
[email protected]420ae012009-04-24 05:16:322498
2499 if (!render_manager_.ShouldCloseTabOnUnresponsiveRenderer())
2500 return;
2501
2502 // If the tab hangs in the beforeunload/unload handler there's really
2503 // nothing we can do to recover. Pretend the unload listeners have
2504 // all fired and close the tab. If the hang is in the beforeunload handler
2505 // then the user will not have the option of cancelling the close.
2506 Close(rvh);
2507 return;
2508 }
2509
2510 if (render_view_host() && render_view_host()->IsRenderViewLive())
[email protected]d82ed61e2009-06-16 02:46:222511 HungRendererDialog::ShowForTabContents(this);
[email protected]420ae012009-04-24 05:16:322512}
2513
2514void TabContents::RendererResponsive(RenderViewHost* render_view_host) {
[email protected]d82ed61e2009-06-16 02:46:222515 HungRendererDialog::HideForTabContents(this);
[email protected]420ae012009-04-24 05:16:322516}
2517
2518void TabContents::LoadStateChanged(const GURL& url,
[email protected]094e5b22009-09-25 04:23:562519 net::LoadState load_state,
2520 uint64 upload_position,
2521 uint64 upload_size) {
[email protected]420ae012009-04-24 05:16:322522 load_state_ = load_state;
[email protected]094e5b22009-09-25 04:23:562523 upload_position_ = upload_position;
2524 upload_size_ = upload_size;
[email protected]d686e812009-06-03 19:10:292525 std::wstring languages =
2526 profile()->GetPrefs()->GetString(prefs::kAcceptLanguages);
2527 load_state_host_.clear();
2528 std::string host = url.host();
2529 net::IDNToUnicode(host.c_str(), host.size(), languages, &load_state_host_);
[email protected]420ae012009-04-24 05:16:322530 if (load_state_ == net::LOAD_STATE_READING_RESPONSE)
2531 SetNotWaitingForResponse();
2532 if (is_loading())
[email protected]c9cd2222009-05-06 05:16:502533 NotifyNavigationStateChanged(INVALIDATE_LOAD | INVALIDATE_TAB);
[email protected]420ae012009-04-24 05:16:322534}
2535
[email protected]1ee614862009-07-06 20:29:182536bool TabContents::IsExternalTabContainer() const {
2537 if (!delegate())
2538 return false;
2539
2540 return delegate()->IsExternalTabContainer();
2541}
2542
[email protected]1ee614862009-07-06 20:29:182543void TabContents::DidInsertCSS() {
2544 // This RVHDelegate function is used for extensions and not us.
[email protected]420ae012009-04-24 05:16:322545}
2546
[email protected]130efb02009-09-18 18:54:352547void TabContents::FocusedNodeChanged() {
2548 NotificationService::current()->Notify(
2549 NotificationType::FOCUS_CHANGED_IN_PAGE,
2550 Source<RenderViewHost>(render_view_host()),
2551 NotificationService::NoDetails());
2552}
2553
[email protected]420ae012009-04-24 05:16:322554void TabContents::FileSelected(const FilePath& path,
2555 int index, void* params) {
2556 render_view_host()->FileSelected(path);
2557}
2558
2559void TabContents::MultiFilesSelected(const std::vector<FilePath>& files,
2560 void* params) {
2561 render_view_host()->MultiFilesSelected(files);
2562}
2563
2564void TabContents::FileSelectionCanceled(void* params) {
2565 // If the user cancels choosing a file to upload we pass back an
2566 // empty vector.
2567 render_view_host()->MultiFilesSelected(std::vector<FilePath>());
2568}
2569
2570void TabContents::BeforeUnloadFiredFromRenderManager(
2571 bool proceed,
2572 bool* proceed_to_fire_unload) {
2573 if (delegate())
2574 delegate()->BeforeUnloadFired(this, proceed, proceed_to_fire_unload);
2575}
2576
2577void TabContents::UpdateRenderViewSizeForRenderManager() {
2578 // TODO(brettw) this is a hack. See TabContentsView::SizeContents.
2579 view_->SizeContents(view_->GetContainerSize());
2580}
2581
2582DOMUI* TabContents::CreateDOMUIForRenderManager(const GURL& url) {
[email protected]d82ed61e2009-06-16 02:46:222583 return DOMUIFactory::CreateDOMUIForURL(this, url);
[email protected]420ae012009-04-24 05:16:322584}
2585
2586NavigationEntry*
2587TabContents::GetLastCommittedNavigationEntryForRenderManager() {
2588 return controller_.GetLastCommittedEntry();
2589}
2590
2591bool TabContents::CreateRenderViewForRenderManager(
2592 RenderViewHost* render_view_host) {
[email protected]811bfe32009-07-01 08:46:252593 // If the pending navigation is to a DOMUI, tell the RenderView about any
2594 // bindings it will need enabled.
[email protected]420ae012009-04-24 05:16:322595 if (render_manager_.pending_dom_ui())
[email protected]811bfe32009-07-01 08:46:252596 render_view_host->AllowBindings(
2597 render_manager_.pending_dom_ui()->bindings());
[email protected]1adff062009-06-02 18:39:552598
[email protected]420ae012009-04-24 05:16:322599 RenderWidgetHostView* rwh_view = view_->CreateViewForWidget(render_view_host);
2600 if (!render_view_host->CreateRenderView())
2601 return false;
2602
2603 // Now that the RenderView has been created, we need to tell it its size.
2604 rwh_view->SetSize(view_->GetContainerSize());
2605
2606 UpdateMaxPageIDIfNecessary(render_view_host->site_instance(),
2607 render_view_host);
2608 return true;
2609}
2610
2611void TabContents::Observe(NotificationType type,
2612 const NotificationSource& source,
2613 const NotificationDetails& details) {
2614 switch (type.value) {
2615 case NotificationType::BOOKMARK_MODEL_LOADED:
2616 // BookmarkModel finished loading, fall through to update starred state.
2617 case NotificationType::URLS_STARRED: {
2618 // Somewhere, a URL has been starred.
2619 // Ignore notifications for profiles other than our current one.
2620 Profile* source_profile = Source<Profile>(source).ptr();
2621 if (!source_profile || !source_profile->IsSameProfile(profile()))
2622 return;
2623
2624 UpdateStarredStateForCurrentURL();
2625 break;
2626 }
2627 case NotificationType::PREF_CHANGED: {
2628 std::wstring* pref_name_in = Details<std::wstring>(details).ptr();
2629 DCHECK(Source<PrefService>(source).ptr() == profile()->GetPrefs());
2630 if (*pref_name_in == prefs::kAlternateErrorPagesEnabled) {
2631 UpdateAlternateErrorPageURL();
2632 } else if (*pref_name_in == prefs::kDefaultCharset ||
2633 StartsWithASCII(WideToUTF8(*pref_name_in), "webkit.webprefs.", true)
2634 ) {
2635 UpdateWebPreferences();
2636 } else {
2637 NOTREACHED() << "unexpected pref change notification" << *pref_name_in;
2638 }
2639 break;
2640 }
2641 case NotificationType::RENDER_WIDGET_HOST_DESTROYED:
2642 view_->RenderWidgetHostDestroyed(Source<RenderWidgetHost>(source).ptr());
2643 break;
2644
2645 case NotificationType::NAV_ENTRY_COMMITTED: {
2646 DCHECK(&controller_ == Source<NavigationController>(source).ptr());
2647
2648 NavigationController::LoadCommittedDetails& committed_details =
2649 *(Details<NavigationController::LoadCommittedDetails>(details).ptr());
2650 ExpireInfoBars(committed_details);
2651 break;
2652 }
2653
[email protected]d3ba77272009-09-03 00:06:092654#if defined(OS_LINUX)
2655 case NotificationType::BROWSER_THEME_CHANGED: {
2656 render_view_host()->SyncRendererPrefs();
2657 break;
2658 }
2659#endif
2660
[email protected]420ae012009-04-24 05:16:322661 default:
2662 NOTREACHED();
2663 }
2664}
[email protected]aacd7cc2009-09-02 14:51:102665
[email protected]41fc0322009-09-04 22:23:402666void TabContents::set_encoding(const std::string& encoding) {
[email protected]aacd7cc2009-09-02 14:51:102667 encoding_ = CharacterEncoding::GetCanonicalEncodingNameByAliasName(encoding);
2668}