blob: 6b8ef7d4ceeddb2282d4524b92755e128c8545ca [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]93623c5d2009-12-10 21:40:3248#include "chrome/browser/renderer_preferences_util.h"
[email protected]bd1ad682009-05-15 22:19:1749#include "chrome/browser/tab_contents/infobar_delegate.h"
[email protected]7e383692009-06-12 19:14:5450#include "chrome/browser/tab_contents/interstitial_page.h"
[email protected]f3ec7742009-01-15 00:59:1651#include "chrome/browser/tab_contents/navigation_entry.h"
[email protected]de34a962009-06-26 23:22:4352#include "chrome/browser/tab_contents/provisional_load_details.h"
[email protected]f3ec7742009-01-15 00:59:1653#include "chrome/browser/tab_contents/tab_contents_delegate.h"
[email protected]fdd61c62009-04-22 19:22:5754#include "chrome/browser/tab_contents/tab_contents_view.h"
[email protected]58dca552009-06-17 00:35:0255#include "chrome/browser/tab_contents/thumbnail_generator.h"
[email protected]449478302009-06-09 20:04:2856#include "chrome/browser/thumbnail_store.h"
[email protected]420ae012009-04-24 05:16:3257#include "chrome/browser/search_engines/template_url_fetcher.h"
58#include "chrome/browser/search_engines/template_url_model.h"
[email protected]35f7d212009-04-29 21:19:2759#include "chrome/common/chrome_switches.h"
[email protected]5d246db22009-10-27 06:17:5760#include "chrome/common/extensions/extension_action.h"
[email protected]bfd04a62009-02-01 18:16:5661#include "chrome/common/notification_service.h"
[email protected]89d6e6e2009-12-03 23:06:5162#include "chrome/common/platform_util.h"
initial.commit09911bf2008-07-26 23:55:2963#include "chrome/common/pref_names.h"
[email protected]1eb89e82008-08-15 12:27:0364#include "chrome/common/pref_service.h"
[email protected]420ae012009-04-24 05:16:3265#include "chrome/common/render_messages.h"
[email protected]1ee614862009-07-06 20:29:1866#include "chrome/common/renderer_preferences.h"
[email protected]96d185d2009-04-24 03:28:5467#include "chrome/common/url_constants.h"
[email protected]8897c382009-05-06 17:53:2668#include "grit/generated_resources.h"
[email protected]420ae012009-04-24 05:16:3269#include "grit/locale_settings.h"
70#include "net/base/mime_util.h"
71#include "net/base/net_errors.h"
[email protected]d686e812009-06-03 19:10:2972#include "net/base/net_util.h"
[email protected]420ae012009-04-24 05:16:3273#include "net/base/registry_controlled_domain.h"
[email protected]1ee614862009-07-06 20:29:1874#include "webkit/glue/password_form.h"
75#include "webkit/glue/webpreferences.h"
[email protected]3e45ba92009-02-20 21:09:0076
[email protected]304f8792009-08-19 21:28:0977#if defined(OS_CHROMEOS)
78// For GdkScreen
79#include <gdk/gdk.h>
[email protected]0ba5c5e2009-08-20 16:47:1380#endif // defined(OS_CHROMEOS)
81
[email protected]420ae012009-04-24 05:16:3282// Cross-Site Navigations
83//
84// If a TabContents is told to navigate to a different web site (as determined
85// by SiteInstance), it will replace its current RenderViewHost with a new
86// RenderViewHost dedicated to the new SiteInstance. This works as follows:
87//
88// - Navigate determines whether the destination is cross-site, and if so,
89// it creates a pending_render_view_host_ and moves into the PENDING
90// RendererState.
91// - The pending RVH is "suspended," so that no navigation messages are sent to
92// its renderer until the onbeforeunload JavaScript handler has a chance to
93// run in the current RVH.
94// - The pending RVH tells CrossSiteRequestManager (a thread-safe singleton)
95// that it has a pending cross-site request. ResourceDispatcherHost will
96// check for this when the response arrives.
97// - The current RVH runs its onbeforeunload handler. If it returns false, we
98// cancel all the pending logic and go back to NORMAL. Otherwise we allow
99// the pending RVH to send the navigation request to its renderer.
100// - ResourceDispatcherHost receives a ResourceRequest on the IO thread. It
101// checks CrossSiteRequestManager to see that the RVH responsible has a
102// pending cross-site request, and then installs a CrossSiteEventHandler.
103// - When RDH receives a response, the BufferedEventHandler determines whether
104// it is a download. If so, it sends a message to the new renderer causing
105// it to cancel the request, and the download proceeds in the download
106// thread. For now, we stay in a PENDING state (with a pending RVH) until
[email protected]57c6a652009-05-04 07:58:34107// the next DidNavigate event for this TabContents. This isn't ideal, but it
[email protected]420ae012009-04-24 05:16:32108// doesn't affect any functionality.
109// - After RDH receives a response and determines that it is safe and not a
110// download, it pauses the response to first run the old page's onunload
111// handler. It does this by asynchronously calling the OnCrossSiteResponse
[email protected]57c6a652009-05-04 07:58:34112// method of TabContents on the UI thread, which sends a ClosePage message
[email protected]420ae012009-04-24 05:16:32113// to the current RVH.
114// - Once the onunload handler is finished, a ClosePage_ACK message is sent to
115// the ResourceDispatcherHost, who unpauses the response. Data is then sent
116// to the pending RVH.
117// - The pending renderer sends a FrameNavigate message that invokes the
118// DidNavigate method. This replaces the current RVH with the
119// pending RVH and goes back to the NORMAL RendererState.
120
121namespace {
122
123// Amount of time we wait between when a key event is received and the renderer
124// is queried for its state and pushed to the NavigationEntry.
125const int kQueryStateDelay = 5000;
126
127const int kSyncWaitDelay = 40;
128
[email protected]304f8792009-08-19 21:28:09129#if defined(OS_CHROMEOS)
130// If a popup window is bigger than this fraction of the screen on chrome os,
131// turn it into a tab
132const float kMaxWidthFactor = 0.5;
133const float kMaxHeightFactor = 0.6;
134#endif
135
[email protected]420ae012009-04-24 05:16:32136// If another javascript message box is displayed within
137// kJavascriptMessageExpectedDelay of a previous javascript message box being
138// dismissed, display an option to suppress future message boxes from this
139// contents.
140const int kJavascriptMessageExpectedDelay = 1000;
141
[email protected]420ae012009-04-24 05:16:32142// The list of prefs we want to observe.
143const wchar_t* kPrefsToObserve[] = {
144 prefs::kAlternateErrorPagesEnabled,
145 prefs::kWebKitJavaEnabled,
146 prefs::kWebKitJavascriptEnabled,
147 prefs::kWebKitLoadsImagesAutomatically,
148 prefs::kWebKitPluginsEnabled,
149 prefs::kWebKitUsesUniversalDetector,
150 prefs::kWebKitSerifFontFamily,
151 prefs::kWebKitSansSerifFontFamily,
152 prefs::kWebKitFixedFontFamily,
153 prefs::kWebKitDefaultFontSize,
154 prefs::kWebKitDefaultFixedFontSize,
155 prefs::kDefaultCharset
156 // kWebKitStandardFontIsSerif needs to be added
157 // if we let users pick which font to use, serif or sans-serif when
158 // no font is specified or a CSS generic family (serif or sans-serif)
159 // is not specified.
160};
161
162const int kPrefsToObserveLength = arraysize(kPrefsToObserve);
163
[email protected]420ae012009-04-24 05:16:32164// Returns true if the entry's transition type is FORM_SUBMIT.
165bool IsFormSubmit(const NavigationEntry* entry) {
166 return (PageTransition::StripQualifier(entry->transition_type()) ==
167 PageTransition::FORM_SUBMIT);
168}
169
170#if defined(OS_WIN)
171
172BOOL CALLBACK InvalidateWindow(HWND hwnd, LPARAM lparam) {
173 // Note: erase is required to properly paint some widgets borders. This can
174 // be seen with textfields.
175 InvalidateRect(hwnd, NULL, TRUE);
176 return TRUE;
177}
178#endif
179
[email protected]5e369672009-11-03 23:48:30180ViewMsg_Navigate_Params::NavigationType GetNavigationType(
181 Profile* profile, const NavigationEntry& entry, bool reload) {
182 if (reload)
183 return ViewMsg_Navigate_Params::RELOAD;
184
185 if (entry.restore_type() == NavigationEntry::RESTORE_LAST_SESSION &&
186 profile->DidLastSessionExitCleanly())
187 return ViewMsg_Navigate_Params::RESTORE;
188
189 return ViewMsg_Navigate_Params::NORMAL;
190}
191
192void MakeNavigateParams(Profile* profile, const NavigationEntry& entry,
193 bool reload, ViewMsg_Navigate_Params* params) {
[email protected]056de2d2009-06-26 16:41:34194 params->page_id = entry.page_id();
195 params->url = entry.url();
196 params->referrer = entry.referrer();
197 params->transition = entry.transition_type();
198 params->state = entry.content_state();
[email protected]5e369672009-11-03 23:48:30199 params->navigation_type = GetNavigationType(profile, entry, reload);
[email protected]056de2d2009-06-26 16:41:34200 params->request_time = base::Time::Now();
201}
202
[email protected]420ae012009-04-24 05:16:32203} // namespace
204
205// -----------------------------------------------------------------------------
206
[email protected]fdd61c62009-04-22 19:22:57207// static
208int TabContents::find_request_id_counter_ = -1;
209
[email protected]420ae012009-04-24 05:16:32210
[email protected]420ae012009-04-24 05:16:32211TabContents::TabContents(Profile* profile,
212 SiteInstance* site_instance,
213 int routing_id,
[email protected]34ac70502009-09-25 17:07:23214 const TabContents* base_tab_contents)
[email protected]b680ad22009-04-15 23:19:42215 : delegate_(NULL),
[email protected]66ba4932009-06-04 19:22:13216 ALLOW_THIS_IN_INITIALIZER_LIST(controller_(this, profile)),
217 ALLOW_THIS_IN_INITIALIZER_LIST(view_(
[email protected]d82ed61e2009-06-16 02:46:22218 TabContentsView::Create(this))),
219 ALLOW_THIS_IN_INITIALIZER_LIST(render_manager_(this, this)),
[email protected]fdd61c62009-04-22 19:22:57220 property_bag_(),
221 registrar_(),
[email protected]d82ed61e2009-06-16 02:46:22222 ALLOW_THIS_IN_INITIALIZER_LIST(printing_(*this)),
[email protected]fdd61c62009-04-22 19:22:57223 save_package_(),
[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_++),
[email protected]e491f1c2009-05-22 20:28:12248 last_search_case_sensitive_(false),
249 last_search_prepopulate_text_(NULL),
250 last_search_result_(),
[email protected]fdd61c62009-04-22 19:22:57251 capturing_contents_(false),
252 is_being_destroyed_(false),
253 notify_disconnection_(false),
254 history_requests_(),
255#if defined(OS_WIN)
256 message_box_active_(CreateEvent(NULL, TRUE, FALSE, NULL)),
257#endif
258 last_javascript_message_dismissal_(),
[email protected]c2e74fe82009-09-03 17:57:44259 suppress_javascript_messages_(false),
[email protected]7ab1e7d62009-10-14 23:32:01260 is_showing_before_unload_dialog_(false),
[email protected]7ff431e2010-01-07 18:18:54261 renderer_preferences_(),
[email protected]c2e74fe82009-09-03 17:57:44262 opener_dom_ui_type_(DOMUIFactory::kNoDOMUI) {
[email protected]7ff431e2010-01-07 18:18:54263 renderer_preferences_util::UpdateFromSystemSettings(
264 &renderer_preferences_, profile);
265
[email protected]16779842009-07-08 23:45:29266#if defined(OS_CHROMEOS)
[email protected]58dca552009-06-17 00:35:02267 // Make sure the thumbnailer is started before starting the render manager.
268 // The thumbnailer will want to listen for RVH creations, one of which will
269 // happen in RVHManager::Init.
270 ThumbnailGenerator* generator = g_browser_process->GetThumbnailGenerator();
271 if (generator)
272 generator->StartThumbnailing();
273#endif
274
[email protected]12636df2009-09-28 22:32:21275 render_manager_.Init(profile, site_instance, routing_id);
[email protected]420ae012009-04-24 05:16:32276
[email protected]34ac70502009-09-25 17:07:23277 // We have the initial size of the view be based on the size of the passed in
278 // tab contents (normally a tab from the same window).
279 view_->CreateView(base_tab_contents ?
280 base_tab_contents->view()->GetContainerSize() : gfx::Size());
[email protected]420ae012009-04-24 05:16:32281
282 // Register for notifications about all interested prefs change.
283 PrefService* prefs = profile->GetPrefs();
284 if (prefs) {
285 for (int i = 0; i < kPrefsToObserveLength; ++i)
286 prefs->AddPrefObserver(kPrefsToObserve[i], this);
287 }
288
289 // Register for notifications about URL starredness changing on any profile.
290 registrar_.Add(this, NotificationType::URLS_STARRED,
291 NotificationService::AllSources());
292 registrar_.Add(this, NotificationType::BOOKMARK_MODEL_LOADED,
293 NotificationService::AllSources());
294 registrar_.Add(this, NotificationType::RENDER_WIDGET_HOST_DESTROYED,
295 NotificationService::AllSources());
[email protected]d3ba77272009-09-03 00:06:09296#if defined(OS_LINUX)
297 registrar_.Add(this, NotificationType::BROWSER_THEME_CHANGED,
298 NotificationService::AllSources());
299#endif
[email protected]420ae012009-04-24 05:16:32300
301 // Keep a global copy of the previous search string (if any).
302 static string16 global_last_search = string16();
[email protected]e491f1c2009-05-22 20:28:12303 last_search_prepopulate_text_ = &global_last_search;
[email protected]840b1512009-07-21 16:53:46304
[email protected]c8865482009-07-23 20:40:10305 // Set-up the showing of the omnibox search infobar if applicable.
306 if (OmniboxSearchHint::IsEnabled(profile))
307 omnibox_search_hint_.reset(new OmniboxSearchHint(this));
[email protected]332af7732009-03-11 13:21:35308}
initial.commit09911bf2008-07-26 23:55:29309
310TabContents::~TabContents() {
[email protected]420ae012009-04-24 05:16:32311 is_being_destroyed_ = true;
312
[email protected]bfe4c1582009-10-02 18:11:09313 // We don't want any notifications while we're running our destructor.
[email protected]420ae012009-04-24 05:16:32314 registrar_.RemoveAll();
315
316 // Unregister the notifications of all observed prefs change.
317 PrefService* prefs = profile()->GetPrefs();
318 if (prefs) {
319 for (int i = 0; i < kPrefsToObserveLength; ++i)
320 prefs->RemovePrefObserver(kPrefsToObserve[i], this);
321 }
322
[email protected]420ae012009-04-24 05:16:32323 NotifyDisconnected();
[email protected]e7b8d832009-11-20 22:41:00324 hung_renderer_dialog::HideForTabContents(this);
[email protected]420ae012009-04-24 05:16:32325
[email protected]420ae012009-04-24 05:16:32326 // First cleanly close all child windows.
327 // TODO(mpcomplete): handle case if MaybeCloseChildWindows() already asked
328 // some of these to close. CloseWindows is async, so it might get called
329 // twice before it runs.
330 int size = static_cast<int>(child_windows_.size());
331 for (int i = size - 1; i >= 0; --i) {
332 ConstrainedWindow* window = child_windows_[i];
333 if (window)
334 window->CloseConstrainedWindow();
335 }
336
[email protected]66ba4932009-06-04 19:22:13337 if (blocked_popups_)
338 blocked_popups_->Destroy();
339
[email protected]420ae012009-04-24 05:16:32340 // Notify any observer that have a reference on this tab contents.
341 NotificationService::current()->Notify(
342 NotificationType::TAB_CONTENTS_DESTROYED,
343 Source<TabContents>(this),
344 NotificationService::NoDetails());
345
[email protected]1e5e74f2009-05-27 20:55:12346 // Notify any lasting InfobarDelegates that have not yet been removed that
347 // whatever infobar they were handling in this TabContents has closed,
348 // because the TabContents is going away entirely.
349 // This must happen after the TAB_CONTENTS_DESTROYED notification as the
350 // notification may trigger infobars calls that access their delegate. (and
351 // some implementations of InfoBarDelegate do delete themselves on
352 // InfoBarClosed()).
353 for (int i = 0; i < infobar_delegate_count(); ++i) {
354 InfoBarDelegate* delegate = GetInfoBarDelegateAt(i);
355 delegate->InfoBarClosed();
356 }
357 infobar_delegates_.clear();
358
[email protected]420ae012009-04-24 05:16:32359 // TODO(brettw) this should be moved to the view.
360#if defined(OS_WIN)
361 // If we still have a window handle, destroy it. GetNativeView can return
362 // NULL if this contents was part of a window that closed.
363 if (GetNativeView())
364 ::DestroyWindow(GetNativeView());
365#endif
[email protected]7ab1e7d62009-10-14 23:32:01366
367 // OnCloseStarted isn't called in unit tests.
368 if (!tab_close_start_time_.is_null()) {
369 UMA_HISTOGRAM_TIMES("Tab.Close",
370 base::TimeTicks::Now() - tab_close_start_time_);
371 }
initial.commit09911bf2008-07-26 23:55:29372}
373
[email protected]d5f942ba2008-09-26 19:30:34374// static
375void TabContents::RegisterUserPrefs(PrefService* prefs) {
[email protected]420ae012009-04-24 05:16:32376 prefs->RegisterBooleanPref(prefs::kAlternateErrorPagesEnabled, true);
377
378 WebPreferences pref_defaults;
379 prefs->RegisterBooleanPref(prefs::kWebKitJavascriptEnabled,
380 pref_defaults.javascript_enabled);
381 prefs->RegisterBooleanPref(prefs::kWebKitWebSecurityEnabled,
382 pref_defaults.web_security_enabled);
383 prefs->RegisterBooleanPref(
384 prefs::kWebKitJavascriptCanOpenWindowsAutomatically, true);
385 prefs->RegisterBooleanPref(prefs::kWebKitLoadsImagesAutomatically,
386 pref_defaults.loads_images_automatically);
387 prefs->RegisterBooleanPref(prefs::kWebKitPluginsEnabled,
388 pref_defaults.plugins_enabled);
389 prefs->RegisterBooleanPref(prefs::kWebKitDomPasteEnabled,
390 pref_defaults.dom_paste_enabled);
391 prefs->RegisterBooleanPref(prefs::kWebKitShrinksStandaloneImagesToFit,
392 pref_defaults.shrinks_standalone_images_to_fit);
[email protected]0df30122009-06-03 12:13:08393 prefs->RegisterStringPref(prefs::kWebKitInspectorSettings,
[email protected]8922e1f2009-10-03 05:01:26394 UTF8ToWide(pref_defaults.inspector_settings));
[email protected]420ae012009-04-24 05:16:32395 prefs->RegisterBooleanPref(prefs::kWebKitTextAreasAreResizable,
396 pref_defaults.text_areas_are_resizable);
397 prefs->RegisterBooleanPref(prefs::kWebKitJavaEnabled,
398 pref_defaults.java_enabled);
399
400 prefs->RegisterLocalizedStringPref(prefs::kAcceptLanguages,
401 IDS_ACCEPT_LANGUAGES);
402 prefs->RegisterLocalizedStringPref(prefs::kDefaultCharset,
403 IDS_DEFAULT_ENCODING);
404 prefs->RegisterLocalizedBooleanPref(prefs::kWebKitStandardFontIsSerif,
405 IDS_STANDARD_FONT_IS_SERIF);
406 prefs->RegisterLocalizedStringPref(prefs::kWebKitFixedFontFamily,
407 IDS_FIXED_FONT_FAMILY);
408 prefs->RegisterLocalizedStringPref(prefs::kWebKitSerifFontFamily,
409 IDS_SERIF_FONT_FAMILY);
410 prefs->RegisterLocalizedStringPref(prefs::kWebKitSansSerifFontFamily,
411 IDS_SANS_SERIF_FONT_FAMILY);
412 prefs->RegisterLocalizedStringPref(prefs::kWebKitCursiveFontFamily,
413 IDS_CURSIVE_FONT_FAMILY);
414 prefs->RegisterLocalizedStringPref(prefs::kWebKitFantasyFontFamily,
415 IDS_FANTASY_FONT_FAMILY);
416 prefs->RegisterLocalizedIntegerPref(prefs::kWebKitDefaultFontSize,
417 IDS_DEFAULT_FONT_SIZE);
418 prefs->RegisterLocalizedIntegerPref(prefs::kWebKitDefaultFixedFontSize,
419 IDS_DEFAULT_FIXED_FONT_SIZE);
420 prefs->RegisterLocalizedIntegerPref(prefs::kWebKitMinimumFontSize,
421 IDS_MINIMUM_FONT_SIZE);
422 prefs->RegisterLocalizedIntegerPref(prefs::kWebKitMinimumLogicalFontSize,
423 IDS_MINIMUM_LOGICAL_FONT_SIZE);
424 prefs->RegisterLocalizedBooleanPref(prefs::kWebKitUsesUniversalDetector,
425 IDS_USES_UNIVERSAL_DETECTOR);
426 prefs->RegisterLocalizedStringPref(prefs::kStaticEncodings,
427 IDS_STATIC_ENCODING_LIST);
initial.commit09911bf2008-07-26 23:55:29428}
429
[email protected]13367f72009-07-08 16:39:02430// Returns true if contains content rendered by an extension.
431bool TabContents::HostsExtension() const {
432 return GetURL().SchemeIs(chrome::kExtensionScheme);
[email protected]d5f942ba2008-09-26 19:30:34433}
434
[email protected]6e95706d2009-04-23 22:55:01435PasswordManager* TabContents::GetPasswordManager() {
436 if (password_manager_.get() == NULL)
437 password_manager_.reset(new PasswordManager(this));
438 return password_manager_.get();
439}
440
441PluginInstaller* TabContents::GetPluginInstaller() {
442 if (plugin_installer_.get() == NULL)
443 plugin_installer_.reset(new PluginInstaller(this));
444 return plugin_installer_.get();
445}
446
[email protected]d5f942ba2008-09-26 19:30:34447const GURL& TabContents::GetURL() const {
448 // We may not have a navigation entry yet
[email protected]ce3fa3c2009-04-20 19:55:57449 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]ebe89e062009-08-13 23:16:54450 return entry ? entry->virtual_url() : GURL::EmptyGURL();
[email protected]d5f942ba2008-09-26 19:30:34451}
452
[email protected]96d185d2009-04-24 03:28:54453const string16& TabContents::GetTitle() const {
[email protected]4c6092c5b2009-06-06 00:23:55454 // Transient entries take precedence. They are used for interstitial pages
455 // that are shown on top of existing pages.
456 NavigationEntry* entry = controller_.GetTransientEntry();
457 if (entry)
458 return entry->GetTitleForDisplay(&controller_);
459
[email protected]96d185d2009-04-24 03:28:54460 DOMUI* our_dom_ui = render_manager_.pending_dom_ui() ?
461 render_manager_.pending_dom_ui() : render_manager_.dom_ui();
462 if (our_dom_ui) {
463 // Don't override the title in view source mode.
[email protected]4c6092c5b2009-06-06 00:23:55464 entry = controller_.GetActiveEntry();
[email protected]96d185d2009-04-24 03:28:54465 if (!(entry && entry->IsViewSourceMode())) {
466 // Give the DOM UI the chance to override our title.
467 const string16& title = our_dom_ui->overridden_title();
468 if (!title.empty())
469 return title;
470 }
471 }
472
473 // We use the title for the last committed entry rather than a pending
474 // navigation entry. For example, when the user types in a URL, we want to
475 // keep the old page's title until the new load has committed and we get a new
476 // title.
[email protected]96d185d2009-04-24 03:28:54477 entry = controller_.GetLastCommittedEntry();
478 if (entry)
479 return entry->GetTitleForDisplay(&controller_);
[email protected]96d185d2009-04-24 03:28:54480 return EmptyString16();
481}
482
[email protected]d5f942ba2008-09-26 19:30:34483int32 TabContents::GetMaxPageID() {
484 if (GetSiteInstance())
485 return GetSiteInstance()->max_page_id();
486 else
487 return max_page_id_;
488}
489
490void TabContents::UpdateMaxPageID(int32 page_id) {
491 // Ensure both the SiteInstance and RenderProcessHost update their max page
[email protected]57c6a652009-05-04 07:58:34492 // IDs in sync. Only TabContents will also have site instances, except during
[email protected]d5f942ba2008-09-26 19:30:34493 // testing.
494 if (GetSiteInstance())
495 GetSiteInstance()->UpdateMaxPageID(page_id);
[email protected]57c6a652009-05-04 07:58:34496 process()->UpdateMaxPageID(page_id);
[email protected]d5f942ba2008-09-26 19:30:34497}
498
[email protected]96d185d2009-04-24 03:28:54499SiteInstance* TabContents::GetSiteInstance() const {
500 return render_manager_.current_host()->site_instance();
501}
502
[email protected]d5f942ba2008-09-26 19:30:34503const std::wstring TabContents::GetDefaultTitle() const {
504 return l10n_util::GetString(IDS_DEFAULT_TAB_TITLE);
505}
506
[email protected]96d185d2009-04-24 03:28:54507bool TabContents::ShouldDisplayURL() {
[email protected]8ab24cc2009-11-13 20:56:15508 // Don't hide the url in view source mode and with interstitials.
[email protected]96d185d2009-04-24 03:28:54509 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]8ab24cc2009-11-13 20:56:15510 if (entry && (entry->IsViewSourceMode() ||
511 entry->page_type() == NavigationEntry::INTERSTITIAL_PAGE)) {
[email protected]96d185d2009-04-24 03:28:54512 return true;
[email protected]8ab24cc2009-11-13 20:56:15513 }
[email protected]83e3895b2009-06-11 00:07:16514
[email protected]96d185d2009-04-24 03:28:54515 DOMUI* dom_ui = GetDOMUIForCurrentState();
516 if (dom_ui)
517 return !dom_ui->should_hide_url();
518 return true;
519}
520
[email protected]d5f942ba2008-09-26 19:30:34521SkBitmap TabContents::GetFavIcon() const {
522 // Like GetTitle(), we also want to use the favicon for the last committed
523 // entry rather than a pending navigation entry.
[email protected]ce3fa3c2009-04-20 19:55:57524 NavigationEntry* entry = controller_.GetTransientEntry();
[email protected]cbab76d2008-10-13 22:42:47525 if (entry)
526 return entry->favicon().bitmap();
527
[email protected]ce3fa3c2009-04-20 19:55:57528 entry = controller_.GetLastCommittedEntry();
[email protected]d5f942ba2008-09-26 19:30:34529 if (entry)
530 return entry->favicon().bitmap();
[email protected]d5f942ba2008-09-26 19:30:34531 return SkBitmap();
532}
533
[email protected]0b303cc2009-09-28 22:35:15534bool TabContents::FavIconIsValid() const {
535 NavigationEntry* entry = controller_.GetTransientEntry();
536 if (entry)
537 return entry->favicon().is_valid();
538
539 entry = controller_.GetLastCommittedEntry();
540 if (entry)
541 return entry->favicon().is_valid();
542
543 return false;
544}
545
[email protected]96d185d2009-04-24 03:28:54546bool TabContents::ShouldDisplayFavIcon() {
547 // Always display a throbber during pending loads.
548 if (controller_.GetLastCommittedEntry() && controller_.pending_entry())
549 return true;
550
551 DOMUI* dom_ui = GetDOMUIForCurrentState();
552 if (dom_ui)
553 return !dom_ui->hide_favicon();
554 return true;
555}
556
[email protected]96d185d2009-04-24 03:28:54557std::wstring TabContents::GetStatusText() const {
558 if (!is_loading() || load_state_ == net::LOAD_STATE_IDLE)
559 return std::wstring();
560
561 switch (load_state_) {
562 case net::LOAD_STATE_WAITING_FOR_CACHE:
563 return l10n_util::GetString(IDS_LOAD_STATE_WAITING_FOR_CACHE);
564 case net::LOAD_STATE_RESOLVING_PROXY_FOR_URL:
565 return l10n_util::GetString(IDS_LOAD_STATE_RESOLVING_PROXY_FOR_URL);
566 case net::LOAD_STATE_RESOLVING_HOST:
567 return l10n_util::GetString(IDS_LOAD_STATE_RESOLVING_HOST);
568 case net::LOAD_STATE_CONNECTING:
569 return l10n_util::GetString(IDS_LOAD_STATE_CONNECTING);
570 case net::LOAD_STATE_SENDING_REQUEST:
[email protected]094e5b22009-09-25 04:23:56571 if (upload_size_)
572 return l10n_util::GetStringF(
573 IDS_LOAD_STATE_SENDING_REQUEST_WITH_PROGRESS,
574 static_cast<int>((100 * upload_position_) / upload_size_));
575 else
576 return l10n_util::GetString(IDS_LOAD_STATE_SENDING_REQUEST);
[email protected]96d185d2009-04-24 03:28:54577 case net::LOAD_STATE_WAITING_FOR_RESPONSE:
578 return l10n_util::GetStringF(IDS_LOAD_STATE_WAITING_FOR_RESPONSE,
579 load_state_host_);
580 // Ignore net::LOAD_STATE_READING_RESPONSE and net::LOAD_STATE_IDLE
581 case net::LOAD_STATE_IDLE:
582 case net::LOAD_STATE_READING_RESPONSE:
583 break;
584 }
585
586 return std::wstring();
587}
588
[email protected]d5f942ba2008-09-26 19:30:34589void TabContents::SetIsCrashed(bool state) {
590 if (state == is_crashed_)
591 return;
592
593 is_crashed_ = state;
[email protected]c9cd2222009-05-06 05:16:50594 NotifyNavigationStateChanged(INVALIDATE_TAB);
[email protected]d5f942ba2008-09-26 19:30:34595}
596
[email protected]744ef172009-10-16 21:53:46597void TabContents::PageActionStateChanged() {
598 NotifyNavigationStateChanged(TabContents::INVALIDATE_PAGE_ACTIONS);
[email protected]d7eaf572009-07-01 21:57:00599}
[email protected]ce5c4502009-05-06 16:46:11600
[email protected]d5f942ba2008-09-26 19:30:34601void TabContents::NotifyNavigationStateChanged(unsigned changed_flags) {
602 if (delegate_)
603 delegate_->NavigationStateChanged(this, changed_flags);
604}
605
[email protected]96d185d2009-04-24 03:28:54606void TabContents::DidBecomeSelected() {
607 controller_.SetActive(true);
[email protected]789e9152009-08-04 21:59:43608 if (render_widget_host_view()) {
[email protected]96d185d2009-04-24 03:28:54609 render_widget_host_view()->DidBecomeSelected();
[email protected]789e9152009-08-04 21:59:43610#if defined(OS_MACOSX)
611 render_widget_host_view()->SetActive(true);
612#endif
613 }
[email protected]96d185d2009-04-24 03:28:54614
[email protected]76543b92009-08-31 17:27:45615 WebCacheManager::GetInstance()->ObserveActivity(process()->id());
[email protected]96d185d2009-04-24 03:28:54616}
617
618void TabContents::WasHidden() {
619 if (!capturing_contents()) {
620 // |render_view_host()| can be NULL if the user middle clicks a link to open
621 // a tab in then background, then closes the tab before selecting it. This
622 // is because closing the tab calls TabContents::Destroy(), which removes
623 // the |render_view_host()|; then when we actually destroy the window,
624 // OnWindowPosChanged() notices and calls HideContents() (which calls us).
625 if (render_widget_host_view())
626 render_widget_host_view()->WasHidden();
[email protected]96d185d2009-04-24 03:28:54627 }
628
629 NotificationService::current()->Notify(
630 NotificationType::TAB_CONTENTS_HIDDEN,
631 Source<TabContents>(this),
632 NotificationService::NoDetails());
633}
634
[email protected]d5f942ba2008-09-26 19:30:34635void TabContents::Activate() {
636 if (delegate_)
637 delegate_->ActivateContents(this);
638}
639
[email protected]96d185d2009-04-24 03:28:54640void TabContents::ShowContents() {
641 if (render_widget_host_view())
642 render_widget_host_view()->DidBecomeSelected();
[email protected]96d185d2009-04-24 03:28:54643}
644
645void TabContents::HideContents() {
646 // TODO(pkasting): http://b/1239839 Right now we purposefully don't call
647 // our superclass HideContents(), because some callers want to be very picky
648 // about the order in which these get called. In addition to making the code
649 // here practically impossible to understand, this also means we end up
650 // calling TabContents::WasHidden() twice if callers call both versions of
[email protected]57c6a652009-05-04 07:58:34651 // HideContents() on a TabContents.
[email protected]96d185d2009-04-24 03:28:54652 WasHidden();
653}
654
[email protected]c0588052008-10-27 23:01:50655void TabContents::OpenURL(const GURL& url, const GURL& referrer,
[email protected]d5f942ba2008-09-26 19:30:34656 WindowOpenDisposition disposition,
657 PageTransition::Type transition) {
658 if (delegate_)
[email protected]c0588052008-10-27 23:01:50659 delegate_->OpenURLFromTab(this, url, referrer, disposition, transition);
[email protected]d5f942ba2008-09-26 19:30:34660}
661
662bool TabContents::NavigateToPendingEntry(bool reload) {
[email protected]96d185d2009-04-24 03:28:54663 const NavigationEntry& entry = *controller_.pending_entry();
664
665 RenderViewHost* dest_render_view_host = render_manager_.Navigate(entry);
666 if (!dest_render_view_host)
667 return false; // Unable to create the desired render view host.
668
669 // Tell DevTools agent that it is attached prior to the navigation.
[email protected]d9f9b792009-06-24 13:17:12670 DevToolsManager* devtools_manager = DevToolsManager::GetInstance();
[email protected]7e8e6b62009-05-08 11:28:32671 if (devtools_manager) { // NULL in unit tests.
[email protected]684b4ba22009-05-07 06:27:41672 devtools_manager->OnNavigatingToPendingEntry(
673 render_view_host(),
674 dest_render_view_host,
675 controller_.pending_entry()->url());
[email protected]7e8e6b62009-05-08 11:28:32676 }
[email protected]96d185d2009-04-24 03:28:54677
678 // Used for page load time metrics.
679 current_load_start_ = base::TimeTicks::Now();
680
681 // Navigate in the desired RenderViewHost.
[email protected]056de2d2009-06-26 16:41:34682 ViewMsg_Navigate_Params navigate_params;
[email protected]5e369672009-11-03 23:48:30683 MakeNavigateParams(profile(), entry, reload, &navigate_params);
[email protected]056de2d2009-06-26 16:41:34684 dest_render_view_host->Navigate(navigate_params);
[email protected]96d185d2009-04-24 03:28:54685
686 if (entry.page_id() == -1) {
687 // HACK!! This code suppresses javascript: URLs from being added to
688 // session history, which is what we want to do for javascript: URLs that
689 // do not generate content. What we really need is a message from the
690 // renderer telling us that a new page was not created. The same message
691 // could be used for mailto: URLs and the like.
692 if (entry.url().SchemeIs(chrome::kJavaScriptScheme))
693 return false;
694 }
695
696 // Clear any provisional password saves - this stops password infobars
697 // showing up on pages the user navigates to while the right page is
698 // loading.
699 GetPasswordManager()->ClearProvisionalSave();
700
701 if (reload && !profile()->IsOffTheRecord()) {
[email protected]0189bc722009-08-28 21:56:48702 FaviconService* favicon_service =
703 profile()->GetFaviconService(Profile::IMPLICIT_ACCESS);
704 if (favicon_service)
705 favicon_service->SetFaviconOutOfDateForPage(entry.url());
[email protected]96d185d2009-04-24 03:28:54706 }
707
[email protected]d5f942ba2008-09-26 19:30:34708 return true;
709}
710
[email protected]96d185d2009-04-24 03:28:54711void TabContents::Stop() {
712 render_manager_.Stop();
713 printing_.Stop();
714}
715
716void TabContents::Cut() {
717 render_view_host()->Cut();
718}
719
720void TabContents::Copy() {
721 render_view_host()->Copy();
722}
723
[email protected]c0cc3092009-09-12 08:27:27724#if defined(OS_MACOSX)
[email protected]a954bf72009-09-12 07:30:35725void TabContents::CopyToFindPboard() {
726 render_view_host()->CopyToFindPboard();
727}
[email protected]c0cc3092009-09-12 08:27:27728#endif
[email protected]a954bf72009-09-12 07:30:35729
[email protected]96d185d2009-04-24 03:28:54730void TabContents::Paste() {
731 render_view_host()->Paste();
732}
733
[email protected]7e786822009-11-07 00:00:27734#if defined(OS_MACOSX)
735void TabContents::PasteAndMatchStyle() {
736 render_view_host()->ForwardEditCommand("PasteAndMatchStyle", "");
737}
738#endif
739
[email protected]96d185d2009-04-24 03:28:54740void TabContents::DisassociateFromPopupCount() {
741 render_view_host()->DisassociateFromPopupCount();
742}
743
744TabContents* TabContents::Clone() {
745 // We create a new SiteInstance so that the new tab won't share processes
746 // with the old one. This can be changed in the future if we need it to share
747 // processes for some reason.
[email protected]420ae012009-04-24 05:16:32748 TabContents* tc = new TabContents(profile(),
[email protected]96d185d2009-04-24 03:28:54749 SiteInstance::CreateSiteInstance(profile()),
[email protected]12636df2009-09-28 22:32:21750 MSG_ROUTING_NONE, this);
[email protected]96d185d2009-04-24 03:28:54751 tc->controller().CopyStateFrom(controller_);
752 return tc;
753}
754
[email protected]4d677202009-07-19 07:37:12755void TabContents::ShowPageInfo(const GURL& url,
756 const NavigationEntry::SSLStatus& ssl,
757 bool show_history) {
758 if (!delegate_)
759 return;
760
[email protected]bb678332009-07-21 00:15:50761 delegate_->ShowPageInfo(profile(), url, ssl, show_history);
[email protected]4d677202009-07-19 07:37:12762}
763
initial.commit09911bf2008-07-26 23:55:29764ConstrainedWindow* TabContents::CreateConstrainedDialog(
[email protected]e8382172009-06-19 22:16:28765 ConstrainedWindowDelegate* delegate) {
initial.commit09911bf2008-07-26 23:55:29766 ConstrainedWindow* window =
[email protected]e8382172009-06-19 22:16:28767 ConstrainedWindow::CreateConstrainedDialog(this, delegate);
initial.commit09911bf2008-07-26 23:55:29768 child_windows_.push_back(window);
769 return window;
770}
771
772void TabContents::AddNewContents(TabContents* new_contents,
773 WindowOpenDisposition disposition,
774 const gfx::Rect& initial_pos,
[email protected]c88a70fe2009-05-05 20:00:22775 bool user_gesture,
776 const GURL& creator_url) {
initial.commit09911bf2008-07-26 23:55:29777 if (!delegate_)
778 return;
779
[email protected]35f7d212009-04-29 21:19:27780 if ((disposition == NEW_POPUP) && !user_gesture &&
781 !CommandLine::ForCurrentProcess()->HasSwitch(
782 switches::kDisablePopupBlocking)) {
[email protected]2c4410d2009-05-06 23:46:22783 // Unrequested popups from normal pages are constrained unless they're in
[email protected]b6c874582009-05-08 19:38:31784 // the whitelist. The popup owner will handle checking this.
785 delegate_->GetConstrainingContents(this)->AddPopup(new_contents,
786 initial_pos,
787 creator_url.is_valid() ? creator_url.host() : std::string());
788 } else {
[email protected]304f8792009-08-19 21:28:09789#if defined(OS_CHROMEOS)
790 if (disposition == NEW_POPUP) {
791 // If the popup is bigger than a given factor of the screen, then
792 // turn it into a foreground tab (on chrome os only)
793 GdkScreen* screen = gdk_screen_get_default();
794 int max_width = gdk_screen_get_width(screen) * kMaxWidthFactor;
795 int max_height = gdk_screen_get_height(screen) * kMaxHeightFactor;
796 if (initial_pos.width() > max_width ||
797 initial_pos.height() > max_height) {
798 disposition = NEW_FOREGROUND_TAB;
799 }
800 }
801#endif
802
[email protected]0aa55312008-10-17 21:53:08803 new_contents->DisassociateFromPopupCount();
initial.commit09911bf2008-07-26 23:55:29804 delegate_->AddNewContents(this, new_contents, disposition, initial_pos,
805 user_gesture);
[email protected]7f37dd92009-09-15 16:35:49806 NotificationService::current()->Notify(
807 NotificationType::TAB_ADDED,
808 Source<TabContentsDelegate>(delegate_),
809 Details<TabContents>(this));
initial.commit09911bf2008-07-26 23:55:29810 }
[email protected]b6c874582009-05-08 19:38:31811 PopupNotificationVisibilityChanged(ShowingBlockedPopupNotification());
initial.commit09911bf2008-07-26 23:55:29812}
813
initial.commit09911bf2008-07-26 23:55:29814void TabContents::CloseAllSuppressedPopups() {
[email protected]d6598c052008-11-05 19:03:25815 if (blocked_popups_)
[email protected]2c4410d2009-05-06 23:46:22816 blocked_popups_->CloseAll();
initial.commit09911bf2008-07-26 23:55:29817}
818
[email protected]912256b32009-09-18 09:47:35819void TabContents::ExecuteCode(int request_id, const std::string& extension_id,
[email protected]61f5a7b2009-12-22 22:21:20820 const std::vector<URLPattern>& host_permissions,
[email protected]20ad2692009-11-20 18:27:20821 bool is_js_code, const std::string& code_string,
822 bool all_frames) {
[email protected]912256b32009-09-18 09:47:35823 render_view_host()->Send(
[email protected]61f5a7b2009-12-22 22:21:20824 new ViewMsg_ExecuteCode(render_view_host()->routing_id(),
825 ViewMsg_ExecuteCode_Params(request_id, extension_id, host_permissions,
826 is_js_code, code_string, all_frames)));
[email protected]912256b32009-09-18 09:47:35827}
828
[email protected]96d185d2009-04-24 03:28:54829void TabContents::PopupNotificationVisibilityChanged(bool visible) {
830 render_view_host()->PopupNotificationVisibilityChanged(visible);
831}
832
[email protected]5c9e97a2009-09-09 23:48:30833gfx::NativeView TabContents::GetContentNativeView() const {
[email protected]96d185d2009-04-24 03:28:54834 return view_->GetContentNativeView();
835}
836
837gfx::NativeView TabContents::GetNativeView() const {
838 return view_->GetNativeView();
839}
840
841void TabContents::GetContainerBounds(gfx::Rect *out) const {
842 view_->GetContainerBounds(out);
843}
844
845void TabContents::Focus() {
846 view_->Focus();
847}
848
[email protected]90daadb42009-06-08 21:27:28849void TabContents::FocusThroughTabTraversal(bool reverse) {
[email protected]7e383692009-06-12 19:14:54850 if (showing_interstitial_page()) {
851 render_manager_.interstitial_page()->FocusThroughTabTraversal(reverse);
852 return;
853 }
[email protected]96d185d2009-04-24 03:28:54854 render_view_host()->SetInitialFocus(reverse);
855}
856
857bool TabContents::FocusLocationBarByDefault() {
858 DOMUI* dom_ui = GetDOMUIForCurrentState();
859 if (dom_ui)
860 return dom_ui->focus_location_bar_by_default();
[email protected]3e3f0eb2009-06-22 18:33:43861 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]e0d481582009-09-15 21:06:25862 if (entry && entry->url() == GURL(chrome::kAboutBlankURL))
[email protected]3e3f0eb2009-06-22 18:33:43863 return true;
[email protected]96d185d2009-04-24 03:28:54864 return false;
865}
866
[email protected]616ed5a2008-11-21 22:27:24867void TabContents::AddInfoBar(InfoBarDelegate* delegate) {
868 // Look through the existing InfoBarDelegates we have for a match. If we've
869 // already got one that matches, then we don't add the new one.
[email protected]f86a07022008-11-25 01:06:05870 for (int i = 0; i < infobar_delegate_count(); ++i) {
[email protected]6d27a7b2009-12-18 23:25:45871 if (GetInfoBarDelegateAt(i)->EqualsDelegate(delegate)) {
872 // Tell the new infobar to close so that it can clean itself up.
873 delegate->InfoBarClosed();
[email protected]616ed5a2008-11-21 22:27:24874 return;
[email protected]6d27a7b2009-12-18 23:25:45875 }
[email protected]616ed5a2008-11-21 22:27:24876 }
877
878 infobar_delegates_.push_back(delegate);
[email protected]bfd04a62009-02-01 18:16:56879 NotificationService::current()->Notify(
880 NotificationType::TAB_CONTENTS_INFOBAR_ADDED,
881 Source<TabContents>(this),
882 Details<InfoBarDelegate>(delegate));
[email protected]616ed5a2008-11-21 22:27:24883
884 // Add ourselves as an observer for navigations the first time a delegate is
885 // added. We use this notification to expire InfoBars that need to expire on
886 // page transitions.
887 if (infobar_delegates_.size() == 1) {
[email protected]bfd04a62009-02-01 18:16:56888 registrar_.Add(this, NotificationType::NAV_ENTRY_COMMITTED,
[email protected]ce3fa3c2009-04-20 19:55:57889 Source<NavigationController>(&controller_));
[email protected]616ed5a2008-11-21 22:27:24890 }
891}
892
893void TabContents::RemoveInfoBar(InfoBarDelegate* delegate) {
894 std::vector<InfoBarDelegate*>::iterator it =
895 find(infobar_delegates_.begin(), infobar_delegates_.end(), delegate);
896 if (it != infobar_delegates_.end()) {
897 InfoBarDelegate* delegate = *it;
[email protected]bfd04a62009-02-01 18:16:56898 NotificationService::current()->Notify(
899 NotificationType::TAB_CONTENTS_INFOBAR_REMOVED,
900 Source<TabContents>(this),
901 Details<InfoBarDelegate>(delegate));
[email protected]f86a07022008-11-25 01:06:05902 infobar_delegates_.erase(it);
[email protected]616ed5a2008-11-21 22:27:24903
[email protected]6a02963e2009-01-06 16:58:03904 // Remove ourselves as an observer if we are tracking no more InfoBars.
905 if (infobar_delegates_.empty()) {
[email protected]bfd04a62009-02-01 18:16:56906 registrar_.Remove(this, NotificationType::NAV_ENTRY_COMMITTED,
[email protected]ce3fa3c2009-04-20 19:55:57907 Source<NavigationController>(&controller_));
[email protected]6a02963e2009-01-06 16:58:03908 }
[email protected]616ed5a2008-11-21 22:27:24909 }
910}
[email protected]3edd9522009-03-04 22:19:41911
[email protected]f81821c2009-08-07 01:29:47912void TabContents::ReplaceInfoBar(InfoBarDelegate* old_delegate,
913 InfoBarDelegate* new_delegate) {
914 std::vector<InfoBarDelegate*>::iterator it =
915 find(infobar_delegates_.begin(), infobar_delegates_.end(), old_delegate);
916 DCHECK(it != infobar_delegates_.end());
917
918 // Notify the container about the change of plans.
919 scoped_ptr<std::pair<InfoBarDelegate*, InfoBarDelegate*> > details(
920 new std::pair<InfoBarDelegate*, InfoBarDelegate*>(
921 old_delegate, new_delegate));
922 NotificationService::current()->Notify(
923 NotificationType::TAB_CONTENTS_INFOBAR_REPLACED,
924 Source<TabContents>(this),
925 Details<std::pair<InfoBarDelegate*, InfoBarDelegate*> >(details.get()));
926
927 // Remove the old one.
928 infobar_delegates_.erase(it);
929
930 // Add the new one.
931 infobar_delegates_.push_back(new_delegate);
932}
933
[email protected]20ede072009-10-06 00:28:35934bool TabContents::ShouldShowBookmarkBar() {
[email protected]aa9196f2009-10-22 16:20:27935 if (showing_interstitial_page())
936 return false;
937
[email protected]96d185d2009-04-24 03:28:54938 // See GetDOMUIForCurrentState() comment for more info. This case is very
939 // similar, but for non-first loads, we want to use the committed entry. This
940 // is so the bookmarks bar disappears at the same time the page does.
941 if (controller_.GetLastCommittedEntry()) {
942 // Not the first load, always use the committed DOM UI.
943 if (render_manager_.dom_ui())
944 return render_manager_.dom_ui()->force_bookmark_bar_visible();
945 return false; // Default.
946 }
947
948 // When it's the first load, we know either the pending one or the committed
949 // one will have the DOM UI in it (see GetDOMUIForCurrentState), and only one
950 // of them will be valid, so we can just check both.
951 if (render_manager_.pending_dom_ui())
952 return render_manager_.pending_dom_ui()->force_bookmark_bar_visible();
953 if (render_manager_.dom_ui())
954 return render_manager_.dom_ui()->force_bookmark_bar_visible();
955 return false; // Default.
956}
957
[email protected]d8f16ae2009-08-30 03:47:26958bool TabContents::IsExtensionShelfAlwaysVisible() {
959 // See GetDOMUIForCurrentState() comment for more info. This case is very
960 // similar, but for non-first loads, we want to use the committed entry. This
961 // is so the bookmarks bar disappears at the same time the page does.
962 if (controller_.GetLastCommittedEntry()) {
963 // Not the first load, always use the committed DOM UI.
964 if (render_manager_.dom_ui())
965 return render_manager_.dom_ui()->force_extension_shelf_visible();
966 return false; // Default.
967 }
968
969 // When it's the first load, we know either the pending one or the committed
970 // one will have the DOM UI in it (see GetDOMUIForCurrentState), and only one
971 // of them will be valid, so we can just check both.
972 if (render_manager_.pending_dom_ui())
973 return render_manager_.pending_dom_ui()->force_extension_shelf_visible();
974 if (render_manager_.dom_ui())
975 return render_manager_.dom_ui()->force_extension_shelf_visible();
976 return false; // Default.
977}
978
[email protected]3edd9522009-03-04 22:19:41979void TabContents::ToolbarSizeChanged(bool is_animating) {
980 TabContentsDelegate* d = delegate();
981 if (d)
982 d->ToolbarSizeChanged(this, is_animating);
983}
[email protected]616ed5a2008-11-21 22:27:24984
[email protected]fc6fb7fb2009-11-07 02:35:04985bool TabContents::CanDownload(int request_id) {
986 TabContentsDelegate* d = delegate();
987 if (d)
988 return d->CanDownload(request_id);
989 return true;
990}
991
[email protected]d5f942ba2008-09-26 19:30:34992void TabContents::OnStartDownload(DownloadItem* download) {
993 DCHECK(download);
[email protected]d5f942ba2008-09-26 19:30:34994
995 // Download in a constrained popup is shown in the tab that opened it.
[email protected]b6c874582009-05-08 19:38:31996 TabContents* tab_contents = delegate()->GetConstrainingContents(this);
[email protected]d5f942ba2008-09-26 19:30:34997
[email protected]59560e0b2009-06-04 03:30:22998 if (tab_contents && tab_contents->delegate())
999 tab_contents->delegate()->OnStartDownload(download);
[email protected]3edd9522009-03-04 22:19:411000}
[email protected]3edd9522009-03-04 22:19:411001
[email protected]d5f942ba2008-09-26 19:30:341002void TabContents::WillClose(ConstrainedWindow* window) {
1003 ConstrainedWindowList::iterator it =
1004 find(child_windows_.begin(), child_windows_.end(), window);
1005 if (it != child_windows_.end())
1006 child_windows_.erase(it);
[email protected]66ba4932009-06-04 19:22:131007}
[email protected]d5f942ba2008-09-26 19:30:341008
[email protected]66ba4932009-06-04 19:22:131009void TabContents::WillCloseBlockedPopupContainer(
1010 BlockedPopupContainer* container) {
1011 DCHECK(blocked_popups_ == container);
1012 blocked_popups_ = NULL;
[email protected]d5f942ba2008-09-26 19:30:341013}
1014
[email protected]d5f942ba2008-09-26 19:30:341015void TabContents::DidMoveOrResize(ConstrainedWindow* window) {
[email protected]332af7732009-03-11 13:21:351016#if defined(OS_WIN)
[email protected]92edc472009-02-10 20:32:061017 UpdateWindow(GetNativeView());
[email protected]332af7732009-03-11 13:21:351018#endif
[email protected]d5f942ba2008-09-26 19:30:341019}
1020
[email protected]c1dc9f132009-11-20 23:13:211021void TabContents::StartFinding(string16 search_string,
[email protected]e491f1c2009-05-22 20:28:121022 bool forward_direction,
1023 bool case_sensitive) {
[email protected]c1dc9f132009-11-20 23:13:211024 // If search_string is empty, it means FindNext was pressed with a keyboard
[email protected]6e95706d2009-04-23 22:55:011025 // shortcut so unless we have something to search for we return early.
[email protected]c1dc9f132009-11-20 23:13:211026 if (search_string.empty() && find_text_.empty()) {
[email protected]87fae512009-12-11 02:20:211027 // Try the last thing we searched for on this tab, then the last thing
1028 // searched for on any tab.
1029 if (!previous_find_text_.empty())
1030 search_string = previous_find_text_;
1031 else if (!last_search_prepopulate_text_->empty())
1032 search_string = *last_search_prepopulate_text_;
1033 else
[email protected]e491f1c2009-05-22 20:28:121034 return;
[email protected]e491f1c2009-05-22 20:28:121035 }
[email protected]6e95706d2009-04-23 22:55:011036
[email protected]101c90e2009-10-26 21:14:441037 // Keep track of the previous search.
1038 previous_find_text_ = find_text_;
1039
[email protected]6e95706d2009-04-23 22:55:011040 // This is a FindNext operation if we are searching for the same text again,
1041 // or if the passed in search text is empty (FindNext keyboard shortcut). The
1042 // exception to this is if the Find was aborted (then we don't want FindNext
1043 // because the highlighting has been cleared and we need it to reappear). We
1044 // therefore treat FindNext after an aborted Find operation as a full fledged
1045 // Find.
[email protected]c1dc9f132009-11-20 23:13:211046 bool find_next = (find_text_ == search_string || search_string.empty()) &&
[email protected]e491f1c2009-05-22 20:28:121047 (last_search_case_sensitive_ == case_sensitive) &&
[email protected]6e95706d2009-04-23 22:55:011048 !find_op_aborted_;
1049 if (!find_next)
1050 current_find_request_id_ = find_request_id_counter_++;
1051
[email protected]c1dc9f132009-11-20 23:13:211052 if (!search_string.empty())
1053 find_text_ = search_string;
[email protected]e491f1c2009-05-22 20:28:121054 last_search_case_sensitive_ = case_sensitive;
[email protected]6e95706d2009-04-23 22:55:011055
1056 find_op_aborted_ = false;
1057
1058 // Keep track of what the last search was across the tabs.
[email protected]c1dc9f132009-11-20 23:13:211059 *last_search_prepopulate_text_ = find_text_;
[email protected]6e95706d2009-04-23 22:55:011060 render_view_host()->StartFinding(current_find_request_id_,
1061 find_text_,
1062 forward_direction,
[email protected]e491f1c2009-05-22 20:28:121063 case_sensitive,
[email protected]6e95706d2009-04-23 22:55:011064 find_next);
1065}
1066
1067void TabContents::StopFinding(bool clear_selection) {
[email protected]2d2dffc2009-06-09 21:41:001068 // When |clear_selection| is true, it means the find string has been cleared
1069 // by the user, but the UI has not been dismissed.
1070 if (!clear_selection)
1071 find_ui_active_ = false;
[email protected]87fae512009-12-11 02:20:211072 previous_find_text_ = find_text_;
[email protected]101c90e2009-10-26 21:14:441073 find_text_.clear();
[email protected]6e95706d2009-04-23 22:55:011074 find_op_aborted_ = true;
[email protected]e491f1c2009-05-22 20:28:121075 last_search_result_ = FindNotificationDetails();
[email protected]6e95706d2009-04-23 22:55:011076 render_view_host()->StopFinding(clear_selection);
1077}
1078
[email protected]cf3f3872010-01-15 17:38:191079void TabContents::GetPageLanguage() {
1080 render_view_host()->GetPageLanguage();
1081}
1082
[email protected]420ae012009-04-24 05:16:321083void TabContents::OnSavePage() {
1084 // If we can not save the page, try to download it.
1085 if (!SavePackage::IsSavableContents(contents_mime_type())) {
1086 DownloadManager* dlm = profile()->GetDownloadManager();
1087 const GURL& current_page_url = GetURL();
1088 if (dlm && current_page_url.is_valid())
[email protected]d82ed61e2009-06-16 02:46:221089 dlm->DownloadUrl(current_page_url, GURL(), "", this);
[email protected]420ae012009-04-24 05:16:321090 return;
1091 }
1092
1093 Stop();
1094
1095 // Create the save package and possibly prompt the user for the name to save
1096 // the page as. The user prompt is an asynchronous operation that runs on
1097 // another thread.
[email protected]d82ed61e2009-06-16 02:46:221098 save_package_ = new SavePackage(this);
[email protected]420ae012009-04-24 05:16:321099 save_package_->GetSaveInfo();
1100}
1101
1102// Used in automated testing to bypass prompting the user for file names.
1103// Instead, the names and paths are hard coded rather than running them through
1104// file name sanitation and extension / mime checking.
[email protected]828cabe2009-09-26 22:47:111105bool TabContents::SavePage(const FilePath& main_file, const FilePath& dir_path,
[email protected]420ae012009-04-24 05:16:321106 SavePackage::SavePackageType save_type) {
1107 // Stop the page from navigating.
1108 Stop();
1109
[email protected]828cabe2009-09-26 22:47:111110 save_package_ = new SavePackage(this, save_type, main_file, dir_path);
[email protected]cdf6cc82009-08-04 21:48:591111 return save_package_->Init();
[email protected]420ae012009-04-24 05:16:321112}
1113
[email protected]89d6e6e2009-12-03 23:06:511114void TabContents::EmailPageLocation() {
1115 std::string title = EscapeQueryParamValue(UTF16ToUTF8(GetTitle()), false);
1116 std::string page_url = EscapeQueryParamValue(GetURL().spec(), false);
1117 std::string mailto = std::string("mailto:?subject=Fwd:%20") +
1118 title + "&body=%0A%0A" + page_url;
1119 platform_util::OpenExternal(GURL(mailto));
1120}
1121
[email protected]420ae012009-04-24 05:16:321122void TabContents::PrintPreview() {
1123 // We don't show the print preview yet, only the print dialog.
1124 PrintNow();
1125}
1126
1127bool TabContents::PrintNow() {
1128 // We can't print interstitial page for now.
1129 if (showing_interstitial_page())
1130 return false;
1131
1132 return render_view_host()->PrintPages();
1133}
1134
[email protected]82270452009-06-19 15:58:011135void TabContents::PrintingDone(int document_cookie, bool success) {
1136 render_view_host()->PrintingDone(document_cookie, success);
1137}
1138
[email protected]420ae012009-04-24 05:16:321139bool TabContents::IsActiveEntry(int32 page_id) {
1140 NavigationEntry* active_entry = controller_.GetActiveEntry();
1141 return (active_entry != NULL &&
1142 active_entry->site_instance() == GetSiteInstance() &&
1143 active_entry->page_id() == page_id);
1144}
1145
[email protected]3f8eb7f2009-10-02 23:58:051146void TabContents::LogNewTabTime(const std::string& event_name) {
1147 // Not all new tab pages get timed. In those cases, we don't have a
1148 // new_tab_start_time_.
1149 if (new_tab_start_time_.is_null())
1150 return;
1151
1152 base::TimeDelta duration = base::TimeTicks::Now() - new_tab_start_time_;
[email protected]84abba62009-10-07 17:01:441153 MetricEventDurationDetails details(event_name,
1154 static_cast<int>(duration.InMilliseconds()));
1155
[email protected]7ab1e7d62009-10-14 23:32:011156 if (event_name == "Tab.NewTabScriptStart") {
1157 UMA_HISTOGRAM_TIMES("Tab.NewTabScriptStart", duration);
1158 } else if (event_name == "Tab.NewTabDOMContentLoaded") {
1159 UMA_HISTOGRAM_TIMES("Tab.NewTabDOMContentLoaded", duration);
1160 } else if (event_name == "Tab.NewTabOnload") {
1161 UMA_HISTOGRAM_TIMES("Tab.NewTabOnload", duration);
[email protected]3f8eb7f2009-10-02 23:58:051162 // The new tab page has finished loading; reset it.
1163 new_tab_start_time_ = base::TimeTicks();
[email protected]84abba62009-10-07 17:01:441164 } else {
1165 NOTREACHED();
[email protected]3f8eb7f2009-10-02 23:58:051166 }
[email protected]84abba62009-10-07 17:01:441167 NotificationService::current()->Notify(
1168 NotificationType::METRIC_EVENT_DURATION,
1169 Source<TabContents>(this),
1170 Details<MetricEventDurationDetails>(&details));
[email protected]3f8eb7f2009-10-02 23:58:051171}
1172
[email protected]7ab1e7d62009-10-14 23:32:011173void TabContents::OnCloseStarted() {
1174 if (tab_close_start_time_.is_null())
1175 tab_close_start_time_ = base::TimeTicks::Now();
1176}
1177
[email protected]96d185d2009-04-24 03:28:541178// Notifies the RenderWidgetHost instance about the fact that the page is
1179// loading, or done loading and calls the base implementation.
1180void TabContents::SetIsLoading(bool is_loading,
1181 LoadNotificationDetails* details) {
1182 if (is_loading == is_loading_)
1183 return;
1184
1185 if (!is_loading) {
1186 load_state_ = net::LOAD_STATE_IDLE;
1187 load_state_host_.clear();
[email protected]094e5b22009-09-25 04:23:561188 upload_size_ = 0;
1189 upload_position_ = 0;
[email protected]96d185d2009-04-24 03:28:541190 }
1191
1192 render_manager_.SetIsLoading(is_loading);
1193
1194 is_loading_ = is_loading;
1195 waiting_for_response_ = is_loading;
1196
1197 if (delegate_)
1198 delegate_->LoadingStateChanged(this);
[email protected]329581b2009-04-28 06:52:351199 NotifyNavigationStateChanged(INVALIDATE_LOAD);
[email protected]96d185d2009-04-24 03:28:541200
1201 NotificationType type = is_loading ? NotificationType::LOAD_START :
1202 NotificationType::LOAD_STOP;
[email protected]ce5c4502009-05-06 16:46:111203 NotificationDetails det = NotificationService::NoDetails();
[email protected]96d185d2009-04-24 03:28:541204 if (details)
1205 det = Details<LoadNotificationDetails>(details);
1206 NotificationService::current()->Notify(type,
1207 Source<NavigationController>(&controller_),
1208 det);
1209}
1210
[email protected]2c4410d2009-05-06 23:46:221211void TabContents::CreateBlockedPopupContainerIfNecessary() {
1212 if (blocked_popups_)
1213 return;
1214
[email protected]4d4c0b92009-06-08 23:13:011215 blocked_popups_ = BlockedPopupContainer::Create(this, profile());
[email protected]2c4410d2009-05-06 23:46:221216}
1217
[email protected]b6c874582009-05-08 19:38:311218void TabContents::AddPopup(TabContents* new_contents,
1219 const gfx::Rect& initial_pos,
1220 const std::string& host) {
[email protected]2c4410d2009-05-06 23:46:221221 CreateBlockedPopupContainerIfNecessary();
1222 blocked_popups_->AddTabContents(new_contents, initial_pos, host);
[email protected]2c4410d2009-05-06 23:46:221223}
1224
[email protected]e69bce3dd2009-04-20 22:05:121225// TODO(brettw) This should be on the TabContentsView.
[email protected]66ba4932009-06-04 19:22:131226void TabContents::RepositionSupressedPopupsToFit() {
[email protected]d6598c052008-11-05 19:03:251227 if (blocked_popups_)
[email protected]4d4c0b92009-06-08 23:13:011228 blocked_popups_->RepositionBlockedPopupContainer();
[email protected]d5f942ba2008-09-26 19:30:341229}
1230
[email protected]b9681312008-11-07 00:08:261231bool TabContents::ShowingBlockedPopupNotification() const {
1232 return blocked_popups_ != NULL &&
[email protected]2c4410d2009-05-06 23:46:221233 blocked_popups_->GetBlockedPopupCount() != 0;
[email protected]b9681312008-11-07 00:08:261234}
[email protected]616ed5a2008-11-21 22:27:241235
1236namespace {
1237bool TransitionIsReload(PageTransition::Type transition) {
1238 return PageTransition::StripQualifier(transition) == PageTransition::RELOAD;
1239}
1240}
1241
1242void TabContents::ExpireInfoBars(
1243 const NavigationController::LoadCommittedDetails& details) {
1244 // Only hide InfoBars when the user has done something that makes the main
1245 // frame load. We don't want various automatic or subframe navigations making
1246 // it disappear.
1247 if (!details.is_user_initiated_main_frame_load())
1248 return;
1249
[email protected]f86a07022008-11-25 01:06:051250 for (int i = infobar_delegate_count() - 1; i >= 0; --i) {
[email protected]616ed5a2008-11-21 22:27:241251 InfoBarDelegate* delegate = GetInfoBarDelegateAt(i);
[email protected]f86a07022008-11-25 01:06:051252 if (delegate->ShouldExpire(details))
[email protected]616ed5a2008-11-21 22:27:241253 RemoveInfoBar(delegate);
[email protected]616ed5a2008-11-21 22:27:241254 }
1255}
[email protected]fdd61c62009-04-22 19:22:571256
[email protected]96d185d2009-04-24 03:28:541257DOMUI* TabContents::GetDOMUIForCurrentState() {
1258 // When there is a pending navigation entry, we want to use the pending DOMUI
1259 // that goes along with it to control the basic flags. For example, we want to
1260 // show the pending URL in the URL bar, so we want the display_url flag to
1261 // be from the pending entry.
1262 //
1263 // The confusion comes because there are multiple possibilities for the
1264 // initial load in a tab as a side effect of the way the RenderViewHostManager
1265 // works.
1266 //
1267 // - For the very first tab the load looks "normal". The new tab DOM UI is
1268 // the pending one, and we want it to apply here.
1269 //
1270 // - For subsequent new tabs, they'll get a new SiteInstance which will then
1271 // get switched to the one previously associated with the new tab pages.
1272 // This switching will cause the manager to commit the RVH/DOMUI. So we'll
1273 // have a committed DOM UI in this case.
1274 //
1275 // This condition handles all of these cases:
1276 //
1277 // - First load in first tab: no committed nav entry + pending nav entry +
1278 // pending dom ui:
1279 // -> Use pending DOM UI if any.
1280 //
1281 // - First load in second tab: no committed nav entry + pending nav entry +
1282 // no pending DOM UI:
1283 // -> Use the committed DOM UI if any.
1284 //
1285 // - Second navigation in any tab: committed nav entry + pending nav entry:
1286 // -> Use pending DOM UI if any.
1287 //
1288 // - Normal state with no load: committed nav entry + no pending nav entry:
1289 // -> Use committed DOM UI.
1290 if (controller_.pending_entry() &&
1291 (controller_.GetLastCommittedEntry() ||
1292 render_manager_.pending_dom_ui()))
1293 return render_manager_.pending_dom_ui();
1294 return render_manager_.dom_ui();
1295}
[email protected]420ae012009-04-24 05:16:321296
1297void TabContents::DidNavigateMainFramePostCommit(
1298 const NavigationController::LoadCommittedDetails& details,
1299 const ViewHostMsg_FrameNavigate_Params& params) {
[email protected]c2e74fe82009-09-03 17:57:441300 if (opener_dom_ui_type_ != DOMUIFactory::kNoDOMUI) {
1301 // If this is a window.open navigation, use the same DOMUI as the renderer
1302 // that opened the window, as long as both renderers have the same
1303 // privileges.
1304 if (opener_dom_ui_type_ == DOMUIFactory::GetDOMUIType(GetURL())) {
1305 DOMUI* dom_ui = DOMUIFactory::CreateDOMUIForURL(this, GetURL());
1306 // dom_ui might be NULL if the URL refers to a non-existent extension.
1307 if (dom_ui) {
1308 render_manager_.SetDOMUIPostCommit(dom_ui);
1309 dom_ui->RenderViewCreated(render_view_host());
1310 }
1311 }
1312 opener_dom_ui_type_ = DOMUIFactory::kNoDOMUI;
1313 }
1314
[email protected]420ae012009-04-24 05:16:321315 if (details.is_user_initiated_main_frame_load()) {
1316 // Clear the status bubble. This is a workaround for a bug where WebKit
1317 // doesn't let us know that the cursor left an element during a
1318 // transition (this is also why the mouse cursor remains as a hand after
1319 // clicking on a link); see bugs 1184641 and 980803. We don't want to
1320 // clear the bubble when a user navigates to a named anchor in the same
1321 // page.
1322 UpdateTargetURL(details.entry->page_id(), GURL());
1323
1324 // UpdateHelpersForDidNavigate will handle the case where the password_form
1325 // origin is valid.
1326 // TODO(brettw) bug 1343111: Password manager stuff in here needs to be
1327 // cleaned up and covered by tests.
1328 if (!params.password_form.origin.is_valid())
1329 GetPasswordManager()->DidNavigate();
1330 }
1331
1332 // The keyword generator uses the navigation entries, so must be called after
1333 // the commit.
1334 GenerateKeywordIfNecessary(params);
1335
1336 // Allow the new page to set the title again.
1337 received_page_title_ = false;
1338
1339 // Get the favicon, either from history or request it from the net.
1340 fav_icon_helper_.FetchFavIcon(details.entry->url());
1341
[email protected]56ce6e52009-10-27 00:10:521342 // Clear all page and browser action state for this tab, unless this is an
1343 // in-page navigation.
[email protected]92c6f9b92009-10-24 04:35:081344 url_canon::Replacements<char> replacements;
1345 replacements.ClearRef();
1346 if (params.url.ReplaceComponents(replacements) !=
1347 params.referrer.ReplaceComponents(replacements)) {
[email protected]92c6f9b92009-10-24 04:35:081348 ExtensionsService* service = profile()->GetExtensionsService();
1349 if (service) {
1350 for (size_t i = 0; i < service->extensions()->size(); ++i) {
[email protected]5d246db22009-10-27 06:17:571351 ExtensionAction* browser_action =
[email protected]92c6f9b92009-10-24 04:35:081352 service->extensions()->at(i)->browser_action();
[email protected]56ce6e52009-10-27 00:10:521353 if (browser_action) {
1354 browser_action->ClearAllValuesForTab(controller().session_id().id());
1355 NotificationService::current()->Notify(
1356 NotificationType::EXTENSION_BROWSER_ACTION_UPDATED,
[email protected]5d246db22009-10-27 06:17:571357 Source<ExtensionAction>(browser_action),
[email protected]56ce6e52009-10-27 00:10:521358 NotificationService::NoDetails());
1359 }
[email protected]92c6f9b92009-10-24 04:35:081360
[email protected]5d246db22009-10-27 06:17:571361 ExtensionAction* page_action =
[email protected]56ce6e52009-10-27 00:10:521362 service->extensions()->at(i)->page_action();
1363 if (page_action) {
1364 page_action->ClearAllValuesForTab(controller().session_id().id());
1365 PageActionStateChanged();
1366 }
[email protected]92c6f9b92009-10-24 04:35:081367 }
[email protected]aece2c7f2009-08-27 20:43:171368 }
1369 }
[email protected]ce5c4502009-05-06 16:46:111370
[email protected]420ae012009-04-24 05:16:321371 // Close constrained popups if necessary.
1372 MaybeCloseChildWindows(details.previous_url, details.entry->url());
1373
1374 // Update the starred state.
1375 UpdateStarredStateForCurrentURL();
1376}
1377
1378void TabContents::DidNavigateAnyFramePostCommit(
1379 RenderViewHost* render_view_host,
1380 const NavigationController::LoadCommittedDetails& details,
1381 const ViewHostMsg_FrameNavigate_Params& params) {
1382 // If we navigate, start showing messages again. This does nothing to prevent
1383 // a malicious script from spamming messages, since the script could just
1384 // reload the page to stop blocking.
1385 suppress_javascript_messages_ = false;
1386
[email protected]420ae012009-04-24 05:16:321387 // Notify the password manager of the navigation or form submit.
1388 // TODO(brettw) bug 1343111: Password manager stuff in here needs to be
1389 // cleaned up and covered by tests.
1390 if (params.password_form.origin.is_valid())
1391 GetPasswordManager()->ProvisionallySavePassword(params.password_form);
1392}
1393
1394void TabContents::MaybeCloseChildWindows(const GURL& previous_url,
1395 const GURL& current_url) {
1396 if (net::RegistryControlledDomainService::SameDomainOrHost(
1397 previous_url, current_url))
1398 return;
1399
1400 // Clear out any child windows since we are leaving this page entirely.
1401 // We use indices instead of iterators in case CloseWindow does something
1402 // that may invalidate an iterator.
1403 int size = static_cast<int>(child_windows_.size());
1404 for (int i = size - 1; i >= 0; --i) {
1405 ConstrainedWindow* window = child_windows_[i];
1406 if (window)
1407 window->CloseConstrainedWindow();
1408 }
[email protected]e29300cd2009-07-23 21:14:591409
1410 // Close the popup container.
1411 if (blocked_popups_) {
1412 blocked_popups_->Destroy();
1413 blocked_popups_ = NULL;
1414 }
[email protected]420ae012009-04-24 05:16:321415}
1416
1417void TabContents::UpdateStarredStateForCurrentURL() {
1418 BookmarkModel* model = profile()->GetBookmarkModel();
1419 const bool old_state = is_starred_;
1420 is_starred_ = (model && model->IsBookmarked(GetURL()));
1421
1422 if (is_starred_ != old_state && delegate())
1423 delegate()->URLStarredChanged(this, is_starred_);
1424}
1425
1426void TabContents::UpdateAlternateErrorPageURL() {
1427 GURL url = GetAlternateErrorPageURL();
1428 render_view_host()->SetAlternateErrorPageURL(url);
1429}
1430
1431void TabContents::UpdateWebPreferences() {
1432 render_view_host()->UpdateWebPreferences(GetWebkitPrefs());
1433}
1434
1435void TabContents::UpdateMaxPageIDIfNecessary(SiteInstance* site_instance,
1436 RenderViewHost* rvh) {
1437 // If we are creating a RVH for a restored controller, then we might
1438 // have more page IDs than the SiteInstance's current max page ID. We must
1439 // make sure that the max page ID is larger than any restored page ID.
1440 // Note that it is ok for conflicting page IDs to exist in another tab
1441 // (i.e., NavigationController), but if any page ID is larger than the max,
1442 // the back/forward list will get confused.
1443 int max_restored_page_id = controller_.max_restored_page_id();
1444 if (max_restored_page_id > 0) {
1445 int curr_max_page_id = site_instance->max_page_id();
1446 if (max_restored_page_id > curr_max_page_id) {
1447 // Need to update the site instance immediately.
1448 site_instance->UpdateMaxPageID(max_restored_page_id);
1449
1450 // Also tell the renderer to update its internal representation. We
1451 // need to reserve enough IDs to make all restored page IDs less than
1452 // the max.
1453 if (curr_max_page_id < 0)
1454 curr_max_page_id = 0;
1455 rvh->ReservePageIDRange(max_restored_page_id - curr_max_page_id);
1456 }
1457 }
1458}
1459
1460void TabContents::UpdateHistoryForNavigation(
[email protected]ebe89e062009-08-13 23:16:541461 const GURL& virtual_url,
[email protected]befd8d822009-07-01 04:51:471462 const NavigationController::LoadCommittedDetails& details,
[email protected]420ae012009-04-24 05:16:321463 const ViewHostMsg_FrameNavigate_Params& params) {
1464 if (profile()->IsOffTheRecord())
1465 return;
1466
1467 // Add to history service.
1468 HistoryService* hs = profile()->GetHistoryService(Profile::IMPLICIT_ACCESS);
1469 if (hs) {
1470 if (PageTransition::IsMainFrame(params.transition) &&
[email protected]ebe89e062009-08-13 23:16:541471 virtual_url != params.url) {
1472 // Hack on the "virtual" URL so that it will appear in history. For some
[email protected]420ae012009-04-24 05:16:321473 // types of URLs, we will display a magic URL that is different from where
1474 // the page is actually navigated. We want the user to see in history
[email protected]ebe89e062009-08-13 23:16:541475 // what they saw in the URL bar, so we add the virtual URL as a redirect.
1476 // This only applies to the main frame, as the virtual URL doesn't apply
[email protected]420ae012009-04-24 05:16:321477 // to sub-frames.
1478 std::vector<GURL> redirects = params.redirects;
1479 if (!redirects.empty())
[email protected]ebe89e062009-08-13 23:16:541480 redirects.back() = virtual_url;
1481 hs->AddPage(virtual_url, this, params.page_id, params.referrer,
[email protected]befd8d822009-07-01 04:51:471482 params.transition, redirects, details.did_replace_entry);
[email protected]420ae012009-04-24 05:16:321483 } else {
1484 hs->AddPage(params.url, this, params.page_id, params.referrer,
[email protected]befd8d822009-07-01 04:51:471485 params.transition, params.redirects,
1486 details.did_replace_entry);
[email protected]420ae012009-04-24 05:16:321487 }
1488 }
1489}
1490
1491bool TabContents::UpdateTitleForEntry(NavigationEntry* entry,
1492 const std::wstring& title) {
1493 // For file URLs without a title, use the pathname instead. In the case of a
1494 // synthesized title, we don't want the update to count toward the "one set
1495 // per page of the title to history."
1496 std::wstring final_title;
1497 bool explicit_set;
1498 if (entry->url().SchemeIsFile() && title.empty()) {
1499 final_title = UTF8ToWide(entry->url().ExtractFileName());
1500 explicit_set = false; // Don't count synthetic titles toward the set limit.
1501 } else {
1502 TrimWhitespace(title, TRIM_ALL, &final_title);
1503 explicit_set = true;
1504 }
1505
1506 if (final_title == UTF16ToWideHack(entry->title()))
1507 return false; // Nothing changed, don't bother.
1508
1509 entry->set_title(WideToUTF16Hack(final_title));
1510
1511 // Update the history system for this page.
1512 if (!profile()->IsOffTheRecord() && !received_page_title_) {
1513 HistoryService* hs =
1514 profile()->GetHistoryService(Profile::IMPLICIT_ACCESS);
1515 if (hs)
[email protected]ebe89e062009-08-13 23:16:541516 hs->SetPageTitle(entry->virtual_url(), final_title);
[email protected]420ae012009-04-24 05:16:321517
1518 // Don't allow the title to be saved again for explicitly set ones.
1519 received_page_title_ = explicit_set;
1520 }
1521
1522 // Lastly, set the title for the view.
1523 view_->SetPageTitle(final_title);
1524
1525 return true;
1526}
1527
1528void TabContents::NotifySwapped() {
1529 // After sending out a swap notification, we need to send a disconnect
1530 // notification so that clients that pick up a pointer to |this| can NULL the
1531 // pointer. See Bug 1230284.
1532 notify_disconnection_ = true;
1533 NotificationService::current()->Notify(
[email protected]57c6a652009-05-04 07:58:341534 NotificationType::TAB_CONTENTS_SWAPPED,
[email protected]d82ed61e2009-06-16 02:46:221535 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:321536 NotificationService::NoDetails());
1537}
1538
1539void TabContents::NotifyConnected() {
1540 notify_disconnection_ = true;
1541 NotificationService::current()->Notify(
[email protected]57c6a652009-05-04 07:58:341542 NotificationType::TAB_CONTENTS_CONNECTED,
[email protected]d82ed61e2009-06-16 02:46:221543 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:321544 NotificationService::NoDetails());
1545}
1546
1547void TabContents::NotifyDisconnected() {
1548 if (!notify_disconnection_)
1549 return;
1550
1551 notify_disconnection_ = false;
1552 NotificationService::current()->Notify(
[email protected]57c6a652009-05-04 07:58:341553 NotificationType::TAB_CONTENTS_DISCONNECTED,
[email protected]d82ed61e2009-06-16 02:46:221554 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:321555 NotificationService::NoDetails());
1556}
1557
1558void TabContents::GenerateKeywordIfNecessary(
1559 const ViewHostMsg_FrameNavigate_Params& params) {
1560 if (!params.searchable_form_url.is_valid())
1561 return;
1562
1563 if (profile()->IsOffTheRecord())
1564 return;
1565
1566 int last_index = controller_.last_committed_entry_index();
1567 // When there was no previous page, the last index will be 0. This is
1568 // normally due to a form submit that opened in a new tab.
1569 // TODO(brettw) bug 916126: we should support keywords when form submits
1570 // happen in new tabs.
1571 if (last_index <= 0)
1572 return;
1573 const NavigationEntry* previous_entry =
1574 controller_.GetEntryAtIndex(last_index - 1);
1575 if (IsFormSubmit(previous_entry)) {
1576 // Only generate a keyword if the previous page wasn't itself a form
1577 // submit.
1578 return;
1579 }
1580
1581 GURL keyword_url = previous_entry->user_typed_url().is_valid() ?
1582 previous_entry->user_typed_url() : previous_entry->url();
1583 std::wstring keyword =
1584 TemplateURLModel::GenerateKeyword(keyword_url, true); // autodetected
1585 if (keyword.empty())
1586 return;
1587
1588 TemplateURLModel* url_model = profile()->GetTemplateURLModel();
1589 if (!url_model)
1590 return;
1591
1592 if (!url_model->loaded()) {
1593 url_model->Load();
1594 return;
1595 }
1596
1597 const TemplateURL* current_url;
1598 std::wstring url = UTF8ToWide(params.searchable_form_url.spec());
1599 if (!url_model->CanReplaceKeyword(keyword, url, &current_url))
1600 return;
1601
1602 if (current_url) {
1603 if (current_url->originating_url().is_valid()) {
1604 // The existing keyword was generated from an OpenSearch description
1605 // document, don't regenerate.
1606 return;
1607 }
1608 url_model->Remove(current_url);
1609 }
1610 TemplateURL* new_url = new TemplateURL();
1611 new_url->set_keyword(keyword);
1612 new_url->set_short_name(keyword);
1613 new_url->SetURL(url, 0, 0);
1614 new_url->add_input_encoding(params.searchable_form_encoding);
1615 DCHECK(controller_.GetLastCommittedEntry());
1616 const GURL& favicon_url =
1617 controller_.GetLastCommittedEntry()->favicon().url();
1618 if (favicon_url.is_valid()) {
1619 new_url->SetFavIconURL(favicon_url);
1620 } else {
1621 // The favicon url isn't valid. This means there really isn't a favicon,
1622 // or the favicon url wasn't obtained before the load started. This assumes
1623 // the later.
1624 // TODO(sky): Need a way to set the favicon that doesn't involve generating
1625 // its url.
1626 new_url->SetFavIconURL(TemplateURL::GenerateFaviconURL(params.referrer));
1627 }
1628 new_url->set_safe_for_autoreplace(true);
1629 url_model->Add(new_url);
1630}
1631
[email protected]bf5c2ff392009-07-08 16:24:331632void TabContents::OnUserGesture() {
1633 // See comment in RenderViewHostDelegate::OnUserGesture as to why we do this.
1634 DownloadRequestManager* drm = g_browser_process->download_request_manager();
1635 if (drm)
1636 drm->OnUserGesture(this);
[email protected]e7eaedde2009-09-25 20:09:491637 ExternalProtocolHandler::OnUserGesture();
[email protected]bf5c2ff392009-07-08 16:24:331638 controller_.OnUserGesture();
1639}
1640
1641void TabContents::OnFindReply(int request_id,
1642 int number_of_matches,
1643 const gfx::Rect& selection_rect,
1644 int active_match_ordinal,
1645 bool final_update) {
[email protected]3ce7ced2009-07-24 18:52:421646 // Ignore responses for requests that have been aborted.
1647 if (find_op_aborted_)
1648 return;
1649
[email protected]bf5c2ff392009-07-08 16:24:331650 // Ignore responses for requests other than the one we have most recently
1651 // issued. That way we won't act on stale results when the user has
1652 // already typed in another query.
1653 if (request_id != current_find_request_id_)
1654 return;
1655
1656 if (number_of_matches == -1)
1657 number_of_matches = last_search_result_.number_of_matches();
1658 if (active_match_ordinal == -1)
1659 active_match_ordinal = last_search_result_.active_match_ordinal();
1660
1661 gfx::Rect selection = selection_rect;
1662 if (selection.IsEmpty())
1663 selection = last_search_result_.selection_rect();
1664
1665 // Notify the UI, automation and any other observers that a find result was
1666 // found.
1667 last_search_result_ = FindNotificationDetails(request_id, number_of_matches,
1668 selection, active_match_ordinal,
1669 final_update);
1670 NotificationService::current()->Notify(
1671 NotificationType::FIND_RESULT_AVAILABLE,
1672 Source<TabContents>(this),
1673 Details<FindNotificationDetails>(&last_search_result_));
1674}
1675
1676void TabContents::GoToEntryAtOffset(int offset) {
[email protected]f9cc4c452009-10-13 14:56:381677 if (!delegate_ || delegate_->OnGoToEntryOffset(offset))
1678 controller_.GoToOffset(offset);
[email protected]bf5c2ff392009-07-08 16:24:331679}
1680
1681void TabContents::GetHistoryListCount(int* back_list_count,
1682 int* forward_list_count) {
1683 int current_index = controller_.last_committed_entry_index();
1684 *back_list_count = current_index;
1685 *forward_list_count = controller_.entry_count() - current_index - 1;
1686}
1687
1688void TabContents::OnMissingPluginStatus(int status) {
1689#if defined(OS_WIN)
1690// TODO(PORT): pull in when plug-ins work
1691 GetPluginInstaller()->OnMissingPluginStatus(status);
1692#endif
1693}
1694
1695void TabContents::OnCrashedPlugin(const FilePath& plugin_path) {
[email protected]bf5c2ff392009-07-08 16:24:331696 DCHECK(!plugin_path.value().empty());
1697
1698 std::wstring plugin_name = plugin_path.ToWStringHack();
[email protected]d32d46182009-07-23 00:09:351699#if defined(OS_WIN) || defined(OS_MACOSX)
[email protected]bf5c2ff392009-07-08 16:24:331700 scoped_ptr<FileVersionInfo> version_info(
1701 FileVersionInfo::CreateFileVersionInfo(plugin_path));
1702 if (version_info.get()) {
1703 const std::wstring& product_name = version_info->product_name();
[email protected]996fd702009-09-04 19:12:371704 if (!product_name.empty()) {
[email protected]bf5c2ff392009-07-08 16:24:331705 plugin_name = product_name;
[email protected]996fd702009-09-04 19:12:371706#if defined(OS_MACOSX)
1707 // Many plugins on the Mac have .plugin in the actual name, which looks
1708 // terrible, so look for that and strip it off if present.
1709 const std::wstring plugin_extension(L".plugin");
1710 if (EndsWith(plugin_name, plugin_extension, true))
1711 plugin_name.erase(plugin_name.length() - plugin_extension.length());
1712#endif // OS_MACOSX
1713 }
[email protected]bf5c2ff392009-07-08 16:24:331714 }
[email protected]4a560ed2009-07-13 23:35:261715#else
1716 NOTIMPLEMENTED() << " convert plugin path to plugin name";
1717#endif
[email protected]bf5c2ff392009-07-08 16:24:331718 AddInfoBar(new SimpleAlertInfoBarDelegate(
1719 this, l10n_util::GetStringF(IDS_PLUGIN_CRASHED_PROMPT, plugin_name),
1720 NULL));
[email protected]bf5c2ff392009-07-08 16:24:331721}
1722
1723void TabContents::OnCrashedWorker() {
1724 AddInfoBar(new SimpleAlertInfoBarDelegate(
1725 this, l10n_util::GetString(IDS_WEBWORKER_CRASHED_PROMPT),
1726 NULL));
1727}
1728
1729void TabContents::OnDidGetApplicationInfo(
1730 int32 page_id,
1731 const webkit_glue::WebApplicationInfo& info) {
[email protected]ed5431872009-11-17 08:39:511732 web_app_info_ = info;
[email protected]bf5c2ff392009-07-08 16:24:331733
[email protected]ed5431872009-11-17 08:39:511734 if (delegate())
1735 delegate()->OnDidGetApplicationInfo(this, page_id);
[email protected]bf5c2ff392009-07-08 16:24:331736}
1737
[email protected]bf5c2ff392009-07-08 16:24:331738void TabContents::DidStartProvisionalLoadForFrame(
1739 RenderViewHost* render_view_host,
1740 bool is_main_frame,
1741 const GURL& url) {
1742 ProvisionalLoadDetails details(is_main_frame,
1743 controller_.IsURLInPageNavigation(url),
1744 url, std::string(), false);
1745 NotificationService::current()->Notify(
1746 NotificationType::FRAME_PROVISIONAL_LOAD_START,
1747 Source<NavigationController>(&controller_),
1748 Details<ProvisionalLoadDetails>(&details));
1749}
1750
1751void TabContents::DidStartReceivingResourceResponse(
1752 ResourceRequestDetails* details) {
1753 NotificationService::current()->Notify(
1754 NotificationType::RESOURCE_RESPONSE_STARTED,
1755 Source<NavigationController>(&controller()),
1756 Details<ResourceRequestDetails>(details));
1757}
1758
1759void TabContents::DidRedirectResource(ResourceRequestDetails* details) {
1760 NotificationService::current()->Notify(
1761 NotificationType::RESOURCE_RECEIVED_REDIRECT,
1762 Source<NavigationController>(&controller()),
1763 Details<ResourceRequestDetails>(details));
1764}
1765
1766void TabContents::DidLoadResourceFromMemoryCache(
1767 const GURL& url,
1768 const std::string& frame_origin,
1769 const std::string& main_frame_origin,
1770 const std::string& security_info) {
1771 // Send out a notification that we loaded a resource from our memory cache.
1772 int cert_id = 0, cert_status = 0, security_bits = 0;
1773 SSLManager::DeserializeSecurityInfo(security_info,
1774 &cert_id, &cert_status,
1775 &security_bits);
1776 LoadFromMemoryCacheDetails details(url, frame_origin, main_frame_origin,
[email protected]76543b92009-08-31 17:27:451777 process()->id(), cert_id, cert_status);
[email protected]bf5c2ff392009-07-08 16:24:331778
1779 NotificationService::current()->Notify(
1780 NotificationType::LOAD_FROM_MEMORY_CACHE,
1781 Source<NavigationController>(&controller_),
1782 Details<LoadFromMemoryCacheDetails>(&details));
1783}
1784
[email protected]e3d60e5d2009-09-25 21:08:291785void TabContents::DidDisplayInsecureContent() {
[email protected]09843b5f2009-09-26 08:30:591786 controller_.ssl_manager()->DidDisplayInsecureContent();
[email protected]e3d60e5d2009-09-25 21:08:291787}
1788
1789void TabContents::DidRunInsecureContent(const std::string& security_origin) {
[email protected]09843b5f2009-09-26 08:30:591790 controller_.ssl_manager()->DidRunInsecureContent(security_origin);
[email protected]e3d60e5d2009-09-25 21:08:291791}
1792
[email protected]bf5c2ff392009-07-08 16:24:331793void TabContents::DidFailProvisionalLoadWithError(
1794 RenderViewHost* render_view_host,
1795 bool is_main_frame,
1796 int error_code,
1797 const GURL& url,
1798 bool showing_repost_interstitial) {
1799 if (net::ERR_ABORTED == error_code) {
1800 // EVIL HACK ALERT! Ignore failed loads when we're showing interstitials.
1801 // This means that the interstitial won't be torn down properly, which is
1802 // bad. But if we have an interstitial, go back to another tab type, and
1803 // then load the same interstitial again, we could end up getting the first
1804 // interstitial's "failed" message (as a result of the cancel) when we're on
1805 // the second one.
1806 //
1807 // We can't tell this apart, so we think we're tearing down the current page
1808 // which will cause a crash later one. There is also some code in
1809 // RenderViewHostManager::RendererAbortedProvisionalLoad that is commented
1810 // out because of this problem.
1811 //
1812 // http://code.google.com/p/chromium/issues/detail?id=2855
1813 // Because this will not tear down the interstitial properly, if "back" is
1814 // back to another tab type, the interstitial will still be somewhat alive
1815 // in the previous tab type. If you navigate somewhere that activates the
1816 // tab with the interstitial again, you'll see a flash before the new load
1817 // commits of the interstitial page.
1818 if (showing_interstitial_page()) {
1819 LOG(WARNING) << "Discarding message during interstitial.";
1820 return;
1821 }
1822
1823 // This will discard our pending entry if we cancelled the load (e.g., if we
1824 // decided to download the file instead of load it). Only discard the
1825 // pending entry if the URLs match, otherwise the user initiated a navigate
1826 // before the page loaded so that the discard would discard the wrong entry.
1827 NavigationEntry* pending_entry = controller_.pending_entry();
1828 if (pending_entry && pending_entry->url() == url) {
1829 controller_.DiscardNonCommittedEntries();
1830 // Update the URL display.
1831 NotifyNavigationStateChanged(TabContents::INVALIDATE_URL);
1832 }
1833
1834 render_manager_.RendererAbortedProvisionalLoad(render_view_host);
1835 }
1836
1837 // Send out a notification that we failed a provisional load with an error.
1838 ProvisionalLoadDetails details(is_main_frame,
1839 controller_.IsURLInPageNavigation(url),
1840 url, std::string(), false);
1841 details.set_error_code(error_code);
1842
1843 NotificationService::current()->Notify(
1844 NotificationType::FAIL_PROVISIONAL_LOAD_WITH_ERROR,
1845 Source<NavigationController>(&controller_),
1846 Details<ProvisionalLoadDetails>(&details));
1847}
1848
1849void TabContents::DocumentLoadedInFrame() {
1850 controller_.DocumentLoadedInFrame();
1851}
1852
[email protected]8d3347f2009-07-09 22:00:211853RenderViewHostDelegate::View* TabContents::GetViewDelegate() {
[email protected]420ae012009-04-24 05:16:321854 return view_.get();
1855}
1856
[email protected]8d3347f2009-07-09 22:00:211857RenderViewHostDelegate::RendererManagement*
1858TabContents::GetRendererManagementDelegate() {
1859 return &render_manager_;
1860}
1861
[email protected]bf5c2ff392009-07-08 16:24:331862RenderViewHostDelegate::BrowserIntegration*
[email protected]8d3347f2009-07-09 22:00:211863 TabContents::GetBrowserIntegrationDelegate() {
1864 return this;
[email protected]bf5c2ff392009-07-08 16:24:331865}
1866
[email protected]8d3347f2009-07-09 22:00:211867RenderViewHostDelegate::Resource* TabContents::GetResourceDelegate() {
1868 return this;
[email protected]bf5c2ff392009-07-08 16:24:331869}
1870
[email protected]8d3347f2009-07-09 22:00:211871RenderViewHostDelegate::Save* TabContents::GetSaveDelegate() {
[email protected]420ae012009-04-24 05:16:321872 return save_package_.get(); // May be NULL, but we can return NULL.
1873}
1874
[email protected]8d3347f2009-07-09 22:00:211875RenderViewHostDelegate::Printing* TabContents::GetPrintingDelegate() {
1876 return &printing_;
1877}
1878
1879RenderViewHostDelegate::FavIcon* TabContents::GetFavIconDelegate() {
1880 return &fav_icon_helper_;
1881}
1882
[email protected]c54d269e2009-10-21 18:07:381883RenderViewHostDelegate::FormFieldHistory*
1884TabContents::GetFormFieldHistoryDelegate() {
[email protected]19289d02009-10-21 21:47:001885 if (form_field_history_manager_.get() == NULL)
1886 form_field_history_manager_.reset(new FormFieldHistoryManager(this));
1887 return form_field_history_manager_.get();
[email protected]bf5c2ff392009-07-08 16:24:331888}
1889
[email protected]21674df2009-10-28 19:51:211890RenderViewHostDelegate::AutoFill* TabContents::GetAutoFillDelegate() {
1891 if (autofill_manager_.get() == NULL)
1892 autofill_manager_.reset(new AutoFillManager(this));
1893 return autofill_manager_.get();
1894}
1895
[email protected]93623c5d2009-12-10 21:40:321896RendererPreferences TabContents::GetRendererPrefs(Profile* profile) const {
[email protected]840b1512009-07-21 16:53:461897 return renderer_preferences_;
[email protected]80d96fa2009-06-10 22:34:511898}
1899
[email protected]57c6a652009-05-04 07:58:341900TabContents* TabContents::GetAsTabContents() {
1901 return this;
1902}
1903
[email protected]3e7cf1362009-08-21 17:47:081904void TabContents::AddBlockedNotice(const GURL& url, const string16& reason) {
1905 CreateBlockedPopupContainerIfNecessary();
1906 blocked_popups_->AddBlockedNotice(url, reason);
1907}
1908
[email protected]7b291f92009-08-14 05:43:531909ViewType::Type TabContents::GetRenderViewType() const {
1910 return ViewType::TAB_CONTENTS;
1911}
1912
1913int TabContents::GetBrowserWindowID() const {
1914 return controller().window_id().id();
1915}
1916
[email protected]420ae012009-04-24 05:16:321917void TabContents::RenderViewCreated(RenderViewHost* render_view_host) {
[email protected]2e4633c2009-07-09 16:58:061918 NotificationService::current()->Notify(
1919 NotificationType::RENDER_VIEW_HOST_CREATED_FOR_TAB,
1920 Source<TabContents>(this),
1921 Details<RenderViewHost>(render_view_host));
[email protected]420ae012009-04-24 05:16:321922 NavigationEntry* entry = controller_.GetActiveEntry();
1923 if (!entry)
1924 return;
1925
1926 // When we're creating views, we're still doing initial setup, so we always
1927 // use the pending DOM UI rather than any possibly existing committed one.
1928 if (render_manager_.pending_dom_ui()) {
1929 render_manager_.pending_dom_ui()->RenderViewCreated(render_view_host);
1930 }
1931
1932 if (entry->IsViewSourceMode()) {
1933 // Put the renderer in view source mode.
1934 render_view_host->Send(
1935 new ViewMsg_EnableViewSourceMode(render_view_host->routing_id()));
1936 }
[email protected]0666aef2009-05-13 19:48:081937
1938 view()->RenderViewCreated(render_view_host);
[email protected]420ae012009-04-24 05:16:321939}
1940
1941void TabContents::RenderViewReady(RenderViewHost* rvh) {
1942 if (rvh != render_view_host()) {
1943 // Don't notify the world, since this came from a renderer in the
1944 // background.
1945 return;
1946 }
1947
1948 NotifyConnected();
[email protected]9d8a4642009-07-29 17:25:301949 bool was_crashed = is_crashed();
[email protected]420ae012009-04-24 05:16:321950 SetIsCrashed(false);
[email protected]9d8a4642009-07-29 17:25:301951
1952 // Restore the focus to the tab (otherwise the focus will be on the top
1953 // window).
1954 if (was_crashed && !FocusLocationBarByDefault())
1955 Focus();
[email protected]420ae012009-04-24 05:16:321956}
1957
1958void TabContents::RenderViewGone(RenderViewHost* rvh) {
1959 // Ask the print preview if this renderer was valuable.
1960 if (!printing_.OnRenderViewGone(rvh))
1961 return;
1962 if (rvh != render_view_host()) {
1963 // The pending page's RenderViewHost is gone.
1964 return;
1965 }
1966
1967 SetIsLoading(false, NULL);
1968 NotifyDisconnected();
1969 SetIsCrashed(true);
1970
[email protected]8e537302009-06-10 21:19:581971 // Tell the view that we've crashed so it can prepare the sad tab page.
1972 view_->OnTabCrashed();
[email protected]420ae012009-04-24 05:16:321973
1974 // Hide any visible hung renderer warning for this web contents' process.
[email protected]e7b8d832009-11-20 22:41:001975 hung_renderer_dialog::HideForTabContents(this);
[email protected]420ae012009-04-24 05:16:321976}
1977
[email protected]2e4633c2009-07-09 16:58:061978void TabContents::RenderViewDeleted(RenderViewHost* rvh) {
1979 NotificationService::current()->Notify(
1980 NotificationType::RENDER_VIEW_HOST_DELETED,
1981 Source<TabContents>(this),
1982 Details<RenderViewHost>(rvh));
[email protected]fb7b79f72009-11-06 18:00:481983 render_manager_.RenderViewDeleted(rvh);
[email protected]2e4633c2009-07-09 16:58:061984}
1985
[email protected]420ae012009-04-24 05:16:321986void TabContents::DidNavigate(RenderViewHost* rvh,
1987 const ViewHostMsg_FrameNavigate_Params& params) {
[email protected]8030f012009-09-25 18:09:371988 int extra_invalidate_flags = 0;
1989
1990 if (PageTransition::IsMainFrame(params.transition)) {
[email protected]20ede072009-10-06 00:28:351991 bool was_bookmark_bar_visible = ShouldShowBookmarkBar();
[email protected]8030f012009-09-25 18:09:371992 bool was_extension_shelf_visible = IsExtensionShelfAlwaysVisible();
1993
[email protected]420ae012009-04-24 05:16:321994 render_manager_.DidNavigateMainFrame(rvh);
1995
[email protected]20ede072009-10-06 00:28:351996 if (was_bookmark_bar_visible != ShouldShowBookmarkBar())
[email protected]8030f012009-09-25 18:09:371997 extra_invalidate_flags |= INVALIDATE_BOOKMARK_BAR;
1998 if (was_extension_shelf_visible != IsExtensionShelfAlwaysVisible())
1999 extra_invalidate_flags |= INVALIDATE_EXTENSION_SHELF;
2000 }
2001
[email protected]420ae012009-04-24 05:16:322002 // Update the site of the SiteInstance if it doesn't have one yet.
2003 if (!GetSiteInstance()->has_site())
2004 GetSiteInstance()->SetSite(params.url);
2005
2006 // Need to update MIME type here because it's referred to in
2007 // UpdateNavigationCommands() called by RendererDidNavigate() to
2008 // determine whether or not to enable the encoding menu.
2009 // It's updated only for the main frame. For a subframe,
2010 // RenderView::UpdateURL does not set params.contents_mime_type.
2011 // (see http://code.google.com/p/chromium/issues/detail?id=2929 )
2012 // TODO(jungshik): Add a test for the encoding menu to avoid
2013 // regressing it again.
2014 if (PageTransition::IsMainFrame(params.transition))
2015 contents_mime_type_ = params.contents_mime_type;
2016
2017 NavigationController::LoadCommittedDetails details;
[email protected]8030f012009-09-25 18:09:372018 bool did_navigate = controller_.RendererDidNavigate(
2019 params, extra_invalidate_flags, &details);
[email protected]3e90d4a2009-07-03 17:38:392020
2021 // Update history. Note that this needs to happen after the entry is complete,
2022 // which WillNavigate[Main,Sub]Frame will do before this function is called.
[email protected]2e29d922009-10-22 22:13:352023 if (params.should_update_history &&
2024 (!delegate() || delegate()->ShouldAddNavigationsToHistory())) {
[email protected]3e90d4a2009-07-03 17:38:392025 // Most of the time, the displayURL matches the loaded URL, but for about:
2026 // URLs, we use a data: URL as the real value. We actually want to save
2027 // the about: URL to the history db and keep the data: URL hidden. This is
2028 // what the TabContents' URL getter does.
2029 UpdateHistoryForNavigation(GetURL(), details, params);
2030 }
2031
[email protected]76543b92009-08-31 17:27:452032 if (!did_navigate)
2033 return; // No navigation happened.
[email protected]420ae012009-04-24 05:16:322034
2035 // DO NOT ADD MORE STUFF TO THIS FUNCTION! Your component should either listen
2036 // for the appropriate notification (best) or you can add it to
2037 // DidNavigateMainFramePostCommit / DidNavigateAnyFramePostCommit (only if
2038 // necessary, please).
2039
2040 // Run post-commit tasks.
2041 if (details.is_main_frame)
2042 DidNavigateMainFramePostCommit(details, params);
2043 DidNavigateAnyFramePostCommit(rvh, details, params);
2044}
2045
2046void TabContents::UpdateState(RenderViewHost* rvh,
2047 int32 page_id,
2048 const std::string& state) {
2049 DCHECK(rvh == render_view_host());
2050
2051 // We must be prepared to handle state updates for any page, these occur
2052 // when the user is scrolling and entering form data, as well as when we're
2053 // leaving a page, in which case our state may have already been moved to
2054 // the next page. The navigation controller will look up the appropriate
2055 // NavigationEntry and update it when it is notified via the delegate.
2056
2057 int entry_index = controller_.GetEntryIndexWithPageID(
2058 GetSiteInstance(), page_id);
2059 if (entry_index < 0)
2060 return;
2061 NavigationEntry* entry = controller_.GetEntryAtIndex(entry_index);
2062
2063 if (state == entry->content_state())
2064 return; // Nothing to update.
2065 entry->set_content_state(state);
2066 controller_.NotifyEntryChanged(entry, entry_index);
2067}
2068
2069void TabContents::UpdateTitle(RenderViewHost* rvh,
2070 int32 page_id, const std::wstring& title) {
2071 // If we have a title, that's a pretty good indication that we've started
2072 // getting useful data.
2073 SetNotWaitingForResponse();
2074
2075 DCHECK(rvh == render_view_host());
2076 NavigationEntry* entry = controller_.GetEntryWithPageID(GetSiteInstance(),
2077 page_id);
2078 if (!entry || !UpdateTitleForEntry(entry, title))
2079 return;
2080
2081 // Broadcast notifications when the UI should be updated.
2082 if (entry == controller_.GetEntryAtOffset(0))
[email protected]f1cd5e82009-10-23 17:19:032083 NotifyNavigationStateChanged(INVALIDATE_TITLE);
[email protected]420ae012009-04-24 05:16:322084}
2085
[email protected]420ae012009-04-24 05:16:322086void TabContents::UpdateEncoding(RenderViewHost* render_view_host,
[email protected]41fc0322009-09-04 22:23:402087 const std::string& encoding) {
[email protected]420ae012009-04-24 05:16:322088 set_encoding(encoding);
2089}
2090
2091void TabContents::UpdateTargetURL(int32 page_id, const GURL& url) {
2092 if (delegate())
2093 delegate()->UpdateTargetURL(this, url);
2094}
2095
2096void TabContents::UpdateThumbnail(const GURL& url,
2097 const SkBitmap& bitmap,
2098 const ThumbnailScore& score) {
2099 // Tell History about this thumbnail
[email protected]449478302009-06-09 20:04:282100 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kThumbnailStore)) {
[email protected]72d0fc72009-07-09 17:30:492101 if (!profile()->IsOffTheRecord())
[email protected]42339402009-07-17 17:27:192102 profile()->GetThumbnailStore()->SetPageThumbnail(url, bitmap,
2103 score, true);
[email protected]449478302009-06-09 20:04:282104 } else {
2105 HistoryService* hs;
2106 if (!profile()->IsOffTheRecord() &&
2107 (hs = profile()->GetHistoryService(Profile::IMPLICIT_ACCESS))) {
2108 hs->SetPageThumbnail(url, bitmap, score);
2109 }
[email protected]420ae012009-04-24 05:16:322110 }
2111}
2112
[email protected]8922e1f2009-10-03 05:01:262113void TabContents::UpdateInspectorSettings(const std::string& raw_settings) {
[email protected]0df30122009-06-03 12:13:082114 profile()->GetPrefs()->SetString(prefs::kWebKitInspectorSettings,
[email protected]8922e1f2009-10-03 05:01:262115 UTF8ToWide(raw_settings));
[email protected]0df30122009-06-03 12:13:082116}
2117
[email protected]420ae012009-04-24 05:16:322118void TabContents::Close(RenderViewHost* rvh) {
[email protected]07707302009-11-06 00:50:292119 // The UI may be in an event-tracking loop, such as between the
2120 // mouse-down and mouse-up in text selection or a button click.
2121 // Defer the close until after tracking is complete, so that we
2122 // don't free objects out from under the UI.
2123 // TODO(shess): This could probably be integrated with the
2124 // IsDoingDrag() test below. Punting for now because I need more
2125 // research to understand how this impacts platforms other than Mac.
2126 // TODO(shess): This could get more fine-grained. For instance,
2127 // closing a tab in another window while selecting text in the
2128 // current window's Omnibox should be just fine.
2129 if (view()->IsEventTracking()) {
2130 view()->CloseTabAfterEventTracking();
2131 return;
2132 }
2133
[email protected]24a4d1062009-07-10 23:10:422134 // If we close the tab while we're in the middle of a drag, we'll crash.
2135 // Instead, cancel the drag and close it as soon as the drag ends.
2136 if (view()->IsDoingDrag()) {
2137 view()->CancelDragAndCloseTab();
2138 return;
2139 }
2140
[email protected]420ae012009-04-24 05:16:322141 // Ignore this if it comes from a RenderViewHost that we aren't showing.
2142 if (delegate() && rvh == render_view_host())
2143 delegate()->CloseContents(this);
2144}
2145
2146void TabContents::RequestMove(const gfx::Rect& new_bounds) {
2147 if (delegate() && delegate()->IsPopup(this))
2148 delegate()->MoveContents(this, new_bounds);
2149}
2150
[email protected]7ab1e7d62009-10-14 23:32:012151void TabContents::DidStartLoading() {
[email protected]420ae012009-04-24 05:16:322152 SetIsLoading(true, NULL);
2153}
2154
[email protected]7ab1e7d62009-10-14 23:32:012155void TabContents::DidStopLoading() {
[email protected]420ae012009-04-24 05:16:322156 scoped_ptr<LoadNotificationDetails> details;
2157
2158 NavigationEntry* entry = controller_.GetActiveEntry();
2159 // An entry may not exist for a stop when loading an initial blank page or
2160 // if an iframe injected by script into a blank page finishes loading.
2161 if (entry) {
[email protected]420ae012009-04-24 05:16:322162 base::TimeDelta elapsed = base::TimeTicks::Now() - current_load_start_;
2163
2164 details.reset(new LoadNotificationDetails(
[email protected]ebe89e062009-08-13 23:16:542165 entry->virtual_url(),
[email protected]420ae012009-04-24 05:16:322166 entry->transition_type(),
2167 elapsed,
2168 &controller_,
2169 controller_.GetCurrentEntryIndex()));
2170 }
2171
2172 // Tell PasswordManager we've finished a page load, which serves as a
2173 // green light to save pending passwords and reset itself.
2174 GetPasswordManager()->DidStopLoading();
2175
2176 SetIsLoading(false, details.get());
2177}
2178
[email protected]420ae012009-04-24 05:16:322179void TabContents::DidRedirectProvisionalLoad(int32 page_id,
2180 const GURL& source_url,
2181 const GURL& target_url) {
2182 NavigationEntry* entry;
2183 if (page_id == -1)
2184 entry = controller_.pending_entry();
2185 else
2186 entry = controller_.GetEntryWithPageID(GetSiteInstance(), page_id);
2187 if (!entry || entry->url() != source_url)
2188 return;
2189 entry->set_url(target_url);
2190}
2191
[email protected]420ae012009-04-24 05:16:322192void TabContents::RequestOpenURL(const GURL& url, const GURL& referrer,
2193 WindowOpenDisposition disposition) {
2194 if (render_manager_.dom_ui()) {
2195 // When we're a DOM UI, it will provide a page transition type for us (this
2196 // is so the new tab page can specify AUTO_BOOKMARK for automatically
2197 // generated suggestions).
2198 //
2199 // Note also that we hide the referrer for DOM UI pages. We don't really
[email protected]60e448982009-05-06 04:21:162200 // want web sites to see a referrer of "chrome://blah" (and some
2201 // chrome: URLs might have search terms or other stuff we don't want to
[email protected]420ae012009-04-24 05:16:322202 // send to the site), so we send no referrer.
2203 OpenURL(url, GURL(), disposition,
2204 render_manager_.dom_ui()->link_transition_type());
2205 } else {
2206 OpenURL(url, referrer, disposition, PageTransition::LINK);
2207 }
2208}
2209
[email protected]1ee614862009-07-06 20:29:182210void TabContents::DomOperationResponse(const std::string& json_string,
2211 int automation_id) {
2212}
2213
[email protected]420ae012009-04-24 05:16:322214void TabContents::ProcessDOMUIMessage(const std::string& message,
[email protected]e4dad9fb2009-10-06 18:15:582215 const Value* content,
[email protected]811bfe32009-07-01 08:46:252216 int request_id,
2217 bool has_callback) {
[email protected]420ae012009-04-24 05:16:322218 if (!render_manager_.dom_ui()) {
[email protected]c2e74fe82009-09-03 17:57:442219 // This can happen if someone uses window.open() to open an extension URL
2220 // from a non-extension context.
2221 render_view_host()->BlockExtensionRequest(request_id);
[email protected]420ae012009-04-24 05:16:322222 return;
2223 }
[email protected]811bfe32009-07-01 08:46:252224 render_manager_.dom_ui()->ProcessDOMUIMessage(message, content, request_id,
2225 has_callback);
[email protected]420ae012009-04-24 05:16:322226}
2227
2228void TabContents::ProcessExternalHostMessage(const std::string& message,
2229 const std::string& origin,
2230 const std::string& target) {
2231 if (delegate())
2232 delegate()->ForwardMessageToExternalHost(message, origin, target);
2233}
2234
[email protected]420ae012009-04-24 05:16:322235void TabContents::RunFileChooser(bool multiple_files,
2236 const string16& title,
2237 const FilePath& default_file) {
2238 if (!select_file_dialog_.get())
2239 select_file_dialog_ = SelectFileDialog::Create(this);
2240 SelectFileDialog::Type dialog_type =
2241 multiple_files ? SelectFileDialog::SELECT_OPEN_MULTI_FILE :
2242 SelectFileDialog::SELECT_OPEN_FILE;
2243 select_file_dialog_->SelectFile(dialog_type, title, default_file,
2244 NULL, 0, FILE_PATH_LITERAL(""),
2245 view_->GetTopLevelNativeWindow(), NULL);
2246}
2247
2248void TabContents::RunJavaScriptMessage(
2249 const std::wstring& message,
2250 const std::wstring& default_prompt,
2251 const GURL& frame_url,
2252 const int flags,
2253 IPC::Message* reply_msg,
2254 bool* did_suppress_message) {
2255 // Suppress javascript messages when requested and when inside a constrained
2256 // popup window (because that activates them and breaks them out of the
2257 // constrained window jail).
[email protected]91ed49a2009-07-07 21:29:552258 // Also suppress messages when showing an interstitial. The interstitial is
2259 // shown over the previous page, we don't want the hidden page dialogs to
2260 // interfere with the interstitial.
2261 bool suppress_this_message = suppress_javascript_messages_ ||
2262 showing_interstitial_page();
[email protected]420ae012009-04-24 05:16:322263 if (delegate())
2264 suppress_this_message |=
[email protected]b6c874582009-05-08 19:38:312265 (delegate()->GetConstrainingContents(this) != this);
[email protected]420ae012009-04-24 05:16:322266
2267 *did_suppress_message = suppress_this_message;
2268
2269 if (!suppress_this_message) {
2270 base::TimeDelta time_since_last_message(
2271 base::TimeTicks::Now() - last_javascript_message_dismissal_);
2272 bool show_suppress_checkbox = false;
2273 // Show a checkbox offering to suppress further messages if this message is
2274 // being displayed within kJavascriptMessageExpectedDelay of the last one.
2275 if (time_since_last_message <
2276 base::TimeDelta::FromMilliseconds(kJavascriptMessageExpectedDelay))
2277 show_suppress_checkbox = true;
2278
[email protected]d82ed61e2009-06-16 02:46:222279 RunJavascriptMessageBox(this, frame_url, flags, message, default_prompt,
[email protected]420ae012009-04-24 05:16:322280 show_suppress_checkbox, reply_msg);
2281 } else {
2282 // If we are suppressing messages, just reply as is if the user immediately
2283 // pressed "Cancel".
[email protected]beb440c2009-11-06 04:08:542284 OnMessageBoxClosed(reply_msg, false, std::wstring());
[email protected]420ae012009-04-24 05:16:322285 }
2286}
2287
2288void TabContents::RunBeforeUnloadConfirm(const std::wstring& message,
2289 IPC::Message* reply_msg) {
[email protected]7ab1e7d62009-10-14 23:32:012290 is_showing_before_unload_dialog_ = true;
[email protected]d82ed61e2009-06-16 02:46:222291 RunBeforeUnloadDialog(this, message, reply_msg);
[email protected]420ae012009-04-24 05:16:322292}
2293
2294void TabContents::ShowModalHTMLDialog(const GURL& url, int width, int height,
2295 const std::string& json_arguments,
2296 IPC::Message* reply_msg) {
2297 if (delegate()) {
2298 HtmlDialogUIDelegate* dialog_delegate =
2299 new ModalHtmlDialogDelegate(url, width, height, json_arguments,
[email protected]d82ed61e2009-06-16 02:46:222300 reply_msg, this);
[email protected]420ae012009-04-24 05:16:322301 delegate()->ShowHtmlDialog(dialog_delegate, NULL);
2302 }
2303}
2304
2305void TabContents::PasswordFormsSeen(
[email protected]ed3fb032009-06-16 19:50:562306 const std::vector<webkit_glue::PasswordForm>& forms) {
[email protected]420ae012009-04-24 05:16:322307 GetPasswordManager()->PasswordFormsSeen(forms);
2308}
2309
[email protected]420ae012009-04-24 05:16:322310// Checks to see if we should generate a keyword based on the OSDD, and if
2311// necessary uses TemplateURLFetcher to download the OSDD and create a keyword.
2312void TabContents::PageHasOSDD(RenderViewHost* render_view_host,
2313 int32 page_id, const GURL& url,
2314 bool autodetected) {
2315 // Make sure page_id is the current page, and the TemplateURLModel is loaded.
2316 DCHECK(url.is_valid());
2317 if (!IsActiveEntry(page_id))
2318 return;
2319 TemplateURLModel* url_model = profile()->GetTemplateURLModel();
2320 if (!url_model)
2321 return;
2322 if (!url_model->loaded()) {
2323 url_model->Load();
2324 return;
2325 }
2326 if (!profile()->GetTemplateURLFetcher())
2327 return;
2328
2329 if (profile()->IsOffTheRecord())
2330 return;
2331
2332 const NavigationEntry* entry = controller_.GetLastCommittedEntry();
2333 DCHECK(entry);
2334
2335 const NavigationEntry* base_entry = entry;
2336 if (IsFormSubmit(base_entry)) {
2337 // If the current page is a form submit, find the last page that was not
2338 // a form submit and use its url to generate the keyword from.
2339 int index = controller_.last_committed_entry_index() - 1;
2340 while (index >= 0 && IsFormSubmit(controller_.GetEntryAtIndex(index)))
2341 index--;
2342 if (index >= 0)
2343 base_entry = controller_.GetEntryAtIndex(index);
2344 else
2345 base_entry = NULL;
2346 }
2347
2348 // We want to use the user typed URL if available since that represents what
2349 // the user typed to get here, and fall back on the regular URL if not.
2350 if (!base_entry)
2351 return;
2352 GURL keyword_url = base_entry->user_typed_url().is_valid() ?
2353 base_entry->user_typed_url() : base_entry->url();
2354 if (!keyword_url.is_valid())
2355 return;
2356 std::wstring keyword = TemplateURLModel::GenerateKeyword(keyword_url,
2357 autodetected);
[email protected]a88dbdd52010-01-15 19:25:532358
2359 // For JS added OSDD empty keyword is OK because we will generate keyword
2360 // later from OSDD content.
2361 if (autodetected && keyword.empty())
[email protected]420ae012009-04-24 05:16:322362 return;
2363 const TemplateURL* template_url =
2364 url_model->GetTemplateURLForKeyword(keyword);
2365 if (template_url && (!template_url->safe_for_autoreplace() ||
2366 template_url->originating_url() == url)) {
2367 // Either there is a user created TemplateURL for this keyword, or the
2368 // keyword has the same OSDD url and we've parsed it.
2369 return;
2370 }
2371
2372 // Download the OpenSearch description document. If this is successful a
2373 // new keyword will be created when done.
[email protected]420ae012009-04-24 05:16:322374 profile()->GetTemplateURLFetcher()->ScheduleDownload(
2375 keyword,
2376 url,
2377 base_entry->favicon().url(),
[email protected]d0cc9fb52009-06-23 02:26:162378 this,
[email protected]420ae012009-04-24 05:16:322379 autodetected);
2380}
2381
[email protected]420ae012009-04-24 05:16:322382GURL TabContents::GetAlternateErrorPageURL() const {
2383 GURL url;
2384 // Disable alternate error pages when in OffTheRecord/Incognito mode.
2385 if (profile()->IsOffTheRecord())
2386 return url;
2387
2388 PrefService* prefs = profile()->GetPrefs();
2389 DCHECK(prefs);
2390 if (prefs->GetBoolean(prefs::kAlternateErrorPagesEnabled)) {
[email protected]a33457b82009-09-21 23:07:152391 url = google_util::AppendGoogleLocaleParam(
2392 GURL(google_util::kLinkDoctorBaseURL));
[email protected]420ae012009-04-24 05:16:322393 url = google_util::AppendGoogleTLDParam(url);
2394 }
2395 return url;
2396}
2397
2398WebPreferences TabContents::GetWebkitPrefs() {
2399 PrefService* prefs = render_view_host()->process()->profile()->GetPrefs();
[email protected]7ccddb8c2009-08-04 17:36:552400 bool is_dom_ui = false;
2401 return RenderViewHostDelegateHelper::GetWebkitPrefs(prefs, is_dom_ui);
[email protected]420ae012009-04-24 05:16:322402}
2403
[email protected]420ae012009-04-24 05:16:322404void TabContents::OnJSOutOfMemory() {
2405 AddInfoBar(new SimpleAlertInfoBarDelegate(
2406 this, l10n_util::GetString(IDS_JS_OUT_OF_MEMORY_PROMPT), NULL));
2407}
2408
[email protected]420ae012009-04-24 05:16:322409void TabContents::OnCrossSiteResponse(int new_render_process_host_id,
2410 int new_request_id) {
[email protected]57c6a652009-05-04 07:58:342411 // Allows the TabContents to react when a cross-site response is ready to be
[email protected]420ae012009-04-24 05:16:322412 // delivered to a pending RenderViewHost. We must first run the onunload
2413 // handler of the old RenderViewHost before we can allow it to proceed.
2414 render_manager_.OnCrossSiteResponse(new_render_process_host_id,
2415 new_request_id);
2416}
2417
2418bool TabContents::CanBlur() const {
2419 return delegate() ? delegate()->CanBlur() : true;
2420}
2421
2422gfx::Rect TabContents::GetRootWindowResizerRect() const {
2423 if (delegate())
2424 return delegate()->GetRootWindowResizerRect();
2425 return gfx::Rect();
2426}
2427
2428void TabContents::RendererUnresponsive(RenderViewHost* rvh,
2429 bool is_during_unload) {
2430 if (is_during_unload) {
2431 // Hang occurred while firing the beforeunload/unload handler.
2432 // Pretend the handler fired so tab closing continues as if it had.
[email protected]829e7612009-04-25 01:15:112433 rvh->set_sudden_termination_allowed(true);
[email protected]420ae012009-04-24 05:16:322434
2435 if (!render_manager_.ShouldCloseTabOnUnresponsiveRenderer())
2436 return;
2437
2438 // If the tab hangs in the beforeunload/unload handler there's really
2439 // nothing we can do to recover. Pretend the unload listeners have
2440 // all fired and close the tab. If the hang is in the beforeunload handler
2441 // then the user will not have the option of cancelling the close.
2442 Close(rvh);
2443 return;
2444 }
2445
2446 if (render_view_host() && render_view_host()->IsRenderViewLive())
[email protected]e7b8d832009-11-20 22:41:002447 hung_renderer_dialog::ShowForTabContents(this);
[email protected]420ae012009-04-24 05:16:322448}
2449
2450void TabContents::RendererResponsive(RenderViewHost* render_view_host) {
[email protected]e7b8d832009-11-20 22:41:002451 hung_renderer_dialog::HideForTabContents(this);
[email protected]420ae012009-04-24 05:16:322452}
2453
2454void TabContents::LoadStateChanged(const GURL& url,
[email protected]094e5b22009-09-25 04:23:562455 net::LoadState load_state,
2456 uint64 upload_position,
2457 uint64 upload_size) {
[email protected]420ae012009-04-24 05:16:322458 load_state_ = load_state;
[email protected]094e5b22009-09-25 04:23:562459 upload_position_ = upload_position;
2460 upload_size_ = upload_size;
[email protected]d686e812009-06-03 19:10:292461 std::wstring languages =
2462 profile()->GetPrefs()->GetString(prefs::kAcceptLanguages);
[email protected]d686e812009-06-03 19:10:292463 std::string host = url.host();
[email protected]ce85f602009-11-07 01:34:532464 load_state_host_ =
2465 net::IDNToUnicode(host.c_str(), host.size(), languages, NULL);
[email protected]420ae012009-04-24 05:16:322466 if (load_state_ == net::LOAD_STATE_READING_RESPONSE)
2467 SetNotWaitingForResponse();
2468 if (is_loading())
[email protected]c9cd2222009-05-06 05:16:502469 NotifyNavigationStateChanged(INVALIDATE_LOAD | INVALIDATE_TAB);
[email protected]420ae012009-04-24 05:16:322470}
2471
[email protected]1ee614862009-07-06 20:29:182472bool TabContents::IsExternalTabContainer() const {
2473 if (!delegate())
2474 return false;
2475
2476 return delegate()->IsExternalTabContainer();
2477}
2478
[email protected]1ee614862009-07-06 20:29:182479void TabContents::DidInsertCSS() {
2480 // This RVHDelegate function is used for extensions and not us.
[email protected]420ae012009-04-24 05:16:322481}
2482
[email protected]130efb02009-09-18 18:54:352483void TabContents::FocusedNodeChanged() {
2484 NotificationService::current()->Notify(
2485 NotificationType::FOCUS_CHANGED_IN_PAGE,
2486 Source<RenderViewHost>(render_view_host()),
2487 NotificationService::NoDetails());
2488}
2489
[email protected]420ae012009-04-24 05:16:322490void TabContents::FileSelected(const FilePath& path,
2491 int index, void* params) {
2492 render_view_host()->FileSelected(path);
2493}
2494
2495void TabContents::MultiFilesSelected(const std::vector<FilePath>& files,
2496 void* params) {
2497 render_view_host()->MultiFilesSelected(files);
2498}
2499
2500void TabContents::FileSelectionCanceled(void* params) {
2501 // If the user cancels choosing a file to upload we pass back an
2502 // empty vector.
2503 render_view_host()->MultiFilesSelected(std::vector<FilePath>());
2504}
2505
2506void TabContents::BeforeUnloadFiredFromRenderManager(
2507 bool proceed,
2508 bool* proceed_to_fire_unload) {
2509 if (delegate())
2510 delegate()->BeforeUnloadFired(this, proceed, proceed_to_fire_unload);
2511}
2512
2513void TabContents::UpdateRenderViewSizeForRenderManager() {
2514 // TODO(brettw) this is a hack. See TabContentsView::SizeContents.
2515 view_->SizeContents(view_->GetContainerSize());
2516}
2517
2518DOMUI* TabContents::CreateDOMUIForRenderManager(const GURL& url) {
[email protected]d82ed61e2009-06-16 02:46:222519 return DOMUIFactory::CreateDOMUIForURL(this, url);
[email protected]420ae012009-04-24 05:16:322520}
2521
2522NavigationEntry*
2523TabContents::GetLastCommittedNavigationEntryForRenderManager() {
2524 return controller_.GetLastCommittedEntry();
2525}
2526
2527bool TabContents::CreateRenderViewForRenderManager(
2528 RenderViewHost* render_view_host) {
[email protected]811bfe32009-07-01 08:46:252529 // If the pending navigation is to a DOMUI, tell the RenderView about any
2530 // bindings it will need enabled.
[email protected]420ae012009-04-24 05:16:322531 if (render_manager_.pending_dom_ui())
[email protected]811bfe32009-07-01 08:46:252532 render_view_host->AllowBindings(
2533 render_manager_.pending_dom_ui()->bindings());
[email protected]1adff062009-06-02 18:39:552534
[email protected]420ae012009-04-24 05:16:322535 RenderWidgetHostView* rwh_view = view_->CreateViewForWidget(render_view_host);
[email protected]1a98a932009-11-17 00:12:522536
2537 scoped_refptr<URLRequestContextGetter> request_context = request_context_;
2538 if (!request_context.get())
2539 request_context = profile()->GetRequestContext();
2540
2541 if (!render_view_host->CreateRenderView(request_context))
[email protected]420ae012009-04-24 05:16:322542 return false;
2543
2544 // Now that the RenderView has been created, we need to tell it its size.
2545 rwh_view->SetSize(view_->GetContainerSize());
2546
2547 UpdateMaxPageIDIfNecessary(render_view_host->site_instance(),
2548 render_view_host);
2549 return true;
2550}
2551
2552void TabContents::Observe(NotificationType type,
2553 const NotificationSource& source,
2554 const NotificationDetails& details) {
2555 switch (type.value) {
2556 case NotificationType::BOOKMARK_MODEL_LOADED:
2557 // BookmarkModel finished loading, fall through to update starred state.
2558 case NotificationType::URLS_STARRED: {
2559 // Somewhere, a URL has been starred.
2560 // Ignore notifications for profiles other than our current one.
2561 Profile* source_profile = Source<Profile>(source).ptr();
2562 if (!source_profile || !source_profile->IsSameProfile(profile()))
2563 return;
2564
2565 UpdateStarredStateForCurrentURL();
2566 break;
2567 }
2568 case NotificationType::PREF_CHANGED: {
2569 std::wstring* pref_name_in = Details<std::wstring>(details).ptr();
2570 DCHECK(Source<PrefService>(source).ptr() == profile()->GetPrefs());
2571 if (*pref_name_in == prefs::kAlternateErrorPagesEnabled) {
2572 UpdateAlternateErrorPageURL();
2573 } else if (*pref_name_in == prefs::kDefaultCharset ||
2574 StartsWithASCII(WideToUTF8(*pref_name_in), "webkit.webprefs.", true)
2575 ) {
2576 UpdateWebPreferences();
2577 } else {
2578 NOTREACHED() << "unexpected pref change notification" << *pref_name_in;
2579 }
2580 break;
2581 }
2582 case NotificationType::RENDER_WIDGET_HOST_DESTROYED:
2583 view_->RenderWidgetHostDestroyed(Source<RenderWidgetHost>(source).ptr());
2584 break;
2585
2586 case NotificationType::NAV_ENTRY_COMMITTED: {
2587 DCHECK(&controller_ == Source<NavigationController>(source).ptr());
2588
2589 NavigationController::LoadCommittedDetails& committed_details =
2590 *(Details<NavigationController::LoadCommittedDetails>(details).ptr());
2591 ExpireInfoBars(committed_details);
2592 break;
2593 }
2594
[email protected]d3ba77272009-09-03 00:06:092595#if defined(OS_LINUX)
2596 case NotificationType::BROWSER_THEME_CHANGED: {
[email protected]7ff431e2010-01-07 18:18:542597 renderer_preferences_util::UpdateFromSystemSettings(
2598 &renderer_preferences_, profile());
[email protected]d3ba77272009-09-03 00:06:092599 render_view_host()->SyncRendererPrefs();
2600 break;
2601 }
2602#endif
2603
[email protected]420ae012009-04-24 05:16:322604 default:
2605 NOTREACHED();
2606 }
2607}
[email protected]aacd7cc2009-09-02 14:51:102608
[email protected]beb440c2009-11-06 04:08:542609std::wstring TabContents::GetMessageBoxTitle(const GURL& frame_url,
2610 bool is_alert) {
2611 if (!frame_url.has_host())
2612 return l10n_util::GetString(
2613 is_alert ? IDS_JAVASCRIPT_ALERT_DEFAULT_TITLE
2614 : IDS_JAVASCRIPT_MESSAGEBOX_DEFAULT_TITLE);
2615
2616 // We really only want the scheme, hostname, and port.
2617 GURL::Replacements replacements;
2618 replacements.ClearUsername();
2619 replacements.ClearPassword();
2620 replacements.ClearPath();
2621 replacements.ClearQuery();
2622 replacements.ClearRef();
2623 GURL clean_url = frame_url.ReplaceComponents(replacements);
2624
2625 // TODO(brettw) it should be easier than this to do the correct language
2626 // handling without getting the accept language from the profile.
2627 std::wstring base_address = gfx::ElideUrl(clean_url, gfx::Font(), 0,
2628 profile()->GetPrefs()->GetString(prefs::kAcceptLanguages));
2629 // Force URL to have LTR directionality.
2630 if (l10n_util::GetTextDirection() == l10n_util::RIGHT_TO_LEFT)
2631 l10n_util::WrapStringWithLTRFormatting(&base_address);
2632
2633 return l10n_util::GetStringF(
2634 is_alert ? IDS_JAVASCRIPT_ALERT_TITLE : IDS_JAVASCRIPT_MESSAGEBOX_TITLE,
2635 base_address);
2636}
2637
2638gfx::NativeWindow TabContents::GetMessageBoxRootWindow() {
2639 return view_->GetTopLevelNativeWindow();
2640}
2641
2642void TabContents::OnMessageBoxClosed(IPC::Message* reply_msg,
2643 bool success,
2644 const std::wstring& prompt) {
2645 last_javascript_message_dismissal_ = base::TimeTicks::Now();
2646 if (is_showing_before_unload_dialog_ && !success) {
2647 // If a beforeunload dialog is canceled, we need to stop the throbber from
2648 // spinning, since we forced it to start spinning in Navigate.
2649 DidStopLoading();
2650
2651 tab_close_start_time_ = base::TimeTicks();
2652 }
2653 is_showing_before_unload_dialog_ = false;
2654 render_view_host()->JavaScriptMessageBoxClosed(reply_msg, success, prompt);
2655}
2656
2657void TabContents::SetSuppressMessageBoxes(bool suppress_message_boxes) {
2658 set_suppress_javascript_messages(suppress_message_boxes);
2659}
2660
[email protected]41fc0322009-09-04 22:23:402661void TabContents::set_encoding(const std::string& encoding) {
[email protected]aacd7cc2009-09-02 14:51:102662 encoding_ = CharacterEncoding::GetCanonicalEncodingNameByAliasName(encoding);
2663}