blob: 4641f3bd61653b8c38fc8b484241c0a56e0e6a23 [file] [log] [blame]
[email protected]7e7414ae2010-01-26 20:19:291// Copyright (c) 2010 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]d4a00a72010-01-29 01:44:4248#include "chrome/browser/renderer_host/translation_service.h"
[email protected]96d185d2009-04-24 03:28:5449#include "chrome/browser/renderer_host/web_cache_manager.h"
[email protected]93623c5d2009-12-10 21:40:3250#include "chrome/browser/renderer_preferences_util.h"
[email protected]c37e3b62010-01-22 00:15:1751#include "chrome/browser/sessions/session_types.h"
[email protected]bd1ad682009-05-15 22:19:1752#include "chrome/browser/tab_contents/infobar_delegate.h"
[email protected]7e383692009-06-12 19:14:5453#include "chrome/browser/tab_contents/interstitial_page.h"
[email protected]f3ec7742009-01-15 00:59:1654#include "chrome/browser/tab_contents/navigation_entry.h"
[email protected]de34a962009-06-26 23:22:4355#include "chrome/browser/tab_contents/provisional_load_details.h"
[email protected]f3ec7742009-01-15 00:59:1656#include "chrome/browser/tab_contents/tab_contents_delegate.h"
[email protected]fdd61c62009-04-22 19:22:5757#include "chrome/browser/tab_contents/tab_contents_view.h"
[email protected]58dca552009-06-17 00:35:0258#include "chrome/browser/tab_contents/thumbnail_generator.h"
[email protected]449478302009-06-09 20:04:2859#include "chrome/browser/thumbnail_store.h"
[email protected]420ae012009-04-24 05:16:3260#include "chrome/browser/search_engines/template_url_fetcher.h"
61#include "chrome/browser/search_engines/template_url_model.h"
[email protected]35f7d212009-04-29 21:19:2762#include "chrome/common/chrome_switches.h"
[email protected]5d246db22009-10-27 06:17:5763#include "chrome/common/extensions/extension_action.h"
[email protected]bfd04a62009-02-01 18:16:5664#include "chrome/common/notification_service.h"
[email protected]89d6e6e2009-12-03 23:06:5165#include "chrome/common/platform_util.h"
initial.commit09911bf2008-07-26 23:55:2966#include "chrome/common/pref_names.h"
[email protected]1eb89e82008-08-15 12:27:0367#include "chrome/common/pref_service.h"
[email protected]420ae012009-04-24 05:16:3268#include "chrome/common/render_messages.h"
[email protected]1ee614862009-07-06 20:29:1869#include "chrome/common/renderer_preferences.h"
[email protected]96d185d2009-04-24 03:28:5470#include "chrome/common/url_constants.h"
[email protected]8897c382009-05-06 17:53:2671#include "grit/generated_resources.h"
[email protected]420ae012009-04-24 05:16:3272#include "grit/locale_settings.h"
[email protected]fd626782010-01-22 05:26:0673#include "grit/platform_locale_settings.h"
[email protected]420ae012009-04-24 05:16:3274#include "net/base/mime_util.h"
75#include "net/base/net_errors.h"
[email protected]d686e812009-06-03 19:10:2976#include "net/base/net_util.h"
[email protected]420ae012009-04-24 05:16:3277#include "net/base/registry_controlled_domain.h"
[email protected]1ee614862009-07-06 20:29:1878#include "webkit/glue/password_form.h"
79#include "webkit/glue/webpreferences.h"
[email protected]3e45ba92009-02-20 21:09:0080
[email protected]304f8792009-08-19 21:28:0981#if defined(OS_CHROMEOS)
82// For GdkScreen
83#include <gdk/gdk.h>
[email protected]0ba5c5e2009-08-20 16:47:1384#endif // defined(OS_CHROMEOS)
85
[email protected]420ae012009-04-24 05:16:3286// Cross-Site Navigations
87//
88// If a TabContents is told to navigate to a different web site (as determined
89// by SiteInstance), it will replace its current RenderViewHost with a new
90// RenderViewHost dedicated to the new SiteInstance. This works as follows:
91//
92// - Navigate determines whether the destination is cross-site, and if so,
93// it creates a pending_render_view_host_ and moves into the PENDING
94// RendererState.
95// - The pending RVH is "suspended," so that no navigation messages are sent to
96// its renderer until the onbeforeunload JavaScript handler has a chance to
97// run in the current RVH.
98// - The pending RVH tells CrossSiteRequestManager (a thread-safe singleton)
99// that it has a pending cross-site request. ResourceDispatcherHost will
100// check for this when the response arrives.
101// - The current RVH runs its onbeforeunload handler. If it returns false, we
102// cancel all the pending logic and go back to NORMAL. Otherwise we allow
103// the pending RVH to send the navigation request to its renderer.
104// - ResourceDispatcherHost receives a ResourceRequest on the IO thread. It
105// checks CrossSiteRequestManager to see that the RVH responsible has a
106// pending cross-site request, and then installs a CrossSiteEventHandler.
107// - When RDH receives a response, the BufferedEventHandler determines whether
108// it is a download. If so, it sends a message to the new renderer causing
109// it to cancel the request, and the download proceeds in the download
110// thread. For now, we stay in a PENDING state (with a pending RVH) until
[email protected]57c6a652009-05-04 07:58:34111// the next DidNavigate event for this TabContents. This isn't ideal, but it
[email protected]420ae012009-04-24 05:16:32112// doesn't affect any functionality.
113// - After RDH receives a response and determines that it is safe and not a
114// download, it pauses the response to first run the old page's onunload
115// handler. It does this by asynchronously calling the OnCrossSiteResponse
[email protected]57c6a652009-05-04 07:58:34116// method of TabContents on the UI thread, which sends a ClosePage message
[email protected]420ae012009-04-24 05:16:32117// to the current RVH.
118// - Once the onunload handler is finished, a ClosePage_ACK message is sent to
119// the ResourceDispatcherHost, who unpauses the response. Data is then sent
120// to the pending RVH.
121// - The pending renderer sends a FrameNavigate message that invokes the
122// DidNavigate method. This replaces the current RVH with the
123// pending RVH and goes back to the NORMAL RendererState.
124
125namespace {
126
127// Amount of time we wait between when a key event is received and the renderer
128// is queried for its state and pushed to the NavigationEntry.
129const int kQueryStateDelay = 5000;
130
131const int kSyncWaitDelay = 40;
132
[email protected]304f8792009-08-19 21:28:09133#if defined(OS_CHROMEOS)
134// If a popup window is bigger than this fraction of the screen on chrome os,
135// turn it into a tab
136const float kMaxWidthFactor = 0.5;
137const float kMaxHeightFactor = 0.6;
138#endif
139
[email protected]420ae012009-04-24 05:16:32140// If another javascript message box is displayed within
141// kJavascriptMessageExpectedDelay of a previous javascript message box being
142// dismissed, display an option to suppress future message boxes from this
143// contents.
144const int kJavascriptMessageExpectedDelay = 1000;
145
[email protected]420ae012009-04-24 05:16:32146// The list of prefs we want to observe.
147const wchar_t* kPrefsToObserve[] = {
148 prefs::kAlternateErrorPagesEnabled,
149 prefs::kWebKitJavaEnabled,
150 prefs::kWebKitJavascriptEnabled,
151 prefs::kWebKitLoadsImagesAutomatically,
152 prefs::kWebKitPluginsEnabled,
153 prefs::kWebKitUsesUniversalDetector,
154 prefs::kWebKitSerifFontFamily,
155 prefs::kWebKitSansSerifFontFamily,
156 prefs::kWebKitFixedFontFamily,
157 prefs::kWebKitDefaultFontSize,
158 prefs::kWebKitDefaultFixedFontSize,
159 prefs::kDefaultCharset
160 // kWebKitStandardFontIsSerif needs to be added
161 // if we let users pick which font to use, serif or sans-serif when
162 // no font is specified or a CSS generic family (serif or sans-serif)
163 // is not specified.
164};
165
166const int kPrefsToObserveLength = arraysize(kPrefsToObserve);
167
[email protected]420ae012009-04-24 05:16:32168// Returns true if the entry's transition type is FORM_SUBMIT.
169bool IsFormSubmit(const NavigationEntry* entry) {
170 return (PageTransition::StripQualifier(entry->transition_type()) ==
171 PageTransition::FORM_SUBMIT);
172}
173
174#if defined(OS_WIN)
175
176BOOL CALLBACK InvalidateWindow(HWND hwnd, LPARAM lparam) {
177 // Note: erase is required to properly paint some widgets borders. This can
178 // be seen with textfields.
179 InvalidateRect(hwnd, NULL, TRUE);
180 return TRUE;
181}
182#endif
183
[email protected]5e369672009-11-03 23:48:30184ViewMsg_Navigate_Params::NavigationType GetNavigationType(
185 Profile* profile, const NavigationEntry& entry, bool reload) {
186 if (reload)
187 return ViewMsg_Navigate_Params::RELOAD;
188
189 if (entry.restore_type() == NavigationEntry::RESTORE_LAST_SESSION &&
190 profile->DidLastSessionExitCleanly())
191 return ViewMsg_Navigate_Params::RESTORE;
192
193 return ViewMsg_Navigate_Params::NORMAL;
194}
195
196void MakeNavigateParams(Profile* profile, const NavigationEntry& entry,
197 bool reload, ViewMsg_Navigate_Params* params) {
[email protected]056de2d2009-06-26 16:41:34198 params->page_id = entry.page_id();
199 params->url = entry.url();
200 params->referrer = entry.referrer();
201 params->transition = entry.transition_type();
202 params->state = entry.content_state();
[email protected]5e369672009-11-03 23:48:30203 params->navigation_type = GetNavigationType(profile, entry, reload);
[email protected]056de2d2009-06-26 16:41:34204 params->request_time = base::Time::Now();
205}
206
[email protected]420ae012009-04-24 05:16:32207} // namespace
208
209// -----------------------------------------------------------------------------
210
[email protected]fdd61c62009-04-22 19:22:57211// static
212int TabContents::find_request_id_counter_ = -1;
213
[email protected]420ae012009-04-24 05:16:32214
[email protected]420ae012009-04-24 05:16:32215TabContents::TabContents(Profile* profile,
216 SiteInstance* site_instance,
217 int routing_id,
[email protected]34ac70502009-09-25 17:07:23218 const TabContents* base_tab_contents)
[email protected]b680ad22009-04-15 23:19:42219 : delegate_(NULL),
[email protected]66ba4932009-06-04 19:22:13220 ALLOW_THIS_IN_INITIALIZER_LIST(controller_(this, profile)),
221 ALLOW_THIS_IN_INITIALIZER_LIST(view_(
[email protected]d82ed61e2009-06-16 02:46:22222 TabContentsView::Create(this))),
223 ALLOW_THIS_IN_INITIALIZER_LIST(render_manager_(this, this)),
[email protected]fdd61c62009-04-22 19:22:57224 property_bag_(),
225 registrar_(),
[email protected]d82ed61e2009-06-16 02:46:22226 ALLOW_THIS_IN_INITIALIZER_LIST(printing_(*this)),
[email protected]fdd61c62009-04-22 19:22:57227 save_package_(),
[email protected]19289d02009-10-21 21:47:00228 form_field_history_manager_(),
[email protected]21674df2009-10-28 19:51:21229 autofill_manager_(),
[email protected]fdd61c62009-04-22 19:22:57230 password_manager_(),
231 plugin_installer_(),
[email protected]d82ed61e2009-06-16 02:46:22232 ALLOW_THIS_IN_INITIALIZER_LIST(fav_icon_helper_(this)),
[email protected]fdd61c62009-04-22 19:22:57233 select_file_dialog_(),
[email protected]d5f942ba2008-09-26 19:30:34234 is_loading_(false),
initial.commit09911bf2008-07-26 23:55:29235 is_crashed_(false),
[email protected]d5f942ba2008-09-26 19:30:34236 waiting_for_response_(false),
[email protected]d5f942ba2008-09-26 19:30:34237 max_page_id_(-1),
[email protected]fdd61c62009-04-22 19:22:57238 current_load_start_(),
239 load_state_(net::LOAD_STATE_IDLE),
240 load_state_host_(),
[email protected]094e5b22009-09-25 04:23:56241 upload_size_(0),
242 upload_position_(0),
[email protected]fdd61c62009-04-22 19:22:57243 received_page_title_(false),
244 is_starred_(false),
245 contents_mime_type_(),
246 encoding_(),
[email protected]332af7732009-03-11 13:21:35247 blocked_popups_(NULL),
[email protected]fdd61c62009-04-22 19:22:57248 infobar_delegates_(),
[email protected]fdd61c62009-04-22 19:22:57249 find_ui_active_(false),
250 find_op_aborted_(false),
251 current_find_request_id_(find_request_id_counter_++),
[email protected]e491f1c2009-05-22 20:28:12252 last_search_case_sensitive_(false),
253 last_search_prepopulate_text_(NULL),
254 last_search_result_(),
[email protected]fdd61c62009-04-22 19:22:57255 capturing_contents_(false),
256 is_being_destroyed_(false),
257 notify_disconnection_(false),
258 history_requests_(),
259#if defined(OS_WIN)
260 message_box_active_(CreateEvent(NULL, TRUE, FALSE, NULL)),
261#endif
262 last_javascript_message_dismissal_(),
[email protected]c2e74fe82009-09-03 17:57:44263 suppress_javascript_messages_(false),
[email protected]7ab1e7d62009-10-14 23:32:01264 is_showing_before_unload_dialog_(false),
[email protected]7ff431e2010-01-07 18:18:54265 renderer_preferences_(),
[email protected]c2e74fe82009-09-03 17:57:44266 opener_dom_ui_type_(DOMUIFactory::kNoDOMUI) {
[email protected]7ff431e2010-01-07 18:18:54267 renderer_preferences_util::UpdateFromSystemSettings(
268 &renderer_preferences_, profile);
269
[email protected]16779842009-07-08 23:45:29270#if defined(OS_CHROMEOS)
[email protected]58dca552009-06-17 00:35:02271 // Make sure the thumbnailer is started before starting the render manager.
272 // The thumbnailer will want to listen for RVH creations, one of which will
273 // happen in RVHManager::Init.
274 ThumbnailGenerator* generator = g_browser_process->GetThumbnailGenerator();
275 if (generator)
276 generator->StartThumbnailing();
277#endif
278
[email protected]12636df2009-09-28 22:32:21279 render_manager_.Init(profile, site_instance, routing_id);
[email protected]420ae012009-04-24 05:16:32280
[email protected]34ac70502009-09-25 17:07:23281 // We have the initial size of the view be based on the size of the passed in
282 // tab contents (normally a tab from the same window).
283 view_->CreateView(base_tab_contents ?
284 base_tab_contents->view()->GetContainerSize() : gfx::Size());
[email protected]420ae012009-04-24 05:16:32285
286 // Register for notifications about all interested prefs change.
287 PrefService* prefs = profile->GetPrefs();
288 if (prefs) {
289 for (int i = 0; i < kPrefsToObserveLength; ++i)
290 prefs->AddPrefObserver(kPrefsToObserve[i], this);
291 }
292
293 // Register for notifications about URL starredness changing on any profile.
294 registrar_.Add(this, NotificationType::URLS_STARRED,
295 NotificationService::AllSources());
296 registrar_.Add(this, NotificationType::BOOKMARK_MODEL_LOADED,
297 NotificationService::AllSources());
298 registrar_.Add(this, NotificationType::RENDER_WIDGET_HOST_DESTROYED,
299 NotificationService::AllSources());
[email protected]d3ba77272009-09-03 00:06:09300#if defined(OS_LINUX)
301 registrar_.Add(this, NotificationType::BROWSER_THEME_CHANGED,
302 NotificationService::AllSources());
303#endif
[email protected]420ae012009-04-24 05:16:32304
305 // Keep a global copy of the previous search string (if any).
306 static string16 global_last_search = string16();
[email protected]e491f1c2009-05-22 20:28:12307 last_search_prepopulate_text_ = &global_last_search;
[email protected]840b1512009-07-21 16:53:46308
[email protected]c8865482009-07-23 20:40:10309 // Set-up the showing of the omnibox search infobar if applicable.
310 if (OmniboxSearchHint::IsEnabled(profile))
311 omnibox_search_hint_.reset(new OmniboxSearchHint(this));
[email protected]332af7732009-03-11 13:21:35312}
initial.commit09911bf2008-07-26 23:55:29313
314TabContents::~TabContents() {
[email protected]420ae012009-04-24 05:16:32315 is_being_destroyed_ = true;
316
[email protected]bfe4c1582009-10-02 18:11:09317 // We don't want any notifications while we're running our destructor.
[email protected]420ae012009-04-24 05:16:32318 registrar_.RemoveAll();
319
320 // Unregister the notifications of all observed prefs change.
321 PrefService* prefs = profile()->GetPrefs();
322 if (prefs) {
323 for (int i = 0; i < kPrefsToObserveLength; ++i)
324 prefs->RemovePrefObserver(kPrefsToObserve[i], this);
325 }
326
[email protected]420ae012009-04-24 05:16:32327 NotifyDisconnected();
[email protected]e7b8d832009-11-20 22:41:00328 hung_renderer_dialog::HideForTabContents(this);
[email protected]420ae012009-04-24 05:16:32329
[email protected]420ae012009-04-24 05:16:32330 // First cleanly close all child windows.
331 // TODO(mpcomplete): handle case if MaybeCloseChildWindows() already asked
332 // some of these to close. CloseWindows is async, so it might get called
333 // twice before it runs.
334 int size = static_cast<int>(child_windows_.size());
335 for (int i = size - 1; i >= 0; --i) {
336 ConstrainedWindow* window = child_windows_[i];
[email protected]fa1cf0b82010-01-15 21:49:44337 if (window) {
[email protected]420ae012009-04-24 05:16:32338 window->CloseConstrainedWindow();
[email protected]fa1cf0b82010-01-15 21:49:44339 BlockTabContent(false);
340 }
[email protected]420ae012009-04-24 05:16:32341 }
342
[email protected]66ba4932009-06-04 19:22:13343 if (blocked_popups_)
344 blocked_popups_->Destroy();
345
[email protected]420ae012009-04-24 05:16:32346 // Notify any observer that have a reference on this tab contents.
347 NotificationService::current()->Notify(
348 NotificationType::TAB_CONTENTS_DESTROYED,
349 Source<TabContents>(this),
350 NotificationService::NoDetails());
351
[email protected]1e5e74f2009-05-27 20:55:12352 // Notify any lasting InfobarDelegates that have not yet been removed that
353 // whatever infobar they were handling in this TabContents has closed,
354 // because the TabContents is going away entirely.
355 // This must happen after the TAB_CONTENTS_DESTROYED notification as the
356 // notification may trigger infobars calls that access their delegate. (and
357 // some implementations of InfoBarDelegate do delete themselves on
358 // InfoBarClosed()).
359 for (int i = 0; i < infobar_delegate_count(); ++i) {
360 InfoBarDelegate* delegate = GetInfoBarDelegateAt(i);
361 delegate->InfoBarClosed();
362 }
363 infobar_delegates_.clear();
364
[email protected]420ae012009-04-24 05:16:32365 // TODO(brettw) this should be moved to the view.
366#if defined(OS_WIN)
367 // If we still have a window handle, destroy it. GetNativeView can return
368 // NULL if this contents was part of a window that closed.
369 if (GetNativeView())
370 ::DestroyWindow(GetNativeView());
371#endif
[email protected]7ab1e7d62009-10-14 23:32:01372
373 // OnCloseStarted isn't called in unit tests.
374 if (!tab_close_start_time_.is_null()) {
375 UMA_HISTOGRAM_TIMES("Tab.Close",
376 base::TimeTicks::Now() - tab_close_start_time_);
377 }
initial.commit09911bf2008-07-26 23:55:29378}
379
[email protected]d5f942ba2008-09-26 19:30:34380// static
381void TabContents::RegisterUserPrefs(PrefService* prefs) {
[email protected]420ae012009-04-24 05:16:32382 prefs->RegisterBooleanPref(prefs::kAlternateErrorPagesEnabled, true);
383
384 WebPreferences pref_defaults;
385 prefs->RegisterBooleanPref(prefs::kWebKitJavascriptEnabled,
386 pref_defaults.javascript_enabled);
387 prefs->RegisterBooleanPref(prefs::kWebKitWebSecurityEnabled,
388 pref_defaults.web_security_enabled);
389 prefs->RegisterBooleanPref(
390 prefs::kWebKitJavascriptCanOpenWindowsAutomatically, true);
391 prefs->RegisterBooleanPref(prefs::kWebKitLoadsImagesAutomatically,
392 pref_defaults.loads_images_automatically);
393 prefs->RegisterBooleanPref(prefs::kWebKitPluginsEnabled,
394 pref_defaults.plugins_enabled);
395 prefs->RegisterBooleanPref(prefs::kWebKitDomPasteEnabled,
396 pref_defaults.dom_paste_enabled);
397 prefs->RegisterBooleanPref(prefs::kWebKitShrinksStandaloneImagesToFit,
398 pref_defaults.shrinks_standalone_images_to_fit);
[email protected]0df30122009-06-03 12:13:08399 prefs->RegisterStringPref(prefs::kWebKitInspectorSettings,
[email protected]8922e1f2009-10-03 05:01:26400 UTF8ToWide(pref_defaults.inspector_settings));
[email protected]420ae012009-04-24 05:16:32401 prefs->RegisterBooleanPref(prefs::kWebKitTextAreasAreResizable,
402 pref_defaults.text_areas_are_resizable);
403 prefs->RegisterBooleanPref(prefs::kWebKitJavaEnabled,
404 pref_defaults.java_enabled);
405
406 prefs->RegisterLocalizedStringPref(prefs::kAcceptLanguages,
407 IDS_ACCEPT_LANGUAGES);
408 prefs->RegisterLocalizedStringPref(prefs::kDefaultCharset,
409 IDS_DEFAULT_ENCODING);
410 prefs->RegisterLocalizedBooleanPref(prefs::kWebKitStandardFontIsSerif,
411 IDS_STANDARD_FONT_IS_SERIF);
412 prefs->RegisterLocalizedStringPref(prefs::kWebKitFixedFontFamily,
413 IDS_FIXED_FONT_FAMILY);
414 prefs->RegisterLocalizedStringPref(prefs::kWebKitSerifFontFamily,
415 IDS_SERIF_FONT_FAMILY);
416 prefs->RegisterLocalizedStringPref(prefs::kWebKitSansSerifFontFamily,
417 IDS_SANS_SERIF_FONT_FAMILY);
418 prefs->RegisterLocalizedStringPref(prefs::kWebKitCursiveFontFamily,
419 IDS_CURSIVE_FONT_FAMILY);
420 prefs->RegisterLocalizedStringPref(prefs::kWebKitFantasyFontFamily,
421 IDS_FANTASY_FONT_FAMILY);
422 prefs->RegisterLocalizedIntegerPref(prefs::kWebKitDefaultFontSize,
423 IDS_DEFAULT_FONT_SIZE);
424 prefs->RegisterLocalizedIntegerPref(prefs::kWebKitDefaultFixedFontSize,
425 IDS_DEFAULT_FIXED_FONT_SIZE);
426 prefs->RegisterLocalizedIntegerPref(prefs::kWebKitMinimumFontSize,
427 IDS_MINIMUM_FONT_SIZE);
428 prefs->RegisterLocalizedIntegerPref(prefs::kWebKitMinimumLogicalFontSize,
429 IDS_MINIMUM_LOGICAL_FONT_SIZE);
430 prefs->RegisterLocalizedBooleanPref(prefs::kWebKitUsesUniversalDetector,
431 IDS_USES_UNIVERSAL_DETECTOR);
432 prefs->RegisterLocalizedStringPref(prefs::kStaticEncodings,
433 IDS_STATIC_ENCODING_LIST);
initial.commit09911bf2008-07-26 23:55:29434}
435
[email protected]13367f72009-07-08 16:39:02436// Returns true if contains content rendered by an extension.
437bool TabContents::HostsExtension() const {
438 return GetURL().SchemeIs(chrome::kExtensionScheme);
[email protected]d5f942ba2008-09-26 19:30:34439}
440
[email protected]6e95706d2009-04-23 22:55:01441PasswordManager* TabContents::GetPasswordManager() {
442 if (password_manager_.get() == NULL)
443 password_manager_.reset(new PasswordManager(this));
444 return password_manager_.get();
445}
446
447PluginInstaller* TabContents::GetPluginInstaller() {
448 if (plugin_installer_.get() == NULL)
449 plugin_installer_.reset(new PluginInstaller(this));
450 return plugin_installer_.get();
451}
452
[email protected]d5f942ba2008-09-26 19:30:34453const GURL& TabContents::GetURL() const {
454 // We may not have a navigation entry yet
[email protected]ce3fa3c2009-04-20 19:55:57455 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]ebe89e062009-08-13 23:16:54456 return entry ? entry->virtual_url() : GURL::EmptyGURL();
[email protected]d5f942ba2008-09-26 19:30:34457}
458
[email protected]96d185d2009-04-24 03:28:54459const string16& TabContents::GetTitle() const {
[email protected]4c6092c5b2009-06-06 00:23:55460 // Transient entries take precedence. They are used for interstitial pages
461 // that are shown on top of existing pages.
462 NavigationEntry* entry = controller_.GetTransientEntry();
463 if (entry)
464 return entry->GetTitleForDisplay(&controller_);
465
[email protected]96d185d2009-04-24 03:28:54466 DOMUI* our_dom_ui = render_manager_.pending_dom_ui() ?
467 render_manager_.pending_dom_ui() : render_manager_.dom_ui();
468 if (our_dom_ui) {
469 // Don't override the title in view source mode.
[email protected]4c6092c5b2009-06-06 00:23:55470 entry = controller_.GetActiveEntry();
[email protected]96d185d2009-04-24 03:28:54471 if (!(entry && entry->IsViewSourceMode())) {
472 // Give the DOM UI the chance to override our title.
473 const string16& title = our_dom_ui->overridden_title();
474 if (!title.empty())
475 return title;
476 }
477 }
478
479 // We use the title for the last committed entry rather than a pending
480 // navigation entry. For example, when the user types in a URL, we want to
481 // keep the old page's title until the new load has committed and we get a new
482 // title.
[email protected]96d185d2009-04-24 03:28:54483 entry = controller_.GetLastCommittedEntry();
484 if (entry)
485 return entry->GetTitleForDisplay(&controller_);
[email protected]96d185d2009-04-24 03:28:54486 return EmptyString16();
487}
488
[email protected]d5f942ba2008-09-26 19:30:34489int32 TabContents::GetMaxPageID() {
490 if (GetSiteInstance())
491 return GetSiteInstance()->max_page_id();
492 else
493 return max_page_id_;
494}
495
496void TabContents::UpdateMaxPageID(int32 page_id) {
497 // Ensure both the SiteInstance and RenderProcessHost update their max page
[email protected]57c6a652009-05-04 07:58:34498 // IDs in sync. Only TabContents will also have site instances, except during
[email protected]d5f942ba2008-09-26 19:30:34499 // testing.
500 if (GetSiteInstance())
501 GetSiteInstance()->UpdateMaxPageID(page_id);
[email protected]57c6a652009-05-04 07:58:34502 process()->UpdateMaxPageID(page_id);
[email protected]d5f942ba2008-09-26 19:30:34503}
504
[email protected]96d185d2009-04-24 03:28:54505SiteInstance* TabContents::GetSiteInstance() const {
506 return render_manager_.current_host()->site_instance();
507}
508
[email protected]d5f942ba2008-09-26 19:30:34509const std::wstring TabContents::GetDefaultTitle() const {
510 return l10n_util::GetString(IDS_DEFAULT_TAB_TITLE);
511}
512
[email protected]96d185d2009-04-24 03:28:54513bool TabContents::ShouldDisplayURL() {
[email protected]8ab24cc2009-11-13 20:56:15514 // Don't hide the url in view source mode and with interstitials.
[email protected]96d185d2009-04-24 03:28:54515 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]8ab24cc2009-11-13 20:56:15516 if (entry && (entry->IsViewSourceMode() ||
517 entry->page_type() == NavigationEntry::INTERSTITIAL_PAGE)) {
[email protected]96d185d2009-04-24 03:28:54518 return true;
[email protected]8ab24cc2009-11-13 20:56:15519 }
[email protected]83e3895b2009-06-11 00:07:16520
[email protected]96d185d2009-04-24 03:28:54521 DOMUI* dom_ui = GetDOMUIForCurrentState();
522 if (dom_ui)
523 return !dom_ui->should_hide_url();
524 return true;
525}
526
[email protected]d5f942ba2008-09-26 19:30:34527SkBitmap TabContents::GetFavIcon() const {
528 // Like GetTitle(), we also want to use the favicon for the last committed
529 // entry rather than a pending navigation entry.
[email protected]ce3fa3c2009-04-20 19:55:57530 NavigationEntry* entry = controller_.GetTransientEntry();
[email protected]cbab76d2008-10-13 22:42:47531 if (entry)
532 return entry->favicon().bitmap();
533
[email protected]ce3fa3c2009-04-20 19:55:57534 entry = controller_.GetLastCommittedEntry();
[email protected]d5f942ba2008-09-26 19:30:34535 if (entry)
536 return entry->favicon().bitmap();
[email protected]d5f942ba2008-09-26 19:30:34537 return SkBitmap();
538}
539
[email protected]0b303cc2009-09-28 22:35:15540bool TabContents::FavIconIsValid() const {
541 NavigationEntry* entry = controller_.GetTransientEntry();
542 if (entry)
543 return entry->favicon().is_valid();
544
545 entry = controller_.GetLastCommittedEntry();
546 if (entry)
547 return entry->favicon().is_valid();
548
549 return false;
550}
551
[email protected]96d185d2009-04-24 03:28:54552bool TabContents::ShouldDisplayFavIcon() {
553 // Always display a throbber during pending loads.
554 if (controller_.GetLastCommittedEntry() && controller_.pending_entry())
555 return true;
556
557 DOMUI* dom_ui = GetDOMUIForCurrentState();
558 if (dom_ui)
559 return !dom_ui->hide_favicon();
560 return true;
561}
562
[email protected]2d5a3592010-01-30 04:29:52563bool TabContents::IsContentBlocked(ContentSettingsType content_type) const {
564 // TODO(pkasting): Return meaningful values here.
565 return false;
566}
567
[email protected]96d185d2009-04-24 03:28:54568std::wstring TabContents::GetStatusText() const {
569 if (!is_loading() || load_state_ == net::LOAD_STATE_IDLE)
570 return std::wstring();
571
572 switch (load_state_) {
573 case net::LOAD_STATE_WAITING_FOR_CACHE:
574 return l10n_util::GetString(IDS_LOAD_STATE_WAITING_FOR_CACHE);
575 case net::LOAD_STATE_RESOLVING_PROXY_FOR_URL:
576 return l10n_util::GetString(IDS_LOAD_STATE_RESOLVING_PROXY_FOR_URL);
577 case net::LOAD_STATE_RESOLVING_HOST:
578 return l10n_util::GetString(IDS_LOAD_STATE_RESOLVING_HOST);
579 case net::LOAD_STATE_CONNECTING:
580 return l10n_util::GetString(IDS_LOAD_STATE_CONNECTING);
581 case net::LOAD_STATE_SENDING_REQUEST:
[email protected]094e5b22009-09-25 04:23:56582 if (upload_size_)
583 return l10n_util::GetStringF(
584 IDS_LOAD_STATE_SENDING_REQUEST_WITH_PROGRESS,
585 static_cast<int>((100 * upload_position_) / upload_size_));
586 else
587 return l10n_util::GetString(IDS_LOAD_STATE_SENDING_REQUEST);
[email protected]96d185d2009-04-24 03:28:54588 case net::LOAD_STATE_WAITING_FOR_RESPONSE:
589 return l10n_util::GetStringF(IDS_LOAD_STATE_WAITING_FOR_RESPONSE,
590 load_state_host_);
591 // Ignore net::LOAD_STATE_READING_RESPONSE and net::LOAD_STATE_IDLE
592 case net::LOAD_STATE_IDLE:
593 case net::LOAD_STATE_READING_RESPONSE:
594 break;
595 }
596
597 return std::wstring();
598}
599
[email protected]d5f942ba2008-09-26 19:30:34600void TabContents::SetIsCrashed(bool state) {
601 if (state == is_crashed_)
602 return;
603
604 is_crashed_ = state;
[email protected]c9cd2222009-05-06 05:16:50605 NotifyNavigationStateChanged(INVALIDATE_TAB);
[email protected]d5f942ba2008-09-26 19:30:34606}
607
[email protected]744ef172009-10-16 21:53:46608void TabContents::PageActionStateChanged() {
609 NotifyNavigationStateChanged(TabContents::INVALIDATE_PAGE_ACTIONS);
[email protected]d7eaf572009-07-01 21:57:00610}
[email protected]ce5c4502009-05-06 16:46:11611
[email protected]d5f942ba2008-09-26 19:30:34612void TabContents::NotifyNavigationStateChanged(unsigned changed_flags) {
613 if (delegate_)
614 delegate_->NavigationStateChanged(this, changed_flags);
615}
616
[email protected]96d185d2009-04-24 03:28:54617void TabContents::DidBecomeSelected() {
618 controller_.SetActive(true);
[email protected]789e9152009-08-04 21:59:43619 if (render_widget_host_view()) {
[email protected]96d185d2009-04-24 03:28:54620 render_widget_host_view()->DidBecomeSelected();
[email protected]789e9152009-08-04 21:59:43621#if defined(OS_MACOSX)
622 render_widget_host_view()->SetActive(true);
623#endif
624 }
[email protected]96d185d2009-04-24 03:28:54625
[email protected]76543b92009-08-31 17:27:45626 WebCacheManager::GetInstance()->ObserveActivity(process()->id());
[email protected]96d185d2009-04-24 03:28:54627}
628
629void TabContents::WasHidden() {
630 if (!capturing_contents()) {
631 // |render_view_host()| can be NULL if the user middle clicks a link to open
632 // a tab in then background, then closes the tab before selecting it. This
633 // is because closing the tab calls TabContents::Destroy(), which removes
634 // the |render_view_host()|; then when we actually destroy the window,
635 // OnWindowPosChanged() notices and calls HideContents() (which calls us).
636 if (render_widget_host_view())
637 render_widget_host_view()->WasHidden();
[email protected]96d185d2009-04-24 03:28:54638 }
639
640 NotificationService::current()->Notify(
641 NotificationType::TAB_CONTENTS_HIDDEN,
642 Source<TabContents>(this),
643 NotificationService::NoDetails());
644}
645
[email protected]d5f942ba2008-09-26 19:30:34646void TabContents::Activate() {
647 if (delegate_)
648 delegate_->ActivateContents(this);
649}
650
[email protected]96d185d2009-04-24 03:28:54651void TabContents::ShowContents() {
652 if (render_widget_host_view())
653 render_widget_host_view()->DidBecomeSelected();
[email protected]96d185d2009-04-24 03:28:54654}
655
656void TabContents::HideContents() {
657 // TODO(pkasting): http://b/1239839 Right now we purposefully don't call
658 // our superclass HideContents(), because some callers want to be very picky
659 // about the order in which these get called. In addition to making the code
660 // here practically impossible to understand, this also means we end up
661 // calling TabContents::WasHidden() twice if callers call both versions of
[email protected]57c6a652009-05-04 07:58:34662 // HideContents() on a TabContents.
[email protected]96d185d2009-04-24 03:28:54663 WasHidden();
664}
665
[email protected]c0588052008-10-27 23:01:50666void TabContents::OpenURL(const GURL& url, const GURL& referrer,
[email protected]d5f942ba2008-09-26 19:30:34667 WindowOpenDisposition disposition,
668 PageTransition::Type transition) {
669 if (delegate_)
[email protected]c0588052008-10-27 23:01:50670 delegate_->OpenURLFromTab(this, url, referrer, disposition, transition);
[email protected]d5f942ba2008-09-26 19:30:34671}
672
673bool TabContents::NavigateToPendingEntry(bool reload) {
[email protected]96d185d2009-04-24 03:28:54674 const NavigationEntry& entry = *controller_.pending_entry();
675
676 RenderViewHost* dest_render_view_host = render_manager_.Navigate(entry);
677 if (!dest_render_view_host)
678 return false; // Unable to create the desired render view host.
679
680 // Tell DevTools agent that it is attached prior to the navigation.
[email protected]d9f9b792009-06-24 13:17:12681 DevToolsManager* devtools_manager = DevToolsManager::GetInstance();
[email protected]7e8e6b62009-05-08 11:28:32682 if (devtools_manager) { // NULL in unit tests.
[email protected]684b4ba22009-05-07 06:27:41683 devtools_manager->OnNavigatingToPendingEntry(
684 render_view_host(),
685 dest_render_view_host,
686 controller_.pending_entry()->url());
[email protected]7e8e6b62009-05-08 11:28:32687 }
[email protected]96d185d2009-04-24 03:28:54688
689 // Used for page load time metrics.
690 current_load_start_ = base::TimeTicks::Now();
691
692 // Navigate in the desired RenderViewHost.
[email protected]056de2d2009-06-26 16:41:34693 ViewMsg_Navigate_Params navigate_params;
[email protected]5e369672009-11-03 23:48:30694 MakeNavigateParams(profile(), entry, reload, &navigate_params);
[email protected]056de2d2009-06-26 16:41:34695 dest_render_view_host->Navigate(navigate_params);
[email protected]96d185d2009-04-24 03:28:54696
697 if (entry.page_id() == -1) {
698 // HACK!! This code suppresses javascript: URLs from being added to
699 // session history, which is what we want to do for javascript: URLs that
700 // do not generate content. What we really need is a message from the
701 // renderer telling us that a new page was not created. The same message
702 // could be used for mailto: URLs and the like.
703 if (entry.url().SchemeIs(chrome::kJavaScriptScheme))
704 return false;
705 }
706
707 // Clear any provisional password saves - this stops password infobars
708 // showing up on pages the user navigates to while the right page is
709 // loading.
710 GetPasswordManager()->ClearProvisionalSave();
711
712 if (reload && !profile()->IsOffTheRecord()) {
[email protected]0189bc722009-08-28 21:56:48713 FaviconService* favicon_service =
714 profile()->GetFaviconService(Profile::IMPLICIT_ACCESS);
715 if (favicon_service)
716 favicon_service->SetFaviconOutOfDateForPage(entry.url());
[email protected]96d185d2009-04-24 03:28:54717 }
718
[email protected]d5f942ba2008-09-26 19:30:34719 return true;
720}
721
[email protected]96d185d2009-04-24 03:28:54722void TabContents::Stop() {
723 render_manager_.Stop();
724 printing_.Stop();
725}
726
727void TabContents::Cut() {
728 render_view_host()->Cut();
729}
730
731void TabContents::Copy() {
732 render_view_host()->Copy();
733}
734
[email protected]c0cc3092009-09-12 08:27:27735#if defined(OS_MACOSX)
[email protected]a954bf72009-09-12 07:30:35736void TabContents::CopyToFindPboard() {
737 render_view_host()->CopyToFindPboard();
738}
[email protected]c0cc3092009-09-12 08:27:27739#endif
[email protected]a954bf72009-09-12 07:30:35740
[email protected]96d185d2009-04-24 03:28:54741void TabContents::Paste() {
742 render_view_host()->Paste();
743}
744
[email protected]7e786822009-11-07 00:00:27745#if defined(OS_MACOSX)
746void TabContents::PasteAndMatchStyle() {
747 render_view_host()->ForwardEditCommand("PasteAndMatchStyle", "");
748}
749#endif
750
[email protected]96d185d2009-04-24 03:28:54751void TabContents::DisassociateFromPopupCount() {
752 render_view_host()->DisassociateFromPopupCount();
753}
754
755TabContents* TabContents::Clone() {
756 // We create a new SiteInstance so that the new tab won't share processes
757 // with the old one. This can be changed in the future if we need it to share
758 // processes for some reason.
[email protected]420ae012009-04-24 05:16:32759 TabContents* tc = new TabContents(profile(),
[email protected]96d185d2009-04-24 03:28:54760 SiteInstance::CreateSiteInstance(profile()),
[email protected]12636df2009-09-28 22:32:21761 MSG_ROUTING_NONE, this);
[email protected]96d185d2009-04-24 03:28:54762 tc->controller().CopyStateFrom(controller_);
763 return tc;
764}
765
[email protected]4d677202009-07-19 07:37:12766void TabContents::ShowPageInfo(const GURL& url,
767 const NavigationEntry::SSLStatus& ssl,
768 bool show_history) {
769 if (!delegate_)
770 return;
771
[email protected]bb678332009-07-21 00:15:50772 delegate_->ShowPageInfo(profile(), url, ssl, show_history);
[email protected]4d677202009-07-19 07:37:12773}
774
[email protected]7e0831b2010-02-01 08:45:15775void TabContents::TranslatePage(const std::string& source_lang,
776 const std::string& target_lang) {
777 NavigationEntry* entry = controller_.GetActiveEntry();
778 if (!entry) {
779 NOTREACHED();
780 return;
781 }
782 render_view_host()->TranslatePage(entry->page_id(), source_lang, target_lang);
783}
784
initial.commit09911bf2008-07-26 23:55:29785ConstrainedWindow* TabContents::CreateConstrainedDialog(
[email protected]e8382172009-06-19 22:16:28786 ConstrainedWindowDelegate* delegate) {
initial.commit09911bf2008-07-26 23:55:29787 ConstrainedWindow* window =
[email protected]e8382172009-06-19 22:16:28788 ConstrainedWindow::CreateConstrainedDialog(this, delegate);
initial.commit09911bf2008-07-26 23:55:29789 child_windows_.push_back(window);
[email protected]fa1cf0b82010-01-15 21:49:44790
791 if (child_windows_.size() == 1) {
792 window->ShowConstrainedWindow();
793 BlockTabContent(true);
794 }
795
initial.commit09911bf2008-07-26 23:55:29796 return window;
797}
798
[email protected]fa1cf0b82010-01-15 21:49:44799void TabContents::BlockTabContent(bool blocked) {
800 render_view_host()->set_ignore_input_events(blocked);
801 if (delegate_)
802 delegate_->SetTabContentBlocked(this, blocked);
803}
804
initial.commit09911bf2008-07-26 23:55:29805void TabContents::AddNewContents(TabContents* new_contents,
806 WindowOpenDisposition disposition,
807 const gfx::Rect& initial_pos,
[email protected]7e7414ae2010-01-26 20:19:29808 bool user_gesture) {
initial.commit09911bf2008-07-26 23:55:29809 if (!delegate_)
810 return;
811
[email protected]35f7d212009-04-29 21:19:27812 if ((disposition == NEW_POPUP) && !user_gesture &&
813 !CommandLine::ForCurrentProcess()->HasSwitch(
814 switches::kDisablePopupBlocking)) {
[email protected]2c4410d2009-05-06 23:46:22815 // Unrequested popups from normal pages are constrained unless they're in
[email protected]b6c874582009-05-08 19:38:31816 // the whitelist. The popup owner will handle checking this.
[email protected]7e7414ae2010-01-26 20:19:29817 delegate_->GetConstrainingContents(this)->AddPopup(
818 new_contents, initial_pos);
[email protected]b6c874582009-05-08 19:38:31819 } else {
[email protected]304f8792009-08-19 21:28:09820#if defined(OS_CHROMEOS)
821 if (disposition == NEW_POPUP) {
822 // If the popup is bigger than a given factor of the screen, then
823 // turn it into a foreground tab (on chrome os only)
824 GdkScreen* screen = gdk_screen_get_default();
825 int max_width = gdk_screen_get_width(screen) * kMaxWidthFactor;
826 int max_height = gdk_screen_get_height(screen) * kMaxHeightFactor;
827 if (initial_pos.width() > max_width ||
828 initial_pos.height() > max_height) {
829 disposition = NEW_FOREGROUND_TAB;
830 }
831 }
832#endif
833
[email protected]0aa55312008-10-17 21:53:08834 new_contents->DisassociateFromPopupCount();
initial.commit09911bf2008-07-26 23:55:29835 delegate_->AddNewContents(this, new_contents, disposition, initial_pos,
836 user_gesture);
[email protected]7f37dd92009-09-15 16:35:49837 NotificationService::current()->Notify(
838 NotificationType::TAB_ADDED,
839 Source<TabContentsDelegate>(delegate_),
840 Details<TabContents>(this));
initial.commit09911bf2008-07-26 23:55:29841 }
[email protected]b6c874582009-05-08 19:38:31842 PopupNotificationVisibilityChanged(ShowingBlockedPopupNotification());
initial.commit09911bf2008-07-26 23:55:29843}
844
initial.commit09911bf2008-07-26 23:55:29845void TabContents::CloseAllSuppressedPopups() {
[email protected]d6598c052008-11-05 19:03:25846 if (blocked_popups_)
[email protected]2c4410d2009-05-06 23:46:22847 blocked_popups_->CloseAll();
initial.commit09911bf2008-07-26 23:55:29848}
849
[email protected]bcd9dc42010-01-23 16:38:18850bool TabContents::ExecuteCode(int request_id, const std::string& extension_id,
[email protected]61f5a7b2009-12-22 22:21:20851 const std::vector<URLPattern>& host_permissions,
[email protected]20ad2692009-11-20 18:27:20852 bool is_js_code, const std::string& code_string,
853 bool all_frames) {
[email protected]bcd9dc42010-01-23 16:38:18854 return render_view_host()->Send(
[email protected]61f5a7b2009-12-22 22:21:20855 new ViewMsg_ExecuteCode(render_view_host()->routing_id(),
856 ViewMsg_ExecuteCode_Params(request_id, extension_id, host_permissions,
857 is_js_code, code_string, all_frames)));
[email protected]912256b32009-09-18 09:47:35858}
859
[email protected]96d185d2009-04-24 03:28:54860void TabContents::PopupNotificationVisibilityChanged(bool visible) {
861 render_view_host()->PopupNotificationVisibilityChanged(visible);
862}
863
[email protected]5c9e97a2009-09-09 23:48:30864gfx::NativeView TabContents::GetContentNativeView() const {
[email protected]96d185d2009-04-24 03:28:54865 return view_->GetContentNativeView();
866}
867
868gfx::NativeView TabContents::GetNativeView() const {
869 return view_->GetNativeView();
870}
871
872void TabContents::GetContainerBounds(gfx::Rect *out) const {
873 view_->GetContainerBounds(out);
874}
875
876void TabContents::Focus() {
877 view_->Focus();
878}
879
[email protected]90daadb42009-06-08 21:27:28880void TabContents::FocusThroughTabTraversal(bool reverse) {
[email protected]7e383692009-06-12 19:14:54881 if (showing_interstitial_page()) {
882 render_manager_.interstitial_page()->FocusThroughTabTraversal(reverse);
883 return;
884 }
[email protected]96d185d2009-04-24 03:28:54885 render_view_host()->SetInitialFocus(reverse);
886}
887
888bool TabContents::FocusLocationBarByDefault() {
889 DOMUI* dom_ui = GetDOMUIForCurrentState();
890 if (dom_ui)
891 return dom_ui->focus_location_bar_by_default();
[email protected]3e3f0eb2009-06-22 18:33:43892 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]e0d481582009-09-15 21:06:25893 if (entry && entry->url() == GURL(chrome::kAboutBlankURL))
[email protected]3e3f0eb2009-06-22 18:33:43894 return true;
[email protected]96d185d2009-04-24 03:28:54895 return false;
896}
897
[email protected]616ed5a2008-11-21 22:27:24898void TabContents::AddInfoBar(InfoBarDelegate* delegate) {
899 // Look through the existing InfoBarDelegates we have for a match. If we've
900 // already got one that matches, then we don't add the new one.
[email protected]f86a07022008-11-25 01:06:05901 for (int i = 0; i < infobar_delegate_count(); ++i) {
[email protected]6d27a7b2009-12-18 23:25:45902 if (GetInfoBarDelegateAt(i)->EqualsDelegate(delegate)) {
903 // Tell the new infobar to close so that it can clean itself up.
904 delegate->InfoBarClosed();
[email protected]616ed5a2008-11-21 22:27:24905 return;
[email protected]6d27a7b2009-12-18 23:25:45906 }
[email protected]616ed5a2008-11-21 22:27:24907 }
908
909 infobar_delegates_.push_back(delegate);
[email protected]bfd04a62009-02-01 18:16:56910 NotificationService::current()->Notify(
911 NotificationType::TAB_CONTENTS_INFOBAR_ADDED,
912 Source<TabContents>(this),
913 Details<InfoBarDelegate>(delegate));
[email protected]616ed5a2008-11-21 22:27:24914
915 // Add ourselves as an observer for navigations the first time a delegate is
916 // added. We use this notification to expire InfoBars that need to expire on
917 // page transitions.
918 if (infobar_delegates_.size() == 1) {
[email protected]bfd04a62009-02-01 18:16:56919 registrar_.Add(this, NotificationType::NAV_ENTRY_COMMITTED,
[email protected]ce3fa3c2009-04-20 19:55:57920 Source<NavigationController>(&controller_));
[email protected]616ed5a2008-11-21 22:27:24921 }
922}
923
924void TabContents::RemoveInfoBar(InfoBarDelegate* delegate) {
925 std::vector<InfoBarDelegate*>::iterator it =
926 find(infobar_delegates_.begin(), infobar_delegates_.end(), delegate);
927 if (it != infobar_delegates_.end()) {
928 InfoBarDelegate* delegate = *it;
[email protected]bfd04a62009-02-01 18:16:56929 NotificationService::current()->Notify(
930 NotificationType::TAB_CONTENTS_INFOBAR_REMOVED,
931 Source<TabContents>(this),
932 Details<InfoBarDelegate>(delegate));
[email protected]f86a07022008-11-25 01:06:05933 infobar_delegates_.erase(it);
[email protected]616ed5a2008-11-21 22:27:24934
[email protected]6a02963e2009-01-06 16:58:03935 // Remove ourselves as an observer if we are tracking no more InfoBars.
936 if (infobar_delegates_.empty()) {
[email protected]bfd04a62009-02-01 18:16:56937 registrar_.Remove(this, NotificationType::NAV_ENTRY_COMMITTED,
[email protected]ce3fa3c2009-04-20 19:55:57938 Source<NavigationController>(&controller_));
[email protected]6a02963e2009-01-06 16:58:03939 }
[email protected]616ed5a2008-11-21 22:27:24940 }
941}
[email protected]3edd9522009-03-04 22:19:41942
[email protected]f81821c2009-08-07 01:29:47943void TabContents::ReplaceInfoBar(InfoBarDelegate* old_delegate,
944 InfoBarDelegate* new_delegate) {
945 std::vector<InfoBarDelegate*>::iterator it =
946 find(infobar_delegates_.begin(), infobar_delegates_.end(), old_delegate);
947 DCHECK(it != infobar_delegates_.end());
948
949 // Notify the container about the change of plans.
950 scoped_ptr<std::pair<InfoBarDelegate*, InfoBarDelegate*> > details(
951 new std::pair<InfoBarDelegate*, InfoBarDelegate*>(
952 old_delegate, new_delegate));
953 NotificationService::current()->Notify(
954 NotificationType::TAB_CONTENTS_INFOBAR_REPLACED,
955 Source<TabContents>(this),
956 Details<std::pair<InfoBarDelegate*, InfoBarDelegate*> >(details.get()));
957
958 // Remove the old one.
959 infobar_delegates_.erase(it);
960
961 // Add the new one.
962 infobar_delegates_.push_back(new_delegate);
963}
964
[email protected]20ede072009-10-06 00:28:35965bool TabContents::ShouldShowBookmarkBar() {
[email protected]aa9196f2009-10-22 16:20:27966 if (showing_interstitial_page())
967 return false;
968
[email protected]96d185d2009-04-24 03:28:54969 // See GetDOMUIForCurrentState() comment for more info. This case is very
970 // similar, but for non-first loads, we want to use the committed entry. This
971 // is so the bookmarks bar disappears at the same time the page does.
972 if (controller_.GetLastCommittedEntry()) {
973 // Not the first load, always use the committed DOM UI.
974 if (render_manager_.dom_ui())
975 return render_manager_.dom_ui()->force_bookmark_bar_visible();
976 return false; // Default.
977 }
978
979 // When it's the first load, we know either the pending one or the committed
980 // one will have the DOM UI in it (see GetDOMUIForCurrentState), and only one
981 // of them will be valid, so we can just check both.
982 if (render_manager_.pending_dom_ui())
983 return render_manager_.pending_dom_ui()->force_bookmark_bar_visible();
984 if (render_manager_.dom_ui())
985 return render_manager_.dom_ui()->force_bookmark_bar_visible();
986 return false; // Default.
987}
988
[email protected]d8f16ae2009-08-30 03:47:26989bool TabContents::IsExtensionShelfAlwaysVisible() {
990 // See GetDOMUIForCurrentState() comment for more info. This case is very
991 // similar, but for non-first loads, we want to use the committed entry. This
992 // is so the bookmarks bar disappears at the same time the page does.
993 if (controller_.GetLastCommittedEntry()) {
994 // Not the first load, always use the committed DOM UI.
995 if (render_manager_.dom_ui())
996 return render_manager_.dom_ui()->force_extension_shelf_visible();
997 return false; // Default.
998 }
999
1000 // When it's the first load, we know either the pending one or the committed
1001 // one will have the DOM UI in it (see GetDOMUIForCurrentState), and only one
1002 // of them will be valid, so we can just check both.
1003 if (render_manager_.pending_dom_ui())
1004 return render_manager_.pending_dom_ui()->force_extension_shelf_visible();
1005 if (render_manager_.dom_ui())
1006 return render_manager_.dom_ui()->force_extension_shelf_visible();
1007 return false; // Default.
1008}
1009
[email protected]3edd9522009-03-04 22:19:411010void TabContents::ToolbarSizeChanged(bool is_animating) {
1011 TabContentsDelegate* d = delegate();
1012 if (d)
1013 d->ToolbarSizeChanged(this, is_animating);
1014}
[email protected]616ed5a2008-11-21 22:27:241015
[email protected]fc6fb7fb2009-11-07 02:35:041016bool TabContents::CanDownload(int request_id) {
1017 TabContentsDelegate* d = delegate();
1018 if (d)
1019 return d->CanDownload(request_id);
1020 return true;
1021}
1022
[email protected]d5f942ba2008-09-26 19:30:341023void TabContents::OnStartDownload(DownloadItem* download) {
1024 DCHECK(download);
[email protected]d5f942ba2008-09-26 19:30:341025
1026 // Download in a constrained popup is shown in the tab that opened it.
[email protected]b6c874582009-05-08 19:38:311027 TabContents* tab_contents = delegate()->GetConstrainingContents(this);
[email protected]d5f942ba2008-09-26 19:30:341028
[email protected]59560e0b2009-06-04 03:30:221029 if (tab_contents && tab_contents->delegate())
1030 tab_contents->delegate()->OnStartDownload(download);
[email protected]3edd9522009-03-04 22:19:411031}
[email protected]3edd9522009-03-04 22:19:411032
[email protected]d5f942ba2008-09-26 19:30:341033void TabContents::WillClose(ConstrainedWindow* window) {
1034 ConstrainedWindowList::iterator it =
1035 find(child_windows_.begin(), child_windows_.end(), window);
[email protected]fa1cf0b82010-01-15 21:49:441036 bool removed_topmost_window = it == child_windows_.begin();
[email protected]d5f942ba2008-09-26 19:30:341037 if (it != child_windows_.end())
1038 child_windows_.erase(it);
[email protected]fa1cf0b82010-01-15 21:49:441039 if (removed_topmost_window && child_windows_.size() > 0) {
1040 child_windows_[0]->ShowConstrainedWindow();
1041 BlockTabContent(true);
1042 } else {
1043 BlockTabContent(false);
1044 }
[email protected]66ba4932009-06-04 19:22:131045}
[email protected]d5f942ba2008-09-26 19:30:341046
[email protected]66ba4932009-06-04 19:22:131047void TabContents::WillCloseBlockedPopupContainer(
1048 BlockedPopupContainer* container) {
1049 DCHECK(blocked_popups_ == container);
1050 blocked_popups_ = NULL;
[email protected]d5f942ba2008-09-26 19:30:341051}
1052
[email protected]d5f942ba2008-09-26 19:30:341053void TabContents::DidMoveOrResize(ConstrainedWindow* window) {
[email protected]332af7732009-03-11 13:21:351054#if defined(OS_WIN)
[email protected]92edc472009-02-10 20:32:061055 UpdateWindow(GetNativeView());
[email protected]332af7732009-03-11 13:21:351056#endif
[email protected]d5f942ba2008-09-26 19:30:341057}
1058
[email protected]c1dc9f132009-11-20 23:13:211059void TabContents::StartFinding(string16 search_string,
[email protected]e491f1c2009-05-22 20:28:121060 bool forward_direction,
1061 bool case_sensitive) {
[email protected]c1dc9f132009-11-20 23:13:211062 // If search_string is empty, it means FindNext was pressed with a keyboard
[email protected]6e95706d2009-04-23 22:55:011063 // shortcut so unless we have something to search for we return early.
[email protected]c1dc9f132009-11-20 23:13:211064 if (search_string.empty() && find_text_.empty()) {
[email protected]87fae512009-12-11 02:20:211065 // Try the last thing we searched for on this tab, then the last thing
1066 // searched for on any tab.
1067 if (!previous_find_text_.empty())
1068 search_string = previous_find_text_;
1069 else if (!last_search_prepopulate_text_->empty())
1070 search_string = *last_search_prepopulate_text_;
1071 else
[email protected]e491f1c2009-05-22 20:28:121072 return;
[email protected]e491f1c2009-05-22 20:28:121073 }
[email protected]6e95706d2009-04-23 22:55:011074
[email protected]101c90e2009-10-26 21:14:441075 // Keep track of the previous search.
1076 previous_find_text_ = find_text_;
1077
[email protected]6e95706d2009-04-23 22:55:011078 // This is a FindNext operation if we are searching for the same text again,
1079 // or if the passed in search text is empty (FindNext keyboard shortcut). The
1080 // exception to this is if the Find was aborted (then we don't want FindNext
1081 // because the highlighting has been cleared and we need it to reappear). We
1082 // therefore treat FindNext after an aborted Find operation as a full fledged
1083 // Find.
[email protected]c1dc9f132009-11-20 23:13:211084 bool find_next = (find_text_ == search_string || search_string.empty()) &&
[email protected]e491f1c2009-05-22 20:28:121085 (last_search_case_sensitive_ == case_sensitive) &&
[email protected]6e95706d2009-04-23 22:55:011086 !find_op_aborted_;
1087 if (!find_next)
1088 current_find_request_id_ = find_request_id_counter_++;
1089
[email protected]c1dc9f132009-11-20 23:13:211090 if (!search_string.empty())
1091 find_text_ = search_string;
[email protected]e491f1c2009-05-22 20:28:121092 last_search_case_sensitive_ = case_sensitive;
[email protected]6e95706d2009-04-23 22:55:011093
1094 find_op_aborted_ = false;
1095
1096 // Keep track of what the last search was across the tabs.
[email protected]c1dc9f132009-11-20 23:13:211097 *last_search_prepopulate_text_ = find_text_;
[email protected]6e95706d2009-04-23 22:55:011098 render_view_host()->StartFinding(current_find_request_id_,
1099 find_text_,
1100 forward_direction,
[email protected]e491f1c2009-05-22 20:28:121101 case_sensitive,
[email protected]6e95706d2009-04-23 22:55:011102 find_next);
1103}
1104
1105void TabContents::StopFinding(bool clear_selection) {
[email protected]2d2dffc2009-06-09 21:41:001106 // When |clear_selection| is true, it means the find string has been cleared
1107 // by the user, but the UI has not been dismissed.
1108 if (!clear_selection)
1109 find_ui_active_ = false;
[email protected]87fae512009-12-11 02:20:211110 previous_find_text_ = find_text_;
[email protected]101c90e2009-10-26 21:14:441111 find_text_.clear();
[email protected]6e95706d2009-04-23 22:55:011112 find_op_aborted_ = true;
[email protected]e491f1c2009-05-22 20:28:121113 last_search_result_ = FindNotificationDetails();
[email protected]6e95706d2009-04-23 22:55:011114 render_view_host()->StopFinding(clear_selection);
1115}
1116
[email protected]420ae012009-04-24 05:16:321117void TabContents::OnSavePage() {
1118 // If we can not save the page, try to download it.
1119 if (!SavePackage::IsSavableContents(contents_mime_type())) {
1120 DownloadManager* dlm = profile()->GetDownloadManager();
1121 const GURL& current_page_url = GetURL();
1122 if (dlm && current_page_url.is_valid())
[email protected]d82ed61e2009-06-16 02:46:221123 dlm->DownloadUrl(current_page_url, GURL(), "", this);
[email protected]420ae012009-04-24 05:16:321124 return;
1125 }
1126
1127 Stop();
1128
1129 // Create the save package and possibly prompt the user for the name to save
1130 // the page as. The user prompt is an asynchronous operation that runs on
1131 // another thread.
[email protected]d82ed61e2009-06-16 02:46:221132 save_package_ = new SavePackage(this);
[email protected]420ae012009-04-24 05:16:321133 save_package_->GetSaveInfo();
1134}
1135
1136// Used in automated testing to bypass prompting the user for file names.
1137// Instead, the names and paths are hard coded rather than running them through
1138// file name sanitation and extension / mime checking.
[email protected]828cabe2009-09-26 22:47:111139bool TabContents::SavePage(const FilePath& main_file, const FilePath& dir_path,
[email protected]420ae012009-04-24 05:16:321140 SavePackage::SavePackageType save_type) {
1141 // Stop the page from navigating.
1142 Stop();
1143
[email protected]828cabe2009-09-26 22:47:111144 save_package_ = new SavePackage(this, save_type, main_file, dir_path);
[email protected]cdf6cc82009-08-04 21:48:591145 return save_package_->Init();
[email protected]420ae012009-04-24 05:16:321146}
1147
[email protected]89d6e6e2009-12-03 23:06:511148void TabContents::EmailPageLocation() {
1149 std::string title = EscapeQueryParamValue(UTF16ToUTF8(GetTitle()), false);
1150 std::string page_url = EscapeQueryParamValue(GetURL().spec(), false);
1151 std::string mailto = std::string("mailto:?subject=Fwd:%20") +
1152 title + "&body=%0A%0A" + page_url;
1153 platform_util::OpenExternal(GURL(mailto));
1154}
1155
[email protected]420ae012009-04-24 05:16:321156void TabContents::PrintPreview() {
1157 // We don't show the print preview yet, only the print dialog.
1158 PrintNow();
1159}
1160
1161bool TabContents::PrintNow() {
1162 // We can't print interstitial page for now.
1163 if (showing_interstitial_page())
1164 return false;
1165
1166 return render_view_host()->PrintPages();
1167}
1168
[email protected]82270452009-06-19 15:58:011169void TabContents::PrintingDone(int document_cookie, bool success) {
1170 render_view_host()->PrintingDone(document_cookie, success);
1171}
1172
[email protected]420ae012009-04-24 05:16:321173bool TabContents::IsActiveEntry(int32 page_id) {
1174 NavigationEntry* active_entry = controller_.GetActiveEntry();
1175 return (active_entry != NULL &&
1176 active_entry->site_instance() == GetSiteInstance() &&
1177 active_entry->page_id() == page_id);
1178}
1179
[email protected]3f8eb7f2009-10-02 23:58:051180void TabContents::LogNewTabTime(const std::string& event_name) {
1181 // Not all new tab pages get timed. In those cases, we don't have a
1182 // new_tab_start_time_.
1183 if (new_tab_start_time_.is_null())
1184 return;
1185
1186 base::TimeDelta duration = base::TimeTicks::Now() - new_tab_start_time_;
[email protected]84abba62009-10-07 17:01:441187 MetricEventDurationDetails details(event_name,
1188 static_cast<int>(duration.InMilliseconds()));
1189
[email protected]7ab1e7d62009-10-14 23:32:011190 if (event_name == "Tab.NewTabScriptStart") {
1191 UMA_HISTOGRAM_TIMES("Tab.NewTabScriptStart", duration);
1192 } else if (event_name == "Tab.NewTabDOMContentLoaded") {
1193 UMA_HISTOGRAM_TIMES("Tab.NewTabDOMContentLoaded", duration);
1194 } else if (event_name == "Tab.NewTabOnload") {
1195 UMA_HISTOGRAM_TIMES("Tab.NewTabOnload", duration);
[email protected]3f8eb7f2009-10-02 23:58:051196 // The new tab page has finished loading; reset it.
1197 new_tab_start_time_ = base::TimeTicks();
[email protected]84abba62009-10-07 17:01:441198 } else {
1199 NOTREACHED();
[email protected]3f8eb7f2009-10-02 23:58:051200 }
[email protected]84abba62009-10-07 17:01:441201 NotificationService::current()->Notify(
1202 NotificationType::METRIC_EVENT_DURATION,
1203 Source<TabContents>(this),
1204 Details<MetricEventDurationDetails>(&details));
[email protected]3f8eb7f2009-10-02 23:58:051205}
1206
[email protected]7ab1e7d62009-10-14 23:32:011207void TabContents::OnCloseStarted() {
1208 if (tab_close_start_time_.is_null())
1209 tab_close_start_time_ = base::TimeTicks::Now();
1210}
1211
[email protected]c37e3b62010-01-22 00:15:171212TabContents* TabContents::CloneAndMakePhantom() {
1213 // TODO: the initial URL, title and what not should come from the app.
1214 NavigationEntry* entry = controller().GetActiveEntry();
1215
1216 TabNavigation tab_nav;
1217 if (entry)
1218 tab_nav.SetFromNavigationEntry(*entry);
1219 std::vector<TabNavigation> navigations;
1220 navigations.push_back(tab_nav);
1221
1222 TabContents* new_contents =
1223 new TabContents(profile(), NULL, MSG_ROUTING_NONE, NULL);
1224 new_contents->controller().RestoreFromState(navigations, 0, false);
1225 if (entry) {
1226 // TODO: this should come from the app.
1227 new_contents->controller().GetActiveEntry()->favicon() = entry->favicon();
1228 }
1229
1230 return new_contents;
1231}
1232
[email protected]96d185d2009-04-24 03:28:541233// Notifies the RenderWidgetHost instance about the fact that the page is
1234// loading, or done loading and calls the base implementation.
1235void TabContents::SetIsLoading(bool is_loading,
1236 LoadNotificationDetails* details) {
1237 if (is_loading == is_loading_)
1238 return;
1239
1240 if (!is_loading) {
1241 load_state_ = net::LOAD_STATE_IDLE;
1242 load_state_host_.clear();
[email protected]094e5b22009-09-25 04:23:561243 upload_size_ = 0;
1244 upload_position_ = 0;
[email protected]96d185d2009-04-24 03:28:541245 }
1246
1247 render_manager_.SetIsLoading(is_loading);
1248
1249 is_loading_ = is_loading;
1250 waiting_for_response_ = is_loading;
1251
1252 if (delegate_)
1253 delegate_->LoadingStateChanged(this);
[email protected]329581b2009-04-28 06:52:351254 NotifyNavigationStateChanged(INVALIDATE_LOAD);
[email protected]96d185d2009-04-24 03:28:541255
1256 NotificationType type = is_loading ? NotificationType::LOAD_START :
1257 NotificationType::LOAD_STOP;
[email protected]ce5c4502009-05-06 16:46:111258 NotificationDetails det = NotificationService::NoDetails();
[email protected]96d185d2009-04-24 03:28:541259 if (details)
1260 det = Details<LoadNotificationDetails>(details);
1261 NotificationService::current()->Notify(type,
1262 Source<NavigationController>(&controller_),
1263 det);
1264}
1265
[email protected]2c4410d2009-05-06 23:46:221266void TabContents::CreateBlockedPopupContainerIfNecessary() {
1267 if (blocked_popups_)
1268 return;
1269
[email protected]4d4c0b92009-06-08 23:13:011270 blocked_popups_ = BlockedPopupContainer::Create(this, profile());
[email protected]2c4410d2009-05-06 23:46:221271}
1272
[email protected]b6c874582009-05-08 19:38:311273void TabContents::AddPopup(TabContents* new_contents,
[email protected]7e7414ae2010-01-26 20:19:291274 const gfx::Rect& initial_pos) {
[email protected]2c4410d2009-05-06 23:46:221275 CreateBlockedPopupContainerIfNecessary();
[email protected]7e7414ae2010-01-26 20:19:291276 // A popup is associated with the toplevel site instead of a potential frame
1277 // that spawns it.
1278 const GURL& url = GetURL();
1279 blocked_popups_->AddTabContents(
1280 new_contents, initial_pos,
1281 url.is_valid() ? url.host() : std::string());
[email protected]2c4410d2009-05-06 23:46:221282}
1283
[email protected]e69bce3dd2009-04-20 22:05:121284// TODO(brettw) This should be on the TabContentsView.
[email protected]66ba4932009-06-04 19:22:131285void TabContents::RepositionSupressedPopupsToFit() {
[email protected]d6598c052008-11-05 19:03:251286 if (blocked_popups_)
[email protected]4d4c0b92009-06-08 23:13:011287 blocked_popups_->RepositionBlockedPopupContainer();
[email protected]d5f942ba2008-09-26 19:30:341288}
1289
[email protected]b9681312008-11-07 00:08:261290bool TabContents::ShowingBlockedPopupNotification() const {
1291 return blocked_popups_ != NULL &&
[email protected]2c4410d2009-05-06 23:46:221292 blocked_popups_->GetBlockedPopupCount() != 0;
[email protected]b9681312008-11-07 00:08:261293}
[email protected]616ed5a2008-11-21 22:27:241294
1295namespace {
1296bool TransitionIsReload(PageTransition::Type transition) {
1297 return PageTransition::StripQualifier(transition) == PageTransition::RELOAD;
1298}
1299}
1300
1301void TabContents::ExpireInfoBars(
1302 const NavigationController::LoadCommittedDetails& details) {
1303 // Only hide InfoBars when the user has done something that makes the main
1304 // frame load. We don't want various automatic or subframe navigations making
1305 // it disappear.
1306 if (!details.is_user_initiated_main_frame_load())
1307 return;
1308
[email protected]f86a07022008-11-25 01:06:051309 for (int i = infobar_delegate_count() - 1; i >= 0; --i) {
[email protected]616ed5a2008-11-21 22:27:241310 InfoBarDelegate* delegate = GetInfoBarDelegateAt(i);
[email protected]f86a07022008-11-25 01:06:051311 if (delegate->ShouldExpire(details))
[email protected]616ed5a2008-11-21 22:27:241312 RemoveInfoBar(delegate);
[email protected]616ed5a2008-11-21 22:27:241313 }
1314}
[email protected]fdd61c62009-04-22 19:22:571315
[email protected]96d185d2009-04-24 03:28:541316DOMUI* TabContents::GetDOMUIForCurrentState() {
1317 // When there is a pending navigation entry, we want to use the pending DOMUI
1318 // that goes along with it to control the basic flags. For example, we want to
1319 // show the pending URL in the URL bar, so we want the display_url flag to
1320 // be from the pending entry.
1321 //
1322 // The confusion comes because there are multiple possibilities for the
1323 // initial load in a tab as a side effect of the way the RenderViewHostManager
1324 // works.
1325 //
1326 // - For the very first tab the load looks "normal". The new tab DOM UI is
1327 // the pending one, and we want it to apply here.
1328 //
1329 // - For subsequent new tabs, they'll get a new SiteInstance which will then
1330 // get switched to the one previously associated with the new tab pages.
1331 // This switching will cause the manager to commit the RVH/DOMUI. So we'll
1332 // have a committed DOM UI in this case.
1333 //
1334 // This condition handles all of these cases:
1335 //
1336 // - First load in first tab: no committed nav entry + pending nav entry +
1337 // pending dom ui:
1338 // -> Use pending DOM UI if any.
1339 //
1340 // - First load in second tab: no committed nav entry + pending nav entry +
1341 // no pending DOM UI:
1342 // -> Use the committed DOM UI if any.
1343 //
1344 // - Second navigation in any tab: committed nav entry + pending nav entry:
1345 // -> Use pending DOM UI if any.
1346 //
1347 // - Normal state with no load: committed nav entry + no pending nav entry:
1348 // -> Use committed DOM UI.
1349 if (controller_.pending_entry() &&
1350 (controller_.GetLastCommittedEntry() ||
1351 render_manager_.pending_dom_ui()))
1352 return render_manager_.pending_dom_ui();
1353 return render_manager_.dom_ui();
1354}
[email protected]420ae012009-04-24 05:16:321355
1356void TabContents::DidNavigateMainFramePostCommit(
1357 const NavigationController::LoadCommittedDetails& details,
1358 const ViewHostMsg_FrameNavigate_Params& params) {
[email protected]c2e74fe82009-09-03 17:57:441359 if (opener_dom_ui_type_ != DOMUIFactory::kNoDOMUI) {
1360 // If this is a window.open navigation, use the same DOMUI as the renderer
1361 // that opened the window, as long as both renderers have the same
1362 // privileges.
1363 if (opener_dom_ui_type_ == DOMUIFactory::GetDOMUIType(GetURL())) {
1364 DOMUI* dom_ui = DOMUIFactory::CreateDOMUIForURL(this, GetURL());
1365 // dom_ui might be NULL if the URL refers to a non-existent extension.
1366 if (dom_ui) {
1367 render_manager_.SetDOMUIPostCommit(dom_ui);
1368 dom_ui->RenderViewCreated(render_view_host());
1369 }
1370 }
1371 opener_dom_ui_type_ = DOMUIFactory::kNoDOMUI;
1372 }
1373
[email protected]420ae012009-04-24 05:16:321374 if (details.is_user_initiated_main_frame_load()) {
1375 // Clear the status bubble. This is a workaround for a bug where WebKit
1376 // doesn't let us know that the cursor left an element during a
1377 // transition (this is also why the mouse cursor remains as a hand after
1378 // clicking on a link); see bugs 1184641 and 980803. We don't want to
1379 // clear the bubble when a user navigates to a named anchor in the same
1380 // page.
1381 UpdateTargetURL(details.entry->page_id(), GURL());
1382
1383 // UpdateHelpersForDidNavigate will handle the case where the password_form
1384 // origin is valid.
1385 // TODO(brettw) bug 1343111: Password manager stuff in here needs to be
1386 // cleaned up and covered by tests.
1387 if (!params.password_form.origin.is_valid())
1388 GetPasswordManager()->DidNavigate();
1389 }
1390
1391 // The keyword generator uses the navigation entries, so must be called after
1392 // the commit.
1393 GenerateKeywordIfNecessary(params);
1394
1395 // Allow the new page to set the title again.
1396 received_page_title_ = false;
1397
1398 // Get the favicon, either from history or request it from the net.
1399 fav_icon_helper_.FetchFavIcon(details.entry->url());
1400
[email protected]56ce6e52009-10-27 00:10:521401 // Clear all page and browser action state for this tab, unless this is an
1402 // in-page navigation.
[email protected]92c6f9b92009-10-24 04:35:081403 url_canon::Replacements<char> replacements;
1404 replacements.ClearRef();
1405 if (params.url.ReplaceComponents(replacements) !=
1406 params.referrer.ReplaceComponents(replacements)) {
[email protected]92c6f9b92009-10-24 04:35:081407 ExtensionsService* service = profile()->GetExtensionsService();
1408 if (service) {
1409 for (size_t i = 0; i < service->extensions()->size(); ++i) {
[email protected]5d246db22009-10-27 06:17:571410 ExtensionAction* browser_action =
[email protected]92c6f9b92009-10-24 04:35:081411 service->extensions()->at(i)->browser_action();
[email protected]56ce6e52009-10-27 00:10:521412 if (browser_action) {
1413 browser_action->ClearAllValuesForTab(controller().session_id().id());
1414 NotificationService::current()->Notify(
1415 NotificationType::EXTENSION_BROWSER_ACTION_UPDATED,
[email protected]5d246db22009-10-27 06:17:571416 Source<ExtensionAction>(browser_action),
[email protected]56ce6e52009-10-27 00:10:521417 NotificationService::NoDetails());
1418 }
[email protected]92c6f9b92009-10-24 04:35:081419
[email protected]5d246db22009-10-27 06:17:571420 ExtensionAction* page_action =
[email protected]56ce6e52009-10-27 00:10:521421 service->extensions()->at(i)->page_action();
1422 if (page_action) {
1423 page_action->ClearAllValuesForTab(controller().session_id().id());
1424 PageActionStateChanged();
1425 }
[email protected]92c6f9b92009-10-24 04:35:081426 }
[email protected]aece2c7f2009-08-27 20:43:171427 }
1428 }
[email protected]ce5c4502009-05-06 16:46:111429
[email protected]420ae012009-04-24 05:16:321430 // Close constrained popups if necessary.
1431 MaybeCloseChildWindows(details.previous_url, details.entry->url());
1432
1433 // Update the starred state.
1434 UpdateStarredStateForCurrentURL();
1435}
1436
1437void TabContents::DidNavigateAnyFramePostCommit(
1438 RenderViewHost* render_view_host,
1439 const NavigationController::LoadCommittedDetails& details,
1440 const ViewHostMsg_FrameNavigate_Params& params) {
1441 // If we navigate, start showing messages again. This does nothing to prevent
1442 // a malicious script from spamming messages, since the script could just
1443 // reload the page to stop blocking.
1444 suppress_javascript_messages_ = false;
1445
[email protected]420ae012009-04-24 05:16:321446 // Notify the password manager of the navigation or form submit.
1447 // TODO(brettw) bug 1343111: Password manager stuff in here needs to be
1448 // cleaned up and covered by tests.
1449 if (params.password_form.origin.is_valid())
1450 GetPasswordManager()->ProvisionallySavePassword(params.password_form);
1451}
1452
1453void TabContents::MaybeCloseChildWindows(const GURL& previous_url,
1454 const GURL& current_url) {
1455 if (net::RegistryControlledDomainService::SameDomainOrHost(
1456 previous_url, current_url))
1457 return;
1458
1459 // Clear out any child windows since we are leaving this page entirely.
1460 // We use indices instead of iterators in case CloseWindow does something
1461 // that may invalidate an iterator.
1462 int size = static_cast<int>(child_windows_.size());
1463 for (int i = size - 1; i >= 0; --i) {
1464 ConstrainedWindow* window = child_windows_[i];
[email protected]fa1cf0b82010-01-15 21:49:441465 if (window) {
1466 DCHECK(delegate_);
[email protected]420ae012009-04-24 05:16:321467 window->CloseConstrainedWindow();
[email protected]fa1cf0b82010-01-15 21:49:441468 BlockTabContent(false);
1469 }
[email protected]420ae012009-04-24 05:16:321470 }
[email protected]e29300cd2009-07-23 21:14:591471
1472 // Close the popup container.
1473 if (blocked_popups_) {
1474 blocked_popups_->Destroy();
1475 blocked_popups_ = NULL;
1476 }
[email protected]420ae012009-04-24 05:16:321477}
1478
1479void TabContents::UpdateStarredStateForCurrentURL() {
1480 BookmarkModel* model = profile()->GetBookmarkModel();
1481 const bool old_state = is_starred_;
1482 is_starred_ = (model && model->IsBookmarked(GetURL()));
1483
1484 if (is_starred_ != old_state && delegate())
1485 delegate()->URLStarredChanged(this, is_starred_);
1486}
1487
1488void TabContents::UpdateAlternateErrorPageURL() {
1489 GURL url = GetAlternateErrorPageURL();
1490 render_view_host()->SetAlternateErrorPageURL(url);
1491}
1492
1493void TabContents::UpdateWebPreferences() {
1494 render_view_host()->UpdateWebPreferences(GetWebkitPrefs());
1495}
1496
1497void TabContents::UpdateMaxPageIDIfNecessary(SiteInstance* site_instance,
1498 RenderViewHost* rvh) {
1499 // If we are creating a RVH for a restored controller, then we might
1500 // have more page IDs than the SiteInstance's current max page ID. We must
1501 // make sure that the max page ID is larger than any restored page ID.
1502 // Note that it is ok for conflicting page IDs to exist in another tab
1503 // (i.e., NavigationController), but if any page ID is larger than the max,
1504 // the back/forward list will get confused.
1505 int max_restored_page_id = controller_.max_restored_page_id();
1506 if (max_restored_page_id > 0) {
1507 int curr_max_page_id = site_instance->max_page_id();
1508 if (max_restored_page_id > curr_max_page_id) {
1509 // Need to update the site instance immediately.
1510 site_instance->UpdateMaxPageID(max_restored_page_id);
1511
1512 // Also tell the renderer to update its internal representation. We
1513 // need to reserve enough IDs to make all restored page IDs less than
1514 // the max.
1515 if (curr_max_page_id < 0)
1516 curr_max_page_id = 0;
1517 rvh->ReservePageIDRange(max_restored_page_id - curr_max_page_id);
1518 }
1519 }
1520}
1521
1522void TabContents::UpdateHistoryForNavigation(
[email protected]ebe89e062009-08-13 23:16:541523 const GURL& virtual_url,
[email protected]befd8d822009-07-01 04:51:471524 const NavigationController::LoadCommittedDetails& details,
[email protected]420ae012009-04-24 05:16:321525 const ViewHostMsg_FrameNavigate_Params& params) {
1526 if (profile()->IsOffTheRecord())
1527 return;
1528
1529 // Add to history service.
1530 HistoryService* hs = profile()->GetHistoryService(Profile::IMPLICIT_ACCESS);
1531 if (hs) {
1532 if (PageTransition::IsMainFrame(params.transition) &&
[email protected]ebe89e062009-08-13 23:16:541533 virtual_url != params.url) {
1534 // Hack on the "virtual" URL so that it will appear in history. For some
[email protected]420ae012009-04-24 05:16:321535 // types of URLs, we will display a magic URL that is different from where
1536 // the page is actually navigated. We want the user to see in history
[email protected]ebe89e062009-08-13 23:16:541537 // what they saw in the URL bar, so we add the virtual URL as a redirect.
1538 // This only applies to the main frame, as the virtual URL doesn't apply
[email protected]420ae012009-04-24 05:16:321539 // to sub-frames.
1540 std::vector<GURL> redirects = params.redirects;
1541 if (!redirects.empty())
[email protected]ebe89e062009-08-13 23:16:541542 redirects.back() = virtual_url;
1543 hs->AddPage(virtual_url, this, params.page_id, params.referrer,
[email protected]befd8d822009-07-01 04:51:471544 params.transition, redirects, details.did_replace_entry);
[email protected]420ae012009-04-24 05:16:321545 } else {
1546 hs->AddPage(params.url, this, params.page_id, params.referrer,
[email protected]befd8d822009-07-01 04:51:471547 params.transition, params.redirects,
1548 details.did_replace_entry);
[email protected]420ae012009-04-24 05:16:321549 }
1550 }
1551}
1552
1553bool TabContents::UpdateTitleForEntry(NavigationEntry* entry,
1554 const std::wstring& title) {
1555 // For file URLs without a title, use the pathname instead. In the case of a
1556 // synthesized title, we don't want the update to count toward the "one set
1557 // per page of the title to history."
1558 std::wstring final_title;
1559 bool explicit_set;
1560 if (entry->url().SchemeIsFile() && title.empty()) {
1561 final_title = UTF8ToWide(entry->url().ExtractFileName());
1562 explicit_set = false; // Don't count synthetic titles toward the set limit.
1563 } else {
1564 TrimWhitespace(title, TRIM_ALL, &final_title);
1565 explicit_set = true;
1566 }
1567
1568 if (final_title == UTF16ToWideHack(entry->title()))
1569 return false; // Nothing changed, don't bother.
1570
1571 entry->set_title(WideToUTF16Hack(final_title));
1572
1573 // Update the history system for this page.
1574 if (!profile()->IsOffTheRecord() && !received_page_title_) {
1575 HistoryService* hs =
1576 profile()->GetHistoryService(Profile::IMPLICIT_ACCESS);
1577 if (hs)
[email protected]ebe89e062009-08-13 23:16:541578 hs->SetPageTitle(entry->virtual_url(), final_title);
[email protected]420ae012009-04-24 05:16:321579
1580 // Don't allow the title to be saved again for explicitly set ones.
1581 received_page_title_ = explicit_set;
1582 }
1583
1584 // Lastly, set the title for the view.
1585 view_->SetPageTitle(final_title);
1586
1587 return true;
1588}
1589
1590void TabContents::NotifySwapped() {
1591 // After sending out a swap notification, we need to send a disconnect
1592 // notification so that clients that pick up a pointer to |this| can NULL the
1593 // pointer. See Bug 1230284.
1594 notify_disconnection_ = true;
1595 NotificationService::current()->Notify(
[email protected]57c6a652009-05-04 07:58:341596 NotificationType::TAB_CONTENTS_SWAPPED,
[email protected]d82ed61e2009-06-16 02:46:221597 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:321598 NotificationService::NoDetails());
1599}
1600
1601void TabContents::NotifyConnected() {
1602 notify_disconnection_ = true;
1603 NotificationService::current()->Notify(
[email protected]57c6a652009-05-04 07:58:341604 NotificationType::TAB_CONTENTS_CONNECTED,
[email protected]d82ed61e2009-06-16 02:46:221605 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:321606 NotificationService::NoDetails());
1607}
1608
1609void TabContents::NotifyDisconnected() {
1610 if (!notify_disconnection_)
1611 return;
1612
1613 notify_disconnection_ = false;
1614 NotificationService::current()->Notify(
[email protected]57c6a652009-05-04 07:58:341615 NotificationType::TAB_CONTENTS_DISCONNECTED,
[email protected]d82ed61e2009-06-16 02:46:221616 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:321617 NotificationService::NoDetails());
1618}
1619
1620void TabContents::GenerateKeywordIfNecessary(
1621 const ViewHostMsg_FrameNavigate_Params& params) {
1622 if (!params.searchable_form_url.is_valid())
1623 return;
1624
1625 if (profile()->IsOffTheRecord())
1626 return;
1627
1628 int last_index = controller_.last_committed_entry_index();
1629 // When there was no previous page, the last index will be 0. This is
1630 // normally due to a form submit that opened in a new tab.
1631 // TODO(brettw) bug 916126: we should support keywords when form submits
1632 // happen in new tabs.
1633 if (last_index <= 0)
1634 return;
1635 const NavigationEntry* previous_entry =
1636 controller_.GetEntryAtIndex(last_index - 1);
1637 if (IsFormSubmit(previous_entry)) {
1638 // Only generate a keyword if the previous page wasn't itself a form
1639 // submit.
1640 return;
1641 }
1642
1643 GURL keyword_url = previous_entry->user_typed_url().is_valid() ?
1644 previous_entry->user_typed_url() : previous_entry->url();
1645 std::wstring keyword =
1646 TemplateURLModel::GenerateKeyword(keyword_url, true); // autodetected
1647 if (keyword.empty())
1648 return;
1649
1650 TemplateURLModel* url_model = profile()->GetTemplateURLModel();
1651 if (!url_model)
1652 return;
1653
1654 if (!url_model->loaded()) {
1655 url_model->Load();
1656 return;
1657 }
1658
1659 const TemplateURL* current_url;
1660 std::wstring url = UTF8ToWide(params.searchable_form_url.spec());
1661 if (!url_model->CanReplaceKeyword(keyword, url, &current_url))
1662 return;
1663
1664 if (current_url) {
1665 if (current_url->originating_url().is_valid()) {
1666 // The existing keyword was generated from an OpenSearch description
1667 // document, don't regenerate.
1668 return;
1669 }
1670 url_model->Remove(current_url);
1671 }
1672 TemplateURL* new_url = new TemplateURL();
1673 new_url->set_keyword(keyword);
1674 new_url->set_short_name(keyword);
1675 new_url->SetURL(url, 0, 0);
1676 new_url->add_input_encoding(params.searchable_form_encoding);
1677 DCHECK(controller_.GetLastCommittedEntry());
1678 const GURL& favicon_url =
1679 controller_.GetLastCommittedEntry()->favicon().url();
1680 if (favicon_url.is_valid()) {
1681 new_url->SetFavIconURL(favicon_url);
1682 } else {
1683 // The favicon url isn't valid. This means there really isn't a favicon,
1684 // or the favicon url wasn't obtained before the load started. This assumes
1685 // the later.
1686 // TODO(sky): Need a way to set the favicon that doesn't involve generating
1687 // its url.
1688 new_url->SetFavIconURL(TemplateURL::GenerateFaviconURL(params.referrer));
1689 }
1690 new_url->set_safe_for_autoreplace(true);
1691 url_model->Add(new_url);
1692}
1693
[email protected]bf5c2ff392009-07-08 16:24:331694void TabContents::OnUserGesture() {
1695 // See comment in RenderViewHostDelegate::OnUserGesture as to why we do this.
1696 DownloadRequestManager* drm = g_browser_process->download_request_manager();
1697 if (drm)
1698 drm->OnUserGesture(this);
[email protected]e7eaedde2009-09-25 20:09:491699 ExternalProtocolHandler::OnUserGesture();
[email protected]bf5c2ff392009-07-08 16:24:331700 controller_.OnUserGesture();
1701}
1702
1703void TabContents::OnFindReply(int request_id,
1704 int number_of_matches,
1705 const gfx::Rect& selection_rect,
1706 int active_match_ordinal,
1707 bool final_update) {
[email protected]3ce7ced2009-07-24 18:52:421708 // Ignore responses for requests that have been aborted.
1709 if (find_op_aborted_)
1710 return;
1711
[email protected]bf5c2ff392009-07-08 16:24:331712 // Ignore responses for requests other than the one we have most recently
1713 // issued. That way we won't act on stale results when the user has
1714 // already typed in another query.
1715 if (request_id != current_find_request_id_)
1716 return;
1717
1718 if (number_of_matches == -1)
1719 number_of_matches = last_search_result_.number_of_matches();
1720 if (active_match_ordinal == -1)
1721 active_match_ordinal = last_search_result_.active_match_ordinal();
1722
1723 gfx::Rect selection = selection_rect;
1724 if (selection.IsEmpty())
1725 selection = last_search_result_.selection_rect();
1726
1727 // Notify the UI, automation and any other observers that a find result was
1728 // found.
1729 last_search_result_ = FindNotificationDetails(request_id, number_of_matches,
1730 selection, active_match_ordinal,
1731 final_update);
1732 NotificationService::current()->Notify(
1733 NotificationType::FIND_RESULT_AVAILABLE,
1734 Source<TabContents>(this),
1735 Details<FindNotificationDetails>(&last_search_result_));
1736}
1737
1738void TabContents::GoToEntryAtOffset(int offset) {
[email protected]f9cc4c452009-10-13 14:56:381739 if (!delegate_ || delegate_->OnGoToEntryOffset(offset))
1740 controller_.GoToOffset(offset);
[email protected]bf5c2ff392009-07-08 16:24:331741}
1742
1743void TabContents::GetHistoryListCount(int* back_list_count,
1744 int* forward_list_count) {
1745 int current_index = controller_.last_committed_entry_index();
1746 *back_list_count = current_index;
1747 *forward_list_count = controller_.entry_count() - current_index - 1;
1748}
1749
1750void TabContents::OnMissingPluginStatus(int status) {
1751#if defined(OS_WIN)
1752// TODO(PORT): pull in when plug-ins work
1753 GetPluginInstaller()->OnMissingPluginStatus(status);
1754#endif
1755}
1756
1757void TabContents::OnCrashedPlugin(const FilePath& plugin_path) {
[email protected]bf5c2ff392009-07-08 16:24:331758 DCHECK(!plugin_path.value().empty());
1759
1760 std::wstring plugin_name = plugin_path.ToWStringHack();
[email protected]d32d46182009-07-23 00:09:351761#if defined(OS_WIN) || defined(OS_MACOSX)
[email protected]bf5c2ff392009-07-08 16:24:331762 scoped_ptr<FileVersionInfo> version_info(
1763 FileVersionInfo::CreateFileVersionInfo(plugin_path));
1764 if (version_info.get()) {
1765 const std::wstring& product_name = version_info->product_name();
[email protected]996fd702009-09-04 19:12:371766 if (!product_name.empty()) {
[email protected]bf5c2ff392009-07-08 16:24:331767 plugin_name = product_name;
[email protected]996fd702009-09-04 19:12:371768#if defined(OS_MACOSX)
1769 // Many plugins on the Mac have .plugin in the actual name, which looks
1770 // terrible, so look for that and strip it off if present.
1771 const std::wstring plugin_extension(L".plugin");
1772 if (EndsWith(plugin_name, plugin_extension, true))
1773 plugin_name.erase(plugin_name.length() - plugin_extension.length());
1774#endif // OS_MACOSX
1775 }
[email protected]bf5c2ff392009-07-08 16:24:331776 }
[email protected]4a560ed2009-07-13 23:35:261777#else
1778 NOTIMPLEMENTED() << " convert plugin path to plugin name";
1779#endif
[email protected]bf5c2ff392009-07-08 16:24:331780 AddInfoBar(new SimpleAlertInfoBarDelegate(
1781 this, l10n_util::GetStringF(IDS_PLUGIN_CRASHED_PROMPT, plugin_name),
1782 NULL));
[email protected]bf5c2ff392009-07-08 16:24:331783}
1784
1785void TabContents::OnCrashedWorker() {
1786 AddInfoBar(new SimpleAlertInfoBarDelegate(
1787 this, l10n_util::GetString(IDS_WEBWORKER_CRASHED_PROMPT),
1788 NULL));
1789}
1790
1791void TabContents::OnDidGetApplicationInfo(
1792 int32 page_id,
1793 const webkit_glue::WebApplicationInfo& info) {
[email protected]ed5431872009-11-17 08:39:511794 web_app_info_ = info;
[email protected]bf5c2ff392009-07-08 16:24:331795
[email protected]ed5431872009-11-17 08:39:511796 if (delegate())
1797 delegate()->OnDidGetApplicationInfo(this, page_id);
[email protected]bf5c2ff392009-07-08 16:24:331798}
1799
[email protected]a2cf65eb2010-01-18 08:20:381800void TabContents::OnPageContents(const GURL& url,
1801 int renderer_process_id,
1802 int32 page_id,
[email protected]a8a81292010-01-21 00:32:451803 const std::wstring& contents,
1804 const std::string& language) {
[email protected]a2cf65eb2010-01-18 08:20:381805 // Don't index any https pages. People generally don't want their bank
1806 // accounts, etc. indexed on their computer, especially since some of these
1807 // things are not marked cachable.
1808 // TODO(brettw) we may want to consider more elaborate heuristics such as
1809 // the cachability of the page. We may also want to consider subframes (this
1810 // test will still index subframes if the subframe is SSL).
1811 if (!url.SchemeIsSecure()) {
1812 Profile* p = profile();
1813 if (p && !p->IsOffTheRecord()) {
1814 HistoryService* hs = p->GetHistoryService(Profile::IMPLICIT_ACCESS);
1815 if (hs)
1816 hs->SetPageContents(url, contents);
1817 }
1818 }
[email protected]a8a81292010-01-21 00:32:451819
1820 NavigationEntry* entry = controller_.GetActiveEntry();
1821 if (process()->id() == renderer_process_id &&
1822 entry && entry->page_id() == page_id) {
1823 entry->set_language(language);
1824 }
1825
1826 std::string lang = language;
1827 NotificationService::current()->Notify(
1828 NotificationType::TAB_LANGUAGE_DETERMINED,
[email protected]7e0831b2010-02-01 08:45:151829 Source<TabContents>(this),
[email protected]a8a81292010-01-21 00:32:451830 Details<std::string>(&lang));
[email protected]a2cf65eb2010-01-18 08:20:381831}
1832
[email protected]7e0831b2010-02-01 08:45:151833void TabContents::OnPageTranslated(int32 page_id,
1834 const std::string& original_lang,
1835 const std::string& translated_lang) {
1836 std::pair<std::string, std::string> lang_pair =
1837 std::make_pair(original_lang, translated_lang);
1838 NotificationService::current()->Notify(
1839 NotificationType::PAGE_TRANSLATED,
1840 Source<TabContents>(this),
1841 Details<std::pair<std::string, std::string> >(&lang_pair));
1842}
1843
[email protected]bf5c2ff392009-07-08 16:24:331844void TabContents::DidStartProvisionalLoadForFrame(
1845 RenderViewHost* render_view_host,
1846 bool is_main_frame,
1847 const GURL& url) {
1848 ProvisionalLoadDetails details(is_main_frame,
1849 controller_.IsURLInPageNavigation(url),
1850 url, std::string(), false);
1851 NotificationService::current()->Notify(
1852 NotificationType::FRAME_PROVISIONAL_LOAD_START,
1853 Source<NavigationController>(&controller_),
1854 Details<ProvisionalLoadDetails>(&details));
1855}
1856
1857void TabContents::DidStartReceivingResourceResponse(
1858 ResourceRequestDetails* details) {
1859 NotificationService::current()->Notify(
1860 NotificationType::RESOURCE_RESPONSE_STARTED,
1861 Source<NavigationController>(&controller()),
1862 Details<ResourceRequestDetails>(details));
1863}
1864
1865void TabContents::DidRedirectResource(ResourceRequestDetails* details) {
1866 NotificationService::current()->Notify(
1867 NotificationType::RESOURCE_RECEIVED_REDIRECT,
1868 Source<NavigationController>(&controller()),
1869 Details<ResourceRequestDetails>(details));
1870}
1871
1872void TabContents::DidLoadResourceFromMemoryCache(
1873 const GURL& url,
1874 const std::string& frame_origin,
1875 const std::string& main_frame_origin,
1876 const std::string& security_info) {
1877 // Send out a notification that we loaded a resource from our memory cache.
1878 int cert_id = 0, cert_status = 0, security_bits = 0;
1879 SSLManager::DeserializeSecurityInfo(security_info,
1880 &cert_id, &cert_status,
1881 &security_bits);
1882 LoadFromMemoryCacheDetails details(url, frame_origin, main_frame_origin,
[email protected]76543b92009-08-31 17:27:451883 process()->id(), cert_id, cert_status);
[email protected]bf5c2ff392009-07-08 16:24:331884
1885 NotificationService::current()->Notify(
1886 NotificationType::LOAD_FROM_MEMORY_CACHE,
1887 Source<NavigationController>(&controller_),
1888 Details<LoadFromMemoryCacheDetails>(&details));
1889}
1890
[email protected]e3d60e5d2009-09-25 21:08:291891void TabContents::DidDisplayInsecureContent() {
[email protected]09843b5f2009-09-26 08:30:591892 controller_.ssl_manager()->DidDisplayInsecureContent();
[email protected]e3d60e5d2009-09-25 21:08:291893}
1894
1895void TabContents::DidRunInsecureContent(const std::string& security_origin) {
[email protected]09843b5f2009-09-26 08:30:591896 controller_.ssl_manager()->DidRunInsecureContent(security_origin);
[email protected]e3d60e5d2009-09-25 21:08:291897}
1898
[email protected]bf5c2ff392009-07-08 16:24:331899void TabContents::DidFailProvisionalLoadWithError(
1900 RenderViewHost* render_view_host,
1901 bool is_main_frame,
1902 int error_code,
1903 const GURL& url,
1904 bool showing_repost_interstitial) {
1905 if (net::ERR_ABORTED == error_code) {
1906 // EVIL HACK ALERT! Ignore failed loads when we're showing interstitials.
1907 // This means that the interstitial won't be torn down properly, which is
1908 // bad. But if we have an interstitial, go back to another tab type, and
1909 // then load the same interstitial again, we could end up getting the first
1910 // interstitial's "failed" message (as a result of the cancel) when we're on
1911 // the second one.
1912 //
1913 // We can't tell this apart, so we think we're tearing down the current page
1914 // which will cause a crash later one. There is also some code in
1915 // RenderViewHostManager::RendererAbortedProvisionalLoad that is commented
1916 // out because of this problem.
1917 //
1918 // http://code.google.com/p/chromium/issues/detail?id=2855
1919 // Because this will not tear down the interstitial properly, if "back" is
1920 // back to another tab type, the interstitial will still be somewhat alive
1921 // in the previous tab type. If you navigate somewhere that activates the
1922 // tab with the interstitial again, you'll see a flash before the new load
1923 // commits of the interstitial page.
1924 if (showing_interstitial_page()) {
1925 LOG(WARNING) << "Discarding message during interstitial.";
1926 return;
1927 }
1928
1929 // This will discard our pending entry if we cancelled the load (e.g., if we
1930 // decided to download the file instead of load it). Only discard the
1931 // pending entry if the URLs match, otherwise the user initiated a navigate
1932 // before the page loaded so that the discard would discard the wrong entry.
1933 NavigationEntry* pending_entry = controller_.pending_entry();
1934 if (pending_entry && pending_entry->url() == url) {
1935 controller_.DiscardNonCommittedEntries();
1936 // Update the URL display.
1937 NotifyNavigationStateChanged(TabContents::INVALIDATE_URL);
1938 }
1939
1940 render_manager_.RendererAbortedProvisionalLoad(render_view_host);
1941 }
1942
1943 // Send out a notification that we failed a provisional load with an error.
1944 ProvisionalLoadDetails details(is_main_frame,
1945 controller_.IsURLInPageNavigation(url),
1946 url, std::string(), false);
1947 details.set_error_code(error_code);
1948
1949 NotificationService::current()->Notify(
1950 NotificationType::FAIL_PROVISIONAL_LOAD_WITH_ERROR,
1951 Source<NavigationController>(&controller_),
1952 Details<ProvisionalLoadDetails>(&details));
1953}
1954
1955void TabContents::DocumentLoadedInFrame() {
1956 controller_.DocumentLoadedInFrame();
1957}
1958
[email protected]8d3347f2009-07-09 22:00:211959RenderViewHostDelegate::View* TabContents::GetViewDelegate() {
[email protected]420ae012009-04-24 05:16:321960 return view_.get();
1961}
1962
[email protected]8d3347f2009-07-09 22:00:211963RenderViewHostDelegate::RendererManagement*
1964TabContents::GetRendererManagementDelegate() {
1965 return &render_manager_;
1966}
1967
[email protected]bf5c2ff392009-07-08 16:24:331968RenderViewHostDelegate::BrowserIntegration*
[email protected]8d3347f2009-07-09 22:00:211969 TabContents::GetBrowserIntegrationDelegate() {
1970 return this;
[email protected]bf5c2ff392009-07-08 16:24:331971}
1972
[email protected]8d3347f2009-07-09 22:00:211973RenderViewHostDelegate::Resource* TabContents::GetResourceDelegate() {
1974 return this;
[email protected]bf5c2ff392009-07-08 16:24:331975}
1976
[email protected]8d3347f2009-07-09 22:00:211977RenderViewHostDelegate::Save* TabContents::GetSaveDelegate() {
[email protected]420ae012009-04-24 05:16:321978 return save_package_.get(); // May be NULL, but we can return NULL.
1979}
1980
[email protected]8d3347f2009-07-09 22:00:211981RenderViewHostDelegate::Printing* TabContents::GetPrintingDelegate() {
1982 return &printing_;
1983}
1984
1985RenderViewHostDelegate::FavIcon* TabContents::GetFavIconDelegate() {
1986 return &fav_icon_helper_;
1987}
1988
[email protected]c54d269e2009-10-21 18:07:381989RenderViewHostDelegate::FormFieldHistory*
1990TabContents::GetFormFieldHistoryDelegate() {
[email protected]19289d02009-10-21 21:47:001991 if (form_field_history_manager_.get() == NULL)
1992 form_field_history_manager_.reset(new FormFieldHistoryManager(this));
1993 return form_field_history_manager_.get();
[email protected]bf5c2ff392009-07-08 16:24:331994}
1995
[email protected]21674df2009-10-28 19:51:211996RenderViewHostDelegate::AutoFill* TabContents::GetAutoFillDelegate() {
1997 if (autofill_manager_.get() == NULL)
1998 autofill_manager_.reset(new AutoFillManager(this));
1999 return autofill_manager_.get();
2000}
2001
[email protected]93623c5d2009-12-10 21:40:322002RendererPreferences TabContents::GetRendererPrefs(Profile* profile) const {
[email protected]840b1512009-07-21 16:53:462003 return renderer_preferences_;
[email protected]80d96fa2009-06-10 22:34:512004}
2005
[email protected]57c6a652009-05-04 07:58:342006TabContents* TabContents::GetAsTabContents() {
2007 return this;
2008}
2009
[email protected]3e7cf1362009-08-21 17:47:082010void TabContents::AddBlockedNotice(const GURL& url, const string16& reason) {
2011 CreateBlockedPopupContainerIfNecessary();
2012 blocked_popups_->AddBlockedNotice(url, reason);
2013}
2014
[email protected]7b291f92009-08-14 05:43:532015ViewType::Type TabContents::GetRenderViewType() const {
2016 return ViewType::TAB_CONTENTS;
2017}
2018
2019int TabContents::GetBrowserWindowID() const {
2020 return controller().window_id().id();
2021}
2022
[email protected]420ae012009-04-24 05:16:322023void TabContents::RenderViewCreated(RenderViewHost* render_view_host) {
[email protected]2e4633c2009-07-09 16:58:062024 NotificationService::current()->Notify(
2025 NotificationType::RENDER_VIEW_HOST_CREATED_FOR_TAB,
2026 Source<TabContents>(this),
2027 Details<RenderViewHost>(render_view_host));
[email protected]420ae012009-04-24 05:16:322028 NavigationEntry* entry = controller_.GetActiveEntry();
2029 if (!entry)
2030 return;
2031
2032 // When we're creating views, we're still doing initial setup, so we always
2033 // use the pending DOM UI rather than any possibly existing committed one.
2034 if (render_manager_.pending_dom_ui()) {
2035 render_manager_.pending_dom_ui()->RenderViewCreated(render_view_host);
2036 }
2037
2038 if (entry->IsViewSourceMode()) {
2039 // Put the renderer in view source mode.
2040 render_view_host->Send(
2041 new ViewMsg_EnableViewSourceMode(render_view_host->routing_id()));
2042 }
[email protected]0666aef2009-05-13 19:48:082043
2044 view()->RenderViewCreated(render_view_host);
[email protected]420ae012009-04-24 05:16:322045}
2046
2047void TabContents::RenderViewReady(RenderViewHost* rvh) {
2048 if (rvh != render_view_host()) {
2049 // Don't notify the world, since this came from a renderer in the
2050 // background.
2051 return;
2052 }
2053
2054 NotifyConnected();
[email protected]9d8a4642009-07-29 17:25:302055 bool was_crashed = is_crashed();
[email protected]420ae012009-04-24 05:16:322056 SetIsCrashed(false);
[email protected]9d8a4642009-07-29 17:25:302057
2058 // Restore the focus to the tab (otherwise the focus will be on the top
2059 // window).
2060 if (was_crashed && !FocusLocationBarByDefault())
2061 Focus();
[email protected]420ae012009-04-24 05:16:322062}
2063
2064void TabContents::RenderViewGone(RenderViewHost* rvh) {
2065 // Ask the print preview if this renderer was valuable.
2066 if (!printing_.OnRenderViewGone(rvh))
2067 return;
2068 if (rvh != render_view_host()) {
2069 // The pending page's RenderViewHost is gone.
2070 return;
2071 }
2072
2073 SetIsLoading(false, NULL);
2074 NotifyDisconnected();
2075 SetIsCrashed(true);
2076
[email protected]8e537302009-06-10 21:19:582077 // Tell the view that we've crashed so it can prepare the sad tab page.
[email protected]cf68b092010-01-15 20:05:552078 // Only do this if we're not in browser shutdown, so that TabContents
2079 // objects that are not in a browser (e.g., HTML dialogs) and thus are
2080 // visible do not flash a sad tab page.
2081 if (browser_shutdown::GetShutdownType() == browser_shutdown::NOT_VALID)
2082 view_->OnTabCrashed();
[email protected]420ae012009-04-24 05:16:322083
2084 // Hide any visible hung renderer warning for this web contents' process.
[email protected]e7b8d832009-11-20 22:41:002085 hung_renderer_dialog::HideForTabContents(this);
[email protected]420ae012009-04-24 05:16:322086}
2087
[email protected]2e4633c2009-07-09 16:58:062088void TabContents::RenderViewDeleted(RenderViewHost* rvh) {
2089 NotificationService::current()->Notify(
2090 NotificationType::RENDER_VIEW_HOST_DELETED,
2091 Source<TabContents>(this),
2092 Details<RenderViewHost>(rvh));
[email protected]fb7b79f72009-11-06 18:00:482093 render_manager_.RenderViewDeleted(rvh);
[email protected]2e4633c2009-07-09 16:58:062094}
2095
[email protected]420ae012009-04-24 05:16:322096void TabContents::DidNavigate(RenderViewHost* rvh,
2097 const ViewHostMsg_FrameNavigate_Params& params) {
[email protected]8030f012009-09-25 18:09:372098 int extra_invalidate_flags = 0;
2099
2100 if (PageTransition::IsMainFrame(params.transition)) {
[email protected]20ede072009-10-06 00:28:352101 bool was_bookmark_bar_visible = ShouldShowBookmarkBar();
[email protected]8030f012009-09-25 18:09:372102 bool was_extension_shelf_visible = IsExtensionShelfAlwaysVisible();
2103
[email protected]420ae012009-04-24 05:16:322104 render_manager_.DidNavigateMainFrame(rvh);
2105
[email protected]20ede072009-10-06 00:28:352106 if (was_bookmark_bar_visible != ShouldShowBookmarkBar())
[email protected]8030f012009-09-25 18:09:372107 extra_invalidate_flags |= INVALIDATE_BOOKMARK_BAR;
2108 if (was_extension_shelf_visible != IsExtensionShelfAlwaysVisible())
2109 extra_invalidate_flags |= INVALIDATE_EXTENSION_SHELF;
2110 }
2111
[email protected]420ae012009-04-24 05:16:322112 // Update the site of the SiteInstance if it doesn't have one yet.
2113 if (!GetSiteInstance()->has_site())
2114 GetSiteInstance()->SetSite(params.url);
2115
2116 // Need to update MIME type here because it's referred to in
2117 // UpdateNavigationCommands() called by RendererDidNavigate() to
2118 // determine whether or not to enable the encoding menu.
2119 // It's updated only for the main frame. For a subframe,
2120 // RenderView::UpdateURL does not set params.contents_mime_type.
2121 // (see http://code.google.com/p/chromium/issues/detail?id=2929 )
2122 // TODO(jungshik): Add a test for the encoding menu to avoid
2123 // regressing it again.
2124 if (PageTransition::IsMainFrame(params.transition))
2125 contents_mime_type_ = params.contents_mime_type;
2126
2127 NavigationController::LoadCommittedDetails details;
[email protected]8030f012009-09-25 18:09:372128 bool did_navigate = controller_.RendererDidNavigate(
2129 params, extra_invalidate_flags, &details);
[email protected]3e90d4a2009-07-03 17:38:392130
2131 // Update history. Note that this needs to happen after the entry is complete,
2132 // which WillNavigate[Main,Sub]Frame will do before this function is called.
[email protected]2e29d922009-10-22 22:13:352133 if (params.should_update_history &&
2134 (!delegate() || delegate()->ShouldAddNavigationsToHistory())) {
[email protected]3e90d4a2009-07-03 17:38:392135 // Most of the time, the displayURL matches the loaded URL, but for about:
2136 // URLs, we use a data: URL as the real value. We actually want to save
2137 // the about: URL to the history db and keep the data: URL hidden. This is
2138 // what the TabContents' URL getter does.
2139 UpdateHistoryForNavigation(GetURL(), details, params);
2140 }
2141
[email protected]76543b92009-08-31 17:27:452142 if (!did_navigate)
2143 return; // No navigation happened.
[email protected]420ae012009-04-24 05:16:322144
2145 // DO NOT ADD MORE STUFF TO THIS FUNCTION! Your component should either listen
2146 // for the appropriate notification (best) or you can add it to
2147 // DidNavigateMainFramePostCommit / DidNavigateAnyFramePostCommit (only if
2148 // necessary, please).
2149
2150 // Run post-commit tasks.
2151 if (details.is_main_frame)
2152 DidNavigateMainFramePostCommit(details, params);
2153 DidNavigateAnyFramePostCommit(rvh, details, params);
2154}
2155
2156void TabContents::UpdateState(RenderViewHost* rvh,
2157 int32 page_id,
2158 const std::string& state) {
2159 DCHECK(rvh == render_view_host());
2160
2161 // We must be prepared to handle state updates for any page, these occur
2162 // when the user is scrolling and entering form data, as well as when we're
2163 // leaving a page, in which case our state may have already been moved to
2164 // the next page. The navigation controller will look up the appropriate
2165 // NavigationEntry and update it when it is notified via the delegate.
2166
2167 int entry_index = controller_.GetEntryIndexWithPageID(
2168 GetSiteInstance(), page_id);
2169 if (entry_index < 0)
2170 return;
2171 NavigationEntry* entry = controller_.GetEntryAtIndex(entry_index);
2172
2173 if (state == entry->content_state())
2174 return; // Nothing to update.
2175 entry->set_content_state(state);
2176 controller_.NotifyEntryChanged(entry, entry_index);
2177}
2178
2179void TabContents::UpdateTitle(RenderViewHost* rvh,
2180 int32 page_id, const std::wstring& title) {
2181 // If we have a title, that's a pretty good indication that we've started
2182 // getting useful data.
2183 SetNotWaitingForResponse();
2184
2185 DCHECK(rvh == render_view_host());
2186 NavigationEntry* entry = controller_.GetEntryWithPageID(GetSiteInstance(),
2187 page_id);
2188 if (!entry || !UpdateTitleForEntry(entry, title))
2189 return;
2190
2191 // Broadcast notifications when the UI should be updated.
2192 if (entry == controller_.GetEntryAtOffset(0))
[email protected]f1cd5e82009-10-23 17:19:032193 NotifyNavigationStateChanged(INVALIDATE_TITLE);
[email protected]420ae012009-04-24 05:16:322194}
2195
[email protected]420ae012009-04-24 05:16:322196void TabContents::UpdateEncoding(RenderViewHost* render_view_host,
[email protected]41fc0322009-09-04 22:23:402197 const std::string& encoding) {
[email protected]420ae012009-04-24 05:16:322198 set_encoding(encoding);
2199}
2200
2201void TabContents::UpdateTargetURL(int32 page_id, const GURL& url) {
2202 if (delegate())
2203 delegate()->UpdateTargetURL(this, url);
2204}
2205
2206void TabContents::UpdateThumbnail(const GURL& url,
2207 const SkBitmap& bitmap,
2208 const ThumbnailScore& score) {
2209 // Tell History about this thumbnail
[email protected]449478302009-06-09 20:04:282210 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kThumbnailStore)) {
[email protected]72d0fc72009-07-09 17:30:492211 if (!profile()->IsOffTheRecord())
[email protected]42339402009-07-17 17:27:192212 profile()->GetThumbnailStore()->SetPageThumbnail(url, bitmap,
2213 score, true);
[email protected]449478302009-06-09 20:04:282214 } else {
2215 HistoryService* hs;
2216 if (!profile()->IsOffTheRecord() &&
2217 (hs = profile()->GetHistoryService(Profile::IMPLICIT_ACCESS))) {
2218 hs->SetPageThumbnail(url, bitmap, score);
2219 }
[email protected]420ae012009-04-24 05:16:322220 }
2221}
2222
[email protected]8922e1f2009-10-03 05:01:262223void TabContents::UpdateInspectorSettings(const std::string& raw_settings) {
[email protected]0df30122009-06-03 12:13:082224 profile()->GetPrefs()->SetString(prefs::kWebKitInspectorSettings,
[email protected]8922e1f2009-10-03 05:01:262225 UTF8ToWide(raw_settings));
[email protected]0df30122009-06-03 12:13:082226}
2227
[email protected]420ae012009-04-24 05:16:322228void TabContents::Close(RenderViewHost* rvh) {
[email protected]07707302009-11-06 00:50:292229 // The UI may be in an event-tracking loop, such as between the
2230 // mouse-down and mouse-up in text selection or a button click.
2231 // Defer the close until after tracking is complete, so that we
2232 // don't free objects out from under the UI.
2233 // TODO(shess): This could probably be integrated with the
2234 // IsDoingDrag() test below. Punting for now because I need more
2235 // research to understand how this impacts platforms other than Mac.
2236 // TODO(shess): This could get more fine-grained. For instance,
2237 // closing a tab in another window while selecting text in the
2238 // current window's Omnibox should be just fine.
2239 if (view()->IsEventTracking()) {
2240 view()->CloseTabAfterEventTracking();
2241 return;
2242 }
2243
[email protected]24a4d1062009-07-10 23:10:422244 // If we close the tab while we're in the middle of a drag, we'll crash.
2245 // Instead, cancel the drag and close it as soon as the drag ends.
2246 if (view()->IsDoingDrag()) {
2247 view()->CancelDragAndCloseTab();
2248 return;
2249 }
2250
[email protected]420ae012009-04-24 05:16:322251 // Ignore this if it comes from a RenderViewHost that we aren't showing.
2252 if (delegate() && rvh == render_view_host())
2253 delegate()->CloseContents(this);
2254}
2255
2256void TabContents::RequestMove(const gfx::Rect& new_bounds) {
2257 if (delegate() && delegate()->IsPopup(this))
2258 delegate()->MoveContents(this, new_bounds);
2259}
2260
[email protected]7ab1e7d62009-10-14 23:32:012261void TabContents::DidStartLoading() {
[email protected]420ae012009-04-24 05:16:322262 SetIsLoading(true, NULL);
2263}
2264
[email protected]7ab1e7d62009-10-14 23:32:012265void TabContents::DidStopLoading() {
[email protected]420ae012009-04-24 05:16:322266 scoped_ptr<LoadNotificationDetails> details;
2267
2268 NavigationEntry* entry = controller_.GetActiveEntry();
2269 // An entry may not exist for a stop when loading an initial blank page or
2270 // if an iframe injected by script into a blank page finishes loading.
2271 if (entry) {
[email protected]420ae012009-04-24 05:16:322272 base::TimeDelta elapsed = base::TimeTicks::Now() - current_load_start_;
2273
2274 details.reset(new LoadNotificationDetails(
[email protected]ebe89e062009-08-13 23:16:542275 entry->virtual_url(),
[email protected]420ae012009-04-24 05:16:322276 entry->transition_type(),
2277 elapsed,
2278 &controller_,
2279 controller_.GetCurrentEntryIndex()));
2280 }
2281
2282 // Tell PasswordManager we've finished a page load, which serves as a
2283 // green light to save pending passwords and reset itself.
2284 GetPasswordManager()->DidStopLoading();
2285
2286 SetIsLoading(false, details.get());
2287}
2288
[email protected]420ae012009-04-24 05:16:322289void TabContents::DidRedirectProvisionalLoad(int32 page_id,
2290 const GURL& source_url,
2291 const GURL& target_url) {
2292 NavigationEntry* entry;
2293 if (page_id == -1)
2294 entry = controller_.pending_entry();
2295 else
2296 entry = controller_.GetEntryWithPageID(GetSiteInstance(), page_id);
2297 if (!entry || entry->url() != source_url)
2298 return;
2299 entry->set_url(target_url);
2300}
2301
[email protected]420ae012009-04-24 05:16:322302void TabContents::RequestOpenURL(const GURL& url, const GURL& referrer,
2303 WindowOpenDisposition disposition) {
2304 if (render_manager_.dom_ui()) {
2305 // When we're a DOM UI, it will provide a page transition type for us (this
2306 // is so the new tab page can specify AUTO_BOOKMARK for automatically
2307 // generated suggestions).
2308 //
2309 // Note also that we hide the referrer for DOM UI pages. We don't really
[email protected]60e448982009-05-06 04:21:162310 // want web sites to see a referrer of "chrome://blah" (and some
2311 // chrome: URLs might have search terms or other stuff we don't want to
[email protected]420ae012009-04-24 05:16:322312 // send to the site), so we send no referrer.
2313 OpenURL(url, GURL(), disposition,
2314 render_manager_.dom_ui()->link_transition_type());
2315 } else {
2316 OpenURL(url, referrer, disposition, PageTransition::LINK);
2317 }
2318}
2319
[email protected]1ee614862009-07-06 20:29:182320void TabContents::DomOperationResponse(const std::string& json_string,
2321 int automation_id) {
2322}
2323
[email protected]420ae012009-04-24 05:16:322324void TabContents::ProcessDOMUIMessage(const std::string& message,
[email protected]e4dad9fb2009-10-06 18:15:582325 const Value* content,
[email protected]811bfe32009-07-01 08:46:252326 int request_id,
2327 bool has_callback) {
[email protected]420ae012009-04-24 05:16:322328 if (!render_manager_.dom_ui()) {
[email protected]c2e74fe82009-09-03 17:57:442329 // This can happen if someone uses window.open() to open an extension URL
2330 // from a non-extension context.
2331 render_view_host()->BlockExtensionRequest(request_id);
[email protected]420ae012009-04-24 05:16:322332 return;
2333 }
[email protected]811bfe32009-07-01 08:46:252334 render_manager_.dom_ui()->ProcessDOMUIMessage(message, content, request_id,
2335 has_callback);
[email protected]420ae012009-04-24 05:16:322336}
2337
2338void TabContents::ProcessExternalHostMessage(const std::string& message,
2339 const std::string& origin,
2340 const std::string& target) {
2341 if (delegate())
2342 delegate()->ForwardMessageToExternalHost(message, origin, target);
2343}
2344
[email protected]420ae012009-04-24 05:16:322345void TabContents::RunFileChooser(bool multiple_files,
2346 const string16& title,
2347 const FilePath& default_file) {
2348 if (!select_file_dialog_.get())
2349 select_file_dialog_ = SelectFileDialog::Create(this);
2350 SelectFileDialog::Type dialog_type =
2351 multiple_files ? SelectFileDialog::SELECT_OPEN_MULTI_FILE :
2352 SelectFileDialog::SELECT_OPEN_FILE;
2353 select_file_dialog_->SelectFile(dialog_type, title, default_file,
2354 NULL, 0, FILE_PATH_LITERAL(""),
2355 view_->GetTopLevelNativeWindow(), NULL);
2356}
2357
2358void TabContents::RunJavaScriptMessage(
2359 const std::wstring& message,
2360 const std::wstring& default_prompt,
2361 const GURL& frame_url,
2362 const int flags,
2363 IPC::Message* reply_msg,
2364 bool* did_suppress_message) {
2365 // Suppress javascript messages when requested and when inside a constrained
2366 // popup window (because that activates them and breaks them out of the
2367 // constrained window jail).
[email protected]91ed49a2009-07-07 21:29:552368 // Also suppress messages when showing an interstitial. The interstitial is
2369 // shown over the previous page, we don't want the hidden page dialogs to
2370 // interfere with the interstitial.
2371 bool suppress_this_message = suppress_javascript_messages_ ||
2372 showing_interstitial_page();
[email protected]420ae012009-04-24 05:16:322373 if (delegate())
2374 suppress_this_message |=
[email protected]b6c874582009-05-08 19:38:312375 (delegate()->GetConstrainingContents(this) != this);
[email protected]420ae012009-04-24 05:16:322376
2377 *did_suppress_message = suppress_this_message;
2378
2379 if (!suppress_this_message) {
2380 base::TimeDelta time_since_last_message(
2381 base::TimeTicks::Now() - last_javascript_message_dismissal_);
2382 bool show_suppress_checkbox = false;
2383 // Show a checkbox offering to suppress further messages if this message is
2384 // being displayed within kJavascriptMessageExpectedDelay of the last one.
2385 if (time_since_last_message <
2386 base::TimeDelta::FromMilliseconds(kJavascriptMessageExpectedDelay))
2387 show_suppress_checkbox = true;
2388
[email protected]d82ed61e2009-06-16 02:46:222389 RunJavascriptMessageBox(this, frame_url, flags, message, default_prompt,
[email protected]420ae012009-04-24 05:16:322390 show_suppress_checkbox, reply_msg);
2391 } else {
2392 // If we are suppressing messages, just reply as is if the user immediately
2393 // pressed "Cancel".
[email protected]beb440c2009-11-06 04:08:542394 OnMessageBoxClosed(reply_msg, false, std::wstring());
[email protected]420ae012009-04-24 05:16:322395 }
2396}
2397
2398void TabContents::RunBeforeUnloadConfirm(const std::wstring& message,
2399 IPC::Message* reply_msg) {
[email protected]7ab1e7d62009-10-14 23:32:012400 is_showing_before_unload_dialog_ = true;
[email protected]d82ed61e2009-06-16 02:46:222401 RunBeforeUnloadDialog(this, message, reply_msg);
[email protected]420ae012009-04-24 05:16:322402}
2403
2404void TabContents::ShowModalHTMLDialog(const GURL& url, int width, int height,
2405 const std::string& json_arguments,
2406 IPC::Message* reply_msg) {
2407 if (delegate()) {
2408 HtmlDialogUIDelegate* dialog_delegate =
2409 new ModalHtmlDialogDelegate(url, width, height, json_arguments,
[email protected]d82ed61e2009-06-16 02:46:222410 reply_msg, this);
[email protected]420ae012009-04-24 05:16:322411 delegate()->ShowHtmlDialog(dialog_delegate, NULL);
2412 }
2413}
2414
2415void TabContents::PasswordFormsSeen(
[email protected]ed3fb032009-06-16 19:50:562416 const std::vector<webkit_glue::PasswordForm>& forms) {
[email protected]420ae012009-04-24 05:16:322417 GetPasswordManager()->PasswordFormsSeen(forms);
2418}
2419
[email protected]420ae012009-04-24 05:16:322420// Checks to see if we should generate a keyword based on the OSDD, and if
2421// necessary uses TemplateURLFetcher to download the OSDD and create a keyword.
2422void TabContents::PageHasOSDD(RenderViewHost* render_view_host,
2423 int32 page_id, const GURL& url,
2424 bool autodetected) {
2425 // Make sure page_id is the current page, and the TemplateURLModel is loaded.
2426 DCHECK(url.is_valid());
2427 if (!IsActiveEntry(page_id))
2428 return;
2429 TemplateURLModel* url_model = profile()->GetTemplateURLModel();
2430 if (!url_model)
2431 return;
2432 if (!url_model->loaded()) {
2433 url_model->Load();
2434 return;
2435 }
2436 if (!profile()->GetTemplateURLFetcher())
2437 return;
2438
2439 if (profile()->IsOffTheRecord())
2440 return;
2441
2442 const NavigationEntry* entry = controller_.GetLastCommittedEntry();
2443 DCHECK(entry);
2444
2445 const NavigationEntry* base_entry = entry;
2446 if (IsFormSubmit(base_entry)) {
2447 // If the current page is a form submit, find the last page that was not
2448 // a form submit and use its url to generate the keyword from.
2449 int index = controller_.last_committed_entry_index() - 1;
2450 while (index >= 0 && IsFormSubmit(controller_.GetEntryAtIndex(index)))
2451 index--;
2452 if (index >= 0)
2453 base_entry = controller_.GetEntryAtIndex(index);
2454 else
2455 base_entry = NULL;
2456 }
2457
2458 // We want to use the user typed URL if available since that represents what
2459 // the user typed to get here, and fall back on the regular URL if not.
2460 if (!base_entry)
2461 return;
2462 GURL keyword_url = base_entry->user_typed_url().is_valid() ?
2463 base_entry->user_typed_url() : base_entry->url();
2464 if (!keyword_url.is_valid())
2465 return;
2466 std::wstring keyword = TemplateURLModel::GenerateKeyword(keyword_url,
2467 autodetected);
[email protected]a88dbdd52010-01-15 19:25:532468
2469 // For JS added OSDD empty keyword is OK because we will generate keyword
2470 // later from OSDD content.
2471 if (autodetected && keyword.empty())
[email protected]420ae012009-04-24 05:16:322472 return;
2473 const TemplateURL* template_url =
2474 url_model->GetTemplateURLForKeyword(keyword);
2475 if (template_url && (!template_url->safe_for_autoreplace() ||
2476 template_url->originating_url() == url)) {
2477 // Either there is a user created TemplateURL for this keyword, or the
2478 // keyword has the same OSDD url and we've parsed it.
2479 return;
2480 }
2481
2482 // Download the OpenSearch description document. If this is successful a
2483 // new keyword will be created when done.
[email protected]420ae012009-04-24 05:16:322484 profile()->GetTemplateURLFetcher()->ScheduleDownload(
2485 keyword,
2486 url,
2487 base_entry->favicon().url(),
[email protected]d0cc9fb52009-06-23 02:26:162488 this,
[email protected]420ae012009-04-24 05:16:322489 autodetected);
2490}
2491
[email protected]420ae012009-04-24 05:16:322492GURL TabContents::GetAlternateErrorPageURL() const {
2493 GURL url;
2494 // Disable alternate error pages when in OffTheRecord/Incognito mode.
2495 if (profile()->IsOffTheRecord())
2496 return url;
2497
2498 PrefService* prefs = profile()->GetPrefs();
2499 DCHECK(prefs);
2500 if (prefs->GetBoolean(prefs::kAlternateErrorPagesEnabled)) {
[email protected]a33457b82009-09-21 23:07:152501 url = google_util::AppendGoogleLocaleParam(
2502 GURL(google_util::kLinkDoctorBaseURL));
[email protected]420ae012009-04-24 05:16:322503 url = google_util::AppendGoogleTLDParam(url);
2504 }
2505 return url;
2506}
2507
2508WebPreferences TabContents::GetWebkitPrefs() {
2509 PrefService* prefs = render_view_host()->process()->profile()->GetPrefs();
[email protected]7ccddb8c2009-08-04 17:36:552510 bool is_dom_ui = false;
2511 return RenderViewHostDelegateHelper::GetWebkitPrefs(prefs, is_dom_ui);
[email protected]420ae012009-04-24 05:16:322512}
2513
[email protected]fa1cf0b82010-01-15 21:49:442514void TabContents::OnIgnoredUIEvent() {
2515 if (constrained_window_count()) {
2516 ConstrainedWindow* window = *constrained_window_begin();
2517 window->FocusConstrainedWindow();
2518 }
2519}
2520
[email protected]420ae012009-04-24 05:16:322521void TabContents::OnJSOutOfMemory() {
2522 AddInfoBar(new SimpleAlertInfoBarDelegate(
2523 this, l10n_util::GetString(IDS_JS_OUT_OF_MEMORY_PROMPT), NULL));
2524}
2525
[email protected]420ae012009-04-24 05:16:322526void TabContents::OnCrossSiteResponse(int new_render_process_host_id,
2527 int new_request_id) {
[email protected]57c6a652009-05-04 07:58:342528 // Allows the TabContents to react when a cross-site response is ready to be
[email protected]420ae012009-04-24 05:16:322529 // delivered to a pending RenderViewHost. We must first run the onunload
2530 // handler of the old RenderViewHost before we can allow it to proceed.
2531 render_manager_.OnCrossSiteResponse(new_render_process_host_id,
2532 new_request_id);
2533}
2534
2535bool TabContents::CanBlur() const {
2536 return delegate() ? delegate()->CanBlur() : true;
2537}
2538
2539gfx::Rect TabContents::GetRootWindowResizerRect() const {
2540 if (delegate())
2541 return delegate()->GetRootWindowResizerRect();
2542 return gfx::Rect();
2543}
2544
2545void TabContents::RendererUnresponsive(RenderViewHost* rvh,
2546 bool is_during_unload) {
2547 if (is_during_unload) {
2548 // Hang occurred while firing the beforeunload/unload handler.
2549 // Pretend the handler fired so tab closing continues as if it had.
[email protected]829e7612009-04-25 01:15:112550 rvh->set_sudden_termination_allowed(true);
[email protected]420ae012009-04-24 05:16:322551
2552 if (!render_manager_.ShouldCloseTabOnUnresponsiveRenderer())
2553 return;
2554
2555 // If the tab hangs in the beforeunload/unload handler there's really
2556 // nothing we can do to recover. Pretend the unload listeners have
2557 // all fired and close the tab. If the hang is in the beforeunload handler
2558 // then the user will not have the option of cancelling the close.
2559 Close(rvh);
2560 return;
2561 }
2562
2563 if (render_view_host() && render_view_host()->IsRenderViewLive())
[email protected]e7b8d832009-11-20 22:41:002564 hung_renderer_dialog::ShowForTabContents(this);
[email protected]420ae012009-04-24 05:16:322565}
2566
2567void TabContents::RendererResponsive(RenderViewHost* render_view_host) {
[email protected]e7b8d832009-11-20 22:41:002568 hung_renderer_dialog::HideForTabContents(this);
[email protected]420ae012009-04-24 05:16:322569}
2570
2571void TabContents::LoadStateChanged(const GURL& url,
[email protected]094e5b22009-09-25 04:23:562572 net::LoadState load_state,
2573 uint64 upload_position,
2574 uint64 upload_size) {
[email protected]420ae012009-04-24 05:16:322575 load_state_ = load_state;
[email protected]094e5b22009-09-25 04:23:562576 upload_position_ = upload_position;
2577 upload_size_ = upload_size;
[email protected]d686e812009-06-03 19:10:292578 std::wstring languages =
2579 profile()->GetPrefs()->GetString(prefs::kAcceptLanguages);
[email protected]d686e812009-06-03 19:10:292580 std::string host = url.host();
[email protected]ce85f602009-11-07 01:34:532581 load_state_host_ =
2582 net::IDNToUnicode(host.c_str(), host.size(), languages, NULL);
[email protected]420ae012009-04-24 05:16:322583 if (load_state_ == net::LOAD_STATE_READING_RESPONSE)
2584 SetNotWaitingForResponse();
2585 if (is_loading())
[email protected]c9cd2222009-05-06 05:16:502586 NotifyNavigationStateChanged(INVALIDATE_LOAD | INVALIDATE_TAB);
[email protected]420ae012009-04-24 05:16:322587}
2588
[email protected]1ee614862009-07-06 20:29:182589bool TabContents::IsExternalTabContainer() const {
2590 if (!delegate())
2591 return false;
2592
2593 return delegate()->IsExternalTabContainer();
2594}
2595
[email protected]1ee614862009-07-06 20:29:182596void TabContents::DidInsertCSS() {
2597 // This RVHDelegate function is used for extensions and not us.
[email protected]420ae012009-04-24 05:16:322598}
2599
[email protected]130efb02009-09-18 18:54:352600void TabContents::FocusedNodeChanged() {
2601 NotificationService::current()->Notify(
2602 NotificationType::FOCUS_CHANGED_IN_PAGE,
2603 Source<RenderViewHost>(render_view_host()),
2604 NotificationService::NoDetails());
2605}
2606
[email protected]420ae012009-04-24 05:16:322607void TabContents::FileSelected(const FilePath& path,
2608 int index, void* params) {
2609 render_view_host()->FileSelected(path);
2610}
2611
2612void TabContents::MultiFilesSelected(const std::vector<FilePath>& files,
2613 void* params) {
2614 render_view_host()->MultiFilesSelected(files);
2615}
2616
2617void TabContents::FileSelectionCanceled(void* params) {
2618 // If the user cancels choosing a file to upload we pass back an
2619 // empty vector.
2620 render_view_host()->MultiFilesSelected(std::vector<FilePath>());
2621}
2622
2623void TabContents::BeforeUnloadFiredFromRenderManager(
2624 bool proceed,
2625 bool* proceed_to_fire_unload) {
2626 if (delegate())
2627 delegate()->BeforeUnloadFired(this, proceed, proceed_to_fire_unload);
2628}
2629
2630void TabContents::UpdateRenderViewSizeForRenderManager() {
2631 // TODO(brettw) this is a hack. See TabContentsView::SizeContents.
2632 view_->SizeContents(view_->GetContainerSize());
2633}
2634
2635DOMUI* TabContents::CreateDOMUIForRenderManager(const GURL& url) {
[email protected]d82ed61e2009-06-16 02:46:222636 return DOMUIFactory::CreateDOMUIForURL(this, url);
[email protected]420ae012009-04-24 05:16:322637}
2638
2639NavigationEntry*
2640TabContents::GetLastCommittedNavigationEntryForRenderManager() {
2641 return controller_.GetLastCommittedEntry();
2642}
2643
2644bool TabContents::CreateRenderViewForRenderManager(
2645 RenderViewHost* render_view_host) {
[email protected]811bfe32009-07-01 08:46:252646 // If the pending navigation is to a DOMUI, tell the RenderView about any
2647 // bindings it will need enabled.
[email protected]420ae012009-04-24 05:16:322648 if (render_manager_.pending_dom_ui())
[email protected]811bfe32009-07-01 08:46:252649 render_view_host->AllowBindings(
2650 render_manager_.pending_dom_ui()->bindings());
[email protected]1adff062009-06-02 18:39:552651
[email protected]420ae012009-04-24 05:16:322652 RenderWidgetHostView* rwh_view = view_->CreateViewForWidget(render_view_host);
[email protected]1a98a932009-11-17 00:12:522653
2654 scoped_refptr<URLRequestContextGetter> request_context = request_context_;
2655 if (!request_context.get())
2656 request_context = profile()->GetRequestContext();
2657
2658 if (!render_view_host->CreateRenderView(request_context))
[email protected]420ae012009-04-24 05:16:322659 return false;
2660
2661 // Now that the RenderView has been created, we need to tell it its size.
2662 rwh_view->SetSize(view_->GetContainerSize());
2663
2664 UpdateMaxPageIDIfNecessary(render_view_host->site_instance(),
2665 render_view_host);
2666 return true;
2667}
2668
2669void TabContents::Observe(NotificationType type,
2670 const NotificationSource& source,
2671 const NotificationDetails& details) {
2672 switch (type.value) {
2673 case NotificationType::BOOKMARK_MODEL_LOADED:
2674 // BookmarkModel finished loading, fall through to update starred state.
2675 case NotificationType::URLS_STARRED: {
2676 // Somewhere, a URL has been starred.
2677 // Ignore notifications for profiles other than our current one.
2678 Profile* source_profile = Source<Profile>(source).ptr();
2679 if (!source_profile || !source_profile->IsSameProfile(profile()))
2680 return;
2681
2682 UpdateStarredStateForCurrentURL();
2683 break;
2684 }
2685 case NotificationType::PREF_CHANGED: {
2686 std::wstring* pref_name_in = Details<std::wstring>(details).ptr();
2687 DCHECK(Source<PrefService>(source).ptr() == profile()->GetPrefs());
2688 if (*pref_name_in == prefs::kAlternateErrorPagesEnabled) {
2689 UpdateAlternateErrorPageURL();
2690 } else if (*pref_name_in == prefs::kDefaultCharset ||
2691 StartsWithASCII(WideToUTF8(*pref_name_in), "webkit.webprefs.", true)
2692 ) {
2693 UpdateWebPreferences();
2694 } else {
2695 NOTREACHED() << "unexpected pref change notification" << *pref_name_in;
2696 }
2697 break;
2698 }
2699 case NotificationType::RENDER_WIDGET_HOST_DESTROYED:
2700 view_->RenderWidgetHostDestroyed(Source<RenderWidgetHost>(source).ptr());
2701 break;
2702
2703 case NotificationType::NAV_ENTRY_COMMITTED: {
2704 DCHECK(&controller_ == Source<NavigationController>(source).ptr());
2705
2706 NavigationController::LoadCommittedDetails& committed_details =
2707 *(Details<NavigationController::LoadCommittedDetails>(details).ptr());
2708 ExpireInfoBars(committed_details);
2709 break;
2710 }
2711
[email protected]d3ba77272009-09-03 00:06:092712#if defined(OS_LINUX)
2713 case NotificationType::BROWSER_THEME_CHANGED: {
[email protected]7ff431e2010-01-07 18:18:542714 renderer_preferences_util::UpdateFromSystemSettings(
2715 &renderer_preferences_, profile());
[email protected]d3ba77272009-09-03 00:06:092716 render_view_host()->SyncRendererPrefs();
2717 break;
2718 }
2719#endif
2720
[email protected]420ae012009-04-24 05:16:322721 default:
2722 NOTREACHED();
2723 }
2724}
[email protected]aacd7cc2009-09-02 14:51:102725
[email protected]beb440c2009-11-06 04:08:542726std::wstring TabContents::GetMessageBoxTitle(const GURL& frame_url,
2727 bool is_alert) {
2728 if (!frame_url.has_host())
2729 return l10n_util::GetString(
2730 is_alert ? IDS_JAVASCRIPT_ALERT_DEFAULT_TITLE
2731 : IDS_JAVASCRIPT_MESSAGEBOX_DEFAULT_TITLE);
2732
2733 // We really only want the scheme, hostname, and port.
2734 GURL::Replacements replacements;
2735 replacements.ClearUsername();
2736 replacements.ClearPassword();
2737 replacements.ClearPath();
2738 replacements.ClearQuery();
2739 replacements.ClearRef();
2740 GURL clean_url = frame_url.ReplaceComponents(replacements);
2741
2742 // TODO(brettw) it should be easier than this to do the correct language
2743 // handling without getting the accept language from the profile.
2744 std::wstring base_address = gfx::ElideUrl(clean_url, gfx::Font(), 0,
2745 profile()->GetPrefs()->GetString(prefs::kAcceptLanguages));
2746 // Force URL to have LTR directionality.
2747 if (l10n_util::GetTextDirection() == l10n_util::RIGHT_TO_LEFT)
2748 l10n_util::WrapStringWithLTRFormatting(&base_address);
2749
2750 return l10n_util::GetStringF(
2751 is_alert ? IDS_JAVASCRIPT_ALERT_TITLE : IDS_JAVASCRIPT_MESSAGEBOX_TITLE,
2752 base_address);
2753}
2754
2755gfx::NativeWindow TabContents::GetMessageBoxRootWindow() {
2756 return view_->GetTopLevelNativeWindow();
2757}
2758
2759void TabContents::OnMessageBoxClosed(IPC::Message* reply_msg,
2760 bool success,
2761 const std::wstring& prompt) {
2762 last_javascript_message_dismissal_ = base::TimeTicks::Now();
2763 if (is_showing_before_unload_dialog_ && !success) {
2764 // If a beforeunload dialog is canceled, we need to stop the throbber from
2765 // spinning, since we forced it to start spinning in Navigate.
2766 DidStopLoading();
2767
2768 tab_close_start_time_ = base::TimeTicks();
2769 }
2770 is_showing_before_unload_dialog_ = false;
2771 render_view_host()->JavaScriptMessageBoxClosed(reply_msg, success, prompt);
2772}
2773
2774void TabContents::SetSuppressMessageBoxes(bool suppress_message_boxes) {
2775 set_suppress_javascript_messages(suppress_message_boxes);
2776}
2777
[email protected]41fc0322009-09-04 22:23:402778void TabContents::set_encoding(const std::string& encoding) {
[email protected]aacd7cc2009-09-02 14:51:102779 encoding_ = CharacterEncoding::GetCanonicalEncodingNameByAliasName(encoding);
2780}