blob: a5ffec08e5f75197c7da33ddf4c76c76c944c952 [file] [log] [blame]
[email protected]ce5c4502009-05-06 16:46:111// Copyright (c) 2009 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
[email protected]f3ec7742009-01-15 00:59:165#include "chrome/browser/tab_contents/tab_contents.h"
initial.commit09911bf2008-07-26 23:55:296
[email protected]a92b8642009-05-05 23:38:567#include "app/l10n_util.h"
[email protected]9929da92009-05-05 02:05:118#include "app/resource_bundle.h"
[email protected]420ae012009-04-24 05:16:329#include "base/file_version_info.h"
10#include "base/process_util.h"
[email protected]96d185d2009-04-24 03:28:5411#include "base/string16.h"
[email protected]996fd702009-09-04 19:12:3712#include "base/string_util.h"
[email protected]96d185d2009-04-24 03:28:5413#include "base/time.h"
[email protected]fdd61c62009-04-22 19:22:5714#include "chrome/browser/autofill_manager.h"
[email protected]66ba4932009-06-04 19:22:1315#include "chrome/browser/blocked_popup_container.h"
[email protected]420ae012009-04-24 05:16:3216#include "chrome/browser/bookmarks/bookmark_model.h"
[email protected]96d185d2009-04-24 03:28:5417#include "chrome/browser/browser_process.h"
initial.commit09911bf2008-07-26 23:55:2918#include "chrome/browser/cert_store.h"
[email protected]aacd7cc2009-09-02 14:51:1019#include "chrome/browser/character_encoding.h"
[email protected]96d185d2009-04-24 03:28:5420#include "chrome/browser/debugger/devtools_manager.h"
[email protected]420ae012009-04-24 05:16:3221#include "chrome/browser/dom_operation_notification_details.h"
[email protected]96d185d2009-04-24 03:28:5422#include "chrome/browser/dom_ui/dom_ui.h"
23#include "chrome/browser/dom_ui/dom_ui_factory.h"
[email protected]bcef0dc02009-02-28 00:35:0224#include "chrome/browser/download/download_item_model.h"
[email protected]420ae012009-04-24 05:16:3225#include "chrome/browser/download/download_manager.h"
26#include "chrome/browser/download/download_request_manager.h"
[email protected]e7eaedde2009-09-25 20:09:4927#include "chrome/browser/external_protocol_handler.h"
[email protected]0189bc722009-08-28 21:56:4828#include "chrome/browser/favicon_service.h"
[email protected]420ae012009-04-24 05:16:3229#include "chrome/browser/gears_integration.h"
30#include "chrome/browser/google_util.h"
[email protected]8897c382009-05-06 17:53:2631#include "chrome/browser/hung_renderer_dialog.h"
[email protected]420ae012009-04-24 05:16:3232#include "chrome/browser/jsmessage_box_handler.h"
33#include "chrome/browser/load_from_memory_cache_details.h"
34#include "chrome/browser/load_notification_details.h"
[email protected]84abba62009-10-07 17:01:4435#include "chrome/browser/metrics/metric_event_duration_details.h"
[email protected]ea0e80892009-08-22 00:36:0536#include "chrome/browser/modal_html_dialog_delegate.h"
[email protected]c8865482009-07-23 20:40:1037#include "chrome/browser/omnibox_search_hint.h"
[email protected]fdd61c62009-04-22 19:22:5738#include "chrome/browser/password_manager/password_manager.h"
39#include "chrome/browser/plugin_installer.h"
[email protected]ce560f82009-06-03 09:39:4440#include "chrome/browser/profile.h"
[email protected]1db6ff152009-10-12 15:32:0741#include "chrome/browser/renderer_host/render_process_host.h"
[email protected]96d185d2009-04-24 03:28:5442#include "chrome/browser/renderer_host/render_widget_host_view.h"
[email protected]fd694982009-07-03 17:32:3743#include "chrome/browser/renderer_host/resource_request_details.h"
[email protected]1db6ff152009-10-12 15:32:0744#include "chrome/browser/renderer_host/site_instance.h"
[email protected]96d185d2009-04-24 03:28:5445#include "chrome/browser/renderer_host/web_cache_manager.h"
[email protected]bd1ad682009-05-15 22:19:1746#include "chrome/browser/tab_contents/infobar_delegate.h"
[email protected]7e383692009-06-12 19:14:5447#include "chrome/browser/tab_contents/interstitial_page.h"
[email protected]f3ec7742009-01-15 00:59:1648#include "chrome/browser/tab_contents/navigation_entry.h"
[email protected]de34a962009-06-26 23:22:4349#include "chrome/browser/tab_contents/provisional_load_details.h"
[email protected]f3ec7742009-01-15 00:59:1650#include "chrome/browser/tab_contents/tab_contents_delegate.h"
[email protected]fdd61c62009-04-22 19:22:5751#include "chrome/browser/tab_contents/tab_contents_view.h"
[email protected]58dca552009-06-17 00:35:0252#include "chrome/browser/tab_contents/thumbnail_generator.h"
[email protected]449478302009-06-09 20:04:2853#include "chrome/browser/thumbnail_store.h"
[email protected]420ae012009-04-24 05:16:3254#include "chrome/browser/search_engines/template_url_fetcher.h"
55#include "chrome/browser/search_engines/template_url_model.h"
[email protected]35f7d212009-04-29 21:19:2756#include "chrome/common/chrome_switches.h"
[email protected]ba69a7d2009-09-28 21:09:5657#include "chrome/common/extensions/extension_action.h"
[email protected]bfd04a62009-02-01 18:16:5658#include "chrome/common/notification_service.h"
initial.commit09911bf2008-07-26 23:55:2959#include "chrome/common/pref_names.h"
[email protected]1eb89e82008-08-15 12:27:0360#include "chrome/common/pref_service.h"
[email protected]420ae012009-04-24 05:16:3261#include "chrome/common/render_messages.h"
[email protected]1ee614862009-07-06 20:29:1862#include "chrome/common/renderer_preferences.h"
[email protected]96d185d2009-04-24 03:28:5463#include "chrome/common/url_constants.h"
[email protected]8897c382009-05-06 17:53:2664#include "grit/generated_resources.h"
[email protected]420ae012009-04-24 05:16:3265#include "grit/locale_settings.h"
66#include "net/base/mime_util.h"
67#include "net/base/net_errors.h"
[email protected]d686e812009-06-03 19:10:2968#include "net/base/net_util.h"
[email protected]420ae012009-04-24 05:16:3269#include "net/base/registry_controlled_domain.h"
[email protected]1ee614862009-07-06 20:29:1870#include "webkit/glue/password_form.h"
71#include "webkit/glue/webpreferences.h"
[email protected]3e45ba92009-02-20 21:09:0072
[email protected]304f8792009-08-19 21:28:0973#if defined(OS_CHROMEOS)
74// For GdkScreen
75#include <gdk/gdk.h>
[email protected]0ba5c5e2009-08-20 16:47:1376#endif // defined(OS_CHROMEOS)
77
78#if defined(OS_LINUX)
79#include "chrome/browser/gtk/create_application_shortcuts_dialog_gtk.h"
[email protected]d3ba77272009-09-03 00:06:0980#include "chrome/browser/gtk/gtk_theme_provider.h"
[email protected]0ba5c5e2009-08-20 16:47:1381#endif // defined(OS_LINUX)
82
[email protected]420ae012009-04-24 05:16:3283// Cross-Site Navigations
84//
85// If a TabContents is told to navigate to a different web site (as determined
86// by SiteInstance), it will replace its current RenderViewHost with a new
87// RenderViewHost dedicated to the new SiteInstance. This works as follows:
88//
89// - Navigate determines whether the destination is cross-site, and if so,
90// it creates a pending_render_view_host_ and moves into the PENDING
91// RendererState.
92// - The pending RVH is "suspended," so that no navigation messages are sent to
93// its renderer until the onbeforeunload JavaScript handler has a chance to
94// run in the current RVH.
95// - The pending RVH tells CrossSiteRequestManager (a thread-safe singleton)
96// that it has a pending cross-site request. ResourceDispatcherHost will
97// check for this when the response arrives.
98// - The current RVH runs its onbeforeunload handler. If it returns false, we
99// cancel all the pending logic and go back to NORMAL. Otherwise we allow
100// the pending RVH to send the navigation request to its renderer.
101// - ResourceDispatcherHost receives a ResourceRequest on the IO thread. It
102// checks CrossSiteRequestManager to see that the RVH responsible has a
103// pending cross-site request, and then installs a CrossSiteEventHandler.
104// - When RDH receives a response, the BufferedEventHandler determines whether
105// it is a download. If so, it sends a message to the new renderer causing
106// it to cancel the request, and the download proceeds in the download
107// thread. For now, we stay in a PENDING state (with a pending RVH) until
[email protected]57c6a652009-05-04 07:58:34108// the next DidNavigate event for this TabContents. This isn't ideal, but it
[email protected]420ae012009-04-24 05:16:32109// doesn't affect any functionality.
110// - After RDH receives a response and determines that it is safe and not a
111// download, it pauses the response to first run the old page's onunload
112// handler. It does this by asynchronously calling the OnCrossSiteResponse
[email protected]57c6a652009-05-04 07:58:34113// method of TabContents on the UI thread, which sends a ClosePage message
[email protected]420ae012009-04-24 05:16:32114// to the current RVH.
115// - Once the onunload handler is finished, a ClosePage_ACK message is sent to
116// the ResourceDispatcherHost, who unpauses the response. Data is then sent
117// to the pending RVH.
118// - The pending renderer sends a FrameNavigate message that invokes the
119// DidNavigate method. This replaces the current RVH with the
120// pending RVH and goes back to the NORMAL RendererState.
121
122namespace {
123
124// Amount of time we wait between when a key event is received and the renderer
125// is queried for its state and pushed to the NavigationEntry.
126const int kQueryStateDelay = 5000;
127
128const int kSyncWaitDelay = 40;
129
[email protected]304f8792009-08-19 21:28:09130#if defined(OS_CHROMEOS)
131// If a popup window is bigger than this fraction of the screen on chrome os,
132// turn it into a tab
133const float kMaxWidthFactor = 0.5;
134const float kMaxHeightFactor = 0.6;
135#endif
136
[email protected]420ae012009-04-24 05:16:32137// If another javascript message box is displayed within
138// kJavascriptMessageExpectedDelay of a previous javascript message box being
139// dismissed, display an option to suppress future message boxes from this
140// contents.
141const int kJavascriptMessageExpectedDelay = 1000;
142
[email protected]420ae012009-04-24 05:16:32143// The list of prefs we want to observe.
144const wchar_t* kPrefsToObserve[] = {
145 prefs::kAlternateErrorPagesEnabled,
146 prefs::kWebKitJavaEnabled,
147 prefs::kWebKitJavascriptEnabled,
148 prefs::kWebKitLoadsImagesAutomatically,
149 prefs::kWebKitPluginsEnabled,
150 prefs::kWebKitUsesUniversalDetector,
151 prefs::kWebKitSerifFontFamily,
152 prefs::kWebKitSansSerifFontFamily,
153 prefs::kWebKitFixedFontFamily,
154 prefs::kWebKitDefaultFontSize,
155 prefs::kWebKitDefaultFixedFontSize,
156 prefs::kDefaultCharset
157 // kWebKitStandardFontIsSerif needs to be added
158 // if we let users pick which font to use, serif or sans-serif when
159 // no font is specified or a CSS generic family (serif or sans-serif)
160 // is not specified.
161};
162
163const int kPrefsToObserveLength = arraysize(kPrefsToObserve);
164
[email protected]420ae012009-04-24 05:16:32165// Returns true if the entry's transition type is FORM_SUBMIT.
166bool IsFormSubmit(const NavigationEntry* entry) {
167 return (PageTransition::StripQualifier(entry->transition_type()) ==
168 PageTransition::FORM_SUBMIT);
169}
170
171#if defined(OS_WIN)
172
173BOOL CALLBACK InvalidateWindow(HWND hwnd, LPARAM lparam) {
174 // Note: erase is required to properly paint some widgets borders. This can
175 // be seen with textfields.
176 InvalidateRect(hwnd, NULL, TRUE);
177 return TRUE;
178}
179#endif
180
[email protected]056de2d2009-06-26 16:41:34181void MakeNavigateParams(const NavigationEntry& entry, bool reload,
182 ViewMsg_Navigate_Params* params) {
183 params->page_id = entry.page_id();
184 params->url = entry.url();
185 params->referrer = entry.referrer();
186 params->transition = entry.transition_type();
187 params->state = entry.content_state();
188 params->reload = reload;
189 params->request_time = base::Time::Now();
190}
191
[email protected]420ae012009-04-24 05:16:32192} // namespace
193
194// -----------------------------------------------------------------------------
195
[email protected]fdd61c62009-04-22 19:22:57196// static
197int TabContents::find_request_id_counter_ = -1;
198
[email protected]420ae012009-04-24 05:16:32199class TabContents::GearsCreateShortcutCallbackFunctor {
200 public:
201 explicit GearsCreateShortcutCallbackFunctor(TabContents* contents)
202 : contents_(contents) {}
203
204 void Run(const GearsShortcutData2& shortcut_data, bool success) {
205 if (contents_)
206 contents_->OnGearsCreateShortcutDone(shortcut_data, success);
207 delete this;
208 }
209 void Cancel() {
210 contents_ = NULL;
211 }
212
213 private:
214 TabContents* contents_;
215};
216
[email protected]420ae012009-04-24 05:16:32217TabContents::TabContents(Profile* profile,
218 SiteInstance* site_instance,
219 int routing_id,
[email protected]34ac70502009-09-25 17:07:23220 const TabContents* base_tab_contents)
[email protected]b680ad22009-04-15 23:19:42221 : delegate_(NULL),
[email protected]66ba4932009-06-04 19:22:13222 ALLOW_THIS_IN_INITIALIZER_LIST(controller_(this, profile)),
223 ALLOW_THIS_IN_INITIALIZER_LIST(view_(
[email protected]d82ed61e2009-06-16 02:46:22224 TabContentsView::Create(this))),
225 ALLOW_THIS_IN_INITIALIZER_LIST(render_manager_(this, this)),
[email protected]fdd61c62009-04-22 19:22:57226 property_bag_(),
227 registrar_(),
[email protected]d82ed61e2009-06-16 02:46:22228 ALLOW_THIS_IN_INITIALIZER_LIST(printing_(*this)),
[email protected]fdd61c62009-04-22 19:22:57229 save_package_(),
230 cancelable_consumer_(),
231 autofill_manager_(),
232 password_manager_(),
233 plugin_installer_(),
[email protected]d82ed61e2009-06-16 02:46:22234 ALLOW_THIS_IN_INITIALIZER_LIST(fav_icon_helper_(this)),
[email protected]fdd61c62009-04-22 19:22:57235 select_file_dialog_(),
236 pending_install_(),
[email protected]d5f942ba2008-09-26 19:30:34237 is_loading_(false),
initial.commit09911bf2008-07-26 23:55:29238 is_crashed_(false),
[email protected]d5f942ba2008-09-26 19:30:34239 waiting_for_response_(false),
[email protected]d5f942ba2008-09-26 19:30:34240 max_page_id_(-1),
[email protected]fdd61c62009-04-22 19:22:57241 current_load_start_(),
242 load_state_(net::LOAD_STATE_IDLE),
243 load_state_host_(),
[email protected]094e5b22009-09-25 04:23:56244 upload_size_(0),
245 upload_position_(0),
[email protected]fdd61c62009-04-22 19:22:57246 received_page_title_(false),
247 is_starred_(false),
248 contents_mime_type_(),
249 encoding_(),
[email protected]332af7732009-03-11 13:21:35250 blocked_popups_(NULL),
[email protected]fdd61c62009-04-22 19:22:57251 infobar_delegates_(),
[email protected]fdd61c62009-04-22 19:22:57252 find_ui_active_(false),
253 find_op_aborted_(false),
254 current_find_request_id_(find_request_id_counter_++),
255 find_text_(),
[email protected]e491f1c2009-05-22 20:28:12256 last_search_case_sensitive_(false),
257 last_search_prepopulate_text_(NULL),
258 last_search_result_(),
[email protected]fdd61c62009-04-22 19:22:57259 capturing_contents_(false),
260 is_being_destroyed_(false),
261 notify_disconnection_(false),
262 history_requests_(),
263#if defined(OS_WIN)
264 message_box_active_(CreateEvent(NULL, TRUE, FALSE, NULL)),
265#endif
266 last_javascript_message_dismissal_(),
[email protected]c2e74fe82009-09-03 17:57:44267 suppress_javascript_messages_(false),
[email protected]7ab1e7d62009-10-14 23:32:01268 is_showing_before_unload_dialog_(false),
[email protected]c2e74fe82009-09-03 17:57:44269 opener_dom_ui_type_(DOMUIFactory::kNoDOMUI) {
[email protected]420ae012009-04-24 05:16:32270 pending_install_.page_id = 0;
271 pending_install_.callback_functor = NULL;
272
[email protected]16779842009-07-08 23:45:29273#if defined(OS_CHROMEOS)
[email protected]58dca552009-06-17 00:35:02274 // Make sure the thumbnailer is started before starting the render manager.
275 // The thumbnailer will want to listen for RVH creations, one of which will
276 // happen in RVHManager::Init.
277 ThumbnailGenerator* generator = g_browser_process->GetThumbnailGenerator();
278 if (generator)
279 generator->StartThumbnailing();
280#endif
281
[email protected]12636df2009-09-28 22:32:21282 render_manager_.Init(profile, site_instance, routing_id);
[email protected]420ae012009-04-24 05:16:32283
[email protected]34ac70502009-09-25 17:07:23284 // We have the initial size of the view be based on the size of the passed in
285 // tab contents (normally a tab from the same window).
286 view_->CreateView(base_tab_contents ?
287 base_tab_contents->view()->GetContainerSize() : gfx::Size());
[email protected]420ae012009-04-24 05:16:32288
289 // Register for notifications about all interested prefs change.
290 PrefService* prefs = profile->GetPrefs();
291 if (prefs) {
292 for (int i = 0; i < kPrefsToObserveLength; ++i)
293 prefs->AddPrefObserver(kPrefsToObserve[i], this);
294 }
295
296 // Register for notifications about URL starredness changing on any profile.
297 registrar_.Add(this, NotificationType::URLS_STARRED,
298 NotificationService::AllSources());
299 registrar_.Add(this, NotificationType::BOOKMARK_MODEL_LOADED,
300 NotificationService::AllSources());
301 registrar_.Add(this, NotificationType::RENDER_WIDGET_HOST_DESTROYED,
302 NotificationService::AllSources());
[email protected]d3ba77272009-09-03 00:06:09303#if defined(OS_LINUX)
304 registrar_.Add(this, NotificationType::BROWSER_THEME_CHANGED,
305 NotificationService::AllSources());
306#endif
[email protected]420ae012009-04-24 05:16:32307
308 // Keep a global copy of the previous search string (if any).
309 static string16 global_last_search = string16();
[email protected]e491f1c2009-05-22 20:28:12310 last_search_prepopulate_text_ = &global_last_search;
[email protected]840b1512009-07-21 16:53:46311
[email protected]c8865482009-07-23 20:40:10312 // Set-up the showing of the omnibox search infobar if applicable.
313 if (OmniboxSearchHint::IsEnabled(profile))
314 omnibox_search_hint_.reset(new OmniboxSearchHint(this));
315
[email protected]840b1512009-07-21 16:53:46316 view_->InitRendererPrefs(&renderer_preferences_);
[email protected]332af7732009-03-11 13:21:35317}
initial.commit09911bf2008-07-26 23:55:29318
319TabContents::~TabContents() {
[email protected]420ae012009-04-24 05:16:32320 is_being_destroyed_ = true;
321
[email protected]bfe4c1582009-10-02 18:11:09322 // We don't want any notifications while we're running our destructor.
[email protected]420ae012009-04-24 05:16:32323 registrar_.RemoveAll();
324
325 // Unregister the notifications of all observed prefs change.
326 PrefService* prefs = profile()->GetPrefs();
327 if (prefs) {
328 for (int i = 0; i < kPrefsToObserveLength; ++i)
329 prefs->RemovePrefObserver(kPrefsToObserve[i], this);
330 }
331
[email protected]420ae012009-04-24 05:16:32332 NotifyDisconnected();
[email protected]d82ed61e2009-06-16 02:46:22333 HungRendererDialog::HideForTabContents(this);
[email protected]420ae012009-04-24 05:16:32334
335 if (pending_install_.callback_functor)
336 pending_install_.callback_functor->Cancel();
337
338 // First cleanly close all child windows.
339 // TODO(mpcomplete): handle case if MaybeCloseChildWindows() already asked
340 // some of these to close. CloseWindows is async, so it might get called
341 // twice before it runs.
342 int size = static_cast<int>(child_windows_.size());
343 for (int i = size - 1; i >= 0; --i) {
344 ConstrainedWindow* window = child_windows_[i];
345 if (window)
346 window->CloseConstrainedWindow();
347 }
348
[email protected]66ba4932009-06-04 19:22:13349 if (blocked_popups_)
350 blocked_popups_->Destroy();
351
[email protected]420ae012009-04-24 05:16:32352 // Notify any observer that have a reference on this tab contents.
353 NotificationService::current()->Notify(
354 NotificationType::TAB_CONTENTS_DESTROYED,
355 Source<TabContents>(this),
356 NotificationService::NoDetails());
357
[email protected]1e5e74f2009-05-27 20:55:12358 // Notify any lasting InfobarDelegates that have not yet been removed that
359 // whatever infobar they were handling in this TabContents has closed,
360 // because the TabContents is going away entirely.
361 // This must happen after the TAB_CONTENTS_DESTROYED notification as the
362 // notification may trigger infobars calls that access their delegate. (and
363 // some implementations of InfoBarDelegate do delete themselves on
364 // InfoBarClosed()).
365 for (int i = 0; i < infobar_delegate_count(); ++i) {
366 InfoBarDelegate* delegate = GetInfoBarDelegateAt(i);
367 delegate->InfoBarClosed();
368 }
369 infobar_delegates_.clear();
370
[email protected]420ae012009-04-24 05:16:32371 // TODO(brettw) this should be moved to the view.
372#if defined(OS_WIN)
373 // If we still have a window handle, destroy it. GetNativeView can return
374 // NULL if this contents was part of a window that closed.
375 if (GetNativeView())
376 ::DestroyWindow(GetNativeView());
377#endif
[email protected]7ab1e7d62009-10-14 23:32:01378
379 // OnCloseStarted isn't called in unit tests.
380 if (!tab_close_start_time_.is_null()) {
381 UMA_HISTOGRAM_TIMES("Tab.Close",
382 base::TimeTicks::Now() - tab_close_start_time_);
383 }
initial.commit09911bf2008-07-26 23:55:29384}
385
[email protected]d5f942ba2008-09-26 19:30:34386// static
387void TabContents::RegisterUserPrefs(PrefService* prefs) {
[email protected]420ae012009-04-24 05:16:32388 prefs->RegisterBooleanPref(prefs::kAlternateErrorPagesEnabled, true);
389
390 WebPreferences pref_defaults;
391 prefs->RegisterBooleanPref(prefs::kWebKitJavascriptEnabled,
392 pref_defaults.javascript_enabled);
393 prefs->RegisterBooleanPref(prefs::kWebKitWebSecurityEnabled,
394 pref_defaults.web_security_enabled);
395 prefs->RegisterBooleanPref(
396 prefs::kWebKitJavascriptCanOpenWindowsAutomatically, true);
397 prefs->RegisterBooleanPref(prefs::kWebKitLoadsImagesAutomatically,
398 pref_defaults.loads_images_automatically);
399 prefs->RegisterBooleanPref(prefs::kWebKitPluginsEnabled,
400 pref_defaults.plugins_enabled);
401 prefs->RegisterBooleanPref(prefs::kWebKitDomPasteEnabled,
402 pref_defaults.dom_paste_enabled);
403 prefs->RegisterBooleanPref(prefs::kWebKitShrinksStandaloneImagesToFit,
404 pref_defaults.shrinks_standalone_images_to_fit);
[email protected]0df30122009-06-03 12:13:08405 prefs->RegisterStringPref(prefs::kWebKitInspectorSettings,
[email protected]8922e1f2009-10-03 05:01:26406 UTF8ToWide(pref_defaults.inspector_settings));
[email protected]420ae012009-04-24 05:16:32407 prefs->RegisterBooleanPref(prefs::kWebKitTextAreasAreResizable,
408 pref_defaults.text_areas_are_resizable);
409 prefs->RegisterBooleanPref(prefs::kWebKitJavaEnabled,
410 pref_defaults.java_enabled);
411
412 prefs->RegisterLocalizedStringPref(prefs::kAcceptLanguages,
413 IDS_ACCEPT_LANGUAGES);
414 prefs->RegisterLocalizedStringPref(prefs::kDefaultCharset,
415 IDS_DEFAULT_ENCODING);
416 prefs->RegisterLocalizedBooleanPref(prefs::kWebKitStandardFontIsSerif,
417 IDS_STANDARD_FONT_IS_SERIF);
418 prefs->RegisterLocalizedStringPref(prefs::kWebKitFixedFontFamily,
419 IDS_FIXED_FONT_FAMILY);
420 prefs->RegisterLocalizedStringPref(prefs::kWebKitSerifFontFamily,
421 IDS_SERIF_FONT_FAMILY);
422 prefs->RegisterLocalizedStringPref(prefs::kWebKitSansSerifFontFamily,
423 IDS_SANS_SERIF_FONT_FAMILY);
424 prefs->RegisterLocalizedStringPref(prefs::kWebKitCursiveFontFamily,
425 IDS_CURSIVE_FONT_FAMILY);
426 prefs->RegisterLocalizedStringPref(prefs::kWebKitFantasyFontFamily,
427 IDS_FANTASY_FONT_FAMILY);
428 prefs->RegisterLocalizedIntegerPref(prefs::kWebKitDefaultFontSize,
429 IDS_DEFAULT_FONT_SIZE);
430 prefs->RegisterLocalizedIntegerPref(prefs::kWebKitDefaultFixedFontSize,
431 IDS_DEFAULT_FIXED_FONT_SIZE);
432 prefs->RegisterLocalizedIntegerPref(prefs::kWebKitMinimumFontSize,
433 IDS_MINIMUM_FONT_SIZE);
434 prefs->RegisterLocalizedIntegerPref(prefs::kWebKitMinimumLogicalFontSize,
435 IDS_MINIMUM_LOGICAL_FONT_SIZE);
436 prefs->RegisterLocalizedBooleanPref(prefs::kWebKitUsesUniversalDetector,
437 IDS_USES_UNIVERSAL_DETECTOR);
438 prefs->RegisterLocalizedStringPref(prefs::kStaticEncodings,
439 IDS_STATIC_ENCODING_LIST);
initial.commit09911bf2008-07-26 23:55:29440}
441
[email protected]13367f72009-07-08 16:39:02442// Returns true if contains content rendered by an extension.
443bool TabContents::HostsExtension() const {
444 return GetURL().SchemeIs(chrome::kExtensionScheme);
[email protected]d5f942ba2008-09-26 19:30:34445}
446
[email protected]6e95706d2009-04-23 22:55:01447PasswordManager* TabContents::GetPasswordManager() {
448 if (password_manager_.get() == NULL)
449 password_manager_.reset(new PasswordManager(this));
450 return password_manager_.get();
451}
452
453PluginInstaller* TabContents::GetPluginInstaller() {
454 if (plugin_installer_.get() == NULL)
455 plugin_installer_.reset(new PluginInstaller(this));
456 return plugin_installer_.get();
457}
458
[email protected]d5f942ba2008-09-26 19:30:34459const GURL& TabContents::GetURL() const {
460 // We may not have a navigation entry yet
[email protected]ce3fa3c2009-04-20 19:55:57461 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]ebe89e062009-08-13 23:16:54462 return entry ? entry->virtual_url() : GURL::EmptyGURL();
[email protected]d5f942ba2008-09-26 19:30:34463}
464
[email protected]96d185d2009-04-24 03:28:54465const string16& TabContents::GetTitle() const {
[email protected]4c6092c5b2009-06-06 00:23:55466 // Transient entries take precedence. They are used for interstitial pages
467 // that are shown on top of existing pages.
468 NavigationEntry* entry = controller_.GetTransientEntry();
469 if (entry)
470 return entry->GetTitleForDisplay(&controller_);
471
[email protected]96d185d2009-04-24 03:28:54472 DOMUI* our_dom_ui = render_manager_.pending_dom_ui() ?
473 render_manager_.pending_dom_ui() : render_manager_.dom_ui();
474 if (our_dom_ui) {
475 // Don't override the title in view source mode.
[email protected]4c6092c5b2009-06-06 00:23:55476 entry = controller_.GetActiveEntry();
[email protected]96d185d2009-04-24 03:28:54477 if (!(entry && entry->IsViewSourceMode())) {
478 // Give the DOM UI the chance to override our title.
479 const string16& title = our_dom_ui->overridden_title();
480 if (!title.empty())
481 return title;
482 }
483 }
484
485 // We use the title for the last committed entry rather than a pending
486 // navigation entry. For example, when the user types in a URL, we want to
487 // keep the old page's title until the new load has committed and we get a new
488 // title.
[email protected]96d185d2009-04-24 03:28:54489 entry = controller_.GetLastCommittedEntry();
490 if (entry)
491 return entry->GetTitleForDisplay(&controller_);
[email protected]96d185d2009-04-24 03:28:54492 return EmptyString16();
493}
494
[email protected]d5f942ba2008-09-26 19:30:34495int32 TabContents::GetMaxPageID() {
496 if (GetSiteInstance())
497 return GetSiteInstance()->max_page_id();
498 else
499 return max_page_id_;
500}
501
502void TabContents::UpdateMaxPageID(int32 page_id) {
503 // Ensure both the SiteInstance and RenderProcessHost update their max page
[email protected]57c6a652009-05-04 07:58:34504 // IDs in sync. Only TabContents will also have site instances, except during
[email protected]d5f942ba2008-09-26 19:30:34505 // testing.
506 if (GetSiteInstance())
507 GetSiteInstance()->UpdateMaxPageID(page_id);
[email protected]57c6a652009-05-04 07:58:34508 process()->UpdateMaxPageID(page_id);
[email protected]d5f942ba2008-09-26 19:30:34509}
510
[email protected]96d185d2009-04-24 03:28:54511SiteInstance* TabContents::GetSiteInstance() const {
512 return render_manager_.current_host()->site_instance();
513}
514
[email protected]d5f942ba2008-09-26 19:30:34515const std::wstring TabContents::GetDefaultTitle() const {
516 return l10n_util::GetString(IDS_DEFAULT_TAB_TITLE);
517}
518
[email protected]96d185d2009-04-24 03:28:54519bool TabContents::ShouldDisplayURL() {
520 // Don't hide the url in view source mode.
521 NavigationEntry* entry = controller_.GetActiveEntry();
522 if (entry && entry->IsViewSourceMode())
523 return true;
[email protected]83e3895b2009-06-11 00:07:16524
[email protected]96d185d2009-04-24 03:28:54525 DOMUI* dom_ui = GetDOMUIForCurrentState();
526 if (dom_ui)
527 return !dom_ui->should_hide_url();
528 return true;
529}
530
[email protected]d5f942ba2008-09-26 19:30:34531SkBitmap TabContents::GetFavIcon() const {
532 // Like GetTitle(), we also want to use the favicon for the last committed
533 // entry rather than a pending navigation entry.
[email protected]ce3fa3c2009-04-20 19:55:57534 NavigationEntry* entry = controller_.GetTransientEntry();
[email protected]cbab76d2008-10-13 22:42:47535 if (entry)
536 return entry->favicon().bitmap();
537
[email protected]ce3fa3c2009-04-20 19:55:57538 entry = controller_.GetLastCommittedEntry();
[email protected]d5f942ba2008-09-26 19:30:34539 if (entry)
540 return entry->favicon().bitmap();
[email protected]d5f942ba2008-09-26 19:30:34541 return SkBitmap();
542}
543
[email protected]0b303cc2009-09-28 22:35:15544bool TabContents::FavIconIsValid() const {
545 NavigationEntry* entry = controller_.GetTransientEntry();
546 if (entry)
547 return entry->favicon().is_valid();
548
549 entry = controller_.GetLastCommittedEntry();
550 if (entry)
551 return entry->favicon().is_valid();
552
553 return false;
554}
555
[email protected]96d185d2009-04-24 03:28:54556bool TabContents::ShouldDisplayFavIcon() {
557 // Always display a throbber during pending loads.
558 if (controller_.GetLastCommittedEntry() && controller_.pending_entry())
559 return true;
560
561 DOMUI* dom_ui = GetDOMUIForCurrentState();
562 if (dom_ui)
563 return !dom_ui->hide_favicon();
564 return true;
565}
566
[email protected]96d185d2009-04-24 03:28:54567std::wstring TabContents::GetStatusText() const {
568 if (!is_loading() || load_state_ == net::LOAD_STATE_IDLE)
569 return std::wstring();
570
571 switch (load_state_) {
572 case net::LOAD_STATE_WAITING_FOR_CACHE:
573 return l10n_util::GetString(IDS_LOAD_STATE_WAITING_FOR_CACHE);
574 case net::LOAD_STATE_RESOLVING_PROXY_FOR_URL:
575 return l10n_util::GetString(IDS_LOAD_STATE_RESOLVING_PROXY_FOR_URL);
576 case net::LOAD_STATE_RESOLVING_HOST:
577 return l10n_util::GetString(IDS_LOAD_STATE_RESOLVING_HOST);
578 case net::LOAD_STATE_CONNECTING:
579 return l10n_util::GetString(IDS_LOAD_STATE_CONNECTING);
580 case net::LOAD_STATE_SENDING_REQUEST:
[email protected]094e5b22009-09-25 04:23:56581 if (upload_size_)
582 return l10n_util::GetStringF(
583 IDS_LOAD_STATE_SENDING_REQUEST_WITH_PROGRESS,
584 static_cast<int>((100 * upload_position_) / upload_size_));
585 else
586 return l10n_util::GetString(IDS_LOAD_STATE_SENDING_REQUEST);
[email protected]96d185d2009-04-24 03:28:54587 case net::LOAD_STATE_WAITING_FOR_RESPONSE:
588 return l10n_util::GetStringF(IDS_LOAD_STATE_WAITING_FOR_RESPONSE,
589 load_state_host_);
590 // Ignore net::LOAD_STATE_READING_RESPONSE and net::LOAD_STATE_IDLE
591 case net::LOAD_STATE_IDLE:
592 case net::LOAD_STATE_READING_RESPONSE:
593 break;
594 }
595
596 return std::wstring();
597}
598
[email protected]d5f942ba2008-09-26 19:30:34599void TabContents::SetIsCrashed(bool state) {
600 if (state == is_crashed_)
601 return;
602
603 is_crashed_ = state;
[email protected]c9cd2222009-05-06 05:16:50604 NotifyNavigationStateChanged(INVALIDATE_TAB);
[email protected]d5f942ba2008-09-26 19:30:34605}
606
[email protected]ba69a7d2009-09-28 21:09:56607void TabContents::SetPageActionEnabled(const ExtensionAction* page_action,
[email protected]d7eaf572009-07-01 21:57:00608 bool enable,
609 const std::string& title,
610 int icon_id) {
[email protected]ce5c4502009-05-06 16:46:11611 DCHECK(page_action);
[email protected]744ef172009-10-16 21:53:46612 ExtensionActionState* state = GetOrCreatePageActionState(page_action);
613 state->set_hidden(!enable);
614 state->set_title(title);
615 state->set_icon_index(icon_id);
616 state->set_icon(NULL);
[email protected]ce5c4502009-05-06 16:46:11617}
618
[email protected]ba69a7d2009-09-28 21:09:56619const ExtensionActionState* TabContents::GetPageActionState(
620 const ExtensionAction* page_action) {
[email protected]744ef172009-10-16 21:53:46621 if (page_actions_.end() == page_actions_.find(page_action))
[email protected]d7eaf572009-07-01 21:57:00622 return NULL;
[email protected]ce5c4502009-05-06 16:46:11623
[email protected]744ef172009-10-16 21:53:46624 return page_actions_[page_action].get();
625}
626
627ExtensionActionState* TabContents::GetOrCreatePageActionState(
628 const ExtensionAction* page_action) {
629 if (page_actions_.end() == page_actions_.find(page_action)) {
630 page_actions_[page_action].reset(
631 new ExtensionActionState(page_action->title(), 0));
632 }
633
634 return page_actions_[page_action].get();
635}
636
637void TabContents::PageActionStateChanged() {
638 NotifyNavigationStateChanged(TabContents::INVALIDATE_PAGE_ACTIONS);
[email protected]d7eaf572009-07-01 21:57:00639}
[email protected]ce5c4502009-05-06 16:46:11640
[email protected]d5f942ba2008-09-26 19:30:34641void TabContents::NotifyNavigationStateChanged(unsigned changed_flags) {
642 if (delegate_)
643 delegate_->NavigationStateChanged(this, changed_flags);
644}
645
[email protected]96d185d2009-04-24 03:28:54646void TabContents::DidBecomeSelected() {
647 controller_.SetActive(true);
[email protected]789e9152009-08-04 21:59:43648 if (render_widget_host_view()) {
[email protected]96d185d2009-04-24 03:28:54649 render_widget_host_view()->DidBecomeSelected();
[email protected]789e9152009-08-04 21:59:43650#if defined(OS_MACOSX)
651 render_widget_host_view()->SetActive(true);
652#endif
653 }
[email protected]96d185d2009-04-24 03:28:54654
[email protected]76543b92009-08-31 17:27:45655 WebCacheManager::GetInstance()->ObserveActivity(process()->id());
[email protected]96d185d2009-04-24 03:28:54656}
657
658void TabContents::WasHidden() {
659 if (!capturing_contents()) {
660 // |render_view_host()| can be NULL if the user middle clicks a link to open
661 // a tab in then background, then closes the tab before selecting it. This
662 // is because closing the tab calls TabContents::Destroy(), which removes
663 // the |render_view_host()|; then when we actually destroy the window,
664 // OnWindowPosChanged() notices and calls HideContents() (which calls us).
665 if (render_widget_host_view())
666 render_widget_host_view()->WasHidden();
[email protected]96d185d2009-04-24 03:28:54667 }
668
669 NotificationService::current()->Notify(
670 NotificationType::TAB_CONTENTS_HIDDEN,
671 Source<TabContents>(this),
672 NotificationService::NoDetails());
673}
674
[email protected]d5f942ba2008-09-26 19:30:34675void TabContents::Activate() {
676 if (delegate_)
677 delegate_->ActivateContents(this);
678}
679
[email protected]96d185d2009-04-24 03:28:54680void TabContents::ShowContents() {
681 if (render_widget_host_view())
682 render_widget_host_view()->DidBecomeSelected();
[email protected]96d185d2009-04-24 03:28:54683}
684
685void TabContents::HideContents() {
686 // TODO(pkasting): http://b/1239839 Right now we purposefully don't call
687 // our superclass HideContents(), because some callers want to be very picky
688 // about the order in which these get called. In addition to making the code
689 // here practically impossible to understand, this also means we end up
690 // calling TabContents::WasHidden() twice if callers call both versions of
[email protected]57c6a652009-05-04 07:58:34691 // HideContents() on a TabContents.
[email protected]96d185d2009-04-24 03:28:54692 WasHidden();
693}
694
[email protected]c0588052008-10-27 23:01:50695void TabContents::OpenURL(const GURL& url, const GURL& referrer,
[email protected]d5f942ba2008-09-26 19:30:34696 WindowOpenDisposition disposition,
697 PageTransition::Type transition) {
698 if (delegate_)
[email protected]c0588052008-10-27 23:01:50699 delegate_->OpenURLFromTab(this, url, referrer, disposition, transition);
[email protected]d5f942ba2008-09-26 19:30:34700}
701
702bool TabContents::NavigateToPendingEntry(bool reload) {
[email protected]96d185d2009-04-24 03:28:54703 const NavigationEntry& entry = *controller_.pending_entry();
704
705 RenderViewHost* dest_render_view_host = render_manager_.Navigate(entry);
706 if (!dest_render_view_host)
707 return false; // Unable to create the desired render view host.
708
709 // Tell DevTools agent that it is attached prior to the navigation.
[email protected]d9f9b792009-06-24 13:17:12710 DevToolsManager* devtools_manager = DevToolsManager::GetInstance();
[email protected]7e8e6b62009-05-08 11:28:32711 if (devtools_manager) { // NULL in unit tests.
[email protected]684b4ba22009-05-07 06:27:41712 devtools_manager->OnNavigatingToPendingEntry(
713 render_view_host(),
714 dest_render_view_host,
715 controller_.pending_entry()->url());
[email protected]7e8e6b62009-05-08 11:28:32716 }
[email protected]96d185d2009-04-24 03:28:54717
718 // Used for page load time metrics.
719 current_load_start_ = base::TimeTicks::Now();
720
721 // Navigate in the desired RenderViewHost.
[email protected]056de2d2009-06-26 16:41:34722 ViewMsg_Navigate_Params navigate_params;
723 MakeNavigateParams(entry, reload, &navigate_params);
724 dest_render_view_host->Navigate(navigate_params);
[email protected]96d185d2009-04-24 03:28:54725
726 if (entry.page_id() == -1) {
727 // HACK!! This code suppresses javascript: URLs from being added to
728 // session history, which is what we want to do for javascript: URLs that
729 // do not generate content. What we really need is a message from the
730 // renderer telling us that a new page was not created. The same message
731 // could be used for mailto: URLs and the like.
732 if (entry.url().SchemeIs(chrome::kJavaScriptScheme))
733 return false;
734 }
735
736 // Clear any provisional password saves - this stops password infobars
737 // showing up on pages the user navigates to while the right page is
738 // loading.
739 GetPasswordManager()->ClearProvisionalSave();
740
741 if (reload && !profile()->IsOffTheRecord()) {
[email protected]0189bc722009-08-28 21:56:48742 FaviconService* favicon_service =
743 profile()->GetFaviconService(Profile::IMPLICIT_ACCESS);
744 if (favicon_service)
745 favicon_service->SetFaviconOutOfDateForPage(entry.url());
[email protected]96d185d2009-04-24 03:28:54746 }
747
[email protected]d5f942ba2008-09-26 19:30:34748 return true;
749}
750
[email protected]96d185d2009-04-24 03:28:54751void TabContents::Stop() {
752 render_manager_.Stop();
753 printing_.Stop();
754}
755
756void TabContents::Cut() {
757 render_view_host()->Cut();
758}
759
760void TabContents::Copy() {
761 render_view_host()->Copy();
762}
763
[email protected]c0cc3092009-09-12 08:27:27764#if defined(OS_MACOSX)
[email protected]a954bf72009-09-12 07:30:35765void TabContents::CopyToFindPboard() {
766 render_view_host()->CopyToFindPboard();
767}
[email protected]c0cc3092009-09-12 08:27:27768#endif
[email protected]a954bf72009-09-12 07:30:35769
[email protected]96d185d2009-04-24 03:28:54770void TabContents::Paste() {
771 render_view_host()->Paste();
772}
773
774void TabContents::DisassociateFromPopupCount() {
775 render_view_host()->DisassociateFromPopupCount();
776}
777
778TabContents* TabContents::Clone() {
779 // We create a new SiteInstance so that the new tab won't share processes
780 // with the old one. This can be changed in the future if we need it to share
781 // processes for some reason.
[email protected]420ae012009-04-24 05:16:32782 TabContents* tc = new TabContents(profile(),
[email protected]96d185d2009-04-24 03:28:54783 SiteInstance::CreateSiteInstance(profile()),
[email protected]12636df2009-09-28 22:32:21784 MSG_ROUTING_NONE, this);
[email protected]96d185d2009-04-24 03:28:54785 tc->controller().CopyStateFrom(controller_);
786 return tc;
787}
788
[email protected]420ae012009-04-24 05:16:32789void TabContents::CreateShortcut() {
790 NavigationEntry* entry = controller_.GetLastCommittedEntry();
791 if (!entry)
792 return;
793
[email protected]70b8d6b2009-08-20 16:56:24794#if defined(OS_LINUX) && !defined(TOOLKIT_VIEWS)
[email protected]0b303cc2009-09-28 22:35:15795 SkBitmap bitmap;
796 if (FavIconIsValid())
797 bitmap = GetFavIcon();
[email protected]0ba5c5e2009-08-20 16:47:13798 CreateApplicationShortcutsDialogGtk::Show(view()->GetTopLevelNativeWindow(),
[email protected]0b303cc2009-09-28 22:35:15799 GetURL(), GetTitle(), bitmap);
[email protected]b96aa932009-08-12 21:34:49800#else
[email protected]420ae012009-04-24 05:16:32801 // We only allow one pending install request. By resetting the page id we
802 // effectively cancel the pending install request.
803 pending_install_.page_id = entry->page_id();
804 pending_install_.icon = GetFavIcon();
[email protected]00e3f152009-10-20 19:25:42805 pending_install_.title = GetTitle();
[email protected]420ae012009-04-24 05:16:32806 pending_install_.url = GetURL();
807 if (pending_install_.callback_functor) {
808 pending_install_.callback_functor->Cancel();
809 pending_install_.callback_functor = NULL;
810 }
811 DCHECK(!pending_install_.icon.isNull()) << "Menu item should be disabled.";
812 if (pending_install_.title.empty())
[email protected]00e3f152009-10-20 19:25:42813 pending_install_.title = UTF8ToUTF16(GetURL().spec());
[email protected]420ae012009-04-24 05:16:32814
815 // Request the application info. When done OnDidGetApplicationInfo is invoked
816 // and we'll create the shortcut.
817 render_view_host()->GetApplicationInfo(pending_install_.page_id);
[email protected]b96aa932009-08-12 21:34:49818#endif
[email protected]420ae012009-04-24 05:16:32819}
820
[email protected]4d677202009-07-19 07:37:12821void TabContents::ShowPageInfo(const GURL& url,
822 const NavigationEntry::SSLStatus& ssl,
823 bool show_history) {
824 if (!delegate_)
825 return;
826
[email protected]bb678332009-07-21 00:15:50827 delegate_->ShowPageInfo(profile(), url, ssl, show_history);
[email protected]4d677202009-07-19 07:37:12828}
829
initial.commit09911bf2008-07-26 23:55:29830ConstrainedWindow* TabContents::CreateConstrainedDialog(
[email protected]e8382172009-06-19 22:16:28831 ConstrainedWindowDelegate* delegate) {
initial.commit09911bf2008-07-26 23:55:29832 ConstrainedWindow* window =
[email protected]e8382172009-06-19 22:16:28833 ConstrainedWindow::CreateConstrainedDialog(this, delegate);
initial.commit09911bf2008-07-26 23:55:29834 child_windows_.push_back(window);
835 return window;
836}
837
838void TabContents::AddNewContents(TabContents* new_contents,
839 WindowOpenDisposition disposition,
840 const gfx::Rect& initial_pos,
[email protected]c88a70fe2009-05-05 20:00:22841 bool user_gesture,
842 const GURL& creator_url) {
initial.commit09911bf2008-07-26 23:55:29843 if (!delegate_)
844 return;
845
[email protected]35f7d212009-04-29 21:19:27846 if ((disposition == NEW_POPUP) && !user_gesture &&
847 !CommandLine::ForCurrentProcess()->HasSwitch(
848 switches::kDisablePopupBlocking)) {
[email protected]2c4410d2009-05-06 23:46:22849 // Unrequested popups from normal pages are constrained unless they're in
[email protected]b6c874582009-05-08 19:38:31850 // the whitelist. The popup owner will handle checking this.
851 delegate_->GetConstrainingContents(this)->AddPopup(new_contents,
852 initial_pos,
853 creator_url.is_valid() ? creator_url.host() : std::string());
854 } else {
[email protected]304f8792009-08-19 21:28:09855#if defined(OS_CHROMEOS)
856 if (disposition == NEW_POPUP) {
857 // If the popup is bigger than a given factor of the screen, then
858 // turn it into a foreground tab (on chrome os only)
859 GdkScreen* screen = gdk_screen_get_default();
860 int max_width = gdk_screen_get_width(screen) * kMaxWidthFactor;
861 int max_height = gdk_screen_get_height(screen) * kMaxHeightFactor;
862 if (initial_pos.width() > max_width ||
863 initial_pos.height() > max_height) {
864 disposition = NEW_FOREGROUND_TAB;
865 }
866 }
867#endif
868
[email protected]0aa55312008-10-17 21:53:08869 new_contents->DisassociateFromPopupCount();
initial.commit09911bf2008-07-26 23:55:29870 delegate_->AddNewContents(this, new_contents, disposition, initial_pos,
871 user_gesture);
[email protected]7f37dd92009-09-15 16:35:49872 NotificationService::current()->Notify(
873 NotificationType::TAB_ADDED,
874 Source<TabContentsDelegate>(delegate_),
875 Details<TabContents>(this));
initial.commit09911bf2008-07-26 23:55:29876 }
[email protected]b6c874582009-05-08 19:38:31877 PopupNotificationVisibilityChanged(ShowingBlockedPopupNotification());
initial.commit09911bf2008-07-26 23:55:29878}
879
initial.commit09911bf2008-07-26 23:55:29880void TabContents::CloseAllSuppressedPopups() {
[email protected]d6598c052008-11-05 19:03:25881 if (blocked_popups_)
[email protected]2c4410d2009-05-06 23:46:22882 blocked_popups_->CloseAll();
initial.commit09911bf2008-07-26 23:55:29883}
884
[email protected]912256b32009-09-18 09:47:35885void TabContents::ExecuteCode(int request_id, const std::string& extension_id,
886 bool is_js_code, const std::string& code_string) {
887 render_view_host()->Send(
888 new ViewMsg_ExecuteCode(render_view_host()->routing_id(), request_id,
889 extension_id, is_js_code, code_string));
890}
891
[email protected]96d185d2009-04-24 03:28:54892void TabContents::PopupNotificationVisibilityChanged(bool visible) {
893 render_view_host()->PopupNotificationVisibilityChanged(visible);
894}
895
[email protected]5c9e97a2009-09-09 23:48:30896gfx::NativeView TabContents::GetContentNativeView() const {
[email protected]96d185d2009-04-24 03:28:54897 return view_->GetContentNativeView();
898}
899
900gfx::NativeView TabContents::GetNativeView() const {
901 return view_->GetNativeView();
902}
903
904void TabContents::GetContainerBounds(gfx::Rect *out) const {
905 view_->GetContainerBounds(out);
906}
907
908void TabContents::Focus() {
909 view_->Focus();
910}
911
[email protected]90daadb42009-06-08 21:27:28912void TabContents::FocusThroughTabTraversal(bool reverse) {
[email protected]7e383692009-06-12 19:14:54913 if (showing_interstitial_page()) {
914 render_manager_.interstitial_page()->FocusThroughTabTraversal(reverse);
915 return;
916 }
[email protected]96d185d2009-04-24 03:28:54917 render_view_host()->SetInitialFocus(reverse);
918}
919
920bool TabContents::FocusLocationBarByDefault() {
921 DOMUI* dom_ui = GetDOMUIForCurrentState();
922 if (dom_ui)
923 return dom_ui->focus_location_bar_by_default();
[email protected]3e3f0eb2009-06-22 18:33:43924 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]e0d481582009-09-15 21:06:25925 if (entry && entry->url() == GURL(chrome::kAboutBlankURL))
[email protected]3e3f0eb2009-06-22 18:33:43926 return true;
[email protected]96d185d2009-04-24 03:28:54927 return false;
928}
929
[email protected]616ed5a2008-11-21 22:27:24930void TabContents::AddInfoBar(InfoBarDelegate* delegate) {
931 // Look through the existing InfoBarDelegates we have for a match. If we've
932 // already got one that matches, then we don't add the new one.
[email protected]f86a07022008-11-25 01:06:05933 for (int i = 0; i < infobar_delegate_count(); ++i) {
[email protected]616ed5a2008-11-21 22:27:24934 if (GetInfoBarDelegateAt(i)->EqualsDelegate(delegate))
935 return;
936 }
937
938 infobar_delegates_.push_back(delegate);
[email protected]bfd04a62009-02-01 18:16:56939 NotificationService::current()->Notify(
940 NotificationType::TAB_CONTENTS_INFOBAR_ADDED,
941 Source<TabContents>(this),
942 Details<InfoBarDelegate>(delegate));
[email protected]616ed5a2008-11-21 22:27:24943
944 // Add ourselves as an observer for navigations the first time a delegate is
945 // added. We use this notification to expire InfoBars that need to expire on
946 // page transitions.
947 if (infobar_delegates_.size() == 1) {
[email protected]bfd04a62009-02-01 18:16:56948 registrar_.Add(this, NotificationType::NAV_ENTRY_COMMITTED,
[email protected]ce3fa3c2009-04-20 19:55:57949 Source<NavigationController>(&controller_));
[email protected]616ed5a2008-11-21 22:27:24950 }
951}
952
953void TabContents::RemoveInfoBar(InfoBarDelegate* delegate) {
954 std::vector<InfoBarDelegate*>::iterator it =
955 find(infobar_delegates_.begin(), infobar_delegates_.end(), delegate);
956 if (it != infobar_delegates_.end()) {
957 InfoBarDelegate* delegate = *it;
[email protected]bfd04a62009-02-01 18:16:56958 NotificationService::current()->Notify(
959 NotificationType::TAB_CONTENTS_INFOBAR_REMOVED,
960 Source<TabContents>(this),
961 Details<InfoBarDelegate>(delegate));
[email protected]f86a07022008-11-25 01:06:05962 infobar_delegates_.erase(it);
[email protected]616ed5a2008-11-21 22:27:24963
[email protected]6a02963e2009-01-06 16:58:03964 // Remove ourselves as an observer if we are tracking no more InfoBars.
965 if (infobar_delegates_.empty()) {
[email protected]bfd04a62009-02-01 18:16:56966 registrar_.Remove(this, NotificationType::NAV_ENTRY_COMMITTED,
[email protected]ce3fa3c2009-04-20 19:55:57967 Source<NavigationController>(&controller_));
[email protected]6a02963e2009-01-06 16:58:03968 }
[email protected]616ed5a2008-11-21 22:27:24969 }
970}
[email protected]3edd9522009-03-04 22:19:41971
[email protected]f81821c2009-08-07 01:29:47972void TabContents::ReplaceInfoBar(InfoBarDelegate* old_delegate,
973 InfoBarDelegate* new_delegate) {
974 std::vector<InfoBarDelegate*>::iterator it =
975 find(infobar_delegates_.begin(), infobar_delegates_.end(), old_delegate);
976 DCHECK(it != infobar_delegates_.end());
977
978 // Notify the container about the change of plans.
979 scoped_ptr<std::pair<InfoBarDelegate*, InfoBarDelegate*> > details(
980 new std::pair<InfoBarDelegate*, InfoBarDelegate*>(
981 old_delegate, new_delegate));
982 NotificationService::current()->Notify(
983 NotificationType::TAB_CONTENTS_INFOBAR_REPLACED,
984 Source<TabContents>(this),
985 Details<std::pair<InfoBarDelegate*, InfoBarDelegate*> >(details.get()));
986
987 // Remove the old one.
988 infobar_delegates_.erase(it);
989
990 // Add the new one.
991 infobar_delegates_.push_back(new_delegate);
992}
993
[email protected]20ede072009-10-06 00:28:35994bool TabContents::ShouldShowBookmarkBar() {
[email protected]96d185d2009-04-24 03:28:54995 // See GetDOMUIForCurrentState() comment for more info. This case is very
996 // similar, but for non-first loads, we want to use the committed entry. This
997 // is so the bookmarks bar disappears at the same time the page does.
998 if (controller_.GetLastCommittedEntry()) {
999 // Not the first load, always use the committed DOM UI.
1000 if (render_manager_.dom_ui())
1001 return render_manager_.dom_ui()->force_bookmark_bar_visible();
1002 return false; // Default.
1003 }
1004
1005 // When it's the first load, we know either the pending one or the committed
1006 // one will have the DOM UI in it (see GetDOMUIForCurrentState), and only one
1007 // of them will be valid, so we can just check both.
1008 if (render_manager_.pending_dom_ui())
1009 return render_manager_.pending_dom_ui()->force_bookmark_bar_visible();
1010 if (render_manager_.dom_ui())
1011 return render_manager_.dom_ui()->force_bookmark_bar_visible();
1012 return false; // Default.
1013}
1014
[email protected]d8f16ae2009-08-30 03:47:261015bool TabContents::IsExtensionShelfAlwaysVisible() {
1016 // See GetDOMUIForCurrentState() comment for more info. This case is very
1017 // similar, but for non-first loads, we want to use the committed entry. This
1018 // is so the bookmarks bar disappears at the same time the page does.
1019 if (controller_.GetLastCommittedEntry()) {
1020 // Not the first load, always use the committed DOM UI.
1021 if (render_manager_.dom_ui())
1022 return render_manager_.dom_ui()->force_extension_shelf_visible();
1023 return false; // Default.
1024 }
1025
1026 // When it's the first load, we know either the pending one or the committed
1027 // one will have the DOM UI in it (see GetDOMUIForCurrentState), and only one
1028 // of them will be valid, so we can just check both.
1029 if (render_manager_.pending_dom_ui())
1030 return render_manager_.pending_dom_ui()->force_extension_shelf_visible();
1031 if (render_manager_.dom_ui())
1032 return render_manager_.dom_ui()->force_extension_shelf_visible();
1033 return false; // Default.
1034}
1035
[email protected]3edd9522009-03-04 22:19:411036void TabContents::ToolbarSizeChanged(bool is_animating) {
1037 TabContentsDelegate* d = delegate();
1038 if (d)
1039 d->ToolbarSizeChanged(this, is_animating);
1040}
[email protected]616ed5a2008-11-21 22:27:241041
[email protected]d5f942ba2008-09-26 19:30:341042void TabContents::OnStartDownload(DownloadItem* download) {
1043 DCHECK(download);
[email protected]d5f942ba2008-09-26 19:30:341044
1045 // Download in a constrained popup is shown in the tab that opened it.
[email protected]b6c874582009-05-08 19:38:311046 TabContents* tab_contents = delegate()->GetConstrainingContents(this);
[email protected]d5f942ba2008-09-26 19:30:341047
[email protected]59560e0b2009-06-04 03:30:221048 if (tab_contents && tab_contents->delegate())
1049 tab_contents->delegate()->OnStartDownload(download);
[email protected]3edd9522009-03-04 22:19:411050}
[email protected]3edd9522009-03-04 22:19:411051
[email protected]d5f942ba2008-09-26 19:30:341052void TabContents::WillClose(ConstrainedWindow* window) {
1053 ConstrainedWindowList::iterator it =
1054 find(child_windows_.begin(), child_windows_.end(), window);
1055 if (it != child_windows_.end())
1056 child_windows_.erase(it);
[email protected]66ba4932009-06-04 19:22:131057}
[email protected]d5f942ba2008-09-26 19:30:341058
[email protected]66ba4932009-06-04 19:22:131059void TabContents::WillCloseBlockedPopupContainer(
1060 BlockedPopupContainer* container) {
1061 DCHECK(blocked_popups_ == container);
1062 blocked_popups_ = NULL;
[email protected]d5f942ba2008-09-26 19:30:341063}
1064
[email protected]d5f942ba2008-09-26 19:30:341065void TabContents::DidMoveOrResize(ConstrainedWindow* window) {
[email protected]332af7732009-03-11 13:21:351066#if defined(OS_WIN)
[email protected]92edc472009-02-10 20:32:061067 UpdateWindow(GetNativeView());
[email protected]332af7732009-03-11 13:21:351068#endif
[email protected]d5f942ba2008-09-26 19:30:341069}
1070
[email protected]e491f1c2009-05-22 20:28:121071void TabContents::StartFinding(string16 find_text,
1072 bool forward_direction,
1073 bool case_sensitive) {
[email protected]6e95706d2009-04-23 22:55:011074 // If find_text is empty, it means FindNext was pressed with a keyboard
1075 // shortcut so unless we have something to search for we return early.
[email protected]e491f1c2009-05-22 20:28:121076 if (find_text.empty() && find_text_.empty()) {
1077 if (last_search_prepopulate_text_->empty())
1078 return;
1079 // Try whatever we searched for last in any tab.
1080 find_text = *last_search_prepopulate_text_;
1081 }
[email protected]6e95706d2009-04-23 22:55:011082
1083 // This is a FindNext operation if we are searching for the same text again,
1084 // or if the passed in search text is empty (FindNext keyboard shortcut). The
1085 // exception to this is if the Find was aborted (then we don't want FindNext
1086 // because the highlighting has been cleared and we need it to reappear). We
1087 // therefore treat FindNext after an aborted Find operation as a full fledged
1088 // Find.
1089 bool find_next = (find_text_ == find_text || find_text.empty()) &&
[email protected]e491f1c2009-05-22 20:28:121090 (last_search_case_sensitive_ == case_sensitive) &&
[email protected]6e95706d2009-04-23 22:55:011091 !find_op_aborted_;
1092 if (!find_next)
1093 current_find_request_id_ = find_request_id_counter_++;
1094
1095 if (!find_text.empty())
1096 find_text_ = find_text;
[email protected]e491f1c2009-05-22 20:28:121097 last_search_case_sensitive_ = case_sensitive;
[email protected]6e95706d2009-04-23 22:55:011098
1099 find_op_aborted_ = false;
1100
1101 // Keep track of what the last search was across the tabs.
[email protected]e491f1c2009-05-22 20:28:121102 *last_search_prepopulate_text_ = find_text;
[email protected]6e95706d2009-04-23 22:55:011103
1104 render_view_host()->StartFinding(current_find_request_id_,
1105 find_text_,
1106 forward_direction,
[email protected]e491f1c2009-05-22 20:28:121107 case_sensitive,
[email protected]6e95706d2009-04-23 22:55:011108 find_next);
1109}
1110
1111void TabContents::StopFinding(bool clear_selection) {
[email protected]2d2dffc2009-06-09 21:41:001112 // When |clear_selection| is true, it means the find string has been cleared
1113 // by the user, but the UI has not been dismissed.
1114 if (!clear_selection)
1115 find_ui_active_ = false;
[email protected]6e95706d2009-04-23 22:55:011116 find_op_aborted_ = true;
[email protected]e491f1c2009-05-22 20:28:121117 last_search_result_ = FindNotificationDetails();
[email protected]6e95706d2009-04-23 22:55:011118 render_view_host()->StopFinding(clear_selection);
1119}
1120
[email protected]5c4266922009-07-10 16:41:271121void TabContents::GetPageLanguage() {
1122 render_view_host()->GetPageLanguage();
1123}
1124
[email protected]420ae012009-04-24 05:16:321125void TabContents::OnJavaScriptMessageBoxClosed(IPC::Message* reply_msg,
1126 bool success,
1127 const std::wstring& prompt) {
1128 last_javascript_message_dismissal_ = base::TimeTicks::Now();
[email protected]7ab1e7d62009-10-14 23:32:011129 if (is_showing_before_unload_dialog_ && !success) {
1130 // If a beforeunload dialog is canceled, we need to stop the throbber from
1131 // spinning, since we forced it to start spinning in Navigate.
1132 DidStopLoading();
1133
1134 tab_close_start_time_ = base::TimeTicks();
1135 }
1136 is_showing_before_unload_dialog_ = false;
1137 render_view_host()->JavaScriptMessageBoxClosed(reply_msg, success, prompt);
[email protected]420ae012009-04-24 05:16:321138}
1139
1140void TabContents::OnSavePage() {
1141 // If we can not save the page, try to download it.
1142 if (!SavePackage::IsSavableContents(contents_mime_type())) {
1143 DownloadManager* dlm = profile()->GetDownloadManager();
1144 const GURL& current_page_url = GetURL();
1145 if (dlm && current_page_url.is_valid())
[email protected]d82ed61e2009-06-16 02:46:221146 dlm->DownloadUrl(current_page_url, GURL(), "", this);
[email protected]420ae012009-04-24 05:16:321147 return;
1148 }
1149
1150 Stop();
1151
1152 // Create the save package and possibly prompt the user for the name to save
1153 // the page as. The user prompt is an asynchronous operation that runs on
1154 // another thread.
[email protected]d82ed61e2009-06-16 02:46:221155 save_package_ = new SavePackage(this);
[email protected]420ae012009-04-24 05:16:321156 save_package_->GetSaveInfo();
1157}
1158
1159// Used in automated testing to bypass prompting the user for file names.
1160// Instead, the names and paths are hard coded rather than running them through
1161// file name sanitation and extension / mime checking.
[email protected]828cabe2009-09-26 22:47:111162bool TabContents::SavePage(const FilePath& main_file, const FilePath& dir_path,
[email protected]420ae012009-04-24 05:16:321163 SavePackage::SavePackageType save_type) {
1164 // Stop the page from navigating.
1165 Stop();
1166
[email protected]828cabe2009-09-26 22:47:111167 save_package_ = new SavePackage(this, save_type, main_file, dir_path);
[email protected]cdf6cc82009-08-04 21:48:591168 return save_package_->Init();
[email protected]420ae012009-04-24 05:16:321169}
1170
1171void TabContents::PrintPreview() {
1172 // We don't show the print preview yet, only the print dialog.
1173 PrintNow();
1174}
1175
1176bool TabContents::PrintNow() {
1177 // We can't print interstitial page for now.
1178 if (showing_interstitial_page())
1179 return false;
1180
1181 return render_view_host()->PrintPages();
1182}
1183
[email protected]82270452009-06-19 15:58:011184void TabContents::PrintingDone(int document_cookie, bool success) {
1185 render_view_host()->PrintingDone(document_cookie, success);
1186}
1187
[email protected]420ae012009-04-24 05:16:321188bool TabContents::IsActiveEntry(int32 page_id) {
1189 NavigationEntry* active_entry = controller_.GetActiveEntry();
1190 return (active_entry != NULL &&
1191 active_entry->site_instance() == GetSiteInstance() &&
1192 active_entry->page_id() == page_id);
1193}
1194
[email protected]3f8eb7f2009-10-02 23:58:051195void TabContents::LogNewTabTime(const std::string& event_name) {
1196 // Not all new tab pages get timed. In those cases, we don't have a
1197 // new_tab_start_time_.
1198 if (new_tab_start_time_.is_null())
1199 return;
1200
1201 base::TimeDelta duration = base::TimeTicks::Now() - new_tab_start_time_;
[email protected]84abba62009-10-07 17:01:441202 MetricEventDurationDetails details(event_name,
1203 static_cast<int>(duration.InMilliseconds()));
1204
[email protected]7ab1e7d62009-10-14 23:32:011205 if (event_name == "Tab.NewTabScriptStart") {
1206 UMA_HISTOGRAM_TIMES("Tab.NewTabScriptStart", duration);
1207 } else if (event_name == "Tab.NewTabDOMContentLoaded") {
1208 UMA_HISTOGRAM_TIMES("Tab.NewTabDOMContentLoaded", duration);
1209 } else if (event_name == "Tab.NewTabOnload") {
1210 UMA_HISTOGRAM_TIMES("Tab.NewTabOnload", duration);
[email protected]3f8eb7f2009-10-02 23:58:051211 // The new tab page has finished loading; reset it.
1212 new_tab_start_time_ = base::TimeTicks();
[email protected]84abba62009-10-07 17:01:441213 } else {
1214 NOTREACHED();
[email protected]3f8eb7f2009-10-02 23:58:051215 }
[email protected]84abba62009-10-07 17:01:441216 NotificationService::current()->Notify(
1217 NotificationType::METRIC_EVENT_DURATION,
1218 Source<TabContents>(this),
1219 Details<MetricEventDurationDetails>(&details));
[email protected]3f8eb7f2009-10-02 23:58:051220}
1221
[email protected]7ab1e7d62009-10-14 23:32:011222void TabContents::OnCloseStarted() {
1223 if (tab_close_start_time_.is_null())
1224 tab_close_start_time_ = base::TimeTicks::Now();
1225}
1226
[email protected]96d185d2009-04-24 03:28:541227// Notifies the RenderWidgetHost instance about the fact that the page is
1228// loading, or done loading and calls the base implementation.
1229void TabContents::SetIsLoading(bool is_loading,
1230 LoadNotificationDetails* details) {
1231 if (is_loading == is_loading_)
1232 return;
1233
1234 if (!is_loading) {
1235 load_state_ = net::LOAD_STATE_IDLE;
1236 load_state_host_.clear();
[email protected]094e5b22009-09-25 04:23:561237 upload_size_ = 0;
1238 upload_position_ = 0;
[email protected]96d185d2009-04-24 03:28:541239 }
1240
1241 render_manager_.SetIsLoading(is_loading);
1242
1243 is_loading_ = is_loading;
1244 waiting_for_response_ = is_loading;
1245
1246 if (delegate_)
1247 delegate_->LoadingStateChanged(this);
[email protected]329581b2009-04-28 06:52:351248 NotifyNavigationStateChanged(INVALIDATE_LOAD);
[email protected]96d185d2009-04-24 03:28:541249
1250 NotificationType type = is_loading ? NotificationType::LOAD_START :
1251 NotificationType::LOAD_STOP;
[email protected]ce5c4502009-05-06 16:46:111252 NotificationDetails det = NotificationService::NoDetails();
[email protected]96d185d2009-04-24 03:28:541253 if (details)
1254 det = Details<LoadNotificationDetails>(details);
1255 NotificationService::current()->Notify(type,
1256 Source<NavigationController>(&controller_),
1257 det);
1258}
1259
[email protected]2c4410d2009-05-06 23:46:221260void TabContents::CreateBlockedPopupContainerIfNecessary() {
1261 if (blocked_popups_)
1262 return;
1263
[email protected]4d4c0b92009-06-08 23:13:011264 blocked_popups_ = BlockedPopupContainer::Create(this, profile());
[email protected]2c4410d2009-05-06 23:46:221265}
1266
[email protected]b6c874582009-05-08 19:38:311267void TabContents::AddPopup(TabContents* new_contents,
1268 const gfx::Rect& initial_pos,
1269 const std::string& host) {
[email protected]2c4410d2009-05-06 23:46:221270 CreateBlockedPopupContainerIfNecessary();
1271 blocked_popups_->AddTabContents(new_contents, initial_pos, host);
[email protected]2c4410d2009-05-06 23:46:221272}
1273
[email protected]e69bce3dd2009-04-20 22:05:121274// TODO(brettw) This should be on the TabContentsView.
[email protected]66ba4932009-06-04 19:22:131275void TabContents::RepositionSupressedPopupsToFit() {
[email protected]d6598c052008-11-05 19:03:251276 if (blocked_popups_)
[email protected]4d4c0b92009-06-08 23:13:011277 blocked_popups_->RepositionBlockedPopupContainer();
[email protected]d5f942ba2008-09-26 19:30:341278}
1279
[email protected]b9681312008-11-07 00:08:261280bool TabContents::ShowingBlockedPopupNotification() const {
1281 return blocked_popups_ != NULL &&
[email protected]2c4410d2009-05-06 23:46:221282 blocked_popups_->GetBlockedPopupCount() != 0;
[email protected]b9681312008-11-07 00:08:261283}
[email protected]616ed5a2008-11-21 22:27:241284
1285namespace {
1286bool TransitionIsReload(PageTransition::Type transition) {
1287 return PageTransition::StripQualifier(transition) == PageTransition::RELOAD;
1288}
1289}
1290
1291void TabContents::ExpireInfoBars(
1292 const NavigationController::LoadCommittedDetails& details) {
1293 // Only hide InfoBars when the user has done something that makes the main
1294 // frame load. We don't want various automatic or subframe navigations making
1295 // it disappear.
1296 if (!details.is_user_initiated_main_frame_load())
1297 return;
1298
[email protected]f86a07022008-11-25 01:06:051299 for (int i = infobar_delegate_count() - 1; i >= 0; --i) {
[email protected]616ed5a2008-11-21 22:27:241300 InfoBarDelegate* delegate = GetInfoBarDelegateAt(i);
[email protected]f86a07022008-11-25 01:06:051301 if (delegate->ShouldExpire(details))
[email protected]616ed5a2008-11-21 22:27:241302 RemoveInfoBar(delegate);
[email protected]616ed5a2008-11-21 22:27:241303 }
1304}
[email protected]fdd61c62009-04-22 19:22:571305
1306void TabContents::OnGearsCreateShortcutDone(
1307 const GearsShortcutData2& shortcut_data, bool success) {
1308 NavigationEntry* current_entry = controller_.GetLastCommittedEntry();
1309 bool same_page =
1310 current_entry && pending_install_.page_id == current_entry->page_id();
1311
1312 if (success && same_page) {
1313 // Only switch to app mode if the user chose to create a shortcut and
1314 // we're still on the same page that it corresponded to.
1315 if (delegate())
1316 delegate()->ConvertContentsToApplication(this);
1317 }
1318
1319 // Reset the page id to indicate no requests are pending.
1320 pending_install_.page_id = 0;
1321 pending_install_.callback_functor = NULL;
1322}
[email protected]96d185d2009-04-24 03:28:541323
1324DOMUI* TabContents::GetDOMUIForCurrentState() {
1325 // When there is a pending navigation entry, we want to use the pending DOMUI
1326 // that goes along with it to control the basic flags. For example, we want to
1327 // show the pending URL in the URL bar, so we want the display_url flag to
1328 // be from the pending entry.
1329 //
1330 // The confusion comes because there are multiple possibilities for the
1331 // initial load in a tab as a side effect of the way the RenderViewHostManager
1332 // works.
1333 //
1334 // - For the very first tab the load looks "normal". The new tab DOM UI is
1335 // the pending one, and we want it to apply here.
1336 //
1337 // - For subsequent new tabs, they'll get a new SiteInstance which will then
1338 // get switched to the one previously associated with the new tab pages.
1339 // This switching will cause the manager to commit the RVH/DOMUI. So we'll
1340 // have a committed DOM UI in this case.
1341 //
1342 // This condition handles all of these cases:
1343 //
1344 // - First load in first tab: no committed nav entry + pending nav entry +
1345 // pending dom ui:
1346 // -> Use pending DOM UI if any.
1347 //
1348 // - First load in second tab: no committed nav entry + pending nav entry +
1349 // no pending DOM UI:
1350 // -> Use the committed DOM UI if any.
1351 //
1352 // - Second navigation in any tab: committed nav entry + pending nav entry:
1353 // -> Use pending DOM UI if any.
1354 //
1355 // - Normal state with no load: committed nav entry + no pending nav entry:
1356 // -> Use committed DOM UI.
1357 if (controller_.pending_entry() &&
1358 (controller_.GetLastCommittedEntry() ||
1359 render_manager_.pending_dom_ui()))
1360 return render_manager_.pending_dom_ui();
1361 return render_manager_.dom_ui();
1362}
[email protected]420ae012009-04-24 05:16:321363
1364void TabContents::DidNavigateMainFramePostCommit(
1365 const NavigationController::LoadCommittedDetails& details,
1366 const ViewHostMsg_FrameNavigate_Params& params) {
[email protected]c2e74fe82009-09-03 17:57:441367 if (opener_dom_ui_type_ != DOMUIFactory::kNoDOMUI) {
1368 // If this is a window.open navigation, use the same DOMUI as the renderer
1369 // that opened the window, as long as both renderers have the same
1370 // privileges.
1371 if (opener_dom_ui_type_ == DOMUIFactory::GetDOMUIType(GetURL())) {
1372 DOMUI* dom_ui = DOMUIFactory::CreateDOMUIForURL(this, GetURL());
1373 // dom_ui might be NULL if the URL refers to a non-existent extension.
1374 if (dom_ui) {
1375 render_manager_.SetDOMUIPostCommit(dom_ui);
1376 dom_ui->RenderViewCreated(render_view_host());
1377 }
1378 }
1379 opener_dom_ui_type_ = DOMUIFactory::kNoDOMUI;
1380 }
1381
[email protected]420ae012009-04-24 05:16:321382 if (details.is_user_initiated_main_frame_load()) {
1383 // Clear the status bubble. This is a workaround for a bug where WebKit
1384 // doesn't let us know that the cursor left an element during a
1385 // transition (this is also why the mouse cursor remains as a hand after
1386 // clicking on a link); see bugs 1184641 and 980803. We don't want to
1387 // clear the bubble when a user navigates to a named anchor in the same
1388 // page.
1389 UpdateTargetURL(details.entry->page_id(), GURL());
1390
1391 // UpdateHelpersForDidNavigate will handle the case where the password_form
1392 // origin is valid.
1393 // TODO(brettw) bug 1343111: Password manager stuff in here needs to be
1394 // cleaned up and covered by tests.
1395 if (!params.password_form.origin.is_valid())
1396 GetPasswordManager()->DidNavigate();
1397 }
1398
1399 // The keyword generator uses the navigation entries, so must be called after
1400 // the commit.
1401 GenerateKeywordIfNecessary(params);
1402
1403 // Allow the new page to set the title again.
1404 received_page_title_ = false;
1405
1406 // Get the favicon, either from history or request it from the net.
1407 fav_icon_helper_.FetchFavIcon(details.entry->url());
1408
[email protected]aece2c7f2009-08-27 20:43:171409 // Disable all page actions, unless this is an in-page navigation.
[email protected]744ef172009-10-16 21:53:461410 if (!page_actions_.empty()) {
[email protected]aece2c7f2009-08-27 20:43:171411 url_canon::Replacements<char> replacements;
1412 replacements.ClearRef();
1413 if (params.url.ReplaceComponents(replacements) !=
1414 params.referrer.ReplaceComponents(replacements)) {
[email protected]744ef172009-10-16 21:53:461415 page_actions_.clear();
[email protected]aece2c7f2009-08-27 20:43:171416 }
1417 }
[email protected]ce5c4502009-05-06 16:46:111418
[email protected]420ae012009-04-24 05:16:321419 // Close constrained popups if necessary.
1420 MaybeCloseChildWindows(details.previous_url, details.entry->url());
1421
1422 // Update the starred state.
1423 UpdateStarredStateForCurrentURL();
1424}
1425
1426void TabContents::DidNavigateAnyFramePostCommit(
1427 RenderViewHost* render_view_host,
1428 const NavigationController::LoadCommittedDetails& details,
1429 const ViewHostMsg_FrameNavigate_Params& params) {
1430 // If we navigate, start showing messages again. This does nothing to prevent
1431 // a malicious script from spamming messages, since the script could just
1432 // reload the page to stop blocking.
1433 suppress_javascript_messages_ = false;
1434
[email protected]420ae012009-04-24 05:16:321435 // Notify the password manager of the navigation or form submit.
1436 // TODO(brettw) bug 1343111: Password manager stuff in here needs to be
1437 // cleaned up and covered by tests.
1438 if (params.password_form.origin.is_valid())
1439 GetPasswordManager()->ProvisionallySavePassword(params.password_form);
1440}
1441
1442void TabContents::MaybeCloseChildWindows(const GURL& previous_url,
1443 const GURL& current_url) {
1444 if (net::RegistryControlledDomainService::SameDomainOrHost(
1445 previous_url, current_url))
1446 return;
1447
1448 // Clear out any child windows since we are leaving this page entirely.
1449 // We use indices instead of iterators in case CloseWindow does something
1450 // that may invalidate an iterator.
1451 int size = static_cast<int>(child_windows_.size());
1452 for (int i = size - 1; i >= 0; --i) {
1453 ConstrainedWindow* window = child_windows_[i];
1454 if (window)
1455 window->CloseConstrainedWindow();
1456 }
[email protected]e29300cd2009-07-23 21:14:591457
1458 // Close the popup container.
1459 if (blocked_popups_) {
1460 blocked_popups_->Destroy();
1461 blocked_popups_ = NULL;
1462 }
[email protected]420ae012009-04-24 05:16:321463}
1464
1465void TabContents::UpdateStarredStateForCurrentURL() {
1466 BookmarkModel* model = profile()->GetBookmarkModel();
1467 const bool old_state = is_starred_;
1468 is_starred_ = (model && model->IsBookmarked(GetURL()));
1469
1470 if (is_starred_ != old_state && delegate())
1471 delegate()->URLStarredChanged(this, is_starred_);
1472}
1473
1474void TabContents::UpdateAlternateErrorPageURL() {
1475 GURL url = GetAlternateErrorPageURL();
1476 render_view_host()->SetAlternateErrorPageURL(url);
1477}
1478
1479void TabContents::UpdateWebPreferences() {
1480 render_view_host()->UpdateWebPreferences(GetWebkitPrefs());
1481}
1482
1483void TabContents::UpdateMaxPageIDIfNecessary(SiteInstance* site_instance,
1484 RenderViewHost* rvh) {
1485 // If we are creating a RVH for a restored controller, then we might
1486 // have more page IDs than the SiteInstance's current max page ID. We must
1487 // make sure that the max page ID is larger than any restored page ID.
1488 // Note that it is ok for conflicting page IDs to exist in another tab
1489 // (i.e., NavigationController), but if any page ID is larger than the max,
1490 // the back/forward list will get confused.
1491 int max_restored_page_id = controller_.max_restored_page_id();
1492 if (max_restored_page_id > 0) {
1493 int curr_max_page_id = site_instance->max_page_id();
1494 if (max_restored_page_id > curr_max_page_id) {
1495 // Need to update the site instance immediately.
1496 site_instance->UpdateMaxPageID(max_restored_page_id);
1497
1498 // Also tell the renderer to update its internal representation. We
1499 // need to reserve enough IDs to make all restored page IDs less than
1500 // the max.
1501 if (curr_max_page_id < 0)
1502 curr_max_page_id = 0;
1503 rvh->ReservePageIDRange(max_restored_page_id - curr_max_page_id);
1504 }
1505 }
1506}
1507
1508void TabContents::UpdateHistoryForNavigation(
[email protected]ebe89e062009-08-13 23:16:541509 const GURL& virtual_url,
[email protected]befd8d822009-07-01 04:51:471510 const NavigationController::LoadCommittedDetails& details,
[email protected]420ae012009-04-24 05:16:321511 const ViewHostMsg_FrameNavigate_Params& params) {
1512 if (profile()->IsOffTheRecord())
1513 return;
1514
1515 // Add to history service.
1516 HistoryService* hs = profile()->GetHistoryService(Profile::IMPLICIT_ACCESS);
1517 if (hs) {
1518 if (PageTransition::IsMainFrame(params.transition) &&
[email protected]ebe89e062009-08-13 23:16:541519 virtual_url != params.url) {
1520 // Hack on the "virtual" URL so that it will appear in history. For some
[email protected]420ae012009-04-24 05:16:321521 // types of URLs, we will display a magic URL that is different from where
1522 // the page is actually navigated. We want the user to see in history
[email protected]ebe89e062009-08-13 23:16:541523 // what they saw in the URL bar, so we add the virtual URL as a redirect.
1524 // This only applies to the main frame, as the virtual URL doesn't apply
[email protected]420ae012009-04-24 05:16:321525 // to sub-frames.
1526 std::vector<GURL> redirects = params.redirects;
1527 if (!redirects.empty())
[email protected]ebe89e062009-08-13 23:16:541528 redirects.back() = virtual_url;
1529 hs->AddPage(virtual_url, this, params.page_id, params.referrer,
[email protected]befd8d822009-07-01 04:51:471530 params.transition, redirects, details.did_replace_entry);
[email protected]420ae012009-04-24 05:16:321531 } else {
1532 hs->AddPage(params.url, this, params.page_id, params.referrer,
[email protected]befd8d822009-07-01 04:51:471533 params.transition, params.redirects,
1534 details.did_replace_entry);
[email protected]420ae012009-04-24 05:16:321535 }
1536 }
1537}
1538
1539bool TabContents::UpdateTitleForEntry(NavigationEntry* entry,
1540 const std::wstring& title) {
1541 // For file URLs without a title, use the pathname instead. In the case of a
1542 // synthesized title, we don't want the update to count toward the "one set
1543 // per page of the title to history."
1544 std::wstring final_title;
1545 bool explicit_set;
1546 if (entry->url().SchemeIsFile() && title.empty()) {
1547 final_title = UTF8ToWide(entry->url().ExtractFileName());
1548 explicit_set = false; // Don't count synthetic titles toward the set limit.
1549 } else {
1550 TrimWhitespace(title, TRIM_ALL, &final_title);
1551 explicit_set = true;
1552 }
1553
1554 if (final_title == UTF16ToWideHack(entry->title()))
1555 return false; // Nothing changed, don't bother.
1556
1557 entry->set_title(WideToUTF16Hack(final_title));
1558
1559 // Update the history system for this page.
1560 if (!profile()->IsOffTheRecord() && !received_page_title_) {
1561 HistoryService* hs =
1562 profile()->GetHistoryService(Profile::IMPLICIT_ACCESS);
1563 if (hs)
[email protected]ebe89e062009-08-13 23:16:541564 hs->SetPageTitle(entry->virtual_url(), final_title);
[email protected]420ae012009-04-24 05:16:321565
1566 // Don't allow the title to be saved again for explicitly set ones.
1567 received_page_title_ = explicit_set;
1568 }
1569
1570 // Lastly, set the title for the view.
1571 view_->SetPageTitle(final_title);
1572
1573 return true;
1574}
1575
1576void TabContents::NotifySwapped() {
1577 // After sending out a swap notification, we need to send a disconnect
1578 // notification so that clients that pick up a pointer to |this| can NULL the
1579 // pointer. See Bug 1230284.
1580 notify_disconnection_ = true;
1581 NotificationService::current()->Notify(
[email protected]57c6a652009-05-04 07:58:341582 NotificationType::TAB_CONTENTS_SWAPPED,
[email protected]d82ed61e2009-06-16 02:46:221583 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:321584 NotificationService::NoDetails());
1585}
1586
1587void TabContents::NotifyConnected() {
1588 notify_disconnection_ = true;
1589 NotificationService::current()->Notify(
[email protected]57c6a652009-05-04 07:58:341590 NotificationType::TAB_CONTENTS_CONNECTED,
[email protected]d82ed61e2009-06-16 02:46:221591 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:321592 NotificationService::NoDetails());
1593}
1594
1595void TabContents::NotifyDisconnected() {
1596 if (!notify_disconnection_)
1597 return;
1598
1599 notify_disconnection_ = false;
1600 NotificationService::current()->Notify(
[email protected]57c6a652009-05-04 07:58:341601 NotificationType::TAB_CONTENTS_DISCONNECTED,
[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::GenerateKeywordIfNecessary(
1607 const ViewHostMsg_FrameNavigate_Params& params) {
1608 if (!params.searchable_form_url.is_valid())
1609 return;
1610
1611 if (profile()->IsOffTheRecord())
1612 return;
1613
1614 int last_index = controller_.last_committed_entry_index();
1615 // When there was no previous page, the last index will be 0. This is
1616 // normally due to a form submit that opened in a new tab.
1617 // TODO(brettw) bug 916126: we should support keywords when form submits
1618 // happen in new tabs.
1619 if (last_index <= 0)
1620 return;
1621 const NavigationEntry* previous_entry =
1622 controller_.GetEntryAtIndex(last_index - 1);
1623 if (IsFormSubmit(previous_entry)) {
1624 // Only generate a keyword if the previous page wasn't itself a form
1625 // submit.
1626 return;
1627 }
1628
1629 GURL keyword_url = previous_entry->user_typed_url().is_valid() ?
1630 previous_entry->user_typed_url() : previous_entry->url();
1631 std::wstring keyword =
1632 TemplateURLModel::GenerateKeyword(keyword_url, true); // autodetected
1633 if (keyword.empty())
1634 return;
1635
1636 TemplateURLModel* url_model = profile()->GetTemplateURLModel();
1637 if (!url_model)
1638 return;
1639
1640 if (!url_model->loaded()) {
1641 url_model->Load();
1642 return;
1643 }
1644
1645 const TemplateURL* current_url;
1646 std::wstring url = UTF8ToWide(params.searchable_form_url.spec());
1647 if (!url_model->CanReplaceKeyword(keyword, url, &current_url))
1648 return;
1649
1650 if (current_url) {
1651 if (current_url->originating_url().is_valid()) {
1652 // The existing keyword was generated from an OpenSearch description
1653 // document, don't regenerate.
1654 return;
1655 }
1656 url_model->Remove(current_url);
1657 }
1658 TemplateURL* new_url = new TemplateURL();
1659 new_url->set_keyword(keyword);
1660 new_url->set_short_name(keyword);
1661 new_url->SetURL(url, 0, 0);
1662 new_url->add_input_encoding(params.searchable_form_encoding);
1663 DCHECK(controller_.GetLastCommittedEntry());
1664 const GURL& favicon_url =
1665 controller_.GetLastCommittedEntry()->favicon().url();
1666 if (favicon_url.is_valid()) {
1667 new_url->SetFavIconURL(favicon_url);
1668 } else {
1669 // The favicon url isn't valid. This means there really isn't a favicon,
1670 // or the favicon url wasn't obtained before the load started. This assumes
1671 // the later.
1672 // TODO(sky): Need a way to set the favicon that doesn't involve generating
1673 // its url.
1674 new_url->SetFavIconURL(TemplateURL::GenerateFaviconURL(params.referrer));
1675 }
1676 new_url->set_safe_for_autoreplace(true);
1677 url_model->Add(new_url);
1678}
1679
[email protected]bf5c2ff392009-07-08 16:24:331680void TabContents::OnUserGesture() {
1681 // See comment in RenderViewHostDelegate::OnUserGesture as to why we do this.
1682 DownloadRequestManager* drm = g_browser_process->download_request_manager();
1683 if (drm)
1684 drm->OnUserGesture(this);
[email protected]e7eaedde2009-09-25 20:09:491685 ExternalProtocolHandler::OnUserGesture();
[email protected]bf5c2ff392009-07-08 16:24:331686 controller_.OnUserGesture();
1687}
1688
1689void TabContents::OnFindReply(int request_id,
1690 int number_of_matches,
1691 const gfx::Rect& selection_rect,
1692 int active_match_ordinal,
1693 bool final_update) {
[email protected]3ce7ced2009-07-24 18:52:421694 // Ignore responses for requests that have been aborted.
1695 if (find_op_aborted_)
1696 return;
1697
[email protected]bf5c2ff392009-07-08 16:24:331698 // Ignore responses for requests other than the one we have most recently
1699 // issued. That way we won't act on stale results when the user has
1700 // already typed in another query.
1701 if (request_id != current_find_request_id_)
1702 return;
1703
1704 if (number_of_matches == -1)
1705 number_of_matches = last_search_result_.number_of_matches();
1706 if (active_match_ordinal == -1)
1707 active_match_ordinal = last_search_result_.active_match_ordinal();
1708
1709 gfx::Rect selection = selection_rect;
1710 if (selection.IsEmpty())
1711 selection = last_search_result_.selection_rect();
1712
1713 // Notify the UI, automation and any other observers that a find result was
1714 // found.
1715 last_search_result_ = FindNotificationDetails(request_id, number_of_matches,
1716 selection, active_match_ordinal,
1717 final_update);
1718 NotificationService::current()->Notify(
1719 NotificationType::FIND_RESULT_AVAILABLE,
1720 Source<TabContents>(this),
1721 Details<FindNotificationDetails>(&last_search_result_));
1722}
1723
1724void TabContents::GoToEntryAtOffset(int offset) {
[email protected]f9cc4c452009-10-13 14:56:381725 if (!delegate_ || delegate_->OnGoToEntryOffset(offset))
1726 controller_.GoToOffset(offset);
[email protected]bf5c2ff392009-07-08 16:24:331727}
1728
1729void TabContents::GetHistoryListCount(int* back_list_count,
1730 int* forward_list_count) {
1731 int current_index = controller_.last_committed_entry_index();
1732 *back_list_count = current_index;
1733 *forward_list_count = controller_.entry_count() - current_index - 1;
1734}
1735
1736void TabContents::OnMissingPluginStatus(int status) {
1737#if defined(OS_WIN)
1738// TODO(PORT): pull in when plug-ins work
1739 GetPluginInstaller()->OnMissingPluginStatus(status);
1740#endif
1741}
1742
1743void TabContents::OnCrashedPlugin(const FilePath& plugin_path) {
[email protected]bf5c2ff392009-07-08 16:24:331744 DCHECK(!plugin_path.value().empty());
1745
1746 std::wstring plugin_name = plugin_path.ToWStringHack();
[email protected]d32d46182009-07-23 00:09:351747#if defined(OS_WIN) || defined(OS_MACOSX)
[email protected]bf5c2ff392009-07-08 16:24:331748 scoped_ptr<FileVersionInfo> version_info(
1749 FileVersionInfo::CreateFileVersionInfo(plugin_path));
1750 if (version_info.get()) {
1751 const std::wstring& product_name = version_info->product_name();
[email protected]996fd702009-09-04 19:12:371752 if (!product_name.empty()) {
[email protected]bf5c2ff392009-07-08 16:24:331753 plugin_name = product_name;
[email protected]996fd702009-09-04 19:12:371754#if defined(OS_MACOSX)
1755 // Many plugins on the Mac have .plugin in the actual name, which looks
1756 // terrible, so look for that and strip it off if present.
1757 const std::wstring plugin_extension(L".plugin");
1758 if (EndsWith(plugin_name, plugin_extension, true))
1759 plugin_name.erase(plugin_name.length() - plugin_extension.length());
1760#endif // OS_MACOSX
1761 }
[email protected]bf5c2ff392009-07-08 16:24:331762 }
[email protected]4a560ed2009-07-13 23:35:261763#else
1764 NOTIMPLEMENTED() << " convert plugin path to plugin name";
1765#endif
[email protected]bf5c2ff392009-07-08 16:24:331766 AddInfoBar(new SimpleAlertInfoBarDelegate(
1767 this, l10n_util::GetStringF(IDS_PLUGIN_CRASHED_PROMPT, plugin_name),
1768 NULL));
[email protected]bf5c2ff392009-07-08 16:24:331769}
1770
1771void TabContents::OnCrashedWorker() {
1772 AddInfoBar(new SimpleAlertInfoBarDelegate(
1773 this, l10n_util::GetString(IDS_WEBWORKER_CRASHED_PROMPT),
1774 NULL));
1775}
1776
1777void TabContents::OnDidGetApplicationInfo(
1778 int32 page_id,
1779 const webkit_glue::WebApplicationInfo& info) {
1780 if (pending_install_.page_id != page_id)
1781 return; // The user clicked create on a separate page. Ignore this.
1782
1783 pending_install_.callback_functor =
1784 new GearsCreateShortcutCallbackFunctor(this);
1785 GearsCreateShortcut(
1786 info, pending_install_.title, pending_install_.url, pending_install_.icon,
1787 NewCallback(pending_install_.callback_functor,
1788 &GearsCreateShortcutCallbackFunctor::Run));
1789}
1790
1791void TabContents::DidStartProvisionalLoadForFrame(
1792 RenderViewHost* render_view_host,
1793 bool is_main_frame,
1794 const GURL& url) {
1795 ProvisionalLoadDetails details(is_main_frame,
1796 controller_.IsURLInPageNavigation(url),
1797 url, std::string(), false);
1798 NotificationService::current()->Notify(
1799 NotificationType::FRAME_PROVISIONAL_LOAD_START,
1800 Source<NavigationController>(&controller_),
1801 Details<ProvisionalLoadDetails>(&details));
1802}
1803
1804void TabContents::DidStartReceivingResourceResponse(
1805 ResourceRequestDetails* details) {
1806 NotificationService::current()->Notify(
1807 NotificationType::RESOURCE_RESPONSE_STARTED,
1808 Source<NavigationController>(&controller()),
1809 Details<ResourceRequestDetails>(details));
1810}
1811
1812void TabContents::DidRedirectResource(ResourceRequestDetails* details) {
1813 NotificationService::current()->Notify(
1814 NotificationType::RESOURCE_RECEIVED_REDIRECT,
1815 Source<NavigationController>(&controller()),
1816 Details<ResourceRequestDetails>(details));
1817}
1818
1819void TabContents::DidLoadResourceFromMemoryCache(
1820 const GURL& url,
1821 const std::string& frame_origin,
1822 const std::string& main_frame_origin,
1823 const std::string& security_info) {
1824 // Send out a notification that we loaded a resource from our memory cache.
1825 int cert_id = 0, cert_status = 0, security_bits = 0;
1826 SSLManager::DeserializeSecurityInfo(security_info,
1827 &cert_id, &cert_status,
1828 &security_bits);
1829 LoadFromMemoryCacheDetails details(url, frame_origin, main_frame_origin,
[email protected]76543b92009-08-31 17:27:451830 process()->id(), cert_id, cert_status);
[email protected]bf5c2ff392009-07-08 16:24:331831
1832 NotificationService::current()->Notify(
1833 NotificationType::LOAD_FROM_MEMORY_CACHE,
1834 Source<NavigationController>(&controller_),
1835 Details<LoadFromMemoryCacheDetails>(&details));
1836}
1837
[email protected]e3d60e5d2009-09-25 21:08:291838void TabContents::DidDisplayInsecureContent() {
[email protected]09843b5f2009-09-26 08:30:591839 controller_.ssl_manager()->DidDisplayInsecureContent();
[email protected]e3d60e5d2009-09-25 21:08:291840}
1841
1842void TabContents::DidRunInsecureContent(const std::string& security_origin) {
[email protected]09843b5f2009-09-26 08:30:591843 controller_.ssl_manager()->DidRunInsecureContent(security_origin);
[email protected]e3d60e5d2009-09-25 21:08:291844}
1845
[email protected]bf5c2ff392009-07-08 16:24:331846void TabContents::DidFailProvisionalLoadWithError(
1847 RenderViewHost* render_view_host,
1848 bool is_main_frame,
1849 int error_code,
1850 const GURL& url,
1851 bool showing_repost_interstitial) {
1852 if (net::ERR_ABORTED == error_code) {
1853 // EVIL HACK ALERT! Ignore failed loads when we're showing interstitials.
1854 // This means that the interstitial won't be torn down properly, which is
1855 // bad. But if we have an interstitial, go back to another tab type, and
1856 // then load the same interstitial again, we could end up getting the first
1857 // interstitial's "failed" message (as a result of the cancel) when we're on
1858 // the second one.
1859 //
1860 // We can't tell this apart, so we think we're tearing down the current page
1861 // which will cause a crash later one. There is also some code in
1862 // RenderViewHostManager::RendererAbortedProvisionalLoad that is commented
1863 // out because of this problem.
1864 //
1865 // http://code.google.com/p/chromium/issues/detail?id=2855
1866 // Because this will not tear down the interstitial properly, if "back" is
1867 // back to another tab type, the interstitial will still be somewhat alive
1868 // in the previous tab type. If you navigate somewhere that activates the
1869 // tab with the interstitial again, you'll see a flash before the new load
1870 // commits of the interstitial page.
1871 if (showing_interstitial_page()) {
1872 LOG(WARNING) << "Discarding message during interstitial.";
1873 return;
1874 }
1875
1876 // This will discard our pending entry if we cancelled the load (e.g., if we
1877 // decided to download the file instead of load it). Only discard the
1878 // pending entry if the URLs match, otherwise the user initiated a navigate
1879 // before the page loaded so that the discard would discard the wrong entry.
1880 NavigationEntry* pending_entry = controller_.pending_entry();
1881 if (pending_entry && pending_entry->url() == url) {
1882 controller_.DiscardNonCommittedEntries();
1883 // Update the URL display.
1884 NotifyNavigationStateChanged(TabContents::INVALIDATE_URL);
1885 }
1886
1887 render_manager_.RendererAbortedProvisionalLoad(render_view_host);
1888 }
1889
1890 // Send out a notification that we failed a provisional load with an error.
1891 ProvisionalLoadDetails details(is_main_frame,
1892 controller_.IsURLInPageNavigation(url),
1893 url, std::string(), false);
1894 details.set_error_code(error_code);
1895
1896 NotificationService::current()->Notify(
1897 NotificationType::FAIL_PROVISIONAL_LOAD_WITH_ERROR,
1898 Source<NavigationController>(&controller_),
1899 Details<ProvisionalLoadDetails>(&details));
1900}
1901
1902void TabContents::DocumentLoadedInFrame() {
1903 controller_.DocumentLoadedInFrame();
1904}
1905
[email protected]8d3347f2009-07-09 22:00:211906RenderViewHostDelegate::View* TabContents::GetViewDelegate() {
[email protected]420ae012009-04-24 05:16:321907 return view_.get();
1908}
1909
[email protected]8d3347f2009-07-09 22:00:211910RenderViewHostDelegate::RendererManagement*
1911TabContents::GetRendererManagementDelegate() {
1912 return &render_manager_;
1913}
1914
[email protected]bf5c2ff392009-07-08 16:24:331915RenderViewHostDelegate::BrowserIntegration*
[email protected]8d3347f2009-07-09 22:00:211916 TabContents::GetBrowserIntegrationDelegate() {
1917 return this;
[email protected]bf5c2ff392009-07-08 16:24:331918}
1919
[email protected]8d3347f2009-07-09 22:00:211920RenderViewHostDelegate::Resource* TabContents::GetResourceDelegate() {
1921 return this;
[email protected]bf5c2ff392009-07-08 16:24:331922}
1923
[email protected]8d3347f2009-07-09 22:00:211924RenderViewHostDelegate::Save* TabContents::GetSaveDelegate() {
[email protected]420ae012009-04-24 05:16:321925 return save_package_.get(); // May be NULL, but we can return NULL.
1926}
1927
[email protected]8d3347f2009-07-09 22:00:211928RenderViewHostDelegate::Printing* TabContents::GetPrintingDelegate() {
1929 return &printing_;
1930}
1931
1932RenderViewHostDelegate::FavIcon* TabContents::GetFavIconDelegate() {
1933 return &fav_icon_helper_;
1934}
1935
1936RenderViewHostDelegate::Autofill* TabContents::GetAutofillDelegate() {
1937 if (autofill_manager_.get() == NULL)
1938 autofill_manager_.reset(new AutofillManager(this));
1939 return autofill_manager_.get();
[email protected]bf5c2ff392009-07-08 16:24:331940}
1941
[email protected]80d96fa2009-06-10 22:34:511942RendererPreferences TabContents::GetRendererPrefs() const {
[email protected]840b1512009-07-21 16:53:461943 return renderer_preferences_;
[email protected]80d96fa2009-06-10 22:34:511944}
1945
[email protected]57c6a652009-05-04 07:58:341946TabContents* TabContents::GetAsTabContents() {
1947 return this;
1948}
1949
[email protected]3e7cf1362009-08-21 17:47:081950void TabContents::AddBlockedNotice(const GURL& url, const string16& reason) {
1951 CreateBlockedPopupContainerIfNecessary();
1952 blocked_popups_->AddBlockedNotice(url, reason);
1953}
1954
[email protected]7b291f92009-08-14 05:43:531955ViewType::Type TabContents::GetRenderViewType() const {
1956 return ViewType::TAB_CONTENTS;
1957}
1958
1959int TabContents::GetBrowserWindowID() const {
1960 return controller().window_id().id();
1961}
1962
[email protected]420ae012009-04-24 05:16:321963void TabContents::RenderViewCreated(RenderViewHost* render_view_host) {
[email protected]2e4633c2009-07-09 16:58:061964 NotificationService::current()->Notify(
1965 NotificationType::RENDER_VIEW_HOST_CREATED_FOR_TAB,
1966 Source<TabContents>(this),
1967 Details<RenderViewHost>(render_view_host));
[email protected]420ae012009-04-24 05:16:321968 NavigationEntry* entry = controller_.GetActiveEntry();
1969 if (!entry)
1970 return;
1971
1972 // When we're creating views, we're still doing initial setup, so we always
1973 // use the pending DOM UI rather than any possibly existing committed one.
1974 if (render_manager_.pending_dom_ui()) {
1975 render_manager_.pending_dom_ui()->RenderViewCreated(render_view_host);
1976 }
1977
1978 if (entry->IsViewSourceMode()) {
1979 // Put the renderer in view source mode.
1980 render_view_host->Send(
1981 new ViewMsg_EnableViewSourceMode(render_view_host->routing_id()));
1982 }
[email protected]0666aef2009-05-13 19:48:081983
1984 view()->RenderViewCreated(render_view_host);
[email protected]420ae012009-04-24 05:16:321985}
1986
1987void TabContents::RenderViewReady(RenderViewHost* rvh) {
1988 if (rvh != render_view_host()) {
1989 // Don't notify the world, since this came from a renderer in the
1990 // background.
1991 return;
1992 }
1993
1994 NotifyConnected();
[email protected]9d8a4642009-07-29 17:25:301995 bool was_crashed = is_crashed();
[email protected]420ae012009-04-24 05:16:321996 SetIsCrashed(false);
[email protected]9d8a4642009-07-29 17:25:301997
1998 // Restore the focus to the tab (otherwise the focus will be on the top
1999 // window).
2000 if (was_crashed && !FocusLocationBarByDefault())
2001 Focus();
[email protected]420ae012009-04-24 05:16:322002}
2003
2004void TabContents::RenderViewGone(RenderViewHost* rvh) {
2005 // Ask the print preview if this renderer was valuable.
2006 if (!printing_.OnRenderViewGone(rvh))
2007 return;
2008 if (rvh != render_view_host()) {
2009 // The pending page's RenderViewHost is gone.
2010 return;
2011 }
2012
2013 SetIsLoading(false, NULL);
2014 NotifyDisconnected();
2015 SetIsCrashed(true);
2016
[email protected]8e537302009-06-10 21:19:582017 // Tell the view that we've crashed so it can prepare the sad tab page.
2018 view_->OnTabCrashed();
[email protected]420ae012009-04-24 05:16:322019
2020 // Hide any visible hung renderer warning for this web contents' process.
[email protected]d82ed61e2009-06-16 02:46:222021 HungRendererDialog::HideForTabContents(this);
[email protected]420ae012009-04-24 05:16:322022}
2023
[email protected]2e4633c2009-07-09 16:58:062024void TabContents::RenderViewDeleted(RenderViewHost* rvh) {
2025 NotificationService::current()->Notify(
2026 NotificationType::RENDER_VIEW_HOST_DELETED,
2027 Source<TabContents>(this),
2028 Details<RenderViewHost>(rvh));
2029}
2030
[email protected]420ae012009-04-24 05:16:322031void TabContents::DidNavigate(RenderViewHost* rvh,
2032 const ViewHostMsg_FrameNavigate_Params& params) {
[email protected]8030f012009-09-25 18:09:372033 int extra_invalidate_flags = 0;
2034
2035 if (PageTransition::IsMainFrame(params.transition)) {
[email protected]20ede072009-10-06 00:28:352036 bool was_bookmark_bar_visible = ShouldShowBookmarkBar();
[email protected]8030f012009-09-25 18:09:372037 bool was_extension_shelf_visible = IsExtensionShelfAlwaysVisible();
2038
[email protected]420ae012009-04-24 05:16:322039 render_manager_.DidNavigateMainFrame(rvh);
2040
[email protected]20ede072009-10-06 00:28:352041 if (was_bookmark_bar_visible != ShouldShowBookmarkBar())
[email protected]8030f012009-09-25 18:09:372042 extra_invalidate_flags |= INVALIDATE_BOOKMARK_BAR;
2043 if (was_extension_shelf_visible != IsExtensionShelfAlwaysVisible())
2044 extra_invalidate_flags |= INVALIDATE_EXTENSION_SHELF;
2045 }
2046
[email protected]420ae012009-04-24 05:16:322047 // Update the site of the SiteInstance if it doesn't have one yet.
2048 if (!GetSiteInstance()->has_site())
2049 GetSiteInstance()->SetSite(params.url);
2050
2051 // Need to update MIME type here because it's referred to in
2052 // UpdateNavigationCommands() called by RendererDidNavigate() to
2053 // determine whether or not to enable the encoding menu.
2054 // It's updated only for the main frame. For a subframe,
2055 // RenderView::UpdateURL does not set params.contents_mime_type.
2056 // (see http://code.google.com/p/chromium/issues/detail?id=2929 )
2057 // TODO(jungshik): Add a test for the encoding menu to avoid
2058 // regressing it again.
2059 if (PageTransition::IsMainFrame(params.transition))
2060 contents_mime_type_ = params.contents_mime_type;
2061
2062 NavigationController::LoadCommittedDetails details;
[email protected]8030f012009-09-25 18:09:372063 bool did_navigate = controller_.RendererDidNavigate(
2064 params, extra_invalidate_flags, &details);
[email protected]3e90d4a2009-07-03 17:38:392065
2066 // Update history. Note that this needs to happen after the entry is complete,
2067 // which WillNavigate[Main,Sub]Frame will do before this function is called.
2068 if (params.should_update_history) {
2069 // Most of the time, the displayURL matches the loaded URL, but for about:
2070 // URLs, we use a data: URL as the real value. We actually want to save
2071 // the about: URL to the history db and keep the data: URL hidden. This is
2072 // what the TabContents' URL getter does.
2073 UpdateHistoryForNavigation(GetURL(), details, params);
2074 }
2075
[email protected]76543b92009-08-31 17:27:452076 if (!did_navigate)
2077 return; // No navigation happened.
[email protected]420ae012009-04-24 05:16:322078
2079 // DO NOT ADD MORE STUFF TO THIS FUNCTION! Your component should either listen
2080 // for the appropriate notification (best) or you can add it to
2081 // DidNavigateMainFramePostCommit / DidNavigateAnyFramePostCommit (only if
2082 // necessary, please).
2083
2084 // Run post-commit tasks.
2085 if (details.is_main_frame)
2086 DidNavigateMainFramePostCommit(details, params);
2087 DidNavigateAnyFramePostCommit(rvh, details, params);
2088}
2089
2090void TabContents::UpdateState(RenderViewHost* rvh,
2091 int32 page_id,
2092 const std::string& state) {
2093 DCHECK(rvh == render_view_host());
2094
2095 // We must be prepared to handle state updates for any page, these occur
2096 // when the user is scrolling and entering form data, as well as when we're
2097 // leaving a page, in which case our state may have already been moved to
2098 // the next page. The navigation controller will look up the appropriate
2099 // NavigationEntry and update it when it is notified via the delegate.
2100
2101 int entry_index = controller_.GetEntryIndexWithPageID(
2102 GetSiteInstance(), page_id);
2103 if (entry_index < 0)
2104 return;
2105 NavigationEntry* entry = controller_.GetEntryAtIndex(entry_index);
2106
2107 if (state == entry->content_state())
2108 return; // Nothing to update.
2109 entry->set_content_state(state);
2110 controller_.NotifyEntryChanged(entry, entry_index);
2111}
2112
2113void TabContents::UpdateTitle(RenderViewHost* rvh,
2114 int32 page_id, const std::wstring& title) {
2115 // If we have a title, that's a pretty good indication that we've started
2116 // getting useful data.
2117 SetNotWaitingForResponse();
2118
2119 DCHECK(rvh == render_view_host());
2120 NavigationEntry* entry = controller_.GetEntryWithPageID(GetSiteInstance(),
2121 page_id);
2122 if (!entry || !UpdateTitleForEntry(entry, title))
2123 return;
2124
2125 // Broadcast notifications when the UI should be updated.
2126 if (entry == controller_.GetEntryAtOffset(0))
[email protected]c9cd2222009-05-06 05:16:502127 NotifyNavigationStateChanged(INVALIDATE_TAB);
[email protected]420ae012009-04-24 05:16:322128}
2129
[email protected]420ae012009-04-24 05:16:322130void TabContents::UpdateEncoding(RenderViewHost* render_view_host,
[email protected]41fc0322009-09-04 22:23:402131 const std::string& encoding) {
[email protected]420ae012009-04-24 05:16:322132 set_encoding(encoding);
2133}
2134
2135void TabContents::UpdateTargetURL(int32 page_id, const GURL& url) {
2136 if (delegate())
2137 delegate()->UpdateTargetURL(this, url);
2138}
2139
2140void TabContents::UpdateThumbnail(const GURL& url,
2141 const SkBitmap& bitmap,
2142 const ThumbnailScore& score) {
2143 // Tell History about this thumbnail
[email protected]449478302009-06-09 20:04:282144 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kThumbnailStore)) {
[email protected]72d0fc72009-07-09 17:30:492145 if (!profile()->IsOffTheRecord())
[email protected]42339402009-07-17 17:27:192146 profile()->GetThumbnailStore()->SetPageThumbnail(url, bitmap,
2147 score, true);
[email protected]449478302009-06-09 20:04:282148 } else {
2149 HistoryService* hs;
2150 if (!profile()->IsOffTheRecord() &&
2151 (hs = profile()->GetHistoryService(Profile::IMPLICIT_ACCESS))) {
2152 hs->SetPageThumbnail(url, bitmap, score);
2153 }
[email protected]420ae012009-04-24 05:16:322154 }
2155}
2156
[email protected]8922e1f2009-10-03 05:01:262157void TabContents::UpdateInspectorSettings(const std::string& raw_settings) {
[email protected]0df30122009-06-03 12:13:082158 profile()->GetPrefs()->SetString(prefs::kWebKitInspectorSettings,
[email protected]8922e1f2009-10-03 05:01:262159 UTF8ToWide(raw_settings));
[email protected]0df30122009-06-03 12:13:082160}
2161
[email protected]420ae012009-04-24 05:16:322162void TabContents::Close(RenderViewHost* rvh) {
[email protected]24a4d1062009-07-10 23:10:422163 // If we close the tab while we're in the middle of a drag, we'll crash.
2164 // Instead, cancel the drag and close it as soon as the drag ends.
2165 if (view()->IsDoingDrag()) {
2166 view()->CancelDragAndCloseTab();
2167 return;
2168 }
2169
[email protected]420ae012009-04-24 05:16:322170 // Ignore this if it comes from a RenderViewHost that we aren't showing.
2171 if (delegate() && rvh == render_view_host())
2172 delegate()->CloseContents(this);
2173}
2174
2175void TabContents::RequestMove(const gfx::Rect& new_bounds) {
2176 if (delegate() && delegate()->IsPopup(this))
2177 delegate()->MoveContents(this, new_bounds);
2178}
2179
[email protected]7ab1e7d62009-10-14 23:32:012180void TabContents::DidStartLoading() {
[email protected]420ae012009-04-24 05:16:322181 SetIsLoading(true, NULL);
2182}
2183
[email protected]7ab1e7d62009-10-14 23:32:012184void TabContents::DidStopLoading() {
[email protected]420ae012009-04-24 05:16:322185 scoped_ptr<LoadNotificationDetails> details;
2186
2187 NavigationEntry* entry = controller_.GetActiveEntry();
2188 // An entry may not exist for a stop when loading an initial blank page or
2189 // if an iframe injected by script into a blank page finishes loading.
2190 if (entry) {
2191 scoped_ptr<base::ProcessMetrics> metrics(
2192 base::ProcessMetrics::CreateProcessMetrics(
2193 process()->process().handle()));
2194
2195 base::TimeDelta elapsed = base::TimeTicks::Now() - current_load_start_;
2196
2197 details.reset(new LoadNotificationDetails(
[email protected]ebe89e062009-08-13 23:16:542198 entry->virtual_url(),
[email protected]420ae012009-04-24 05:16:322199 entry->transition_type(),
2200 elapsed,
2201 &controller_,
2202 controller_.GetCurrentEntryIndex()));
2203 }
2204
2205 // Tell PasswordManager we've finished a page load, which serves as a
2206 // green light to save pending passwords and reset itself.
2207 GetPasswordManager()->DidStopLoading();
2208
2209 SetIsLoading(false, details.get());
2210}
2211
[email protected]420ae012009-04-24 05:16:322212void TabContents::DidRedirectProvisionalLoad(int32 page_id,
2213 const GURL& source_url,
2214 const GURL& target_url) {
2215 NavigationEntry* entry;
2216 if (page_id == -1)
2217 entry = controller_.pending_entry();
2218 else
2219 entry = controller_.GetEntryWithPageID(GetSiteInstance(), page_id);
2220 if (!entry || entry->url() != source_url)
2221 return;
2222 entry->set_url(target_url);
2223}
2224
[email protected]420ae012009-04-24 05:16:322225void TabContents::RequestOpenURL(const GURL& url, const GURL& referrer,
2226 WindowOpenDisposition disposition) {
2227 if (render_manager_.dom_ui()) {
2228 // When we're a DOM UI, it will provide a page transition type for us (this
2229 // is so the new tab page can specify AUTO_BOOKMARK for automatically
2230 // generated suggestions).
2231 //
2232 // Note also that we hide the referrer for DOM UI pages. We don't really
[email protected]60e448982009-05-06 04:21:162233 // want web sites to see a referrer of "chrome://blah" (and some
2234 // chrome: URLs might have search terms or other stuff we don't want to
[email protected]420ae012009-04-24 05:16:322235 // send to the site), so we send no referrer.
2236 OpenURL(url, GURL(), disposition,
2237 render_manager_.dom_ui()->link_transition_type());
2238 } else {
2239 OpenURL(url, referrer, disposition, PageTransition::LINK);
2240 }
2241}
2242
[email protected]1ee614862009-07-06 20:29:182243void TabContents::DomOperationResponse(const std::string& json_string,
2244 int automation_id) {
2245}
2246
[email protected]420ae012009-04-24 05:16:322247void TabContents::ProcessDOMUIMessage(const std::string& message,
[email protected]e4dad9fb2009-10-06 18:15:582248 const Value* content,
[email protected]811bfe32009-07-01 08:46:252249 int request_id,
2250 bool has_callback) {
[email protected]420ae012009-04-24 05:16:322251 if (!render_manager_.dom_ui()) {
[email protected]c2e74fe82009-09-03 17:57:442252 // This can happen if someone uses window.open() to open an extension URL
2253 // from a non-extension context.
2254 render_view_host()->BlockExtensionRequest(request_id);
[email protected]420ae012009-04-24 05:16:322255 return;
2256 }
[email protected]811bfe32009-07-01 08:46:252257 render_manager_.dom_ui()->ProcessDOMUIMessage(message, content, request_id,
2258 has_callback);
[email protected]420ae012009-04-24 05:16:322259}
2260
2261void TabContents::ProcessExternalHostMessage(const std::string& message,
2262 const std::string& origin,
2263 const std::string& target) {
2264 if (delegate())
2265 delegate()->ForwardMessageToExternalHost(message, origin, target);
2266}
2267
[email protected]420ae012009-04-24 05:16:322268void TabContents::RunFileChooser(bool multiple_files,
2269 const string16& title,
2270 const FilePath& default_file) {
2271 if (!select_file_dialog_.get())
2272 select_file_dialog_ = SelectFileDialog::Create(this);
2273 SelectFileDialog::Type dialog_type =
2274 multiple_files ? SelectFileDialog::SELECT_OPEN_MULTI_FILE :
2275 SelectFileDialog::SELECT_OPEN_FILE;
2276 select_file_dialog_->SelectFile(dialog_type, title, default_file,
2277 NULL, 0, FILE_PATH_LITERAL(""),
2278 view_->GetTopLevelNativeWindow(), NULL);
2279}
2280
2281void TabContents::RunJavaScriptMessage(
2282 const std::wstring& message,
2283 const std::wstring& default_prompt,
2284 const GURL& frame_url,
2285 const int flags,
2286 IPC::Message* reply_msg,
2287 bool* did_suppress_message) {
2288 // Suppress javascript messages when requested and when inside a constrained
2289 // popup window (because that activates them and breaks them out of the
2290 // constrained window jail).
[email protected]91ed49a2009-07-07 21:29:552291 // Also suppress messages when showing an interstitial. The interstitial is
2292 // shown over the previous page, we don't want the hidden page dialogs to
2293 // interfere with the interstitial.
2294 bool suppress_this_message = suppress_javascript_messages_ ||
2295 showing_interstitial_page();
[email protected]420ae012009-04-24 05:16:322296 if (delegate())
2297 suppress_this_message |=
[email protected]b6c874582009-05-08 19:38:312298 (delegate()->GetConstrainingContents(this) != this);
[email protected]420ae012009-04-24 05:16:322299
2300 *did_suppress_message = suppress_this_message;
2301
2302 if (!suppress_this_message) {
2303 base::TimeDelta time_since_last_message(
2304 base::TimeTicks::Now() - last_javascript_message_dismissal_);
2305 bool show_suppress_checkbox = false;
2306 // Show a checkbox offering to suppress further messages if this message is
2307 // being displayed within kJavascriptMessageExpectedDelay of the last one.
2308 if (time_since_last_message <
2309 base::TimeDelta::FromMilliseconds(kJavascriptMessageExpectedDelay))
2310 show_suppress_checkbox = true;
2311
[email protected]d82ed61e2009-06-16 02:46:222312 RunJavascriptMessageBox(this, frame_url, flags, message, default_prompt,
[email protected]420ae012009-04-24 05:16:322313 show_suppress_checkbox, reply_msg);
2314 } else {
2315 // If we are suppressing messages, just reply as is if the user immediately
2316 // pressed "Cancel".
2317 OnJavaScriptMessageBoxClosed(reply_msg, false, std::wstring());
2318 }
2319}
2320
2321void TabContents::RunBeforeUnloadConfirm(const std::wstring& message,
2322 IPC::Message* reply_msg) {
[email protected]7ab1e7d62009-10-14 23:32:012323 is_showing_before_unload_dialog_ = true;
[email protected]d82ed61e2009-06-16 02:46:222324 RunBeforeUnloadDialog(this, message, reply_msg);
[email protected]420ae012009-04-24 05:16:322325}
2326
2327void TabContents::ShowModalHTMLDialog(const GURL& url, int width, int height,
2328 const std::string& json_arguments,
2329 IPC::Message* reply_msg) {
2330 if (delegate()) {
2331 HtmlDialogUIDelegate* dialog_delegate =
2332 new ModalHtmlDialogDelegate(url, width, height, json_arguments,
[email protected]d82ed61e2009-06-16 02:46:222333 reply_msg, this);
[email protected]420ae012009-04-24 05:16:322334 delegate()->ShowHtmlDialog(dialog_delegate, NULL);
2335 }
2336}
2337
2338void TabContents::PasswordFormsSeen(
[email protected]ed3fb032009-06-16 19:50:562339 const std::vector<webkit_glue::PasswordForm>& forms) {
[email protected]420ae012009-04-24 05:16:322340 GetPasswordManager()->PasswordFormsSeen(forms);
2341}
2342
[email protected]420ae012009-04-24 05:16:322343// Checks to see if we should generate a keyword based on the OSDD, and if
2344// necessary uses TemplateURLFetcher to download the OSDD and create a keyword.
2345void TabContents::PageHasOSDD(RenderViewHost* render_view_host,
2346 int32 page_id, const GURL& url,
2347 bool autodetected) {
2348 // Make sure page_id is the current page, and the TemplateURLModel is loaded.
2349 DCHECK(url.is_valid());
2350 if (!IsActiveEntry(page_id))
2351 return;
2352 TemplateURLModel* url_model = profile()->GetTemplateURLModel();
2353 if (!url_model)
2354 return;
2355 if (!url_model->loaded()) {
2356 url_model->Load();
2357 return;
2358 }
2359 if (!profile()->GetTemplateURLFetcher())
2360 return;
2361
2362 if (profile()->IsOffTheRecord())
2363 return;
2364
2365 const NavigationEntry* entry = controller_.GetLastCommittedEntry();
2366 DCHECK(entry);
2367
2368 const NavigationEntry* base_entry = entry;
2369 if (IsFormSubmit(base_entry)) {
2370 // If the current page is a form submit, find the last page that was not
2371 // a form submit and use its url to generate the keyword from.
2372 int index = controller_.last_committed_entry_index() - 1;
2373 while (index >= 0 && IsFormSubmit(controller_.GetEntryAtIndex(index)))
2374 index--;
2375 if (index >= 0)
2376 base_entry = controller_.GetEntryAtIndex(index);
2377 else
2378 base_entry = NULL;
2379 }
2380
2381 // We want to use the user typed URL if available since that represents what
2382 // the user typed to get here, and fall back on the regular URL if not.
2383 if (!base_entry)
2384 return;
2385 GURL keyword_url = base_entry->user_typed_url().is_valid() ?
2386 base_entry->user_typed_url() : base_entry->url();
2387 if (!keyword_url.is_valid())
2388 return;
2389 std::wstring keyword = TemplateURLModel::GenerateKeyword(keyword_url,
2390 autodetected);
2391 if (keyword.empty())
2392 return;
2393 const TemplateURL* template_url =
2394 url_model->GetTemplateURLForKeyword(keyword);
2395 if (template_url && (!template_url->safe_for_autoreplace() ||
2396 template_url->originating_url() == url)) {
2397 // Either there is a user created TemplateURL for this keyword, or the
2398 // keyword has the same OSDD url and we've parsed it.
2399 return;
2400 }
2401
2402 // Download the OpenSearch description document. If this is successful a
2403 // new keyword will be created when done.
[email protected]420ae012009-04-24 05:16:322404 profile()->GetTemplateURLFetcher()->ScheduleDownload(
2405 keyword,
2406 url,
2407 base_entry->favicon().url(),
[email protected]d0cc9fb52009-06-23 02:26:162408 this,
[email protected]420ae012009-04-24 05:16:322409 autodetected);
2410}
2411
[email protected]420ae012009-04-24 05:16:322412GURL TabContents::GetAlternateErrorPageURL() const {
2413 GURL url;
2414 // Disable alternate error pages when in OffTheRecord/Incognito mode.
2415 if (profile()->IsOffTheRecord())
2416 return url;
2417
2418 PrefService* prefs = profile()->GetPrefs();
2419 DCHECK(prefs);
2420 if (prefs->GetBoolean(prefs::kAlternateErrorPagesEnabled)) {
[email protected]a33457b82009-09-21 23:07:152421 url = google_util::AppendGoogleLocaleParam(
2422 GURL(google_util::kLinkDoctorBaseURL));
[email protected]420ae012009-04-24 05:16:322423 url = google_util::AppendGoogleTLDParam(url);
2424 }
2425 return url;
2426}
2427
2428WebPreferences TabContents::GetWebkitPrefs() {
2429 PrefService* prefs = render_view_host()->process()->profile()->GetPrefs();
[email protected]7ccddb8c2009-08-04 17:36:552430 bool is_dom_ui = false;
2431 return RenderViewHostDelegateHelper::GetWebkitPrefs(prefs, is_dom_ui);
[email protected]420ae012009-04-24 05:16:322432}
2433
[email protected]420ae012009-04-24 05:16:322434void TabContents::OnJSOutOfMemory() {
2435 AddInfoBar(new SimpleAlertInfoBarDelegate(
2436 this, l10n_util::GetString(IDS_JS_OUT_OF_MEMORY_PROMPT), NULL));
2437}
2438
[email protected]420ae012009-04-24 05:16:322439void TabContents::OnCrossSiteResponse(int new_render_process_host_id,
2440 int new_request_id) {
[email protected]57c6a652009-05-04 07:58:342441 // Allows the TabContents to react when a cross-site response is ready to be
[email protected]420ae012009-04-24 05:16:322442 // delivered to a pending RenderViewHost. We must first run the onunload
2443 // handler of the old RenderViewHost before we can allow it to proceed.
2444 render_manager_.OnCrossSiteResponse(new_render_process_host_id,
2445 new_request_id);
2446}
2447
2448bool TabContents::CanBlur() const {
2449 return delegate() ? delegate()->CanBlur() : true;
2450}
2451
2452gfx::Rect TabContents::GetRootWindowResizerRect() const {
2453 if (delegate())
2454 return delegate()->GetRootWindowResizerRect();
2455 return gfx::Rect();
2456}
2457
2458void TabContents::RendererUnresponsive(RenderViewHost* rvh,
2459 bool is_during_unload) {
2460 if (is_during_unload) {
2461 // Hang occurred while firing the beforeunload/unload handler.
2462 // Pretend the handler fired so tab closing continues as if it had.
[email protected]829e7612009-04-25 01:15:112463 rvh->set_sudden_termination_allowed(true);
[email protected]420ae012009-04-24 05:16:322464
2465 if (!render_manager_.ShouldCloseTabOnUnresponsiveRenderer())
2466 return;
2467
2468 // If the tab hangs in the beforeunload/unload handler there's really
2469 // nothing we can do to recover. Pretend the unload listeners have
2470 // all fired and close the tab. If the hang is in the beforeunload handler
2471 // then the user will not have the option of cancelling the close.
2472 Close(rvh);
2473 return;
2474 }
2475
2476 if (render_view_host() && render_view_host()->IsRenderViewLive())
[email protected]d82ed61e2009-06-16 02:46:222477 HungRendererDialog::ShowForTabContents(this);
[email protected]420ae012009-04-24 05:16:322478}
2479
2480void TabContents::RendererResponsive(RenderViewHost* render_view_host) {
[email protected]d82ed61e2009-06-16 02:46:222481 HungRendererDialog::HideForTabContents(this);
[email protected]420ae012009-04-24 05:16:322482}
2483
2484void TabContents::LoadStateChanged(const GURL& url,
[email protected]094e5b22009-09-25 04:23:562485 net::LoadState load_state,
2486 uint64 upload_position,
2487 uint64 upload_size) {
[email protected]420ae012009-04-24 05:16:322488 load_state_ = load_state;
[email protected]094e5b22009-09-25 04:23:562489 upload_position_ = upload_position;
2490 upload_size_ = upload_size;
[email protected]d686e812009-06-03 19:10:292491 std::wstring languages =
2492 profile()->GetPrefs()->GetString(prefs::kAcceptLanguages);
2493 load_state_host_.clear();
2494 std::string host = url.host();
2495 net::IDNToUnicode(host.c_str(), host.size(), languages, &load_state_host_);
[email protected]420ae012009-04-24 05:16:322496 if (load_state_ == net::LOAD_STATE_READING_RESPONSE)
2497 SetNotWaitingForResponse();
2498 if (is_loading())
[email protected]c9cd2222009-05-06 05:16:502499 NotifyNavigationStateChanged(INVALIDATE_LOAD | INVALIDATE_TAB);
[email protected]420ae012009-04-24 05:16:322500}
2501
[email protected]1ee614862009-07-06 20:29:182502bool TabContents::IsExternalTabContainer() const {
2503 if (!delegate())
2504 return false;
2505
2506 return delegate()->IsExternalTabContainer();
2507}
2508
[email protected]1ee614862009-07-06 20:29:182509void TabContents::DidInsertCSS() {
2510 // This RVHDelegate function is used for extensions and not us.
[email protected]420ae012009-04-24 05:16:322511}
2512
[email protected]130efb02009-09-18 18:54:352513void TabContents::FocusedNodeChanged() {
2514 NotificationService::current()->Notify(
2515 NotificationType::FOCUS_CHANGED_IN_PAGE,
2516 Source<RenderViewHost>(render_view_host()),
2517 NotificationService::NoDetails());
2518}
2519
[email protected]420ae012009-04-24 05:16:322520void TabContents::FileSelected(const FilePath& path,
2521 int index, void* params) {
2522 render_view_host()->FileSelected(path);
2523}
2524
2525void TabContents::MultiFilesSelected(const std::vector<FilePath>& files,
2526 void* params) {
2527 render_view_host()->MultiFilesSelected(files);
2528}
2529
2530void TabContents::FileSelectionCanceled(void* params) {
2531 // If the user cancels choosing a file to upload we pass back an
2532 // empty vector.
2533 render_view_host()->MultiFilesSelected(std::vector<FilePath>());
2534}
2535
2536void TabContents::BeforeUnloadFiredFromRenderManager(
2537 bool proceed,
2538 bool* proceed_to_fire_unload) {
2539 if (delegate())
2540 delegate()->BeforeUnloadFired(this, proceed, proceed_to_fire_unload);
2541}
2542
2543void TabContents::UpdateRenderViewSizeForRenderManager() {
2544 // TODO(brettw) this is a hack. See TabContentsView::SizeContents.
2545 view_->SizeContents(view_->GetContainerSize());
2546}
2547
2548DOMUI* TabContents::CreateDOMUIForRenderManager(const GURL& url) {
[email protected]d82ed61e2009-06-16 02:46:222549 return DOMUIFactory::CreateDOMUIForURL(this, url);
[email protected]420ae012009-04-24 05:16:322550}
2551
2552NavigationEntry*
2553TabContents::GetLastCommittedNavigationEntryForRenderManager() {
2554 return controller_.GetLastCommittedEntry();
2555}
2556
2557bool TabContents::CreateRenderViewForRenderManager(
2558 RenderViewHost* render_view_host) {
[email protected]811bfe32009-07-01 08:46:252559 // If the pending navigation is to a DOMUI, tell the RenderView about any
2560 // bindings it will need enabled.
[email protected]420ae012009-04-24 05:16:322561 if (render_manager_.pending_dom_ui())
[email protected]811bfe32009-07-01 08:46:252562 render_view_host->AllowBindings(
2563 render_manager_.pending_dom_ui()->bindings());
[email protected]1adff062009-06-02 18:39:552564
[email protected]420ae012009-04-24 05:16:322565 RenderWidgetHostView* rwh_view = view_->CreateViewForWidget(render_view_host);
2566 if (!render_view_host->CreateRenderView())
2567 return false;
2568
2569 // Now that the RenderView has been created, we need to tell it its size.
2570 rwh_view->SetSize(view_->GetContainerSize());
2571
2572 UpdateMaxPageIDIfNecessary(render_view_host->site_instance(),
2573 render_view_host);
2574 return true;
2575}
2576
2577void TabContents::Observe(NotificationType type,
2578 const NotificationSource& source,
2579 const NotificationDetails& details) {
2580 switch (type.value) {
2581 case NotificationType::BOOKMARK_MODEL_LOADED:
2582 // BookmarkModel finished loading, fall through to update starred state.
2583 case NotificationType::URLS_STARRED: {
2584 // Somewhere, a URL has been starred.
2585 // Ignore notifications for profiles other than our current one.
2586 Profile* source_profile = Source<Profile>(source).ptr();
2587 if (!source_profile || !source_profile->IsSameProfile(profile()))
2588 return;
2589
2590 UpdateStarredStateForCurrentURL();
2591 break;
2592 }
2593 case NotificationType::PREF_CHANGED: {
2594 std::wstring* pref_name_in = Details<std::wstring>(details).ptr();
2595 DCHECK(Source<PrefService>(source).ptr() == profile()->GetPrefs());
2596 if (*pref_name_in == prefs::kAlternateErrorPagesEnabled) {
2597 UpdateAlternateErrorPageURL();
2598 } else if (*pref_name_in == prefs::kDefaultCharset ||
2599 StartsWithASCII(WideToUTF8(*pref_name_in), "webkit.webprefs.", true)
2600 ) {
2601 UpdateWebPreferences();
2602 } else {
2603 NOTREACHED() << "unexpected pref change notification" << *pref_name_in;
2604 }
2605 break;
2606 }
2607 case NotificationType::RENDER_WIDGET_HOST_DESTROYED:
2608 view_->RenderWidgetHostDestroyed(Source<RenderWidgetHost>(source).ptr());
2609 break;
2610
2611 case NotificationType::NAV_ENTRY_COMMITTED: {
2612 DCHECK(&controller_ == Source<NavigationController>(source).ptr());
2613
2614 NavigationController::LoadCommittedDetails& committed_details =
2615 *(Details<NavigationController::LoadCommittedDetails>(details).ptr());
2616 ExpireInfoBars(committed_details);
2617 break;
2618 }
2619
[email protected]d3ba77272009-09-03 00:06:092620#if defined(OS_LINUX)
2621 case NotificationType::BROWSER_THEME_CHANGED: {
2622 render_view_host()->SyncRendererPrefs();
2623 break;
2624 }
2625#endif
2626
[email protected]420ae012009-04-24 05:16:322627 default:
2628 NOTREACHED();
2629 }
2630}
[email protected]aacd7cc2009-09-02 14:51:102631
[email protected]41fc0322009-09-04 22:23:402632void TabContents::set_encoding(const std::string& encoding) {
[email protected]aacd7cc2009-09-02 14:51:102633 encoding_ = CharacterEncoding::GetCanonicalEncodingNameByAliasName(encoding);
2634}