blob: 27c215e6c2ba2a40113c08547211a94eec030183 [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"
[email protected]cf68b092010-01-15 20:05:5519#include "chrome/browser/browser_shutdown.h"
initial.commit09911bf2008-07-26 23:55:2920#include "chrome/browser/cert_store.h"
[email protected]aacd7cc2009-09-02 14:51:1021#include "chrome/browser/character_encoding.h"
[email protected]96d185d2009-04-24 03:28:5422#include "chrome/browser/debugger/devtools_manager.h"
[email protected]420ae012009-04-24 05:16:3223#include "chrome/browser/dom_operation_notification_details.h"
[email protected]96d185d2009-04-24 03:28:5424#include "chrome/browser/dom_ui/dom_ui.h"
25#include "chrome/browser/dom_ui/dom_ui_factory.h"
[email protected]bcef0dc02009-02-28 00:35:0226#include "chrome/browser/download/download_item_model.h"
[email protected]420ae012009-04-24 05:16:3227#include "chrome/browser/download/download_manager.h"
28#include "chrome/browser/download/download_request_manager.h"
[email protected]e7eaedde2009-09-25 20:09:4929#include "chrome/browser/external_protocol_handler.h"
[email protected]92c6f9b92009-10-24 04:35:0830#include "chrome/browser/extensions/extensions_service.h"
[email protected]0189bc722009-08-28 21:56:4831#include "chrome/browser/favicon_service.h"
[email protected]19289d02009-10-21 21:47:0032#include "chrome/browser/form_field_history_manager.h"
[email protected]420ae012009-04-24 05:16:3233#include "chrome/browser/google_util.h"
[email protected]8897c382009-05-06 17:53:2634#include "chrome/browser/hung_renderer_dialog.h"
[email protected]420ae012009-04-24 05:16:3235#include "chrome/browser/jsmessage_box_handler.h"
36#include "chrome/browser/load_from_memory_cache_details.h"
37#include "chrome/browser/load_notification_details.h"
[email protected]84abba62009-10-07 17:01:4438#include "chrome/browser/metrics/metric_event_duration_details.h"
[email protected]ea0e80892009-08-22 00:36:0539#include "chrome/browser/modal_html_dialog_delegate.h"
[email protected]c8865482009-07-23 20:40:1040#include "chrome/browser/omnibox_search_hint.h"
[email protected]fdd61c62009-04-22 19:22:5741#include "chrome/browser/password_manager/password_manager.h"
42#include "chrome/browser/plugin_installer.h"
[email protected]ce560f82009-06-03 09:39:4443#include "chrome/browser/profile.h"
[email protected]1db6ff152009-10-12 15:32:0744#include "chrome/browser/renderer_host/render_process_host.h"
[email protected]96d185d2009-04-24 03:28:5445#include "chrome/browser/renderer_host/render_widget_host_view.h"
[email protected]fd694982009-07-03 17:32:3746#include "chrome/browser/renderer_host/resource_request_details.h"
[email protected]1db6ff152009-10-12 15:32:0747#include "chrome/browser/renderer_host/site_instance.h"
[email protected]96d185d2009-04-24 03:28:5448#include "chrome/browser/renderer_host/web_cache_manager.h"
[email protected]93623c5d2009-12-10 21:40:3249#include "chrome/browser/renderer_preferences_util.h"
[email protected]c37e3b62010-01-22 00:15:1750#include "chrome/browser/sessions/session_types.h"
[email protected]bd1ad682009-05-15 22:19:1751#include "chrome/browser/tab_contents/infobar_delegate.h"
[email protected]7e383692009-06-12 19:14:5452#include "chrome/browser/tab_contents/interstitial_page.h"
[email protected]f3ec7742009-01-15 00:59:1653#include "chrome/browser/tab_contents/navigation_entry.h"
[email protected]de34a962009-06-26 23:22:4354#include "chrome/browser/tab_contents/provisional_load_details.h"
[email protected]f3ec7742009-01-15 00:59:1655#include "chrome/browser/tab_contents/tab_contents_delegate.h"
[email protected]fdd61c62009-04-22 19:22:5756#include "chrome/browser/tab_contents/tab_contents_view.h"
[email protected]58dca552009-06-17 00:35:0257#include "chrome/browser/tab_contents/thumbnail_generator.h"
[email protected]449478302009-06-09 20:04:2858#include "chrome/browser/thumbnail_store.h"
[email protected]420ae012009-04-24 05:16:3259#include "chrome/browser/search_engines/template_url_fetcher.h"
60#include "chrome/browser/search_engines/template_url_model.h"
[email protected]35f7d212009-04-29 21:19:2761#include "chrome/common/chrome_switches.h"
[email protected]5d246db22009-10-27 06:17:5762#include "chrome/common/extensions/extension_action.h"
[email protected]bfd04a62009-02-01 18:16:5663#include "chrome/common/notification_service.h"
[email protected]89d6e6e2009-12-03 23:06:5164#include "chrome/common/platform_util.h"
initial.commit09911bf2008-07-26 23:55:2965#include "chrome/common/pref_names.h"
[email protected]1eb89e82008-08-15 12:27:0366#include "chrome/common/pref_service.h"
[email protected]420ae012009-04-24 05:16:3267#include "chrome/common/render_messages.h"
[email protected]1ee614862009-07-06 20:29:1868#include "chrome/common/renderer_preferences.h"
[email protected]96d185d2009-04-24 03:28:5469#include "chrome/common/url_constants.h"
[email protected]8897c382009-05-06 17:53:2670#include "grit/generated_resources.h"
[email protected]420ae012009-04-24 05:16:3271#include "grit/locale_settings.h"
[email protected]fd626782010-01-22 05:26:0672#include "grit/platform_locale_settings.h"
[email protected]420ae012009-04-24 05:16:3273#include "net/base/mime_util.h"
74#include "net/base/net_errors.h"
[email protected]d686e812009-06-03 19:10:2975#include "net/base/net_util.h"
[email protected]420ae012009-04-24 05:16:3276#include "net/base/registry_controlled_domain.h"
[email protected]1ee614862009-07-06 20:29:1877#include "webkit/glue/password_form.h"
78#include "webkit/glue/webpreferences.h"
[email protected]3e45ba92009-02-20 21:09:0079
[email protected]304f8792009-08-19 21:28:0980#if defined(OS_CHROMEOS)
81// For GdkScreen
82#include <gdk/gdk.h>
[email protected]0ba5c5e2009-08-20 16:47:1383#endif // defined(OS_CHROMEOS)
84
[email protected]420ae012009-04-24 05:16:3285// Cross-Site Navigations
86//
87// If a TabContents is told to navigate to a different web site (as determined
88// by SiteInstance), it will replace its current RenderViewHost with a new
89// RenderViewHost dedicated to the new SiteInstance. This works as follows:
90//
91// - Navigate determines whether the destination is cross-site, and if so,
92// it creates a pending_render_view_host_ and moves into the PENDING
93// RendererState.
94// - The pending RVH is "suspended," so that no navigation messages are sent to
95// its renderer until the onbeforeunload JavaScript handler has a chance to
96// run in the current RVH.
97// - The pending RVH tells CrossSiteRequestManager (a thread-safe singleton)
98// that it has a pending cross-site request. ResourceDispatcherHost will
99// check for this when the response arrives.
100// - The current RVH runs its onbeforeunload handler. If it returns false, we
101// cancel all the pending logic and go back to NORMAL. Otherwise we allow
102// the pending RVH to send the navigation request to its renderer.
103// - ResourceDispatcherHost receives a ResourceRequest on the IO thread. It
104// checks CrossSiteRequestManager to see that the RVH responsible has a
105// pending cross-site request, and then installs a CrossSiteEventHandler.
106// - When RDH receives a response, the BufferedEventHandler determines whether
107// it is a download. If so, it sends a message to the new renderer causing
108// it to cancel the request, and the download proceeds in the download
109// thread. For now, we stay in a PENDING state (with a pending RVH) until
[email protected]57c6a652009-05-04 07:58:34110// the next DidNavigate event for this TabContents. This isn't ideal, but it
[email protected]420ae012009-04-24 05:16:32111// doesn't affect any functionality.
112// - After RDH receives a response and determines that it is safe and not a
113// download, it pauses the response to first run the old page's onunload
114// handler. It does this by asynchronously calling the OnCrossSiteResponse
[email protected]57c6a652009-05-04 07:58:34115// method of TabContents on the UI thread, which sends a ClosePage message
[email protected]420ae012009-04-24 05:16:32116// to the current RVH.
117// - Once the onunload handler is finished, a ClosePage_ACK message is sent to
118// the ResourceDispatcherHost, who unpauses the response. Data is then sent
119// to the pending RVH.
120// - The pending renderer sends a FrameNavigate message that invokes the
121// DidNavigate method. This replaces the current RVH with the
122// pending RVH and goes back to the NORMAL RendererState.
123
124namespace {
125
126// Amount of time we wait between when a key event is received and the renderer
127// is queried for its state and pushed to the NavigationEntry.
128const int kQueryStateDelay = 5000;
129
130const int kSyncWaitDelay = 40;
131
[email protected]304f8792009-08-19 21:28:09132#if defined(OS_CHROMEOS)
133// If a popup window is bigger than this fraction of the screen on chrome os,
134// turn it into a tab
135const float kMaxWidthFactor = 0.5;
136const float kMaxHeightFactor = 0.6;
137#endif
138
[email protected]420ae012009-04-24 05:16:32139// If another javascript message box is displayed within
140// kJavascriptMessageExpectedDelay of a previous javascript message box being
141// dismissed, display an option to suppress future message boxes from this
142// contents.
143const int kJavascriptMessageExpectedDelay = 1000;
144
[email protected]420ae012009-04-24 05:16:32145// The list of prefs we want to observe.
146const wchar_t* kPrefsToObserve[] = {
147 prefs::kAlternateErrorPagesEnabled,
148 prefs::kWebKitJavaEnabled,
149 prefs::kWebKitJavascriptEnabled,
150 prefs::kWebKitLoadsImagesAutomatically,
151 prefs::kWebKitPluginsEnabled,
152 prefs::kWebKitUsesUniversalDetector,
153 prefs::kWebKitSerifFontFamily,
154 prefs::kWebKitSansSerifFontFamily,
155 prefs::kWebKitFixedFontFamily,
156 prefs::kWebKitDefaultFontSize,
157 prefs::kWebKitDefaultFixedFontSize,
158 prefs::kDefaultCharset
159 // kWebKitStandardFontIsSerif needs to be added
160 // if we let users pick which font to use, serif or sans-serif when
161 // no font is specified or a CSS generic family (serif or sans-serif)
162 // is not specified.
163};
164
165const int kPrefsToObserveLength = arraysize(kPrefsToObserve);
166
[email protected]420ae012009-04-24 05:16:32167// Returns true if the entry's transition type is FORM_SUBMIT.
168bool IsFormSubmit(const NavigationEntry* entry) {
169 return (PageTransition::StripQualifier(entry->transition_type()) ==
170 PageTransition::FORM_SUBMIT);
171}
172
173#if defined(OS_WIN)
174
175BOOL CALLBACK InvalidateWindow(HWND hwnd, LPARAM lparam) {
176 // Note: erase is required to properly paint some widgets borders. This can
177 // be seen with textfields.
178 InvalidateRect(hwnd, NULL, TRUE);
179 return TRUE;
180}
181#endif
182
[email protected]5e369672009-11-03 23:48:30183ViewMsg_Navigate_Params::NavigationType GetNavigationType(
184 Profile* profile, const NavigationEntry& entry, bool reload) {
185 if (reload)
186 return ViewMsg_Navigate_Params::RELOAD;
187
188 if (entry.restore_type() == NavigationEntry::RESTORE_LAST_SESSION &&
189 profile->DidLastSessionExitCleanly())
190 return ViewMsg_Navigate_Params::RESTORE;
191
192 return ViewMsg_Navigate_Params::NORMAL;
193}
194
195void MakeNavigateParams(Profile* profile, const NavigationEntry& entry,
196 bool reload, ViewMsg_Navigate_Params* params) {
[email protected]056de2d2009-06-26 16:41:34197 params->page_id = entry.page_id();
198 params->url = entry.url();
199 params->referrer = entry.referrer();
200 params->transition = entry.transition_type();
201 params->state = entry.content_state();
[email protected]5e369672009-11-03 23:48:30202 params->navigation_type = GetNavigationType(profile, entry, reload);
[email protected]056de2d2009-06-26 16:41:34203 params->request_time = base::Time::Now();
204}
205
[email protected]420ae012009-04-24 05:16:32206} // namespace
207
208// -----------------------------------------------------------------------------
209
[email protected]fdd61c62009-04-22 19:22:57210// static
211int TabContents::find_request_id_counter_ = -1;
212
[email protected]420ae012009-04-24 05:16:32213
[email protected]420ae012009-04-24 05:16:32214TabContents::TabContents(Profile* profile,
215 SiteInstance* site_instance,
216 int routing_id,
[email protected]34ac70502009-09-25 17:07:23217 const TabContents* base_tab_contents)
[email protected]b680ad22009-04-15 23:19:42218 : delegate_(NULL),
[email protected]66ba4932009-06-04 19:22:13219 ALLOW_THIS_IN_INITIALIZER_LIST(controller_(this, profile)),
220 ALLOW_THIS_IN_INITIALIZER_LIST(view_(
[email protected]d82ed61e2009-06-16 02:46:22221 TabContentsView::Create(this))),
222 ALLOW_THIS_IN_INITIALIZER_LIST(render_manager_(this, this)),
[email protected]fdd61c62009-04-22 19:22:57223 property_bag_(),
224 registrar_(),
[email protected]d82ed61e2009-06-16 02:46:22225 ALLOW_THIS_IN_INITIALIZER_LIST(printing_(*this)),
[email protected]fdd61c62009-04-22 19:22:57226 save_package_(),
[email protected]19289d02009-10-21 21:47:00227 form_field_history_manager_(),
[email protected]21674df2009-10-28 19:51:21228 autofill_manager_(),
[email protected]fdd61c62009-04-22 19:22:57229 password_manager_(),
230 plugin_installer_(),
[email protected]d82ed61e2009-06-16 02:46:22231 ALLOW_THIS_IN_INITIALIZER_LIST(fav_icon_helper_(this)),
[email protected]fdd61c62009-04-22 19:22:57232 select_file_dialog_(),
[email protected]d5f942ba2008-09-26 19:30:34233 is_loading_(false),
initial.commit09911bf2008-07-26 23:55:29234 is_crashed_(false),
[email protected]d5f942ba2008-09-26 19:30:34235 waiting_for_response_(false),
[email protected]d5f942ba2008-09-26 19:30:34236 max_page_id_(-1),
[email protected]fdd61c62009-04-22 19:22:57237 current_load_start_(),
238 load_state_(net::LOAD_STATE_IDLE),
239 load_state_host_(),
[email protected]094e5b22009-09-25 04:23:56240 upload_size_(0),
241 upload_position_(0),
[email protected]fdd61c62009-04-22 19:22:57242 received_page_title_(false),
243 is_starred_(false),
244 contents_mime_type_(),
245 encoding_(),
[email protected]332af7732009-03-11 13:21:35246 blocked_popups_(NULL),
[email protected]fdd61c62009-04-22 19:22:57247 infobar_delegates_(),
[email protected]fdd61c62009-04-22 19:22:57248 find_ui_active_(false),
249 find_op_aborted_(false),
250 current_find_request_id_(find_request_id_counter_++),
[email protected]e491f1c2009-05-22 20:28:12251 last_search_case_sensitive_(false),
252 last_search_prepopulate_text_(NULL),
253 last_search_result_(),
[email protected]fdd61c62009-04-22 19:22:57254 capturing_contents_(false),
255 is_being_destroyed_(false),
256 notify_disconnection_(false),
257 history_requests_(),
258#if defined(OS_WIN)
259 message_box_active_(CreateEvent(NULL, TRUE, FALSE, NULL)),
260#endif
261 last_javascript_message_dismissal_(),
[email protected]c2e74fe82009-09-03 17:57:44262 suppress_javascript_messages_(false),
[email protected]7ab1e7d62009-10-14 23:32:01263 is_showing_before_unload_dialog_(false),
[email protected]7ff431e2010-01-07 18:18:54264 renderer_preferences_(),
[email protected]c2e74fe82009-09-03 17:57:44265 opener_dom_ui_type_(DOMUIFactory::kNoDOMUI) {
[email protected]7ff431e2010-01-07 18:18:54266 renderer_preferences_util::UpdateFromSystemSettings(
267 &renderer_preferences_, profile);
268
[email protected]16779842009-07-08 23:45:29269#if defined(OS_CHROMEOS)
[email protected]58dca552009-06-17 00:35:02270 // Make sure the thumbnailer is started before starting the render manager.
271 // The thumbnailer will want to listen for RVH creations, one of which will
272 // happen in RVHManager::Init.
273 ThumbnailGenerator* generator = g_browser_process->GetThumbnailGenerator();
274 if (generator)
275 generator->StartThumbnailing();
276#endif
277
[email protected]12636df2009-09-28 22:32:21278 render_manager_.Init(profile, site_instance, routing_id);
[email protected]420ae012009-04-24 05:16:32279
[email protected]34ac70502009-09-25 17:07:23280 // We have the initial size of the view be based on the size of the passed in
281 // tab contents (normally a tab from the same window).
282 view_->CreateView(base_tab_contents ?
283 base_tab_contents->view()->GetContainerSize() : gfx::Size());
[email protected]420ae012009-04-24 05:16:32284
285 // Register for notifications about all interested prefs change.
286 PrefService* prefs = profile->GetPrefs();
287 if (prefs) {
288 for (int i = 0; i < kPrefsToObserveLength; ++i)
289 prefs->AddPrefObserver(kPrefsToObserve[i], this);
290 }
291
292 // Register for notifications about URL starredness changing on any profile.
293 registrar_.Add(this, NotificationType::URLS_STARRED,
294 NotificationService::AllSources());
295 registrar_.Add(this, NotificationType::BOOKMARK_MODEL_LOADED,
296 NotificationService::AllSources());
297 registrar_.Add(this, NotificationType::RENDER_WIDGET_HOST_DESTROYED,
298 NotificationService::AllSources());
[email protected]d3ba77272009-09-03 00:06:09299#if defined(OS_LINUX)
300 registrar_.Add(this, NotificationType::BROWSER_THEME_CHANGED,
301 NotificationService::AllSources());
302#endif
[email protected]420ae012009-04-24 05:16:32303
304 // Keep a global copy of the previous search string (if any).
305 static string16 global_last_search = string16();
[email protected]e491f1c2009-05-22 20:28:12306 last_search_prepopulate_text_ = &global_last_search;
[email protected]840b1512009-07-21 16:53:46307
[email protected]c8865482009-07-23 20:40:10308 // Set-up the showing of the omnibox search infobar if applicable.
309 if (OmniboxSearchHint::IsEnabled(profile))
310 omnibox_search_hint_.reset(new OmniboxSearchHint(this));
[email protected]332af7732009-03-11 13:21:35311}
initial.commit09911bf2008-07-26 23:55:29312
313TabContents::~TabContents() {
[email protected]420ae012009-04-24 05:16:32314 is_being_destroyed_ = true;
315
[email protected]bfe4c1582009-10-02 18:11:09316 // We don't want any notifications while we're running our destructor.
[email protected]420ae012009-04-24 05:16:32317 registrar_.RemoveAll();
318
319 // Unregister the notifications of all observed prefs change.
320 PrefService* prefs = profile()->GetPrefs();
321 if (prefs) {
322 for (int i = 0; i < kPrefsToObserveLength; ++i)
323 prefs->RemovePrefObserver(kPrefsToObserve[i], this);
324 }
325
[email protected]420ae012009-04-24 05:16:32326 NotifyDisconnected();
[email protected]e7b8d832009-11-20 22:41:00327 hung_renderer_dialog::HideForTabContents(this);
[email protected]420ae012009-04-24 05:16:32328
[email protected]420ae012009-04-24 05:16:32329 // First cleanly close all child windows.
330 // TODO(mpcomplete): handle case if MaybeCloseChildWindows() already asked
331 // some of these to close. CloseWindows is async, so it might get called
332 // twice before it runs.
333 int size = static_cast<int>(child_windows_.size());
334 for (int i = size - 1; i >= 0; --i) {
335 ConstrainedWindow* window = child_windows_[i];
[email protected]fa1cf0b82010-01-15 21:49:44336 if (window) {
[email protected]420ae012009-04-24 05:16:32337 window->CloseConstrainedWindow();
[email protected]fa1cf0b82010-01-15 21:49:44338 BlockTabContent(false);
339 }
[email protected]420ae012009-04-24 05:16:32340 }
341
[email protected]66ba4932009-06-04 19:22:13342 if (blocked_popups_)
343 blocked_popups_->Destroy();
344
[email protected]420ae012009-04-24 05:16:32345 // Notify any observer that have a reference on this tab contents.
346 NotificationService::current()->Notify(
347 NotificationType::TAB_CONTENTS_DESTROYED,
348 Source<TabContents>(this),
349 NotificationService::NoDetails());
350
[email protected]1e5e74f2009-05-27 20:55:12351 // Notify any lasting InfobarDelegates that have not yet been removed that
352 // whatever infobar they were handling in this TabContents has closed,
353 // because the TabContents is going away entirely.
354 // This must happen after the TAB_CONTENTS_DESTROYED notification as the
355 // notification may trigger infobars calls that access their delegate. (and
356 // some implementations of InfoBarDelegate do delete themselves on
357 // InfoBarClosed()).
358 for (int i = 0; i < infobar_delegate_count(); ++i) {
359 InfoBarDelegate* delegate = GetInfoBarDelegateAt(i);
360 delegate->InfoBarClosed();
361 }
362 infobar_delegates_.clear();
363
[email protected]420ae012009-04-24 05:16:32364 // TODO(brettw) this should be moved to the view.
365#if defined(OS_WIN)
366 // If we still have a window handle, destroy it. GetNativeView can return
367 // NULL if this contents was part of a window that closed.
368 if (GetNativeView())
369 ::DestroyWindow(GetNativeView());
370#endif
[email protected]7ab1e7d62009-10-14 23:32:01371
372 // OnCloseStarted isn't called in unit tests.
373 if (!tab_close_start_time_.is_null()) {
374 UMA_HISTOGRAM_TIMES("Tab.Close",
375 base::TimeTicks::Now() - tab_close_start_time_);
376 }
initial.commit09911bf2008-07-26 23:55:29377}
378
[email protected]d5f942ba2008-09-26 19:30:34379// static
380void TabContents::RegisterUserPrefs(PrefService* prefs) {
[email protected]420ae012009-04-24 05:16:32381 prefs->RegisterBooleanPref(prefs::kAlternateErrorPagesEnabled, true);
382
383 WebPreferences pref_defaults;
384 prefs->RegisterBooleanPref(prefs::kWebKitJavascriptEnabled,
385 pref_defaults.javascript_enabled);
386 prefs->RegisterBooleanPref(prefs::kWebKitWebSecurityEnabled,
387 pref_defaults.web_security_enabled);
388 prefs->RegisterBooleanPref(
389 prefs::kWebKitJavascriptCanOpenWindowsAutomatically, true);
390 prefs->RegisterBooleanPref(prefs::kWebKitLoadsImagesAutomatically,
391 pref_defaults.loads_images_automatically);
392 prefs->RegisterBooleanPref(prefs::kWebKitPluginsEnabled,
393 pref_defaults.plugins_enabled);
394 prefs->RegisterBooleanPref(prefs::kWebKitDomPasteEnabled,
395 pref_defaults.dom_paste_enabled);
396 prefs->RegisterBooleanPref(prefs::kWebKitShrinksStandaloneImagesToFit,
397 pref_defaults.shrinks_standalone_images_to_fit);
[email protected]0df30122009-06-03 12:13:08398 prefs->RegisterStringPref(prefs::kWebKitInspectorSettings,
[email protected]8922e1f2009-10-03 05:01:26399 UTF8ToWide(pref_defaults.inspector_settings));
[email protected]420ae012009-04-24 05:16:32400 prefs->RegisterBooleanPref(prefs::kWebKitTextAreasAreResizable,
401 pref_defaults.text_areas_are_resizable);
402 prefs->RegisterBooleanPref(prefs::kWebKitJavaEnabled,
403 pref_defaults.java_enabled);
404
405 prefs->RegisterLocalizedStringPref(prefs::kAcceptLanguages,
406 IDS_ACCEPT_LANGUAGES);
407 prefs->RegisterLocalizedStringPref(prefs::kDefaultCharset,
408 IDS_DEFAULT_ENCODING);
409 prefs->RegisterLocalizedBooleanPref(prefs::kWebKitStandardFontIsSerif,
410 IDS_STANDARD_FONT_IS_SERIF);
411 prefs->RegisterLocalizedStringPref(prefs::kWebKitFixedFontFamily,
412 IDS_FIXED_FONT_FAMILY);
413 prefs->RegisterLocalizedStringPref(prefs::kWebKitSerifFontFamily,
414 IDS_SERIF_FONT_FAMILY);
415 prefs->RegisterLocalizedStringPref(prefs::kWebKitSansSerifFontFamily,
416 IDS_SANS_SERIF_FONT_FAMILY);
417 prefs->RegisterLocalizedStringPref(prefs::kWebKitCursiveFontFamily,
418 IDS_CURSIVE_FONT_FAMILY);
419 prefs->RegisterLocalizedStringPref(prefs::kWebKitFantasyFontFamily,
420 IDS_FANTASY_FONT_FAMILY);
421 prefs->RegisterLocalizedIntegerPref(prefs::kWebKitDefaultFontSize,
422 IDS_DEFAULT_FONT_SIZE);
423 prefs->RegisterLocalizedIntegerPref(prefs::kWebKitDefaultFixedFontSize,
424 IDS_DEFAULT_FIXED_FONT_SIZE);
425 prefs->RegisterLocalizedIntegerPref(prefs::kWebKitMinimumFontSize,
426 IDS_MINIMUM_FONT_SIZE);
427 prefs->RegisterLocalizedIntegerPref(prefs::kWebKitMinimumLogicalFontSize,
428 IDS_MINIMUM_LOGICAL_FONT_SIZE);
429 prefs->RegisterLocalizedBooleanPref(prefs::kWebKitUsesUniversalDetector,
430 IDS_USES_UNIVERSAL_DETECTOR);
431 prefs->RegisterLocalizedStringPref(prefs::kStaticEncodings,
432 IDS_STATIC_ENCODING_LIST);
initial.commit09911bf2008-07-26 23:55:29433}
434
[email protected]13367f72009-07-08 16:39:02435// Returns true if contains content rendered by an extension.
436bool TabContents::HostsExtension() const {
437 return GetURL().SchemeIs(chrome::kExtensionScheme);
[email protected]d5f942ba2008-09-26 19:30:34438}
439
[email protected]6e95706d2009-04-23 22:55:01440PasswordManager* TabContents::GetPasswordManager() {
441 if (password_manager_.get() == NULL)
442 password_manager_.reset(new PasswordManager(this));
443 return password_manager_.get();
444}
445
446PluginInstaller* TabContents::GetPluginInstaller() {
447 if (plugin_installer_.get() == NULL)
448 plugin_installer_.reset(new PluginInstaller(this));
449 return plugin_installer_.get();
450}
451
[email protected]d5f942ba2008-09-26 19:30:34452const GURL& TabContents::GetURL() const {
453 // We may not have a navigation entry yet
[email protected]ce3fa3c2009-04-20 19:55:57454 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]ebe89e062009-08-13 23:16:54455 return entry ? entry->virtual_url() : GURL::EmptyGURL();
[email protected]d5f942ba2008-09-26 19:30:34456}
457
[email protected]96d185d2009-04-24 03:28:54458const string16& TabContents::GetTitle() const {
[email protected]4c6092c5b2009-06-06 00:23:55459 // Transient entries take precedence. They are used for interstitial pages
460 // that are shown on top of existing pages.
461 NavigationEntry* entry = controller_.GetTransientEntry();
462 if (entry)
463 return entry->GetTitleForDisplay(&controller_);
464
[email protected]96d185d2009-04-24 03:28:54465 DOMUI* our_dom_ui = render_manager_.pending_dom_ui() ?
466 render_manager_.pending_dom_ui() : render_manager_.dom_ui();
467 if (our_dom_ui) {
468 // Don't override the title in view source mode.
[email protected]4c6092c5b2009-06-06 00:23:55469 entry = controller_.GetActiveEntry();
[email protected]96d185d2009-04-24 03:28:54470 if (!(entry && entry->IsViewSourceMode())) {
471 // Give the DOM UI the chance to override our title.
472 const string16& title = our_dom_ui->overridden_title();
473 if (!title.empty())
474 return title;
475 }
476 }
477
478 // We use the title for the last committed entry rather than a pending
479 // navigation entry. For example, when the user types in a URL, we want to
480 // keep the old page's title until the new load has committed and we get a new
481 // title.
[email protected]96d185d2009-04-24 03:28:54482 entry = controller_.GetLastCommittedEntry();
483 if (entry)
484 return entry->GetTitleForDisplay(&controller_);
[email protected]96d185d2009-04-24 03:28:54485 return EmptyString16();
486}
487
[email protected]d5f942ba2008-09-26 19:30:34488int32 TabContents::GetMaxPageID() {
489 if (GetSiteInstance())
490 return GetSiteInstance()->max_page_id();
491 else
492 return max_page_id_;
493}
494
495void TabContents::UpdateMaxPageID(int32 page_id) {
496 // Ensure both the SiteInstance and RenderProcessHost update their max page
[email protected]57c6a652009-05-04 07:58:34497 // IDs in sync. Only TabContents will also have site instances, except during
[email protected]d5f942ba2008-09-26 19:30:34498 // testing.
499 if (GetSiteInstance())
500 GetSiteInstance()->UpdateMaxPageID(page_id);
[email protected]57c6a652009-05-04 07:58:34501 process()->UpdateMaxPageID(page_id);
[email protected]d5f942ba2008-09-26 19:30:34502}
503
[email protected]96d185d2009-04-24 03:28:54504SiteInstance* TabContents::GetSiteInstance() const {
505 return render_manager_.current_host()->site_instance();
506}
507
[email protected]d5f942ba2008-09-26 19:30:34508const std::wstring TabContents::GetDefaultTitle() const {
509 return l10n_util::GetString(IDS_DEFAULT_TAB_TITLE);
510}
511
[email protected]96d185d2009-04-24 03:28:54512bool TabContents::ShouldDisplayURL() {
[email protected]8ab24cc2009-11-13 20:56:15513 // Don't hide the url in view source mode and with interstitials.
[email protected]96d185d2009-04-24 03:28:54514 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]8ab24cc2009-11-13 20:56:15515 if (entry && (entry->IsViewSourceMode() ||
516 entry->page_type() == NavigationEntry::INTERSTITIAL_PAGE)) {
[email protected]96d185d2009-04-24 03:28:54517 return true;
[email protected]8ab24cc2009-11-13 20:56:15518 }
[email protected]83e3895b2009-06-11 00:07:16519
[email protected]96d185d2009-04-24 03:28:54520 DOMUI* dom_ui = GetDOMUIForCurrentState();
521 if (dom_ui)
522 return !dom_ui->should_hide_url();
523 return true;
524}
525
[email protected]d5f942ba2008-09-26 19:30:34526SkBitmap TabContents::GetFavIcon() const {
527 // Like GetTitle(), we also want to use the favicon for the last committed
528 // entry rather than a pending navigation entry.
[email protected]ce3fa3c2009-04-20 19:55:57529 NavigationEntry* entry = controller_.GetTransientEntry();
[email protected]cbab76d2008-10-13 22:42:47530 if (entry)
531 return entry->favicon().bitmap();
532
[email protected]ce3fa3c2009-04-20 19:55:57533 entry = controller_.GetLastCommittedEntry();
[email protected]d5f942ba2008-09-26 19:30:34534 if (entry)
535 return entry->favicon().bitmap();
[email protected]d5f942ba2008-09-26 19:30:34536 return SkBitmap();
537}
538
[email protected]0b303cc2009-09-28 22:35:15539bool TabContents::FavIconIsValid() const {
540 NavigationEntry* entry = controller_.GetTransientEntry();
541 if (entry)
542 return entry->favicon().is_valid();
543
544 entry = controller_.GetLastCommittedEntry();
545 if (entry)
546 return entry->favicon().is_valid();
547
548 return false;
549}
550
[email protected]96d185d2009-04-24 03:28:54551bool TabContents::ShouldDisplayFavIcon() {
552 // Always display a throbber during pending loads.
553 if (controller_.GetLastCommittedEntry() && controller_.pending_entry())
554 return true;
555
556 DOMUI* dom_ui = GetDOMUIForCurrentState();
557 if (dom_ui)
558 return !dom_ui->hide_favicon();
559 return true;
560}
561
[email protected]96d185d2009-04-24 03:28:54562std::wstring TabContents::GetStatusText() const {
563 if (!is_loading() || load_state_ == net::LOAD_STATE_IDLE)
564 return std::wstring();
565
566 switch (load_state_) {
567 case net::LOAD_STATE_WAITING_FOR_CACHE:
568 return l10n_util::GetString(IDS_LOAD_STATE_WAITING_FOR_CACHE);
569 case net::LOAD_STATE_RESOLVING_PROXY_FOR_URL:
570 return l10n_util::GetString(IDS_LOAD_STATE_RESOLVING_PROXY_FOR_URL);
571 case net::LOAD_STATE_RESOLVING_HOST:
572 return l10n_util::GetString(IDS_LOAD_STATE_RESOLVING_HOST);
573 case net::LOAD_STATE_CONNECTING:
574 return l10n_util::GetString(IDS_LOAD_STATE_CONNECTING);
575 case net::LOAD_STATE_SENDING_REQUEST:
[email protected]094e5b22009-09-25 04:23:56576 if (upload_size_)
577 return l10n_util::GetStringF(
578 IDS_LOAD_STATE_SENDING_REQUEST_WITH_PROGRESS,
579 static_cast<int>((100 * upload_position_) / upload_size_));
580 else
581 return l10n_util::GetString(IDS_LOAD_STATE_SENDING_REQUEST);
[email protected]96d185d2009-04-24 03:28:54582 case net::LOAD_STATE_WAITING_FOR_RESPONSE:
583 return l10n_util::GetStringF(IDS_LOAD_STATE_WAITING_FOR_RESPONSE,
584 load_state_host_);
585 // Ignore net::LOAD_STATE_READING_RESPONSE and net::LOAD_STATE_IDLE
586 case net::LOAD_STATE_IDLE:
587 case net::LOAD_STATE_READING_RESPONSE:
588 break;
589 }
590
591 return std::wstring();
592}
593
[email protected]d5f942ba2008-09-26 19:30:34594void TabContents::SetIsCrashed(bool state) {
595 if (state == is_crashed_)
596 return;
597
598 is_crashed_ = state;
[email protected]c9cd2222009-05-06 05:16:50599 NotifyNavigationStateChanged(INVALIDATE_TAB);
[email protected]d5f942ba2008-09-26 19:30:34600}
601
[email protected]744ef172009-10-16 21:53:46602void TabContents::PageActionStateChanged() {
603 NotifyNavigationStateChanged(TabContents::INVALIDATE_PAGE_ACTIONS);
[email protected]d7eaf572009-07-01 21:57:00604}
[email protected]ce5c4502009-05-06 16:46:11605
[email protected]d5f942ba2008-09-26 19:30:34606void TabContents::NotifyNavigationStateChanged(unsigned changed_flags) {
607 if (delegate_)
608 delegate_->NavigationStateChanged(this, changed_flags);
609}
610
[email protected]96d185d2009-04-24 03:28:54611void TabContents::DidBecomeSelected() {
612 controller_.SetActive(true);
[email protected]789e9152009-08-04 21:59:43613 if (render_widget_host_view()) {
[email protected]96d185d2009-04-24 03:28:54614 render_widget_host_view()->DidBecomeSelected();
[email protected]789e9152009-08-04 21:59:43615#if defined(OS_MACOSX)
616 render_widget_host_view()->SetActive(true);
617#endif
618 }
[email protected]96d185d2009-04-24 03:28:54619
[email protected]76543b92009-08-31 17:27:45620 WebCacheManager::GetInstance()->ObserveActivity(process()->id());
[email protected]96d185d2009-04-24 03:28:54621}
622
623void TabContents::WasHidden() {
624 if (!capturing_contents()) {
625 // |render_view_host()| can be NULL if the user middle clicks a link to open
626 // a tab in then background, then closes the tab before selecting it. This
627 // is because closing the tab calls TabContents::Destroy(), which removes
628 // the |render_view_host()|; then when we actually destroy the window,
629 // OnWindowPosChanged() notices and calls HideContents() (which calls us).
630 if (render_widget_host_view())
631 render_widget_host_view()->WasHidden();
[email protected]96d185d2009-04-24 03:28:54632 }
633
634 NotificationService::current()->Notify(
635 NotificationType::TAB_CONTENTS_HIDDEN,
636 Source<TabContents>(this),
637 NotificationService::NoDetails());
638}
639
[email protected]d5f942ba2008-09-26 19:30:34640void TabContents::Activate() {
641 if (delegate_)
642 delegate_->ActivateContents(this);
643}
644
[email protected]96d185d2009-04-24 03:28:54645void TabContents::ShowContents() {
646 if (render_widget_host_view())
647 render_widget_host_view()->DidBecomeSelected();
[email protected]96d185d2009-04-24 03:28:54648}
649
650void TabContents::HideContents() {
651 // TODO(pkasting): http://b/1239839 Right now we purposefully don't call
652 // our superclass HideContents(), because some callers want to be very picky
653 // about the order in which these get called. In addition to making the code
654 // here practically impossible to understand, this also means we end up
655 // calling TabContents::WasHidden() twice if callers call both versions of
[email protected]57c6a652009-05-04 07:58:34656 // HideContents() on a TabContents.
[email protected]96d185d2009-04-24 03:28:54657 WasHidden();
658}
659
[email protected]c0588052008-10-27 23:01:50660void TabContents::OpenURL(const GURL& url, const GURL& referrer,
[email protected]d5f942ba2008-09-26 19:30:34661 WindowOpenDisposition disposition,
662 PageTransition::Type transition) {
663 if (delegate_)
[email protected]c0588052008-10-27 23:01:50664 delegate_->OpenURLFromTab(this, url, referrer, disposition, transition);
[email protected]d5f942ba2008-09-26 19:30:34665}
666
667bool TabContents::NavigateToPendingEntry(bool reload) {
[email protected]96d185d2009-04-24 03:28:54668 const NavigationEntry& entry = *controller_.pending_entry();
669
670 RenderViewHost* dest_render_view_host = render_manager_.Navigate(entry);
671 if (!dest_render_view_host)
672 return false; // Unable to create the desired render view host.
673
674 // Tell DevTools agent that it is attached prior to the navigation.
[email protected]d9f9b792009-06-24 13:17:12675 DevToolsManager* devtools_manager = DevToolsManager::GetInstance();
[email protected]7e8e6b62009-05-08 11:28:32676 if (devtools_manager) { // NULL in unit tests.
[email protected]684b4ba22009-05-07 06:27:41677 devtools_manager->OnNavigatingToPendingEntry(
678 render_view_host(),
679 dest_render_view_host,
680 controller_.pending_entry()->url());
[email protected]7e8e6b62009-05-08 11:28:32681 }
[email protected]96d185d2009-04-24 03:28:54682
683 // Used for page load time metrics.
684 current_load_start_ = base::TimeTicks::Now();
685
686 // Navigate in the desired RenderViewHost.
[email protected]056de2d2009-06-26 16:41:34687 ViewMsg_Navigate_Params navigate_params;
[email protected]5e369672009-11-03 23:48:30688 MakeNavigateParams(profile(), entry, reload, &navigate_params);
[email protected]056de2d2009-06-26 16:41:34689 dest_render_view_host->Navigate(navigate_params);
[email protected]96d185d2009-04-24 03:28:54690
691 if (entry.page_id() == -1) {
692 // HACK!! This code suppresses javascript: URLs from being added to
693 // session history, which is what we want to do for javascript: URLs that
694 // do not generate content. What we really need is a message from the
695 // renderer telling us that a new page was not created. The same message
696 // could be used for mailto: URLs and the like.
697 if (entry.url().SchemeIs(chrome::kJavaScriptScheme))
698 return false;
699 }
700
701 // Clear any provisional password saves - this stops password infobars
702 // showing up on pages the user navigates to while the right page is
703 // loading.
704 GetPasswordManager()->ClearProvisionalSave();
705
706 if (reload && !profile()->IsOffTheRecord()) {
[email protected]0189bc722009-08-28 21:56:48707 FaviconService* favicon_service =
708 profile()->GetFaviconService(Profile::IMPLICIT_ACCESS);
709 if (favicon_service)
710 favicon_service->SetFaviconOutOfDateForPage(entry.url());
[email protected]96d185d2009-04-24 03:28:54711 }
712
[email protected]d5f942ba2008-09-26 19:30:34713 return true;
714}
715
[email protected]96d185d2009-04-24 03:28:54716void TabContents::Stop() {
717 render_manager_.Stop();
718 printing_.Stop();
719}
720
721void TabContents::Cut() {
722 render_view_host()->Cut();
723}
724
725void TabContents::Copy() {
726 render_view_host()->Copy();
727}
728
[email protected]c0cc3092009-09-12 08:27:27729#if defined(OS_MACOSX)
[email protected]a954bf72009-09-12 07:30:35730void TabContents::CopyToFindPboard() {
731 render_view_host()->CopyToFindPboard();
732}
[email protected]c0cc3092009-09-12 08:27:27733#endif
[email protected]a954bf72009-09-12 07:30:35734
[email protected]96d185d2009-04-24 03:28:54735void TabContents::Paste() {
736 render_view_host()->Paste();
737}
738
[email protected]7e786822009-11-07 00:00:27739#if defined(OS_MACOSX)
740void TabContents::PasteAndMatchStyle() {
741 render_view_host()->ForwardEditCommand("PasteAndMatchStyle", "");
742}
743#endif
744
[email protected]96d185d2009-04-24 03:28:54745void TabContents::DisassociateFromPopupCount() {
746 render_view_host()->DisassociateFromPopupCount();
747}
748
749TabContents* TabContents::Clone() {
750 // We create a new SiteInstance so that the new tab won't share processes
751 // with the old one. This can be changed in the future if we need it to share
752 // processes for some reason.
[email protected]420ae012009-04-24 05:16:32753 TabContents* tc = new TabContents(profile(),
[email protected]96d185d2009-04-24 03:28:54754 SiteInstance::CreateSiteInstance(profile()),
[email protected]12636df2009-09-28 22:32:21755 MSG_ROUTING_NONE, this);
[email protected]96d185d2009-04-24 03:28:54756 tc->controller().CopyStateFrom(controller_);
757 return tc;
758}
759
[email protected]4d677202009-07-19 07:37:12760void TabContents::ShowPageInfo(const GURL& url,
761 const NavigationEntry::SSLStatus& ssl,
762 bool show_history) {
763 if (!delegate_)
764 return;
765
[email protected]bb678332009-07-21 00:15:50766 delegate_->ShowPageInfo(profile(), url, ssl, show_history);
[email protected]4d677202009-07-19 07:37:12767}
768
initial.commit09911bf2008-07-26 23:55:29769ConstrainedWindow* TabContents::CreateConstrainedDialog(
[email protected]e8382172009-06-19 22:16:28770 ConstrainedWindowDelegate* delegate) {
initial.commit09911bf2008-07-26 23:55:29771 ConstrainedWindow* window =
[email protected]e8382172009-06-19 22:16:28772 ConstrainedWindow::CreateConstrainedDialog(this, delegate);
initial.commit09911bf2008-07-26 23:55:29773 child_windows_.push_back(window);
[email protected]fa1cf0b82010-01-15 21:49:44774
775 if (child_windows_.size() == 1) {
776 window->ShowConstrainedWindow();
777 BlockTabContent(true);
778 }
779
initial.commit09911bf2008-07-26 23:55:29780 return window;
781}
782
[email protected]fa1cf0b82010-01-15 21:49:44783void TabContents::BlockTabContent(bool blocked) {
784 render_view_host()->set_ignore_input_events(blocked);
785 if (delegate_)
786 delegate_->SetTabContentBlocked(this, blocked);
787}
788
initial.commit09911bf2008-07-26 23:55:29789void TabContents::AddNewContents(TabContents* new_contents,
790 WindowOpenDisposition disposition,
791 const gfx::Rect& initial_pos,
[email protected]c88a70fe2009-05-05 20:00:22792 bool user_gesture,
793 const GURL& creator_url) {
initial.commit09911bf2008-07-26 23:55:29794 if (!delegate_)
795 return;
796
[email protected]35f7d212009-04-29 21:19:27797 if ((disposition == NEW_POPUP) && !user_gesture &&
798 !CommandLine::ForCurrentProcess()->HasSwitch(
799 switches::kDisablePopupBlocking)) {
[email protected]2c4410d2009-05-06 23:46:22800 // Unrequested popups from normal pages are constrained unless they're in
[email protected]b6c874582009-05-08 19:38:31801 // the whitelist. The popup owner will handle checking this.
802 delegate_->GetConstrainingContents(this)->AddPopup(new_contents,
803 initial_pos,
804 creator_url.is_valid() ? creator_url.host() : std::string());
805 } else {
[email protected]304f8792009-08-19 21:28:09806#if defined(OS_CHROMEOS)
807 if (disposition == NEW_POPUP) {
808 // If the popup is bigger than a given factor of the screen, then
809 // turn it into a foreground tab (on chrome os only)
810 GdkScreen* screen = gdk_screen_get_default();
811 int max_width = gdk_screen_get_width(screen) * kMaxWidthFactor;
812 int max_height = gdk_screen_get_height(screen) * kMaxHeightFactor;
813 if (initial_pos.width() > max_width ||
814 initial_pos.height() > max_height) {
815 disposition = NEW_FOREGROUND_TAB;
816 }
817 }
818#endif
819
[email protected]0aa55312008-10-17 21:53:08820 new_contents->DisassociateFromPopupCount();
initial.commit09911bf2008-07-26 23:55:29821 delegate_->AddNewContents(this, new_contents, disposition, initial_pos,
822 user_gesture);
[email protected]7f37dd92009-09-15 16:35:49823 NotificationService::current()->Notify(
824 NotificationType::TAB_ADDED,
825 Source<TabContentsDelegate>(delegate_),
826 Details<TabContents>(this));
initial.commit09911bf2008-07-26 23:55:29827 }
[email protected]b6c874582009-05-08 19:38:31828 PopupNotificationVisibilityChanged(ShowingBlockedPopupNotification());
initial.commit09911bf2008-07-26 23:55:29829}
830
initial.commit09911bf2008-07-26 23:55:29831void TabContents::CloseAllSuppressedPopups() {
[email protected]d6598c052008-11-05 19:03:25832 if (blocked_popups_)
[email protected]2c4410d2009-05-06 23:46:22833 blocked_popups_->CloseAll();
initial.commit09911bf2008-07-26 23:55:29834}
835
[email protected]bcd9dc42010-01-23 16:38:18836bool TabContents::ExecuteCode(int request_id, const std::string& extension_id,
[email protected]61f5a7b2009-12-22 22:21:20837 const std::vector<URLPattern>& host_permissions,
[email protected]20ad2692009-11-20 18:27:20838 bool is_js_code, const std::string& code_string,
839 bool all_frames) {
[email protected]bcd9dc42010-01-23 16:38:18840 return render_view_host()->Send(
[email protected]61f5a7b2009-12-22 22:21:20841 new ViewMsg_ExecuteCode(render_view_host()->routing_id(),
842 ViewMsg_ExecuteCode_Params(request_id, extension_id, host_permissions,
843 is_js_code, code_string, all_frames)));
[email protected]912256b32009-09-18 09:47:35844}
845
[email protected]96d185d2009-04-24 03:28:54846void TabContents::PopupNotificationVisibilityChanged(bool visible) {
847 render_view_host()->PopupNotificationVisibilityChanged(visible);
848}
849
[email protected]5c9e97a2009-09-09 23:48:30850gfx::NativeView TabContents::GetContentNativeView() const {
[email protected]96d185d2009-04-24 03:28:54851 return view_->GetContentNativeView();
852}
853
854gfx::NativeView TabContents::GetNativeView() const {
855 return view_->GetNativeView();
856}
857
858void TabContents::GetContainerBounds(gfx::Rect *out) const {
859 view_->GetContainerBounds(out);
860}
861
862void TabContents::Focus() {
863 view_->Focus();
864}
865
[email protected]90daadb42009-06-08 21:27:28866void TabContents::FocusThroughTabTraversal(bool reverse) {
[email protected]7e383692009-06-12 19:14:54867 if (showing_interstitial_page()) {
868 render_manager_.interstitial_page()->FocusThroughTabTraversal(reverse);
869 return;
870 }
[email protected]96d185d2009-04-24 03:28:54871 render_view_host()->SetInitialFocus(reverse);
872}
873
874bool TabContents::FocusLocationBarByDefault() {
875 DOMUI* dom_ui = GetDOMUIForCurrentState();
876 if (dom_ui)
877 return dom_ui->focus_location_bar_by_default();
[email protected]3e3f0eb2009-06-22 18:33:43878 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]e0d481582009-09-15 21:06:25879 if (entry && entry->url() == GURL(chrome::kAboutBlankURL))
[email protected]3e3f0eb2009-06-22 18:33:43880 return true;
[email protected]96d185d2009-04-24 03:28:54881 return false;
882}
883
[email protected]616ed5a2008-11-21 22:27:24884void TabContents::AddInfoBar(InfoBarDelegate* delegate) {
885 // Look through the existing InfoBarDelegates we have for a match. If we've
886 // already got one that matches, then we don't add the new one.
[email protected]f86a07022008-11-25 01:06:05887 for (int i = 0; i < infobar_delegate_count(); ++i) {
[email protected]6d27a7b2009-12-18 23:25:45888 if (GetInfoBarDelegateAt(i)->EqualsDelegate(delegate)) {
889 // Tell the new infobar to close so that it can clean itself up.
890 delegate->InfoBarClosed();
[email protected]616ed5a2008-11-21 22:27:24891 return;
[email protected]6d27a7b2009-12-18 23:25:45892 }
[email protected]616ed5a2008-11-21 22:27:24893 }
894
895 infobar_delegates_.push_back(delegate);
[email protected]bfd04a62009-02-01 18:16:56896 NotificationService::current()->Notify(
897 NotificationType::TAB_CONTENTS_INFOBAR_ADDED,
898 Source<TabContents>(this),
899 Details<InfoBarDelegate>(delegate));
[email protected]616ed5a2008-11-21 22:27:24900
901 // Add ourselves as an observer for navigations the first time a delegate is
902 // added. We use this notification to expire InfoBars that need to expire on
903 // page transitions.
904 if (infobar_delegates_.size() == 1) {
[email protected]bfd04a62009-02-01 18:16:56905 registrar_.Add(this, NotificationType::NAV_ENTRY_COMMITTED,
[email protected]ce3fa3c2009-04-20 19:55:57906 Source<NavigationController>(&controller_));
[email protected]616ed5a2008-11-21 22:27:24907 }
908}
909
910void TabContents::RemoveInfoBar(InfoBarDelegate* delegate) {
911 std::vector<InfoBarDelegate*>::iterator it =
912 find(infobar_delegates_.begin(), infobar_delegates_.end(), delegate);
913 if (it != infobar_delegates_.end()) {
914 InfoBarDelegate* delegate = *it;
[email protected]bfd04a62009-02-01 18:16:56915 NotificationService::current()->Notify(
916 NotificationType::TAB_CONTENTS_INFOBAR_REMOVED,
917 Source<TabContents>(this),
918 Details<InfoBarDelegate>(delegate));
[email protected]f86a07022008-11-25 01:06:05919 infobar_delegates_.erase(it);
[email protected]616ed5a2008-11-21 22:27:24920
[email protected]6a02963e2009-01-06 16:58:03921 // Remove ourselves as an observer if we are tracking no more InfoBars.
922 if (infobar_delegates_.empty()) {
[email protected]bfd04a62009-02-01 18:16:56923 registrar_.Remove(this, NotificationType::NAV_ENTRY_COMMITTED,
[email protected]ce3fa3c2009-04-20 19:55:57924 Source<NavigationController>(&controller_));
[email protected]6a02963e2009-01-06 16:58:03925 }
[email protected]616ed5a2008-11-21 22:27:24926 }
927}
[email protected]3edd9522009-03-04 22:19:41928
[email protected]f81821c2009-08-07 01:29:47929void TabContents::ReplaceInfoBar(InfoBarDelegate* old_delegate,
930 InfoBarDelegate* new_delegate) {
931 std::vector<InfoBarDelegate*>::iterator it =
932 find(infobar_delegates_.begin(), infobar_delegates_.end(), old_delegate);
933 DCHECK(it != infobar_delegates_.end());
934
935 // Notify the container about the change of plans.
936 scoped_ptr<std::pair<InfoBarDelegate*, InfoBarDelegate*> > details(
937 new std::pair<InfoBarDelegate*, InfoBarDelegate*>(
938 old_delegate, new_delegate));
939 NotificationService::current()->Notify(
940 NotificationType::TAB_CONTENTS_INFOBAR_REPLACED,
941 Source<TabContents>(this),
942 Details<std::pair<InfoBarDelegate*, InfoBarDelegate*> >(details.get()));
943
944 // Remove the old one.
945 infobar_delegates_.erase(it);
946
947 // Add the new one.
948 infobar_delegates_.push_back(new_delegate);
949}
950
[email protected]20ede072009-10-06 00:28:35951bool TabContents::ShouldShowBookmarkBar() {
[email protected]aa9196f2009-10-22 16:20:27952 if (showing_interstitial_page())
953 return false;
954
[email protected]96d185d2009-04-24 03:28:54955 // See GetDOMUIForCurrentState() comment for more info. This case is very
956 // similar, but for non-first loads, we want to use the committed entry. This
957 // is so the bookmarks bar disappears at the same time the page does.
958 if (controller_.GetLastCommittedEntry()) {
959 // Not the first load, always use the committed DOM UI.
960 if (render_manager_.dom_ui())
961 return render_manager_.dom_ui()->force_bookmark_bar_visible();
962 return false; // Default.
963 }
964
965 // When it's the first load, we know either the pending one or the committed
966 // one will have the DOM UI in it (see GetDOMUIForCurrentState), and only one
967 // of them will be valid, so we can just check both.
968 if (render_manager_.pending_dom_ui())
969 return render_manager_.pending_dom_ui()->force_bookmark_bar_visible();
970 if (render_manager_.dom_ui())
971 return render_manager_.dom_ui()->force_bookmark_bar_visible();
972 return false; // Default.
973}
974
[email protected]d8f16ae2009-08-30 03:47:26975bool TabContents::IsExtensionShelfAlwaysVisible() {
976 // See GetDOMUIForCurrentState() comment for more info. This case is very
977 // similar, but for non-first loads, we want to use the committed entry. This
978 // is so the bookmarks bar disappears at the same time the page does.
979 if (controller_.GetLastCommittedEntry()) {
980 // Not the first load, always use the committed DOM UI.
981 if (render_manager_.dom_ui())
982 return render_manager_.dom_ui()->force_extension_shelf_visible();
983 return false; // Default.
984 }
985
986 // When it's the first load, we know either the pending one or the committed
987 // one will have the DOM UI in it (see GetDOMUIForCurrentState), and only one
988 // of them will be valid, so we can just check both.
989 if (render_manager_.pending_dom_ui())
990 return render_manager_.pending_dom_ui()->force_extension_shelf_visible();
991 if (render_manager_.dom_ui())
992 return render_manager_.dom_ui()->force_extension_shelf_visible();
993 return false; // Default.
994}
995
[email protected]3edd9522009-03-04 22:19:41996void TabContents::ToolbarSizeChanged(bool is_animating) {
997 TabContentsDelegate* d = delegate();
998 if (d)
999 d->ToolbarSizeChanged(this, is_animating);
1000}
[email protected]616ed5a2008-11-21 22:27:241001
[email protected]fc6fb7fb2009-11-07 02:35:041002bool TabContents::CanDownload(int request_id) {
1003 TabContentsDelegate* d = delegate();
1004 if (d)
1005 return d->CanDownload(request_id);
1006 return true;
1007}
1008
[email protected]d5f942ba2008-09-26 19:30:341009void TabContents::OnStartDownload(DownloadItem* download) {
1010 DCHECK(download);
[email protected]d5f942ba2008-09-26 19:30:341011
1012 // Download in a constrained popup is shown in the tab that opened it.
[email protected]b6c874582009-05-08 19:38:311013 TabContents* tab_contents = delegate()->GetConstrainingContents(this);
[email protected]d5f942ba2008-09-26 19:30:341014
[email protected]59560e0b2009-06-04 03:30:221015 if (tab_contents && tab_contents->delegate())
1016 tab_contents->delegate()->OnStartDownload(download);
[email protected]3edd9522009-03-04 22:19:411017}
[email protected]3edd9522009-03-04 22:19:411018
[email protected]d5f942ba2008-09-26 19:30:341019void TabContents::WillClose(ConstrainedWindow* window) {
1020 ConstrainedWindowList::iterator it =
1021 find(child_windows_.begin(), child_windows_.end(), window);
[email protected]fa1cf0b82010-01-15 21:49:441022 bool removed_topmost_window = it == child_windows_.begin();
[email protected]d5f942ba2008-09-26 19:30:341023 if (it != child_windows_.end())
1024 child_windows_.erase(it);
[email protected]fa1cf0b82010-01-15 21:49:441025 if (removed_topmost_window && child_windows_.size() > 0) {
1026 child_windows_[0]->ShowConstrainedWindow();
1027 BlockTabContent(true);
1028 } else {
1029 BlockTabContent(false);
1030 }
[email protected]66ba4932009-06-04 19:22:131031}
[email protected]d5f942ba2008-09-26 19:30:341032
[email protected]66ba4932009-06-04 19:22:131033void TabContents::WillCloseBlockedPopupContainer(
1034 BlockedPopupContainer* container) {
1035 DCHECK(blocked_popups_ == container);
1036 blocked_popups_ = NULL;
[email protected]d5f942ba2008-09-26 19:30:341037}
1038
[email protected]d5f942ba2008-09-26 19:30:341039void TabContents::DidMoveOrResize(ConstrainedWindow* window) {
[email protected]332af7732009-03-11 13:21:351040#if defined(OS_WIN)
[email protected]92edc472009-02-10 20:32:061041 UpdateWindow(GetNativeView());
[email protected]332af7732009-03-11 13:21:351042#endif
[email protected]d5f942ba2008-09-26 19:30:341043}
1044
[email protected]c1dc9f132009-11-20 23:13:211045void TabContents::StartFinding(string16 search_string,
[email protected]e491f1c2009-05-22 20:28:121046 bool forward_direction,
1047 bool case_sensitive) {
[email protected]c1dc9f132009-11-20 23:13:211048 // If search_string is empty, it means FindNext was pressed with a keyboard
[email protected]6e95706d2009-04-23 22:55:011049 // shortcut so unless we have something to search for we return early.
[email protected]c1dc9f132009-11-20 23:13:211050 if (search_string.empty() && find_text_.empty()) {
[email protected]87fae512009-12-11 02:20:211051 // Try the last thing we searched for on this tab, then the last thing
1052 // searched for on any tab.
1053 if (!previous_find_text_.empty())
1054 search_string = previous_find_text_;
1055 else if (!last_search_prepopulate_text_->empty())
1056 search_string = *last_search_prepopulate_text_;
1057 else
[email protected]e491f1c2009-05-22 20:28:121058 return;
[email protected]e491f1c2009-05-22 20:28:121059 }
[email protected]6e95706d2009-04-23 22:55:011060
[email protected]101c90e2009-10-26 21:14:441061 // Keep track of the previous search.
1062 previous_find_text_ = find_text_;
1063
[email protected]6e95706d2009-04-23 22:55:011064 // This is a FindNext operation if we are searching for the same text again,
1065 // or if the passed in search text is empty (FindNext keyboard shortcut). The
1066 // exception to this is if the Find was aborted (then we don't want FindNext
1067 // because the highlighting has been cleared and we need it to reappear). We
1068 // therefore treat FindNext after an aborted Find operation as a full fledged
1069 // Find.
[email protected]c1dc9f132009-11-20 23:13:211070 bool find_next = (find_text_ == search_string || search_string.empty()) &&
[email protected]e491f1c2009-05-22 20:28:121071 (last_search_case_sensitive_ == case_sensitive) &&
[email protected]6e95706d2009-04-23 22:55:011072 !find_op_aborted_;
1073 if (!find_next)
1074 current_find_request_id_ = find_request_id_counter_++;
1075
[email protected]c1dc9f132009-11-20 23:13:211076 if (!search_string.empty())
1077 find_text_ = search_string;
[email protected]e491f1c2009-05-22 20:28:121078 last_search_case_sensitive_ = case_sensitive;
[email protected]6e95706d2009-04-23 22:55:011079
1080 find_op_aborted_ = false;
1081
1082 // Keep track of what the last search was across the tabs.
[email protected]c1dc9f132009-11-20 23:13:211083 *last_search_prepopulate_text_ = find_text_;
[email protected]6e95706d2009-04-23 22:55:011084 render_view_host()->StartFinding(current_find_request_id_,
1085 find_text_,
1086 forward_direction,
[email protected]e491f1c2009-05-22 20:28:121087 case_sensitive,
[email protected]6e95706d2009-04-23 22:55:011088 find_next);
1089}
1090
1091void TabContents::StopFinding(bool clear_selection) {
[email protected]2d2dffc2009-06-09 21:41:001092 // When |clear_selection| is true, it means the find string has been cleared
1093 // by the user, but the UI has not been dismissed.
1094 if (!clear_selection)
1095 find_ui_active_ = false;
[email protected]87fae512009-12-11 02:20:211096 previous_find_text_ = find_text_;
[email protected]101c90e2009-10-26 21:14:441097 find_text_.clear();
[email protected]6e95706d2009-04-23 22:55:011098 find_op_aborted_ = true;
[email protected]e491f1c2009-05-22 20:28:121099 last_search_result_ = FindNotificationDetails();
[email protected]6e95706d2009-04-23 22:55:011100 render_view_host()->StopFinding(clear_selection);
1101}
1102
[email protected]420ae012009-04-24 05:16:321103void TabContents::OnSavePage() {
1104 // If we can not save the page, try to download it.
1105 if (!SavePackage::IsSavableContents(contents_mime_type())) {
1106 DownloadManager* dlm = profile()->GetDownloadManager();
1107 const GURL& current_page_url = GetURL();
1108 if (dlm && current_page_url.is_valid())
[email protected]d82ed61e2009-06-16 02:46:221109 dlm->DownloadUrl(current_page_url, GURL(), "", this);
[email protected]420ae012009-04-24 05:16:321110 return;
1111 }
1112
1113 Stop();
1114
1115 // Create the save package and possibly prompt the user for the name to save
1116 // the page as. The user prompt is an asynchronous operation that runs on
1117 // another thread.
[email protected]d82ed61e2009-06-16 02:46:221118 save_package_ = new SavePackage(this);
[email protected]420ae012009-04-24 05:16:321119 save_package_->GetSaveInfo();
1120}
1121
1122// Used in automated testing to bypass prompting the user for file names.
1123// Instead, the names and paths are hard coded rather than running them through
1124// file name sanitation and extension / mime checking.
[email protected]828cabe2009-09-26 22:47:111125bool TabContents::SavePage(const FilePath& main_file, const FilePath& dir_path,
[email protected]420ae012009-04-24 05:16:321126 SavePackage::SavePackageType save_type) {
1127 // Stop the page from navigating.
1128 Stop();
1129
[email protected]828cabe2009-09-26 22:47:111130 save_package_ = new SavePackage(this, save_type, main_file, dir_path);
[email protected]cdf6cc82009-08-04 21:48:591131 return save_package_->Init();
[email protected]420ae012009-04-24 05:16:321132}
1133
[email protected]89d6e6e2009-12-03 23:06:511134void TabContents::EmailPageLocation() {
1135 std::string title = EscapeQueryParamValue(UTF16ToUTF8(GetTitle()), false);
1136 std::string page_url = EscapeQueryParamValue(GetURL().spec(), false);
1137 std::string mailto = std::string("mailto:?subject=Fwd:%20") +
1138 title + "&body=%0A%0A" + page_url;
1139 platform_util::OpenExternal(GURL(mailto));
1140}
1141
[email protected]420ae012009-04-24 05:16:321142void TabContents::PrintPreview() {
1143 // We don't show the print preview yet, only the print dialog.
1144 PrintNow();
1145}
1146
1147bool TabContents::PrintNow() {
1148 // We can't print interstitial page for now.
1149 if (showing_interstitial_page())
1150 return false;
1151
1152 return render_view_host()->PrintPages();
1153}
1154
[email protected]82270452009-06-19 15:58:011155void TabContents::PrintingDone(int document_cookie, bool success) {
1156 render_view_host()->PrintingDone(document_cookie, success);
1157}
1158
[email protected]420ae012009-04-24 05:16:321159bool TabContents::IsActiveEntry(int32 page_id) {
1160 NavigationEntry* active_entry = controller_.GetActiveEntry();
1161 return (active_entry != NULL &&
1162 active_entry->site_instance() == GetSiteInstance() &&
1163 active_entry->page_id() == page_id);
1164}
1165
[email protected]3f8eb7f2009-10-02 23:58:051166void TabContents::LogNewTabTime(const std::string& event_name) {
1167 // Not all new tab pages get timed. In those cases, we don't have a
1168 // new_tab_start_time_.
1169 if (new_tab_start_time_.is_null())
1170 return;
1171
1172 base::TimeDelta duration = base::TimeTicks::Now() - new_tab_start_time_;
[email protected]84abba62009-10-07 17:01:441173 MetricEventDurationDetails details(event_name,
1174 static_cast<int>(duration.InMilliseconds()));
1175
[email protected]7ab1e7d62009-10-14 23:32:011176 if (event_name == "Tab.NewTabScriptStart") {
1177 UMA_HISTOGRAM_TIMES("Tab.NewTabScriptStart", duration);
1178 } else if (event_name == "Tab.NewTabDOMContentLoaded") {
1179 UMA_HISTOGRAM_TIMES("Tab.NewTabDOMContentLoaded", duration);
1180 } else if (event_name == "Tab.NewTabOnload") {
1181 UMA_HISTOGRAM_TIMES("Tab.NewTabOnload", duration);
[email protected]3f8eb7f2009-10-02 23:58:051182 // The new tab page has finished loading; reset it.
1183 new_tab_start_time_ = base::TimeTicks();
[email protected]84abba62009-10-07 17:01:441184 } else {
1185 NOTREACHED();
[email protected]3f8eb7f2009-10-02 23:58:051186 }
[email protected]84abba62009-10-07 17:01:441187 NotificationService::current()->Notify(
1188 NotificationType::METRIC_EVENT_DURATION,
1189 Source<TabContents>(this),
1190 Details<MetricEventDurationDetails>(&details));
[email protected]3f8eb7f2009-10-02 23:58:051191}
1192
[email protected]7ab1e7d62009-10-14 23:32:011193void TabContents::OnCloseStarted() {
1194 if (tab_close_start_time_.is_null())
1195 tab_close_start_time_ = base::TimeTicks::Now();
1196}
1197
[email protected]c37e3b62010-01-22 00:15:171198TabContents* TabContents::CloneAndMakePhantom() {
1199 // TODO: the initial URL, title and what not should come from the app.
1200 NavigationEntry* entry = controller().GetActiveEntry();
1201
1202 TabNavigation tab_nav;
1203 if (entry)
1204 tab_nav.SetFromNavigationEntry(*entry);
1205 std::vector<TabNavigation> navigations;
1206 navigations.push_back(tab_nav);
1207
1208 TabContents* new_contents =
1209 new TabContents(profile(), NULL, MSG_ROUTING_NONE, NULL);
1210 new_contents->controller().RestoreFromState(navigations, 0, false);
1211 if (entry) {
1212 // TODO: this should come from the app.
1213 new_contents->controller().GetActiveEntry()->favicon() = entry->favicon();
1214 }
1215
1216 return new_contents;
1217}
1218
[email protected]96d185d2009-04-24 03:28:541219// Notifies the RenderWidgetHost instance about the fact that the page is
1220// loading, or done loading and calls the base implementation.
1221void TabContents::SetIsLoading(bool is_loading,
1222 LoadNotificationDetails* details) {
1223 if (is_loading == is_loading_)
1224 return;
1225
1226 if (!is_loading) {
1227 load_state_ = net::LOAD_STATE_IDLE;
1228 load_state_host_.clear();
[email protected]094e5b22009-09-25 04:23:561229 upload_size_ = 0;
1230 upload_position_ = 0;
[email protected]96d185d2009-04-24 03:28:541231 }
1232
1233 render_manager_.SetIsLoading(is_loading);
1234
1235 is_loading_ = is_loading;
1236 waiting_for_response_ = is_loading;
1237
1238 if (delegate_)
1239 delegate_->LoadingStateChanged(this);
[email protected]329581b2009-04-28 06:52:351240 NotifyNavigationStateChanged(INVALIDATE_LOAD);
[email protected]96d185d2009-04-24 03:28:541241
1242 NotificationType type = is_loading ? NotificationType::LOAD_START :
1243 NotificationType::LOAD_STOP;
[email protected]ce5c4502009-05-06 16:46:111244 NotificationDetails det = NotificationService::NoDetails();
[email protected]96d185d2009-04-24 03:28:541245 if (details)
1246 det = Details<LoadNotificationDetails>(details);
1247 NotificationService::current()->Notify(type,
1248 Source<NavigationController>(&controller_),
1249 det);
1250}
1251
[email protected]2c4410d2009-05-06 23:46:221252void TabContents::CreateBlockedPopupContainerIfNecessary() {
1253 if (blocked_popups_)
1254 return;
1255
[email protected]4d4c0b92009-06-08 23:13:011256 blocked_popups_ = BlockedPopupContainer::Create(this, profile());
[email protected]2c4410d2009-05-06 23:46:221257}
1258
[email protected]b6c874582009-05-08 19:38:311259void TabContents::AddPopup(TabContents* new_contents,
1260 const gfx::Rect& initial_pos,
1261 const std::string& host) {
[email protected]2c4410d2009-05-06 23:46:221262 CreateBlockedPopupContainerIfNecessary();
1263 blocked_popups_->AddTabContents(new_contents, initial_pos, host);
[email protected]2c4410d2009-05-06 23:46:221264}
1265
[email protected]e69bce3dd2009-04-20 22:05:121266// TODO(brettw) This should be on the TabContentsView.
[email protected]66ba4932009-06-04 19:22:131267void TabContents::RepositionSupressedPopupsToFit() {
[email protected]d6598c052008-11-05 19:03:251268 if (blocked_popups_)
[email protected]4d4c0b92009-06-08 23:13:011269 blocked_popups_->RepositionBlockedPopupContainer();
[email protected]d5f942ba2008-09-26 19:30:341270}
1271
[email protected]b9681312008-11-07 00:08:261272bool TabContents::ShowingBlockedPopupNotification() const {
1273 return blocked_popups_ != NULL &&
[email protected]2c4410d2009-05-06 23:46:221274 blocked_popups_->GetBlockedPopupCount() != 0;
[email protected]b9681312008-11-07 00:08:261275}
[email protected]616ed5a2008-11-21 22:27:241276
1277namespace {
1278bool TransitionIsReload(PageTransition::Type transition) {
1279 return PageTransition::StripQualifier(transition) == PageTransition::RELOAD;
1280}
1281}
1282
1283void TabContents::ExpireInfoBars(
1284 const NavigationController::LoadCommittedDetails& details) {
1285 // Only hide InfoBars when the user has done something that makes the main
1286 // frame load. We don't want various automatic or subframe navigations making
1287 // it disappear.
1288 if (!details.is_user_initiated_main_frame_load())
1289 return;
1290
[email protected]f86a07022008-11-25 01:06:051291 for (int i = infobar_delegate_count() - 1; i >= 0; --i) {
[email protected]616ed5a2008-11-21 22:27:241292 InfoBarDelegate* delegate = GetInfoBarDelegateAt(i);
[email protected]f86a07022008-11-25 01:06:051293 if (delegate->ShouldExpire(details))
[email protected]616ed5a2008-11-21 22:27:241294 RemoveInfoBar(delegate);
[email protected]616ed5a2008-11-21 22:27:241295 }
1296}
[email protected]fdd61c62009-04-22 19:22:571297
[email protected]96d185d2009-04-24 03:28:541298DOMUI* TabContents::GetDOMUIForCurrentState() {
1299 // When there is a pending navigation entry, we want to use the pending DOMUI
1300 // that goes along with it to control the basic flags. For example, we want to
1301 // show the pending URL in the URL bar, so we want the display_url flag to
1302 // be from the pending entry.
1303 //
1304 // The confusion comes because there are multiple possibilities for the
1305 // initial load in a tab as a side effect of the way the RenderViewHostManager
1306 // works.
1307 //
1308 // - For the very first tab the load looks "normal". The new tab DOM UI is
1309 // the pending one, and we want it to apply here.
1310 //
1311 // - For subsequent new tabs, they'll get a new SiteInstance which will then
1312 // get switched to the one previously associated with the new tab pages.
1313 // This switching will cause the manager to commit the RVH/DOMUI. So we'll
1314 // have a committed DOM UI in this case.
1315 //
1316 // This condition handles all of these cases:
1317 //
1318 // - First load in first tab: no committed nav entry + pending nav entry +
1319 // pending dom ui:
1320 // -> Use pending DOM UI if any.
1321 //
1322 // - First load in second tab: no committed nav entry + pending nav entry +
1323 // no pending DOM UI:
1324 // -> Use the committed DOM UI if any.
1325 //
1326 // - Second navigation in any tab: committed nav entry + pending nav entry:
1327 // -> Use pending DOM UI if any.
1328 //
1329 // - Normal state with no load: committed nav entry + no pending nav entry:
1330 // -> Use committed DOM UI.
1331 if (controller_.pending_entry() &&
1332 (controller_.GetLastCommittedEntry() ||
1333 render_manager_.pending_dom_ui()))
1334 return render_manager_.pending_dom_ui();
1335 return render_manager_.dom_ui();
1336}
[email protected]420ae012009-04-24 05:16:321337
1338void TabContents::DidNavigateMainFramePostCommit(
1339 const NavigationController::LoadCommittedDetails& details,
1340 const ViewHostMsg_FrameNavigate_Params& params) {
[email protected]c2e74fe82009-09-03 17:57:441341 if (opener_dom_ui_type_ != DOMUIFactory::kNoDOMUI) {
1342 // If this is a window.open navigation, use the same DOMUI as the renderer
1343 // that opened the window, as long as both renderers have the same
1344 // privileges.
1345 if (opener_dom_ui_type_ == DOMUIFactory::GetDOMUIType(GetURL())) {
1346 DOMUI* dom_ui = DOMUIFactory::CreateDOMUIForURL(this, GetURL());
1347 // dom_ui might be NULL if the URL refers to a non-existent extension.
1348 if (dom_ui) {
1349 render_manager_.SetDOMUIPostCommit(dom_ui);
1350 dom_ui->RenderViewCreated(render_view_host());
1351 }
1352 }
1353 opener_dom_ui_type_ = DOMUIFactory::kNoDOMUI;
1354 }
1355
[email protected]420ae012009-04-24 05:16:321356 if (details.is_user_initiated_main_frame_load()) {
1357 // Clear the status bubble. This is a workaround for a bug where WebKit
1358 // doesn't let us know that the cursor left an element during a
1359 // transition (this is also why the mouse cursor remains as a hand after
1360 // clicking on a link); see bugs 1184641 and 980803. We don't want to
1361 // clear the bubble when a user navigates to a named anchor in the same
1362 // page.
1363 UpdateTargetURL(details.entry->page_id(), GURL());
1364
1365 // UpdateHelpersForDidNavigate will handle the case where the password_form
1366 // origin is valid.
1367 // TODO(brettw) bug 1343111: Password manager stuff in here needs to be
1368 // cleaned up and covered by tests.
1369 if (!params.password_form.origin.is_valid())
1370 GetPasswordManager()->DidNavigate();
1371 }
1372
1373 // The keyword generator uses the navigation entries, so must be called after
1374 // the commit.
1375 GenerateKeywordIfNecessary(params);
1376
1377 // Allow the new page to set the title again.
1378 received_page_title_ = false;
1379
1380 // Get the favicon, either from history or request it from the net.
1381 fav_icon_helper_.FetchFavIcon(details.entry->url());
1382
[email protected]56ce6e52009-10-27 00:10:521383 // Clear all page and browser action state for this tab, unless this is an
1384 // in-page navigation.
[email protected]92c6f9b92009-10-24 04:35:081385 url_canon::Replacements<char> replacements;
1386 replacements.ClearRef();
1387 if (params.url.ReplaceComponents(replacements) !=
1388 params.referrer.ReplaceComponents(replacements)) {
[email protected]92c6f9b92009-10-24 04:35:081389 ExtensionsService* service = profile()->GetExtensionsService();
1390 if (service) {
1391 for (size_t i = 0; i < service->extensions()->size(); ++i) {
[email protected]5d246db22009-10-27 06:17:571392 ExtensionAction* browser_action =
[email protected]92c6f9b92009-10-24 04:35:081393 service->extensions()->at(i)->browser_action();
[email protected]56ce6e52009-10-27 00:10:521394 if (browser_action) {
1395 browser_action->ClearAllValuesForTab(controller().session_id().id());
1396 NotificationService::current()->Notify(
1397 NotificationType::EXTENSION_BROWSER_ACTION_UPDATED,
[email protected]5d246db22009-10-27 06:17:571398 Source<ExtensionAction>(browser_action),
[email protected]56ce6e52009-10-27 00:10:521399 NotificationService::NoDetails());
1400 }
[email protected]92c6f9b92009-10-24 04:35:081401
[email protected]5d246db22009-10-27 06:17:571402 ExtensionAction* page_action =
[email protected]56ce6e52009-10-27 00:10:521403 service->extensions()->at(i)->page_action();
1404 if (page_action) {
1405 page_action->ClearAllValuesForTab(controller().session_id().id());
1406 PageActionStateChanged();
1407 }
[email protected]92c6f9b92009-10-24 04:35:081408 }
[email protected]aece2c7f2009-08-27 20:43:171409 }
1410 }
[email protected]ce5c4502009-05-06 16:46:111411
[email protected]420ae012009-04-24 05:16:321412 // Close constrained popups if necessary.
1413 MaybeCloseChildWindows(details.previous_url, details.entry->url());
1414
1415 // Update the starred state.
1416 UpdateStarredStateForCurrentURL();
1417}
1418
1419void TabContents::DidNavigateAnyFramePostCommit(
1420 RenderViewHost* render_view_host,
1421 const NavigationController::LoadCommittedDetails& details,
1422 const ViewHostMsg_FrameNavigate_Params& params) {
1423 // If we navigate, start showing messages again. This does nothing to prevent
1424 // a malicious script from spamming messages, since the script could just
1425 // reload the page to stop blocking.
1426 suppress_javascript_messages_ = false;
1427
[email protected]420ae012009-04-24 05:16:321428 // Notify the password manager of the navigation or form submit.
1429 // TODO(brettw) bug 1343111: Password manager stuff in here needs to be
1430 // cleaned up and covered by tests.
1431 if (params.password_form.origin.is_valid())
1432 GetPasswordManager()->ProvisionallySavePassword(params.password_form);
1433}
1434
1435void TabContents::MaybeCloseChildWindows(const GURL& previous_url,
1436 const GURL& current_url) {
1437 if (net::RegistryControlledDomainService::SameDomainOrHost(
1438 previous_url, current_url))
1439 return;
1440
1441 // Clear out any child windows since we are leaving this page entirely.
1442 // We use indices instead of iterators in case CloseWindow does something
1443 // that may invalidate an iterator.
1444 int size = static_cast<int>(child_windows_.size());
1445 for (int i = size - 1; i >= 0; --i) {
1446 ConstrainedWindow* window = child_windows_[i];
[email protected]fa1cf0b82010-01-15 21:49:441447 if (window) {
1448 DCHECK(delegate_);
[email protected]420ae012009-04-24 05:16:321449 window->CloseConstrainedWindow();
[email protected]fa1cf0b82010-01-15 21:49:441450 BlockTabContent(false);
1451 }
[email protected]420ae012009-04-24 05:16:321452 }
[email protected]e29300cd2009-07-23 21:14:591453
1454 // Close the popup container.
1455 if (blocked_popups_) {
1456 blocked_popups_->Destroy();
1457 blocked_popups_ = NULL;
1458 }
[email protected]420ae012009-04-24 05:16:321459}
1460
1461void TabContents::UpdateStarredStateForCurrentURL() {
1462 BookmarkModel* model = profile()->GetBookmarkModel();
1463 const bool old_state = is_starred_;
1464 is_starred_ = (model && model->IsBookmarked(GetURL()));
1465
1466 if (is_starred_ != old_state && delegate())
1467 delegate()->URLStarredChanged(this, is_starred_);
1468}
1469
1470void TabContents::UpdateAlternateErrorPageURL() {
1471 GURL url = GetAlternateErrorPageURL();
1472 render_view_host()->SetAlternateErrorPageURL(url);
1473}
1474
1475void TabContents::UpdateWebPreferences() {
1476 render_view_host()->UpdateWebPreferences(GetWebkitPrefs());
1477}
1478
1479void TabContents::UpdateMaxPageIDIfNecessary(SiteInstance* site_instance,
1480 RenderViewHost* rvh) {
1481 // If we are creating a RVH for a restored controller, then we might
1482 // have more page IDs than the SiteInstance's current max page ID. We must
1483 // make sure that the max page ID is larger than any restored page ID.
1484 // Note that it is ok for conflicting page IDs to exist in another tab
1485 // (i.e., NavigationController), but if any page ID is larger than the max,
1486 // the back/forward list will get confused.
1487 int max_restored_page_id = controller_.max_restored_page_id();
1488 if (max_restored_page_id > 0) {
1489 int curr_max_page_id = site_instance->max_page_id();
1490 if (max_restored_page_id > curr_max_page_id) {
1491 // Need to update the site instance immediately.
1492 site_instance->UpdateMaxPageID(max_restored_page_id);
1493
1494 // Also tell the renderer to update its internal representation. We
1495 // need to reserve enough IDs to make all restored page IDs less than
1496 // the max.
1497 if (curr_max_page_id < 0)
1498 curr_max_page_id = 0;
1499 rvh->ReservePageIDRange(max_restored_page_id - curr_max_page_id);
1500 }
1501 }
1502}
1503
1504void TabContents::UpdateHistoryForNavigation(
[email protected]ebe89e062009-08-13 23:16:541505 const GURL& virtual_url,
[email protected]befd8d822009-07-01 04:51:471506 const NavigationController::LoadCommittedDetails& details,
[email protected]420ae012009-04-24 05:16:321507 const ViewHostMsg_FrameNavigate_Params& params) {
1508 if (profile()->IsOffTheRecord())
1509 return;
1510
1511 // Add to history service.
1512 HistoryService* hs = profile()->GetHistoryService(Profile::IMPLICIT_ACCESS);
1513 if (hs) {
1514 if (PageTransition::IsMainFrame(params.transition) &&
[email protected]ebe89e062009-08-13 23:16:541515 virtual_url != params.url) {
1516 // Hack on the "virtual" URL so that it will appear in history. For some
[email protected]420ae012009-04-24 05:16:321517 // types of URLs, we will display a magic URL that is different from where
1518 // the page is actually navigated. We want the user to see in history
[email protected]ebe89e062009-08-13 23:16:541519 // what they saw in the URL bar, so we add the virtual URL as a redirect.
1520 // This only applies to the main frame, as the virtual URL doesn't apply
[email protected]420ae012009-04-24 05:16:321521 // to sub-frames.
1522 std::vector<GURL> redirects = params.redirects;
1523 if (!redirects.empty())
[email protected]ebe89e062009-08-13 23:16:541524 redirects.back() = virtual_url;
1525 hs->AddPage(virtual_url, this, params.page_id, params.referrer,
[email protected]befd8d822009-07-01 04:51:471526 params.transition, redirects, details.did_replace_entry);
[email protected]420ae012009-04-24 05:16:321527 } else {
1528 hs->AddPage(params.url, this, params.page_id, params.referrer,
[email protected]befd8d822009-07-01 04:51:471529 params.transition, params.redirects,
1530 details.did_replace_entry);
[email protected]420ae012009-04-24 05:16:321531 }
1532 }
1533}
1534
1535bool TabContents::UpdateTitleForEntry(NavigationEntry* entry,
1536 const std::wstring& title) {
1537 // For file URLs without a title, use the pathname instead. In the case of a
1538 // synthesized title, we don't want the update to count toward the "one set
1539 // per page of the title to history."
1540 std::wstring final_title;
1541 bool explicit_set;
1542 if (entry->url().SchemeIsFile() && title.empty()) {
1543 final_title = UTF8ToWide(entry->url().ExtractFileName());
1544 explicit_set = false; // Don't count synthetic titles toward the set limit.
1545 } else {
1546 TrimWhitespace(title, TRIM_ALL, &final_title);
1547 explicit_set = true;
1548 }
1549
1550 if (final_title == UTF16ToWideHack(entry->title()))
1551 return false; // Nothing changed, don't bother.
1552
1553 entry->set_title(WideToUTF16Hack(final_title));
1554
1555 // Update the history system for this page.
1556 if (!profile()->IsOffTheRecord() && !received_page_title_) {
1557 HistoryService* hs =
1558 profile()->GetHistoryService(Profile::IMPLICIT_ACCESS);
1559 if (hs)
[email protected]ebe89e062009-08-13 23:16:541560 hs->SetPageTitle(entry->virtual_url(), final_title);
[email protected]420ae012009-04-24 05:16:321561
1562 // Don't allow the title to be saved again for explicitly set ones.
1563 received_page_title_ = explicit_set;
1564 }
1565
1566 // Lastly, set the title for the view.
1567 view_->SetPageTitle(final_title);
1568
1569 return true;
1570}
1571
1572void TabContents::NotifySwapped() {
1573 // After sending out a swap notification, we need to send a disconnect
1574 // notification so that clients that pick up a pointer to |this| can NULL the
1575 // pointer. See Bug 1230284.
1576 notify_disconnection_ = true;
1577 NotificationService::current()->Notify(
[email protected]57c6a652009-05-04 07:58:341578 NotificationType::TAB_CONTENTS_SWAPPED,
[email protected]d82ed61e2009-06-16 02:46:221579 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:321580 NotificationService::NoDetails());
1581}
1582
1583void TabContents::NotifyConnected() {
1584 notify_disconnection_ = true;
1585 NotificationService::current()->Notify(
[email protected]57c6a652009-05-04 07:58:341586 NotificationType::TAB_CONTENTS_CONNECTED,
[email protected]d82ed61e2009-06-16 02:46:221587 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:321588 NotificationService::NoDetails());
1589}
1590
1591void TabContents::NotifyDisconnected() {
1592 if (!notify_disconnection_)
1593 return;
1594
1595 notify_disconnection_ = false;
1596 NotificationService::current()->Notify(
[email protected]57c6a652009-05-04 07:58:341597 NotificationType::TAB_CONTENTS_DISCONNECTED,
[email protected]d82ed61e2009-06-16 02:46:221598 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:321599 NotificationService::NoDetails());
1600}
1601
1602void TabContents::GenerateKeywordIfNecessary(
1603 const ViewHostMsg_FrameNavigate_Params& params) {
1604 if (!params.searchable_form_url.is_valid())
1605 return;
1606
1607 if (profile()->IsOffTheRecord())
1608 return;
1609
1610 int last_index = controller_.last_committed_entry_index();
1611 // When there was no previous page, the last index will be 0. This is
1612 // normally due to a form submit that opened in a new tab.
1613 // TODO(brettw) bug 916126: we should support keywords when form submits
1614 // happen in new tabs.
1615 if (last_index <= 0)
1616 return;
1617 const NavigationEntry* previous_entry =
1618 controller_.GetEntryAtIndex(last_index - 1);
1619 if (IsFormSubmit(previous_entry)) {
1620 // Only generate a keyword if the previous page wasn't itself a form
1621 // submit.
1622 return;
1623 }
1624
1625 GURL keyword_url = previous_entry->user_typed_url().is_valid() ?
1626 previous_entry->user_typed_url() : previous_entry->url();
1627 std::wstring keyword =
1628 TemplateURLModel::GenerateKeyword(keyword_url, true); // autodetected
1629 if (keyword.empty())
1630 return;
1631
1632 TemplateURLModel* url_model = profile()->GetTemplateURLModel();
1633 if (!url_model)
1634 return;
1635
1636 if (!url_model->loaded()) {
1637 url_model->Load();
1638 return;
1639 }
1640
1641 const TemplateURL* current_url;
1642 std::wstring url = UTF8ToWide(params.searchable_form_url.spec());
1643 if (!url_model->CanReplaceKeyword(keyword, url, &current_url))
1644 return;
1645
1646 if (current_url) {
1647 if (current_url->originating_url().is_valid()) {
1648 // The existing keyword was generated from an OpenSearch description
1649 // document, don't regenerate.
1650 return;
1651 }
1652 url_model->Remove(current_url);
1653 }
1654 TemplateURL* new_url = new TemplateURL();
1655 new_url->set_keyword(keyword);
1656 new_url->set_short_name(keyword);
1657 new_url->SetURL(url, 0, 0);
1658 new_url->add_input_encoding(params.searchable_form_encoding);
1659 DCHECK(controller_.GetLastCommittedEntry());
1660 const GURL& favicon_url =
1661 controller_.GetLastCommittedEntry()->favicon().url();
1662 if (favicon_url.is_valid()) {
1663 new_url->SetFavIconURL(favicon_url);
1664 } else {
1665 // The favicon url isn't valid. This means there really isn't a favicon,
1666 // or the favicon url wasn't obtained before the load started. This assumes
1667 // the later.
1668 // TODO(sky): Need a way to set the favicon that doesn't involve generating
1669 // its url.
1670 new_url->SetFavIconURL(TemplateURL::GenerateFaviconURL(params.referrer));
1671 }
1672 new_url->set_safe_for_autoreplace(true);
1673 url_model->Add(new_url);
1674}
1675
[email protected]bf5c2ff392009-07-08 16:24:331676void TabContents::OnUserGesture() {
1677 // See comment in RenderViewHostDelegate::OnUserGesture as to why we do this.
1678 DownloadRequestManager* drm = g_browser_process->download_request_manager();
1679 if (drm)
1680 drm->OnUserGesture(this);
[email protected]e7eaedde2009-09-25 20:09:491681 ExternalProtocolHandler::OnUserGesture();
[email protected]bf5c2ff392009-07-08 16:24:331682 controller_.OnUserGesture();
1683}
1684
1685void TabContents::OnFindReply(int request_id,
1686 int number_of_matches,
1687 const gfx::Rect& selection_rect,
1688 int active_match_ordinal,
1689 bool final_update) {
[email protected]3ce7ced2009-07-24 18:52:421690 // Ignore responses for requests that have been aborted.
1691 if (find_op_aborted_)
1692 return;
1693
[email protected]bf5c2ff392009-07-08 16:24:331694 // Ignore responses for requests other than the one we have most recently
1695 // issued. That way we won't act on stale results when the user has
1696 // already typed in another query.
1697 if (request_id != current_find_request_id_)
1698 return;
1699
1700 if (number_of_matches == -1)
1701 number_of_matches = last_search_result_.number_of_matches();
1702 if (active_match_ordinal == -1)
1703 active_match_ordinal = last_search_result_.active_match_ordinal();
1704
1705 gfx::Rect selection = selection_rect;
1706 if (selection.IsEmpty())
1707 selection = last_search_result_.selection_rect();
1708
1709 // Notify the UI, automation and any other observers that a find result was
1710 // found.
1711 last_search_result_ = FindNotificationDetails(request_id, number_of_matches,
1712 selection, active_match_ordinal,
1713 final_update);
1714 NotificationService::current()->Notify(
1715 NotificationType::FIND_RESULT_AVAILABLE,
1716 Source<TabContents>(this),
1717 Details<FindNotificationDetails>(&last_search_result_));
1718}
1719
1720void TabContents::GoToEntryAtOffset(int offset) {
[email protected]f9cc4c452009-10-13 14:56:381721 if (!delegate_ || delegate_->OnGoToEntryOffset(offset))
1722 controller_.GoToOffset(offset);
[email protected]bf5c2ff392009-07-08 16:24:331723}
1724
1725void TabContents::GetHistoryListCount(int* back_list_count,
1726 int* forward_list_count) {
1727 int current_index = controller_.last_committed_entry_index();
1728 *back_list_count = current_index;
1729 *forward_list_count = controller_.entry_count() - current_index - 1;
1730}
1731
1732void TabContents::OnMissingPluginStatus(int status) {
1733#if defined(OS_WIN)
1734// TODO(PORT): pull in when plug-ins work
1735 GetPluginInstaller()->OnMissingPluginStatus(status);
1736#endif
1737}
1738
1739void TabContents::OnCrashedPlugin(const FilePath& plugin_path) {
[email protected]bf5c2ff392009-07-08 16:24:331740 DCHECK(!plugin_path.value().empty());
1741
1742 std::wstring plugin_name = plugin_path.ToWStringHack();
[email protected]d32d46182009-07-23 00:09:351743#if defined(OS_WIN) || defined(OS_MACOSX)
[email protected]bf5c2ff392009-07-08 16:24:331744 scoped_ptr<FileVersionInfo> version_info(
1745 FileVersionInfo::CreateFileVersionInfo(plugin_path));
1746 if (version_info.get()) {
1747 const std::wstring& product_name = version_info->product_name();
[email protected]996fd702009-09-04 19:12:371748 if (!product_name.empty()) {
[email protected]bf5c2ff392009-07-08 16:24:331749 plugin_name = product_name;
[email protected]996fd702009-09-04 19:12:371750#if defined(OS_MACOSX)
1751 // Many plugins on the Mac have .plugin in the actual name, which looks
1752 // terrible, so look for that and strip it off if present.
1753 const std::wstring plugin_extension(L".plugin");
1754 if (EndsWith(plugin_name, plugin_extension, true))
1755 plugin_name.erase(plugin_name.length() - plugin_extension.length());
1756#endif // OS_MACOSX
1757 }
[email protected]bf5c2ff392009-07-08 16:24:331758 }
[email protected]4a560ed2009-07-13 23:35:261759#else
1760 NOTIMPLEMENTED() << " convert plugin path to plugin name";
1761#endif
[email protected]bf5c2ff392009-07-08 16:24:331762 AddInfoBar(new SimpleAlertInfoBarDelegate(
1763 this, l10n_util::GetStringF(IDS_PLUGIN_CRASHED_PROMPT, plugin_name),
1764 NULL));
[email protected]bf5c2ff392009-07-08 16:24:331765}
1766
1767void TabContents::OnCrashedWorker() {
1768 AddInfoBar(new SimpleAlertInfoBarDelegate(
1769 this, l10n_util::GetString(IDS_WEBWORKER_CRASHED_PROMPT),
1770 NULL));
1771}
1772
1773void TabContents::OnDidGetApplicationInfo(
1774 int32 page_id,
1775 const webkit_glue::WebApplicationInfo& info) {
[email protected]ed5431872009-11-17 08:39:511776 web_app_info_ = info;
[email protected]bf5c2ff392009-07-08 16:24:331777
[email protected]ed5431872009-11-17 08:39:511778 if (delegate())
1779 delegate()->OnDidGetApplicationInfo(this, page_id);
[email protected]bf5c2ff392009-07-08 16:24:331780}
1781
[email protected]a2cf65eb2010-01-18 08:20:381782void TabContents::OnPageContents(const GURL& url,
1783 int renderer_process_id,
1784 int32 page_id,
[email protected]a8a81292010-01-21 00:32:451785 const std::wstring& contents,
1786 const std::string& language) {
[email protected]a2cf65eb2010-01-18 08:20:381787 // Don't index any https pages. People generally don't want their bank
1788 // accounts, etc. indexed on their computer, especially since some of these
1789 // things are not marked cachable.
1790 // TODO(brettw) we may want to consider more elaborate heuristics such as
1791 // the cachability of the page. We may also want to consider subframes (this
1792 // test will still index subframes if the subframe is SSL).
1793 if (!url.SchemeIsSecure()) {
1794 Profile* p = profile();
1795 if (p && !p->IsOffTheRecord()) {
1796 HistoryService* hs = p->GetHistoryService(Profile::IMPLICIT_ACCESS);
1797 if (hs)
1798 hs->SetPageContents(url, contents);
1799 }
1800 }
[email protected]a8a81292010-01-21 00:32:451801
1802 NavigationEntry* entry = controller_.GetActiveEntry();
1803 if (process()->id() == renderer_process_id &&
1804 entry && entry->page_id() == page_id) {
1805 entry->set_language(language);
1806 }
1807
1808 std::string lang = language;
1809 NotificationService::current()->Notify(
1810 NotificationType::TAB_LANGUAGE_DETERMINED,
1811 Source<RenderViewHost>(render_view_host()),
1812 Details<std::string>(&lang));
[email protected]a2cf65eb2010-01-18 08:20:381813}
1814
[email protected]bf5c2ff392009-07-08 16:24:331815void TabContents::DidStartProvisionalLoadForFrame(
1816 RenderViewHost* render_view_host,
1817 bool is_main_frame,
1818 const GURL& url) {
1819 ProvisionalLoadDetails details(is_main_frame,
1820 controller_.IsURLInPageNavigation(url),
1821 url, std::string(), false);
1822 NotificationService::current()->Notify(
1823 NotificationType::FRAME_PROVISIONAL_LOAD_START,
1824 Source<NavigationController>(&controller_),
1825 Details<ProvisionalLoadDetails>(&details));
1826}
1827
1828void TabContents::DidStartReceivingResourceResponse(
1829 ResourceRequestDetails* details) {
1830 NotificationService::current()->Notify(
1831 NotificationType::RESOURCE_RESPONSE_STARTED,
1832 Source<NavigationController>(&controller()),
1833 Details<ResourceRequestDetails>(details));
1834}
1835
1836void TabContents::DidRedirectResource(ResourceRequestDetails* details) {
1837 NotificationService::current()->Notify(
1838 NotificationType::RESOURCE_RECEIVED_REDIRECT,
1839 Source<NavigationController>(&controller()),
1840 Details<ResourceRequestDetails>(details));
1841}
1842
1843void TabContents::DidLoadResourceFromMemoryCache(
1844 const GURL& url,
1845 const std::string& frame_origin,
1846 const std::string& main_frame_origin,
1847 const std::string& security_info) {
1848 // Send out a notification that we loaded a resource from our memory cache.
1849 int cert_id = 0, cert_status = 0, security_bits = 0;
1850 SSLManager::DeserializeSecurityInfo(security_info,
1851 &cert_id, &cert_status,
1852 &security_bits);
1853 LoadFromMemoryCacheDetails details(url, frame_origin, main_frame_origin,
[email protected]76543b92009-08-31 17:27:451854 process()->id(), cert_id, cert_status);
[email protected]bf5c2ff392009-07-08 16:24:331855
1856 NotificationService::current()->Notify(
1857 NotificationType::LOAD_FROM_MEMORY_CACHE,
1858 Source<NavigationController>(&controller_),
1859 Details<LoadFromMemoryCacheDetails>(&details));
1860}
1861
[email protected]e3d60e5d2009-09-25 21:08:291862void TabContents::DidDisplayInsecureContent() {
[email protected]09843b5f2009-09-26 08:30:591863 controller_.ssl_manager()->DidDisplayInsecureContent();
[email protected]e3d60e5d2009-09-25 21:08:291864}
1865
1866void TabContents::DidRunInsecureContent(const std::string& security_origin) {
[email protected]09843b5f2009-09-26 08:30:591867 controller_.ssl_manager()->DidRunInsecureContent(security_origin);
[email protected]e3d60e5d2009-09-25 21:08:291868}
1869
[email protected]bf5c2ff392009-07-08 16:24:331870void TabContents::DidFailProvisionalLoadWithError(
1871 RenderViewHost* render_view_host,
1872 bool is_main_frame,
1873 int error_code,
1874 const GURL& url,
1875 bool showing_repost_interstitial) {
1876 if (net::ERR_ABORTED == error_code) {
1877 // EVIL HACK ALERT! Ignore failed loads when we're showing interstitials.
1878 // This means that the interstitial won't be torn down properly, which is
1879 // bad. But if we have an interstitial, go back to another tab type, and
1880 // then load the same interstitial again, we could end up getting the first
1881 // interstitial's "failed" message (as a result of the cancel) when we're on
1882 // the second one.
1883 //
1884 // We can't tell this apart, so we think we're tearing down the current page
1885 // which will cause a crash later one. There is also some code in
1886 // RenderViewHostManager::RendererAbortedProvisionalLoad that is commented
1887 // out because of this problem.
1888 //
1889 // http://code.google.com/p/chromium/issues/detail?id=2855
1890 // Because this will not tear down the interstitial properly, if "back" is
1891 // back to another tab type, the interstitial will still be somewhat alive
1892 // in the previous tab type. If you navigate somewhere that activates the
1893 // tab with the interstitial again, you'll see a flash before the new load
1894 // commits of the interstitial page.
1895 if (showing_interstitial_page()) {
1896 LOG(WARNING) << "Discarding message during interstitial.";
1897 return;
1898 }
1899
1900 // This will discard our pending entry if we cancelled the load (e.g., if we
1901 // decided to download the file instead of load it). Only discard the
1902 // pending entry if the URLs match, otherwise the user initiated a navigate
1903 // before the page loaded so that the discard would discard the wrong entry.
1904 NavigationEntry* pending_entry = controller_.pending_entry();
1905 if (pending_entry && pending_entry->url() == url) {
1906 controller_.DiscardNonCommittedEntries();
1907 // Update the URL display.
1908 NotifyNavigationStateChanged(TabContents::INVALIDATE_URL);
1909 }
1910
1911 render_manager_.RendererAbortedProvisionalLoad(render_view_host);
1912 }
1913
1914 // Send out a notification that we failed a provisional load with an error.
1915 ProvisionalLoadDetails details(is_main_frame,
1916 controller_.IsURLInPageNavigation(url),
1917 url, std::string(), false);
1918 details.set_error_code(error_code);
1919
1920 NotificationService::current()->Notify(
1921 NotificationType::FAIL_PROVISIONAL_LOAD_WITH_ERROR,
1922 Source<NavigationController>(&controller_),
1923 Details<ProvisionalLoadDetails>(&details));
1924}
1925
1926void TabContents::DocumentLoadedInFrame() {
1927 controller_.DocumentLoadedInFrame();
1928}
1929
[email protected]8d3347f2009-07-09 22:00:211930RenderViewHostDelegate::View* TabContents::GetViewDelegate() {
[email protected]420ae012009-04-24 05:16:321931 return view_.get();
1932}
1933
[email protected]8d3347f2009-07-09 22:00:211934RenderViewHostDelegate::RendererManagement*
1935TabContents::GetRendererManagementDelegate() {
1936 return &render_manager_;
1937}
1938
[email protected]bf5c2ff392009-07-08 16:24:331939RenderViewHostDelegate::BrowserIntegration*
[email protected]8d3347f2009-07-09 22:00:211940 TabContents::GetBrowserIntegrationDelegate() {
1941 return this;
[email protected]bf5c2ff392009-07-08 16:24:331942}
1943
[email protected]8d3347f2009-07-09 22:00:211944RenderViewHostDelegate::Resource* TabContents::GetResourceDelegate() {
1945 return this;
[email protected]bf5c2ff392009-07-08 16:24:331946}
1947
[email protected]8d3347f2009-07-09 22:00:211948RenderViewHostDelegate::Save* TabContents::GetSaveDelegate() {
[email protected]420ae012009-04-24 05:16:321949 return save_package_.get(); // May be NULL, but we can return NULL.
1950}
1951
[email protected]8d3347f2009-07-09 22:00:211952RenderViewHostDelegate::Printing* TabContents::GetPrintingDelegate() {
1953 return &printing_;
1954}
1955
1956RenderViewHostDelegate::FavIcon* TabContents::GetFavIconDelegate() {
1957 return &fav_icon_helper_;
1958}
1959
[email protected]c54d269e2009-10-21 18:07:381960RenderViewHostDelegate::FormFieldHistory*
1961TabContents::GetFormFieldHistoryDelegate() {
[email protected]19289d02009-10-21 21:47:001962 if (form_field_history_manager_.get() == NULL)
1963 form_field_history_manager_.reset(new FormFieldHistoryManager(this));
1964 return form_field_history_manager_.get();
[email protected]bf5c2ff392009-07-08 16:24:331965}
1966
[email protected]21674df2009-10-28 19:51:211967RenderViewHostDelegate::AutoFill* TabContents::GetAutoFillDelegate() {
1968 if (autofill_manager_.get() == NULL)
1969 autofill_manager_.reset(new AutoFillManager(this));
1970 return autofill_manager_.get();
1971}
1972
[email protected]93623c5d2009-12-10 21:40:321973RendererPreferences TabContents::GetRendererPrefs(Profile* profile) const {
[email protected]840b1512009-07-21 16:53:461974 return renderer_preferences_;
[email protected]80d96fa2009-06-10 22:34:511975}
1976
[email protected]57c6a652009-05-04 07:58:341977TabContents* TabContents::GetAsTabContents() {
1978 return this;
1979}
1980
[email protected]3e7cf1362009-08-21 17:47:081981void TabContents::AddBlockedNotice(const GURL& url, const string16& reason) {
1982 CreateBlockedPopupContainerIfNecessary();
1983 blocked_popups_->AddBlockedNotice(url, reason);
1984}
1985
[email protected]7b291f92009-08-14 05:43:531986ViewType::Type TabContents::GetRenderViewType() const {
1987 return ViewType::TAB_CONTENTS;
1988}
1989
1990int TabContents::GetBrowserWindowID() const {
1991 return controller().window_id().id();
1992}
1993
[email protected]420ae012009-04-24 05:16:321994void TabContents::RenderViewCreated(RenderViewHost* render_view_host) {
[email protected]2e4633c2009-07-09 16:58:061995 NotificationService::current()->Notify(
1996 NotificationType::RENDER_VIEW_HOST_CREATED_FOR_TAB,
1997 Source<TabContents>(this),
1998 Details<RenderViewHost>(render_view_host));
[email protected]420ae012009-04-24 05:16:321999 NavigationEntry* entry = controller_.GetActiveEntry();
2000 if (!entry)
2001 return;
2002
2003 // When we're creating views, we're still doing initial setup, so we always
2004 // use the pending DOM UI rather than any possibly existing committed one.
2005 if (render_manager_.pending_dom_ui()) {
2006 render_manager_.pending_dom_ui()->RenderViewCreated(render_view_host);
2007 }
2008
2009 if (entry->IsViewSourceMode()) {
2010 // Put the renderer in view source mode.
2011 render_view_host->Send(
2012 new ViewMsg_EnableViewSourceMode(render_view_host->routing_id()));
2013 }
[email protected]0666aef2009-05-13 19:48:082014
2015 view()->RenderViewCreated(render_view_host);
[email protected]420ae012009-04-24 05:16:322016}
2017
2018void TabContents::RenderViewReady(RenderViewHost* rvh) {
2019 if (rvh != render_view_host()) {
2020 // Don't notify the world, since this came from a renderer in the
2021 // background.
2022 return;
2023 }
2024
2025 NotifyConnected();
[email protected]9d8a4642009-07-29 17:25:302026 bool was_crashed = is_crashed();
[email protected]420ae012009-04-24 05:16:322027 SetIsCrashed(false);
[email protected]9d8a4642009-07-29 17:25:302028
2029 // Restore the focus to the tab (otherwise the focus will be on the top
2030 // window).
2031 if (was_crashed && !FocusLocationBarByDefault())
2032 Focus();
[email protected]420ae012009-04-24 05:16:322033}
2034
2035void TabContents::RenderViewGone(RenderViewHost* rvh) {
2036 // Ask the print preview if this renderer was valuable.
2037 if (!printing_.OnRenderViewGone(rvh))
2038 return;
2039 if (rvh != render_view_host()) {
2040 // The pending page's RenderViewHost is gone.
2041 return;
2042 }
2043
2044 SetIsLoading(false, NULL);
2045 NotifyDisconnected();
2046 SetIsCrashed(true);
2047
[email protected]8e537302009-06-10 21:19:582048 // Tell the view that we've crashed so it can prepare the sad tab page.
[email protected]cf68b092010-01-15 20:05:552049 // Only do this if we're not in browser shutdown, so that TabContents
2050 // objects that are not in a browser (e.g., HTML dialogs) and thus are
2051 // visible do not flash a sad tab page.
2052 if (browser_shutdown::GetShutdownType() == browser_shutdown::NOT_VALID)
2053 view_->OnTabCrashed();
[email protected]420ae012009-04-24 05:16:322054
2055 // Hide any visible hung renderer warning for this web contents' process.
[email protected]e7b8d832009-11-20 22:41:002056 hung_renderer_dialog::HideForTabContents(this);
[email protected]420ae012009-04-24 05:16:322057}
2058
[email protected]2e4633c2009-07-09 16:58:062059void TabContents::RenderViewDeleted(RenderViewHost* rvh) {
2060 NotificationService::current()->Notify(
2061 NotificationType::RENDER_VIEW_HOST_DELETED,
2062 Source<TabContents>(this),
2063 Details<RenderViewHost>(rvh));
[email protected]fb7b79f72009-11-06 18:00:482064 render_manager_.RenderViewDeleted(rvh);
[email protected]2e4633c2009-07-09 16:58:062065}
2066
[email protected]420ae012009-04-24 05:16:322067void TabContents::DidNavigate(RenderViewHost* rvh,
2068 const ViewHostMsg_FrameNavigate_Params& params) {
[email protected]8030f012009-09-25 18:09:372069 int extra_invalidate_flags = 0;
2070
2071 if (PageTransition::IsMainFrame(params.transition)) {
[email protected]20ede072009-10-06 00:28:352072 bool was_bookmark_bar_visible = ShouldShowBookmarkBar();
[email protected]8030f012009-09-25 18:09:372073 bool was_extension_shelf_visible = IsExtensionShelfAlwaysVisible();
2074
[email protected]420ae012009-04-24 05:16:322075 render_manager_.DidNavigateMainFrame(rvh);
2076
[email protected]20ede072009-10-06 00:28:352077 if (was_bookmark_bar_visible != ShouldShowBookmarkBar())
[email protected]8030f012009-09-25 18:09:372078 extra_invalidate_flags |= INVALIDATE_BOOKMARK_BAR;
2079 if (was_extension_shelf_visible != IsExtensionShelfAlwaysVisible())
2080 extra_invalidate_flags |= INVALIDATE_EXTENSION_SHELF;
2081 }
2082
[email protected]420ae012009-04-24 05:16:322083 // Update the site of the SiteInstance if it doesn't have one yet.
2084 if (!GetSiteInstance()->has_site())
2085 GetSiteInstance()->SetSite(params.url);
2086
2087 // Need to update MIME type here because it's referred to in
2088 // UpdateNavigationCommands() called by RendererDidNavigate() to
2089 // determine whether or not to enable the encoding menu.
2090 // It's updated only for the main frame. For a subframe,
2091 // RenderView::UpdateURL does not set params.contents_mime_type.
2092 // (see http://code.google.com/p/chromium/issues/detail?id=2929 )
2093 // TODO(jungshik): Add a test for the encoding menu to avoid
2094 // regressing it again.
2095 if (PageTransition::IsMainFrame(params.transition))
2096 contents_mime_type_ = params.contents_mime_type;
2097
2098 NavigationController::LoadCommittedDetails details;
[email protected]8030f012009-09-25 18:09:372099 bool did_navigate = controller_.RendererDidNavigate(
2100 params, extra_invalidate_flags, &details);
[email protected]3e90d4a2009-07-03 17:38:392101
2102 // Update history. Note that this needs to happen after the entry is complete,
2103 // which WillNavigate[Main,Sub]Frame will do before this function is called.
[email protected]2e29d922009-10-22 22:13:352104 if (params.should_update_history &&
2105 (!delegate() || delegate()->ShouldAddNavigationsToHistory())) {
[email protected]3e90d4a2009-07-03 17:38:392106 // Most of the time, the displayURL matches the loaded URL, but for about:
2107 // URLs, we use a data: URL as the real value. We actually want to save
2108 // the about: URL to the history db and keep the data: URL hidden. This is
2109 // what the TabContents' URL getter does.
2110 UpdateHistoryForNavigation(GetURL(), details, params);
2111 }
2112
[email protected]76543b92009-08-31 17:27:452113 if (!did_navigate)
2114 return; // No navigation happened.
[email protected]420ae012009-04-24 05:16:322115
2116 // DO NOT ADD MORE STUFF TO THIS FUNCTION! Your component should either listen
2117 // for the appropriate notification (best) or you can add it to
2118 // DidNavigateMainFramePostCommit / DidNavigateAnyFramePostCommit (only if
2119 // necessary, please).
2120
2121 // Run post-commit tasks.
2122 if (details.is_main_frame)
2123 DidNavigateMainFramePostCommit(details, params);
2124 DidNavigateAnyFramePostCommit(rvh, details, params);
2125}
2126
2127void TabContents::UpdateState(RenderViewHost* rvh,
2128 int32 page_id,
2129 const std::string& state) {
2130 DCHECK(rvh == render_view_host());
2131
2132 // We must be prepared to handle state updates for any page, these occur
2133 // when the user is scrolling and entering form data, as well as when we're
2134 // leaving a page, in which case our state may have already been moved to
2135 // the next page. The navigation controller will look up the appropriate
2136 // NavigationEntry and update it when it is notified via the delegate.
2137
2138 int entry_index = controller_.GetEntryIndexWithPageID(
2139 GetSiteInstance(), page_id);
2140 if (entry_index < 0)
2141 return;
2142 NavigationEntry* entry = controller_.GetEntryAtIndex(entry_index);
2143
2144 if (state == entry->content_state())
2145 return; // Nothing to update.
2146 entry->set_content_state(state);
2147 controller_.NotifyEntryChanged(entry, entry_index);
2148}
2149
2150void TabContents::UpdateTitle(RenderViewHost* rvh,
2151 int32 page_id, const std::wstring& title) {
2152 // If we have a title, that's a pretty good indication that we've started
2153 // getting useful data.
2154 SetNotWaitingForResponse();
2155
2156 DCHECK(rvh == render_view_host());
2157 NavigationEntry* entry = controller_.GetEntryWithPageID(GetSiteInstance(),
2158 page_id);
2159 if (!entry || !UpdateTitleForEntry(entry, title))
2160 return;
2161
2162 // Broadcast notifications when the UI should be updated.
2163 if (entry == controller_.GetEntryAtOffset(0))
[email protected]f1cd5e82009-10-23 17:19:032164 NotifyNavigationStateChanged(INVALIDATE_TITLE);
[email protected]420ae012009-04-24 05:16:322165}
2166
[email protected]420ae012009-04-24 05:16:322167void TabContents::UpdateEncoding(RenderViewHost* render_view_host,
[email protected]41fc0322009-09-04 22:23:402168 const std::string& encoding) {
[email protected]420ae012009-04-24 05:16:322169 set_encoding(encoding);
2170}
2171
2172void TabContents::UpdateTargetURL(int32 page_id, const GURL& url) {
2173 if (delegate())
2174 delegate()->UpdateTargetURL(this, url);
2175}
2176
2177void TabContents::UpdateThumbnail(const GURL& url,
2178 const SkBitmap& bitmap,
2179 const ThumbnailScore& score) {
2180 // Tell History about this thumbnail
[email protected]449478302009-06-09 20:04:282181 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kThumbnailStore)) {
[email protected]72d0fc72009-07-09 17:30:492182 if (!profile()->IsOffTheRecord())
[email protected]42339402009-07-17 17:27:192183 profile()->GetThumbnailStore()->SetPageThumbnail(url, bitmap,
2184 score, true);
[email protected]449478302009-06-09 20:04:282185 } else {
2186 HistoryService* hs;
2187 if (!profile()->IsOffTheRecord() &&
2188 (hs = profile()->GetHistoryService(Profile::IMPLICIT_ACCESS))) {
2189 hs->SetPageThumbnail(url, bitmap, score);
2190 }
[email protected]420ae012009-04-24 05:16:322191 }
2192}
2193
[email protected]8922e1f2009-10-03 05:01:262194void TabContents::UpdateInspectorSettings(const std::string& raw_settings) {
[email protected]0df30122009-06-03 12:13:082195 profile()->GetPrefs()->SetString(prefs::kWebKitInspectorSettings,
[email protected]8922e1f2009-10-03 05:01:262196 UTF8ToWide(raw_settings));
[email protected]0df30122009-06-03 12:13:082197}
2198
[email protected]420ae012009-04-24 05:16:322199void TabContents::Close(RenderViewHost* rvh) {
[email protected]07707302009-11-06 00:50:292200 // The UI may be in an event-tracking loop, such as between the
2201 // mouse-down and mouse-up in text selection or a button click.
2202 // Defer the close until after tracking is complete, so that we
2203 // don't free objects out from under the UI.
2204 // TODO(shess): This could probably be integrated with the
2205 // IsDoingDrag() test below. Punting for now because I need more
2206 // research to understand how this impacts platforms other than Mac.
2207 // TODO(shess): This could get more fine-grained. For instance,
2208 // closing a tab in another window while selecting text in the
2209 // current window's Omnibox should be just fine.
2210 if (view()->IsEventTracking()) {
2211 view()->CloseTabAfterEventTracking();
2212 return;
2213 }
2214
[email protected]24a4d1062009-07-10 23:10:422215 // If we close the tab while we're in the middle of a drag, we'll crash.
2216 // Instead, cancel the drag and close it as soon as the drag ends.
2217 if (view()->IsDoingDrag()) {
2218 view()->CancelDragAndCloseTab();
2219 return;
2220 }
2221
[email protected]420ae012009-04-24 05:16:322222 // Ignore this if it comes from a RenderViewHost that we aren't showing.
2223 if (delegate() && rvh == render_view_host())
2224 delegate()->CloseContents(this);
2225}
2226
2227void TabContents::RequestMove(const gfx::Rect& new_bounds) {
2228 if (delegate() && delegate()->IsPopup(this))
2229 delegate()->MoveContents(this, new_bounds);
2230}
2231
[email protected]7ab1e7d62009-10-14 23:32:012232void TabContents::DidStartLoading() {
[email protected]420ae012009-04-24 05:16:322233 SetIsLoading(true, NULL);
2234}
2235
[email protected]7ab1e7d62009-10-14 23:32:012236void TabContents::DidStopLoading() {
[email protected]420ae012009-04-24 05:16:322237 scoped_ptr<LoadNotificationDetails> details;
2238
2239 NavigationEntry* entry = controller_.GetActiveEntry();
2240 // An entry may not exist for a stop when loading an initial blank page or
2241 // if an iframe injected by script into a blank page finishes loading.
2242 if (entry) {
[email protected]420ae012009-04-24 05:16:322243 base::TimeDelta elapsed = base::TimeTicks::Now() - current_load_start_;
2244
2245 details.reset(new LoadNotificationDetails(
[email protected]ebe89e062009-08-13 23:16:542246 entry->virtual_url(),
[email protected]420ae012009-04-24 05:16:322247 entry->transition_type(),
2248 elapsed,
2249 &controller_,
2250 controller_.GetCurrentEntryIndex()));
2251 }
2252
2253 // Tell PasswordManager we've finished a page load, which serves as a
2254 // green light to save pending passwords and reset itself.
2255 GetPasswordManager()->DidStopLoading();
2256
2257 SetIsLoading(false, details.get());
2258}
2259
[email protected]420ae012009-04-24 05:16:322260void TabContents::DidRedirectProvisionalLoad(int32 page_id,
2261 const GURL& source_url,
2262 const GURL& target_url) {
2263 NavigationEntry* entry;
2264 if (page_id == -1)
2265 entry = controller_.pending_entry();
2266 else
2267 entry = controller_.GetEntryWithPageID(GetSiteInstance(), page_id);
2268 if (!entry || entry->url() != source_url)
2269 return;
2270 entry->set_url(target_url);
2271}
2272
[email protected]420ae012009-04-24 05:16:322273void TabContents::RequestOpenURL(const GURL& url, const GURL& referrer,
2274 WindowOpenDisposition disposition) {
2275 if (render_manager_.dom_ui()) {
2276 // When we're a DOM UI, it will provide a page transition type for us (this
2277 // is so the new tab page can specify AUTO_BOOKMARK for automatically
2278 // generated suggestions).
2279 //
2280 // Note also that we hide the referrer for DOM UI pages. We don't really
[email protected]60e448982009-05-06 04:21:162281 // want web sites to see a referrer of "chrome://blah" (and some
2282 // chrome: URLs might have search terms or other stuff we don't want to
[email protected]420ae012009-04-24 05:16:322283 // send to the site), so we send no referrer.
2284 OpenURL(url, GURL(), disposition,
2285 render_manager_.dom_ui()->link_transition_type());
2286 } else {
2287 OpenURL(url, referrer, disposition, PageTransition::LINK);
2288 }
2289}
2290
[email protected]1ee614862009-07-06 20:29:182291void TabContents::DomOperationResponse(const std::string& json_string,
2292 int automation_id) {
2293}
2294
[email protected]420ae012009-04-24 05:16:322295void TabContents::ProcessDOMUIMessage(const std::string& message,
[email protected]e4dad9fb2009-10-06 18:15:582296 const Value* content,
[email protected]811bfe32009-07-01 08:46:252297 int request_id,
2298 bool has_callback) {
[email protected]420ae012009-04-24 05:16:322299 if (!render_manager_.dom_ui()) {
[email protected]c2e74fe82009-09-03 17:57:442300 // This can happen if someone uses window.open() to open an extension URL
2301 // from a non-extension context.
2302 render_view_host()->BlockExtensionRequest(request_id);
[email protected]420ae012009-04-24 05:16:322303 return;
2304 }
[email protected]811bfe32009-07-01 08:46:252305 render_manager_.dom_ui()->ProcessDOMUIMessage(message, content, request_id,
2306 has_callback);
[email protected]420ae012009-04-24 05:16:322307}
2308
2309void TabContents::ProcessExternalHostMessage(const std::string& message,
2310 const std::string& origin,
2311 const std::string& target) {
2312 if (delegate())
2313 delegate()->ForwardMessageToExternalHost(message, origin, target);
2314}
2315
[email protected]420ae012009-04-24 05:16:322316void TabContents::RunFileChooser(bool multiple_files,
2317 const string16& title,
2318 const FilePath& default_file) {
2319 if (!select_file_dialog_.get())
2320 select_file_dialog_ = SelectFileDialog::Create(this);
2321 SelectFileDialog::Type dialog_type =
2322 multiple_files ? SelectFileDialog::SELECT_OPEN_MULTI_FILE :
2323 SelectFileDialog::SELECT_OPEN_FILE;
2324 select_file_dialog_->SelectFile(dialog_type, title, default_file,
2325 NULL, 0, FILE_PATH_LITERAL(""),
2326 view_->GetTopLevelNativeWindow(), NULL);
2327}
2328
2329void TabContents::RunJavaScriptMessage(
2330 const std::wstring& message,
2331 const std::wstring& default_prompt,
2332 const GURL& frame_url,
2333 const int flags,
2334 IPC::Message* reply_msg,
2335 bool* did_suppress_message) {
2336 // Suppress javascript messages when requested and when inside a constrained
2337 // popup window (because that activates them and breaks them out of the
2338 // constrained window jail).
[email protected]91ed49a2009-07-07 21:29:552339 // Also suppress messages when showing an interstitial. The interstitial is
2340 // shown over the previous page, we don't want the hidden page dialogs to
2341 // interfere with the interstitial.
2342 bool suppress_this_message = suppress_javascript_messages_ ||
2343 showing_interstitial_page();
[email protected]420ae012009-04-24 05:16:322344 if (delegate())
2345 suppress_this_message |=
[email protected]b6c874582009-05-08 19:38:312346 (delegate()->GetConstrainingContents(this) != this);
[email protected]420ae012009-04-24 05:16:322347
2348 *did_suppress_message = suppress_this_message;
2349
2350 if (!suppress_this_message) {
2351 base::TimeDelta time_since_last_message(
2352 base::TimeTicks::Now() - last_javascript_message_dismissal_);
2353 bool show_suppress_checkbox = false;
2354 // Show a checkbox offering to suppress further messages if this message is
2355 // being displayed within kJavascriptMessageExpectedDelay of the last one.
2356 if (time_since_last_message <
2357 base::TimeDelta::FromMilliseconds(kJavascriptMessageExpectedDelay))
2358 show_suppress_checkbox = true;
2359
[email protected]d82ed61e2009-06-16 02:46:222360 RunJavascriptMessageBox(this, frame_url, flags, message, default_prompt,
[email protected]420ae012009-04-24 05:16:322361 show_suppress_checkbox, reply_msg);
2362 } else {
2363 // If we are suppressing messages, just reply as is if the user immediately
2364 // pressed "Cancel".
[email protected]beb440c2009-11-06 04:08:542365 OnMessageBoxClosed(reply_msg, false, std::wstring());
[email protected]420ae012009-04-24 05:16:322366 }
2367}
2368
2369void TabContents::RunBeforeUnloadConfirm(const std::wstring& message,
2370 IPC::Message* reply_msg) {
[email protected]7ab1e7d62009-10-14 23:32:012371 is_showing_before_unload_dialog_ = true;
[email protected]d82ed61e2009-06-16 02:46:222372 RunBeforeUnloadDialog(this, message, reply_msg);
[email protected]420ae012009-04-24 05:16:322373}
2374
2375void TabContents::ShowModalHTMLDialog(const GURL& url, int width, int height,
2376 const std::string& json_arguments,
2377 IPC::Message* reply_msg) {
2378 if (delegate()) {
2379 HtmlDialogUIDelegate* dialog_delegate =
2380 new ModalHtmlDialogDelegate(url, width, height, json_arguments,
[email protected]d82ed61e2009-06-16 02:46:222381 reply_msg, this);
[email protected]420ae012009-04-24 05:16:322382 delegate()->ShowHtmlDialog(dialog_delegate, NULL);
2383 }
2384}
2385
2386void TabContents::PasswordFormsSeen(
[email protected]ed3fb032009-06-16 19:50:562387 const std::vector<webkit_glue::PasswordForm>& forms) {
[email protected]420ae012009-04-24 05:16:322388 GetPasswordManager()->PasswordFormsSeen(forms);
2389}
2390
[email protected]420ae012009-04-24 05:16:322391// Checks to see if we should generate a keyword based on the OSDD, and if
2392// necessary uses TemplateURLFetcher to download the OSDD and create a keyword.
2393void TabContents::PageHasOSDD(RenderViewHost* render_view_host,
2394 int32 page_id, const GURL& url,
2395 bool autodetected) {
2396 // Make sure page_id is the current page, and the TemplateURLModel is loaded.
2397 DCHECK(url.is_valid());
2398 if (!IsActiveEntry(page_id))
2399 return;
2400 TemplateURLModel* url_model = profile()->GetTemplateURLModel();
2401 if (!url_model)
2402 return;
2403 if (!url_model->loaded()) {
2404 url_model->Load();
2405 return;
2406 }
2407 if (!profile()->GetTemplateURLFetcher())
2408 return;
2409
2410 if (profile()->IsOffTheRecord())
2411 return;
2412
2413 const NavigationEntry* entry = controller_.GetLastCommittedEntry();
2414 DCHECK(entry);
2415
2416 const NavigationEntry* base_entry = entry;
2417 if (IsFormSubmit(base_entry)) {
2418 // If the current page is a form submit, find the last page that was not
2419 // a form submit and use its url to generate the keyword from.
2420 int index = controller_.last_committed_entry_index() - 1;
2421 while (index >= 0 && IsFormSubmit(controller_.GetEntryAtIndex(index)))
2422 index--;
2423 if (index >= 0)
2424 base_entry = controller_.GetEntryAtIndex(index);
2425 else
2426 base_entry = NULL;
2427 }
2428
2429 // We want to use the user typed URL if available since that represents what
2430 // the user typed to get here, and fall back on the regular URL if not.
2431 if (!base_entry)
2432 return;
2433 GURL keyword_url = base_entry->user_typed_url().is_valid() ?
2434 base_entry->user_typed_url() : base_entry->url();
2435 if (!keyword_url.is_valid())
2436 return;
2437 std::wstring keyword = TemplateURLModel::GenerateKeyword(keyword_url,
2438 autodetected);
[email protected]a88dbdd52010-01-15 19:25:532439
2440 // For JS added OSDD empty keyword is OK because we will generate keyword
2441 // later from OSDD content.
2442 if (autodetected && keyword.empty())
[email protected]420ae012009-04-24 05:16:322443 return;
2444 const TemplateURL* template_url =
2445 url_model->GetTemplateURLForKeyword(keyword);
2446 if (template_url && (!template_url->safe_for_autoreplace() ||
2447 template_url->originating_url() == url)) {
2448 // Either there is a user created TemplateURL for this keyword, or the
2449 // keyword has the same OSDD url and we've parsed it.
2450 return;
2451 }
2452
2453 // Download the OpenSearch description document. If this is successful a
2454 // new keyword will be created when done.
[email protected]420ae012009-04-24 05:16:322455 profile()->GetTemplateURLFetcher()->ScheduleDownload(
2456 keyword,
2457 url,
2458 base_entry->favicon().url(),
[email protected]d0cc9fb52009-06-23 02:26:162459 this,
[email protected]420ae012009-04-24 05:16:322460 autodetected);
2461}
2462
[email protected]420ae012009-04-24 05:16:322463GURL TabContents::GetAlternateErrorPageURL() const {
2464 GURL url;
2465 // Disable alternate error pages when in OffTheRecord/Incognito mode.
2466 if (profile()->IsOffTheRecord())
2467 return url;
2468
2469 PrefService* prefs = profile()->GetPrefs();
2470 DCHECK(prefs);
2471 if (prefs->GetBoolean(prefs::kAlternateErrorPagesEnabled)) {
[email protected]a33457b82009-09-21 23:07:152472 url = google_util::AppendGoogleLocaleParam(
2473 GURL(google_util::kLinkDoctorBaseURL));
[email protected]420ae012009-04-24 05:16:322474 url = google_util::AppendGoogleTLDParam(url);
2475 }
2476 return url;
2477}
2478
2479WebPreferences TabContents::GetWebkitPrefs() {
2480 PrefService* prefs = render_view_host()->process()->profile()->GetPrefs();
[email protected]7ccddb8c2009-08-04 17:36:552481 bool is_dom_ui = false;
2482 return RenderViewHostDelegateHelper::GetWebkitPrefs(prefs, is_dom_ui);
[email protected]420ae012009-04-24 05:16:322483}
2484
[email protected]fa1cf0b82010-01-15 21:49:442485void TabContents::OnIgnoredUIEvent() {
2486 if (constrained_window_count()) {
2487 ConstrainedWindow* window = *constrained_window_begin();
2488 window->FocusConstrainedWindow();
2489 }
2490}
2491
[email protected]420ae012009-04-24 05:16:322492void TabContents::OnJSOutOfMemory() {
2493 AddInfoBar(new SimpleAlertInfoBarDelegate(
2494 this, l10n_util::GetString(IDS_JS_OUT_OF_MEMORY_PROMPT), NULL));
2495}
2496
[email protected]420ae012009-04-24 05:16:322497void TabContents::OnCrossSiteResponse(int new_render_process_host_id,
2498 int new_request_id) {
[email protected]57c6a652009-05-04 07:58:342499 // Allows the TabContents to react when a cross-site response is ready to be
[email protected]420ae012009-04-24 05:16:322500 // delivered to a pending RenderViewHost. We must first run the onunload
2501 // handler of the old RenderViewHost before we can allow it to proceed.
2502 render_manager_.OnCrossSiteResponse(new_render_process_host_id,
2503 new_request_id);
2504}
2505
2506bool TabContents::CanBlur() const {
2507 return delegate() ? delegate()->CanBlur() : true;
2508}
2509
2510gfx::Rect TabContents::GetRootWindowResizerRect() const {
2511 if (delegate())
2512 return delegate()->GetRootWindowResizerRect();
2513 return gfx::Rect();
2514}
2515
2516void TabContents::RendererUnresponsive(RenderViewHost* rvh,
2517 bool is_during_unload) {
2518 if (is_during_unload) {
2519 // Hang occurred while firing the beforeunload/unload handler.
2520 // Pretend the handler fired so tab closing continues as if it had.
[email protected]829e7612009-04-25 01:15:112521 rvh->set_sudden_termination_allowed(true);
[email protected]420ae012009-04-24 05:16:322522
2523 if (!render_manager_.ShouldCloseTabOnUnresponsiveRenderer())
2524 return;
2525
2526 // If the tab hangs in the beforeunload/unload handler there's really
2527 // nothing we can do to recover. Pretend the unload listeners have
2528 // all fired and close the tab. If the hang is in the beforeunload handler
2529 // then the user will not have the option of cancelling the close.
2530 Close(rvh);
2531 return;
2532 }
2533
2534 if (render_view_host() && render_view_host()->IsRenderViewLive())
[email protected]e7b8d832009-11-20 22:41:002535 hung_renderer_dialog::ShowForTabContents(this);
[email protected]420ae012009-04-24 05:16:322536}
2537
2538void TabContents::RendererResponsive(RenderViewHost* render_view_host) {
[email protected]e7b8d832009-11-20 22:41:002539 hung_renderer_dialog::HideForTabContents(this);
[email protected]420ae012009-04-24 05:16:322540}
2541
2542void TabContents::LoadStateChanged(const GURL& url,
[email protected]094e5b22009-09-25 04:23:562543 net::LoadState load_state,
2544 uint64 upload_position,
2545 uint64 upload_size) {
[email protected]420ae012009-04-24 05:16:322546 load_state_ = load_state;
[email protected]094e5b22009-09-25 04:23:562547 upload_position_ = upload_position;
2548 upload_size_ = upload_size;
[email protected]d686e812009-06-03 19:10:292549 std::wstring languages =
2550 profile()->GetPrefs()->GetString(prefs::kAcceptLanguages);
[email protected]d686e812009-06-03 19:10:292551 std::string host = url.host();
[email protected]ce85f602009-11-07 01:34:532552 load_state_host_ =
2553 net::IDNToUnicode(host.c_str(), host.size(), languages, NULL);
[email protected]420ae012009-04-24 05:16:322554 if (load_state_ == net::LOAD_STATE_READING_RESPONSE)
2555 SetNotWaitingForResponse();
2556 if (is_loading())
[email protected]c9cd2222009-05-06 05:16:502557 NotifyNavigationStateChanged(INVALIDATE_LOAD | INVALIDATE_TAB);
[email protected]420ae012009-04-24 05:16:322558}
2559
[email protected]1ee614862009-07-06 20:29:182560bool TabContents::IsExternalTabContainer() const {
2561 if (!delegate())
2562 return false;
2563
2564 return delegate()->IsExternalTabContainer();
2565}
2566
[email protected]1ee614862009-07-06 20:29:182567void TabContents::DidInsertCSS() {
2568 // This RVHDelegate function is used for extensions and not us.
[email protected]420ae012009-04-24 05:16:322569}
2570
[email protected]130efb02009-09-18 18:54:352571void TabContents::FocusedNodeChanged() {
2572 NotificationService::current()->Notify(
2573 NotificationType::FOCUS_CHANGED_IN_PAGE,
2574 Source<RenderViewHost>(render_view_host()),
2575 NotificationService::NoDetails());
2576}
2577
[email protected]420ae012009-04-24 05:16:322578void TabContents::FileSelected(const FilePath& path,
2579 int index, void* params) {
2580 render_view_host()->FileSelected(path);
2581}
2582
2583void TabContents::MultiFilesSelected(const std::vector<FilePath>& files,
2584 void* params) {
2585 render_view_host()->MultiFilesSelected(files);
2586}
2587
2588void TabContents::FileSelectionCanceled(void* params) {
2589 // If the user cancels choosing a file to upload we pass back an
2590 // empty vector.
2591 render_view_host()->MultiFilesSelected(std::vector<FilePath>());
2592}
2593
2594void TabContents::BeforeUnloadFiredFromRenderManager(
2595 bool proceed,
2596 bool* proceed_to_fire_unload) {
2597 if (delegate())
2598 delegate()->BeforeUnloadFired(this, proceed, proceed_to_fire_unload);
2599}
2600
2601void TabContents::UpdateRenderViewSizeForRenderManager() {
2602 // TODO(brettw) this is a hack. See TabContentsView::SizeContents.
2603 view_->SizeContents(view_->GetContainerSize());
2604}
2605
2606DOMUI* TabContents::CreateDOMUIForRenderManager(const GURL& url) {
[email protected]d82ed61e2009-06-16 02:46:222607 return DOMUIFactory::CreateDOMUIForURL(this, url);
[email protected]420ae012009-04-24 05:16:322608}
2609
2610NavigationEntry*
2611TabContents::GetLastCommittedNavigationEntryForRenderManager() {
2612 return controller_.GetLastCommittedEntry();
2613}
2614
2615bool TabContents::CreateRenderViewForRenderManager(
2616 RenderViewHost* render_view_host) {
[email protected]811bfe32009-07-01 08:46:252617 // If the pending navigation is to a DOMUI, tell the RenderView about any
2618 // bindings it will need enabled.
[email protected]420ae012009-04-24 05:16:322619 if (render_manager_.pending_dom_ui())
[email protected]811bfe32009-07-01 08:46:252620 render_view_host->AllowBindings(
2621 render_manager_.pending_dom_ui()->bindings());
[email protected]1adff062009-06-02 18:39:552622
[email protected]420ae012009-04-24 05:16:322623 RenderWidgetHostView* rwh_view = view_->CreateViewForWidget(render_view_host);
[email protected]1a98a932009-11-17 00:12:522624
2625 scoped_refptr<URLRequestContextGetter> request_context = request_context_;
2626 if (!request_context.get())
2627 request_context = profile()->GetRequestContext();
2628
2629 if (!render_view_host->CreateRenderView(request_context))
[email protected]420ae012009-04-24 05:16:322630 return false;
2631
2632 // Now that the RenderView has been created, we need to tell it its size.
2633 rwh_view->SetSize(view_->GetContainerSize());
2634
2635 UpdateMaxPageIDIfNecessary(render_view_host->site_instance(),
2636 render_view_host);
2637 return true;
2638}
2639
2640void TabContents::Observe(NotificationType type,
2641 const NotificationSource& source,
2642 const NotificationDetails& details) {
2643 switch (type.value) {
2644 case NotificationType::BOOKMARK_MODEL_LOADED:
2645 // BookmarkModel finished loading, fall through to update starred state.
2646 case NotificationType::URLS_STARRED: {
2647 // Somewhere, a URL has been starred.
2648 // Ignore notifications for profiles other than our current one.
2649 Profile* source_profile = Source<Profile>(source).ptr();
2650 if (!source_profile || !source_profile->IsSameProfile(profile()))
2651 return;
2652
2653 UpdateStarredStateForCurrentURL();
2654 break;
2655 }
2656 case NotificationType::PREF_CHANGED: {
2657 std::wstring* pref_name_in = Details<std::wstring>(details).ptr();
2658 DCHECK(Source<PrefService>(source).ptr() == profile()->GetPrefs());
2659 if (*pref_name_in == prefs::kAlternateErrorPagesEnabled) {
2660 UpdateAlternateErrorPageURL();
2661 } else if (*pref_name_in == prefs::kDefaultCharset ||
2662 StartsWithASCII(WideToUTF8(*pref_name_in), "webkit.webprefs.", true)
2663 ) {
2664 UpdateWebPreferences();
2665 } else {
2666 NOTREACHED() << "unexpected pref change notification" << *pref_name_in;
2667 }
2668 break;
2669 }
2670 case NotificationType::RENDER_WIDGET_HOST_DESTROYED:
2671 view_->RenderWidgetHostDestroyed(Source<RenderWidgetHost>(source).ptr());
2672 break;
2673
2674 case NotificationType::NAV_ENTRY_COMMITTED: {
2675 DCHECK(&controller_ == Source<NavigationController>(source).ptr());
2676
2677 NavigationController::LoadCommittedDetails& committed_details =
2678 *(Details<NavigationController::LoadCommittedDetails>(details).ptr());
2679 ExpireInfoBars(committed_details);
2680 break;
2681 }
2682
[email protected]d3ba77272009-09-03 00:06:092683#if defined(OS_LINUX)
2684 case NotificationType::BROWSER_THEME_CHANGED: {
[email protected]7ff431e2010-01-07 18:18:542685 renderer_preferences_util::UpdateFromSystemSettings(
2686 &renderer_preferences_, profile());
[email protected]d3ba77272009-09-03 00:06:092687 render_view_host()->SyncRendererPrefs();
2688 break;
2689 }
2690#endif
2691
[email protected]420ae012009-04-24 05:16:322692 default:
2693 NOTREACHED();
2694 }
2695}
[email protected]aacd7cc2009-09-02 14:51:102696
[email protected]beb440c2009-11-06 04:08:542697std::wstring TabContents::GetMessageBoxTitle(const GURL& frame_url,
2698 bool is_alert) {
2699 if (!frame_url.has_host())
2700 return l10n_util::GetString(
2701 is_alert ? IDS_JAVASCRIPT_ALERT_DEFAULT_TITLE
2702 : IDS_JAVASCRIPT_MESSAGEBOX_DEFAULT_TITLE);
2703
2704 // We really only want the scheme, hostname, and port.
2705 GURL::Replacements replacements;
2706 replacements.ClearUsername();
2707 replacements.ClearPassword();
2708 replacements.ClearPath();
2709 replacements.ClearQuery();
2710 replacements.ClearRef();
2711 GURL clean_url = frame_url.ReplaceComponents(replacements);
2712
2713 // TODO(brettw) it should be easier than this to do the correct language
2714 // handling without getting the accept language from the profile.
2715 std::wstring base_address = gfx::ElideUrl(clean_url, gfx::Font(), 0,
2716 profile()->GetPrefs()->GetString(prefs::kAcceptLanguages));
2717 // Force URL to have LTR directionality.
2718 if (l10n_util::GetTextDirection() == l10n_util::RIGHT_TO_LEFT)
2719 l10n_util::WrapStringWithLTRFormatting(&base_address);
2720
2721 return l10n_util::GetStringF(
2722 is_alert ? IDS_JAVASCRIPT_ALERT_TITLE : IDS_JAVASCRIPT_MESSAGEBOX_TITLE,
2723 base_address);
2724}
2725
2726gfx::NativeWindow TabContents::GetMessageBoxRootWindow() {
2727 return view_->GetTopLevelNativeWindow();
2728}
2729
2730void TabContents::OnMessageBoxClosed(IPC::Message* reply_msg,
2731 bool success,
2732 const std::wstring& prompt) {
2733 last_javascript_message_dismissal_ = base::TimeTicks::Now();
2734 if (is_showing_before_unload_dialog_ && !success) {
2735 // If a beforeunload dialog is canceled, we need to stop the throbber from
2736 // spinning, since we forced it to start spinning in Navigate.
2737 DidStopLoading();
2738
2739 tab_close_start_time_ = base::TimeTicks();
2740 }
2741 is_showing_before_unload_dialog_ = false;
2742 render_view_host()->JavaScriptMessageBoxClosed(reply_msg, success, prompt);
2743}
2744
2745void TabContents::SetSuppressMessageBoxes(bool suppress_message_boxes) {
2746 set_suppress_javascript_messages(suppress_message_boxes);
2747}
2748
[email protected]41fc0322009-09-04 22:23:402749void TabContents::set_encoding(const std::string& encoding) {
[email protected]aacd7cc2009-09-02 14:51:102750 encoding_ = CharacterEncoding::GetCanonicalEncodingNameByAliasName(encoding);
2751}