blob: 300be68d6883d14d0414b98cfd9a34c11141a8dd [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]beb440c2009-11-06 04:08:547#include "app/gfx/text_elider.h"
[email protected]a92b8642009-05-05 23:38:568#include "app/l10n_util.h"
[email protected]9929da92009-05-05 02:05:119#include "app/resource_bundle.h"
[email protected]420ae012009-04-24 05:16:3210#include "base/file_version_info.h"
11#include "base/process_util.h"
[email protected]96d185d2009-04-24 03:28:5412#include "base/string16.h"
[email protected]996fd702009-09-04 19:12:3713#include "base/string_util.h"
[email protected]96d185d2009-04-24 03:28:5414#include "base/time.h"
[email protected]21674df2009-10-28 19:51:2115#include "chrome/browser/autofill/autofill_manager.h"
[email protected]66ba4932009-06-04 19:22:1316#include "chrome/browser/blocked_popup_container.h"
[email protected]420ae012009-04-24 05:16:3217#include "chrome/browser/bookmarks/bookmark_model.h"
[email protected]96d185d2009-04-24 03:28:5418#include "chrome/browser/browser_process.h"
initial.commit09911bf2008-07-26 23:55:2919#include "chrome/browser/cert_store.h"
[email protected]aacd7cc2009-09-02 14:51:1020#include "chrome/browser/character_encoding.h"
[email protected]96d185d2009-04-24 03:28:5421#include "chrome/browser/debugger/devtools_manager.h"
[email protected]420ae012009-04-24 05:16:3222#include "chrome/browser/dom_operation_notification_details.h"
[email protected]96d185d2009-04-24 03:28:5423#include "chrome/browser/dom_ui/dom_ui.h"
24#include "chrome/browser/dom_ui/dom_ui_factory.h"
[email protected]bcef0dc02009-02-28 00:35:0225#include "chrome/browser/download/download_item_model.h"
[email protected]420ae012009-04-24 05:16:3226#include "chrome/browser/download/download_manager.h"
27#include "chrome/browser/download/download_request_manager.h"
[email protected]e7eaedde2009-09-25 20:09:4928#include "chrome/browser/external_protocol_handler.h"
[email protected]92c6f9b92009-10-24 04:35:0829#include "chrome/browser/extensions/extensions_service.h"
[email protected]0189bc722009-08-28 21:56:4830#include "chrome/browser/favicon_service.h"
[email protected]19289d02009-10-21 21:47:0031#include "chrome/browser/form_field_history_manager.h"
[email protected]420ae012009-04-24 05:16:3232#include "chrome/browser/google_util.h"
[email protected]8897c382009-05-06 17:53:2633#include "chrome/browser/hung_renderer_dialog.h"
[email protected]420ae012009-04-24 05:16:3234#include "chrome/browser/jsmessage_box_handler.h"
35#include "chrome/browser/load_from_memory_cache_details.h"
36#include "chrome/browser/load_notification_details.h"
[email protected]84abba62009-10-07 17:01:4437#include "chrome/browser/metrics/metric_event_duration_details.h"
[email protected]ea0e80892009-08-22 00:36:0538#include "chrome/browser/modal_html_dialog_delegate.h"
[email protected]c8865482009-07-23 20:40:1039#include "chrome/browser/omnibox_search_hint.h"
[email protected]fdd61c62009-04-22 19:22:5740#include "chrome/browser/password_manager/password_manager.h"
41#include "chrome/browser/plugin_installer.h"
[email protected]ce560f82009-06-03 09:39:4442#include "chrome/browser/profile.h"
[email protected]1db6ff152009-10-12 15:32:0743#include "chrome/browser/renderer_host/render_process_host.h"
[email protected]96d185d2009-04-24 03:28:5444#include "chrome/browser/renderer_host/render_widget_host_view.h"
[email protected]fd694982009-07-03 17:32:3745#include "chrome/browser/renderer_host/resource_request_details.h"
[email protected]1db6ff152009-10-12 15:32:0746#include "chrome/browser/renderer_host/site_instance.h"
[email protected]96d185d2009-04-24 03:28:5447#include "chrome/browser/renderer_host/web_cache_manager.h"
[email protected]bd1ad682009-05-15 22:19:1748#include "chrome/browser/tab_contents/infobar_delegate.h"
[email protected]7e383692009-06-12 19:14:5449#include "chrome/browser/tab_contents/interstitial_page.h"
[email protected]f3ec7742009-01-15 00:59:1650#include "chrome/browser/tab_contents/navigation_entry.h"
[email protected]de34a962009-06-26 23:22:4351#include "chrome/browser/tab_contents/provisional_load_details.h"
[email protected]f3ec7742009-01-15 00:59:1652#include "chrome/browser/tab_contents/tab_contents_delegate.h"
[email protected]fdd61c62009-04-22 19:22:5753#include "chrome/browser/tab_contents/tab_contents_view.h"
[email protected]58dca552009-06-17 00:35:0254#include "chrome/browser/tab_contents/thumbnail_generator.h"
[email protected]449478302009-06-09 20:04:2855#include "chrome/browser/thumbnail_store.h"
[email protected]420ae012009-04-24 05:16:3256#include "chrome/browser/search_engines/template_url_fetcher.h"
57#include "chrome/browser/search_engines/template_url_model.h"
[email protected]35f7d212009-04-29 21:19:2758#include "chrome/common/chrome_switches.h"
[email protected]5d246db22009-10-27 06:17:5759#include "chrome/common/extensions/extension_action.h"
[email protected]bfd04a62009-02-01 18:16:5660#include "chrome/common/notification_service.h"
[email protected]89d6e6e2009-12-03 23:06:5161#include "chrome/common/platform_util.h"
initial.commit09911bf2008-07-26 23:55:2962#include "chrome/common/pref_names.h"
[email protected]1eb89e82008-08-15 12:27:0363#include "chrome/common/pref_service.h"
[email protected]420ae012009-04-24 05:16:3264#include "chrome/common/render_messages.h"
[email protected]1ee614862009-07-06 20:29:1865#include "chrome/common/renderer_preferences.h"
[email protected]96d185d2009-04-24 03:28:5466#include "chrome/common/url_constants.h"
[email protected]8897c382009-05-06 17:53:2667#include "grit/generated_resources.h"
[email protected]420ae012009-04-24 05:16:3268#include "grit/locale_settings.h"
69#include "net/base/mime_util.h"
70#include "net/base/net_errors.h"
[email protected]d686e812009-06-03 19:10:2971#include "net/base/net_util.h"
[email protected]420ae012009-04-24 05:16:3272#include "net/base/registry_controlled_domain.h"
[email protected]1ee614862009-07-06 20:29:1873#include "webkit/glue/password_form.h"
74#include "webkit/glue/webpreferences.h"
[email protected]3e45ba92009-02-20 21:09:0075
[email protected]304f8792009-08-19 21:28:0976#if defined(OS_CHROMEOS)
77// For GdkScreen
78#include <gdk/gdk.h>
[email protected]0ba5c5e2009-08-20 16:47:1379#endif // defined(OS_CHROMEOS)
80
[email protected]420ae012009-04-24 05:16:3281// Cross-Site Navigations
82//
83// If a TabContents is told to navigate to a different web site (as determined
84// by SiteInstance), it will replace its current RenderViewHost with a new
85// RenderViewHost dedicated to the new SiteInstance. This works as follows:
86//
87// - Navigate determines whether the destination is cross-site, and if so,
88// it creates a pending_render_view_host_ and moves into the PENDING
89// RendererState.
90// - The pending RVH is "suspended," so that no navigation messages are sent to
91// its renderer until the onbeforeunload JavaScript handler has a chance to
92// run in the current RVH.
93// - The pending RVH tells CrossSiteRequestManager (a thread-safe singleton)
94// that it has a pending cross-site request. ResourceDispatcherHost will
95// check for this when the response arrives.
96// - The current RVH runs its onbeforeunload handler. If it returns false, we
97// cancel all the pending logic and go back to NORMAL. Otherwise we allow
98// the pending RVH to send the navigation request to its renderer.
99// - ResourceDispatcherHost receives a ResourceRequest on the IO thread. It
100// checks CrossSiteRequestManager to see that the RVH responsible has a
101// pending cross-site request, and then installs a CrossSiteEventHandler.
102// - When RDH receives a response, the BufferedEventHandler determines whether
103// it is a download. If so, it sends a message to the new renderer causing
104// it to cancel the request, and the download proceeds in the download
105// thread. For now, we stay in a PENDING state (with a pending RVH) until
[email protected]57c6a652009-05-04 07:58:34106// the next DidNavigate event for this TabContents. This isn't ideal, but it
[email protected]420ae012009-04-24 05:16:32107// doesn't affect any functionality.
108// - After RDH receives a response and determines that it is safe and not a
109// download, it pauses the response to first run the old page's onunload
110// handler. It does this by asynchronously calling the OnCrossSiteResponse
[email protected]57c6a652009-05-04 07:58:34111// method of TabContents on the UI thread, which sends a ClosePage message
[email protected]420ae012009-04-24 05:16:32112// to the current RVH.
113// - Once the onunload handler is finished, a ClosePage_ACK message is sent to
114// the ResourceDispatcherHost, who unpauses the response. Data is then sent
115// to the pending RVH.
116// - The pending renderer sends a FrameNavigate message that invokes the
117// DidNavigate method. This replaces the current RVH with the
118// pending RVH and goes back to the NORMAL RendererState.
119
120namespace {
121
122// Amount of time we wait between when a key event is received and the renderer
123// is queried for its state and pushed to the NavigationEntry.
124const int kQueryStateDelay = 5000;
125
126const int kSyncWaitDelay = 40;
127
[email protected]304f8792009-08-19 21:28:09128#if defined(OS_CHROMEOS)
129// If a popup window is bigger than this fraction of the screen on chrome os,
130// turn it into a tab
131const float kMaxWidthFactor = 0.5;
132const float kMaxHeightFactor = 0.6;
133#endif
134
[email protected]420ae012009-04-24 05:16:32135// If another javascript message box is displayed within
136// kJavascriptMessageExpectedDelay of a previous javascript message box being
137// dismissed, display an option to suppress future message boxes from this
138// contents.
139const int kJavascriptMessageExpectedDelay = 1000;
140
[email protected]420ae012009-04-24 05:16:32141// The list of prefs we want to observe.
142const wchar_t* kPrefsToObserve[] = {
143 prefs::kAlternateErrorPagesEnabled,
144 prefs::kWebKitJavaEnabled,
145 prefs::kWebKitJavascriptEnabled,
146 prefs::kWebKitLoadsImagesAutomatically,
147 prefs::kWebKitPluginsEnabled,
148 prefs::kWebKitUsesUniversalDetector,
149 prefs::kWebKitSerifFontFamily,
150 prefs::kWebKitSansSerifFontFamily,
151 prefs::kWebKitFixedFontFamily,
152 prefs::kWebKitDefaultFontSize,
153 prefs::kWebKitDefaultFixedFontSize,
154 prefs::kDefaultCharset
155 // kWebKitStandardFontIsSerif needs to be added
156 // if we let users pick which font to use, serif or sans-serif when
157 // no font is specified or a CSS generic family (serif or sans-serif)
158 // is not specified.
159};
160
161const int kPrefsToObserveLength = arraysize(kPrefsToObserve);
162
[email protected]420ae012009-04-24 05:16:32163// Returns true if the entry's transition type is FORM_SUBMIT.
164bool IsFormSubmit(const NavigationEntry* entry) {
165 return (PageTransition::StripQualifier(entry->transition_type()) ==
166 PageTransition::FORM_SUBMIT);
167}
168
169#if defined(OS_WIN)
170
171BOOL CALLBACK InvalidateWindow(HWND hwnd, LPARAM lparam) {
172 // Note: erase is required to properly paint some widgets borders. This can
173 // be seen with textfields.
174 InvalidateRect(hwnd, NULL, TRUE);
175 return TRUE;
176}
177#endif
178
[email protected]5e369672009-11-03 23:48:30179ViewMsg_Navigate_Params::NavigationType GetNavigationType(
180 Profile* profile, const NavigationEntry& entry, bool reload) {
181 if (reload)
182 return ViewMsg_Navigate_Params::RELOAD;
183
184 if (entry.restore_type() == NavigationEntry::RESTORE_LAST_SESSION &&
185 profile->DidLastSessionExitCleanly())
186 return ViewMsg_Navigate_Params::RESTORE;
187
188 return ViewMsg_Navigate_Params::NORMAL;
189}
190
191void MakeNavigateParams(Profile* profile, const NavigationEntry& entry,
192 bool reload, ViewMsg_Navigate_Params* params) {
[email protected]056de2d2009-06-26 16:41:34193 params->page_id = entry.page_id();
194 params->url = entry.url();
195 params->referrer = entry.referrer();
196 params->transition = entry.transition_type();
197 params->state = entry.content_state();
[email protected]5e369672009-11-03 23:48:30198 params->navigation_type = GetNavigationType(profile, entry, reload);
[email protected]056de2d2009-06-26 16:41:34199 params->request_time = base::Time::Now();
200}
201
[email protected]420ae012009-04-24 05:16:32202} // namespace
203
204// -----------------------------------------------------------------------------
205
[email protected]fdd61c62009-04-22 19:22:57206// static
207int TabContents::find_request_id_counter_ = -1;
208
[email protected]420ae012009-04-24 05:16:32209
[email protected]420ae012009-04-24 05:16:32210TabContents::TabContents(Profile* profile,
211 SiteInstance* site_instance,
212 int routing_id,
[email protected]34ac70502009-09-25 17:07:23213 const TabContents* base_tab_contents)
[email protected]b680ad22009-04-15 23:19:42214 : delegate_(NULL),
[email protected]66ba4932009-06-04 19:22:13215 ALLOW_THIS_IN_INITIALIZER_LIST(controller_(this, profile)),
216 ALLOW_THIS_IN_INITIALIZER_LIST(view_(
[email protected]d82ed61e2009-06-16 02:46:22217 TabContentsView::Create(this))),
218 ALLOW_THIS_IN_INITIALIZER_LIST(render_manager_(this, this)),
[email protected]fdd61c62009-04-22 19:22:57219 property_bag_(),
220 registrar_(),
[email protected]d82ed61e2009-06-16 02:46:22221 ALLOW_THIS_IN_INITIALIZER_LIST(printing_(*this)),
[email protected]fdd61c62009-04-22 19:22:57222 save_package_(),
223 cancelable_consumer_(),
[email protected]19289d02009-10-21 21:47:00224 form_field_history_manager_(),
[email protected]21674df2009-10-28 19:51:21225 autofill_manager_(),
[email protected]fdd61c62009-04-22 19:22:57226 password_manager_(),
227 plugin_installer_(),
[email protected]d82ed61e2009-06-16 02:46:22228 ALLOW_THIS_IN_INITIALIZER_LIST(fav_icon_helper_(this)),
[email protected]fdd61c62009-04-22 19:22:57229 select_file_dialog_(),
[email protected]d5f942ba2008-09-26 19:30:34230 is_loading_(false),
initial.commit09911bf2008-07-26 23:55:29231 is_crashed_(false),
[email protected]d5f942ba2008-09-26 19:30:34232 waiting_for_response_(false),
[email protected]d5f942ba2008-09-26 19:30:34233 max_page_id_(-1),
[email protected]fdd61c62009-04-22 19:22:57234 current_load_start_(),
235 load_state_(net::LOAD_STATE_IDLE),
236 load_state_host_(),
[email protected]094e5b22009-09-25 04:23:56237 upload_size_(0),
238 upload_position_(0),
[email protected]fdd61c62009-04-22 19:22:57239 received_page_title_(false),
240 is_starred_(false),
241 contents_mime_type_(),
242 encoding_(),
[email protected]332af7732009-03-11 13:21:35243 blocked_popups_(NULL),
[email protected]fdd61c62009-04-22 19:22:57244 infobar_delegates_(),
[email protected]fdd61c62009-04-22 19:22:57245 find_ui_active_(false),
246 find_op_aborted_(false),
247 current_find_request_id_(find_request_id_counter_++),
248 find_text_(),
[email protected]e491f1c2009-05-22 20:28:12249 last_search_case_sensitive_(false),
250 last_search_prepopulate_text_(NULL),
251 last_search_result_(),
[email protected]fdd61c62009-04-22 19:22:57252 capturing_contents_(false),
253 is_being_destroyed_(false),
254 notify_disconnection_(false),
255 history_requests_(),
256#if defined(OS_WIN)
257 message_box_active_(CreateEvent(NULL, TRUE, FALSE, NULL)),
258#endif
259 last_javascript_message_dismissal_(),
[email protected]c2e74fe82009-09-03 17:57:44260 suppress_javascript_messages_(false),
[email protected]7ab1e7d62009-10-14 23:32:01261 is_showing_before_unload_dialog_(false),
[email protected]c2e74fe82009-09-03 17:57:44262 opener_dom_ui_type_(DOMUIFactory::kNoDOMUI) {
[email protected]16779842009-07-08 23:45:29263#if defined(OS_CHROMEOS)
[email protected]58dca552009-06-17 00:35:02264 // Make sure the thumbnailer is started before starting the render manager.
265 // The thumbnailer will want to listen for RVH creations, one of which will
266 // happen in RVHManager::Init.
267 ThumbnailGenerator* generator = g_browser_process->GetThumbnailGenerator();
268 if (generator)
269 generator->StartThumbnailing();
270#endif
271
[email protected]12636df2009-09-28 22:32:21272 render_manager_.Init(profile, site_instance, routing_id);
[email protected]420ae012009-04-24 05:16:32273
[email protected]34ac70502009-09-25 17:07:23274 // We have the initial size of the view be based on the size of the passed in
275 // tab contents (normally a tab from the same window).
276 view_->CreateView(base_tab_contents ?
277 base_tab_contents->view()->GetContainerSize() : gfx::Size());
[email protected]420ae012009-04-24 05:16:32278
279 // Register for notifications about all interested prefs change.
280 PrefService* prefs = profile->GetPrefs();
281 if (prefs) {
282 for (int i = 0; i < kPrefsToObserveLength; ++i)
283 prefs->AddPrefObserver(kPrefsToObserve[i], this);
284 }
285
286 // Register for notifications about URL starredness changing on any profile.
287 registrar_.Add(this, NotificationType::URLS_STARRED,
288 NotificationService::AllSources());
289 registrar_.Add(this, NotificationType::BOOKMARK_MODEL_LOADED,
290 NotificationService::AllSources());
291 registrar_.Add(this, NotificationType::RENDER_WIDGET_HOST_DESTROYED,
292 NotificationService::AllSources());
[email protected]d3ba77272009-09-03 00:06:09293#if defined(OS_LINUX)
294 registrar_.Add(this, NotificationType::BROWSER_THEME_CHANGED,
295 NotificationService::AllSources());
296#endif
[email protected]420ae012009-04-24 05:16:32297
298 // Keep a global copy of the previous search string (if any).
299 static string16 global_last_search = string16();
[email protected]e491f1c2009-05-22 20:28:12300 last_search_prepopulate_text_ = &global_last_search;
[email protected]840b1512009-07-21 16:53:46301
[email protected]c8865482009-07-23 20:40:10302 // Set-up the showing of the omnibox search infobar if applicable.
303 if (OmniboxSearchHint::IsEnabled(profile))
304 omnibox_search_hint_.reset(new OmniboxSearchHint(this));
305
[email protected]840b1512009-07-21 16:53:46306 view_->InitRendererPrefs(&renderer_preferences_);
[email protected]332af7732009-03-11 13:21:35307}
initial.commit09911bf2008-07-26 23:55:29308
309TabContents::~TabContents() {
[email protected]420ae012009-04-24 05:16:32310 is_being_destroyed_ = true;
311
[email protected]bfe4c1582009-10-02 18:11:09312 // We don't want any notifications while we're running our destructor.
[email protected]420ae012009-04-24 05:16:32313 registrar_.RemoveAll();
314
315 // Unregister the notifications of all observed prefs change.
316 PrefService* prefs = profile()->GetPrefs();
317 if (prefs) {
318 for (int i = 0; i < kPrefsToObserveLength; ++i)
319 prefs->RemovePrefObserver(kPrefsToObserve[i], this);
320 }
321
[email protected]420ae012009-04-24 05:16:32322 NotifyDisconnected();
[email protected]e7b8d832009-11-20 22:41:00323 hung_renderer_dialog::HideForTabContents(this);
[email protected]420ae012009-04-24 05:16:32324
[email protected]420ae012009-04-24 05:16:32325 // First cleanly close all child windows.
326 // TODO(mpcomplete): handle case if MaybeCloseChildWindows() already asked
327 // some of these to close. CloseWindows is async, so it might get called
328 // twice before it runs.
329 int size = static_cast<int>(child_windows_.size());
330 for (int i = size - 1; i >= 0; --i) {
331 ConstrainedWindow* window = child_windows_[i];
332 if (window)
333 window->CloseConstrainedWindow();
334 }
335
[email protected]66ba4932009-06-04 19:22:13336 if (blocked_popups_)
337 blocked_popups_->Destroy();
338
[email protected]420ae012009-04-24 05:16:32339 // Notify any observer that have a reference on this tab contents.
340 NotificationService::current()->Notify(
341 NotificationType::TAB_CONTENTS_DESTROYED,
342 Source<TabContents>(this),
343 NotificationService::NoDetails());
344
[email protected]1e5e74f2009-05-27 20:55:12345 // Notify any lasting InfobarDelegates that have not yet been removed that
346 // whatever infobar they were handling in this TabContents has closed,
347 // because the TabContents is going away entirely.
348 // This must happen after the TAB_CONTENTS_DESTROYED notification as the
349 // notification may trigger infobars calls that access their delegate. (and
350 // some implementations of InfoBarDelegate do delete themselves on
351 // InfoBarClosed()).
352 for (int i = 0; i < infobar_delegate_count(); ++i) {
353 InfoBarDelegate* delegate = GetInfoBarDelegateAt(i);
354 delegate->InfoBarClosed();
355 }
356 infobar_delegates_.clear();
357
[email protected]420ae012009-04-24 05:16:32358 // TODO(brettw) this should be moved to the view.
359#if defined(OS_WIN)
360 // If we still have a window handle, destroy it. GetNativeView can return
361 // NULL if this contents was part of a window that closed.
362 if (GetNativeView())
363 ::DestroyWindow(GetNativeView());
364#endif
[email protected]7ab1e7d62009-10-14 23:32:01365
366 // OnCloseStarted isn't called in unit tests.
367 if (!tab_close_start_time_.is_null()) {
368 UMA_HISTOGRAM_TIMES("Tab.Close",
369 base::TimeTicks::Now() - tab_close_start_time_);
370 }
initial.commit09911bf2008-07-26 23:55:29371}
372
[email protected]d5f942ba2008-09-26 19:30:34373// static
374void TabContents::RegisterUserPrefs(PrefService* prefs) {
[email protected]420ae012009-04-24 05:16:32375 prefs->RegisterBooleanPref(prefs::kAlternateErrorPagesEnabled, true);
376
377 WebPreferences pref_defaults;
378 prefs->RegisterBooleanPref(prefs::kWebKitJavascriptEnabled,
379 pref_defaults.javascript_enabled);
380 prefs->RegisterBooleanPref(prefs::kWebKitWebSecurityEnabled,
381 pref_defaults.web_security_enabled);
382 prefs->RegisterBooleanPref(
383 prefs::kWebKitJavascriptCanOpenWindowsAutomatically, true);
384 prefs->RegisterBooleanPref(prefs::kWebKitLoadsImagesAutomatically,
385 pref_defaults.loads_images_automatically);
386 prefs->RegisterBooleanPref(prefs::kWebKitPluginsEnabled,
387 pref_defaults.plugins_enabled);
388 prefs->RegisterBooleanPref(prefs::kWebKitDomPasteEnabled,
389 pref_defaults.dom_paste_enabled);
390 prefs->RegisterBooleanPref(prefs::kWebKitShrinksStandaloneImagesToFit,
391 pref_defaults.shrinks_standalone_images_to_fit);
[email protected]0df30122009-06-03 12:13:08392 prefs->RegisterStringPref(prefs::kWebKitInspectorSettings,
[email protected]8922e1f2009-10-03 05:01:26393 UTF8ToWide(pref_defaults.inspector_settings));
[email protected]420ae012009-04-24 05:16:32394 prefs->RegisterBooleanPref(prefs::kWebKitTextAreasAreResizable,
395 pref_defaults.text_areas_are_resizable);
396 prefs->RegisterBooleanPref(prefs::kWebKitJavaEnabled,
397 pref_defaults.java_enabled);
398
399 prefs->RegisterLocalizedStringPref(prefs::kAcceptLanguages,
400 IDS_ACCEPT_LANGUAGES);
401 prefs->RegisterLocalizedStringPref(prefs::kDefaultCharset,
402 IDS_DEFAULT_ENCODING);
403 prefs->RegisterLocalizedBooleanPref(prefs::kWebKitStandardFontIsSerif,
404 IDS_STANDARD_FONT_IS_SERIF);
405 prefs->RegisterLocalizedStringPref(prefs::kWebKitFixedFontFamily,
406 IDS_FIXED_FONT_FAMILY);
407 prefs->RegisterLocalizedStringPref(prefs::kWebKitSerifFontFamily,
408 IDS_SERIF_FONT_FAMILY);
409 prefs->RegisterLocalizedStringPref(prefs::kWebKitSansSerifFontFamily,
410 IDS_SANS_SERIF_FONT_FAMILY);
411 prefs->RegisterLocalizedStringPref(prefs::kWebKitCursiveFontFamily,
412 IDS_CURSIVE_FONT_FAMILY);
413 prefs->RegisterLocalizedStringPref(prefs::kWebKitFantasyFontFamily,
414 IDS_FANTASY_FONT_FAMILY);
415 prefs->RegisterLocalizedIntegerPref(prefs::kWebKitDefaultFontSize,
416 IDS_DEFAULT_FONT_SIZE);
417 prefs->RegisterLocalizedIntegerPref(prefs::kWebKitDefaultFixedFontSize,
418 IDS_DEFAULT_FIXED_FONT_SIZE);
419 prefs->RegisterLocalizedIntegerPref(prefs::kWebKitMinimumFontSize,
420 IDS_MINIMUM_FONT_SIZE);
421 prefs->RegisterLocalizedIntegerPref(prefs::kWebKitMinimumLogicalFontSize,
422 IDS_MINIMUM_LOGICAL_FONT_SIZE);
423 prefs->RegisterLocalizedBooleanPref(prefs::kWebKitUsesUniversalDetector,
424 IDS_USES_UNIVERSAL_DETECTOR);
425 prefs->RegisterLocalizedStringPref(prefs::kStaticEncodings,
426 IDS_STATIC_ENCODING_LIST);
initial.commit09911bf2008-07-26 23:55:29427}
428
[email protected]13367f72009-07-08 16:39:02429// Returns true if contains content rendered by an extension.
430bool TabContents::HostsExtension() const {
431 return GetURL().SchemeIs(chrome::kExtensionScheme);
[email protected]d5f942ba2008-09-26 19:30:34432}
433
[email protected]6e95706d2009-04-23 22:55:01434PasswordManager* TabContents::GetPasswordManager() {
435 if (password_manager_.get() == NULL)
436 password_manager_.reset(new PasswordManager(this));
437 return password_manager_.get();
438}
439
440PluginInstaller* TabContents::GetPluginInstaller() {
441 if (plugin_installer_.get() == NULL)
442 plugin_installer_.reset(new PluginInstaller(this));
443 return plugin_installer_.get();
444}
445
[email protected]d5f942ba2008-09-26 19:30:34446const GURL& TabContents::GetURL() const {
447 // We may not have a navigation entry yet
[email protected]ce3fa3c2009-04-20 19:55:57448 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]ebe89e062009-08-13 23:16:54449 return entry ? entry->virtual_url() : GURL::EmptyGURL();
[email protected]d5f942ba2008-09-26 19:30:34450}
451
[email protected]96d185d2009-04-24 03:28:54452const string16& TabContents::GetTitle() const {
[email protected]4c6092c5b2009-06-06 00:23:55453 // Transient entries take precedence. They are used for interstitial pages
454 // that are shown on top of existing pages.
455 NavigationEntry* entry = controller_.GetTransientEntry();
456 if (entry)
457 return entry->GetTitleForDisplay(&controller_);
458
[email protected]96d185d2009-04-24 03:28:54459 DOMUI* our_dom_ui = render_manager_.pending_dom_ui() ?
460 render_manager_.pending_dom_ui() : render_manager_.dom_ui();
461 if (our_dom_ui) {
462 // Don't override the title in view source mode.
[email protected]4c6092c5b2009-06-06 00:23:55463 entry = controller_.GetActiveEntry();
[email protected]96d185d2009-04-24 03:28:54464 if (!(entry && entry->IsViewSourceMode())) {
465 // Give the DOM UI the chance to override our title.
466 const string16& title = our_dom_ui->overridden_title();
467 if (!title.empty())
468 return title;
469 }
470 }
471
472 // We use the title for the last committed entry rather than a pending
473 // navigation entry. For example, when the user types in a URL, we want to
474 // keep the old page's title until the new load has committed and we get a new
475 // title.
[email protected]96d185d2009-04-24 03:28:54476 entry = controller_.GetLastCommittedEntry();
477 if (entry)
478 return entry->GetTitleForDisplay(&controller_);
[email protected]96d185d2009-04-24 03:28:54479 return EmptyString16();
480}
481
[email protected]d5f942ba2008-09-26 19:30:34482int32 TabContents::GetMaxPageID() {
483 if (GetSiteInstance())
484 return GetSiteInstance()->max_page_id();
485 else
486 return max_page_id_;
487}
488
489void TabContents::UpdateMaxPageID(int32 page_id) {
490 // Ensure both the SiteInstance and RenderProcessHost update their max page
[email protected]57c6a652009-05-04 07:58:34491 // IDs in sync. Only TabContents will also have site instances, except during
[email protected]d5f942ba2008-09-26 19:30:34492 // testing.
493 if (GetSiteInstance())
494 GetSiteInstance()->UpdateMaxPageID(page_id);
[email protected]57c6a652009-05-04 07:58:34495 process()->UpdateMaxPageID(page_id);
[email protected]d5f942ba2008-09-26 19:30:34496}
497
[email protected]96d185d2009-04-24 03:28:54498SiteInstance* TabContents::GetSiteInstance() const {
499 return render_manager_.current_host()->site_instance();
500}
501
[email protected]d5f942ba2008-09-26 19:30:34502const std::wstring TabContents::GetDefaultTitle() const {
503 return l10n_util::GetString(IDS_DEFAULT_TAB_TITLE);
504}
505
[email protected]96d185d2009-04-24 03:28:54506bool TabContents::ShouldDisplayURL() {
[email protected]8ab24cc2009-11-13 20:56:15507 // Don't hide the url in view source mode and with interstitials.
[email protected]96d185d2009-04-24 03:28:54508 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]8ab24cc2009-11-13 20:56:15509 if (entry && (entry->IsViewSourceMode() ||
510 entry->page_type() == NavigationEntry::INTERSTITIAL_PAGE)) {
[email protected]96d185d2009-04-24 03:28:54511 return true;
[email protected]8ab24cc2009-11-13 20:56:15512 }
[email protected]83e3895b2009-06-11 00:07:16513
[email protected]96d185d2009-04-24 03:28:54514 DOMUI* dom_ui = GetDOMUIForCurrentState();
515 if (dom_ui)
516 return !dom_ui->should_hide_url();
517 return true;
518}
519
[email protected]d5f942ba2008-09-26 19:30:34520SkBitmap TabContents::GetFavIcon() const {
521 // Like GetTitle(), we also want to use the favicon for the last committed
522 // entry rather than a pending navigation entry.
[email protected]ce3fa3c2009-04-20 19:55:57523 NavigationEntry* entry = controller_.GetTransientEntry();
[email protected]cbab76d2008-10-13 22:42:47524 if (entry)
525 return entry->favicon().bitmap();
526
[email protected]ce3fa3c2009-04-20 19:55:57527 entry = controller_.GetLastCommittedEntry();
[email protected]d5f942ba2008-09-26 19:30:34528 if (entry)
529 return entry->favicon().bitmap();
[email protected]d5f942ba2008-09-26 19:30:34530 return SkBitmap();
531}
532
[email protected]0b303cc2009-09-28 22:35:15533bool TabContents::FavIconIsValid() const {
534 NavigationEntry* entry = controller_.GetTransientEntry();
535 if (entry)
536 return entry->favicon().is_valid();
537
538 entry = controller_.GetLastCommittedEntry();
539 if (entry)
540 return entry->favicon().is_valid();
541
542 return false;
543}
544
[email protected]96d185d2009-04-24 03:28:54545bool TabContents::ShouldDisplayFavIcon() {
546 // Always display a throbber during pending loads.
547 if (controller_.GetLastCommittedEntry() && controller_.pending_entry())
548 return true;
549
550 DOMUI* dom_ui = GetDOMUIForCurrentState();
551 if (dom_ui)
552 return !dom_ui->hide_favicon();
553 return true;
554}
555
[email protected]96d185d2009-04-24 03:28:54556std::wstring TabContents::GetStatusText() const {
557 if (!is_loading() || load_state_ == net::LOAD_STATE_IDLE)
558 return std::wstring();
559
560 switch (load_state_) {
561 case net::LOAD_STATE_WAITING_FOR_CACHE:
562 return l10n_util::GetString(IDS_LOAD_STATE_WAITING_FOR_CACHE);
563 case net::LOAD_STATE_RESOLVING_PROXY_FOR_URL:
564 return l10n_util::GetString(IDS_LOAD_STATE_RESOLVING_PROXY_FOR_URL);
565 case net::LOAD_STATE_RESOLVING_HOST:
566 return l10n_util::GetString(IDS_LOAD_STATE_RESOLVING_HOST);
567 case net::LOAD_STATE_CONNECTING:
568 return l10n_util::GetString(IDS_LOAD_STATE_CONNECTING);
569 case net::LOAD_STATE_SENDING_REQUEST:
[email protected]094e5b22009-09-25 04:23:56570 if (upload_size_)
571 return l10n_util::GetStringF(
572 IDS_LOAD_STATE_SENDING_REQUEST_WITH_PROGRESS,
573 static_cast<int>((100 * upload_position_) / upload_size_));
574 else
575 return l10n_util::GetString(IDS_LOAD_STATE_SENDING_REQUEST);
[email protected]96d185d2009-04-24 03:28:54576 case net::LOAD_STATE_WAITING_FOR_RESPONSE:
577 return l10n_util::GetStringF(IDS_LOAD_STATE_WAITING_FOR_RESPONSE,
578 load_state_host_);
579 // Ignore net::LOAD_STATE_READING_RESPONSE and net::LOAD_STATE_IDLE
580 case net::LOAD_STATE_IDLE:
581 case net::LOAD_STATE_READING_RESPONSE:
582 break;
583 }
584
585 return std::wstring();
586}
587
[email protected]d5f942ba2008-09-26 19:30:34588void TabContents::SetIsCrashed(bool state) {
589 if (state == is_crashed_)
590 return;
591
592 is_crashed_ = state;
[email protected]c9cd2222009-05-06 05:16:50593 NotifyNavigationStateChanged(INVALIDATE_TAB);
[email protected]d5f942ba2008-09-26 19:30:34594}
595
[email protected]744ef172009-10-16 21:53:46596void TabContents::PageActionStateChanged() {
597 NotifyNavigationStateChanged(TabContents::INVALIDATE_PAGE_ACTIONS);
[email protected]d7eaf572009-07-01 21:57:00598}
[email protected]ce5c4502009-05-06 16:46:11599
[email protected]d5f942ba2008-09-26 19:30:34600void TabContents::NotifyNavigationStateChanged(unsigned changed_flags) {
601 if (delegate_)
602 delegate_->NavigationStateChanged(this, changed_flags);
603}
604
[email protected]96d185d2009-04-24 03:28:54605void TabContents::DidBecomeSelected() {
606 controller_.SetActive(true);
[email protected]789e9152009-08-04 21:59:43607 if (render_widget_host_view()) {
[email protected]96d185d2009-04-24 03:28:54608 render_widget_host_view()->DidBecomeSelected();
[email protected]789e9152009-08-04 21:59:43609#if defined(OS_MACOSX)
610 render_widget_host_view()->SetActive(true);
611#endif
612 }
[email protected]96d185d2009-04-24 03:28:54613
[email protected]76543b92009-08-31 17:27:45614 WebCacheManager::GetInstance()->ObserveActivity(process()->id());
[email protected]96d185d2009-04-24 03:28:54615}
616
617void TabContents::WasHidden() {
618 if (!capturing_contents()) {
619 // |render_view_host()| can be NULL if the user middle clicks a link to open
620 // a tab in then background, then closes the tab before selecting it. This
621 // is because closing the tab calls TabContents::Destroy(), which removes
622 // the |render_view_host()|; then when we actually destroy the window,
623 // OnWindowPosChanged() notices and calls HideContents() (which calls us).
624 if (render_widget_host_view())
625 render_widget_host_view()->WasHidden();
[email protected]96d185d2009-04-24 03:28:54626 }
627
628 NotificationService::current()->Notify(
629 NotificationType::TAB_CONTENTS_HIDDEN,
630 Source<TabContents>(this),
631 NotificationService::NoDetails());
632}
633
[email protected]d5f942ba2008-09-26 19:30:34634void TabContents::Activate() {
635 if (delegate_)
636 delegate_->ActivateContents(this);
637}
638
[email protected]96d185d2009-04-24 03:28:54639void TabContents::ShowContents() {
640 if (render_widget_host_view())
641 render_widget_host_view()->DidBecomeSelected();
[email protected]96d185d2009-04-24 03:28:54642}
643
644void TabContents::HideContents() {
645 // TODO(pkasting): http://b/1239839 Right now we purposefully don't call
646 // our superclass HideContents(), because some callers want to be very picky
647 // about the order in which these get called. In addition to making the code
648 // here practically impossible to understand, this also means we end up
649 // calling TabContents::WasHidden() twice if callers call both versions of
[email protected]57c6a652009-05-04 07:58:34650 // HideContents() on a TabContents.
[email protected]96d185d2009-04-24 03:28:54651 WasHidden();
652}
653
[email protected]c0588052008-10-27 23:01:50654void TabContents::OpenURL(const GURL& url, const GURL& referrer,
[email protected]d5f942ba2008-09-26 19:30:34655 WindowOpenDisposition disposition,
656 PageTransition::Type transition) {
657 if (delegate_)
[email protected]c0588052008-10-27 23:01:50658 delegate_->OpenURLFromTab(this, url, referrer, disposition, transition);
[email protected]d5f942ba2008-09-26 19:30:34659}
660
661bool TabContents::NavigateToPendingEntry(bool reload) {
[email protected]96d185d2009-04-24 03:28:54662 const NavigationEntry& entry = *controller_.pending_entry();
663
664 RenderViewHost* dest_render_view_host = render_manager_.Navigate(entry);
665 if (!dest_render_view_host)
666 return false; // Unable to create the desired render view host.
667
668 // Tell DevTools agent that it is attached prior to the navigation.
[email protected]d9f9b792009-06-24 13:17:12669 DevToolsManager* devtools_manager = DevToolsManager::GetInstance();
[email protected]7e8e6b62009-05-08 11:28:32670 if (devtools_manager) { // NULL in unit tests.
[email protected]684b4ba22009-05-07 06:27:41671 devtools_manager->OnNavigatingToPendingEntry(
672 render_view_host(),
673 dest_render_view_host,
674 controller_.pending_entry()->url());
[email protected]7e8e6b62009-05-08 11:28:32675 }
[email protected]96d185d2009-04-24 03:28:54676
677 // Used for page load time metrics.
678 current_load_start_ = base::TimeTicks::Now();
679
680 // Navigate in the desired RenderViewHost.
[email protected]056de2d2009-06-26 16:41:34681 ViewMsg_Navigate_Params navigate_params;
[email protected]5e369672009-11-03 23:48:30682 MakeNavigateParams(profile(), entry, reload, &navigate_params);
[email protected]056de2d2009-06-26 16:41:34683 dest_render_view_host->Navigate(navigate_params);
[email protected]96d185d2009-04-24 03:28:54684
685 if (entry.page_id() == -1) {
686 // HACK!! This code suppresses javascript: URLs from being added to
687 // session history, which is what we want to do for javascript: URLs that
688 // do not generate content. What we really need is a message from the
689 // renderer telling us that a new page was not created. The same message
690 // could be used for mailto: URLs and the like.
691 if (entry.url().SchemeIs(chrome::kJavaScriptScheme))
692 return false;
693 }
694
695 // Clear any provisional password saves - this stops password infobars
696 // showing up on pages the user navigates to while the right page is
697 // loading.
698 GetPasswordManager()->ClearProvisionalSave();
699
700 if (reload && !profile()->IsOffTheRecord()) {
[email protected]0189bc722009-08-28 21:56:48701 FaviconService* favicon_service =
702 profile()->GetFaviconService(Profile::IMPLICIT_ACCESS);
703 if (favicon_service)
704 favicon_service->SetFaviconOutOfDateForPage(entry.url());
[email protected]96d185d2009-04-24 03:28:54705 }
706
[email protected]d5f942ba2008-09-26 19:30:34707 return true;
708}
709
[email protected]96d185d2009-04-24 03:28:54710void TabContents::Stop() {
711 render_manager_.Stop();
712 printing_.Stop();
713}
714
715void TabContents::Cut() {
716 render_view_host()->Cut();
717}
718
719void TabContents::Copy() {
720 render_view_host()->Copy();
721}
722
[email protected]c0cc3092009-09-12 08:27:27723#if defined(OS_MACOSX)
[email protected]a954bf72009-09-12 07:30:35724void TabContents::CopyToFindPboard() {
725 render_view_host()->CopyToFindPboard();
726}
[email protected]c0cc3092009-09-12 08:27:27727#endif
[email protected]a954bf72009-09-12 07:30:35728
[email protected]96d185d2009-04-24 03:28:54729void TabContents::Paste() {
730 render_view_host()->Paste();
731}
732
[email protected]7e786822009-11-07 00:00:27733#if defined(OS_MACOSX)
734void TabContents::PasteAndMatchStyle() {
735 render_view_host()->ForwardEditCommand("PasteAndMatchStyle", "");
736}
737#endif
738
[email protected]96d185d2009-04-24 03:28:54739void TabContents::DisassociateFromPopupCount() {
740 render_view_host()->DisassociateFromPopupCount();
741}
742
743TabContents* TabContents::Clone() {
744 // We create a new SiteInstance so that the new tab won't share processes
745 // with the old one. This can be changed in the future if we need it to share
746 // processes for some reason.
[email protected]420ae012009-04-24 05:16:32747 TabContents* tc = new TabContents(profile(),
[email protected]96d185d2009-04-24 03:28:54748 SiteInstance::CreateSiteInstance(profile()),
[email protected]12636df2009-09-28 22:32:21749 MSG_ROUTING_NONE, this);
[email protected]96d185d2009-04-24 03:28:54750 tc->controller().CopyStateFrom(controller_);
751 return tc;
752}
753
[email protected]4d677202009-07-19 07:37:12754void TabContents::ShowPageInfo(const GURL& url,
755 const NavigationEntry::SSLStatus& ssl,
756 bool show_history) {
757 if (!delegate_)
758 return;
759
[email protected]bb678332009-07-21 00:15:50760 delegate_->ShowPageInfo(profile(), url, ssl, show_history);
[email protected]4d677202009-07-19 07:37:12761}
762
initial.commit09911bf2008-07-26 23:55:29763ConstrainedWindow* TabContents::CreateConstrainedDialog(
[email protected]e8382172009-06-19 22:16:28764 ConstrainedWindowDelegate* delegate) {
initial.commit09911bf2008-07-26 23:55:29765 ConstrainedWindow* window =
[email protected]e8382172009-06-19 22:16:28766 ConstrainedWindow::CreateConstrainedDialog(this, delegate);
initial.commit09911bf2008-07-26 23:55:29767 child_windows_.push_back(window);
768 return window;
769}
770
771void TabContents::AddNewContents(TabContents* new_contents,
772 WindowOpenDisposition disposition,
773 const gfx::Rect& initial_pos,
[email protected]c88a70fe2009-05-05 20:00:22774 bool user_gesture,
775 const GURL& creator_url) {
initial.commit09911bf2008-07-26 23:55:29776 if (!delegate_)
777 return;
778
[email protected]35f7d212009-04-29 21:19:27779 if ((disposition == NEW_POPUP) && !user_gesture &&
780 !CommandLine::ForCurrentProcess()->HasSwitch(
781 switches::kDisablePopupBlocking)) {
[email protected]2c4410d2009-05-06 23:46:22782 // Unrequested popups from normal pages are constrained unless they're in
[email protected]b6c874582009-05-08 19:38:31783 // the whitelist. The popup owner will handle checking this.
784 delegate_->GetConstrainingContents(this)->AddPopup(new_contents,
785 initial_pos,
786 creator_url.is_valid() ? creator_url.host() : std::string());
787 } else {
[email protected]304f8792009-08-19 21:28:09788#if defined(OS_CHROMEOS)
789 if (disposition == NEW_POPUP) {
790 // If the popup is bigger than a given factor of the screen, then
791 // turn it into a foreground tab (on chrome os only)
792 GdkScreen* screen = gdk_screen_get_default();
793 int max_width = gdk_screen_get_width(screen) * kMaxWidthFactor;
794 int max_height = gdk_screen_get_height(screen) * kMaxHeightFactor;
795 if (initial_pos.width() > max_width ||
796 initial_pos.height() > max_height) {
797 disposition = NEW_FOREGROUND_TAB;
798 }
799 }
800#endif
801
[email protected]0aa55312008-10-17 21:53:08802 new_contents->DisassociateFromPopupCount();
initial.commit09911bf2008-07-26 23:55:29803 delegate_->AddNewContents(this, new_contents, disposition, initial_pos,
804 user_gesture);
[email protected]7f37dd92009-09-15 16:35:49805 NotificationService::current()->Notify(
806 NotificationType::TAB_ADDED,
807 Source<TabContentsDelegate>(delegate_),
808 Details<TabContents>(this));
initial.commit09911bf2008-07-26 23:55:29809 }
[email protected]b6c874582009-05-08 19:38:31810 PopupNotificationVisibilityChanged(ShowingBlockedPopupNotification());
initial.commit09911bf2008-07-26 23:55:29811}
812
initial.commit09911bf2008-07-26 23:55:29813void TabContents::CloseAllSuppressedPopups() {
[email protected]d6598c052008-11-05 19:03:25814 if (blocked_popups_)
[email protected]2c4410d2009-05-06 23:46:22815 blocked_popups_->CloseAll();
initial.commit09911bf2008-07-26 23:55:29816}
817
[email protected]912256b32009-09-18 09:47:35818void TabContents::ExecuteCode(int request_id, const std::string& extension_id,
[email protected]20ad2692009-11-20 18:27:20819 bool is_js_code, const std::string& code_string,
820 bool all_frames) {
[email protected]912256b32009-09-18 09:47:35821 render_view_host()->Send(
822 new ViewMsg_ExecuteCode(render_view_host()->routing_id(), request_id,
[email protected]20ad2692009-11-20 18:27:20823 extension_id, is_js_code, code_string,
824 all_frames));
[email protected]912256b32009-09-18 09:47:35825}
826
[email protected]96d185d2009-04-24 03:28:54827void TabContents::PopupNotificationVisibilityChanged(bool visible) {
828 render_view_host()->PopupNotificationVisibilityChanged(visible);
829}
830
[email protected]5c9e97a2009-09-09 23:48:30831gfx::NativeView TabContents::GetContentNativeView() const {
[email protected]96d185d2009-04-24 03:28:54832 return view_->GetContentNativeView();
833}
834
835gfx::NativeView TabContents::GetNativeView() const {
836 return view_->GetNativeView();
837}
838
839void TabContents::GetContainerBounds(gfx::Rect *out) const {
840 view_->GetContainerBounds(out);
841}
842
843void TabContents::Focus() {
844 view_->Focus();
845}
846
[email protected]90daadb42009-06-08 21:27:28847void TabContents::FocusThroughTabTraversal(bool reverse) {
[email protected]7e383692009-06-12 19:14:54848 if (showing_interstitial_page()) {
849 render_manager_.interstitial_page()->FocusThroughTabTraversal(reverse);
850 return;
851 }
[email protected]96d185d2009-04-24 03:28:54852 render_view_host()->SetInitialFocus(reverse);
853}
854
855bool TabContents::FocusLocationBarByDefault() {
856 DOMUI* dom_ui = GetDOMUIForCurrentState();
857 if (dom_ui)
858 return dom_ui->focus_location_bar_by_default();
[email protected]3e3f0eb2009-06-22 18:33:43859 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]e0d481582009-09-15 21:06:25860 if (entry && entry->url() == GURL(chrome::kAboutBlankURL))
[email protected]3e3f0eb2009-06-22 18:33:43861 return true;
[email protected]96d185d2009-04-24 03:28:54862 return false;
863}
864
[email protected]616ed5a2008-11-21 22:27:24865void TabContents::AddInfoBar(InfoBarDelegate* delegate) {
866 // Look through the existing InfoBarDelegates we have for a match. If we've
867 // already got one that matches, then we don't add the new one.
[email protected]f86a07022008-11-25 01:06:05868 for (int i = 0; i < infobar_delegate_count(); ++i) {
[email protected]616ed5a2008-11-21 22:27:24869 if (GetInfoBarDelegateAt(i)->EqualsDelegate(delegate))
870 return;
871 }
872
873 infobar_delegates_.push_back(delegate);
[email protected]bfd04a62009-02-01 18:16:56874 NotificationService::current()->Notify(
875 NotificationType::TAB_CONTENTS_INFOBAR_ADDED,
876 Source<TabContents>(this),
877 Details<InfoBarDelegate>(delegate));
[email protected]616ed5a2008-11-21 22:27:24878
879 // Add ourselves as an observer for navigations the first time a delegate is
880 // added. We use this notification to expire InfoBars that need to expire on
881 // page transitions.
882 if (infobar_delegates_.size() == 1) {
[email protected]bfd04a62009-02-01 18:16:56883 registrar_.Add(this, NotificationType::NAV_ENTRY_COMMITTED,
[email protected]ce3fa3c2009-04-20 19:55:57884 Source<NavigationController>(&controller_));
[email protected]616ed5a2008-11-21 22:27:24885 }
886}
887
888void TabContents::RemoveInfoBar(InfoBarDelegate* delegate) {
889 std::vector<InfoBarDelegate*>::iterator it =
890 find(infobar_delegates_.begin(), infobar_delegates_.end(), delegate);
891 if (it != infobar_delegates_.end()) {
892 InfoBarDelegate* delegate = *it;
[email protected]bfd04a62009-02-01 18:16:56893 NotificationService::current()->Notify(
894 NotificationType::TAB_CONTENTS_INFOBAR_REMOVED,
895 Source<TabContents>(this),
896 Details<InfoBarDelegate>(delegate));
[email protected]f86a07022008-11-25 01:06:05897 infobar_delegates_.erase(it);
[email protected]616ed5a2008-11-21 22:27:24898
[email protected]6a02963e2009-01-06 16:58:03899 // Remove ourselves as an observer if we are tracking no more InfoBars.
900 if (infobar_delegates_.empty()) {
[email protected]bfd04a62009-02-01 18:16:56901 registrar_.Remove(this, NotificationType::NAV_ENTRY_COMMITTED,
[email protected]ce3fa3c2009-04-20 19:55:57902 Source<NavigationController>(&controller_));
[email protected]6a02963e2009-01-06 16:58:03903 }
[email protected]616ed5a2008-11-21 22:27:24904 }
905}
[email protected]3edd9522009-03-04 22:19:41906
[email protected]f81821c2009-08-07 01:29:47907void TabContents::ReplaceInfoBar(InfoBarDelegate* old_delegate,
908 InfoBarDelegate* new_delegate) {
909 std::vector<InfoBarDelegate*>::iterator it =
910 find(infobar_delegates_.begin(), infobar_delegates_.end(), old_delegate);
911 DCHECK(it != infobar_delegates_.end());
912
913 // Notify the container about the change of plans.
914 scoped_ptr<std::pair<InfoBarDelegate*, InfoBarDelegate*> > details(
915 new std::pair<InfoBarDelegate*, InfoBarDelegate*>(
916 old_delegate, new_delegate));
917 NotificationService::current()->Notify(
918 NotificationType::TAB_CONTENTS_INFOBAR_REPLACED,
919 Source<TabContents>(this),
920 Details<std::pair<InfoBarDelegate*, InfoBarDelegate*> >(details.get()));
921
922 // Remove the old one.
923 infobar_delegates_.erase(it);
924
925 // Add the new one.
926 infobar_delegates_.push_back(new_delegate);
927}
928
[email protected]20ede072009-10-06 00:28:35929bool TabContents::ShouldShowBookmarkBar() {
[email protected]aa9196f2009-10-22 16:20:27930 if (showing_interstitial_page())
931 return false;
932
[email protected]96d185d2009-04-24 03:28:54933 // See GetDOMUIForCurrentState() comment for more info. This case is very
934 // similar, but for non-first loads, we want to use the committed entry. This
935 // is so the bookmarks bar disappears at the same time the page does.
936 if (controller_.GetLastCommittedEntry()) {
937 // Not the first load, always use the committed DOM UI.
938 if (render_manager_.dom_ui())
939 return render_manager_.dom_ui()->force_bookmark_bar_visible();
940 return false; // Default.
941 }
942
943 // When it's the first load, we know either the pending one or the committed
944 // one will have the DOM UI in it (see GetDOMUIForCurrentState), and only one
945 // of them will be valid, so we can just check both.
946 if (render_manager_.pending_dom_ui())
947 return render_manager_.pending_dom_ui()->force_bookmark_bar_visible();
948 if (render_manager_.dom_ui())
949 return render_manager_.dom_ui()->force_bookmark_bar_visible();
950 return false; // Default.
951}
952
[email protected]d8f16ae2009-08-30 03:47:26953bool TabContents::IsExtensionShelfAlwaysVisible() {
954 // See GetDOMUIForCurrentState() comment for more info. This case is very
955 // similar, but for non-first loads, we want to use the committed entry. This
956 // is so the bookmarks bar disappears at the same time the page does.
957 if (controller_.GetLastCommittedEntry()) {
958 // Not the first load, always use the committed DOM UI.
959 if (render_manager_.dom_ui())
960 return render_manager_.dom_ui()->force_extension_shelf_visible();
961 return false; // Default.
962 }
963
964 // When it's the first load, we know either the pending one or the committed
965 // one will have the DOM UI in it (see GetDOMUIForCurrentState), and only one
966 // of them will be valid, so we can just check both.
967 if (render_manager_.pending_dom_ui())
968 return render_manager_.pending_dom_ui()->force_extension_shelf_visible();
969 if (render_manager_.dom_ui())
970 return render_manager_.dom_ui()->force_extension_shelf_visible();
971 return false; // Default.
972}
973
[email protected]3edd9522009-03-04 22:19:41974void TabContents::ToolbarSizeChanged(bool is_animating) {
975 TabContentsDelegate* d = delegate();
976 if (d)
977 d->ToolbarSizeChanged(this, is_animating);
978}
[email protected]616ed5a2008-11-21 22:27:24979
[email protected]fc6fb7fb2009-11-07 02:35:04980bool TabContents::CanDownload(int request_id) {
981 TabContentsDelegate* d = delegate();
982 if (d)
983 return d->CanDownload(request_id);
984 return true;
985}
986
[email protected]d5f942ba2008-09-26 19:30:34987void TabContents::OnStartDownload(DownloadItem* download) {
988 DCHECK(download);
[email protected]d5f942ba2008-09-26 19:30:34989
990 // Download in a constrained popup is shown in the tab that opened it.
[email protected]b6c874582009-05-08 19:38:31991 TabContents* tab_contents = delegate()->GetConstrainingContents(this);
[email protected]d5f942ba2008-09-26 19:30:34992
[email protected]59560e0b2009-06-04 03:30:22993 if (tab_contents && tab_contents->delegate())
994 tab_contents->delegate()->OnStartDownload(download);
[email protected]3edd9522009-03-04 22:19:41995}
[email protected]3edd9522009-03-04 22:19:41996
[email protected]d5f942ba2008-09-26 19:30:34997void TabContents::WillClose(ConstrainedWindow* window) {
998 ConstrainedWindowList::iterator it =
999 find(child_windows_.begin(), child_windows_.end(), window);
1000 if (it != child_windows_.end())
1001 child_windows_.erase(it);
[email protected]66ba4932009-06-04 19:22:131002}
[email protected]d5f942ba2008-09-26 19:30:341003
[email protected]66ba4932009-06-04 19:22:131004void TabContents::WillCloseBlockedPopupContainer(
1005 BlockedPopupContainer* container) {
1006 DCHECK(blocked_popups_ == container);
1007 blocked_popups_ = NULL;
[email protected]d5f942ba2008-09-26 19:30:341008}
1009
[email protected]d5f942ba2008-09-26 19:30:341010void TabContents::DidMoveOrResize(ConstrainedWindow* window) {
[email protected]332af7732009-03-11 13:21:351011#if defined(OS_WIN)
[email protected]92edc472009-02-10 20:32:061012 UpdateWindow(GetNativeView());
[email protected]332af7732009-03-11 13:21:351013#endif
[email protected]d5f942ba2008-09-26 19:30:341014}
1015
[email protected]c1dc9f132009-11-20 23:13:211016void TabContents::StartFinding(string16 search_string,
[email protected]e491f1c2009-05-22 20:28:121017 bool forward_direction,
1018 bool case_sensitive) {
[email protected]c1dc9f132009-11-20 23:13:211019 // If search_string is empty, it means FindNext was pressed with a keyboard
[email protected]6e95706d2009-04-23 22:55:011020 // shortcut so unless we have something to search for we return early.
[email protected]c1dc9f132009-11-20 23:13:211021 if (search_string.empty() && find_text_.empty()) {
[email protected]e491f1c2009-05-22 20:28:121022 if (last_search_prepopulate_text_->empty())
1023 return;
1024 // Try whatever we searched for last in any tab.
[email protected]c1dc9f132009-11-20 23:13:211025 search_string = *last_search_prepopulate_text_;
[email protected]e491f1c2009-05-22 20:28:121026 }
[email protected]6e95706d2009-04-23 22:55:011027
[email protected]101c90e2009-10-26 21:14:441028 // Keep track of the previous search.
1029 previous_find_text_ = find_text_;
1030
[email protected]6e95706d2009-04-23 22:55:011031 // This is a FindNext operation if we are searching for the same text again,
1032 // or if the passed in search text is empty (FindNext keyboard shortcut). The
1033 // exception to this is if the Find was aborted (then we don't want FindNext
1034 // because the highlighting has been cleared and we need it to reappear). We
1035 // therefore treat FindNext after an aborted Find operation as a full fledged
1036 // Find.
[email protected]c1dc9f132009-11-20 23:13:211037 bool find_next = (find_text_ == search_string || search_string.empty()) &&
[email protected]e491f1c2009-05-22 20:28:121038 (last_search_case_sensitive_ == case_sensitive) &&
[email protected]6e95706d2009-04-23 22:55:011039 !find_op_aborted_;
1040 if (!find_next)
1041 current_find_request_id_ = find_request_id_counter_++;
1042
[email protected]c1dc9f132009-11-20 23:13:211043 if (!search_string.empty())
1044 find_text_ = search_string;
[email protected]e491f1c2009-05-22 20:28:121045 last_search_case_sensitive_ = case_sensitive;
[email protected]6e95706d2009-04-23 22:55:011046
1047 find_op_aborted_ = false;
1048
1049 // Keep track of what the last search was across the tabs.
[email protected]c1dc9f132009-11-20 23:13:211050 *last_search_prepopulate_text_ = find_text_;
[email protected]6e95706d2009-04-23 22:55:011051 render_view_host()->StartFinding(current_find_request_id_,
1052 find_text_,
1053 forward_direction,
[email protected]e491f1c2009-05-22 20:28:121054 case_sensitive,
[email protected]6e95706d2009-04-23 22:55:011055 find_next);
1056}
1057
1058void TabContents::StopFinding(bool clear_selection) {
[email protected]2d2dffc2009-06-09 21:41:001059 // When |clear_selection| is true, it means the find string has been cleared
1060 // by the user, but the UI has not been dismissed.
1061 if (!clear_selection)
1062 find_ui_active_ = false;
[email protected]101c90e2009-10-26 21:14:441063 find_text_.clear();
[email protected]6e95706d2009-04-23 22:55:011064 find_op_aborted_ = true;
[email protected]e491f1c2009-05-22 20:28:121065 last_search_result_ = FindNotificationDetails();
[email protected]6e95706d2009-04-23 22:55:011066 render_view_host()->StopFinding(clear_selection);
1067}
1068
[email protected]5c4266922009-07-10 16:41:271069void TabContents::GetPageLanguage() {
1070 render_view_host()->GetPageLanguage();
1071}
1072
[email protected]420ae012009-04-24 05:16:321073void TabContents::OnSavePage() {
1074 // If we can not save the page, try to download it.
1075 if (!SavePackage::IsSavableContents(contents_mime_type())) {
1076 DownloadManager* dlm = profile()->GetDownloadManager();
1077 const GURL& current_page_url = GetURL();
1078 if (dlm && current_page_url.is_valid())
[email protected]d82ed61e2009-06-16 02:46:221079 dlm->DownloadUrl(current_page_url, GURL(), "", this);
[email protected]420ae012009-04-24 05:16:321080 return;
1081 }
1082
1083 Stop();
1084
1085 // Create the save package and possibly prompt the user for the name to save
1086 // the page as. The user prompt is an asynchronous operation that runs on
1087 // another thread.
[email protected]d82ed61e2009-06-16 02:46:221088 save_package_ = new SavePackage(this);
[email protected]420ae012009-04-24 05:16:321089 save_package_->GetSaveInfo();
1090}
1091
1092// Used in automated testing to bypass prompting the user for file names.
1093// Instead, the names and paths are hard coded rather than running them through
1094// file name sanitation and extension / mime checking.
[email protected]828cabe2009-09-26 22:47:111095bool TabContents::SavePage(const FilePath& main_file, const FilePath& dir_path,
[email protected]420ae012009-04-24 05:16:321096 SavePackage::SavePackageType save_type) {
1097 // Stop the page from navigating.
1098 Stop();
1099
[email protected]828cabe2009-09-26 22:47:111100 save_package_ = new SavePackage(this, save_type, main_file, dir_path);
[email protected]cdf6cc82009-08-04 21:48:591101 return save_package_->Init();
[email protected]420ae012009-04-24 05:16:321102}
1103
[email protected]89d6e6e2009-12-03 23:06:511104void TabContents::EmailPageLocation() {
1105 std::string title = EscapeQueryParamValue(UTF16ToUTF8(GetTitle()), false);
1106 std::string page_url = EscapeQueryParamValue(GetURL().spec(), false);
1107 std::string mailto = std::string("mailto:?subject=Fwd:%20") +
1108 title + "&body=%0A%0A" + page_url;
1109 platform_util::OpenExternal(GURL(mailto));
1110}
1111
[email protected]420ae012009-04-24 05:16:321112void TabContents::PrintPreview() {
1113 // We don't show the print preview yet, only the print dialog.
1114 PrintNow();
1115}
1116
1117bool TabContents::PrintNow() {
1118 // We can't print interstitial page for now.
1119 if (showing_interstitial_page())
1120 return false;
1121
1122 return render_view_host()->PrintPages();
1123}
1124
[email protected]82270452009-06-19 15:58:011125void TabContents::PrintingDone(int document_cookie, bool success) {
1126 render_view_host()->PrintingDone(document_cookie, success);
1127}
1128
[email protected]420ae012009-04-24 05:16:321129bool TabContents::IsActiveEntry(int32 page_id) {
1130 NavigationEntry* active_entry = controller_.GetActiveEntry();
1131 return (active_entry != NULL &&
1132 active_entry->site_instance() == GetSiteInstance() &&
1133 active_entry->page_id() == page_id);
1134}
1135
[email protected]3f8eb7f2009-10-02 23:58:051136void TabContents::LogNewTabTime(const std::string& event_name) {
1137 // Not all new tab pages get timed. In those cases, we don't have a
1138 // new_tab_start_time_.
1139 if (new_tab_start_time_.is_null())
1140 return;
1141
1142 base::TimeDelta duration = base::TimeTicks::Now() - new_tab_start_time_;
[email protected]84abba62009-10-07 17:01:441143 MetricEventDurationDetails details(event_name,
1144 static_cast<int>(duration.InMilliseconds()));
1145
[email protected]7ab1e7d62009-10-14 23:32:011146 if (event_name == "Tab.NewTabScriptStart") {
1147 UMA_HISTOGRAM_TIMES("Tab.NewTabScriptStart", duration);
1148 } else if (event_name == "Tab.NewTabDOMContentLoaded") {
1149 UMA_HISTOGRAM_TIMES("Tab.NewTabDOMContentLoaded", duration);
1150 } else if (event_name == "Tab.NewTabOnload") {
1151 UMA_HISTOGRAM_TIMES("Tab.NewTabOnload", duration);
[email protected]3f8eb7f2009-10-02 23:58:051152 // The new tab page has finished loading; reset it.
1153 new_tab_start_time_ = base::TimeTicks();
[email protected]84abba62009-10-07 17:01:441154 } else {
1155 NOTREACHED();
[email protected]3f8eb7f2009-10-02 23:58:051156 }
[email protected]84abba62009-10-07 17:01:441157 NotificationService::current()->Notify(
1158 NotificationType::METRIC_EVENT_DURATION,
1159 Source<TabContents>(this),
1160 Details<MetricEventDurationDetails>(&details));
[email protected]3f8eb7f2009-10-02 23:58:051161}
1162
[email protected]7ab1e7d62009-10-14 23:32:011163void TabContents::OnCloseStarted() {
1164 if (tab_close_start_time_.is_null())
1165 tab_close_start_time_ = base::TimeTicks::Now();
1166}
1167
[email protected]96d185d2009-04-24 03:28:541168// Notifies the RenderWidgetHost instance about the fact that the page is
1169// loading, or done loading and calls the base implementation.
1170void TabContents::SetIsLoading(bool is_loading,
1171 LoadNotificationDetails* details) {
1172 if (is_loading == is_loading_)
1173 return;
1174
1175 if (!is_loading) {
1176 load_state_ = net::LOAD_STATE_IDLE;
1177 load_state_host_.clear();
[email protected]094e5b22009-09-25 04:23:561178 upload_size_ = 0;
1179 upload_position_ = 0;
[email protected]96d185d2009-04-24 03:28:541180 }
1181
1182 render_manager_.SetIsLoading(is_loading);
1183
1184 is_loading_ = is_loading;
1185 waiting_for_response_ = is_loading;
1186
1187 if (delegate_)
1188 delegate_->LoadingStateChanged(this);
[email protected]329581b2009-04-28 06:52:351189 NotifyNavigationStateChanged(INVALIDATE_LOAD);
[email protected]96d185d2009-04-24 03:28:541190
1191 NotificationType type = is_loading ? NotificationType::LOAD_START :
1192 NotificationType::LOAD_STOP;
[email protected]ce5c4502009-05-06 16:46:111193 NotificationDetails det = NotificationService::NoDetails();
[email protected]96d185d2009-04-24 03:28:541194 if (details)
1195 det = Details<LoadNotificationDetails>(details);
1196 NotificationService::current()->Notify(type,
1197 Source<NavigationController>(&controller_),
1198 det);
1199}
1200
[email protected]2c4410d2009-05-06 23:46:221201void TabContents::CreateBlockedPopupContainerIfNecessary() {
1202 if (blocked_popups_)
1203 return;
1204
[email protected]4d4c0b92009-06-08 23:13:011205 blocked_popups_ = BlockedPopupContainer::Create(this, profile());
[email protected]2c4410d2009-05-06 23:46:221206}
1207
[email protected]b6c874582009-05-08 19:38:311208void TabContents::AddPopup(TabContents* new_contents,
1209 const gfx::Rect& initial_pos,
1210 const std::string& host) {
[email protected]2c4410d2009-05-06 23:46:221211 CreateBlockedPopupContainerIfNecessary();
1212 blocked_popups_->AddTabContents(new_contents, initial_pos, host);
[email protected]2c4410d2009-05-06 23:46:221213}
1214
[email protected]e69bce3dd2009-04-20 22:05:121215// TODO(brettw) This should be on the TabContentsView.
[email protected]66ba4932009-06-04 19:22:131216void TabContents::RepositionSupressedPopupsToFit() {
[email protected]d6598c052008-11-05 19:03:251217 if (blocked_popups_)
[email protected]4d4c0b92009-06-08 23:13:011218 blocked_popups_->RepositionBlockedPopupContainer();
[email protected]d5f942ba2008-09-26 19:30:341219}
1220
[email protected]b9681312008-11-07 00:08:261221bool TabContents::ShowingBlockedPopupNotification() const {
1222 return blocked_popups_ != NULL &&
[email protected]2c4410d2009-05-06 23:46:221223 blocked_popups_->GetBlockedPopupCount() != 0;
[email protected]b9681312008-11-07 00:08:261224}
[email protected]616ed5a2008-11-21 22:27:241225
1226namespace {
1227bool TransitionIsReload(PageTransition::Type transition) {
1228 return PageTransition::StripQualifier(transition) == PageTransition::RELOAD;
1229}
1230}
1231
1232void TabContents::ExpireInfoBars(
1233 const NavigationController::LoadCommittedDetails& details) {
1234 // Only hide InfoBars when the user has done something that makes the main
1235 // frame load. We don't want various automatic or subframe navigations making
1236 // it disappear.
1237 if (!details.is_user_initiated_main_frame_load())
1238 return;
1239
[email protected]f86a07022008-11-25 01:06:051240 for (int i = infobar_delegate_count() - 1; i >= 0; --i) {
[email protected]616ed5a2008-11-21 22:27:241241 InfoBarDelegate* delegate = GetInfoBarDelegateAt(i);
[email protected]f86a07022008-11-25 01:06:051242 if (delegate->ShouldExpire(details))
[email protected]616ed5a2008-11-21 22:27:241243 RemoveInfoBar(delegate);
[email protected]616ed5a2008-11-21 22:27:241244 }
1245}
[email protected]fdd61c62009-04-22 19:22:571246
[email protected]96d185d2009-04-24 03:28:541247DOMUI* TabContents::GetDOMUIForCurrentState() {
1248 // When there is a pending navigation entry, we want to use the pending DOMUI
1249 // that goes along with it to control the basic flags. For example, we want to
1250 // show the pending URL in the URL bar, so we want the display_url flag to
1251 // be from the pending entry.
1252 //
1253 // The confusion comes because there are multiple possibilities for the
1254 // initial load in a tab as a side effect of the way the RenderViewHostManager
1255 // works.
1256 //
1257 // - For the very first tab the load looks "normal". The new tab DOM UI is
1258 // the pending one, and we want it to apply here.
1259 //
1260 // - For subsequent new tabs, they'll get a new SiteInstance which will then
1261 // get switched to the one previously associated with the new tab pages.
1262 // This switching will cause the manager to commit the RVH/DOMUI. So we'll
1263 // have a committed DOM UI in this case.
1264 //
1265 // This condition handles all of these cases:
1266 //
1267 // - First load in first tab: no committed nav entry + pending nav entry +
1268 // pending dom ui:
1269 // -> Use pending DOM UI if any.
1270 //
1271 // - First load in second tab: no committed nav entry + pending nav entry +
1272 // no pending DOM UI:
1273 // -> Use the committed DOM UI if any.
1274 //
1275 // - Second navigation in any tab: committed nav entry + pending nav entry:
1276 // -> Use pending DOM UI if any.
1277 //
1278 // - Normal state with no load: committed nav entry + no pending nav entry:
1279 // -> Use committed DOM UI.
1280 if (controller_.pending_entry() &&
1281 (controller_.GetLastCommittedEntry() ||
1282 render_manager_.pending_dom_ui()))
1283 return render_manager_.pending_dom_ui();
1284 return render_manager_.dom_ui();
1285}
[email protected]420ae012009-04-24 05:16:321286
1287void TabContents::DidNavigateMainFramePostCommit(
1288 const NavigationController::LoadCommittedDetails& details,
1289 const ViewHostMsg_FrameNavigate_Params& params) {
[email protected]c2e74fe82009-09-03 17:57:441290 if (opener_dom_ui_type_ != DOMUIFactory::kNoDOMUI) {
1291 // If this is a window.open navigation, use the same DOMUI as the renderer
1292 // that opened the window, as long as both renderers have the same
1293 // privileges.
1294 if (opener_dom_ui_type_ == DOMUIFactory::GetDOMUIType(GetURL())) {
1295 DOMUI* dom_ui = DOMUIFactory::CreateDOMUIForURL(this, GetURL());
1296 // dom_ui might be NULL if the URL refers to a non-existent extension.
1297 if (dom_ui) {
1298 render_manager_.SetDOMUIPostCommit(dom_ui);
1299 dom_ui->RenderViewCreated(render_view_host());
1300 }
1301 }
1302 opener_dom_ui_type_ = DOMUIFactory::kNoDOMUI;
1303 }
1304
[email protected]420ae012009-04-24 05:16:321305 if (details.is_user_initiated_main_frame_load()) {
1306 // Clear the status bubble. This is a workaround for a bug where WebKit
1307 // doesn't let us know that the cursor left an element during a
1308 // transition (this is also why the mouse cursor remains as a hand after
1309 // clicking on a link); see bugs 1184641 and 980803. We don't want to
1310 // clear the bubble when a user navigates to a named anchor in the same
1311 // page.
1312 UpdateTargetURL(details.entry->page_id(), GURL());
1313
1314 // UpdateHelpersForDidNavigate will handle the case where the password_form
1315 // origin is valid.
1316 // TODO(brettw) bug 1343111: Password manager stuff in here needs to be
1317 // cleaned up and covered by tests.
1318 if (!params.password_form.origin.is_valid())
1319 GetPasswordManager()->DidNavigate();
1320 }
1321
1322 // The keyword generator uses the navigation entries, so must be called after
1323 // the commit.
1324 GenerateKeywordIfNecessary(params);
1325
1326 // Allow the new page to set the title again.
1327 received_page_title_ = false;
1328
1329 // Get the favicon, either from history or request it from the net.
1330 fav_icon_helper_.FetchFavIcon(details.entry->url());
1331
[email protected]56ce6e52009-10-27 00:10:521332 // Clear all page and browser action state for this tab, unless this is an
1333 // in-page navigation.
[email protected]92c6f9b92009-10-24 04:35:081334 url_canon::Replacements<char> replacements;
1335 replacements.ClearRef();
1336 if (params.url.ReplaceComponents(replacements) !=
1337 params.referrer.ReplaceComponents(replacements)) {
[email protected]92c6f9b92009-10-24 04:35:081338 ExtensionsService* service = profile()->GetExtensionsService();
1339 if (service) {
1340 for (size_t i = 0; i < service->extensions()->size(); ++i) {
[email protected]5d246db22009-10-27 06:17:571341 ExtensionAction* browser_action =
[email protected]92c6f9b92009-10-24 04:35:081342 service->extensions()->at(i)->browser_action();
[email protected]56ce6e52009-10-27 00:10:521343 if (browser_action) {
1344 browser_action->ClearAllValuesForTab(controller().session_id().id());
1345 NotificationService::current()->Notify(
1346 NotificationType::EXTENSION_BROWSER_ACTION_UPDATED,
[email protected]5d246db22009-10-27 06:17:571347 Source<ExtensionAction>(browser_action),
[email protected]56ce6e52009-10-27 00:10:521348 NotificationService::NoDetails());
1349 }
[email protected]92c6f9b92009-10-24 04:35:081350
[email protected]5d246db22009-10-27 06:17:571351 ExtensionAction* page_action =
[email protected]56ce6e52009-10-27 00:10:521352 service->extensions()->at(i)->page_action();
1353 if (page_action) {
1354 page_action->ClearAllValuesForTab(controller().session_id().id());
1355 PageActionStateChanged();
1356 }
[email protected]92c6f9b92009-10-24 04:35:081357 }
[email protected]aece2c7f2009-08-27 20:43:171358 }
1359 }
[email protected]ce5c4502009-05-06 16:46:111360
[email protected]420ae012009-04-24 05:16:321361 // Close constrained popups if necessary.
1362 MaybeCloseChildWindows(details.previous_url, details.entry->url());
1363
1364 // Update the starred state.
1365 UpdateStarredStateForCurrentURL();
1366}
1367
1368void TabContents::DidNavigateAnyFramePostCommit(
1369 RenderViewHost* render_view_host,
1370 const NavigationController::LoadCommittedDetails& details,
1371 const ViewHostMsg_FrameNavigate_Params& params) {
1372 // If we navigate, start showing messages again. This does nothing to prevent
1373 // a malicious script from spamming messages, since the script could just
1374 // reload the page to stop blocking.
1375 suppress_javascript_messages_ = false;
1376
[email protected]420ae012009-04-24 05:16:321377 // Notify the password manager of the navigation or form submit.
1378 // TODO(brettw) bug 1343111: Password manager stuff in here needs to be
1379 // cleaned up and covered by tests.
1380 if (params.password_form.origin.is_valid())
1381 GetPasswordManager()->ProvisionallySavePassword(params.password_form);
1382}
1383
1384void TabContents::MaybeCloseChildWindows(const GURL& previous_url,
1385 const GURL& current_url) {
1386 if (net::RegistryControlledDomainService::SameDomainOrHost(
1387 previous_url, current_url))
1388 return;
1389
1390 // Clear out any child windows since we are leaving this page entirely.
1391 // We use indices instead of iterators in case CloseWindow does something
1392 // that may invalidate an iterator.
1393 int size = static_cast<int>(child_windows_.size());
1394 for (int i = size - 1; i >= 0; --i) {
1395 ConstrainedWindow* window = child_windows_[i];
1396 if (window)
1397 window->CloseConstrainedWindow();
1398 }
[email protected]e29300cd2009-07-23 21:14:591399
1400 // Close the popup container.
1401 if (blocked_popups_) {
1402 blocked_popups_->Destroy();
1403 blocked_popups_ = NULL;
1404 }
[email protected]420ae012009-04-24 05:16:321405}
1406
1407void TabContents::UpdateStarredStateForCurrentURL() {
1408 BookmarkModel* model = profile()->GetBookmarkModel();
1409 const bool old_state = is_starred_;
1410 is_starred_ = (model && model->IsBookmarked(GetURL()));
1411
1412 if (is_starred_ != old_state && delegate())
1413 delegate()->URLStarredChanged(this, is_starred_);
1414}
1415
1416void TabContents::UpdateAlternateErrorPageURL() {
1417 GURL url = GetAlternateErrorPageURL();
1418 render_view_host()->SetAlternateErrorPageURL(url);
1419}
1420
1421void TabContents::UpdateWebPreferences() {
1422 render_view_host()->UpdateWebPreferences(GetWebkitPrefs());
1423}
1424
1425void TabContents::UpdateMaxPageIDIfNecessary(SiteInstance* site_instance,
1426 RenderViewHost* rvh) {
1427 // If we are creating a RVH for a restored controller, then we might
1428 // have more page IDs than the SiteInstance's current max page ID. We must
1429 // make sure that the max page ID is larger than any restored page ID.
1430 // Note that it is ok for conflicting page IDs to exist in another tab
1431 // (i.e., NavigationController), but if any page ID is larger than the max,
1432 // the back/forward list will get confused.
1433 int max_restored_page_id = controller_.max_restored_page_id();
1434 if (max_restored_page_id > 0) {
1435 int curr_max_page_id = site_instance->max_page_id();
1436 if (max_restored_page_id > curr_max_page_id) {
1437 // Need to update the site instance immediately.
1438 site_instance->UpdateMaxPageID(max_restored_page_id);
1439
1440 // Also tell the renderer to update its internal representation. We
1441 // need to reserve enough IDs to make all restored page IDs less than
1442 // the max.
1443 if (curr_max_page_id < 0)
1444 curr_max_page_id = 0;
1445 rvh->ReservePageIDRange(max_restored_page_id - curr_max_page_id);
1446 }
1447 }
1448}
1449
1450void TabContents::UpdateHistoryForNavigation(
[email protected]ebe89e062009-08-13 23:16:541451 const GURL& virtual_url,
[email protected]befd8d822009-07-01 04:51:471452 const NavigationController::LoadCommittedDetails& details,
[email protected]420ae012009-04-24 05:16:321453 const ViewHostMsg_FrameNavigate_Params& params) {
1454 if (profile()->IsOffTheRecord())
1455 return;
1456
1457 // Add to history service.
1458 HistoryService* hs = profile()->GetHistoryService(Profile::IMPLICIT_ACCESS);
1459 if (hs) {
1460 if (PageTransition::IsMainFrame(params.transition) &&
[email protected]ebe89e062009-08-13 23:16:541461 virtual_url != params.url) {
1462 // Hack on the "virtual" URL so that it will appear in history. For some
[email protected]420ae012009-04-24 05:16:321463 // types of URLs, we will display a magic URL that is different from where
1464 // the page is actually navigated. We want the user to see in history
[email protected]ebe89e062009-08-13 23:16:541465 // what they saw in the URL bar, so we add the virtual URL as a redirect.
1466 // This only applies to the main frame, as the virtual URL doesn't apply
[email protected]420ae012009-04-24 05:16:321467 // to sub-frames.
1468 std::vector<GURL> redirects = params.redirects;
1469 if (!redirects.empty())
[email protected]ebe89e062009-08-13 23:16:541470 redirects.back() = virtual_url;
1471 hs->AddPage(virtual_url, this, params.page_id, params.referrer,
[email protected]befd8d822009-07-01 04:51:471472 params.transition, redirects, details.did_replace_entry);
[email protected]420ae012009-04-24 05:16:321473 } else {
1474 hs->AddPage(params.url, this, params.page_id, params.referrer,
[email protected]befd8d822009-07-01 04:51:471475 params.transition, params.redirects,
1476 details.did_replace_entry);
[email protected]420ae012009-04-24 05:16:321477 }
1478 }
1479}
1480
1481bool TabContents::UpdateTitleForEntry(NavigationEntry* entry,
1482 const std::wstring& title) {
1483 // For file URLs without a title, use the pathname instead. In the case of a
1484 // synthesized title, we don't want the update to count toward the "one set
1485 // per page of the title to history."
1486 std::wstring final_title;
1487 bool explicit_set;
1488 if (entry->url().SchemeIsFile() && title.empty()) {
1489 final_title = UTF8ToWide(entry->url().ExtractFileName());
1490 explicit_set = false; // Don't count synthetic titles toward the set limit.
1491 } else {
1492 TrimWhitespace(title, TRIM_ALL, &final_title);
1493 explicit_set = true;
1494 }
1495
1496 if (final_title == UTF16ToWideHack(entry->title()))
1497 return false; // Nothing changed, don't bother.
1498
1499 entry->set_title(WideToUTF16Hack(final_title));
1500
1501 // Update the history system for this page.
1502 if (!profile()->IsOffTheRecord() && !received_page_title_) {
1503 HistoryService* hs =
1504 profile()->GetHistoryService(Profile::IMPLICIT_ACCESS);
1505 if (hs)
[email protected]ebe89e062009-08-13 23:16:541506 hs->SetPageTitle(entry->virtual_url(), final_title);
[email protected]420ae012009-04-24 05:16:321507
1508 // Don't allow the title to be saved again for explicitly set ones.
1509 received_page_title_ = explicit_set;
1510 }
1511
1512 // Lastly, set the title for the view.
1513 view_->SetPageTitle(final_title);
1514
1515 return true;
1516}
1517
1518void TabContents::NotifySwapped() {
1519 // After sending out a swap notification, we need to send a disconnect
1520 // notification so that clients that pick up a pointer to |this| can NULL the
1521 // pointer. See Bug 1230284.
1522 notify_disconnection_ = true;
1523 NotificationService::current()->Notify(
[email protected]57c6a652009-05-04 07:58:341524 NotificationType::TAB_CONTENTS_SWAPPED,
[email protected]d82ed61e2009-06-16 02:46:221525 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:321526 NotificationService::NoDetails());
1527}
1528
1529void TabContents::NotifyConnected() {
1530 notify_disconnection_ = true;
1531 NotificationService::current()->Notify(
[email protected]57c6a652009-05-04 07:58:341532 NotificationType::TAB_CONTENTS_CONNECTED,
[email protected]d82ed61e2009-06-16 02:46:221533 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:321534 NotificationService::NoDetails());
1535}
1536
1537void TabContents::NotifyDisconnected() {
1538 if (!notify_disconnection_)
1539 return;
1540
1541 notify_disconnection_ = false;
1542 NotificationService::current()->Notify(
[email protected]57c6a652009-05-04 07:58:341543 NotificationType::TAB_CONTENTS_DISCONNECTED,
[email protected]d82ed61e2009-06-16 02:46:221544 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:321545 NotificationService::NoDetails());
1546}
1547
1548void TabContents::GenerateKeywordIfNecessary(
1549 const ViewHostMsg_FrameNavigate_Params& params) {
1550 if (!params.searchable_form_url.is_valid())
1551 return;
1552
1553 if (profile()->IsOffTheRecord())
1554 return;
1555
1556 int last_index = controller_.last_committed_entry_index();
1557 // When there was no previous page, the last index will be 0. This is
1558 // normally due to a form submit that opened in a new tab.
1559 // TODO(brettw) bug 916126: we should support keywords when form submits
1560 // happen in new tabs.
1561 if (last_index <= 0)
1562 return;
1563 const NavigationEntry* previous_entry =
1564 controller_.GetEntryAtIndex(last_index - 1);
1565 if (IsFormSubmit(previous_entry)) {
1566 // Only generate a keyword if the previous page wasn't itself a form
1567 // submit.
1568 return;
1569 }
1570
1571 GURL keyword_url = previous_entry->user_typed_url().is_valid() ?
1572 previous_entry->user_typed_url() : previous_entry->url();
1573 std::wstring keyword =
1574 TemplateURLModel::GenerateKeyword(keyword_url, true); // autodetected
1575 if (keyword.empty())
1576 return;
1577
1578 TemplateURLModel* url_model = profile()->GetTemplateURLModel();
1579 if (!url_model)
1580 return;
1581
1582 if (!url_model->loaded()) {
1583 url_model->Load();
1584 return;
1585 }
1586
1587 const TemplateURL* current_url;
1588 std::wstring url = UTF8ToWide(params.searchable_form_url.spec());
1589 if (!url_model->CanReplaceKeyword(keyword, url, &current_url))
1590 return;
1591
1592 if (current_url) {
1593 if (current_url->originating_url().is_valid()) {
1594 // The existing keyword was generated from an OpenSearch description
1595 // document, don't regenerate.
1596 return;
1597 }
1598 url_model->Remove(current_url);
1599 }
1600 TemplateURL* new_url = new TemplateURL();
1601 new_url->set_keyword(keyword);
1602 new_url->set_short_name(keyword);
1603 new_url->SetURL(url, 0, 0);
1604 new_url->add_input_encoding(params.searchable_form_encoding);
1605 DCHECK(controller_.GetLastCommittedEntry());
1606 const GURL& favicon_url =
1607 controller_.GetLastCommittedEntry()->favicon().url();
1608 if (favicon_url.is_valid()) {
1609 new_url->SetFavIconURL(favicon_url);
1610 } else {
1611 // The favicon url isn't valid. This means there really isn't a favicon,
1612 // or the favicon url wasn't obtained before the load started. This assumes
1613 // the later.
1614 // TODO(sky): Need a way to set the favicon that doesn't involve generating
1615 // its url.
1616 new_url->SetFavIconURL(TemplateURL::GenerateFaviconURL(params.referrer));
1617 }
1618 new_url->set_safe_for_autoreplace(true);
1619 url_model->Add(new_url);
1620}
1621
[email protected]bf5c2ff392009-07-08 16:24:331622void TabContents::OnUserGesture() {
1623 // See comment in RenderViewHostDelegate::OnUserGesture as to why we do this.
1624 DownloadRequestManager* drm = g_browser_process->download_request_manager();
1625 if (drm)
1626 drm->OnUserGesture(this);
[email protected]e7eaedde2009-09-25 20:09:491627 ExternalProtocolHandler::OnUserGesture();
[email protected]bf5c2ff392009-07-08 16:24:331628 controller_.OnUserGesture();
1629}
1630
1631void TabContents::OnFindReply(int request_id,
1632 int number_of_matches,
1633 const gfx::Rect& selection_rect,
1634 int active_match_ordinal,
1635 bool final_update) {
[email protected]3ce7ced2009-07-24 18:52:421636 // Ignore responses for requests that have been aborted.
1637 if (find_op_aborted_)
1638 return;
1639
[email protected]bf5c2ff392009-07-08 16:24:331640 // Ignore responses for requests other than the one we have most recently
1641 // issued. That way we won't act on stale results when the user has
1642 // already typed in another query.
1643 if (request_id != current_find_request_id_)
1644 return;
1645
1646 if (number_of_matches == -1)
1647 number_of_matches = last_search_result_.number_of_matches();
1648 if (active_match_ordinal == -1)
1649 active_match_ordinal = last_search_result_.active_match_ordinal();
1650
1651 gfx::Rect selection = selection_rect;
1652 if (selection.IsEmpty())
1653 selection = last_search_result_.selection_rect();
1654
1655 // Notify the UI, automation and any other observers that a find result was
1656 // found.
1657 last_search_result_ = FindNotificationDetails(request_id, number_of_matches,
1658 selection, active_match_ordinal,
1659 final_update);
1660 NotificationService::current()->Notify(
1661 NotificationType::FIND_RESULT_AVAILABLE,
1662 Source<TabContents>(this),
1663 Details<FindNotificationDetails>(&last_search_result_));
1664}
1665
1666void TabContents::GoToEntryAtOffset(int offset) {
[email protected]f9cc4c452009-10-13 14:56:381667 if (!delegate_ || delegate_->OnGoToEntryOffset(offset))
1668 controller_.GoToOffset(offset);
[email protected]bf5c2ff392009-07-08 16:24:331669}
1670
1671void TabContents::GetHistoryListCount(int* back_list_count,
1672 int* forward_list_count) {
1673 int current_index = controller_.last_committed_entry_index();
1674 *back_list_count = current_index;
1675 *forward_list_count = controller_.entry_count() - current_index - 1;
1676}
1677
1678void TabContents::OnMissingPluginStatus(int status) {
1679#if defined(OS_WIN)
1680// TODO(PORT): pull in when plug-ins work
1681 GetPluginInstaller()->OnMissingPluginStatus(status);
1682#endif
1683}
1684
1685void TabContents::OnCrashedPlugin(const FilePath& plugin_path) {
[email protected]bf5c2ff392009-07-08 16:24:331686 DCHECK(!plugin_path.value().empty());
1687
1688 std::wstring plugin_name = plugin_path.ToWStringHack();
[email protected]d32d46182009-07-23 00:09:351689#if defined(OS_WIN) || defined(OS_MACOSX)
[email protected]bf5c2ff392009-07-08 16:24:331690 scoped_ptr<FileVersionInfo> version_info(
1691 FileVersionInfo::CreateFileVersionInfo(plugin_path));
1692 if (version_info.get()) {
1693 const std::wstring& product_name = version_info->product_name();
[email protected]996fd702009-09-04 19:12:371694 if (!product_name.empty()) {
[email protected]bf5c2ff392009-07-08 16:24:331695 plugin_name = product_name;
[email protected]996fd702009-09-04 19:12:371696#if defined(OS_MACOSX)
1697 // Many plugins on the Mac have .plugin in the actual name, which looks
1698 // terrible, so look for that and strip it off if present.
1699 const std::wstring plugin_extension(L".plugin");
1700 if (EndsWith(plugin_name, plugin_extension, true))
1701 plugin_name.erase(plugin_name.length() - plugin_extension.length());
1702#endif // OS_MACOSX
1703 }
[email protected]bf5c2ff392009-07-08 16:24:331704 }
[email protected]4a560ed2009-07-13 23:35:261705#else
1706 NOTIMPLEMENTED() << " convert plugin path to plugin name";
1707#endif
[email protected]bf5c2ff392009-07-08 16:24:331708 AddInfoBar(new SimpleAlertInfoBarDelegate(
1709 this, l10n_util::GetStringF(IDS_PLUGIN_CRASHED_PROMPT, plugin_name),
1710 NULL));
[email protected]bf5c2ff392009-07-08 16:24:331711}
1712
1713void TabContents::OnCrashedWorker() {
1714 AddInfoBar(new SimpleAlertInfoBarDelegate(
1715 this, l10n_util::GetString(IDS_WEBWORKER_CRASHED_PROMPT),
1716 NULL));
1717}
1718
1719void TabContents::OnDidGetApplicationInfo(
1720 int32 page_id,
1721 const webkit_glue::WebApplicationInfo& info) {
[email protected]ed5431872009-11-17 08:39:511722 web_app_info_ = info;
[email protected]bf5c2ff392009-07-08 16:24:331723
[email protected]ed5431872009-11-17 08:39:511724 if (delegate())
1725 delegate()->OnDidGetApplicationInfo(this, page_id);
[email protected]bf5c2ff392009-07-08 16:24:331726}
1727
1728void TabContents::DidStartProvisionalLoadForFrame(
1729 RenderViewHost* render_view_host,
1730 bool is_main_frame,
1731 const GURL& url) {
1732 ProvisionalLoadDetails details(is_main_frame,
1733 controller_.IsURLInPageNavigation(url),
1734 url, std::string(), false);
1735 NotificationService::current()->Notify(
1736 NotificationType::FRAME_PROVISIONAL_LOAD_START,
1737 Source<NavigationController>(&controller_),
1738 Details<ProvisionalLoadDetails>(&details));
1739}
1740
1741void TabContents::DidStartReceivingResourceResponse(
1742 ResourceRequestDetails* details) {
1743 NotificationService::current()->Notify(
1744 NotificationType::RESOURCE_RESPONSE_STARTED,
1745 Source<NavigationController>(&controller()),
1746 Details<ResourceRequestDetails>(details));
1747}
1748
1749void TabContents::DidRedirectResource(ResourceRequestDetails* details) {
1750 NotificationService::current()->Notify(
1751 NotificationType::RESOURCE_RECEIVED_REDIRECT,
1752 Source<NavigationController>(&controller()),
1753 Details<ResourceRequestDetails>(details));
1754}
1755
1756void TabContents::DidLoadResourceFromMemoryCache(
1757 const GURL& url,
1758 const std::string& frame_origin,
1759 const std::string& main_frame_origin,
1760 const std::string& security_info) {
1761 // Send out a notification that we loaded a resource from our memory cache.
1762 int cert_id = 0, cert_status = 0, security_bits = 0;
1763 SSLManager::DeserializeSecurityInfo(security_info,
1764 &cert_id, &cert_status,
1765 &security_bits);
1766 LoadFromMemoryCacheDetails details(url, frame_origin, main_frame_origin,
[email protected]76543b92009-08-31 17:27:451767 process()->id(), cert_id, cert_status);
[email protected]bf5c2ff392009-07-08 16:24:331768
1769 NotificationService::current()->Notify(
1770 NotificationType::LOAD_FROM_MEMORY_CACHE,
1771 Source<NavigationController>(&controller_),
1772 Details<LoadFromMemoryCacheDetails>(&details));
1773}
1774
[email protected]e3d60e5d2009-09-25 21:08:291775void TabContents::DidDisplayInsecureContent() {
[email protected]09843b5f2009-09-26 08:30:591776 controller_.ssl_manager()->DidDisplayInsecureContent();
[email protected]e3d60e5d2009-09-25 21:08:291777}
1778
1779void TabContents::DidRunInsecureContent(const std::string& security_origin) {
[email protected]09843b5f2009-09-26 08:30:591780 controller_.ssl_manager()->DidRunInsecureContent(security_origin);
[email protected]e3d60e5d2009-09-25 21:08:291781}
1782
[email protected]bf5c2ff392009-07-08 16:24:331783void TabContents::DidFailProvisionalLoadWithError(
1784 RenderViewHost* render_view_host,
1785 bool is_main_frame,
1786 int error_code,
1787 const GURL& url,
1788 bool showing_repost_interstitial) {
1789 if (net::ERR_ABORTED == error_code) {
1790 // EVIL HACK ALERT! Ignore failed loads when we're showing interstitials.
1791 // This means that the interstitial won't be torn down properly, which is
1792 // bad. But if we have an interstitial, go back to another tab type, and
1793 // then load the same interstitial again, we could end up getting the first
1794 // interstitial's "failed" message (as a result of the cancel) when we're on
1795 // the second one.
1796 //
1797 // We can't tell this apart, so we think we're tearing down the current page
1798 // which will cause a crash later one. There is also some code in
1799 // RenderViewHostManager::RendererAbortedProvisionalLoad that is commented
1800 // out because of this problem.
1801 //
1802 // http://code.google.com/p/chromium/issues/detail?id=2855
1803 // Because this will not tear down the interstitial properly, if "back" is
1804 // back to another tab type, the interstitial will still be somewhat alive
1805 // in the previous tab type. If you navigate somewhere that activates the
1806 // tab with the interstitial again, you'll see a flash before the new load
1807 // commits of the interstitial page.
1808 if (showing_interstitial_page()) {
1809 LOG(WARNING) << "Discarding message during interstitial.";
1810 return;
1811 }
1812
1813 // This will discard our pending entry if we cancelled the load (e.g., if we
1814 // decided to download the file instead of load it). Only discard the
1815 // pending entry if the URLs match, otherwise the user initiated a navigate
1816 // before the page loaded so that the discard would discard the wrong entry.
1817 NavigationEntry* pending_entry = controller_.pending_entry();
1818 if (pending_entry && pending_entry->url() == url) {
1819 controller_.DiscardNonCommittedEntries();
1820 // Update the URL display.
1821 NotifyNavigationStateChanged(TabContents::INVALIDATE_URL);
1822 }
1823
1824 render_manager_.RendererAbortedProvisionalLoad(render_view_host);
1825 }
1826
1827 // Send out a notification that we failed a provisional load with an error.
1828 ProvisionalLoadDetails details(is_main_frame,
1829 controller_.IsURLInPageNavigation(url),
1830 url, std::string(), false);
1831 details.set_error_code(error_code);
1832
1833 NotificationService::current()->Notify(
1834 NotificationType::FAIL_PROVISIONAL_LOAD_WITH_ERROR,
1835 Source<NavigationController>(&controller_),
1836 Details<ProvisionalLoadDetails>(&details));
1837}
1838
1839void TabContents::DocumentLoadedInFrame() {
1840 controller_.DocumentLoadedInFrame();
1841}
1842
[email protected]8d3347f2009-07-09 22:00:211843RenderViewHostDelegate::View* TabContents::GetViewDelegate() {
[email protected]420ae012009-04-24 05:16:321844 return view_.get();
1845}
1846
[email protected]8d3347f2009-07-09 22:00:211847RenderViewHostDelegate::RendererManagement*
1848TabContents::GetRendererManagementDelegate() {
1849 return &render_manager_;
1850}
1851
[email protected]bf5c2ff392009-07-08 16:24:331852RenderViewHostDelegate::BrowserIntegration*
[email protected]8d3347f2009-07-09 22:00:211853 TabContents::GetBrowserIntegrationDelegate() {
1854 return this;
[email protected]bf5c2ff392009-07-08 16:24:331855}
1856
[email protected]8d3347f2009-07-09 22:00:211857RenderViewHostDelegate::Resource* TabContents::GetResourceDelegate() {
1858 return this;
[email protected]bf5c2ff392009-07-08 16:24:331859}
1860
[email protected]8d3347f2009-07-09 22:00:211861RenderViewHostDelegate::Save* TabContents::GetSaveDelegate() {
[email protected]420ae012009-04-24 05:16:321862 return save_package_.get(); // May be NULL, but we can return NULL.
1863}
1864
[email protected]8d3347f2009-07-09 22:00:211865RenderViewHostDelegate::Printing* TabContents::GetPrintingDelegate() {
1866 return &printing_;
1867}
1868
1869RenderViewHostDelegate::FavIcon* TabContents::GetFavIconDelegate() {
1870 return &fav_icon_helper_;
1871}
1872
[email protected]c54d269e2009-10-21 18:07:381873RenderViewHostDelegate::FormFieldHistory*
1874TabContents::GetFormFieldHistoryDelegate() {
[email protected]19289d02009-10-21 21:47:001875 if (form_field_history_manager_.get() == NULL)
1876 form_field_history_manager_.reset(new FormFieldHistoryManager(this));
1877 return form_field_history_manager_.get();
[email protected]bf5c2ff392009-07-08 16:24:331878}
1879
[email protected]21674df2009-10-28 19:51:211880RenderViewHostDelegate::AutoFill* TabContents::GetAutoFillDelegate() {
1881 if (autofill_manager_.get() == NULL)
1882 autofill_manager_.reset(new AutoFillManager(this));
1883 return autofill_manager_.get();
1884}
1885
[email protected]80d96fa2009-06-10 22:34:511886RendererPreferences TabContents::GetRendererPrefs() const {
[email protected]840b1512009-07-21 16:53:461887 return renderer_preferences_;
[email protected]80d96fa2009-06-10 22:34:511888}
1889
[email protected]57c6a652009-05-04 07:58:341890TabContents* TabContents::GetAsTabContents() {
1891 return this;
1892}
1893
[email protected]3e7cf1362009-08-21 17:47:081894void TabContents::AddBlockedNotice(const GURL& url, const string16& reason) {
1895 CreateBlockedPopupContainerIfNecessary();
1896 blocked_popups_->AddBlockedNotice(url, reason);
1897}
1898
[email protected]7b291f92009-08-14 05:43:531899ViewType::Type TabContents::GetRenderViewType() const {
1900 return ViewType::TAB_CONTENTS;
1901}
1902
1903int TabContents::GetBrowserWindowID() const {
1904 return controller().window_id().id();
1905}
1906
[email protected]420ae012009-04-24 05:16:321907void TabContents::RenderViewCreated(RenderViewHost* render_view_host) {
[email protected]2e4633c2009-07-09 16:58:061908 NotificationService::current()->Notify(
1909 NotificationType::RENDER_VIEW_HOST_CREATED_FOR_TAB,
1910 Source<TabContents>(this),
1911 Details<RenderViewHost>(render_view_host));
[email protected]420ae012009-04-24 05:16:321912 NavigationEntry* entry = controller_.GetActiveEntry();
1913 if (!entry)
1914 return;
1915
1916 // When we're creating views, we're still doing initial setup, so we always
1917 // use the pending DOM UI rather than any possibly existing committed one.
1918 if (render_manager_.pending_dom_ui()) {
1919 render_manager_.pending_dom_ui()->RenderViewCreated(render_view_host);
1920 }
1921
1922 if (entry->IsViewSourceMode()) {
1923 // Put the renderer in view source mode.
1924 render_view_host->Send(
1925 new ViewMsg_EnableViewSourceMode(render_view_host->routing_id()));
1926 }
[email protected]0666aef2009-05-13 19:48:081927
1928 view()->RenderViewCreated(render_view_host);
[email protected]420ae012009-04-24 05:16:321929}
1930
1931void TabContents::RenderViewReady(RenderViewHost* rvh) {
1932 if (rvh != render_view_host()) {
1933 // Don't notify the world, since this came from a renderer in the
1934 // background.
1935 return;
1936 }
1937
1938 NotifyConnected();
[email protected]9d8a4642009-07-29 17:25:301939 bool was_crashed = is_crashed();
[email protected]420ae012009-04-24 05:16:321940 SetIsCrashed(false);
[email protected]9d8a4642009-07-29 17:25:301941
1942 // Restore the focus to the tab (otherwise the focus will be on the top
1943 // window).
1944 if (was_crashed && !FocusLocationBarByDefault())
1945 Focus();
[email protected]420ae012009-04-24 05:16:321946}
1947
1948void TabContents::RenderViewGone(RenderViewHost* rvh) {
1949 // Ask the print preview if this renderer was valuable.
1950 if (!printing_.OnRenderViewGone(rvh))
1951 return;
1952 if (rvh != render_view_host()) {
1953 // The pending page's RenderViewHost is gone.
1954 return;
1955 }
1956
1957 SetIsLoading(false, NULL);
1958 NotifyDisconnected();
1959 SetIsCrashed(true);
1960
[email protected]8e537302009-06-10 21:19:581961 // Tell the view that we've crashed so it can prepare the sad tab page.
1962 view_->OnTabCrashed();
[email protected]420ae012009-04-24 05:16:321963
1964 // Hide any visible hung renderer warning for this web contents' process.
[email protected]e7b8d832009-11-20 22:41:001965 hung_renderer_dialog::HideForTabContents(this);
[email protected]420ae012009-04-24 05:16:321966}
1967
[email protected]2e4633c2009-07-09 16:58:061968void TabContents::RenderViewDeleted(RenderViewHost* rvh) {
1969 NotificationService::current()->Notify(
1970 NotificationType::RENDER_VIEW_HOST_DELETED,
1971 Source<TabContents>(this),
1972 Details<RenderViewHost>(rvh));
[email protected]fb7b79f72009-11-06 18:00:481973 render_manager_.RenderViewDeleted(rvh);
[email protected]2e4633c2009-07-09 16:58:061974}
1975
[email protected]420ae012009-04-24 05:16:321976void TabContents::DidNavigate(RenderViewHost* rvh,
1977 const ViewHostMsg_FrameNavigate_Params& params) {
[email protected]8030f012009-09-25 18:09:371978 int extra_invalidate_flags = 0;
1979
1980 if (PageTransition::IsMainFrame(params.transition)) {
[email protected]20ede072009-10-06 00:28:351981 bool was_bookmark_bar_visible = ShouldShowBookmarkBar();
[email protected]8030f012009-09-25 18:09:371982 bool was_extension_shelf_visible = IsExtensionShelfAlwaysVisible();
1983
[email protected]420ae012009-04-24 05:16:321984 render_manager_.DidNavigateMainFrame(rvh);
1985
[email protected]20ede072009-10-06 00:28:351986 if (was_bookmark_bar_visible != ShouldShowBookmarkBar())
[email protected]8030f012009-09-25 18:09:371987 extra_invalidate_flags |= INVALIDATE_BOOKMARK_BAR;
1988 if (was_extension_shelf_visible != IsExtensionShelfAlwaysVisible())
1989 extra_invalidate_flags |= INVALIDATE_EXTENSION_SHELF;
1990 }
1991
[email protected]420ae012009-04-24 05:16:321992 // Update the site of the SiteInstance if it doesn't have one yet.
1993 if (!GetSiteInstance()->has_site())
1994 GetSiteInstance()->SetSite(params.url);
1995
1996 // Need to update MIME type here because it's referred to in
1997 // UpdateNavigationCommands() called by RendererDidNavigate() to
1998 // determine whether or not to enable the encoding menu.
1999 // It's updated only for the main frame. For a subframe,
2000 // RenderView::UpdateURL does not set params.contents_mime_type.
2001 // (see http://code.google.com/p/chromium/issues/detail?id=2929 )
2002 // TODO(jungshik): Add a test for the encoding menu to avoid
2003 // regressing it again.
2004 if (PageTransition::IsMainFrame(params.transition))
2005 contents_mime_type_ = params.contents_mime_type;
2006
2007 NavigationController::LoadCommittedDetails details;
[email protected]8030f012009-09-25 18:09:372008 bool did_navigate = controller_.RendererDidNavigate(
2009 params, extra_invalidate_flags, &details);
[email protected]3e90d4a2009-07-03 17:38:392010
2011 // Update history. Note that this needs to happen after the entry is complete,
2012 // which WillNavigate[Main,Sub]Frame will do before this function is called.
[email protected]2e29d922009-10-22 22:13:352013 if (params.should_update_history &&
2014 (!delegate() || delegate()->ShouldAddNavigationsToHistory())) {
[email protected]3e90d4a2009-07-03 17:38:392015 // Most of the time, the displayURL matches the loaded URL, but for about:
2016 // URLs, we use a data: URL as the real value. We actually want to save
2017 // the about: URL to the history db and keep the data: URL hidden. This is
2018 // what the TabContents' URL getter does.
2019 UpdateHistoryForNavigation(GetURL(), details, params);
2020 }
2021
[email protected]76543b92009-08-31 17:27:452022 if (!did_navigate)
2023 return; // No navigation happened.
[email protected]420ae012009-04-24 05:16:322024
2025 // DO NOT ADD MORE STUFF TO THIS FUNCTION! Your component should either listen
2026 // for the appropriate notification (best) or you can add it to
2027 // DidNavigateMainFramePostCommit / DidNavigateAnyFramePostCommit (only if
2028 // necessary, please).
2029
2030 // Run post-commit tasks.
2031 if (details.is_main_frame)
2032 DidNavigateMainFramePostCommit(details, params);
2033 DidNavigateAnyFramePostCommit(rvh, details, params);
2034}
2035
2036void TabContents::UpdateState(RenderViewHost* rvh,
2037 int32 page_id,
2038 const std::string& state) {
2039 DCHECK(rvh == render_view_host());
2040
2041 // We must be prepared to handle state updates for any page, these occur
2042 // when the user is scrolling and entering form data, as well as when we're
2043 // leaving a page, in which case our state may have already been moved to
2044 // the next page. The navigation controller will look up the appropriate
2045 // NavigationEntry and update it when it is notified via the delegate.
2046
2047 int entry_index = controller_.GetEntryIndexWithPageID(
2048 GetSiteInstance(), page_id);
2049 if (entry_index < 0)
2050 return;
2051 NavigationEntry* entry = controller_.GetEntryAtIndex(entry_index);
2052
2053 if (state == entry->content_state())
2054 return; // Nothing to update.
2055 entry->set_content_state(state);
2056 controller_.NotifyEntryChanged(entry, entry_index);
2057}
2058
2059void TabContents::UpdateTitle(RenderViewHost* rvh,
2060 int32 page_id, const std::wstring& title) {
2061 // If we have a title, that's a pretty good indication that we've started
2062 // getting useful data.
2063 SetNotWaitingForResponse();
2064
2065 DCHECK(rvh == render_view_host());
2066 NavigationEntry* entry = controller_.GetEntryWithPageID(GetSiteInstance(),
2067 page_id);
2068 if (!entry || !UpdateTitleForEntry(entry, title))
2069 return;
2070
2071 // Broadcast notifications when the UI should be updated.
2072 if (entry == controller_.GetEntryAtOffset(0))
[email protected]f1cd5e82009-10-23 17:19:032073 NotifyNavigationStateChanged(INVALIDATE_TITLE);
[email protected]420ae012009-04-24 05:16:322074}
2075
[email protected]420ae012009-04-24 05:16:322076void TabContents::UpdateEncoding(RenderViewHost* render_view_host,
[email protected]41fc0322009-09-04 22:23:402077 const std::string& encoding) {
[email protected]420ae012009-04-24 05:16:322078 set_encoding(encoding);
2079}
2080
2081void TabContents::UpdateTargetURL(int32 page_id, const GURL& url) {
2082 if (delegate())
2083 delegate()->UpdateTargetURL(this, url);
2084}
2085
2086void TabContents::UpdateThumbnail(const GURL& url,
2087 const SkBitmap& bitmap,
2088 const ThumbnailScore& score) {
2089 // Tell History about this thumbnail
[email protected]449478302009-06-09 20:04:282090 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kThumbnailStore)) {
[email protected]72d0fc72009-07-09 17:30:492091 if (!profile()->IsOffTheRecord())
[email protected]42339402009-07-17 17:27:192092 profile()->GetThumbnailStore()->SetPageThumbnail(url, bitmap,
2093 score, true);
[email protected]449478302009-06-09 20:04:282094 } else {
2095 HistoryService* hs;
2096 if (!profile()->IsOffTheRecord() &&
2097 (hs = profile()->GetHistoryService(Profile::IMPLICIT_ACCESS))) {
2098 hs->SetPageThumbnail(url, bitmap, score);
2099 }
[email protected]420ae012009-04-24 05:16:322100 }
2101}
2102
[email protected]8922e1f2009-10-03 05:01:262103void TabContents::UpdateInspectorSettings(const std::string& raw_settings) {
[email protected]0df30122009-06-03 12:13:082104 profile()->GetPrefs()->SetString(prefs::kWebKitInspectorSettings,
[email protected]8922e1f2009-10-03 05:01:262105 UTF8ToWide(raw_settings));
[email protected]0df30122009-06-03 12:13:082106}
2107
[email protected]420ae012009-04-24 05:16:322108void TabContents::Close(RenderViewHost* rvh) {
[email protected]07707302009-11-06 00:50:292109 // The UI may be in an event-tracking loop, such as between the
2110 // mouse-down and mouse-up in text selection or a button click.
2111 // Defer the close until after tracking is complete, so that we
2112 // don't free objects out from under the UI.
2113 // TODO(shess): This could probably be integrated with the
2114 // IsDoingDrag() test below. Punting for now because I need more
2115 // research to understand how this impacts platforms other than Mac.
2116 // TODO(shess): This could get more fine-grained. For instance,
2117 // closing a tab in another window while selecting text in the
2118 // current window's Omnibox should be just fine.
2119 if (view()->IsEventTracking()) {
2120 view()->CloseTabAfterEventTracking();
2121 return;
2122 }
2123
[email protected]24a4d1062009-07-10 23:10:422124 // If we close the tab while we're in the middle of a drag, we'll crash.
2125 // Instead, cancel the drag and close it as soon as the drag ends.
2126 if (view()->IsDoingDrag()) {
2127 view()->CancelDragAndCloseTab();
2128 return;
2129 }
2130
[email protected]420ae012009-04-24 05:16:322131 // Ignore this if it comes from a RenderViewHost that we aren't showing.
2132 if (delegate() && rvh == render_view_host())
2133 delegate()->CloseContents(this);
2134}
2135
2136void TabContents::RequestMove(const gfx::Rect& new_bounds) {
2137 if (delegate() && delegate()->IsPopup(this))
2138 delegate()->MoveContents(this, new_bounds);
2139}
2140
[email protected]7ab1e7d62009-10-14 23:32:012141void TabContents::DidStartLoading() {
[email protected]420ae012009-04-24 05:16:322142 SetIsLoading(true, NULL);
2143}
2144
[email protected]7ab1e7d62009-10-14 23:32:012145void TabContents::DidStopLoading() {
[email protected]420ae012009-04-24 05:16:322146 scoped_ptr<LoadNotificationDetails> details;
2147
2148 NavigationEntry* entry = controller_.GetActiveEntry();
2149 // An entry may not exist for a stop when loading an initial blank page or
2150 // if an iframe injected by script into a blank page finishes loading.
2151 if (entry) {
[email protected]420ae012009-04-24 05:16:322152 base::TimeDelta elapsed = base::TimeTicks::Now() - current_load_start_;
2153
2154 details.reset(new LoadNotificationDetails(
[email protected]ebe89e062009-08-13 23:16:542155 entry->virtual_url(),
[email protected]420ae012009-04-24 05:16:322156 entry->transition_type(),
2157 elapsed,
2158 &controller_,
2159 controller_.GetCurrentEntryIndex()));
2160 }
2161
2162 // Tell PasswordManager we've finished a page load, which serves as a
2163 // green light to save pending passwords and reset itself.
2164 GetPasswordManager()->DidStopLoading();
2165
2166 SetIsLoading(false, details.get());
2167}
2168
[email protected]420ae012009-04-24 05:16:322169void TabContents::DidRedirectProvisionalLoad(int32 page_id,
2170 const GURL& source_url,
2171 const GURL& target_url) {
2172 NavigationEntry* entry;
2173 if (page_id == -1)
2174 entry = controller_.pending_entry();
2175 else
2176 entry = controller_.GetEntryWithPageID(GetSiteInstance(), page_id);
2177 if (!entry || entry->url() != source_url)
2178 return;
2179 entry->set_url(target_url);
2180}
2181
[email protected]420ae012009-04-24 05:16:322182void TabContents::RequestOpenURL(const GURL& url, const GURL& referrer,
2183 WindowOpenDisposition disposition) {
2184 if (render_manager_.dom_ui()) {
2185 // When we're a DOM UI, it will provide a page transition type for us (this
2186 // is so the new tab page can specify AUTO_BOOKMARK for automatically
2187 // generated suggestions).
2188 //
2189 // Note also that we hide the referrer for DOM UI pages. We don't really
[email protected]60e448982009-05-06 04:21:162190 // want web sites to see a referrer of "chrome://blah" (and some
2191 // chrome: URLs might have search terms or other stuff we don't want to
[email protected]420ae012009-04-24 05:16:322192 // send to the site), so we send no referrer.
2193 OpenURL(url, GURL(), disposition,
2194 render_manager_.dom_ui()->link_transition_type());
2195 } else {
2196 OpenURL(url, referrer, disposition, PageTransition::LINK);
2197 }
2198}
2199
[email protected]1ee614862009-07-06 20:29:182200void TabContents::DomOperationResponse(const std::string& json_string,
2201 int automation_id) {
2202}
2203
[email protected]420ae012009-04-24 05:16:322204void TabContents::ProcessDOMUIMessage(const std::string& message,
[email protected]e4dad9fb2009-10-06 18:15:582205 const Value* content,
[email protected]811bfe32009-07-01 08:46:252206 int request_id,
2207 bool has_callback) {
[email protected]420ae012009-04-24 05:16:322208 if (!render_manager_.dom_ui()) {
[email protected]c2e74fe82009-09-03 17:57:442209 // This can happen if someone uses window.open() to open an extension URL
2210 // from a non-extension context.
2211 render_view_host()->BlockExtensionRequest(request_id);
[email protected]420ae012009-04-24 05:16:322212 return;
2213 }
[email protected]811bfe32009-07-01 08:46:252214 render_manager_.dom_ui()->ProcessDOMUIMessage(message, content, request_id,
2215 has_callback);
[email protected]420ae012009-04-24 05:16:322216}
2217
2218void TabContents::ProcessExternalHostMessage(const std::string& message,
2219 const std::string& origin,
2220 const std::string& target) {
2221 if (delegate())
2222 delegate()->ForwardMessageToExternalHost(message, origin, target);
2223}
2224
[email protected]420ae012009-04-24 05:16:322225void TabContents::RunFileChooser(bool multiple_files,
2226 const string16& title,
2227 const FilePath& default_file) {
2228 if (!select_file_dialog_.get())
2229 select_file_dialog_ = SelectFileDialog::Create(this);
2230 SelectFileDialog::Type dialog_type =
2231 multiple_files ? SelectFileDialog::SELECT_OPEN_MULTI_FILE :
2232 SelectFileDialog::SELECT_OPEN_FILE;
2233 select_file_dialog_->SelectFile(dialog_type, title, default_file,
2234 NULL, 0, FILE_PATH_LITERAL(""),
2235 view_->GetTopLevelNativeWindow(), NULL);
2236}
2237
2238void TabContents::RunJavaScriptMessage(
2239 const std::wstring& message,
2240 const std::wstring& default_prompt,
2241 const GURL& frame_url,
2242 const int flags,
2243 IPC::Message* reply_msg,
2244 bool* did_suppress_message) {
2245 // Suppress javascript messages when requested and when inside a constrained
2246 // popup window (because that activates them and breaks them out of the
2247 // constrained window jail).
[email protected]91ed49a2009-07-07 21:29:552248 // Also suppress messages when showing an interstitial. The interstitial is
2249 // shown over the previous page, we don't want the hidden page dialogs to
2250 // interfere with the interstitial.
2251 bool suppress_this_message = suppress_javascript_messages_ ||
2252 showing_interstitial_page();
[email protected]420ae012009-04-24 05:16:322253 if (delegate())
2254 suppress_this_message |=
[email protected]b6c874582009-05-08 19:38:312255 (delegate()->GetConstrainingContents(this) != this);
[email protected]420ae012009-04-24 05:16:322256
2257 *did_suppress_message = suppress_this_message;
2258
2259 if (!suppress_this_message) {
2260 base::TimeDelta time_since_last_message(
2261 base::TimeTicks::Now() - last_javascript_message_dismissal_);
2262 bool show_suppress_checkbox = false;
2263 // Show a checkbox offering to suppress further messages if this message is
2264 // being displayed within kJavascriptMessageExpectedDelay of the last one.
2265 if (time_since_last_message <
2266 base::TimeDelta::FromMilliseconds(kJavascriptMessageExpectedDelay))
2267 show_suppress_checkbox = true;
2268
[email protected]d82ed61e2009-06-16 02:46:222269 RunJavascriptMessageBox(this, frame_url, flags, message, default_prompt,
[email protected]420ae012009-04-24 05:16:322270 show_suppress_checkbox, reply_msg);
2271 } else {
2272 // If we are suppressing messages, just reply as is if the user immediately
2273 // pressed "Cancel".
[email protected]beb440c2009-11-06 04:08:542274 OnMessageBoxClosed(reply_msg, false, std::wstring());
[email protected]420ae012009-04-24 05:16:322275 }
2276}
2277
2278void TabContents::RunBeforeUnloadConfirm(const std::wstring& message,
2279 IPC::Message* reply_msg) {
[email protected]7ab1e7d62009-10-14 23:32:012280 is_showing_before_unload_dialog_ = true;
[email protected]d82ed61e2009-06-16 02:46:222281 RunBeforeUnloadDialog(this, message, reply_msg);
[email protected]420ae012009-04-24 05:16:322282}
2283
2284void TabContents::ShowModalHTMLDialog(const GURL& url, int width, int height,
2285 const std::string& json_arguments,
2286 IPC::Message* reply_msg) {
2287 if (delegate()) {
2288 HtmlDialogUIDelegate* dialog_delegate =
2289 new ModalHtmlDialogDelegate(url, width, height, json_arguments,
[email protected]d82ed61e2009-06-16 02:46:222290 reply_msg, this);
[email protected]420ae012009-04-24 05:16:322291 delegate()->ShowHtmlDialog(dialog_delegate, NULL);
2292 }
2293}
2294
2295void TabContents::PasswordFormsSeen(
[email protected]ed3fb032009-06-16 19:50:562296 const std::vector<webkit_glue::PasswordForm>& forms) {
[email protected]420ae012009-04-24 05:16:322297 GetPasswordManager()->PasswordFormsSeen(forms);
2298}
2299
[email protected]420ae012009-04-24 05:16:322300// Checks to see if we should generate a keyword based on the OSDD, and if
2301// necessary uses TemplateURLFetcher to download the OSDD and create a keyword.
2302void TabContents::PageHasOSDD(RenderViewHost* render_view_host,
2303 int32 page_id, const GURL& url,
2304 bool autodetected) {
2305 // Make sure page_id is the current page, and the TemplateURLModel is loaded.
2306 DCHECK(url.is_valid());
2307 if (!IsActiveEntry(page_id))
2308 return;
2309 TemplateURLModel* url_model = profile()->GetTemplateURLModel();
2310 if (!url_model)
2311 return;
2312 if (!url_model->loaded()) {
2313 url_model->Load();
2314 return;
2315 }
2316 if (!profile()->GetTemplateURLFetcher())
2317 return;
2318
2319 if (profile()->IsOffTheRecord())
2320 return;
2321
2322 const NavigationEntry* entry = controller_.GetLastCommittedEntry();
2323 DCHECK(entry);
2324
2325 const NavigationEntry* base_entry = entry;
2326 if (IsFormSubmit(base_entry)) {
2327 // If the current page is a form submit, find the last page that was not
2328 // a form submit and use its url to generate the keyword from.
2329 int index = controller_.last_committed_entry_index() - 1;
2330 while (index >= 0 && IsFormSubmit(controller_.GetEntryAtIndex(index)))
2331 index--;
2332 if (index >= 0)
2333 base_entry = controller_.GetEntryAtIndex(index);
2334 else
2335 base_entry = NULL;
2336 }
2337
2338 // We want to use the user typed URL if available since that represents what
2339 // the user typed to get here, and fall back on the regular URL if not.
2340 if (!base_entry)
2341 return;
2342 GURL keyword_url = base_entry->user_typed_url().is_valid() ?
2343 base_entry->user_typed_url() : base_entry->url();
2344 if (!keyword_url.is_valid())
2345 return;
2346 std::wstring keyword = TemplateURLModel::GenerateKeyword(keyword_url,
2347 autodetected);
2348 if (keyword.empty())
2349 return;
2350 const TemplateURL* template_url =
2351 url_model->GetTemplateURLForKeyword(keyword);
2352 if (template_url && (!template_url->safe_for_autoreplace() ||
2353 template_url->originating_url() == url)) {
2354 // Either there is a user created TemplateURL for this keyword, or the
2355 // keyword has the same OSDD url and we've parsed it.
2356 return;
2357 }
2358
2359 // Download the OpenSearch description document. If this is successful a
2360 // new keyword will be created when done.
[email protected]420ae012009-04-24 05:16:322361 profile()->GetTemplateURLFetcher()->ScheduleDownload(
2362 keyword,
2363 url,
2364 base_entry->favicon().url(),
[email protected]d0cc9fb52009-06-23 02:26:162365 this,
[email protected]420ae012009-04-24 05:16:322366 autodetected);
2367}
2368
[email protected]420ae012009-04-24 05:16:322369GURL TabContents::GetAlternateErrorPageURL() const {
2370 GURL url;
2371 // Disable alternate error pages when in OffTheRecord/Incognito mode.
2372 if (profile()->IsOffTheRecord())
2373 return url;
2374
2375 PrefService* prefs = profile()->GetPrefs();
2376 DCHECK(prefs);
2377 if (prefs->GetBoolean(prefs::kAlternateErrorPagesEnabled)) {
[email protected]a33457b82009-09-21 23:07:152378 url = google_util::AppendGoogleLocaleParam(
2379 GURL(google_util::kLinkDoctorBaseURL));
[email protected]420ae012009-04-24 05:16:322380 url = google_util::AppendGoogleTLDParam(url);
2381 }
2382 return url;
2383}
2384
2385WebPreferences TabContents::GetWebkitPrefs() {
2386 PrefService* prefs = render_view_host()->process()->profile()->GetPrefs();
[email protected]7ccddb8c2009-08-04 17:36:552387 bool is_dom_ui = false;
2388 return RenderViewHostDelegateHelper::GetWebkitPrefs(prefs, is_dom_ui);
[email protected]420ae012009-04-24 05:16:322389}
2390
[email protected]420ae012009-04-24 05:16:322391void TabContents::OnJSOutOfMemory() {
2392 AddInfoBar(new SimpleAlertInfoBarDelegate(
2393 this, l10n_util::GetString(IDS_JS_OUT_OF_MEMORY_PROMPT), NULL));
2394}
2395
[email protected]420ae012009-04-24 05:16:322396void TabContents::OnCrossSiteResponse(int new_render_process_host_id,
2397 int new_request_id) {
[email protected]57c6a652009-05-04 07:58:342398 // Allows the TabContents to react when a cross-site response is ready to be
[email protected]420ae012009-04-24 05:16:322399 // delivered to a pending RenderViewHost. We must first run the onunload
2400 // handler of the old RenderViewHost before we can allow it to proceed.
2401 render_manager_.OnCrossSiteResponse(new_render_process_host_id,
2402 new_request_id);
2403}
2404
2405bool TabContents::CanBlur() const {
2406 return delegate() ? delegate()->CanBlur() : true;
2407}
2408
2409gfx::Rect TabContents::GetRootWindowResizerRect() const {
2410 if (delegate())
2411 return delegate()->GetRootWindowResizerRect();
2412 return gfx::Rect();
2413}
2414
2415void TabContents::RendererUnresponsive(RenderViewHost* rvh,
2416 bool is_during_unload) {
2417 if (is_during_unload) {
2418 // Hang occurred while firing the beforeunload/unload handler.
2419 // Pretend the handler fired so tab closing continues as if it had.
[email protected]829e7612009-04-25 01:15:112420 rvh->set_sudden_termination_allowed(true);
[email protected]420ae012009-04-24 05:16:322421
2422 if (!render_manager_.ShouldCloseTabOnUnresponsiveRenderer())
2423 return;
2424
2425 // If the tab hangs in the beforeunload/unload handler there's really
2426 // nothing we can do to recover. Pretend the unload listeners have
2427 // all fired and close the tab. If the hang is in the beforeunload handler
2428 // then the user will not have the option of cancelling the close.
2429 Close(rvh);
2430 return;
2431 }
2432
2433 if (render_view_host() && render_view_host()->IsRenderViewLive())
[email protected]e7b8d832009-11-20 22:41:002434 hung_renderer_dialog::ShowForTabContents(this);
[email protected]420ae012009-04-24 05:16:322435}
2436
2437void TabContents::RendererResponsive(RenderViewHost* render_view_host) {
[email protected]e7b8d832009-11-20 22:41:002438 hung_renderer_dialog::HideForTabContents(this);
[email protected]420ae012009-04-24 05:16:322439}
2440
2441void TabContents::LoadStateChanged(const GURL& url,
[email protected]094e5b22009-09-25 04:23:562442 net::LoadState load_state,
2443 uint64 upload_position,
2444 uint64 upload_size) {
[email protected]420ae012009-04-24 05:16:322445 load_state_ = load_state;
[email protected]094e5b22009-09-25 04:23:562446 upload_position_ = upload_position;
2447 upload_size_ = upload_size;
[email protected]d686e812009-06-03 19:10:292448 std::wstring languages =
2449 profile()->GetPrefs()->GetString(prefs::kAcceptLanguages);
[email protected]d686e812009-06-03 19:10:292450 std::string host = url.host();
[email protected]ce85f602009-11-07 01:34:532451 load_state_host_ =
2452 net::IDNToUnicode(host.c_str(), host.size(), languages, NULL);
[email protected]420ae012009-04-24 05:16:322453 if (load_state_ == net::LOAD_STATE_READING_RESPONSE)
2454 SetNotWaitingForResponse();
2455 if (is_loading())
[email protected]c9cd2222009-05-06 05:16:502456 NotifyNavigationStateChanged(INVALIDATE_LOAD | INVALIDATE_TAB);
[email protected]420ae012009-04-24 05:16:322457}
2458
[email protected]1ee614862009-07-06 20:29:182459bool TabContents::IsExternalTabContainer() const {
2460 if (!delegate())
2461 return false;
2462
2463 return delegate()->IsExternalTabContainer();
2464}
2465
[email protected]1ee614862009-07-06 20:29:182466void TabContents::DidInsertCSS() {
2467 // This RVHDelegate function is used for extensions and not us.
[email protected]420ae012009-04-24 05:16:322468}
2469
[email protected]130efb02009-09-18 18:54:352470void TabContents::FocusedNodeChanged() {
2471 NotificationService::current()->Notify(
2472 NotificationType::FOCUS_CHANGED_IN_PAGE,
2473 Source<RenderViewHost>(render_view_host()),
2474 NotificationService::NoDetails());
2475}
2476
[email protected]420ae012009-04-24 05:16:322477void TabContents::FileSelected(const FilePath& path,
2478 int index, void* params) {
2479 render_view_host()->FileSelected(path);
2480}
2481
2482void TabContents::MultiFilesSelected(const std::vector<FilePath>& files,
2483 void* params) {
2484 render_view_host()->MultiFilesSelected(files);
2485}
2486
2487void TabContents::FileSelectionCanceled(void* params) {
2488 // If the user cancels choosing a file to upload we pass back an
2489 // empty vector.
2490 render_view_host()->MultiFilesSelected(std::vector<FilePath>());
2491}
2492
2493void TabContents::BeforeUnloadFiredFromRenderManager(
2494 bool proceed,
2495 bool* proceed_to_fire_unload) {
2496 if (delegate())
2497 delegate()->BeforeUnloadFired(this, proceed, proceed_to_fire_unload);
2498}
2499
2500void TabContents::UpdateRenderViewSizeForRenderManager() {
2501 // TODO(brettw) this is a hack. See TabContentsView::SizeContents.
2502 view_->SizeContents(view_->GetContainerSize());
2503}
2504
2505DOMUI* TabContents::CreateDOMUIForRenderManager(const GURL& url) {
[email protected]d82ed61e2009-06-16 02:46:222506 return DOMUIFactory::CreateDOMUIForURL(this, url);
[email protected]420ae012009-04-24 05:16:322507}
2508
2509NavigationEntry*
2510TabContents::GetLastCommittedNavigationEntryForRenderManager() {
2511 return controller_.GetLastCommittedEntry();
2512}
2513
2514bool TabContents::CreateRenderViewForRenderManager(
2515 RenderViewHost* render_view_host) {
[email protected]811bfe32009-07-01 08:46:252516 // If the pending navigation is to a DOMUI, tell the RenderView about any
2517 // bindings it will need enabled.
[email protected]420ae012009-04-24 05:16:322518 if (render_manager_.pending_dom_ui())
[email protected]811bfe32009-07-01 08:46:252519 render_view_host->AllowBindings(
2520 render_manager_.pending_dom_ui()->bindings());
[email protected]1adff062009-06-02 18:39:552521
[email protected]420ae012009-04-24 05:16:322522 RenderWidgetHostView* rwh_view = view_->CreateViewForWidget(render_view_host);
[email protected]1a98a932009-11-17 00:12:522523
2524 scoped_refptr<URLRequestContextGetter> request_context = request_context_;
2525 if (!request_context.get())
2526 request_context = profile()->GetRequestContext();
2527
2528 if (!render_view_host->CreateRenderView(request_context))
[email protected]420ae012009-04-24 05:16:322529 return false;
2530
2531 // Now that the RenderView has been created, we need to tell it its size.
2532 rwh_view->SetSize(view_->GetContainerSize());
2533
2534 UpdateMaxPageIDIfNecessary(render_view_host->site_instance(),
2535 render_view_host);
2536 return true;
2537}
2538
2539void TabContents::Observe(NotificationType type,
2540 const NotificationSource& source,
2541 const NotificationDetails& details) {
2542 switch (type.value) {
2543 case NotificationType::BOOKMARK_MODEL_LOADED:
2544 // BookmarkModel finished loading, fall through to update starred state.
2545 case NotificationType::URLS_STARRED: {
2546 // Somewhere, a URL has been starred.
2547 // Ignore notifications for profiles other than our current one.
2548 Profile* source_profile = Source<Profile>(source).ptr();
2549 if (!source_profile || !source_profile->IsSameProfile(profile()))
2550 return;
2551
2552 UpdateStarredStateForCurrentURL();
2553 break;
2554 }
2555 case NotificationType::PREF_CHANGED: {
2556 std::wstring* pref_name_in = Details<std::wstring>(details).ptr();
2557 DCHECK(Source<PrefService>(source).ptr() == profile()->GetPrefs());
2558 if (*pref_name_in == prefs::kAlternateErrorPagesEnabled) {
2559 UpdateAlternateErrorPageURL();
2560 } else if (*pref_name_in == prefs::kDefaultCharset ||
2561 StartsWithASCII(WideToUTF8(*pref_name_in), "webkit.webprefs.", true)
2562 ) {
2563 UpdateWebPreferences();
2564 } else {
2565 NOTREACHED() << "unexpected pref change notification" << *pref_name_in;
2566 }
2567 break;
2568 }
2569 case NotificationType::RENDER_WIDGET_HOST_DESTROYED:
2570 view_->RenderWidgetHostDestroyed(Source<RenderWidgetHost>(source).ptr());
2571 break;
2572
2573 case NotificationType::NAV_ENTRY_COMMITTED: {
2574 DCHECK(&controller_ == Source<NavigationController>(source).ptr());
2575
2576 NavigationController::LoadCommittedDetails& committed_details =
2577 *(Details<NavigationController::LoadCommittedDetails>(details).ptr());
2578 ExpireInfoBars(committed_details);
2579 break;
2580 }
2581
[email protected]d3ba77272009-09-03 00:06:092582#if defined(OS_LINUX)
2583 case NotificationType::BROWSER_THEME_CHANGED: {
2584 render_view_host()->SyncRendererPrefs();
2585 break;
2586 }
2587#endif
2588
[email protected]420ae012009-04-24 05:16:322589 default:
2590 NOTREACHED();
2591 }
2592}
[email protected]aacd7cc2009-09-02 14:51:102593
[email protected]beb440c2009-11-06 04:08:542594std::wstring TabContents::GetMessageBoxTitle(const GURL& frame_url,
2595 bool is_alert) {
2596 if (!frame_url.has_host())
2597 return l10n_util::GetString(
2598 is_alert ? IDS_JAVASCRIPT_ALERT_DEFAULT_TITLE
2599 : IDS_JAVASCRIPT_MESSAGEBOX_DEFAULT_TITLE);
2600
2601 // We really only want the scheme, hostname, and port.
2602 GURL::Replacements replacements;
2603 replacements.ClearUsername();
2604 replacements.ClearPassword();
2605 replacements.ClearPath();
2606 replacements.ClearQuery();
2607 replacements.ClearRef();
2608 GURL clean_url = frame_url.ReplaceComponents(replacements);
2609
2610 // TODO(brettw) it should be easier than this to do the correct language
2611 // handling without getting the accept language from the profile.
2612 std::wstring base_address = gfx::ElideUrl(clean_url, gfx::Font(), 0,
2613 profile()->GetPrefs()->GetString(prefs::kAcceptLanguages));
2614 // Force URL to have LTR directionality.
2615 if (l10n_util::GetTextDirection() == l10n_util::RIGHT_TO_LEFT)
2616 l10n_util::WrapStringWithLTRFormatting(&base_address);
2617
2618 return l10n_util::GetStringF(
2619 is_alert ? IDS_JAVASCRIPT_ALERT_TITLE : IDS_JAVASCRIPT_MESSAGEBOX_TITLE,
2620 base_address);
2621}
2622
2623gfx::NativeWindow TabContents::GetMessageBoxRootWindow() {
2624 return view_->GetTopLevelNativeWindow();
2625}
2626
2627void TabContents::OnMessageBoxClosed(IPC::Message* reply_msg,
2628 bool success,
2629 const std::wstring& prompt) {
2630 last_javascript_message_dismissal_ = base::TimeTicks::Now();
2631 if (is_showing_before_unload_dialog_ && !success) {
2632 // If a beforeunload dialog is canceled, we need to stop the throbber from
2633 // spinning, since we forced it to start spinning in Navigate.
2634 DidStopLoading();
2635
2636 tab_close_start_time_ = base::TimeTicks();
2637 }
2638 is_showing_before_unload_dialog_ = false;
2639 render_view_host()->JavaScriptMessageBoxClosed(reply_msg, success, prompt);
2640}
2641
2642void TabContents::SetSuppressMessageBoxes(bool suppress_message_boxes) {
2643 set_suppress_javascript_messages(suppress_message_boxes);
2644}
2645
[email protected]41fc0322009-09-04 22:23:402646void TabContents::set_encoding(const std::string& encoding) {
[email protected]aacd7cc2009-09-02 14:51:102647 encoding_ = CharacterEncoding::GetCanonicalEncodingNameByAliasName(encoding);
2648}